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(policy->pol_req_queued == 0);
66 if (policy->pol_desc->pd_ops->op_policy_fini != NULL)
67 policy->pol_desc->pd_ops->op_policy_fini(policy);
70 static int nrs_policy_ctl_locked(struct ptlrpc_nrs_policy *policy,
71 enum ptlrpc_nrs_ctl opc, void *arg)
74 * The policy may be stopped, but the lprocfs files and
75 * ptlrpc_nrs_policy instances remain present until unregistration time.
76 * Do not perform the ctl operation if the policy is stopped, as
77 * policy->pol_private will be NULL in such a case.
79 if (policy->pol_state == NRS_POL_STATE_STOPPED)
82 RETURN(policy->pol_desc->pd_ops->op_policy_ctl != NULL ?
83 policy->pol_desc->pd_ops->op_policy_ctl(policy, opc, arg) :
87 static void nrs_policy_stop0(struct ptlrpc_nrs_policy *policy)
91 if (policy->pol_desc->pd_ops->op_policy_stop != NULL)
92 policy->pol_desc->pd_ops->op_policy_stop(policy);
94 LASSERT(list_empty(&policy->pol_list_queued));
95 LASSERT(policy->pol_req_queued == 0 &&
96 policy->pol_req_started == 0);
98 policy->pol_private = NULL;
100 policy->pol_state = NRS_POL_STATE_STOPPED;
102 if (atomic_dec_and_test(&policy->pol_desc->pd_refs))
103 module_put(policy->pol_desc->pd_owner);
108 static int nrs_policy_stop_locked(struct ptlrpc_nrs_policy *policy)
110 struct ptlrpc_nrs *nrs = policy->pol_nrs;
113 if (nrs->nrs_policy_fallback == policy && !nrs->nrs_stopping)
116 if (policy->pol_state == NRS_POL_STATE_STARTING)
119 /* In progress or already stopped */
120 if (policy->pol_state != NRS_POL_STATE_STARTED)
123 policy->pol_state = NRS_POL_STATE_STOPPING;
125 /* Immediately make it invisible */
126 if (nrs->nrs_policy_primary == policy) {
127 nrs->nrs_policy_primary = NULL;
130 LASSERT(nrs->nrs_policy_fallback == policy);
131 nrs->nrs_policy_fallback = NULL;
134 /* I have the only refcount */
135 if (policy->pol_ref == 1)
136 nrs_policy_stop0(policy);
142 * Transitions the \a nrs NRS head's primary policy to
143 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING and if the policy has no
144 * pending usage references, to ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED.
146 * \param[in] nrs the NRS head to carry out this operation on
148 static void nrs_policy_stop_primary(struct ptlrpc_nrs *nrs)
150 struct ptlrpc_nrs_policy *tmp = nrs->nrs_policy_primary;
155 * XXX: This should really be RETURN_EXIT, but the latter does
156 * not currently print anything out, and possibly should be
163 nrs->nrs_policy_primary = NULL;
165 LASSERT(tmp->pol_state == NRS_POL_STATE_STARTED);
166 tmp->pol_state = NRS_POL_STATE_STOPPING;
168 if (tmp->pol_ref == 0)
169 nrs_policy_stop0(tmp);
174 * Transitions a policy across the ptlrpc_nrs_pol_state range of values, in
175 * response to an lprocfs command to start a policy.
177 * If a primary policy different to the current one is specified, this function
178 * will transition the new policy to the
179 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTING and then to
180 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED, and will then transition
181 * the old primary policy (if there is one) to
182 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING, and if there are no outstanding
183 * references on the policy to ptlrpc_nrs_pol_stae::NRS_POL_STATE_STOPPED.
185 * If the fallback policy is specified, this is taken to indicate an instruction
186 * to stop the current primary policy, without substituting it with another
187 * primary policy, so the primary policy (if any) is transitioned to
188 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING, and if there are no outstanding
189 * references on the policy to ptlrpc_nrs_pol_stae::NRS_POL_STATE_STOPPED. In
190 * this case, the fallback policy is only left active in the NRS head.
192 static int nrs_policy_start_locked(struct ptlrpc_nrs_policy *policy, char *arg)
194 struct ptlrpc_nrs *nrs = policy->pol_nrs;
199 * Don't allow multiple starting which is too complex, and has no real
202 if (nrs->nrs_policy_starting)
205 LASSERT(policy->pol_state != NRS_POL_STATE_STARTING);
207 if (policy->pol_state == NRS_POL_STATE_STOPPING)
210 if (policy->pol_flags & PTLRPC_NRS_FL_FALLBACK) {
212 * This is for cases in which the user sets the policy to the
213 * fallback policy (currently fifo for all services); i.e. the
214 * user is resetting the policy to the default; so we stop the
215 * primary policy, if any.
217 if (policy == nrs->nrs_policy_fallback) {
218 nrs_policy_stop_primary(nrs);
223 * If we reach here, we must be setting up the fallback policy
224 * at service startup time, and only a single policy with the
225 * nrs_policy_flags::PTLRPC_NRS_FL_FALLBACK flag set can
226 * register with NRS core.
228 LASSERT(nrs->nrs_policy_fallback == NULL);
231 * Shouldn't start primary policy if w/o fallback policy.
233 if (nrs->nrs_policy_fallback == NULL)
236 if (policy->pol_state == NRS_POL_STATE_STARTED) {
238 * If the policy argument now is different from the last time,
239 * stop the policy first and start it again with the new
242 if ((arg != NULL) && (strlen(arg) >= NRS_POL_ARG_MAX))
245 if ((arg == NULL && strlen(policy->pol_arg) == 0) ||
246 (arg != NULL && strcmp(policy->pol_arg, arg) == 0))
249 rc = nrs_policy_stop_locked(policy);
256 * Increase the module usage count for policies registering from other
259 if (atomic_inc_return(&policy->pol_desc->pd_refs) == 1 &&
260 !try_module_get(policy->pol_desc->pd_owner)) {
261 atomic_dec(&policy->pol_desc->pd_refs);
262 CERROR("NRS: cannot get module for policy %s; is it alive?\n",
263 policy->pol_desc->pd_name);
268 * Serialize policy starting across the NRS head
270 nrs->nrs_policy_starting = 1;
272 policy->pol_state = NRS_POL_STATE_STARTING;
274 if (policy->pol_desc->pd_ops->op_policy_start) {
275 spin_unlock(&nrs->nrs_lock);
277 rc = policy->pol_desc->pd_ops->op_policy_start(policy, arg);
279 spin_lock(&nrs->nrs_lock);
281 if (atomic_dec_and_test(&policy->pol_desc->pd_refs))
282 module_put(policy->pol_desc->pd_owner);
284 policy->pol_state = NRS_POL_STATE_STOPPED;
290 if (strlcpy(policy->pol_arg, arg, sizeof(policy->pol_arg)) >=
291 sizeof(policy->pol_arg)) {
292 CERROR("NRS: arg '%s' is too long\n", arg);
293 GOTO(out, rc = -E2BIG);
297 policy->pol_state = NRS_POL_STATE_STARTED;
299 if (policy->pol_flags & PTLRPC_NRS_FL_FALLBACK) {
301 * This path is only used at PTLRPC service setup time.
303 nrs->nrs_policy_fallback = policy;
306 * Try to stop the current primary policy if there is one.
308 nrs_policy_stop_primary(nrs);
311 * And set the newly-started policy as the primary one.
313 nrs->nrs_policy_primary = policy;
317 nrs->nrs_policy_starting = 0;
323 * Increases the policy's usage reference count.
325 static inline void nrs_policy_get_locked(struct ptlrpc_nrs_policy *policy)
331 * Decreases the policy's usage reference count, and stops the policy in case it
332 * was already stopping and have no more outstanding usage references (which
333 * indicates it has no more queued or started requests, and can be safely
336 static void nrs_policy_put_locked(struct ptlrpc_nrs_policy *policy)
338 LASSERT(policy->pol_ref > 0);
341 if (unlikely(policy->pol_ref == 0 &&
342 policy->pol_state == NRS_POL_STATE_STOPPING))
343 nrs_policy_stop0(policy);
346 static void nrs_policy_put(struct ptlrpc_nrs_policy *policy)
348 spin_lock(&policy->pol_nrs->nrs_lock);
349 nrs_policy_put_locked(policy);
350 spin_unlock(&policy->pol_nrs->nrs_lock);
354 * Find and return a policy by name.
356 static struct ptlrpc_nrs_policy * nrs_policy_find_locked(struct ptlrpc_nrs *nrs,
359 struct ptlrpc_nrs_policy *tmp;
361 list_for_each_entry(tmp, &nrs->nrs_policy_list, pol_list) {
362 if (strncmp(tmp->pol_desc->pd_name, name,
363 NRS_POL_NAME_MAX) == 0) {
364 nrs_policy_get_locked(tmp);
372 * Release references for the resource hierarchy moving upwards towards the
373 * policy instance resource.
375 static void nrs_resource_put(struct ptlrpc_nrs_resource *res)
377 struct ptlrpc_nrs_policy *policy = res->res_policy;
379 if (policy->pol_desc->pd_ops->op_res_put != NULL) {
380 struct ptlrpc_nrs_resource *parent;
382 for (; res != NULL; res = parent) {
383 parent = res->res_parent;
384 policy->pol_desc->pd_ops->op_res_put(policy, res);
390 * Obtains references for each resource in the resource hierarchy for request
391 * \a nrq if it is to be handled by \a policy.
393 * \param[in] policy the policy
394 * \param[in] nrq the request
395 * \param[in] moving_req denotes whether this is a call to the function by
396 * ldlm_lock_reorder_req(), in order to move \a nrq to
397 * the high-priority NRS head; we should not sleep when
400 * \retval NULL resource hierarchy references not obtained
401 * \retval valid-pointer the bottom level of the resource hierarchy
403 * \see ptlrpc_nrs_pol_ops::op_res_get()
406 struct ptlrpc_nrs_resource * nrs_resource_get(struct ptlrpc_nrs_policy *policy,
407 struct ptlrpc_nrs_request *nrq,
411 * Set to NULL to traverse the resource hierarchy from the top.
413 struct ptlrpc_nrs_resource *res = NULL;
414 struct ptlrpc_nrs_resource *tmp = NULL;
418 rc = policy->pol_desc->pd_ops->op_res_get(policy, nrq, res,
422 nrs_resource_put(res);
426 LASSERT(tmp != NULL);
427 tmp->res_parent = res;
428 tmp->res_policy = policy;
432 * Return once we have obtained a reference to the bottom level
433 * of the resource hierarchy.
441 * Obtains resources for the resource hierarchies and policy references for
442 * the fallback and current primary policy (if any), that will later be used
443 * to handle request \a nrq.
445 * \param[in] nrs the NRS head instance that will be handling request \a nrq.
446 * \param[in] nrq the request that is being handled.
447 * \param[out] resp the array where references to the resource hierarchy are
449 * \param[in] moving_req is set when obtaining resources while moving a
450 * request from a policy on the regular NRS head to a
451 * policy on the HP NRS head (via
452 * ldlm_lock_reorder_req()). It signifies that
453 * allocations to get resources should be atomic; for
454 * a full explanation, see comment in
455 * ptlrpc_nrs_pol_ops::op_res_get().
457 static void nrs_resource_get_safe(struct ptlrpc_nrs *nrs,
458 struct ptlrpc_nrs_request *nrq,
459 struct ptlrpc_nrs_resource **resp,
462 struct ptlrpc_nrs_policy *primary = NULL;
463 struct ptlrpc_nrs_policy *fallback = NULL;
465 memset(resp, 0, sizeof(resp[0]) * NRS_RES_MAX);
468 * Obtain policy references.
470 spin_lock(&nrs->nrs_lock);
472 fallback = nrs->nrs_policy_fallback;
473 nrs_policy_get_locked(fallback);
475 primary = nrs->nrs_policy_primary;
477 nrs_policy_get_locked(primary);
479 spin_unlock(&nrs->nrs_lock);
482 * Obtain resource hierarchy references.
484 resp[NRS_RES_FALLBACK] = nrs_resource_get(fallback, nrq, moving_req);
485 LASSERT(resp[NRS_RES_FALLBACK] != NULL);
487 if (primary != NULL) {
488 resp[NRS_RES_PRIMARY] = nrs_resource_get(primary, nrq,
491 * A primary policy may exist which may not wish to serve a
492 * particular request for different reasons; release the
493 * reference on the policy as it will not be used for this
496 if (resp[NRS_RES_PRIMARY] == NULL)
497 nrs_policy_put(primary);
502 * Releases references to resource hierarchies and policies, because they are no
503 * longer required; used when request handling has been completed, or the
504 * request is moving to the high priority NRS head.
506 * \param resp the resource hierarchy that is being released
508 * \see ptlrpcnrs_req_hp_move()
509 * \see ptlrpc_nrs_req_finalize()
511 static void nrs_resource_put_safe(struct ptlrpc_nrs_resource **resp)
513 struct ptlrpc_nrs_policy *pols[NRS_RES_MAX];
514 struct ptlrpc_nrs *nrs = NULL;
517 for (i = 0; i < NRS_RES_MAX; i++) {
518 if (resp[i] != NULL) {
519 pols[i] = resp[i]->res_policy;
520 nrs_resource_put(resp[i]);
527 for (i = 0; i < NRS_RES_MAX; i++) {
532 nrs = pols[i]->pol_nrs;
533 spin_lock(&nrs->nrs_lock);
535 nrs_policy_put_locked(pols[i]);
539 spin_unlock(&nrs->nrs_lock);
543 * Obtains an NRS request from \a policy for handling or examination; the
544 * request should be removed in the 'handling' case.
546 * Calling into this function implies we already know the policy has a request
547 * waiting to be handled.
549 * \param[in] policy the policy from which a request
550 * \param[in] peek when set, signifies that we just want to examine the
551 * request, and not handle it, so the request is not removed
553 * \param[in] force when set, it will force a policy to return a request if it
556 * \retval the NRS request to be handled
559 struct ptlrpc_nrs_request * nrs_request_get(struct ptlrpc_nrs_policy *policy,
560 bool peek, bool force)
562 struct ptlrpc_nrs_request *nrq;
564 LASSERT(policy->pol_req_queued > 0);
566 nrq = policy->pol_desc->pd_ops->op_req_get(policy, peek, force);
568 LASSERT(ergo(nrq != NULL, nrs_request_policy(nrq) == policy));
574 * Enqueues request \a nrq for later handling, via one one the policies for
575 * which resources where earlier obtained via nrs_resource_get_safe(). The
576 * function attempts to enqueue the request first on the primary policy
577 * (if any), since this is the preferred choice.
579 * \param nrq the request being enqueued
581 * \see nrs_resource_get_safe()
583 static inline void nrs_request_enqueue(struct ptlrpc_nrs_request *nrq)
585 struct ptlrpc_nrs_policy *policy;
590 * Try in descending order, because the primary policy (if any) is
591 * the preferred choice.
593 for (i = NRS_RES_MAX - 1; i >= 0; i--) {
594 if (nrq->nr_res_ptrs[i] == NULL)
598 policy = nrq->nr_res_ptrs[i]->res_policy;
600 rc = policy->pol_desc->pd_ops->op_req_enqueue(policy, nrq);
602 policy->pol_nrs->nrs_req_queued++;
603 policy->pol_req_queued++;
608 * Should never get here, as at least the primary policy's
609 * ptlrpc_nrs_pol_ops::op_req_enqueue() implementation should always
616 * Called when a request has been handled
618 * \param[in] nrs the request that has been handled; can be used for
619 * job/resource control.
621 * \see ptlrpc_nrs_req_stop_nolock()
623 static inline void nrs_request_stop(struct ptlrpc_nrs_request *nrq)
625 struct ptlrpc_nrs_policy *policy = nrs_request_policy(nrq);
627 if (policy->pol_desc->pd_ops->op_req_stop)
628 policy->pol_desc->pd_ops->op_req_stop(policy, nrq);
630 LASSERT(policy->pol_nrs->nrs_req_started > 0);
631 LASSERT(policy->pol_req_started > 0);
633 policy->pol_nrs->nrs_req_started--;
634 policy->pol_req_started--;
638 * Handler for operations that can be carried out on policies.
640 * Handles opcodes that are common to all policy types within NRS core, and
641 * passes any unknown opcodes to the policy-specific control function.
643 * \param[in] nrs the NRS head this policy belongs to.
644 * \param[in] name the human-readable policy name; should be the same as
645 * ptlrpc_nrs_pol_desc::pd_name.
646 * \param[in] opc the opcode of the operation being carried out.
647 * \param[in,out] arg can be used to pass information in and out between when
648 * carrying an operation; usually data that is private to
649 * the policy at some level, or generic policy status
652 * \retval -ve error condition
653 * \retval 0 operation was carried out successfully
655 static int nrs_policy_ctl(struct ptlrpc_nrs *nrs, char *name,
656 enum ptlrpc_nrs_ctl opc, void *arg)
658 struct ptlrpc_nrs_policy *policy;
662 spin_lock(&nrs->nrs_lock);
664 policy = nrs_policy_find_locked(nrs, name);
666 GOTO(out, rc = -ENOENT);
668 if (policy->pol_state != NRS_POL_STATE_STARTED &&
669 policy->pol_state != NRS_POL_STATE_STOPPED)
670 GOTO(out, rc = -EAGAIN);
674 * Unknown opcode, pass it down to the policy-specific control
675 * function for handling.
678 rc = nrs_policy_ctl_locked(policy, opc, arg);
684 case PTLRPC_NRS_CTL_START:
685 rc = nrs_policy_start_locked(policy, arg);
690 nrs_policy_put_locked(policy);
692 spin_unlock(&nrs->nrs_lock);
698 * Unregisters a policy by name.
700 * \param[in] nrs the NRS head this policy belongs to.
701 * \param[in] name the human-readable policy name; should be the same as
702 * ptlrpc_nrs_pol_desc::pd_name
707 static int nrs_policy_unregister(struct ptlrpc_nrs *nrs, char *name)
709 struct ptlrpc_nrs_policy *policy = NULL;
712 spin_lock(&nrs->nrs_lock);
714 policy = nrs_policy_find_locked(nrs, name);
715 if (policy == NULL) {
716 spin_unlock(&nrs->nrs_lock);
718 CERROR("Can't find NRS policy %s\n", name);
722 if (policy->pol_ref > 1) {
723 CERROR("Policy %s is busy with %d references\n", name,
724 (int)policy->pol_ref);
725 nrs_policy_put_locked(policy);
727 spin_unlock(&nrs->nrs_lock);
731 LASSERT(policy->pol_req_queued == 0);
732 LASSERT(policy->pol_req_started == 0);
734 if (policy->pol_state != NRS_POL_STATE_STOPPED) {
735 nrs_policy_stop_locked(policy);
736 LASSERT(policy->pol_state == NRS_POL_STATE_STOPPED);
739 list_del(&policy->pol_list);
742 nrs_policy_put_locked(policy);
744 spin_unlock(&nrs->nrs_lock);
746 nrs_policy_fini(policy);
748 LASSERT(policy->pol_private == NULL);
749 OBD_FREE_PTR(policy);
755 * Register a policy from \policy descriptor \a desc with NRS head \a nrs.
757 * \param[in] nrs the NRS head on which the policy will be registered.
758 * \param[in] desc the policy descriptor from which the information will be
759 * obtained to register the policy.
764 static int nrs_policy_register(struct ptlrpc_nrs *nrs,
765 struct ptlrpc_nrs_pol_desc *desc)
767 struct ptlrpc_nrs_policy *policy;
768 struct ptlrpc_nrs_policy *tmp;
769 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
773 LASSERT(svcpt != NULL);
774 LASSERT(desc->pd_ops != NULL);
775 LASSERT(desc->pd_ops->op_res_get != NULL);
776 LASSERT(desc->pd_ops->op_req_get != NULL);
777 LASSERT(desc->pd_ops->op_req_enqueue != NULL);
778 LASSERT(desc->pd_ops->op_req_dequeue != NULL);
779 LASSERT(desc->pd_compat != NULL);
781 OBD_CPT_ALLOC_GFP(policy, svcpt->scp_service->srv_cptable,
782 svcpt->scp_cpt, sizeof(*policy), GFP_NOFS);
786 policy->pol_nrs = nrs;
787 policy->pol_desc = desc;
788 policy->pol_state = NRS_POL_STATE_STOPPED;
789 policy->pol_flags = desc->pd_flags;
791 INIT_LIST_HEAD(&policy->pol_list);
792 INIT_LIST_HEAD(&policy->pol_list_queued);
794 rc = nrs_policy_init(policy);
796 OBD_FREE_PTR(policy);
800 spin_lock(&nrs->nrs_lock);
802 tmp = nrs_policy_find_locked(nrs, policy->pol_desc->pd_name);
804 CERROR("NRS policy %s has been registered, can't register it "
805 "for %s\n", policy->pol_desc->pd_name,
806 svcpt->scp_service->srv_name);
807 nrs_policy_put_locked(tmp);
809 spin_unlock(&nrs->nrs_lock);
810 nrs_policy_fini(policy);
811 OBD_FREE_PTR(policy);
816 list_add_tail(&policy->pol_list, &nrs->nrs_policy_list);
819 if (policy->pol_flags & PTLRPC_NRS_FL_REG_START)
820 rc = nrs_policy_start_locked(policy, NULL);
822 spin_unlock(&nrs->nrs_lock);
825 (void) nrs_policy_unregister(nrs, policy->pol_desc->pd_name);
831 * Enqueue request \a req using one of the policies its resources are referring
834 * \param[in] req the request to enqueue.
836 static void ptlrpc_nrs_req_add_nolock(struct ptlrpc_request *req)
838 struct ptlrpc_nrs_policy *policy;
840 LASSERT(req->rq_nrq.nr_initialized);
841 LASSERT(!req->rq_nrq.nr_enqueued);
843 nrs_request_enqueue(&req->rq_nrq);
844 req->rq_nrq.nr_enqueued = 1;
846 policy = nrs_request_policy(&req->rq_nrq);
848 * Add the policy to the NRS head's list of policies with enqueued
849 * requests, if it has not been added there.
851 if (unlikely(list_empty(&policy->pol_list_queued)))
852 list_add_tail(&policy->pol_list_queued,
853 &policy->pol_nrs->nrs_policy_queued);
857 * Enqueue a request on the high priority NRS head.
859 * \param req the request to enqueue.
861 static void ptlrpc_nrs_hpreq_add_nolock(struct ptlrpc_request *req)
863 int opc = lustre_msg_get_opc(req->rq_reqmsg);
866 spin_lock(&req->rq_lock);
868 ptlrpc_nrs_req_add_nolock(req);
870 DEBUG_REQ(D_NET, req, "high priority req");
871 spin_unlock(&req->rq_lock);
876 * Returns a boolean predicate indicating whether the policy described by
877 * \a desc is adequate for use with service \a svc.
879 * \param[in] svc the service
880 * \param[in] desc the policy descriptor
882 * \retval false the policy is not compatible with the service
883 * \retval true the policy is compatible with the service
885 static inline bool nrs_policy_compatible(const struct ptlrpc_service *svc,
886 const struct ptlrpc_nrs_pol_desc *desc)
888 return desc->pd_compat(svc, desc);
892 * Registers all compatible policies in nrs_core.nrs_policies, for NRS head
895 * \param[in] nrs the NRS head
900 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
902 * \see ptlrpc_service_nrs_setup()
904 static int nrs_register_policies_locked(struct ptlrpc_nrs *nrs)
906 struct ptlrpc_nrs_pol_desc *desc;
907 /* for convenience */
908 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
909 struct ptlrpc_service *svc = svcpt->scp_service;
913 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
915 list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
916 if (nrs_policy_compatible(svc, desc)) {
917 rc = nrs_policy_register(nrs, desc);
919 CERROR("Failed to register NRS policy %s for "
920 "partition %d of service %s: %d\n",
921 desc->pd_name, svcpt->scp_cpt,
924 * Fail registration if any of the policies'
925 * registration fails.
936 * Initializes NRS head \a nrs of service partition \a svcpt, and registers all
937 * compatible policies in NRS core, with the NRS head.
939 * \param[in] nrs the NRS head
940 * \param[in] svcpt the PTLRPC service partition to setup
945 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
947 static int nrs_svcpt_setup_locked0(struct ptlrpc_nrs *nrs,
948 struct ptlrpc_service_part *svcpt)
951 enum ptlrpc_nrs_queue_type queue;
953 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
955 if (nrs == &svcpt->scp_nrs_reg)
956 queue = PTLRPC_NRS_QUEUE_REG;
957 else if (nrs == svcpt->scp_nrs_hp)
958 queue = PTLRPC_NRS_QUEUE_HP;
962 nrs->nrs_svcpt = svcpt;
963 nrs->nrs_queue_type = queue;
964 spin_lock_init(&nrs->nrs_lock);
965 INIT_LIST_HEAD(&nrs->nrs_policy_list);
966 INIT_LIST_HEAD(&nrs->nrs_policy_queued);
967 nrs->nrs_throttling = 0;
969 rc = nrs_register_policies_locked(nrs);
975 * Allocates a regular and optionally a high-priority NRS head (if the service
976 * handles high-priority RPCs), and then registers all available compatible
977 * policies on those NRS heads.
979 * \param[in,out] svcpt the PTLRPC service partition to setup
981 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
983 static int nrs_svcpt_setup_locked(struct ptlrpc_service_part *svcpt)
985 struct ptlrpc_nrs *nrs;
989 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
992 * Initialize the regular NRS head.
994 nrs = nrs_svcpt2nrs(svcpt, false);
995 rc = nrs_svcpt_setup_locked0(nrs, svcpt);
1000 * Optionally allocate a high-priority NRS head.
1002 if (svcpt->scp_service->srv_ops.so_hpreq_handler == NULL)
1005 OBD_CPT_ALLOC_PTR(svcpt->scp_nrs_hp,
1006 svcpt->scp_service->srv_cptable,
1008 if (svcpt->scp_nrs_hp == NULL)
1009 GOTO(out, rc = -ENOMEM);
1011 nrs = nrs_svcpt2nrs(svcpt, true);
1012 rc = nrs_svcpt_setup_locked0(nrs, svcpt);
1019 * Unregisters all policies on all available NRS heads in a service partition;
1020 * called at PTLRPC service unregistration time.
1022 * \param[in] svcpt the PTLRPC service partition
1024 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
1026 static void nrs_svcpt_cleanup_locked(struct ptlrpc_service_part *svcpt)
1028 struct ptlrpc_nrs *nrs;
1029 struct ptlrpc_nrs_policy *policy;
1030 struct ptlrpc_nrs_policy *tmp;
1035 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
1038 /* scp_nrs_hp could be NULL due to short of memory. */
1039 nrs = hp ? svcpt->scp_nrs_hp : &svcpt->scp_nrs_reg;
1040 /* check the nrs_svcpt to see if nrs is initialized. */
1041 if (!nrs || !nrs->nrs_svcpt) {
1045 nrs->nrs_stopping = 1;
1047 list_for_each_entry_safe(policy, tmp, &nrs->nrs_policy_list,
1049 rc = nrs_policy_unregister(nrs, policy->pol_desc->pd_name);
1054 * If the service partition has an HP NRS head, clean that up as well.
1056 if (!hp && nrs_svcpt_has_hp(svcpt)) {
1068 * Returns the descriptor for a policy as identified by by \a name.
1070 * \param[in] name the policy name
1072 * \retval the policy descriptor
1075 static struct ptlrpc_nrs_pol_desc *nrs_policy_find_desc_locked(const char *name)
1077 struct ptlrpc_nrs_pol_desc *tmp;
1080 list_for_each_entry(tmp, &nrs_core.nrs_policies, pd_list) {
1081 if (strncmp(tmp->pd_name, name, NRS_POL_NAME_MAX) == 0)
1088 * Removes the policy from all supported NRS heads of all partitions of all
1091 * \param[in] desc the policy descriptor to unregister
1094 * \retval 0 successfully unregistered policy on all supported NRS heads
1096 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
1097 * \pre mutex_is_locked(&ptlrpc_all_services_mutex)
1099 static int nrs_policy_unregister_locked(struct ptlrpc_nrs_pol_desc *desc)
1101 struct ptlrpc_nrs *nrs;
1102 struct ptlrpc_service *svc;
1103 struct ptlrpc_service_part *svcpt;
1108 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
1109 LASSERT(mutex_is_locked(&ptlrpc_all_services_mutex));
1111 list_for_each_entry(svc, &ptlrpc_all_services, srv_list) {
1113 if (!nrs_policy_compatible(svc, desc) ||
1114 unlikely(svc->srv_is_stopping))
1117 ptlrpc_service_for_each_part(svcpt, i, svc) {
1121 nrs = nrs_svcpt2nrs(svcpt, hp);
1122 rc = nrs_policy_unregister(nrs, desc->pd_name);
1124 * Ignore -ENOENT as the policy may not have registered
1125 * successfully on all service partitions.
1127 if (rc == -ENOENT) {
1129 } else if (rc != 0) {
1130 CERROR("Failed to unregister NRS policy %s for "
1131 "partition %d of service %s: %d\n",
1132 desc->pd_name, svcpt->scp_cpt,
1133 svcpt->scp_service->srv_name, rc);
1137 if (!hp && nrs_svc_has_hp(svc)) {
1143 if (desc->pd_ops->op_lprocfs_fini != NULL)
1144 desc->pd_ops->op_lprocfs_fini(svc);
1151 * Registers a new policy with NRS core.
1153 * The function will only succeed if policy registration with all compatible
1154 * service partitions (if any) is successful.
1156 * N.B. This function should be called either at ptlrpc module initialization
1157 * time when registering a policy that ships with NRS core, or in a
1158 * module's init() function for policies registering from other modules.
1160 * \param[in] conf configuration information for the new policy to register
1165 int ptlrpc_nrs_policy_register(struct ptlrpc_nrs_pol_conf *conf)
1167 struct ptlrpc_service *svc;
1168 struct ptlrpc_nrs_pol_desc *desc;
1172 LASSERT(conf != NULL);
1173 LASSERT(conf->nc_ops != NULL);
1174 LASSERT(conf->nc_compat != NULL);
1175 LASSERT(ergo(conf->nc_compat == nrs_policy_compat_one,
1176 conf->nc_compat_svc_name != NULL));
1177 LASSERT(ergo((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) != 0,
1178 conf->nc_owner != NULL));
1180 conf->nc_name[NRS_POL_NAME_MAX - 1] = '\0';
1183 * External policies are not allowed to start immediately upon
1184 * registration, as there is a relatively higher chance that their
1185 * registration might fail. In such a case, some policy instances may
1186 * already have requests queued wen unregistration needs to happen as
1187 * part o cleanup; since there is currently no way to drain requests
1188 * from a policy unless the service is unregistering, we just disallow
1191 if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) &&
1192 (conf->nc_flags & (PTLRPC_NRS_FL_FALLBACK |
1193 PTLRPC_NRS_FL_REG_START))) {
1194 CERROR("NRS: failing to register policy %s. Please check "
1195 "policy flags; external policies cannot act as fallback "
1196 "policies, or be started immediately upon registration "
1197 "without interaction with lprocfs\n", conf->nc_name);
1201 mutex_lock(&nrs_core.nrs_mutex);
1203 if (nrs_policy_find_desc_locked(conf->nc_name) != NULL) {
1204 CERROR("NRS: failing to register policy %s which has already "
1205 "been registered with NRS core!\n",
1207 GOTO(fail, rc = -EEXIST);
1210 OBD_ALLOC_PTR(desc);
1212 GOTO(fail, rc = -ENOMEM);
1214 if (strlcpy(desc->pd_name, conf->nc_name, sizeof(desc->pd_name)) >=
1215 sizeof(desc->pd_name)) {
1217 GOTO(fail, rc = -E2BIG);
1219 desc->pd_ops = conf->nc_ops;
1220 desc->pd_compat = conf->nc_compat;
1221 desc->pd_compat_svc_name = conf->nc_compat_svc_name;
1222 if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) != 0)
1223 desc->pd_owner = conf->nc_owner;
1224 desc->pd_flags = conf->nc_flags;
1225 atomic_set(&desc->pd_refs, 0);
1228 * For policies that are held in the same module as NRS (currently
1229 * ptlrpc), do not register the policy with all compatible services,
1230 * as the services will not have started at this point, since we are
1231 * calling from ptlrpc module initialization code. In such cases each
1232 * service will register all compatible policies later, via
1233 * ptlrpc_service_nrs_setup().
1235 if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) == 0)
1239 * Register the new policy on all compatible services
1241 mutex_lock(&ptlrpc_all_services_mutex);
1243 list_for_each_entry(svc, &ptlrpc_all_services, srv_list) {
1244 struct ptlrpc_service_part *svcpt;
1248 if (!nrs_policy_compatible(svc, desc) ||
1249 unlikely(svc->srv_is_stopping))
1252 ptlrpc_service_for_each_part(svcpt, i, svc) {
1253 struct ptlrpc_nrs *nrs;
1256 nrs = nrs_svcpt2nrs(svcpt, hp);
1257 rc = nrs_policy_register(nrs, desc);
1259 CERROR("Failed to register NRS policy %s for "
1260 "partition %d of service %s: %d\n",
1261 desc->pd_name, svcpt->scp_cpt,
1262 svcpt->scp_service->srv_name, rc);
1264 rc2 = nrs_policy_unregister_locked(desc);
1266 * Should not fail at this point
1269 mutex_unlock(&ptlrpc_all_services_mutex);
1274 if (!hp && nrs_svc_has_hp(svc)) {
1281 * No need to take a reference to other modules here, as we
1282 * will be calling from the module's init() function.
1284 if (desc->pd_ops->op_lprocfs_init != NULL) {
1285 rc = desc->pd_ops->op_lprocfs_init(svc);
1287 rc2 = nrs_policy_unregister_locked(desc);
1289 * Should not fail at this point
1292 mutex_unlock(&ptlrpc_all_services_mutex);
1299 mutex_unlock(&ptlrpc_all_services_mutex);
1301 list_add_tail(&desc->pd_list, &nrs_core.nrs_policies);
1303 mutex_unlock(&nrs_core.nrs_mutex);
1307 EXPORT_SYMBOL(ptlrpc_nrs_policy_register);
1310 * Unregisters a previously registered policy with NRS core. All instances of
1311 * the policy on all NRS heads of all supported services are removed.
1313 * N.B. This function should only be called from a module's exit() function.
1314 * Although it can be used for policies that ship alongside NRS core, the
1315 * function is primarily intended for policies that register externally,
1316 * from other modules.
1318 * \param[in] conf configuration information for the policy to unregister
1323 int ptlrpc_nrs_policy_unregister(struct ptlrpc_nrs_pol_conf *conf)
1325 struct ptlrpc_nrs_pol_desc *desc;
1329 LASSERT(conf != NULL);
1331 if (conf->nc_flags & PTLRPC_NRS_FL_FALLBACK) {
1332 CERROR("Unable to unregister a fallback policy, unless the "
1333 "PTLRPC service is stopping.\n");
1337 conf->nc_name[NRS_POL_NAME_MAX - 1] = '\0';
1339 mutex_lock(&nrs_core.nrs_mutex);
1341 desc = nrs_policy_find_desc_locked(conf->nc_name);
1343 CERROR("Failing to unregister NRS policy %s which has "
1344 "not been registered with NRS core!\n",
1346 GOTO(not_exist, rc = -ENOENT);
1349 mutex_lock(&ptlrpc_all_services_mutex);
1351 rc = nrs_policy_unregister_locked(desc);
1354 CERROR("Please first stop policy %s on all service "
1355 "partitions and then retry to unregister the "
1356 "policy.\n", conf->nc_name);
1360 CDEBUG(D_INFO, "Unregistering policy %s from NRS core.\n",
1363 list_del(&desc->pd_list);
1367 mutex_unlock(&ptlrpc_all_services_mutex);
1370 mutex_unlock(&nrs_core.nrs_mutex);
1374 EXPORT_SYMBOL(ptlrpc_nrs_policy_unregister);
1377 * Setup NRS heads on all service partitions of service \a svc, and register
1378 * all compatible policies on those NRS heads.
1380 * To be called from withing ptl
1381 * \param[in] svc the service to setup
1383 * \retval -ve error, the calling logic should eventually call
1384 * ptlrpc_service_nrs_cleanup() to undo any work performed
1387 * \see ptlrpc_register_service()
1388 * \see ptlrpc_service_nrs_cleanup()
1390 int ptlrpc_service_nrs_setup(struct ptlrpc_service *svc)
1392 struct ptlrpc_service_part *svcpt;
1393 const struct ptlrpc_nrs_pol_desc *desc;
1397 mutex_lock(&nrs_core.nrs_mutex);
1400 * Initialize NRS heads on all service CPTs.
1402 ptlrpc_service_for_each_part(svcpt, i, svc) {
1403 rc = nrs_svcpt_setup_locked(svcpt);
1409 * Set up lprocfs interfaces for all supported policies for the
1412 list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
1413 if (!nrs_policy_compatible(svc, desc))
1416 if (desc->pd_ops->op_lprocfs_init != NULL) {
1417 rc = desc->pd_ops->op_lprocfs_init(svc);
1425 mutex_unlock(&nrs_core.nrs_mutex);
1431 * Unregisters all policies on all service partitions of service \a svc.
1433 * \param[in] svc the PTLRPC service to unregister
1435 void ptlrpc_service_nrs_cleanup(struct ptlrpc_service *svc)
1437 struct ptlrpc_service_part *svcpt;
1438 const struct ptlrpc_nrs_pol_desc *desc;
1441 mutex_lock(&nrs_core.nrs_mutex);
1444 * Clean up NRS heads on all service partitions
1446 ptlrpc_service_for_each_part(svcpt, i, svc)
1447 nrs_svcpt_cleanup_locked(svcpt);
1450 * Clean up lprocfs interfaces for all supported policies for the
1453 list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
1454 if (!nrs_policy_compatible(svc, desc))
1457 if (desc->pd_ops->op_lprocfs_fini != NULL)
1458 desc->pd_ops->op_lprocfs_fini(svc);
1461 mutex_unlock(&nrs_core.nrs_mutex);
1465 * Obtains NRS head resources for request \a req.
1467 * These could be either on the regular or HP NRS head of \a svcpt; resources
1468 * taken on the regular head can later be swapped for HP head resources by
1469 * ldlm_lock_reorder_req().
1471 * \param[in] svcpt the service partition
1472 * \param[in] req the request
1473 * \param[in] hp which NRS head of \a svcpt to use
1475 void ptlrpc_nrs_req_initialize(struct ptlrpc_service_part *svcpt,
1476 struct ptlrpc_request *req, bool hp)
1478 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1480 memset(&req->rq_nrq, 0, sizeof(req->rq_nrq));
1481 nrs_resource_get_safe(nrs, &req->rq_nrq, req->rq_nrq.nr_res_ptrs,
1485 * It is fine to access \e nr_initialized without locking as there is
1486 * no contention at this early stage.
1488 req->rq_nrq.nr_initialized = 1;
1492 * Releases resources for a request; is called after the request has been
1495 * \param[in] req the request
1497 * \see ptlrpc_server_finish_request()
1499 void ptlrpc_nrs_req_finalize(struct ptlrpc_request *req)
1501 if (req->rq_nrq.nr_initialized) {
1502 nrs_resource_put_safe(req->rq_nrq.nr_res_ptrs);
1503 /* no protection on bit nr_initialized because no
1504 * contention at this late stage */
1505 req->rq_nrq.nr_finalized = 1;
1509 void ptlrpc_nrs_req_stop_nolock(struct ptlrpc_request *req)
1511 if (req->rq_nrq.nr_started)
1512 nrs_request_stop(&req->rq_nrq);
1516 * Enqueues request \a req on either the regular or high-priority NRS head
1517 * of service partition \a svcpt.
1519 * \param[in] svcpt the service partition
1520 * \param[in] req the request to be enqueued
1521 * \param[in] hp whether to enqueue the request on the regular or
1522 * high-priority NRS head.
1524 void ptlrpc_nrs_req_add(struct ptlrpc_service_part *svcpt,
1525 struct ptlrpc_request *req, bool hp)
1527 spin_lock(&svcpt->scp_req_lock);
1530 ptlrpc_nrs_hpreq_add_nolock(req);
1532 ptlrpc_nrs_req_add_nolock(req);
1534 spin_unlock(&svcpt->scp_req_lock);
1537 static void nrs_request_removed(struct ptlrpc_nrs_policy *policy)
1539 LASSERT(policy->pol_nrs->nrs_req_queued > 0);
1540 LASSERT(policy->pol_req_queued > 0);
1542 policy->pol_nrs->nrs_req_queued--;
1543 policy->pol_req_queued--;
1546 * If the policy has no more requests queued, remove it from
1547 * ptlrpc_nrs::nrs_policy_queued.
1549 if (unlikely(policy->pol_req_queued == 0)) {
1550 list_del_init(&policy->pol_list_queued);
1553 * If there are other policies with queued requests, move the
1554 * current policy to the end so that we can round robin over
1555 * all policies and drain the requests.
1557 } else if (policy->pol_req_queued != policy->pol_nrs->nrs_req_queued) {
1558 LASSERT(policy->pol_req_queued <
1559 policy->pol_nrs->nrs_req_queued);
1561 list_move_tail(&policy->pol_list_queued,
1562 &policy->pol_nrs->nrs_policy_queued);
1567 * Obtains a request for handling from an NRS head of service partition
1570 * \param[in] svcpt the service partition
1571 * \param[in] hp whether to obtain a request from the regular or
1572 * high-priority NRS head.
1573 * \param[in] peek when set, signifies that we just want to examine the
1574 * request, and not handle it, so the request is not removed
1576 * \param[in] force when set, it will force a policy to return a request if it
1579 * \retval the request to be handled
1580 * \retval NULL the head has no requests to serve
1582 struct ptlrpc_request *
1583 ptlrpc_nrs_req_get_nolock0(struct ptlrpc_service_part *svcpt, bool hp,
1584 bool peek, bool force)
1586 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1587 struct ptlrpc_nrs_policy *policy;
1588 struct ptlrpc_nrs_request *nrq;
1591 * Always try to drain requests from all NRS polices even if they are
1592 * inactive, because the user can change policy status at runtime.
1594 list_for_each_entry(policy, &nrs->nrs_policy_queued,
1596 nrq = nrs_request_get(policy, peek, force);
1598 if (likely(!peek)) {
1599 nrq->nr_started = 1;
1601 policy->pol_req_started++;
1602 policy->pol_nrs->nrs_req_started++;
1604 nrs_request_removed(policy);
1607 return container_of(nrq, struct ptlrpc_request, rq_nrq);
1615 * Dequeues request \a req from the policy it has been enqueued on.
1617 * \param[in] req the request
1619 void ptlrpc_nrs_req_del_nolock(struct ptlrpc_request *req)
1621 struct ptlrpc_nrs_policy *policy = nrs_request_policy(&req->rq_nrq);
1623 policy->pol_desc->pd_ops->op_req_dequeue(policy, &req->rq_nrq);
1625 req->rq_nrq.nr_enqueued = 0;
1627 nrs_request_removed(policy);
1631 * Returns whether there are any requests currently enqueued on any of the
1632 * policies of service partition's \a svcpt NRS head specified by \a hp. Should
1633 * be called while holding ptlrpc_service_part::scp_req_lock to get a reliable
1636 * \param[in] svcpt the service partition to enquire.
1637 * \param[in] hp whether the regular or high-priority NRS head is to be
1640 * \retval false the indicated NRS head has no enqueued requests.
1641 * \retval true the indicated NRS head has some enqueued requests.
1643 bool ptlrpc_nrs_req_pending_nolock(struct ptlrpc_service_part *svcpt, bool hp)
1645 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1647 return nrs->nrs_req_queued > 0;
1651 * Returns whether NRS policy is throttling reqeust
1653 * \param[in] svcpt the service partition to enquire.
1654 * \param[in] hp whether the regular or high-priority NRS head is to be
1657 * \retval false the indicated NRS head has no enqueued requests.
1658 * \retval true the indicated NRS head has some enqueued requests.
1660 bool ptlrpc_nrs_req_throttling_nolock(struct ptlrpc_service_part *svcpt,
1663 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1665 return !!nrs->nrs_throttling;
1669 * Moves request \a req from the regular to the high-priority NRS head.
1671 * \param[in] req the request to move
1673 void ptlrpc_nrs_req_hp_move(struct ptlrpc_request *req)
1675 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1676 struct ptlrpc_nrs_request *nrq = &req->rq_nrq;
1677 struct ptlrpc_nrs_resource *res1[NRS_RES_MAX];
1678 struct ptlrpc_nrs_resource *res2[NRS_RES_MAX];
1682 * Obtain the high-priority NRS head resources.
1684 nrs_resource_get_safe(nrs_svcpt2nrs(svcpt, true), nrq, res1, true);
1686 spin_lock(&svcpt->scp_req_lock);
1688 if (!ptlrpc_nrs_req_can_move(req))
1691 ptlrpc_nrs_req_del_nolock(req);
1693 memcpy(res2, nrq->nr_res_ptrs, NRS_RES_MAX * sizeof(res2[0]));
1694 memcpy(nrq->nr_res_ptrs, res1, NRS_RES_MAX * sizeof(res1[0]));
1696 ptlrpc_nrs_hpreq_add_nolock(req);
1698 memcpy(res1, res2, NRS_RES_MAX * sizeof(res1[0]));
1700 spin_unlock(&svcpt->scp_req_lock);
1703 * Release either the regular NRS head resources if we moved the
1704 * request, or the high-priority NRS head resources if we took a
1705 * reference earlier in this function and ptlrpc_nrs_req_can_move()
1708 nrs_resource_put_safe(res1);
1713 * Carries out a control operation \a opc on the policy identified by the
1714 * human-readable \a name, on either all partitions, or only on the first
1715 * partition of service \a svc.
1717 * \param[in] svc the service the policy belongs to.
1718 * \param[in] queue whether to carry out the command on the policy which
1719 * belongs to the regular, high-priority, or both NRS
1720 * heads of service partitions of \a svc.
1721 * \param[in] name the policy to act upon, by human-readable name
1722 * \param[in] opc the opcode of the operation to carry out
1723 * \param[in] single when set, the operation will only be carried out on the
1724 * NRS heads of the first service partition of \a svc.
1725 * This is useful for some policies which e.g. share
1726 * identical values on the same parameters of different
1727 * service partitions; when reading these parameters via
1728 * lprocfs, these policies may just want to obtain and
1729 * print out the values from the first service partition.
1730 * Storing these values centrally elsewhere then could be
1731 * another solution for this.
1732 * \param[in,out] arg can be used as a generic in/out buffer between control
1733 * operations and the user environment.
1735 *\retval -ve error condition
1736 *\retval 0 operation was carried out successfully
1738 int ptlrpc_nrs_policy_control(const struct ptlrpc_service *svc,
1739 enum ptlrpc_nrs_queue_type queue, char *name,
1740 enum ptlrpc_nrs_ctl opc, bool single, void *arg)
1742 struct ptlrpc_service_part *svcpt;
1747 LASSERT(opc != PTLRPC_NRS_CTL_INVALID);
1749 if ((queue & PTLRPC_NRS_QUEUE_BOTH) == 0)
1752 ptlrpc_service_for_each_part(svcpt, i, svc) {
1753 if ((queue & PTLRPC_NRS_QUEUE_REG) != 0) {
1754 rc = nrs_policy_ctl(nrs_svcpt2nrs(svcpt, false), name,
1756 if (rc != 0 || (queue == PTLRPC_NRS_QUEUE_REG &&
1761 if ((queue & PTLRPC_NRS_QUEUE_HP) != 0) {
1763 * XXX: We could optionally check for
1764 * nrs_svc_has_hp(svc) here, and return an error if it
1765 * is false. Right now we rely on the policies' lprocfs
1766 * handlers that call the present function to make this
1767 * check; if they fail to do so, they might hit the
1768 * assertion inside nrs_svcpt2nrs() below.
1770 rc = nrs_policy_ctl(nrs_svcpt2nrs(svcpt, true), name,
1772 if (rc != 0 || single)
1781 * Adds all policies that ship with the ptlrpc module, to NRS core's list of
1782 * policies \e nrs_core.nrs_policies.
1784 * \retval 0 all policies have been registered successfully
1787 int ptlrpc_nrs_init(void)
1792 mutex_init(&nrs_core.nrs_mutex);
1793 INIT_LIST_HEAD(&nrs_core.nrs_policies);
1795 rc = ptlrpc_nrs_policy_register(&nrs_conf_fifo);
1799 #ifdef HAVE_SERVER_SUPPORT
1800 rc = ptlrpc_nrs_policy_register(&nrs_conf_crrn);
1804 rc = ptlrpc_nrs_policy_register(&nrs_conf_orr);
1808 rc = ptlrpc_nrs_policy_register(&nrs_conf_trr);
1811 rc = ptlrpc_nrs_policy_register(&nrs_conf_tbf);
1815 rc = ptlrpc_nrs_policy_register(&nrs_conf_delay);
1818 #endif /* HAVE_SERVER_SUPPORT */
1823 * Since no PTLRPC services have been started at this point, all we need
1824 * to do for cleanup is to free the descriptors.
1832 * Removes all policy descriptors from nrs_core::nrs_policies, and frees the
1833 * policy descriptors.
1835 * Since all PTLRPC services are stopped at this point, there are no more
1836 * instances of any policies, because each service will have stopped its policy
1837 * instances in ptlrpc_service_nrs_cleanup(), so we just need to free the
1840 void ptlrpc_nrs_fini(void)
1842 struct ptlrpc_nrs_pol_desc *desc;
1843 struct ptlrpc_nrs_pol_desc *tmp;
1845 list_for_each_entry_safe(desc, tmp, &nrs_core.nrs_policies,
1847 list_del_init(&desc->pd_list);