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, OBD_PING, 0, NULL,
50 DEBUG_REQ(D_HA, req, "pinging %s->%s",
51 imp->imp_obd->obd_uuid.uuid,
52 imp->imp_target_uuid.uuid);
53 req->rq_no_resend = req->rq_no_delay = 1;
54 req->rq_replen = lustre_msg_size(0,
58 CERROR("OOM trying to ping %s->%s\n",
59 imp->imp_obd->obd_uuid.uuid,
60 imp->imp_target_uuid.uuid);
68 static int ptlrpc_pinger_main(void *arg)
70 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
71 struct ptlrpc_thread *thread = data->thread;
78 SIGNAL_MASK_LOCK(current, flags);
79 sigfillset(¤t->blocked);
81 SIGNAL_MASK_UNLOCK(current, flags);
83 LASSERTF(strlen(data->name) < sizeof(current->comm),
84 "name %d > len %d\n",strlen(data->name),sizeof(current->comm));
85 THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s", data->name);
88 /* Record that the thread is running */
89 thread->t_flags = SVC_RUNNING;
90 wake_up(&thread->t_ctl_waitq);
92 /* And now, loop forever, pinging as needed. */
94 unsigned long this_ping = jiffies;
95 long time_to_next_ping;
96 struct l_wait_info lwi = LWI_TIMEOUT(obd_timeout * HZ,
98 struct list_head *iter;
101 list_for_each(iter, &pinger_imports) {
102 struct obd_import *imp =
103 list_entry(iter, struct obd_import,
109 spin_lock_irqsave(&imp->imp_lock, flags);
110 level = imp->imp_state;
111 force = imp->imp_force_verify;
113 imp->imp_force_verify = 0;
114 spin_unlock_irqrestore(&imp->imp_lock, flags);
116 if (imp->imp_next_ping <= this_ping || force) {
117 if (level == LUSTRE_IMP_DISCON) {
118 /* wait at least a timeout before
119 trying recovery again. */
120 imp->imp_next_ping = jiffies +
122 ptlrpc_initiate_recovery(imp);
124 else if (level != LUSTRE_IMP_FULL ||
125 imp->imp_obd->obd_no_recov) {
127 "not pinging %s (in recovery "
128 " or recovery disabled: %s)\n",
129 imp->imp_target_uuid.uuid,
130 ptlrpc_import_state_name(level));
132 else if (imp->imp_pingable || force) {
137 if (imp->imp_pingable)
138 CDEBUG(D_HA, "don't need to ping %s "
140 imp->imp_target_uuid.uuid,
141 imp->imp_next_ping, this_ping);
146 /* Wait until the next ping time, or until we're stopped. */
147 time_to_next_ping = this_ping + (obd_timeout * HZ) - jiffies;
148 CDEBUG(D_HA, "next ping in %lu (%lu)\n", time_to_next_ping,
149 this_ping + (obd_timeout * HZ));
150 if (time_to_next_ping > 0) {
151 lwi = LWI_TIMEOUT(time_to_next_ping, NULL, NULL);
152 l_wait_event(thread->t_ctl_waitq,
153 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
155 if (thread->t_flags & SVC_STOPPING) {
156 thread->t_flags &= ~SVC_STOPPING;
159 } else if (thread->t_flags & SVC_EVENT) {
160 /* woken after adding import to reset timer */
161 thread->t_flags &= ~SVC_EVENT;
166 thread->t_flags = SVC_STOPPED;
167 wake_up(&thread->t_ctl_waitq);
169 CDEBUG(D_NET, "pinger thread exiting, process %d\n", current->pid);
173 static struct ptlrpc_thread *pinger_thread = NULL;
175 int ptlrpc_start_pinger(void)
177 struct l_wait_info lwi = { 0 };
178 struct ptlrpc_svc_data d;
180 #ifndef ENABLE_PINGER
185 if (pinger_thread != NULL)
188 OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
189 if (pinger_thread == NULL)
191 init_waitqueue_head(&pinger_thread->t_ctl_waitq);
194 d.thread = pinger_thread;
196 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
197 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
198 rc = kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
200 CERROR("cannot start thread: %d\n", rc);
201 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
204 l_wait_event(pinger_thread->t_ctl_waitq,
205 pinger_thread->t_flags & SVC_RUNNING, &lwi);
210 int ptlrpc_stop_pinger(void)
212 struct l_wait_info lwi = { 0 };
214 #ifndef ENABLE_PINGER
219 if (pinger_thread == NULL)
222 pinger_thread->t_flags = SVC_STOPPING;
223 wake_up(&pinger_thread->t_ctl_waitq);
226 l_wait_event(pinger_thread->t_ctl_waitq,
227 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
229 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
230 pinger_thread = NULL;
234 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
237 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
241 int ptlrpc_pinger_add_import(struct obd_import *imp)
244 if (!list_empty(&imp->imp_pinger_chain))
248 CDEBUG(D_HA, "adding pingable import %s->%s\n",
249 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
250 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
251 /* XXX sort, blah blah */
252 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
253 class_import_get(imp);
255 ptlrpc_pinger_wake_up();
261 int ptlrpc_pinger_del_import(struct obd_import *imp)
264 if (list_empty(&imp->imp_pinger_chain))
268 list_del_init(&imp->imp_pinger_chain);
269 CDEBUG(D_HA, "removing pingable import %s->%s\n",
270 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
271 class_import_put(imp);
276 void ptlrpc_pinger_wake_up()
279 pinger_thread->t_flags |= SVC_EVENT;
280 wake_up(&pinger_thread->t_ctl_waitq);
284 #else /* !__KERNEL__ */
287 * the current implementation of pinger in liblustre is not optimized
290 static struct pinger_data {
292 unsigned long pd_this_ping;
293 unsigned long pd_next_ping;
297 static int pinger_check_rpcs(void *arg)
299 unsigned long curtime = time(NULL);
300 struct list_head *iter;
301 struct pinger_data *pd = &pinger_args;
303 /* prevent recursion */
304 if (pd->pd_recursion++) {
305 CDEBUG(D_HA, "pinger: recursion! quit\n");
310 /* have we reached ping point? */
311 if (pd->pd_next_ping > curtime && !pd->pd_force_check) {
316 if (pd->pd_force_check)
317 pd->pd_force_check = 0;
319 pd->pd_this_ping = curtime;
321 /* add rpcs into set */
323 list_for_each(iter, &pinger_imports) {
324 struct obd_import *imp =
325 list_entry(iter, struct obd_import,
331 spin_lock_irqsave(&imp->imp_lock, flags);
332 level = imp->imp_state;
333 force = imp->imp_force_verify;
335 imp->imp_force_verify = 0;
336 spin_unlock_irqrestore(&imp->imp_lock, flags);
338 if (imp->imp_next_ping <= pd->pd_this_ping || force) {
339 if (level == LUSTRE_IMP_DISCON) {
340 /* wait at least a timeout before
341 trying recovery again. */
342 imp->imp_next_ping = time(NULL) +
344 ptlrpc_initiate_recovery(imp);
346 else if (level != LUSTRE_IMP_FULL ||
347 imp->imp_obd->obd_no_recov) {
349 "not pinging %s (in recovery "
350 " or recovery disabled: %s)\n",
351 imp->imp_target_uuid.uuid,
352 ptlrpc_import_state_name(level));
354 else if (imp->imp_pingable || force) {
359 if (imp->imp_pingable) {
360 CDEBUG(D_HA, "don't need to ping %s "
362 imp->imp_target_uuid.uuid,
363 imp->imp_next_ping, pd->pd_this_ping);
370 pd->pd_next_ping = pd->pd_this_ping + (obd_timeout * HZ);
372 CDEBUG(D_HA, "finished a round ping\n");
377 static void *pinger_callback = NULL;
379 int ptlrpc_start_pinger(void)
381 memset(&pinger_args, 0, sizeof(pinger_args));
384 liblustre_register_wait_callback(&pinger_check_rpcs, &pinger_args);
389 int ptlrpc_stop_pinger(void)
393 liblustre_deregister_wait_callback(pinger_callback);
398 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
401 imp->imp_next_ping = time(NULL) + obd_timeout;
402 if (pinger_args.pd_next_ping > imp->imp_next_ping) {
403 CDEBUG(D_HA, "set next ping to %ld(cur %ld)\n",
404 imp->imp_next_ping, time(NULL));
405 pinger_args.pd_next_ping = imp->imp_next_ping;
410 int ptlrpc_pinger_add_import(struct obd_import *imp)
413 if (!list_empty(&imp->imp_pinger_chain))
416 CDEBUG(D_HA, "adding pingable import %s->%s\n",
417 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
418 ptlrpc_pinger_sending_on_import(imp);
421 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
422 class_import_get(imp);
428 int ptlrpc_pinger_del_import(struct obd_import *imp)
431 if (list_empty(&imp->imp_pinger_chain))
435 list_del_init(&imp->imp_pinger_chain);
436 CDEBUG(D_HA, "removing pingable import %s->%s\n",
437 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
438 class_import_put(imp);
443 void ptlrpc_pinger_wake_up()
445 pinger_args.pd_force_check = 1;
447 #endif /* !__KERNEL__ */