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