Whamcloud - gitweb
be520f6c781aa11cbdaf70b3c367010442d5c178
[fs/lustre-release.git] / lustre / ptlrpc / pinger.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
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 cfs_mutex_t pinger_mutex;
52 static CFS_LIST_HEAD(pinger_imports);
53 static cfs_list_t 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, PDL_POLICY_ROUND, -1);
104
105         RETURN(0);
106 }
107
108 void ptlrpc_update_next_ping(struct obd_import *imp, int soon)
109 {
110 #ifdef ENABLE_PINGER
111         int time = soon ? PING_INTERVAL_SHORT : 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 cfs_atomic_t suspend_timeouts = CFS_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         cfs_mutex_lock(&pinger_mutex);
151         cfs_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         cfs_mutex_unlock(&pinger_mutex);
158
159         return cfs_time_sub(cfs_time_add(time, cfs_time_seconds(timeout)),
160                                          cfs_time_current());
161 }
162
163 static cfs_waitq_t suspend_timeouts_waitq;
164
165 cfs_time_t ptlrpc_suspend_wakeup_time(void)
166 {
167         return suspend_wakeup_time;
168 }
169
170 void ptlrpc_deactivate_timeouts(struct obd_import *imp)
171 {
172         /*XXX: disabled for now, will be replaced by adaptive timeouts */
173 #if 0
174         if (imp->imp_no_timeout)
175                 return;
176         imp->imp_no_timeout = 1;
177         cfs_atomic_inc(&suspend_timeouts);
178         CDEBUG(D_HA|D_WARNING, "deactivate timeouts %u\n",
179                cfs_atomic_read(&suspend_timeouts));
180 #endif
181 }
182
183 void ptlrpc_activate_timeouts(struct obd_import *imp)
184 {
185         /*XXX: disabled for now, will be replaced by adaptive timeouts */
186 #if 0
187         if (!imp->imp_no_timeout)
188                 return;
189         imp->imp_no_timeout = 0;
190         LASSERT(cfs_atomic_read(&suspend_timeouts) > 0);
191         if (cfs_atomic_dec_and_test(&suspend_timeouts)) {
192                 suspend_wakeup_time = cfs_time_current();
193                 cfs_waitq_signal(&suspend_timeouts_waitq);
194         }
195         CDEBUG(D_HA|D_WARNING, "activate timeouts %u\n",
196                cfs_atomic_read(&suspend_timeouts));
197 #endif
198 }
199
200 int ptlrpc_check_suspend(void)
201 {
202         if (cfs_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 (cfs_atomic_read(&suspend_timeouts)) {
212                 DEBUG_REQ(D_NET, req, "-- suspend %d regular timeout",
213                           cfs_atomic_read(&suspend_timeouts));
214                 lwi = LWI_INTR(NULL, NULL);
215                 l_wait_event(suspend_timeouts_waitq,
216                              cfs_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         cfs_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         cfs_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 for a while before trying recovery again */
248                 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
249                 if (!imp->imp_no_pinger_recover)
250                         ptlrpc_initiate_recovery(imp);
251         } else if (level != LUSTRE_IMP_FULL ||
252                    imp->imp_obd->obd_no_recov ||
253                    imp_is_deactive(imp)) {
254                 CDEBUG(D_HA, "not pinging %s (in recovery "
255                        " or recovery disabled: %s)\n",
256                        obd2cli_tgt(imp->imp_obd),
257                        ptlrpc_import_state_name(level));
258         } else if (imp->imp_pingable || force) {
259                 ptlrpc_ping(imp);
260         }
261 }
262
263 static int ptlrpc_pinger_main(void *arg)
264 {
265         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
266         struct ptlrpc_thread *thread = data->thread;
267         ENTRY;
268
269         cfs_daemonize(data->name);
270
271         /* Record that the thread is running */
272         thread_set_flags(thread, SVC_RUNNING);
273         cfs_waitq_signal(&thread->t_ctl_waitq);
274
275         /* And now, loop forever, pinging as needed. */
276         while (1) {
277                 cfs_time_t this_ping = cfs_time_current();
278                 struct l_wait_info lwi;
279                 cfs_duration_t time_to_next_wake;
280                 struct timeout_item *item;
281                 cfs_list_t *iter;
282
283                 cfs_mutex_lock(&pinger_mutex);
284                 cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
285                         item->ti_cb(item, item->ti_cb_data);
286                 }
287                 cfs_list_for_each(iter, &pinger_imports) {
288                         struct obd_import *imp =
289                                 cfs_list_entry(iter, struct obd_import,
290                                                imp_pinger_chain);
291
292                         ptlrpc_pinger_process_import(imp, this_ping);
293                         /* obd_timeout might have changed */
294                         if (imp->imp_pingable && imp->imp_next_ping &&
295                             cfs_time_after(imp->imp_next_ping,
296                                            cfs_time_add(this_ping,
297                                                         cfs_time_seconds(PING_INTERVAL))))
298                                 ptlrpc_update_next_ping(imp, 0);
299                 }
300                 cfs_mutex_unlock(&pinger_mutex);
301                 /* update memory usage info */
302                 obd_update_maxusage();
303
304                 /* Wait until the next ping time, or until we're stopped. */
305                 time_to_next_wake = pinger_check_timeout(this_ping);
306                 /* The ping sent by ptlrpc_send_rpc may get sent out
307                    say .01 second after this.
308                    ptlrpc_pinger_sending_on_import will then set the
309                    next ping time to next_ping + .01 sec, which means
310                    we will SKIP the next ping at next_ping, and the
311                    ping will get sent 2 timeouts from now!  Beware. */
312                 CDEBUG(D_INFO, "next wakeup in "CFS_DURATION_T" ("
313                        CFS_TIME_T")\n", time_to_next_wake,
314                        cfs_time_add(this_ping,cfs_time_seconds(PING_INTERVAL)));
315                 if (time_to_next_wake > 0) {
316                         lwi = LWI_TIMEOUT(max_t(cfs_duration_t,
317                                                 time_to_next_wake,
318                                                 cfs_time_seconds(1)),
319                                           NULL, NULL);
320                         l_wait_event(thread->t_ctl_waitq,
321                                      thread_is_stopping(thread) ||
322                                      thread_is_event(thread),
323                                      &lwi);
324                         if (thread_test_and_clear_flags(thread, SVC_STOPPING)) {
325                                 EXIT;
326                                 break;
327                         } else {
328                                 /* woken after adding import to reset timer */
329                                 thread_test_and_clear_flags(thread, SVC_EVENT);
330                         }
331                 }
332         }
333
334         thread_set_flags(thread, SVC_STOPPED);
335         cfs_waitq_signal(&thread->t_ctl_waitq);
336
337         CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
338         return 0;
339 }
340
341 static struct ptlrpc_thread *pinger_thread = NULL;
342
343 int ptlrpc_start_pinger(void)
344 {
345         struct l_wait_info lwi = { 0 };
346         struct ptlrpc_svc_data d;
347         int rc;
348 #ifndef ENABLE_PINGER
349         return 0;
350 #endif
351         ENTRY;
352
353         if (pinger_thread != NULL)
354                 RETURN(-EALREADY);
355
356         OBD_ALLOC_PTR(pinger_thread);
357         if (pinger_thread == NULL)
358                 RETURN(-ENOMEM);
359         cfs_waitq_init(&pinger_thread->t_ctl_waitq);
360         cfs_waitq_init(&suspend_timeouts_waitq);
361
362         d.name = "ll_ping";
363         d.thread = pinger_thread;
364
365         /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
366          * just drop the VM and FILES in cfs_daemonize_ctxt() right away. */
367         rc = cfs_create_thread(ptlrpc_pinger_main, &d, CFS_DAEMON_FLAGS);
368         if (rc < 0) {
369                 CERROR("cannot start thread: %d\n", rc);
370                 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
371                 pinger_thread = NULL;
372                 RETURN(rc);
373         }
374         l_wait_event(pinger_thread->t_ctl_waitq,
375                      thread_is_running(pinger_thread), &lwi);
376
377         RETURN(0);
378 }
379
380 int ptlrpc_pinger_remove_timeouts(void);
381
382 int ptlrpc_stop_pinger(void)
383 {
384         struct l_wait_info lwi = { 0 };
385         int rc = 0;
386 #ifndef ENABLE_PINGER
387         return 0;
388 #endif
389         ENTRY;
390
391         if (pinger_thread == NULL)
392                 RETURN(-EALREADY);
393
394         ptlrpc_pinger_remove_timeouts();
395         cfs_mutex_lock(&pinger_mutex);
396         thread_set_flags(pinger_thread, SVC_STOPPING);
397         cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
398         cfs_mutex_unlock(&pinger_mutex);
399
400         l_wait_event(pinger_thread->t_ctl_waitq,
401                      thread_is_stopped(pinger_thread), &lwi);
402
403         OBD_FREE_PTR(pinger_thread);
404         pinger_thread = NULL;
405         RETURN(rc);
406 }
407
408 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
409 {
410         ptlrpc_update_next_ping(imp, 0);
411 }
412
413 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
414 {
415         ptlrpc_update_next_ping(imp, 1);
416 }
417
418 int ptlrpc_pinger_add_import(struct obd_import *imp)
419 {
420         ENTRY;
421         if (!cfs_list_empty(&imp->imp_pinger_chain))
422                 RETURN(-EALREADY);
423
424         cfs_mutex_lock(&pinger_mutex);
425         CDEBUG(D_HA, "adding pingable import %s->%s\n",
426                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
427         /* if we add to pinger we want recovery on this import */
428         imp->imp_obd->obd_no_recov = 0;
429         ptlrpc_update_next_ping(imp, 0);
430         /* XXX sort, blah blah */
431         cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
432         class_import_get(imp);
433
434         ptlrpc_pinger_wake_up();
435         cfs_mutex_unlock(&pinger_mutex);
436
437         RETURN(0);
438 }
439
440 int ptlrpc_pinger_del_import(struct obd_import *imp)
441 {
442         ENTRY;
443         if (cfs_list_empty(&imp->imp_pinger_chain))
444                 RETURN(-ENOENT);
445
446         cfs_mutex_lock(&pinger_mutex);
447         cfs_list_del_init(&imp->imp_pinger_chain);
448         CDEBUG(D_HA, "removing pingable import %s->%s\n",
449                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
450         /* if we remove from pinger we don't want recovery on this import */
451         imp->imp_obd->obd_no_recov = 1;
452         class_import_put(imp);
453         cfs_mutex_unlock(&pinger_mutex);
454         RETURN(0);
455 }
456
457 /**
458  * Register a timeout callback to the pinger list, and the callback will
459  * be called when timeout happens.
460  */
461 struct timeout_item* ptlrpc_new_timeout(int time, enum timeout_event event,
462                                         timeout_cb_t cb, void *data)
463 {
464         struct timeout_item *ti;
465
466         OBD_ALLOC_PTR(ti);
467         if (!ti)
468                 return(NULL);
469
470         CFS_INIT_LIST_HEAD(&ti->ti_obd_list);
471         CFS_INIT_LIST_HEAD(&ti->ti_chain);
472         ti->ti_timeout = time;
473         ti->ti_event = event;
474         ti->ti_cb = cb;
475         ti->ti_cb_data = data;
476
477         return ti;
478 }
479
480 /**
481  * Register timeout event on the the pinger thread.
482  * Note: the timeout list is an sorted list with increased timeout value.
483  */
484 static struct timeout_item*
485 ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
486                                timeout_cb_t cb, void *data)
487 {
488         struct timeout_item *item, *tmp;
489
490         LASSERT_MUTEX_LOCKED(&pinger_mutex);
491
492         cfs_list_for_each_entry(item, &timeout_list, ti_chain)
493                 if (item->ti_event == event)
494                         goto out;
495
496         item = ptlrpc_new_timeout(time, event, cb, data);
497         if (item) {
498                 cfs_list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
499                         if (tmp->ti_timeout < time) {
500                                 cfs_list_add(&item->ti_chain, &tmp->ti_chain);
501                                 goto out;
502                         }
503                 }
504                 cfs_list_add(&item->ti_chain, &timeout_list);
505         }
506 out:
507         return item;
508 }
509
510 /* Add a client_obd to the timeout event list, when timeout(@time)
511  * happens, the callback(@cb) will be called.
512  */
513 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
514                               timeout_cb_t cb, void *data,
515                               cfs_list_t *obd_list)
516 {
517         struct timeout_item *ti;
518
519         cfs_mutex_lock(&pinger_mutex);
520         ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
521         if (!ti) {
522                 cfs_mutex_unlock(&pinger_mutex);
523                 return (-EINVAL);
524         }
525         cfs_list_add(obd_list, &ti->ti_obd_list);
526         cfs_mutex_unlock(&pinger_mutex);
527         return 0;
528 }
529
530 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
531                               enum timeout_event event)
532 {
533         struct timeout_item *ti = NULL, *item;
534
535         if (cfs_list_empty(obd_list))
536                 return 0;
537         cfs_mutex_lock(&pinger_mutex);
538         cfs_list_del_init(obd_list);
539         /**
540          * If there are no obd attached to the timeout event
541          * list, remove this timeout event from the pinger
542          */
543         cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
544                 if (item->ti_event == event) {
545                         ti = item;
546                         break;
547                 }
548         }
549         LASSERTF(ti != NULL, "ti is NULL ! \n");
550         if (cfs_list_empty(&ti->ti_obd_list)) {
551                 cfs_list_del(&ti->ti_chain);
552                 OBD_FREE_PTR(ti);
553         }
554         cfs_mutex_unlock(&pinger_mutex);
555         return 0;
556 }
557
558 int ptlrpc_pinger_remove_timeouts(void)
559 {
560         struct timeout_item *item, *tmp;
561
562         cfs_mutex_lock(&pinger_mutex);
563         cfs_list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
564                 LASSERT(cfs_list_empty(&item->ti_obd_list));
565                 cfs_list_del(&item->ti_chain);
566                 OBD_FREE_PTR(item);
567         }
568         cfs_mutex_unlock(&pinger_mutex);
569         return 0;
570 }
571
572 void ptlrpc_pinger_wake_up()
573 {
574 #ifdef ENABLE_PINGER
575         thread_add_flags(pinger_thread, SVC_EVENT);
576         cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
577 #endif
578 }
579
580 /* Ping evictor thread */
581 #define PET_READY     1
582 #define PET_TERMINATE 2
583
584 static int               pet_refcount = 0;
585 static int               pet_state;
586 static cfs_waitq_t       pet_waitq;
587 CFS_LIST_HEAD(pet_list);
588 static cfs_spinlock_t    pet_lock = CFS_SPIN_LOCK_UNLOCKED;
589
590 int ping_evictor_wake(struct obd_export *exp)
591 {
592         struct obd_device *obd;
593
594         cfs_spin_lock(&pet_lock);
595         if (pet_state != PET_READY) {
596                 /* eventually the new obd will call here again. */
597                 cfs_spin_unlock(&pet_lock);
598                 return 1;
599         }
600
601         obd = class_exp2obd(exp);
602         if (cfs_list_empty(&obd->obd_evict_list)) {
603                 class_incref(obd, "evictor", obd);
604                 cfs_list_add(&obd->obd_evict_list, &pet_list);
605         }
606         cfs_spin_unlock(&pet_lock);
607
608         cfs_waitq_signal(&pet_waitq);
609         return 0;
610 }
611
612 static int ping_evictor_main(void *arg)
613 {
614         struct obd_device *obd;
615         struct obd_export *exp;
616         struct l_wait_info lwi = { 0 };
617         time_t expire_time;
618         ENTRY;
619
620         cfs_daemonize_ctxt("ll_evictor");
621
622         CDEBUG(D_HA, "Starting Ping Evictor\n");
623         pet_state = PET_READY;
624         while (1) {
625                 l_wait_event(pet_waitq, (!cfs_list_empty(&pet_list)) ||
626                              (pet_state == PET_TERMINATE), &lwi);
627
628                 /* loop until all obd's will be removed */
629                 if ((pet_state == PET_TERMINATE) && cfs_list_empty(&pet_list))
630                         break;
631
632                 /* we only get here if pet_exp != NULL, and the end of this
633                  * loop is the only place which sets it NULL again, so lock
634                  * is not strictly necessary. */
635                 cfs_spin_lock(&pet_lock);
636                 obd = cfs_list_entry(pet_list.next, struct obd_device,
637                                      obd_evict_list);
638                 cfs_spin_unlock(&pet_lock);
639
640                 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
641
642                 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
643                        obd->obd_name, expire_time);
644
645                 /* Exports can't be deleted out of the list while we hold
646                  * the obd lock (class_unlink_export), which means we can't
647                  * lose the last ref on the export.  If they've already been
648                  * removed from the list, we won't find them here. */
649                 cfs_spin_lock(&obd->obd_dev_lock);
650                 while (!cfs_list_empty(&obd->obd_exports_timed)) {
651                         exp = cfs_list_entry(obd->obd_exports_timed.next,
652                                              struct obd_export,
653                                              exp_obd_chain_timed);
654                         if (expire_time > exp->exp_last_request_time) {
655                                 class_export_get(exp);
656                                 cfs_spin_unlock(&obd->obd_dev_lock);
657                                  LCONSOLE_WARN("%s: haven't heard from client %s"
658                                               " (at %s) in %ld seconds. I think"
659                                               " it's dead, and I am evicting"
660                                               " it. exp %p, cur %ld expire %ld"
661                                               " last %ld\n",
662                                               obd->obd_name,
663                                               obd_uuid2str(&exp->exp_client_uuid),
664                                               obd_export_nid2str(exp),
665                                               (long)(cfs_time_current_sec() -
666                                                      exp->exp_last_request_time),
667                                               exp, (long)cfs_time_current_sec(),
668                                               (long)expire_time,
669                                               (long)exp->exp_last_request_time);
670                                 CDEBUG(D_HA, "Last request was at %ld\n",
671                                        exp->exp_last_request_time);
672                                 class_fail_export(exp);
673                                 class_export_put(exp);
674                                 cfs_spin_lock(&obd->obd_dev_lock);
675                         } else {
676                                 /* List is sorted, so everyone below is ok */
677                                 break;
678                         }
679                 }
680                 cfs_spin_unlock(&obd->obd_dev_lock);
681
682                 cfs_spin_lock(&pet_lock);
683                 cfs_list_del_init(&obd->obd_evict_list);
684                 cfs_spin_unlock(&pet_lock);
685
686                 class_decref(obd, "evictor", obd);
687         }
688         CDEBUG(D_HA, "Exiting Ping Evictor\n");
689
690         RETURN(0);
691 }
692
693 void ping_evictor_start(void)
694 {
695         int rc;
696
697         if (++pet_refcount > 1)
698                 return;
699
700         cfs_waitq_init(&pet_waitq);
701
702         rc = cfs_create_thread(ping_evictor_main, NULL, CFS_DAEMON_FLAGS);
703         if (rc < 0) {
704                 pet_refcount--;
705                 CERROR("Cannot start ping evictor thread: %d\n", rc);
706         }
707 }
708 EXPORT_SYMBOL(ping_evictor_start);
709
710 void ping_evictor_stop(void)
711 {
712         if (--pet_refcount > 0)
713                 return;
714
715         pet_state = PET_TERMINATE;
716         cfs_waitq_signal(&pet_waitq);
717 }
718 EXPORT_SYMBOL(ping_evictor_stop);
719 #else /* !__KERNEL__ */
720
721 /* XXX
722  * the current implementation of pinger in liblustre is not optimized
723  */
724
725 #ifdef ENABLE_PINGER
726 static struct pinger_data {
727         int             pd_recursion;
728         cfs_time_t      pd_this_ping;   /* jiffies */
729         cfs_time_t      pd_next_ping;   /* jiffies */
730         struct ptlrpc_request_set *pd_set;
731 } pinger_args;
732
733 static int pinger_check_rpcs(void *arg)
734 {
735         cfs_time_t curtime = cfs_time_current();
736         struct ptlrpc_request *req;
737         struct ptlrpc_request_set *set;
738         cfs_list_t *iter;
739         struct obd_import *imp;
740         struct pinger_data *pd = &pinger_args;
741         int rc;
742
743         /* prevent recursion */
744         if (pd->pd_recursion++) {
745                 CDEBUG(D_HA, "pinger: recursion! quit\n");
746                 LASSERT(pd->pd_set);
747                 pd->pd_recursion--;
748                 return 0;
749         }
750
751         /* have we reached ping point? */
752         if (!pd->pd_set && cfs_time_before(curtime, pd->pd_next_ping)) {
753                 pd->pd_recursion--;
754                 return 0;
755         }
756
757         /* if we have rpc_set already, continue processing it */
758         if (pd->pd_set) {
759                 LASSERT(pd->pd_this_ping);
760                 set = pd->pd_set;
761                 goto do_check_set;
762         }
763
764         pd->pd_this_ping = curtime;
765         pd->pd_set = ptlrpc_prep_set();
766         if (pd->pd_set == NULL)
767                 goto out;
768         set = pd->pd_set;
769
770         /* add rpcs into set */
771         cfs_mutex_lock(&pinger_mutex);
772         cfs_list_for_each(iter, &pinger_imports) {
773                 struct obd_import *imp = cfs_list_entry(iter, struct obd_import,
774                                                         imp_pinger_chain);
775                 int generation, level;
776
777                 if (cfs_time_aftereq(pd->pd_this_ping,
778                                      imp->imp_next_ping - 5 * CFS_TICK)) {
779                         /* Add a ping. */
780                         cfs_spin_lock(&imp->imp_lock);
781                         generation = imp->imp_generation;
782                         level = imp->imp_state;
783                         cfs_spin_unlock(&imp->imp_lock);
784
785                         if (level != LUSTRE_IMP_FULL) {
786                                 CDEBUG(D_HA,
787                                        "not pinging %s (in recovery)\n",
788                                        obd2cli_tgt(imp->imp_obd));
789                                 continue;
790                         }
791
792                         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
793                                                         LUSTRE_OBD_VERSION,
794                                                         OBD_PING);
795                         if (req == NULL) {
796                                 CERROR("OOM trying to ping %s->%s\n",
797                                        imp->imp_obd->obd_uuid.uuid,
798                                        obd2cli_tgt(imp->imp_obd));
799                                 break;
800                         }
801
802                         req->rq_no_resend = 1;
803                         ptlrpc_request_set_replen(req);
804                         req->rq_send_state = LUSTRE_IMP_FULL;
805                         ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
806                         req->rq_import_generation = generation;
807                         ptlrpc_set_add_req(set, req);
808                 } else {
809                         CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
810                                " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
811                                imp->imp_next_ping, pd->pd_this_ping);
812                 }
813         }
814         pd->pd_this_ping = curtime;
815         cfs_mutex_unlock(&pinger_mutex);
816
817         /* Might be empty, that's OK. */
818         if (cfs_atomic_read(&set->set_remaining) == 0)
819                 CDEBUG(D_RPCTRACE, "nothing to ping\n");
820
821         cfs_list_for_each(iter, &set->set_requests) {
822                 struct ptlrpc_request *req =
823                         cfs_list_entry(iter, struct ptlrpc_request,
824                                        rq_set_chain);
825                 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
826                           req->rq_import->imp_obd->obd_uuid.uuid,
827                           obd2cli_tgt(req->rq_import->imp_obd));
828                 (void)ptl_send_rpc(req, 0);
829         }
830
831 do_check_set:
832         rc = ptlrpc_check_set(NULL, set);
833
834         /* not finished, and we are not expired, simply return */
835         if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
836                                             cfs_time_seconds(PING_INTERVAL)))) {
837                 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
838                 pd->pd_recursion--;
839                 return 0;
840         }
841
842         /* Expire all the requests that didn't come back. */
843         cfs_mutex_lock(&pinger_mutex);
844         cfs_list_for_each(iter, &set->set_requests) {
845                 req = cfs_list_entry(iter, struct ptlrpc_request,
846                                      rq_set_chain);
847
848                 if (req->rq_phase == RQ_PHASE_COMPLETE)
849                         continue;
850
851                 CDEBUG(D_RPCTRACE, "Pinger initiate expire request(%p)\n",
852                        req);
853
854                 /* This will also unregister reply. */
855                 ptlrpc_expire_one_request(req, 0);
856
857                 /* We're done with this req, let's finally move it to complete
858                  * phase and take care of inflights. */
859                 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
860                 imp = req->rq_import;
861                 cfs_spin_lock(&imp->imp_lock);
862                 if (!cfs_list_empty(&req->rq_list)) {
863                         cfs_list_del_init(&req->rq_list);
864                         cfs_atomic_dec(&imp->imp_inflight);
865                 }
866                 cfs_spin_unlock(&imp->imp_lock);
867                 cfs_atomic_dec(&set->set_remaining);
868         }
869         cfs_mutex_unlock(&pinger_mutex);
870
871         ptlrpc_set_destroy(set);
872         pd->pd_set = NULL;
873
874 out:
875         pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
876                                         cfs_time_seconds(PING_INTERVAL));
877         pd->pd_this_ping = 0; /* XXX for debug */
878
879         CDEBUG(D_INFO, "finished a round ping\n");
880         pd->pd_recursion--;
881         return 0;
882 }
883
884 static void *pinger_callback = NULL;
885 #endif /* ENABLE_PINGER */
886
887 int ptlrpc_start_pinger(void)
888 {
889 #ifdef ENABLE_PINGER
890         memset(&pinger_args, 0, sizeof(pinger_args));
891         pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
892                                                            &pinger_check_rpcs,
893                                                            &pinger_args);
894 #endif
895         return 0;
896 }
897
898 int ptlrpc_stop_pinger(void)
899 {
900 #ifdef ENABLE_PINGER
901         if (pinger_callback)
902                 liblustre_deregister_wait_callback(pinger_callback);
903 #endif
904         return 0;
905 }
906
907 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
908 {
909 #ifdef ENABLE_PINGER
910         cfs_mutex_lock(&pinger_mutex);
911         ptlrpc_update_next_ping(imp, 0);
912         if (pinger_args.pd_set == NULL &&
913             cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
914                 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
915                         imp->imp_next_ping, cfs_time_current());
916                 pinger_args.pd_next_ping = imp->imp_next_ping;
917         }
918         cfs_mutex_unlock(&pinger_mutex);
919 #endif
920 }
921
922 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
923 {
924 #ifdef ENABLE_PINGER
925         cfs_mutex_lock(&pinger_mutex);
926         ptlrpc_update_next_ping(imp, 1);
927         if (pinger_args.pd_set == NULL &&
928             cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
929                 CDEBUG(D_HA,"set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
930                         imp->imp_next_ping, cfs_time_current());
931                 pinger_args.pd_next_ping = imp->imp_next_ping;
932         }
933         cfs_mutex_unlock(&pinger_mutex);
934 #endif
935 }
936
937 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
938                               timeout_cb_t cb, void *data,
939                               cfs_list_t *obd_list)
940 {
941         return 0;
942 }
943
944 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
945                               enum timeout_event event)
946 {
947         return 0;
948 }
949
950 int ptlrpc_pinger_add_import(struct obd_import *imp)
951 {
952         ENTRY;
953         if (!cfs_list_empty(&imp->imp_pinger_chain))
954                 RETURN(-EALREADY);
955
956         CDEBUG(D_HA, "adding pingable import %s->%s\n",
957                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
958         ptlrpc_pinger_sending_on_import(imp);
959
960         cfs_mutex_lock(&pinger_mutex);
961         cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
962         class_import_get(imp);
963         cfs_mutex_unlock(&pinger_mutex);
964
965         RETURN(0);
966 }
967
968 int ptlrpc_pinger_del_import(struct obd_import *imp)
969 {
970         ENTRY;
971         if (cfs_list_empty(&imp->imp_pinger_chain))
972                 RETURN(-ENOENT);
973
974         cfs_mutex_lock(&pinger_mutex);
975         cfs_list_del_init(&imp->imp_pinger_chain);
976         CDEBUG(D_HA, "removing pingable import %s->%s\n",
977                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
978         class_import_put(imp);
979         cfs_mutex_unlock(&pinger_mutex);
980         RETURN(0);
981 }
982
983 void ptlrpc_pinger_wake_up()
984 {
985 #ifdef ENABLE_PINGER
986         /* XXX force pinger to run, if needed */
987         struct obd_import *imp;
988         ENTRY;
989         cfs_list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
990                 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
991                        imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
992 #ifdef ENABLE_LIBLUSTRE_RECOVERY
993                 if (imp->imp_state == LUSTRE_IMP_DISCON &&
994                     !imp_is_deactive(imp))
995 #else
996                 /*XXX only recover for the initial connection */
997                 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
998                     imp->imp_state == LUSTRE_IMP_DISCON &&
999                     !imp_is_deactive(imp))
1000 #endif
1001                         ptlrpc_initiate_recovery(imp);
1002                 else if (imp->imp_state != LUSTRE_IMP_FULL)
1003                         CDEBUG(D_HA, "Refused to recover import %s->%s "
1004                                      "state %d, deactive %d\n",
1005                                      imp->imp_obd->obd_uuid.uuid,
1006                                      obd2cli_tgt(imp->imp_obd), imp->imp_state,
1007                                      imp_is_deactive(imp));
1008         }
1009         EXIT;
1010 #endif
1011 }
1012 #endif /* !__KERNEL__ */