Whamcloud - gitweb
19b7d0162cf57dc61e991d36ce0cb5c149d80e50
[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 static struct list_head timeout_list =
53                 LIST_HEAD_INIT(timeout_list);
54
55 int ptlrpc_pinger_suppress_pings()
56 {
57         return suppress_pings;
58 }
59 EXPORT_SYMBOL(ptlrpc_pinger_suppress_pings);
60
61 struct ptlrpc_request *
62 ptlrpc_prep_ping(struct obd_import *imp)
63 {
64         struct ptlrpc_request *req;
65
66         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
67                                         LUSTRE_OBD_VERSION, OBD_PING);
68         if (req) {
69                 ptlrpc_request_set_replen(req);
70                 req->rq_no_resend = req->rq_no_delay = 1;
71         }
72         return req;
73 }
74
75 int ptlrpc_obd_ping(struct obd_device *obd)
76 {
77         int rc;
78         struct ptlrpc_request *req;
79         ENTRY;
80
81         req = ptlrpc_prep_ping(obd->u.cli.cl_import);
82         if (req == NULL)
83                 RETURN(-ENOMEM);
84
85         req->rq_send_state = LUSTRE_IMP_FULL;
86
87         rc = ptlrpc_queue_wait(req);
88
89         ptlrpc_req_finished(req);
90
91         RETURN(rc);
92 }
93 EXPORT_SYMBOL(ptlrpc_obd_ping);
94
95 static int ptlrpc_ping(struct obd_import *imp)
96 {
97         struct ptlrpc_request   *req;
98         ENTRY;
99
100         req = ptlrpc_prep_ping(imp);
101         if (req == NULL) {
102                 CERROR("OOM trying to ping %s->%s\n",
103                        imp->imp_obd->obd_uuid.uuid,
104                        obd2cli_tgt(imp->imp_obd));
105                 RETURN(-ENOMEM);
106         }
107
108         DEBUG_REQ(D_INFO, req, "pinging %s->%s",
109                   imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
110         ptlrpcd_add_req(req);
111
112         RETURN(0);
113 }
114
115 static void ptlrpc_update_next_ping(struct obd_import *imp, int soon)
116 {
117 #ifdef ENABLE_PINGER
118         time64_t time = soon ? PING_INTERVAL_SHORT : PING_INTERVAL;
119
120         if (imp->imp_state == LUSTRE_IMP_DISCON) {
121                 time64_t dtime = max_t(time64_t, CONNECTION_SWITCH_MIN,
122                                        AT_OFF ? 0 :
123                                        at_get(&imp->imp_at.iat_net_latency));
124                 time = min(time, dtime);
125         }
126         imp->imp_next_ping = ktime_get_seconds() + time;
127 #endif /* ENABLE_PINGER */
128 }
129
130 void ptlrpc_ping_import_soon(struct obd_import *imp)
131 {
132         imp->imp_next_ping = ktime_get_seconds();
133 }
134
135 static inline int imp_is_deactive(struct obd_import *imp)
136 {
137         return (imp->imp_deactive ||
138                 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_IMP_DEACTIVE));
139 }
140
141 static inline time64_t ptlrpc_next_reconnect(struct obd_import *imp)
142 {
143         if (imp->imp_server_timeout)
144                 return ktime_get_seconds() + (obd_timeout >> 1);
145         else
146                 return ktime_get_seconds() + obd_timeout;
147 }
148
149 static time64_t pinger_check_timeout(time64_t time)
150 {
151         struct timeout_item *item;
152         time64_t timeout = PING_INTERVAL;
153
154         /* This list is sorted in increasing timeout order */
155         mutex_lock(&pinger_mutex);
156         list_for_each_entry(item, &timeout_list, ti_chain) {
157                 time64_t ti_timeout = item->ti_timeout;
158
159                 if (timeout > ti_timeout)
160                         timeout = ti_timeout;
161                 break;
162         }
163         mutex_unlock(&pinger_mutex);
164
165         return time + timeout - ktime_get_seconds();
166 }
167
168 static bool ir_up;
169
170 void ptlrpc_pinger_ir_up(void)
171 {
172         CDEBUG(D_HA, "IR up\n");
173         ir_up = true;
174 }
175 EXPORT_SYMBOL(ptlrpc_pinger_ir_up);
176
177 void ptlrpc_pinger_ir_down(void)
178 {
179         CDEBUG(D_HA, "IR down\n");
180         ir_up = false;
181 }
182 EXPORT_SYMBOL(ptlrpc_pinger_ir_down);
183
184 static void ptlrpc_pinger_process_import(struct obd_import *imp,
185                                          time64_t this_ping)
186 {
187         int level;
188         int force;
189         int force_next;
190         int suppress;
191
192         spin_lock(&imp->imp_lock);
193
194         level = imp->imp_state;
195         force = imp->imp_force_verify;
196         force_next = imp->imp_force_next_verify;
197         /*
198          * This will be used below only if the import is "FULL".
199          */
200         suppress = ir_up && OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS);
201
202         imp->imp_force_verify = 0;
203
204         if (imp->imp_next_ping - 5 >= this_ping && !force) {
205                 spin_unlock(&imp->imp_lock);
206                 return;
207         }
208
209         imp->imp_force_next_verify = 0;
210
211         spin_unlock(&imp->imp_lock);
212
213         CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA, "%s->%s: level %s/%u "
214                "force %u force_next %u deactive %u pingable %u suppress %u\n",
215                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
216                ptlrpc_import_state_name(level), level, force, force_next,
217                imp->imp_deactive, imp->imp_pingable, suppress);
218
219         if (level == LUSTRE_IMP_DISCON && !imp_is_deactive(imp)) {
220                 /* wait for a while before trying recovery again */
221                 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
222                 if (!imp->imp_no_pinger_recover)
223                         ptlrpc_initiate_recovery(imp);
224         } else if (level != LUSTRE_IMP_FULL ||
225                    imp->imp_obd->obd_no_recov ||
226                    imp_is_deactive(imp)) {
227                 CDEBUG(D_HA, "%s->%s: not pinging (in recovery "
228                        "or recovery disabled: %s)\n",
229                        imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
230                        ptlrpc_import_state_name(level));
231                 if (force) {
232                         spin_lock(&imp->imp_lock);
233                         imp->imp_force_verify = 1;
234                         spin_unlock(&imp->imp_lock);
235                 }
236         } else if ((imp->imp_pingable && !suppress) || force_next || force) {
237                 ptlrpc_ping(imp);
238         }
239 }
240
241 static struct workqueue_struct *pinger_wq;
242 static void ptlrpc_pinger_main(struct work_struct *ws);
243 static DECLARE_DELAYED_WORK(ping_work, ptlrpc_pinger_main);
244
245 static void ptlrpc_pinger_main(struct work_struct *ws)
246 {
247         time64_t this_ping = ktime_get_seconds();
248         time64_t time_to_next_wake;
249         struct timeout_item *item;
250         struct obd_import *imp;
251         struct list_head *iter;
252
253         do {
254                 mutex_lock(&pinger_mutex);
255                 list_for_each_entry(item, &timeout_list, ti_chain)
256                         item->ti_cb(item, item->ti_cb_data);
257
258                 list_for_each(iter, &pinger_imports) {
259                         imp = list_entry(iter, struct obd_import,
260                                          imp_pinger_chain);
261
262                         ptlrpc_pinger_process_import(imp, this_ping);
263                         /* obd_timeout might have changed */
264                         if (imp->imp_pingable && imp->imp_next_ping &&
265                             imp->imp_next_ping > this_ping + PING_INTERVAL)
266                                 ptlrpc_update_next_ping(imp, 0);
267                 }
268                 mutex_unlock(&pinger_mutex);
269                 /* update memory usage info */
270                 obd_update_maxusage();
271
272                 /* Wait until the next ping time, or until we're stopped. */
273                 time_to_next_wake = pinger_check_timeout(this_ping);
274                 /* The ping sent by ptlrpc_send_rpc may get sent out
275                  * say .01 second after this.
276                  * ptlrpc_pinger_sending_on_import will then set the
277                  * next ping time to next_ping + .01 sec, which means
278                  * we will SKIP the next ping at next_ping, and the
279                  * ping will get sent 2 timeouts from now!  Beware. */
280                 CDEBUG(D_INFO, "next wakeup in %lld (%lld)\n",
281                        time_to_next_wake, this_ping + PING_INTERVAL);
282         } while (time_to_next_wake <= 0);
283
284         queue_delayed_work(pinger_wq, &ping_work,
285                            cfs_time_seconds(max(time_to_next_wake, 1LL)));
286 }
287
288 int ptlrpc_start_pinger(void)
289 {
290 #ifdef ENABLE_PINGER
291         if (pinger_wq)
292                 return -EALREADY;
293
294         pinger_wq = alloc_workqueue("ptlrpc_pinger", 0, 1);
295         if (!pinger_wq) {
296                 CERROR("cannot start pinger workqueue\n");
297                 return -ENOMEM;
298         }
299
300         queue_delayed_work(pinger_wq, &ping_work, 0);
301
302         if (suppress_pings)
303                 CWARN("Pings will be suppressed at the request of the "
304                       "administrator.  The configuration shall meet the "
305                       "additional requirements described in the manual.  "
306                       "(Search for the \"suppress_pings\" kernel module "
307                       "parameter.)\n");
308 #endif
309         return 0;
310 }
311
312 int ptlrpc_pinger_remove_timeouts(void);
313
314 int ptlrpc_stop_pinger(void)
315 {
316 #ifdef ENABLE_PINGER
317         if (!pinger_wq)
318                 return -EALREADY;
319
320         ptlrpc_pinger_remove_timeouts();
321
322         cancel_delayed_work_sync(&ping_work);
323         destroy_workqueue(pinger_wq);
324         pinger_wq = NULL;
325 #endif
326         return 0;
327 }
328
329 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
330 {
331         ptlrpc_update_next_ping(imp, 0);
332 }
333
334 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
335 {
336         ptlrpc_update_next_ping(imp, 1);
337         assert_spin_locked(&imp->imp_lock);
338         /*
339          * Avoid reading stale imp_connect_data.  When not sure if pings are
340          * expected or not on next connection, we assume they are not and force
341          * one anyway to guarantee the chance of updating
342          * imp_peer_committed_transno.
343          */
344         if (imp->imp_state != LUSTRE_IMP_FULL ||
345             OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS))
346                 imp->imp_force_next_verify = 1;
347 }
348
349 int ptlrpc_pinger_add_import(struct obd_import *imp)
350 {
351         ENTRY;
352         if (!list_empty(&imp->imp_pinger_chain))
353                 RETURN(-EALREADY);
354
355         mutex_lock(&pinger_mutex);
356         CDEBUG(D_HA, "adding pingable import %s->%s\n",
357                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
358         /* if we add to pinger we want recovery on this import */
359         imp->imp_obd->obd_no_recov = 0;
360         ptlrpc_update_next_ping(imp, 0);
361         /* XXX sort, blah blah */
362         list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
363         class_import_get(imp);
364
365         ptlrpc_pinger_wake_up();
366         mutex_unlock(&pinger_mutex);
367
368         RETURN(0);
369 }
370 EXPORT_SYMBOL(ptlrpc_pinger_add_import);
371
372 int ptlrpc_pinger_del_import(struct obd_import *imp)
373 {
374         ENTRY;
375
376         if (list_empty(&imp->imp_pinger_chain))
377                 RETURN(-ENOENT);
378
379         mutex_lock(&pinger_mutex);
380         list_del_init(&imp->imp_pinger_chain);
381         CDEBUG(D_HA, "removing pingable import %s->%s\n",
382                imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
383         /* if we remove from pinger we don't want recovery on this import */
384         imp->imp_obd->obd_no_recov = 1;
385         class_import_put(imp);
386         mutex_unlock(&pinger_mutex);
387         RETURN(0);
388 }
389 EXPORT_SYMBOL(ptlrpc_pinger_del_import);
390
391 /**
392  * Register a timeout callback to the pinger list, and the callback will
393  * be called when timeout happens.
394  */
395 static struct timeout_item *ptlrpc_new_timeout(time64_t time,
396                                                enum timeout_event event,
397                                                timeout_cb_t cb, void *data)
398 {
399         struct timeout_item *ti;
400
401         OBD_ALLOC_PTR(ti);
402         if (!ti)
403                 return(NULL);
404
405         INIT_LIST_HEAD(&ti->ti_obd_list);
406         INIT_LIST_HEAD(&ti->ti_chain);
407         ti->ti_timeout = time;
408         ti->ti_event = event;
409         ti->ti_cb = cb;
410         ti->ti_cb_data = data;
411
412         return ti;
413 }
414
415 /**
416  * Register timeout event on the the pinger thread.
417  * Note: the timeout list is an sorted list with increased timeout value.
418  */
419 static struct timeout_item*
420 ptlrpc_pinger_register_timeout(time64_t time, enum timeout_event event,
421                                timeout_cb_t cb, void *data)
422 {
423         struct timeout_item *item, *tmp;
424
425         LASSERT(mutex_is_locked(&pinger_mutex));
426
427         list_for_each_entry(item, &timeout_list, ti_chain)
428                 if (item->ti_event == event)
429                         goto out;
430
431         item = ptlrpc_new_timeout(time, event, cb, data);
432         if (item) {
433                 list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
434                         if (tmp->ti_timeout < time) {
435                                 list_add(&item->ti_chain, &tmp->ti_chain);
436                                 goto out;
437                         }
438                 }
439                 list_add(&item->ti_chain, &timeout_list);
440         }
441 out:
442         return item;
443 }
444
445 /* Add a client_obd to the timeout event list, when timeout(@time)
446  * happens, the callback(@cb) will be called.
447  */
448 int ptlrpc_add_timeout_client(time64_t time, enum timeout_event event,
449                               timeout_cb_t cb, void *data,
450                               struct list_head *obd_list)
451 {
452         struct timeout_item *ti;
453
454         mutex_lock(&pinger_mutex);
455         ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
456         if (!ti) {
457                 mutex_unlock(&pinger_mutex);
458                 return (-EINVAL);
459         }
460         list_add(obd_list, &ti->ti_obd_list);
461         mutex_unlock(&pinger_mutex);
462         return 0;
463 }
464 EXPORT_SYMBOL(ptlrpc_add_timeout_client);
465
466 int ptlrpc_del_timeout_client(struct list_head *obd_list,
467                               enum timeout_event event)
468 {
469         struct timeout_item *ti = NULL, *item;
470
471         if (list_empty(obd_list))
472                 return 0;
473         mutex_lock(&pinger_mutex);
474         list_del_init(obd_list);
475         /**
476          * If there are no obd attached to the timeout event
477          * list, remove this timeout event from the pinger
478          */
479         list_for_each_entry(item, &timeout_list, ti_chain) {
480                 if (item->ti_event == event) {
481                         ti = item;
482                         break;
483                 }
484         }
485         LASSERTF(ti != NULL, "ti is NULL !\n");
486         if (list_empty(&ti->ti_obd_list)) {
487                 list_del(&ti->ti_chain);
488                 OBD_FREE_PTR(ti);
489         }
490         mutex_unlock(&pinger_mutex);
491         return 0;
492 }
493 EXPORT_SYMBOL(ptlrpc_del_timeout_client);
494
495 int ptlrpc_pinger_remove_timeouts(void)
496 {
497         struct timeout_item *item, *tmp;
498
499         mutex_lock(&pinger_mutex);
500         list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
501                 LASSERT(list_empty(&item->ti_obd_list));
502                 list_del(&item->ti_chain);
503                 OBD_FREE_PTR(item);
504         }
505         mutex_unlock(&pinger_mutex);
506         return 0;
507 }
508
509 void ptlrpc_pinger_wake_up()
510 {
511 #ifdef ENABLE_PINGER
512         mod_delayed_work(pinger_wq, &ping_work, 0);
513 #endif
514 }
515
516 /* Ping evictor thread */
517 #define PET_READY     1
518 #define PET_TERMINATE 2
519
520 static int               pet_refcount = 0;
521 static int               pet_state;
522 static wait_queue_head_t pet_waitq;
523 static struct list_head  pet_list;
524 static DEFINE_SPINLOCK(pet_lock);
525
526 int ping_evictor_wake(struct obd_export *exp)
527 {
528         struct obd_device *obd;
529
530         spin_lock(&pet_lock);
531         if (pet_state != PET_READY) {
532                 /* eventually the new obd will call here again. */
533                 spin_unlock(&pet_lock);
534                 return 1;
535         }
536
537         obd = class_exp2obd(exp);
538         if (list_empty(&obd->obd_evict_list)) {
539                 class_incref(obd, "evictor", obd);
540                 list_add(&obd->obd_evict_list, &pet_list);
541         }
542         spin_unlock(&pet_lock);
543
544         wake_up(&pet_waitq);
545         return 0;
546 }
547
548 static int ping_evictor_main(void *arg)
549 {
550         struct obd_device *obd;
551         struct obd_export *exp;
552         struct l_wait_info lwi = { 0 };
553         time64_t expire_time;
554
555         ENTRY;
556         unshare_fs_struct();
557
558         CDEBUG(D_HA, "Starting Ping Evictor\n");
559         pet_state = PET_READY;
560         while (1) {
561                 l_wait_event(pet_waitq, (!list_empty(&pet_list)) ||
562                             (pet_state == PET_TERMINATE), &lwi);
563
564                 /* loop until all obd's will be removed */
565                 if ((pet_state == PET_TERMINATE) && list_empty(&pet_list))
566                         break;
567
568                 /* we only get here if pet_exp != NULL, and the end of this
569                  * loop is the only place which sets it NULL again, so lock
570                  * is not strictly necessary. */
571                 spin_lock(&pet_lock);
572                 obd = list_entry(pet_list.next, struct obd_device,
573                                  obd_evict_list);
574                 spin_unlock(&pet_lock);
575
576                 expire_time = ktime_get_real_seconds() - PING_EVICT_TIMEOUT;
577
578                 CDEBUG(D_HA, "evicting all exports of obd %s older than %lld\n",
579                        obd->obd_name, expire_time);
580
581                 /* Exports can't be deleted out of the list while we hold
582                  * the obd lock (class_unlink_export), which means we can't
583                  * lose the last ref on the export.  If they've already been
584                  * removed from the list, we won't find them here. */
585                 spin_lock(&obd->obd_dev_lock);
586                 while (!list_empty(&obd->obd_exports_timed)) {
587                         exp = list_entry(obd->obd_exports_timed.next,
588                                          struct obd_export,
589                                          exp_obd_chain_timed);
590                         if (expire_time > exp->exp_last_request_time) {
591                                 class_export_get(exp);
592                                 spin_unlock(&obd->obd_dev_lock);
593                                 LCONSOLE_WARN("%s: haven't heard from client %s"
594                                               " (at %s) in %lld seconds. I think"
595                                               " it's dead, and I am evicting"
596                                               " it. exp %p, cur %lld expire %lld"
597                                               " last %lld\n",
598                                               obd->obd_name,
599                                               obd_uuid2str(&exp->exp_client_uuid),
600                                               obd_export_nid2str(exp),
601                                               ktime_get_real_seconds() -
602                                               exp->exp_last_request_time,
603                                               exp, ktime_get_real_seconds(),
604                                               expire_time,
605                                               exp->exp_last_request_time);
606                                 CDEBUG(D_HA, "Last request was at %lld\n",
607                                        exp->exp_last_request_time);
608                                 class_fail_export(exp);
609                                 class_export_put(exp);
610                                 spin_lock(&obd->obd_dev_lock);
611                         } else {
612                                 /* List is sorted, so everyone below is ok */
613                                 break;
614                         }
615                 }
616                 spin_unlock(&obd->obd_dev_lock);
617
618                 spin_lock(&pet_lock);
619                 list_del_init(&obd->obd_evict_list);
620                 spin_unlock(&pet_lock);
621
622                 class_decref(obd, "evictor", obd);
623         }
624         CDEBUG(D_HA, "Exiting Ping Evictor\n");
625
626         RETURN(0);
627 }
628
629 void ping_evictor_start(void)
630 {
631         struct task_struct *task;
632
633         if (++pet_refcount > 1)
634                 return;
635
636         INIT_LIST_HEAD(&pet_list);
637         init_waitqueue_head(&pet_waitq);
638
639         task = kthread_run(ping_evictor_main, NULL, "ll_evictor");
640         if (IS_ERR(task)) {
641                 pet_refcount--;
642                 CERROR("Cannot start ping evictor thread: %ld\n",
643                         PTR_ERR(task));
644         }
645 }
646 EXPORT_SYMBOL(ping_evictor_start);
647
648 void ping_evictor_stop(void)
649 {
650         if (--pet_refcount > 0)
651                 return;
652
653         pet_state = PET_TERMINATE;
654         wake_up(&pet_waitq);
655 }
656 EXPORT_SYMBOL(ping_evictor_stop);