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