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