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