Whamcloud - gitweb
LU-2850 kernel: 3.8 upstream kills daemonize()
[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, 2013, 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         /* Record that the thread is running */
310         thread_set_flags(thread, SVC_RUNNING);
311         cfs_waitq_signal(&thread->t_ctl_waitq);
312
313         /* And now, loop forever, pinging as needed. */
314         while (1) {
315                 cfs_time_t this_ping = cfs_time_current();
316                 struct l_wait_info lwi;
317                 cfs_duration_t time_to_next_wake;
318                 struct timeout_item *item;
319                 cfs_list_t *iter;
320
321                 mutex_lock(&pinger_mutex);
322                 cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
323                         item->ti_cb(item, item->ti_cb_data);
324                 }
325                 cfs_list_for_each(iter, &pinger_imports) {
326                         struct obd_import *imp =
327                                 cfs_list_entry(iter, struct obd_import,
328                                                imp_pinger_chain);
329
330                         ptlrpc_pinger_process_import(imp, this_ping);
331                         /* obd_timeout might have changed */
332                         if (imp->imp_pingable && imp->imp_next_ping &&
333                             cfs_time_after(imp->imp_next_ping,
334                                            cfs_time_add(this_ping,
335                                                         cfs_time_seconds(PING_INTERVAL))))
336                                 ptlrpc_update_next_ping(imp, 0);
337                 }
338                 mutex_unlock(&pinger_mutex);
339                 /* update memory usage info */
340                 obd_update_maxusage();
341
342                 /* Wait until the next ping time, or until we're stopped. */
343                 time_to_next_wake = pinger_check_timeout(this_ping);
344                 /* The ping sent by ptlrpc_send_rpc may get sent out
345                    say .01 second after this.
346                    ptlrpc_pinger_sending_on_import will then set the
347                    next ping time to next_ping + .01 sec, which means
348                    we will SKIP the next ping at next_ping, and the
349                    ping will get sent 2 timeouts from now!  Beware. */
350                 CDEBUG(D_INFO, "next wakeup in "CFS_DURATION_T" ("
351                        CFS_TIME_T")\n", time_to_next_wake,
352                        cfs_time_add(this_ping,cfs_time_seconds(PING_INTERVAL)));
353                 if (time_to_next_wake > 0) {
354                         lwi = LWI_TIMEOUT(max_t(cfs_duration_t,
355                                                 time_to_next_wake,
356                                                 cfs_time_seconds(1)),
357                                           NULL, NULL);
358                         l_wait_event(thread->t_ctl_waitq,
359                                      thread_is_stopping(thread) ||
360                                      thread_is_event(thread),
361                                      &lwi);
362                         if (thread_test_and_clear_flags(thread, SVC_STOPPING)) {
363                                 EXIT;
364                                 break;
365                         } else {
366                                 /* woken after adding import to reset timer */
367                                 thread_test_and_clear_flags(thread, SVC_EVENT);
368                         }
369                 }
370         }
371
372         thread_set_flags(thread, SVC_STOPPED);
373         cfs_waitq_signal(&thread->t_ctl_waitq);
374
375         CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
376         return 0;
377 }
378
379 static struct ptlrpc_thread pinger_thread;
380
381 int ptlrpc_start_pinger(void)
382 {
383         struct l_wait_info lwi = { 0 };
384         int rc;
385 #ifndef ENABLE_PINGER
386         return 0;
387 #endif
388         ENTRY;
389
390         if (!thread_is_init(&pinger_thread) &&
391             !thread_is_stopped(&pinger_thread))
392                 RETURN(-EALREADY);
393
394         cfs_waitq_init(&pinger_thread.t_ctl_waitq);
395         cfs_waitq_init(&suspend_timeouts_waitq);
396
397         strcpy(pinger_thread.t_name, "ll_ping");
398
399         /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
400          * just drop the VM and FILES in kthread_run() right away. */
401         rc = PTR_ERR(kthread_run(ptlrpc_pinger_main,
402                                  &pinger_thread, pinger_thread.t_name));
403         if (IS_ERR_VALUE(rc)) {
404                 CERROR("cannot start thread: %d\n", rc);
405                 RETURN(rc);
406         }
407         l_wait_event(pinger_thread.t_ctl_waitq,
408                      thread_is_running(&pinger_thread), &lwi);
409
410         if (suppress_pings)
411                 CWARN("Pings will be suppressed at the request of the "
412                       "administrator.  The configuration shall meet the "
413                       "additional requirements described in the manual.  "
414                       "(Search for the \"suppress_pings\" kernel module "
415                       "parameter.)\n");
416
417         RETURN(0);
418 }
419
420 int ptlrpc_pinger_remove_timeouts(void);
421
422 int ptlrpc_stop_pinger(void)
423 {
424         struct l_wait_info lwi = { 0 };
425 #ifndef ENABLE_PINGER
426         return 0;
427 #endif
428         ENTRY;
429
430         if (thread_is_init(&pinger_thread) ||
431             thread_is_stopped(&pinger_thread))
432                 RETURN(-EALREADY);
433
434         ptlrpc_pinger_remove_timeouts();
435
436         thread_set_flags(&pinger_thread, SVC_STOPPING);
437         cfs_waitq_signal(&pinger_thread.t_ctl_waitq);
438
439         l_wait_event(pinger_thread.t_ctl_waitq,
440                      thread_is_stopped(&pinger_thread), &lwi);
441         RETURN(0);
442 }
443
444 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
445 {
446         ptlrpc_update_next_ping(imp, 0);
447 }
448 EXPORT_SYMBOL(ptlrpc_pinger_sending_on_import);
449
450 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
451 {
452         ptlrpc_update_next_ping(imp, 1);
453         LASSERT_SPIN_LOCKED(&imp->imp_lock);
454         /*
455          * Avoid reading stale imp_connect_data.  When not sure if pings are
456          * expected or not on next connection, we assume they are not and force
457          * one anyway to guarantee the chance of updating
458          * imp_peer_committed_transno.
459          */
460         if (imp->imp_state != LUSTRE_IMP_FULL ||
461             OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS))
462                 imp->imp_force_next_verify = 1;
463 }
464
465 int ptlrpc_pinger_add_import(struct obd_import *imp)
466 {
467         ENTRY;
468         if (!cfs_list_empty(&imp->imp_pinger_chain))
469                 RETURN(-EALREADY);
470
471         mutex_lock(&pinger_mutex);
472         CDEBUG(D_HA, "adding pingable import %s->%s\n",
473                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
474         /* if we add to pinger we want recovery on this import */
475         imp->imp_obd->obd_no_recov = 0;
476         ptlrpc_update_next_ping(imp, 0);
477         /* XXX sort, blah blah */
478         cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
479         class_import_get(imp);
480
481         ptlrpc_pinger_wake_up();
482         mutex_unlock(&pinger_mutex);
483
484         RETURN(0);
485 }
486 EXPORT_SYMBOL(ptlrpc_pinger_add_import);
487
488 int ptlrpc_pinger_del_import(struct obd_import *imp)
489 {
490         ENTRY;
491         if (cfs_list_empty(&imp->imp_pinger_chain))
492                 RETURN(-ENOENT);
493
494         mutex_lock(&pinger_mutex);
495         cfs_list_del_init(&imp->imp_pinger_chain);
496         CDEBUG(D_HA, "removing pingable import %s->%s\n",
497                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
498         /* if we remove from pinger we don't want recovery on this import */
499         imp->imp_obd->obd_no_recov = 1;
500         class_import_put(imp);
501         mutex_unlock(&pinger_mutex);
502         RETURN(0);
503 }
504 EXPORT_SYMBOL(ptlrpc_pinger_del_import);
505
506 /**
507  * Register a timeout callback to the pinger list, and the callback will
508  * be called when timeout happens.
509  */
510 struct timeout_item* ptlrpc_new_timeout(int time, enum timeout_event event,
511                                         timeout_cb_t cb, void *data)
512 {
513         struct timeout_item *ti;
514
515         OBD_ALLOC_PTR(ti);
516         if (!ti)
517                 return(NULL);
518
519         CFS_INIT_LIST_HEAD(&ti->ti_obd_list);
520         CFS_INIT_LIST_HEAD(&ti->ti_chain);
521         ti->ti_timeout = time;
522         ti->ti_event = event;
523         ti->ti_cb = cb;
524         ti->ti_cb_data = data;
525
526         return ti;
527 }
528
529 /**
530  * Register timeout event on the the pinger thread.
531  * Note: the timeout list is an sorted list with increased timeout value.
532  */
533 static struct timeout_item*
534 ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
535                                timeout_cb_t cb, void *data)
536 {
537         struct timeout_item *item, *tmp;
538
539         LASSERT_MUTEX_LOCKED(&pinger_mutex);
540
541         cfs_list_for_each_entry(item, &timeout_list, ti_chain)
542                 if (item->ti_event == event)
543                         goto out;
544
545         item = ptlrpc_new_timeout(time, event, cb, data);
546         if (item) {
547                 cfs_list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
548                         if (tmp->ti_timeout < time) {
549                                 cfs_list_add(&item->ti_chain, &tmp->ti_chain);
550                                 goto out;
551                         }
552                 }
553                 cfs_list_add(&item->ti_chain, &timeout_list);
554         }
555 out:
556         return item;
557 }
558
559 /* Add a client_obd to the timeout event list, when timeout(@time)
560  * happens, the callback(@cb) will be called.
561  */
562 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
563                               timeout_cb_t cb, void *data,
564                               cfs_list_t *obd_list)
565 {
566         struct timeout_item *ti;
567
568         mutex_lock(&pinger_mutex);
569         ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
570         if (!ti) {
571                 mutex_unlock(&pinger_mutex);
572                 return (-EINVAL);
573         }
574         cfs_list_add(obd_list, &ti->ti_obd_list);
575         mutex_unlock(&pinger_mutex);
576         return 0;
577 }
578 EXPORT_SYMBOL(ptlrpc_add_timeout_client);
579
580 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
581                               enum timeout_event event)
582 {
583         struct timeout_item *ti = NULL, *item;
584
585         if (cfs_list_empty(obd_list))
586                 return 0;
587         mutex_lock(&pinger_mutex);
588         cfs_list_del_init(obd_list);
589         /**
590          * If there are no obd attached to the timeout event
591          * list, remove this timeout event from the pinger
592          */
593         cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
594                 if (item->ti_event == event) {
595                         ti = item;
596                         break;
597                 }
598         }
599         LASSERTF(ti != NULL, "ti is NULL ! \n");
600         if (cfs_list_empty(&ti->ti_obd_list)) {
601                 cfs_list_del(&ti->ti_chain);
602                 OBD_FREE_PTR(ti);
603         }
604         mutex_unlock(&pinger_mutex);
605         return 0;
606 }
607 EXPORT_SYMBOL(ptlrpc_del_timeout_client);
608
609 int ptlrpc_pinger_remove_timeouts(void)
610 {
611         struct timeout_item *item, *tmp;
612
613         mutex_lock(&pinger_mutex);
614         cfs_list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
615                 LASSERT(cfs_list_empty(&item->ti_obd_list));
616                 cfs_list_del(&item->ti_chain);
617                 OBD_FREE_PTR(item);
618         }
619         mutex_unlock(&pinger_mutex);
620         return 0;
621 }
622
623 void ptlrpc_pinger_wake_up()
624 {
625 #ifdef ENABLE_PINGER
626         thread_add_flags(&pinger_thread, SVC_EVENT);
627         cfs_waitq_signal(&pinger_thread.t_ctl_waitq);
628 #endif
629 }
630
631 /* Ping evictor thread */
632 #define PET_READY     1
633 #define PET_TERMINATE 2
634
635 static int               pet_refcount = 0;
636 static int               pet_state;
637 static cfs_waitq_t       pet_waitq;
638 CFS_LIST_HEAD(pet_list);
639 static DEFINE_SPINLOCK(pet_lock);
640
641 int ping_evictor_wake(struct obd_export *exp)
642 {
643         struct obd_device *obd;
644
645         spin_lock(&pet_lock);
646         if (pet_state != PET_READY) {
647                 /* eventually the new obd will call here again. */
648                 spin_unlock(&pet_lock);
649                 return 1;
650         }
651
652         obd = class_exp2obd(exp);
653         if (cfs_list_empty(&obd->obd_evict_list)) {
654                 class_incref(obd, "evictor", obd);
655                 cfs_list_add(&obd->obd_evict_list, &pet_list);
656         }
657         spin_unlock(&pet_lock);
658
659         cfs_waitq_signal(&pet_waitq);
660         return 0;
661 }
662
663 static int ping_evictor_main(void *arg)
664 {
665         struct obd_device *obd;
666         struct obd_export *exp;
667         struct l_wait_info lwi = { 0 };
668         time_t expire_time;
669         ENTRY;
670
671         unshare_fs_struct();
672
673         CDEBUG(D_HA, "Starting Ping Evictor\n");
674         pet_state = PET_READY;
675         while (1) {
676                 l_wait_event(pet_waitq, (!cfs_list_empty(&pet_list)) ||
677                              (pet_state == PET_TERMINATE), &lwi);
678
679                 /* loop until all obd's will be removed */
680                 if ((pet_state == PET_TERMINATE) && cfs_list_empty(&pet_list))
681                         break;
682
683                 /* we only get here if pet_exp != NULL, and the end of this
684                  * loop is the only place which sets it NULL again, so lock
685                  * is not strictly necessary. */
686                 spin_lock(&pet_lock);
687                 obd = cfs_list_entry(pet_list.next, struct obd_device,
688                                      obd_evict_list);
689                 spin_unlock(&pet_lock);
690
691                 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
692
693                 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
694                        obd->obd_name, expire_time);
695
696                 /* Exports can't be deleted out of the list while we hold
697                  * the obd lock (class_unlink_export), which means we can't
698                  * lose the last ref on the export.  If they've already been
699                  * removed from the list, we won't find them here. */
700                 spin_lock(&obd->obd_dev_lock);
701                 while (!cfs_list_empty(&obd->obd_exports_timed)) {
702                         exp = cfs_list_entry(obd->obd_exports_timed.next,
703                                              struct obd_export,
704                                              exp_obd_chain_timed);
705                         if (expire_time > exp->exp_last_request_time) {
706                                 class_export_get(exp);
707                                 spin_unlock(&obd->obd_dev_lock);
708                                 LCONSOLE_WARN("%s: haven't heard from client %s"
709                                               " (at %s) in %ld seconds. I think"
710                                               " it's dead, and I am evicting"
711                                               " it. exp %p, cur %ld expire %ld"
712                                               " last %ld\n",
713                                               obd->obd_name,
714                                               obd_uuid2str(&exp->exp_client_uuid),
715                                               obd_export_nid2str(exp),
716                                               (long)(cfs_time_current_sec() -
717                                                      exp->exp_last_request_time),
718                                               exp, (long)cfs_time_current_sec(),
719                                               (long)expire_time,
720                                               (long)exp->exp_last_request_time);
721                                 CDEBUG(D_HA, "Last request was at %ld\n",
722                                        exp->exp_last_request_time);
723                                 class_fail_export(exp);
724                                 class_export_put(exp);
725                                 spin_lock(&obd->obd_dev_lock);
726                         } else {
727                                 /* List is sorted, so everyone below is ok */
728                                 break;
729                         }
730                 }
731                 spin_unlock(&obd->obd_dev_lock);
732
733                 spin_lock(&pet_lock);
734                 cfs_list_del_init(&obd->obd_evict_list);
735                 spin_unlock(&pet_lock);
736
737                 class_decref(obd, "evictor", obd);
738         }
739         CDEBUG(D_HA, "Exiting Ping Evictor\n");
740
741         RETURN(0);
742 }
743
744 void ping_evictor_start(void)
745 {
746         cfs_task_t *task;
747
748         if (++pet_refcount > 1)
749                 return;
750
751         cfs_waitq_init(&pet_waitq);
752
753         task = kthread_run(ping_evictor_main, NULL, "ll_evictor");
754         if (IS_ERR(task)) {
755                 pet_refcount--;
756                 CERROR("Cannot start ping evictor thread: %ld\n",
757                         PTR_ERR(task));
758         }
759 }
760 EXPORT_SYMBOL(ping_evictor_start);
761
762 void ping_evictor_stop(void)
763 {
764         if (--pet_refcount > 0)
765                 return;
766
767         pet_state = PET_TERMINATE;
768         cfs_waitq_signal(&pet_waitq);
769 }
770 EXPORT_SYMBOL(ping_evictor_stop);
771 #else /* !__KERNEL__ */
772
773 /* XXX
774  * the current implementation of pinger in liblustre is not optimized
775  */
776
777 #ifdef ENABLE_PINGER
778 static struct pinger_data {
779         int             pd_recursion;
780         cfs_time_t      pd_this_ping;   /* jiffies */
781         cfs_time_t      pd_next_ping;   /* jiffies */
782         struct ptlrpc_request_set *pd_set;
783 } pinger_args;
784
785 static int pinger_check_rpcs(void *arg)
786 {
787         cfs_time_t curtime = cfs_time_current();
788         struct ptlrpc_request *req;
789         struct ptlrpc_request_set *set;
790         cfs_list_t *iter;
791         struct obd_import *imp;
792         struct pinger_data *pd = &pinger_args;
793         int rc;
794
795         /* prevent recursion */
796         if (pd->pd_recursion++) {
797                 CDEBUG(D_HA, "pinger: recursion! quit\n");
798                 LASSERT(pd->pd_set);
799                 pd->pd_recursion--;
800                 return 0;
801         }
802
803         /* have we reached ping point? */
804         if (!pd->pd_set && cfs_time_before(curtime, pd->pd_next_ping)) {
805                 pd->pd_recursion--;
806                 return 0;
807         }
808
809         /* if we have rpc_set already, continue processing it */
810         if (pd->pd_set) {
811                 LASSERT(pd->pd_this_ping);
812                 set = pd->pd_set;
813                 goto do_check_set;
814         }
815
816         pd->pd_this_ping = curtime;
817         pd->pd_set = ptlrpc_prep_set();
818         if (pd->pd_set == NULL)
819                 goto out;
820         set = pd->pd_set;
821
822         /* add rpcs into set */
823         mutex_lock(&pinger_mutex);
824         cfs_list_for_each(iter, &pinger_imports) {
825                 struct obd_import *imp = cfs_list_entry(iter, struct obd_import,
826                                                         imp_pinger_chain);
827                 int generation, level;
828
829                 if (cfs_time_aftereq(pd->pd_this_ping,
830                                      imp->imp_next_ping - 5 * CFS_TICK)) {
831                         /* Add a ping. */
832                         spin_lock(&imp->imp_lock);
833                         generation = imp->imp_generation;
834                         level = imp->imp_state;
835                         spin_unlock(&imp->imp_lock);
836
837                         if (level != LUSTRE_IMP_FULL) {
838                                 CDEBUG(D_HA,
839                                        "not pinging %s (in recovery)\n",
840                                        obd2cli_tgt(imp->imp_obd));
841                                 continue;
842                         }
843
844                         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
845                                                         LUSTRE_OBD_VERSION,
846                                                         OBD_PING);
847                         if (req == NULL) {
848                                 CERROR("OOM trying to ping %s->%s\n",
849                                        imp->imp_obd->obd_uuid.uuid,
850                                        obd2cli_tgt(imp->imp_obd));
851                                 break;
852                         }
853
854                         req->rq_no_resend = 1;
855                         ptlrpc_request_set_replen(req);
856                         req->rq_send_state = LUSTRE_IMP_FULL;
857                         ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
858                         req->rq_import_generation = generation;
859                         ptlrpc_set_add_req(set, req);
860                 } else {
861                         CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
862                                " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
863                                imp->imp_next_ping, pd->pd_this_ping);
864                 }
865         }
866         pd->pd_this_ping = curtime;
867         mutex_unlock(&pinger_mutex);
868
869         /* Might be empty, that's OK. */
870         if (cfs_atomic_read(&set->set_remaining) == 0)
871                 CDEBUG(D_RPCTRACE, "nothing to ping\n");
872
873         cfs_list_for_each(iter, &set->set_requests) {
874                 struct ptlrpc_request *req =
875                         cfs_list_entry(iter, struct ptlrpc_request,
876                                        rq_set_chain);
877                 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
878                           req->rq_import->imp_obd->obd_uuid.uuid,
879                           obd2cli_tgt(req->rq_import->imp_obd));
880                 (void)ptl_send_rpc(req, 0);
881         }
882
883 do_check_set:
884         rc = ptlrpc_check_set(NULL, set);
885
886         /* not finished, and we are not expired, simply return */
887         if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
888                                             cfs_time_seconds(PING_INTERVAL)))) {
889                 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
890                 pd->pd_recursion--;
891                 return 0;
892         }
893
894         /* Expire all the requests that didn't come back. */
895         mutex_lock(&pinger_mutex);
896         cfs_list_for_each(iter, &set->set_requests) {
897                 req = cfs_list_entry(iter, struct ptlrpc_request,
898                                      rq_set_chain);
899
900                 if (req->rq_phase == RQ_PHASE_COMPLETE)
901                         continue;
902
903                 CDEBUG(D_RPCTRACE, "Pinger initiate expire request(%p)\n",
904                        req);
905
906                 /* This will also unregister reply. */
907                 ptlrpc_expire_one_request(req, 0);
908
909                 /* We're done with this req, let's finally move it to complete
910                  * phase and take care of inflights. */
911                 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
912                 imp = req->rq_import;
913                 spin_lock(&imp->imp_lock);
914                 if (!cfs_list_empty(&req->rq_list)) {
915                         cfs_list_del_init(&req->rq_list);
916                         cfs_atomic_dec(&imp->imp_inflight);
917                 }
918                 spin_unlock(&imp->imp_lock);
919                 cfs_atomic_dec(&set->set_remaining);
920         }
921         mutex_unlock(&pinger_mutex);
922
923         ptlrpc_set_destroy(set);
924         pd->pd_set = NULL;
925
926 out:
927         pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
928                                         cfs_time_seconds(PING_INTERVAL));
929         pd->pd_this_ping = 0; /* XXX for debug */
930
931         CDEBUG(D_INFO, "finished a round ping\n");
932         pd->pd_recursion--;
933         return 0;
934 }
935
936 static void *pinger_callback = NULL;
937 #endif /* ENABLE_PINGER */
938
939 int ptlrpc_start_pinger(void)
940 {
941 #ifdef ENABLE_PINGER
942         memset(&pinger_args, 0, sizeof(pinger_args));
943         pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
944                                                            &pinger_check_rpcs,
945                                                            &pinger_args);
946 #endif
947         return 0;
948 }
949
950 int ptlrpc_stop_pinger(void)
951 {
952 #ifdef ENABLE_PINGER
953         if (pinger_callback)
954                 liblustre_deregister_wait_callback(pinger_callback);
955 #endif
956         return 0;
957 }
958
959 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
960 {
961 #ifdef ENABLE_PINGER
962         mutex_lock(&pinger_mutex);
963         ptlrpc_update_next_ping(imp, 0);
964         if (pinger_args.pd_set == NULL &&
965             cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
966                 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
967                         imp->imp_next_ping, cfs_time_current());
968                 pinger_args.pd_next_ping = imp->imp_next_ping;
969         }
970         mutex_unlock(&pinger_mutex);
971 #endif
972 }
973
974 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
975 {
976 #ifdef ENABLE_PINGER
977         mutex_lock(&pinger_mutex);
978         ptlrpc_update_next_ping(imp, 1);
979         if (pinger_args.pd_set == NULL &&
980             cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
981                 CDEBUG(D_HA,"set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
982                         imp->imp_next_ping, cfs_time_current());
983                 pinger_args.pd_next_ping = imp->imp_next_ping;
984         }
985         mutex_unlock(&pinger_mutex);
986 #endif
987 }
988
989 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
990                               timeout_cb_t cb, void *data,
991                               cfs_list_t *obd_list)
992 {
993         return 0;
994 }
995
996 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
997                               enum timeout_event event)
998 {
999         return 0;
1000 }
1001
1002 int ptlrpc_pinger_add_import(struct obd_import *imp)
1003 {
1004         ENTRY;
1005         if (!cfs_list_empty(&imp->imp_pinger_chain))
1006                 RETURN(-EALREADY);
1007
1008         CDEBUG(D_HA, "adding pingable import %s->%s\n",
1009                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
1010         ptlrpc_pinger_sending_on_import(imp);
1011
1012         mutex_lock(&pinger_mutex);
1013         cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
1014         class_import_get(imp);
1015         mutex_unlock(&pinger_mutex);
1016
1017         RETURN(0);
1018 }
1019
1020 int ptlrpc_pinger_del_import(struct obd_import *imp)
1021 {
1022         ENTRY;
1023         if (cfs_list_empty(&imp->imp_pinger_chain))
1024                 RETURN(-ENOENT);
1025
1026         mutex_lock(&pinger_mutex);
1027         cfs_list_del_init(&imp->imp_pinger_chain);
1028         CDEBUG(D_HA, "removing pingable import %s->%s\n",
1029                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
1030         class_import_put(imp);
1031         mutex_unlock(&pinger_mutex);
1032         RETURN(0);
1033 }
1034
1035 void ptlrpc_pinger_wake_up()
1036 {
1037 #ifdef ENABLE_PINGER
1038         /* XXX force pinger to run, if needed */
1039         struct obd_import *imp;
1040         ENTRY;
1041         cfs_list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
1042                 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
1043                        imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
1044 #ifdef ENABLE_LIBLUSTRE_RECOVERY
1045                 if (imp->imp_state == LUSTRE_IMP_DISCON &&
1046                     !imp_is_deactive(imp))
1047 #else
1048                 /*XXX only recover for the initial connection */
1049                 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
1050                     imp->imp_state == LUSTRE_IMP_DISCON &&
1051                     !imp_is_deactive(imp))
1052 #endif
1053                         ptlrpc_initiate_recovery(imp);
1054                 else if (imp->imp_state != LUSTRE_IMP_FULL)
1055                         CDEBUG(D_HA, "Refused to recover import %s->%s "
1056                                      "state %d, deactive %d\n",
1057                                      imp->imp_obd->obd_uuid.uuid,
1058                                      obd2cli_tgt(imp->imp_obd), imp->imp_state,
1059                                      imp_is_deactive(imp));
1060         }
1061         EXIT;
1062 #endif
1063 }
1064 #endif /* !__KERNEL__ */