1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41#define DEBUG_SUBSYSTEM S_RPC
42
43#include <obd_support.h>
44#include <obd_class.h>
45#include "ptlrpc_internal.h"
46
47static int suppress_pings;
48module_param(suppress_pings, int, 0644);
49MODULE_PARM_DESC(suppress_pings, "Suppress pings");
50
51struct mutex pinger_mutex;
52static LIST_HEAD(pinger_imports);
53static struct list_head timeout_list = LIST_HEAD_INIT(timeout_list);
54
55int ptlrpc_pinger_suppress_pings(void)
56{
57 return suppress_pings;
58}
59EXPORT_SYMBOL(ptlrpc_pinger_suppress_pings);
60
61struct ptlrpc_request *
62ptlrpc_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
75int ptlrpc_obd_ping(struct obd_device *obd)
76{
77 int rc;
78 struct ptlrpc_request *req;
79
80 req = ptlrpc_prep_ping(obd->u.cli.cl_import);
81 if (req == NULL)
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}
92EXPORT_SYMBOL(ptlrpc_obd_ping);
93
94int ptlrpc_ping(struct obd_import *imp)
95{
96 struct ptlrpc_request *req;
97
98 req = ptlrpc_prep_ping(imp);
99 if (req == NULL) {
100 CERROR("OOM trying to ping %s->%s\n",
101 imp->imp_obd->obd_uuid.uuid,
102 obd2cli_tgt(imp->imp_obd));
103 return -ENOMEM;
104 }
105
106 DEBUG_REQ(D_INFO, req, "pinging %s->%s",
107 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
108 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
109
110 return 0;
111}
112
113void ptlrpc_update_next_ping(struct obd_import *imp, int soon)
114{
115 int time = soon ? PING_INTERVAL_SHORT : PING_INTERVAL;
116 if (imp->imp_state == LUSTRE_IMP_DISCON) {
117 int dtime = max_t(int, CONNECTION_SWITCH_MIN,
118 AT_OFF ? 0 :
119 at_get(&imp->imp_at.iat_net_latency));
120 time = min(time, dtime);
121 }
122 imp->imp_next_ping = cfs_time_shift(time);
123}
124
125void ptlrpc_ping_import_soon(struct obd_import *imp)
126{
127 imp->imp_next_ping = cfs_time_current();
128}
129
130static inline int imp_is_deactive(struct obd_import *imp)
131{
132 return (imp->imp_deactive ||
133 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_IMP_DEACTIVE));
134}
135
136static inline int ptlrpc_next_reconnect(struct obd_import *imp)
137{
138 if (imp->imp_server_timeout)
139 return cfs_time_shift(obd_timeout / 2);
140 else
141 return cfs_time_shift(obd_timeout);
142}
143
144cfs_duration_t pinger_check_timeout(cfs_time_t time)
145{
146 struct timeout_item *item;
147 cfs_time_t timeout = PING_INTERVAL;
148
149
150 mutex_lock(&pinger_mutex);
151 list_for_each_entry(item, &timeout_list, ti_chain) {
152 int ti_timeout = item->ti_timeout;
153 if (timeout > ti_timeout)
154 timeout = ti_timeout;
155 break;
156 }
157 mutex_unlock(&pinger_mutex);
158
159 return cfs_time_sub(cfs_time_add(time, cfs_time_seconds(timeout)),
160 cfs_time_current());
161}
162
163static bool ir_up;
164
165void ptlrpc_pinger_ir_up(void)
166{
167 CDEBUG(D_HA, "IR up\n");
168 ir_up = true;
169}
170EXPORT_SYMBOL(ptlrpc_pinger_ir_up);
171
172void ptlrpc_pinger_ir_down(void)
173{
174 CDEBUG(D_HA, "IR down\n");
175 ir_up = false;
176}
177EXPORT_SYMBOL(ptlrpc_pinger_ir_down);
178
179static void ptlrpc_pinger_process_import(struct obd_import *imp,
180 unsigned long this_ping)
181{
182 int level;
183 int force;
184 int force_next;
185 int suppress;
186
187 spin_lock(&imp->imp_lock);
188
189 level = imp->imp_state;
190 force = imp->imp_force_verify;
191 force_next = imp->imp_force_next_verify;
192
193
194
195 suppress = ir_up && OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS);
196
197 imp->imp_force_verify = 0;
198
199 if (cfs_time_aftereq(imp->imp_next_ping - 5 * CFS_TICK, this_ping) &&
200 !force) {
201 spin_unlock(&imp->imp_lock);
202 return;
203 }
204
205 imp->imp_force_next_verify = 0;
206
207 spin_unlock(&imp->imp_lock);
208
209 CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA, "%s->%s: level %s/%u "
210 "force %u force_next %u deactive %u pingable %u suppress %u\n",
211 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
212 ptlrpc_import_state_name(level), level, force, force_next,
213 imp->imp_deactive, imp->imp_pingable, suppress);
214
215 if (level == LUSTRE_IMP_DISCON && !imp_is_deactive(imp)) {
216
217 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
218 if (!imp->imp_no_pinger_recover)
219 ptlrpc_initiate_recovery(imp);
220 } else if (level != LUSTRE_IMP_FULL ||
221 imp->imp_obd->obd_no_recov ||
222 imp_is_deactive(imp)) {
223 CDEBUG(D_HA, "%s->%s: not pinging (in recovery "
224 "or recovery disabled: %s)\n",
225 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
226 ptlrpc_import_state_name(level));
227 } else if ((imp->imp_pingable && !suppress) || force_next || force) {
228 ptlrpc_ping(imp);
229 }
230}
231
232static int ptlrpc_pinger_main(void *arg)
233{
234 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
235
236
237 thread_set_flags(thread, SVC_RUNNING);
238 wake_up(&thread->t_ctl_waitq);
239
240
241 while (1) {
242 cfs_time_t this_ping = cfs_time_current();
243 struct l_wait_info lwi;
244 cfs_duration_t time_to_next_wake;
245 struct timeout_item *item;
246 struct list_head *iter;
247
248 mutex_lock(&pinger_mutex);
249 list_for_each_entry(item, &timeout_list, ti_chain) {
250 item->ti_cb(item, item->ti_cb_data);
251 }
252 list_for_each(iter, &pinger_imports) {
253 struct obd_import *imp =
254 list_entry(iter, struct obd_import,
255 imp_pinger_chain);
256
257 ptlrpc_pinger_process_import(imp, this_ping);
258
259 if (imp->imp_pingable && imp->imp_next_ping &&
260 cfs_time_after(imp->imp_next_ping,
261 cfs_time_add(this_ping,
262 cfs_time_seconds(PING_INTERVAL))))
263 ptlrpc_update_next_ping(imp, 0);
264 }
265 mutex_unlock(&pinger_mutex);
266
267 obd_update_maxusage();
268
269
270 time_to_next_wake = pinger_check_timeout(this_ping);
271
272
273
274
275
276
277 CDEBUG(D_INFO, "next wakeup in "CFS_DURATION_T" ("
278 CFS_TIME_T")\n", time_to_next_wake,
279 cfs_time_add(this_ping,cfs_time_seconds(PING_INTERVAL)));
280 if (time_to_next_wake > 0) {
281 lwi = LWI_TIMEOUT(max_t(cfs_duration_t,
282 time_to_next_wake,
283 cfs_time_seconds(1)),
284 NULL, NULL);
285 l_wait_event(thread->t_ctl_waitq,
286 thread_is_stopping(thread) ||
287 thread_is_event(thread),
288 &lwi);
289 if (thread_test_and_clear_flags(thread, SVC_STOPPING)) {
290 break;
291 } else {
292
293 thread_test_and_clear_flags(thread, SVC_EVENT);
294 }
295 }
296 }
297
298 thread_set_flags(thread, SVC_STOPPED);
299 wake_up(&thread->t_ctl_waitq);
300
301 CDEBUG(D_NET, "pinger thread exiting, process %d\n", current_pid());
302 return 0;
303}
304
305static struct ptlrpc_thread pinger_thread;
306
307int ptlrpc_start_pinger(void)
308{
309 struct l_wait_info lwi = { 0 };
310 int rc;
311
312 if (!thread_is_init(&pinger_thread) &&
313 !thread_is_stopped(&pinger_thread))
314 return -EALREADY;
315
316 init_waitqueue_head(&pinger_thread.t_ctl_waitq);
317
318 strcpy(pinger_thread.t_name, "ll_ping");
319
320
321
322 rc = PTR_ERR(kthread_run(ptlrpc_pinger_main, &pinger_thread,
323 "%s", pinger_thread.t_name));
324 if (IS_ERR_VALUE(rc)) {
325 CERROR("cannot start thread: %d\n", rc);
326 return rc;
327 }
328 l_wait_event(pinger_thread.t_ctl_waitq,
329 thread_is_running(&pinger_thread), &lwi);
330
331 if (suppress_pings)
332 CWARN("Pings will be suppressed at the request of the "
333 "administrator. The configuration shall meet the "
334 "additional requirements described in the manual. "
335 "(Search for the \"suppress_pings\" kernel module "
336 "parameter.)\n");
337
338 return 0;
339}
340
341int ptlrpc_pinger_remove_timeouts(void);
342
343int ptlrpc_stop_pinger(void)
344{
345 struct l_wait_info lwi = { 0 };
346 int rc = 0;
347
348 if (thread_is_init(&pinger_thread) ||
349 thread_is_stopped(&pinger_thread))
350 return -EALREADY;
351
352 ptlrpc_pinger_remove_timeouts();
353 thread_set_flags(&pinger_thread, SVC_STOPPING);
354 wake_up(&pinger_thread.t_ctl_waitq);
355
356 l_wait_event(pinger_thread.t_ctl_waitq,
357 thread_is_stopped(&pinger_thread), &lwi);
358
359 return rc;
360}
361
362void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
363{
364 ptlrpc_update_next_ping(imp, 0);
365}
366EXPORT_SYMBOL(ptlrpc_pinger_sending_on_import);
367
368void ptlrpc_pinger_commit_expected(struct obd_import *imp)
369{
370 ptlrpc_update_next_ping(imp, 1);
371 LASSERT(spin_is_locked(&imp->imp_lock));
372
373
374
375
376
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
383int ptlrpc_pinger_add_import(struct obd_import *imp)
384{
385 if (!list_empty(&imp->imp_pinger_chain))
386 return -EALREADY;
387
388 mutex_lock(&pinger_mutex);
389 CDEBUG(D_HA, "adding pingable import %s->%s\n",
390 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
391
392 imp->imp_obd->obd_no_recov = 0;
393 ptlrpc_update_next_ping(imp, 0);
394
395 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
396 class_import_get(imp);
397
398 ptlrpc_pinger_wake_up();
399 mutex_unlock(&pinger_mutex);
400
401 return 0;
402}
403EXPORT_SYMBOL(ptlrpc_pinger_add_import);
404
405int ptlrpc_pinger_del_import(struct obd_import *imp)
406{
407 if (list_empty(&imp->imp_pinger_chain))
408 return -ENOENT;
409
410 mutex_lock(&pinger_mutex);
411 list_del_init(&imp->imp_pinger_chain);
412 CDEBUG(D_HA, "removing pingable import %s->%s\n",
413 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
414
415 imp->imp_obd->obd_no_recov = 1;
416 class_import_put(imp);
417 mutex_unlock(&pinger_mutex);
418 return 0;
419}
420EXPORT_SYMBOL(ptlrpc_pinger_del_import);
421
422
423
424
425
426struct timeout_item* ptlrpc_new_timeout(int time, enum timeout_event event,
427 timeout_cb_t cb, void *data)
428{
429 struct timeout_item *ti;
430
431 OBD_ALLOC_PTR(ti);
432 if (!ti)
433 return(NULL);
434
435 INIT_LIST_HEAD(&ti->ti_obd_list);
436 INIT_LIST_HEAD(&ti->ti_chain);
437 ti->ti_timeout = time;
438 ti->ti_event = event;
439 ti->ti_cb = cb;
440 ti->ti_cb_data = data;
441
442 return ti;
443}
444
445
446
447
448
449static struct timeout_item*
450ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
451 timeout_cb_t cb, void *data)
452{
453 struct timeout_item *item, *tmp;
454
455 LASSERT(mutex_is_locked(&pinger_mutex));
456
457 list_for_each_entry(item, &timeout_list, ti_chain)
458 if (item->ti_event == event)
459 goto out;
460
461 item = ptlrpc_new_timeout(time, event, cb, data);
462 if (item) {
463 list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
464 if (tmp->ti_timeout < time) {
465 list_add(&item->ti_chain, &tmp->ti_chain);
466 goto out;
467 }
468 }
469 list_add(&item->ti_chain, &timeout_list);
470 }
471out:
472 return item;
473}
474
475
476
477
478int ptlrpc_add_timeout_client(int time, enum timeout_event event,
479 timeout_cb_t cb, void *data,
480 struct list_head *obd_list)
481{
482 struct timeout_item *ti;
483
484 mutex_lock(&pinger_mutex);
485 ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
486 if (!ti) {
487 mutex_unlock(&pinger_mutex);
488 return (-EINVAL);
489 }
490 list_add(obd_list, &ti->ti_obd_list);
491 mutex_unlock(&pinger_mutex);
492 return 0;
493}
494EXPORT_SYMBOL(ptlrpc_add_timeout_client);
495
496int ptlrpc_del_timeout_client(struct list_head *obd_list,
497 enum timeout_event event)
498{
499 struct timeout_item *ti = NULL, *item;
500
501 if (list_empty(obd_list))
502 return 0;
503 mutex_lock(&pinger_mutex);
504 list_del_init(obd_list);
505
506
507
508
509 list_for_each_entry(item, &timeout_list, ti_chain) {
510 if (item->ti_event == event) {
511 ti = item;
512 break;
513 }
514 }
515 LASSERTF(ti != NULL, "ti is NULL !\n");
516 if (list_empty(&ti->ti_obd_list)) {
517 list_del(&ti->ti_chain);
518 OBD_FREE_PTR(ti);
519 }
520 mutex_unlock(&pinger_mutex);
521 return 0;
522}
523EXPORT_SYMBOL(ptlrpc_del_timeout_client);
524
525int ptlrpc_pinger_remove_timeouts(void)
526{
527 struct timeout_item *item, *tmp;
528
529 mutex_lock(&pinger_mutex);
530 list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
531 LASSERT(list_empty(&item->ti_obd_list));
532 list_del(&item->ti_chain);
533 OBD_FREE_PTR(item);
534 }
535 mutex_unlock(&pinger_mutex);
536 return 0;
537}
538
539void ptlrpc_pinger_wake_up(void)
540{
541 thread_add_flags(&pinger_thread, SVC_EVENT);
542 wake_up(&pinger_thread.t_ctl_waitq);
543}
544
545
546#define PET_READY 1
547#define PET_TERMINATE 2
548
549static int pet_refcount = 0;
550static int pet_state;
551static wait_queue_head_t pet_waitq;
552LIST_HEAD(pet_list);
553static DEFINE_SPINLOCK(pet_lock);
554
555int ping_evictor_wake(struct obd_export *exp)
556{
557 struct obd_device *obd;
558
559 spin_lock(&pet_lock);
560 if (pet_state != PET_READY) {
561
562 spin_unlock(&pet_lock);
563 return 1;
564 }
565
566 obd = class_exp2obd(exp);
567 if (list_empty(&obd->obd_evict_list)) {
568 class_incref(obd, "evictor", obd);
569 list_add(&obd->obd_evict_list, &pet_list);
570 }
571 spin_unlock(&pet_lock);
572
573 wake_up(&pet_waitq);
574 return 0;
575}
576
577static int ping_evictor_main(void *arg)
578{
579 struct obd_device *obd;
580 struct obd_export *exp;
581 struct l_wait_info lwi = { 0 };
582 time_t expire_time;
583
584 unshare_fs_struct();
585
586 CDEBUG(D_HA, "Starting Ping Evictor\n");
587 pet_state = PET_READY;
588 while (1) {
589 l_wait_event(pet_waitq, (!list_empty(&pet_list)) ||
590 (pet_state == PET_TERMINATE), &lwi);
591
592
593 if ((pet_state == PET_TERMINATE) && list_empty(&pet_list))
594 break;
595
596
597
598
599 spin_lock(&pet_lock);
600 obd = list_entry(pet_list.next, struct obd_device,
601 obd_evict_list);
602 spin_unlock(&pet_lock);
603
604 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
605
606 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
607 obd->obd_name, expire_time);
608
609
610
611
612
613 spin_lock(&obd->obd_dev_lock);
614 while (!list_empty(&obd->obd_exports_timed)) {
615 exp = list_entry(obd->obd_exports_timed.next,
616 struct obd_export,
617 exp_obd_chain_timed);
618 if (expire_time > exp->exp_last_request_time) {
619 class_export_get(exp);
620 spin_unlock(&obd->obd_dev_lock);
621 LCONSOLE_WARN("%s: haven't heard from client %s"
622 " (at %s) in %ld seconds. I think"
623 " it's dead, and I am evicting"
624 " it. exp %p, cur %ld expire %ld"
625 " last %ld\n",
626 obd->obd_name,
627 obd_uuid2str(&exp->exp_client_uuid),
628 obd_export_nid2str(exp),
629 (long)(cfs_time_current_sec() -
630 exp->exp_last_request_time),
631 exp, (long)cfs_time_current_sec(),
632 (long)expire_time,
633 (long)exp->exp_last_request_time);
634 CDEBUG(D_HA, "Last request was at %ld\n",
635 exp->exp_last_request_time);
636 class_fail_export(exp);
637 class_export_put(exp);
638 spin_lock(&obd->obd_dev_lock);
639 } else {
640
641 break;
642 }
643 }
644 spin_unlock(&obd->obd_dev_lock);
645
646 spin_lock(&pet_lock);
647 list_del_init(&obd->obd_evict_list);
648 spin_unlock(&pet_lock);
649
650 class_decref(obd, "evictor", obd);
651 }
652 CDEBUG(D_HA, "Exiting Ping Evictor\n");
653
654 return 0;
655}
656
657void ping_evictor_start(void)
658{
659 struct task_struct *task;
660
661 if (++pet_refcount > 1)
662 return;
663
664 init_waitqueue_head(&pet_waitq);
665
666 task = kthread_run(ping_evictor_main, NULL, "ll_evictor");
667 if (IS_ERR(task)) {
668 pet_refcount--;
669 CERROR("Cannot start ping evictor thread: %ld\n",
670 PTR_ERR(task));
671 }
672}
673EXPORT_SYMBOL(ping_evictor_start);
674
675void ping_evictor_stop(void)
676{
677 if (--pet_refcount > 0)
678 return;
679
680 pet_state = PET_TERMINATE;
681 wake_up(&pet_waitq);
682}
683EXPORT_SYMBOL(ping_evictor_stop);
684