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 THREAD_NAME(current->comm, "%s", data->name);
86 /* Record that the thread is running */
87 thread->t_flags = SVC_RUNNING;
88 wake_up(&thread->t_ctl_waitq);
90 /* And now, loop forever, pinging as needed. */
92 unsigned long this_ping = jiffies;
93 long time_to_next_ping;
94 struct l_wait_info lwi = LWI_TIMEOUT(obd_timeout * HZ,
96 struct list_head *iter;
99 list_for_each(iter, &pinger_imports) {
100 struct obd_import *imp =
101 list_entry(iter, struct obd_import,
107 spin_lock_irqsave(&imp->imp_lock, flags);
108 level = imp->imp_state;
109 force = imp->imp_force_verify;
111 imp->imp_force_verify = 0;
112 spin_unlock_irqrestore(&imp->imp_lock, flags);
114 if (imp->imp_next_ping <= this_ping || force) {
115 if (level == LUSTRE_IMP_DISCON) {
116 /* wait at least a timeout before
117 trying recovery again. */
118 imp->imp_next_ping = jiffies +
120 ptlrpc_initiate_recovery(imp);
122 else if (level != LUSTRE_IMP_FULL ||
123 imp->imp_obd->obd_no_recov) {
125 "not pinging %s (in recovery "
126 " or recovery disabled: %s)\n",
127 imp->imp_target_uuid.uuid,
128 ptlrpc_import_state_name(level));
130 else if (imp->imp_pingable || force) {
135 if (imp->imp_pingable)
136 CDEBUG(D_HA, "don't need to ping %s "
138 imp->imp_target_uuid.uuid,
139 imp->imp_next_ping, this_ping);
144 /* Wait until the next ping time, or until we're stopped. */
145 time_to_next_ping = this_ping + (obd_timeout * HZ) - jiffies;
146 CDEBUG(D_HA, "next ping in %lu (%lu)\n", time_to_next_ping,
147 this_ping + (obd_timeout * HZ));
148 if (time_to_next_ping > 0) {
149 lwi = LWI_TIMEOUT(time_to_next_ping, NULL, NULL);
150 l_wait_event(thread->t_ctl_waitq,
151 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
153 if (thread->t_flags & SVC_STOPPING) {
154 thread->t_flags &= ~SVC_STOPPING;
157 } else if (thread->t_flags & SVC_EVENT) {
158 /* woken after adding import to reset timer */
159 thread->t_flags &= ~SVC_EVENT;
164 thread->t_flags = SVC_STOPPED;
165 wake_up(&thread->t_ctl_waitq);
167 CDEBUG(D_NET, "pinger thread exiting, process %d\n", current->pid);
171 static struct ptlrpc_thread *pinger_thread = NULL;
173 int ptlrpc_start_pinger(void)
175 struct l_wait_info lwi = { 0 };
176 struct ptlrpc_svc_data d;
178 #ifndef ENABLE_PINGER
183 if (pinger_thread != NULL)
186 OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
187 if (pinger_thread == NULL)
189 init_waitqueue_head(&pinger_thread->t_ctl_waitq);
192 d.thread = pinger_thread;
194 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
195 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
196 rc = kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
198 CERROR("cannot start thread: %d\n", rc);
199 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
202 l_wait_event(pinger_thread->t_ctl_waitq,
203 pinger_thread->t_flags & SVC_RUNNING, &lwi);
208 int ptlrpc_stop_pinger(void)
210 struct l_wait_info lwi = { 0 };
212 #ifndef ENABLE_PINGER
217 if (pinger_thread == NULL)
220 pinger_thread->t_flags = SVC_STOPPING;
221 wake_up(&pinger_thread->t_ctl_waitq);
224 l_wait_event(pinger_thread->t_ctl_waitq,
225 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
227 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
228 pinger_thread = NULL;
232 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
235 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
239 int ptlrpc_pinger_add_import(struct obd_import *imp)
242 if (!list_empty(&imp->imp_pinger_chain))
246 CDEBUG(D_HA, "adding pingable import %s->%s\n",
247 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
248 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
249 /* XXX sort, blah blah */
250 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
251 class_import_get(imp);
253 ptlrpc_pinger_wake_up();
259 int ptlrpc_pinger_del_import(struct obd_import *imp)
262 if (list_empty(&imp->imp_pinger_chain))
266 list_del_init(&imp->imp_pinger_chain);
267 CDEBUG(D_HA, "removing pingable import %s->%s\n",
268 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
269 class_import_put(imp);
274 void ptlrpc_pinger_wake_up()
277 pinger_thread->t_flags |= SVC_EVENT;
278 wake_up(&pinger_thread->t_ctl_waitq);
282 #else /* !__KERNEL__ */
285 * the current implementation of pinger in liblustre is not optimized
288 static struct pinger_data {
290 unsigned long pd_this_ping;
291 unsigned long pd_next_ping;
295 static int pinger_check_rpcs(void *arg)
297 unsigned long curtime = time(NULL);
298 struct list_head *iter;
299 struct pinger_data *pd = &pinger_args;
301 /* prevent recursion */
302 if (pd->pd_recursion++) {
303 CDEBUG(D_HA, "pinger: recursion! quit\n");
308 /* have we reached ping point? */
309 if (pd->pd_next_ping > curtime && !pd->pd_force_check) {
314 if (pd->pd_force_check)
315 pd->pd_force_check = 0;
317 pd->pd_this_ping = curtime;
319 /* add rpcs into set */
321 list_for_each(iter, &pinger_imports) {
322 struct obd_import *imp =
323 list_entry(iter, struct obd_import,
329 spin_lock_irqsave(&imp->imp_lock, flags);
330 level = imp->imp_state;
331 force = imp->imp_force_verify;
333 imp->imp_force_verify = 0;
334 spin_unlock_irqrestore(&imp->imp_lock, flags);
336 if (imp->imp_next_ping <= pd->pd_this_ping || force) {
337 if (level == LUSTRE_IMP_DISCON) {
338 /* wait at least a timeout before
339 trying recovery again. */
340 imp->imp_next_ping = time(NULL) +
342 ptlrpc_initiate_recovery(imp);
344 else if (level != LUSTRE_IMP_FULL ||
345 imp->imp_obd->obd_no_recov) {
347 "not pinging %s (in recovery "
348 " or recovery disabled: %s)\n",
349 imp->imp_target_uuid.uuid,
350 ptlrpc_import_state_name(level));
352 else if (imp->imp_pingable || force) {
357 if (imp->imp_pingable) {
358 CDEBUG(D_HA, "don't need to ping %s "
360 imp->imp_target_uuid.uuid,
361 imp->imp_next_ping, pd->pd_this_ping);
368 pd->pd_next_ping = pd->pd_this_ping + (obd_timeout * HZ);
370 CDEBUG(D_HA, "finished a round ping\n");
375 static void *pinger_callback = NULL;
377 int ptlrpc_start_pinger(void)
379 memset(&pinger_args, 0, sizeof(pinger_args));
382 liblustre_register_wait_callback(&pinger_check_rpcs, &pinger_args);
387 int ptlrpc_stop_pinger(void)
391 liblustre_deregister_wait_callback(pinger_callback);
396 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
399 imp->imp_next_ping = time(NULL) + obd_timeout;
400 if (pinger_args.pd_next_ping > imp->imp_next_ping) {
401 CDEBUG(D_HA, "set next ping to %ld(cur %ld)\n",
402 imp->imp_next_ping, time(NULL));
403 pinger_args.pd_next_ping = imp->imp_next_ping;
408 int ptlrpc_pinger_add_import(struct obd_import *imp)
411 if (!list_empty(&imp->imp_pinger_chain))
414 CDEBUG(D_HA, "adding pingable import %s->%s\n",
415 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
416 ptlrpc_pinger_sending_on_import(imp);
419 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
420 class_import_get(imp);
426 int ptlrpc_pinger_del_import(struct obd_import *imp)
429 if (list_empty(&imp->imp_pinger_chain))
433 list_del_init(&imp->imp_pinger_chain);
434 CDEBUG(D_HA, "removing pingable import %s->%s\n",
435 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
436 class_import_put(imp);
441 void ptlrpc_pinger_wake_up()
443 pinger_args.pd_force_check = 1;
445 #endif /* !__KERNEL__ */