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