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 (time_after_eq(imp->imp_next_ping, this_ping) && force == 0)
138 if (level == LUSTRE_IMP_DISCON && !imp->imp_deactive) {
139 /* wait at least a timeout before trying recovery again */
140 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
141 ptlrpc_initiate_recovery(imp);
142 } else if (level != LUSTRE_IMP_FULL || imp->imp_obd->obd_no_recov) {
143 CDEBUG(D_HA, "not pinging %s (in recovery "
144 " or recovery disabled: %s)\n",
145 imp->imp_target_uuid.uuid,
146 ptlrpc_import_state_name(level));
147 } else if (imp->imp_pingable || force) {
148 imp->imp_next_ping = ptlrpc_next_ping(imp);
153 static int ptlrpc_pinger_main(void *arg)
155 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
156 struct ptlrpc_thread *thread = data->thread;
163 SIGNAL_MASK_LOCK(current, flags);
164 sigfillset(¤t->blocked);
166 SIGNAL_MASK_UNLOCK(current, flags);
168 LASSERTF(strlen(data->name) < sizeof(current->comm),
169 "name %d > len %d\n",
170 (int)strlen(data->name), (int)sizeof(current->comm));
171 THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s", data->name);
174 /* Record that the thread is running */
175 thread->t_flags = SVC_RUNNING;
176 wake_up(&thread->t_ctl_waitq);
178 /* And now, loop forever, pinging as needed. */
180 unsigned long this_ping = jiffies;
181 long time_to_next_ping;
182 struct l_wait_info lwi = LWI_TIMEOUT(obd_timeout * HZ,
184 struct list_head *iter;
186 time_to_next_ping = this_ping + (obd_timeout * HZ) - jiffies;
188 list_for_each(iter, &pinger_imports) {
189 struct obd_import *imp =
190 list_entry(iter, struct obd_import,
193 ptlrpc_pinger_process_import(imp, this_ping);
195 CDEBUG(D_OTHER, "%s: pingable %d, next_ping %lu(%lu)\n",
196 imp->imp_target_uuid.uuid,
197 imp->imp_pingable, imp->imp_next_ping, jiffies);
199 if (imp->imp_pingable && imp->imp_next_ping &&
200 imp->imp_next_ping - jiffies < time_to_next_ping &&
201 imp->imp_next_ping > jiffies)
202 time_to_next_ping = imp->imp_next_ping - jiffies;
206 /* Wait until the next ping time, or until we're stopped. */
207 CDEBUG(D_HA, "next ping in %lu (%lu)\n", time_to_next_ping,
208 this_ping + (obd_timeout * HZ));
209 if (time_to_next_ping > 0) {
210 lwi = LWI_TIMEOUT(time_to_next_ping, NULL, NULL);
211 l_wait_event(thread->t_ctl_waitq,
212 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
214 if (thread->t_flags & SVC_STOPPING) {
215 thread->t_flags &= ~SVC_STOPPING;
218 } else if (thread->t_flags & SVC_EVENT) {
219 /* woken after adding import to reset timer */
220 thread->t_flags &= ~SVC_EVENT;
225 thread->t_flags = SVC_STOPPED;
226 wake_up(&thread->t_ctl_waitq);
228 CDEBUG(D_NET, "pinger thread exiting, process %d\n", current->pid);
232 static struct ptlrpc_thread *pinger_thread = NULL;
234 int ptlrpc_start_pinger(void)
236 struct l_wait_info lwi = { 0 };
237 struct ptlrpc_svc_data d;
239 #ifndef ENABLE_PINGER
244 LASSERT(obd_timeout > PINGER_RATE);
246 if (pinger_thread != NULL)
249 OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
250 if (pinger_thread == NULL)
252 init_waitqueue_head(&pinger_thread->t_ctl_waitq);
253 init_waitqueue_head(&suspend_timeouts_waitq);
256 d.thread = pinger_thread;
258 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
259 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
260 rc = kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
262 CERROR("cannot start thread: %d\n", rc);
263 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
266 l_wait_event(pinger_thread->t_ctl_waitq,
267 pinger_thread->t_flags & SVC_RUNNING, &lwi);
272 int ptlrpc_stop_pinger(void)
274 struct l_wait_info lwi = { 0 };
276 #ifndef ENABLE_PINGER
281 if (pinger_thread == NULL)
284 pinger_thread->t_flags = SVC_STOPPING;
285 wake_up(&pinger_thread->t_ctl_waitq);
288 l_wait_event(pinger_thread->t_ctl_waitq,
289 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
291 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
292 pinger_thread = NULL;
296 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
299 imp->imp_next_ping = ptlrpc_next_ping(imp);
303 int ptlrpc_pinger_add_import(struct obd_import *imp)
306 if (!list_empty(&imp->imp_pinger_chain))
310 CDEBUG(D_HA, "adding pingable import %s->%s\n",
311 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
312 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
313 /* XXX sort, blah blah */
314 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
315 class_import_get(imp);
317 ptlrpc_pinger_wake_up();
323 int ptlrpc_pinger_del_import(struct obd_import *imp)
326 if (list_empty(&imp->imp_pinger_chain))
330 list_del_init(&imp->imp_pinger_chain);
331 CDEBUG(D_HA, "removing pingable import %s->%s\n",
332 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
333 class_import_put(imp);
338 void ptlrpc_pinger_wake_up()
341 pinger_thread->t_flags |= SVC_EVENT;
342 wake_up(&pinger_thread->t_ctl_waitq);
346 #else /* !__KERNEL__ */
349 * the current implementation of pinger in liblustre is not optimized
352 static struct pinger_data {
354 unsigned long pd_this_ping;
355 unsigned long pd_next_ping;
359 static int pinger_check_rpcs(void *arg)
361 unsigned long curtime = time(NULL);
362 struct list_head *iter;
363 struct pinger_data *pd = &pinger_args;
365 /* prevent recursion */
366 if (pd->pd_recursion++) {
367 CDEBUG(D_HA, "pinger: recursion! quit\n");
372 /* have we reached ping point? */
373 if (pd->pd_next_ping > curtime && !pd->pd_force_check) {
378 if (pd->pd_force_check)
379 pd->pd_force_check = 0;
381 pd->pd_this_ping = curtime;
383 /* add rpcs into set */
385 list_for_each(iter, &pinger_imports) {
386 struct obd_import *imp =
387 list_entry(iter, struct obd_import,
393 spin_lock_irqsave(&imp->imp_lock, flags);
394 level = imp->imp_state;
395 force = imp->imp_force_verify;
397 imp->imp_force_verify = 0;
398 spin_unlock_irqrestore(&imp->imp_lock, flags);
400 if (imp->imp_next_ping <= pd->pd_this_ping || force) {
401 if (level == LUSTRE_IMP_DISCON) {
402 /* wait at least a timeout before
403 trying recovery again. */
404 unsigned long timeout = obd_timeout;
405 if (imp->imp_server_timeout)
406 timeout = obd_timeout / 2;
407 imp->imp_next_ping = time(NULL) +
409 ptlrpc_initiate_recovery(imp);
411 else if (level != LUSTRE_IMP_FULL ||
412 imp->imp_obd->obd_no_recov) {
414 "not pinging %s (in recovery "
415 " or recovery disabled: %s)\n",
416 imp->imp_target_uuid.uuid,
417 ptlrpc_import_state_name(level));
419 else if (imp->imp_pingable || force) {
424 if (imp->imp_pingable) {
425 CDEBUG(D_HA, "don't need to ping %s "
427 imp->imp_target_uuid.uuid,
428 imp->imp_next_ping, pd->pd_this_ping);
435 pd->pd_next_ping = pd->pd_this_ping + (obd_timeout * HZ);
437 CDEBUG(D_HA, "finished a round ping\n");
442 static void *pinger_callback = NULL;
444 int ptlrpc_start_pinger(void)
446 memset(&pinger_args, 0, sizeof(pinger_args));
449 liblustre_register_wait_callback(&pinger_check_rpcs, &pinger_args);
454 int ptlrpc_stop_pinger(void)
458 liblustre_deregister_wait_callback(pinger_callback);
463 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
466 imp->imp_next_ping = time(NULL) + obd_timeout;
467 if (pinger_args.pd_next_ping > imp->imp_next_ping) {
468 CDEBUG(D_HA, "set next ping to %ld(cur %ld)\n",
469 imp->imp_next_ping, time(NULL));
470 pinger_args.pd_next_ping = imp->imp_next_ping;
475 int ptlrpc_pinger_add_import(struct obd_import *imp)
478 if (!list_empty(&imp->imp_pinger_chain))
481 CDEBUG(D_HA, "adding pingable import %s->%s\n",
482 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
483 ptlrpc_pinger_sending_on_import(imp);
486 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
487 class_import_get(imp);
493 int ptlrpc_pinger_del_import(struct obd_import *imp)
496 if (list_empty(&imp->imp_pinger_chain))
500 list_del_init(&imp->imp_pinger_chain);
501 CDEBUG(D_HA, "removing pingable import %s->%s\n",
502 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
503 class_import_put(imp);
508 void ptlrpc_pinger_wake_up()
510 pinger_args.pd_force_check = 1;
512 #endif /* !__KERNEL__ */