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