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