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