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  * Portal-RPC reconnection and replay operations, for use in recovery.
5  *
6  *  Copyright (c) 2003 Cluster File Systems, Inc.
7  *   Authors: Phil Schwan <phil@clusterfs.com>
8  *            Mike Shaver <shaver@clusterfs.com>
9  *
10  *   This file is part of the Lustre file system, http://www.lustre.org
11  *   Lustre is a trademark of Cluster File Systems, Inc.
12  *
13  *   You may have signed or agreed to another license before downloading
14  *   this software.  If so, you are bound by the terms and conditions
15  *   of that agreement, and the following does not apply to you.  See the
16  *   LICENSE file included with this distribution for more information.
17  *
18  *   If you did not agree to a different license, then this copy of Lustre
19  *   is open source software; you can redistribute it and/or modify it
20  *   under the terms of version 2 of the GNU General Public License as
21  *   published by the Free Software Foundation.
22  *
23  *   In either case, Lustre is distributed in the hope that it will be
24  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *   license text for more details.
27  */
28
29 #ifndef __KERNEL__
30 #include <liblustre.h>
31 #else
32 #define DEBUG_SUBSYSTEM S_RPC
33 #endif
34
35 #include <obd_support.h>
36 #include <obd_class.h>
37 #include "ptlrpc_internal.h"
38
39 struct semaphore pinger_sem;
40 static CFS_LIST_HEAD(pinger_imports);
41
42 int ptlrpc_ping(struct obd_import *imp)
43 {
44         struct ptlrpc_request *req;
45         ENTRY;
46
47         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
48                                         OBD_PING);
49         if (req == NULL) {
50                 CERROR("OOM trying to ping %s->%s\n",
51                        imp->imp_obd->obd_uuid.uuid,
52                        obd2cli_tgt(imp->imp_obd));
53                 RETURN(-ENOMEM);
54         }
55
56         DEBUG_REQ(D_INFO, req, "pinging %s->%s",
57                   imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
58         req->rq_no_resend = req->rq_no_delay = 1;
59         ptlrpc_request_set_replen(req);
60         ptlrpcd_add_req(req);
61
62         RETURN(0);
63 }
64
65 void ptlrpc_update_next_ping(struct obd_import *imp)
66 {
67 #ifdef ENABLE_PINGER
68         int time = PING_INTERVAL;
69         if (imp->imp_state == LUSTRE_IMP_DISCON) {
70                 int dtime = max_t(int, CONNECTION_SWITCH_MIN,
71                                   AT_OFF ? 0 :
72                                   at_get(&imp->imp_at.iat_net_latency));
73                 time = min(time, dtime);
74         }
75         imp->imp_next_ping = cfs_time_shift(time);
76 #endif /* ENABLE_PINGER */
77 }
78
79 void ptlrpc_ping_import_soon(struct obd_import *imp)
80 {
81         imp->imp_next_ping = cfs_time_current();
82 }
83
84 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
85 {
86         if (imp->imp_server_timeout)
87                 return cfs_time_shift(obd_timeout / 2);
88         else
89                 return cfs_time_shift(obd_timeout);
90 }
91
92 static atomic_t suspend_timeouts = ATOMIC_INIT(0);
93 static cfs_time_t suspend_wakeup_time = 0;
94
95 #ifdef __KERNEL__
96 static wait_queue_head_t suspend_timeouts_waitq;
97 #endif
98
99 cfs_time_t ptlrpc_suspend_wakeup_time(void)
100 {
101         return suspend_wakeup_time;
102 }
103
104 void ptlrpc_deactivate_timeouts(struct obd_import *imp)
105 {
106         /*XXX: disabled for now, will be replaced by adaptive timeouts */
107 #if 0
108         if (imp->imp_no_timeout)
109                 return;
110         imp->imp_no_timeout = 1;
111         atomic_inc(&suspend_timeouts);
112         CDEBUG(D_HA|D_WARNING, "deactivate timeouts %u\n", atomic_read(&suspend_timeouts));
113 #endif
114 }
115
116 void ptlrpc_activate_timeouts(struct obd_import *imp)
117 {
118         /*XXX: disabled for now, will be replaced by adaptive timeouts */
119 #if 0
120         if (!imp->imp_no_timeout)
121                 return;
122         imp->imp_no_timeout = 0;
123         LASSERT(atomic_read(&suspend_timeouts) > 0);
124         if (atomic_dec_and_test(&suspend_timeouts)) {
125                 suspend_wakeup_time = cfs_time_current();
126                 wake_up(&suspend_timeouts_waitq);
127         }
128         CDEBUG(D_HA|D_WARNING, "activate timeouts %u\n", atomic_read(&suspend_timeouts));
129 #endif
130 }
131
132 int ptlrpc_check_suspend(void)
133 {
134         if (atomic_read(&suspend_timeouts))
135                 return 1;
136         return 0;
137 }
138
139 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req)
140 {
141         struct l_wait_info lwi;
142
143         if (atomic_read(&suspend_timeouts)) {
144                 DEBUG_REQ(D_NET, req, "-- suspend %d regular timeout",
145                           atomic_read(&suspend_timeouts));
146                 lwi = LWI_INTR(NULL, NULL);
147                 l_wait_event(suspend_timeouts_waitq,
148                              atomic_read(&suspend_timeouts) == 0, &lwi);
149                 DEBUG_REQ(D_NET, req, "-- recharge regular timeout");
150                 return 1;
151         }
152         return 0;
153 }
154
155 #ifdef __KERNEL__
156
157 static void ptlrpc_pinger_process_import(struct obd_import *imp,
158                                          unsigned long this_ping)
159 {
160         int force, level;
161
162         spin_lock(&imp->imp_lock);
163         level = imp->imp_state;
164         force = imp->imp_force_verify;
165         if (force)
166                 imp->imp_force_verify = 0;
167         spin_unlock(&imp->imp_lock);
168
169         CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA,
170                "level %s/%u force %u deactive %u pingable %u\n",
171                ptlrpc_import_state_name(level), level,
172                force, imp->imp_deactive, imp->imp_pingable);
173
174         if (cfs_time_aftereq(imp->imp_next_ping - 5 * CFS_TICK,
175                              this_ping) && force == 0)
176                 return;
177
178         if (level == LUSTRE_IMP_DISCON && !imp->imp_deactive) {
179                 /* wait at least a timeout before trying recovery again */
180                 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
181                 ptlrpc_initiate_recovery(imp);
182         } else if (level != LUSTRE_IMP_FULL ||
183                    imp->imp_obd->obd_no_recov ||
184                    imp->imp_deactive) {
185                 CDEBUG(D_HA, "not pinging %s (in recovery "
186                        " or recovery disabled: %s)\n",
187                        obd2cli_tgt(imp->imp_obd),
188                        ptlrpc_import_state_name(level));
189         } else if (imp->imp_pingable || force) {
190                 ptlrpc_ping(imp);
191         }
192 }
193
194 static int ptlrpc_pinger_main(void *arg)
195 {
196         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
197         struct ptlrpc_thread *thread = data->thread;
198         ENTRY;
199
200         cfs_daemonize(data->name);
201
202         /* Record that the thread is running */
203         thread->t_flags = SVC_RUNNING;
204         cfs_waitq_signal(&thread->t_ctl_waitq);
205
206         /* And now, loop forever, pinging as needed. */
207         while (1) {
208                 cfs_time_t this_ping = cfs_time_current();
209                 struct l_wait_info lwi;
210                 cfs_duration_t time_to_next_ping;
211                 struct list_head *iter;
212
213                 mutex_down(&pinger_sem);
214                 list_for_each(iter, &pinger_imports) {
215                         struct obd_import *imp =
216                                 list_entry(iter, struct obd_import,
217                                            imp_pinger_chain);
218
219                         ptlrpc_pinger_process_import(imp, this_ping);
220                         /* obd_timeout might have changed */
221                         if (imp->imp_pingable && imp->imp_next_ping &&
222                             cfs_time_after(imp->imp_next_ping,
223                                            cfs_time_add(this_ping,
224                                                         cfs_time_seconds(PING_INTERVAL))))
225                                 ptlrpc_update_next_ping(imp);
226                 }
227                 mutex_up(&pinger_sem);
228                 /* update memory usage info */
229                 obd_update_maxusage();
230
231                 /* Wait until the next ping time, or until we're stopped. */
232                 time_to_next_ping = cfs_time_sub(cfs_time_add(this_ping,
233                                                cfs_time_seconds(PING_INTERVAL)),
234                                                cfs_time_current());
235
236                 /* The ping sent by ptlrpc_send_rpc may get sent out
237                    say .01 second after this.
238                    ptlrpc_pinger_sending_on_import will then set the
239                    next ping time to next_ping + .01 sec, which means
240                    we will SKIP the next ping at next_ping, and the
241                    ping will get sent 2 timeouts from now!  Beware. */
242                 CDEBUG(D_INFO, "next ping in "CFS_DURATION_T" ("CFS_TIME_T")\n",
243                                time_to_next_ping,
244                                cfs_time_add(this_ping,
245                                             cfs_time_seconds(PING_INTERVAL)));
246                 if (time_to_next_ping > 0) {
247                         lwi = LWI_TIMEOUT(max_t(cfs_duration_t,
248                                                 time_to_next_ping,
249                                                 cfs_time_seconds(1)),
250                                           NULL, NULL);
251                         l_wait_event(thread->t_ctl_waitq,
252                                      thread->t_flags & (SVC_STOPPING|SVC_EVENT),
253                                      &lwi);
254                         if (thread->t_flags & SVC_STOPPING) {
255                                 thread->t_flags &= ~SVC_STOPPING;
256                                 EXIT;
257                                 break;
258                         } else if (thread->t_flags & SVC_EVENT) {
259                                 /* woken after adding import to reset timer */
260                                 thread->t_flags &= ~SVC_EVENT;
261                         }
262                 }
263         }
264
265         thread->t_flags = SVC_STOPPED;
266         cfs_waitq_signal(&thread->t_ctl_waitq);
267
268         CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
269         return 0;
270 }
271
272 static struct ptlrpc_thread *pinger_thread = NULL;
273
274 int ptlrpc_start_pinger(void)
275 {
276         struct l_wait_info lwi = { 0 };
277         struct ptlrpc_svc_data d;
278         int rc;
279 #ifndef ENABLE_PINGER
280         return 0;
281 #endif
282         ENTRY;
283
284         if (pinger_thread != NULL)
285                 RETURN(-EALREADY);
286
287         OBD_ALLOC_PTR(pinger_thread);
288         if (pinger_thread == NULL)
289                 RETURN(-ENOMEM);
290         cfs_waitq_init(&pinger_thread->t_ctl_waitq);
291         cfs_waitq_init(&suspend_timeouts_waitq);
292
293         d.name = "ll_ping";
294         d.thread = pinger_thread;
295
296         /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
297          * just drop the VM and FILES in ptlrpc_daemonize() right away. */
298         rc = cfs_kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
299         if (rc < 0) {
300                 CERROR("cannot start thread: %d\n", rc);
301                 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
302                 pinger_thread = NULL;
303                 RETURN(rc);
304         }
305         l_wait_event(pinger_thread->t_ctl_waitq,
306                      pinger_thread->t_flags & SVC_RUNNING, &lwi);
307
308         RETURN(0);
309 }
310
311 int ptlrpc_stop_pinger(void)
312 {
313         struct l_wait_info lwi = { 0 };
314         int rc = 0;
315 #ifndef ENABLE_PINGER
316         return 0;
317 #endif
318         ENTRY;
319
320         if (pinger_thread == NULL)
321                 RETURN(-EALREADY);
322         mutex_down(&pinger_sem);
323         pinger_thread->t_flags = SVC_STOPPING;
324         cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
325         mutex_up(&pinger_sem);
326
327         l_wait_event(pinger_thread->t_ctl_waitq,
328                      (pinger_thread->t_flags & SVC_STOPPED), &lwi);
329
330         OBD_FREE_PTR(pinger_thread);
331         pinger_thread = NULL;
332         RETURN(rc);
333 }
334
335 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
336 {
337         ptlrpc_update_next_ping(imp);
338 }
339
340 int ptlrpc_pinger_add_import(struct obd_import *imp)
341 {
342         ENTRY;
343         if (!list_empty(&imp->imp_pinger_chain))
344                 RETURN(-EALREADY);
345
346         mutex_down(&pinger_sem);
347         CDEBUG(D_HA, "adding pingable import %s->%s\n",
348                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
349         /* if we add to pinger we want recovery on this import */
350         imp->imp_obd->obd_no_recov = 0;
351         ptlrpc_update_next_ping(imp);
352         /* XXX sort, blah blah */
353         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
354         class_import_get(imp);
355
356         ptlrpc_pinger_wake_up();
357         mutex_up(&pinger_sem);
358
359         RETURN(0);
360 }
361
362 int ptlrpc_pinger_del_import(struct obd_import *imp)
363 {
364         ENTRY;
365         if (list_empty(&imp->imp_pinger_chain))
366                 RETURN(-ENOENT);
367
368         mutex_down(&pinger_sem);
369         list_del_init(&imp->imp_pinger_chain);
370         CDEBUG(D_HA, "removing pingable import %s->%s\n",
371                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
372         /* if we remove from pinger we don't want recovery on this import */
373         imp->imp_obd->obd_no_recov = 1;
374         class_import_put(imp);
375         mutex_up(&pinger_sem);
376         RETURN(0);
377 }
378
379 void ptlrpc_pinger_wake_up()
380 {
381 #ifdef ENABLE_PINGER
382         pinger_thread->t_flags |= SVC_EVENT;
383         cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
384 #endif
385 }
386
387 /* Ping evictor thread */
388 #define PET_READY     1
389 #define PET_TERMINATE 2
390
391 static int               pet_refcount = 0;
392 static int               pet_state;
393 static wait_queue_head_t pet_waitq;
394 static struct obd_export *pet_exp = NULL;
395 static spinlock_t        pet_lock = SPIN_LOCK_UNLOCKED;
396
397 int ping_evictor_wake(struct obd_export *exp)
398 {
399         spin_lock(&pet_lock);
400         if (pet_exp || (pet_state != PET_READY)) {
401                 /* eventually the new obd will call here again. */
402                 spin_unlock(&pet_lock);
403                 return 1;
404         }
405
406         /* We have to make sure the obd isn't destroyed between now and when
407          * the ping evictor runs.  We'll take a reference here, and drop it
408          * when we finish in the evictor.  We don't really care about this
409          * export in particular; we just need one to keep the obd alive. */
410         pet_exp = class_export_get(exp);
411         spin_unlock(&pet_lock);
412
413         wake_up(&pet_waitq);
414         return 0;
415 }
416
417 static int ping_evictor_main(void *arg)
418 {
419         struct obd_device *obd;
420         struct obd_export *exp;
421         struct l_wait_info lwi = { 0 };
422         time_t expire_time;
423         ENTRY;
424
425         ptlrpc_daemonize("ll_evictor");
426
427         CDEBUG(D_HA, "Starting Ping Evictor\n");
428         pet_exp = NULL;
429         pet_state = PET_READY;
430         while (1) {
431                 l_wait_event(pet_waitq, pet_exp ||
432                              (pet_state == PET_TERMINATE), &lwi);
433                 if (pet_state == PET_TERMINATE)
434                         break;
435
436                 /* we only get here if pet_exp != NULL, and the end of this
437                  * loop is the only place which sets it NULL again, so lock
438                  * is not strictly necessary. */
439                 spin_lock(&pet_lock);
440                 obd = pet_exp->exp_obd;
441                 spin_unlock(&pet_lock);
442
443                 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
444
445                 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
446                        obd->obd_name, expire_time);
447
448                 /* Exports can't be deleted out of the list while we hold
449                  * the obd lock (class_unlink_export), which means we can't
450                  * lose the last ref on the export.  If they've already been
451                  * removed from the list, we won't find them here. */
452                 spin_lock(&obd->obd_dev_lock);
453                 while (!list_empty(&obd->obd_exports_timed)) {
454                         exp = list_entry(obd->obd_exports_timed.next,
455                                          struct obd_export,exp_obd_chain_timed);
456                         if (expire_time > exp->exp_last_request_time) {
457                                 class_export_get(exp);
458                                 spin_unlock(&obd->obd_dev_lock);
459                                  LCONSOLE_WARN("%s: haven't heard from client %s"
460                                               " (at %s) in %ld seconds. I think"
461                                               " it's dead, and I am evicting"
462                                               " it. exp %p, cur %ld expire %ld"
463                                               " last %ld\n",
464                                               obd->obd_name,
465                                               obd_uuid2str(&exp->exp_client_uuid),
466                                               obd_export_nid2str(exp),
467                                               (long)(cfs_time_current_sec() -
468                                                      exp->exp_last_request_time),
469                                               exp, (long)cfs_time_current_sec(),
470                                               (long)expire_time,
471                                               (long)exp->exp_last_request_time);
472                                 CDEBUG(D_HA, "Last request was at %ld\n",
473                                        exp->exp_last_request_time);
474                                 class_fail_export(exp);
475                                 class_export_put(exp);
476                                 spin_lock(&obd->obd_dev_lock);
477                         } else {
478                                 /* List is sorted, so everyone below is ok */
479                                 break;
480                         }
481                 }
482                 spin_unlock(&obd->obd_dev_lock);
483
484                 class_export_put(pet_exp);
485
486                 spin_lock(&pet_lock);
487                 pet_exp = NULL;
488                 spin_unlock(&pet_lock);
489         }
490         CDEBUG(D_HA, "Exiting Ping Evictor\n");
491
492         RETURN(0);
493 }
494
495 void ping_evictor_start(void)
496 {
497         int rc;
498
499         if (++pet_refcount > 1)
500                 return;
501
502         init_waitqueue_head(&pet_waitq);
503
504         rc = cfs_kernel_thread(ping_evictor_main, NULL, CLONE_VM | CLONE_FILES);
505         if (rc < 0) {
506                 pet_refcount--;
507                 CERROR("Cannot start ping evictor thread: %d\n", rc);
508         }
509 }
510 EXPORT_SYMBOL(ping_evictor_start);
511
512 void ping_evictor_stop(void)
513 {
514         if (--pet_refcount > 0)
515                 return;
516
517         pet_state = PET_TERMINATE;
518         wake_up(&pet_waitq);
519 }
520 EXPORT_SYMBOL(ping_evictor_stop);
521 #else /* !__KERNEL__ */
522
523 /* XXX
524  * the current implementation of pinger in liblustre is not optimized
525  */
526
527 #ifdef ENABLE_PINGER
528 static struct pinger_data {
529         int             pd_recursion;
530         cfs_time_t      pd_this_ping;   /* jiffies */
531         cfs_time_t      pd_next_ping;   /* jiffies */
532         struct ptlrpc_request_set *pd_set;
533 } pinger_args;
534
535 static int pinger_check_rpcs(void *arg)
536 {
537         cfs_time_t curtime = cfs_time_current();
538         struct ptlrpc_request *req;
539         struct ptlrpc_request_set *set;
540         struct list_head *iter;
541         struct pinger_data *pd = &pinger_args;
542         int rc;
543
544         /* prevent recursion */
545         if (pd->pd_recursion++) {
546                 CDEBUG(D_HA, "pinger: recursion! quit\n");
547                 LASSERT(pd->pd_set);
548                 pd->pd_recursion--;
549                 return 0;
550         }
551
552         /* have we reached ping point? */
553         if (!pd->pd_set && time_before(curtime, pd->pd_next_ping)) {
554                 pd->pd_recursion--;
555                 return 0;
556         }
557
558         /* if we have rpc_set already, continue processing it */
559         if (pd->pd_set) {
560                 LASSERT(pd->pd_this_ping);
561                 set = pd->pd_set;
562                 goto do_check_set;
563         }
564
565         pd->pd_this_ping = curtime;
566         pd->pd_set = ptlrpc_prep_set();
567         if (pd->pd_set == NULL)
568                 goto out;
569         set = pd->pd_set;
570
571         /* add rpcs into set */
572         mutex_down(&pinger_sem);
573         list_for_each(iter, &pinger_imports) {
574                 struct obd_import *imp =
575                         list_entry(iter, struct obd_import, imp_pinger_chain);
576                 int generation, level;
577
578                 if (cfs_time_aftereq(pd->pd_this_ping,
579                                      imp->imp_next_ping - 5 * CFS_TICK)) {
580                         /* Add a ping. */
581                         spin_lock(&imp->imp_lock);
582                         generation = imp->imp_generation;
583                         level = imp->imp_state;
584                         spin_unlock(&imp->imp_lock);
585
586                         if (level != LUSTRE_IMP_FULL) {
587                                 CDEBUG(D_HA,
588                                        "not pinging %s (in recovery)\n",
589                                        obd2cli_tgt(imp->imp_obd));
590                                 continue;
591                         }
592
593                         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
594                                                         LUSTRE_OBD_VERSION,
595                                                         OBD_PING);
596                         if (req == NULL) {
597                                 CERROR("OOM trying to ping %s->%s\n",
598                                        imp->imp_obd->obd_uuid.uuid,
599                                        obd2cli_tgt(imp->imp_obd));
600                                 break;
601                         }
602
603                         req->rq_no_resend = 1;
604                         ptlrpc_request_set_replen(req);
605                         req->rq_send_state = LUSTRE_IMP_FULL;
606                         req->rq_phase = RQ_PHASE_RPC;
607                         req->rq_import_generation = generation;
608                         ptlrpc_set_add_req(set, req);
609                 } else {
610                         CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
611                                " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
612                                imp->imp_next_ping, pd->pd_this_ping);
613                 }
614         }
615         pd->pd_this_ping = curtime;
616         mutex_up(&pinger_sem);
617
618         /* Might be empty, that's OK. */
619         if (set->set_remaining == 0)
620                 CDEBUG(D_RPCTRACE, "nothing to ping\n");
621
622         list_for_each(iter, &set->set_requests) {
623                 struct ptlrpc_request *req =
624                         list_entry(iter, struct ptlrpc_request,
625                                    rq_set_chain);
626                 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
627                           req->rq_import->imp_obd->obd_uuid.uuid,
628                           obd2cli_tgt(req->rq_import->imp_obd));
629                 (void)ptl_send_rpc(req, 0);
630         }
631
632 do_check_set:
633         rc = ptlrpc_check_set(set);
634
635         /* not finished, and we are not expired, simply return */
636         if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
637                                             cfs_time_seconds(PING_INTERVAL)))) {
638                 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
639                 pd->pd_recursion--;
640                 return 0;
641         }
642
643         /* Expire all the requests that didn't come back. */
644         mutex_down(&pinger_sem);
645         list_for_each(iter, &set->set_requests) {
646                 req = list_entry(iter, struct ptlrpc_request,
647                                  rq_set_chain);
648
649                 if (req->rq_phase == RQ_PHASE_COMPLETE)
650                         continue;
651
652                 req->rq_phase = RQ_PHASE_COMPLETE;
653                 atomic_dec(&req->rq_import->imp_inflight);
654                 set->set_remaining--;
655                 /* If it was disconnected, don't sweat it. */
656                 if (list_empty(&req->rq_import->imp_pinger_chain)) {
657                         ptlrpc_unregister_reply(req);
658                         continue;
659                 }
660
661                 CDEBUG(D_RPCTRACE, "pinger initiate expire_one_request\n");
662                 ptlrpc_expire_one_request(req);
663         }
664         mutex_up(&pinger_sem);
665
666         ptlrpc_set_destroy(set);
667         pd->pd_set = NULL;
668
669 out:
670         pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
671                                         cfs_time_seconds(PING_INTERVAL));
672         pd->pd_this_ping = 0; /* XXX for debug */
673
674         CDEBUG(D_INFO, "finished a round ping\n");
675         pd->pd_recursion--;
676         return 0;
677 }
678
679 static void *pinger_callback = NULL;
680 #endif /* ENABLE_PINGER */
681
682 int ptlrpc_start_pinger(void)
683 {
684 #ifdef ENABLE_PINGER
685         memset(&pinger_args, 0, sizeof(pinger_args));
686         pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
687                                                            &pinger_check_rpcs,
688                                                            &pinger_args);
689 #endif
690         return 0;
691 }
692
693 int ptlrpc_stop_pinger(void)
694 {
695 #ifdef ENABLE_PINGER
696         if (pinger_callback)
697                 liblustre_deregister_wait_callback(pinger_callback);
698 #endif
699         return 0;
700 }
701
702 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
703 {
704 #ifdef ENABLE_PINGER
705         mutex_down(&pinger_sem);
706         ptlrpc_update_next_ping(imp);
707         if (pinger_args.pd_set == NULL &&
708             time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
709                 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
710                         imp->imp_next_ping, cfs_time_current());
711                 pinger_args.pd_next_ping = imp->imp_next_ping;
712         }
713         mutex_up(&pinger_sem);
714 #endif
715 }
716
717 int ptlrpc_pinger_add_import(struct obd_import *imp)
718 {
719         ENTRY;
720         if (!list_empty(&imp->imp_pinger_chain))
721                 RETURN(-EALREADY);
722
723         CDEBUG(D_HA, "adding pingable import %s->%s\n",
724                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
725         ptlrpc_pinger_sending_on_import(imp);
726
727         mutex_down(&pinger_sem);
728         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
729         class_import_get(imp);
730         mutex_up(&pinger_sem);
731
732         RETURN(0);
733 }
734
735 int ptlrpc_pinger_del_import(struct obd_import *imp)
736 {
737         ENTRY;
738         if (list_empty(&imp->imp_pinger_chain))
739                 RETURN(-ENOENT);
740
741         mutex_down(&pinger_sem);
742         list_del_init(&imp->imp_pinger_chain);
743         CDEBUG(D_HA, "removing pingable import %s->%s\n",
744                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
745         class_import_put(imp);
746         mutex_up(&pinger_sem);
747         RETURN(0);
748 }
749
750 void ptlrpc_pinger_wake_up()
751 {
752 #ifdef ENABLE_PINGER
753         /* XXX force pinger to run, if needed */
754         struct obd_import *imp;
755         ENTRY;
756         list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
757                 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
758                        imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
759 #ifdef ENABLE_LIBLUSTRE_RECOVERY
760                 if (imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
761 #else
762                 /*XXX only recover for the initial connection */
763                 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
764                     imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
765 #endif
766                         ptlrpc_initiate_recovery(imp);
767                 else if (imp->imp_state != LUSTRE_IMP_FULL)
768                         CDEBUG(D_HA, "Refused to recover import %s->%s "
769                                      "state %d, deactive %d\n",
770                                      imp->imp_obd->obd_uuid.uuid,
771                                      obd2cli_tgt(imp->imp_obd), imp->imp_state,
772                                      imp->imp_deactive);
773         }
774         EXIT;
775 #endif
776 }
777 #endif /* !__KERNEL__ */