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