]> Pileus Git - ~andy/linux/blob - drivers/staging/lustre/lustre/ptlrpc/pinger.c
4684b03c4a5f8bbc6bb43957f920bdfbd3f54868
[~andy/linux] / drivers / staging / lustre / lustre / ptlrpc / pinger.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
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 #define DEBUG_SUBSYSTEM S_RPC
42
43 #include <obd_support.h>
44 #include <obd_class.h>
45 #include "ptlrpc_internal.h"
46
47 static int suppress_pings;
48 module_param(suppress_pings, int, 0644);
49 MODULE_PARM_DESC(suppress_pings, "Suppress pings");
50
51 struct mutex pinger_mutex;
52 static LIST_HEAD(pinger_imports);
53 static struct list_head timeout_list = LIST_HEAD_INIT(timeout_list);
54
55 int ptlrpc_pinger_suppress_pings(void)
56 {
57         return suppress_pings;
58 }
59 EXPORT_SYMBOL(ptlrpc_pinger_suppress_pings);
60
61 struct ptlrpc_request *
62 ptlrpc_prep_ping(struct obd_import *imp)
63 {
64         struct ptlrpc_request *req;
65
66         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
67                                         LUSTRE_OBD_VERSION, OBD_PING);
68         if (req) {
69                 ptlrpc_request_set_replen(req);
70                 req->rq_no_resend = req->rq_no_delay = 1;
71         }
72         return req;
73 }
74
75 int ptlrpc_obd_ping(struct obd_device *obd)
76 {
77         int rc;
78         struct ptlrpc_request *req;
79
80         req = ptlrpc_prep_ping(obd->u.cli.cl_import);
81         if (req == NULL)
82                 return -ENOMEM;
83
84         req->rq_send_state = LUSTRE_IMP_FULL;
85
86         rc = ptlrpc_queue_wait(req);
87
88         ptlrpc_req_finished(req);
89
90         return rc;
91 }
92 EXPORT_SYMBOL(ptlrpc_obd_ping);
93
94 int ptlrpc_ping(struct obd_import *imp)
95 {
96         struct ptlrpc_request *req;
97
98         req = ptlrpc_prep_ping(imp);
99         if (req == NULL) {
100                 CERROR("OOM trying to ping %s->%s\n",
101                        imp->imp_obd->obd_uuid.uuid,
102                        obd2cli_tgt(imp->imp_obd));
103                 return -ENOMEM;
104         }
105
106         DEBUG_REQ(D_INFO, req, "pinging %s->%s",
107                   imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
108         ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
109
110         return 0;
111 }
112
113 void ptlrpc_update_next_ping(struct obd_import *imp, int soon)
114 {
115         int time = soon ? PING_INTERVAL_SHORT : PING_INTERVAL;
116         if (imp->imp_state == LUSTRE_IMP_DISCON) {
117                 int dtime = max_t(int, CONNECTION_SWITCH_MIN,
118                                   AT_OFF ? 0 :
119                                   at_get(&imp->imp_at.iat_net_latency));
120                 time = min(time, dtime);
121         }
122         imp->imp_next_ping = cfs_time_shift(time);
123 }
124
125 void ptlrpc_ping_import_soon(struct obd_import *imp)
126 {
127         imp->imp_next_ping = cfs_time_current();
128 }
129
130 static inline int imp_is_deactive(struct obd_import *imp)
131 {
132         return (imp->imp_deactive ||
133                 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_IMP_DEACTIVE));
134 }
135
136 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
137 {
138         if (imp->imp_server_timeout)
139                 return cfs_time_shift(obd_timeout / 2);
140         else
141                 return cfs_time_shift(obd_timeout);
142 }
143
144 static atomic_t suspend_timeouts = ATOMIC_INIT(0);
145 static cfs_time_t suspend_wakeup_time = 0;
146
147 cfs_duration_t pinger_check_timeout(cfs_time_t time)
148 {
149         struct timeout_item *item;
150         cfs_time_t timeout = PING_INTERVAL;
151
152         /* The timeout list is a increase order sorted list */
153         mutex_lock(&pinger_mutex);
154         list_for_each_entry(item, &timeout_list, ti_chain) {
155                 int ti_timeout = item->ti_timeout;
156                 if (timeout > ti_timeout)
157                         timeout = ti_timeout;
158                 break;
159         }
160         mutex_unlock(&pinger_mutex);
161
162         return cfs_time_sub(cfs_time_add(time, cfs_time_seconds(timeout)),
163                                          cfs_time_current());
164 }
165
166 static wait_queue_head_t suspend_timeouts_waitq;
167
168 cfs_time_t ptlrpc_suspend_wakeup_time(void)
169 {
170         return suspend_wakeup_time;
171 }
172
173 void ptlrpc_deactivate_timeouts(struct obd_import *imp)
174 {
175         /*XXX: disabled for now, will be replaced by adaptive timeouts */
176 #if 0
177         if (imp->imp_no_timeout)
178                 return;
179         imp->imp_no_timeout = 1;
180         atomic_inc(&suspend_timeouts);
181         CDEBUG(D_HA|D_WARNING, "deactivate timeouts %u\n",
182                atomic_read(&suspend_timeouts));
183 #endif
184 }
185
186 void ptlrpc_activate_timeouts(struct obd_import *imp)
187 {
188         /*XXX: disabled for now, will be replaced by adaptive timeouts */
189 #if 0
190         if (!imp->imp_no_timeout)
191                 return;
192         imp->imp_no_timeout = 0;
193         LASSERT(atomic_read(&suspend_timeouts) > 0);
194         if (atomic_dec_and_test(&suspend_timeouts)) {
195                 suspend_wakeup_time = cfs_time_current();
196                 wake_up(&suspend_timeouts_waitq);
197         }
198         CDEBUG(D_HA|D_WARNING, "activate timeouts %u\n",
199                atomic_read(&suspend_timeouts));
200 #endif
201 }
202
203 int ptlrpc_check_suspend(void)
204 {
205         if (atomic_read(&suspend_timeouts))
206                 return 1;
207         return 0;
208 }
209
210 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req)
211 {
212         struct l_wait_info lwi;
213
214         if (atomic_read(&suspend_timeouts)) {
215                 DEBUG_REQ(D_NET, req, "-- suspend %d regular timeout",
216                           atomic_read(&suspend_timeouts));
217                 lwi = LWI_INTR(NULL, NULL);
218                 l_wait_event(suspend_timeouts_waitq,
219                              atomic_read(&suspend_timeouts) == 0, &lwi);
220                 DEBUG_REQ(D_NET, req, "-- recharge regular timeout");
221                 return 1;
222         }
223         return 0;
224 }
225
226
227 static bool ir_up;
228
229 void ptlrpc_pinger_ir_up(void)
230 {
231         CDEBUG(D_HA, "IR up\n");
232         ir_up = true;
233 }
234 EXPORT_SYMBOL(ptlrpc_pinger_ir_up);
235
236 void ptlrpc_pinger_ir_down(void)
237 {
238         CDEBUG(D_HA, "IR down\n");
239         ir_up = false;
240 }
241 EXPORT_SYMBOL(ptlrpc_pinger_ir_down);
242
243 static void ptlrpc_pinger_process_import(struct obd_import *imp,
244                                          unsigned long this_ping)
245 {
246         int level;
247         int force;
248         int force_next;
249         int suppress;
250
251         spin_lock(&imp->imp_lock);
252
253         level = imp->imp_state;
254         force = imp->imp_force_verify;
255         force_next = imp->imp_force_next_verify;
256         /*
257          * This will be used below only if the import is "FULL".
258          */
259         suppress = ir_up && OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS);
260
261         imp->imp_force_verify = 0;
262
263         if (cfs_time_aftereq(imp->imp_next_ping - 5 * CFS_TICK, this_ping) &&
264             !force) {
265                 spin_unlock(&imp->imp_lock);
266                 return;
267         }
268
269         imp->imp_force_next_verify = 0;
270
271         spin_unlock(&imp->imp_lock);
272
273         CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA, "%s->%s: level %s/%u "
274                "force %u force_next %u deactive %u pingable %u suppress %u\n",
275                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
276                ptlrpc_import_state_name(level), level, force, force_next,
277                imp->imp_deactive, imp->imp_pingable, suppress);
278
279         if (level == LUSTRE_IMP_DISCON && !imp_is_deactive(imp)) {
280                 /* wait for a while before trying recovery again */
281                 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
282                 if (!imp->imp_no_pinger_recover)
283                         ptlrpc_initiate_recovery(imp);
284         } else if (level != LUSTRE_IMP_FULL ||
285                    imp->imp_obd->obd_no_recov ||
286                    imp_is_deactive(imp)) {
287                 CDEBUG(D_HA, "%s->%s: not pinging (in recovery "
288                        "or recovery disabled: %s)\n",
289                        imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
290                        ptlrpc_import_state_name(level));
291         } else if ((imp->imp_pingable && !suppress) || force_next || force) {
292                 ptlrpc_ping(imp);
293         }
294 }
295
296 static int ptlrpc_pinger_main(void *arg)
297 {
298         struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
299
300         /* Record that the thread is running */
301         thread_set_flags(thread, SVC_RUNNING);
302         wake_up(&thread->t_ctl_waitq);
303
304         /* And now, loop forever, pinging as needed. */
305         while (1) {
306                 cfs_time_t this_ping = cfs_time_current();
307                 struct l_wait_info lwi;
308                 cfs_duration_t time_to_next_wake;
309                 struct timeout_item *item;
310                 struct list_head *iter;
311
312                 mutex_lock(&pinger_mutex);
313                 list_for_each_entry(item, &timeout_list, ti_chain) {
314                         item->ti_cb(item, item->ti_cb_data);
315                 }
316                 list_for_each(iter, &pinger_imports) {
317                         struct obd_import *imp =
318                                 list_entry(iter, struct obd_import,
319                                                imp_pinger_chain);
320
321                         ptlrpc_pinger_process_import(imp, this_ping);
322                         /* obd_timeout might have changed */
323                         if (imp->imp_pingable && imp->imp_next_ping &&
324                             cfs_time_after(imp->imp_next_ping,
325                                            cfs_time_add(this_ping,
326                                                         cfs_time_seconds(PING_INTERVAL))))
327                                 ptlrpc_update_next_ping(imp, 0);
328                 }
329                 mutex_unlock(&pinger_mutex);
330                 /* update memory usage info */
331                 obd_update_maxusage();
332
333                 /* Wait until the next ping time, or until we're stopped. */
334                 time_to_next_wake = pinger_check_timeout(this_ping);
335                 /* The ping sent by ptlrpc_send_rpc may get sent out
336                    say .01 second after this.
337                    ptlrpc_pinger_sending_on_import will then set the
338                    next ping time to next_ping + .01 sec, which means
339                    we will SKIP the next ping at next_ping, and the
340                    ping will get sent 2 timeouts from now!  Beware. */
341                 CDEBUG(D_INFO, "next wakeup in "CFS_DURATION_T" ("
342                        CFS_TIME_T")\n", time_to_next_wake,
343                        cfs_time_add(this_ping,cfs_time_seconds(PING_INTERVAL)));
344                 if (time_to_next_wake > 0) {
345                         lwi = LWI_TIMEOUT(max_t(cfs_duration_t,
346                                                 time_to_next_wake,
347                                                 cfs_time_seconds(1)),
348                                           NULL, NULL);
349                         l_wait_event(thread->t_ctl_waitq,
350                                      thread_is_stopping(thread) ||
351                                      thread_is_event(thread),
352                                      &lwi);
353                         if (thread_test_and_clear_flags(thread, SVC_STOPPING)) {
354                                 break;
355                         } else {
356                                 /* woken after adding import to reset timer */
357                                 thread_test_and_clear_flags(thread, SVC_EVENT);
358                         }
359                 }
360         }
361
362         thread_set_flags(thread, SVC_STOPPED);
363         wake_up(&thread->t_ctl_waitq);
364
365         CDEBUG(D_NET, "pinger thread exiting, process %d\n", current_pid());
366         return 0;
367 }
368
369 static struct ptlrpc_thread pinger_thread;
370
371 int ptlrpc_start_pinger(void)
372 {
373         struct l_wait_info lwi = { 0 };
374         int rc;
375
376         if (!thread_is_init(&pinger_thread) &&
377             !thread_is_stopped(&pinger_thread))
378                 return -EALREADY;
379
380         init_waitqueue_head(&pinger_thread.t_ctl_waitq);
381         init_waitqueue_head(&suspend_timeouts_waitq);
382
383         strcpy(pinger_thread.t_name, "ll_ping");
384
385         /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
386          * just drop the VM and FILES in cfs_daemonize_ctxt() right away. */
387         rc = PTR_ERR(kthread_run(ptlrpc_pinger_main, &pinger_thread,
388                                  "%s", pinger_thread.t_name));
389         if (IS_ERR_VALUE(rc)) {
390                 CERROR("cannot start thread: %d\n", rc);
391                 return rc;
392         }
393         l_wait_event(pinger_thread.t_ctl_waitq,
394                      thread_is_running(&pinger_thread), &lwi);
395
396         if (suppress_pings)
397                 CWARN("Pings will be suppressed at the request of the "
398                       "administrator.  The configuration shall meet the "
399                       "additional requirements described in the manual.  "
400                       "(Search for the \"suppress_pings\" kernel module "
401                       "parameter.)\n");
402
403         return 0;
404 }
405
406 int ptlrpc_pinger_remove_timeouts(void);
407
408 int ptlrpc_stop_pinger(void)
409 {
410         struct l_wait_info lwi = { 0 };
411         int rc = 0;
412
413         if (thread_is_init(&pinger_thread) ||
414             thread_is_stopped(&pinger_thread))
415                 return -EALREADY;
416
417         ptlrpc_pinger_remove_timeouts();
418         thread_set_flags(&pinger_thread, SVC_STOPPING);
419         wake_up(&pinger_thread.t_ctl_waitq);
420
421         l_wait_event(pinger_thread.t_ctl_waitq,
422                      thread_is_stopped(&pinger_thread), &lwi);
423
424         return rc;
425 }
426
427 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
428 {
429         ptlrpc_update_next_ping(imp, 0);
430 }
431 EXPORT_SYMBOL(ptlrpc_pinger_sending_on_import);
432
433 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
434 {
435         ptlrpc_update_next_ping(imp, 1);
436         LASSERT(spin_is_locked(&imp->imp_lock));
437         /*
438          * Avoid reading stale imp_connect_data.  When not sure if pings are
439          * expected or not on next connection, we assume they are not and force
440          * one anyway to guarantee the chance of updating
441          * imp_peer_committed_transno.
442          */
443         if (imp->imp_state != LUSTRE_IMP_FULL ||
444             OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS))
445                 imp->imp_force_next_verify = 1;
446 }
447
448 int ptlrpc_pinger_add_import(struct obd_import *imp)
449 {
450         if (!list_empty(&imp->imp_pinger_chain))
451                 return -EALREADY;
452
453         mutex_lock(&pinger_mutex);
454         CDEBUG(D_HA, "adding pingable import %s->%s\n",
455                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
456         /* if we add to pinger we want recovery on this import */
457         imp->imp_obd->obd_no_recov = 0;
458         ptlrpc_update_next_ping(imp, 0);
459         /* XXX sort, blah blah */
460         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
461         class_import_get(imp);
462
463         ptlrpc_pinger_wake_up();
464         mutex_unlock(&pinger_mutex);
465
466         return 0;
467 }
468 EXPORT_SYMBOL(ptlrpc_pinger_add_import);
469
470 int ptlrpc_pinger_del_import(struct obd_import *imp)
471 {
472         if (list_empty(&imp->imp_pinger_chain))
473                 return -ENOENT;
474
475         mutex_lock(&pinger_mutex);
476         list_del_init(&imp->imp_pinger_chain);
477         CDEBUG(D_HA, "removing pingable import %s->%s\n",
478                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
479         /* if we remove from pinger we don't want recovery on this import */
480         imp->imp_obd->obd_no_recov = 1;
481         class_import_put(imp);
482         mutex_unlock(&pinger_mutex);
483         return 0;
484 }
485 EXPORT_SYMBOL(ptlrpc_pinger_del_import);
486
487 /**
488  * Register a timeout callback to the pinger list, and the callback will
489  * be called when timeout happens.
490  */
491 struct timeout_item* ptlrpc_new_timeout(int time, enum timeout_event event,
492                                         timeout_cb_t cb, void *data)
493 {
494         struct timeout_item *ti;
495
496         OBD_ALLOC_PTR(ti);
497         if (!ti)
498                 return(NULL);
499
500         INIT_LIST_HEAD(&ti->ti_obd_list);
501         INIT_LIST_HEAD(&ti->ti_chain);
502         ti->ti_timeout = time;
503         ti->ti_event = event;
504         ti->ti_cb = cb;
505         ti->ti_cb_data = data;
506
507         return ti;
508 }
509
510 /**
511  * Register timeout event on the the pinger thread.
512  * Note: the timeout list is an sorted list with increased timeout value.
513  */
514 static struct timeout_item*
515 ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
516                                timeout_cb_t cb, void *data)
517 {
518         struct timeout_item *item, *tmp;
519
520         LASSERT(mutex_is_locked(&pinger_mutex));
521
522         list_for_each_entry(item, &timeout_list, ti_chain)
523                 if (item->ti_event == event)
524                         goto out;
525
526         item = ptlrpc_new_timeout(time, event, cb, data);
527         if (item) {
528                 list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
529                         if (tmp->ti_timeout < time) {
530                                 list_add(&item->ti_chain, &tmp->ti_chain);
531                                 goto out;
532                         }
533                 }
534                 list_add(&item->ti_chain, &timeout_list);
535         }
536 out:
537         return item;
538 }
539
540 /* Add a client_obd to the timeout event list, when timeout(@time)
541  * happens, the callback(@cb) will be called.
542  */
543 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
544                               timeout_cb_t cb, void *data,
545                               struct list_head *obd_list)
546 {
547         struct timeout_item *ti;
548
549         mutex_lock(&pinger_mutex);
550         ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
551         if (!ti) {
552                 mutex_unlock(&pinger_mutex);
553                 return (-EINVAL);
554         }
555         list_add(obd_list, &ti->ti_obd_list);
556         mutex_unlock(&pinger_mutex);
557         return 0;
558 }
559 EXPORT_SYMBOL(ptlrpc_add_timeout_client);
560
561 int ptlrpc_del_timeout_client(struct list_head *obd_list,
562                               enum timeout_event event)
563 {
564         struct timeout_item *ti = NULL, *item;
565
566         if (list_empty(obd_list))
567                 return 0;
568         mutex_lock(&pinger_mutex);
569         list_del_init(obd_list);
570         /**
571          * If there are no obd attached to the timeout event
572          * list, remove this timeout event from the pinger
573          */
574         list_for_each_entry(item, &timeout_list, ti_chain) {
575                 if (item->ti_event == event) {
576                         ti = item;
577                         break;
578                 }
579         }
580         LASSERTF(ti != NULL, "ti is NULL !\n");
581         if (list_empty(&ti->ti_obd_list)) {
582                 list_del(&ti->ti_chain);
583                 OBD_FREE_PTR(ti);
584         }
585         mutex_unlock(&pinger_mutex);
586         return 0;
587 }
588 EXPORT_SYMBOL(ptlrpc_del_timeout_client);
589
590 int ptlrpc_pinger_remove_timeouts(void)
591 {
592         struct timeout_item *item, *tmp;
593
594         mutex_lock(&pinger_mutex);
595         list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
596                 LASSERT(list_empty(&item->ti_obd_list));
597                 list_del(&item->ti_chain);
598                 OBD_FREE_PTR(item);
599         }
600         mutex_unlock(&pinger_mutex);
601         return 0;
602 }
603
604 void ptlrpc_pinger_wake_up(void)
605 {
606         thread_add_flags(&pinger_thread, SVC_EVENT);
607         wake_up(&pinger_thread.t_ctl_waitq);
608 }
609
610 /* Ping evictor thread */
611 #define PET_READY     1
612 #define PET_TERMINATE 2
613
614 static int             pet_refcount = 0;
615 static int             pet_state;
616 static wait_queue_head_t       pet_waitq;
617 LIST_HEAD(pet_list);
618 static DEFINE_SPINLOCK(pet_lock);
619
620 int ping_evictor_wake(struct obd_export *exp)
621 {
622         struct obd_device *obd;
623
624         spin_lock(&pet_lock);
625         if (pet_state != PET_READY) {
626                 /* eventually the new obd will call here again. */
627                 spin_unlock(&pet_lock);
628                 return 1;
629         }
630
631         obd = class_exp2obd(exp);
632         if (list_empty(&obd->obd_evict_list)) {
633                 class_incref(obd, "evictor", obd);
634                 list_add(&obd->obd_evict_list, &pet_list);
635         }
636         spin_unlock(&pet_lock);
637
638         wake_up(&pet_waitq);
639         return 0;
640 }
641
642 static int ping_evictor_main(void *arg)
643 {
644         struct obd_device *obd;
645         struct obd_export *exp;
646         struct l_wait_info lwi = { 0 };
647         time_t expire_time;
648
649         unshare_fs_struct();
650
651         CDEBUG(D_HA, "Starting Ping Evictor\n");
652         pet_state = PET_READY;
653         while (1) {
654                 l_wait_event(pet_waitq, (!list_empty(&pet_list)) ||
655                              (pet_state == PET_TERMINATE), &lwi);
656
657                 /* loop until all obd's will be removed */
658                 if ((pet_state == PET_TERMINATE) && list_empty(&pet_list))
659                         break;
660
661                 /* we only get here if pet_exp != NULL, and the end of this
662                  * loop is the only place which sets it NULL again, so lock
663                  * is not strictly necessary. */
664                 spin_lock(&pet_lock);
665                 obd = list_entry(pet_list.next, struct obd_device,
666                                      obd_evict_list);
667                 spin_unlock(&pet_lock);
668
669                 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
670
671                 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
672                        obd->obd_name, expire_time);
673
674                 /* Exports can't be deleted out of the list while we hold
675                  * the obd lock (class_unlink_export), which means we can't
676                  * lose the last ref on the export.  If they've already been
677                  * removed from the list, we won't find them here. */
678                 spin_lock(&obd->obd_dev_lock);
679                 while (!list_empty(&obd->obd_exports_timed)) {
680                         exp = list_entry(obd->obd_exports_timed.next,
681                                              struct obd_export,
682                                              exp_obd_chain_timed);
683                         if (expire_time > exp->exp_last_request_time) {
684                                 class_export_get(exp);
685                                 spin_unlock(&obd->obd_dev_lock);
686                                 LCONSOLE_WARN("%s: haven't heard from client %s"
687                                               " (at %s) in %ld seconds. I think"
688                                               " it's dead, and I am evicting"
689                                               " it. exp %p, cur %ld expire %ld"
690                                               " last %ld\n",
691                                               obd->obd_name,
692                                               obd_uuid2str(&exp->exp_client_uuid),
693                                               obd_export_nid2str(exp),
694                                               (long)(cfs_time_current_sec() -
695                                                      exp->exp_last_request_time),
696                                               exp, (long)cfs_time_current_sec(),
697                                               (long)expire_time,
698                                               (long)exp->exp_last_request_time);
699                                 CDEBUG(D_HA, "Last request was at %ld\n",
700                                        exp->exp_last_request_time);
701                                 class_fail_export(exp);
702                                 class_export_put(exp);
703                                 spin_lock(&obd->obd_dev_lock);
704                         } else {
705                                 /* List is sorted, so everyone below is ok */
706                                 break;
707                         }
708                 }
709                 spin_unlock(&obd->obd_dev_lock);
710
711                 spin_lock(&pet_lock);
712                 list_del_init(&obd->obd_evict_list);
713                 spin_unlock(&pet_lock);
714
715                 class_decref(obd, "evictor", obd);
716         }
717         CDEBUG(D_HA, "Exiting Ping Evictor\n");
718
719         return 0;
720 }
721
722 void ping_evictor_start(void)
723 {
724         struct task_struct *task;
725
726         if (++pet_refcount > 1)
727                 return;
728
729         init_waitqueue_head(&pet_waitq);
730
731         task = kthread_run(ping_evictor_main, NULL, "ll_evictor");
732         if (IS_ERR(task)) {
733                 pet_refcount--;
734                 CERROR("Cannot start ping evictor thread: %ld\n",
735                         PTR_ERR(task));
736         }
737 }
738 EXPORT_SYMBOL(ping_evictor_start);
739
740 void ping_evictor_stop(void)
741 {
742         if (--pet_refcount > 0)
743                 return;
744
745         pet_state = PET_TERMINATE;
746         wake_up(&pet_waitq);
747 }
748 EXPORT_SYMBOL(ping_evictor_stop);