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