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