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