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 static DECLARE_MUTEX(pinger_sem);
39 static struct list_head pinger_imports = LIST_HEAD_INIT(pinger_imports);
41 int ptlrpc_ping(struct obd_import *imp)
43 struct ptlrpc_request *req;
47 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING, 0, NULL, NULL);
49 DEBUG_REQ(D_HA, req, "pinging %s->%s",
50 imp->imp_obd->obd_uuid.uuid,
51 imp->imp_target_uuid.uuid);
52 req->rq_no_resend = req->rq_no_delay = 1;
53 req->rq_replen = lustre_msg_size(0, NULL);
56 CERROR("OOM trying to ping %s->%s\n",
57 imp->imp_obd->obd_uuid.uuid,
58 imp->imp_target_uuid.uuid);
66 int ptlrpc_next_ping(struct obd_import *imp)
68 if (imp->imp_server_timeout)
69 return jiffies + (obd_timeout / 4 * HZ);
71 return jiffies + (obd_timeout / 2 * HZ);
74 static int ptlrpc_pinger_main(void *arg)
76 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
77 struct ptlrpc_thread *thread = data->thread;
84 SIGNAL_MASK_LOCK(current, flags);
85 sigfillset(¤t->blocked);
87 SIGNAL_MASK_UNLOCK(current, flags);
89 LASSERTF(strlen(data->name) < sizeof(current->comm),
91 (int)strlen(data->name), (int)sizeof(current->comm));
92 THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s", data->name);
95 /* Record that the thread is running */
96 thread->t_flags = SVC_RUNNING;
97 wake_up(&thread->t_ctl_waitq);
99 /* And now, loop forever, pinging as needed. */
101 unsigned long this_ping = jiffies;
102 long time_to_next_ping;
103 struct l_wait_info lwi = LWI_TIMEOUT(obd_timeout * HZ,
105 struct list_head *iter;
107 time_to_next_ping = this_ping + (obd_timeout * HZ) - jiffies;
109 list_for_each(iter, &pinger_imports) {
110 struct obd_import *imp =
111 list_entry(iter, struct obd_import,
117 spin_lock_irqsave(&imp->imp_lock, flags);
118 level = imp->imp_state;
119 force = imp->imp_force_verify;
121 imp->imp_force_verify = 0;
122 spin_unlock_irqrestore(&imp->imp_lock, flags);
124 if (imp->imp_next_ping <= this_ping || force) {
125 if (level == LUSTRE_IMP_DISCON) {
126 /* wait at least a timeout before
127 trying recovery again. */
129 ptlrpc_next_ping(imp);
130 ptlrpc_initiate_recovery(imp);
131 } else if (level != LUSTRE_IMP_FULL ||
132 imp->imp_obd->obd_no_recov) {
134 "not pinging %s (in recovery "
135 " or recovery disabled: %s)\n",
136 imp->imp_target_uuid.uuid,
137 ptlrpc_import_state_name(level));
138 } else if (imp->imp_pingable || force) {
142 } else if (imp->imp_pingable) {
143 CDEBUG(D_HA, "don't need to ping %s "
145 imp->imp_target_uuid.uuid,
146 imp->imp_next_ping, this_ping);
148 CDEBUG(D_OTHER, "%s: pingable %d, next_ping %lu(%lu)\n",
149 imp->imp_target_uuid.uuid,
150 imp->imp_pingable, imp->imp_next_ping, jiffies);
151 if (imp->imp_pingable && imp->imp_next_ping &&
152 imp->imp_next_ping - jiffies < time_to_next_ping &&
153 imp->imp_next_ping > jiffies)
154 time_to_next_ping = imp->imp_next_ping - jiffies;
158 /* Wait until the next ping time, or until we're stopped. */
159 CDEBUG(D_HA, "next ping in %lu (%lu)\n", time_to_next_ping,
160 this_ping + (obd_timeout * HZ));
161 if (time_to_next_ping > 0) {
162 lwi = LWI_TIMEOUT(time_to_next_ping, NULL, NULL);
163 l_wait_event(thread->t_ctl_waitq,
164 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
166 if (thread->t_flags & SVC_STOPPING) {
167 thread->t_flags &= ~SVC_STOPPING;
170 } else if (thread->t_flags & SVC_EVENT) {
171 /* woken after adding import to reset timer */
172 thread->t_flags &= ~SVC_EVENT;
177 thread->t_flags = SVC_STOPPED;
178 wake_up(&thread->t_ctl_waitq);
180 CDEBUG(D_NET, "pinger thread exiting, process %d\n", current->pid);
184 static struct ptlrpc_thread *pinger_thread = NULL;
186 int ptlrpc_start_pinger(void)
188 struct l_wait_info lwi = { 0 };
189 struct ptlrpc_svc_data d;
191 #ifndef ENABLE_PINGER
196 if (pinger_thread != NULL)
199 OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
200 if (pinger_thread == NULL)
202 init_waitqueue_head(&pinger_thread->t_ctl_waitq);
205 d.thread = pinger_thread;
207 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
208 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
209 rc = kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
211 CERROR("cannot start thread: %d\n", rc);
212 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
215 l_wait_event(pinger_thread->t_ctl_waitq,
216 pinger_thread->t_flags & SVC_RUNNING, &lwi);
221 int ptlrpc_stop_pinger(void)
223 struct l_wait_info lwi = { 0 };
225 #ifndef ENABLE_PINGER
230 if (pinger_thread == NULL)
233 pinger_thread->t_flags = SVC_STOPPING;
234 wake_up(&pinger_thread->t_ctl_waitq);
237 l_wait_event(pinger_thread->t_ctl_waitq,
238 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
240 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
241 pinger_thread = NULL;
245 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
248 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
252 int ptlrpc_pinger_add_import(struct obd_import *imp)
255 if (!list_empty(&imp->imp_pinger_chain))
259 CDEBUG(D_HA, "adding pingable import %s->%s\n",
260 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
261 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
262 /* XXX sort, blah blah */
263 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
264 class_import_get(imp);
266 ptlrpc_pinger_wake_up();
272 int ptlrpc_pinger_del_import(struct obd_import *imp)
275 if (list_empty(&imp->imp_pinger_chain))
279 list_del_init(&imp->imp_pinger_chain);
280 CDEBUG(D_HA, "removing pingable import %s->%s\n",
281 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
282 class_import_put(imp);
287 void ptlrpc_pinger_wake_up()
290 pinger_thread->t_flags |= SVC_EVENT;
291 wake_up(&pinger_thread->t_ctl_waitq);
295 #else /* !__KERNEL__ */
298 * the current implementation of pinger in liblustre is not optimized
301 static struct pinger_data {
303 unsigned long pd_this_ping;
304 unsigned long pd_next_ping;
308 static int pinger_check_rpcs(void *arg)
310 unsigned long curtime = time(NULL);
311 struct list_head *iter;
312 struct pinger_data *pd = &pinger_args;
314 /* prevent recursion */
315 if (pd->pd_recursion++) {
316 CDEBUG(D_HA, "pinger: recursion! quit\n");
321 /* have we reached ping point? */
322 if (pd->pd_next_ping > curtime && !pd->pd_force_check) {
327 if (pd->pd_force_check)
328 pd->pd_force_check = 0;
330 pd->pd_this_ping = curtime;
332 /* add rpcs into set */
334 list_for_each(iter, &pinger_imports) {
335 struct obd_import *imp =
336 list_entry(iter, struct obd_import,
342 spin_lock_irqsave(&imp->imp_lock, flags);
343 level = imp->imp_state;
344 force = imp->imp_force_verify;
346 imp->imp_force_verify = 0;
347 spin_unlock_irqrestore(&imp->imp_lock, flags);
349 if (imp->imp_next_ping <= pd->pd_this_ping || force) {
350 if (level == LUSTRE_IMP_DISCON) {
351 /* wait at least a timeout before
352 trying recovery again. */
353 unsigned long timeout = obd_timeout;
354 if (imp->imp_server_timeout)
355 timeout = obd_timeout / 2;
356 imp->imp_next_ping = time(NULL) +
358 ptlrpc_initiate_recovery(imp);
360 else if (level != LUSTRE_IMP_FULL ||
361 imp->imp_obd->obd_no_recov) {
363 "not pinging %s (in recovery "
364 " or recovery disabled: %s)\n",
365 imp->imp_target_uuid.uuid,
366 ptlrpc_import_state_name(level));
368 else if (imp->imp_pingable || force) {
373 if (imp->imp_pingable) {
374 CDEBUG(D_HA, "don't need to ping %s "
376 imp->imp_target_uuid.uuid,
377 imp->imp_next_ping, pd->pd_this_ping);
384 pd->pd_next_ping = pd->pd_this_ping + (obd_timeout * HZ);
386 CDEBUG(D_HA, "finished a round ping\n");
391 static void *pinger_callback = NULL;
393 int ptlrpc_start_pinger(void)
395 memset(&pinger_args, 0, sizeof(pinger_args));
398 liblustre_register_wait_callback(&pinger_check_rpcs, &pinger_args);
403 int ptlrpc_stop_pinger(void)
407 liblustre_deregister_wait_callback(pinger_callback);
412 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
415 imp->imp_next_ping = time(NULL) + obd_timeout;
416 if (pinger_args.pd_next_ping > imp->imp_next_ping) {
417 CDEBUG(D_HA, "set next ping to %ld(cur %ld)\n",
418 imp->imp_next_ping, time(NULL));
419 pinger_args.pd_next_ping = imp->imp_next_ping;
424 int ptlrpc_pinger_add_import(struct obd_import *imp)
427 if (!list_empty(&imp->imp_pinger_chain))
430 CDEBUG(D_HA, "adding pingable import %s->%s\n",
431 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
432 ptlrpc_pinger_sending_on_import(imp);
435 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
436 class_import_get(imp);
442 int ptlrpc_pinger_del_import(struct obd_import *imp)
445 if (list_empty(&imp->imp_pinger_chain))
449 list_del_init(&imp->imp_pinger_chain);
450 CDEBUG(D_HA, "removing pingable import %s->%s\n",
451 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
452 class_import_put(imp);
457 void ptlrpc_pinger_wake_up()
459 pinger_args.pd_force_check = 1;
461 #endif /* !__KERNEL__ */