Whamcloud - gitweb
LU-17662 osd-zfs: Support for ZFS 2.2.3
[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_put(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         enum lustre_imp_state 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 || level == LUSTRE_IMP_IDLE) ?
249                 D_INFO : D_HA,
250                "%s->%s: level %s/%u force %u force_next %u deactive %u pingable %u suppress %u\n",
251                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
252                ptlrpc_import_state_name(level), level, force, force_next,
253                imp->imp_deactive, imp->imp_pingable, suppress);
254
255         if (level == LUSTRE_IMP_DISCON && !imp_is_deactive(imp)) {
256                 /* wait for a while before trying recovery again */
257                 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
258                 if (!imp->imp_no_pinger_recover ||
259                     imp->imp_connect_error == -EAGAIN) {
260                         CDEBUG(D_HA, "%s: starting recovery\n",
261                                obd2cli_tgt(imp->imp_obd));
262                         ptlrpc_connect_import_locked(imp);
263                 } else {
264                         spin_unlock(&imp->imp_lock);
265                 }
266         } else if (level != LUSTRE_IMP_FULL || imp->imp_obd->obd_no_recov ||
267                    imp_is_deactive(imp)) {
268                 CDEBUG(level == LUSTRE_IMP_IDLE ? D_INFO : D_HA,
269                        "%s->%s: not pinging (in recovery or recovery disabled: %s)\n",
270                        imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
271                        ptlrpc_import_state_name(level));
272                 if (force)
273                         imp->imp_force_verify = 1;
274                 spin_unlock(&imp->imp_lock);
275         } else if ((imp->imp_pingable && !suppress) || force_next || force) {
276                 spin_unlock(&imp->imp_lock);
277                 ptlrpc_ping(imp);
278         } else {
279                 spin_unlock(&imp->imp_lock);
280         }
281 }
282
283 static struct workqueue_struct *pinger_wq;
284 static void ptlrpc_pinger_main(struct work_struct *ws);
285 static DECLARE_DELAYED_WORK(ping_work, ptlrpc_pinger_main);
286
287 static void ptlrpc_pinger_main(struct work_struct *ws)
288 {
289         time64_t this_ping, time_after_ping;
290         timeout_t time_to_next_wake;
291         struct obd_import *imp;
292
293         do {
294                 this_ping = ktime_get_seconds();
295
296                 mutex_lock(&pinger_mutex);
297
298                 list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
299                         ptlrpc_pinger_process_import(imp, this_ping);
300                         /* obd_timeout might have changed */
301                         if (imp->imp_pingable && imp->imp_next_ping &&
302                             imp->imp_next_ping > this_ping + PING_INTERVAL)
303                                 ptlrpc_update_next_ping(imp, 0);
304                 }
305                 mutex_unlock(&pinger_mutex);
306
307                 time_after_ping = ktime_get_seconds();
308                 /* update memory usage info */
309                 obd_update_maxusage();
310
311                 if ((ktime_get_seconds() - this_ping - 3) > PING_INTERVAL)
312                         CDEBUG(D_HA, "long time to ping: %lld, %lld, %lld\n",
313                                this_ping, time_after_ping, ktime_get_seconds());
314
315                 /* Wait until the next ping time, or until we're stopped. */
316                 time_to_next_wake = pinger_check_timeout(this_ping);
317                 /*
318                  * The ping sent by ptlrpc_send_rpc may get sent out
319                  * say .01 second after this.
320                  * ptlrpc_pinger_sending_on_import will then set the
321                  * next ping time to next_ping + .01 sec, which means
322                  * we will SKIP the next ping at next_ping, and the
323                  * ping will get sent 2 timeouts from now!  Beware.
324                  */
325                 CDEBUG(D_INFO, "next wakeup in %d (%lld)\n",
326                        time_to_next_wake, this_ping + PING_INTERVAL);
327         } while (time_to_next_wake <= 0);
328
329         queue_delayed_work(pinger_wq, &ping_work,
330                            cfs_time_seconds(max(time_to_next_wake, 1)));
331 }
332
333 int ptlrpc_start_pinger(void)
334 {
335 #ifdef CONFIG_LUSTRE_FS_PINGER
336         if (pinger_wq)
337                 return -EALREADY;
338
339         pinger_wq = cfs_cpt_bind_workqueue("ptlrpc_pinger", cfs_cpt_tab,
340                                            0, CFS_CPT_ANY, 1);
341         if (IS_ERR(pinger_wq)) {
342                 CERROR("cannot start pinger workqueue\n");
343                 return PTR_ERR(pinger_wq);
344         }
345
346         queue_delayed_work(pinger_wq, &ping_work, 0);
347
348         if (suppress_pings)
349                 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");
350 #endif
351         return 0;
352 }
353
354 int ptlrpc_stop_pinger(void)
355 {
356 #ifdef CONFIG_LUSTRE_FS_PINGER
357         if (!pinger_wq)
358                 return -EALREADY;
359
360         cancel_delayed_work_sync(&ping_work);
361         destroy_workqueue(pinger_wq);
362         pinger_wq = NULL;
363 #endif
364         return 0;
365 }
366
367 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
368 {
369         ptlrpc_update_next_ping(imp, 0);
370 }
371
372 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
373 {
374         ptlrpc_update_next_ping(imp, 1);
375         assert_spin_locked(&imp->imp_lock);
376         /*
377          * Avoid reading stale imp_connect_data.  When not sure if pings are
378          * expected or not on next connection, we assume they are not and force
379          * one anyway to guarantee the chance of updating
380          * imp_peer_committed_transno.
381          */
382         if (imp->imp_state != LUSTRE_IMP_FULL ||
383             OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS))
384                 imp->imp_force_next_verify = 1;
385 }
386
387 int ptlrpc_pinger_add_import(struct obd_import *imp)
388 {
389         ENTRY;
390         if (!list_empty(&imp->imp_pinger_chain))
391                 RETURN(-EALREADY);
392
393         mutex_lock(&pinger_mutex);
394         CDEBUG(D_HA, "adding pingable import %s->%s\n",
395                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
396         /* if we add to pinger we want recovery on this import */
397         imp->imp_obd->obd_no_recov = 0;
398         ptlrpc_update_next_ping(imp, 0);
399         /* XXX sort, blah blah */
400         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
401         class_import_get(imp);
402
403         ptlrpc_pinger_wake_up();
404         mutex_unlock(&pinger_mutex);
405
406         RETURN(0);
407 }
408 EXPORT_SYMBOL(ptlrpc_pinger_add_import);
409
410 int ptlrpc_pinger_del_import(struct obd_import *imp)
411 {
412         ENTRY;
413
414         if (list_empty(&imp->imp_pinger_chain))
415                 RETURN(-ENOENT);
416
417         mutex_lock(&pinger_mutex);
418         list_del_init(&imp->imp_pinger_chain);
419         CDEBUG(D_HA, "removing pingable import %s->%s\n",
420                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
421         /* if we remove from pinger we don't want recovery on this import */
422         imp->imp_obd->obd_no_recov = 1;
423         class_import_put(imp);
424         mutex_unlock(&pinger_mutex);
425         RETURN(0);
426 }
427 EXPORT_SYMBOL(ptlrpc_pinger_del_import);
428
429 void ptlrpc_pinger_wake_up(void)
430 {
431 #ifdef CONFIG_LUSTRE_FS_PINGER
432         mod_delayed_work(pinger_wq, &ping_work, 0);
433 #endif
434 }
435
436 /* Ping evictor thread */
437 #define PET_READY     1
438 #define PET_TERMINATE 2
439
440 static int pet_refcount;
441 static int pet_state;
442 static wait_queue_head_t pet_waitq;
443 static LIST_HEAD(pet_list);
444 static DEFINE_SPINLOCK(pet_lock);
445
446 int ping_evictor_wake(struct obd_export *exp)
447 {
448         struct obd_device *obd;
449
450         spin_lock(&pet_lock);
451         if (pet_state != PET_READY) {
452                 /* eventually the new obd will call here again. */
453                 spin_unlock(&pet_lock);
454                 return 1;
455         }
456
457         obd = class_exp2obd(exp);
458         if (list_empty(&obd->obd_evict_list)) {
459                 class_incref(obd, "evictor", obd);
460                 list_add(&obd->obd_evict_list, &pet_list);
461         }
462         spin_unlock(&pet_lock);
463
464         wake_up(&pet_waitq);
465         return 0;
466 }
467
468 static int ping_evictor_main(void *arg)
469 {
470         struct obd_device *obd;
471         struct obd_export *exp;
472         time64_t expire_time;
473
474         ENTRY;
475         unshare_fs_struct();
476         CDEBUG(D_HA, "Starting Ping Evictor\n");
477         pet_state = PET_READY;
478         while (1) {
479                 wait_event_idle(pet_waitq,
480                                 (!list_empty(&pet_list)) ||
481                                 (pet_state == PET_TERMINATE));
482
483                 /* loop until all obd's will be removed */
484                 if ((pet_state == PET_TERMINATE) && list_empty(&pet_list))
485                         break;
486
487                 /*
488                  * we only get here if pet_exp != NULL, and the end of this
489                  * loop is the only place which sets it NULL again, so lock
490                  * is not strictly necessary.
491                  */
492                 spin_lock(&pet_lock);
493                 obd = list_first_entry(&pet_list, struct obd_device,
494                                        obd_evict_list);
495                 spin_unlock(&pet_lock);
496
497                 expire_time = ktime_get_real_seconds() - PING_EVICT_TIMEOUT;
498
499                 CDEBUG(D_HA, "evicting all exports of obd %s older than %lld\n",
500                        obd->obd_name, expire_time);
501
502                 /*
503                  * Exports can't be deleted out of the list while we hold
504                  * the obd lock (class_unlink_export), which means we can't
505                  * lose the last ref on the export.  If they've already been
506                  * removed from the list, we won't find them here.
507                  */
508                 spin_lock(&obd->obd_dev_lock);
509                 while (!list_empty(&obd->obd_exports_timed)) {
510                         exp = list_first_entry(&obd->obd_exports_timed,
511                                                struct obd_export,
512                                                exp_obd_chain_timed);
513                         if (expire_time > exp->exp_last_request_time) {
514                                 struct obd_uuid *client_uuid;
515
516                                 class_export_get(exp);
517                                 client_uuid = &exp->exp_client_uuid;
518                                 spin_unlock(&obd->obd_dev_lock);
519                                 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",
520                                               obd->obd_name,
521                                               obd_uuid2str(client_uuid),
522                                               obd_export_nid2str(exp),
523                                               ktime_get_real_seconds() -
524                                               exp->exp_last_request_time,
525                                               exp, ktime_get_real_seconds(),
526                                               expire_time,
527                                               exp->exp_last_request_time);
528                                 CDEBUG(D_HA, "Last request was at %lld\n",
529                                        exp->exp_last_request_time);
530                                 class_fail_export(exp);
531                                 class_export_put(exp);
532                                 spin_lock(&obd->obd_dev_lock);
533                         } else {
534                                 /* List is sorted, so everyone below is ok */
535                                 break;
536                         }
537                 }
538                 spin_unlock(&obd->obd_dev_lock);
539
540                 spin_lock(&pet_lock);
541                 list_del_init(&obd->obd_evict_list);
542                 spin_unlock(&pet_lock);
543
544                 class_decref(obd, "evictor", obd);
545         }
546         CDEBUG(D_HA, "Exiting Ping Evictor\n");
547
548         RETURN(0);
549 }
550
551 void ping_evictor_start(void)
552 {
553         struct task_struct *task;
554
555         if (++pet_refcount > 1)
556                 return;
557
558         init_waitqueue_head(&pet_waitq);
559
560         task = kthread_run(ping_evictor_main, NULL, "ll_evictor");
561         if (IS_ERR(task)) {
562                 pet_refcount--;
563                 CERROR("Cannot start ping evictor thread: %ld\n",
564                         PTR_ERR(task));
565         }
566 }
567 EXPORT_SYMBOL(ping_evictor_start);
568
569 void ping_evictor_stop(void)
570 {
571         if (--pet_refcount > 0)
572                 return;
573
574         pet_state = PET_TERMINATE;
575         wake_up(&pet_waitq);
576 }
577 EXPORT_SYMBOL(ping_evictor_stop);