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