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 static int ptlrpc_pinger_main(void *arg)
46 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
47 struct ptlrpc_thread *thread = data->thread;
54 SIGNAL_MASK_LOCK(current, flags);
55 sigfillset(¤t->blocked);
57 SIGNAL_MASK_UNLOCK(current, flags);
59 THREAD_NAME(current->comm, "%s", data->name);
62 /* Record that the thread is running */
63 thread->t_flags = SVC_RUNNING;
64 wake_up(&thread->t_ctl_waitq);
66 /* And now, loop forever, pinging as needed. */
68 unsigned long this_ping = jiffies;
69 long time_to_next_ping;
70 struct l_wait_info lwi = LWI_TIMEOUT(obd_timeout * HZ,
72 struct ptlrpc_request_set *set;
73 struct ptlrpc_request *req;
74 struct list_head *iter;
75 wait_queue_t set_wait;
78 set = ptlrpc_prep_set();
80 list_for_each(iter, &pinger_imports) {
81 struct obd_import *imp =
82 list_entry(iter, struct obd_import,
84 int generation, level;
87 if (imp->imp_next_ping <= this_ping) {
89 spin_lock_irqsave(&imp->imp_lock, flags);
90 generation = imp->imp_generation;
91 level = imp->imp_state;
92 spin_unlock_irqrestore(&imp->imp_lock, flags);
94 if (level != LUSTRE_IMP_FULL) {
96 "not pinging %s (in recovery)\n",
97 imp->imp_target_uuid.uuid);
101 req = ptlrpc_prep_req(imp, OBD_PING, 0, NULL,
104 CERROR("OOM trying to ping\n");
107 req->rq_no_resend = 1;
108 req->rq_replen = lustre_msg_size(0, NULL);
109 req->rq_send_state = LUSTRE_IMP_FULL;
110 req->rq_phase = RQ_PHASE_RPC;
111 req->rq_import_generation = generation;
112 ptlrpc_set_add_req(set, req);
114 CDEBUG(D_HA, "don't need to ping %s (%lu > "
115 "%lu)\n", imp->imp_target_uuid.uuid,
116 imp->imp_next_ping, this_ping);
121 /* Might be empty, that's OK. */
122 if (set->set_remaining == 0)
123 CDEBUG(D_HA, "nothing to ping\n");
124 list_for_each(iter, &set->set_requests) {
125 struct ptlrpc_request *req =
126 list_entry(iter, struct ptlrpc_request,
128 DEBUG_REQ(D_HA, req, "pinging %s->%s",
129 req->rq_import->imp_obd->obd_uuid.uuid,
130 req->rq_import->imp_target_uuid.uuid);
131 (void)ptl_send_rpc(req);
134 /* Have to wait on both the thread's queue and the set's. */
135 init_waitqueue_entry(&set_wait, current);
136 add_wait_queue(&set->set_waitq, &set_wait);
137 rc = l_wait_event(thread->t_ctl_waitq,
138 thread->t_flags & SVC_STOPPING ||
139 ptlrpc_check_set(set),
141 remove_wait_queue(&set->set_waitq, &set_wait);
142 CDEBUG(D_HA, "ping complete (%lu)\n", jiffies);
144 if (thread->t_flags & SVC_STOPPING) {
145 thread->t_flags &= ~SVC_STOPPING;
146 list_for_each(iter, &set->set_requests) {
147 req = list_entry(iter, struct ptlrpc_request,
149 if (!req->rq_replied)
150 ptlrpc_unregister_reply(req);
152 ptlrpc_set_destroy(set);
157 /* Expire all the requests that didn't come back. */
159 list_for_each(iter, &set->set_requests) {
160 req = list_entry(iter, struct ptlrpc_request,
166 req->rq_phase = RQ_PHASE_COMPLETE;
167 set->set_remaining--;
168 /* If it was disconnected, don't sweat it. */
169 if (list_empty(&req->rq_import->imp_pinger_chain)) {
170 ptlrpc_unregister_reply(req);
174 ptlrpc_expire_one_request(req);
177 ptlrpc_set_destroy(set);
179 /* Wait until the next ping time, or until we're stopped. */
180 time_to_next_ping = this_ping + (obd_timeout * HZ) - jiffies;
181 CDEBUG(D_HA, "next ping in %lu (%lu)\n", time_to_next_ping,
182 this_ping + (obd_timeout * HZ));
183 if (time_to_next_ping > 0) {
184 lwi = LWI_TIMEOUT(time_to_next_ping, NULL, NULL);
185 l_wait_event(thread->t_ctl_waitq,
186 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
188 if (thread->t_flags & SVC_STOPPING) {
189 thread->t_flags &= ~SVC_STOPPING;
192 } else if (thread->t_flags & SVC_EVENT) {
193 /* woken after adding import to reset timer */
194 thread->t_flags &= ~SVC_EVENT;
199 thread->t_flags = SVC_STOPPED;
200 wake_up(&thread->t_ctl_waitq);
202 CDEBUG(D_NET, "pinger thread exiting, process %d\n", current->pid);
206 int ptlrpc_start_pinger(void)
208 struct l_wait_info lwi = { 0 };
209 struct ptlrpc_svc_data d;
211 #ifndef ENABLE_PINGER
216 if (pinger_thread != NULL)
219 OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
220 if (pinger_thread == NULL)
222 init_waitqueue_head(&pinger_thread->t_ctl_waitq);
225 d.thread = pinger_thread;
227 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
228 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
229 rc = kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
231 CERROR("cannot start thread: %d\n", rc);
232 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
235 l_wait_event(pinger_thread->t_ctl_waitq,
236 pinger_thread->t_flags & SVC_RUNNING, &lwi);
241 int ptlrpc_stop_pinger(void)
243 struct l_wait_info lwi = { 0 };
245 #ifndef ENABLE_PINGER
250 if (pinger_thread == NULL)
253 pinger_thread->t_flags = SVC_STOPPING;
254 wake_up(&pinger_thread->t_ctl_waitq);
257 l_wait_event(pinger_thread->t_ctl_waitq,
258 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
260 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
261 pinger_thread = NULL;
265 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
268 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
272 int ptlrpc_pinger_add_import(struct obd_import *imp)
275 if (!list_empty(&imp->imp_pinger_chain))
279 CDEBUG(D_HA, "adding pingable import %s->%s\n",
280 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
281 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
282 /* XXX sort, blah blah */
283 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
284 class_import_get(imp);
287 pinger_thread->t_flags |= SVC_EVENT;
288 wake_up(&pinger_thread->t_ctl_waitq);
295 int ptlrpc_pinger_del_import(struct obd_import *imp)
298 if (list_empty(&imp->imp_pinger_chain))
302 list_del_init(&imp->imp_pinger_chain);
303 CDEBUG(D_HA, "removing pingable import %s->%s\n",
304 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
305 class_import_put(imp);
312 * the current implementation of pinger in liblustre is not optimized
315 static struct pinger_data {
317 unsigned long pd_this_ping;
318 unsigned long pd_next_ping;
319 struct ptlrpc_request_set *pd_set;
322 static int pinger_check_rpcs(void *arg)
324 unsigned long curtime = time(NULL);
325 struct ptlrpc_request *req;
326 struct ptlrpc_request_set *set;
327 struct list_head *iter;
328 struct pinger_data *pd = &pinger_args;
331 /* prevent recursion */
332 if (pd->pd_recursion++) {
333 CDEBUG(D_HA, "pinger: recursion! quit\n");
339 /* have we reached ping point? */
340 if (!pd->pd_set && pd->pd_next_ping > curtime) {
345 /* if we have rpc_set already, continue processing it */
347 LASSERT(pd->pd_this_ping);
352 pd->pd_this_ping = curtime;
353 pd->pd_set = ptlrpc_prep_set();
356 /* add rpcs into set */
358 list_for_each(iter, &pinger_imports) {
359 struct obd_import *imp =
360 list_entry(iter, struct obd_import,
362 int generation, level;
365 if (imp->imp_next_ping <= pd->pd_this_ping) {
367 spin_lock_irqsave(&imp->imp_lock, flags);
368 generation = imp->imp_generation;
369 level = imp->imp_state;
370 spin_unlock_irqrestore(&imp->imp_lock, flags);
372 if (level != LUSTRE_IMP_FULL) {
374 "not pinging %s (in recovery)\n",
375 imp->imp_target_uuid.uuid);
379 req = ptlrpc_prep_req(imp, OBD_PING, 0, NULL,
382 CERROR("out of memory\n");
385 req->rq_no_resend = 1;
386 req->rq_replen = lustre_msg_size(0, NULL);
387 req->rq_send_state = LUSTRE_IMP_FULL;
388 req->rq_phase = RQ_PHASE_RPC;
389 req->rq_import_generation = generation;
390 ptlrpc_set_add_req(set, req);
392 CDEBUG(D_HA, "don't need to ping %s (%lu > "
393 "%lu)\n", imp->imp_target_uuid.uuid,
394 imp->imp_next_ping, pd->pd_this_ping);
397 pd->pd_this_ping = curtime;
400 /* Might be empty, that's OK. */
401 if (set->set_remaining == 0)
402 CDEBUG(D_HA, "nothing to ping\n");
404 list_for_each(iter, &set->set_requests) {
405 struct ptlrpc_request *req =
406 list_entry(iter, struct ptlrpc_request,
408 DEBUG_REQ(D_HA, req, "pinging %s->%s",
409 req->rq_import->imp_obd->obd_uuid.uuid,
410 req->rq_import->imp_target_uuid.uuid);
411 (void)ptl_send_rpc(req);
415 rc = ptlrpc_check_set(set);
417 /* not finished, and we are not expired, simply return */
418 if (!rc && curtime < pd->pd_this_ping + obd_timeout) {
419 CDEBUG(D_HA, "not finished, but also not expired\n");
424 /* Expire all the requests that didn't come back. */
426 list_for_each(iter, &set->set_requests) {
427 req = list_entry(iter, struct ptlrpc_request,
433 req->rq_phase = RQ_PHASE_COMPLETE;
434 set->set_remaining--;
435 /* If it was disconnected, don't sweat it. */
436 if (list_empty(&req->rq_import->imp_pinger_chain)) {
437 ptlrpc_unregister_reply(req);
441 CDEBUG(D_HA, "pinger initiate expire_one_request\n");
442 ptlrpc_expire_one_request(req);
446 ptlrpc_set_destroy(set);
449 pd->pd_next_ping = pd->pd_this_ping + obd_timeout;
450 pd->pd_this_ping = 0; /* XXX for debug */
452 CDEBUG(D_HA, "finished a round ping\n");
457 static void *pinger_callback = NULL;
459 int ptlrpc_start_pinger(void)
461 memset(&pinger_args, 0, sizeof(pinger_args));
464 liblustre_register_wait_callback(&pinger_check_rpcs, &pinger_args);
470 int ptlrpc_stop_pinger(void)
474 liblustre_deregister_wait_callback(pinger_callback);
479 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
482 imp->imp_next_ping = time(NULL) + obd_timeout;
483 if (pinger_args.pd_set == NULL &&
484 pinger_args.pd_next_ping > imp->imp_next_ping) {
485 CDEBUG(D_HA, "set next ping to %ld(cur %ld)\n",
486 imp->imp_next_ping, time(NULL));
487 pinger_args.pd_next_ping = imp->imp_next_ping;
492 int ptlrpc_pinger_add_import(struct obd_import *imp)
495 if (!list_empty(&imp->imp_pinger_chain))
498 CDEBUG(D_HA, "adding pingable import %s->%s\n",
499 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
500 ptlrpc_pinger_sending_on_import(imp);
503 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
504 class_import_get(imp);
510 int ptlrpc_pinger_del_import(struct obd_import *imp)
513 if (list_empty(&imp->imp_pinger_chain))
517 list_del_init(&imp->imp_pinger_chain);
518 CDEBUG(D_HA, "removing pingable import %s->%s\n",
519 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
520 class_import_put(imp);
525 #endif /* !__KERNEL__ */