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