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