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