Whamcloud - gitweb
daec0f5891aab643998504b89cfe62168e8baf9a
[fs/lustre-release.git] / lustre / ptlrpc / pinger.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/pinger.c
37  *
38  * Portal-RPC reconnection and replay operations, for use in recovery.
39  */
40
41 #ifndef __KERNEL__
42 #include <liblustre.h>
43 #else
44 #define DEBUG_SUBSYSTEM S_RPC
45 #endif
46
47 #include <obd_support.h>
48 #include <obd_class.h>
49 #include "ptlrpc_internal.h"
50
51 struct semaphore pinger_sem;
52 static CFS_LIST_HEAD(pinger_imports);
53 static struct list_head timeout_list = CFS_LIST_HEAD_INIT(timeout_list); 
54 struct ptlrpc_request *
55 ptlrpc_prep_ping(struct obd_import *imp)
56 {
57         struct ptlrpc_request *req;
58
59         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
60                                         LUSTRE_OBD_VERSION, OBD_PING);
61         if (req) {
62                 ptlrpc_request_set_replen(req);
63                 req->rq_no_resend = req->rq_no_delay = 1;
64         }
65         return req;
66 }
67
68 int ptlrpc_obd_ping(struct obd_device *obd)
69 {
70         int rc;
71         struct ptlrpc_request *req;
72         ENTRY;
73
74         req = ptlrpc_prep_ping(obd->u.cli.cl_import);
75         if (req == NULL)
76                 RETURN(-ENOMEM);
77
78         req->rq_send_state = LUSTRE_IMP_FULL;
79
80         rc = ptlrpc_queue_wait(req);
81
82         ptlrpc_req_finished(req);
83
84         RETURN(rc);
85 }
86 EXPORT_SYMBOL(ptlrpc_obd_ping);
87
88 int ptlrpc_ping(struct obd_import *imp)
89 {
90         struct ptlrpc_request *req;
91         ENTRY;
92
93         req = ptlrpc_prep_ping(imp);
94         if (req == NULL) {
95                 CERROR("OOM trying to ping %s->%s\n",
96                        imp->imp_obd->obd_uuid.uuid,
97                        obd2cli_tgt(imp->imp_obd));
98                 RETURN(-ENOMEM);
99         }
100
101         DEBUG_REQ(D_INFO, req, "pinging %s->%s",
102                   imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
103         ptlrpcd_add_req(req, PSCOPE_OTHER);
104
105         RETURN(0);
106 }
107
108 void ptlrpc_update_next_ping(struct obd_import *imp)
109 {
110 #ifdef ENABLE_PINGER
111         int time = PING_INTERVAL;
112         if (imp->imp_state == LUSTRE_IMP_DISCON) {
113                 int dtime = max_t(int, CONNECTION_SWITCH_MIN,
114                                   AT_OFF ? 0 :
115                                   at_get(&imp->imp_at.iat_net_latency));
116                 time = min(time, dtime);
117         }
118         imp->imp_next_ping = cfs_time_shift(time);
119 #endif /* ENABLE_PINGER */
120 }
121
122 void ptlrpc_ping_import_soon(struct obd_import *imp)
123 {
124         imp->imp_next_ping = cfs_time_current();
125 }
126
127 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
128 {
129         if (imp->imp_server_timeout)
130                 return cfs_time_shift(obd_timeout / 2);
131         else
132                 return cfs_time_shift(obd_timeout);
133 }
134
135 static atomic_t suspend_timeouts = ATOMIC_INIT(0);
136 static cfs_time_t suspend_wakeup_time = 0;
137
138 cfs_duration_t pinger_check_timeout(cfs_time_t time)
139 {
140         struct timeout_item *item;
141         cfs_time_t timeout = PING_INTERVAL;
142
143         /* The timeout list is a increase order sorted list */
144         mutex_down(&pinger_sem);
145         list_for_each_entry(item, &timeout_list, ti_chain) {
146                 int ti_timeout = item->ti_timeout;
147                 if (timeout > ti_timeout)
148                          timeout = ti_timeout;
149                 break;
150         }
151         mutex_up(&pinger_sem);
152         
153         return cfs_time_sub(cfs_time_add(time, cfs_time_seconds(timeout)),
154                                          cfs_time_current());
155 }
156
157 #ifdef __KERNEL__
158 static wait_queue_head_t suspend_timeouts_waitq;
159 #endif
160
161 cfs_time_t ptlrpc_suspend_wakeup_time(void)
162 {
163         return suspend_wakeup_time;
164 }
165
166 void ptlrpc_deactivate_timeouts(struct obd_import *imp)
167 {
168         /*XXX: disabled for now, will be replaced by adaptive timeouts */
169 #if 0
170         if (imp->imp_no_timeout)
171                 return;
172         imp->imp_no_timeout = 1;
173         atomic_inc(&suspend_timeouts);
174         CDEBUG(D_HA|D_WARNING, "deactivate timeouts %u\n", atomic_read(&suspend_timeouts));
175 #endif
176 }
177
178 void ptlrpc_activate_timeouts(struct obd_import *imp)
179 {
180         /*XXX: disabled for now, will be replaced by adaptive timeouts */
181 #if 0
182         if (!imp->imp_no_timeout)
183                 return;
184         imp->imp_no_timeout = 0;
185         LASSERT(atomic_read(&suspend_timeouts) > 0);
186         if (atomic_dec_and_test(&suspend_timeouts)) {
187                 suspend_wakeup_time = cfs_time_current();
188                 wake_up(&suspend_timeouts_waitq);
189         }
190         CDEBUG(D_HA|D_WARNING, "activate timeouts %u\n", atomic_read(&suspend_timeouts));
191 #endif
192 }
193
194 int ptlrpc_check_suspend(void)
195 {
196         if (atomic_read(&suspend_timeouts))
197                 return 1;
198         return 0;
199 }
200
201 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req)
202 {
203         struct l_wait_info lwi;
204
205         if (atomic_read(&suspend_timeouts)) {
206                 DEBUG_REQ(D_NET, req, "-- suspend %d regular timeout",
207                           atomic_read(&suspend_timeouts));
208                 lwi = LWI_INTR(NULL, NULL);
209                 l_wait_event(suspend_timeouts_waitq,
210                              atomic_read(&suspend_timeouts) == 0, &lwi);
211                 DEBUG_REQ(D_NET, req, "-- recharge regular timeout");
212                 return 1;
213         }
214         return 0;
215 }
216
217 #ifdef __KERNEL__
218
219 static void ptlrpc_pinger_process_import(struct obd_import *imp,
220                                          unsigned long this_ping)
221 {
222         int force, level;
223
224         spin_lock(&imp->imp_lock);
225         level = imp->imp_state;
226         force = imp->imp_force_verify;
227         if (force)
228                 imp->imp_force_verify = 0;
229         spin_unlock(&imp->imp_lock);
230
231         CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA,
232                "level %s/%u force %u deactive %u pingable %u\n",
233                ptlrpc_import_state_name(level), level,
234                force, imp->imp_deactive, imp->imp_pingable);
235
236         if (cfs_time_aftereq(imp->imp_next_ping - 5 * CFS_TICK,
237                              this_ping) && force == 0)
238                 return;
239
240         if (level == LUSTRE_IMP_DISCON && !imp->imp_deactive) {
241                 /* wait at least a timeout before trying recovery again */
242                 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
243                 ptlrpc_initiate_recovery(imp);
244         } else if (level != LUSTRE_IMP_FULL ||
245                    imp->imp_obd->obd_no_recov ||
246                    imp->imp_deactive) {
247                 CDEBUG(D_HA, "not pinging %s (in recovery "
248                        " or recovery disabled: %s)\n",
249                        obd2cli_tgt(imp->imp_obd),
250                        ptlrpc_import_state_name(level));
251         } else if (imp->imp_pingable || force) {
252                 ptlrpc_ping(imp);
253         }
254 }
255
256 static int ptlrpc_pinger_main(void *arg)
257 {
258         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
259         struct ptlrpc_thread *thread = data->thread;
260         ENTRY;
261
262         cfs_daemonize(data->name);
263
264         /* Record that the thread is running */
265         thread->t_flags = SVC_RUNNING;
266         cfs_waitq_signal(&thread->t_ctl_waitq);
267
268         /* And now, loop forever, pinging as needed. */
269         while (1) {
270                 cfs_time_t this_ping = cfs_time_current();
271                 struct l_wait_info lwi;
272                 cfs_duration_t time_to_next_wake;
273                 struct timeout_item *item;
274                 struct list_head *iter;
275
276                 mutex_down(&pinger_sem);
277                 list_for_each_entry(item, &timeout_list, ti_chain) {
278                         item->ti_cb(item, item->ti_cb_data);
279                 }
280                 list_for_each(iter, &pinger_imports) {
281                         struct obd_import *imp =
282                                 list_entry(iter, struct obd_import,
283                                            imp_pinger_chain);
284
285                         ptlrpc_pinger_process_import(imp, this_ping);
286                         /* obd_timeout might have changed */
287                         if (imp->imp_pingable && imp->imp_next_ping &&
288                             cfs_time_after(imp->imp_next_ping,
289                                            cfs_time_add(this_ping,
290                                                         cfs_time_seconds(PING_INTERVAL))))
291                                 ptlrpc_update_next_ping(imp);
292                 }
293                 mutex_up(&pinger_sem);
294                 /* update memory usage info */
295                 obd_update_maxusage();
296
297                 /* Wait until the next ping time, or until we're stopped. */
298                 time_to_next_wake = pinger_check_timeout(this_ping);
299                 /* The ping sent by ptlrpc_send_rpc may get sent out
300                    say .01 second after this.
301                    ptlrpc_pinger_sending_on_import will then set the
302                    next ping time to next_ping + .01 sec, which means
303                    we will SKIP the next ping at next_ping, and the
304                    ping will get sent 2 timeouts from now!  Beware. */
305                 CDEBUG(D_INFO, "next wakeup in "CFS_DURATION_T" ("CFS_TIME_T")\n",
306                                 time_to_next_wake,
307                                 cfs_time_add(this_ping, cfs_time_seconds(PING_INTERVAL)));
308                 if (time_to_next_wake > 0) {
309                         lwi = LWI_TIMEOUT(max_t(cfs_duration_t, time_to_next_wake, cfs_time_seconds(1)),
310                                             NULL, NULL);
311                         l_wait_event(thread->t_ctl_waitq,
312                                      thread->t_flags & (SVC_STOPPING|SVC_EVENT),
313                                      &lwi);
314                         if (thread->t_flags & SVC_STOPPING) {
315                                 thread->t_flags &= ~SVC_STOPPING;
316                                 EXIT;
317                                 break;
318                         } else if (thread->t_flags & SVC_EVENT) {
319                                 /* woken after adding import to reset timer */
320                                 thread->t_flags &= ~SVC_EVENT;
321                         }
322                 }
323         }
324
325         thread->t_flags = SVC_STOPPED;
326         cfs_waitq_signal(&thread->t_ctl_waitq);
327
328         CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
329         return 0;
330 }
331
332 static struct ptlrpc_thread *pinger_thread = NULL;
333
334 int ptlrpc_start_pinger(void)
335 {
336         struct l_wait_info lwi = { 0 };
337         struct ptlrpc_svc_data d;
338         int rc;
339 #ifndef ENABLE_PINGER
340         return 0;
341 #endif
342         ENTRY;
343
344         if (pinger_thread != NULL)
345                 RETURN(-EALREADY);
346
347         OBD_ALLOC_PTR(pinger_thread);
348         if (pinger_thread == NULL)
349                 RETURN(-ENOMEM);
350         cfs_waitq_init(&pinger_thread->t_ctl_waitq);
351         cfs_waitq_init(&suspend_timeouts_waitq);
352
353         d.name = "ll_ping";
354         d.thread = pinger_thread;
355
356         /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
357          * just drop the VM and FILES in ptlrpc_daemonize() right away. */
358         rc = cfs_kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
359         if (rc < 0) {
360                 CERROR("cannot start thread: %d\n", rc);
361                 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
362                 pinger_thread = NULL;
363                 RETURN(rc);
364         }
365         l_wait_event(pinger_thread->t_ctl_waitq,
366                      pinger_thread->t_flags & SVC_RUNNING, &lwi);
367
368         RETURN(0);
369 }
370
371 int ptlrpc_pinger_remove_timeouts(void);
372
373 int ptlrpc_stop_pinger(void)
374 {
375         struct l_wait_info lwi = { 0 };
376         int rc = 0;
377 #ifndef ENABLE_PINGER
378         return 0;
379 #endif
380         ENTRY;
381
382         if (pinger_thread == NULL)
383                 RETURN(-EALREADY);
384
385         ptlrpc_pinger_remove_timeouts();
386         mutex_down(&pinger_sem);
387         pinger_thread->t_flags = SVC_STOPPING;
388         cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
389         mutex_up(&pinger_sem);
390
391         l_wait_event(pinger_thread->t_ctl_waitq,
392                      (pinger_thread->t_flags & SVC_STOPPED), &lwi);
393
394         OBD_FREE_PTR(pinger_thread);
395         pinger_thread = NULL;
396         RETURN(rc);
397 }
398
399 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
400 {
401         ptlrpc_update_next_ping(imp);
402 }
403
404 int ptlrpc_pinger_add_import(struct obd_import *imp)
405 {
406         ENTRY;
407         if (!list_empty(&imp->imp_pinger_chain))
408                 RETURN(-EALREADY);
409
410         mutex_down(&pinger_sem);
411         CDEBUG(D_HA, "adding pingable import %s->%s\n",
412                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
413         /* if we add to pinger we want recovery on this import */
414         imp->imp_obd->obd_no_recov = 0;
415         ptlrpc_update_next_ping(imp);
416         /* XXX sort, blah blah */
417         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
418         class_import_get(imp);
419
420         ptlrpc_pinger_wake_up();
421         mutex_up(&pinger_sem);
422
423         RETURN(0);
424 }
425
426 int ptlrpc_pinger_del_import(struct obd_import *imp)
427 {
428         ENTRY;
429         if (list_empty(&imp->imp_pinger_chain))
430                 RETURN(-ENOENT);
431
432         mutex_down(&pinger_sem);
433         list_del_init(&imp->imp_pinger_chain);
434         CDEBUG(D_HA, "removing pingable import %s->%s\n",
435                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
436         /* if we remove from pinger we don't want recovery on this import */
437         imp->imp_obd->obd_no_recov = 1;
438         class_import_put(imp);
439         mutex_up(&pinger_sem);
440         RETURN(0);
441 }
442
443 /**
444  * Register a timeout callback to the pinger list, and the callback will
445  * be called when timeout happens.
446  */
447 struct timeout_item* ptlrpc_new_timeout(int time, enum timeout_event event,
448                                         timeout_cb_t cb, void *data)
449 {
450         struct timeout_item *ti;
451         
452         OBD_ALLOC_PTR(ti);
453         if (!ti)
454                 return(NULL);
455
456         CFS_INIT_LIST_HEAD(&ti->ti_obd_list);
457         CFS_INIT_LIST_HEAD(&ti->ti_chain);
458         ti->ti_timeout = time;
459         ti->ti_event = event;
460         ti->ti_cb = cb;
461         ti->ti_cb_data = data;
462         
463         return ti;
464 }
465
466 /**
467  * Register timeout event on the the pinger thread.
468  * Note: the timeout list is an sorted list with increased timeout value.
469  */
470 static struct timeout_item*
471 ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
472                                timeout_cb_t cb, void *data)
473 {
474         struct timeout_item *item;
475         struct timeout_item *ti = NULL;
476
477         LASSERT_SEM_LOCKED(&pinger_sem);
478         list_for_each_entry_reverse(item, &timeout_list, ti_chain) {
479                 if (item->ti_event == event) {
480                         ti = item;
481                         break;
482                 }
483                 if (item->ti_timeout < ti->ti_timeout) {
484                         ti = ptlrpc_new_timeout(time, event, cb, data);
485                         if (!ti) {
486                                 ti = ERR_PTR(-ENOMEM);
487                                 break;
488                         }
489                         list_add(&ti->ti_chain, &item->ti_chain);
490                 }
491         }
492         if (!ti) {
493                 ti = ptlrpc_new_timeout(time, event, cb, data);
494                 if (ti)
495                         list_add(&ti->ti_chain, &timeout_list);
496         }
497         
498         return ti;
499 }
500 /* Add a client_obd to the timeout event list, when timeout(@time) 
501  * happens, the callback(@cb) will be called.
502  */
503 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
504                               timeout_cb_t cb, void *data,
505                               struct list_head *obd_list)
506 {
507         struct timeout_item *ti;
508
509         mutex_down(&pinger_sem);
510         ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
511         if (!ti) {
512                 mutex_up(&pinger_sem);
513                 return (-EINVAL);
514         }
515         list_add(obd_list, &ti->ti_obd_list);
516         mutex_up(&pinger_sem);
517         return 0;
518 }           
519
520 int ptlrpc_del_timeout_client(struct list_head *obd_list)
521 {
522         mutex_down(&pinger_sem);
523         list_del_init(obd_list);
524         mutex_up(&pinger_sem);
525         return 0;
526 }  
527
528 int ptlrpc_pinger_remove_timeouts(void)
529 {
530         struct timeout_item *item, *tmp;
531
532         mutex_down(&pinger_sem);
533         list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
534                 LASSERT(list_empty(&item->ti_obd_list));
535                 list_del(&item->ti_chain);
536                 OBD_FREE_PTR(item);
537         }
538         mutex_up(&pinger_sem);
539         return 0;
540 }
541
542 void ptlrpc_pinger_wake_up()
543 {
544 #ifdef ENABLE_PINGER
545         pinger_thread->t_flags |= SVC_EVENT;
546         cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
547 #endif
548 }
549
550 /* Ping evictor thread */
551 #define PET_READY     1
552 #define PET_TERMINATE 2
553
554 static int               pet_refcount = 0;
555 static int               pet_state;
556 static wait_queue_head_t pet_waitq;
557 static struct obd_export *pet_exp = NULL;
558 static spinlock_t        pet_lock = SPIN_LOCK_UNLOCKED;
559
560 int ping_evictor_wake(struct obd_export *exp)
561 {
562         spin_lock(&pet_lock);
563         if (pet_exp || (pet_state != PET_READY)) {
564                 /* eventually the new obd will call here again. */
565                 spin_unlock(&pet_lock);
566                 return 1;
567         }
568
569         /* We have to make sure the obd isn't destroyed between now and when
570          * the ping evictor runs.  We'll take a reference here, and drop it
571          * when we finish in the evictor.  We don't really care about this
572          * export in particular; we just need one to keep the obd alive. */
573         pet_exp = class_export_get(exp);
574         spin_unlock(&pet_lock);
575
576         wake_up(&pet_waitq);
577         return 0;
578 }
579
580 static int ping_evictor_main(void *arg)
581 {
582         struct obd_device *obd;
583         struct obd_export *exp;
584         struct l_wait_info lwi = { 0 };
585         time_t expire_time;
586         ENTRY;
587
588         ptlrpc_daemonize("ll_evictor");
589
590         CDEBUG(D_HA, "Starting Ping Evictor\n");
591         pet_exp = NULL;
592         pet_state = PET_READY;
593         while (1) {
594                 l_wait_event(pet_waitq, pet_exp ||
595                              (pet_state == PET_TERMINATE), &lwi);
596                 if (pet_state == PET_TERMINATE)
597                         break;
598
599                 /* we only get here if pet_exp != NULL, and the end of this
600                  * loop is the only place which sets it NULL again, so lock
601                  * is not strictly necessary. */
602                 spin_lock(&pet_lock);
603                 obd = pet_exp->exp_obd;
604                 spin_unlock(&pet_lock);
605
606                 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
607
608                 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
609                        obd->obd_name, expire_time);
610
611                 /* Exports can't be deleted out of the list while we hold
612                  * the obd lock (class_unlink_export), which means we can't
613                  * lose the last ref on the export.  If they've already been
614                  * removed from the list, we won't find them here. */
615                 spin_lock(&obd->obd_dev_lock);
616                 while (!list_empty(&obd->obd_exports_timed)) {
617                         exp = list_entry(obd->obd_exports_timed.next,
618                                          struct obd_export,exp_obd_chain_timed);
619                         if (expire_time > exp->exp_last_request_time) {
620                                 class_export_get(exp);
621                                 spin_unlock(&obd->obd_dev_lock);
622                                  LCONSOLE_WARN("%s: haven't heard from client %s"
623                                               " (at %s) in %ld seconds. I think"
624                                               " it's dead, and I am evicting"
625                                               " it. exp %p, cur %ld expire %ld"
626                                               " last %ld\n",
627                                               obd->obd_name,
628                                               obd_uuid2str(&exp->exp_client_uuid),
629                                               obd_export_nid2str(exp),
630                                               (long)(cfs_time_current_sec() -
631                                                      exp->exp_last_request_time),
632                                               exp, (long)cfs_time_current_sec(),
633                                               (long)expire_time,
634                                               (long)exp->exp_last_request_time);
635                                 CDEBUG(D_HA, "Last request was at %ld\n",
636                                        exp->exp_last_request_time);
637                                 class_fail_export(exp);
638                                 class_export_put(exp);
639                                 spin_lock(&obd->obd_dev_lock);
640                         } else {
641                                 /* List is sorted, so everyone below is ok */
642                                 break;
643                         }
644                 }
645                 spin_unlock(&obd->obd_dev_lock);
646
647                 class_export_put(pet_exp);
648
649                 spin_lock(&pet_lock);
650                 pet_exp = NULL;
651                 spin_unlock(&pet_lock);
652         }
653         CDEBUG(D_HA, "Exiting Ping Evictor\n");
654
655         RETURN(0);
656 }
657
658 void ping_evictor_start(void)
659 {
660         int rc;
661
662         if (++pet_refcount > 1)
663                 return;
664
665         init_waitqueue_head(&pet_waitq);
666
667         rc = cfs_kernel_thread(ping_evictor_main, NULL, CLONE_VM | CLONE_FILES);
668         if (rc < 0) {
669                 pet_refcount--;
670                 CERROR("Cannot start ping evictor thread: %d\n", rc);
671         }
672 }
673 EXPORT_SYMBOL(ping_evictor_start);
674
675 void ping_evictor_stop(void)
676 {
677         if (--pet_refcount > 0)
678                 return;
679
680         pet_state = PET_TERMINATE;
681         wake_up(&pet_waitq);
682 }
683 EXPORT_SYMBOL(ping_evictor_stop);
684 #else /* !__KERNEL__ */
685
686 /* XXX
687  * the current implementation of pinger in liblustre is not optimized
688  */
689
690 #ifdef ENABLE_PINGER
691 static struct pinger_data {
692         int             pd_recursion;
693         cfs_time_t      pd_this_ping;   /* jiffies */
694         cfs_time_t      pd_next_ping;   /* jiffies */
695         struct ptlrpc_request_set *pd_set;
696 } pinger_args;
697
698 static int pinger_check_rpcs(void *arg)
699 {
700         cfs_time_t curtime = cfs_time_current();
701         struct ptlrpc_request *req;
702         struct ptlrpc_request_set *set;
703         struct list_head *iter;
704         struct obd_import *imp;
705         struct pinger_data *pd = &pinger_args;
706         int rc;
707
708         /* prevent recursion */
709         if (pd->pd_recursion++) {
710                 CDEBUG(D_HA, "pinger: recursion! quit\n");
711                 LASSERT(pd->pd_set);
712                 pd->pd_recursion--;
713                 return 0;
714         }
715
716         /* have we reached ping point? */
717         if (!pd->pd_set && time_before(curtime, pd->pd_next_ping)) {
718                 pd->pd_recursion--;
719                 return 0;
720         }
721
722         /* if we have rpc_set already, continue processing it */
723         if (pd->pd_set) {
724                 LASSERT(pd->pd_this_ping);
725                 set = pd->pd_set;
726                 goto do_check_set;
727         }
728
729         pd->pd_this_ping = curtime;
730         pd->pd_set = ptlrpc_prep_set();
731         if (pd->pd_set == NULL)
732                 goto out;
733         set = pd->pd_set;
734
735         /* add rpcs into set */
736         mutex_down(&pinger_sem);
737         list_for_each(iter, &pinger_imports) {
738                 struct obd_import *imp =
739                         list_entry(iter, struct obd_import, imp_pinger_chain);
740                 int generation, level;
741
742                 if (cfs_time_aftereq(pd->pd_this_ping,
743                                      imp->imp_next_ping - 5 * CFS_TICK)) {
744                         /* Add a ping. */
745                         spin_lock(&imp->imp_lock);
746                         generation = imp->imp_generation;
747                         level = imp->imp_state;
748                         spin_unlock(&imp->imp_lock);
749
750                         if (level != LUSTRE_IMP_FULL) {
751                                 CDEBUG(D_HA,
752                                        "not pinging %s (in recovery)\n",
753                                        obd2cli_tgt(imp->imp_obd));
754                                 continue;
755                         }
756
757                         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
758                                                         LUSTRE_OBD_VERSION,
759                                                         OBD_PING);
760                         if (req == NULL) {
761                                 CERROR("OOM trying to ping %s->%s\n",
762                                        imp->imp_obd->obd_uuid.uuid,
763                                        obd2cli_tgt(imp->imp_obd));
764                                 break;
765                         }
766
767                         req->rq_no_resend = 1;
768                         ptlrpc_request_set_replen(req);
769                         req->rq_send_state = LUSTRE_IMP_FULL;
770                         ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
771                         req->rq_import_generation = generation;
772                         ptlrpc_set_add_req(set, req);
773                 } else {
774                         CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
775                                " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
776                                imp->imp_next_ping, pd->pd_this_ping);
777                 }
778         }
779         pd->pd_this_ping = curtime;
780         mutex_up(&pinger_sem);
781
782         /* Might be empty, that's OK. */
783         if (set->set_remaining == 0)
784                 CDEBUG(D_RPCTRACE, "nothing to ping\n");
785
786         list_for_each(iter, &set->set_requests) {
787                 struct ptlrpc_request *req =
788                         list_entry(iter, struct ptlrpc_request,
789                                    rq_set_chain);
790                 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
791                           req->rq_import->imp_obd->obd_uuid.uuid,
792                           obd2cli_tgt(req->rq_import->imp_obd));
793                 (void)ptl_send_rpc(req, 0);
794         }
795
796 do_check_set:
797         rc = ptlrpc_check_set(NULL, set);
798
799         /* not finished, and we are not expired, simply return */
800         if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
801                                             cfs_time_seconds(PING_INTERVAL)))) {
802                 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
803                 pd->pd_recursion--;
804                 return 0;
805         }
806
807         /* Expire all the requests that didn't come back. */
808         mutex_down(&pinger_sem);
809         list_for_each(iter, &set->set_requests) {
810                 req = list_entry(iter, struct ptlrpc_request,
811                                  rq_set_chain);
812
813                 if (req->rq_phase == RQ_PHASE_COMPLETE)
814                         continue;
815
816                 CDEBUG(D_RPCTRACE, "Pinger initiate expire request(%p)\n",
817                        req);
818
819                 /* This will also unregister reply. */
820                 ptlrpc_expire_one_request(req, 0);
821
822                 /* We're done with this req, let's finally move it to complete
823                  * phase and take care of inflights. */
824                 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
825                 imp = req->rq_import;
826                 spin_lock(&imp->imp_lock);
827                 if (!list_empty(&req->rq_list)) {
828                         list_del_init(&req->rq_list);
829                         atomic_dec(&imp->imp_inflight);
830                 }
831                 spin_unlock(&imp->imp_lock);
832                 set->set_remaining--;
833         }
834         mutex_up(&pinger_sem);
835
836         ptlrpc_set_destroy(set);
837         pd->pd_set = NULL;
838
839 out:
840         pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
841                                         cfs_time_seconds(PING_INTERVAL));
842         pd->pd_this_ping = 0; /* XXX for debug */
843
844         CDEBUG(D_INFO, "finished a round ping\n");
845         pd->pd_recursion--;
846         return 0;
847 }
848
849 static void *pinger_callback = NULL;
850 #endif /* ENABLE_PINGER */
851
852 int ptlrpc_start_pinger(void)
853 {
854 #ifdef ENABLE_PINGER
855         memset(&pinger_args, 0, sizeof(pinger_args));
856         pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
857                                                            &pinger_check_rpcs,
858                                                            &pinger_args);
859 #endif
860         return 0;
861 }
862
863 int ptlrpc_stop_pinger(void)
864 {
865 #ifdef ENABLE_PINGER
866         if (pinger_callback)
867                 liblustre_deregister_wait_callback(pinger_callback);
868 #endif
869         return 0;
870 }
871
872 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
873 {
874 #ifdef ENABLE_PINGER
875         mutex_down(&pinger_sem);
876         ptlrpc_update_next_ping(imp);
877         if (pinger_args.pd_set == NULL &&
878             time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
879                 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
880                         imp->imp_next_ping, cfs_time_current());
881                 pinger_args.pd_next_ping = imp->imp_next_ping;
882         }
883         mutex_up(&pinger_sem);
884 #endif
885 }
886
887 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
888                               timeout_cb_t cb, void *data,
889                               struct list_head *obd_list)
890 {
891         return 0;
892 }           
893
894 int ptlrpc_del_timeout_client(struct list_head *obd_list)
895 {
896         return 0;
897 }  
898
899 int ptlrpc_pinger_add_import(struct obd_import *imp)
900 {
901         ENTRY;
902         if (!list_empty(&imp->imp_pinger_chain))
903                 RETURN(-EALREADY);
904
905         CDEBUG(D_HA, "adding pingable import %s->%s\n",
906                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
907         ptlrpc_pinger_sending_on_import(imp);
908
909         mutex_down(&pinger_sem);
910         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
911         class_import_get(imp);
912         mutex_up(&pinger_sem);
913
914         RETURN(0);
915 }
916
917 int ptlrpc_pinger_del_import(struct obd_import *imp)
918 {
919         ENTRY;
920         if (list_empty(&imp->imp_pinger_chain))
921                 RETURN(-ENOENT);
922
923         mutex_down(&pinger_sem);
924         list_del_init(&imp->imp_pinger_chain);
925         CDEBUG(D_HA, "removing pingable import %s->%s\n",
926                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
927         class_import_put(imp);
928         mutex_up(&pinger_sem);
929         RETURN(0);
930 }
931
932 void ptlrpc_pinger_wake_up()
933 {
934 #ifdef ENABLE_PINGER
935         /* XXX force pinger to run, if needed */
936         struct obd_import *imp;
937         ENTRY;
938         list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
939                 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
940                        imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
941 #ifdef ENABLE_LIBLUSTRE_RECOVERY
942                 if (imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
943 #else
944                 /*XXX only recover for the initial connection */
945                 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
946                     imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
947 #endif
948                         ptlrpc_initiate_recovery(imp);
949                 else if (imp->imp_state != LUSTRE_IMP_FULL)
950                         CDEBUG(D_HA, "Refused to recover import %s->%s "
951                                      "state %d, deactive %d\n",
952                                      imp->imp_obd->obd_uuid.uuid,
953                                      obd2cli_tgt(imp->imp_obd), imp->imp_state,
954                                      imp->imp_deactive);
955         }
956         EXIT;
957 #endif
958 }
959 #endif /* !__KERNEL__ */