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 !imp->imp_deactive) {
127 /* wait at least a timeout before
128 trying recovery again. */
130 ptlrpc_next_ping(imp);
131 ptlrpc_initiate_recovery(imp);
132 } else if (level != LUSTRE_IMP_FULL ||
133 imp->imp_obd->obd_no_recov) {
135 "not pinging %s (in recovery "
136 "or recovery disabled: %s)\n",
137 imp->imp_target_uuid.uuid,
138 ptlrpc_import_state_name(level));
139 } else if (imp->imp_pingable || force) {
143 } else if (imp->imp_pingable) {
144 CDEBUG(D_HA, "don't need to ping %s "
146 imp->imp_target_uuid.uuid,
147 imp->imp_next_ping, this_ping);
149 CDEBUG(D_OTHER, "%s: pingable %d, next_ping %lu(%lu)\n",
150 imp->imp_target_uuid.uuid,
151 imp->imp_pingable, imp->imp_next_ping, jiffies);
152 if (imp->imp_pingable && imp->imp_next_ping &&
153 imp->imp_next_ping - jiffies < time_to_next_ping &&
154 imp->imp_next_ping > jiffies)
155 time_to_next_ping = imp->imp_next_ping - jiffies;
159 /* Wait until the next ping time, or until we're stopped. */
160 CDEBUG(D_HA, "next ping in %lu (%lu)\n", time_to_next_ping,
161 this_ping + (obd_timeout * HZ));
162 if (time_to_next_ping > 0) {
163 lwi = LWI_TIMEOUT(time_to_next_ping, NULL, NULL);
164 l_wait_event(thread->t_ctl_waitq,
165 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
167 if (thread->t_flags & SVC_STOPPING) {
168 thread->t_flags &= ~SVC_STOPPING;
171 } else if (thread->t_flags & SVC_EVENT) {
172 /* woken after adding import to reset timer */
173 thread->t_flags &= ~SVC_EVENT;
178 thread->t_flags = SVC_STOPPED;
179 wake_up(&thread->t_ctl_waitq);
181 CDEBUG(D_NET, "pinger thread exiting, process %d\n", current->pid);
185 static struct ptlrpc_thread *pinger_thread = NULL;
187 int ptlrpc_start_pinger(void)
189 struct l_wait_info lwi = { 0 };
190 struct ptlrpc_svc_data d;
192 #ifndef ENABLE_PINGER
197 if (pinger_thread != NULL)
200 OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
201 if (pinger_thread == NULL)
203 init_waitqueue_head(&pinger_thread->t_ctl_waitq);
206 d.thread = pinger_thread;
208 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
209 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
210 rc = kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
212 CERROR("cannot start thread: %d\n", rc);
213 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
216 l_wait_event(pinger_thread->t_ctl_waitq,
217 pinger_thread->t_flags & SVC_RUNNING, &lwi);
222 int ptlrpc_stop_pinger(void)
224 struct l_wait_info lwi = { 0 };
226 #ifndef ENABLE_PINGER
231 if (pinger_thread == NULL)
234 pinger_thread->t_flags = SVC_STOPPING;
235 wake_up(&pinger_thread->t_ctl_waitq);
238 l_wait_event(pinger_thread->t_ctl_waitq,
239 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
241 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
242 pinger_thread = NULL;
246 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
249 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
253 int ptlrpc_pinger_add_import(struct obd_import *imp)
256 if (!list_empty(&imp->imp_pinger_chain))
260 CDEBUG(D_HA, "adding pingable import %s->%s\n",
261 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
262 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
263 /* XXX sort, blah blah */
264 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
265 class_import_get(imp);
267 ptlrpc_pinger_wake_up();
273 int ptlrpc_pinger_del_import(struct obd_import *imp)
276 if (list_empty(&imp->imp_pinger_chain))
280 list_del_init(&imp->imp_pinger_chain);
281 CDEBUG(D_HA, "removing pingable import %s->%s\n",
282 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
283 class_import_put(imp);
288 void ptlrpc_pinger_wake_up()
291 pinger_thread->t_flags |= SVC_EVENT;
292 wake_up(&pinger_thread->t_ctl_waitq);
296 #else /* !__KERNEL__ */
299 * the current implementation of pinger in liblustre is not optimized
302 static struct pinger_data {
304 unsigned long pd_this_ping;
305 unsigned long pd_next_ping;
309 static int pinger_check_rpcs(void *arg)
311 unsigned long curtime = time(NULL);
312 struct list_head *iter;
313 struct pinger_data *pd = &pinger_args;
315 /* prevent recursion */
316 if (pd->pd_recursion++) {
317 CDEBUG(D_HA, "pinger: recursion! quit\n");
322 /* have we reached ping point? */
323 if (pd->pd_next_ping > curtime && !pd->pd_force_check) {
328 if (pd->pd_force_check)
329 pd->pd_force_check = 0;
331 pd->pd_this_ping = curtime;
333 /* add rpcs into set */
335 list_for_each(iter, &pinger_imports) {
336 struct obd_import *imp =
337 list_entry(iter, struct obd_import,
343 spin_lock_irqsave(&imp->imp_lock, flags);
344 level = imp->imp_state;
345 force = imp->imp_force_verify;
347 imp->imp_force_verify = 0;
348 spin_unlock_irqrestore(&imp->imp_lock, flags);
350 if (imp->imp_next_ping <= pd->pd_this_ping || force) {
351 if (level == LUSTRE_IMP_DISCON) {
352 /* wait at least a timeout before
353 trying recovery again. */
354 unsigned long timeout = obd_timeout;
355 if (imp->imp_server_timeout)
356 timeout = obd_timeout / 2;
357 imp->imp_next_ping = time(NULL) +
359 ptlrpc_initiate_recovery(imp);
361 else if (level != LUSTRE_IMP_FULL ||
362 imp->imp_obd->obd_no_recov) {
364 "not pinging %s (in recovery "
365 " or recovery disabled: %s)\n",
366 imp->imp_target_uuid.uuid,
367 ptlrpc_import_state_name(level));
369 else if (imp->imp_pingable || force) {
374 if (imp->imp_pingable) {
375 CDEBUG(D_HA, "don't need to ping %s "
377 imp->imp_target_uuid.uuid,
378 imp->imp_next_ping, pd->pd_this_ping);
385 pd->pd_next_ping = pd->pd_this_ping + (obd_timeout * HZ);
387 CDEBUG(D_HA, "finished a round ping\n");
392 static void *pinger_callback = NULL;
394 int ptlrpc_start_pinger(void)
396 memset(&pinger_args, 0, sizeof(pinger_args));
399 liblustre_register_wait_callback(&pinger_check_rpcs, &pinger_args);
404 int ptlrpc_stop_pinger(void)
408 liblustre_deregister_wait_callback(pinger_callback);
413 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
416 imp->imp_next_ping = time(NULL) + obd_timeout;
417 if (pinger_args.pd_next_ping > imp->imp_next_ping) {
418 CDEBUG(D_HA, "set next ping to %ld(cur %ld)\n",
419 imp->imp_next_ping, time(NULL));
420 pinger_args.pd_next_ping = imp->imp_next_ping;
425 int ptlrpc_pinger_add_import(struct obd_import *imp)
428 if (!list_empty(&imp->imp_pinger_chain))
431 CDEBUG(D_HA, "adding pingable import %s->%s\n",
432 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
433 ptlrpc_pinger_sending_on_import(imp);
436 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
437 class_import_get(imp);
443 int ptlrpc_pinger_del_import(struct obd_import *imp)
446 if (list_empty(&imp->imp_pinger_chain))
450 list_del_init(&imp->imp_pinger_chain);
451 CDEBUG(D_HA, "removing pingable import %s->%s\n",
452 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
453 class_import_put(imp);
458 void ptlrpc_pinger_wake_up()
460 pinger_args.pd_force_check = 1;
462 #endif /* !__KERNEL__ */