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"
40 static struct ptlrpc_thread *pinger_thread = NULL;
41 static DECLARE_MUTEX(pinger_sem);
42 static struct list_head pinger_imports = LIST_HEAD_INIT(pinger_imports);
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);
310 #else /* !__KERNEL__ */
312 int ptlrpc_start_pinger(void)
317 int ptlrpc_stop_pinger(void)
322 int ptlrpc_pinger_add_import(struct obd_import *imp)
327 int ptlrpc_pinger_del_import(struct obd_import *imp)
332 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)