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