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 #define PINGER_RATE 3 /* how many pings we'll do in obd_timeout period */
40 static DECLARE_MUTEX(pinger_sem);
41 static struct list_head pinger_imports = LIST_HEAD_INIT(pinger_imports);
43 static int ptlrpc_ping_interpret(struct ptlrpc_request *req,
46 struct obd_import *imp = req->rq_import;
47 DEBUG_REQ(D_HA, req, "ping reply");
48 if (imp->imp_waiting_ping_reply == 0)
49 DEBUG_REQ(D_ERROR, req, "late ping reply?");
50 if (imp->imp_last_ping_xid != req->rq_xid)
51 DEBUG_REQ(D_ERROR, req, "uh, wrong ping reply on x%lx",
52 imp->imp_last_ping_xid);
54 imp->imp_last_ping_xid = 0;
56 /* if ping reply is an error, don't drop "replied" flag
57 * on import, so pinger will invalidate it */
58 if (ptlrpc_client_replied(req) && req->rq_repmsg == NULL)
59 CWARN("replied (%d) w/o rep buffer?\n", req->rq_replied);
60 if (ptlrpc_client_replied(req) && req->rq_repmsg &&
61 req->rq_repmsg->type == PTL_RPC_MSG_ERR)
64 imp->imp_waiting_ping_reply = 0;
68 int ptlrpc_ping(struct obd_import *imp)
70 struct ptlrpc_request *req;
74 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING, 0, NULL, NULL);
76 DEBUG_REQ(D_HA, req, "pinging %s->%s",
77 imp->imp_obd->obd_uuid.uuid,
78 imp->imp_target_uuid.uuid);
79 req->rq_no_resend = req->rq_no_delay = 1;
80 req->rq_replen = lustre_msg_size(0, NULL);
81 req->rq_interpret_reply = ptlrpc_ping_interpret;
82 req->rq_timeout = obd_timeout / PINGER_RATE;
83 imp->imp_waiting_ping_reply = 1;
84 imp->imp_last_ping_xid = req->rq_xid;
87 CERROR("OOM trying to ping %s->%s\n",
88 imp->imp_obd->obd_uuid.uuid,
89 imp->imp_target_uuid.uuid);
97 static inline int ptlrpc_next_ping(struct obd_import *imp)
99 return jiffies + (obd_timeout / PINGER_RATE * HZ);
102 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
104 if (imp->imp_server_timeout)
105 return jiffies + (obd_timeout / 2 * HZ);
107 return jiffies + (obd_timeout * HZ);
110 static atomic_t suspend_timeouts = ATOMIC_INIT(0);
111 static wait_queue_head_t suspend_timeouts_waitq;
113 void ptlrpc_deactivate_timeouts(void)
115 CDEBUG(D_HA, "deactivate timeouts\n");
116 atomic_inc(&suspend_timeouts);
119 void ptlrpc_activate_timeouts(void)
121 CDEBUG(D_HA, "activate timeouts\n");
122 LASSERT(atomic_read(&suspend_timeouts) > 0);
123 if (atomic_dec_and_test(&suspend_timeouts))
124 wake_up(&suspend_timeouts_waitq);
127 int ptlrpc_check_suspend(void)
129 if (atomic_read(&suspend_timeouts))
134 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req)
136 struct l_wait_info lwi;
138 if (atomic_read(&suspend_timeouts)) {
139 DEBUG_REQ(D_NET, req, "-- suspend %d regular timeout",
140 atomic_read(&suspend_timeouts));
141 lwi = LWI_INTR(NULL, NULL);
142 l_wait_event(suspend_timeouts_waitq,
143 atomic_read(&suspend_timeouts) == 0, &lwi);
144 DEBUG_REQ(D_NET, req, "-- recharge regular timeout");
150 static void ptlrpc_pinger_process_import(struct obd_import *imp,
151 unsigned long this_ping)
156 spin_lock_irqsave(&imp->imp_lock, flags);
157 level = imp->imp_state;
158 force = imp->imp_force_verify;
160 imp->imp_force_verify = 0;
161 spin_unlock_irqrestore(&imp->imp_lock, flags);
163 if (imp->imp_next_ping > this_ping && force == 0)
166 if (level == LUSTRE_IMP_DISCON && !imp->imp_deactive) {
167 /* wait at least a timeout before trying recovery again */
168 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
169 ptlrpc_initiate_recovery(imp);
170 } else if (level != LUSTRE_IMP_FULL || imp->imp_obd->obd_no_recov) {
171 CDEBUG(D_HA, "not pinging %s (in recovery "
172 " or recovery disabled: %s)\n",
173 imp->imp_target_uuid.uuid,
174 ptlrpc_import_state_name(level));
175 } else if (level == LUSTRE_IMP_FULL && imp->imp_waiting_ping_reply &&
176 imp->imp_next_ping >= this_ping && imp->imp_pingable) {
177 CDEBUG(D_HA, "%s: %s hasn't respond on ping x%lu\n",
178 imp->imp_obd->obd_uuid.uuid,
179 imp->imp_target_uuid.uuid, imp->imp_last_ping_xid);
180 CDEBUG(D_ERROR, "%s: %s hasn't respond on ping x%lu\n",
181 imp->imp_obd->obd_uuid.uuid,
182 imp->imp_target_uuid.uuid, imp->imp_last_ping_xid);
183 ptlrpc_fail_import(imp, 0);
184 } else if (imp->imp_pingable || force) {
185 imp->imp_next_ping = ptlrpc_next_ping(imp);
190 static int ptlrpc_pinger_main(void *arg)
192 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
193 struct ptlrpc_thread *thread = data->thread;
200 SIGNAL_MASK_LOCK(current, flags);
201 sigfillset(¤t->blocked);
203 SIGNAL_MASK_UNLOCK(current, flags);
205 LASSERTF(strlen(data->name) < sizeof(current->comm),
206 "name %d > len %d\n",
207 (int)strlen(data->name), (int)sizeof(current->comm));
208 THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s", data->name);
211 /* Record that the thread is running */
212 thread->t_flags = SVC_RUNNING;
213 wake_up(&thread->t_ctl_waitq);
215 /* And now, loop forever, pinging as needed. */
217 unsigned long this_ping = jiffies;
218 long time_to_next_ping;
219 struct l_wait_info lwi = LWI_TIMEOUT(obd_timeout * HZ,
221 struct list_head *iter;
223 time_to_next_ping = this_ping + (obd_timeout * HZ) - jiffies;
225 list_for_each(iter, &pinger_imports) {
226 struct obd_import *imp =
227 list_entry(iter, struct obd_import,
230 ptlrpc_pinger_process_import(imp, this_ping);
232 CDEBUG(D_OTHER, "%s: pingable %d, next_ping %lu(%lu)\n",
233 imp->imp_target_uuid.uuid,
234 imp->imp_pingable, imp->imp_next_ping, jiffies);
236 if (imp->imp_pingable && imp->imp_next_ping &&
237 imp->imp_next_ping - jiffies < time_to_next_ping &&
238 imp->imp_next_ping > jiffies)
239 time_to_next_ping = imp->imp_next_ping - jiffies;
243 /* Wait until the next ping time, or until we're stopped. */
244 CDEBUG(D_HA, "next ping in %lu (%lu)\n", time_to_next_ping,
245 this_ping + (obd_timeout * HZ));
246 if (time_to_next_ping > 0) {
247 lwi = LWI_TIMEOUT(time_to_next_ping, NULL, NULL);
248 l_wait_event(thread->t_ctl_waitq,
249 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
251 if (thread->t_flags & SVC_STOPPING) {
252 thread->t_flags &= ~SVC_STOPPING;
255 } else if (thread->t_flags & SVC_EVENT) {
256 /* woken after adding import to reset timer */
257 thread->t_flags &= ~SVC_EVENT;
262 thread->t_flags = SVC_STOPPED;
263 wake_up(&thread->t_ctl_waitq);
265 CDEBUG(D_NET, "pinger thread exiting, process %d\n", current->pid);
269 static struct ptlrpc_thread *pinger_thread = NULL;
271 int ptlrpc_start_pinger(void)
273 struct l_wait_info lwi = { 0 };
274 struct ptlrpc_svc_data d;
276 #ifndef ENABLE_PINGER
281 LASSERT(obd_timeout > PINGER_RATE);
283 if (pinger_thread != NULL)
286 OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
287 if (pinger_thread == NULL)
289 init_waitqueue_head(&pinger_thread->t_ctl_waitq);
290 init_waitqueue_head(&suspend_timeouts_waitq);
293 d.thread = pinger_thread;
295 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
296 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
297 rc = kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
299 CERROR("cannot start thread: %d\n", rc);
300 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
303 l_wait_event(pinger_thread->t_ctl_waitq,
304 pinger_thread->t_flags & SVC_RUNNING, &lwi);
309 int ptlrpc_stop_pinger(void)
311 struct l_wait_info lwi = { 0 };
313 #ifndef ENABLE_PINGER
318 if (pinger_thread == NULL)
321 pinger_thread->t_flags = SVC_STOPPING;
322 wake_up(&pinger_thread->t_ctl_waitq);
325 l_wait_event(pinger_thread->t_ctl_waitq,
326 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
328 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
329 pinger_thread = NULL;
333 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
336 imp->imp_next_ping = ptlrpc_next_ping(imp);
340 int ptlrpc_pinger_add_import(struct obd_import *imp)
343 if (!list_empty(&imp->imp_pinger_chain))
347 CDEBUG(D_HA, "adding pingable import %s->%s\n",
348 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
349 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
350 /* XXX sort, blah blah */
351 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
352 class_import_get(imp);
354 ptlrpc_pinger_wake_up();
360 int ptlrpc_pinger_del_import(struct obd_import *imp)
363 if (list_empty(&imp->imp_pinger_chain))
367 list_del_init(&imp->imp_pinger_chain);
368 CDEBUG(D_HA, "removing pingable import %s->%s\n",
369 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
370 class_import_put(imp);
375 void ptlrpc_pinger_wake_up()
378 pinger_thread->t_flags |= SVC_EVENT;
379 wake_up(&pinger_thread->t_ctl_waitq);
383 #else /* !__KERNEL__ */
386 * the current implementation of pinger in liblustre is not optimized
389 static struct pinger_data {
391 unsigned long pd_this_ping;
392 unsigned long pd_next_ping;
396 static int pinger_check_rpcs(void *arg)
398 unsigned long curtime = time(NULL);
399 struct list_head *iter;
400 struct pinger_data *pd = &pinger_args;
402 /* prevent recursion */
403 if (pd->pd_recursion++) {
404 CDEBUG(D_HA, "pinger: recursion! quit\n");
409 /* have we reached ping point? */
410 if (pd->pd_next_ping > curtime && !pd->pd_force_check) {
415 if (pd->pd_force_check)
416 pd->pd_force_check = 0;
418 pd->pd_this_ping = curtime;
420 /* add rpcs into set */
422 list_for_each(iter, &pinger_imports) {
423 struct obd_import *imp =
424 list_entry(iter, struct obd_import,
430 spin_lock_irqsave(&imp->imp_lock, flags);
431 level = imp->imp_state;
432 force = imp->imp_force_verify;
434 imp->imp_force_verify = 0;
435 spin_unlock_irqrestore(&imp->imp_lock, flags);
437 if (imp->imp_next_ping <= pd->pd_this_ping || force) {
438 if (level == LUSTRE_IMP_DISCON) {
439 /* wait at least a timeout before
440 trying recovery again. */
441 unsigned long timeout = obd_timeout;
442 if (imp->imp_server_timeout)
443 timeout = obd_timeout / 2;
444 imp->imp_next_ping = time(NULL) +
446 ptlrpc_initiate_recovery(imp);
448 else if (level != LUSTRE_IMP_FULL ||
449 imp->imp_obd->obd_no_recov) {
451 "not pinging %s (in recovery "
452 " or recovery disabled: %s)\n",
453 imp->imp_target_uuid.uuid,
454 ptlrpc_import_state_name(level));
456 else if (imp->imp_pingable || force) {
461 if (imp->imp_pingable) {
462 CDEBUG(D_HA, "don't need to ping %s "
464 imp->imp_target_uuid.uuid,
465 imp->imp_next_ping, pd->pd_this_ping);
472 pd->pd_next_ping = pd->pd_this_ping + (obd_timeout * HZ);
474 CDEBUG(D_HA, "finished a round ping\n");
479 static void *pinger_callback = NULL;
481 int ptlrpc_start_pinger(void)
483 memset(&pinger_args, 0, sizeof(pinger_args));
486 liblustre_register_wait_callback(&pinger_check_rpcs, &pinger_args);
491 int ptlrpc_stop_pinger(void)
495 liblustre_deregister_wait_callback(pinger_callback);
500 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
503 imp->imp_next_ping = time(NULL) + obd_timeout;
504 if (pinger_args.pd_next_ping > imp->imp_next_ping) {
505 CDEBUG(D_HA, "set next ping to %ld(cur %ld)\n",
506 imp->imp_next_ping, time(NULL));
507 pinger_args.pd_next_ping = imp->imp_next_ping;
512 int ptlrpc_pinger_add_import(struct obd_import *imp)
515 if (!list_empty(&imp->imp_pinger_chain))
518 CDEBUG(D_HA, "adding pingable import %s->%s\n",
519 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
520 ptlrpc_pinger_sending_on_import(imp);
523 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
524 class_import_get(imp);
530 int ptlrpc_pinger_del_import(struct obd_import *imp)
533 if (list_empty(&imp->imp_pinger_chain))
537 list_del_init(&imp->imp_pinger_chain);
538 CDEBUG(D_HA, "removing pingable import %s->%s\n",
539 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
540 class_import_put(imp);
545 void ptlrpc_pinger_wake_up()
547 pinger_args.pd_force_check = 1;
549 #endif /* !__KERNEL__ */