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,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2014, 2016, Intel Corporation.
25 * Copyright 2012 Xyratex Technology Limited
30 * Network Request Scheduler (NRS)
32 * Allows to reorder the handling of RPCs at servers.
34 * Author: Liang Zhen <liang@whamcloud.com>
35 * Author: Nikitas Angelinas <nikitas_angelinas@xyratex.com>
42 #define DEBUG_SUBSYSTEM S_RPC
43 #include <obd_support.h>
44 #include <obd_class.h>
45 #include <lustre_net.h>
46 #include <lprocfs_status.h>
47 #include <libcfs/libcfs.h>
48 #include "ptlrpc_internal.h"
53 struct nrs_core nrs_core;
55 static int nrs_policy_init(struct ptlrpc_nrs_policy *policy)
57 return policy->pol_desc->pd_ops->op_policy_init != NULL ?
58 policy->pol_desc->pd_ops->op_policy_init(policy) : 0;
61 static void nrs_policy_fini(struct ptlrpc_nrs_policy *policy)
63 LASSERT(policy->pol_ref == 0);
64 LASSERT(refcount_read(&policy->pol_start_ref) == 0);
65 LASSERT(policy->pol_req_queued == 0);
67 if (policy->pol_desc->pd_ops->op_policy_fini != NULL)
68 policy->pol_desc->pd_ops->op_policy_fini(policy);
71 static int nrs_policy_ctl_locked(struct ptlrpc_nrs_policy *policy,
72 enum ptlrpc_nrs_ctl opc, void *arg)
75 * The policy may be stopped, but the lprocfs files and
76 * ptlrpc_nrs_policy instances remain present until unregistration time.
77 * Do not perform the ctl operation if the policy is stopped, as
78 * policy->pol_private will be NULL in such a case.
80 if (policy->pol_state == NRS_POL_STATE_STOPPED)
83 RETURN(policy->pol_desc->pd_ops->op_policy_ctl != NULL ?
84 policy->pol_desc->pd_ops->op_policy_ctl(policy, opc, arg) :
88 static void nrs_policy_stop0(struct ptlrpc_nrs_policy *policy)
92 if (policy->pol_desc->pd_ops->op_policy_stop != NULL)
93 policy->pol_desc->pd_ops->op_policy_stop(policy);
95 LASSERT(list_empty(&policy->pol_list_queued));
96 LASSERT(policy->pol_req_queued == 0 &&
97 policy->pol_req_started == 0);
99 policy->pol_private = NULL;
100 policy->pol_arg[0] = '\0';
102 policy->pol_state = NRS_POL_STATE_STOPPED;
103 wake_up(&policy->pol_wq);
105 if (atomic_dec_and_test(&policy->pol_desc->pd_refs))
106 module_put(policy->pol_desc->pd_owner);
112 * Increases the policy's usage started reference count.
114 static inline void nrs_policy_started_get(struct ptlrpc_nrs_policy *policy)
116 refcount_inc(&policy->pol_start_ref);
120 * Decreases the policy's usage started reference count, and stops the policy
121 * in case it was already stopping and have no more outstanding usage
122 * references (which indicates it has no more queued or started requests, and
123 * can be safely stopped).
125 static void nrs_policy_started_put(struct ptlrpc_nrs_policy *policy)
127 if (refcount_dec_and_test(&policy->pol_start_ref))
128 nrs_policy_stop0(policy);
131 static int nrs_policy_stop_locked(struct ptlrpc_nrs_policy *policy)
133 struct ptlrpc_nrs *nrs = policy->pol_nrs;
136 if (nrs->nrs_policy_fallback == policy && !nrs->nrs_stopping)
139 if (policy->pol_state == NRS_POL_STATE_STARTING)
142 /* In progress or already stopped */
143 if (policy->pol_state != NRS_POL_STATE_STARTED)
146 policy->pol_state = NRS_POL_STATE_STOPPING;
148 /* Immediately make it invisible */
149 if (nrs->nrs_policy_primary == policy) {
150 nrs->nrs_policy_primary = NULL;
153 LASSERT(nrs->nrs_policy_fallback == policy);
154 nrs->nrs_policy_fallback = NULL;
157 /* Drop started ref and wait for requests to be drained */
158 spin_unlock(&nrs->nrs_lock);
159 nrs_policy_started_put(policy);
161 wait_event_timeout(policy->pol_wq,
162 policy->pol_state == NRS_POL_STATE_STOPPED,
163 cfs_time_seconds(30));
165 spin_lock(&nrs->nrs_lock);
167 if (policy->pol_state != NRS_POL_STATE_STOPPED)
174 * Transitions the \a nrs NRS head's primary policy to
175 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING and if the policy has no
176 * pending usage references, to ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED.
178 * \param[in] nrs the NRS head to carry out this operation on
180 static void nrs_policy_stop_primary(struct ptlrpc_nrs *nrs)
182 struct ptlrpc_nrs_policy *tmp = nrs->nrs_policy_primary;
187 * XXX: This should really be RETURN_EXIT, but the latter does
188 * not currently print anything out, and possibly should be
195 nrs->nrs_policy_primary = NULL;
197 LASSERT(tmp->pol_state == NRS_POL_STATE_STARTED);
198 tmp->pol_state = NRS_POL_STATE_STOPPING;
200 /* Drop started ref to free the policy */
201 spin_unlock(&nrs->nrs_lock);
202 nrs_policy_started_put(tmp);
203 spin_lock(&nrs->nrs_lock);
208 * Transitions a policy across the ptlrpc_nrs_pol_state range of values, in
209 * response to an lprocfs command to start a policy.
211 * If a primary policy different to the current one is specified, this function
212 * will transition the new policy to the
213 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTING and then to
214 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED, and will then transition
215 * the old primary policy (if there is one) to
216 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING, and if there are no outstanding
217 * references on the policy to ptlrpc_nrs_pol_stae::NRS_POL_STATE_STOPPED.
219 * If the fallback policy is specified, this is taken to indicate an instruction
220 * to stop the current primary policy, without substituting it with another
221 * primary policy, so the primary policy (if any) is transitioned to
222 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING, and if there are no outstanding
223 * references on the policy to ptlrpc_nrs_pol_stae::NRS_POL_STATE_STOPPED. In
224 * this case, the fallback policy is only left active in the NRS head.
226 static int nrs_policy_start_locked(struct ptlrpc_nrs_policy *policy, char *arg)
228 struct ptlrpc_nrs *nrs = policy->pol_nrs;
233 * Don't allow multiple starting which is too complex, and has no real
236 if (nrs->nrs_policy_starting)
239 LASSERT(policy->pol_state != NRS_POL_STATE_STARTING);
241 if (policy->pol_state == NRS_POL_STATE_STOPPING)
244 if (arg && strlen(arg) >= sizeof(policy->pol_arg)) {
245 CWARN("NRS: arg '%s' is too long\n", arg);
249 if (policy->pol_flags & PTLRPC_NRS_FL_FALLBACK) {
251 * This is for cases in which the user sets the policy to the
252 * fallback policy (currently fifo for all services); i.e. the
253 * user is resetting the policy to the default; so we stop the
254 * primary policy, if any.
256 if (policy == nrs->nrs_policy_fallback) {
257 nrs_policy_stop_primary(nrs);
262 * If we reach here, we must be setting up the fallback policy
263 * at service startup time, and only a single policy with the
264 * nrs_policy_flags::PTLRPC_NRS_FL_FALLBACK flag set can
265 * register with NRS core.
267 LASSERT(nrs->nrs_policy_fallback == NULL);
270 * Shouldn't start primary policy if w/o fallback policy.
272 if (nrs->nrs_policy_fallback == NULL)
275 if (policy->pol_state == NRS_POL_STATE_STARTED) {
277 * If the policy argument now is different from the last time,
278 * stop the policy first and start it again with the new
281 if ((arg == NULL && strlen(policy->pol_arg) == 0) ||
282 (arg != NULL && strcmp(policy->pol_arg, arg) == 0))
285 rc = nrs_policy_stop_locked(policy);
292 * Increase the module usage count for policies registering from other
295 if (atomic_inc_return(&policy->pol_desc->pd_refs) == 1 &&
296 !try_module_get(policy->pol_desc->pd_owner)) {
297 atomic_dec(&policy->pol_desc->pd_refs);
298 CERROR("NRS: cannot get module for policy %s; is it alive?\n",
299 policy->pol_desc->pd_name);
304 * Serialize policy starting across the NRS head
306 nrs->nrs_policy_starting = 1;
308 policy->pol_state = NRS_POL_STATE_STARTING;
310 if (policy->pol_desc->pd_ops->op_policy_start) {
311 spin_unlock(&nrs->nrs_lock);
313 rc = policy->pol_desc->pd_ops->op_policy_start(policy, arg);
315 spin_lock(&nrs->nrs_lock);
317 if (atomic_dec_and_test(&policy->pol_desc->pd_refs))
318 module_put(policy->pol_desc->pd_owner);
320 policy->pol_state = NRS_POL_STATE_STOPPED;
326 strlcpy(policy->pol_arg, arg, sizeof(policy->pol_arg));
328 /* take the started reference */
329 refcount_set(&policy->pol_start_ref, 1);
330 policy->pol_state = NRS_POL_STATE_STARTED;
332 if (policy->pol_flags & PTLRPC_NRS_FL_FALLBACK) {
334 * This path is only used at PTLRPC service setup time.
336 nrs->nrs_policy_fallback = policy;
339 * Try to stop the current primary policy if there is one.
341 nrs_policy_stop_primary(nrs);
344 * And set the newly-started policy as the primary one.
346 nrs->nrs_policy_primary = policy;
350 nrs->nrs_policy_starting = 0;
356 * Increases the policy's usage reference count (caller count).
358 static inline void nrs_policy_get_locked(struct ptlrpc_nrs_policy *policy)
359 __must_hold(&policy->pol_nrs->nrs_lock)
365 * Decreases the policy's usage reference count.
367 static void nrs_policy_put_locked(struct ptlrpc_nrs_policy *policy)
368 __must_hold(&policy->pol_nrs->nrs_lock)
370 LASSERT(policy->pol_ref > 0);
376 * Find and return a policy by name.
378 static struct ptlrpc_nrs_policy * nrs_policy_find_locked(struct ptlrpc_nrs *nrs,
381 struct ptlrpc_nrs_policy *tmp;
383 list_for_each_entry(tmp, &nrs->nrs_policy_list, pol_list) {
384 if (strncmp(tmp->pol_desc->pd_name, name,
385 NRS_POL_NAME_MAX) == 0) {
386 nrs_policy_get_locked(tmp);
394 * Release references for the resource hierarchy moving upwards towards the
395 * policy instance resource.
397 static void nrs_resource_put(struct ptlrpc_nrs_resource *res)
399 struct ptlrpc_nrs_policy *policy = res->res_policy;
401 if (policy->pol_desc->pd_ops->op_res_put != NULL) {
402 struct ptlrpc_nrs_resource *parent;
404 for (; res != NULL; res = parent) {
405 parent = res->res_parent;
406 policy->pol_desc->pd_ops->op_res_put(policy, res);
412 * Obtains references for each resource in the resource hierarchy for request
413 * \a nrq if it is to be handled by \a policy.
415 * \param[in] policy the policy
416 * \param[in] nrq the request
417 * \param[in] moving_req denotes whether this is a call to the function by
418 * ldlm_lock_reorder_req(), in order to move \a nrq to
419 * the high-priority NRS head; we should not sleep when
422 * \retval NULL resource hierarchy references not obtained
423 * \retval valid-pointer the bottom level of the resource hierarchy
425 * \see ptlrpc_nrs_pol_ops::op_res_get()
428 struct ptlrpc_nrs_resource * nrs_resource_get(struct ptlrpc_nrs_policy *policy,
429 struct ptlrpc_nrs_request *nrq,
433 * Set to NULL to traverse the resource hierarchy from the top.
435 struct ptlrpc_nrs_resource *res = NULL;
436 struct ptlrpc_nrs_resource *tmp = NULL;
440 rc = policy->pol_desc->pd_ops->op_res_get(policy, nrq, res,
444 nrs_resource_put(res);
448 LASSERT(tmp != NULL);
449 tmp->res_parent = res;
450 tmp->res_policy = policy;
454 * Return once we have obtained a reference to the bottom level
455 * of the resource hierarchy.
463 * Obtains resources for the resource hierarchies and policy references for
464 * the fallback and current primary policy (if any), that will later be used
465 * to handle request \a nrq.
467 * \param[in] nrs the NRS head instance that will be handling request \a nrq.
468 * \param[in] nrq the request that is being handled.
469 * \param[out] resp the array where references to the resource hierarchy are
471 * \param[in] moving_req is set when obtaining resources while moving a
472 * request from a policy on the regular NRS head to a
473 * policy on the HP NRS head (via
474 * ldlm_lock_reorder_req()). It signifies that
475 * allocations to get resources should be atomic; for
476 * a full explanation, see comment in
477 * ptlrpc_nrs_pol_ops::op_res_get().
479 static void nrs_resource_get_safe(struct ptlrpc_nrs *nrs,
480 struct ptlrpc_nrs_request *nrq,
481 struct ptlrpc_nrs_resource **resp,
484 struct ptlrpc_nrs_policy *primary = NULL;
485 struct ptlrpc_nrs_policy *fallback = NULL;
487 memset(resp, 0, sizeof(resp[0]) * NRS_RES_MAX);
490 * Obtain policy references.
492 spin_lock(&nrs->nrs_lock);
494 fallback = nrs->nrs_policy_fallback;
495 nrs_policy_started_get(fallback);
497 primary = nrs->nrs_policy_primary;
499 nrs_policy_started_get(primary);
501 spin_unlock(&nrs->nrs_lock);
504 * Obtain resource hierarchy references.
506 resp[NRS_RES_FALLBACK] = nrs_resource_get(fallback, nrq, moving_req);
507 LASSERT(resp[NRS_RES_FALLBACK] != NULL);
509 if (primary != NULL) {
510 resp[NRS_RES_PRIMARY] = nrs_resource_get(primary, nrq,
513 * A primary policy may exist which may not wish to serve a
514 * particular request for different reasons; release the
515 * reference on the policy as it will not be used for this
518 if (resp[NRS_RES_PRIMARY] == NULL)
519 nrs_policy_started_put(primary);
524 * Releases references to resource hierarchies and policies, because they are no
525 * longer required; used when request handling has been completed, or the
526 * request is moving to the high priority NRS head.
528 * \param resp the resource hierarchy that is being released
530 * \see ptlrpcnrs_req_hp_move()
531 * \see ptlrpc_nrs_req_finalize()
533 static void nrs_resource_put_safe(struct ptlrpc_nrs_resource **resp)
535 struct ptlrpc_nrs_policy *pols[NRS_RES_MAX];
538 for (i = 0; i < NRS_RES_MAX; i++) {
539 if (resp[i] != NULL) {
540 pols[i] = resp[i]->res_policy;
541 nrs_resource_put(resp[i]);
548 for (i = 0; i < NRS_RES_MAX; i++) {
552 nrs_policy_started_put(pols[i]);
557 * Obtains an NRS request from \a policy for handling or examination; the
558 * request should be removed in the 'handling' case.
560 * Calling into this function implies we already know the policy has a request
561 * waiting to be handled.
563 * \param[in] policy the policy from which a request
564 * \param[in] peek when set, signifies that we just want to examine the
565 * request, and not handle it, so the request is not removed
567 * \param[in] force when set, it will force a policy to return a request if it
570 * \retval the NRS request to be handled
573 struct ptlrpc_nrs_request * nrs_request_get(struct ptlrpc_nrs_policy *policy,
574 bool peek, bool force)
576 struct ptlrpc_nrs_request *nrq;
578 LASSERT(policy->pol_req_queued > 0);
580 /* for a non-started policy, use force mode to drain requests */
581 if (unlikely(policy->pol_state != NRS_POL_STATE_STARTED))
584 nrq = policy->pol_desc->pd_ops->op_req_get(policy, peek, force);
586 LASSERT(ergo(nrq != NULL, nrs_request_policy(nrq) == policy));
592 * Enqueues request \a nrq for later handling, via one one the policies for
593 * which resources where earlier obtained via nrs_resource_get_safe(). The
594 * function attempts to enqueue the request first on the primary policy
595 * (if any), since this is the preferred choice.
597 * \param nrq the request being enqueued
599 * \see nrs_resource_get_safe()
601 static inline void nrs_request_enqueue(struct ptlrpc_nrs_request *nrq)
603 struct ptlrpc_nrs_policy *policy;
608 * Try in descending order, because the primary policy (if any) is
609 * the preferred choice.
611 for (i = NRS_RES_MAX - 1; i >= 0; i--) {
612 if (nrq->nr_res_ptrs[i] == NULL)
616 policy = nrq->nr_res_ptrs[i]->res_policy;
618 rc = policy->pol_desc->pd_ops->op_req_enqueue(policy, nrq);
620 policy->pol_nrs->nrs_req_queued++;
621 policy->pol_req_queued++;
623 * Take an extra ref to avoid stopping policy with
624 * pending request in it
626 nrs_policy_started_get(policy);
631 * Should never get here, as at least the primary policy's
632 * ptlrpc_nrs_pol_ops::op_req_enqueue() implementation should always
639 * Called when a request has been handled
641 * \param[in] nrs the request that has been handled; can be used for
642 * job/resource control.
644 * \see ptlrpc_nrs_req_stop_nolock()
646 static inline void nrs_request_stop(struct ptlrpc_nrs_request *nrq)
648 struct ptlrpc_nrs_policy *policy = nrs_request_policy(nrq);
650 if (policy->pol_desc->pd_ops->op_req_stop)
651 policy->pol_desc->pd_ops->op_req_stop(policy, nrq);
653 LASSERT(policy->pol_nrs->nrs_req_started > 0);
654 LASSERT(policy->pol_req_started > 0);
656 policy->pol_nrs->nrs_req_started--;
657 policy->pol_req_started--;
661 * Handler for operations that can be carried out on policies.
663 * Handles opcodes that are common to all policy types within NRS core, and
664 * passes any unknown opcodes to the policy-specific control function.
666 * \param[in] nrs the NRS head this policy belongs to.
667 * \param[in] name the human-readable policy name; should be the same as
668 * ptlrpc_nrs_pol_desc::pd_name.
669 * \param[in] opc the opcode of the operation being carried out.
670 * \param[in,out] arg can be used to pass information in and out between when
671 * carrying an operation; usually data that is private to
672 * the policy at some level, or generic policy status
675 * \retval -ve error condition
676 * \retval 0 operation was carried out successfully
678 static int nrs_policy_ctl(struct ptlrpc_nrs *nrs, char *name,
679 enum ptlrpc_nrs_ctl opc, void *arg)
681 struct ptlrpc_nrs_policy *policy;
685 spin_lock(&nrs->nrs_lock);
687 policy = nrs_policy_find_locked(nrs, name);
689 GOTO(out, rc = -ENOENT);
691 if (policy->pol_state != NRS_POL_STATE_STARTED &&
692 policy->pol_state != NRS_POL_STATE_STOPPED)
693 GOTO(out, rc = -EAGAIN);
697 * Unknown opcode, pass it down to the policy-specific control
698 * function for handling.
701 rc = nrs_policy_ctl_locked(policy, opc, arg);
707 case PTLRPC_NRS_CTL_START:
708 rc = nrs_policy_start_locked(policy, arg);
713 nrs_policy_put_locked(policy);
715 spin_unlock(&nrs->nrs_lock);
721 * Unregisters a policy by name.
723 * \param[in] nrs the NRS head this policy belongs to.
724 * \param[in] name the human-readable policy name; should be the same as
725 * ptlrpc_nrs_pol_desc::pd_name
730 static int nrs_policy_unregister(struct ptlrpc_nrs *nrs, char *name)
732 struct ptlrpc_nrs_policy *policy = NULL;
736 spin_lock(&nrs->nrs_lock);
738 policy = nrs_policy_find_locked(nrs, name);
739 if (policy == NULL) {
741 CERROR("NRS: cannot find policy '%s': rc = %d\n", name, rc);
742 GOTO(out_unlock, rc);
745 if (policy->pol_ref > 1) {
747 CERROR("NRS: policy '%s' is busy with %ld references: rc = %d",
748 name, policy->pol_ref, rc);
752 LASSERT(policy->pol_req_queued == 0);
753 LASSERT(policy->pol_req_started == 0);
755 if (policy->pol_state != NRS_POL_STATE_STOPPED) {
756 rc = nrs_policy_stop_locked(policy);
758 CERROR("NRS: failed to stop policy '%s' with refcount %d: rc = %d\n",
759 name, refcount_read(&policy->pol_start_ref), rc);
764 LASSERT(policy->pol_private == NULL);
765 list_del(&policy->pol_list);
770 nrs_policy_put_locked(policy);
772 spin_unlock(&nrs->nrs_lock);
775 nrs_policy_fini(policy);
776 OBD_FREE_PTR(policy);
783 * Register a policy from \policy descriptor \a desc with NRS head \a nrs.
785 * \param[in] nrs the NRS head on which the policy will be registered.
786 * \param[in] desc the policy descriptor from which the information will be
787 * obtained to register the policy.
792 static int nrs_policy_register(struct ptlrpc_nrs *nrs,
793 struct ptlrpc_nrs_pol_desc *desc)
795 struct ptlrpc_nrs_policy *policy;
796 struct ptlrpc_nrs_policy *tmp;
797 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
801 LASSERT(svcpt != NULL);
802 LASSERT(desc->pd_ops != NULL);
803 LASSERT(desc->pd_ops->op_res_get != NULL);
804 LASSERT(desc->pd_ops->op_req_get != NULL);
805 LASSERT(desc->pd_ops->op_req_enqueue != NULL);
806 LASSERT(desc->pd_ops->op_req_dequeue != NULL);
807 LASSERT(desc->pd_compat != NULL);
809 OBD_CPT_ALLOC_GFP(policy, svcpt->scp_service->srv_cptable,
810 svcpt->scp_cpt, sizeof(*policy), GFP_NOFS);
814 policy->pol_nrs = nrs;
815 policy->pol_desc = desc;
816 policy->pol_state = NRS_POL_STATE_STOPPED;
817 policy->pol_flags = desc->pd_flags;
819 INIT_LIST_HEAD(&policy->pol_list);
820 INIT_LIST_HEAD(&policy->pol_list_queued);
822 init_waitqueue_head(&policy->pol_wq);
824 rc = nrs_policy_init(policy);
826 OBD_FREE_PTR(policy);
830 spin_lock(&nrs->nrs_lock);
832 tmp = nrs_policy_find_locked(nrs, policy->pol_desc->pd_name);
834 CERROR("NRS policy %s has been registered, can't register it "
835 "for %s\n", policy->pol_desc->pd_name,
836 svcpt->scp_service->srv_name);
837 nrs_policy_put_locked(tmp);
839 spin_unlock(&nrs->nrs_lock);
840 nrs_policy_fini(policy);
841 OBD_FREE_PTR(policy);
846 list_add_tail(&policy->pol_list, &nrs->nrs_policy_list);
849 if (policy->pol_flags & PTLRPC_NRS_FL_REG_START)
850 rc = nrs_policy_start_locked(policy, NULL);
852 spin_unlock(&nrs->nrs_lock);
855 (void) nrs_policy_unregister(nrs, policy->pol_desc->pd_name);
861 * Enqueue request \a req using one of the policies its resources are referring
864 * \param[in] req the request to enqueue.
866 static void ptlrpc_nrs_req_add_nolock(struct ptlrpc_request *req)
868 struct ptlrpc_nrs_policy *policy;
870 LASSERT(req->rq_nrq.nr_initialized);
871 LASSERT(!req->rq_nrq.nr_enqueued);
873 nrs_request_enqueue(&req->rq_nrq);
874 req->rq_nrq.nr_enqueued = 1;
876 policy = nrs_request_policy(&req->rq_nrq);
878 * Add the policy to the NRS head's list of policies with enqueued
879 * requests, if it has not been added there.
881 if (unlikely(list_empty(&policy->pol_list_queued)))
882 list_add_tail(&policy->pol_list_queued,
883 &policy->pol_nrs->nrs_policy_queued);
887 * Enqueue a request on the high priority NRS head.
889 * \param req the request to enqueue.
891 static void ptlrpc_nrs_hpreq_add_nolock(struct ptlrpc_request *req)
893 int opc = lustre_msg_get_opc(req->rq_reqmsg);
896 spin_lock(&req->rq_lock);
898 ptlrpc_nrs_req_add_nolock(req);
900 DEBUG_REQ(D_NET, req, "high priority req");
901 spin_unlock(&req->rq_lock);
906 * Returns a boolean predicate indicating whether the policy described by
907 * \a desc is adequate for use with service \a svc.
909 * \param[in] svc the service
910 * \param[in] desc the policy descriptor
912 * \retval false the policy is not compatible with the service
913 * \retval true the policy is compatible with the service
915 static inline bool nrs_policy_compatible(const struct ptlrpc_service *svc,
916 const struct ptlrpc_nrs_pol_desc *desc)
918 return desc->pd_compat(svc, desc);
922 * Registers all compatible policies in nrs_core.nrs_policies, for NRS head
925 * \param[in] nrs the NRS head
930 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
932 * \see ptlrpc_service_nrs_setup()
934 static int nrs_register_policies_locked(struct ptlrpc_nrs *nrs)
936 struct ptlrpc_nrs_pol_desc *desc;
937 /* for convenience */
938 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
939 struct ptlrpc_service *svc = svcpt->scp_service;
943 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
945 list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
946 if (nrs_policy_compatible(svc, desc)) {
947 rc = nrs_policy_register(nrs, desc);
949 CERROR("Failed to register NRS policy %s for "
950 "partition %d of service %s: %d\n",
951 desc->pd_name, svcpt->scp_cpt,
954 * Fail registration if any of the policies'
955 * registration fails.
966 * Initializes NRS head \a nrs of service partition \a svcpt, and registers all
967 * compatible policies in NRS core, with the NRS head.
969 * \param[in] nrs the NRS head
970 * \param[in] svcpt the PTLRPC service partition to setup
975 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
977 static int nrs_svcpt_setup_locked0(struct ptlrpc_nrs *nrs,
978 struct ptlrpc_service_part *svcpt)
981 enum ptlrpc_nrs_queue_type queue;
983 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
985 if (nrs == &svcpt->scp_nrs_reg)
986 queue = PTLRPC_NRS_QUEUE_REG;
987 else if (nrs == svcpt->scp_nrs_hp)
988 queue = PTLRPC_NRS_QUEUE_HP;
992 nrs->nrs_svcpt = svcpt;
993 nrs->nrs_queue_type = queue;
994 spin_lock_init(&nrs->nrs_lock);
995 INIT_LIST_HEAD(&nrs->nrs_policy_list);
996 INIT_LIST_HEAD(&nrs->nrs_policy_queued);
997 nrs->nrs_throttling = 0;
999 rc = nrs_register_policies_locked(nrs);
1005 * Allocates a regular and optionally a high-priority NRS head (if the service
1006 * handles high-priority RPCs), and then registers all available compatible
1007 * policies on those NRS heads.
1009 * \param[in,out] svcpt the PTLRPC service partition to setup
1011 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
1013 static int nrs_svcpt_setup_locked(struct ptlrpc_service_part *svcpt)
1015 struct ptlrpc_nrs *nrs;
1019 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
1022 * Initialize the regular NRS head.
1024 nrs = nrs_svcpt2nrs(svcpt, false);
1025 rc = nrs_svcpt_setup_locked0(nrs, svcpt);
1030 * Optionally allocate a high-priority NRS head.
1032 if (svcpt->scp_service->srv_ops.so_hpreq_handler == NULL)
1035 OBD_CPT_ALLOC_PTR(svcpt->scp_nrs_hp,
1036 svcpt->scp_service->srv_cptable,
1038 if (svcpt->scp_nrs_hp == NULL)
1039 GOTO(out, rc = -ENOMEM);
1041 nrs = nrs_svcpt2nrs(svcpt, true);
1042 rc = nrs_svcpt_setup_locked0(nrs, svcpt);
1049 * Unregisters all policies on all available NRS heads in a service partition;
1050 * called at PTLRPC service unregistration time.
1052 * \param[in] svcpt the PTLRPC service partition
1054 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
1056 static void nrs_svcpt_cleanup_locked(struct ptlrpc_service_part *svcpt)
1058 struct ptlrpc_nrs *nrs;
1059 struct ptlrpc_nrs_policy *policy;
1060 struct ptlrpc_nrs_policy *tmp;
1065 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
1068 /* scp_nrs_hp could be NULL due to short of memory. */
1069 nrs = hp ? svcpt->scp_nrs_hp : &svcpt->scp_nrs_reg;
1070 /* check the nrs_svcpt to see if nrs is initialized. */
1071 if (!nrs || !nrs->nrs_svcpt) {
1075 nrs->nrs_stopping = 1;
1077 list_for_each_entry_safe(policy, tmp, &nrs->nrs_policy_list,
1079 rc = nrs_policy_unregister(nrs, policy->pol_desc->pd_name);
1084 * If the service partition has an HP NRS head, clean that up as well.
1086 if (!hp && nrs_svcpt_has_hp(svcpt)) {
1098 * Returns the descriptor for a policy as identified by by \a name.
1100 * \param[in] name the policy name
1102 * \retval the policy descriptor
1105 static struct ptlrpc_nrs_pol_desc *nrs_policy_find_desc_locked(const char *name)
1107 struct ptlrpc_nrs_pol_desc *tmp;
1110 list_for_each_entry(tmp, &nrs_core.nrs_policies, pd_list) {
1111 if (strncmp(tmp->pd_name, name, NRS_POL_NAME_MAX) == 0)
1118 * Removes the policy from all supported NRS heads of all partitions of all
1121 * \param[in] desc the policy descriptor to unregister
1124 * \retval 0 successfully unregistered policy on all supported NRS heads
1126 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
1127 * \pre mutex_is_locked(&ptlrpc_all_services_mutex)
1129 static int nrs_policy_unregister_locked(struct ptlrpc_nrs_pol_desc *desc)
1131 struct ptlrpc_nrs *nrs;
1132 struct ptlrpc_service *svc;
1133 struct ptlrpc_service_part *svcpt;
1138 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
1139 LASSERT(mutex_is_locked(&ptlrpc_all_services_mutex));
1141 list_for_each_entry(svc, &ptlrpc_all_services, srv_list) {
1143 if (!nrs_policy_compatible(svc, desc) ||
1144 unlikely(svc->srv_is_stopping))
1147 ptlrpc_service_for_each_part(svcpt, i, svc) {
1151 nrs = nrs_svcpt2nrs(svcpt, hp);
1152 rc = nrs_policy_unregister(nrs, desc->pd_name);
1154 * Ignore -ENOENT as the policy may not have registered
1155 * successfully on all service partitions.
1157 if (rc == -ENOENT) {
1159 } else if (rc != 0) {
1160 CERROR("Failed to unregister NRS policy %s for "
1161 "partition %d of service %s: %d\n",
1162 desc->pd_name, svcpt->scp_cpt,
1163 svcpt->scp_service->srv_name, rc);
1167 if (!hp && nrs_svc_has_hp(svc)) {
1173 if (desc->pd_ops->op_lprocfs_fini != NULL)
1174 desc->pd_ops->op_lprocfs_fini(svc);
1181 * Registers a new policy with NRS core.
1183 * The function will only succeed if policy registration with all compatible
1184 * service partitions (if any) is successful.
1186 * N.B. This function should be called either at ptlrpc module initialization
1187 * time when registering a policy that ships with NRS core, or in a
1188 * module's init() function for policies registering from other modules.
1190 * \param[in] conf configuration information for the new policy to register
1195 static int ptlrpc_nrs_policy_register(struct ptlrpc_nrs_pol_conf *conf)
1197 struct ptlrpc_service *svc;
1198 struct ptlrpc_nrs_pol_desc *desc;
1202 LASSERT(conf != NULL);
1203 LASSERT(conf->nc_ops != NULL);
1204 LASSERT(conf->nc_compat != NULL);
1205 LASSERT(ergo(conf->nc_compat == nrs_policy_compat_one,
1206 conf->nc_compat_svc_name != NULL));
1207 LASSERT(ergo((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) != 0,
1208 conf->nc_owner != NULL));
1210 conf->nc_name[NRS_POL_NAME_MAX - 1] = '\0';
1213 * External policies are not allowed to start immediately upon
1214 * registration, as there is a relatively higher chance that their
1215 * registration might fail. In such a case, some policy instances may
1216 * already have requests queued wen unregistration needs to happen as
1217 * part o cleanup; since there is currently no way to drain requests
1218 * from a policy unless the service is unregistering, we just disallow
1221 if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) &&
1222 (conf->nc_flags & (PTLRPC_NRS_FL_FALLBACK |
1223 PTLRPC_NRS_FL_REG_START))) {
1224 CERROR("NRS: failing to register policy %s. Please check "
1225 "policy flags; external policies cannot act as fallback "
1226 "policies, or be started immediately upon registration "
1227 "without interaction with lprocfs\n", conf->nc_name);
1231 mutex_lock(&nrs_core.nrs_mutex);
1233 if (nrs_policy_find_desc_locked(conf->nc_name) != NULL) {
1234 CERROR("NRS: failing to register policy %s which has already "
1235 "been registered with NRS core!\n",
1237 GOTO(fail, rc = -EEXIST);
1240 OBD_ALLOC_PTR(desc);
1242 GOTO(fail, rc = -ENOMEM);
1244 if (strlcpy(desc->pd_name, conf->nc_name, sizeof(desc->pd_name)) >=
1245 sizeof(desc->pd_name)) {
1247 GOTO(fail, rc = -E2BIG);
1249 desc->pd_ops = conf->nc_ops;
1250 desc->pd_compat = conf->nc_compat;
1251 desc->pd_compat_svc_name = conf->nc_compat_svc_name;
1252 if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) != 0)
1253 desc->pd_owner = conf->nc_owner;
1254 desc->pd_flags = conf->nc_flags;
1255 atomic_set(&desc->pd_refs, 0);
1258 * For policies that are held in the same module as NRS (currently
1259 * ptlrpc), do not register the policy with all compatible services,
1260 * as the services will not have started at this point, since we are
1261 * calling from ptlrpc module initialization code. In such cases each
1262 * service will register all compatible policies later, via
1263 * ptlrpc_service_nrs_setup().
1265 if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) == 0)
1269 * Register the new policy on all compatible services
1271 mutex_lock(&ptlrpc_all_services_mutex);
1273 list_for_each_entry(svc, &ptlrpc_all_services, srv_list) {
1274 struct ptlrpc_service_part *svcpt;
1278 if (!nrs_policy_compatible(svc, desc) ||
1279 unlikely(svc->srv_is_stopping))
1282 ptlrpc_service_for_each_part(svcpt, i, svc) {
1283 struct ptlrpc_nrs *nrs;
1286 nrs = nrs_svcpt2nrs(svcpt, hp);
1287 rc = nrs_policy_register(nrs, desc);
1289 CERROR("Failed to register NRS policy %s for "
1290 "partition %d of service %s: %d\n",
1291 desc->pd_name, svcpt->scp_cpt,
1292 svcpt->scp_service->srv_name, rc);
1294 rc2 = nrs_policy_unregister_locked(desc);
1296 * Should not fail at this point
1299 mutex_unlock(&ptlrpc_all_services_mutex);
1304 if (!hp && nrs_svc_has_hp(svc)) {
1311 * No need to take a reference to other modules here, as we
1312 * will be calling from the module's init() function.
1314 if (desc->pd_ops->op_lprocfs_init != NULL) {
1315 rc = desc->pd_ops->op_lprocfs_init(svc);
1317 rc2 = nrs_policy_unregister_locked(desc);
1319 * Should not fail at this point
1322 mutex_unlock(&ptlrpc_all_services_mutex);
1329 mutex_unlock(&ptlrpc_all_services_mutex);
1331 list_add_tail(&desc->pd_list, &nrs_core.nrs_policies);
1333 mutex_unlock(&nrs_core.nrs_mutex);
1339 * Setup NRS heads on all service partitions of service \a svc, and register
1340 * all compatible policies on those NRS heads.
1342 * To be called from withing ptl
1343 * \param[in] svc the service to setup
1345 * \retval -ve error, the calling logic should eventually call
1346 * ptlrpc_service_nrs_cleanup() to undo any work performed
1349 * \see ptlrpc_register_service()
1350 * \see ptlrpc_service_nrs_cleanup()
1352 int ptlrpc_service_nrs_setup(struct ptlrpc_service *svc)
1354 struct ptlrpc_service_part *svcpt;
1355 const struct ptlrpc_nrs_pol_desc *desc;
1359 mutex_lock(&nrs_core.nrs_mutex);
1362 * Initialize NRS heads on all service CPTs.
1364 ptlrpc_service_for_each_part(svcpt, i, svc) {
1365 rc = nrs_svcpt_setup_locked(svcpt);
1371 * Set up lprocfs interfaces for all supported policies for the
1374 list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
1375 if (!nrs_policy_compatible(svc, desc))
1378 if (desc->pd_ops->op_lprocfs_init != NULL) {
1379 rc = desc->pd_ops->op_lprocfs_init(svc);
1387 mutex_unlock(&nrs_core.nrs_mutex);
1393 * Unregisters all policies on all service partitions of service \a svc.
1395 * \param[in] svc the PTLRPC service to unregister
1397 void ptlrpc_service_nrs_cleanup(struct ptlrpc_service *svc)
1399 struct ptlrpc_service_part *svcpt;
1400 const struct ptlrpc_nrs_pol_desc *desc;
1403 mutex_lock(&nrs_core.nrs_mutex);
1406 * Clean up NRS heads on all service partitions
1408 ptlrpc_service_for_each_part(svcpt, i, svc)
1409 nrs_svcpt_cleanup_locked(svcpt);
1412 * Clean up lprocfs interfaces for all supported policies for the
1415 list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
1416 if (!nrs_policy_compatible(svc, desc))
1419 if (desc->pd_ops->op_lprocfs_fini != NULL)
1420 desc->pd_ops->op_lprocfs_fini(svc);
1423 mutex_unlock(&nrs_core.nrs_mutex);
1427 * Obtains NRS head resources for request \a req.
1429 * These could be either on the regular or HP NRS head of \a svcpt; resources
1430 * taken on the regular head can later be swapped for HP head resources by
1431 * ldlm_lock_reorder_req().
1433 * \param[in] svcpt the service partition
1434 * \param[in] req the request
1435 * \param[in] hp which NRS head of \a svcpt to use
1437 void ptlrpc_nrs_req_initialize(struct ptlrpc_service_part *svcpt,
1438 struct ptlrpc_request *req, bool hp)
1440 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1442 memset(&req->rq_nrq, 0, sizeof(req->rq_nrq));
1443 nrs_resource_get_safe(nrs, &req->rq_nrq, req->rq_nrq.nr_res_ptrs,
1447 * It is fine to access \e nr_initialized without locking as there is
1448 * no contention at this early stage.
1450 req->rq_nrq.nr_initialized = 1;
1454 * Releases resources for a request; is called after the request has been
1457 * \param[in] req the request
1459 * \see ptlrpc_server_finish_request()
1461 void ptlrpc_nrs_req_finalize(struct ptlrpc_request *req)
1463 if (req->rq_nrq.nr_initialized) {
1464 nrs_resource_put_safe(req->rq_nrq.nr_res_ptrs);
1465 /* no protection on bit nr_initialized because no
1466 * contention at this late stage */
1467 req->rq_nrq.nr_finalized = 1;
1471 void ptlrpc_nrs_req_stop_nolock(struct ptlrpc_request *req)
1473 if (req->rq_nrq.nr_started)
1474 nrs_request_stop(&req->rq_nrq);
1478 * Enqueues request \a req on either the regular or high-priority NRS head
1479 * of service partition \a svcpt.
1481 * \param[in] svcpt the service partition
1482 * \param[in] req the request to be enqueued
1483 * \param[in] hp whether to enqueue the request on the regular or
1484 * high-priority NRS head.
1486 void ptlrpc_nrs_req_add(struct ptlrpc_service_part *svcpt,
1487 struct ptlrpc_request *req, bool hp)
1489 spin_lock(&svcpt->scp_req_lock);
1492 ptlrpc_nrs_hpreq_add_nolock(req);
1494 ptlrpc_nrs_req_add_nolock(req);
1496 spin_unlock(&svcpt->scp_req_lock);
1499 static void nrs_request_removed(struct ptlrpc_nrs_policy *policy)
1501 LASSERT(policy->pol_nrs->nrs_req_queued > 0);
1502 LASSERT(policy->pol_req_queued > 0);
1504 policy->pol_nrs->nrs_req_queued--;
1505 policy->pol_req_queued--;
1508 * If the policy has no more requests queued, remove it from
1509 * ptlrpc_nrs::nrs_policy_queued.
1511 if (unlikely(policy->pol_req_queued == 0)) {
1512 list_del_init(&policy->pol_list_queued);
1515 * If there are other policies with queued requests, move the
1516 * current policy to the end so that we can round robin over
1517 * all policies and drain the requests.
1519 } else if (policy->pol_req_queued != policy->pol_nrs->nrs_req_queued) {
1520 LASSERT(policy->pol_req_queued <
1521 policy->pol_nrs->nrs_req_queued);
1523 list_move_tail(&policy->pol_list_queued,
1524 &policy->pol_nrs->nrs_policy_queued);
1527 /* remove the extra ref for policy pending requests */
1528 nrs_policy_started_put(policy);
1532 * Obtains a request for handling from an NRS head of service partition
1535 * \param[in] svcpt the service partition
1536 * \param[in] hp whether to obtain a request from the regular or
1537 * high-priority NRS head.
1538 * \param[in] peek when set, signifies that we just want to examine the
1539 * request, and not handle it, so the request is not removed
1541 * \param[in] force when set, it will force a policy to return a request if it
1544 * \retval the request to be handled
1545 * \retval NULL the head has no requests to serve
1547 struct ptlrpc_request *
1548 ptlrpc_nrs_req_get_nolock0(struct ptlrpc_service_part *svcpt, bool hp,
1549 bool peek, bool force)
1551 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1552 struct ptlrpc_nrs_policy *policy;
1553 struct ptlrpc_nrs_request *nrq;
1556 * Always try to drain requests from all NRS polices even if they are
1557 * inactive, because the user can change policy status at runtime.
1559 list_for_each_entry(policy, &nrs->nrs_policy_queued,
1561 nrq = nrs_request_get(policy, peek, force);
1563 if (likely(!peek)) {
1564 nrq->nr_started = 1;
1566 policy->pol_req_started++;
1567 policy->pol_nrs->nrs_req_started++;
1569 nrs_request_removed(policy);
1572 return container_of(nrq, struct ptlrpc_request, rq_nrq);
1580 * Dequeues request \a req from the policy it has been enqueued on.
1582 * \param[in] req the request
1584 void ptlrpc_nrs_req_del_nolock(struct ptlrpc_request *req)
1586 struct ptlrpc_nrs_policy *policy = nrs_request_policy(&req->rq_nrq);
1588 policy->pol_desc->pd_ops->op_req_dequeue(policy, &req->rq_nrq);
1590 req->rq_nrq.nr_enqueued = 0;
1592 nrs_request_removed(policy);
1596 * Returns whether there are any requests currently enqueued on any of the
1597 * policies of service partition's \a svcpt NRS head specified by \a hp. Should
1598 * be called while holding ptlrpc_service_part::scp_req_lock to get a reliable
1601 * \param[in] svcpt the service partition to enquire.
1602 * \param[in] hp whether the regular or high-priority NRS head is to be
1605 * \retval false the indicated NRS head has no enqueued requests.
1606 * \retval true the indicated NRS head has some enqueued requests.
1608 bool ptlrpc_nrs_req_pending_nolock(struct ptlrpc_service_part *svcpt, bool hp)
1610 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1612 return nrs->nrs_req_queued > 0;
1616 * Returns whether NRS policy is throttling reqeust
1618 * \param[in] svcpt the service partition to enquire.
1619 * \param[in] hp whether the regular or high-priority NRS head is to be
1622 * \retval false the indicated NRS head has no enqueued requests.
1623 * \retval true the indicated NRS head has some enqueued requests.
1625 bool ptlrpc_nrs_req_throttling_nolock(struct ptlrpc_service_part *svcpt,
1628 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1630 return !!nrs->nrs_throttling;
1634 * Moves request \a req from the regular to the high-priority NRS head.
1636 * \param[in] req the request to move
1638 void ptlrpc_nrs_req_hp_move(struct ptlrpc_request *req)
1640 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1641 struct ptlrpc_nrs_request *nrq = &req->rq_nrq;
1642 struct ptlrpc_nrs_resource *res1[NRS_RES_MAX];
1643 struct ptlrpc_nrs_resource *res2[NRS_RES_MAX];
1647 * Obtain the high-priority NRS head resources.
1649 nrs_resource_get_safe(nrs_svcpt2nrs(svcpt, true), nrq, res1, true);
1651 spin_lock(&svcpt->scp_req_lock);
1653 if (!ptlrpc_nrs_req_can_move(req))
1656 ptlrpc_nrs_req_del_nolock(req);
1658 memcpy(res2, nrq->nr_res_ptrs, NRS_RES_MAX * sizeof(res2[0]));
1659 memcpy(nrq->nr_res_ptrs, res1, NRS_RES_MAX * sizeof(res1[0]));
1661 ptlrpc_nrs_hpreq_add_nolock(req);
1663 memcpy(res1, res2, NRS_RES_MAX * sizeof(res1[0]));
1665 spin_unlock(&svcpt->scp_req_lock);
1668 * Release either the regular NRS head resources if we moved the
1669 * request, or the high-priority NRS head resources if we took a
1670 * reference earlier in this function and ptlrpc_nrs_req_can_move()
1673 nrs_resource_put_safe(res1);
1678 * Carries out a control operation \a opc on the policy identified by the
1679 * human-readable \a name, on either all partitions, or only on the first
1680 * partition of service \a svc.
1682 * \param[in] svc the service the policy belongs to.
1683 * \param[in] queue whether to carry out the command on the policy which
1684 * belongs to the regular, high-priority, or both NRS
1685 * heads of service partitions of \a svc.
1686 * \param[in] name the policy to act upon, by human-readable name
1687 * \param[in] opc the opcode of the operation to carry out
1688 * \param[in] single when set, the operation will only be carried out on the
1689 * NRS heads of the first service partition of \a svc.
1690 * This is useful for some policies which e.g. share
1691 * identical values on the same parameters of different
1692 * service partitions; when reading these parameters via
1693 * lprocfs, these policies may just want to obtain and
1694 * print out the values from the first service partition.
1695 * Storing these values centrally elsewhere then could be
1696 * another solution for this.
1697 * \param[in,out] arg can be used as a generic in/out buffer between control
1698 * operations and the user environment.
1700 *\retval -ve error condition
1701 *\retval 0 operation was carried out successfully
1703 int ptlrpc_nrs_policy_control(const struct ptlrpc_service *svc,
1704 enum ptlrpc_nrs_queue_type queue, char *name,
1705 enum ptlrpc_nrs_ctl opc, bool single, void *arg)
1707 struct ptlrpc_service_part *svcpt;
1712 LASSERT(opc != PTLRPC_NRS_CTL_INVALID);
1714 if ((queue & PTLRPC_NRS_QUEUE_BOTH) == 0)
1717 ptlrpc_service_for_each_part(svcpt, i, svc) {
1718 if ((queue & PTLRPC_NRS_QUEUE_REG) != 0) {
1719 rc = nrs_policy_ctl(nrs_svcpt2nrs(svcpt, false), name,
1721 if (rc != 0 || (queue == PTLRPC_NRS_QUEUE_REG &&
1726 if ((queue & PTLRPC_NRS_QUEUE_HP) != 0) {
1728 * XXX: We could optionally check for
1729 * nrs_svc_has_hp(svc) here, and return an error if it
1730 * is false. Right now we rely on the policies' lprocfs
1731 * handlers that call the present function to make this
1732 * check; if they fail to do so, they might hit the
1733 * assertion inside nrs_svcpt2nrs() below.
1735 rc = nrs_policy_ctl(nrs_svcpt2nrs(svcpt, true), name,
1737 if (rc != 0 || single)
1746 * Adds all policies that ship with the ptlrpc module, to NRS core's list of
1747 * policies \e nrs_core.nrs_policies.
1749 * \retval 0 all policies have been registered successfully
1752 int ptlrpc_nrs_init(void)
1757 mutex_init(&nrs_core.nrs_mutex);
1758 INIT_LIST_HEAD(&nrs_core.nrs_policies);
1760 rc = ptlrpc_nrs_policy_register(&nrs_conf_fifo);
1764 #ifdef HAVE_SERVER_SUPPORT
1765 rc = ptlrpc_nrs_policy_register(&nrs_conf_crrn);
1769 rc = ptlrpc_nrs_policy_register(&nrs_conf_orr);
1773 rc = ptlrpc_nrs_policy_register(&nrs_conf_trr);
1776 rc = ptlrpc_nrs_policy_register(&nrs_conf_tbf);
1779 #endif /* HAVE_SERVER_SUPPORT */
1781 rc = ptlrpc_nrs_policy_register(&nrs_conf_delay);
1788 * Since no PTLRPC services have been started at this point, all we need
1789 * to do for cleanup is to free the descriptors.
1797 * Removes all policy descriptors from nrs_core::nrs_policies, and frees the
1798 * policy descriptors.
1800 * Since all PTLRPC services are stopped at this point, there are no more
1801 * instances of any policies, because each service will have stopped its policy
1802 * instances in ptlrpc_service_nrs_cleanup(), so we just need to free the
1805 void ptlrpc_nrs_fini(void)
1807 struct ptlrpc_nrs_pol_desc *desc;
1808 struct ptlrpc_nrs_pol_desc *tmp;
1810 list_for_each_entry_safe(desc, tmp, &nrs_core.nrs_policies,
1812 list_del_init(&desc->pd_list);