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),
85 (int)strlen(data->name), (int)sizeof(current->comm));
86 THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s", data->name);
89 /* Record that the thread is running */
90 thread->t_flags = SVC_RUNNING;
91 wake_up(&thread->t_ctl_waitq);
93 /* And now, loop forever, pinging as needed. */
95 unsigned long this_ping = jiffies;
96 long time_to_next_ping;
97 struct l_wait_info lwi = LWI_TIMEOUT(obd_timeout * HZ,
99 struct list_head *iter;
102 list_for_each(iter, &pinger_imports) {
103 struct obd_import *imp =
104 list_entry(iter, struct obd_import,
110 spin_lock_irqsave(&imp->imp_lock, flags);
111 level = imp->imp_state;
112 force = imp->imp_force_verify;
114 imp->imp_force_verify = 0;
115 spin_unlock_irqrestore(&imp->imp_lock, flags);
117 if (imp->imp_next_ping <= this_ping || force) {
118 if (level == LUSTRE_IMP_DISCON) {
119 /* wait at least a timeout before
120 trying recovery again. */
121 imp->imp_next_ping = jiffies +
123 ptlrpc_initiate_recovery(imp);
125 else if (level != LUSTRE_IMP_FULL ||
126 imp->imp_obd->obd_no_recov) {
128 "not pinging %s (in recovery "
129 " or recovery disabled: %s)\n",
130 imp->imp_target_uuid.uuid,
131 ptlrpc_import_state_name(level));
133 else if (imp->imp_pingable || force) {
138 if (imp->imp_pingable)
139 CDEBUG(D_HA, "don't need to ping %s "
141 imp->imp_target_uuid.uuid,
142 imp->imp_next_ping, this_ping);
147 /* Wait until the next ping time, or until we're stopped. */
148 time_to_next_ping = this_ping + (obd_timeout * HZ) - jiffies;
149 CDEBUG(D_HA, "next ping in %lu (%lu)\n", time_to_next_ping,
150 this_ping + (obd_timeout * HZ));
151 if (time_to_next_ping > 0) {
152 lwi = LWI_TIMEOUT(time_to_next_ping, NULL, NULL);
153 l_wait_event(thread->t_ctl_waitq,
154 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
156 if (thread->t_flags & SVC_STOPPING) {
157 thread->t_flags &= ~SVC_STOPPING;
160 } else if (thread->t_flags & SVC_EVENT) {
161 /* woken after adding import to reset timer */
162 thread->t_flags &= ~SVC_EVENT;
167 thread->t_flags = SVC_STOPPED;
168 wake_up(&thread->t_ctl_waitq);
170 CDEBUG(D_NET, "pinger thread exiting, process %d\n", current->pid);
174 static struct ptlrpc_thread *pinger_thread = NULL;
176 int ptlrpc_start_pinger(void)
178 struct l_wait_info lwi = { 0 };
179 struct ptlrpc_svc_data d;
181 #ifndef ENABLE_PINGER
186 if (pinger_thread != NULL)
189 OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
190 if (pinger_thread == NULL)
192 init_waitqueue_head(&pinger_thread->t_ctl_waitq);
195 d.thread = pinger_thread;
197 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
198 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
199 rc = kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
201 CERROR("cannot start thread: %d\n", rc);
202 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
205 l_wait_event(pinger_thread->t_ctl_waitq,
206 pinger_thread->t_flags & SVC_RUNNING, &lwi);
211 int ptlrpc_stop_pinger(void)
213 struct l_wait_info lwi = { 0 };
215 #ifndef ENABLE_PINGER
220 if (pinger_thread == NULL)
223 pinger_thread->t_flags = SVC_STOPPING;
224 wake_up(&pinger_thread->t_ctl_waitq);
227 l_wait_event(pinger_thread->t_ctl_waitq,
228 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
230 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
231 pinger_thread = NULL;
235 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
238 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
242 int ptlrpc_pinger_add_import(struct obd_import *imp)
245 if (!list_empty(&imp->imp_pinger_chain))
249 CDEBUG(D_HA, "adding pingable import %s->%s\n",
250 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
251 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
252 /* XXX sort, blah blah */
253 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
254 class_import_get(imp);
256 ptlrpc_pinger_wake_up();
262 int ptlrpc_pinger_del_import(struct obd_import *imp)
265 if (list_empty(&imp->imp_pinger_chain))
269 list_del_init(&imp->imp_pinger_chain);
270 CDEBUG(D_HA, "removing pingable import %s->%s\n",
271 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
272 class_import_put(imp);
277 void ptlrpc_pinger_wake_up()
280 pinger_thread->t_flags |= SVC_EVENT;
281 wake_up(&pinger_thread->t_ctl_waitq);
285 #else /* !__KERNEL__ */
288 * the current implementation of pinger in liblustre is not optimized
291 static struct pinger_data {
293 unsigned long pd_this_ping;
294 unsigned long pd_next_ping;
298 static int pinger_check_rpcs(void *arg)
300 unsigned long curtime = time(NULL);
301 struct list_head *iter;
302 struct pinger_data *pd = &pinger_args;
304 /* prevent recursion */
305 if (pd->pd_recursion++) {
306 CDEBUG(D_HA, "pinger: recursion! quit\n");
311 /* have we reached ping point? */
312 if (pd->pd_next_ping > curtime && !pd->pd_force_check) {
317 if (pd->pd_force_check)
318 pd->pd_force_check = 0;
320 pd->pd_this_ping = curtime;
322 /* add rpcs into set */
324 list_for_each(iter, &pinger_imports) {
325 struct obd_import *imp =
326 list_entry(iter, struct obd_import,
332 spin_lock_irqsave(&imp->imp_lock, flags);
333 level = imp->imp_state;
334 force = imp->imp_force_verify;
336 imp->imp_force_verify = 0;
337 spin_unlock_irqrestore(&imp->imp_lock, flags);
339 if (imp->imp_next_ping <= pd->pd_this_ping || force) {
340 if (level == LUSTRE_IMP_DISCON) {
341 /* wait at least a timeout before
342 trying recovery again. */
343 unsigned long timeout = obd_timeout;
344 if (imp->imp_server_timeout)
345 timeout = obd_timeout / 2;
346 imp->imp_next_ping = time(NULL) +
348 ptlrpc_initiate_recovery(imp);
350 else if (level != LUSTRE_IMP_FULL ||
351 imp->imp_obd->obd_no_recov) {
353 "not pinging %s (in recovery "
354 " or recovery disabled: %s)\n",
355 imp->imp_target_uuid.uuid,
356 ptlrpc_import_state_name(level));
358 else if (imp->imp_pingable || force) {
363 if (imp->imp_pingable) {
364 CDEBUG(D_HA, "don't need to ping %s "
366 imp->imp_target_uuid.uuid,
367 imp->imp_next_ping, pd->pd_this_ping);
374 pd->pd_next_ping = pd->pd_this_ping + (obd_timeout * HZ);
376 CDEBUG(D_HA, "finished a round ping\n");
381 static void *pinger_callback = NULL;
383 int ptlrpc_start_pinger(void)
385 memset(&pinger_args, 0, sizeof(pinger_args));
388 liblustre_register_wait_callback(&pinger_check_rpcs, &pinger_args);
393 int ptlrpc_stop_pinger(void)
397 liblustre_deregister_wait_callback(pinger_callback);
402 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
405 imp->imp_next_ping = time(NULL) + obd_timeout;
406 if (pinger_args.pd_next_ping > imp->imp_next_ping) {
407 CDEBUG(D_HA, "set next ping to %ld(cur %ld)\n",
408 imp->imp_next_ping, time(NULL));
409 pinger_args.pd_next_ping = imp->imp_next_ping;
414 int ptlrpc_pinger_add_import(struct obd_import *imp)
417 if (!list_empty(&imp->imp_pinger_chain))
420 CDEBUG(D_HA, "adding pingable import %s->%s\n",
421 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
422 ptlrpc_pinger_sending_on_import(imp);
425 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
426 class_import_get(imp);
432 int ptlrpc_pinger_del_import(struct obd_import *imp)
435 if (list_empty(&imp->imp_pinger_chain))
439 list_del_init(&imp->imp_pinger_chain);
440 CDEBUG(D_HA, "removing pingable import %s->%s\n",
441 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
442 class_import_put(imp);
447 void ptlrpc_pinger_wake_up()
449 pinger_args.pd_force_check = 1;
451 #endif /* !__KERNEL__ */