Whamcloud - gitweb
- ptlrpc_ping_interpret is needless:
[fs/lustre-release.git] / lustre / ptlrpc / pinger.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Portal-RPC reconnection and replay operations, for use in recovery.
5  *
6  *  Copyright (c) 2003 Cluster File Systems, Inc.
7  *   Authors: Phil Schwan <phil@clusterfs.com>
8  *            Mike Shaver <shaver@clusterfs.com>
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
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.
15  *
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.
20  *
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.
24  */
25
26 #ifndef __KERNEL__
27 #include <liblustre.h>
28 #else
29 #include <linux/version.h>
30 #include <asm/semaphore.h>
31 #define DEBUG_SUBSYSTEM S_RPC
32 #endif
33
34 #include <linux/obd_support.h>
35 #include <linux/obd_class.h>
36 #include "ptlrpc_internal.h"
37
38 #define PINGER_RATE     3 /* how many pings we'll do in obd_timeout period */
39
40 static DECLARE_MUTEX(pinger_sem);
41 static struct list_head pinger_imports = LIST_HEAD_INIT(pinger_imports);
42
43 int ptlrpc_ping(struct obd_import *imp) 
44 {
45         struct ptlrpc_request *req;
46         int rc = 0;
47         ENTRY;
48
49         req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING, 0, NULL, NULL);
50         if (req) {
51                 DEBUG_REQ(D_HA, req, "pinging %s->%s",
52                           imp->imp_obd->obd_uuid.uuid,
53                           imp->imp_target_uuid.uuid);
54                 req->rq_no_resend = req->rq_no_delay = 1;
55                 req->rq_replen = lustre_msg_size(0, NULL);
56                 req->rq_timeout = obd_timeout / PINGER_RATE;
57                 ptlrpcd_add_req(req);
58         } else {
59                 CERROR("OOM trying to ping %s->%s\n",
60                        imp->imp_obd->obd_uuid.uuid,
61                        imp->imp_target_uuid.uuid);
62                 rc = -ENOMEM;
63         }
64
65         RETURN(rc);
66 }
67
68 #ifdef __KERNEL__
69 static inline int ptlrpc_next_ping(struct obd_import *imp)
70 {
71         return jiffies + (obd_timeout / PINGER_RATE * HZ);
72 }
73
74 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
75 {
76         if (imp->imp_server_timeout)
77                 return jiffies + (obd_timeout / 2 * HZ);
78         else
79                 return jiffies + (obd_timeout * HZ);
80 }
81
82 static atomic_t suspend_timeouts = ATOMIC_INIT(0);
83 static wait_queue_head_t suspend_timeouts_waitq;
84
85 void ptlrpc_deactivate_timeouts(void)
86 {
87         CDEBUG(D_HA, "deactivate timeouts\n");
88         atomic_inc(&suspend_timeouts);
89 }
90                 
91 void ptlrpc_activate_timeouts(void)
92 {
93         CDEBUG(D_HA, "activate timeouts\n");
94         LASSERT(atomic_read(&suspend_timeouts) > 0);
95         if (atomic_dec_and_test(&suspend_timeouts))
96                 wake_up(&suspend_timeouts_waitq);
97 }
98
99 int ptlrpc_check_suspend(void)
100 {
101         if (atomic_read(&suspend_timeouts))
102                 return 1;
103         return 0;
104 }
105
106 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req)
107 {
108         struct l_wait_info lwi;
109
110         if (atomic_read(&suspend_timeouts)) {
111                 DEBUG_REQ(D_NET, req, "-- suspend %d regular timeout",
112                           atomic_read(&suspend_timeouts));
113                 lwi = LWI_INTR(NULL, NULL);
114                 l_wait_event(suspend_timeouts_waitq,
115                              atomic_read(&suspend_timeouts) == 0, &lwi);
116                 DEBUG_REQ(D_NET, req, "-- recharge regular timeout");
117                 return 1;
118         }
119         return 0;
120 }
121
122 static void ptlrpc_pinger_process_import(struct obd_import *imp,
123                                          unsigned long this_ping)
124 {
125         unsigned long flags;
126         int force, level;
127
128         spin_lock_irqsave(&imp->imp_lock, flags);
129         level = imp->imp_state;
130         force = imp->imp_force_verify;
131         if (force)
132                 imp->imp_force_verify = 0;
133         spin_unlock_irqrestore(&imp->imp_lock, flags);
134
135         if (imp->imp_next_ping > this_ping && force == 0)
136                 return;
137
138         if (level == LUSTRE_IMP_DISCON && !imp->imp_deactive) {
139                 /* wait at least a timeout before trying recovery again */
140                 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
141                 ptlrpc_initiate_recovery(imp);
142         } else if (level != LUSTRE_IMP_FULL || imp->imp_obd->obd_no_recov) {
143                 CDEBUG(D_HA, "not pinging %s (in recovery "
144                        " or recovery disabled: %s)\n",
145                        imp->imp_target_uuid.uuid,
146                        ptlrpc_import_state_name(level));
147         } else if (imp->imp_pingable || force) {
148                 imp->imp_next_ping = ptlrpc_next_ping(imp);
149                 ptlrpc_ping(imp);
150         }
151 }
152
153 static int ptlrpc_pinger_main(void *arg)
154 {
155         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
156         struct ptlrpc_thread *thread = data->thread;
157         unsigned long flags;
158         ENTRY;
159
160         lock_kernel();
161         ptlrpc_daemonize();
162
163         SIGNAL_MASK_LOCK(current, flags);
164         sigfillset(&current->blocked);
165         RECALC_SIGPENDING;
166         SIGNAL_MASK_UNLOCK(current, flags);
167
168         LASSERTF(strlen(data->name) < sizeof(current->comm),
169                  "name %d > len %d\n",
170                  (int)strlen(data->name), (int)sizeof(current->comm));
171         THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s", data->name);
172         unlock_kernel();
173
174         /* Record that the thread is running */
175         thread->t_flags = SVC_RUNNING;
176         wake_up(&thread->t_ctl_waitq);
177
178         /* And now, loop forever, pinging as needed. */
179         while (1) {
180                 unsigned long this_ping = jiffies;
181                 long time_to_next_ping;
182                 struct l_wait_info lwi = LWI_TIMEOUT(obd_timeout * HZ,
183                                                      NULL, NULL);
184                 struct list_head *iter;
185
186                 time_to_next_ping = this_ping + (obd_timeout * HZ) - jiffies;
187                 down(&pinger_sem);
188                 list_for_each(iter, &pinger_imports) {
189                         struct obd_import *imp =
190                                 list_entry(iter, struct obd_import,
191                                            imp_pinger_chain);
192
193                         ptlrpc_pinger_process_import(imp, this_ping);
194
195                         CDEBUG(D_OTHER, "%s: pingable %d, next_ping %lu(%lu)\n",
196                                 imp->imp_target_uuid.uuid,
197                                 imp->imp_pingable, imp->imp_next_ping, jiffies);
198
199                         if (imp->imp_pingable && imp->imp_next_ping &&
200                             imp->imp_next_ping - jiffies < time_to_next_ping &&
201                             imp->imp_next_ping > jiffies)
202                                 time_to_next_ping = imp->imp_next_ping - jiffies;
203                 }
204                 up(&pinger_sem);
205
206                 /* Wait until the next ping time, or until we're stopped. */
207                 CDEBUG(D_HA, "next ping in %lu (%lu)\n", time_to_next_ping,
208                        this_ping + (obd_timeout * HZ));
209                 if (time_to_next_ping > 0) {
210                         lwi = LWI_TIMEOUT(time_to_next_ping, NULL, NULL);
211                         l_wait_event(thread->t_ctl_waitq,
212                                      thread->t_flags & (SVC_STOPPING|SVC_EVENT),
213                                      &lwi);
214                         if (thread->t_flags & SVC_STOPPING) {
215                                 thread->t_flags &= ~SVC_STOPPING;
216                                 EXIT;
217                                 break;
218                         } else if (thread->t_flags & SVC_EVENT) {
219                                 /* woken after adding import to reset timer */
220                                 thread->t_flags &= ~SVC_EVENT;
221                         }
222                 }
223         }
224
225         thread->t_flags = SVC_STOPPED;
226         wake_up(&thread->t_ctl_waitq);
227
228         CDEBUG(D_NET, "pinger thread exiting, process %d\n", current->pid);
229         return 0;
230 }
231
232 static struct ptlrpc_thread *pinger_thread = NULL;
233
234 int ptlrpc_start_pinger(void)
235 {
236         struct l_wait_info lwi = { 0 };
237         struct ptlrpc_svc_data d;
238         int rc;
239 #ifndef ENABLE_PINGER
240         return 0;
241 #endif
242         ENTRY;
243
244         LASSERT(obd_timeout > PINGER_RATE);
245
246         if (pinger_thread != NULL)
247                 RETURN(-EALREADY);
248
249         OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
250         if (pinger_thread == NULL)
251                 RETURN(-ENOMEM);
252         init_waitqueue_head(&pinger_thread->t_ctl_waitq);
253         init_waitqueue_head(&suspend_timeouts_waitq);
254
255         d.name = "ll_ping";
256         d.thread = pinger_thread;
257
258         /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
259          * just drop the VM and FILES in ptlrpc_daemonize() right away. */
260         rc = kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
261         if (rc < 0) {
262                 CERROR("cannot start thread: %d\n", rc);
263                 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
264                 RETURN(rc);
265         }
266         l_wait_event(pinger_thread->t_ctl_waitq,
267                      pinger_thread->t_flags & SVC_RUNNING, &lwi);
268
269         RETURN(rc);
270 }
271
272 int ptlrpc_stop_pinger(void)
273 {
274         struct l_wait_info lwi = { 0 };
275         int rc = 0;
276 #ifndef ENABLE_PINGER
277         return 0;
278 #endif
279         ENTRY;
280
281         if (pinger_thread == NULL)
282                 RETURN(-EALREADY);
283         down(&pinger_sem);
284         pinger_thread->t_flags = SVC_STOPPING;
285         wake_up(&pinger_thread->t_ctl_waitq);
286         up(&pinger_sem);
287
288         l_wait_event(pinger_thread->t_ctl_waitq,
289                      (pinger_thread->t_flags & SVC_STOPPED), &lwi);
290
291         OBD_FREE(pinger_thread, sizeof(*pinger_thread));
292         pinger_thread = NULL;
293         RETURN(rc);
294 }
295
296 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
297 {
298         down(&pinger_sem);
299         imp->imp_next_ping = ptlrpc_next_ping(imp);
300         up(&pinger_sem);
301 }
302
303 int ptlrpc_pinger_add_import(struct obd_import *imp)
304 {
305         ENTRY;
306         if (!list_empty(&imp->imp_pinger_chain))
307                 RETURN(-EALREADY);
308
309         down(&pinger_sem);
310         CDEBUG(D_HA, "adding pingable import %s->%s\n",
311                imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
312         imp->imp_next_ping = jiffies + (obd_timeout * HZ);
313         /* XXX sort, blah blah */
314         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
315         class_import_get(imp);
316
317         ptlrpc_pinger_wake_up();
318         up(&pinger_sem);
319
320         RETURN(0);
321 }
322
323 int ptlrpc_pinger_del_import(struct obd_import *imp)
324 {
325         ENTRY;
326         if (list_empty(&imp->imp_pinger_chain))
327                 RETURN(-ENOENT);
328
329         down(&pinger_sem);
330         list_del_init(&imp->imp_pinger_chain);
331         CDEBUG(D_HA, "removing pingable import %s->%s\n",
332                imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
333         class_import_put(imp);
334         up(&pinger_sem);
335         RETURN(0);
336 }
337
338 void ptlrpc_pinger_wake_up()
339 {
340 #ifdef ENABLE_PINGER
341         pinger_thread->t_flags |= SVC_EVENT;
342         wake_up(&pinger_thread->t_ctl_waitq);
343 #endif
344 }
345
346 #else /* !__KERNEL__ */
347
348 /* XXX
349  * the current implementation of pinger in liblustre is not optimized
350  */
351
352 static struct pinger_data {
353         int             pd_recursion;
354         unsigned long   pd_this_ping;
355         unsigned long   pd_next_ping;
356         int             pd_force_check;
357 } pinger_args;
358
359 static int pinger_check_rpcs(void *arg)
360 {
361         unsigned long curtime = time(NULL);
362         struct list_head *iter;
363         struct pinger_data *pd = &pinger_args;
364
365         /* prevent recursion */
366         if (pd->pd_recursion++) {
367                 CDEBUG(D_HA, "pinger: recursion! quit\n");
368                 pd->pd_recursion--;
369                 return 0;
370         }
371
372         /* have we reached ping point? */
373         if (pd->pd_next_ping > curtime && !pd->pd_force_check) {
374                 pd->pd_recursion--;
375                 return 0;
376         }
377
378         if (pd->pd_force_check)
379                 pd->pd_force_check = 0;
380
381         pd->pd_this_ping = curtime;
382
383         /* add rpcs into set */
384         down(&pinger_sem);
385         list_for_each(iter, &pinger_imports) {
386                 struct obd_import *imp =
387                         list_entry(iter, struct obd_import,
388                                    imp_pinger_chain);
389                 int level, force;
390                 unsigned long flags;
391
392
393                 spin_lock_irqsave(&imp->imp_lock, flags);
394                 level = imp->imp_state;
395                 force = imp->imp_force_verify;
396                 if (force)
397                         imp->imp_force_verify = 0;
398                 spin_unlock_irqrestore(&imp->imp_lock, flags);
399
400                 if (imp->imp_next_ping <= pd->pd_this_ping || force) {
401                         if (level == LUSTRE_IMP_DISCON) {
402                                 /* wait at least a timeout before 
403                                    trying recovery again. */
404                                 unsigned long timeout = obd_timeout;
405                                 if (imp->imp_server_timeout)
406                                         timeout = obd_timeout / 2;
407                                 imp->imp_next_ping = time(NULL) + 
408                                         (timeout * HZ);
409                                 ptlrpc_initiate_recovery(imp);
410                         } 
411                         else if (level != LUSTRE_IMP_FULL ||
412                                  imp->imp_obd->obd_no_recov) {
413                                 CDEBUG(D_HA, 
414                                        "not pinging %s (in recovery "
415                                        " or recovery disabled: %s)\n",
416                                        imp->imp_target_uuid.uuid,
417                                        ptlrpc_import_state_name(level));
418                         } 
419                         else if (imp->imp_pingable || force) {
420                                 ptlrpc_ping(imp);
421                         }
422
423                 } else {
424                         if (imp->imp_pingable) {
425                                 CDEBUG(D_HA, "don't need to ping %s "
426                                        "(%lu > %lu)\n", 
427                                        imp->imp_target_uuid.uuid,
428                                        imp->imp_next_ping, pd->pd_this_ping);
429                         }
430                 }
431         }
432
433         up(&pinger_sem);
434
435         pd->pd_next_ping = pd->pd_this_ping + (obd_timeout * HZ);
436
437         CDEBUG(D_HA, "finished a round ping\n");
438         pd->pd_recursion--;
439         return 0;
440 }
441
442 static void *pinger_callback = NULL;
443
444 int ptlrpc_start_pinger(void)
445 {
446         memset(&pinger_args, 0, sizeof(pinger_args));
447 #ifdef ENABLE_PINGER
448         pinger_callback =
449                 liblustre_register_wait_callback(&pinger_check_rpcs, &pinger_args);
450 #endif
451         return 0;
452 }
453
454 int ptlrpc_stop_pinger(void)
455 {
456 #ifdef ENABLE_PINGER
457         if (pinger_callback)
458                 liblustre_deregister_wait_callback(pinger_callback);
459 #endif
460         return 0;
461 }
462
463 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
464 {
465         down(&pinger_sem);
466         imp->imp_next_ping = time(NULL) + obd_timeout;
467         if (pinger_args.pd_next_ping > imp->imp_next_ping) {
468                 CDEBUG(D_HA, "set next ping to %ld(cur %ld)\n",
469                         imp->imp_next_ping, time(NULL));
470                 pinger_args.pd_next_ping = imp->imp_next_ping;
471         }
472         up(&pinger_sem);
473 }
474
475 int ptlrpc_pinger_add_import(struct obd_import *imp)
476 {
477         ENTRY;
478         if (!list_empty(&imp->imp_pinger_chain))
479                 RETURN(-EALREADY);
480
481         CDEBUG(D_HA, "adding pingable import %s->%s\n",
482                imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
483         ptlrpc_pinger_sending_on_import(imp);
484
485         down(&pinger_sem);
486         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
487         class_import_get(imp);
488         up(&pinger_sem);
489
490         RETURN(0);
491 }
492
493 int ptlrpc_pinger_del_import(struct obd_import *imp)
494 {
495         ENTRY;
496         if (list_empty(&imp->imp_pinger_chain))
497                 RETURN(-ENOENT);
498
499         down(&pinger_sem);
500         list_del_init(&imp->imp_pinger_chain);
501         CDEBUG(D_HA, "removing pingable import %s->%s\n",
502                imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
503         class_import_put(imp);
504         up(&pinger_sem);
505         RETURN(0);
506 }
507
508 void ptlrpc_pinger_wake_up()
509 {
510         pinger_args.pd_force_check = 1;
511 }
512 #endif /* !__KERNEL__ */