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