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);
42 static struct ptlrpc_thread *pinger_thread = NULL;
44 int ptlrpc_ping(struct obd_import *imp)
46 struct ptlrpc_request *req;
50 req = ptlrpc_prep_req(imp, OBD_PING, 0, NULL,
53 DEBUG_REQ(D_HA, req, "pinging %s->%s",
54 imp->imp_obd->obd_uuid.uuid,
55 imp->imp_target_uuid.uuid);
56 req->rq_no_resend = req->rq_no_delay = 1;
57 req->rq_replen = lustre_msg_size(0,
61 CERROR("OOM trying to ping %s->%s\n",
62 imp->imp_obd->obd_uuid.uuid,
63 imp->imp_target_uuid.uuid);
70 static int ptlrpc_pinger_main(void *arg)
72 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
73 struct ptlrpc_thread *thread = data->thread;
80 SIGNAL_MASK_LOCK(current, flags);
81 sigfillset(¤t->blocked);
83 SIGNAL_MASK_UNLOCK(current, flags);
85 THREAD_NAME(current->comm, "%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 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);
284 * the current implementation of pinger in liblustre is not optimized
287 static struct pinger_data {
289 unsigned long pd_this_ping;
290 unsigned long pd_next_ping;
291 struct ptlrpc_request_set *pd_set;
294 static int pinger_check_rpcs(void *arg)
296 unsigned long curtime = time(NULL);
297 struct ptlrpc_request *req;
298 struct ptlrpc_request_set *set;
299 struct list_head *iter;
300 struct pinger_data *pd = &pinger_args;
303 /* prevent recursion */
304 if (pd->pd_recursion++) {
305 CDEBUG(D_HA, "pinger: recursion! quit\n");
311 /* have we reached ping point? */
312 if (!pd->pd_set && pd->pd_next_ping > curtime) {
317 /* if we have rpc_set already, continue processing it */
319 LASSERT(pd->pd_this_ping);
324 pd->pd_this_ping = curtime;
325 pd->pd_set = ptlrpc_prep_set();
328 /* add rpcs into set */
330 list_for_each(iter, &pinger_imports) {
331 struct obd_import *imp =
332 list_entry(iter, struct obd_import,
334 int generation, level;
337 if (imp->imp_next_ping <= pd->pd_this_ping) {
339 spin_lock_irqsave(&imp->imp_lock, flags);
340 generation = imp->imp_generation;
341 level = imp->imp_state;
342 spin_unlock_irqrestore(&imp->imp_lock, flags);
344 if (level != LUSTRE_IMP_FULL) {
346 "not pinging %s (in recovery)\n",
347 imp->imp_target_uuid.uuid);
351 req = ptlrpc_prep_req(imp, OBD_PING, 0, NULL,
354 CERROR("out of memory\n");
357 req->rq_no_resend = 1;
358 req->rq_replen = lustre_msg_size(0, NULL);
359 req->rq_send_state = LUSTRE_IMP_FULL;
360 req->rq_phase = RQ_PHASE_RPC;
361 req->rq_import_generation = generation;
362 ptlrpc_set_add_req(set, req);
364 CDEBUG(D_HA, "don't need to ping %s (%lu > "
365 "%lu)\n", imp->imp_target_uuid.uuid,
366 imp->imp_next_ping, pd->pd_this_ping);
369 pd->pd_this_ping = curtime;
372 /* Might be empty, that's OK. */
373 if (set->set_remaining == 0)
374 CDEBUG(D_HA, "nothing to ping\n");
376 list_for_each(iter, &set->set_requests) {
377 struct ptlrpc_request *req =
378 list_entry(iter, struct ptlrpc_request,
380 DEBUG_REQ(D_HA, req, "pinging %s->%s",
381 req->rq_import->imp_obd->obd_uuid.uuid,
382 req->rq_import->imp_target_uuid.uuid);
383 (void)ptl_send_rpc(req);
387 rc = ptlrpc_check_set(set);
389 /* not finished, and we are not expired, simply return */
390 if (!rc && curtime < pd->pd_this_ping + obd_timeout) {
391 CDEBUG(D_HA, "not finished, but also not expired\n");
396 /* Expire all the requests that didn't come back. */
398 list_for_each(iter, &set->set_requests) {
399 req = list_entry(iter, struct ptlrpc_request,
405 req->rq_phase = RQ_PHASE_COMPLETE;
406 set->set_remaining--;
407 /* If it was disconnected, don't sweat it. */
408 if (list_empty(&req->rq_import->imp_pinger_chain)) {
409 ptlrpc_unregister_reply(req);
413 CDEBUG(D_HA, "pinger initiate expire_one_request\n");
414 ptlrpc_expire_one_request(req);
418 ptlrpc_set_destroy(set);
421 pd->pd_next_ping = pd->pd_this_ping + obd_timeout;
422 pd->pd_this_ping = 0; /* XXX for debug */
424 CDEBUG(D_HA, "finished a round ping\n");
429 static void *pinger_callback = NULL;
431 int ptlrpc_start_pinger(void)
433 memset(&pinger_args, 0, sizeof(pinger_args));
436 liblustre_register_wait_callback(&pinger_check_rpcs, &pinger_args);
441 int ptlrpc_stop_pinger(void)
445 liblustre_deregister_wait_callback(pinger_callback);
450 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
453 imp->imp_next_ping = time(NULL) + obd_timeout;
454 if (pinger_args.pd_set == NULL &&
455 pinger_args.pd_next_ping > imp->imp_next_ping) {
456 CDEBUG(D_HA, "set next ping to %ld(cur %ld)\n",
457 imp->imp_next_ping, time(NULL));
458 pinger_args.pd_next_ping = imp->imp_next_ping;
463 int ptlrpc_pinger_add_import(struct obd_import *imp)
466 if (!list_empty(&imp->imp_pinger_chain))
469 CDEBUG(D_HA, "adding pingable import %s->%s\n",
470 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
471 ptlrpc_pinger_sending_on_import(imp);
474 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
475 class_import_get(imp);
481 int ptlrpc_pinger_del_import(struct obd_import *imp)
484 if (list_empty(&imp->imp_pinger_chain))
488 list_del_init(&imp->imp_pinger_chain);
489 CDEBUG(D_HA, "removing pingable import %s->%s\n",
490 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
491 class_import_put(imp);
496 void ptlrpc_pinger_wake_up()
499 /* XXX force pinger to run, if needed */
502 #endif /* !__KERNEL__ */