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 !imp->imp_deactive) {
120 /* wait at least a timeout before
121 trying recovery again. */
122 imp->imp_next_ping = jiffies +
124 ptlrpc_initiate_recovery(imp);
126 else if (level != LUSTRE_IMP_FULL ||
127 imp->imp_obd->obd_no_recov) {
129 "not pinging %s (in recovery "
130 " or recovery disabled: %s)\n",
131 imp->imp_target_uuid.uuid,
132 ptlrpc_import_state_name(level));
134 else if (imp->imp_pingable || force) {
139 if (imp->imp_pingable)
140 CDEBUG(D_HA, "don't need to ping %s "
142 imp->imp_target_uuid.uuid,
143 imp->imp_next_ping, this_ping);
148 /* Wait until the next ping time, or until we're stopped. */
149 time_to_next_ping = this_ping + (obd_timeout * HZ) - jiffies;
150 CDEBUG(D_HA, "next ping in %lu (%lu)\n", time_to_next_ping,
151 this_ping + (obd_timeout * HZ));
152 if (time_to_next_ping > 0) {
153 lwi = LWI_TIMEOUT(time_to_next_ping, NULL, NULL);
154 l_wait_event(thread->t_ctl_waitq,
155 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
157 if (thread->t_flags & SVC_STOPPING) {
158 thread->t_flags &= ~SVC_STOPPING;
161 } else if (thread->t_flags & SVC_EVENT) {
162 /* woken after adding import to reset timer */
163 thread->t_flags &= ~SVC_EVENT;
168 thread->t_flags = SVC_STOPPED;
169 wake_up(&thread->t_ctl_waitq);
171 CDEBUG(D_NET, "pinger thread exiting, process %d\n", current->pid);
175 static struct ptlrpc_thread *pinger_thread = NULL;
177 int ptlrpc_start_pinger(void)
179 struct l_wait_info lwi = { 0 };
180 struct ptlrpc_svc_data d;
182 #ifndef ENABLE_PINGER
187 if (pinger_thread != NULL)
190 OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
191 if (pinger_thread == NULL)
193 init_waitqueue_head(&pinger_thread->t_ctl_waitq);
196 d.thread = pinger_thread;
198 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
199 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
200 rc = kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
202 CERROR("cannot start thread: %d\n", rc);
203 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
206 l_wait_event(pinger_thread->t_ctl_waitq,
207 pinger_thread->t_flags & SVC_RUNNING, &lwi);
212 int ptlrpc_stop_pinger(void)
214 struct l_wait_info lwi = { 0 };
216 #ifndef ENABLE_PINGER
221 if (pinger_thread == NULL)
224 pinger_thread->t_flags = SVC_STOPPING;
225 wake_up(&pinger_thread->t_ctl_waitq);
228 l_wait_event(pinger_thread->t_ctl_waitq,
229 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
231 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
232 pinger_thread = NULL;
236 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
239 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
243 int ptlrpc_pinger_add_import(struct obd_import *imp)
246 if (!list_empty(&imp->imp_pinger_chain))
250 CDEBUG(D_HA, "adding pingable import %s->%s\n",
251 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
252 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
253 /* XXX sort, blah blah */
254 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
255 class_import_get(imp);
257 ptlrpc_pinger_wake_up();
263 int ptlrpc_pinger_del_import(struct obd_import *imp)
266 if (list_empty(&imp->imp_pinger_chain))
270 list_del_init(&imp->imp_pinger_chain);
271 CDEBUG(D_HA, "removing pingable import %s->%s\n",
272 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
273 class_import_put(imp);
278 void ptlrpc_pinger_wake_up()
281 pinger_thread->t_flags |= SVC_EVENT;
282 wake_up(&pinger_thread->t_ctl_waitq);
286 #else /* !__KERNEL__ */
289 * the current implementation of pinger in liblustre is not optimized
292 static struct pinger_data {
294 unsigned long pd_this_ping;
295 unsigned long pd_next_ping;
296 struct ptlrpc_request_set *pd_set;
299 static int pinger_check_rpcs(void *arg)
301 unsigned long curtime = time(NULL);
302 struct ptlrpc_request *req;
303 struct ptlrpc_request_set *set;
304 struct list_head *iter;
305 struct pinger_data *pd = &pinger_args;
308 /* prevent recursion */
309 if (pd->pd_recursion++) {
310 CDEBUG(D_HA, "pinger: recursion! quit\n");
316 /* have we reached ping point? */
317 if (!pd->pd_set && pd->pd_next_ping > curtime) {
322 /* if we have rpc_set already, continue processing it */
324 LASSERT(pd->pd_this_ping);
329 pd->pd_this_ping = curtime;
330 pd->pd_set = ptlrpc_prep_set();
333 /* add rpcs into set */
335 list_for_each(iter, &pinger_imports) {
336 struct obd_import *imp =
337 list_entry(iter, struct obd_import,
339 int generation, level;
342 if (imp->imp_next_ping <= pd->pd_this_ping) {
344 spin_lock_irqsave(&imp->imp_lock, flags);
345 generation = imp->imp_generation;
346 level = imp->imp_state;
347 spin_unlock_irqrestore(&imp->imp_lock, flags);
349 if (level != LUSTRE_IMP_FULL) {
351 "not pinging %s (in recovery)\n",
352 imp->imp_target_uuid.uuid);
356 req = ptlrpc_prep_req(imp, OBD_PING, 0, NULL,
359 CERROR("out of memory\n");
362 req->rq_no_resend = 1;
363 req->rq_replen = lustre_msg_size(0, NULL);
364 req->rq_send_state = LUSTRE_IMP_FULL;
365 req->rq_phase = RQ_PHASE_RPC;
366 req->rq_import_generation = generation;
367 ptlrpc_set_add_req(set, req);
369 CDEBUG(D_HA, "don't need to ping %s (%lu > "
370 "%lu)\n", imp->imp_target_uuid.uuid,
371 imp->imp_next_ping, pd->pd_this_ping);
374 pd->pd_this_ping = curtime;
377 /* Might be empty, that's OK. */
378 if (set->set_remaining == 0)
379 CDEBUG(D_HA, "nothing to ping\n");
381 list_for_each(iter, &set->set_requests) {
382 struct ptlrpc_request *req =
383 list_entry(iter, struct ptlrpc_request,
385 DEBUG_REQ(D_HA, req, "pinging %s->%s",
386 req->rq_import->imp_obd->obd_uuid.uuid,
387 req->rq_import->imp_target_uuid.uuid);
388 (void)ptl_send_rpc(req);
392 rc = ptlrpc_check_set(set);
394 /* not finished, and we are not expired, simply return */
395 if (!rc && curtime < pd->pd_this_ping + obd_timeout) {
396 CDEBUG(D_HA, "not finished, but also not expired\n");
401 /* Expire all the requests that didn't come back. */
403 list_for_each(iter, &set->set_requests) {
404 req = list_entry(iter, struct ptlrpc_request,
410 req->rq_phase = RQ_PHASE_COMPLETE;
411 set->set_remaining--;
412 /* If it was disconnected, don't sweat it. */
413 if (list_empty(&req->rq_import->imp_pinger_chain)) {
414 ptlrpc_unregister_reply(req);
418 CDEBUG(D_HA, "pinger initiate expire_one_request\n");
419 ptlrpc_expire_one_request(req);
423 ptlrpc_set_destroy(set);
426 pd->pd_next_ping = pd->pd_this_ping + obd_timeout;
427 pd->pd_this_ping = 0; /* XXX for debug */
429 CDEBUG(D_HA, "finished a round ping\n");
434 static void *pinger_callback = NULL;
436 int ptlrpc_start_pinger(void)
438 memset(&pinger_args, 0, sizeof(pinger_args));
441 liblustre_register_wait_callback(&pinger_check_rpcs, &pinger_args);
446 int ptlrpc_stop_pinger(void)
450 liblustre_deregister_wait_callback(pinger_callback);
455 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
458 imp->imp_next_ping = time(NULL) + obd_timeout;
459 if (pinger_args.pd_set == NULL &&
460 pinger_args.pd_next_ping > imp->imp_next_ping) {
461 CDEBUG(D_HA, "set next ping to %ld(cur %ld)\n",
462 imp->imp_next_ping, time(NULL));
463 pinger_args.pd_next_ping = imp->imp_next_ping;
468 int ptlrpc_pinger_add_import(struct obd_import *imp)
471 if (!list_empty(&imp->imp_pinger_chain))
474 CDEBUG(D_HA, "adding pingable import %s->%s\n",
475 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
476 ptlrpc_pinger_sending_on_import(imp);
479 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
480 class_import_get(imp);
486 int ptlrpc_pinger_del_import(struct obd_import *imp)
489 if (list_empty(&imp->imp_pinger_chain))
493 list_del_init(&imp->imp_pinger_chain);
494 CDEBUG(D_HA, "removing pingable import %s->%s\n",
495 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
496 class_import_put(imp);
501 void ptlrpc_pinger_wake_up()
504 /* XXX force pinger to run, if needed */
507 #endif /* !__KERNEL__ */