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