Whamcloud - gitweb
- take HZ into account
[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 > obd_timeout * HZ) {
136                 CWARN("wrong ping time %lu (current %lu)\n",
137                       imp->imp_next_ping, this_ping);
138                 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
139         }
140
141         if (imp->imp_next_ping > this_ping && force == 0)
142                 return;
143
144         if (level == LUSTRE_IMP_DISCON && !imp->imp_deactive) {
145                 /* wait at least a timeout before trying recovery again */
146                 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
147                 ptlrpc_initiate_recovery(imp);
148         } else if (level != LUSTRE_IMP_FULL || imp->imp_obd->obd_no_recov) {
149                 CDEBUG(D_HA, "not pinging %s (in recovery "
150                        " or recovery disabled: %s)\n",
151                        imp->imp_target_uuid.uuid,
152                        ptlrpc_import_state_name(level));
153         } else if (imp->imp_pingable || force) {
154                 imp->imp_next_ping = ptlrpc_next_ping(imp);
155                 ptlrpc_ping(imp);
156         }
157 }
158
159 static int ptlrpc_pinger_main(void *arg)
160 {
161         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
162         struct ptlrpc_thread *thread = data->thread;
163         unsigned long flags;
164         ENTRY;
165
166         lock_kernel();
167         ptlrpc_daemonize();
168
169         SIGNAL_MASK_LOCK(current, flags);
170         sigfillset(&current->blocked);
171         RECALC_SIGPENDING;
172         SIGNAL_MASK_UNLOCK(current, flags);
173
174         LASSERTF(strlen(data->name) < sizeof(current->comm),
175                  "name %d > len %d\n",
176                  (int)strlen(data->name), (int)sizeof(current->comm));
177         THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s", data->name);
178         unlock_kernel();
179
180         /* Record that the thread is running */
181         thread->t_flags = SVC_RUNNING;
182         wake_up(&thread->t_ctl_waitq);
183
184         /* And now, loop forever, pinging as needed. */
185         while (1) {
186                 unsigned long this_ping = jiffies;
187                 long time_to_next_ping;
188                 struct l_wait_info lwi = LWI_TIMEOUT(obd_timeout * HZ,
189                                                      NULL, NULL);
190                 struct list_head *iter;
191
192                 time_to_next_ping = this_ping + (obd_timeout * HZ) - jiffies;
193                 down(&pinger_sem);
194                 list_for_each(iter, &pinger_imports) {
195                         struct obd_import *imp =
196                                 list_entry(iter, struct obd_import,
197                                            imp_pinger_chain);
198
199                         ptlrpc_pinger_process_import(imp, this_ping);
200
201                         CDEBUG(D_OTHER, "%s: pingable %d, next_ping %lu(%lu)\n",
202                                 imp->imp_target_uuid.uuid,
203                                 imp->imp_pingable, imp->imp_next_ping, jiffies);
204
205                         if (imp->imp_pingable && imp->imp_next_ping &&
206                             imp->imp_next_ping - jiffies < time_to_next_ping &&
207                             imp->imp_next_ping > jiffies)
208                                 time_to_next_ping = imp->imp_next_ping - jiffies;
209                 }
210                 up(&pinger_sem);
211
212                 /* Wait until the next ping time, or until we're stopped. */
213                 CDEBUG(D_HA, "next ping in %lu (%lu)\n", time_to_next_ping,
214                        this_ping + (obd_timeout * HZ));
215                 if (time_to_next_ping > 0) {
216                         lwi = LWI_TIMEOUT(time_to_next_ping, NULL, NULL);
217                         l_wait_event(thread->t_ctl_waitq,
218                                      thread->t_flags & (SVC_STOPPING|SVC_EVENT),
219                                      &lwi);
220                         if (thread->t_flags & SVC_STOPPING) {
221                                 thread->t_flags &= ~SVC_STOPPING;
222                                 EXIT;
223                                 break;
224                         } else if (thread->t_flags & SVC_EVENT) {
225                                 /* woken after adding import to reset timer */
226                                 thread->t_flags &= ~SVC_EVENT;
227                         }
228                 }
229         }
230
231         thread->t_flags = SVC_STOPPED;
232         wake_up(&thread->t_ctl_waitq);
233
234         CDEBUG(D_NET, "pinger thread exiting, process %d\n", current->pid);
235         return 0;
236 }
237
238 static struct ptlrpc_thread *pinger_thread = NULL;
239
240 int ptlrpc_start_pinger(void)
241 {
242         struct l_wait_info lwi = { 0 };
243         struct ptlrpc_svc_data d;
244         int rc;
245 #ifndef ENABLE_PINGER
246         return 0;
247 #endif
248         ENTRY;
249
250         LASSERT(obd_timeout > PINGER_RATE);
251
252         if (pinger_thread != NULL)
253                 RETURN(-EALREADY);
254
255         OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
256         if (pinger_thread == NULL)
257                 RETURN(-ENOMEM);
258         init_waitqueue_head(&pinger_thread->t_ctl_waitq);
259         init_waitqueue_head(&suspend_timeouts_waitq);
260
261         d.name = "ll_ping";
262         d.thread = pinger_thread;
263
264         /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
265          * just drop the VM and FILES in ptlrpc_daemonize() right away. */
266         rc = kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
267         if (rc < 0) {
268                 CERROR("cannot start thread: %d\n", rc);
269                 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
270                 RETURN(rc);
271         }
272         l_wait_event(pinger_thread->t_ctl_waitq,
273                      pinger_thread->t_flags & SVC_RUNNING, &lwi);
274
275         RETURN(rc);
276 }
277
278 int ptlrpc_stop_pinger(void)
279 {
280         struct l_wait_info lwi = { 0 };
281         int rc = 0;
282 #ifndef ENABLE_PINGER
283         return 0;
284 #endif
285         ENTRY;
286
287         if (pinger_thread == NULL)
288                 RETURN(-EALREADY);
289         down(&pinger_sem);
290         pinger_thread->t_flags = SVC_STOPPING;
291         wake_up(&pinger_thread->t_ctl_waitq);
292         up(&pinger_sem);
293
294         l_wait_event(pinger_thread->t_ctl_waitq,
295                      (pinger_thread->t_flags & SVC_STOPPED), &lwi);
296
297         OBD_FREE(pinger_thread, sizeof(*pinger_thread));
298         pinger_thread = NULL;
299         RETURN(rc);
300 }
301
302 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
303 {
304         down(&pinger_sem);
305         imp->imp_next_ping = ptlrpc_next_ping(imp);
306         up(&pinger_sem);
307 }
308
309 int ptlrpc_pinger_add_import(struct obd_import *imp)
310 {
311         ENTRY;
312         if (!list_empty(&imp->imp_pinger_chain))
313                 RETURN(-EALREADY);
314
315         down(&pinger_sem);
316         CDEBUG(D_HA, "adding pingable import %s->%s\n",
317                imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
318         imp->imp_next_ping = jiffies + (obd_timeout * HZ);
319         /* XXX sort, blah blah */
320         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
321         class_import_get(imp);
322
323         ptlrpc_pinger_wake_up();
324         up(&pinger_sem);
325
326         RETURN(0);
327 }
328
329 int ptlrpc_pinger_del_import(struct obd_import *imp)
330 {
331         ENTRY;
332         if (list_empty(&imp->imp_pinger_chain))
333                 RETURN(-ENOENT);
334
335         down(&pinger_sem);
336         list_del_init(&imp->imp_pinger_chain);
337         CDEBUG(D_HA, "removing pingable import %s->%s\n",
338                imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
339         class_import_put(imp);
340         up(&pinger_sem);
341         RETURN(0);
342 }
343
344 void ptlrpc_pinger_wake_up()
345 {
346 #ifdef ENABLE_PINGER
347         pinger_thread->t_flags |= SVC_EVENT;
348         wake_up(&pinger_thread->t_ctl_waitq);
349 #endif
350 }
351
352 #else /* !__KERNEL__ */
353
354 /* XXX
355  * the current implementation of pinger in liblustre is not optimized
356  */
357
358 static struct pinger_data {
359         int             pd_recursion;
360         unsigned long   pd_this_ping;
361         unsigned long   pd_next_ping;
362         int             pd_force_check;
363 } pinger_args;
364
365 static int pinger_check_rpcs(void *arg)
366 {
367         unsigned long curtime = time(NULL);
368         struct list_head *iter;
369         struct pinger_data *pd = &pinger_args;
370
371         /* prevent recursion */
372         if (pd->pd_recursion++) {
373                 CDEBUG(D_HA, "pinger: recursion! quit\n");
374                 pd->pd_recursion--;
375                 return 0;
376         }
377
378         /* have we reached ping point? */
379         if (pd->pd_next_ping > curtime && !pd->pd_force_check) {
380                 pd->pd_recursion--;
381                 return 0;
382         }
383
384         if (pd->pd_force_check)
385                 pd->pd_force_check = 0;
386
387         pd->pd_this_ping = curtime;
388
389         /* add rpcs into set */
390         down(&pinger_sem);
391         list_for_each(iter, &pinger_imports) {
392                 struct obd_import *imp =
393                         list_entry(iter, struct obd_import,
394                                    imp_pinger_chain);
395                 int level, force;
396                 unsigned long flags;
397
398
399                 spin_lock_irqsave(&imp->imp_lock, flags);
400                 level = imp->imp_state;
401                 force = imp->imp_force_verify;
402                 if (force)
403                         imp->imp_force_verify = 0;
404                 spin_unlock_irqrestore(&imp->imp_lock, flags);
405
406                 if (imp->imp_next_ping <= pd->pd_this_ping || force) {
407                         if (level == LUSTRE_IMP_DISCON) {
408                                 /* wait at least a timeout before 
409                                    trying recovery again. */
410                                 unsigned long timeout = obd_timeout;
411                                 if (imp->imp_server_timeout)
412                                         timeout = obd_timeout / 2;
413                                 imp->imp_next_ping = time(NULL) + 
414                                         (timeout * HZ);
415                                 ptlrpc_initiate_recovery(imp);
416                         } 
417                         else if (level != LUSTRE_IMP_FULL ||
418                                  imp->imp_obd->obd_no_recov) {
419                                 CDEBUG(D_HA, 
420                                        "not pinging %s (in recovery "
421                                        " or recovery disabled: %s)\n",
422                                        imp->imp_target_uuid.uuid,
423                                        ptlrpc_import_state_name(level));
424                         } 
425                         else if (imp->imp_pingable || force) {
426                                 ptlrpc_ping(imp);
427                         }
428
429                 } else {
430                         if (imp->imp_pingable) {
431                                 CDEBUG(D_HA, "don't need to ping %s "
432                                        "(%lu > %lu)\n", 
433                                        imp->imp_target_uuid.uuid,
434                                        imp->imp_next_ping, pd->pd_this_ping);
435                         }
436                 }
437         }
438
439         up(&pinger_sem);
440
441         pd->pd_next_ping = pd->pd_this_ping + (obd_timeout * HZ);
442
443         CDEBUG(D_HA, "finished a round ping\n");
444         pd->pd_recursion--;
445         return 0;
446 }
447
448 static void *pinger_callback = NULL;
449
450 int ptlrpc_start_pinger(void)
451 {
452         memset(&pinger_args, 0, sizeof(pinger_args));
453 #ifdef ENABLE_PINGER
454         pinger_callback =
455                 liblustre_register_wait_callback(&pinger_check_rpcs, &pinger_args);
456 #endif
457         return 0;
458 }
459
460 int ptlrpc_stop_pinger(void)
461 {
462 #ifdef ENABLE_PINGER
463         if (pinger_callback)
464                 liblustre_deregister_wait_callback(pinger_callback);
465 #endif
466         return 0;
467 }
468
469 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
470 {
471         down(&pinger_sem);
472         imp->imp_next_ping = time(NULL) + obd_timeout;
473         if (pinger_args.pd_next_ping > imp->imp_next_ping) {
474                 CDEBUG(D_HA, "set next ping to %ld(cur %ld)\n",
475                         imp->imp_next_ping, time(NULL));
476                 pinger_args.pd_next_ping = imp->imp_next_ping;
477         }
478         up(&pinger_sem);
479 }
480
481 int ptlrpc_pinger_add_import(struct obd_import *imp)
482 {
483         ENTRY;
484         if (!list_empty(&imp->imp_pinger_chain))
485                 RETURN(-EALREADY);
486
487         CDEBUG(D_HA, "adding pingable import %s->%s\n",
488                imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
489         ptlrpc_pinger_sending_on_import(imp);
490
491         down(&pinger_sem);
492         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
493         class_import_get(imp);
494         up(&pinger_sem);
495
496         RETURN(0);
497 }
498
499 int ptlrpc_pinger_del_import(struct obd_import *imp)
500 {
501         ENTRY;
502         if (list_empty(&imp->imp_pinger_chain))
503                 RETURN(-ENOENT);
504
505         down(&pinger_sem);
506         list_del_init(&imp->imp_pinger_chain);
507         CDEBUG(D_HA, "removing pingable import %s->%s\n",
508                imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
509         class_import_put(imp);
510         up(&pinger_sem);
511         RETURN(0);
512 }
513
514 void ptlrpc_pinger_wake_up()
515 {
516         pinger_args.pd_force_check = 1;
517 }
518 #endif /* !__KERNEL__ */