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 > 0) &&
136 (imp->imp_next_ping - this_ping > obd_timeout * HZ)) {
137 CWARN("wrong ping time %lu (current %lu)\n",
138 imp->imp_next_ping, this_ping);
139 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
142 if (imp->imp_next_ping > this_ping && force == 0)
145 if (level == LUSTRE_IMP_DISCON && !imp->imp_deactive) {
146 /* wait at least a timeout before trying recovery again */
147 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
148 ptlrpc_initiate_recovery(imp);
149 } else if (level != LUSTRE_IMP_FULL || imp->imp_obd->obd_no_recov) {
150 CDEBUG(D_HA, "not pinging %s (in recovery "
151 " or recovery disabled: %s)\n",
152 imp->imp_target_uuid.uuid,
153 ptlrpc_import_state_name(level));
154 } else if (imp->imp_pingable || force) {
155 imp->imp_next_ping = ptlrpc_next_ping(imp);
160 static int ptlrpc_pinger_main(void *arg)
162 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
163 struct ptlrpc_thread *thread = data->thread;
170 SIGNAL_MASK_LOCK(current, flags);
171 sigfillset(¤t->blocked);
173 SIGNAL_MASK_UNLOCK(current, flags);
175 LASSERTF(strlen(data->name) < sizeof(current->comm),
176 "name %d > len %d\n",
177 (int)strlen(data->name), (int)sizeof(current->comm));
178 THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s", data->name);
181 /* Record that the thread is running */
182 thread->t_flags = SVC_RUNNING;
183 wake_up(&thread->t_ctl_waitq);
185 /* And now, loop forever, pinging as needed. */
187 unsigned long this_ping = jiffies;
188 long time_to_next_ping;
189 struct l_wait_info lwi = LWI_TIMEOUT(obd_timeout * HZ,
191 struct list_head *iter;
193 time_to_next_ping = this_ping + (obd_timeout * HZ) - jiffies;
195 list_for_each(iter, &pinger_imports) {
196 struct obd_import *imp =
197 list_entry(iter, struct obd_import,
200 ptlrpc_pinger_process_import(imp, this_ping);
202 CDEBUG(D_OTHER, "%s: pingable %d, next_ping %lu(%lu)\n",
203 imp->imp_target_uuid.uuid,
204 imp->imp_pingable, imp->imp_next_ping, jiffies);
206 if (imp->imp_pingable && imp->imp_next_ping &&
207 imp->imp_next_ping - jiffies < time_to_next_ping &&
208 imp->imp_next_ping > jiffies)
209 time_to_next_ping = imp->imp_next_ping - jiffies;
213 /* Wait until the next ping time, or until we're stopped. */
214 CDEBUG(D_HA, "next ping in %lu (%lu)\n", time_to_next_ping,
215 this_ping + (obd_timeout * HZ));
216 if (time_to_next_ping > 0) {
217 lwi = LWI_TIMEOUT(time_to_next_ping, NULL, NULL);
218 l_wait_event(thread->t_ctl_waitq,
219 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
221 if (thread->t_flags & SVC_STOPPING) {
222 thread->t_flags &= ~SVC_STOPPING;
225 } else if (thread->t_flags & SVC_EVENT) {
226 /* woken after adding import to reset timer */
227 thread->t_flags &= ~SVC_EVENT;
232 thread->t_flags = SVC_STOPPED;
233 wake_up(&thread->t_ctl_waitq);
235 CDEBUG(D_NET, "pinger thread exiting, process %d\n", current->pid);
239 static struct ptlrpc_thread *pinger_thread = NULL;
241 int ptlrpc_start_pinger(void)
243 struct l_wait_info lwi = { 0 };
244 struct ptlrpc_svc_data d;
246 #ifndef ENABLE_PINGER
251 LASSERT(obd_timeout > PINGER_RATE);
253 if (pinger_thread != NULL)
256 OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
257 if (pinger_thread == NULL)
259 init_waitqueue_head(&pinger_thread->t_ctl_waitq);
260 init_waitqueue_head(&suspend_timeouts_waitq);
263 d.thread = pinger_thread;
265 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
266 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
267 rc = kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
269 CERROR("cannot start thread: %d\n", rc);
270 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
273 l_wait_event(pinger_thread->t_ctl_waitq,
274 pinger_thread->t_flags & SVC_RUNNING, &lwi);
279 int ptlrpc_stop_pinger(void)
281 struct l_wait_info lwi = { 0 };
283 #ifndef ENABLE_PINGER
288 if (pinger_thread == NULL)
291 pinger_thread->t_flags = SVC_STOPPING;
292 wake_up(&pinger_thread->t_ctl_waitq);
295 l_wait_event(pinger_thread->t_ctl_waitq,
296 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
298 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
299 pinger_thread = NULL;
303 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
306 imp->imp_next_ping = ptlrpc_next_ping(imp);
310 int ptlrpc_pinger_add_import(struct obd_import *imp)
313 if (!list_empty(&imp->imp_pinger_chain))
317 CDEBUG(D_HA, "adding pingable import %s->%s\n",
318 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
319 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
320 /* XXX sort, blah blah */
321 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
322 class_import_get(imp);
324 ptlrpc_pinger_wake_up();
330 int ptlrpc_pinger_del_import(struct obd_import *imp)
333 if (list_empty(&imp->imp_pinger_chain))
337 list_del_init(&imp->imp_pinger_chain);
338 CDEBUG(D_HA, "removing pingable import %s->%s\n",
339 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
340 class_import_put(imp);
345 void ptlrpc_pinger_wake_up()
348 pinger_thread->t_flags |= SVC_EVENT;
349 wake_up(&pinger_thread->t_ctl_waitq);
353 #else /* !__KERNEL__ */
356 * the current implementation of pinger in liblustre is not optimized
359 static struct pinger_data {
361 unsigned long pd_this_ping;
362 unsigned long pd_next_ping;
366 static int pinger_check_rpcs(void *arg)
368 unsigned long curtime = time(NULL);
369 struct list_head *iter;
370 struct pinger_data *pd = &pinger_args;
372 /* prevent recursion */
373 if (pd->pd_recursion++) {
374 CDEBUG(D_HA, "pinger: recursion! quit\n");
379 /* have we reached ping point? */
380 if (pd->pd_next_ping > curtime && !pd->pd_force_check) {
385 if (pd->pd_force_check)
386 pd->pd_force_check = 0;
388 pd->pd_this_ping = curtime;
390 /* add rpcs into set */
392 list_for_each(iter, &pinger_imports) {
393 struct obd_import *imp =
394 list_entry(iter, struct obd_import,
400 spin_lock_irqsave(&imp->imp_lock, flags);
401 level = imp->imp_state;
402 force = imp->imp_force_verify;
404 imp->imp_force_verify = 0;
405 spin_unlock_irqrestore(&imp->imp_lock, flags);
407 if (imp->imp_next_ping <= pd->pd_this_ping || force) {
408 if (level == LUSTRE_IMP_DISCON) {
409 /* wait at least a timeout before
410 trying recovery again. */
411 unsigned long timeout = obd_timeout;
412 if (imp->imp_server_timeout)
413 timeout = obd_timeout / 2;
414 imp->imp_next_ping = time(NULL) +
416 ptlrpc_initiate_recovery(imp);
418 else if (level != LUSTRE_IMP_FULL ||
419 imp->imp_obd->obd_no_recov) {
421 "not pinging %s (in recovery "
422 " or recovery disabled: %s)\n",
423 imp->imp_target_uuid.uuid,
424 ptlrpc_import_state_name(level));
426 else if (imp->imp_pingable || force) {
431 if (imp->imp_pingable) {
432 CDEBUG(D_HA, "don't need to ping %s "
434 imp->imp_target_uuid.uuid,
435 imp->imp_next_ping, pd->pd_this_ping);
442 pd->pd_next_ping = pd->pd_this_ping + (obd_timeout * HZ);
444 CDEBUG(D_HA, "finished a round ping\n");
449 static void *pinger_callback = NULL;
451 int ptlrpc_start_pinger(void)
453 memset(&pinger_args, 0, sizeof(pinger_args));
456 liblustre_register_wait_callback(&pinger_check_rpcs, &pinger_args);
461 int ptlrpc_stop_pinger(void)
465 liblustre_deregister_wait_callback(pinger_callback);
470 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
473 imp->imp_next_ping = time(NULL) + obd_timeout;
474 if (pinger_args.pd_next_ping > imp->imp_next_ping) {
475 CDEBUG(D_HA, "set next ping to %ld(cur %ld)\n",
476 imp->imp_next_ping, time(NULL));
477 pinger_args.pd_next_ping = imp->imp_next_ping;
482 int ptlrpc_pinger_add_import(struct obd_import *imp)
485 if (!list_empty(&imp->imp_pinger_chain))
488 CDEBUG(D_HA, "adding pingable import %s->%s\n",
489 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
490 ptlrpc_pinger_sending_on_import(imp);
493 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
494 class_import_get(imp);
500 int ptlrpc_pinger_del_import(struct obd_import *imp)
503 if (list_empty(&imp->imp_pinger_chain))
507 list_del_init(&imp->imp_pinger_chain);
508 CDEBUG(D_HA, "removing pingable import %s->%s\n",
509 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
510 class_import_put(imp);
515 void ptlrpc_pinger_wake_up()
517 pinger_args.pd_force_check = 1;
519 #endif /* !__KERNEL__ */