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) 2011, 2014, 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)
89 struct ptlrpc_nrs *nrs = policy->pol_nrs;
92 if (policy->pol_desc->pd_ops->op_policy_stop != NULL) {
93 spin_unlock(&nrs->nrs_lock);
95 policy->pol_desc->pd_ops->op_policy_stop(policy);
97 spin_lock(&nrs->nrs_lock);
100 LASSERT(list_empty(&policy->pol_list_queued));
101 LASSERT(policy->pol_req_queued == 0 &&
102 policy->pol_req_started == 0);
104 policy->pol_private = NULL;
106 policy->pol_state = NRS_POL_STATE_STOPPED;
108 if (atomic_dec_and_test(&policy->pol_desc->pd_refs))
109 module_put(policy->pol_desc->pd_owner);
114 static int nrs_policy_stop_locked(struct ptlrpc_nrs_policy *policy)
116 struct ptlrpc_nrs *nrs = policy->pol_nrs;
119 if (nrs->nrs_policy_fallback == policy && !nrs->nrs_stopping)
122 if (policy->pol_state == NRS_POL_STATE_STARTING)
125 /* In progress or already stopped */
126 if (policy->pol_state != NRS_POL_STATE_STARTED)
129 policy->pol_state = NRS_POL_STATE_STOPPING;
131 /* Immediately make it invisible */
132 if (nrs->nrs_policy_primary == policy) {
133 nrs->nrs_policy_primary = NULL;
136 LASSERT(nrs->nrs_policy_fallback == policy);
137 nrs->nrs_policy_fallback = NULL;
140 /* I have the only refcount */
141 if (policy->pol_ref == 1)
142 nrs_policy_stop0(policy);
148 * Transitions the \a nrs NRS head's primary policy to
149 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING and if the policy has no
150 * pending usage references, to ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED.
152 * \param[in] nrs the NRS head to carry out this operation on
154 static void nrs_policy_stop_primary(struct ptlrpc_nrs *nrs)
156 struct ptlrpc_nrs_policy *tmp = nrs->nrs_policy_primary;
161 * XXX: This should really be RETURN_EXIT, but the latter does
162 * not currently print anything out, and possibly should be
169 nrs->nrs_policy_primary = NULL;
171 LASSERT(tmp->pol_state == NRS_POL_STATE_STARTED);
172 tmp->pol_state = NRS_POL_STATE_STOPPING;
174 if (tmp->pol_ref == 0)
175 nrs_policy_stop0(tmp);
180 * Transitions a policy across the ptlrpc_nrs_pol_state range of values, in
181 * response to an lprocfs command to start a policy.
183 * If a primary policy different to the current one is specified, this function
184 * will transition the new policy to the
185 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTING and then to
186 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED, and will then transition
187 * the old primary policy (if there is one) 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.
191 * If the fallback policy is specified, this is taken to indicate an instruction
192 * to stop the current primary policy, without substituting it with another
193 * primary policy, so the primary policy (if any) is transitioned to
194 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING, and if there are no outstanding
195 * references on the policy to ptlrpc_nrs_pol_stae::NRS_POL_STATE_STOPPED. In
196 * this case, the fallback policy is only left active in the NRS head.
198 static int nrs_policy_start_locked(struct ptlrpc_nrs_policy *policy, char *arg)
200 struct ptlrpc_nrs *nrs = policy->pol_nrs;
205 * Don't allow multiple starting which is too complex, and has no real
208 if (nrs->nrs_policy_starting)
211 LASSERT(policy->pol_state != NRS_POL_STATE_STARTING);
213 if (policy->pol_state == NRS_POL_STATE_STOPPING)
216 if (policy->pol_flags & PTLRPC_NRS_FL_FALLBACK) {
218 * This is for cases in which the user sets the policy to the
219 * fallback policy (currently fifo for all services); i.e. the
220 * user is resetting the policy to the default; so we stop the
221 * primary policy, if any.
223 if (policy == nrs->nrs_policy_fallback) {
224 nrs_policy_stop_primary(nrs);
229 * If we reach here, we must be setting up the fallback policy
230 * at service startup time, and only a single policy with the
231 * nrs_policy_flags::PTLRPC_NRS_FL_FALLBACK flag set can
232 * register with NRS core.
234 LASSERT(nrs->nrs_policy_fallback == NULL);
237 * Shouldn't start primary policy if w/o fallback policy.
239 if (nrs->nrs_policy_fallback == NULL)
242 if (policy->pol_state == NRS_POL_STATE_STARTED) {
244 * If the policy argument now is different from the last time,
245 * stop the policy first and start it again with the new
248 if ((arg != NULL) && (strlen(arg) >= NRS_POL_ARG_MAX))
251 if ((arg == NULL && strlen(policy->pol_arg) == 0) ||
252 (arg != NULL && strcmp(policy->pol_arg, arg) == 0))
255 rc = nrs_policy_stop_locked(policy);
262 * Increase the module usage count for policies registering from other
265 if (atomic_inc_return(&policy->pol_desc->pd_refs) == 1 &&
266 !try_module_get(policy->pol_desc->pd_owner)) {
267 atomic_dec(&policy->pol_desc->pd_refs);
268 CERROR("NRS: cannot get module for policy %s; is it alive?\n",
269 policy->pol_desc->pd_name);
274 if (strlen(arg) + 1 > sizeof(policy->pol_arg)) {
275 CERROR("NRS: arg '%s' is too long\n", arg);
276 GOTO(out, rc = -E2BIG);
278 strncpy(policy->pol_arg, arg, sizeof(policy->pol_arg));
282 * Serialize policy starting across the NRS head
284 nrs->nrs_policy_starting = 1;
286 policy->pol_state = NRS_POL_STATE_STARTING;
288 if (policy->pol_desc->pd_ops->op_policy_start) {
289 spin_unlock(&nrs->nrs_lock);
291 rc = policy->pol_desc->pd_ops->op_policy_start(policy, arg);
293 spin_lock(&nrs->nrs_lock);
295 if (atomic_dec_and_test(&policy->pol_desc->pd_refs))
296 module_put(policy->pol_desc->pd_owner);
298 policy->pol_state = NRS_POL_STATE_STOPPED;
303 policy->pol_state = NRS_POL_STATE_STARTED;
305 if (policy->pol_flags & PTLRPC_NRS_FL_FALLBACK) {
307 * This path is only used at PTLRPC service setup time.
309 nrs->nrs_policy_fallback = policy;
312 * Try to stop the current primary policy if there is one.
314 nrs_policy_stop_primary(nrs);
317 * And set the newly-started policy as the primary one.
319 nrs->nrs_policy_primary = policy;
323 nrs->nrs_policy_starting = 0;
329 * Increases the policy's usage reference count.
331 static inline void nrs_policy_get_locked(struct ptlrpc_nrs_policy *policy)
337 * Decreases the policy's usage reference count, and stops the policy in case it
338 * was already stopping and have no more outstanding usage references (which
339 * indicates it has no more queued or started requests, and can be safely
342 static void nrs_policy_put_locked(struct ptlrpc_nrs_policy *policy)
344 LASSERT(policy->pol_ref > 0);
347 if (unlikely(policy->pol_ref == 0 &&
348 policy->pol_state == NRS_POL_STATE_STOPPING))
349 nrs_policy_stop0(policy);
352 static void nrs_policy_put(struct ptlrpc_nrs_policy *policy)
354 spin_lock(&policy->pol_nrs->nrs_lock);
355 nrs_policy_put_locked(policy);
356 spin_unlock(&policy->pol_nrs->nrs_lock);
360 * Find and return a policy by name.
362 static struct ptlrpc_nrs_policy * nrs_policy_find_locked(struct ptlrpc_nrs *nrs,
365 struct ptlrpc_nrs_policy *tmp;
367 list_for_each_entry(tmp, &nrs->nrs_policy_list, pol_list) {
368 if (strncmp(tmp->pol_desc->pd_name, name,
369 NRS_POL_NAME_MAX) == 0) {
370 nrs_policy_get_locked(tmp);
378 * Release references for the resource hierarchy moving upwards towards the
379 * policy instance resource.
381 static void nrs_resource_put(struct ptlrpc_nrs_resource *res)
383 struct ptlrpc_nrs_policy *policy = res->res_policy;
385 if (policy->pol_desc->pd_ops->op_res_put != NULL) {
386 struct ptlrpc_nrs_resource *parent;
388 for (; res != NULL; res = parent) {
389 parent = res->res_parent;
390 policy->pol_desc->pd_ops->op_res_put(policy, res);
396 * Obtains references for each resource in the resource hierarchy for request
397 * \a nrq if it is to be handled by \a policy.
399 * \param[in] policy the policy
400 * \param[in] nrq the request
401 * \param[in] moving_req denotes whether this is a call to the function by
402 * ldlm_lock_reorder_req(), in order to move \a nrq to
403 * the high-priority NRS head; we should not sleep when
406 * \retval NULL resource hierarchy references not obtained
407 * \retval valid-pointer the bottom level of the resource hierarchy
409 * \see ptlrpc_nrs_pol_ops::op_res_get()
412 struct ptlrpc_nrs_resource * nrs_resource_get(struct ptlrpc_nrs_policy *policy,
413 struct ptlrpc_nrs_request *nrq,
417 * Set to NULL to traverse the resource hierarchy from the top.
419 struct ptlrpc_nrs_resource *res = NULL;
420 struct ptlrpc_nrs_resource *tmp = NULL;
424 rc = policy->pol_desc->pd_ops->op_res_get(policy, nrq, res,
428 nrs_resource_put(res);
432 LASSERT(tmp != NULL);
433 tmp->res_parent = res;
434 tmp->res_policy = policy;
438 * Return once we have obtained a reference to the bottom level
439 * of the resource hierarchy.
447 * Obtains resources for the resource hierarchies and policy references for
448 * the fallback and current primary policy (if any), that will later be used
449 * to handle request \a nrq.
451 * \param[in] nrs the NRS head instance that will be handling request \a nrq.
452 * \param[in] nrq the request that is being handled.
453 * \param[out] resp the array where references to the resource hierarchy are
455 * \param[in] moving_req is set when obtaining resources while moving a
456 * request from a policy on the regular NRS head to a
457 * policy on the HP NRS head (via
458 * ldlm_lock_reorder_req()). It signifies that
459 * allocations to get resources should be atomic; for
460 * a full explanation, see comment in
461 * ptlrpc_nrs_pol_ops::op_res_get().
463 static void nrs_resource_get_safe(struct ptlrpc_nrs *nrs,
464 struct ptlrpc_nrs_request *nrq,
465 struct ptlrpc_nrs_resource **resp,
468 struct ptlrpc_nrs_policy *primary = NULL;
469 struct ptlrpc_nrs_policy *fallback = NULL;
471 memset(resp, 0, sizeof(resp[0]) * NRS_RES_MAX);
474 * Obtain policy references.
476 spin_lock(&nrs->nrs_lock);
478 fallback = nrs->nrs_policy_fallback;
479 nrs_policy_get_locked(fallback);
481 primary = nrs->nrs_policy_primary;
483 nrs_policy_get_locked(primary);
485 spin_unlock(&nrs->nrs_lock);
488 * Obtain resource hierarchy references.
490 resp[NRS_RES_FALLBACK] = nrs_resource_get(fallback, nrq, moving_req);
491 LASSERT(resp[NRS_RES_FALLBACK] != NULL);
493 if (primary != NULL) {
494 resp[NRS_RES_PRIMARY] = nrs_resource_get(primary, nrq,
497 * A primary policy may exist which may not wish to serve a
498 * particular request for different reasons; release the
499 * reference on the policy as it will not be used for this
502 if (resp[NRS_RES_PRIMARY] == NULL)
503 nrs_policy_put(primary);
508 * Releases references to resource hierarchies and policies, because they are no
509 * longer required; used when request handling has been completed, or the
510 * request is moving to the high priority NRS head.
512 * \param resp the resource hierarchy that is being released
514 * \see ptlrpcnrs_req_hp_move()
515 * \see ptlrpc_nrs_req_finalize()
517 static void nrs_resource_put_safe(struct ptlrpc_nrs_resource **resp)
519 struct ptlrpc_nrs_policy *pols[NRS_RES_MAX];
520 struct ptlrpc_nrs *nrs = NULL;
523 for (i = 0; i < NRS_RES_MAX; i++) {
524 if (resp[i] != NULL) {
525 pols[i] = resp[i]->res_policy;
526 nrs_resource_put(resp[i]);
533 for (i = 0; i < NRS_RES_MAX; i++) {
538 nrs = pols[i]->pol_nrs;
539 spin_lock(&nrs->nrs_lock);
541 nrs_policy_put_locked(pols[i]);
545 spin_unlock(&nrs->nrs_lock);
549 * Obtains an NRS request from \a policy for handling or examination; the
550 * request should be removed in the 'handling' case.
552 * Calling into this function implies we already know the policy has a request
553 * waiting to be handled.
555 * \param[in] policy the policy from which a request
556 * \param[in] peek when set, signifies that we just want to examine the
557 * request, and not handle it, so the request is not removed
559 * \param[in] force when set, it will force a policy to return a request if it
562 * \retval the NRS request to be handled
565 struct ptlrpc_nrs_request * nrs_request_get(struct ptlrpc_nrs_policy *policy,
566 bool peek, bool force)
568 struct ptlrpc_nrs_request *nrq;
570 LASSERT(policy->pol_req_queued > 0);
572 nrq = policy->pol_desc->pd_ops->op_req_get(policy, peek, force);
574 LASSERT(ergo(nrq != NULL, nrs_request_policy(nrq) == policy));
580 * Enqueues request \a nrq for later handling, via one one the policies for
581 * which resources where earlier obtained via nrs_resource_get_safe(). The
582 * function attempts to enqueue the request first on the primary policy
583 * (if any), since this is the preferred choice.
585 * \param nrq the request being enqueued
587 * \see nrs_resource_get_safe()
589 static inline void nrs_request_enqueue(struct ptlrpc_nrs_request *nrq)
591 struct ptlrpc_nrs_policy *policy;
596 * Try in descending order, because the primary policy (if any) is
597 * the preferred choice.
599 for (i = NRS_RES_MAX - 1; i >= 0; i--) {
600 if (nrq->nr_res_ptrs[i] == NULL)
604 policy = nrq->nr_res_ptrs[i]->res_policy;
606 rc = policy->pol_desc->pd_ops->op_req_enqueue(policy, nrq);
608 policy->pol_nrs->nrs_req_queued++;
609 policy->pol_req_queued++;
614 * Should never get here, as at least the primary policy's
615 * ptlrpc_nrs_pol_ops::op_req_enqueue() implementation should always
622 * Called when a request has been handled
624 * \param[in] nrs the request that has been handled; can be used for
625 * job/resource control.
627 * \see ptlrpc_nrs_req_stop_nolock()
629 static inline void nrs_request_stop(struct ptlrpc_nrs_request *nrq)
631 struct ptlrpc_nrs_policy *policy = nrs_request_policy(nrq);
633 if (policy->pol_desc->pd_ops->op_req_stop)
634 policy->pol_desc->pd_ops->op_req_stop(policy, nrq);
636 LASSERT(policy->pol_nrs->nrs_req_started > 0);
637 LASSERT(policy->pol_req_started > 0);
639 policy->pol_nrs->nrs_req_started--;
640 policy->pol_req_started--;
644 * Handler for operations that can be carried out on policies.
646 * Handles opcodes that are common to all policy types within NRS core, and
647 * passes any unknown opcodes to the policy-specific control function.
649 * \param[in] nrs the NRS head this policy belongs to.
650 * \param[in] name the human-readable policy name; should be the same as
651 * ptlrpc_nrs_pol_desc::pd_name.
652 * \param[in] opc the opcode of the operation being carried out.
653 * \param[in,out] arg can be used to pass information in and out between when
654 * carrying an operation; usually data that is private to
655 * the policy at some level, or generic policy status
658 * \retval -ve error condition
659 * \retval 0 operation was carried out successfully
661 static int nrs_policy_ctl(struct ptlrpc_nrs *nrs, char *name,
662 enum ptlrpc_nrs_ctl opc, void *arg)
664 struct ptlrpc_nrs_policy *policy;
668 spin_lock(&nrs->nrs_lock);
670 policy = nrs_policy_find_locked(nrs, name);
672 GOTO(out, rc = -ENOENT);
675 * Wait for the policy to be fully started before attempting
678 if (policy->pol_state == NRS_POL_STATE_STARTING)
679 GOTO(out, rc = -EAGAIN);
683 * Unknown opcode, pass it down to the policy-specific control
684 * function for handling.
687 rc = nrs_policy_ctl_locked(policy, opc, arg);
693 case PTLRPC_NRS_CTL_START:
694 rc = nrs_policy_start_locked(policy, arg);
699 nrs_policy_put_locked(policy);
701 spin_unlock(&nrs->nrs_lock);
707 * Unregisters a policy by name.
709 * \param[in] nrs the NRS head this policy belongs to.
710 * \param[in] name the human-readable policy name; should be the same as
711 * ptlrpc_nrs_pol_desc::pd_name
716 static int nrs_policy_unregister(struct ptlrpc_nrs *nrs, char *name)
718 struct ptlrpc_nrs_policy *policy = NULL;
721 spin_lock(&nrs->nrs_lock);
723 policy = nrs_policy_find_locked(nrs, name);
724 if (policy == NULL) {
725 spin_unlock(&nrs->nrs_lock);
727 CERROR("Can't find NRS policy %s\n", name);
731 if (policy->pol_ref > 1) {
732 CERROR("Policy %s is busy with %d references\n", name,
733 (int)policy->pol_ref);
734 nrs_policy_put_locked(policy);
736 spin_unlock(&nrs->nrs_lock);
740 LASSERT(policy->pol_req_queued == 0);
741 LASSERT(policy->pol_req_started == 0);
743 if (policy->pol_state != NRS_POL_STATE_STOPPED) {
744 nrs_policy_stop_locked(policy);
745 LASSERT(policy->pol_state == NRS_POL_STATE_STOPPED);
748 list_del(&policy->pol_list);
751 nrs_policy_put_locked(policy);
753 spin_unlock(&nrs->nrs_lock);
755 nrs_policy_fini(policy);
757 LASSERT(policy->pol_private == NULL);
758 OBD_FREE_PTR(policy);
764 * Register a policy from \policy descriptor \a desc with NRS head \a nrs.
766 * \param[in] nrs the NRS head on which the policy will be registered.
767 * \param[in] desc the policy descriptor from which the information will be
768 * obtained to register the policy.
773 static int nrs_policy_register(struct ptlrpc_nrs *nrs,
774 struct ptlrpc_nrs_pol_desc *desc)
776 struct ptlrpc_nrs_policy *policy;
777 struct ptlrpc_nrs_policy *tmp;
778 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
782 LASSERT(svcpt != NULL);
783 LASSERT(desc->pd_ops != NULL);
784 LASSERT(desc->pd_ops->op_res_get != NULL);
785 LASSERT(desc->pd_ops->op_req_get != NULL);
786 LASSERT(desc->pd_ops->op_req_enqueue != NULL);
787 LASSERT(desc->pd_ops->op_req_dequeue != NULL);
788 LASSERT(desc->pd_compat != NULL);
790 OBD_CPT_ALLOC_GFP(policy, svcpt->scp_service->srv_cptable,
791 svcpt->scp_cpt, sizeof(*policy), GFP_NOFS);
795 policy->pol_nrs = nrs;
796 policy->pol_desc = desc;
797 policy->pol_state = NRS_POL_STATE_STOPPED;
798 policy->pol_flags = desc->pd_flags;
800 INIT_LIST_HEAD(&policy->pol_list);
801 INIT_LIST_HEAD(&policy->pol_list_queued);
803 rc = nrs_policy_init(policy);
805 OBD_FREE_PTR(policy);
809 spin_lock(&nrs->nrs_lock);
811 tmp = nrs_policy_find_locked(nrs, policy->pol_desc->pd_name);
813 CERROR("NRS policy %s has been registered, can't register it "
814 "for %s\n", policy->pol_desc->pd_name,
815 svcpt->scp_service->srv_name);
816 nrs_policy_put_locked(tmp);
818 spin_unlock(&nrs->nrs_lock);
819 nrs_policy_fini(policy);
820 OBD_FREE_PTR(policy);
825 list_add_tail(&policy->pol_list, &nrs->nrs_policy_list);
828 if (policy->pol_flags & PTLRPC_NRS_FL_REG_START)
829 rc = nrs_policy_start_locked(policy, NULL);
831 spin_unlock(&nrs->nrs_lock);
834 (void) nrs_policy_unregister(nrs, policy->pol_desc->pd_name);
840 * Enqueue request \a req using one of the policies its resources are referring
843 * \param[in] req the request to enqueue.
845 static void ptlrpc_nrs_req_add_nolock(struct ptlrpc_request *req)
847 struct ptlrpc_nrs_policy *policy;
849 LASSERT(req->rq_nrq.nr_initialized);
850 LASSERT(!req->rq_nrq.nr_enqueued);
852 nrs_request_enqueue(&req->rq_nrq);
853 req->rq_nrq.nr_enqueued = 1;
855 policy = nrs_request_policy(&req->rq_nrq);
857 * Add the policy to the NRS head's list of policies with enqueued
858 * requests, if it has not been added there.
860 if (unlikely(list_empty(&policy->pol_list_queued)))
861 list_add_tail(&policy->pol_list_queued,
862 &policy->pol_nrs->nrs_policy_queued);
866 * Enqueue a request on the high priority NRS head.
868 * \param req the request to enqueue.
870 static void ptlrpc_nrs_hpreq_add_nolock(struct ptlrpc_request *req)
872 int opc = lustre_msg_get_opc(req->rq_reqmsg);
875 spin_lock(&req->rq_lock);
877 ptlrpc_nrs_req_add_nolock(req);
879 DEBUG_REQ(D_NET, req, "high priority req");
880 spin_unlock(&req->rq_lock);
885 * Returns a boolean predicate indicating whether the policy described by
886 * \a desc is adequate for use with service \a svc.
888 * \param[in] svc the service
889 * \param[in] desc the policy descriptor
891 * \retval false the policy is not compatible with the service
892 * \retval true the policy is compatible with the service
894 static inline bool nrs_policy_compatible(const struct ptlrpc_service *svc,
895 const struct ptlrpc_nrs_pol_desc *desc)
897 return desc->pd_compat(svc, desc);
901 * Registers all compatible policies in nrs_core.nrs_policies, for NRS head
904 * \param[in] nrs the NRS head
909 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
911 * \see ptlrpc_service_nrs_setup()
913 static int nrs_register_policies_locked(struct ptlrpc_nrs *nrs)
915 struct ptlrpc_nrs_pol_desc *desc;
916 /* for convenience */
917 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
918 struct ptlrpc_service *svc = svcpt->scp_service;
922 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
924 list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
925 if (nrs_policy_compatible(svc, desc)) {
926 rc = nrs_policy_register(nrs, desc);
928 CERROR("Failed to register NRS policy %s for "
929 "partition %d of service %s: %d\n",
930 desc->pd_name, svcpt->scp_cpt,
933 * Fail registration if any of the policies'
934 * registration fails.
945 * Initializes NRS head \a nrs of service partition \a svcpt, and registers all
946 * compatible policies in NRS core, with the NRS head.
948 * \param[in] nrs the NRS head
949 * \param[in] svcpt the PTLRPC service partition to setup
954 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
956 static int nrs_svcpt_setup_locked0(struct ptlrpc_nrs *nrs,
957 struct ptlrpc_service_part *svcpt)
960 enum ptlrpc_nrs_queue_type queue;
962 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
964 if (nrs == &svcpt->scp_nrs_reg)
965 queue = PTLRPC_NRS_QUEUE_REG;
966 else if (nrs == svcpt->scp_nrs_hp)
967 queue = PTLRPC_NRS_QUEUE_HP;
971 nrs->nrs_svcpt = svcpt;
972 nrs->nrs_queue_type = queue;
973 spin_lock_init(&nrs->nrs_lock);
974 INIT_LIST_HEAD(&nrs->nrs_policy_list);
975 INIT_LIST_HEAD(&nrs->nrs_policy_queued);
976 nrs->nrs_throttling = 0;
978 rc = nrs_register_policies_locked(nrs);
984 * Allocates a regular and optionally a high-priority NRS head (if the service
985 * handles high-priority RPCs), and then registers all available compatible
986 * policies on those NRS heads.
988 * \param[in,out] svcpt the PTLRPC service partition to setup
990 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
992 static int nrs_svcpt_setup_locked(struct ptlrpc_service_part *svcpt)
994 struct ptlrpc_nrs *nrs;
998 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
1001 * Initialize the regular NRS head.
1003 nrs = nrs_svcpt2nrs(svcpt, false);
1004 rc = nrs_svcpt_setup_locked0(nrs, svcpt);
1009 * Optionally allocate a high-priority NRS head.
1011 if (svcpt->scp_service->srv_ops.so_hpreq_handler == NULL)
1014 OBD_CPT_ALLOC_PTR(svcpt->scp_nrs_hp,
1015 svcpt->scp_service->srv_cptable,
1017 if (svcpt->scp_nrs_hp == NULL)
1018 GOTO(out, rc = -ENOMEM);
1020 nrs = nrs_svcpt2nrs(svcpt, true);
1021 rc = nrs_svcpt_setup_locked0(nrs, svcpt);
1028 * Unregisters all policies on all available NRS heads in a service partition;
1029 * called at PTLRPC service unregistration time.
1031 * \param[in] svcpt the PTLRPC service partition
1033 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
1035 static void nrs_svcpt_cleanup_locked(struct ptlrpc_service_part *svcpt)
1037 struct ptlrpc_nrs *nrs;
1038 struct ptlrpc_nrs_policy *policy;
1039 struct ptlrpc_nrs_policy *tmp;
1044 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
1047 /* scp_nrs_hp could be NULL due to short of memory. */
1048 nrs = hp ? svcpt->scp_nrs_hp : &svcpt->scp_nrs_reg;
1049 /* check the nrs_svcpt to see if nrs is initialized. */
1050 if (!nrs || !nrs->nrs_svcpt) {
1054 nrs->nrs_stopping = 1;
1056 list_for_each_entry_safe(policy, tmp, &nrs->nrs_policy_list,
1058 rc = nrs_policy_unregister(nrs, policy->pol_desc->pd_name);
1063 * If the service partition has an HP NRS head, clean that up as well.
1065 if (!hp && nrs_svcpt_has_hp(svcpt)) {
1077 * Returns the descriptor for a policy as identified by by \a name.
1079 * \param[in] name the policy name
1081 * \retval the policy descriptor
1084 static struct ptlrpc_nrs_pol_desc *nrs_policy_find_desc_locked(const char *name)
1086 struct ptlrpc_nrs_pol_desc *tmp;
1089 list_for_each_entry(tmp, &nrs_core.nrs_policies, pd_list) {
1090 if (strncmp(tmp->pd_name, name, NRS_POL_NAME_MAX) == 0)
1097 * Removes the policy from all supported NRS heads of all partitions of all
1100 * \param[in] desc the policy descriptor to unregister
1103 * \retval 0 successfully unregistered policy on all supported NRS heads
1105 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
1106 * \pre mutex_is_locked(&ptlrpc_all_services_mutex)
1108 static int nrs_policy_unregister_locked(struct ptlrpc_nrs_pol_desc *desc)
1110 struct ptlrpc_nrs *nrs;
1111 struct ptlrpc_service *svc;
1112 struct ptlrpc_service_part *svcpt;
1117 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
1118 LASSERT(mutex_is_locked(&ptlrpc_all_services_mutex));
1120 list_for_each_entry(svc, &ptlrpc_all_services, srv_list) {
1122 if (!nrs_policy_compatible(svc, desc) ||
1123 unlikely(svc->srv_is_stopping))
1126 ptlrpc_service_for_each_part(svcpt, i, svc) {
1130 nrs = nrs_svcpt2nrs(svcpt, hp);
1131 rc = nrs_policy_unregister(nrs, desc->pd_name);
1133 * Ignore -ENOENT as the policy may not have registered
1134 * successfully on all service partitions.
1136 if (rc == -ENOENT) {
1138 } else if (rc != 0) {
1139 CERROR("Failed to unregister NRS policy %s for "
1140 "partition %d of service %s: %d\n",
1141 desc->pd_name, svcpt->scp_cpt,
1142 svcpt->scp_service->srv_name, rc);
1146 if (!hp && nrs_svc_has_hp(svc)) {
1152 if (desc->pd_ops->op_lprocfs_fini != NULL)
1153 desc->pd_ops->op_lprocfs_fini(svc);
1160 * Registers a new policy with NRS core.
1162 * The function will only succeed if policy registration with all compatible
1163 * service partitions (if any) is successful.
1165 * N.B. This function should be called either at ptlrpc module initialization
1166 * time when registering a policy that ships with NRS core, or in a
1167 * module's init() function for policies registering from other modules.
1169 * \param[in] conf configuration information for the new policy to register
1174 int ptlrpc_nrs_policy_register(struct ptlrpc_nrs_pol_conf *conf)
1176 struct ptlrpc_service *svc;
1177 struct ptlrpc_nrs_pol_desc *desc;
1181 LASSERT(conf != NULL);
1182 LASSERT(conf->nc_ops != NULL);
1183 LASSERT(conf->nc_compat != NULL);
1184 LASSERT(ergo(conf->nc_compat == nrs_policy_compat_one,
1185 conf->nc_compat_svc_name != NULL));
1186 LASSERT(ergo((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) != 0,
1187 conf->nc_owner != NULL));
1189 conf->nc_name[NRS_POL_NAME_MAX - 1] = '\0';
1192 * External policies are not allowed to start immediately upon
1193 * registration, as there is a relatively higher chance that their
1194 * registration might fail. In such a case, some policy instances may
1195 * already have requests queued wen unregistration needs to happen as
1196 * part o cleanup; since there is currently no way to drain requests
1197 * from a policy unless the service is unregistering, we just disallow
1200 if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) &&
1201 (conf->nc_flags & (PTLRPC_NRS_FL_FALLBACK |
1202 PTLRPC_NRS_FL_REG_START))) {
1203 CERROR("NRS: failing to register policy %s. Please check "
1204 "policy flags; external policies cannot act as fallback "
1205 "policies, or be started immediately upon registration "
1206 "without interaction with lprocfs\n", conf->nc_name);
1210 mutex_lock(&nrs_core.nrs_mutex);
1212 if (nrs_policy_find_desc_locked(conf->nc_name) != NULL) {
1213 CERROR("NRS: failing to register policy %s which has already "
1214 "been registered with NRS core!\n",
1216 GOTO(fail, rc = -EEXIST);
1219 OBD_ALLOC_PTR(desc);
1221 GOTO(fail, rc = -ENOMEM);
1223 if (strlcpy(desc->pd_name, conf->nc_name, sizeof(desc->pd_name)) >=
1224 sizeof(desc->pd_name)) {
1226 GOTO(fail, rc = -E2BIG);
1228 desc->pd_ops = conf->nc_ops;
1229 desc->pd_compat = conf->nc_compat;
1230 desc->pd_compat_svc_name = conf->nc_compat_svc_name;
1231 if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) != 0)
1232 desc->pd_owner = conf->nc_owner;
1233 desc->pd_flags = conf->nc_flags;
1234 atomic_set(&desc->pd_refs, 0);
1237 * For policies that are held in the same module as NRS (currently
1238 * ptlrpc), do not register the policy with all compatible services,
1239 * as the services will not have started at this point, since we are
1240 * calling from ptlrpc module initialization code. In such cases each
1241 * service will register all compatible policies later, via
1242 * ptlrpc_service_nrs_setup().
1244 if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) == 0)
1248 * Register the new policy on all compatible services
1250 mutex_lock(&ptlrpc_all_services_mutex);
1252 list_for_each_entry(svc, &ptlrpc_all_services, srv_list) {
1253 struct ptlrpc_service_part *svcpt;
1257 if (!nrs_policy_compatible(svc, desc) ||
1258 unlikely(svc->srv_is_stopping))
1261 ptlrpc_service_for_each_part(svcpt, i, svc) {
1262 struct ptlrpc_nrs *nrs;
1265 nrs = nrs_svcpt2nrs(svcpt, hp);
1266 rc = nrs_policy_register(nrs, desc);
1268 CERROR("Failed to register NRS policy %s for "
1269 "partition %d of service %s: %d\n",
1270 desc->pd_name, svcpt->scp_cpt,
1271 svcpt->scp_service->srv_name, rc);
1273 rc2 = nrs_policy_unregister_locked(desc);
1275 * Should not fail at this point
1278 mutex_unlock(&ptlrpc_all_services_mutex);
1283 if (!hp && nrs_svc_has_hp(svc)) {
1290 * No need to take a reference to other modules here, as we
1291 * will be calling from the module's init() function.
1293 if (desc->pd_ops->op_lprocfs_init != NULL) {
1294 rc = desc->pd_ops->op_lprocfs_init(svc);
1296 rc2 = nrs_policy_unregister_locked(desc);
1298 * Should not fail at this point
1301 mutex_unlock(&ptlrpc_all_services_mutex);
1308 mutex_unlock(&ptlrpc_all_services_mutex);
1310 list_add_tail(&desc->pd_list, &nrs_core.nrs_policies);
1312 mutex_unlock(&nrs_core.nrs_mutex);
1316 EXPORT_SYMBOL(ptlrpc_nrs_policy_register);
1319 * Unregisters a previously registered policy with NRS core. All instances of
1320 * the policy on all NRS heads of all supported services are removed.
1322 * N.B. This function should only be called from a module's exit() function.
1323 * Although it can be used for policies that ship alongside NRS core, the
1324 * function is primarily intended for policies that register externally,
1325 * from other modules.
1327 * \param[in] conf configuration information for the policy to unregister
1332 int ptlrpc_nrs_policy_unregister(struct ptlrpc_nrs_pol_conf *conf)
1334 struct ptlrpc_nrs_pol_desc *desc;
1338 LASSERT(conf != NULL);
1340 if (conf->nc_flags & PTLRPC_NRS_FL_FALLBACK) {
1341 CERROR("Unable to unregister a fallback policy, unless the "
1342 "PTLRPC service is stopping.\n");
1346 conf->nc_name[NRS_POL_NAME_MAX - 1] = '\0';
1348 mutex_lock(&nrs_core.nrs_mutex);
1350 desc = nrs_policy_find_desc_locked(conf->nc_name);
1352 CERROR("Failing to unregister NRS policy %s which has "
1353 "not been registered with NRS core!\n",
1355 GOTO(not_exist, rc = -ENOENT);
1358 mutex_lock(&ptlrpc_all_services_mutex);
1360 rc = nrs_policy_unregister_locked(desc);
1363 CERROR("Please first stop policy %s on all service "
1364 "partitions and then retry to unregister the "
1365 "policy.\n", conf->nc_name);
1369 CDEBUG(D_INFO, "Unregistering policy %s from NRS core.\n",
1372 list_del(&desc->pd_list);
1376 mutex_unlock(&ptlrpc_all_services_mutex);
1379 mutex_unlock(&nrs_core.nrs_mutex);
1383 EXPORT_SYMBOL(ptlrpc_nrs_policy_unregister);
1386 * Setup NRS heads on all service partitions of service \a svc, and register
1387 * all compatible policies on those NRS heads.
1389 * To be called from withing ptl
1390 * \param[in] svc the service to setup
1392 * \retval -ve error, the calling logic should eventually call
1393 * ptlrpc_service_nrs_cleanup() to undo any work performed
1396 * \see ptlrpc_register_service()
1397 * \see ptlrpc_service_nrs_cleanup()
1399 int ptlrpc_service_nrs_setup(struct ptlrpc_service *svc)
1401 struct ptlrpc_service_part *svcpt;
1402 const struct ptlrpc_nrs_pol_desc *desc;
1406 mutex_lock(&nrs_core.nrs_mutex);
1409 * Initialize NRS heads on all service CPTs.
1411 ptlrpc_service_for_each_part(svcpt, i, svc) {
1412 rc = nrs_svcpt_setup_locked(svcpt);
1418 * Set up lprocfs interfaces for all supported policies for the
1421 list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
1422 if (!nrs_policy_compatible(svc, desc))
1425 if (desc->pd_ops->op_lprocfs_init != NULL) {
1426 rc = desc->pd_ops->op_lprocfs_init(svc);
1434 mutex_unlock(&nrs_core.nrs_mutex);
1440 * Unregisters all policies on all service partitions of service \a svc.
1442 * \param[in] svc the PTLRPC service to unregister
1444 void ptlrpc_service_nrs_cleanup(struct ptlrpc_service *svc)
1446 struct ptlrpc_service_part *svcpt;
1447 const struct ptlrpc_nrs_pol_desc *desc;
1450 mutex_lock(&nrs_core.nrs_mutex);
1453 * Clean up NRS heads on all service partitions
1455 ptlrpc_service_for_each_part(svcpt, i, svc)
1456 nrs_svcpt_cleanup_locked(svcpt);
1459 * Clean up lprocfs interfaces for all supported policies for the
1462 list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
1463 if (!nrs_policy_compatible(svc, desc))
1466 if (desc->pd_ops->op_lprocfs_fini != NULL)
1467 desc->pd_ops->op_lprocfs_fini(svc);
1470 mutex_unlock(&nrs_core.nrs_mutex);
1474 * Obtains NRS head resources for request \a req.
1476 * These could be either on the regular or HP NRS head of \a svcpt; resources
1477 * taken on the regular head can later be swapped for HP head resources by
1478 * ldlm_lock_reorder_req().
1480 * \param[in] svcpt the service partition
1481 * \param[in] req the request
1482 * \param[in] hp which NRS head of \a svcpt to use
1484 void ptlrpc_nrs_req_initialize(struct ptlrpc_service_part *svcpt,
1485 struct ptlrpc_request *req, bool hp)
1487 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1489 memset(&req->rq_nrq, 0, sizeof(req->rq_nrq));
1490 nrs_resource_get_safe(nrs, &req->rq_nrq, req->rq_nrq.nr_res_ptrs,
1494 * It is fine to access \e nr_initialized without locking as there is
1495 * no contention at this early stage.
1497 req->rq_nrq.nr_initialized = 1;
1501 * Releases resources for a request; is called after the request has been
1504 * \param[in] req the request
1506 * \see ptlrpc_server_finish_request()
1508 void ptlrpc_nrs_req_finalize(struct ptlrpc_request *req)
1510 if (req->rq_nrq.nr_initialized) {
1511 nrs_resource_put_safe(req->rq_nrq.nr_res_ptrs);
1512 /* no protection on bit nr_initialized because no
1513 * contention at this late stage */
1514 req->rq_nrq.nr_finalized = 1;
1518 void ptlrpc_nrs_req_stop_nolock(struct ptlrpc_request *req)
1520 if (req->rq_nrq.nr_started)
1521 nrs_request_stop(&req->rq_nrq);
1525 * Enqueues request \a req on either the regular or high-priority NRS head
1526 * of service partition \a svcpt.
1528 * \param[in] svcpt the service partition
1529 * \param[in] req the request to be enqueued
1530 * \param[in] hp whether to enqueue the request on the regular or
1531 * high-priority NRS head.
1533 void ptlrpc_nrs_req_add(struct ptlrpc_service_part *svcpt,
1534 struct ptlrpc_request *req, bool hp)
1536 spin_lock(&svcpt->scp_req_lock);
1539 ptlrpc_nrs_hpreq_add_nolock(req);
1541 ptlrpc_nrs_req_add_nolock(req);
1543 spin_unlock(&svcpt->scp_req_lock);
1546 static void nrs_request_removed(struct ptlrpc_nrs_policy *policy)
1548 LASSERT(policy->pol_nrs->nrs_req_queued > 0);
1549 LASSERT(policy->pol_req_queued > 0);
1551 policy->pol_nrs->nrs_req_queued--;
1552 policy->pol_req_queued--;
1555 * If the policy has no more requests queued, remove it from
1556 * ptlrpc_nrs::nrs_policy_queued.
1558 if (unlikely(policy->pol_req_queued == 0)) {
1559 list_del_init(&policy->pol_list_queued);
1562 * If there are other policies with queued requests, move the
1563 * current policy to the end so that we can round robin over
1564 * all policies and drain the requests.
1566 } else if (policy->pol_req_queued != policy->pol_nrs->nrs_req_queued) {
1567 LASSERT(policy->pol_req_queued <
1568 policy->pol_nrs->nrs_req_queued);
1570 list_move_tail(&policy->pol_list_queued,
1571 &policy->pol_nrs->nrs_policy_queued);
1576 * Obtains a request for handling from an NRS head of service partition
1579 * \param[in] svcpt the service partition
1580 * \param[in] hp whether to obtain a request from the regular or
1581 * high-priority NRS head.
1582 * \param[in] peek when set, signifies that we just want to examine the
1583 * request, and not handle it, so the request is not removed
1585 * \param[in] force when set, it will force a policy to return a request if it
1588 * \retval the request to be handled
1589 * \retval NULL the head has no requests to serve
1591 struct ptlrpc_request *
1592 ptlrpc_nrs_req_get_nolock0(struct ptlrpc_service_part *svcpt, bool hp,
1593 bool peek, bool force)
1595 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1596 struct ptlrpc_nrs_policy *policy;
1597 struct ptlrpc_nrs_request *nrq;
1600 * Always try to drain requests from all NRS polices even if they are
1601 * inactive, because the user can change policy status at runtime.
1603 list_for_each_entry(policy, &nrs->nrs_policy_queued,
1605 nrq = nrs_request_get(policy, peek, force);
1607 if (likely(!peek)) {
1608 nrq->nr_started = 1;
1610 policy->pol_req_started++;
1611 policy->pol_nrs->nrs_req_started++;
1613 nrs_request_removed(policy);
1616 return container_of(nrq, struct ptlrpc_request, rq_nrq);
1624 * Dequeues request \a req from the policy it has been enqueued on.
1626 * \param[in] req the request
1628 void ptlrpc_nrs_req_del_nolock(struct ptlrpc_request *req)
1630 struct ptlrpc_nrs_policy *policy = nrs_request_policy(&req->rq_nrq);
1632 policy->pol_desc->pd_ops->op_req_dequeue(policy, &req->rq_nrq);
1634 req->rq_nrq.nr_enqueued = 0;
1636 nrs_request_removed(policy);
1640 * Returns whether there are any requests currently enqueued on any of the
1641 * policies of service partition's \a svcpt NRS head specified by \a hp. Should
1642 * be called while holding ptlrpc_service_part::scp_req_lock to get a reliable
1645 * \param[in] svcpt the service partition to enquire.
1646 * \param[in] hp whether the regular or high-priority NRS head is to be
1649 * \retval false the indicated NRS head has no enqueued requests.
1650 * \retval true the indicated NRS head has some enqueued requests.
1652 bool ptlrpc_nrs_req_pending_nolock(struct ptlrpc_service_part *svcpt, bool hp)
1654 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1656 return nrs->nrs_req_queued > 0;
1660 * Returns whether NRS policy is throttling reqeust
1662 * \param[in] svcpt the service partition to enquire.
1663 * \param[in] hp whether the regular or high-priority NRS head is to be
1666 * \retval false the indicated NRS head has no enqueued requests.
1667 * \retval true the indicated NRS head has some enqueued requests.
1669 bool ptlrpc_nrs_req_throttling_nolock(struct ptlrpc_service_part *svcpt,
1672 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1674 return !!nrs->nrs_throttling;
1678 * Moves request \a req from the regular to the high-priority NRS head.
1680 * \param[in] req the request to move
1682 void ptlrpc_nrs_req_hp_move(struct ptlrpc_request *req)
1684 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1685 struct ptlrpc_nrs_request *nrq = &req->rq_nrq;
1686 struct ptlrpc_nrs_resource *res1[NRS_RES_MAX];
1687 struct ptlrpc_nrs_resource *res2[NRS_RES_MAX];
1691 * Obtain the high-priority NRS head resources.
1693 nrs_resource_get_safe(nrs_svcpt2nrs(svcpt, true), nrq, res1, true);
1695 spin_lock(&svcpt->scp_req_lock);
1697 if (!ptlrpc_nrs_req_can_move(req))
1700 ptlrpc_nrs_req_del_nolock(req);
1702 memcpy(res2, nrq->nr_res_ptrs, NRS_RES_MAX * sizeof(res2[0]));
1703 memcpy(nrq->nr_res_ptrs, res1, NRS_RES_MAX * sizeof(res1[0]));
1705 ptlrpc_nrs_hpreq_add_nolock(req);
1707 memcpy(res1, res2, NRS_RES_MAX * sizeof(res1[0]));
1709 spin_unlock(&svcpt->scp_req_lock);
1712 * Release either the regular NRS head resources if we moved the
1713 * request, or the high-priority NRS head resources if we took a
1714 * reference earlier in this function and ptlrpc_nrs_req_can_move()
1717 nrs_resource_put_safe(res1);
1722 * Carries out a control operation \a opc on the policy identified by the
1723 * human-readable \a name, on either all partitions, or only on the first
1724 * partition of service \a svc.
1726 * \param[in] svc the service the policy belongs to.
1727 * \param[in] queue whether to carry out the command on the policy which
1728 * belongs to the regular, high-priority, or both NRS
1729 * heads of service partitions of \a svc.
1730 * \param[in] name the policy to act upon, by human-readable name
1731 * \param[in] opc the opcode of the operation to carry out
1732 * \param[in] single when set, the operation will only be carried out on the
1733 * NRS heads of the first service partition of \a svc.
1734 * This is useful for some policies which e.g. share
1735 * identical values on the same parameters of different
1736 * service partitions; when reading these parameters via
1737 * lprocfs, these policies may just want to obtain and
1738 * print out the values from the first service partition.
1739 * Storing these values centrally elsewhere then could be
1740 * another solution for this.
1741 * \param[in,out] arg can be used as a generic in/out buffer between control
1742 * operations and the user environment.
1744 *\retval -ve error condition
1745 *\retval 0 operation was carried out successfully
1747 int ptlrpc_nrs_policy_control(const struct ptlrpc_service *svc,
1748 enum ptlrpc_nrs_queue_type queue, char *name,
1749 enum ptlrpc_nrs_ctl opc, bool single, void *arg)
1751 struct ptlrpc_service_part *svcpt;
1756 LASSERT(opc != PTLRPC_NRS_CTL_INVALID);
1758 if ((queue & PTLRPC_NRS_QUEUE_BOTH) == 0)
1761 ptlrpc_service_for_each_part(svcpt, i, svc) {
1762 if ((queue & PTLRPC_NRS_QUEUE_REG) != 0) {
1763 rc = nrs_policy_ctl(nrs_svcpt2nrs(svcpt, false), name,
1765 if (rc != 0 || (queue == PTLRPC_NRS_QUEUE_REG &&
1770 if ((queue & PTLRPC_NRS_QUEUE_HP) != 0) {
1772 * XXX: We could optionally check for
1773 * nrs_svc_has_hp(svc) here, and return an error if it
1774 * is false. Right now we rely on the policies' lprocfs
1775 * handlers that call the present function to make this
1776 * check; if they fail to do so, they might hit the
1777 * assertion inside nrs_svcpt2nrs() below.
1779 rc = nrs_policy_ctl(nrs_svcpt2nrs(svcpt, true), name,
1781 if (rc != 0 || single)
1790 * Adds all policies that ship with the ptlrpc module, to NRS core's list of
1791 * policies \e nrs_core.nrs_policies.
1793 * \retval 0 all policies have been registered successfully
1796 int ptlrpc_nrs_init(void)
1801 mutex_init(&nrs_core.nrs_mutex);
1802 INIT_LIST_HEAD(&nrs_core.nrs_policies);
1804 rc = ptlrpc_nrs_policy_register(&nrs_conf_fifo);
1808 #ifdef HAVE_SERVER_SUPPORT
1809 rc = ptlrpc_nrs_policy_register(&nrs_conf_crrn);
1813 rc = ptlrpc_nrs_policy_register(&nrs_conf_orr);
1817 rc = ptlrpc_nrs_policy_register(&nrs_conf_trr);
1820 rc = ptlrpc_nrs_policy_register(&nrs_conf_tbf);
1823 #endif /* HAVE_SERVER_SUPPORT */
1828 * Since no PTLRPC services have been started at this point, all we need
1829 * to do for cleanup is to free the descriptors.
1837 * Removes all policy descriptors from nrs_core::nrs_policies, and frees the
1838 * policy descriptors.
1840 * Since all PTLRPC services are stopped at this point, there are no more
1841 * instances of any policies, because each service will have stopped its policy
1842 * instances in ptlrpc_service_nrs_cleanup(), so we just need to free the
1845 void ptlrpc_nrs_fini(void)
1847 struct ptlrpc_nrs_pol_desc *desc;
1848 struct ptlrpc_nrs_pol_desc *tmp;
1850 list_for_each_entry_safe(desc, tmp, &nrs_core.nrs_policies,
1852 list_del_init(&desc->pd_list);