Whamcloud - gitweb
land clio.
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/pinger.c
37  *
38  * Portal-RPC reconnection and replay operations, for use in recovery.
39  */
40
41 #ifndef __KERNEL__
42 #include <liblustre.h>
43 #else
44 #define DEBUG_SUBSYSTEM S_RPC
45 #endif
46
47 #include <obd_support.h>
48 #include <obd_class.h>
49 #include "ptlrpc_internal.h"
50
51 struct semaphore pinger_sem;
52 static CFS_LIST_HEAD(pinger_imports);
53
54 int ptlrpc_ping(struct obd_import *imp)
55 {
56         struct ptlrpc_request *req;
57         ENTRY;
58
59         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
60                                         OBD_PING);
61         if (req == NULL) {
62                 CERROR("OOM trying to ping %s->%s\n",
63                        imp->imp_obd->obd_uuid.uuid,
64                        obd2cli_tgt(imp->imp_obd));
65                 RETURN(-ENOMEM);
66         }
67
68         DEBUG_REQ(D_INFO, req, "pinging %s->%s",
69                   imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
70         req->rq_no_resend = req->rq_no_delay = 1;
71         ptlrpc_request_set_replen(req);
72         ptlrpcd_add_req(req, PSCOPE_OTHER);
73
74         RETURN(0);
75 }
76
77 void ptlrpc_update_next_ping(struct obd_import *imp)
78 {
79 #ifdef ENABLE_PINGER
80         int time = PING_INTERVAL;
81         if (imp->imp_state == LUSTRE_IMP_DISCON) {
82                 int dtime = max_t(int, CONNECTION_SWITCH_MIN,
83                                   AT_OFF ? 0 :
84                                   at_get(&imp->imp_at.iat_net_latency));
85                 time = min(time, dtime);
86         }
87         imp->imp_next_ping = cfs_time_shift(time);
88 #endif /* ENABLE_PINGER */
89 }
90
91 void ptlrpc_ping_import_soon(struct obd_import *imp)
92 {
93         imp->imp_next_ping = cfs_time_current();
94 }
95
96 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
97 {
98         if (imp->imp_server_timeout)
99                 return cfs_time_shift(obd_timeout / 2);
100         else
101                 return cfs_time_shift(obd_timeout);
102 }
103
104 static atomic_t suspend_timeouts = ATOMIC_INIT(0);
105 static cfs_time_t suspend_wakeup_time = 0;
106
107 #ifdef __KERNEL__
108 static wait_queue_head_t suspend_timeouts_waitq;
109 #endif
110
111 cfs_time_t ptlrpc_suspend_wakeup_time(void)
112 {
113         return suspend_wakeup_time;
114 }
115
116 void ptlrpc_deactivate_timeouts(struct obd_import *imp)
117 {
118         /*XXX: disabled for now, will be replaced by adaptive timeouts */
119 #if 0
120         if (imp->imp_no_timeout)
121                 return;
122         imp->imp_no_timeout = 1;
123         atomic_inc(&suspend_timeouts);
124         CDEBUG(D_HA|D_WARNING, "deactivate timeouts %u\n", atomic_read(&suspend_timeouts));
125 #endif
126 }
127
128 void ptlrpc_activate_timeouts(struct obd_import *imp)
129 {
130         /*XXX: disabled for now, will be replaced by adaptive timeouts */
131 #if 0
132         if (!imp->imp_no_timeout)
133                 return;
134         imp->imp_no_timeout = 0;
135         LASSERT(atomic_read(&suspend_timeouts) > 0);
136         if (atomic_dec_and_test(&suspend_timeouts)) {
137                 suspend_wakeup_time = cfs_time_current();
138                 wake_up(&suspend_timeouts_waitq);
139         }
140         CDEBUG(D_HA|D_WARNING, "activate timeouts %u\n", atomic_read(&suspend_timeouts));
141 #endif
142 }
143
144 int ptlrpc_check_suspend(void)
145 {
146         if (atomic_read(&suspend_timeouts))
147                 return 1;
148         return 0;
149 }
150
151 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req)
152 {
153         struct l_wait_info lwi;
154
155         if (atomic_read(&suspend_timeouts)) {
156                 DEBUG_REQ(D_NET, req, "-- suspend %d regular timeout",
157                           atomic_read(&suspend_timeouts));
158                 lwi = LWI_INTR(NULL, NULL);
159                 l_wait_event(suspend_timeouts_waitq,
160                              atomic_read(&suspend_timeouts) == 0, &lwi);
161                 DEBUG_REQ(D_NET, req, "-- recharge regular timeout");
162                 return 1;
163         }
164         return 0;
165 }
166
167 #ifdef __KERNEL__
168
169 static void ptlrpc_pinger_process_import(struct obd_import *imp,
170                                          unsigned long this_ping)
171 {
172         int force, level;
173
174         spin_lock(&imp->imp_lock);
175         level = imp->imp_state;
176         force = imp->imp_force_verify;
177         if (force)
178                 imp->imp_force_verify = 0;
179         spin_unlock(&imp->imp_lock);
180
181         CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA,
182                "level %s/%u force %u deactive %u pingable %u\n",
183                ptlrpc_import_state_name(level), level,
184                force, imp->imp_deactive, imp->imp_pingable);
185
186         if (cfs_time_aftereq(imp->imp_next_ping - 5 * CFS_TICK,
187                              this_ping) && force == 0)
188                 return;
189
190         if (level == LUSTRE_IMP_DISCON && !imp->imp_deactive) {
191                 /* wait at least a timeout before trying recovery again */
192                 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
193                 ptlrpc_initiate_recovery(imp);
194         } else if (level != LUSTRE_IMP_FULL ||
195                    imp->imp_obd->obd_no_recov ||
196                    imp->imp_deactive) {
197                 CDEBUG(D_HA, "not pinging %s (in recovery "
198                        " or recovery disabled: %s)\n",
199                        obd2cli_tgt(imp->imp_obd),
200                        ptlrpc_import_state_name(level));
201         } else if (imp->imp_pingable || force) {
202                 ptlrpc_ping(imp);
203         }
204 }
205
206 static int ptlrpc_pinger_main(void *arg)
207 {
208         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
209         struct ptlrpc_thread *thread = data->thread;
210         ENTRY;
211
212         cfs_daemonize(data->name);
213
214         /* Record that the thread is running */
215         thread->t_flags = SVC_RUNNING;
216         cfs_waitq_signal(&thread->t_ctl_waitq);
217
218         /* And now, loop forever, pinging as needed. */
219         while (1) {
220                 cfs_time_t this_ping = cfs_time_current();
221                 struct l_wait_info lwi;
222                 cfs_duration_t time_to_next_ping;
223                 struct list_head *iter;
224
225                 mutex_down(&pinger_sem);
226                 list_for_each(iter, &pinger_imports) {
227                         struct obd_import *imp =
228                                 list_entry(iter, struct obd_import,
229                                            imp_pinger_chain);
230
231                         ptlrpc_pinger_process_import(imp, this_ping);
232                         /* obd_timeout might have changed */
233                         if (imp->imp_pingable && imp->imp_next_ping &&
234                             cfs_time_after(imp->imp_next_ping,
235                                            cfs_time_add(this_ping,
236                                                         cfs_time_seconds(PING_INTERVAL))))
237                                 ptlrpc_update_next_ping(imp);
238                 }
239                 mutex_up(&pinger_sem);
240                 /* update memory usage info */
241                 obd_update_maxusage();
242
243                 /* Wait until the next ping time, or until we're stopped. */
244                 time_to_next_ping = cfs_time_sub(cfs_time_add(this_ping,
245                                                cfs_time_seconds(PING_INTERVAL)),
246                                                cfs_time_current());
247
248                 /* The ping sent by ptlrpc_send_rpc may get sent out
249                    say .01 second after this.
250                    ptlrpc_pinger_sending_on_import will then set the
251                    next ping time to next_ping + .01 sec, which means
252                    we will SKIP the next ping at next_ping, and the
253                    ping will get sent 2 timeouts from now!  Beware. */
254                 CDEBUG(D_INFO, "next ping in "CFS_DURATION_T" ("CFS_TIME_T")\n",
255                                time_to_next_ping,
256                                cfs_time_add(this_ping,
257                                             cfs_time_seconds(PING_INTERVAL)));
258                 if (time_to_next_ping > 0) {
259                         lwi = LWI_TIMEOUT(max_t(cfs_duration_t,
260                                                 time_to_next_ping,
261                                                 cfs_time_seconds(1)),
262                                           NULL, NULL);
263                         l_wait_event(thread->t_ctl_waitq,
264                                      thread->t_flags & (SVC_STOPPING|SVC_EVENT),
265                                      &lwi);
266                         if (thread->t_flags & SVC_STOPPING) {
267                                 thread->t_flags &= ~SVC_STOPPING;
268                                 EXIT;
269                                 break;
270                         } else if (thread->t_flags & SVC_EVENT) {
271                                 /* woken after adding import to reset timer */
272                                 thread->t_flags &= ~SVC_EVENT;
273                         }
274                 }
275         }
276
277         thread->t_flags = SVC_STOPPED;
278         cfs_waitq_signal(&thread->t_ctl_waitq);
279
280         CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
281         return 0;
282 }
283
284 static struct ptlrpc_thread *pinger_thread = NULL;
285
286 int ptlrpc_start_pinger(void)
287 {
288         struct l_wait_info lwi = { 0 };
289         struct ptlrpc_svc_data d;
290         int rc;
291 #ifndef ENABLE_PINGER
292         return 0;
293 #endif
294         ENTRY;
295
296         if (pinger_thread != NULL)
297                 RETURN(-EALREADY);
298
299         OBD_ALLOC_PTR(pinger_thread);
300         if (pinger_thread == NULL)
301                 RETURN(-ENOMEM);
302         cfs_waitq_init(&pinger_thread->t_ctl_waitq);
303         cfs_waitq_init(&suspend_timeouts_waitq);
304
305         d.name = "ll_ping";
306         d.thread = pinger_thread;
307
308         /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
309          * just drop the VM and FILES in ptlrpc_daemonize() right away. */
310         rc = cfs_kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
311         if (rc < 0) {
312                 CERROR("cannot start thread: %d\n", rc);
313                 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
314                 pinger_thread = NULL;
315                 RETURN(rc);
316         }
317         l_wait_event(pinger_thread->t_ctl_waitq,
318                      pinger_thread->t_flags & SVC_RUNNING, &lwi);
319
320         RETURN(0);
321 }
322
323 int ptlrpc_stop_pinger(void)
324 {
325         struct l_wait_info lwi = { 0 };
326         int rc = 0;
327 #ifndef ENABLE_PINGER
328         return 0;
329 #endif
330         ENTRY;
331
332         if (pinger_thread == NULL)
333                 RETURN(-EALREADY);
334         mutex_down(&pinger_sem);
335         pinger_thread->t_flags = SVC_STOPPING;
336         cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
337         mutex_up(&pinger_sem);
338
339         l_wait_event(pinger_thread->t_ctl_waitq,
340                      (pinger_thread->t_flags & SVC_STOPPED), &lwi);
341
342         OBD_FREE_PTR(pinger_thread);
343         pinger_thread = NULL;
344         RETURN(rc);
345 }
346
347 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
348 {
349         ptlrpc_update_next_ping(imp);
350 }
351
352 int ptlrpc_pinger_add_import(struct obd_import *imp)
353 {
354         ENTRY;
355         if (!list_empty(&imp->imp_pinger_chain))
356                 RETURN(-EALREADY);
357
358         mutex_down(&pinger_sem);
359         CDEBUG(D_HA, "adding pingable import %s->%s\n",
360                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
361         /* if we add to pinger we want recovery on this import */
362         imp->imp_obd->obd_no_recov = 0;
363         ptlrpc_update_next_ping(imp);
364         /* XXX sort, blah blah */
365         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
366         class_import_get(imp);
367
368         ptlrpc_pinger_wake_up();
369         mutex_up(&pinger_sem);
370
371         RETURN(0);
372 }
373
374 int ptlrpc_pinger_del_import(struct obd_import *imp)
375 {
376         ENTRY;
377         if (list_empty(&imp->imp_pinger_chain))
378                 RETURN(-ENOENT);
379
380         mutex_down(&pinger_sem);
381         list_del_init(&imp->imp_pinger_chain);
382         CDEBUG(D_HA, "removing pingable import %s->%s\n",
383                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
384         /* if we remove from pinger we don't want recovery on this import */
385         imp->imp_obd->obd_no_recov = 1;
386         class_import_put(imp);
387         mutex_up(&pinger_sem);
388         RETURN(0);
389 }
390
391 void ptlrpc_pinger_wake_up()
392 {
393 #ifdef ENABLE_PINGER
394         pinger_thread->t_flags |= SVC_EVENT;
395         cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
396 #endif
397 }
398
399 /* Ping evictor thread */
400 #define PET_READY     1
401 #define PET_TERMINATE 2
402
403 static int               pet_refcount = 0;
404 static int               pet_state;
405 static wait_queue_head_t pet_waitq;
406 static struct obd_export *pet_exp = NULL;
407 static spinlock_t        pet_lock = SPIN_LOCK_UNLOCKED;
408
409 int ping_evictor_wake(struct obd_export *exp)
410 {
411         spin_lock(&pet_lock);
412         if (pet_exp || (pet_state != PET_READY)) {
413                 /* eventually the new obd will call here again. */
414                 spin_unlock(&pet_lock);
415                 return 1;
416         }
417
418         /* We have to make sure the obd isn't destroyed between now and when
419          * the ping evictor runs.  We'll take a reference here, and drop it
420          * when we finish in the evictor.  We don't really care about this
421          * export in particular; we just need one to keep the obd alive. */
422         pet_exp = class_export_get(exp);
423         spin_unlock(&pet_lock);
424
425         wake_up(&pet_waitq);
426         return 0;
427 }
428
429 static int ping_evictor_main(void *arg)
430 {
431         struct obd_device *obd;
432         struct obd_export *exp;
433         struct l_wait_info lwi = { 0 };
434         time_t expire_time;
435         ENTRY;
436
437         ptlrpc_daemonize("ll_evictor");
438
439         CDEBUG(D_HA, "Starting Ping Evictor\n");
440         pet_exp = NULL;
441         pet_state = PET_READY;
442         while (1) {
443                 l_wait_event(pet_waitq, pet_exp ||
444                              (pet_state == PET_TERMINATE), &lwi);
445                 if (pet_state == PET_TERMINATE)
446                         break;
447
448                 /* we only get here if pet_exp != NULL, and the end of this
449                  * loop is the only place which sets it NULL again, so lock
450                  * is not strictly necessary. */
451                 spin_lock(&pet_lock);
452                 obd = pet_exp->exp_obd;
453                 spin_unlock(&pet_lock);
454
455                 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
456
457                 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
458                        obd->obd_name, expire_time);
459
460                 /* Exports can't be deleted out of the list while we hold
461                  * the obd lock (class_unlink_export), which means we can't
462                  * lose the last ref on the export.  If they've already been
463                  * removed from the list, we won't find them here. */
464                 spin_lock(&obd->obd_dev_lock);
465                 while (!list_empty(&obd->obd_exports_timed)) {
466                         exp = list_entry(obd->obd_exports_timed.next,
467                                          struct obd_export,exp_obd_chain_timed);
468                         if (expire_time > exp->exp_last_request_time) {
469                                 class_export_get(exp);
470                                 spin_unlock(&obd->obd_dev_lock);
471                                  LCONSOLE_WARN("%s: haven't heard from client %s"
472                                               " (at %s) in %ld seconds. I think"
473                                               " it's dead, and I am evicting"
474                                               " it. exp %p, cur %ld expire %ld"
475                                               " last %ld\n",
476                                               obd->obd_name,
477                                               obd_uuid2str(&exp->exp_client_uuid),
478                                               obd_export_nid2str(exp),
479                                               (long)(cfs_time_current_sec() -
480                                                      exp->exp_last_request_time),
481                                               exp, (long)cfs_time_current_sec(),
482                                               (long)expire_time,
483                                               (long)exp->exp_last_request_time);
484                                 CDEBUG(D_HA, "Last request was at %ld\n",
485                                        exp->exp_last_request_time);
486                                 class_fail_export(exp);
487                                 class_export_put(exp);
488                                 spin_lock(&obd->obd_dev_lock);
489                         } else {
490                                 /* List is sorted, so everyone below is ok */
491                                 break;
492                         }
493                 }
494                 spin_unlock(&obd->obd_dev_lock);
495
496                 class_export_put(pet_exp);
497
498                 spin_lock(&pet_lock);
499                 pet_exp = NULL;
500                 spin_unlock(&pet_lock);
501         }
502         CDEBUG(D_HA, "Exiting Ping Evictor\n");
503
504         RETURN(0);
505 }
506
507 void ping_evictor_start(void)
508 {
509         int rc;
510
511         if (++pet_refcount > 1)
512                 return;
513
514         init_waitqueue_head(&pet_waitq);
515
516         rc = cfs_kernel_thread(ping_evictor_main, NULL, CLONE_VM | CLONE_FILES);
517         if (rc < 0) {
518                 pet_refcount--;
519                 CERROR("Cannot start ping evictor thread: %d\n", rc);
520         }
521 }
522 EXPORT_SYMBOL(ping_evictor_start);
523
524 void ping_evictor_stop(void)
525 {
526         if (--pet_refcount > 0)
527                 return;
528
529         pet_state = PET_TERMINATE;
530         wake_up(&pet_waitq);
531 }
532 EXPORT_SYMBOL(ping_evictor_stop);
533 #else /* !__KERNEL__ */
534
535 /* XXX
536  * the current implementation of pinger in liblustre is not optimized
537  */
538
539 #ifdef ENABLE_PINGER
540 static struct pinger_data {
541         int             pd_recursion;
542         cfs_time_t      pd_this_ping;   /* jiffies */
543         cfs_time_t      pd_next_ping;   /* jiffies */
544         struct ptlrpc_request_set *pd_set;
545 } pinger_args;
546
547 static int pinger_check_rpcs(void *arg)
548 {
549         cfs_time_t curtime = cfs_time_current();
550         struct ptlrpc_request *req;
551         struct ptlrpc_request_set *set;
552         struct list_head *iter;
553         struct pinger_data *pd = &pinger_args;
554         int rc;
555
556         /* prevent recursion */
557         if (pd->pd_recursion++) {
558                 CDEBUG(D_HA, "pinger: recursion! quit\n");
559                 LASSERT(pd->pd_set);
560                 pd->pd_recursion--;
561                 return 0;
562         }
563
564         /* have we reached ping point? */
565         if (!pd->pd_set && time_before(curtime, pd->pd_next_ping)) {
566                 pd->pd_recursion--;
567                 return 0;
568         }
569
570         /* if we have rpc_set already, continue processing it */
571         if (pd->pd_set) {
572                 LASSERT(pd->pd_this_ping);
573                 set = pd->pd_set;
574                 goto do_check_set;
575         }
576
577         pd->pd_this_ping = curtime;
578         pd->pd_set = ptlrpc_prep_set();
579         if (pd->pd_set == NULL)
580                 goto out;
581         set = pd->pd_set;
582
583         /* add rpcs into set */
584         mutex_down(&pinger_sem);
585         list_for_each(iter, &pinger_imports) {
586                 struct obd_import *imp =
587                         list_entry(iter, struct obd_import, imp_pinger_chain);
588                 int generation, level;
589
590                 if (cfs_time_aftereq(pd->pd_this_ping,
591                                      imp->imp_next_ping - 5 * CFS_TICK)) {
592                         /* Add a ping. */
593                         spin_lock(&imp->imp_lock);
594                         generation = imp->imp_generation;
595                         level = imp->imp_state;
596                         spin_unlock(&imp->imp_lock);
597
598                         if (level != LUSTRE_IMP_FULL) {
599                                 CDEBUG(D_HA,
600                                        "not pinging %s (in recovery)\n",
601                                        obd2cli_tgt(imp->imp_obd));
602                                 continue;
603                         }
604
605                         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
606                                                         LUSTRE_OBD_VERSION,
607                                                         OBD_PING);
608                         if (req == NULL) {
609                                 CERROR("OOM trying to ping %s->%s\n",
610                                        imp->imp_obd->obd_uuid.uuid,
611                                        obd2cli_tgt(imp->imp_obd));
612                                 break;
613                         }
614
615                         req->rq_no_resend = 1;
616                         ptlrpc_request_set_replen(req);
617                         req->rq_send_state = LUSTRE_IMP_FULL;
618                         ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
619                         req->rq_import_generation = generation;
620                         ptlrpc_set_add_req(set, req);
621                 } else {
622                         CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
623                                " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
624                                imp->imp_next_ping, pd->pd_this_ping);
625                 }
626         }
627         pd->pd_this_ping = curtime;
628         mutex_up(&pinger_sem);
629
630         /* Might be empty, that's OK. */
631         if (set->set_remaining == 0)
632                 CDEBUG(D_RPCTRACE, "nothing to ping\n");
633
634         list_for_each(iter, &set->set_requests) {
635                 struct ptlrpc_request *req =
636                         list_entry(iter, struct ptlrpc_request,
637                                    rq_set_chain);
638                 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
639                           req->rq_import->imp_obd->obd_uuid.uuid,
640                           obd2cli_tgt(req->rq_import->imp_obd));
641                 (void)ptl_send_rpc(req, 0);
642         }
643
644 do_check_set:
645         rc = ptlrpc_check_set(NULL, set);
646
647         /* not finished, and we are not expired, simply return */
648         if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
649                                             cfs_time_seconds(PING_INTERVAL)))) {
650                 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
651                 pd->pd_recursion--;
652                 return 0;
653         }
654
655         /* Expire all the requests that didn't come back. */
656         mutex_down(&pinger_sem);
657         list_for_each(iter, &set->set_requests) {
658                 req = list_entry(iter, struct ptlrpc_request,
659                                  rq_set_chain);
660
661                 if (req->rq_phase == RQ_PHASE_COMPLETE)
662                         continue;
663
664                 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
665                 atomic_dec(&req->rq_import->imp_inflight);
666                 set->set_remaining--;
667                 /* If it was disconnected, don't sweat it. */
668                 if (list_empty(&req->rq_import->imp_pinger_chain)) {
669                         ptlrpc_unregister_reply(req, 0);
670                         continue;
671                 }
672
673                 CDEBUG(D_RPCTRACE, "pinger initiate expire_one_request\n");
674                 ptlrpc_expire_one_request(req, 0);
675         }
676         mutex_up(&pinger_sem);
677
678         ptlrpc_set_destroy(set);
679         pd->pd_set = NULL;
680
681 out:
682         pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
683                                         cfs_time_seconds(PING_INTERVAL));
684         pd->pd_this_ping = 0; /* XXX for debug */
685
686         CDEBUG(D_INFO, "finished a round ping\n");
687         pd->pd_recursion--;
688         return 0;
689 }
690
691 static void *pinger_callback = NULL;
692 #endif /* ENABLE_PINGER */
693
694 int ptlrpc_start_pinger(void)
695 {
696 #ifdef ENABLE_PINGER
697         memset(&pinger_args, 0, sizeof(pinger_args));
698         pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
699                                                            &pinger_check_rpcs,
700                                                            &pinger_args);
701 #endif
702         return 0;
703 }
704
705 int ptlrpc_stop_pinger(void)
706 {
707 #ifdef ENABLE_PINGER
708         if (pinger_callback)
709                 liblustre_deregister_wait_callback(pinger_callback);
710 #endif
711         return 0;
712 }
713
714 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
715 {
716 #ifdef ENABLE_PINGER
717         mutex_down(&pinger_sem);
718         ptlrpc_update_next_ping(imp);
719         if (pinger_args.pd_set == NULL &&
720             time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
721                 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
722                         imp->imp_next_ping, cfs_time_current());
723                 pinger_args.pd_next_ping = imp->imp_next_ping;
724         }
725         mutex_up(&pinger_sem);
726 #endif
727 }
728
729 int ptlrpc_pinger_add_import(struct obd_import *imp)
730 {
731         ENTRY;
732         if (!list_empty(&imp->imp_pinger_chain))
733                 RETURN(-EALREADY);
734
735         CDEBUG(D_HA, "adding pingable import %s->%s\n",
736                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
737         ptlrpc_pinger_sending_on_import(imp);
738
739         mutex_down(&pinger_sem);
740         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
741         class_import_get(imp);
742         mutex_up(&pinger_sem);
743
744         RETURN(0);
745 }
746
747 int ptlrpc_pinger_del_import(struct obd_import *imp)
748 {
749         ENTRY;
750         if (list_empty(&imp->imp_pinger_chain))
751                 RETURN(-ENOENT);
752
753         mutex_down(&pinger_sem);
754         list_del_init(&imp->imp_pinger_chain);
755         CDEBUG(D_HA, "removing pingable import %s->%s\n",
756                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
757         class_import_put(imp);
758         mutex_up(&pinger_sem);
759         RETURN(0);
760 }
761
762 void ptlrpc_pinger_wake_up()
763 {
764 #ifdef ENABLE_PINGER
765         /* XXX force pinger to run, if needed */
766         struct obd_import *imp;
767         ENTRY;
768         list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
769                 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
770                        imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
771 #ifdef ENABLE_LIBLUSTRE_RECOVERY
772                 if (imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
773 #else
774                 /*XXX only recover for the initial connection */
775                 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
776                     imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
777 #endif
778                         ptlrpc_initiate_recovery(imp);
779                 else if (imp->imp_state != LUSTRE_IMP_FULL)
780                         CDEBUG(D_HA, "Refused to recover import %s->%s "
781                                      "state %d, deactive %d\n",
782                                      imp->imp_obd->obd_uuid.uuid,
783                                      obd2cli_tgt(imp->imp_obd), imp->imp_state,
784                                      imp->imp_deactive);
785         }
786         EXIT;
787 #endif
788 }
789 #endif /* !__KERNEL__ */