Whamcloud - gitweb
Branch b1_8
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/pinger.c
37  *
38  * Portal-RPC reconnection and replay operations, for use in recovery.
39  */
40
41 #ifndef __KERNEL__
42 #include <liblustre.h>
43 #else
44 #define DEBUG_SUBSYSTEM S_RPC
45 #endif
46
47 #include <obd_support.h>
48 #include <obd_class.h>
49 #include "ptlrpc_internal.h"
50
51 struct semaphore pinger_sem;
52 static struct list_head pinger_imports = CFS_LIST_HEAD_INIT(pinger_imports);
53 static struct list_head timeout_list = CFS_LIST_HEAD_INIT(timeout_list); 
54
55 struct ptlrpc_request *
56 ptlrpc_prep_ping(struct obd_import *imp)
57 {
58         struct ptlrpc_request *req;
59
60         req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION,
61                               OBD_PING, 1, NULL, NULL);
62         if (req) {
63                 ptlrpc_req_set_repsize(req, 1, NULL);
64                 req->rq_no_resend = req->rq_no_delay = 1;
65         }
66         return req;
67 }
68
69 int ptlrpc_obd_ping(struct obd_device *obd)
70 {
71         int rc;
72         struct ptlrpc_request *req;
73         ENTRY;
74
75         req = ptlrpc_prep_ping(obd->u.cli.cl_import);
76         if (req == NULL)
77                 RETURN(-ENOMEM);
78
79         req->rq_send_state = LUSTRE_IMP_FULL;
80
81         rc = ptlrpc_queue_wait(req);
82
83         ptlrpc_req_finished(req);
84
85         RETURN(rc);
86 }
87 EXPORT_SYMBOL(ptlrpc_obd_ping);
88
89 int ptlrpc_ping(struct obd_import *imp)
90 {
91         struct ptlrpc_request *req;
92         int rc = 0;
93         ENTRY;
94
95         req = ptlrpc_prep_ping(imp);
96         if (req) {
97                 DEBUG_REQ(D_INFO, req, "pinging %s->%s",
98                           imp->imp_obd->obd_uuid.uuid,
99                           obd2cli_tgt(imp->imp_obd));
100                 ptlrpcd_add_req(req);
101         } else {
102                 CERROR("OOM trying to ping %s->%s\n",
103                        imp->imp_obd->obd_uuid.uuid,
104                        obd2cli_tgt(imp->imp_obd));
105                 rc = -ENOMEM;
106         }
107
108         RETURN(rc);
109 }
110 EXPORT_SYMBOL(ptlrpc_ping);
111
112 void ptlrpc_update_next_ping(struct obd_import *imp, int soon)
113 {
114 #ifdef ENABLE_PINGER
115         int time = soon ? PING_INTERVAL_SHORT : PING_INTERVAL;
116         if (imp->imp_state == LUSTRE_IMP_DISCON) {
117                 int dtime = max_t(int, CONNECTION_SWITCH_MIN,
118                                   AT_OFF ? 0 :
119                                   at_get(&imp->imp_at.iat_net_latency));
120                 time = min(time, dtime);
121         }
122         imp->imp_next_ping = cfs_time_shift(time);
123 #endif /* ENABLE_PINGER */
124 }
125
126 void ptlrpc_ping_import_soon(struct obd_import *imp)
127 {
128         imp->imp_next_ping = cfs_time_current();
129 }
130
131 static inline int imp_is_deactive(struct obd_import *imp)
132 {
133         return (imp->imp_deactive ||
134                 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_IMP_DEACTIVE));
135 }
136
137 cfs_duration_t pinger_check_timeout(cfs_time_t time)
138 {
139         struct timeout_item *item;
140         cfs_time_t timeout = PING_INTERVAL;
141
142         /* The timeout list is a increase order sorted list */
143         mutex_down(&pinger_sem);
144         list_for_each_entry(item, &timeout_list, ti_chain) {
145                 int ti_timeout = item->ti_timeout;
146                 if (timeout > ti_timeout)
147                          timeout = ti_timeout;
148                 break;
149         }
150         mutex_up(&pinger_sem);
151         
152         return cfs_time_sub(cfs_time_add(time, cfs_time_seconds(timeout)),
153                                          cfs_time_current());
154 }
155
156 #ifdef __KERNEL__
157 static int ptlrpc_pinger_main(void *arg)
158 {
159         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
160         struct ptlrpc_thread *thread = data->thread;
161         ENTRY;
162
163         cfs_daemonize(data->name);
164
165         /* Record that the thread is running */
166         thread->t_flags = SVC_RUNNING;
167         cfs_waitq_signal(&thread->t_ctl_waitq);
168
169         /* And now, loop forever, pinging as needed. */
170         while (1) {
171                 cfs_time_t this_ping = cfs_time_current();
172                 struct l_wait_info lwi;
173                 cfs_duration_t time_to_next_wake;
174                 struct timeout_item *item;
175                 struct list_head *iter;
176
177                 mutex_down(&pinger_sem);
178                 list_for_each_entry(item, &timeout_list, ti_chain) {
179                         item->ti_cb(item, item->ti_cb_data);
180                 }
181                 list_for_each(iter, &pinger_imports) {
182                         struct obd_import *imp =
183                                 list_entry(iter, struct obd_import,
184                                            imp_pinger_chain);
185                         int force, level;
186
187                         spin_lock(&imp->imp_lock);
188                         level = imp->imp_state;
189                         force = imp->imp_force_verify;
190                         imp->imp_force_verify = 0;
191                         spin_unlock(&imp->imp_lock);
192
193                         CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_RPCTRACE,
194                                "level %s/%u force %u deactive %u pingable %u\n",
195                                ptlrpc_import_state_name(level), level,
196                                force, imp->imp_deactive, imp->imp_pingable);
197
198                         if (force ||
199                             /* if the next ping is within, say, 5 jiffies from
200                                now, go ahead and ping. See note below. */
201                             cfs_time_aftereq(this_ping, 
202                                              imp->imp_next_ping - 5 * CFS_TICK)) {
203                                 if (level == LUSTRE_IMP_DISCON &&
204                                     !imp_is_deactive(imp)) {
205                                         /* wait at least a timeout before
206                                            trying recovery again. */
207                                         imp->imp_next_ping = cfs_time_shift(obd_timeout);
208                                         ptlrpc_initiate_recovery(imp);
209                                 } else if (level != LUSTRE_IMP_FULL ||
210                                          imp->imp_obd->obd_no_recov ||
211                                          imp_is_deactive(imp)) {
212                                         CDEBUG(D_HA, "not pinging %s "
213                                                "(in recovery: %s or recovery "
214                                                "disabled: %u/%u)\n",
215                                                obd2cli_tgt(imp->imp_obd),
216                                                ptlrpc_import_state_name(level),
217                                                imp->imp_deactive,
218                                                imp->imp_obd->obd_no_recov);
219                                 } else if (imp->imp_pingable || force) {
220                                                 ptlrpc_ping(imp);
221                                 }
222                         } else {
223                                 if (!imp->imp_pingable)
224                                         continue;
225                                 CDEBUG(D_INFO,
226                                        "don't need to ping %s ("CFS_TIME_T
227                                        " > "CFS_TIME_T")\n",
228                                        obd2cli_tgt(imp->imp_obd),
229                                        imp->imp_next_ping, this_ping);
230                         }
231
232                         /* obd_timeout might have changed */
233                         if (cfs_time_after(imp->imp_next_ping,
234                                            cfs_time_add(this_ping, 
235                                                         cfs_time_seconds(PING_INTERVAL))))
236                                 ptlrpc_update_next_ping(imp, 0);
237                 }
238                 mutex_up(&pinger_sem);
239                 /* update memory usage info */
240                 obd_update_maxusage();
241
242                 /* Wait until the next ping time, or until we're stopped. */
243                 time_to_next_wake = pinger_check_timeout(this_ping);
244                 /* The ping sent by ptlrpc_send_rpc may get sent out
245                    say .01 second after this.
246                    ptlrpc_pinger_eending_on_import will then set the
247                    next ping time to next_ping + .01 sec, which means
248                    we will SKIP the next ping at next_ping, and the
249                    ping will get sent 2 timeouts from now!  Beware. */
250                 CDEBUG(D_INFO, "next wakeup in "CFS_DURATION_T" ("CFS_TIME_T")\n",
251                                time_to_next_wake,
252                                cfs_time_add(this_ping, cfs_time_seconds(PING_INTERVAL)));
253                 if (time_to_next_wake > 0) {
254                         lwi = LWI_TIMEOUT(max_t(cfs_duration_t, time_to_next_wake, cfs_time_seconds(1)),
255                                           NULL, NULL);
256                         l_wait_event(thread->t_ctl_waitq,
257                                      thread->t_flags & (SVC_STOPPING|SVC_EVENT),
258                                      &lwi);
259                         if (thread->t_flags & SVC_STOPPING) {
260                                 thread->t_flags &= ~SVC_STOPPING;
261                                 EXIT;
262                                 break;
263                         } else if (thread->t_flags & SVC_EVENT) {
264                                 /* woken after adding import to reset timer */
265                                 thread->t_flags &= ~SVC_EVENT;
266                         }
267                 }
268         }
269
270         thread->t_flags = SVC_STOPPED;
271         cfs_waitq_signal(&thread->t_ctl_waitq);
272
273         CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
274         return 0;
275 }
276
277 static struct ptlrpc_thread *pinger_thread = NULL;
278
279 int ptlrpc_start_pinger(void)
280 {
281         struct l_wait_info lwi = { 0 };
282         struct ptlrpc_svc_data d;
283         int rc;
284 #ifndef ENABLE_PINGER
285         return 0;
286 #endif
287         ENTRY;
288
289         if (pinger_thread != NULL)
290                 RETURN(-EALREADY);
291
292         OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
293         if (pinger_thread == NULL)
294                 RETURN(-ENOMEM);
295         cfs_waitq_init(&pinger_thread->t_ctl_waitq);
296
297         d.name = "ll_ping";
298         d.thread = pinger_thread;
299
300         /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
301          * just drop the VM and FILES in ptlrpc_daemonize() right away. */
302         rc = cfs_kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
303         if (rc < 0) {
304                 CERROR("cannot start thread: %d\n", rc);
305                 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
306                 pinger_thread = NULL;
307                 RETURN(rc);
308         }
309         l_wait_event(pinger_thread->t_ctl_waitq,
310                      pinger_thread->t_flags & SVC_RUNNING, &lwi);
311
312         RETURN(0);
313 }
314
315 int ptlrpc_pinger_remove_timeouts(void);
316
317 int ptlrpc_stop_pinger(void)
318 {
319         struct l_wait_info lwi = { 0 };
320         int rc = 0;
321 #ifndef ENABLE_PINGER
322         return 0;
323 #endif
324         ENTRY;
325
326         if (pinger_thread == NULL)
327                 RETURN(-EALREADY);
328
329         ptlrpc_pinger_remove_timeouts();
330         mutex_down(&pinger_sem);
331         pinger_thread->t_flags = SVC_STOPPING;
332         cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
333         mutex_up(&pinger_sem);
334
335         l_wait_event(pinger_thread->t_ctl_waitq,
336                      (pinger_thread->t_flags & SVC_STOPPED), &lwi);
337
338         OBD_FREE(pinger_thread, sizeof(*pinger_thread));
339         pinger_thread = NULL;
340         RETURN(rc);
341 }
342
343 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
344 {
345         ptlrpc_update_next_ping(imp, 0);
346 }
347
348 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
349 {
350         ptlrpc_update_next_ping(imp, 1);
351 }
352
353 int ptlrpc_pinger_add_import(struct obd_import *imp)
354 {
355         ENTRY;
356         if (!list_empty(&imp->imp_pinger_chain))
357                 RETURN(-EALREADY);
358
359         mutex_down(&pinger_sem);
360         CDEBUG(D_HA, "adding pingable import %s->%s\n",
361                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
362         /* if we add to pinger we want recovery on this import */
363         imp->imp_obd->obd_no_recov = 0;
364
365         ptlrpc_update_next_ping(imp, 0);
366         /* XXX sort, blah blah */
367         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
368         class_import_get(imp);
369
370         ptlrpc_pinger_wake_up();
371         mutex_up(&pinger_sem);
372
373         RETURN(0);
374 }
375
376 int ptlrpc_pinger_del_import(struct obd_import *imp)
377 {
378         ENTRY;
379         if (list_empty(&imp->imp_pinger_chain))
380                 RETURN(-ENOENT);
381
382         mutex_down(&pinger_sem);
383         list_del_init(&imp->imp_pinger_chain);
384         CDEBUG(D_HA, "removing pingable import %s->%s\n",
385                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
386         /* if we remove from pinger we don't want recovery on this import */
387         imp->imp_obd->obd_no_recov = 1;
388         class_import_put(imp);
389         mutex_up(&pinger_sem);
390         RETURN(0);
391 }
392
393 /**
394  * Register a timeout callback to the pinger list, and the callback will
395  * be called when timeout happens.
396  */
397 struct timeout_item* ptlrpc_new_timeout(int time, enum timeout_event event,
398                                         timeout_cb_t cb, void *data)
399 {
400         struct timeout_item *ti;
401         
402         OBD_ALLOC_PTR(ti);
403         if (!ti)
404                 return(NULL);
405
406         CFS_INIT_LIST_HEAD(&ti->ti_obd_list);
407         CFS_INIT_LIST_HEAD(&ti->ti_chain);
408         ti->ti_timeout = time;
409         ti->ti_event = event;
410         ti->ti_cb = cb;
411         ti->ti_cb_data = data;
412         
413         return ti;
414 }
415
416 /**
417  * Register timeout event on the the pinger thread.
418  * Note: the timeout list is an sorted list with increased timeout value.
419  */
420 static struct timeout_item*
421 ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
422                                timeout_cb_t cb, void *data)
423 {
424         struct timeout_item *item, *tmp;
425
426         LASSERT_SEM_LOCKED(&pinger_sem);
427
428         list_for_each_entry(item, &timeout_list, ti_chain)
429                 if (item->ti_event == event)
430                         goto out;
431
432         item = ptlrpc_new_timeout(time, event, cb, data);
433         if (item) {
434                 list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
435                         if (tmp->ti_timeout < time) {
436                                 list_add(&item->ti_chain, &tmp->ti_chain);
437                                 goto out;
438                         }
439                 }
440                 list_add(&item->ti_chain, &timeout_list);
441         }
442 out:
443         return item;
444 }
445
446 /* Add a client_obd to the timeout event list, when timeout(@time) 
447  * happens, the callback(@cb) will be called.
448  */
449 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
450                               timeout_cb_t cb, void *data,
451                               struct list_head *obd_list)
452 {
453         struct timeout_item *ti;
454
455         mutex_down(&pinger_sem);
456         ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
457         if (!ti) {
458                 mutex_up(&pinger_sem);
459                 return (-EINVAL);
460         }
461         list_add(obd_list, &ti->ti_obd_list);
462         mutex_up(&pinger_sem);
463         return 0;
464 }           
465
466 int ptlrpc_del_timeout_client(struct list_head *obd_list)
467 {
468         mutex_down(&pinger_sem);
469         list_del_init(obd_list);
470         mutex_up(&pinger_sem);
471         return 0;
472 }  
473
474 int ptlrpc_pinger_remove_timeouts(void)
475 {
476         struct timeout_item *item, *tmp;
477
478         mutex_down(&pinger_sem);
479         list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
480                 LASSERT(list_empty(&item->ti_obd_list));
481                 list_del(&item->ti_chain);
482                 OBD_FREE_PTR(item);
483         }
484         mutex_up(&pinger_sem);
485         return 0;
486 }
487
488 void ptlrpc_pinger_wake_up()
489 {
490 #ifdef ENABLE_PINGER
491         pinger_thread->t_flags |= SVC_EVENT;
492         cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
493 #endif
494 }
495
496 /* Ping evictor thread */
497 #define PET_READY     1
498 #define PET_TERMINATE 2
499
500 static int               pet_refcount = 0;
501 static int               pet_state;
502 static wait_queue_head_t pet_waitq;
503 static struct obd_export *pet_exp = NULL;
504 static spinlock_t        pet_lock = SPIN_LOCK_UNLOCKED;
505
506 int ping_evictor_wake(struct obd_export *exp)
507 {
508         spin_lock(&pet_lock);
509         if (pet_exp || (pet_state != PET_READY)) {
510                 /* eventually the new obd will call here again. */
511                 spin_unlock(&pet_lock);
512                 return 1;
513         }
514
515         /* We have to make sure the obd isn't destroyed between now and when
516          * the ping evictor runs.  We'll take a reference here, and drop it
517          * when we finish in the evictor.  We don't really care about this
518          * export in particular; we just need one to keep the obd alive. */
519         pet_exp = class_export_get(exp);
520         spin_unlock(&pet_lock);
521
522         wake_up(&pet_waitq);
523         return 0;
524 }
525
526 static int ping_evictor_main(void *arg)
527 {
528         struct obd_device *obd;
529         struct obd_export *exp;
530         struct l_wait_info lwi = { 0 };
531         time_t expire_time;
532         ENTRY;
533
534         ptlrpc_daemonize("ll_evictor");
535
536         CDEBUG(D_HA, "Starting Ping Evictor\n");
537         pet_exp = NULL;
538         pet_state = PET_READY;
539         while (1) {
540                 l_wait_event(pet_waitq, pet_exp ||
541                              (pet_state == PET_TERMINATE), &lwi);
542                 if (pet_state == PET_TERMINATE)
543                         break;
544
545                 /* we only get here if pet_exp != NULL, and the end of this
546                  * loop is the only place which sets it NULL again, so lock
547                  * is not strictly necessary. */
548                 spin_lock(&pet_lock);
549                 obd = pet_exp->exp_obd;
550                 spin_unlock(&pet_lock);
551
552                 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
553
554                 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
555                        obd->obd_name, expire_time);
556
557                 /* Exports can't be deleted out of the list while we hold
558                  * the obd lock (class_unlink_export), which means we can't
559                  * lose the last ref on the export.  If they've already been
560                  * removed from the list, we won't find them here. */
561                 spin_lock(&obd->obd_dev_lock);
562                 while (!list_empty(&obd->obd_exports_timed)) {
563                         exp = list_entry(obd->obd_exports_timed.next,
564                                          struct obd_export,exp_obd_chain_timed);
565                         if (expire_time > exp->exp_last_request_time) {
566                                 class_export_get(exp);
567                                 spin_unlock(&obd->obd_dev_lock);
568                                 LCONSOLE_WARN("%s: haven't heard from client %s"
569                                               " (at %s) in %ld seconds. I think"
570                                               " it's dead, and I am evicting"
571                                               " it.\n", obd->obd_name,
572                                               obd_uuid2str(&exp->exp_client_uuid),
573                                               obd_export_nid2str(exp),
574                                               (long)(cfs_time_current_sec() -
575                                                      exp->exp_last_request_time));
576                                 CDEBUG(D_HA, "Last request was at %ld\n",
577                                        exp->exp_last_request_time);
578                                 class_fail_export(exp);
579                                 class_export_put(exp);
580                                 spin_lock(&obd->obd_dev_lock);
581                         } else {
582                                 /* List is sorted, so everyone below is ok */
583                                 break;
584                         }
585                 }
586                 spin_unlock(&obd->obd_dev_lock);
587
588                 class_export_put(pet_exp);
589
590                 spin_lock(&pet_lock);
591                 pet_exp = NULL;
592                 spin_unlock(&pet_lock);
593         }
594         CDEBUG(D_HA, "Exiting Ping Evictor\n");
595
596         RETURN(0);
597 }
598
599 void ping_evictor_start(void)
600 {
601         int rc;
602
603         if (++pet_refcount > 1)
604                 return;
605
606         init_waitqueue_head(&pet_waitq);
607
608         rc = cfs_kernel_thread(ping_evictor_main, NULL, CLONE_VM | CLONE_FILES);
609         if (rc < 0) {
610                 pet_refcount--;
611                 CERROR("Cannot start ping evictor thread: %d\n", rc);
612         }
613 }
614 EXPORT_SYMBOL(ping_evictor_start);
615
616 void ping_evictor_stop(void)
617 {
618         if (--pet_refcount > 0)
619                 return;
620
621         pet_state = PET_TERMINATE;
622         wake_up(&pet_waitq);
623 }
624 EXPORT_SYMBOL(ping_evictor_stop);
625 #else /* !__KERNEL__ */
626
627 /* XXX
628  * the current implementation of pinger in liblustre is not optimized
629  */
630
631 #ifdef ENABLE_PINGER
632 static struct pinger_data {
633         int             pd_recursion;
634         cfs_time_t      pd_this_ping;   /* jiffies */
635         cfs_time_t      pd_next_ping;   /* jiffies */
636         struct ptlrpc_request_set *pd_set;
637 } pinger_args;
638
639 static int pinger_check_rpcs(void *arg)
640 {
641         cfs_time_t curtime = cfs_time_current();
642         struct ptlrpc_request *req;
643         struct ptlrpc_request_set *set;
644         struct list_head *iter;
645         struct obd_import *imp;
646         struct pinger_data *pd = &pinger_args;
647         int rc;
648
649         /* prevent recursion */
650         if (pd->pd_recursion++) {
651                 CDEBUG(D_HA, "pinger: recursion! quit\n");
652                 LASSERT(pd->pd_set);
653                 pd->pd_recursion--;
654                 return 0;
655         }
656
657         /* have we reached ping point? */
658         if (!pd->pd_set && time_before(curtime, pd->pd_next_ping)) {
659                 pd->pd_recursion--;
660                 return 0;
661         }
662
663         /* if we have rpc_set already, continue processing it */
664         if (pd->pd_set) {
665                 LASSERT(pd->pd_this_ping);
666                 set = pd->pd_set;
667                 goto do_check_set;
668         }
669
670         pd->pd_this_ping = curtime;
671         pd->pd_set = ptlrpc_prep_set();
672         if (pd->pd_set == NULL)
673                 goto out;
674         set = pd->pd_set;
675
676         /* add rpcs into set */
677         mutex_down(&pinger_sem);
678         list_for_each(iter, &pinger_imports) {
679                 struct obd_import *imp =
680                         list_entry(iter, struct obd_import, imp_pinger_chain);
681                 int generation, level;
682
683                 if (cfs_time_aftereq(pd->pd_this_ping, 
684                                      imp->imp_next_ping - 5 * CFS_TICK)) {
685                         /* Add a ping. */
686                         spin_lock(&imp->imp_lock);
687                         generation = imp->imp_generation;
688                         level = imp->imp_state;
689                         spin_unlock(&imp->imp_lock);
690
691                         if (level != LUSTRE_IMP_FULL) {
692                                 CDEBUG(D_HA,
693                                        "not pinging %s (in recovery)\n",
694                                        obd2cli_tgt(imp->imp_obd));
695                                 continue;
696                         }
697
698                         req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING,
699                                               1, NULL, NULL);
700                         if (!req) {
701                                 CERROR("out of memory\n");
702                                 break;
703                         }
704                         req->rq_no_resend = 1;
705                         ptlrpc_req_set_repsize(req, 1, NULL);
706                         req->rq_send_state = LUSTRE_IMP_FULL;
707                         ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
708                         req->rq_import_generation = generation;
709                         ptlrpc_set_add_req(set, req);
710                 } else {
711                         CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
712                                " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
713                                imp->imp_next_ping, pd->pd_this_ping);
714                 }
715         }
716         pd->pd_this_ping = curtime;
717         mutex_up(&pinger_sem);
718
719         /* Might be empty, that's OK. */
720         if (set->set_remaining == 0)
721                 CDEBUG(D_RPCTRACE, "nothing to ping\n");
722
723         list_for_each(iter, &set->set_requests) {
724                 struct ptlrpc_request *req =
725                         list_entry(iter, struct ptlrpc_request,
726                                    rq_set_chain);
727                 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
728                           req->rq_import->imp_obd->obd_uuid.uuid,
729                           obd2cli_tgt(req->rq_import->imp_obd));
730                 (void)ptl_send_rpc(req, 0);
731         }
732
733 do_check_set:
734         rc = ptlrpc_check_set(set);
735
736         /* not finished, and we are not expired, simply return */
737         if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
738                                             cfs_time_seconds(PING_INTERVAL)))) {
739                 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
740                 pd->pd_recursion--;
741                 return 0;
742         }
743
744         /* Expire all the requests that didn't come back. */
745         mutex_down(&pinger_sem);
746         list_for_each(iter, &set->set_requests) {
747                 req = list_entry(iter, struct ptlrpc_request,
748                                  rq_set_chain);
749
750                 if (req->rq_phase == RQ_PHASE_COMPLETE)
751                         continue;
752
753                 CDEBUG(D_RPCTRACE, "Pinger initiate expire request(%p)\n",
754                        req);
755
756                 /* This will also unregister reply. */
757                 ptlrpc_expire_one_request(req, 0);
758
759                 /* We're done with this req, let's finally move it to complete
760                  * phase and take care of inflights. */
761                 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
762                 imp = req->rq_import;
763                 spin_lock(&imp->imp_lock);
764                 if (!list_empty(&req->rq_list)) {
765                         list_del_init(&req->rq_list);
766                         atomic_dec(&imp->imp_inflight);
767                 }
768                 spin_unlock(&imp->imp_lock);
769                 set->set_remaining--;
770         }
771         mutex_up(&pinger_sem);
772
773         ptlrpc_set_destroy(set);
774         pd->pd_set = NULL;
775
776 out:
777         pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
778                                         cfs_time_seconds(PING_INTERVAL));
779         pd->pd_this_ping = 0; /* XXX for debug */
780
781         CDEBUG(D_INFO, "finished a round ping\n");
782         pd->pd_recursion--;
783         return 0;
784 }
785
786 static void *pinger_callback = NULL;
787 #endif /* ENABLE_PINGER */
788
789 int ptlrpc_start_pinger(void)
790 {
791 #ifdef ENABLE_PINGER
792         memset(&pinger_args, 0, sizeof(pinger_args));
793         pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
794                                                            &pinger_check_rpcs,
795                                                            &pinger_args);
796 #endif
797         return 0;
798 }
799
800 int ptlrpc_stop_pinger(void)
801 {
802 #ifdef ENABLE_PINGER
803         if (pinger_callback)
804                 liblustre_deregister_wait_callback(pinger_callback);
805 #endif
806         return 0;
807 }
808
809 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
810 {
811 #ifdef ENABLE_PINGER
812         mutex_down(&pinger_sem);
813         ptlrpc_update_next_ping(imp, 0);
814         if (pinger_args.pd_set == NULL &&
815             time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
816                 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
817                         imp->imp_next_ping, cfs_time_current());
818                 pinger_args.pd_next_ping = imp->imp_next_ping;
819         }
820         mutex_up(&pinger_sem);
821 #endif
822 }
823
824 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
825                               timeout_cb_t cb, void *data,
826                               struct list_head *obd_list)
827 {
828         return 0;
829 }           
830
831 int ptlrpc_del_timeout_client(struct list_head *obd_list)
832 {
833         return 0;
834 }  
835
836 int ptlrpc_pinger_add_import(struct obd_import *imp)
837 {
838         ENTRY;
839         if (!list_empty(&imp->imp_pinger_chain))
840                 RETURN(-EALREADY);
841
842         CDEBUG(D_HA, "adding pingable import %s->%s\n",
843                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
844         ptlrpc_pinger_sending_on_import(imp);
845
846         mutex_down(&pinger_sem);
847         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
848         class_import_get(imp);
849         mutex_up(&pinger_sem);
850
851         RETURN(0);
852 }
853
854 int ptlrpc_pinger_del_import(struct obd_import *imp)
855 {
856         ENTRY;
857         if (list_empty(&imp->imp_pinger_chain))
858                 RETURN(-ENOENT);
859
860         mutex_down(&pinger_sem);
861         list_del_init(&imp->imp_pinger_chain);
862         CDEBUG(D_HA, "removing pingable import %s->%s\n",
863                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
864         class_import_put(imp);
865         mutex_up(&pinger_sem);
866         RETURN(0);
867 }
868
869 void ptlrpc_pinger_wake_up()
870 {
871 #ifdef ENABLE_PINGER
872         ENTRY;
873         /* XXX force pinger to run, if needed */
874         struct obd_import *imp;
875         list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
876                 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
877                        imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
878 #ifdef ENABLE_LIBLUSTRE_RECOVERY
879                 if (imp->imp_state == LUSTRE_IMP_DISCON &&
880                     !imp_is_deactive(imp))
881 #else
882                 /*XXX only recover for the initial connection */
883                 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
884                     imp->imp_state == LUSTRE_IMP_DISCON &&
885                     !imp_is_deactive(imp))
886 #endif
887                         ptlrpc_initiate_recovery(imp);
888                 else if (imp->imp_state != LUSTRE_IMP_FULL)
889                         CDEBUG(D_HA, "Refused to recover import %s->%s "
890                                      "state %d, deactive %d\n",
891                                      imp->imp_obd->obd_uuid.uuid,
892                                      obd2cli_tgt(imp->imp_obd), imp->imp_state,
893                                      imp_is_deactive(imp));
894         }
895 #endif
896         EXIT;
897 }
898 #endif /* !__KERNEL__ */