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