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