Whamcloud - gitweb
82516367ecaf46496b3b0afa8dfdd226c4350397
[fs/lustre-release.git] / lustre / ptlrpc / pinger.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/pinger.c
37  *
38  * Portal-RPC reconnection and replay operations, for use in recovery.
39  */
40
41 #ifndef __KERNEL__
42 #include <liblustre.h>
43 #else
44 #define DEBUG_SUBSYSTEM S_RPC
45 #endif
46
47 #include <obd_support.h>
48 #include <obd_class.h>
49 #include "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, int soon)
109 {
110 #ifdef ENABLE_PINGER
111         int time = soon ? PING_INTERVAL_SHORT : 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, 0);
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_create_thread(ptlrpc_pinger_main, &d, CFS_DAEMON_FLAGS);
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, 0);
410 }
411
412 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
413 {
414         ptlrpc_update_next_ping(imp, 1);
415 }
416
417 int ptlrpc_pinger_add_import(struct obd_import *imp)
418 {
419         ENTRY;
420         if (!cfs_list_empty(&imp->imp_pinger_chain))
421                 RETURN(-EALREADY);
422
423         cfs_mutex_down(&pinger_sem);
424         CDEBUG(D_HA, "adding pingable import %s->%s\n",
425                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
426         /* if we add to pinger we want recovery on this import */
427         imp->imp_obd->obd_no_recov = 0;
428         ptlrpc_update_next_ping(imp, 0);
429         /* XXX sort, blah blah */
430         cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
431         class_import_get(imp);
432
433         ptlrpc_pinger_wake_up();
434         cfs_mutex_up(&pinger_sem);
435
436         RETURN(0);
437 }
438
439 int ptlrpc_pinger_del_import(struct obd_import *imp)
440 {
441         ENTRY;
442         if (cfs_list_empty(&imp->imp_pinger_chain))
443                 RETURN(-ENOENT);
444
445         cfs_mutex_down(&pinger_sem);
446         cfs_list_del_init(&imp->imp_pinger_chain);
447         CDEBUG(D_HA, "removing pingable import %s->%s\n",
448                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
449         /* if we remove from pinger we don't want recovery on this import */
450         imp->imp_obd->obd_no_recov = 1;
451         class_import_put(imp);
452         cfs_mutex_up(&pinger_sem);
453         RETURN(0);
454 }
455
456 /**
457  * Register a timeout callback to the pinger list, and the callback will
458  * be called when timeout happens.
459  */
460 struct timeout_item* ptlrpc_new_timeout(int time, enum timeout_event event,
461                                         timeout_cb_t cb, void *data)
462 {
463         struct timeout_item *ti;
464
465         OBD_ALLOC_PTR(ti);
466         if (!ti)
467                 return(NULL);
468
469         CFS_INIT_LIST_HEAD(&ti->ti_obd_list);
470         CFS_INIT_LIST_HEAD(&ti->ti_chain);
471         ti->ti_timeout = time;
472         ti->ti_event = event;
473         ti->ti_cb = cb;
474         ti->ti_cb_data = data;
475
476         return ti;
477 }
478
479 /**
480  * Register timeout event on the the pinger thread.
481  * Note: the timeout list is an sorted list with increased timeout value.
482  */
483 static struct timeout_item*
484 ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
485                                timeout_cb_t cb, void *data)
486 {
487         struct timeout_item *item, *tmp;
488
489         LASSERT_SEM_LOCKED(&pinger_sem);
490
491         cfs_list_for_each_entry(item, &timeout_list, ti_chain)
492                 if (item->ti_event == event)
493                         goto out;
494
495         item = ptlrpc_new_timeout(time, event, cb, data);
496         if (item) {
497                 cfs_list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
498                         if (tmp->ti_timeout < time) {
499                                 cfs_list_add(&item->ti_chain, &tmp->ti_chain);
500                                 goto out;
501                         }
502                 }
503                 cfs_list_add(&item->ti_chain, &timeout_list);
504         }
505 out:
506         return item;
507 }
508
509 /* Add a client_obd to the timeout event list, when timeout(@time)
510  * happens, the callback(@cb) will be called.
511  */
512 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
513                               timeout_cb_t cb, void *data,
514                               cfs_list_t *obd_list)
515 {
516         struct timeout_item *ti;
517
518         cfs_mutex_down(&pinger_sem);
519         ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
520         if (!ti) {
521                 cfs_mutex_up(&pinger_sem);
522                 return (-EINVAL);
523         }
524         cfs_list_add(obd_list, &ti->ti_obd_list);
525         cfs_mutex_up(&pinger_sem);
526         return 0;
527 }
528
529 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
530                               enum timeout_event event)
531 {
532         struct timeout_item *ti = NULL, *item;
533
534         if (cfs_list_empty(obd_list))
535                 return 0;
536         cfs_mutex_down(&pinger_sem);
537         cfs_list_del_init(obd_list);
538         /**
539          * If there are no obd attached to the timeout event
540          * list, remove this timeout event from the pinger
541          */
542         cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
543                 if (item->ti_event == event) {
544                         ti = item;
545                         break;
546                 }
547         }
548         LASSERTF(ti != NULL, "ti is NULL ! \n");
549         if (cfs_list_empty(&ti->ti_obd_list)) {
550                 cfs_list_del(&ti->ti_chain);
551                 OBD_FREE_PTR(ti);
552         }
553         cfs_mutex_up(&pinger_sem);
554         return 0;
555 }
556
557 int ptlrpc_pinger_remove_timeouts(void)
558 {
559         struct timeout_item *item, *tmp;
560
561         cfs_mutex_down(&pinger_sem);
562         cfs_list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
563                 LASSERT(cfs_list_empty(&item->ti_obd_list));
564                 cfs_list_del(&item->ti_chain);
565                 OBD_FREE_PTR(item);
566         }
567         cfs_mutex_up(&pinger_sem);
568         return 0;
569 }
570
571 void ptlrpc_pinger_wake_up()
572 {
573 #ifdef ENABLE_PINGER
574         pinger_thread->t_flags |= SVC_EVENT;
575         cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
576 #endif
577 }
578
579 /* Ping evictor thread */
580 #define PET_READY     1
581 #define PET_TERMINATE 2
582
583 static int               pet_refcount = 0;
584 static int               pet_state;
585 static cfs_waitq_t       pet_waitq;
586 CFS_LIST_HEAD(pet_list);
587 static cfs_spinlock_t    pet_lock = CFS_SPIN_LOCK_UNLOCKED;
588
589 int ping_evictor_wake(struct obd_export *exp)
590 {
591         struct obd_device *obd;
592
593         cfs_spin_lock(&pet_lock);
594         if (pet_state != PET_READY) {
595                 /* eventually the new obd will call here again. */
596                 cfs_spin_unlock(&pet_lock);
597                 return 1;
598         }
599
600         obd = class_exp2obd(exp);
601         if (cfs_list_empty(&obd->obd_evict_list)) {
602                 class_incref(obd, "evictor", obd);
603                 cfs_list_add(&obd->obd_evict_list, &pet_list);
604         }
605         cfs_spin_unlock(&pet_lock);
606
607         cfs_waitq_signal(&pet_waitq);
608         return 0;
609 }
610
611 static int ping_evictor_main(void *arg)
612 {
613         struct obd_device *obd;
614         struct obd_export *exp;
615         struct l_wait_info lwi = { 0 };
616         time_t expire_time;
617         ENTRY;
618
619         cfs_daemonize_ctxt("ll_evictor");
620
621         CDEBUG(D_HA, "Starting Ping Evictor\n");
622         pet_state = PET_READY;
623         while (1) {
624                 l_wait_event(pet_waitq, (!cfs_list_empty(&pet_list)) ||
625                              (pet_state == PET_TERMINATE), &lwi);
626
627                 /* loop until all obd's will be removed */
628                 if ((pet_state == PET_TERMINATE) && cfs_list_empty(&pet_list))
629                         break;
630
631                 /* we only get here if pet_exp != NULL, and the end of this
632                  * loop is the only place which sets it NULL again, so lock
633                  * is not strictly necessary. */
634                 cfs_spin_lock(&pet_lock);
635                 obd = cfs_list_entry(pet_list.next, struct obd_device,
636                                      obd_evict_list);
637                 cfs_spin_unlock(&pet_lock);
638
639                 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
640
641                 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
642                        obd->obd_name, expire_time);
643
644                 /* Exports can't be deleted out of the list while we hold
645                  * the obd lock (class_unlink_export), which means we can't
646                  * lose the last ref on the export.  If they've already been
647                  * removed from the list, we won't find them here. */
648                 cfs_spin_lock(&obd->obd_dev_lock);
649                 while (!cfs_list_empty(&obd->obd_exports_timed)) {
650                         exp = cfs_list_entry(obd->obd_exports_timed.next,
651                                              struct obd_export,
652                                              exp_obd_chain_timed);
653                         if (expire_time > exp->exp_last_request_time) {
654                                 class_export_get(exp);
655                                 cfs_spin_unlock(&obd->obd_dev_lock);
656                                  LCONSOLE_WARN("%s: haven't heard from client %s"
657                                               " (at %s) in %ld seconds. I think"
658                                               " it's dead, and I am evicting"
659                                               " it. exp %p, cur %ld expire %ld"
660                                               " last %ld\n",
661                                               obd->obd_name,
662                                               obd_uuid2str(&exp->exp_client_uuid),
663                                               obd_export_nid2str(exp),
664                                               (long)(cfs_time_current_sec() -
665                                                      exp->exp_last_request_time),
666                                               exp, (long)cfs_time_current_sec(),
667                                               (long)expire_time,
668                                               (long)exp->exp_last_request_time);
669                                 CDEBUG(D_HA, "Last request was at %ld\n",
670                                        exp->exp_last_request_time);
671                                 class_fail_export(exp);
672                                 class_export_put(exp);
673                                 cfs_spin_lock(&obd->obd_dev_lock);
674                         } else {
675                                 /* List is sorted, so everyone below is ok */
676                                 break;
677                         }
678                 }
679                 cfs_spin_unlock(&obd->obd_dev_lock);
680
681                 cfs_spin_lock(&pet_lock);
682                 cfs_list_del_init(&obd->obd_evict_list);
683                 cfs_spin_unlock(&pet_lock);
684
685                 class_decref(obd, "evictor", obd);
686         }
687         CDEBUG(D_HA, "Exiting Ping Evictor\n");
688
689         RETURN(0);
690 }
691
692 void ping_evictor_start(void)
693 {
694         int rc;
695
696         if (++pet_refcount > 1)
697                 return;
698
699         cfs_waitq_init(&pet_waitq);
700
701         rc = cfs_create_thread(ping_evictor_main, NULL, CFS_DAEMON_FLAGS);
702         if (rc < 0) {
703                 pet_refcount--;
704                 CERROR("Cannot start ping evictor thread: %d\n", rc);
705         }
706 }
707 EXPORT_SYMBOL(ping_evictor_start);
708
709 void ping_evictor_stop(void)
710 {
711         if (--pet_refcount > 0)
712                 return;
713
714         pet_state = PET_TERMINATE;
715         cfs_waitq_signal(&pet_waitq);
716 }
717 EXPORT_SYMBOL(ping_evictor_stop);
718 #else /* !__KERNEL__ */
719
720 /* XXX
721  * the current implementation of pinger in liblustre is not optimized
722  */
723
724 #ifdef ENABLE_PINGER
725 static struct pinger_data {
726         int             pd_recursion;
727         cfs_time_t      pd_this_ping;   /* jiffies */
728         cfs_time_t      pd_next_ping;   /* jiffies */
729         struct ptlrpc_request_set *pd_set;
730 } pinger_args;
731
732 static int pinger_check_rpcs(void *arg)
733 {
734         cfs_time_t curtime = cfs_time_current();
735         struct ptlrpc_request *req;
736         struct ptlrpc_request_set *set;
737         cfs_list_t *iter;
738         struct obd_import *imp;
739         struct pinger_data *pd = &pinger_args;
740         int rc;
741
742         /* prevent recursion */
743         if (pd->pd_recursion++) {
744                 CDEBUG(D_HA, "pinger: recursion! quit\n");
745                 LASSERT(pd->pd_set);
746                 pd->pd_recursion--;
747                 return 0;
748         }
749
750         /* have we reached ping point? */
751         if (!pd->pd_set && cfs_time_before(curtime, pd->pd_next_ping)) {
752                 pd->pd_recursion--;
753                 return 0;
754         }
755
756         /* if we have rpc_set already, continue processing it */
757         if (pd->pd_set) {
758                 LASSERT(pd->pd_this_ping);
759                 set = pd->pd_set;
760                 goto do_check_set;
761         }
762
763         pd->pd_this_ping = curtime;
764         pd->pd_set = ptlrpc_prep_set();
765         if (pd->pd_set == NULL)
766                 goto out;
767         set = pd->pd_set;
768
769         /* add rpcs into set */
770         cfs_mutex_down(&pinger_sem);
771         cfs_list_for_each(iter, &pinger_imports) {
772                 struct obd_import *imp = cfs_list_entry(iter, struct obd_import,
773                                                         imp_pinger_chain);
774                 int generation, level;
775
776                 if (cfs_time_aftereq(pd->pd_this_ping,
777                                      imp->imp_next_ping - 5 * CFS_TICK)) {
778                         /* Add a ping. */
779                         cfs_spin_lock(&imp->imp_lock);
780                         generation = imp->imp_generation;
781                         level = imp->imp_state;
782                         cfs_spin_unlock(&imp->imp_lock);
783
784                         if (level != LUSTRE_IMP_FULL) {
785                                 CDEBUG(D_HA,
786                                        "not pinging %s (in recovery)\n",
787                                        obd2cli_tgt(imp->imp_obd));
788                                 continue;
789                         }
790
791                         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
792                                                         LUSTRE_OBD_VERSION,
793                                                         OBD_PING);
794                         if (req == NULL) {
795                                 CERROR("OOM trying to ping %s->%s\n",
796                                        imp->imp_obd->obd_uuid.uuid,
797                                        obd2cli_tgt(imp->imp_obd));
798                                 break;
799                         }
800
801                         req->rq_no_resend = 1;
802                         ptlrpc_request_set_replen(req);
803                         req->rq_send_state = LUSTRE_IMP_FULL;
804                         ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
805                         req->rq_import_generation = generation;
806                         ptlrpc_set_add_req(set, req);
807                 } else {
808                         CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
809                                " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
810                                imp->imp_next_ping, pd->pd_this_ping);
811                 }
812         }
813         pd->pd_this_ping = curtime;
814         cfs_mutex_up(&pinger_sem);
815
816         /* Might be empty, that's OK. */
817         if (cfs_atomic_read(&set->set_remaining) == 0)
818                 CDEBUG(D_RPCTRACE, "nothing to ping\n");
819
820         cfs_list_for_each(iter, &set->set_requests) {
821                 struct ptlrpc_request *req =
822                         cfs_list_entry(iter, struct ptlrpc_request,
823                                        rq_set_chain);
824                 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
825                           req->rq_import->imp_obd->obd_uuid.uuid,
826                           obd2cli_tgt(req->rq_import->imp_obd));
827                 (void)ptl_send_rpc(req, 0);
828         }
829
830 do_check_set:
831         rc = ptlrpc_check_set(NULL, set);
832
833         /* not finished, and we are not expired, simply return */
834         if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
835                                             cfs_time_seconds(PING_INTERVAL)))) {
836                 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
837                 pd->pd_recursion--;
838                 return 0;
839         }
840
841         /* Expire all the requests that didn't come back. */
842         cfs_mutex_down(&pinger_sem);
843         cfs_list_for_each(iter, &set->set_requests) {
844                 req = cfs_list_entry(iter, struct ptlrpc_request,
845                                      rq_set_chain);
846
847                 if (req->rq_phase == RQ_PHASE_COMPLETE)
848                         continue;
849
850                 CDEBUG(D_RPCTRACE, "Pinger initiate expire request(%p)\n",
851                        req);
852
853                 /* This will also unregister reply. */
854                 ptlrpc_expire_one_request(req, 0);
855
856                 /* We're done with this req, let's finally move it to complete
857                  * phase and take care of inflights. */
858                 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
859                 imp = req->rq_import;
860                 cfs_spin_lock(&imp->imp_lock);
861                 if (!cfs_list_empty(&req->rq_list)) {
862                         cfs_list_del_init(&req->rq_list);
863                         cfs_atomic_dec(&imp->imp_inflight);
864                 }
865                 cfs_spin_unlock(&imp->imp_lock);
866                 cfs_atomic_dec(&set->set_remaining);
867         }
868         cfs_mutex_up(&pinger_sem);
869
870         ptlrpc_set_destroy(set);
871         pd->pd_set = NULL;
872
873 out:
874         pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
875                                         cfs_time_seconds(PING_INTERVAL));
876         pd->pd_this_ping = 0; /* XXX for debug */
877
878         CDEBUG(D_INFO, "finished a round ping\n");
879         pd->pd_recursion--;
880         return 0;
881 }
882
883 static void *pinger_callback = NULL;
884 #endif /* ENABLE_PINGER */
885
886 int ptlrpc_start_pinger(void)
887 {
888 #ifdef ENABLE_PINGER
889         memset(&pinger_args, 0, sizeof(pinger_args));
890         pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
891                                                            &pinger_check_rpcs,
892                                                            &pinger_args);
893 #endif
894         return 0;
895 }
896
897 int ptlrpc_stop_pinger(void)
898 {
899 #ifdef ENABLE_PINGER
900         if (pinger_callback)
901                 liblustre_deregister_wait_callback(pinger_callback);
902 #endif
903         return 0;
904 }
905
906 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
907 {
908 #ifdef ENABLE_PINGER
909         cfs_mutex_down(&pinger_sem);
910         ptlrpc_update_next_ping(imp, 0);
911         if (pinger_args.pd_set == NULL &&
912             cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
913                 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
914                         imp->imp_next_ping, cfs_time_current());
915                 pinger_args.pd_next_ping = imp->imp_next_ping;
916         }
917         cfs_mutex_up(&pinger_sem);
918 #endif
919 }
920
921 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
922 {
923 #ifdef ENABLE_PINGER
924         cfs_mutex_down(&pinger_sem);
925         ptlrpc_update_next_ping(imp, 1);
926         if (pinger_args.pd_set == NULL &&
927             cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
928                 CDEBUG(D_HA,"set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
929                         imp->imp_next_ping, cfs_time_current());
930                 pinger_args.pd_next_ping = imp->imp_next_ping;
931         }
932         cfs_mutex_up(&pinger_sem);
933 #endif
934 }
935
936 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
937                               timeout_cb_t cb, void *data,
938                               cfs_list_t *obd_list)
939 {
940         return 0;
941 }
942
943 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
944                               enum timeout_event event)
945 {
946         return 0;
947 }
948
949 int ptlrpc_pinger_add_import(struct obd_import *imp)
950 {
951         ENTRY;
952         if (!cfs_list_empty(&imp->imp_pinger_chain))
953                 RETURN(-EALREADY);
954
955         CDEBUG(D_HA, "adding pingable import %s->%s\n",
956                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
957         ptlrpc_pinger_sending_on_import(imp);
958
959         cfs_mutex_down(&pinger_sem);
960         cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
961         class_import_get(imp);
962         cfs_mutex_up(&pinger_sem);
963
964         RETURN(0);
965 }
966
967 int ptlrpc_pinger_del_import(struct obd_import *imp)
968 {
969         ENTRY;
970         if (cfs_list_empty(&imp->imp_pinger_chain))
971                 RETURN(-ENOENT);
972
973         cfs_mutex_down(&pinger_sem);
974         cfs_list_del_init(&imp->imp_pinger_chain);
975         CDEBUG(D_HA, "removing pingable import %s->%s\n",
976                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
977         class_import_put(imp);
978         cfs_mutex_up(&pinger_sem);
979         RETURN(0);
980 }
981
982 void ptlrpc_pinger_wake_up()
983 {
984 #ifdef ENABLE_PINGER
985         /* XXX force pinger to run, if needed */
986         struct obd_import *imp;
987         ENTRY;
988         cfs_list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
989                 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
990                        imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
991 #ifdef ENABLE_LIBLUSTRE_RECOVERY
992                 if (imp->imp_state == LUSTRE_IMP_DISCON &&
993                     !imp_is_deactive(imp))
994 #else
995                 /*XXX only recover for the initial connection */
996                 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
997                     imp->imp_state == LUSTRE_IMP_DISCON &&
998                     !imp_is_deactive(imp))
999 #endif
1000                         ptlrpc_initiate_recovery(imp);
1001                 else if (imp->imp_state != LUSTRE_IMP_FULL)
1002                         CDEBUG(D_HA, "Refused to recover import %s->%s "
1003                                      "state %d, deactive %d\n",
1004                                      imp->imp_obd->obd_uuid.uuid,
1005                                      obd2cli_tgt(imp->imp_obd), imp->imp_state,
1006                                      imp_is_deactive(imp));
1007         }
1008         EXIT;
1009 #endif
1010 }
1011 #endif /* !__KERNEL__ */