Whamcloud - gitweb
b=3055
[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 struct list_head pinger_imports = CFS_LIST_HEAD_INIT(pinger_imports);
41
42 int ptlrpc_ping(struct obd_import *imp)
43 {
44         struct ptlrpc_request *req;
45         int rc = 0;
46         ENTRY;
47
48         req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING, 1, NULL, NULL);
49         if (req) {
50                 DEBUG_REQ(D_INFO, req, "pinging %s->%s",
51                           imp->imp_obd->obd_uuid.uuid,
52                           obd2cli_tgt(imp->imp_obd));
53                 req->rq_no_resend = req->rq_no_delay = 1;
54                 ptlrpc_req_set_repsize(req, 1, NULL);
55                 ptlrpcd_add_req(req);
56         } else {
57                 CERROR("OOM trying to ping %s->%s\n",
58                        imp->imp_obd->obd_uuid.uuid,
59                        obd2cli_tgt(imp->imp_obd));
60                 rc = -ENOMEM;
61         }
62
63         RETURN(rc);
64 }
65
66 static void ptlrpc_update_next_ping(struct obd_import *imp)
67 {
68         int time = (imp->imp_state != LUSTRE_IMP_DISCON) ? PING_INTERVAL :
69                 /* FIXME should this be limited to LND_TIMEOUT so we don't
70                    build up pings in LND output queues? */
71                 max_t(int, CONNECTION_SWITCH_MIN, 
72                       at_get(&imp->imp_at.iat_net_latency));
73         imp->imp_next_ping = cfs_time_shift(time);
74 }
75
76 void ptlrpc_ping_import_soon(struct obd_import *imp)
77 {
78         imp->imp_next_ping = cfs_time_current();
79 }
80
81 #ifdef __KERNEL__
82 static int ptlrpc_pinger_main(void *arg)
83 {
84         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
85         struct ptlrpc_thread *thread = data->thread;
86         ENTRY;
87
88         cfs_daemonize(data->name);
89
90         /* Record that the thread is running */
91         thread->t_flags = SVC_RUNNING;
92         cfs_waitq_signal(&thread->t_ctl_waitq);
93
94         /* And now, loop forever, pinging as needed. */
95         while (1) {
96                 cfs_time_t this_ping = cfs_time_current();
97                 struct l_wait_info lwi;
98                 cfs_duration_t time_to_next_ping;
99                 struct list_head *iter;
100
101                 mutex_down(&pinger_sem);
102                 list_for_each(iter, &pinger_imports) {
103                         struct obd_import *imp =
104                                 list_entry(iter, struct obd_import,
105                                            imp_pinger_chain);
106                         int force, level;
107
108                         spin_lock(&imp->imp_lock);
109                         level = imp->imp_state;
110                         force = imp->imp_force_verify;
111                         imp->imp_force_verify = 0;
112                         spin_unlock(&imp->imp_lock);
113
114                         CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_RPCTRACE,
115                                "level %s/%u force %u deactive %u pingable %u\n",
116                                ptlrpc_import_state_name(level), level,
117                                force, imp->imp_deactive, imp->imp_pingable);
118
119                         if (force ||
120                             /* if the next ping is within, say, 5 jiffies from
121                                now, go ahead and ping. See note below. */
122                             cfs_time_aftereq(this_ping, 
123                                              imp->imp_next_ping - 5 * CFS_TICK)) {
124                                 if (level == LUSTRE_IMP_DISCON &&
125                                     !imp->imp_deactive) {
126                                         /* wait at least a timeout before
127                                            trying recovery again. */
128                                         imp->imp_next_ping = cfs_time_shift(obd_timeout);
129                                         ptlrpc_initiate_recovery(imp);
130                                 } else if (level != LUSTRE_IMP_FULL ||
131                                          imp->imp_obd->obd_no_recov ||
132                                          imp->imp_deactive) {
133                                         CDEBUG(D_HA, "not pinging %s "
134                                                "(in recovery: %s or recovery "
135                                                "disabled: %u/%u)\n",
136                                                obd2cli_tgt(imp->imp_obd),
137                                                ptlrpc_import_state_name(level),
138                                                imp->imp_deactive,
139                                                imp->imp_obd->obd_no_recov);
140                                 } else if (imp->imp_pingable || force) {
141                                         ptlrpc_ping(imp);
142                                 }
143                         } else {
144                                 if (!imp->imp_pingable)
145                                         continue;
146                                 CDEBUG(D_INFO,
147                                        "don't need to ping %s ("CFS_TIME_T
148                                        " > "CFS_TIME_T")\n",
149                                        obd2cli_tgt(imp->imp_obd),
150                                        imp->imp_next_ping, this_ping);
151                         }
152
153                         /* obd_timeout might have changed */
154                         if (cfs_time_after(imp->imp_next_ping,
155                                            cfs_time_add(this_ping, 
156                                                         cfs_time_seconds(PING_INTERVAL))))
157                                 ptlrpc_update_next_ping(imp);
158                 }
159                 mutex_up(&pinger_sem);
160
161                 /* Wait until the next ping time, or until we're stopped. */
162                 time_to_next_ping = cfs_time_sub(cfs_time_add(this_ping, 
163                                                               cfs_time_seconds(PING_INTERVAL)), 
164                                                  cfs_time_current());
165
166                 /* The ping sent by ptlrpc_send_rpc may get sent out
167                    say .01 second after this.
168                    ptlrpc_pinger_eending_on_import will then set the
169                    next ping time to next_ping + .01 sec, which means
170                    we will SKIP the next ping at next_ping, and the
171                    ping will get sent 2 timeouts from now!  Beware. */
172                 CDEBUG(D_INFO, "next ping in "CFS_DURATION_T" ("CFS_TIME_T")\n", 
173                                time_to_next_ping, 
174                                cfs_time_add(this_ping, cfs_time_seconds(PING_INTERVAL)));
175                 if (time_to_next_ping > 0) {
176                         lwi = LWI_TIMEOUT(max_t(cfs_duration_t, time_to_next_ping, cfs_time_seconds(1)),
177                                           NULL, NULL);
178                         l_wait_event(thread->t_ctl_waitq,
179                                      thread->t_flags & (SVC_STOPPING|SVC_EVENT),
180                                      &lwi);
181                         if (thread->t_flags & SVC_STOPPING) {
182                                 thread->t_flags &= ~SVC_STOPPING;
183                                 EXIT;
184                                 break;
185                         } else if (thread->t_flags & SVC_EVENT) {
186                                 /* woken after adding import to reset timer */
187                                 thread->t_flags &= ~SVC_EVENT;
188                         }
189                 }
190         }
191
192         thread->t_flags = SVC_STOPPED;
193         cfs_waitq_signal(&thread->t_ctl_waitq);
194
195         CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
196         return 0;
197 }
198
199 static struct ptlrpc_thread *pinger_thread = NULL;
200
201 int ptlrpc_start_pinger(void)
202 {
203         struct l_wait_info lwi = { 0 };
204         struct ptlrpc_svc_data d;
205         int rc;
206 #ifndef ENABLE_PINGER
207         return 0;
208 #endif
209         ENTRY;
210
211         if (pinger_thread != NULL)
212                 RETURN(-EALREADY);
213
214         OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
215         if (pinger_thread == NULL)
216                 RETURN(-ENOMEM);
217         cfs_waitq_init(&pinger_thread->t_ctl_waitq);
218
219         d.name = "ll_ping";
220         d.thread = pinger_thread;
221
222         /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
223          * just drop the VM and FILES in ptlrpc_daemonize() right away. */
224         rc = cfs_kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
225         if (rc < 0) {
226                 CERROR("cannot start thread: %d\n", rc);
227                 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
228                 pinger_thread = NULL;
229                 RETURN(rc);
230         }
231         l_wait_event(pinger_thread->t_ctl_waitq,
232                      pinger_thread->t_flags & SVC_RUNNING, &lwi);
233
234         RETURN(0);
235 }
236
237 int ptlrpc_stop_pinger(void)
238 {
239         struct l_wait_info lwi = { 0 };
240         int rc = 0;
241 #ifndef ENABLE_PINGER
242         return 0;
243 #endif
244         ENTRY;
245
246         if (pinger_thread == NULL)
247                 RETURN(-EALREADY);
248         mutex_down(&pinger_sem);
249         pinger_thread->t_flags = SVC_STOPPING;
250         cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
251         mutex_up(&pinger_sem);
252
253         l_wait_event(pinger_thread->t_ctl_waitq,
254                      (pinger_thread->t_flags & SVC_STOPPED), &lwi);
255
256         OBD_FREE(pinger_thread, sizeof(*pinger_thread));
257         pinger_thread = NULL;
258         RETURN(rc);
259 }
260
261 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
262 {
263         ptlrpc_update_next_ping(imp);
264 }
265
266 int ptlrpc_pinger_add_import(struct obd_import *imp)
267 {
268         ENTRY;
269         if (!list_empty(&imp->imp_pinger_chain))
270                 RETURN(-EALREADY);
271
272         mutex_down(&pinger_sem);
273         CDEBUG(D_HA, "adding pingable import %s->%s\n",
274                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
275         /* if we add to pinger we want recovery on this import */
276         imp->imp_obd->obd_no_recov = 0;
277
278         ptlrpc_update_next_ping(imp);
279         /* XXX sort, blah blah */
280         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
281         class_import_get(imp);
282
283         ptlrpc_pinger_wake_up();
284         mutex_up(&pinger_sem);
285
286         RETURN(0);
287 }
288
289 int ptlrpc_pinger_del_import(struct obd_import *imp)
290 {
291         ENTRY;
292         if (list_empty(&imp->imp_pinger_chain))
293                 RETURN(-ENOENT);
294
295         mutex_down(&pinger_sem);
296         list_del_init(&imp->imp_pinger_chain);
297         CDEBUG(D_HA, "removing pingable import %s->%s\n",
298                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
299         /* if we remove from pinger we don't want recovery on this import */
300         imp->imp_obd->obd_no_recov = 1;
301         class_import_put(imp);
302         mutex_up(&pinger_sem);
303         RETURN(0);
304 }
305
306 void ptlrpc_pinger_wake_up()
307 {
308 #ifdef ENABLE_PINGER
309         pinger_thread->t_flags |= SVC_EVENT;
310         cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
311 #endif
312 }
313
314 /* Ping evictor thread */
315 #define PET_READY     1
316 #define PET_TERMINATE 2
317
318 static int               pet_refcount = 0;
319 static int               pet_state;
320 static wait_queue_head_t pet_waitq;
321 static struct obd_export *pet_exp = NULL;
322 static spinlock_t        pet_lock = SPIN_LOCK_UNLOCKED;
323
324 int ping_evictor_wake(struct obd_export *exp)
325 {
326         spin_lock(&pet_lock);
327         if (pet_exp) {
328                 /* eventually the new obd will call here again. */
329                 spin_unlock(&pet_lock);
330                 return 1;
331         }
332
333         /* We have to make sure the obd isn't destroyed between now and when
334          * the ping evictor runs.  We'll take a reference here, and drop it
335          * when we finish in the evictor.  We don't really care about this
336          * export in particular; we just need one to keep the obd alive. */
337         pet_exp = class_export_get(exp);
338         spin_unlock(&pet_lock);
339
340         wake_up(&pet_waitq);
341         return 0;
342 }
343
344 static int ping_evictor_main(void *arg)
345 {
346         struct obd_device *obd;
347         struct obd_export *exp;
348         struct l_wait_info lwi = { 0 };
349         time_t expire_time;
350         ENTRY;
351
352         ptlrpc_daemonize("ll_evictor");
353
354         CDEBUG(D_HA, "Starting Ping Evictor\n");
355         pet_exp = NULL;
356         pet_state = PET_READY;
357         while (1) {
358                 l_wait_event(pet_waitq, pet_exp ||
359                              (pet_state == PET_TERMINATE), &lwi);
360                 if (pet_state == PET_TERMINATE)
361                         break;
362
363                 /* we only get here if pet_exp != NULL, and the end of this
364                  * loop is the only place which sets it NULL again, so lock
365                  * is not strictly necessary. */
366                 spin_lock(&pet_lock);
367                 obd = pet_exp->exp_obd;
368                 spin_unlock(&pet_lock);
369
370                 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
371
372                 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
373                        obd->obd_name, expire_time);
374
375                 /* Exports can't be deleted out of the list while we hold
376                  * the obd lock (class_unlink_export), which means we can't
377                  * lose the last ref on the export.  If they've already been
378                  * removed from the list, we won't find them here. */
379                 spin_lock(&obd->obd_dev_lock);
380                 while (!list_empty(&obd->obd_exports_timed)) {
381                         exp = list_entry(obd->obd_exports_timed.next,
382                                          struct obd_export,exp_obd_chain_timed);
383                         if (expire_time > exp->exp_last_request_time) {
384                                 class_export_get(exp);
385                                 spin_unlock(&obd->obd_dev_lock);
386                                 LCONSOLE_WARN("%s: haven't heard from client %s"
387                                               " (at %s) in %ld seconds. I think"
388                                               " it's dead, and I am evicting"
389                                               " it.\n", obd->obd_name,
390                                               obd_uuid2str(&exp->exp_client_uuid),
391                                               obd_export_nid2str(exp),
392                                               (long)(cfs_time_current_sec() -
393                                                      exp->exp_last_request_time));
394                                 CDEBUG(D_HA, "Last request was at %ld\n",
395                                        exp->exp_last_request_time);
396                                 class_fail_export(exp);
397                                 class_export_put(exp);
398                                 spin_lock(&obd->obd_dev_lock);
399                         } else {
400                                 /* List is sorted, so everyone below is ok */
401                                 break;
402                         }
403                 }
404                 spin_unlock(&obd->obd_dev_lock);
405
406                 class_export_put(pet_exp);
407
408                 spin_lock(&pet_lock);
409                 pet_exp = NULL;
410                 spin_unlock(&pet_lock);
411         }
412         CDEBUG(D_HA, "Exiting Ping Evictor\n");
413
414         RETURN(0);
415 }
416
417 void ping_evictor_start(void)
418 {
419         int rc;
420
421         if (++pet_refcount > 1)
422                 return;
423
424         init_waitqueue_head(&pet_waitq);
425
426         rc = cfs_kernel_thread(ping_evictor_main, NULL, CLONE_VM | CLONE_FILES);
427         if (rc < 0) {
428                 pet_refcount--;
429                 CERROR("Cannot start ping evictor thread: %d\n", rc);
430         }
431 }
432 EXPORT_SYMBOL(ping_evictor_start);
433
434 void ping_evictor_stop(void)
435 {
436         if (--pet_refcount > 0)
437                 return;
438
439         pet_state = PET_TERMINATE;
440         wake_up(&pet_waitq);
441 }
442 EXPORT_SYMBOL(ping_evictor_stop);
443 #else /* !__KERNEL__ */
444
445 /* XXX
446  * the current implementation of pinger in liblustre is not optimized
447  */
448
449 #ifdef ENABLE_PINGER
450 static struct pinger_data {
451         int             pd_recursion;
452         cfs_time_t      pd_this_ping;   /* jiffies */
453         cfs_time_t      pd_next_ping;   /* jiffies */
454         struct ptlrpc_request_set *pd_set;
455 } pinger_args;
456
457 static int pinger_check_rpcs(void *arg)
458 {
459         cfs_time_t curtime = cfs_time_current();
460         struct ptlrpc_request *req;
461         struct ptlrpc_request_set *set;
462         struct list_head *iter;
463         struct pinger_data *pd = &pinger_args;
464         int rc;
465
466         /* prevent recursion */
467         if (pd->pd_recursion++) {
468                 CDEBUG(D_HA, "pinger: recursion! quit\n");
469                 LASSERT(pd->pd_set);
470                 pd->pd_recursion--;
471                 return 0;
472         }
473
474         /* have we reached ping point? */
475         if (!pd->pd_set && time_before(curtime, pd->pd_next_ping)) {
476                 pd->pd_recursion--;
477                 return 0;
478         }
479
480         /* if we have rpc_set already, continue processing it */
481         if (pd->pd_set) {
482                 LASSERT(pd->pd_this_ping);
483                 set = pd->pd_set;
484                 goto do_check_set;
485         }
486
487         pd->pd_this_ping = curtime;
488         pd->pd_set = ptlrpc_prep_set();
489         if (pd->pd_set == NULL)
490                 goto out;
491         set = pd->pd_set;
492
493         /* add rpcs into set */
494         mutex_down(&pinger_sem);
495         list_for_each(iter, &pinger_imports) {
496                 struct obd_import *imp =
497                         list_entry(iter, struct obd_import, imp_pinger_chain);
498                 int generation, level;
499
500                 if (cfs_time_aftereq(pd->pd_this_ping, 
501                                      imp->imp_next_ping - 5 * CFS_TICK)) {
502                         /* Add a ping. */
503                         spin_lock(&imp->imp_lock);
504                         generation = imp->imp_generation;
505                         level = imp->imp_state;
506                         spin_unlock(&imp->imp_lock);
507
508                         if (level != LUSTRE_IMP_FULL) {
509                                 CDEBUG(D_HA,
510                                        "not pinging %s (in recovery)\n",
511                                        obd2cli_tgt(imp->imp_obd));
512                                 continue;
513                         }
514
515                         req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING,
516                                               1, NULL, NULL);
517                         if (!req) {
518                                 CERROR("out of memory\n");
519                                 break;
520                         }
521                         req->rq_no_resend = 1;
522                         ptlrpc_req_set_repsize(req, 1, NULL);
523                         req->rq_send_state = LUSTRE_IMP_FULL;
524                         req->rq_phase = RQ_PHASE_RPC;
525                         req->rq_import_generation = generation;
526                         ptlrpc_set_add_req(set, req);
527                 } else {
528                         CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
529                                " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
530                                imp->imp_next_ping, pd->pd_this_ping);
531                 }
532         }
533         pd->pd_this_ping = curtime;
534         mutex_up(&pinger_sem);
535
536         /* Might be empty, that's OK. */
537         if (set->set_remaining == 0)
538                 CDEBUG(D_RPCTRACE, "nothing to ping\n");
539
540         list_for_each(iter, &set->set_requests) {
541                 struct ptlrpc_request *req =
542                         list_entry(iter, struct ptlrpc_request,
543                                    rq_set_chain);
544                 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
545                           req->rq_import->imp_obd->obd_uuid.uuid,
546                           obd2cli_tgt(req->rq_import->imp_obd));
547                 (void)ptl_send_rpc(req, 0);
548         }
549
550 do_check_set:
551         rc = ptlrpc_check_set(set);
552
553         /* not finished, and we are not expired, simply return */
554         if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
555                                             cfs_time_seconds(PING_INTERVAL)))) {
556                 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
557                 pd->pd_recursion--;
558                 return 0;
559         }
560
561         /* Expire all the requests that didn't come back. */
562         mutex_down(&pinger_sem);
563         list_for_each(iter, &set->set_requests) {
564                 req = list_entry(iter, struct ptlrpc_request,
565                                  rq_set_chain);
566
567                 if (req->rq_phase == RQ_PHASE_COMPLETE)
568                         continue;
569
570                 req->rq_phase = RQ_PHASE_COMPLETE;
571                 atomic_dec(&req->rq_import->imp_inflight);
572                 set->set_remaining--;
573                 /* If it was disconnected, don't sweat it. */
574                 if (list_empty(&req->rq_import->imp_pinger_chain)) {
575                         ptlrpc_unregister_reply(req);
576                         continue;
577                 }
578
579                 CDEBUG(D_RPCTRACE, "pinger initiate expire_one_request\n");
580                 ptlrpc_expire_one_request(req);
581         }
582         mutex_up(&pinger_sem);
583
584         ptlrpc_set_destroy(set);
585         pd->pd_set = NULL;
586
587 out:
588         pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
589                                         cfs_time_seconds(PING_INTERVAL));
590         pd->pd_this_ping = 0; /* XXX for debug */
591
592         CDEBUG(D_INFO, "finished a round ping\n");
593         pd->pd_recursion--;
594         return 0;
595 }
596
597 static void *pinger_callback = NULL;
598 #endif /* ENABLE_PINGER */
599
600 int ptlrpc_start_pinger(void)
601 {
602 #ifdef ENABLE_PINGER
603         memset(&pinger_args, 0, sizeof(pinger_args));
604         pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
605                                                            &pinger_check_rpcs,
606                                                            &pinger_args);
607 #endif
608         return 0;
609 }
610
611 int ptlrpc_stop_pinger(void)
612 {
613 #ifdef ENABLE_PINGER
614         if (pinger_callback)
615                 liblustre_deregister_wait_callback(pinger_callback);
616 #endif
617         return 0;
618 }
619
620 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
621 {
622 #ifdef ENABLE_PINGER
623         mutex_down(&pinger_sem);
624         ptlrpc_update_next_ping(imp);
625         if (pinger_args.pd_set == NULL &&
626             time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
627                 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
628                         imp->imp_next_ping, cfs_time_current());
629                 pinger_args.pd_next_ping = imp->imp_next_ping;
630         }
631         mutex_up(&pinger_sem);
632 #endif
633 }
634
635 int ptlrpc_pinger_add_import(struct obd_import *imp)
636 {
637         ENTRY;
638         if (!list_empty(&imp->imp_pinger_chain))
639                 RETURN(-EALREADY);
640
641         CDEBUG(D_HA, "adding pingable import %s->%s\n",
642                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
643         ptlrpc_pinger_sending_on_import(imp);
644
645         mutex_down(&pinger_sem);
646         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
647         class_import_get(imp);
648         mutex_up(&pinger_sem);
649
650         RETURN(0);
651 }
652
653 int ptlrpc_pinger_del_import(struct obd_import *imp)
654 {
655         ENTRY;
656         if (list_empty(&imp->imp_pinger_chain))
657                 RETURN(-ENOENT);
658
659         mutex_down(&pinger_sem);
660         list_del_init(&imp->imp_pinger_chain);
661         CDEBUG(D_HA, "removing pingable import %s->%s\n",
662                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
663         class_import_put(imp);
664         mutex_up(&pinger_sem);
665         RETURN(0);
666 }
667
668 void ptlrpc_pinger_wake_up()
669 {
670 #ifdef ENABLE_PINGER
671         ENTRY;
672         /* XXX force pinger to run, if needed */
673         struct obd_import *imp;
674         list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
675                 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
676                        imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
677 #ifdef ENABLE_LIBLUSTRE_RECOVERY
678                 if (imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
679 #else
680                 /*XXX only recover for the initial connection */
681                 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
682                     imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
683 #endif
684                         ptlrpc_initiate_recovery(imp);
685                 else if (imp->imp_state != LUSTRE_IMP_FULL)
686                         CDEBUG(D_HA, "Refused to recover import %s->%s "
687                                      "state %d, deactive %d\n",
688                                      imp->imp_obd->obd_uuid.uuid,
689                                      obd2cli_tgt(imp->imp_obd), imp->imp_state,
690                                      imp->imp_deactive);
691         }
692 #endif
693         EXIT;
694 }
695 #endif /* !__KERNEL__ */