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