Whamcloud - gitweb
f6f536c94ea99aaab1872cd57aa6dcce62ea6198
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/ptlrpc/pinger.c
33  *
34  * Portal-RPC reconnection and replay operations, for use in recovery.
35  */
36
37 #define DEBUG_SUBSYSTEM S_RPC
38
39 #include <linux/kthread.h>
40 #include <linux/workqueue.h>
41 #include <obd_support.h>
42 #include <obd_class.h>
43 #include "ptlrpc_internal.h"
44
45 static int suppress_pings;
46 module_param(suppress_pings, int, 0644);
47 MODULE_PARM_DESC(suppress_pings, "Suppress pings");
48
49 struct mutex pinger_mutex;
50 static struct list_head pinger_imports =
51                 LIST_HEAD_INIT(pinger_imports);
52
53 int ptlrpc_pinger_suppress_pings(void)
54 {
55         return suppress_pings;
56 }
57 EXPORT_SYMBOL(ptlrpc_pinger_suppress_pings);
58
59 struct ptlrpc_request *
60 ptlrpc_prep_ping(struct obd_import *imp)
61 {
62         struct ptlrpc_request *req;
63
64         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
65                                         LUSTRE_OBD_VERSION, OBD_PING);
66         if (req) {
67                 ptlrpc_request_set_replen(req);
68                 req->rq_no_resend = req->rq_no_delay = 1;
69         }
70         return req;
71 }
72
73 int ptlrpc_obd_ping(struct obd_device *obd)
74 {
75         int rc;
76         struct ptlrpc_request *req;
77
78         ENTRY;
79
80         req = ptlrpc_prep_ping(obd->u.cli.cl_import);
81         if (!req)
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 bool ptlrpc_check_import_is_idle(struct obd_import *imp)
95 {
96         struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
97         time64_t now;
98
99         if (!imp->imp_idle_timeout)
100                 return false;
101
102         if (atomic_read(&imp->imp_reqs) > 0)
103                 return false;
104
105         /* any lock increases ns_bref being a resource holder */
106         if (ns && atomic_read(&ns->ns_bref) > 0)
107                 return false;
108
109         now = ktime_get_real_seconds();
110         if (now - imp->imp_last_reply_time < imp->imp_idle_timeout)
111                 return false;
112
113         return true;
114 }
115
116 static void ptlrpc_update_next_ping(struct obd_import *imp, int soon)
117 {
118 #ifdef CONFIG_LUSTRE_FS_PINGER
119         time64_t time = soon ? PING_INTERVAL_SHORT : PING_INTERVAL;
120
121         if (imp->imp_state == LUSTRE_IMP_DISCON) {
122                 time64_t dtime = max_t(time64_t, CONNECTION_SWITCH_MIN,
123                                        AT_OFF ? 0 :
124                                        at_get(&imp->imp_at.iat_net_latency));
125                 time = min(time, dtime);
126         }
127         imp->imp_next_ping = ktime_get_seconds() + time;
128 #endif /* CONFIG_LUSTRE_FS_PINGER */
129 }
130
131 static int ptlrpc_ping(struct obd_import *imp)
132 {
133         struct ptlrpc_request *req;
134
135         ENTRY;
136
137         if (ptlrpc_check_import_is_idle(imp))
138                 RETURN(ptlrpc_disconnect_and_idle_import(imp));
139
140         req = ptlrpc_prep_ping(imp);
141         if (!req) {
142                 CERROR("OOM trying to ping %s->%s\n",
143                        imp->imp_obd->obd_uuid.uuid,
144                        obd2cli_tgt(imp->imp_obd));
145                 RETURN(-ENOMEM);
146         }
147
148         DEBUG_REQ(D_INFO, req, "pinging %s->%s",
149                   imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
150         /* Updating imp_next_ping early, it allows pinger_check_timeout to
151          * see an actual time for next awake. request_out_callback update
152          * happens at another thread, and ptlrpc_pinger_main may sleep
153          * already.
154          */
155         ptlrpc_update_next_ping(imp, 0);
156         ptlrpcd_add_req(req);
157
158         RETURN(0);
159 }
160
161 void ptlrpc_ping_import_soon(struct obd_import *imp)
162 {
163         imp->imp_next_ping = ktime_get_seconds();
164 }
165
166 static inline int imp_is_deactive(struct obd_import *imp)
167 {
168         return imp->imp_deactive ||
169                OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_IMP_DEACTIVE);
170 }
171
172 static inline time64_t ptlrpc_next_reconnect(struct obd_import *imp)
173 {
174         return ktime_get_seconds() + INITIAL_CONNECT_TIMEOUT;
175 }
176
177 static timeout_t pinger_check_timeout(time64_t time)
178 {
179         timeout_t timeout = PING_INTERVAL;
180         timeout_t next_timeout;
181         time64_t now;
182         struct list_head *iter;
183         struct obd_import *imp;
184
185         mutex_lock(&pinger_mutex);
186         now = ktime_get_seconds();
187         /* Process imports to find a nearest next ping */
188         list_for_each(iter, &pinger_imports) {
189                 imp = list_entry(iter, struct obd_import, imp_pinger_chain);
190                 if (!imp->imp_pingable || imp->imp_next_ping < now)
191                         continue;
192                 next_timeout = imp->imp_next_ping - now;
193                 /* make sure imp_next_ping in the future from time */
194                 if (next_timeout > (now - time) && timeout > next_timeout)
195                         timeout = next_timeout;
196         }
197         mutex_unlock(&pinger_mutex);
198
199         return timeout - (now - time);
200 }
201
202 static bool ir_up;
203
204 void ptlrpc_pinger_ir_up(void)
205 {
206         CDEBUG(D_HA, "IR up\n");
207         ir_up = true;
208 }
209 EXPORT_SYMBOL(ptlrpc_pinger_ir_up);
210
211 void ptlrpc_pinger_ir_down(void)
212 {
213         CDEBUG(D_HA, "IR down\n");
214         ir_up = false;
215 }
216 EXPORT_SYMBOL(ptlrpc_pinger_ir_down);
217
218 static void ptlrpc_pinger_process_import(struct obd_import *imp,
219                                          time64_t this_ping)
220 {
221         int level;
222         int force;
223         int force_next;
224         int suppress;
225
226         spin_lock(&imp->imp_lock);
227
228         level = imp->imp_state;
229         force = imp->imp_force_verify;
230         force_next = imp->imp_force_next_verify;
231         /*
232          * This will be used below only if the import is "FULL".
233          */
234         suppress = ir_up && OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS);
235
236         imp->imp_force_verify = 0;
237
238         if (imp->imp_next_ping - 5 >= this_ping && !force) {
239                 spin_unlock(&imp->imp_lock);
240                 return;
241         }
242
243         imp->imp_force_next_verify = 0;
244
245         CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA,
246                "%s->%s: level %s/%u force %u force_next %u deactive %u pingable %u suppress %u\n",
247                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
248                ptlrpc_import_state_name(level), level, force, force_next,
249                imp->imp_deactive, imp->imp_pingable, suppress);
250
251         if (level == LUSTRE_IMP_DISCON && !imp_is_deactive(imp)) {
252                 /* wait for a while before trying recovery again */
253                 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
254                 spin_unlock(&imp->imp_lock);
255                 if (!imp->imp_no_pinger_recover ||
256                     imp->imp_connect_error == -EAGAIN)
257                         ptlrpc_initiate_recovery(imp);
258         } else if (level != LUSTRE_IMP_FULL || imp->imp_obd->obd_no_recov ||
259                    imp_is_deactive(imp)) {
260                 CDEBUG(D_HA,
261                        "%s->%s: not pinging (in recovery or recovery disabled: %s)\n",
262                        imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
263                        ptlrpc_import_state_name(level));
264                 if (force)
265                         imp->imp_force_verify = 1;
266                 spin_unlock(&imp->imp_lock);
267         } else if ((imp->imp_pingable && !suppress) || force_next || force) {
268                 spin_unlock(&imp->imp_lock);
269                 ptlrpc_ping(imp);
270         } else {
271                 spin_unlock(&imp->imp_lock);
272         }
273 }
274
275 static struct workqueue_struct *pinger_wq;
276 static void ptlrpc_pinger_main(struct work_struct *ws);
277 static DECLARE_DELAYED_WORK(ping_work, ptlrpc_pinger_main);
278
279 static void ptlrpc_pinger_main(struct work_struct *ws)
280 {
281         time64_t this_ping, time_after_ping;
282         timeout_t time_to_next_wake;
283         struct obd_import *imp;
284         struct list_head *iter;
285
286         do {
287                 this_ping = ktime_get_seconds();
288
289                 mutex_lock(&pinger_mutex);
290
291                 list_for_each(iter, &pinger_imports) {
292                         imp = list_entry(iter, struct obd_import,
293                                          imp_pinger_chain);
294
295                         ptlrpc_pinger_process_import(imp, this_ping);
296                         /* obd_timeout might have changed */
297                         if (imp->imp_pingable && imp->imp_next_ping &&
298                             imp->imp_next_ping > this_ping + PING_INTERVAL)
299                                 ptlrpc_update_next_ping(imp, 0);
300                 }
301                 mutex_unlock(&pinger_mutex);
302
303                 time_after_ping = ktime_get_seconds();
304                 /* update memory usage info */
305                 obd_update_maxusage();
306
307                 if ((ktime_get_seconds() - this_ping - 3) > PING_INTERVAL)
308                         CDEBUG(D_HA, "long time to ping: %lld, %lld, %lld\n",
309                                this_ping, time_after_ping, ktime_get_seconds());
310
311                 /* Wait until the next ping time, or until we're stopped. */
312                 time_to_next_wake = pinger_check_timeout(this_ping);
313                 /*
314                  * The ping sent by ptlrpc_send_rpc may get sent out
315                  * say .01 second after this.
316                  * ptlrpc_pinger_sending_on_import will then set the
317                  * next ping time to next_ping + .01 sec, which means
318                  * we will SKIP the next ping at next_ping, and the
319                  * ping will get sent 2 timeouts from now!  Beware.
320                  */
321                 CDEBUG(D_INFO, "next wakeup in %d (%lld)\n",
322                        time_to_next_wake, this_ping + PING_INTERVAL);
323         } while (time_to_next_wake <= 0);
324
325         queue_delayed_work(pinger_wq, &ping_work,
326                            cfs_time_seconds(max(time_to_next_wake, 1)));
327 }
328
329 int ptlrpc_start_pinger(void)
330 {
331 #ifdef CONFIG_LUSTRE_FS_PINGER
332         if (pinger_wq)
333                 return -EALREADY;
334
335         pinger_wq = cfs_cpt_bind_workqueue("ptlrpc_pinger", cfs_cpt_tab,
336                                            0, CFS_CPT_ANY, 1);
337         if (IS_ERR(pinger_wq)) {
338                 CERROR("cannot start pinger workqueue\n");
339                 return PTR_ERR(pinger_wq);
340         }
341
342         queue_delayed_work(pinger_wq, &ping_work, 0);
343
344         if (suppress_pings)
345                 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");
346 #endif
347         return 0;
348 }
349
350 int ptlrpc_stop_pinger(void)
351 {
352 #ifdef CONFIG_LUSTRE_FS_PINGER
353         if (!pinger_wq)
354                 return -EALREADY;
355
356         cancel_delayed_work_sync(&ping_work);
357         destroy_workqueue(pinger_wq);
358         pinger_wq = NULL;
359 #endif
360         return 0;
361 }
362
363 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
364 {
365         ptlrpc_update_next_ping(imp, 0);
366 }
367
368 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
369 {
370         ptlrpc_update_next_ping(imp, 1);
371         assert_spin_locked(&imp->imp_lock);
372         /*
373          * Avoid reading stale imp_connect_data.  When not sure if pings are
374          * expected or not on next connection, we assume they are not and force
375          * one anyway to guarantee the chance of updating
376          * imp_peer_committed_transno.
377          */
378         if (imp->imp_state != LUSTRE_IMP_FULL ||
379             OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS))
380                 imp->imp_force_next_verify = 1;
381 }
382
383 int ptlrpc_pinger_add_import(struct obd_import *imp)
384 {
385         ENTRY;
386         if (!list_empty(&imp->imp_pinger_chain))
387                 RETURN(-EALREADY);
388
389         mutex_lock(&pinger_mutex);
390         CDEBUG(D_HA, "adding pingable import %s->%s\n",
391                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
392         /* if we add to pinger we want recovery on this import */
393         imp->imp_obd->obd_no_recov = 0;
394         ptlrpc_update_next_ping(imp, 0);
395         /* XXX sort, blah blah */
396         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
397         class_import_get(imp);
398
399         ptlrpc_pinger_wake_up();
400         mutex_unlock(&pinger_mutex);
401
402         RETURN(0);
403 }
404 EXPORT_SYMBOL(ptlrpc_pinger_add_import);
405
406 int ptlrpc_pinger_del_import(struct obd_import *imp)
407 {
408         ENTRY;
409
410         if (list_empty(&imp->imp_pinger_chain))
411                 RETURN(-ENOENT);
412
413         mutex_lock(&pinger_mutex);
414         list_del_init(&imp->imp_pinger_chain);
415         CDEBUG(D_HA, "removing pingable import %s->%s\n",
416                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
417         /* if we remove from pinger we don't want recovery on this import */
418         imp->imp_obd->obd_no_recov = 1;
419         class_import_put(imp);
420         mutex_unlock(&pinger_mutex);
421         RETURN(0);
422 }
423 EXPORT_SYMBOL(ptlrpc_pinger_del_import);
424
425 void ptlrpc_pinger_wake_up(void)
426 {
427 #ifdef CONFIG_LUSTRE_FS_PINGER
428         mod_delayed_work(pinger_wq, &ping_work, 0);
429 #endif
430 }
431
432 /* Ping evictor thread */
433 #define PET_READY     1
434 #define PET_TERMINATE 2
435
436 static int pet_refcount;
437 static int pet_state;
438 static wait_queue_head_t pet_waitq;
439 static LIST_HEAD(pet_list);
440 static DEFINE_SPINLOCK(pet_lock);
441
442 int ping_evictor_wake(struct obd_export *exp)
443 {
444         struct obd_device *obd;
445
446         spin_lock(&pet_lock);
447         if (pet_state != PET_READY) {
448                 /* eventually the new obd will call here again. */
449                 spin_unlock(&pet_lock);
450                 return 1;
451         }
452
453         obd = class_exp2obd(exp);
454         if (list_empty(&obd->obd_evict_list)) {
455                 class_incref(obd, "evictor", obd);
456                 list_add(&obd->obd_evict_list, &pet_list);
457         }
458         spin_unlock(&pet_lock);
459
460         wake_up(&pet_waitq);
461         return 0;
462 }
463
464 static int ping_evictor_main(void *arg)
465 {
466         struct obd_device *obd;
467         struct obd_export *exp;
468         time64_t expire_time;
469
470         ENTRY;
471         CDEBUG(D_HA, "Starting Ping Evictor\n");
472         pet_state = PET_READY;
473         while (1) {
474                 wait_event_idle(pet_waitq,
475                                 (!list_empty(&pet_list)) ||
476                                 (pet_state == PET_TERMINATE));
477
478                 /* loop until all obd's will be removed */
479                 if ((pet_state == PET_TERMINATE) && list_empty(&pet_list))
480                         break;
481
482                 /*
483                  * we only get here if pet_exp != NULL, and the end of this
484                  * loop is the only place which sets it NULL again, so lock
485                  * is not strictly necessary.
486                  */
487                 spin_lock(&pet_lock);
488                 obd = list_entry(pet_list.next, struct obd_device,
489                                  obd_evict_list);
490                 spin_unlock(&pet_lock);
491
492                 expire_time = ktime_get_real_seconds() - PING_EVICT_TIMEOUT;
493
494                 CDEBUG(D_HA, "evicting all exports of obd %s older than %lld\n",
495                        obd->obd_name, expire_time);
496
497                 /*
498                  * Exports can't be deleted out of the list while we hold
499                  * the obd lock (class_unlink_export), which means we can't
500                  * lose the last ref on the export.  If they've already been
501                  * removed from the list, we won't find them here.
502                  */
503                 spin_lock(&obd->obd_dev_lock);
504                 while (!list_empty(&obd->obd_exports_timed)) {
505                         exp = list_entry(obd->obd_exports_timed.next,
506                                          struct obd_export,
507                                          exp_obd_chain_timed);
508                         if (expire_time > exp->exp_last_request_time) {
509                                 struct obd_uuid *client_uuid;
510
511                                 class_export_get(exp);
512                                 client_uuid = &exp->exp_client_uuid;
513                                 spin_unlock(&obd->obd_dev_lock);
514                                 LCONSOLE_WARN("%s: haven't heard from client %s (at %s) in %lld seconds. I think it's dead, and I am evicting it. exp %p, cur %lld expire %lld last %lld\n",
515                                               obd->obd_name,
516                                               obd_uuid2str(client_uuid),
517                                               obd_export_nid2str(exp),
518                                               ktime_get_real_seconds() -
519                                               exp->exp_last_request_time,
520                                               exp, ktime_get_real_seconds(),
521                                               expire_time,
522                                               exp->exp_last_request_time);
523                                 CDEBUG(D_HA, "Last request was at %lld\n",
524                                        exp->exp_last_request_time);
525                                 class_fail_export(exp);
526                                 class_export_put(exp);
527                                 spin_lock(&obd->obd_dev_lock);
528                         } else {
529                                 /* List is sorted, so everyone below is ok */
530                                 break;
531                         }
532                 }
533                 spin_unlock(&obd->obd_dev_lock);
534
535                 spin_lock(&pet_lock);
536                 list_del_init(&obd->obd_evict_list);
537                 spin_unlock(&pet_lock);
538
539                 class_decref(obd, "evictor", obd);
540         }
541         CDEBUG(D_HA, "Exiting Ping Evictor\n");
542
543         RETURN(0);
544 }
545
546 void ping_evictor_start(void)
547 {
548         struct task_struct *task;
549
550         if (++pet_refcount > 1)
551                 return;
552
553         init_waitqueue_head(&pet_waitq);
554
555         task = kthread_run(ping_evictor_main, NULL, "ll_evictor");
556         if (IS_ERR(task)) {
557                 pet_refcount--;
558                 CERROR("Cannot start ping evictor thread: %ld\n",
559                         PTR_ERR(task));
560         }
561 }
562 EXPORT_SYMBOL(ping_evictor_start);
563
564 void ping_evictor_stop(void)
565 {
566         if (--pet_refcount > 0)
567                 return;
568
569         pet_state = PET_TERMINATE;
570         wake_up(&pet_waitq);
571 }
572 EXPORT_SYMBOL(ping_evictor_stop);