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