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