47061740f0bfeb934ede87ff075d4d3342a5b30a
[firefly-linux-kernel-4.4.55.git] / drivers / staging / lustre / 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 #define DEBUG_SUBSYSTEM S_RPC
42
43 #include "../include/obd_support.h"
44 #include "../include/obd_class.h"
45 #include "ptlrpc_internal.h"
46
47 static int suppress_pings;
48 module_param(suppress_pings, int, 0644);
49 MODULE_PARM_DESC(suppress_pings, "Suppress pings");
50
51 struct mutex pinger_mutex;
52 static LIST_HEAD(pinger_imports);
53 static struct list_head timeout_list = LIST_HEAD_INIT(timeout_list);
54
55 int ptlrpc_pinger_suppress_pings(void)
56 {
57         return suppress_pings;
58 }
59 EXPORT_SYMBOL(ptlrpc_pinger_suppress_pings);
60
61 struct ptlrpc_request *
62 ptlrpc_prep_ping(struct obd_import *imp)
63 {
64         struct ptlrpc_request *req;
65
66         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
67                                         LUSTRE_OBD_VERSION, OBD_PING);
68         if (req) {
69                 ptlrpc_request_set_replen(req);
70                 req->rq_no_resend = req->rq_no_delay = 1;
71         }
72         return req;
73 }
74
75 int ptlrpc_obd_ping(struct obd_device *obd)
76 {
77         int rc;
78         struct ptlrpc_request *req;
79
80         req = ptlrpc_prep_ping(obd->u.cli.cl_import);
81         if (req == NULL)
82                 return -ENOMEM;
83
84         req->rq_send_state = LUSTRE_IMP_FULL;
85
86         rc = ptlrpc_queue_wait(req);
87
88         ptlrpc_req_finished(req);
89
90         return rc;
91 }
92 EXPORT_SYMBOL(ptlrpc_obd_ping);
93
94 static int ptlrpc_ping(struct obd_import *imp)
95 {
96         struct ptlrpc_request *req;
97
98         req = ptlrpc_prep_ping(imp);
99         if (req == NULL) {
100                 CERROR("OOM trying to ping %s->%s\n",
101                        imp->imp_obd->obd_uuid.uuid,
102                        obd2cli_tgt(imp->imp_obd));
103                 return -ENOMEM;
104         }
105
106         DEBUG_REQ(D_INFO, req, "pinging %s->%s",
107                   imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
108         ptlrpcd_add_req(req);
109
110         return 0;
111 }
112
113 static void ptlrpc_update_next_ping(struct obd_import *imp, int soon)
114 {
115         int time = soon ? PING_INTERVAL_SHORT : PING_INTERVAL;
116         if (imp->imp_state == LUSTRE_IMP_DISCON) {
117                 int dtime = max_t(int, CONNECTION_SWITCH_MIN,
118                                   AT_OFF ? 0 :
119                                   at_get(&imp->imp_at.iat_net_latency));
120                 time = min(time, dtime);
121         }
122         imp->imp_next_ping = cfs_time_shift(time);
123 }
124
125 void ptlrpc_ping_import_soon(struct obd_import *imp)
126 {
127         imp->imp_next_ping = cfs_time_current();
128 }
129
130 static inline int imp_is_deactive(struct obd_import *imp)
131 {
132         return (imp->imp_deactive ||
133                 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_IMP_DEACTIVE));
134 }
135
136 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
137 {
138         if (imp->imp_server_timeout)
139                 return cfs_time_shift(obd_timeout / 2);
140         else
141                 return cfs_time_shift(obd_timeout);
142 }
143
144 static long pinger_check_timeout(unsigned long time)
145 {
146         struct timeout_item *item;
147         unsigned long timeout = PING_INTERVAL;
148
149         /* The timeout list is a increase order sorted list */
150         mutex_lock(&pinger_mutex);
151         list_for_each_entry(item, &timeout_list, ti_chain) {
152                 int ti_timeout = item->ti_timeout;
153                 if (timeout > ti_timeout)
154                         timeout = ti_timeout;
155                 break;
156         }
157         mutex_unlock(&pinger_mutex);
158
159         return cfs_time_sub(cfs_time_add(time, cfs_time_seconds(timeout)),
160                                          cfs_time_current());
161 }
162
163 static bool ir_up;
164
165 void ptlrpc_pinger_ir_up(void)
166 {
167         CDEBUG(D_HA, "IR up\n");
168         ir_up = true;
169 }
170 EXPORT_SYMBOL(ptlrpc_pinger_ir_up);
171
172 void ptlrpc_pinger_ir_down(void)
173 {
174         CDEBUG(D_HA, "IR down\n");
175         ir_up = false;
176 }
177 EXPORT_SYMBOL(ptlrpc_pinger_ir_down);
178
179 static void ptlrpc_pinger_process_import(struct obd_import *imp,
180                                          unsigned long this_ping)
181 {
182         int level;
183         int force;
184         int force_next;
185         int suppress;
186
187         spin_lock(&imp->imp_lock);
188
189         level = imp->imp_state;
190         force = imp->imp_force_verify;
191         force_next = imp->imp_force_next_verify;
192         /*
193          * This will be used below only if the import is "FULL".
194          */
195         suppress = ir_up && OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS);
196
197         imp->imp_force_verify = 0;
198
199         if (cfs_time_aftereq(imp->imp_next_ping - 5 * CFS_TICK, this_ping) &&
200             !force) {
201                 spin_unlock(&imp->imp_lock);
202                 return;
203         }
204
205         imp->imp_force_next_verify = 0;
206
207         spin_unlock(&imp->imp_lock);
208
209         CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA, "%s->%s: level %s/%u force %u force_next %u deactive %u pingable %u suppress %u\n",
210                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
211                ptlrpc_import_state_name(level), level, force, force_next,
212                imp->imp_deactive, imp->imp_pingable, suppress);
213
214         if (level == LUSTRE_IMP_DISCON && !imp_is_deactive(imp)) {
215                 /* wait for a while before trying recovery again */
216                 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
217                 if (!imp->imp_no_pinger_recover)
218                         ptlrpc_initiate_recovery(imp);
219         } else if (level != LUSTRE_IMP_FULL ||
220                    imp->imp_obd->obd_no_recov ||
221                    imp_is_deactive(imp)) {
222                 CDEBUG(D_HA, "%s->%s: not pinging (in recovery or recovery disabled: %s)\n",
223                        imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
224                        ptlrpc_import_state_name(level));
225                 if (force) {
226                         spin_lock(&imp->imp_lock);
227                         imp->imp_force_verify = 1;
228                         spin_unlock(&imp->imp_lock);
229                 }
230         } else if ((imp->imp_pingable && !suppress) || force_next || force) {
231                 ptlrpc_ping(imp);
232         }
233 }
234
235 static int ptlrpc_pinger_main(void *arg)
236 {
237         struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
238
239         /* Record that the thread is running */
240         thread_set_flags(thread, SVC_RUNNING);
241         wake_up(&thread->t_ctl_waitq);
242
243         /* And now, loop forever, pinging as needed. */
244         while (1) {
245                 unsigned long this_ping = cfs_time_current();
246                 struct l_wait_info lwi;
247                 long time_to_next_wake;
248                 struct timeout_item *item;
249                 struct list_head *iter;
250
251                 mutex_lock(&pinger_mutex);
252                 list_for_each_entry(item, &timeout_list, ti_chain) {
253                         item->ti_cb(item, item->ti_cb_data);
254                 }
255                 list_for_each(iter, &pinger_imports) {
256                         struct obd_import *imp =
257                                 list_entry(iter, struct obd_import,
258                                                imp_pinger_chain);
259
260                         ptlrpc_pinger_process_import(imp, this_ping);
261                         /* obd_timeout might have changed */
262                         if (imp->imp_pingable && imp->imp_next_ping &&
263                             cfs_time_after(imp->imp_next_ping,
264                                            cfs_time_add(this_ping,
265                                                         cfs_time_seconds(PING_INTERVAL))))
266                                 ptlrpc_update_next_ping(imp, 0);
267                 }
268                 mutex_unlock(&pinger_mutex);
269
270                 /* Wait until the next ping time, or until we're stopped. */
271                 time_to_next_wake = pinger_check_timeout(this_ping);
272                 /* The ping sent by ptlrpc_send_rpc may get sent out
273                    say .01 second after this.
274                    ptlrpc_pinger_sending_on_import will then set the
275                    next ping time to next_ping + .01 sec, which means
276                    we will SKIP the next ping at next_ping, and the
277                    ping will get sent 2 timeouts from now!  Beware. */
278                 CDEBUG(D_INFO, "next wakeup in " CFS_DURATION_T " (%ld)\n",
279                        time_to_next_wake,
280                        cfs_time_add(this_ping,
281                                     cfs_time_seconds(PING_INTERVAL)));
282                 if (time_to_next_wake > 0) {
283                         lwi = LWI_TIMEOUT(max_t(long, time_to_next_wake,
284                                                 cfs_time_seconds(1)),
285                                           NULL, NULL);
286                         l_wait_event(thread->t_ctl_waitq,
287                                      thread_is_stopping(thread) ||
288                                      thread_is_event(thread),
289                                      &lwi);
290                         if (thread_test_and_clear_flags(thread, SVC_STOPPING))
291                                 break;
292                         /* woken after adding import to reset timer */
293                         thread_test_and_clear_flags(thread, SVC_EVENT);
294                 }
295         }
296
297         thread_set_flags(thread, SVC_STOPPED);
298         wake_up(&thread->t_ctl_waitq);
299
300         CDEBUG(D_NET, "pinger thread exiting, process %d\n", current_pid());
301         return 0;
302 }
303
304 static struct ptlrpc_thread pinger_thread;
305
306 int ptlrpc_start_pinger(void)
307 {
308         struct l_wait_info lwi = { 0 };
309         int rc;
310
311         if (!thread_is_init(&pinger_thread) &&
312             !thread_is_stopped(&pinger_thread))
313                 return -EALREADY;
314
315         init_waitqueue_head(&pinger_thread.t_ctl_waitq);
316
317         strcpy(pinger_thread.t_name, "ll_ping");
318
319         rc = PTR_ERR(kthread_run(ptlrpc_pinger_main, &pinger_thread,
320                                  "%s", pinger_thread.t_name));
321         if (IS_ERR_VALUE(rc)) {
322                 CERROR("cannot start thread: %d\n", rc);
323                 return rc;
324         }
325         l_wait_event(pinger_thread.t_ctl_waitq,
326                      thread_is_running(&pinger_thread), &lwi);
327
328         if (suppress_pings)
329                 CWARN("Pings will be suppressed at the request of the administrator.  The configuration shall meet the additional requirements described in the manual.  (Search for the \"suppress_pings\" kernel module parameter.)\n");
330
331         return 0;
332 }
333
334 int ptlrpc_pinger_remove_timeouts(void);
335
336 int ptlrpc_stop_pinger(void)
337 {
338         struct l_wait_info lwi = { 0 };
339         int rc = 0;
340
341         if (thread_is_init(&pinger_thread) ||
342             thread_is_stopped(&pinger_thread))
343                 return -EALREADY;
344
345         ptlrpc_pinger_remove_timeouts();
346         thread_set_flags(&pinger_thread, SVC_STOPPING);
347         wake_up(&pinger_thread.t_ctl_waitq);
348
349         l_wait_event(pinger_thread.t_ctl_waitq,
350                      thread_is_stopped(&pinger_thread), &lwi);
351
352         return rc;
353 }
354
355 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
356 {
357         ptlrpc_update_next_ping(imp, 0);
358 }
359 EXPORT_SYMBOL(ptlrpc_pinger_sending_on_import);
360
361 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
362 {
363         ptlrpc_update_next_ping(imp, 1);
364         assert_spin_locked(&imp->imp_lock);
365         /*
366          * Avoid reading stale imp_connect_data.  When not sure if pings are
367          * expected or not on next connection, we assume they are not and force
368          * one anyway to guarantee the chance of updating
369          * imp_peer_committed_transno.
370          */
371         if (imp->imp_state != LUSTRE_IMP_FULL ||
372             OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS))
373                 imp->imp_force_next_verify = 1;
374 }
375
376 int ptlrpc_pinger_add_import(struct obd_import *imp)
377 {
378         if (!list_empty(&imp->imp_pinger_chain))
379                 return -EALREADY;
380
381         mutex_lock(&pinger_mutex);
382         CDEBUG(D_HA, "adding pingable import %s->%s\n",
383                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
384         /* if we add to pinger we want recovery on this import */
385         imp->imp_obd->obd_no_recov = 0;
386         ptlrpc_update_next_ping(imp, 0);
387         /* XXX sort, blah blah */
388         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
389         class_import_get(imp);
390
391         ptlrpc_pinger_wake_up();
392         mutex_unlock(&pinger_mutex);
393
394         return 0;
395 }
396 EXPORT_SYMBOL(ptlrpc_pinger_add_import);
397
398 int ptlrpc_pinger_del_import(struct obd_import *imp)
399 {
400         if (list_empty(&imp->imp_pinger_chain))
401                 return -ENOENT;
402
403         mutex_lock(&pinger_mutex);
404         list_del_init(&imp->imp_pinger_chain);
405         CDEBUG(D_HA, "removing pingable import %s->%s\n",
406                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
407         /* if we remove from pinger we don't want recovery on this import */
408         imp->imp_obd->obd_no_recov = 1;
409         class_import_put(imp);
410         mutex_unlock(&pinger_mutex);
411         return 0;
412 }
413 EXPORT_SYMBOL(ptlrpc_pinger_del_import);
414
415 /**
416  * Register a timeout callback to the pinger list, and the callback will
417  * be called when timeout happens.
418  */
419 static struct timeout_item *ptlrpc_new_timeout(int time,
420         enum timeout_event event, timeout_cb_t cb, void *data)
421 {
422         struct timeout_item *ti;
423
424         ti = kzalloc(sizeof(*ti), GFP_NOFS);
425         if (!ti)
426                 return NULL;
427
428         INIT_LIST_HEAD(&ti->ti_obd_list);
429         INIT_LIST_HEAD(&ti->ti_chain);
430         ti->ti_timeout = time;
431         ti->ti_event = event;
432         ti->ti_cb = cb;
433         ti->ti_cb_data = data;
434
435         return ti;
436 }
437
438 /**
439  * Register timeout event on the pinger thread.
440  * Note: the timeout list is an sorted list with increased timeout value.
441  */
442 static struct timeout_item*
443 ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
444                                timeout_cb_t cb, void *data)
445 {
446         struct timeout_item *item, *tmp;
447
448         LASSERT(mutex_is_locked(&pinger_mutex));
449
450         list_for_each_entry(item, &timeout_list, ti_chain)
451                 if (item->ti_event == event)
452                         goto out;
453
454         item = ptlrpc_new_timeout(time, event, cb, data);
455         if (item) {
456                 list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
457                         if (tmp->ti_timeout < time) {
458                                 list_add(&item->ti_chain, &tmp->ti_chain);
459                                 goto out;
460                         }
461                 }
462                 list_add(&item->ti_chain, &timeout_list);
463         }
464 out:
465         return item;
466 }
467
468 /* Add a client_obd to the timeout event list, when timeout(@time)
469  * happens, the callback(@cb) will be called.
470  */
471 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
472                               timeout_cb_t cb, void *data,
473                               struct list_head *obd_list)
474 {
475         struct timeout_item *ti;
476
477         mutex_lock(&pinger_mutex);
478         ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
479         if (!ti) {
480                 mutex_unlock(&pinger_mutex);
481                 return -EINVAL;
482         }
483         list_add(obd_list, &ti->ti_obd_list);
484         mutex_unlock(&pinger_mutex);
485         return 0;
486 }
487 EXPORT_SYMBOL(ptlrpc_add_timeout_client);
488
489 int ptlrpc_del_timeout_client(struct list_head *obd_list,
490                               enum timeout_event event)
491 {
492         struct timeout_item *ti = NULL, *item;
493
494         if (list_empty(obd_list))
495                 return 0;
496         mutex_lock(&pinger_mutex);
497         list_del_init(obd_list);
498         /**
499          * If there are no obd attached to the timeout event
500          * list, remove this timeout event from the pinger
501          */
502         list_for_each_entry(item, &timeout_list, ti_chain) {
503                 if (item->ti_event == event) {
504                         ti = item;
505                         break;
506                 }
507         }
508         LASSERTF(ti != NULL, "ti is NULL !\n");
509         if (list_empty(&ti->ti_obd_list)) {
510                 list_del(&ti->ti_chain);
511                 kfree(ti);
512         }
513         mutex_unlock(&pinger_mutex);
514         return 0;
515 }
516 EXPORT_SYMBOL(ptlrpc_del_timeout_client);
517
518 int ptlrpc_pinger_remove_timeouts(void)
519 {
520         struct timeout_item *item, *tmp;
521
522         mutex_lock(&pinger_mutex);
523         list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
524                 LASSERT(list_empty(&item->ti_obd_list));
525                 list_del(&item->ti_chain);
526                 kfree(item);
527         }
528         mutex_unlock(&pinger_mutex);
529         return 0;
530 }
531
532 void ptlrpc_pinger_wake_up(void)
533 {
534         thread_add_flags(&pinger_thread, SVC_EVENT);
535         wake_up(&pinger_thread.t_ctl_waitq);
536 }