Whamcloud - gitweb
LU-3963 ptlrpc: convert to linux list api
[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, 2013, Intel Corporation.
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 static int suppress_pings;
52 CFS_MODULE_PARM(suppress_pings, "i", int, 0644, "Suppress pings");
53
54 struct mutex pinger_mutex;
55 static struct list_head pinger_imports =
56                 LIST_HEAD_INIT(pinger_imports);
57 static struct list_head timeout_list =
58                 LIST_HEAD_INIT(timeout_list);
59
60 int ptlrpc_pinger_suppress_pings()
61 {
62         return suppress_pings;
63 }
64 EXPORT_SYMBOL(ptlrpc_pinger_suppress_pings);
65
66 struct ptlrpc_request *
67 ptlrpc_prep_ping(struct obd_import *imp)
68 {
69         struct ptlrpc_request *req;
70
71         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
72                                         LUSTRE_OBD_VERSION, OBD_PING);
73         if (req) {
74                 ptlrpc_request_set_replen(req);
75                 req->rq_no_resend = req->rq_no_delay = 1;
76         }
77         return req;
78 }
79
80 int ptlrpc_obd_ping(struct obd_device *obd)
81 {
82         int rc;
83         struct ptlrpc_request *req;
84         ENTRY;
85
86         req = ptlrpc_prep_ping(obd->u.cli.cl_import);
87         if (req == NULL)
88                 RETURN(-ENOMEM);
89
90         req->rq_send_state = LUSTRE_IMP_FULL;
91
92         rc = ptlrpc_queue_wait(req);
93
94         ptlrpc_req_finished(req);
95
96         RETURN(rc);
97 }
98 EXPORT_SYMBOL(ptlrpc_obd_ping);
99
100 int ptlrpc_ping(struct obd_import *imp)
101 {
102         struct ptlrpc_request *req;
103         ENTRY;
104
105         req = ptlrpc_prep_ping(imp);
106         if (req == NULL) {
107                 CERROR("OOM trying to ping %s->%s\n",
108                        imp->imp_obd->obd_uuid.uuid,
109                        obd2cli_tgt(imp->imp_obd));
110                 RETURN(-ENOMEM);
111         }
112
113         DEBUG_REQ(D_INFO, req, "pinging %s->%s",
114                   imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
115         ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
116
117         RETURN(0);
118 }
119
120 void ptlrpc_update_next_ping(struct obd_import *imp, int soon)
121 {
122 #ifdef ENABLE_PINGER
123         int time = soon ? PING_INTERVAL_SHORT : PING_INTERVAL;
124         if (imp->imp_state == LUSTRE_IMP_DISCON) {
125                 int dtime = max_t(int, CONNECTION_SWITCH_MIN,
126                                   AT_OFF ? 0 :
127                                   at_get(&imp->imp_at.iat_net_latency));
128                 time = min(time, dtime);
129         }
130         imp->imp_next_ping = cfs_time_shift(time);
131 #endif /* ENABLE_PINGER */
132 }
133
134 void ptlrpc_ping_import_soon(struct obd_import *imp)
135 {
136         imp->imp_next_ping = cfs_time_current();
137 }
138
139 static inline int imp_is_deactive(struct obd_import *imp)
140 {
141         return (imp->imp_deactive ||
142                 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_IMP_DEACTIVE));
143 }
144
145 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
146 {
147         if (imp->imp_server_timeout)
148                 return cfs_time_shift(obd_timeout / 2);
149         else
150                 return cfs_time_shift(obd_timeout);
151 }
152
153 cfs_duration_t pinger_check_timeout(cfs_time_t time)
154 {
155         struct timeout_item *item;
156         cfs_time_t timeout = PING_INTERVAL;
157
158         /* The timeout list is a increase order sorted list */
159         mutex_lock(&pinger_mutex);
160         list_for_each_entry(item, &timeout_list, ti_chain) {
161                 int ti_timeout = item->ti_timeout;
162                 if (timeout > ti_timeout)
163                         timeout = ti_timeout;
164                 break;
165         }
166         mutex_unlock(&pinger_mutex);
167
168         return cfs_time_sub(cfs_time_add(time, cfs_time_seconds(timeout)),
169                                          cfs_time_current());
170 }
171
172 #ifdef __KERNEL__
173
174 static bool ir_up;
175
176 void ptlrpc_pinger_ir_up(void)
177 {
178         CDEBUG(D_HA, "IR up\n");
179         ir_up = true;
180 }
181 EXPORT_SYMBOL(ptlrpc_pinger_ir_up);
182
183 void ptlrpc_pinger_ir_down(void)
184 {
185         CDEBUG(D_HA, "IR down\n");
186         ir_up = false;
187 }
188 EXPORT_SYMBOL(ptlrpc_pinger_ir_down);
189
190 static void ptlrpc_pinger_process_import(struct obd_import *imp,
191                                          unsigned long this_ping)
192 {
193         int level;
194         int force;
195         int force_next;
196         int suppress;
197
198         spin_lock(&imp->imp_lock);
199
200         level = imp->imp_state;
201         force = imp->imp_force_verify;
202         force_next = imp->imp_force_next_verify;
203         /*
204          * This will be used below only if the import is "FULL".
205          */
206         suppress = ir_up && OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS);
207
208         imp->imp_force_verify = 0;
209
210         if (cfs_time_aftereq(imp->imp_next_ping - 5 * CFS_TICK, this_ping) &&
211             !force) {
212                 spin_unlock(&imp->imp_lock);
213                 return;
214         }
215
216         imp->imp_force_next_verify = 0;
217
218         spin_unlock(&imp->imp_lock);
219
220         CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA, "%s->%s: level %s/%u "
221                "force %u force_next %u deactive %u pingable %u suppress %u\n",
222                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
223                ptlrpc_import_state_name(level), level, force, force_next,
224                imp->imp_deactive, imp->imp_pingable, suppress);
225
226         if (level == LUSTRE_IMP_DISCON && !imp_is_deactive(imp)) {
227                 /* wait for a while before trying recovery again */
228                 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
229                 if (!imp->imp_no_pinger_recover)
230                         ptlrpc_initiate_recovery(imp);
231         } else if (level != LUSTRE_IMP_FULL ||
232                    imp->imp_obd->obd_no_recov ||
233                    imp_is_deactive(imp)) {
234                 CDEBUG(D_HA, "%s->%s: not pinging (in recovery "
235                        "or recovery disabled: %s)\n",
236                        imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
237                        ptlrpc_import_state_name(level));
238                 if (force) {
239                         spin_lock(&imp->imp_lock);
240                         imp->imp_force_verify = 1;
241                         spin_unlock(&imp->imp_lock);
242                 }
243         } else if ((imp->imp_pingable && !suppress) || force_next || force) {
244                 ptlrpc_ping(imp);
245         }
246 }
247
248 static int ptlrpc_pinger_main(void *arg)
249 {
250         struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
251         ENTRY;
252
253         /* Record that the thread is running */
254         thread_set_flags(thread, SVC_RUNNING);
255         wake_up(&thread->t_ctl_waitq);
256
257         /* And now, loop forever, pinging as needed. */
258         while (1) {
259                 cfs_time_t this_ping = cfs_time_current();
260                 struct l_wait_info lwi;
261                 cfs_duration_t time_to_next_wake;
262                 struct timeout_item *item;
263                 struct list_head *iter;
264
265                 mutex_lock(&pinger_mutex);
266                 list_for_each_entry(item, &timeout_list, ti_chain)
267                         item->ti_cb(item, item->ti_cb_data);
268
269                 list_for_each(iter, &pinger_imports) {
270                         struct obd_import *imp = list_entry(iter,
271                                                             struct obd_import,
272                                                             imp_pinger_chain);
273
274                         ptlrpc_pinger_process_import(imp, this_ping);
275                         /* obd_timeout might have changed */
276                         if (imp->imp_pingable && imp->imp_next_ping &&
277                             cfs_time_after(imp->imp_next_ping,
278                                            cfs_time_add(this_ping,
279                                                         cfs_time_seconds(PING_INTERVAL))))
280                                 ptlrpc_update_next_ping(imp, 0);
281                 }
282                 mutex_unlock(&pinger_mutex);
283                 /* update memory usage info */
284                 obd_update_maxusage();
285
286                 /* Wait until the next ping time, or until we're stopped. */
287                 time_to_next_wake = pinger_check_timeout(this_ping);
288                 /* The ping sent by ptlrpc_send_rpc may get sent out
289                    say .01 second after this.
290                    ptlrpc_pinger_sending_on_import will then set the
291                    next ping time to next_ping + .01 sec, which means
292                    we will SKIP the next ping at next_ping, and the
293                    ping will get sent 2 timeouts from now!  Beware. */
294                 CDEBUG(D_INFO, "next wakeup in "CFS_DURATION_T" ("
295                        CFS_TIME_T")\n", time_to_next_wake,
296                        cfs_time_add(this_ping,cfs_time_seconds(PING_INTERVAL)));
297                 if (time_to_next_wake > 0) {
298                         lwi = LWI_TIMEOUT(max_t(cfs_duration_t,
299                                                 time_to_next_wake,
300                                                 cfs_time_seconds(1)),
301                                           NULL, NULL);
302                         l_wait_event(thread->t_ctl_waitq,
303                                      thread_is_stopping(thread) ||
304                                      thread_is_event(thread),
305                                      &lwi);
306                         if (thread_test_and_clear_flags(thread, SVC_STOPPING)) {
307                                 EXIT;
308                                 break;
309                         } else {
310                                 /* woken after adding import to reset timer */
311                                 thread_test_and_clear_flags(thread, SVC_EVENT);
312                         }
313                 }
314         }
315
316         thread_set_flags(thread, SVC_STOPPED);
317         wake_up(&thread->t_ctl_waitq);
318
319         CDEBUG(D_NET, "pinger thread exiting, process %d\n", current_pid());
320         return 0;
321 }
322
323 static struct ptlrpc_thread pinger_thread;
324
325 int ptlrpc_start_pinger(void)
326 {
327         struct l_wait_info lwi = { 0 };
328         struct task_struct *task;
329         int rc;
330 #ifndef ENABLE_PINGER
331         return 0;
332 #endif
333         ENTRY;
334
335         if (!thread_is_init(&pinger_thread) &&
336             !thread_is_stopped(&pinger_thread))
337                 RETURN(-EALREADY);
338
339         init_waitqueue_head(&pinger_thread.t_ctl_waitq);
340
341         strcpy(pinger_thread.t_name, "ll_ping");
342
343         /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
344          * just drop the VM and FILES in kthread_run() right away. */
345         task = kthread_run(ptlrpc_pinger_main, &pinger_thread,
346                            pinger_thread.t_name);
347         if (IS_ERR(task)) {
348                 rc = PTR_ERR(task);
349                 CERROR("cannot start pinger thread: rc = %d\n", rc);
350                 RETURN(rc);
351         }
352
353         l_wait_event(pinger_thread.t_ctl_waitq,
354                      thread_is_running(&pinger_thread), &lwi);
355
356         if (suppress_pings)
357                 CWARN("Pings will be suppressed at the request of the "
358                       "administrator.  The configuration shall meet the "
359                       "additional requirements described in the manual.  "
360                       "(Search for the \"suppress_pings\" kernel module "
361                       "parameter.)\n");
362
363         RETURN(0);
364 }
365
366 int ptlrpc_pinger_remove_timeouts(void);
367
368 int ptlrpc_stop_pinger(void)
369 {
370         struct l_wait_info lwi = { 0 };
371 #ifndef ENABLE_PINGER
372         return 0;
373 #endif
374         ENTRY;
375
376         if (thread_is_init(&pinger_thread) ||
377             thread_is_stopped(&pinger_thread))
378                 RETURN(-EALREADY);
379
380         ptlrpc_pinger_remove_timeouts();
381
382         thread_set_flags(&pinger_thread, SVC_STOPPING);
383         wake_up(&pinger_thread.t_ctl_waitq);
384
385         l_wait_event(pinger_thread.t_ctl_waitq,
386                      thread_is_stopped(&pinger_thread), &lwi);
387         RETURN(0);
388 }
389
390 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
391 {
392         ptlrpc_update_next_ping(imp, 0);
393 }
394 EXPORT_SYMBOL(ptlrpc_pinger_sending_on_import);
395
396 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
397 {
398         ptlrpc_update_next_ping(imp, 1);
399         assert_spin_locked(&imp->imp_lock);
400         /*
401          * Avoid reading stale imp_connect_data.  When not sure if pings are
402          * expected or not on next connection, we assume they are not and force
403          * one anyway to guarantee the chance of updating
404          * imp_peer_committed_transno.
405          */
406         if (imp->imp_state != LUSTRE_IMP_FULL ||
407             OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS))
408                 imp->imp_force_next_verify = 1;
409 }
410
411 int ptlrpc_pinger_add_import(struct obd_import *imp)
412 {
413         ENTRY;
414         if (!list_empty(&imp->imp_pinger_chain))
415                 RETURN(-EALREADY);
416
417         mutex_lock(&pinger_mutex);
418         CDEBUG(D_HA, "adding pingable import %s->%s\n",
419                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
420         /* if we add to pinger we want recovery on this import */
421         imp->imp_obd->obd_no_recov = 0;
422         ptlrpc_update_next_ping(imp, 0);
423         /* XXX sort, blah blah */
424         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
425         class_import_get(imp);
426
427         ptlrpc_pinger_wake_up();
428         mutex_unlock(&pinger_mutex);
429
430         RETURN(0);
431 }
432 EXPORT_SYMBOL(ptlrpc_pinger_add_import);
433
434 int ptlrpc_pinger_del_import(struct obd_import *imp)
435 {
436         ENTRY;
437
438         if (list_empty(&imp->imp_pinger_chain))
439                 RETURN(-ENOENT);
440
441         mutex_lock(&pinger_mutex);
442         list_del_init(&imp->imp_pinger_chain);
443         CDEBUG(D_HA, "removing pingable import %s->%s\n",
444                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
445         /* if we remove from pinger we don't want recovery on this import */
446         imp->imp_obd->obd_no_recov = 1;
447         class_import_put(imp);
448         mutex_unlock(&pinger_mutex);
449         RETURN(0);
450 }
451 EXPORT_SYMBOL(ptlrpc_pinger_del_import);
452
453 /**
454  * Register a timeout callback to the pinger list, and the callback will
455  * be called when timeout happens.
456  */
457 struct timeout_item* ptlrpc_new_timeout(int time, enum timeout_event event,
458                                         timeout_cb_t cb, void *data)
459 {
460         struct timeout_item *ti;
461
462         OBD_ALLOC_PTR(ti);
463         if (!ti)
464                 return(NULL);
465
466         INIT_LIST_HEAD(&ti->ti_obd_list);
467         INIT_LIST_HEAD(&ti->ti_chain);
468         ti->ti_timeout = time;
469         ti->ti_event = event;
470         ti->ti_cb = cb;
471         ti->ti_cb_data = data;
472
473         return ti;
474 }
475
476 /**
477  * Register timeout event on the the pinger thread.
478  * Note: the timeout list is an sorted list with increased timeout value.
479  */
480 static struct timeout_item*
481 ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
482                                timeout_cb_t cb, void *data)
483 {
484         struct timeout_item *item, *tmp;
485
486         LASSERT(mutex_is_locked(&pinger_mutex));
487
488         list_for_each_entry(item, &timeout_list, ti_chain)
489                 if (item->ti_event == event)
490                         goto out;
491
492         item = ptlrpc_new_timeout(time, event, cb, data);
493         if (item) {
494                 list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
495                         if (tmp->ti_timeout < time) {
496                                 list_add(&item->ti_chain, &tmp->ti_chain);
497                                 goto out;
498                         }
499                 }
500                 list_add(&item->ti_chain, &timeout_list);
501         }
502 out:
503         return item;
504 }
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_lock(&pinger_mutex);
516         ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
517         if (!ti) {
518                 mutex_unlock(&pinger_mutex);
519                 return (-EINVAL);
520         }
521         list_add(obd_list, &ti->ti_obd_list);
522         mutex_unlock(&pinger_mutex);
523         return 0;
524 }
525 EXPORT_SYMBOL(ptlrpc_add_timeout_client);
526
527 int ptlrpc_del_timeout_client(struct list_head *obd_list,
528                               enum timeout_event event)
529 {
530         struct timeout_item *ti = NULL, *item;
531
532         if (list_empty(obd_list))
533                 return 0;
534         mutex_lock(&pinger_mutex);
535         list_del_init(obd_list);
536         /**
537          * If there are no obd attached to the timeout event
538          * list, remove this timeout event from the pinger
539          */
540         list_for_each_entry(item, &timeout_list, ti_chain) {
541                 if (item->ti_event == event) {
542                         ti = item;
543                         break;
544                 }
545         }
546         LASSERTF(ti != NULL, "ti is NULL !\n");
547         if (list_empty(&ti->ti_obd_list)) {
548                 list_del(&ti->ti_chain);
549                 OBD_FREE_PTR(ti);
550         }
551         mutex_unlock(&pinger_mutex);
552         return 0;
553 }
554 EXPORT_SYMBOL(ptlrpc_del_timeout_client);
555
556 int ptlrpc_pinger_remove_timeouts(void)
557 {
558         struct timeout_item *item, *tmp;
559
560         mutex_lock(&pinger_mutex);
561         list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
562                 LASSERT(list_empty(&item->ti_obd_list));
563                 list_del(&item->ti_chain);
564                 OBD_FREE_PTR(item);
565         }
566         mutex_unlock(&pinger_mutex);
567         return 0;
568 }
569
570 void ptlrpc_pinger_wake_up()
571 {
572 #ifdef ENABLE_PINGER
573         thread_add_flags(&pinger_thread, SVC_EVENT);
574         wake_up(&pinger_thread.t_ctl_waitq);
575 #endif
576 }
577
578 /* Ping evictor thread */
579 #define PET_READY     1
580 #define PET_TERMINATE 2
581
582 static int               pet_refcount = 0;
583 static int               pet_state;
584 static wait_queue_head_t pet_waitq;
585 struct list_head         pet_list;
586 static DEFINE_SPINLOCK(pet_lock);
587
588 int ping_evictor_wake(struct obd_export *exp)
589 {
590         struct obd_device *obd;
591
592         spin_lock(&pet_lock);
593         if (pet_state != PET_READY) {
594                 /* eventually the new obd will call here again. */
595                 spin_unlock(&pet_lock);
596                 return 1;
597         }
598
599         obd = class_exp2obd(exp);
600         if (list_empty(&obd->obd_evict_list)) {
601                 class_incref(obd, "evictor", obd);
602                 list_add(&obd->obd_evict_list, &pet_list);
603         }
604         spin_unlock(&pet_lock);
605
606         wake_up(&pet_waitq);
607         return 0;
608 }
609
610 static int ping_evictor_main(void *arg)
611 {
612         struct obd_device *obd;
613         struct obd_export *exp;
614         struct l_wait_info lwi = { 0 };
615         time_t expire_time;
616         ENTRY;
617
618         unshare_fs_struct();
619
620         CDEBUG(D_HA, "Starting Ping Evictor\n");
621         pet_state = PET_READY;
622         while (1) {
623                 l_wait_event(pet_waitq, (!list_empty(&pet_list)) ||
624                             (pet_state == PET_TERMINATE), &lwi);
625
626                 /* loop until all obd's will be removed */
627                 if ((pet_state == PET_TERMINATE) && list_empty(&pet_list))
628                         break;
629
630                 /* we only get here if pet_exp != NULL, and the end of this
631                  * loop is the only place which sets it NULL again, so lock
632                  * is not strictly necessary. */
633                 spin_lock(&pet_lock);
634                 obd = list_entry(pet_list.next, struct obd_device,
635                                  obd_evict_list);
636                 spin_unlock(&pet_lock);
637
638                 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
639
640                 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
641                        obd->obd_name, expire_time);
642
643                 /* Exports can't be deleted out of the list while we hold
644                  * the obd lock (class_unlink_export), which means we can't
645                  * lose the last ref on the export.  If they've already been
646                  * removed from the list, we won't find them here. */
647                 spin_lock(&obd->obd_dev_lock);
648                 while (!list_empty(&obd->obd_exports_timed)) {
649                         exp = list_entry(obd->obd_exports_timed.next,
650                                          struct obd_export,
651                                          exp_obd_chain_timed);
652                         if (expire_time > exp->exp_last_request_time) {
653                                 class_export_get(exp);
654                                 spin_unlock(&obd->obd_dev_lock);
655                                 LCONSOLE_WARN("%s: haven't heard from client %s"
656                                               " (at %s) in %ld seconds. I think"
657                                               " it's dead, and I am evicting"
658                                               " it. exp %p, cur %ld expire %ld"
659                                               " last %ld\n",
660                                               obd->obd_name,
661                                               obd_uuid2str(&exp->exp_client_uuid),
662                                               obd_export_nid2str(exp),
663                                               (long)(cfs_time_current_sec() -
664                                                      exp->exp_last_request_time),
665                                               exp, (long)cfs_time_current_sec(),
666                                               (long)expire_time,
667                                               (long)exp->exp_last_request_time);
668                                 CDEBUG(D_HA, "Last request was at %ld\n",
669                                        exp->exp_last_request_time);
670                                 class_fail_export(exp);
671                                 class_export_put(exp);
672                                 spin_lock(&obd->obd_dev_lock);
673                         } else {
674                                 /* List is sorted, so everyone below is ok */
675                                 break;
676                         }
677                 }
678                 spin_unlock(&obd->obd_dev_lock);
679
680                 spin_lock(&pet_lock);
681                 list_del_init(&obd->obd_evict_list);
682                 spin_unlock(&pet_lock);
683
684                 class_decref(obd, "evictor", obd);
685         }
686         CDEBUG(D_HA, "Exiting Ping Evictor\n");
687
688         RETURN(0);
689 }
690
691 void ping_evictor_start(void)
692 {
693         struct task_struct *task;
694
695         if (++pet_refcount > 1)
696                 return;
697
698         INIT_LIST_HEAD(&pet_list);
699         init_waitqueue_head(&pet_waitq);
700
701         task = kthread_run(ping_evictor_main, NULL, "ll_evictor");
702         if (IS_ERR(task)) {
703                 pet_refcount--;
704                 CERROR("Cannot start ping evictor thread: %ld\n",
705                         PTR_ERR(task));
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         wake_up(&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         struct list_head *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         mutex_lock(&pinger_mutex);
772         list_for_each(iter, &pinger_imports) {
773                 struct obd_import *imp = 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                         spin_lock(&imp->imp_lock);
781                         generation = imp->imp_generation;
782                         level = imp->imp_state;
783                         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         mutex_unlock(&pinger_mutex);
816
817         /* Might be empty, that's OK. */
818         if (atomic_read(&set->set_remaining) == 0)
819                 CDEBUG(D_RPCTRACE, "nothing to ping\n");
820
821         list_for_each(iter, &set->set_requests) {
822                 struct ptlrpc_request *req =
823                         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         mutex_lock(&pinger_mutex);
844         list_for_each(iter, &set->set_requests) {
845                 req = 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                 spin_lock(&imp->imp_lock);
862                 if (!list_empty(&req->rq_list)) {
863                         list_del_init(&req->rq_list);
864                         atomic_dec(&imp->imp_inflight);
865                 }
866                 spin_unlock(&imp->imp_lock);
867                 atomic_dec(&set->set_remaining);
868         }
869         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         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         mutex_unlock(&pinger_mutex);
919 #endif
920 }
921
922 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
923 {
924 #ifdef ENABLE_PINGER
925         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         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                               struct list_head *obd_list)
940 {
941         return 0;
942 }
943
944 int ptlrpc_del_timeout_client(struct list_head *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
954         if (!list_empty(&imp->imp_pinger_chain))
955                 RETURN(-EALREADY);
956
957         CDEBUG(D_HA, "adding pingable import %s->%s\n",
958                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
959         ptlrpc_pinger_sending_on_import(imp);
960
961         mutex_lock(&pinger_mutex);
962         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
963         class_import_get(imp);
964         mutex_unlock(&pinger_mutex);
965
966         RETURN(0);
967 }
968
969 int ptlrpc_pinger_del_import(struct obd_import *imp)
970 {
971         ENTRY;
972
973         if (list_empty(&imp->imp_pinger_chain))
974                 RETURN(-ENOENT);
975
976         mutex_lock(&pinger_mutex);
977         list_del_init(&imp->imp_pinger_chain);
978         CDEBUG(D_HA, "removing pingable import %s->%s\n",
979                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
980         class_import_put(imp);
981         mutex_unlock(&pinger_mutex);
982         RETURN(0);
983 }
984
985 void ptlrpc_pinger_wake_up()
986 {
987 #ifdef ENABLE_PINGER
988         /* XXX force pinger to run, if needed */
989         struct obd_import *imp;
990         ENTRY;
991         list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
992                 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
993                        imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
994 #ifdef ENABLE_LIBLUSTRE_RECOVERY
995                 if (imp->imp_state == LUSTRE_IMP_DISCON &&
996                     !imp_is_deactive(imp))
997 #else
998                 /*XXX only recover for the initial connection */
999                 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
1000                     imp->imp_state == LUSTRE_IMP_DISCON &&
1001                     !imp_is_deactive(imp))
1002 #endif
1003                         ptlrpc_initiate_recovery(imp);
1004                 else if (imp->imp_state != LUSTRE_IMP_FULL)
1005                         CDEBUG(D_HA, "Refused to recover import %s->%s "
1006                                      "state %d, deactive %d\n",
1007                                      imp->imp_obd->obd_uuid.uuid,
1008                                      obd2cli_tgt(imp->imp_obd), imp->imp_state,
1009                                      imp_is_deactive(imp));
1010         }
1011         EXIT;
1012 #endif
1013 }
1014 #endif /* !__KERNEL__ */