4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
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_part *svcpt);
67 static void ptlrpc_server_hpreq_fini(struct ptlrpc_request *req);
68 static void ptlrpc_at_remove_timed(struct ptlrpc_request *req);
70 static CFS_LIST_HEAD(ptlrpc_all_services);
71 spinlock_t ptlrpc_all_services_lock;
73 struct ptlrpc_request_buffer_desc *
74 ptlrpc_alloc_rqbd(struct ptlrpc_service_part *svcpt)
76 struct ptlrpc_service *svc = svcpt->scp_service;
77 struct ptlrpc_request_buffer_desc *rqbd;
79 OBD_CPT_ALLOC_PTR(rqbd, svc->srv_cptable, svcpt->scp_cpt);
83 rqbd->rqbd_svcpt = svcpt;
84 rqbd->rqbd_refcount = 0;
85 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
86 rqbd->rqbd_cbid.cbid_arg = rqbd;
87 CFS_INIT_LIST_HEAD(&rqbd->rqbd_reqs);
88 OBD_CPT_ALLOC_LARGE(rqbd->rqbd_buffer, svc->srv_cptable,
89 svcpt->scp_cpt, svc->srv_buf_size);
90 if (rqbd->rqbd_buffer == NULL) {
95 spin_lock(&svcpt->scp_lock);
96 cfs_list_add(&rqbd->rqbd_list, &svcpt->scp_rqbd_idle);
97 svcpt->scp_nrqbds_total++;
98 spin_unlock(&svcpt->scp_lock);
104 ptlrpc_free_rqbd(struct ptlrpc_request_buffer_desc *rqbd)
106 struct ptlrpc_service_part *svcpt = rqbd->rqbd_svcpt;
108 LASSERT(rqbd->rqbd_refcount == 0);
109 LASSERT(cfs_list_empty(&rqbd->rqbd_reqs));
111 spin_lock(&svcpt->scp_lock);
112 cfs_list_del(&rqbd->rqbd_list);
113 svcpt->scp_nrqbds_total--;
114 spin_unlock(&svcpt->scp_lock);
116 OBD_FREE_LARGE(rqbd->rqbd_buffer, svcpt->scp_service->srv_buf_size);
121 ptlrpc_grow_req_bufs(struct ptlrpc_service_part *svcpt, int post)
123 struct ptlrpc_service *svc = svcpt->scp_service;
124 struct ptlrpc_request_buffer_desc *rqbd;
128 for (i = 0; i < svc->srv_nbuf_per_group; i++) {
129 /* NB: another thread might be doing this as well, we need to
130 * make sure that it wouldn't over-allocate, see LU-1212. */
131 if (svcpt->scp_nrqbds_posted >= svc->srv_nbuf_per_group)
134 rqbd = ptlrpc_alloc_rqbd(svcpt);
137 CERROR("%s: Can't allocate request buffer\n",
145 "%s: allocate %d new %d-byte reqbufs (%d/%d left), rc = %d\n",
146 svc->srv_name, i, svc->srv_buf_size, svcpt->scp_nrqbds_posted,
147 svcpt->scp_nrqbds_total, rc);
150 rc = ptlrpc_server_post_idle_rqbds(svcpt);
156 * Part of Rep-Ack logic.
157 * Puts a lock and its mode into reply state assotiated to request reply.
160 ptlrpc_save_lock(struct ptlrpc_request *req,
161 struct lustre_handle *lock, int mode, int no_ack)
163 struct ptlrpc_reply_state *rs = req->rq_reply_state;
167 LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
169 if (req->rq_export->exp_disconnected) {
170 ldlm_lock_decref(lock, mode);
172 idx = rs->rs_nlocks++;
173 rs->rs_locks[idx] = *lock;
174 rs->rs_modes[idx] = mode;
175 rs->rs_difficult = 1;
176 rs->rs_no_ack = !!no_ack;
179 EXPORT_SYMBOL(ptlrpc_save_lock);
183 struct ptlrpc_hr_partition;
185 struct ptlrpc_hr_thread {
186 int hrt_id; /* thread ID */
188 cfs_waitq_t hrt_waitq;
189 cfs_list_t hrt_queue; /* RS queue */
190 struct ptlrpc_hr_partition *hrt_partition;
193 struct ptlrpc_hr_partition {
194 /* # of started threads */
195 cfs_atomic_t hrp_nstarted;
196 /* # of stopped threads */
197 cfs_atomic_t hrp_nstopped;
198 /* cpu partition id */
200 /* round-robin rotor for choosing thread */
202 /* total number of threads on this partition */
205 struct ptlrpc_hr_thread *hrp_thrs;
208 #define HRT_RUNNING 0
209 #define HRT_STOPPING 1
211 struct ptlrpc_hr_service {
212 /* CPU partition table, it's just cfs_cpt_table for now */
213 struct cfs_cpt_table *hr_cpt_table;
214 /** controller sleep waitq */
215 cfs_waitq_t hr_waitq;
216 unsigned int hr_stopping;
217 /** roundrobin rotor for non-affinity service */
218 unsigned int hr_rotor;
220 struct ptlrpc_hr_partition **hr_partitions;
224 cfs_list_t rsb_replies;
225 unsigned int rsb_n_replies;
226 struct ptlrpc_service_part *rsb_svcpt;
229 /** reply handling service. */
230 static struct ptlrpc_hr_service ptlrpc_hr;
233 * maximum mumber of replies scheduled in one batch
235 #define MAX_SCHEDULED 256
238 * Initialize a reply batch.
242 static void rs_batch_init(struct rs_batch *b)
244 memset(b, 0, sizeof *b);
245 CFS_INIT_LIST_HEAD(&b->rsb_replies);
249 * Choose an hr thread to dispatch requests to.
251 static struct ptlrpc_hr_thread *
252 ptlrpc_hr_select(struct ptlrpc_service_part *svcpt)
254 struct ptlrpc_hr_partition *hrp;
257 if (svcpt->scp_cpt >= 0 &&
258 svcpt->scp_service->srv_cptable == ptlrpc_hr.hr_cpt_table) {
259 /* directly match partition */
260 hrp = ptlrpc_hr.hr_partitions[svcpt->scp_cpt];
263 rotor = ptlrpc_hr.hr_rotor++;
264 rotor %= cfs_cpt_number(ptlrpc_hr.hr_cpt_table);
266 hrp = ptlrpc_hr.hr_partitions[rotor];
269 rotor = hrp->hrp_rotor++;
270 return &hrp->hrp_thrs[rotor % hrp->hrp_nthrs];
274 * Dispatch all replies accumulated in the batch to one from
275 * dedicated reply handling threads.
279 static void rs_batch_dispatch(struct rs_batch *b)
281 if (b->rsb_n_replies != 0) {
282 struct ptlrpc_hr_thread *hrt;
284 hrt = ptlrpc_hr_select(b->rsb_svcpt);
286 spin_lock(&hrt->hrt_lock);
287 cfs_list_splice_init(&b->rsb_replies, &hrt->hrt_queue);
288 spin_unlock(&hrt->hrt_lock);
290 cfs_waitq_signal(&hrt->hrt_waitq);
291 b->rsb_n_replies = 0;
296 * Add a reply to a batch.
297 * Add one reply object to a batch, schedule batched replies if overload.
302 static void rs_batch_add(struct rs_batch *b, struct ptlrpc_reply_state *rs)
304 struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
306 if (svcpt != b->rsb_svcpt || b->rsb_n_replies >= MAX_SCHEDULED) {
307 if (b->rsb_svcpt != NULL) {
308 rs_batch_dispatch(b);
309 spin_unlock(&b->rsb_svcpt->scp_rep_lock);
311 spin_lock(&svcpt->scp_rep_lock);
312 b->rsb_svcpt = svcpt;
314 spin_lock(&rs->rs_lock);
315 rs->rs_scheduled_ever = 1;
316 if (rs->rs_scheduled == 0) {
317 cfs_list_move(&rs->rs_list, &b->rsb_replies);
318 rs->rs_scheduled = 1;
321 rs->rs_committed = 1;
322 spin_unlock(&rs->rs_lock);
326 * Reply batch finalization.
327 * Dispatch remaining replies from the batch
328 * and release remaining spinlock.
332 static void rs_batch_fini(struct rs_batch *b)
334 if (b->rsb_svcpt != NULL) {
335 rs_batch_dispatch(b);
336 spin_unlock(&b->rsb_svcpt->scp_rep_lock);
340 #define DECLARE_RS_BATCH(b) struct rs_batch b
342 #else /* __KERNEL__ */
344 #define rs_batch_init(b) do{}while(0)
345 #define rs_batch_fini(b) do{}while(0)
346 #define rs_batch_add(b, r) ptlrpc_schedule_difficult_reply(r)
347 #define DECLARE_RS_BATCH(b)
349 #endif /* __KERNEL__ */
352 * Put reply state into a queue for processing because we received
353 * ACK from the client
355 void ptlrpc_dispatch_difficult_reply(struct ptlrpc_reply_state *rs)
358 struct ptlrpc_hr_thread *hrt;
361 LASSERT(cfs_list_empty(&rs->rs_list));
363 hrt = ptlrpc_hr_select(rs->rs_svcpt);
365 spin_lock(&hrt->hrt_lock);
366 cfs_list_add_tail(&rs->rs_list, &hrt->hrt_queue);
367 spin_unlock(&hrt->hrt_lock);
369 cfs_waitq_signal(&hrt->hrt_waitq);
372 cfs_list_add_tail(&rs->rs_list, &rs->rs_svcpt->scp_rep_queue);
377 ptlrpc_schedule_difficult_reply(struct ptlrpc_reply_state *rs)
381 LASSERT_SPIN_LOCKED(&rs->rs_svcpt->scp_rep_lock);
382 LASSERT_SPIN_LOCKED(&rs->rs_lock);
383 LASSERT (rs->rs_difficult);
384 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
386 if (rs->rs_scheduled) { /* being set up or already notified */
391 rs->rs_scheduled = 1;
392 cfs_list_del_init(&rs->rs_list);
393 ptlrpc_dispatch_difficult_reply(rs);
396 EXPORT_SYMBOL(ptlrpc_schedule_difficult_reply);
398 void ptlrpc_commit_replies(struct obd_export *exp)
400 struct ptlrpc_reply_state *rs, *nxt;
401 DECLARE_RS_BATCH(batch);
404 rs_batch_init(&batch);
405 /* Find any replies that have been committed and get their service
406 * to attend to complete them. */
408 /* CAVEAT EMPTOR: spinlock ordering!!! */
409 spin_lock(&exp->exp_uncommitted_replies_lock);
410 cfs_list_for_each_entry_safe(rs, nxt, &exp->exp_uncommitted_replies,
412 LASSERT (rs->rs_difficult);
413 /* VBR: per-export last_committed */
414 LASSERT(rs->rs_export);
415 if (rs->rs_transno <= exp->exp_last_committed) {
416 cfs_list_del_init(&rs->rs_obd_list);
417 rs_batch_add(&batch, rs);
420 spin_unlock(&exp->exp_uncommitted_replies_lock);
421 rs_batch_fini(&batch);
424 EXPORT_SYMBOL(ptlrpc_commit_replies);
427 ptlrpc_server_post_idle_rqbds(struct ptlrpc_service_part *svcpt)
429 struct ptlrpc_request_buffer_desc *rqbd;
434 spin_lock(&svcpt->scp_lock);
436 if (cfs_list_empty(&svcpt->scp_rqbd_idle)) {
437 spin_unlock(&svcpt->scp_lock);
441 rqbd = cfs_list_entry(svcpt->scp_rqbd_idle.next,
442 struct ptlrpc_request_buffer_desc,
444 cfs_list_del(&rqbd->rqbd_list);
446 /* assume we will post successfully */
447 svcpt->scp_nrqbds_posted++;
448 cfs_list_add(&rqbd->rqbd_list, &svcpt->scp_rqbd_posted);
450 spin_unlock(&svcpt->scp_lock);
452 rc = ptlrpc_register_rqbd(rqbd);
459 spin_lock(&svcpt->scp_lock);
461 svcpt->scp_nrqbds_posted--;
462 cfs_list_del(&rqbd->rqbd_list);
463 cfs_list_add_tail(&rqbd->rqbd_list, &svcpt->scp_rqbd_idle);
465 /* Don't complain if no request buffers are posted right now; LNET
466 * won't drop requests because we set the portal lazy! */
468 spin_unlock(&svcpt->scp_lock);
473 static void ptlrpc_at_timer(unsigned long castmeharder)
475 struct ptlrpc_service_part *svcpt;
477 svcpt = (struct ptlrpc_service_part *)castmeharder;
479 svcpt->scp_at_check = 1;
480 svcpt->scp_at_checktime = cfs_time_current();
481 cfs_waitq_signal(&svcpt->scp_waitq);
485 ptlrpc_server_nthreads_check(struct ptlrpc_service *svc,
486 struct ptlrpc_service_conf *conf)
489 struct ptlrpc_service_thr_conf *tc = &conf->psc_thr;
496 * Common code for estimating & validating threads number.
497 * CPT affinity service could have percpt thread-pool instead
498 * of a global thread-pool, which means user might not always
499 * get the threads number they give it in conf::tc_nthrs_user
500 * even they did set. It's because we need to validate threads
501 * number for each CPT to guarantee each pool will have enough
502 * threads to keep the service healthy.
504 init = PTLRPC_NTHRS_INIT + (svc->srv_ops.so_hpreq_handler != NULL);
505 init = max_t(int, init, tc->tc_nthrs_init);
507 /* NB: please see comments in lustre_lnet.h for definition
508 * details of these members */
509 LASSERT(tc->tc_nthrs_max != 0);
511 if (tc->tc_nthrs_user != 0) {
512 /* In case there is a reason to test a service with many
513 * threads, we give a less strict check here, it can
514 * be up to 8 * nthrs_max */
515 total = min(tc->tc_nthrs_max * 8, tc->tc_nthrs_user);
516 nthrs = total / svc->srv_ncpts;
517 init = max(init, nthrs);
521 total = tc->tc_nthrs_max;
522 if (tc->tc_nthrs_base == 0) {
523 /* don't care about base threads number per partition,
524 * this is most for non-affinity service */
525 nthrs = total / svc->srv_ncpts;
529 nthrs = tc->tc_nthrs_base;
530 if (svc->srv_ncpts == 1) {
533 /* NB: Increase the base number if it's single partition
534 * and total number of cores/HTs is larger or equal to 4.
535 * result will always < 2 * nthrs_base */
536 weight = cfs_cpt_weight(svc->srv_cptable, CFS_CPT_ANY);
537 for (i = 1; (weight >> (i + 1)) != 0 && /* >= 4 cores/HTs */
538 (tc->tc_nthrs_base >> i) != 0; i++)
539 nthrs += tc->tc_nthrs_base >> i;
542 if (tc->tc_thr_factor != 0) {
543 int factor = tc->tc_thr_factor;
547 * User wants to increase number of threads with for
548 * each CPU core/HT, most likely the factor is larger then
549 * one thread/core because service threads are supposed to
550 * be blocked by lock or wait for IO.
553 * Amdahl's law says that adding processors wouldn't give
554 * a linear increasing of parallelism, so it's nonsense to
555 * have too many threads no matter how many cores/HTs
558 if (cfs_cpu_ht_nsiblings(0) > 1) { /* weight is # of HTs */
559 /* depress thread factor for hyper-thread */
560 factor = factor - (factor >> 1) + (factor >> 3);
563 weight = cfs_cpt_weight(svc->srv_cptable, 0);
566 for (; factor > 0 && weight > 0; factor--, weight -= fade)
567 nthrs += min(weight, fade) * factor;
570 if (nthrs * svc->srv_ncpts > tc->tc_nthrs_max) {
571 nthrs = max(tc->tc_nthrs_base,
572 tc->tc_nthrs_max / svc->srv_ncpts);
575 nthrs = max(nthrs, tc->tc_nthrs_init);
576 svc->srv_nthrs_cpt_limit = nthrs;
577 svc->srv_nthrs_cpt_init = init;
579 if (nthrs * svc->srv_ncpts > tc->tc_nthrs_max) {
580 LCONSOLE_WARN("%s: This service may have more threads (%d) "
581 "than the given soft limit (%d)\n",
582 svc->srv_name, nthrs * svc->srv_ncpts,
589 * Initialize percpt data for a service
592 ptlrpc_service_part_init(struct ptlrpc_service *svc,
593 struct ptlrpc_service_part *svcpt, int cpt)
595 struct ptlrpc_at_array *array;
600 svcpt->scp_cpt = cpt;
601 CFS_INIT_LIST_HEAD(&svcpt->scp_threads);
603 /* rqbd and incoming request queue */
604 spin_lock_init(&svcpt->scp_lock);
605 CFS_INIT_LIST_HEAD(&svcpt->scp_rqbd_idle);
606 CFS_INIT_LIST_HEAD(&svcpt->scp_rqbd_posted);
607 CFS_INIT_LIST_HEAD(&svcpt->scp_req_incoming);
608 cfs_waitq_init(&svcpt->scp_waitq);
609 /* history request & rqbd list */
610 CFS_INIT_LIST_HEAD(&svcpt->scp_hist_reqs);
611 CFS_INIT_LIST_HEAD(&svcpt->scp_hist_rqbds);
613 /* acitve requests and hp requests */
614 spin_lock_init(&svcpt->scp_req_lock);
615 CFS_INIT_LIST_HEAD(&svcpt->scp_req_pending);
616 CFS_INIT_LIST_HEAD(&svcpt->scp_hreq_pending);
619 spin_lock_init(&svcpt->scp_rep_lock);
620 CFS_INIT_LIST_HEAD(&svcpt->scp_rep_active);
622 CFS_INIT_LIST_HEAD(&svcpt->scp_rep_queue);
624 CFS_INIT_LIST_HEAD(&svcpt->scp_rep_idle);
625 cfs_waitq_init(&svcpt->scp_rep_waitq);
626 cfs_atomic_set(&svcpt->scp_nreps_difficult, 0);
628 /* adaptive timeout */
629 spin_lock_init(&svcpt->scp_at_lock);
630 array = &svcpt->scp_at_array;
632 size = at_est2timeout(at_max);
633 array->paa_size = size;
634 array->paa_count = 0;
635 array->paa_deadline = -1;
637 /* allocate memory for scp_at_array (ptlrpc_at_array) */
638 OBD_CPT_ALLOC(array->paa_reqs_array,
639 svc->srv_cptable, cpt, sizeof(cfs_list_t) * size);
640 if (array->paa_reqs_array == NULL)
643 for (index = 0; index < size; index++)
644 CFS_INIT_LIST_HEAD(&array->paa_reqs_array[index]);
646 OBD_CPT_ALLOC(array->paa_reqs_count,
647 svc->srv_cptable, cpt, sizeof(__u32) * size);
648 if (array->paa_reqs_count == NULL)
651 cfs_timer_init(&svcpt->scp_at_timer, ptlrpc_at_timer, svcpt);
652 /* At SOW, service time should be quick; 10s seems generous. If client
653 * timeout is less than this, we'll be sending an early reply. */
654 at_init(&svcpt->scp_at_estimate, 10, 0);
656 /* assign this before call ptlrpc_grow_req_bufs */
657 svcpt->scp_service = svc;
658 /* Now allocate the request buffers, but don't post them now */
659 rc = ptlrpc_grow_req_bufs(svcpt, 0);
660 /* We shouldn't be under memory pressure at startup, so
661 * fail if we can't allocate all our buffers at this time. */
668 if (array->paa_reqs_count != NULL) {
669 OBD_FREE(array->paa_reqs_count, sizeof(__u32) * size);
670 array->paa_reqs_count = NULL;
673 if (array->paa_reqs_array != NULL) {
674 OBD_FREE(array->paa_reqs_array,
675 sizeof(cfs_list_t) * array->paa_size);
676 array->paa_reqs_array = NULL;
683 * Initialize service on a given portal.
684 * This includes starting serving threads , allocating and posting rqbds and
687 struct ptlrpc_service *
688 ptlrpc_register_service(struct ptlrpc_service_conf *conf,
689 cfs_proc_dir_entry_t *proc_entry)
691 struct ptlrpc_service_cpt_conf *cconf = &conf->psc_cpt;
692 struct ptlrpc_service *service;
693 struct ptlrpc_service_part *svcpt;
694 struct cfs_cpt_table *cptable;
702 LASSERT(conf->psc_buf.bc_nbufs > 0);
703 LASSERT(conf->psc_buf.bc_buf_size >=
704 conf->psc_buf.bc_req_max_size + SPTLRPC_MAX_PAYLOAD);
705 LASSERT(conf->psc_thr.tc_ctx_tags != 0);
707 cptable = cconf->cc_cptable;
709 cptable = cfs_cpt_table;
711 if (!conf->psc_thr.tc_cpu_affinity) {
714 ncpts = cfs_cpt_number(cptable);
715 if (cconf->cc_pattern != NULL) {
716 struct cfs_expr_list *el;
718 rc = cfs_expr_list_parse(cconf->cc_pattern,
719 strlen(cconf->cc_pattern),
722 CERROR("%s: invalid CPT pattern string: %s",
723 conf->psc_name, cconf->cc_pattern);
724 RETURN(ERR_PTR(-EINVAL));
727 rc = cfs_expr_list_values(el, ncpts, &cpts);
728 cfs_expr_list_free(el);
730 CERROR("%s: failed to parse CPT array %s: %d\n",
731 conf->psc_name, cconf->cc_pattern, rc);
733 OBD_FREE(cpts, sizeof(*cpts) * ncpts);
734 RETURN(ERR_PTR(rc < 0 ? rc : -EINVAL));
740 OBD_ALLOC(service, offsetof(struct ptlrpc_service, srv_parts[ncpts]));
741 if (service == NULL) {
743 OBD_FREE(cpts, sizeof(*cpts) * ncpts);
744 RETURN(ERR_PTR(-ENOMEM));
747 service->srv_cptable = cptable;
748 service->srv_cpts = cpts;
749 service->srv_ncpts = ncpts;
751 service->srv_cpt_bits = 0; /* it's zero already, easy to read... */
752 while ((1 << service->srv_cpt_bits) < cfs_cpt_number(cptable))
753 service->srv_cpt_bits++;
756 spin_lock_init(&service->srv_lock);
757 service->srv_name = conf->psc_name;
758 service->srv_watchdog_factor = conf->psc_watchdog_factor;
759 CFS_INIT_LIST_HEAD(&service->srv_list); /* for safty of cleanup */
761 /* buffer configuration */
762 service->srv_nbuf_per_group = test_req_buffer_pressure ? 1 :
763 max(conf->psc_buf.bc_nbufs /
764 service->srv_ncpts, 1U);
765 service->srv_max_req_size = conf->psc_buf.bc_req_max_size +
767 service->srv_buf_size = conf->psc_buf.bc_buf_size;
768 service->srv_rep_portal = conf->psc_buf.bc_rep_portal;
769 service->srv_req_portal = conf->psc_buf.bc_req_portal;
771 /* Increase max reply size to next power of two */
772 service->srv_max_reply_size = 1;
773 while (service->srv_max_reply_size <
774 conf->psc_buf.bc_rep_max_size + SPTLRPC_MAX_PAYLOAD)
775 service->srv_max_reply_size <<= 1;
777 service->srv_thread_name = conf->psc_thr.tc_thr_name;
778 service->srv_ctx_tags = conf->psc_thr.tc_ctx_tags;
779 service->srv_hpreq_ratio = PTLRPC_SVC_HP_RATIO;
780 service->srv_ops = conf->psc_ops;
782 for (i = 0; i < ncpts; i++) {
783 if (!conf->psc_thr.tc_cpu_affinity)
786 cpt = cpts != NULL ? cpts[i] : i;
788 OBD_CPT_ALLOC(svcpt, cptable, cpt, sizeof(*svcpt));
790 GOTO(failed, rc = -ENOMEM);
792 service->srv_parts[i] = svcpt;
793 rc = ptlrpc_service_part_init(service, svcpt, cpt);
798 ptlrpc_server_nthreads_check(service, conf);
800 rc = LNetSetLazyPortal(service->srv_req_portal);
803 spin_lock(&ptlrpc_all_services_lock);
804 cfs_list_add (&service->srv_list, &ptlrpc_all_services);
805 spin_unlock(&ptlrpc_all_services_lock);
807 if (proc_entry != NULL)
808 ptlrpc_lprocfs_register_service(proc_entry, service);
810 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
811 service->srv_name, service->srv_req_portal);
814 rc = ptlrpc_start_threads(service);
816 CERROR("Failed to start threads for service %s: %d\n",
817 service->srv_name, rc);
824 ptlrpc_unregister_service(service);
827 EXPORT_SYMBOL(ptlrpc_register_service);
830 * to actually free the request, must be called without holding svc_lock.
831 * note it's caller's responsibility to unlink req->rq_list.
833 static void ptlrpc_server_free_request(struct ptlrpc_request *req)
835 LASSERT(cfs_atomic_read(&req->rq_refcount) == 0);
836 LASSERT(cfs_list_empty(&req->rq_timed_list));
838 /* DEBUG_REQ() assumes the reply state of a request with a valid
839 * ref will not be destroyed until that reference is dropped. */
840 ptlrpc_req_drop_rs(req);
842 sptlrpc_svc_ctx_decref(req);
844 if (req != &req->rq_rqbd->rqbd_req) {
845 /* NB request buffers use an embedded
846 * req if the incoming req unlinked the
847 * MD; this isn't one of them! */
848 OBD_FREE(req, sizeof(*req));
853 * drop a reference count of the request. if it reaches 0, we either
854 * put it into history list, or free it immediately.
856 void ptlrpc_server_drop_request(struct ptlrpc_request *req)
858 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
859 struct ptlrpc_service_part *svcpt = rqbd->rqbd_svcpt;
860 struct ptlrpc_service *svc = svcpt->scp_service;
865 if (!cfs_atomic_dec_and_test(&req->rq_refcount))
868 if (req->rq_at_linked) {
869 spin_lock(&svcpt->scp_at_lock);
870 /* recheck with lock, in case it's unlinked by
871 * ptlrpc_at_check_timed() */
872 if (likely(req->rq_at_linked))
873 ptlrpc_at_remove_timed(req);
874 spin_unlock(&svcpt->scp_at_lock);
877 LASSERT(cfs_list_empty(&req->rq_timed_list));
879 /* finalize request */
880 if (req->rq_export) {
881 class_export_put(req->rq_export);
882 req->rq_export = NULL;
885 spin_lock(&svcpt->scp_lock);
887 cfs_list_add(&req->rq_list, &rqbd->rqbd_reqs);
889 refcount = --(rqbd->rqbd_refcount);
891 /* request buffer is now idle: add to history */
892 cfs_list_del(&rqbd->rqbd_list);
894 cfs_list_add_tail(&rqbd->rqbd_list, &svcpt->scp_hist_rqbds);
895 svcpt->scp_hist_nrqbds++;
897 /* cull some history?
898 * I expect only about 1 or 2 rqbds need to be recycled here */
899 while (svcpt->scp_hist_nrqbds > svc->srv_hist_nrqbds_cpt_max) {
900 rqbd = cfs_list_entry(svcpt->scp_hist_rqbds.next,
901 struct ptlrpc_request_buffer_desc,
904 cfs_list_del(&rqbd->rqbd_list);
905 svcpt->scp_hist_nrqbds--;
907 /* remove rqbd's reqs from svc's req history while
908 * I've got the service lock */
909 cfs_list_for_each(tmp, &rqbd->rqbd_reqs) {
910 req = cfs_list_entry(tmp, struct ptlrpc_request,
912 /* Track the highest culled req seq */
913 if (req->rq_history_seq >
914 svcpt->scp_hist_seq_culled) {
915 svcpt->scp_hist_seq_culled =
918 cfs_list_del(&req->rq_history_list);
921 spin_unlock(&svcpt->scp_lock);
923 cfs_list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
924 req = cfs_list_entry(rqbd->rqbd_reqs.next,
925 struct ptlrpc_request,
927 cfs_list_del(&req->rq_list);
928 ptlrpc_server_free_request(req);
931 spin_lock(&svcpt->scp_lock);
933 * now all reqs including the embedded req has been
934 * disposed, schedule request buffer for re-use.
936 LASSERT(cfs_atomic_read(&rqbd->rqbd_req.rq_refcount) ==
938 cfs_list_add_tail(&rqbd->rqbd_list,
939 &svcpt->scp_rqbd_idle);
942 spin_unlock(&svcpt->scp_lock);
943 } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
944 /* If we are low on memory, we are not interested in history */
945 cfs_list_del(&req->rq_list);
946 cfs_list_del_init(&req->rq_history_list);
948 /* Track the highest culled req seq */
949 if (req->rq_history_seq > svcpt->scp_hist_seq_culled)
950 svcpt->scp_hist_seq_culled = req->rq_history_seq;
952 spin_unlock(&svcpt->scp_lock);
954 ptlrpc_server_free_request(req);
956 spin_unlock(&svcpt->scp_lock);
961 * to finish a request: stop sending more early replies, and release
962 * the request. should be called after we finished handling the request.
964 static void ptlrpc_server_finish_request(struct ptlrpc_service_part *svcpt,
965 struct ptlrpc_request *req)
967 ptlrpc_server_hpreq_fini(req);
969 spin_lock(&svcpt->scp_req_lock);
970 svcpt->scp_nreqs_active--;
972 svcpt->scp_nhreqs_active--;
973 spin_unlock(&svcpt->scp_req_lock);
975 ptlrpc_server_drop_request(req);
979 * This function makes sure dead exports are evicted in a timely manner.
980 * This function is only called when some export receives a message (i.e.,
981 * the network is up.)
983 static void ptlrpc_update_export_timer(struct obd_export *exp, long extra_delay)
985 struct obd_export *oldest_exp;
986 time_t oldest_time, new_time;
992 /* Compensate for slow machines, etc, by faking our request time
993 into the future. Although this can break the strict time-ordering
994 of the list, we can be really lazy here - we don't have to evict
995 at the exact right moment. Eventually, all silent exports
996 will make it to the top of the list. */
998 /* Do not pay attention on 1sec or smaller renewals. */
999 new_time = cfs_time_current_sec() + extra_delay;
1000 if (exp->exp_last_request_time + 1 /*second */ >= new_time)
1003 exp->exp_last_request_time = new_time;
1004 CDEBUG(D_HA, "updating export %s at "CFS_TIME_T" exp %p\n",
1005 exp->exp_client_uuid.uuid,
1006 exp->exp_last_request_time, exp);
1008 /* exports may get disconnected from the chain even though the
1009 export has references, so we must keep the spin lock while
1010 manipulating the lists */
1011 spin_lock(&exp->exp_obd->obd_dev_lock);
1013 if (cfs_list_empty(&exp->exp_obd_chain_timed)) {
1014 /* this one is not timed */
1015 spin_unlock(&exp->exp_obd->obd_dev_lock);
1019 cfs_list_move_tail(&exp->exp_obd_chain_timed,
1020 &exp->exp_obd->obd_exports_timed);
1022 oldest_exp = cfs_list_entry(exp->exp_obd->obd_exports_timed.next,
1023 struct obd_export, exp_obd_chain_timed);
1024 oldest_time = oldest_exp->exp_last_request_time;
1025 spin_unlock(&exp->exp_obd->obd_dev_lock);
1027 if (exp->exp_obd->obd_recovering) {
1028 /* be nice to everyone during recovery */
1033 /* Note - racing to start/reset the obd_eviction timer is safe */
1034 if (exp->exp_obd->obd_eviction_timer == 0) {
1035 /* Check if the oldest entry is expired. */
1036 if (cfs_time_current_sec() > (oldest_time + PING_EVICT_TIMEOUT +
1038 /* We need a second timer, in case the net was down and
1039 * it just came back. Since the pinger may skip every
1040 * other PING_INTERVAL (see note in ptlrpc_pinger_main),
1041 * we better wait for 3. */
1042 exp->exp_obd->obd_eviction_timer =
1043 cfs_time_current_sec() + 3 * PING_INTERVAL;
1044 CDEBUG(D_HA, "%s: Think about evicting %s from "CFS_TIME_T"\n",
1045 exp->exp_obd->obd_name,
1046 obd_export_nid2str(oldest_exp), oldest_time);
1049 if (cfs_time_current_sec() >
1050 (exp->exp_obd->obd_eviction_timer + extra_delay)) {
1051 /* The evictor won't evict anyone who we've heard from
1052 * recently, so we don't have to check before we start
1054 if (!ping_evictor_wake(exp))
1055 exp->exp_obd->obd_eviction_timer = 0;
1063 * Sanity check request \a req.
1064 * Return 0 if all is ok, error code otherwise.
1066 static int ptlrpc_check_req(struct ptlrpc_request *req)
1070 if (unlikely(lustre_msg_get_conn_cnt(req->rq_reqmsg) <
1071 req->rq_export->exp_conn_cnt)) {
1072 DEBUG_REQ(D_ERROR, req,
1073 "DROPPING req from old connection %d < %d",
1074 lustre_msg_get_conn_cnt(req->rq_reqmsg),
1075 req->rq_export->exp_conn_cnt);
1078 if (unlikely(req->rq_export->exp_obd &&
1079 req->rq_export->exp_obd->obd_fail)) {
1080 /* Failing over, don't handle any more reqs, send
1081 error response instead. */
1082 CDEBUG(D_RPCTRACE, "Dropping req %p for failed obd %s\n",
1083 req, req->rq_export->exp_obd->obd_name);
1085 } else if (lustre_msg_get_flags(req->rq_reqmsg) &
1086 (MSG_REPLAY | MSG_REQ_REPLAY_DONE) &&
1087 !(req->rq_export->exp_obd->obd_recovering)) {
1088 DEBUG_REQ(D_ERROR, req,
1089 "Invalid replay without recovery");
1090 class_fail_export(req->rq_export);
1092 } else if (lustre_msg_get_transno(req->rq_reqmsg) != 0 &&
1093 !(req->rq_export->exp_obd->obd_recovering)) {
1094 DEBUG_REQ(D_ERROR, req, "Invalid req with transno "
1095 LPU64" without recovery",
1096 lustre_msg_get_transno(req->rq_reqmsg));
1097 class_fail_export(req->rq_export);
1101 if (unlikely(rc < 0)) {
1102 req->rq_status = rc;
1108 static void ptlrpc_at_set_timer(struct ptlrpc_service_part *svcpt)
1110 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
1113 if (array->paa_count == 0) {
1114 cfs_timer_disarm(&svcpt->scp_at_timer);
1118 /* Set timer for closest deadline */
1119 next = (__s32)(array->paa_deadline - cfs_time_current_sec() -
1122 ptlrpc_at_timer((unsigned long)svcpt);
1124 cfs_timer_arm(&svcpt->scp_at_timer, cfs_time_shift(next));
1125 CDEBUG(D_INFO, "armed %s at %+ds\n",
1126 svcpt->scp_service->srv_name, next);
1130 /* Add rpc to early reply check list */
1131 static int ptlrpc_at_add_timed(struct ptlrpc_request *req)
1133 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1134 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
1135 struct ptlrpc_request *rq = NULL;
1141 if (req->rq_no_reply)
1144 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0)
1147 spin_lock(&svcpt->scp_at_lock);
1148 LASSERT(cfs_list_empty(&req->rq_timed_list));
1150 index = (unsigned long)req->rq_deadline % array->paa_size;
1151 if (array->paa_reqs_count[index] > 0) {
1152 /* latest rpcs will have the latest deadlines in the list,
1153 * so search backward. */
1154 cfs_list_for_each_entry_reverse(rq,
1155 &array->paa_reqs_array[index],
1157 if (req->rq_deadline >= rq->rq_deadline) {
1158 cfs_list_add(&req->rq_timed_list,
1159 &rq->rq_timed_list);
1165 /* Add the request at the head of the list */
1166 if (cfs_list_empty(&req->rq_timed_list))
1167 cfs_list_add(&req->rq_timed_list,
1168 &array->paa_reqs_array[index]);
1170 spin_lock(&req->rq_lock);
1171 req->rq_at_linked = 1;
1172 spin_unlock(&req->rq_lock);
1173 req->rq_at_index = index;
1174 array->paa_reqs_count[index]++;
1176 if (array->paa_count == 1 || array->paa_deadline > req->rq_deadline) {
1177 array->paa_deadline = req->rq_deadline;
1178 ptlrpc_at_set_timer(svcpt);
1180 spin_unlock(&svcpt->scp_at_lock);
1186 ptlrpc_at_remove_timed(struct ptlrpc_request *req)
1188 struct ptlrpc_at_array *array;
1190 array = &req->rq_rqbd->rqbd_svcpt->scp_at_array;
1192 /* NB: must call with hold svcpt::scp_at_lock */
1193 LASSERT(!cfs_list_empty(&req->rq_timed_list));
1194 cfs_list_del_init(&req->rq_timed_list);
1196 spin_lock(&req->rq_lock);
1197 req->rq_at_linked = 0;
1198 spin_unlock(&req->rq_lock);
1200 array->paa_reqs_count[req->rq_at_index]--;
1204 static int ptlrpc_at_send_early_reply(struct ptlrpc_request *req)
1206 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1207 struct ptlrpc_request *reqcopy;
1208 struct lustre_msg *reqmsg;
1209 cfs_duration_t olddl = req->rq_deadline - cfs_time_current_sec();
1214 /* deadline is when the client expects us to reply, margin is the
1215 difference between clients' and servers' expectations */
1216 DEBUG_REQ(D_ADAPTTO, req,
1217 "%ssending early reply (deadline %+lds, margin %+lds) for "
1218 "%d+%d", AT_OFF ? "AT off - not " : "",
1219 olddl, olddl - at_get(&svcpt->scp_at_estimate),
1220 at_get(&svcpt->scp_at_estimate), at_extra);
1226 DEBUG_REQ(D_WARNING, req, "Already past deadline (%+lds), "
1227 "not sending early reply. Consider increasing "
1228 "at_early_margin (%d)?", olddl, at_early_margin);
1230 /* Return an error so we're not re-added to the timed list. */
1234 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0){
1235 DEBUG_REQ(D_INFO, req, "Wanted to ask client for more time, "
1236 "but no AT support");
1240 if (req->rq_export &&
1241 lustre_msg_get_flags(req->rq_reqmsg) &
1242 (MSG_REPLAY | MSG_REQ_REPLAY_DONE | MSG_LOCK_REPLAY_DONE)) {
1243 /* During recovery, we don't want to send too many early
1244 * replies, but on the other hand we want to make sure the
1245 * client has enough time to resend if the rpc is lost. So
1246 * during the recovery period send at least 4 early replies,
1247 * spacing them every at_extra if we can. at_estimate should
1248 * always equal this fixed value during recovery. */
1249 at_measured(&svcpt->scp_at_estimate, min(at_extra,
1250 req->rq_export->exp_obd->obd_recovery_timeout / 4));
1252 /* Fake our processing time into the future to ask the clients
1253 * for some extra amount of time */
1254 at_measured(&svcpt->scp_at_estimate, at_extra +
1255 cfs_time_current_sec() -
1256 req->rq_arrival_time.tv_sec);
1258 /* Check to see if we've actually increased the deadline -
1259 * we may be past adaptive_max */
1260 if (req->rq_deadline >= req->rq_arrival_time.tv_sec +
1261 at_get(&svcpt->scp_at_estimate)) {
1262 DEBUG_REQ(D_WARNING, req, "Couldn't add any time "
1263 "(%ld/%ld), not sending early reply\n",
1264 olddl, req->rq_arrival_time.tv_sec +
1265 at_get(&svcpt->scp_at_estimate) -
1266 cfs_time_current_sec());
1270 newdl = cfs_time_current_sec() + at_get(&svcpt->scp_at_estimate);
1272 OBD_ALLOC(reqcopy, sizeof *reqcopy);
1273 if (reqcopy == NULL)
1275 OBD_ALLOC_LARGE(reqmsg, req->rq_reqlen);
1277 OBD_FREE(reqcopy, sizeof *reqcopy);
1282 reqcopy->rq_reply_state = NULL;
1283 reqcopy->rq_rep_swab_mask = 0;
1284 reqcopy->rq_pack_bulk = 0;
1285 reqcopy->rq_pack_udesc = 0;
1286 reqcopy->rq_packed_final = 0;
1287 sptlrpc_svc_ctx_addref(reqcopy);
1288 /* We only need the reqmsg for the magic */
1289 reqcopy->rq_reqmsg = reqmsg;
1290 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1292 LASSERT(cfs_atomic_read(&req->rq_refcount));
1293 /** if it is last refcount then early reply isn't needed */
1294 if (cfs_atomic_read(&req->rq_refcount) == 1) {
1295 DEBUG_REQ(D_ADAPTTO, reqcopy, "Normal reply already sent out, "
1296 "abort sending early reply\n");
1297 GOTO(out, rc = -EINVAL);
1300 /* Connection ref */
1301 reqcopy->rq_export = class_conn2export(
1302 lustre_msg_get_handle(reqcopy->rq_reqmsg));
1303 if (reqcopy->rq_export == NULL)
1304 GOTO(out, rc = -ENODEV);
1307 class_export_rpc_get(reqcopy->rq_export);
1308 if (reqcopy->rq_export->exp_obd &&
1309 reqcopy->rq_export->exp_obd->obd_fail)
1310 GOTO(out_put, rc = -ENODEV);
1312 rc = lustre_pack_reply_flags(reqcopy, 1, NULL, NULL, LPRFL_EARLY_REPLY);
1316 rc = ptlrpc_send_reply(reqcopy, PTLRPC_REPLY_EARLY);
1319 /* Adjust our own deadline to what we told the client */
1320 req->rq_deadline = newdl;
1321 req->rq_early_count++; /* number sent, server side */
1323 DEBUG_REQ(D_ERROR, req, "Early reply send failed %d", rc);
1326 /* Free the (early) reply state from lustre_pack_reply.
1327 (ptlrpc_send_reply takes it's own rs ref, so this is safe here) */
1328 ptlrpc_req_drop_rs(reqcopy);
1331 class_export_rpc_put(reqcopy->rq_export);
1332 class_export_put(reqcopy->rq_export);
1334 sptlrpc_svc_ctx_decref(reqcopy);
1335 OBD_FREE_LARGE(reqmsg, req->rq_reqlen);
1336 OBD_FREE(reqcopy, sizeof *reqcopy);
1340 /* Send early replies to everybody expiring within at_early_margin
1341 asking for at_extra time */
1342 static int ptlrpc_at_check_timed(struct ptlrpc_service_part *svcpt)
1344 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
1345 struct ptlrpc_request *rq, *n;
1346 cfs_list_t work_list;
1349 time_t now = cfs_time_current_sec();
1350 cfs_duration_t delay;
1351 int first, counter = 0;
1354 spin_lock(&svcpt->scp_at_lock);
1355 if (svcpt->scp_at_check == 0) {
1356 spin_unlock(&svcpt->scp_at_lock);
1359 delay = cfs_time_sub(cfs_time_current(), svcpt->scp_at_checktime);
1360 svcpt->scp_at_check = 0;
1362 if (array->paa_count == 0) {
1363 spin_unlock(&svcpt->scp_at_lock);
1367 /* The timer went off, but maybe the nearest rpc already completed. */
1368 first = array->paa_deadline - now;
1369 if (first > at_early_margin) {
1370 /* We've still got plenty of time. Reset the timer. */
1371 ptlrpc_at_set_timer(svcpt);
1372 spin_unlock(&svcpt->scp_at_lock);
1376 /* We're close to a timeout, and we don't know how much longer the
1377 server will take. Send early replies to everyone expiring soon. */
1378 CFS_INIT_LIST_HEAD(&work_list);
1380 index = (unsigned long)array->paa_deadline % array->paa_size;
1381 count = array->paa_count;
1383 count -= array->paa_reqs_count[index];
1384 cfs_list_for_each_entry_safe(rq, n,
1385 &array->paa_reqs_array[index],
1387 if (rq->rq_deadline > now + at_early_margin) {
1388 /* update the earliest deadline */
1389 if (deadline == -1 ||
1390 rq->rq_deadline < deadline)
1391 deadline = rq->rq_deadline;
1395 ptlrpc_at_remove_timed(rq);
1397 * ptlrpc_server_drop_request() may drop
1398 * refcount to 0 already. Let's check this and
1399 * don't add entry to work_list
1401 if (likely(cfs_atomic_inc_not_zero(&rq->rq_refcount)))
1402 cfs_list_add(&rq->rq_timed_list, &work_list);
1406 if (++index >= array->paa_size)
1409 array->paa_deadline = deadline;
1410 /* we have a new earliest deadline, restart the timer */
1411 ptlrpc_at_set_timer(svcpt);
1413 spin_unlock(&svcpt->scp_at_lock);
1415 CDEBUG(D_ADAPTTO, "timeout in %+ds, asking for %d secs on %d early "
1416 "replies\n", first, at_extra, counter);
1418 /* We're already past request deadlines before we even get a
1419 chance to send early replies */
1420 LCONSOLE_WARN("%s: This server is not able to keep up with "
1421 "request traffic (cpu-bound).\n",
1422 svcpt->scp_service->srv_name);
1423 CWARN("earlyQ=%d reqQ=%d recA=%d, svcEst=%d, "
1424 "delay="CFS_DURATION_T"(jiff)\n",
1425 counter, svcpt->scp_nreqs_incoming,
1426 svcpt->scp_nreqs_active,
1427 at_get(&svcpt->scp_at_estimate), delay);
1430 /* we took additional refcount so entries can't be deleted from list, no
1431 * locking is needed */
1432 while (!cfs_list_empty(&work_list)) {
1433 rq = cfs_list_entry(work_list.next, struct ptlrpc_request,
1435 cfs_list_del_init(&rq->rq_timed_list);
1437 if (ptlrpc_at_send_early_reply(rq) == 0)
1438 ptlrpc_at_add_timed(rq);
1440 ptlrpc_server_drop_request(rq);
1443 RETURN(1); /* return "did_something" for liblustre */
1447 * Put the request to the export list if the request may become
1448 * a high priority one.
1450 static int ptlrpc_server_hpreq_init(struct ptlrpc_service *svc,
1451 struct ptlrpc_request *req)
1456 if (svc->srv_ops.so_hpreq_handler) {
1457 rc = svc->srv_ops.so_hpreq_handler(req);
1461 if (req->rq_export && req->rq_ops) {
1462 /* Perform request specific check. We should do this check
1463 * before the request is added into exp_hp_rpcs list otherwise
1464 * it may hit swab race at LU-1044. */
1465 if (req->rq_ops->hpreq_check)
1466 rc = req->rq_ops->hpreq_check(req);
1468 spin_lock_bh(&req->rq_export->exp_rpc_lock);
1469 cfs_list_add(&req->rq_exp_list,
1470 &req->rq_export->exp_hp_rpcs);
1471 spin_unlock_bh(&req->rq_export->exp_rpc_lock);
1477 /** Remove the request from the export list. */
1478 static void ptlrpc_server_hpreq_fini(struct ptlrpc_request *req)
1481 if (req->rq_export && req->rq_ops) {
1482 /* refresh lock timeout again so that client has more
1483 * room to send lock cancel RPC. */
1484 if (req->rq_ops->hpreq_fini)
1485 req->rq_ops->hpreq_fini(req);
1487 spin_lock_bh(&req->rq_export->exp_rpc_lock);
1488 cfs_list_del_init(&req->rq_exp_list);
1489 spin_unlock_bh(&req->rq_export->exp_rpc_lock);
1494 static int ptlrpc_hpreq_check(struct ptlrpc_request *req)
1499 static struct ptlrpc_hpreq_ops ptlrpc_hpreq_common = {
1500 .hpreq_check = ptlrpc_hpreq_check,
1503 /* Hi-Priority RPC check by RPC operation code. */
1504 int ptlrpc_hpreq_handler(struct ptlrpc_request *req)
1506 int opc = lustre_msg_get_opc(req->rq_reqmsg);
1508 /* Check for export to let only reconnects for not yet evicted
1509 * export to become a HP rpc. */
1510 if ((req->rq_export != NULL) &&
1511 (opc == OBD_PING || opc == MDS_CONNECT || opc == OST_CONNECT))
1512 req->rq_ops = &ptlrpc_hpreq_common;
1516 EXPORT_SYMBOL(ptlrpc_hpreq_handler);
1519 * Make the request a high priority one.
1521 * All the high priority requests are queued in a separate FIFO
1522 * ptlrpc_service_part::scp_hpreq_pending list which is parallel to
1523 * ptlrpc_service_part::scp_req_pending list but has a higher priority
1526 * \see ptlrpc_server_handle_request().
1528 static void ptlrpc_hpreq_reorder_nolock(struct ptlrpc_service_part *svcpt,
1529 struct ptlrpc_request *req)
1533 spin_lock(&req->rq_lock);
1534 if (req->rq_hp == 0) {
1535 int opc = lustre_msg_get_opc(req->rq_reqmsg);
1537 /* Add to the high priority queue. */
1538 cfs_list_move_tail(&req->rq_list, &svcpt->scp_hreq_pending);
1540 if (opc != OBD_PING)
1541 DEBUG_REQ(D_RPCTRACE, req, "high priority req");
1543 spin_unlock(&req->rq_lock);
1548 * \see ptlrpc_hpreq_reorder_nolock
1550 void ptlrpc_hpreq_reorder(struct ptlrpc_request *req)
1552 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1555 spin_lock(&svcpt->scp_req_lock);
1556 /* It may happen that the request is already taken for the processing
1557 * but still in the export list, or the request is not in the request
1558 * queue but in the export list already, do not add it into the
1560 if (!cfs_list_empty(&req->rq_list))
1561 ptlrpc_hpreq_reorder_nolock(svcpt, req);
1562 spin_unlock(&svcpt->scp_req_lock);
1565 EXPORT_SYMBOL(ptlrpc_hpreq_reorder);
1568 * Add a request to the regular or HP queue; optionally perform HP request
1571 static int ptlrpc_server_request_add(struct ptlrpc_service_part *svcpt,
1572 struct ptlrpc_request *req)
1577 rc = ptlrpc_server_hpreq_init(svcpt->scp_service, req);
1581 spin_lock(&svcpt->scp_req_lock);
1584 ptlrpc_hpreq_reorder_nolock(svcpt, req);
1586 cfs_list_add_tail(&req->rq_list, &svcpt->scp_req_pending);
1588 spin_unlock(&svcpt->scp_req_lock);
1594 * Allow to handle high priority request
1595 * User can call it w/o any lock but need to hold
1596 * ptlrpc_service_part::scp_req_lock to get reliable result
1598 static int ptlrpc_server_allow_high(struct ptlrpc_service_part *svcpt,
1601 int running = svcpt->scp_nthrs_running;
1606 if (unlikely(svcpt->scp_service->srv_req_portal == MDS_REQUEST_PORTAL &&
1607 CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CANCEL_RESEND))) {
1608 /* leave just 1 thread for normal RPCs */
1609 running = PTLRPC_NTHRS_INIT;
1610 if (svcpt->scp_service->srv_ops.so_hpreq_handler != NULL)
1614 if (svcpt->scp_nreqs_active >= running - 1)
1617 if (svcpt->scp_nhreqs_active == 0)
1620 return cfs_list_empty(&svcpt->scp_req_pending) ||
1621 svcpt->scp_hreq_count < svcpt->scp_service->srv_hpreq_ratio;
1624 static int ptlrpc_server_high_pending(struct ptlrpc_service_part *svcpt,
1627 return ptlrpc_server_allow_high(svcpt, force) &&
1628 !cfs_list_empty(&svcpt->scp_hreq_pending);
1632 * Only allow normal priority requests on a service that has a high-priority
1633 * queue if forced (i.e. cleanup), if there are other high priority requests
1634 * already being processed (i.e. those threads can service more high-priority
1635 * requests), or if there are enough idle threads that a later thread can do
1636 * a high priority request.
1637 * User can call it w/o any lock but need to hold
1638 * ptlrpc_service_part::scp_req_lock to get reliable result
1640 static int ptlrpc_server_allow_normal(struct ptlrpc_service_part *svcpt,
1643 int running = svcpt->scp_nthrs_running;
1645 if (1) /* always allow to handle normal request for liblustre */
1648 if (unlikely(svcpt->scp_service->srv_req_portal == MDS_REQUEST_PORTAL &&
1649 CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CANCEL_RESEND))) {
1650 /* leave just 1 thread for normal RPCs */
1651 running = PTLRPC_NTHRS_INIT;
1652 if (svcpt->scp_service->srv_ops.so_hpreq_handler != NULL)
1657 svcpt->scp_nreqs_active < running - 2)
1660 if (svcpt->scp_nreqs_active >= running - 1)
1663 return svcpt->scp_nhreqs_active > 0 ||
1664 svcpt->scp_service->srv_ops.so_hpreq_handler == NULL;
1667 static int ptlrpc_server_normal_pending(struct ptlrpc_service_part *svcpt,
1670 return ptlrpc_server_allow_normal(svcpt, force) &&
1671 !cfs_list_empty(&svcpt->scp_req_pending);
1675 * Returns true if there are requests available in incoming
1676 * request queue for processing and it is allowed to fetch them.
1677 * User can call it w/o any lock but need to hold ptlrpc_service::scp_req_lock
1678 * to get reliable result
1679 * \see ptlrpc_server_allow_normal
1680 * \see ptlrpc_server_allow high
1683 ptlrpc_server_request_pending(struct ptlrpc_service_part *svcpt, int force)
1685 return ptlrpc_server_high_pending(svcpt, force) ||
1686 ptlrpc_server_normal_pending(svcpt, force);
1690 * Fetch a request for processing from queue of unprocessed requests.
1691 * Favors high-priority requests.
1692 * Returns a pointer to fetched request.
1694 static struct ptlrpc_request *
1695 ptlrpc_server_request_get(struct ptlrpc_service_part *svcpt, int force)
1697 struct ptlrpc_request *req;
1700 if (ptlrpc_server_high_pending(svcpt, force)) {
1701 req = cfs_list_entry(svcpt->scp_hreq_pending.next,
1702 struct ptlrpc_request, rq_list);
1703 svcpt->scp_hreq_count++;
1707 if (ptlrpc_server_normal_pending(svcpt, force)) {
1708 req = cfs_list_entry(svcpt->scp_req_pending.next,
1709 struct ptlrpc_request, rq_list);
1710 svcpt->scp_hreq_count = 0;
1717 * Handle freshly incoming reqs, add to timed early reply list,
1718 * pass on to regular request queue.
1719 * All incoming requests pass through here before getting into
1720 * ptlrpc_server_handle_req later on.
1723 ptlrpc_server_handle_req_in(struct ptlrpc_service_part *svcpt)
1725 struct ptlrpc_service *svc = svcpt->scp_service;
1726 struct ptlrpc_request *req;
1731 spin_lock(&svcpt->scp_lock);
1732 if (cfs_list_empty(&svcpt->scp_req_incoming)) {
1733 spin_unlock(&svcpt->scp_lock);
1737 req = cfs_list_entry(svcpt->scp_req_incoming.next,
1738 struct ptlrpc_request, rq_list);
1739 cfs_list_del_init(&req->rq_list);
1740 svcpt->scp_nreqs_incoming--;
1741 /* Consider this still a "queued" request as far as stats are
1743 spin_unlock(&svcpt->scp_lock);
1745 /* go through security check/transform */
1746 rc = sptlrpc_svc_unwrap_request(req);
1750 case SECSVC_COMPLETE:
1751 target_send_reply(req, 0, OBD_FAIL_MDS_ALL_REPLY_NET);
1760 * for null-flavored rpc, msg has been unpacked by sptlrpc, although
1761 * redo it wouldn't be harmful.
1763 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) {
1764 rc = ptlrpc_unpack_req_msg(req, req->rq_reqlen);
1766 CERROR("error unpacking request: ptl %d from %s "
1767 "x"LPU64"\n", svc->srv_req_portal,
1768 libcfs_id2str(req->rq_peer), req->rq_xid);
1773 rc = lustre_unpack_req_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
1775 CERROR ("error unpacking ptlrpc body: ptl %d from %s x"
1776 LPU64"\n", svc->srv_req_portal,
1777 libcfs_id2str(req->rq_peer), req->rq_xid);
1781 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_REQ_OPC) &&
1782 lustre_msg_get_opc(req->rq_reqmsg) == cfs_fail_val) {
1783 CERROR("drop incoming rpc opc %u, x"LPU64"\n",
1784 cfs_fail_val, req->rq_xid);
1789 if (lustre_msg_get_type(req->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
1790 CERROR("wrong packet type received (type=%u) from %s\n",
1791 lustre_msg_get_type(req->rq_reqmsg),
1792 libcfs_id2str(req->rq_peer));
1796 switch(lustre_msg_get_opc(req->rq_reqmsg)) {
1799 req->rq_bulk_write = 1;
1803 case MGS_CONFIG_READ:
1804 req->rq_bulk_read = 1;
1808 CDEBUG(D_RPCTRACE, "got req x"LPU64"\n", req->rq_xid);
1810 req->rq_export = class_conn2export(
1811 lustre_msg_get_handle(req->rq_reqmsg));
1812 if (req->rq_export) {
1813 class_export_rpc_get(req->rq_export);
1814 rc = ptlrpc_check_req(req);
1816 rc = sptlrpc_target_export_check(req->rq_export, req);
1818 DEBUG_REQ(D_ERROR, req, "DROPPING req with "
1819 "illegal security flavor,");
1824 ptlrpc_update_export_timer(req->rq_export, 0);
1827 /* req_in handling should/must be fast */
1828 if (cfs_time_current_sec() - req->rq_arrival_time.tv_sec > 5)
1829 DEBUG_REQ(D_WARNING, req, "Slow req_in handling "CFS_DURATION_T"s",
1830 cfs_time_sub(cfs_time_current_sec(),
1831 req->rq_arrival_time.tv_sec));
1833 /* Set rpc server deadline and add it to the timed list */
1834 deadline = (lustre_msghdr_get_flags(req->rq_reqmsg) &
1835 MSGHDR_AT_SUPPORT) ?
1836 /* The max time the client expects us to take */
1837 lustre_msg_get_timeout(req->rq_reqmsg) : obd_timeout;
1838 req->rq_deadline = req->rq_arrival_time.tv_sec + deadline;
1839 if (unlikely(deadline == 0)) {
1840 DEBUG_REQ(D_ERROR, req, "Dropping request with 0 timeout");
1844 ptlrpc_at_add_timed(req);
1846 /* Move it over to the request processing queue */
1847 rc = ptlrpc_server_request_add(svcpt, req);
1849 ptlrpc_server_hpreq_fini(req);
1852 cfs_waitq_signal(&svcpt->scp_waitq);
1857 class_export_rpc_put(req->rq_export);
1858 spin_lock(&svcpt->scp_req_lock);
1859 svcpt->scp_nreqs_active++;
1860 spin_unlock(&svcpt->scp_req_lock);
1861 ptlrpc_server_finish_request(svcpt, req);
1867 * Main incoming request handling logic.
1868 * Calls handler function from service to do actual processing.
1871 ptlrpc_server_handle_request(struct ptlrpc_service_part *svcpt,
1872 struct ptlrpc_thread *thread)
1874 struct ptlrpc_service *svc = svcpt->scp_service;
1875 struct obd_export *export = NULL;
1876 struct ptlrpc_request *request;
1877 struct timeval work_start;
1878 struct timeval work_end;
1884 spin_lock(&svcpt->scp_req_lock);
1886 /* !@%$# liblustre only has 1 thread */
1887 if (cfs_atomic_read(&svcpt->scp_nreps_difficult) != 0) {
1888 spin_unlock(&svcpt->scp_req_lock);
1892 request = ptlrpc_server_request_get(svcpt, 0);
1893 if (request == NULL) {
1894 spin_unlock(&svcpt->scp_req_lock);
1898 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT))
1899 fail_opc = OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT;
1900 else if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
1901 fail_opc = OBD_FAIL_PTLRPC_HPREQ_TIMEOUT;
1903 if (unlikely(fail_opc)) {
1904 if (request->rq_export && request->rq_ops) {
1905 spin_unlock(&svcpt->scp_req_lock);
1907 OBD_FAIL_TIMEOUT(fail_opc, 4);
1909 spin_lock(&svcpt->scp_req_lock);
1910 request = ptlrpc_server_request_get(svcpt, 0);
1911 if (request == NULL) {
1912 spin_unlock(&svcpt->scp_req_lock);
1918 cfs_list_del_init(&request->rq_list);
1919 svcpt->scp_nreqs_active++;
1921 svcpt->scp_nhreqs_active++;
1923 spin_unlock(&svcpt->scp_req_lock);
1925 ptlrpc_rqphase_move(request, RQ_PHASE_INTERPRET);
1927 if(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DUMP_LOG))
1928 libcfs_debug_dumplog();
1930 cfs_gettimeofday(&work_start);
1931 timediff = cfs_timeval_sub(&work_start, &request->rq_arrival_time,NULL);
1932 if (likely(svc->srv_stats != NULL)) {
1933 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
1935 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
1936 svcpt->scp_nreqs_incoming);
1937 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
1938 svcpt->scp_nreqs_active);
1939 lprocfs_counter_add(svc->srv_stats, PTLRPC_TIMEOUT,
1940 at_get(&svcpt->scp_at_estimate));
1943 export = request->rq_export;
1944 rc = lu_context_init(&request->rq_session, LCT_SESSION | LCT_NOREF);
1946 CERROR("Failure to initialize session: %d\n", rc);
1949 request->rq_session.lc_thread = thread;
1950 request->rq_session.lc_cookie = 0x5;
1951 lu_context_enter(&request->rq_session);
1953 CDEBUG(D_NET, "got req "LPU64"\n", request->rq_xid);
1955 request->rq_svc_thread = thread;
1957 request->rq_svc_thread->t_env->le_ses = &request->rq_session;
1959 if (likely(request->rq_export)) {
1960 if (unlikely(ptlrpc_check_req(request)))
1962 ptlrpc_update_export_timer(request->rq_export, timediff >> 19);
1965 /* Discard requests queued for longer than the deadline.
1966 The deadline is increased if we send an early reply. */
1967 if (cfs_time_current_sec() > request->rq_deadline) {
1968 DEBUG_REQ(D_ERROR, request, "Dropping timed-out request from %s"
1969 ": deadline "CFS_DURATION_T":"CFS_DURATION_T"s ago\n",
1970 libcfs_id2str(request->rq_peer),
1971 cfs_time_sub(request->rq_deadline,
1972 request->rq_arrival_time.tv_sec),
1973 cfs_time_sub(cfs_time_current_sec(),
1974 request->rq_deadline));
1978 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc "
1979 "%s:%s+%d:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1980 (request->rq_export ?
1981 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1982 (request->rq_export ?
1983 cfs_atomic_read(&request->rq_export->exp_refcount) : -99),
1984 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
1985 libcfs_id2str(request->rq_peer),
1986 lustre_msg_get_opc(request->rq_reqmsg));
1988 if (lustre_msg_get_opc(request->rq_reqmsg) != OBD_PING)
1989 CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_PAUSE_REQ, cfs_fail_val);
1991 rc = svc->srv_ops.so_req_handler(request);
1993 ptlrpc_rqphase_move(request, RQ_PHASE_COMPLETE);
1996 lu_context_exit(&request->rq_session);
1997 lu_context_fini(&request->rq_session);
1999 if (unlikely(cfs_time_current_sec() > request->rq_deadline)) {
2000 DEBUG_REQ(D_WARNING, request, "Request x"LPU64" took longer "
2001 "than estimated ("CFS_DURATION_T":"CFS_DURATION_T"s);"
2002 " client may timeout.",
2003 request->rq_xid, cfs_time_sub(request->rq_deadline,
2004 request->rq_arrival_time.tv_sec),
2005 cfs_time_sub(cfs_time_current_sec(),
2006 request->rq_deadline));
2009 cfs_gettimeofday(&work_end);
2010 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
2011 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc "
2012 "%s:%s+%d:%d:x"LPU64":%s:%d Request procesed in "
2013 "%ldus (%ldus total) trans "LPU64" rc %d/%d\n",
2015 (request->rq_export ?
2016 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
2017 (request->rq_export ?
2018 cfs_atomic_read(&request->rq_export->exp_refcount) : -99),
2019 lustre_msg_get_status(request->rq_reqmsg),
2021 libcfs_id2str(request->rq_peer),
2022 lustre_msg_get_opc(request->rq_reqmsg),
2024 cfs_timeval_sub(&work_end, &request->rq_arrival_time, NULL),
2025 (request->rq_repmsg ?
2026 lustre_msg_get_transno(request->rq_repmsg) :
2027 request->rq_transno),
2029 (request->rq_repmsg ?
2030 lustre_msg_get_status(request->rq_repmsg) : -999));
2031 if (likely(svc->srv_stats != NULL && request->rq_reqmsg != NULL)) {
2032 __u32 op = lustre_msg_get_opc(request->rq_reqmsg);
2033 int opc = opcode_offset(op);
2034 if (opc > 0 && !(op == LDLM_ENQUEUE || op == MDS_REINT)) {
2035 LASSERT(opc < LUSTRE_MAX_OPCODES);
2036 lprocfs_counter_add(svc->srv_stats,
2037 opc + EXTRA_MAX_OPCODES,
2041 if (unlikely(request->rq_early_count)) {
2042 DEBUG_REQ(D_ADAPTTO, request,
2043 "sent %d early replies before finishing in "
2045 request->rq_early_count,
2046 cfs_time_sub(work_end.tv_sec,
2047 request->rq_arrival_time.tv_sec));
2052 class_export_rpc_put(export);
2053 ptlrpc_server_finish_request(svcpt, request);
2059 * An internal function to process a single reply state object.
2062 ptlrpc_handle_rs(struct ptlrpc_reply_state *rs)
2064 struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
2065 struct ptlrpc_service *svc = svcpt->scp_service;
2066 struct obd_export *exp;
2071 exp = rs->rs_export;
2073 LASSERT (rs->rs_difficult);
2074 LASSERT (rs->rs_scheduled);
2075 LASSERT (cfs_list_empty(&rs->rs_list));
2077 spin_lock(&exp->exp_lock);
2078 /* Noop if removed already */
2079 cfs_list_del_init (&rs->rs_exp_list);
2080 spin_unlock(&exp->exp_lock);
2082 /* The disk commit callback holds exp_uncommitted_replies_lock while it
2083 * iterates over newly committed replies, removing them from
2084 * exp_uncommitted_replies. It then drops this lock and schedules the
2085 * replies it found for handling here.
2087 * We can avoid contention for exp_uncommitted_replies_lock between the
2088 * HRT threads and further commit callbacks by checking rs_committed
2089 * which is set in the commit callback while it holds both
2090 * rs_lock and exp_uncommitted_reples.
2092 * If we see rs_committed clear, the commit callback _may_ not have
2093 * handled this reply yet and we race with it to grab
2094 * exp_uncommitted_replies_lock before removing the reply from
2095 * exp_uncommitted_replies. Note that if we lose the race and the
2096 * reply has already been removed, list_del_init() is a noop.
2098 * If we see rs_committed set, we know the commit callback is handling,
2099 * or has handled this reply since store reordering might allow us to
2100 * see rs_committed set out of sequence. But since this is done
2101 * holding rs_lock, we can be sure it has all completed once we hold
2102 * rs_lock, which we do right next.
2104 if (!rs->rs_committed) {
2105 spin_lock(&exp->exp_uncommitted_replies_lock);
2106 cfs_list_del_init(&rs->rs_obd_list);
2107 spin_unlock(&exp->exp_uncommitted_replies_lock);
2110 spin_lock(&rs->rs_lock);
2112 been_handled = rs->rs_handled;
2115 nlocks = rs->rs_nlocks; /* atomic "steal", but */
2116 rs->rs_nlocks = 0; /* locks still on rs_locks! */
2118 if (nlocks == 0 && !been_handled) {
2119 /* If we see this, we should already have seen the warning
2120 * in mds_steal_ack_locks() */
2121 CDEBUG(D_HA, "All locks stolen from rs %p x"LPD64".t"LPD64
2124 rs->rs_xid, rs->rs_transno, rs->rs_opc,
2125 libcfs_nid2str(exp->exp_connection->c_peer.nid));
2128 if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
2129 spin_unlock(&rs->rs_lock);
2131 if (!been_handled && rs->rs_on_net) {
2132 LNetMDUnlink(rs->rs_md_h);
2133 /* Ignore return code; we're racing with completion */
2136 while (nlocks-- > 0)
2137 ldlm_lock_decref(&rs->rs_locks[nlocks],
2138 rs->rs_modes[nlocks]);
2140 spin_lock(&rs->rs_lock);
2143 rs->rs_scheduled = 0;
2145 if (!rs->rs_on_net) {
2147 spin_unlock(&rs->rs_lock);
2149 class_export_put (exp);
2150 rs->rs_export = NULL;
2151 ptlrpc_rs_decref (rs);
2152 if (cfs_atomic_dec_and_test(&svcpt->scp_nreps_difficult) &&
2153 svc->srv_is_stopping)
2154 cfs_waitq_broadcast(&svcpt->scp_waitq);
2158 /* still on the net; callback will schedule */
2159 spin_unlock(&rs->rs_lock);
2166 * Check whether given service has a reply available for processing
2169 * \param svc a ptlrpc service
2170 * \retval 0 no replies processed
2171 * \retval 1 one reply processed
2174 ptlrpc_server_handle_reply(struct ptlrpc_service_part *svcpt)
2176 struct ptlrpc_reply_state *rs = NULL;
2179 spin_lock(&svcpt->scp_rep_lock);
2180 if (!cfs_list_empty(&svcpt->scp_rep_queue)) {
2181 rs = cfs_list_entry(svcpt->scp_rep_queue.prev,
2182 struct ptlrpc_reply_state,
2184 cfs_list_del_init(&rs->rs_list);
2186 spin_unlock(&svcpt->scp_rep_lock);
2188 ptlrpc_handle_rs(rs);
2192 /* FIXME make use of timeout later */
2194 liblustre_check_services (void *arg)
2196 int did_something = 0;
2198 cfs_list_t *tmp, *nxt;
2201 /* I'm relying on being single threaded, not to have to lock
2202 * ptlrpc_all_services etc */
2203 cfs_list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
2204 struct ptlrpc_service *svc =
2205 cfs_list_entry (tmp, struct ptlrpc_service, srv_list);
2206 struct ptlrpc_service_part *svcpt;
2208 LASSERT(svc->srv_ncpts == 1);
2209 svcpt = svc->srv_parts[0];
2211 if (svcpt->scp_nthrs_running != 0) /* I've recursed */
2214 /* service threads can block for bulk, so this limits us
2215 * (arbitrarily) to recursing 1 stack frame per service.
2216 * Note that the problem with recursion is that we have to
2217 * unwind completely before our caller can resume. */
2219 svcpt->scp_nthrs_running++;
2222 rc = ptlrpc_server_handle_req_in(svcpt);
2223 rc |= ptlrpc_server_handle_reply(svcpt);
2224 rc |= ptlrpc_at_check_timed(svcpt);
2225 rc |= ptlrpc_server_handle_request(svcpt, NULL);
2226 rc |= (ptlrpc_server_post_idle_rqbds(svcpt) > 0);
2227 did_something |= rc;
2230 svcpt->scp_nthrs_running--;
2233 RETURN(did_something);
2235 #define ptlrpc_stop_all_threads(s) do {} while (0)
2237 #else /* __KERNEL__ */
2240 ptlrpc_check_rqbd_pool(struct ptlrpc_service_part *svcpt)
2242 int avail = svcpt->scp_nrqbds_posted;
2243 int low_water = test_req_buffer_pressure ? 0 :
2244 svcpt->scp_service->srv_nbuf_per_group / 2;
2246 /* NB I'm not locking; just looking. */
2248 /* CAVEAT EMPTOR: We might be allocating buffers here because we've
2249 * allowed the request history to grow out of control. We could put a
2250 * sanity check on that here and cull some history if we need the
2253 if (avail <= low_water)
2254 ptlrpc_grow_req_bufs(svcpt, 1);
2256 if (svcpt->scp_service->srv_stats) {
2257 lprocfs_counter_add(svcpt->scp_service->srv_stats,
2258 PTLRPC_REQBUF_AVAIL_CNTR, avail);
2263 ptlrpc_retry_rqbds(void *arg)
2265 struct ptlrpc_service_part *svcpt = (struct ptlrpc_service_part *)arg;
2267 svcpt->scp_rqbd_timeout = 0;
2272 ptlrpc_threads_enough(struct ptlrpc_service_part *svcpt)
2274 return svcpt->scp_nreqs_active <
2275 svcpt->scp_nthrs_running - 1 -
2276 (svcpt->scp_service->srv_ops.so_hpreq_handler != NULL);
2280 * allowed to create more threads
2281 * user can call it w/o any lock but need to hold
2282 * ptlrpc_service_part::scp_lock to get reliable result
2285 ptlrpc_threads_increasable(struct ptlrpc_service_part *svcpt)
2287 return svcpt->scp_nthrs_running +
2288 svcpt->scp_nthrs_starting <
2289 svcpt->scp_service->srv_nthrs_cpt_limit;
2293 * too many requests and allowed to create more threads
2296 ptlrpc_threads_need_create(struct ptlrpc_service_part *svcpt)
2298 return !ptlrpc_threads_enough(svcpt) &&
2299 ptlrpc_threads_increasable(svcpt);
2303 ptlrpc_thread_stopping(struct ptlrpc_thread *thread)
2305 return thread_is_stopping(thread) ||
2306 thread->t_svcpt->scp_service->srv_is_stopping;
2310 ptlrpc_rqbd_pending(struct ptlrpc_service_part *svcpt)
2312 return !cfs_list_empty(&svcpt->scp_rqbd_idle) &&
2313 svcpt->scp_rqbd_timeout == 0;
2317 ptlrpc_at_check(struct ptlrpc_service_part *svcpt)
2319 return svcpt->scp_at_check;
2323 * requests wait on preprocessing
2324 * user can call it w/o any lock but need to hold
2325 * ptlrpc_service_part::scp_lock to get reliable result
2328 ptlrpc_server_request_incoming(struct ptlrpc_service_part *svcpt)
2330 return !cfs_list_empty(&svcpt->scp_req_incoming);
2333 static __attribute__((__noinline__)) int
2334 ptlrpc_wait_event(struct ptlrpc_service_part *svcpt,
2335 struct ptlrpc_thread *thread)
2337 /* Don't exit while there are replies to be handled */
2338 struct l_wait_info lwi = LWI_TIMEOUT(svcpt->scp_rqbd_timeout,
2339 ptlrpc_retry_rqbds, svcpt);
2341 lc_watchdog_disable(thread->t_watchdog);
2345 l_wait_event_exclusive_head(svcpt->scp_waitq,
2346 ptlrpc_thread_stopping(thread) ||
2347 ptlrpc_server_request_incoming(svcpt) ||
2348 ptlrpc_server_request_pending(svcpt, 0) ||
2349 ptlrpc_rqbd_pending(svcpt) ||
2350 ptlrpc_at_check(svcpt), &lwi);
2352 if (ptlrpc_thread_stopping(thread))
2355 lc_watchdog_touch(thread->t_watchdog,
2356 ptlrpc_server_get_timeout(svcpt));
2361 * Main thread body for service threads.
2362 * Waits in a loop waiting for new requests to process to appear.
2363 * Every time an incoming requests is added to its queue, a waitq
2364 * is woken up and one of the threads will handle it.
2366 static int ptlrpc_main(void *arg)
2368 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
2369 struct ptlrpc_service_part *svcpt = thread->t_svcpt;
2370 struct ptlrpc_service *svc = svcpt->scp_service;
2371 struct ptlrpc_reply_state *rs;
2372 #ifdef WITH_GROUP_INFO
2373 cfs_group_info_t *ginfo = NULL;
2376 int counter = 0, rc = 0;
2379 thread->t_pid = cfs_curproc_pid();
2380 cfs_daemonize_ctxt(thread->t_name);
2382 /* NB: we will call cfs_cpt_bind() for all threads, because we
2383 * might want to run lustre server only on a subset of system CPUs,
2384 * in that case ->scp_cpt is CFS_CPT_ANY */
2385 rc = cfs_cpt_bind(svc->srv_cptable, svcpt->scp_cpt);
2387 CWARN("%s: failed to bind %s on CPT %d\n",
2388 svc->srv_name, thread->t_name, svcpt->scp_cpt);
2391 #ifdef WITH_GROUP_INFO
2392 ginfo = cfs_groups_alloc(0);
2398 cfs_set_current_groups(ginfo);
2399 cfs_put_group_info(ginfo);
2402 if (svc->srv_ops.so_thr_init != NULL) {
2403 rc = svc->srv_ops.so_thr_init(thread);
2414 rc = lu_context_init(&env->le_ctx,
2415 svc->srv_ctx_tags|LCT_REMEMBER|LCT_NOREF);
2419 thread->t_env = env;
2420 env->le_ctx.lc_thread = thread;
2421 env->le_ctx.lc_cookie = 0x6;
2423 while (!cfs_list_empty(&svcpt->scp_rqbd_idle)) {
2424 rc = ptlrpc_server_post_idle_rqbds(svcpt);
2428 CERROR("Failed to post rqbd for %s on CPT %d: %d\n",
2429 svc->srv_name, svcpt->scp_cpt, rc);
2433 /* Alloc reply state structure for this one */
2434 OBD_ALLOC_LARGE(rs, svc->srv_max_reply_size);
2440 spin_lock(&svcpt->scp_lock);
2442 LASSERT(thread_is_starting(thread));
2443 thread_clear_flags(thread, SVC_STARTING);
2445 LASSERT(svcpt->scp_nthrs_starting == 1);
2446 svcpt->scp_nthrs_starting--;
2448 /* SVC_STOPPING may already be set here if someone else is trying
2449 * to stop the service while this new thread has been dynamically
2450 * forked. We still set SVC_RUNNING to let our creator know that
2451 * we are now running, however we will exit as soon as possible */
2452 thread_add_flags(thread, SVC_RUNNING);
2453 svcpt->scp_nthrs_running++;
2454 spin_unlock(&svcpt->scp_lock);
2456 /* wake up our creator in case he's still waiting. */
2457 cfs_waitq_signal(&thread->t_ctl_waitq);
2459 thread->t_watchdog = lc_watchdog_add(ptlrpc_server_get_timeout(svcpt),
2462 spin_lock(&svcpt->scp_rep_lock);
2463 cfs_list_add(&rs->rs_list, &svcpt->scp_rep_idle);
2464 cfs_waitq_signal(&svcpt->scp_rep_waitq);
2465 spin_unlock(&svcpt->scp_rep_lock);
2467 CDEBUG(D_NET, "service thread %d (#%d) started\n", thread->t_id,
2468 svcpt->scp_nthrs_running);
2470 /* XXX maintain a list of all managed devices: insert here */
2471 while (!ptlrpc_thread_stopping(thread)) {
2472 if (ptlrpc_wait_event(svcpt, thread))
2475 ptlrpc_check_rqbd_pool(svcpt);
2477 if (ptlrpc_threads_need_create(svcpt)) {
2478 /* Ignore return code - we tried... */
2479 ptlrpc_start_thread(svcpt, 0);
2482 /* Process all incoming reqs before handling any */
2483 if (ptlrpc_server_request_incoming(svcpt)) {
2484 ptlrpc_server_handle_req_in(svcpt);
2485 /* but limit ourselves in case of flood */
2486 if (counter++ < 100)
2491 if (ptlrpc_at_check(svcpt))
2492 ptlrpc_at_check_timed(svcpt);
2494 if (ptlrpc_server_request_pending(svcpt, 0)) {
2495 lu_context_enter(&env->le_ctx);
2496 ptlrpc_server_handle_request(svcpt, thread);
2497 lu_context_exit(&env->le_ctx);
2500 if (ptlrpc_rqbd_pending(svcpt) &&
2501 ptlrpc_server_post_idle_rqbds(svcpt) < 0) {
2502 /* I just failed to repost request buffers.
2503 * Wait for a timeout (unless something else
2504 * happens) before I try again */
2505 svcpt->scp_rqbd_timeout = cfs_time_seconds(1) / 10;
2506 CDEBUG(D_RPCTRACE, "Posted buffers: %d\n",
2507 svcpt->scp_nrqbds_posted);
2511 lc_watchdog_delete(thread->t_watchdog);
2512 thread->t_watchdog = NULL;
2516 * deconstruct service specific state created by ptlrpc_start_thread()
2518 if (svc->srv_ops.so_thr_done != NULL)
2519 svc->srv_ops.so_thr_done(thread);
2522 lu_context_fini(&env->le_ctx);
2526 CDEBUG(D_RPCTRACE, "service thread [ %p : %u ] %d exiting: rc %d\n",
2527 thread, thread->t_pid, thread->t_id, rc);
2529 spin_lock(&svcpt->scp_lock);
2530 if (thread_test_and_clear_flags(thread, SVC_STARTING))
2531 svcpt->scp_nthrs_starting--;
2533 if (thread_test_and_clear_flags(thread, SVC_RUNNING)) {
2534 /* must know immediately */
2535 svcpt->scp_nthrs_running--;
2539 thread_add_flags(thread, SVC_STOPPED);
2541 cfs_waitq_signal(&thread->t_ctl_waitq);
2542 spin_unlock(&svcpt->scp_lock);
2547 static int hrt_dont_sleep(struct ptlrpc_hr_thread *hrt,
2548 cfs_list_t *replies)
2552 spin_lock(&hrt->hrt_lock);
2554 cfs_list_splice_init(&hrt->hrt_queue, replies);
2555 result = ptlrpc_hr.hr_stopping || !cfs_list_empty(replies);
2557 spin_unlock(&hrt->hrt_lock);
2562 * Main body of "handle reply" function.
2563 * It processes acked reply states
2565 static int ptlrpc_hr_main(void *arg)
2567 struct ptlrpc_hr_thread *hrt = (struct ptlrpc_hr_thread *)arg;
2568 struct ptlrpc_hr_partition *hrp = hrt->hrt_partition;
2569 CFS_LIST_HEAD (replies);
2570 char threadname[20];
2573 snprintf(threadname, sizeof(threadname), "ptlrpc_hr%02d_%03d",
2574 hrp->hrp_cpt, hrt->hrt_id);
2575 cfs_daemonize_ctxt(threadname);
2577 rc = cfs_cpt_bind(ptlrpc_hr.hr_cpt_table, hrp->hrp_cpt);
2579 CWARN("Failed to bind %s on CPT %d of CPT table %p: rc = %d\n",
2580 threadname, hrp->hrp_cpt, ptlrpc_hr.hr_cpt_table, rc);
2583 cfs_atomic_inc(&hrp->hrp_nstarted);
2584 cfs_waitq_signal(&ptlrpc_hr.hr_waitq);
2586 while (!ptlrpc_hr.hr_stopping) {
2587 l_wait_condition(hrt->hrt_waitq, hrt_dont_sleep(hrt, &replies));
2589 while (!cfs_list_empty(&replies)) {
2590 struct ptlrpc_reply_state *rs;
2592 rs = cfs_list_entry(replies.prev,
2593 struct ptlrpc_reply_state,
2595 cfs_list_del_init(&rs->rs_list);
2596 ptlrpc_handle_rs(rs);
2600 cfs_atomic_inc(&hrp->hrp_nstopped);
2601 cfs_waitq_signal(&ptlrpc_hr.hr_waitq);
2606 static void ptlrpc_stop_hr_threads(void)
2608 struct ptlrpc_hr_partition *hrp;
2612 ptlrpc_hr.hr_stopping = 1;
2614 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2615 if (hrp->hrp_thrs == NULL)
2616 continue; /* uninitialized */
2617 for (j = 0; j < hrp->hrp_nthrs; j++)
2618 cfs_waitq_broadcast(&hrp->hrp_thrs[j].hrt_waitq);
2621 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2622 if (hrp->hrp_thrs == NULL)
2623 continue; /* uninitialized */
2624 cfs_wait_event(ptlrpc_hr.hr_waitq,
2625 cfs_atomic_read(&hrp->hrp_nstopped) ==
2626 cfs_atomic_read(&hrp->hrp_nstarted));
2630 static int ptlrpc_start_hr_threads(void)
2632 struct ptlrpc_hr_partition *hrp;
2637 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2640 for (j = 0; j < hrp->hrp_nthrs; j++) {
2641 rc = cfs_create_thread(ptlrpc_hr_main,
2643 CLONE_VM | CLONE_FILES);
2647 cfs_wait_event(ptlrpc_hr.hr_waitq,
2648 cfs_atomic_read(&hrp->hrp_nstarted) == j);
2652 CERROR("Reply handling thread %d:%d Failed on starting: "
2653 "rc = %d\n", i, j, rc);
2654 ptlrpc_stop_hr_threads();
2660 static void ptlrpc_svcpt_stop_threads(struct ptlrpc_service_part *svcpt)
2662 struct l_wait_info lwi = { 0 };
2663 struct ptlrpc_thread *thread;
2664 CFS_LIST_HEAD (zombie);
2668 CDEBUG(D_INFO, "Stopping threads for service %s\n",
2669 svcpt->scp_service->srv_name);
2671 spin_lock(&svcpt->scp_lock);
2672 /* let the thread know that we would like it to stop asap */
2673 list_for_each_entry(thread, &svcpt->scp_threads, t_link) {
2674 CDEBUG(D_INFO, "Stopping thread %s #%u\n",
2675 svcpt->scp_service->srv_thread_name, thread->t_id);
2676 thread_add_flags(thread, SVC_STOPPING);
2679 cfs_waitq_broadcast(&svcpt->scp_waitq);
2681 while (!cfs_list_empty(&svcpt->scp_threads)) {
2682 thread = cfs_list_entry(svcpt->scp_threads.next,
2683 struct ptlrpc_thread, t_link);
2684 if (thread_is_stopped(thread)) {
2685 cfs_list_del(&thread->t_link);
2686 cfs_list_add(&thread->t_link, &zombie);
2689 spin_unlock(&svcpt->scp_lock);
2691 CDEBUG(D_INFO, "waiting for stopping-thread %s #%u\n",
2692 svcpt->scp_service->srv_thread_name, thread->t_id);
2693 l_wait_event(thread->t_ctl_waitq,
2694 thread_is_stopped(thread), &lwi);
2696 spin_lock(&svcpt->scp_lock);
2699 spin_unlock(&svcpt->scp_lock);
2701 while (!cfs_list_empty(&zombie)) {
2702 thread = cfs_list_entry(zombie.next,
2703 struct ptlrpc_thread, t_link);
2704 cfs_list_del(&thread->t_link);
2705 OBD_FREE_PTR(thread);
2711 * Stops all threads of a particular service \a svc
2713 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
2715 struct ptlrpc_service_part *svcpt;
2719 ptlrpc_service_for_each_part(svcpt, i, svc) {
2720 if (svcpt->scp_service != NULL)
2721 ptlrpc_svcpt_stop_threads(svcpt);
2726 EXPORT_SYMBOL(ptlrpc_stop_all_threads);
2728 int ptlrpc_start_threads(struct ptlrpc_service *svc)
2735 /* We require 2 threads min, see note in ptlrpc_server_handle_request */
2736 LASSERT(svc->srv_nthrs_cpt_init >= PTLRPC_NTHRS_INIT);
2738 for (i = 0; i < svc->srv_ncpts; i++) {
2739 for (j = 0; j < svc->srv_nthrs_cpt_init; j++) {
2740 rc = ptlrpc_start_thread(svc->srv_parts[i], 1);
2746 /* We have enough threads, don't start more. b=15759 */
2753 CERROR("cannot start %s thread #%d_%d: rc %d\n",
2754 svc->srv_thread_name, i, j, rc);
2755 ptlrpc_stop_all_threads(svc);
2758 EXPORT_SYMBOL(ptlrpc_start_threads);
2760 int ptlrpc_start_thread(struct ptlrpc_service_part *svcpt, int wait)
2762 struct l_wait_info lwi = { 0 };
2763 struct ptlrpc_thread *thread;
2764 struct ptlrpc_service *svc = svcpt->scp_service;
2768 LASSERT(svcpt != NULL);
2770 CDEBUG(D_RPCTRACE, "%s[%d] started %d min %d max %d\n",
2771 svc->srv_name, svcpt->scp_cpt, svcpt->scp_nthrs_running,
2772 svc->srv_nthrs_cpt_init, svc->srv_nthrs_cpt_limit);
2775 if (unlikely(svc->srv_is_stopping))
2778 if (!ptlrpc_threads_increasable(svcpt) ||
2779 (OBD_FAIL_CHECK(OBD_FAIL_TGT_TOOMANY_THREADS) &&
2780 svcpt->scp_nthrs_running == svc->srv_nthrs_cpt_init - 1))
2783 OBD_CPT_ALLOC_PTR(thread, svc->srv_cptable, svcpt->scp_cpt);
2786 cfs_waitq_init(&thread->t_ctl_waitq);
2788 spin_lock(&svcpt->scp_lock);
2789 if (!ptlrpc_threads_increasable(svcpt)) {
2790 spin_unlock(&svcpt->scp_lock);
2791 OBD_FREE_PTR(thread);
2795 if (svcpt->scp_nthrs_starting != 0) {
2796 /* serialize starting because some modules (obdfilter)
2797 * might require unique and contiguous t_id */
2798 LASSERT(svcpt->scp_nthrs_starting == 1);
2799 spin_unlock(&svcpt->scp_lock);
2800 OBD_FREE_PTR(thread);
2802 CDEBUG(D_INFO, "Waiting for creating thread %s #%d\n",
2803 svc->srv_thread_name, svcpt->scp_thr_nextid);
2808 CDEBUG(D_INFO, "Creating thread %s #%d race, retry later\n",
2809 svc->srv_thread_name, svcpt->scp_thr_nextid);
2813 svcpt->scp_nthrs_starting++;
2814 thread->t_id = svcpt->scp_thr_nextid++;
2815 thread_add_flags(thread, SVC_STARTING);
2816 thread->t_svcpt = svcpt;
2818 cfs_list_add(&thread->t_link, &svcpt->scp_threads);
2819 spin_unlock(&svcpt->scp_lock);
2821 if (svcpt->scp_cpt >= 0) {
2822 snprintf(thread->t_name, PTLRPC_THR_NAME_LEN, "%s%02d_%03d",
2823 svc->srv_thread_name, svcpt->scp_cpt, thread->t_id);
2825 snprintf(thread->t_name, PTLRPC_THR_NAME_LEN, "%s_%04d",
2826 svc->srv_thread_name, thread->t_id);
2829 CDEBUG(D_RPCTRACE, "starting thread '%s'\n", thread->t_name);
2831 * CLONE_VM and CLONE_FILES just avoid a needless copy, because we
2832 * just drop the VM and FILES in cfs_daemonize_ctxt() right away.
2834 rc = cfs_create_thread(ptlrpc_main, thread, CFS_DAEMON_FLAGS);
2836 CERROR("cannot start thread '%s': rc %d\n",
2837 thread->t_name, rc);
2838 spin_lock(&svcpt->scp_lock);
2839 cfs_list_del(&thread->t_link);
2840 --svcpt->scp_nthrs_starting;
2841 spin_unlock(&svcpt->scp_lock);
2843 OBD_FREE(thread, sizeof(*thread));
2850 l_wait_event(thread->t_ctl_waitq,
2851 thread_is_running(thread) || thread_is_stopped(thread),
2854 rc = thread_is_stopped(thread) ? thread->t_id : 0;
2858 int ptlrpc_hr_init(void)
2860 struct ptlrpc_hr_partition *hrp;
2861 struct ptlrpc_hr_thread *hrt;
2867 memset(&ptlrpc_hr, 0, sizeof(ptlrpc_hr));
2868 ptlrpc_hr.hr_cpt_table = cfs_cpt_table;
2870 ptlrpc_hr.hr_partitions = cfs_percpt_alloc(ptlrpc_hr.hr_cpt_table,
2872 if (ptlrpc_hr.hr_partitions == NULL)
2875 cfs_waitq_init(&ptlrpc_hr.hr_waitq);
2877 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2880 cfs_atomic_set(&hrp->hrp_nstarted, 0);
2881 cfs_atomic_set(&hrp->hrp_nstopped, 0);
2883 hrp->hrp_nthrs = cfs_cpt_weight(ptlrpc_hr.hr_cpt_table, i);
2884 hrp->hrp_nthrs /= cfs_cpu_ht_nsiblings(0);
2886 LASSERT(hrp->hrp_nthrs > 0);
2887 OBD_CPT_ALLOC(hrp->hrp_thrs, ptlrpc_hr.hr_cpt_table, i,
2888 hrp->hrp_nthrs * sizeof(*hrt));
2889 if (hrp->hrp_thrs == NULL)
2890 GOTO(out, rc = -ENOMEM);
2892 for (j = 0; j < hrp->hrp_nthrs; j++) {
2893 hrt = &hrp->hrp_thrs[j];
2896 hrt->hrt_partition = hrp;
2897 cfs_waitq_init(&hrt->hrt_waitq);
2898 spin_lock_init(&hrt->hrt_lock);
2899 CFS_INIT_LIST_HEAD(&hrt->hrt_queue);
2903 rc = ptlrpc_start_hr_threads();
2910 void ptlrpc_hr_fini(void)
2912 struct ptlrpc_hr_partition *hrp;
2915 if (ptlrpc_hr.hr_partitions == NULL)
2918 ptlrpc_stop_hr_threads();
2920 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2921 if (hrp->hrp_thrs != NULL) {
2922 OBD_FREE(hrp->hrp_thrs,
2923 hrp->hrp_nthrs * sizeof(hrp->hrp_thrs[0]));
2927 cfs_percpt_free(ptlrpc_hr.hr_partitions);
2928 ptlrpc_hr.hr_partitions = NULL;
2931 #endif /* __KERNEL__ */
2934 * Wait until all already scheduled replies are processed.
2936 static void ptlrpc_wait_replies(struct ptlrpc_service_part *svcpt)
2940 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10),
2943 rc = l_wait_event(svcpt->scp_waitq,
2944 cfs_atomic_read(&svcpt->scp_nreps_difficult) == 0, &lwi);
2947 CWARN("Unexpectedly long timeout %s %p\n",
2948 svcpt->scp_service->srv_name, svcpt->scp_service);
2953 ptlrpc_service_del_atimer(struct ptlrpc_service *svc)
2955 struct ptlrpc_service_part *svcpt;
2958 /* early disarm AT timer... */
2959 ptlrpc_service_for_each_part(svcpt, i, svc) {
2960 if (svcpt->scp_service != NULL)
2961 cfs_timer_disarm(&svcpt->scp_at_timer);
2966 ptlrpc_service_unlink_rqbd(struct ptlrpc_service *svc)
2968 struct ptlrpc_service_part *svcpt;
2969 struct ptlrpc_request_buffer_desc *rqbd;
2970 struct l_wait_info lwi;
2974 /* All history will be culled when the next request buffer is
2975 * freed in ptlrpc_service_purge_all() */
2976 svc->srv_hist_nrqbds_cpt_max = 0;
2978 rc = LNetClearLazyPortal(svc->srv_req_portal);
2981 ptlrpc_service_for_each_part(svcpt, i, svc) {
2982 if (svcpt->scp_service == NULL)
2985 /* Unlink all the request buffers. This forces a 'final'
2986 * event with its 'unlink' flag set for each posted rqbd */
2987 cfs_list_for_each_entry(rqbd, &svcpt->scp_rqbd_posted,
2989 rc = LNetMDUnlink(rqbd->rqbd_md_h);
2990 LASSERT(rc == 0 || rc == -ENOENT);
2994 ptlrpc_service_for_each_part(svcpt, i, svc) {
2995 if (svcpt->scp_service == NULL)
2998 /* Wait for the network to release any buffers
2999 * it's currently filling */
3000 spin_lock(&svcpt->scp_lock);
3001 while (svcpt->scp_nrqbds_posted != 0) {
3002 spin_unlock(&svcpt->scp_lock);
3003 /* Network access will complete in finite time but
3004 * the HUGE timeout lets us CWARN for visibility
3005 * of sluggish NALs */
3006 lwi = LWI_TIMEOUT_INTERVAL(
3007 cfs_time_seconds(LONG_UNLINK),
3008 cfs_time_seconds(1), NULL, NULL);
3009 rc = l_wait_event(svcpt->scp_waitq,
3010 svcpt->scp_nrqbds_posted == 0, &lwi);
3011 if (rc == -ETIMEDOUT) {
3012 CWARN("Service %s waiting for "
3013 "request buffers\n",
3014 svcpt->scp_service->srv_name);
3016 spin_lock(&svcpt->scp_lock);
3018 spin_unlock(&svcpt->scp_lock);
3023 ptlrpc_service_purge_all(struct ptlrpc_service *svc)
3025 struct ptlrpc_service_part *svcpt;
3026 struct ptlrpc_request_buffer_desc *rqbd;
3027 struct ptlrpc_request *req;
3028 struct ptlrpc_reply_state *rs;
3031 ptlrpc_service_for_each_part(svcpt, i, svc) {
3032 if (svcpt->scp_service == NULL)
3035 spin_lock(&svcpt->scp_rep_lock);
3036 while (!cfs_list_empty(&svcpt->scp_rep_active)) {
3037 rs = cfs_list_entry(svcpt->scp_rep_active.next,
3038 struct ptlrpc_reply_state, rs_list);
3039 spin_lock(&rs->rs_lock);
3040 ptlrpc_schedule_difficult_reply(rs);
3041 spin_unlock(&rs->rs_lock);
3043 spin_unlock(&svcpt->scp_rep_lock);
3045 /* purge the request queue. NB No new replies (rqbds
3046 * all unlinked) and no service threads, so I'm the only
3047 * thread noodling the request queue now */
3048 while (!cfs_list_empty(&svcpt->scp_req_incoming)) {
3049 req = cfs_list_entry(svcpt->scp_req_incoming.next,
3050 struct ptlrpc_request, rq_list);
3052 cfs_list_del(&req->rq_list);
3053 svcpt->scp_nreqs_incoming--;
3054 svcpt->scp_nreqs_active++;
3055 ptlrpc_server_finish_request(svcpt, req);
3058 while (ptlrpc_server_request_pending(svcpt, 1)) {
3059 req = ptlrpc_server_request_get(svcpt, 1);
3060 cfs_list_del(&req->rq_list);
3061 svcpt->scp_nreqs_active++;
3062 ptlrpc_server_hpreq_fini(req);
3064 if (req->rq_export != NULL)
3065 class_export_rpc_put(req->rq_export);
3066 ptlrpc_server_finish_request(svcpt, req);
3069 LASSERT(cfs_list_empty(&svcpt->scp_rqbd_posted));
3070 LASSERT(svcpt->scp_nreqs_incoming == 0);
3071 LASSERT(svcpt->scp_nreqs_active == 0);
3072 /* history should have been culled by
3073 * ptlrpc_server_finish_request */
3074 LASSERT(svcpt->scp_hist_nrqbds == 0);
3076 /* Now free all the request buffers since nothing
3077 * references them any more... */
3079 while (!cfs_list_empty(&svcpt->scp_rqbd_idle)) {
3080 rqbd = cfs_list_entry(svcpt->scp_rqbd_idle.next,
3081 struct ptlrpc_request_buffer_desc,
3083 ptlrpc_free_rqbd(rqbd);
3085 ptlrpc_wait_replies(svcpt);
3087 while (!cfs_list_empty(&svcpt->scp_rep_idle)) {
3088 rs = cfs_list_entry(svcpt->scp_rep_idle.next,
3089 struct ptlrpc_reply_state,
3091 cfs_list_del(&rs->rs_list);
3092 OBD_FREE_LARGE(rs, svc->srv_max_reply_size);
3098 ptlrpc_service_free(struct ptlrpc_service *svc)
3100 struct ptlrpc_service_part *svcpt;
3101 struct ptlrpc_at_array *array;
3104 ptlrpc_service_for_each_part(svcpt, i, svc) {
3105 if (svcpt->scp_service == NULL)
3108 /* In case somebody rearmed this in the meantime */
3109 cfs_timer_disarm(&svcpt->scp_at_timer);
3110 array = &svcpt->scp_at_array;
3112 if (array->paa_reqs_array != NULL) {
3113 OBD_FREE(array->paa_reqs_array,
3114 sizeof(cfs_list_t) * array->paa_size);
3115 array->paa_reqs_array = NULL;
3118 if (array->paa_reqs_count != NULL) {
3119 OBD_FREE(array->paa_reqs_count,
3120 sizeof(__u32) * array->paa_size);
3121 array->paa_reqs_count = NULL;
3125 ptlrpc_service_for_each_part(svcpt, i, svc)
3126 OBD_FREE_PTR(svcpt);
3128 if (svc->srv_cpts != NULL)
3129 cfs_expr_list_values_free(svc->srv_cpts, svc->srv_ncpts);
3131 OBD_FREE(svc, offsetof(struct ptlrpc_service,
3132 srv_parts[svc->srv_ncpts]));
3135 int ptlrpc_unregister_service(struct ptlrpc_service *service)
3139 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
3141 service->srv_is_stopping = 1;
3143 spin_lock(&ptlrpc_all_services_lock);
3144 cfs_list_del_init(&service->srv_list);
3145 spin_unlock(&ptlrpc_all_services_lock);
3147 ptlrpc_lprocfs_unregister_service(service);
3149 ptlrpc_service_del_atimer(service);
3150 ptlrpc_stop_all_threads(service);
3152 ptlrpc_service_unlink_rqbd(service);
3153 ptlrpc_service_purge_all(service);
3154 ptlrpc_service_free(service);
3158 EXPORT_SYMBOL(ptlrpc_unregister_service);
3161 * Returns 0 if the service is healthy.
3163 * Right now, it just checks to make sure that requests aren't languishing
3164 * in the queue. We'll use this health check to govern whether a node needs
3165 * to be shot, so it's intentionally non-aggressive. */
3166 int ptlrpc_svcpt_health_check(struct ptlrpc_service_part *svcpt)
3168 struct ptlrpc_request *request;
3169 struct timeval right_now;
3172 cfs_gettimeofday(&right_now);
3174 spin_lock(&svcpt->scp_req_lock);
3175 if (!ptlrpc_server_request_pending(svcpt, 1)) {
3176 spin_unlock(&svcpt->scp_req_lock);
3180 /* How long has the next entry been waiting? */
3181 if (cfs_list_empty(&svcpt->scp_req_pending)) {
3182 request = cfs_list_entry(svcpt->scp_hreq_pending.next,
3183 struct ptlrpc_request, rq_list);
3185 request = cfs_list_entry(svcpt->scp_req_pending.next,
3186 struct ptlrpc_request, rq_list);
3189 timediff = cfs_timeval_sub(&right_now, &request->rq_arrival_time, NULL);
3190 spin_unlock(&svcpt->scp_req_lock);
3192 if ((timediff / ONE_MILLION) >
3193 (AT_OFF ? obd_timeout * 3 / 2 : at_max)) {
3194 CERROR("%s: unhealthy - request has been waiting %lds\n",
3195 svcpt->scp_service->srv_name, timediff / ONE_MILLION);
3203 ptlrpc_service_health_check(struct ptlrpc_service *svc)
3205 struct ptlrpc_service_part *svcpt;
3208 if (svc == NULL || svc->srv_parts == NULL)
3211 ptlrpc_service_for_each_part(svcpt, i, svc) {
3212 int rc = ptlrpc_svcpt_health_check(svcpt);
3219 EXPORT_SYMBOL(ptlrpc_service_health_check);