1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_RPC
39 #include <liblustre.h>
41 #include <obd_support.h>
42 #include <obd_class.h>
43 #include <lustre_net.h>
44 #include <lu_object.h>
45 #include <lnet/types.h>
46 #include "ptlrpc_internal.h"
48 /* The following are visible and mutable through /sys/module/ptlrpc */
49 int test_req_buffer_pressure = 0;
50 CFS_MODULE_PARM(test_req_buffer_pressure, "i", int, 0444,
51 "set non-zero to put pressure on request buffer pools");
52 CFS_MODULE_PARM(at_min, "i", int, 0644,
53 "Adaptive timeout minimum (sec)");
54 CFS_MODULE_PARM(at_max, "i", int, 0644,
55 "Adaptive timeout maximum (sec)");
56 CFS_MODULE_PARM(at_history, "i", int, 0644,
57 "Adaptive timeouts remember the slowest event that took place "
58 "within this period (sec)");
59 CFS_MODULE_PARM(at_early_margin, "i", int, 0644,
60 "How soon before an RPC deadline to send an early reply");
61 CFS_MODULE_PARM(at_extra, "i", int, 0644,
62 "How much extra time to give with each early reply");
66 static int ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc);
68 static CFS_LIST_HEAD(ptlrpc_all_services);
69 spinlock_t ptlrpc_all_services_lock;
72 ptlrpc_alloc_request_buffer (int size)
76 if (size > SVC_BUF_VMALLOC_THRESHOLD)
77 OBD_VMALLOC(ptr, size);
85 ptlrpc_free_request_buffer (char *ptr, int size)
87 if (size > SVC_BUF_VMALLOC_THRESHOLD)
93 struct ptlrpc_request_buffer_desc *
94 ptlrpc_alloc_rqbd (struct ptlrpc_service *svc)
96 struct ptlrpc_request_buffer_desc *rqbd;
102 rqbd->rqbd_service = svc;
103 rqbd->rqbd_refcount = 0;
104 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
105 rqbd->rqbd_cbid.cbid_arg = rqbd;
106 CFS_INIT_LIST_HEAD(&rqbd->rqbd_reqs);
107 rqbd->rqbd_buffer = ptlrpc_alloc_request_buffer(svc->srv_buf_size);
109 if (rqbd->rqbd_buffer == NULL) {
114 spin_lock(&svc->srv_lock);
115 list_add(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
117 spin_unlock(&svc->srv_lock);
123 ptlrpc_free_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
125 struct ptlrpc_service *svc = rqbd->rqbd_service;
127 LASSERT (rqbd->rqbd_refcount == 0);
128 LASSERT (list_empty(&rqbd->rqbd_reqs));
130 spin_lock(&svc->srv_lock);
131 list_del(&rqbd->rqbd_list);
133 spin_unlock(&svc->srv_lock);
135 ptlrpc_free_request_buffer (rqbd->rqbd_buffer, svc->srv_buf_size);
140 ptlrpc_grow_req_bufs(struct ptlrpc_service *svc)
142 struct ptlrpc_request_buffer_desc *rqbd;
145 CDEBUG(D_RPCTRACE, "%s: allocate %d new %d-byte reqbufs (%d/%d left)\n",
146 svc->srv_name, svc->srv_nbuf_per_group, svc->srv_buf_size,
147 svc->srv_nrqbd_receiving, svc->srv_nbufs);
148 for (i = 0; i < svc->srv_nbuf_per_group; i++) {
149 rqbd = ptlrpc_alloc_rqbd(svc);
152 CERROR ("%s: Can't allocate request buffer\n",
157 if (ptlrpc_server_post_idle_rqbds(svc) < 0)
165 ptlrpc_save_lock(struct ptlrpc_request *req,
166 struct lustre_handle *lock, int mode, int no_ack)
168 struct ptlrpc_reply_state *rs = req->rq_reply_state;
172 LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
174 if (req->rq_export->exp_disconnected) {
175 ldlm_lock_decref(lock, mode);
177 idx = rs->rs_nlocks++;
178 rs->rs_locks[idx] = *lock;
179 rs->rs_modes[idx] = mode;
180 rs->rs_difficult = 1;
181 rs->rs_no_ack = !!no_ack;
187 #define HRT_RUNNING 0
188 #define HRT_STOPPING 1
190 struct ptlrpc_hr_thread {
192 unsigned long hrt_flags;
193 cfs_waitq_t hrt_wait;
194 struct list_head hrt_queue;
195 struct completion hrt_completion;
198 struct ptlrpc_hr_service {
202 struct ptlrpc_hr_thread hr_threads[0];
206 struct list_head rsb_replies;
207 struct ptlrpc_service *rsb_svc;
208 unsigned int rsb_n_replies;
212 * A pointer to per-node reply handling service.
214 static struct ptlrpc_hr_service *ptlrpc_hr = NULL;
217 * maximum mumber of replies scheduled in one batch
219 #define MAX_SCHEDULED 256
222 * Initialize a reply batch.
226 static void rs_batch_init(struct rs_batch *b)
228 memset(b, 0, sizeof *b);
229 CFS_INIT_LIST_HEAD(&b->rsb_replies);
233 * Dispatch all replies accumulated in the batch to one from
234 * dedicated reply handling threads.
238 static void rs_batch_dispatch(struct rs_batch *b)
240 if (b->rsb_n_replies != 0) {
241 struct ptlrpc_hr_service *hr = ptlrpc_hr;
244 idx = hr->hr_index++;
245 if (hr->hr_index >= hr->hr_n_threads)
248 spin_lock(&hr->hr_threads[idx].hrt_lock);
249 list_splice_init(&b->rsb_replies,
250 &hr->hr_threads[idx].hrt_queue);
251 spin_unlock(&hr->hr_threads[idx].hrt_lock);
252 cfs_waitq_signal(&hr->hr_threads[idx].hrt_wait);
253 b->rsb_n_replies = 0;
258 * Add a reply to a batch.
259 * Add one reply object to a batch, schedule batched replies if overload.
264 static void rs_batch_add(struct rs_batch *b, struct ptlrpc_reply_state *rs)
266 struct ptlrpc_service *svc = rs->rs_service;
268 if (svc != b->rsb_svc || b->rsb_n_replies >= MAX_SCHEDULED) {
269 if (b->rsb_svc != NULL) {
270 rs_batch_dispatch(b);
271 spin_unlock(&b->rsb_svc->srv_lock);
273 spin_lock(&svc->srv_lock);
276 spin_lock(&rs->rs_lock);
277 rs->rs_scheduled_ever = 1;
278 if (rs->rs_scheduled == 0) {
279 list_move(&rs->rs_list, &b->rsb_replies);
280 rs->rs_scheduled = 1;
283 rs->rs_committed = 1;
284 spin_unlock(&rs->rs_lock);
288 * Reply batch finalization.
289 * Dispatch remaining replies from the batch
290 * and release remaining spinlock.
294 static void rs_batch_fini(struct rs_batch *b)
296 if (b->rsb_svc != 0) {
297 rs_batch_dispatch(b);
298 spin_unlock(&b->rsb_svc->srv_lock);
302 #define DECLARE_RS_BATCH(b) struct rs_batch b
304 #else /* __KERNEL__ */
306 #define rs_batch_init(b) do{}while(0)
307 #define rs_batch_fini(b) do{}while(0)
308 #define rs_batch_add(b, r) ptlrpc_schedule_difficult_reply(r)
309 #define DECLARE_RS_BATCH(b)
311 #endif /* __KERNEL__ */
313 void ptlrpc_dispatch_difficult_reply(struct ptlrpc_reply_state *rs)
316 struct ptlrpc_hr_service *hr = ptlrpc_hr;
320 LASSERT(list_empty(&rs->rs_list));
322 idx = hr->hr_index++;
323 if (hr->hr_index >= hr->hr_n_threads)
325 spin_lock(&hr->hr_threads[idx].hrt_lock);
326 list_add_tail(&rs->rs_list, &hr->hr_threads[idx].hrt_queue);
327 spin_unlock(&hr->hr_threads[idx].hrt_lock);
328 cfs_waitq_signal(&hr->hr_threads[idx].hrt_wait);
331 list_add_tail(&rs->rs_list, &rs->rs_service->srv_reply_queue);
336 ptlrpc_schedule_difficult_reply (struct ptlrpc_reply_state *rs)
340 LASSERT_SPIN_LOCKED(&rs->rs_service->srv_lock);
341 LASSERT_SPIN_LOCKED(&rs->rs_lock);
342 LASSERT (rs->rs_difficult);
343 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
345 if (rs->rs_scheduled) { /* being set up or already notified */
350 rs->rs_scheduled = 1;
351 list_del_init(&rs->rs_list);
352 ptlrpc_dispatch_difficult_reply(rs);
356 void ptlrpc_commit_replies(struct obd_export *exp)
358 struct ptlrpc_reply_state *rs, *nxt;
359 DECLARE_RS_BATCH(batch);
362 rs_batch_init(&batch);
363 /* Find any replies that have been committed and get their service
364 * to attend to complete them. */
366 /* CAVEAT EMPTOR: spinlock ordering!!! */
367 spin_lock(&exp->exp_uncommitted_replies_lock);
368 list_for_each_entry_safe(rs, nxt, &exp->exp_uncommitted_replies,
370 LASSERT (rs->rs_difficult);
371 /* VBR: per-export last_committed */
372 LASSERT(rs->rs_export);
373 if (rs->rs_transno <= exp->exp_last_committed) {
374 list_del_init(&rs->rs_obd_list);
375 rs_batch_add(&batch, rs);
378 spin_unlock(&exp->exp_uncommitted_replies_lock);
379 rs_batch_fini(&batch);
384 ptlrpc_server_post_idle_rqbds (struct ptlrpc_service *svc)
386 struct ptlrpc_request_buffer_desc *rqbd;
391 spin_lock(&svc->srv_lock);
393 if (list_empty (&svc->srv_idle_rqbds)) {
394 spin_unlock(&svc->srv_lock);
398 rqbd = list_entry(svc->srv_idle_rqbds.next,
399 struct ptlrpc_request_buffer_desc,
401 list_del (&rqbd->rqbd_list);
403 /* assume we will post successfully */
404 svc->srv_nrqbd_receiving++;
405 list_add (&rqbd->rqbd_list, &svc->srv_active_rqbds);
407 spin_unlock(&svc->srv_lock);
409 rc = ptlrpc_register_rqbd(rqbd);
416 spin_lock(&svc->srv_lock);
418 svc->srv_nrqbd_receiving--;
419 list_del(&rqbd->rqbd_list);
420 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
422 /* Don't complain if no request buffers are posted right now; LNET
423 * won't drop requests because we set the portal lazy! */
425 spin_unlock(&svc->srv_lock);
430 struct ptlrpc_service *ptlrpc_init_svc_conf(struct ptlrpc_service_conf *c,
431 svc_handler_t h, char *name,
432 struct proc_dir_entry *proc_entry,
433 svcreq_printfn_t prntfn,
436 return ptlrpc_init_svc(c->psc_nbufs, c->psc_bufsize,
437 c->psc_max_req_size, c->psc_max_reply_size,
438 c->psc_req_portal, c->psc_rep_portal,
439 c->psc_watchdog_factor,
441 prntfn, c->psc_min_threads, c->psc_max_threads,
442 threadname, c->psc_ctx_tags, NULL);
444 EXPORT_SYMBOL(ptlrpc_init_svc_conf);
446 static void ptlrpc_at_timer(unsigned long castmeharder)
448 struct ptlrpc_service *svc = (struct ptlrpc_service *)castmeharder;
449 svc->srv_at_check = 1;
450 svc->srv_at_checktime = cfs_time_current();
451 cfs_waitq_signal(&svc->srv_waitq);
454 /* @threadname should be 11 characters or less - 3 will be added on */
455 struct ptlrpc_service *
456 ptlrpc_init_svc(int nbufs, int bufsize, int max_req_size, int max_reply_size,
457 int req_portal, int rep_portal, int watchdog_factor,
458 svc_handler_t handler, char *name,
459 cfs_proc_dir_entry_t *proc_entry,
460 svcreq_printfn_t svcreq_printfn,
461 int min_threads, int max_threads,
462 char *threadname, __u32 ctx_tags,
463 svc_hpreq_handler_t hp_handler)
466 struct ptlrpc_at_array *array;
467 struct ptlrpc_service *service;
468 unsigned int size, index;
472 LASSERT (bufsize >= max_req_size + SPTLRPC_MAX_PAYLOAD);
473 LASSERT (ctx_tags != 0);
475 OBD_ALLOC_PTR(service);
479 /* First initialise enough for early teardown */
481 service->srv_name = name;
482 spin_lock_init(&service->srv_lock);
483 CFS_INIT_LIST_HEAD(&service->srv_threads);
484 cfs_waitq_init(&service->srv_waitq);
486 service->srv_nbuf_per_group = test_req_buffer_pressure ? 1 : nbufs;
487 service->srv_max_req_size = max_req_size + SPTLRPC_MAX_PAYLOAD;
488 service->srv_buf_size = bufsize;
489 service->srv_rep_portal = rep_portal;
490 service->srv_req_portal = req_portal;
491 service->srv_watchdog_factor = watchdog_factor;
492 service->srv_handler = handler;
493 service->srv_request_history_print_fn = svcreq_printfn;
494 service->srv_request_seq = 1; /* valid seq #s start at 1 */
495 service->srv_request_max_cull_seq = 0;
496 service->srv_threads_min = min_threads;
497 service->srv_threads_max = max_threads;
498 service->srv_thread_name = threadname;
499 service->srv_ctx_tags = ctx_tags;
500 service->srv_hpreq_handler = hp_handler;
501 service->srv_hpreq_ratio = PTLRPC_SVC_HP_RATIO;
502 service->srv_hpreq_count = 0;
503 service->srv_n_hpreq = 0;
505 rc = LNetSetLazyPortal(service->srv_req_portal);
508 CFS_INIT_LIST_HEAD(&service->srv_request_queue);
509 CFS_INIT_LIST_HEAD(&service->srv_request_hpq);
510 CFS_INIT_LIST_HEAD(&service->srv_idle_rqbds);
511 CFS_INIT_LIST_HEAD(&service->srv_active_rqbds);
512 CFS_INIT_LIST_HEAD(&service->srv_history_rqbds);
513 CFS_INIT_LIST_HEAD(&service->srv_request_history);
514 CFS_INIT_LIST_HEAD(&service->srv_active_replies);
516 CFS_INIT_LIST_HEAD(&service->srv_reply_queue);
518 CFS_INIT_LIST_HEAD(&service->srv_free_rs_list);
519 cfs_waitq_init(&service->srv_free_rs_waitq);
520 atomic_set(&service->srv_n_difficult_replies, 0);
522 spin_lock_init(&service->srv_at_lock);
523 CFS_INIT_LIST_HEAD(&service->srv_req_in_queue);
525 array = &service->srv_at_array;
526 size = at_est2timeout(at_max);
527 array->paa_size = size;
528 array->paa_count = 0;
529 array->paa_deadline = -1;
531 /* allocate memory for srv_at_array (ptlrpc_at_array) */
532 OBD_ALLOC(array->paa_reqs_array, sizeof(struct list_head) * size);
533 if (array->paa_reqs_array == NULL)
536 for (index = 0; index < size; index++)
537 CFS_INIT_LIST_HEAD(&array->paa_reqs_array[index]);
539 OBD_ALLOC(array->paa_reqs_count, sizeof(__u32) * size);
540 if (array->paa_reqs_count == NULL)
543 cfs_timer_init(&service->srv_at_timer, ptlrpc_at_timer, service);
544 /* At SOW, service time should be quick; 10s seems generous. If client
545 timeout is less than this, we'll be sending an early reply. */
546 at_init(&service->srv_at_estimate, 10, 0);
548 spin_lock (&ptlrpc_all_services_lock);
549 list_add (&service->srv_list, &ptlrpc_all_services);
550 spin_unlock (&ptlrpc_all_services_lock);
552 /* Now allocate the request buffers */
553 rc = ptlrpc_grow_req_bufs(service);
554 /* We shouldn't be under memory pressure at startup, so
555 * fail if we can't post all our buffers at this time. */
559 /* Now allocate pool of reply buffers */
560 /* Increase max reply size to next power of two */
561 service->srv_max_reply_size = 1;
562 while (service->srv_max_reply_size <
563 max_reply_size + SPTLRPC_MAX_PAYLOAD)
564 service->srv_max_reply_size <<= 1;
566 if (proc_entry != NULL)
567 ptlrpc_lprocfs_register_service(proc_entry, service);
569 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
570 service->srv_name, service->srv_req_portal);
574 ptlrpc_unregister_service(service);
579 * to actually free the request, must be called without holding svc_lock.
580 * note it's caller's responsibility to unlink req->rq_list.
582 static void ptlrpc_server_free_request(struct ptlrpc_request *req)
584 LASSERT(atomic_read(&req->rq_refcount) == 0);
585 LASSERT(list_empty(&req->rq_timed_list));
587 /* DEBUG_REQ() assumes the reply state of a request with a valid
588 * ref will not be destroyed until that reference is dropped. */
589 ptlrpc_req_drop_rs(req);
591 sptlrpc_svc_ctx_decref(req);
593 if (req != &req->rq_rqbd->rqbd_req) {
594 /* NB request buffers use an embedded
595 * req if the incoming req unlinked the
596 * MD; this isn't one of them! */
597 OBD_FREE(req, sizeof(*req));
602 * drop a reference count of the request. if it reaches 0, we either
603 * put it into history list, or free it immediately.
605 void ptlrpc_server_drop_request(struct ptlrpc_request *req)
607 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
608 struct ptlrpc_service *svc = rqbd->rqbd_service;
610 struct list_head *tmp;
611 struct list_head *nxt;
613 if (!atomic_dec_and_test(&req->rq_refcount))
616 spin_lock(&svc->srv_at_lock);
617 if (req->rq_at_linked) {
618 struct ptlrpc_at_array *array = &svc->srv_at_array;
619 __u32 index = req->rq_at_index;
621 LASSERT(!list_empty(&req->rq_timed_list));
622 list_del_init(&req->rq_timed_list);
623 req->rq_at_linked = 0;
624 array->paa_reqs_count[index]--;
627 LASSERT(list_empty(&req->rq_timed_list));
628 spin_unlock(&svc->srv_at_lock);
630 /* finalize request */
631 if (req->rq_export) {
632 class_export_put(req->rq_export);
633 req->rq_export = NULL;
636 spin_lock(&svc->srv_lock);
638 svc->srv_n_active_reqs--;
639 list_add(&req->rq_list, &rqbd->rqbd_reqs);
641 refcount = --(rqbd->rqbd_refcount);
643 /* request buffer is now idle: add to history */
644 list_del(&rqbd->rqbd_list);
645 list_add_tail(&rqbd->rqbd_list, &svc->srv_history_rqbds);
646 svc->srv_n_history_rqbds++;
648 /* cull some history?
649 * I expect only about 1 or 2 rqbds need to be recycled here */
650 while (svc->srv_n_history_rqbds > svc->srv_max_history_rqbds) {
651 rqbd = list_entry(svc->srv_history_rqbds.next,
652 struct ptlrpc_request_buffer_desc,
655 list_del(&rqbd->rqbd_list);
656 svc->srv_n_history_rqbds--;
658 /* remove rqbd's reqs from svc's req history while
659 * I've got the service lock */
660 list_for_each(tmp, &rqbd->rqbd_reqs) {
661 req = list_entry(tmp, struct ptlrpc_request,
663 /* Track the highest culled req seq */
664 if (req->rq_history_seq >
665 svc->srv_request_max_cull_seq)
666 svc->srv_request_max_cull_seq =
668 list_del(&req->rq_history_list);
671 spin_unlock(&svc->srv_lock);
673 list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
674 req = list_entry(rqbd->rqbd_reqs.next,
675 struct ptlrpc_request,
677 list_del(&req->rq_list);
678 ptlrpc_server_free_request(req);
681 spin_lock(&svc->srv_lock);
683 * now all reqs including the embedded req has been
684 * disposed, schedule request buffer for re-use.
686 LASSERT(atomic_read(&rqbd->rqbd_req.rq_refcount) == 0);
687 list_add_tail(&rqbd->rqbd_list, &svc->srv_idle_rqbds);
690 spin_unlock(&svc->srv_lock);
691 } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
692 /* If we are low on memory, we are not interested in history */
693 list_del(&req->rq_list);
694 list_del_init(&req->rq_history_list);
695 spin_unlock(&svc->srv_lock);
697 ptlrpc_server_free_request(req);
699 spin_unlock(&svc->srv_lock);
704 * to finish a request: stop sending more early replies, and release
705 * the request. should be called after we finished handling the request.
707 static void ptlrpc_server_finish_request(struct ptlrpc_request *req)
709 ptlrpc_server_drop_request(req);
712 /* This function makes sure dead exports are evicted in a timely manner.
713 This function is only called when some export receives a message (i.e.,
714 the network is up.) */
715 static void ptlrpc_update_export_timer(struct obd_export *exp, long extra_delay)
717 struct obd_export *oldest_exp;
718 time_t oldest_time, new_time;
724 /* Compensate for slow machines, etc, by faking our request time
725 into the future. Although this can break the strict time-ordering
726 of the list, we can be really lazy here - we don't have to evict
727 at the exact right moment. Eventually, all silent exports
728 will make it to the top of the list. */
730 /* Do not pay attention on 1sec or smaller renewals. */
731 new_time = cfs_time_current_sec() + extra_delay;
732 if (exp->exp_last_request_time + 1 /*second */ >= new_time)
735 exp->exp_last_request_time = new_time;
736 CDEBUG(D_HA, "updating export %s at "CFS_TIME_T" exp %p\n",
737 exp->exp_client_uuid.uuid,
738 exp->exp_last_request_time, exp);
740 /* exports may get disconnected from the chain even though the
741 export has references, so we must keep the spin lock while
742 manipulating the lists */
743 spin_lock(&exp->exp_obd->obd_dev_lock);
745 if (list_empty(&exp->exp_obd_chain_timed)) {
746 /* this one is not timed */
747 spin_unlock(&exp->exp_obd->obd_dev_lock);
751 list_move_tail(&exp->exp_obd_chain_timed,
752 &exp->exp_obd->obd_exports_timed);
754 oldest_exp = list_entry(exp->exp_obd->obd_exports_timed.next,
755 struct obd_export, exp_obd_chain_timed);
756 oldest_time = oldest_exp->exp_last_request_time;
757 spin_unlock(&exp->exp_obd->obd_dev_lock);
759 if (exp->exp_obd->obd_recovering) {
760 /* be nice to everyone during recovery */
765 /* Note - racing to start/reset the obd_eviction timer is safe */
766 if (exp->exp_obd->obd_eviction_timer == 0) {
767 /* Check if the oldest entry is expired. */
768 if (cfs_time_current_sec() > (oldest_time + PING_EVICT_TIMEOUT +
770 /* We need a second timer, in case the net was down and
771 * it just came back. Since the pinger may skip every
772 * other PING_INTERVAL (see note in ptlrpc_pinger_main),
773 * we better wait for 3. */
774 exp->exp_obd->obd_eviction_timer =
775 cfs_time_current_sec() + 3 * PING_INTERVAL;
776 CDEBUG(D_HA, "%s: Think about evicting %s from "CFS_TIME_T"\n",
777 exp->exp_obd->obd_name,
778 obd_export_nid2str(oldest_exp), oldest_time);
781 if (cfs_time_current_sec() >
782 (exp->exp_obd->obd_eviction_timer + extra_delay)) {
783 /* The evictor won't evict anyone who we've heard from
784 * recently, so we don't have to check before we start
786 if (!ping_evictor_wake(exp))
787 exp->exp_obd->obd_eviction_timer = 0;
794 static int ptlrpc_check_req(struct ptlrpc_request *req)
796 if (unlikely(lustre_msg_get_conn_cnt(req->rq_reqmsg) <
797 req->rq_export->exp_conn_cnt)) {
798 DEBUG_REQ(D_ERROR, req,
799 "DROPPING req from old connection %d < %d",
800 lustre_msg_get_conn_cnt(req->rq_reqmsg),
801 req->rq_export->exp_conn_cnt);
804 if (unlikely(req->rq_export->exp_obd &&
805 req->rq_export->exp_obd->obd_fail)) {
806 /* Failing over, don't handle any more reqs, send
807 error response instead. */
808 CDEBUG(D_RPCTRACE, "Dropping req %p for failed obd %s\n",
809 req, req->rq_export->exp_obd->obd_name);
810 req->rq_status = -ENODEV;
818 static void ptlrpc_at_set_timer(struct ptlrpc_service *svc)
820 struct ptlrpc_at_array *array = &svc->srv_at_array;
823 spin_lock(&svc->srv_at_lock);
824 if (array->paa_count == 0) {
825 cfs_timer_disarm(&svc->srv_at_timer);
826 spin_unlock(&svc->srv_at_lock);
830 /* Set timer for closest deadline */
831 next = (__s32)(array->paa_deadline - cfs_time_current_sec() -
834 ptlrpc_at_timer((unsigned long)svc);
836 cfs_timer_arm(&svc->srv_at_timer, cfs_time_shift(next));
837 spin_unlock(&svc->srv_at_lock);
838 CDEBUG(D_INFO, "armed %s at %+ds\n", svc->srv_name, next);
841 /* Add rpc to early reply check list */
842 static int ptlrpc_at_add_timed(struct ptlrpc_request *req)
844 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
845 struct ptlrpc_request *rq = NULL;
846 struct ptlrpc_at_array *array = &svc->srv_at_array;
853 if (req->rq_no_reply)
856 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0)
859 spin_lock(&svc->srv_at_lock);
860 LASSERT(list_empty(&req->rq_timed_list));
862 index = (unsigned long)req->rq_deadline % array->paa_size;
863 if (array->paa_reqs_count[index] > 0) {
864 /* latest rpcs will have the latest deadlines in the list,
865 * so search backward. */
866 list_for_each_entry_reverse(rq, &array->paa_reqs_array[index],
868 if (req->rq_deadline >= rq->rq_deadline) {
869 list_add(&req->rq_timed_list,
876 /* Add the request at the head of the list */
877 if (list_empty(&req->rq_timed_list))
878 list_add(&req->rq_timed_list, &array->paa_reqs_array[index]);
880 req->rq_at_linked = 1;
881 req->rq_at_index = index;
882 array->paa_reqs_count[index]++;
884 if (array->paa_count == 1 || array->paa_deadline > req->rq_deadline) {
885 array->paa_deadline = req->rq_deadline;
888 spin_unlock(&svc->srv_at_lock);
891 ptlrpc_at_set_timer(svc);
896 static int ptlrpc_at_send_early_reply(struct ptlrpc_request *req)
898 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
899 struct ptlrpc_request *reqcopy;
900 struct lustre_msg *reqmsg;
901 cfs_duration_t olddl = req->rq_deadline - cfs_time_current_sec();
906 /* deadline is when the client expects us to reply, margin is the
907 difference between clients' and servers' expectations */
908 DEBUG_REQ(D_ADAPTTO, req,
909 "%ssending early reply (deadline %+lds, margin %+lds) for "
910 "%d+%d", AT_OFF ? "AT off - not " : "",
911 olddl, olddl - at_get(&svc->srv_at_estimate),
912 at_get(&svc->srv_at_estimate), at_extra);
918 DEBUG_REQ(D_WARNING, req, "Already past deadline (%+lds), "
919 "not sending early reply. Consider increasing "
920 "at_early_margin (%d)?", olddl, at_early_margin);
922 /* Return an error so we're not re-added to the timed list. */
926 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0){
927 DEBUG_REQ(D_INFO, req, "Wanted to ask client for more time, "
928 "but no AT support");
932 if (req->rq_export &&
933 lustre_msg_get_flags(req->rq_reqmsg) &
934 (MSG_REPLAY | MSG_REQ_REPLAY_DONE | MSG_LOCK_REPLAY_DONE)) {
936 * Use at_extra as early reply period for recovery requests but
937 * make sure it is not bigger than recovery time / 4
939 at_add(&svc->srv_at_estimate,
941 req->rq_export->exp_obd->obd_recovery_timeout / 4));
943 /* Fake our processing time into the future to ask the clients
944 * for some extra amount of time */
945 at_add(&svc->srv_at_estimate, at_extra);
948 newdl = cfs_time_current_sec() + at_get(&svc->srv_at_estimate);
949 if (req->rq_deadline >= newdl) {
950 /* We're not adding any time, no need to send an early reply
951 (e.g. maybe at adaptive_max) */
952 DEBUG_REQ(D_WARNING, req, "Couldn't add any time ("
953 CFS_DURATION_T"/"CFS_DURATION_T"), "
954 "not sending early reply\n", olddl,
955 cfs_time_sub(newdl, cfs_time_current_sec()));
959 OBD_ALLOC(reqcopy, sizeof *reqcopy);
962 OBD_ALLOC(reqmsg, req->rq_reqlen);
964 OBD_FREE(reqcopy, sizeof *reqcopy);
969 reqcopy->rq_reply_state = NULL;
970 reqcopy->rq_rep_swab_mask = 0;
971 reqcopy->rq_pack_bulk = 0;
972 reqcopy->rq_pack_udesc = 0;
973 reqcopy->rq_packed_final = 0;
974 sptlrpc_svc_ctx_addref(reqcopy);
975 /* We only need the reqmsg for the magic */
976 reqcopy->rq_reqmsg = reqmsg;
977 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
979 LASSERT(atomic_read(&req->rq_refcount));
980 /** if it is last refcount then early reply isn't needed */
981 if (atomic_read(&req->rq_refcount) == 1) {
982 DEBUG_REQ(D_ADAPTTO, reqcopy, "Normal reply already sent out, "
983 "abort sending early reply\n");
984 GOTO(out, rc = -EINVAL);
988 reqcopy->rq_export = class_conn2export(
989 lustre_msg_get_handle(reqcopy->rq_reqmsg));
990 if (reqcopy->rq_export == NULL)
991 GOTO(out, rc = -ENODEV);
994 class_export_rpc_get(reqcopy->rq_export);
995 if (reqcopy->rq_export->exp_obd &&
996 reqcopy->rq_export->exp_obd->obd_fail)
997 GOTO(out_put, rc = -ENODEV);
999 rc = lustre_pack_reply_flags(reqcopy, 1, NULL, NULL, LPRFL_EARLY_REPLY);
1003 rc = ptlrpc_send_reply(reqcopy, PTLRPC_REPLY_EARLY);
1006 /* Adjust our own deadline to what we told the client */
1007 req->rq_deadline = newdl;
1008 req->rq_early_count++; /* number sent, server side */
1010 DEBUG_REQ(D_ERROR, req, "Early reply send failed %d", rc);
1013 /* Free the (early) reply state from lustre_pack_reply.
1014 (ptlrpc_send_reply takes it's own rs ref, so this is safe here) */
1015 ptlrpc_req_drop_rs(reqcopy);
1018 class_export_rpc_put(reqcopy->rq_export);
1019 class_export_put(reqcopy->rq_export);
1021 sptlrpc_svc_ctx_decref(reqcopy);
1022 OBD_FREE(reqmsg, req->rq_reqlen);
1023 OBD_FREE(reqcopy, sizeof *reqcopy);
1027 /* Send early replies to everybody expiring within at_early_margin
1028 asking for at_extra time */
1029 static int ptlrpc_at_check_timed(struct ptlrpc_service *svc)
1031 struct ptlrpc_request *rq, *n;
1032 struct list_head work_list;
1033 struct ptlrpc_at_array *array = &svc->srv_at_array;
1036 time_t now = cfs_time_current_sec();
1037 cfs_duration_t delay;
1038 int first, counter = 0;
1041 spin_lock(&svc->srv_at_lock);
1042 if (svc->srv_at_check == 0) {
1043 spin_unlock(&svc->srv_at_lock);
1046 delay = cfs_time_sub(cfs_time_current(), svc->srv_at_checktime);
1047 svc->srv_at_check = 0;
1049 if (array->paa_count == 0) {
1050 spin_unlock(&svc->srv_at_lock);
1054 /* The timer went off, but maybe the nearest rpc already completed. */
1055 first = array->paa_deadline - now;
1056 if (first > at_early_margin) {
1057 /* We've still got plenty of time. Reset the timer. */
1058 spin_unlock(&svc->srv_at_lock);
1059 ptlrpc_at_set_timer(svc);
1063 /* We're close to a timeout, and we don't know how much longer the
1064 server will take. Send early replies to everyone expiring soon. */
1065 CFS_INIT_LIST_HEAD(&work_list);
1067 index = (unsigned long)array->paa_deadline % array->paa_size;
1068 count = array->paa_count;
1070 count -= array->paa_reqs_count[index];
1071 list_for_each_entry_safe(rq, n, &array->paa_reqs_array[index],
1073 if (rq->rq_deadline <= now + at_early_margin) {
1074 list_del_init(&rq->rq_timed_list);
1076 * ptlrpc_server_drop_request() may drop
1077 * refcount to 0 already. Let's check this and
1078 * don't add entry to work_list
1080 if (likely(atomic_inc_not_zero(&rq->rq_refcount)))
1081 list_add(&rq->rq_timed_list, &work_list);
1083 array->paa_reqs_count[index]--;
1085 rq->rq_at_linked = 0;
1089 /* update the earliest deadline */
1090 if (deadline == -1 || rq->rq_deadline < deadline)
1091 deadline = rq->rq_deadline;
1096 if (++index >= array->paa_size)
1099 array->paa_deadline = deadline;
1100 spin_unlock(&svc->srv_at_lock);
1102 /* we have a new earliest deadline, restart the timer */
1103 ptlrpc_at_set_timer(svc);
1105 CDEBUG(D_ADAPTTO, "timeout in %+ds, asking for %d secs on %d early "
1106 "replies\n", first, at_extra, counter);
1108 /* We're already past request deadlines before we even get a
1109 chance to send early replies */
1110 LCONSOLE_WARN("%s: This server is not able to keep up with "
1111 "request traffic (cpu-bound).\n", svc->srv_name);
1112 CWARN("earlyQ=%d reqQ=%d recA=%d, svcEst=%d, "
1113 "delay="CFS_DURATION_T"(jiff)\n",
1114 counter, svc->srv_n_queued_reqs, svc->srv_n_active_reqs,
1115 at_get(&svc->srv_at_estimate), delay);
1118 /* we took additional refcount so entries can't be deleted from list, no
1119 * locking is needed */
1120 while (!list_empty(&work_list)) {
1121 rq = list_entry(work_list.next, struct ptlrpc_request,
1123 list_del_init(&rq->rq_timed_list);
1125 if (ptlrpc_at_send_early_reply(rq) == 0)
1126 ptlrpc_at_add_timed(rq);
1128 ptlrpc_server_drop_request(rq);
1135 * Put the request to the export list if the request may become
1136 * a high priority one.
1138 static int ptlrpc_hpreq_init(struct ptlrpc_service *svc,
1139 struct ptlrpc_request *req)
1144 if (svc->srv_hpreq_handler) {
1145 rc = svc->srv_hpreq_handler(req);
1149 if (req->rq_export && req->rq_ops) {
1150 spin_lock(&req->rq_export->exp_lock);
1151 list_add(&req->rq_exp_list, &req->rq_export->exp_queued_rpc);
1152 spin_unlock(&req->rq_export->exp_lock);
1158 /** Remove the request from the export list. */
1159 static void ptlrpc_hpreq_fini(struct ptlrpc_request *req)
1162 if (req->rq_export && req->rq_ops) {
1163 spin_lock(&req->rq_export->exp_lock);
1164 list_del_init(&req->rq_exp_list);
1165 spin_unlock(&req->rq_export->exp_lock);
1171 * Make the request a high priority one.
1173 * All the high priority requests are queued in a separate FIFO
1174 * ptlrpc_service::srv_request_hpq list which is parallel to
1175 * ptlrpc_service::srv_request_queue list but has a higher priority
1178 * \see ptlrpc_server_handle_request().
1180 static void ptlrpc_hpreq_reorder_nolock(struct ptlrpc_service *svc,
1181 struct ptlrpc_request *req)
1184 LASSERT(svc != NULL);
1185 spin_lock(&req->rq_lock);
1186 if (req->rq_hp == 0) {
1187 int opc = lustre_msg_get_opc(req->rq_reqmsg);
1189 /* Add to the high priority queue. */
1190 list_move_tail(&req->rq_list, &svc->srv_request_hpq);
1192 if (opc != OBD_PING)
1193 DEBUG_REQ(D_NET, req, "high priority req");
1195 spin_unlock(&req->rq_lock);
1199 void ptlrpc_hpreq_reorder(struct ptlrpc_request *req)
1201 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
1204 spin_lock(&svc->srv_lock);
1205 /* It may happen that the request is already taken for the processing
1206 * but still in the export list, do not re-add it into the HP list. */
1207 if (req->rq_phase == RQ_PHASE_NEW)
1208 ptlrpc_hpreq_reorder_nolock(svc, req);
1209 spin_unlock(&svc->srv_lock);
1213 /** Check if the request is a high priority one. */
1214 static int ptlrpc_server_hpreq_check(struct ptlrpc_request *req)
1219 /* Check by request opc. */
1220 opc = lustre_msg_get_opc(req->rq_reqmsg);
1221 if (opc == OBD_PING)
1224 /* Perform request specific check. */
1225 if (req->rq_ops && req->rq_ops->hpreq_check)
1226 rc = req->rq_ops->hpreq_check(req);
1230 /** Check if a request is a high priority one. */
1231 static int ptlrpc_server_request_add(struct ptlrpc_service *svc,
1232 struct ptlrpc_request *req)
1237 rc = ptlrpc_server_hpreq_check(req);
1241 spin_lock(&svc->srv_lock);
1242 /* Before inserting the request into the queue, check if it is not
1243 * inserted yet, or even already handled -- it may happen due to
1244 * a racing ldlm_server_blocking_ast(). */
1245 if (req->rq_phase == RQ_PHASE_NEW && list_empty(&req->rq_list)) {
1247 ptlrpc_hpreq_reorder_nolock(svc, req);
1249 list_add_tail(&req->rq_list, &svc->srv_request_queue);
1251 spin_unlock(&svc->srv_lock);
1256 /* Only allow normal priority requests on a service that has a high-priority
1257 * queue if forced (i.e. cleanup), if there are other high priority requests
1258 * already being processed (i.e. those threads can service more high-priority
1259 * requests), or if there are enough idle threads that a later thread can do
1260 * a high priority request. */
1261 static int ptlrpc_server_allow_normal(struct ptlrpc_service *svc, int force)
1263 return force || !svc->srv_hpreq_handler || svc->srv_n_hpreq > 0 ||
1264 svc->srv_n_active_reqs < svc->srv_threads_running - 2;
1267 static struct ptlrpc_request *
1268 ptlrpc_server_request_get(struct ptlrpc_service *svc, int force)
1270 struct ptlrpc_request *req = NULL;
1273 if (ptlrpc_server_allow_normal(svc, force) &&
1274 !list_empty(&svc->srv_request_queue) &&
1275 (list_empty(&svc->srv_request_hpq) ||
1276 svc->srv_hpreq_count >= svc->srv_hpreq_ratio)) {
1277 req = list_entry(svc->srv_request_queue.next,
1278 struct ptlrpc_request, rq_list);
1279 svc->srv_hpreq_count = 0;
1280 } else if (!list_empty(&svc->srv_request_hpq)) {
1281 req = list_entry(svc->srv_request_hpq.next,
1282 struct ptlrpc_request, rq_list);
1283 svc->srv_hpreq_count++;
1288 static int ptlrpc_server_request_pending(struct ptlrpc_service *svc, int force)
1290 return ((ptlrpc_server_allow_normal(svc, force) &&
1291 !list_empty(&svc->srv_request_queue)) ||
1292 !list_empty(&svc->srv_request_hpq));
1295 /* Handle freshly incoming reqs, add to timed early reply list,
1296 pass on to regular request queue */
1298 ptlrpc_server_handle_req_in(struct ptlrpc_service *svc)
1300 struct ptlrpc_request *req;
1307 spin_lock(&svc->srv_lock);
1308 if (list_empty(&svc->srv_req_in_queue)) {
1309 spin_unlock(&svc->srv_lock);
1313 req = list_entry(svc->srv_req_in_queue.next,
1314 struct ptlrpc_request, rq_list);
1315 list_del_init (&req->rq_list);
1316 /* Consider this still a "queued" request as far as stats are
1318 spin_unlock(&svc->srv_lock);
1320 /* go through security check/transform */
1321 rc = sptlrpc_svc_unwrap_request(req);
1325 case SECSVC_COMPLETE:
1326 target_send_reply(req, 0, OBD_FAIL_MDS_ALL_REPLY_NET);
1335 * for null-flavored rpc, msg has been unpacked by sptlrpc, although
1336 * redo it wouldn't be harmful.
1338 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) {
1339 rc = ptlrpc_unpack_req_msg(req, req->rq_reqlen);
1341 CERROR("error unpacking request: ptl %d from %s "
1342 "x"LPU64"\n", svc->srv_req_portal,
1343 libcfs_id2str(req->rq_peer), req->rq_xid);
1348 rc = lustre_unpack_req_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
1350 CERROR ("error unpacking ptlrpc body: ptl %d from %s x"
1351 LPU64"\n", svc->srv_req_portal,
1352 libcfs_id2str(req->rq_peer), req->rq_xid);
1356 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_REQ_OPC) &&
1357 lustre_msg_get_opc(req->rq_reqmsg) == obd_fail_val) {
1358 CERROR("drop incoming rpc opc %u, x"LPU64"\n",
1359 obd_fail_val, req->rq_xid);
1364 if (lustre_msg_get_type(req->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
1365 CERROR("wrong packet type received (type=%u) from %s\n",
1366 lustre_msg_get_type(req->rq_reqmsg),
1367 libcfs_id2str(req->rq_peer));
1371 switch(lustre_msg_get_opc(req->rq_reqmsg)) {
1374 req->rq_bulk_write = 1;
1378 req->rq_bulk_read = 1;
1382 CDEBUG(D_NET, "got req "LPU64"\n", req->rq_xid);
1384 req->rq_export = class_conn2export(
1385 lustre_msg_get_handle(req->rq_reqmsg));
1386 if (req->rq_export) {
1387 rc = ptlrpc_check_req(req);
1389 rc = sptlrpc_target_export_check(req->rq_export, req);
1391 DEBUG_REQ(D_ERROR, req, "DROPPING req with "
1392 "illegal security flavor,");
1397 ptlrpc_update_export_timer(req->rq_export, 0);
1400 /* req_in handling should/must be fast */
1401 if (cfs_time_current_sec() - req->rq_arrival_time.tv_sec > 5)
1402 DEBUG_REQ(D_WARNING, req, "Slow req_in handling "CFS_DURATION_T"s",
1403 cfs_time_sub(cfs_time_current_sec(),
1404 req->rq_arrival_time.tv_sec));
1406 /* Set rpc server deadline and add it to the timed list */
1407 deadline = (lustre_msghdr_get_flags(req->rq_reqmsg) &
1408 MSGHDR_AT_SUPPORT) ?
1409 /* The max time the client expects us to take */
1410 lustre_msg_get_timeout(req->rq_reqmsg) : obd_timeout;
1411 req->rq_deadline = req->rq_arrival_time.tv_sec + deadline;
1412 if (unlikely(deadline == 0)) {
1413 DEBUG_REQ(D_ERROR, req, "Dropping request with 0 timeout");
1417 ptlrpc_at_add_timed(req);
1418 rc = ptlrpc_hpreq_init(svc, req);
1422 /* Move it over to the request processing queue */
1423 rc = ptlrpc_server_request_add(svc, req);
1426 cfs_waitq_signal(&svc->srv_waitq);
1430 spin_lock(&svc->srv_lock);
1431 svc->srv_n_queued_reqs--;
1432 svc->srv_n_active_reqs++;
1433 spin_unlock(&svc->srv_lock);
1434 ptlrpc_server_finish_request(req);
1440 ptlrpc_server_handle_request(struct ptlrpc_service *svc,
1441 struct ptlrpc_thread *thread)
1443 struct obd_export *export = NULL;
1444 struct ptlrpc_request *request;
1445 struct timeval work_start;
1446 struct timeval work_end;
1454 spin_lock(&svc->srv_lock);
1455 if (unlikely(!ptlrpc_server_request_pending(svc, 0) ||
1458 /* !@%$# liblustre only has 1 thread */
1459 atomic_read(&svc->srv_n_difficult_replies) != 0 &&
1461 svc->srv_n_active_reqs >= (svc->srv_threads_running - 1)))) {
1462 /* Don't handle regular requests in the last thread, in order * re
1463 * to handle difficult replies (which might block other threads)
1464 * as well as handle any incoming reqs, early replies, etc.
1465 * That means we always need at least 2 service threads. */
1466 spin_unlock(&svc->srv_lock);
1470 request = ptlrpc_server_request_get(svc, 0);
1471 if (request == NULL) {
1472 spin_unlock(&svc->srv_lock);
1476 opc = lustre_msg_get_opc(request->rq_reqmsg);
1477 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT))
1478 fail_opc = OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT;
1479 else if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
1480 fail_opc = OBD_FAIL_PTLRPC_HPREQ_TIMEOUT;
1482 if (unlikely(fail_opc)) {
1483 if (request->rq_export && request->rq_ops) {
1484 spin_unlock(&svc->srv_lock);
1485 OBD_FAIL_TIMEOUT(fail_opc, 4);
1486 spin_lock(&svc->srv_lock);
1487 request = ptlrpc_server_request_get(svc, 0);
1488 if (request == NULL) {
1489 spin_unlock(&svc->srv_lock);
1492 LASSERT(ptlrpc_server_request_pending(svc, 0));
1496 list_del_init(&request->rq_list);
1497 svc->srv_n_queued_reqs--;
1498 svc->srv_n_active_reqs++;
1502 /* The phase is changed under the lock here because we need to know
1503 * the request is under processing (see ptlrpc_hpreq_reorder()). */
1504 ptlrpc_rqphase_move(request, RQ_PHASE_INTERPRET);
1505 spin_unlock(&svc->srv_lock);
1507 ptlrpc_hpreq_fini(request);
1509 if(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DUMP_LOG))
1510 libcfs_debug_dumplog();
1512 do_gettimeofday(&work_start);
1513 timediff = cfs_timeval_sub(&work_start, &request->rq_arrival_time,NULL);
1514 if (likely(svc->srv_stats != NULL)) {
1515 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
1517 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
1518 svc->srv_n_queued_reqs);
1519 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
1520 svc->srv_n_active_reqs);
1521 lprocfs_counter_add(svc->srv_stats, PTLRPC_TIMEOUT,
1522 at_get(&svc->srv_at_estimate));
1525 rc = lu_context_init(&request->rq_session,
1526 LCT_SESSION|LCT_REMEMBER|LCT_NOREF);
1528 CERROR("Failure to initialize session: %d\n", rc);
1531 request->rq_session.lc_thread = thread;
1532 request->rq_session.lc_cookie = 0x5;
1533 lu_context_enter(&request->rq_session);
1535 CDEBUG(D_NET, "got req "LPU64"\n", request->rq_xid);
1537 request->rq_svc_thread = thread;
1539 request->rq_svc_thread->t_env->le_ses = &request->rq_session;
1541 if (likely(request->rq_export)) {
1542 if (unlikely(ptlrpc_check_req(request)))
1544 ptlrpc_update_export_timer(request->rq_export, timediff >> 19);
1545 export = class_export_rpc_get(request->rq_export);
1548 /* Discard requests queued for longer than the deadline.
1549 The deadline is increased if we send an early reply. */
1550 if (cfs_time_current_sec() > request->rq_deadline) {
1551 DEBUG_REQ(D_ERROR, request, "Dropping timed-out request from %s"
1552 ": deadline "CFS_DURATION_T":"CFS_DURATION_T"s ago\n",
1553 libcfs_id2str(request->rq_peer),
1554 cfs_time_sub(request->rq_deadline,
1555 request->rq_arrival_time.tv_sec),
1556 cfs_time_sub(cfs_time_current_sec(),
1557 request->rq_deadline));
1558 goto put_rpc_export;
1561 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc "
1562 "%s:%s+%d:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1563 (request->rq_export ?
1564 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1565 (request->rq_export ?
1566 atomic_read(&request->rq_export->exp_refcount) : -99),
1567 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
1568 libcfs_id2str(request->rq_peer),
1569 lustre_msg_get_opc(request->rq_reqmsg));
1571 if (lustre_msg_get_opc(request->rq_reqmsg) != OBD_PING)
1572 OBD_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_PAUSE_REQ, obd_fail_val);
1574 rc = svc->srv_handler(request);
1576 ptlrpc_rqphase_move(request, RQ_PHASE_COMPLETE);
1580 class_export_rpc_put(export);
1582 lu_context_exit(&request->rq_session);
1583 lu_context_fini(&request->rq_session);
1585 if (unlikely(cfs_time_current_sec() > request->rq_deadline)) {
1586 DEBUG_REQ(D_WARNING, request, "Request x"LPU64" took longer "
1587 "than estimated ("CFS_DURATION_T":"CFS_DURATION_T"s);"
1588 " client may timeout.",
1589 request->rq_xid, cfs_time_sub(request->rq_deadline,
1590 request->rq_arrival_time.tv_sec),
1591 cfs_time_sub(cfs_time_current_sec(),
1592 request->rq_deadline));
1595 do_gettimeofday(&work_end);
1596 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1597 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc "
1598 "%s:%s+%d:%d:x"LPU64":%s:%d Request procesed in "
1599 "%lds (%lds total) trans "LPU64" rc %d/%d\n",
1601 (request->rq_export ?
1602 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1603 (request->rq_export ?
1604 atomic_read(&request->rq_export->exp_refcount) : -99),
1605 lustre_msg_get_status(request->rq_reqmsg),
1607 libcfs_id2str(request->rq_peer),
1608 lustre_msg_get_opc(request->rq_reqmsg),
1610 cfs_timeval_sub(&work_end, &request->rq_arrival_time, NULL),
1611 (request->rq_repmsg ?
1612 lustre_msg_get_transno(request->rq_repmsg) :
1613 request->rq_transno),
1615 (request->rq_repmsg ?
1616 lustre_msg_get_status(request->rq_repmsg) : -999));
1617 if (likely(svc->srv_stats != NULL && request->rq_reqmsg != NULL)) {
1618 __u32 op = lustre_msg_get_opc(request->rq_reqmsg);
1619 int opc = opcode_offset(op);
1620 if (opc > 0 && !(op == LDLM_ENQUEUE || op == MDS_REINT)) {
1621 LASSERT(opc < LUSTRE_MAX_OPCODES);
1622 lprocfs_counter_add(svc->srv_stats,
1623 opc + EXTRA_MAX_OPCODES,
1627 if (unlikely(request->rq_early_count)) {
1628 DEBUG_REQ(D_ADAPTTO, request,
1629 "sent %d early replies before finishing in "
1631 request->rq_early_count,
1632 cfs_time_sub(work_end.tv_sec,
1633 request->rq_arrival_time.tv_sec));
1637 spin_lock(&svc->srv_lock);
1640 spin_unlock(&svc->srv_lock);
1641 ptlrpc_server_finish_request(request);
1647 * An internal function to process a single reply state object.
1650 ptlrpc_handle_rs (struct ptlrpc_reply_state *rs)
1652 struct ptlrpc_service *svc = rs->rs_service;
1653 struct obd_export *exp;
1654 struct obd_device *obd;
1659 exp = rs->rs_export;
1662 LASSERT (rs->rs_difficult);
1663 LASSERT (rs->rs_scheduled);
1664 LASSERT (list_empty(&rs->rs_list));
1666 spin_lock (&exp->exp_lock);
1667 /* Noop if removed already */
1668 list_del_init (&rs->rs_exp_list);
1669 spin_unlock (&exp->exp_lock);
1671 /* The disk commit callback holds exp_uncommitted_replies_lock while it
1672 * iterates over newly committed replies, removing them from
1673 * exp_uncommitted_replies. It then drops this lock and schedules the
1674 * replies it found for handling here.
1676 * We can avoid contention for exp_uncommitted_replies_lock between the
1677 * HRT threads and further commit callbacks by checking rs_committed
1678 * which is set in the commit callback while it holds both
1679 * rs_lock and exp_uncommitted_reples.
1681 * If we see rs_committed clear, the commit callback _may_ not have
1682 * handled this reply yet and we race with it to grab
1683 * exp_uncommitted_replies_lock before removing the reply from
1684 * exp_uncommitted_replies. Note that if we lose the race and the
1685 * reply has already been removed, list_del_init() is a noop.
1687 * If we see rs_committed set, we know the commit callback is handling,
1688 * or has handled this reply since store reordering might allow us to
1689 * see rs_committed set out of sequence. But since this is done
1690 * holding rs_lock, we can be sure it has all completed once we hold
1691 * rs_lock, which we do right next.
1693 if (!rs->rs_committed) {
1694 spin_lock(&exp->exp_uncommitted_replies_lock);
1695 list_del_init(&rs->rs_obd_list);
1696 spin_unlock(&exp->exp_uncommitted_replies_lock);
1699 spin_lock(&rs->rs_lock);
1701 been_handled = rs->rs_handled;
1704 nlocks = rs->rs_nlocks; /* atomic "steal", but */
1705 rs->rs_nlocks = 0; /* locks still on rs_locks! */
1707 if (nlocks == 0 && !been_handled) {
1708 /* If we see this, we should already have seen the warning
1709 * in mds_steal_ack_locks() */
1710 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
1713 rs->rs_xid, rs->rs_transno, rs->rs_opc,
1714 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1717 if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
1718 spin_unlock(&rs->rs_lock);
1720 if (!been_handled && rs->rs_on_net) {
1721 LNetMDUnlink(rs->rs_md_h);
1722 /* Ignore return code; we're racing with
1726 while (nlocks-- > 0)
1727 ldlm_lock_decref(&rs->rs_locks[nlocks],
1728 rs->rs_modes[nlocks]);
1730 spin_lock(&rs->rs_lock);
1733 rs->rs_scheduled = 0;
1735 if (!rs->rs_on_net) {
1737 spin_unlock(&rs->rs_lock);
1739 class_export_put (exp);
1740 rs->rs_export = NULL;
1741 ptlrpc_rs_decref (rs);
1742 atomic_dec (&svc->srv_outstanding_replies);
1743 if (atomic_dec_and_test(&svc->srv_n_difficult_replies) &&
1744 svc->srv_is_stopping)
1745 cfs_waitq_broadcast(&svc->srv_waitq);
1749 /* still on the net; callback will schedule */
1750 spin_unlock(&rs->rs_lock);
1757 * Check whether given service has a reply available for processing
1760 * \param svc a ptlrpc service
1761 * \retval 0 no replies processed
1762 * \retval 1 one reply processed
1765 ptlrpc_server_handle_reply(struct ptlrpc_service *svc)
1767 struct ptlrpc_reply_state *rs = NULL;
1770 spin_lock(&svc->srv_lock);
1771 if (!list_empty(&svc->srv_reply_queue)) {
1772 rs = list_entry(svc->srv_reply_queue.prev,
1773 struct ptlrpc_reply_state,
1775 list_del_init(&rs->rs_list);
1777 spin_unlock(&svc->srv_lock);
1779 ptlrpc_handle_rs(rs);
1783 /* FIXME make use of timeout later */
1785 liblustre_check_services (void *arg)
1787 int did_something = 0;
1789 struct list_head *tmp, *nxt;
1792 /* I'm relying on being single threaded, not to have to lock
1793 * ptlrpc_all_services etc */
1794 list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
1795 struct ptlrpc_service *svc =
1796 list_entry (tmp, struct ptlrpc_service, srv_list);
1798 if (svc->srv_threads_running != 0) /* I've recursed */
1801 /* service threads can block for bulk, so this limits us
1802 * (arbitrarily) to recursing 1 stack frame per service.
1803 * Note that the problem with recursion is that we have to
1804 * unwind completely before our caller can resume. */
1806 svc->srv_threads_running++;
1809 rc = ptlrpc_server_handle_req_in(svc);
1810 rc |= ptlrpc_server_handle_reply(svc);
1811 rc |= ptlrpc_at_check_timed(svc);
1812 rc |= ptlrpc_server_handle_request(svc, NULL);
1813 rc |= (ptlrpc_server_post_idle_rqbds(svc) > 0);
1814 did_something |= rc;
1817 svc->srv_threads_running--;
1820 RETURN(did_something);
1822 #define ptlrpc_stop_all_threads(s) do {} while (0)
1824 #else /* __KERNEL__ */
1827 ptlrpc_check_rqbd_pool(struct ptlrpc_service *svc)
1829 int avail = svc->srv_nrqbd_receiving;
1830 int low_water = test_req_buffer_pressure ? 0 :
1831 svc->srv_nbuf_per_group/2;
1833 /* NB I'm not locking; just looking. */
1835 /* CAVEAT EMPTOR: We might be allocating buffers here because we've
1836 * allowed the request history to grow out of control. We could put a
1837 * sanity check on that here and cull some history if we need the
1840 if (avail <= low_water)
1841 ptlrpc_grow_req_bufs(svc);
1844 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQBUF_AVAIL_CNTR,
1849 ptlrpc_retry_rqbds(void *arg)
1851 struct ptlrpc_service *svc = (struct ptlrpc_service *)arg;
1853 svc->srv_rqbd_timeout = 0;
1854 return (-ETIMEDOUT);
1857 static int ptlrpc_main(void *arg)
1859 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
1860 struct ptlrpc_service *svc = data->svc;
1861 struct ptlrpc_thread *thread = data->thread;
1862 struct obd_device *dev = data->dev;
1863 struct ptlrpc_reply_state *rs;
1864 #ifdef WITH_GROUP_INFO
1865 struct group_info *ginfo = NULL;
1868 int counter = 0, rc = 0;
1871 thread->t_pid = cfs_curproc_pid();
1872 cfs_daemonize_ctxt(data->name);
1874 #if defined(HAVE_NODE_TO_CPUMASK) && defined(CONFIG_NUMA)
1875 /* we need to do this before any per-thread allocation is done so that
1876 * we get the per-thread allocations on local node. bug 7342 */
1877 if (svc->srv_cpu_affinity) {
1880 for (cpu = 0, num_cpu = 0; cpu < num_possible_cpus(); cpu++) {
1881 if (!cpu_online(cpu))
1883 if (num_cpu == thread->t_id % num_online_cpus())
1887 set_cpus_allowed(cfs_current(), node_to_cpumask(cpu_to_node(cpu)));
1891 #ifdef WITH_GROUP_INFO
1892 ginfo = groups_alloc(0);
1898 set_current_groups(ginfo);
1899 put_group_info(ginfo);
1902 if (svc->srv_init != NULL) {
1903 rc = svc->srv_init(thread);
1908 rc = lu_context_init(&env.le_ctx,
1909 svc->srv_ctx_tags|LCT_REMEMBER|LCT_NOREF);
1913 thread->t_env = &env;
1914 env.le_ctx.lc_thread = thread;
1915 env.le_ctx.lc_cookie = 0x6;
1917 /* Alloc reply state structure for this one */
1918 OBD_ALLOC_GFP(rs, svc->srv_max_reply_size, CFS_ALLOC_STD);
1924 spin_lock(&svc->srv_lock);
1925 /* SVC_STOPPING may already be set here if someone else is trying
1926 * to stop the service while this new thread has been dynamically
1927 * forked. We still set SVC_RUNNING to let our creator know that
1928 * we are now running, however we will exit as soon as possible */
1929 thread->t_flags |= SVC_RUNNING;
1930 spin_unlock(&svc->srv_lock);
1933 * wake up our creator. Note: @data is invalid after this point,
1934 * because it's allocated on ptlrpc_start_thread() stack.
1936 cfs_waitq_signal(&thread->t_ctl_waitq);
1938 thread->t_watchdog = lc_watchdog_add(GET_TIMEOUT(svc), NULL, NULL);
1940 spin_lock(&svc->srv_lock);
1941 svc->srv_threads_running++;
1942 list_add(&rs->rs_list, &svc->srv_free_rs_list);
1943 spin_unlock(&svc->srv_lock);
1944 cfs_waitq_signal(&svc->srv_free_rs_waitq);
1946 CDEBUG(D_NET, "service thread %d (#%d) started\n", thread->t_id,
1947 svc->srv_threads_running);
1949 /* XXX maintain a list of all managed devices: insert here */
1951 while (!(thread->t_flags & SVC_STOPPING) && !svc->srv_is_stopping) {
1952 /* Don't exit while there are replies to be handled */
1953 struct l_wait_info lwi = LWI_TIMEOUT(svc->srv_rqbd_timeout,
1954 ptlrpc_retry_rqbds, svc);
1956 lc_watchdog_disable(thread->t_watchdog);
1960 l_wait_event_exclusive (svc->srv_waitq,
1961 thread->t_flags & SVC_STOPPING ||
1962 svc->srv_is_stopping ||
1963 (!list_empty(&svc->srv_idle_rqbds) &&
1964 svc->srv_rqbd_timeout == 0) ||
1965 !list_empty(&svc->srv_req_in_queue) ||
1966 (ptlrpc_server_request_pending(svc, 0) &&
1967 (svc->srv_n_active_reqs <
1968 (svc->srv_threads_running - 1))) ||
1972 if (thread->t_flags & SVC_STOPPING || svc->srv_is_stopping)
1975 lc_watchdog_touch(thread->t_watchdog, GET_TIMEOUT(svc));
1977 ptlrpc_check_rqbd_pool(svc);
1979 if (svc->srv_threads_started < svc->srv_threads_max &&
1980 svc->srv_n_active_reqs >= (svc->srv_threads_started - 1))
1981 /* Ignore return code - we tried... */
1982 ptlrpc_start_thread(dev, svc);
1984 if (!list_empty(&svc->srv_req_in_queue)) {
1985 /* Process all incoming reqs before handling any */
1986 ptlrpc_server_handle_req_in(svc);
1987 /* but limit ourselves in case of flood */
1988 if (counter++ < 1000)
1993 if (svc->srv_at_check)
1994 ptlrpc_at_check_timed(svc);
1996 /* don't handle requests in the last thread */
1997 if (ptlrpc_server_request_pending(svc, 0) &&
1998 (svc->srv_n_active_reqs < (svc->srv_threads_running - 1))) {
1999 lu_context_enter(&env.le_ctx);
2000 ptlrpc_server_handle_request(svc, thread);
2001 lu_context_exit(&env.le_ctx);
2004 if (!list_empty(&svc->srv_idle_rqbds) &&
2005 ptlrpc_server_post_idle_rqbds(svc) < 0) {
2006 /* I just failed to repost request buffers. Wait
2007 * for a timeout (unless something else happens)
2008 * before I try again */
2009 svc->srv_rqbd_timeout = cfs_time_seconds(1)/10;
2010 CDEBUG(D_RPCTRACE,"Posted buffers: %d\n",
2011 svc->srv_nrqbd_receiving);
2015 lc_watchdog_delete(thread->t_watchdog);
2016 thread->t_watchdog = NULL;
2020 * deconstruct service specific state created by ptlrpc_start_thread()
2022 if (svc->srv_done != NULL)
2023 svc->srv_done(thread);
2025 lu_context_fini(&env.le_ctx);
2027 CDEBUG(D_RPCTRACE, "service thread [ %p : %u ] %d exiting: rc %d\n",
2028 thread, thread->t_pid, thread->t_id, rc);
2030 spin_lock(&svc->srv_lock);
2031 svc->srv_threads_running--; /* must know immediately */
2033 thread->t_flags = SVC_STOPPED;
2035 cfs_waitq_signal(&thread->t_ctl_waitq);
2036 spin_unlock(&svc->srv_lock);
2041 struct ptlrpc_hr_args {
2044 struct ptlrpc_hr_service *hrs;
2047 static int hrt_dont_sleep(struct ptlrpc_hr_thread *t,
2048 struct list_head *replies)
2052 spin_lock(&t->hrt_lock);
2053 list_splice_init(&t->hrt_queue, replies);
2054 result = test_bit(HRT_STOPPING, &t->hrt_flags) ||
2055 !list_empty(replies);
2056 spin_unlock(&t->hrt_lock);
2060 static int ptlrpc_hr_main(void *arg)
2062 struct ptlrpc_hr_args * hr_args = arg;
2063 struct ptlrpc_hr_service *hr = hr_args->hrs;
2064 struct ptlrpc_hr_thread *t = &hr->hr_threads[hr_args->thread_index];
2065 char threadname[20];
2066 CFS_LIST_HEAD(replies);
2068 snprintf(threadname, sizeof(threadname),
2069 "ptlrpc_hr_%d", hr_args->thread_index);
2071 cfs_daemonize_ctxt(threadname);
2072 #if defined(CONFIG_SMP) && defined(HAVE_NODE_TO_CPUMASK)
2073 set_cpus_allowed(cfs_current(),
2074 node_to_cpumask(cpu_to_node(hr_args->cpu_index)));
2076 set_bit(HRT_RUNNING, &t->hrt_flags);
2077 cfs_waitq_signal(&t->hrt_wait);
2079 while (!test_bit(HRT_STOPPING, &t->hrt_flags)) {
2081 cfs_wait_event(t->hrt_wait, hrt_dont_sleep(t, &replies));
2082 while (!list_empty(&replies)) {
2083 struct ptlrpc_reply_state *rs;
2085 rs = list_entry(replies.prev,
2086 struct ptlrpc_reply_state,
2088 list_del_init(&rs->rs_list);
2089 ptlrpc_handle_rs(rs);
2093 clear_bit(HRT_RUNNING, &t->hrt_flags);
2094 complete(&t->hrt_completion);
2099 static int ptlrpc_start_hr_thread(struct ptlrpc_hr_service *hr, int n, int cpu)
2101 struct ptlrpc_hr_thread *t = &hr->hr_threads[n];
2102 struct ptlrpc_hr_args args;
2106 args.thread_index = n;
2107 args.cpu_index = cpu;
2110 rc = cfs_kernel_thread(ptlrpc_hr_main, (void*)&args,
2111 CLONE_VM|CLONE_FILES);
2113 complete(&t->hrt_completion);
2116 cfs_wait_event(t->hrt_wait, test_bit(HRT_RUNNING, &t->hrt_flags));
2122 static void ptlrpc_stop_hr_thread(struct ptlrpc_hr_thread *t)
2126 set_bit(HRT_STOPPING, &t->hrt_flags);
2127 cfs_waitq_signal(&t->hrt_wait);
2128 wait_for_completion(&t->hrt_completion);
2133 static void ptlrpc_stop_hr_threads(struct ptlrpc_hr_service *hrs)
2138 for (n = 0; n < hrs->hr_n_threads; n++)
2139 ptlrpc_stop_hr_thread(&hrs->hr_threads[n]);
2144 static int ptlrpc_start_hr_threads(struct ptlrpc_hr_service *hr)
2147 int n, cpu, threads_started = 0;
2150 LASSERT(hr != NULL);
2151 LASSERT(hr->hr_n_threads > 0);
2153 for (n = 0, cpu = 0; n < hr->hr_n_threads; n++) {
2154 #if defined(CONFIG_SMP) && defined(HAVE_NODE_TO_CPUMASK)
2155 while(!cpu_online(cpu)) {
2157 if (cpu >= num_possible_cpus())
2161 rc = ptlrpc_start_hr_thread(hr, n, cpu);
2167 if (threads_started == 0) {
2168 CERROR("No reply handling threads started\n");
2171 if (threads_started < hr->hr_n_threads) {
2172 CWARN("Started only %d reply handling threads from %d\n",
2173 threads_started, hr->hr_n_threads);
2174 hr->hr_n_threads = threads_started;
2179 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
2180 struct ptlrpc_thread *thread)
2182 struct l_wait_info lwi = { 0 };
2185 CDEBUG(D_RPCTRACE, "Stopping thread [ %p : %u ]\n",
2186 thread, thread->t_pid);
2188 spin_lock(&svc->srv_lock);
2189 /* let the thread know that we would like it to stop asap */
2190 thread->t_flags |= SVC_STOPPING;
2191 spin_unlock(&svc->srv_lock);
2193 cfs_waitq_broadcast(&svc->srv_waitq);
2194 l_wait_event(thread->t_ctl_waitq,
2195 (thread->t_flags & SVC_STOPPED), &lwi);
2197 spin_lock(&svc->srv_lock);
2198 list_del(&thread->t_link);
2199 spin_unlock(&svc->srv_lock);
2201 OBD_FREE_PTR(thread);
2205 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
2207 struct ptlrpc_thread *thread;
2210 spin_lock(&svc->srv_lock);
2211 while (!list_empty(&svc->srv_threads)) {
2212 thread = list_entry(svc->srv_threads.next,
2213 struct ptlrpc_thread, t_link);
2215 spin_unlock(&svc->srv_lock);
2216 ptlrpc_stop_thread(svc, thread);
2217 spin_lock(&svc->srv_lock);
2220 spin_unlock(&svc->srv_lock);
2224 int ptlrpc_start_threads(struct obd_device *dev, struct ptlrpc_service *svc)
2229 /* We require 2 threads min - see note in
2230 ptlrpc_server_handle_request */
2231 LASSERT(svc->srv_threads_min >= 2);
2232 for (i = 0; i < svc->srv_threads_min; i++) {
2233 rc = ptlrpc_start_thread(dev, svc);
2234 /* We have enough threads, don't start more. b=15759 */
2238 CERROR("cannot start %s thread #%d: rc %d\n",
2239 svc->srv_thread_name, i, rc);
2240 ptlrpc_stop_all_threads(svc);
2247 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc)
2249 struct l_wait_info lwi = { 0 };
2250 struct ptlrpc_svc_data d;
2251 struct ptlrpc_thread *thread;
2256 CDEBUG(D_RPCTRACE, "%s started %d min %d max %d running %d\n",
2257 svc->srv_name, svc->srv_threads_started, svc->srv_threads_min,
2258 svc->srv_threads_max, svc->srv_threads_running);
2260 if (unlikely(svc->srv_is_stopping))
2263 if (unlikely(svc->srv_threads_started >= svc->srv_threads_max) ||
2264 (OBD_FAIL_CHECK(OBD_FAIL_TGT_TOOMANY_THREADS) &&
2265 svc->srv_threads_started == svc->srv_threads_min - 1))
2268 OBD_ALLOC_PTR(thread);
2271 cfs_waitq_init(&thread->t_ctl_waitq);
2273 spin_lock(&svc->srv_lock);
2274 if (svc->srv_threads_started >= svc->srv_threads_max) {
2275 spin_unlock(&svc->srv_lock);
2276 OBD_FREE_PTR(thread);
2279 list_add(&thread->t_link, &svc->srv_threads);
2280 id = svc->srv_threads_started++;
2281 spin_unlock(&svc->srv_lock);
2283 thread->t_svc = svc;
2285 sprintf(name, "%s_%02d", svc->srv_thread_name, id);
2291 CDEBUG(D_RPCTRACE, "starting thread '%s'\n", name);
2293 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
2294 * just drop the VM and FILES in cfs_daemonize_ctxt() right away.
2296 rc = cfs_kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
2298 CERROR("cannot start thread '%s': rc %d\n", name, rc);
2300 spin_lock(&svc->srv_lock);
2301 list_del(&thread->t_link);
2302 --svc->srv_threads_started;
2303 spin_unlock(&svc->srv_lock);
2305 OBD_FREE(thread, sizeof(*thread));
2308 l_wait_event(thread->t_ctl_waitq,
2309 thread->t_flags & (SVC_RUNNING | SVC_STOPPED), &lwi);
2311 rc = (thread->t_flags & SVC_STOPPED) ? thread->t_id : 0;
2316 int ptlrpc_hr_init(void)
2319 int n_cpus = num_online_cpus();
2320 struct ptlrpc_hr_service *hr;
2325 LASSERT(ptlrpc_hr == NULL);
2327 size = offsetof(struct ptlrpc_hr_service, hr_threads[n_cpus]);
2328 OBD_ALLOC(hr, size);
2331 for (i = 0; i < n_cpus; i++) {
2332 struct ptlrpc_hr_thread *t = &hr->hr_threads[i];
2334 spin_lock_init(&t->hrt_lock);
2335 cfs_waitq_init(&t->hrt_wait);
2336 CFS_INIT_LIST_HEAD(&t->hrt_queue);
2337 init_completion(&t->hrt_completion);
2339 hr->hr_n_threads = n_cpus;
2343 rc = ptlrpc_start_hr_threads(hr);
2345 OBD_FREE(hr, hr->hr_size);
2351 void ptlrpc_hr_fini(void)
2353 if (ptlrpc_hr != NULL) {
2354 ptlrpc_stop_hr_threads(ptlrpc_hr);
2355 OBD_FREE(ptlrpc_hr, ptlrpc_hr->hr_size);
2360 #endif /* __KERNEL__ */
2363 * Wait until all already scheduled replies are processed.
2365 static void ptlrpc_wait_replies(struct ptlrpc_service *svc)
2369 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10),
2371 rc = l_wait_event(svc->srv_waitq,
2372 atomic_read(&svc->srv_n_difficult_replies) == 0,
2376 CWARN("Unexpectedly long timeout %p\n", svc);
2380 int ptlrpc_unregister_service(struct ptlrpc_service *service)
2383 struct l_wait_info lwi;
2384 struct list_head *tmp;
2385 struct ptlrpc_reply_state *rs, *t;
2386 struct ptlrpc_at_array *array = &service->srv_at_array;
2389 service->srv_is_stopping = 1;
2390 cfs_timer_disarm(&service->srv_at_timer);
2392 ptlrpc_stop_all_threads(service);
2393 LASSERT(list_empty(&service->srv_threads));
2395 spin_lock (&ptlrpc_all_services_lock);
2396 list_del_init (&service->srv_list);
2397 spin_unlock (&ptlrpc_all_services_lock);
2399 ptlrpc_lprocfs_unregister_service(service);
2401 /* All history will be culled when the next request buffer is
2403 service->srv_max_history_rqbds = 0;
2405 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
2407 rc = LNetClearLazyPortal(service->srv_req_portal);
2410 /* Unlink all the request buffers. This forces a 'final' event with
2411 * its 'unlink' flag set for each posted rqbd */
2412 list_for_each(tmp, &service->srv_active_rqbds) {
2413 struct ptlrpc_request_buffer_desc *rqbd =
2414 list_entry(tmp, struct ptlrpc_request_buffer_desc,
2417 rc = LNetMDUnlink(rqbd->rqbd_md_h);
2418 LASSERT (rc == 0 || rc == -ENOENT);
2421 /* Wait for the network to release any buffers it's currently
2424 spin_lock(&service->srv_lock);
2425 rc = service->srv_nrqbd_receiving;
2426 spin_unlock(&service->srv_lock);
2431 /* Network access will complete in finite time but the HUGE
2432 * timeout lets us CWARN for visibility of sluggish NALs */
2433 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
2434 cfs_time_seconds(1), NULL, NULL);
2435 rc = l_wait_event(service->srv_waitq,
2436 service->srv_nrqbd_receiving == 0,
2438 if (rc == -ETIMEDOUT)
2439 CWARN("Service %s waiting for request buffers\n",
2443 /* schedule all outstanding replies to terminate them */
2444 spin_lock(&service->srv_lock);
2445 while (!list_empty(&service->srv_active_replies)) {
2446 struct ptlrpc_reply_state *rs =
2447 list_entry(service->srv_active_replies.next,
2448 struct ptlrpc_reply_state, rs_list);
2449 spin_lock(&rs->rs_lock);
2450 ptlrpc_schedule_difficult_reply(rs);
2451 spin_unlock(&rs->rs_lock);
2453 spin_unlock(&service->srv_lock);
2455 /* purge the request queue. NB No new replies (rqbds all unlinked)
2456 * and no service threads, so I'm the only thread noodling the
2457 * request queue now */
2458 while (!list_empty(&service->srv_req_in_queue)) {
2459 struct ptlrpc_request *req =
2460 list_entry(service->srv_req_in_queue.next,
2461 struct ptlrpc_request,
2464 list_del(&req->rq_list);
2465 service->srv_n_queued_reqs--;
2466 service->srv_n_active_reqs++;
2467 ptlrpc_server_finish_request(req);
2469 while (ptlrpc_server_request_pending(service, 1)) {
2470 struct ptlrpc_request *req;
2472 req = ptlrpc_server_request_get(service, 1);
2473 list_del(&req->rq_list);
2474 service->srv_n_queued_reqs--;
2475 service->srv_n_active_reqs++;
2476 ptlrpc_hpreq_fini(req);
2477 ptlrpc_server_finish_request(req);
2479 LASSERT(service->srv_n_queued_reqs == 0);
2480 LASSERT(service->srv_n_active_reqs == 0);
2481 LASSERT(service->srv_n_history_rqbds == 0);
2482 LASSERT(list_empty(&service->srv_active_rqbds));
2484 /* Now free all the request buffers since nothing references them
2486 while (!list_empty(&service->srv_idle_rqbds)) {
2487 struct ptlrpc_request_buffer_desc *rqbd =
2488 list_entry(service->srv_idle_rqbds.next,
2489 struct ptlrpc_request_buffer_desc,
2492 ptlrpc_free_rqbd(rqbd);
2495 ptlrpc_wait_replies(service);
2497 list_for_each_entry_safe(rs, t, &service->srv_free_rs_list, rs_list) {
2498 list_del(&rs->rs_list);
2499 OBD_FREE(rs, service->srv_max_reply_size);
2502 /* In case somebody rearmed this in the meantime */
2503 cfs_timer_disarm(&service->srv_at_timer);
2505 if (array->paa_reqs_array != NULL) {
2506 OBD_FREE(array->paa_reqs_array,
2507 sizeof(struct list_head) * array->paa_size);
2508 array->paa_reqs_array = NULL;
2511 if (array->paa_reqs_count != NULL) {
2512 OBD_FREE(array->paa_reqs_count,
2513 sizeof(__u32) * array->paa_size);
2514 array->paa_reqs_count= NULL;
2517 OBD_FREE_PTR(service);
2521 /* Returns 0 if the service is healthy.
2523 * Right now, it just checks to make sure that requests aren't languishing
2524 * in the queue. We'll use this health check to govern whether a node needs
2525 * to be shot, so it's intentionally non-aggressive. */
2526 int ptlrpc_service_health_check(struct ptlrpc_service *svc)
2528 struct ptlrpc_request *request;
2529 struct timeval right_now;
2535 do_gettimeofday(&right_now);
2537 spin_lock(&svc->srv_lock);
2538 if (!ptlrpc_server_request_pending(svc, 1)) {
2539 spin_unlock(&svc->srv_lock);
2543 /* How long has the next entry been waiting? */
2544 if (list_empty(&svc->srv_request_queue))
2545 request = list_entry(svc->srv_request_hpq.next,
2546 struct ptlrpc_request, rq_list);
2548 request = list_entry(svc->srv_request_queue.next,
2549 struct ptlrpc_request, rq_list);
2550 timediff = cfs_timeval_sub(&right_now, &request->rq_arrival_time, NULL);
2551 spin_unlock(&svc->srv_lock);
2553 if ((timediff / ONE_MILLION) > (AT_OFF ? obd_timeout * 3/2 :
2555 CERROR("%s: unhealthy - request has been waiting %lds\n",
2556 svc->srv_name, timediff / ONE_MILLION);