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