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