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