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