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);
296 policy->pol_arg[0] = '\0';
299 policy->pol_state = NRS_POL_STATE_STARTED;
301 if (policy->pol_flags & PTLRPC_NRS_FL_FALLBACK) {
303 * This path is only used at PTLRPC service setup time.
305 nrs->nrs_policy_fallback = policy;
308 * Try to stop the current primary policy if there is one.
310 nrs_policy_stop_primary(nrs);
313 * And set the newly-started policy as the primary one.
315 nrs->nrs_policy_primary = policy;
319 nrs->nrs_policy_starting = 0;
325 * Increases the policy's usage reference count.
327 static inline void nrs_policy_get_locked(struct ptlrpc_nrs_policy *policy)
333 * Decreases the policy's usage reference count, and stops the policy in case it
334 * was already stopping and have no more outstanding usage references (which
335 * indicates it has no more queued or started requests, and can be safely
338 static void nrs_policy_put_locked(struct ptlrpc_nrs_policy *policy)
340 LASSERT(policy->pol_ref > 0);
343 if (unlikely(policy->pol_ref == 0 &&
344 policy->pol_state == NRS_POL_STATE_STOPPING))
345 nrs_policy_stop0(policy);
348 static void nrs_policy_put(struct ptlrpc_nrs_policy *policy)
350 spin_lock(&policy->pol_nrs->nrs_lock);
351 nrs_policy_put_locked(policy);
352 spin_unlock(&policy->pol_nrs->nrs_lock);
356 * Find and return a policy by name.
358 static struct ptlrpc_nrs_policy * nrs_policy_find_locked(struct ptlrpc_nrs *nrs,
361 struct ptlrpc_nrs_policy *tmp;
363 list_for_each_entry(tmp, &nrs->nrs_policy_list, pol_list) {
364 if (strncmp(tmp->pol_desc->pd_name, name,
365 NRS_POL_NAME_MAX) == 0) {
366 nrs_policy_get_locked(tmp);
374 * Release references for the resource hierarchy moving upwards towards the
375 * policy instance resource.
377 static void nrs_resource_put(struct ptlrpc_nrs_resource *res)
379 struct ptlrpc_nrs_policy *policy = res->res_policy;
381 if (policy->pol_desc->pd_ops->op_res_put != NULL) {
382 struct ptlrpc_nrs_resource *parent;
384 for (; res != NULL; res = parent) {
385 parent = res->res_parent;
386 policy->pol_desc->pd_ops->op_res_put(policy, res);
392 * Obtains references for each resource in the resource hierarchy for request
393 * \a nrq if it is to be handled by \a policy.
395 * \param[in] policy the policy
396 * \param[in] nrq the request
397 * \param[in] moving_req denotes whether this is a call to the function by
398 * ldlm_lock_reorder_req(), in order to move \a nrq to
399 * the high-priority NRS head; we should not sleep when
402 * \retval NULL resource hierarchy references not obtained
403 * \retval valid-pointer the bottom level of the resource hierarchy
405 * \see ptlrpc_nrs_pol_ops::op_res_get()
408 struct ptlrpc_nrs_resource * nrs_resource_get(struct ptlrpc_nrs_policy *policy,
409 struct ptlrpc_nrs_request *nrq,
413 * Set to NULL to traverse the resource hierarchy from the top.
415 struct ptlrpc_nrs_resource *res = NULL;
416 struct ptlrpc_nrs_resource *tmp = NULL;
420 rc = policy->pol_desc->pd_ops->op_res_get(policy, nrq, res,
424 nrs_resource_put(res);
428 LASSERT(tmp != NULL);
429 tmp->res_parent = res;
430 tmp->res_policy = policy;
434 * Return once we have obtained a reference to the bottom level
435 * of the resource hierarchy.
443 * Obtains resources for the resource hierarchies and policy references for
444 * the fallback and current primary policy (if any), that will later be used
445 * to handle request \a nrq.
447 * \param[in] nrs the NRS head instance that will be handling request \a nrq.
448 * \param[in] nrq the request that is being handled.
449 * \param[out] resp the array where references to the resource hierarchy are
451 * \param[in] moving_req is set when obtaining resources while moving a
452 * request from a policy on the regular NRS head to a
453 * policy on the HP NRS head (via
454 * ldlm_lock_reorder_req()). It signifies that
455 * allocations to get resources should be atomic; for
456 * a full explanation, see comment in
457 * ptlrpc_nrs_pol_ops::op_res_get().
459 static void nrs_resource_get_safe(struct ptlrpc_nrs *nrs,
460 struct ptlrpc_nrs_request *nrq,
461 struct ptlrpc_nrs_resource **resp,
464 struct ptlrpc_nrs_policy *primary = NULL;
465 struct ptlrpc_nrs_policy *fallback = NULL;
467 memset(resp, 0, sizeof(resp[0]) * NRS_RES_MAX);
470 * Obtain policy references.
472 spin_lock(&nrs->nrs_lock);
474 fallback = nrs->nrs_policy_fallback;
475 nrs_policy_get_locked(fallback);
477 primary = nrs->nrs_policy_primary;
479 nrs_policy_get_locked(primary);
481 spin_unlock(&nrs->nrs_lock);
484 * Obtain resource hierarchy references.
486 resp[NRS_RES_FALLBACK] = nrs_resource_get(fallback, nrq, moving_req);
487 LASSERT(resp[NRS_RES_FALLBACK] != NULL);
489 if (primary != NULL) {
490 resp[NRS_RES_PRIMARY] = nrs_resource_get(primary, nrq,
493 * A primary policy may exist which may not wish to serve a
494 * particular request for different reasons; release the
495 * reference on the policy as it will not be used for this
498 if (resp[NRS_RES_PRIMARY] == NULL)
499 nrs_policy_put(primary);
504 * Releases references to resource hierarchies and policies, because they are no
505 * longer required; used when request handling has been completed, or the
506 * request is moving to the high priority NRS head.
508 * \param resp the resource hierarchy that is being released
510 * \see ptlrpcnrs_req_hp_move()
511 * \see ptlrpc_nrs_req_finalize()
513 static void nrs_resource_put_safe(struct ptlrpc_nrs_resource **resp)
515 struct ptlrpc_nrs_policy *pols[NRS_RES_MAX];
516 struct ptlrpc_nrs *nrs = NULL;
519 for (i = 0; i < NRS_RES_MAX; i++) {
520 if (resp[i] != NULL) {
521 pols[i] = resp[i]->res_policy;
522 nrs_resource_put(resp[i]);
529 for (i = 0; i < NRS_RES_MAX; i++) {
534 nrs = pols[i]->pol_nrs;
535 spin_lock(&nrs->nrs_lock);
537 nrs_policy_put_locked(pols[i]);
541 spin_unlock(&nrs->nrs_lock);
545 * Obtains an NRS request from \a policy for handling or examination; the
546 * request should be removed in the 'handling' case.
548 * Calling into this function implies we already know the policy has a request
549 * waiting to be handled.
551 * \param[in] policy the policy from which a request
552 * \param[in] peek when set, signifies that we just want to examine the
553 * request, and not handle it, so the request is not removed
555 * \param[in] force when set, it will force a policy to return a request if it
558 * \retval the NRS request to be handled
561 struct ptlrpc_nrs_request * nrs_request_get(struct ptlrpc_nrs_policy *policy,
562 bool peek, bool force)
564 struct ptlrpc_nrs_request *nrq;
566 LASSERT(policy->pol_req_queued > 0);
568 nrq = policy->pol_desc->pd_ops->op_req_get(policy, peek, force);
570 LASSERT(ergo(nrq != NULL, nrs_request_policy(nrq) == policy));
576 * Enqueues request \a nrq for later handling, via one one the policies for
577 * which resources where earlier obtained via nrs_resource_get_safe(). The
578 * function attempts to enqueue the request first on the primary policy
579 * (if any), since this is the preferred choice.
581 * \param nrq the request being enqueued
583 * \see nrs_resource_get_safe()
585 static inline void nrs_request_enqueue(struct ptlrpc_nrs_request *nrq)
587 struct ptlrpc_nrs_policy *policy;
592 * Try in descending order, because the primary policy (if any) is
593 * the preferred choice.
595 for (i = NRS_RES_MAX - 1; i >= 0; i--) {
596 if (nrq->nr_res_ptrs[i] == NULL)
600 policy = nrq->nr_res_ptrs[i]->res_policy;
602 rc = policy->pol_desc->pd_ops->op_req_enqueue(policy, nrq);
604 policy->pol_nrs->nrs_req_queued++;
605 policy->pol_req_queued++;
610 * Should never get here, as at least the primary policy's
611 * ptlrpc_nrs_pol_ops::op_req_enqueue() implementation should always
618 * Called when a request has been handled
620 * \param[in] nrs the request that has been handled; can be used for
621 * job/resource control.
623 * \see ptlrpc_nrs_req_stop_nolock()
625 static inline void nrs_request_stop(struct ptlrpc_nrs_request *nrq)
627 struct ptlrpc_nrs_policy *policy = nrs_request_policy(nrq);
629 if (policy->pol_desc->pd_ops->op_req_stop)
630 policy->pol_desc->pd_ops->op_req_stop(policy, nrq);
632 LASSERT(policy->pol_nrs->nrs_req_started > 0);
633 LASSERT(policy->pol_req_started > 0);
635 policy->pol_nrs->nrs_req_started--;
636 policy->pol_req_started--;
640 * Handler for operations that can be carried out on policies.
642 * Handles opcodes that are common to all policy types within NRS core, and
643 * passes any unknown opcodes to the policy-specific control function.
645 * \param[in] nrs the NRS head this policy belongs to.
646 * \param[in] name the human-readable policy name; should be the same as
647 * ptlrpc_nrs_pol_desc::pd_name.
648 * \param[in] opc the opcode of the operation being carried out.
649 * \param[in,out] arg can be used to pass information in and out between when
650 * carrying an operation; usually data that is private to
651 * the policy at some level, or generic policy status
654 * \retval -ve error condition
655 * \retval 0 operation was carried out successfully
657 static int nrs_policy_ctl(struct ptlrpc_nrs *nrs, char *name,
658 enum ptlrpc_nrs_ctl opc, void *arg)
660 struct ptlrpc_nrs_policy *policy;
664 spin_lock(&nrs->nrs_lock);
666 policy = nrs_policy_find_locked(nrs, name);
668 GOTO(out, rc = -ENOENT);
670 if (policy->pol_state != NRS_POL_STATE_STARTED &&
671 policy->pol_state != NRS_POL_STATE_STOPPED)
672 GOTO(out, rc = -EAGAIN);
676 * Unknown opcode, pass it down to the policy-specific control
677 * function for handling.
680 rc = nrs_policy_ctl_locked(policy, opc, arg);
686 case PTLRPC_NRS_CTL_START:
687 rc = nrs_policy_start_locked(policy, arg);
692 nrs_policy_put_locked(policy);
694 spin_unlock(&nrs->nrs_lock);
700 * Unregisters a policy by name.
702 * \param[in] nrs the NRS head this policy belongs to.
703 * \param[in] name the human-readable policy name; should be the same as
704 * ptlrpc_nrs_pol_desc::pd_name
709 static int nrs_policy_unregister(struct ptlrpc_nrs *nrs, char *name)
711 struct ptlrpc_nrs_policy *policy = NULL;
714 spin_lock(&nrs->nrs_lock);
716 policy = nrs_policy_find_locked(nrs, name);
717 if (policy == NULL) {
718 spin_unlock(&nrs->nrs_lock);
720 CERROR("Can't find NRS policy %s\n", name);
724 if (policy->pol_ref > 1) {
725 CERROR("Policy %s is busy with %d references\n", name,
726 (int)policy->pol_ref);
727 nrs_policy_put_locked(policy);
729 spin_unlock(&nrs->nrs_lock);
733 LASSERT(policy->pol_req_queued == 0);
734 LASSERT(policy->pol_req_started == 0);
736 if (policy->pol_state != NRS_POL_STATE_STOPPED) {
737 nrs_policy_stop_locked(policy);
738 LASSERT(policy->pol_state == NRS_POL_STATE_STOPPED);
741 list_del(&policy->pol_list);
744 nrs_policy_put_locked(policy);
746 spin_unlock(&nrs->nrs_lock);
748 nrs_policy_fini(policy);
750 LASSERT(policy->pol_private == NULL);
751 OBD_FREE_PTR(policy);
757 * Register a policy from \policy descriptor \a desc with NRS head \a nrs.
759 * \param[in] nrs the NRS head on which the policy will be registered.
760 * \param[in] desc the policy descriptor from which the information will be
761 * obtained to register the policy.
766 static int nrs_policy_register(struct ptlrpc_nrs *nrs,
767 struct ptlrpc_nrs_pol_desc *desc)
769 struct ptlrpc_nrs_policy *policy;
770 struct ptlrpc_nrs_policy *tmp;
771 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
775 LASSERT(svcpt != NULL);
776 LASSERT(desc->pd_ops != NULL);
777 LASSERT(desc->pd_ops->op_res_get != NULL);
778 LASSERT(desc->pd_ops->op_req_get != NULL);
779 LASSERT(desc->pd_ops->op_req_enqueue != NULL);
780 LASSERT(desc->pd_ops->op_req_dequeue != NULL);
781 LASSERT(desc->pd_compat != NULL);
783 OBD_CPT_ALLOC_GFP(policy, svcpt->scp_service->srv_cptable,
784 svcpt->scp_cpt, sizeof(*policy), GFP_NOFS);
788 policy->pol_nrs = nrs;
789 policy->pol_desc = desc;
790 policy->pol_state = NRS_POL_STATE_STOPPED;
791 policy->pol_flags = desc->pd_flags;
793 INIT_LIST_HEAD(&policy->pol_list);
794 INIT_LIST_HEAD(&policy->pol_list_queued);
796 rc = nrs_policy_init(policy);
798 OBD_FREE_PTR(policy);
802 spin_lock(&nrs->nrs_lock);
804 tmp = nrs_policy_find_locked(nrs, policy->pol_desc->pd_name);
806 CERROR("NRS policy %s has been registered, can't register it "
807 "for %s\n", policy->pol_desc->pd_name,
808 svcpt->scp_service->srv_name);
809 nrs_policy_put_locked(tmp);
811 spin_unlock(&nrs->nrs_lock);
812 nrs_policy_fini(policy);
813 OBD_FREE_PTR(policy);
818 list_add_tail(&policy->pol_list, &nrs->nrs_policy_list);
821 if (policy->pol_flags & PTLRPC_NRS_FL_REG_START)
822 rc = nrs_policy_start_locked(policy, NULL);
824 spin_unlock(&nrs->nrs_lock);
827 (void) nrs_policy_unregister(nrs, policy->pol_desc->pd_name);
833 * Enqueue request \a req using one of the policies its resources are referring
836 * \param[in] req the request to enqueue.
838 static void ptlrpc_nrs_req_add_nolock(struct ptlrpc_request *req)
840 struct ptlrpc_nrs_policy *policy;
842 LASSERT(req->rq_nrq.nr_initialized);
843 LASSERT(!req->rq_nrq.nr_enqueued);
845 nrs_request_enqueue(&req->rq_nrq);
846 req->rq_nrq.nr_enqueued = 1;
848 policy = nrs_request_policy(&req->rq_nrq);
850 * Add the policy to the NRS head's list of policies with enqueued
851 * requests, if it has not been added there.
853 if (unlikely(list_empty(&policy->pol_list_queued)))
854 list_add_tail(&policy->pol_list_queued,
855 &policy->pol_nrs->nrs_policy_queued);
859 * Enqueue a request on the high priority NRS head.
861 * \param req the request to enqueue.
863 static void ptlrpc_nrs_hpreq_add_nolock(struct ptlrpc_request *req)
865 int opc = lustre_msg_get_opc(req->rq_reqmsg);
868 spin_lock(&req->rq_lock);
870 ptlrpc_nrs_req_add_nolock(req);
872 DEBUG_REQ(D_NET, req, "high priority req");
873 spin_unlock(&req->rq_lock);
878 * Returns a boolean predicate indicating whether the policy described by
879 * \a desc is adequate for use with service \a svc.
881 * \param[in] svc the service
882 * \param[in] desc the policy descriptor
884 * \retval false the policy is not compatible with the service
885 * \retval true the policy is compatible with the service
887 static inline bool nrs_policy_compatible(const struct ptlrpc_service *svc,
888 const struct ptlrpc_nrs_pol_desc *desc)
890 return desc->pd_compat(svc, desc);
894 * Registers all compatible policies in nrs_core.nrs_policies, for NRS head
897 * \param[in] nrs the NRS head
902 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
904 * \see ptlrpc_service_nrs_setup()
906 static int nrs_register_policies_locked(struct ptlrpc_nrs *nrs)
908 struct ptlrpc_nrs_pol_desc *desc;
909 /* for convenience */
910 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
911 struct ptlrpc_service *svc = svcpt->scp_service;
915 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
917 list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
918 if (nrs_policy_compatible(svc, desc)) {
919 rc = nrs_policy_register(nrs, desc);
921 CERROR("Failed to register NRS policy %s for "
922 "partition %d of service %s: %d\n",
923 desc->pd_name, svcpt->scp_cpt,
926 * Fail registration if any of the policies'
927 * registration fails.
938 * Initializes NRS head \a nrs of service partition \a svcpt, and registers all
939 * compatible policies in NRS core, with the NRS head.
941 * \param[in] nrs the NRS head
942 * \param[in] svcpt the PTLRPC service partition to setup
947 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
949 static int nrs_svcpt_setup_locked0(struct ptlrpc_nrs *nrs,
950 struct ptlrpc_service_part *svcpt)
953 enum ptlrpc_nrs_queue_type queue;
955 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
957 if (nrs == &svcpt->scp_nrs_reg)
958 queue = PTLRPC_NRS_QUEUE_REG;
959 else if (nrs == svcpt->scp_nrs_hp)
960 queue = PTLRPC_NRS_QUEUE_HP;
964 nrs->nrs_svcpt = svcpt;
965 nrs->nrs_queue_type = queue;
966 spin_lock_init(&nrs->nrs_lock);
967 INIT_LIST_HEAD(&nrs->nrs_policy_list);
968 INIT_LIST_HEAD(&nrs->nrs_policy_queued);
969 nrs->nrs_throttling = 0;
971 rc = nrs_register_policies_locked(nrs);
977 * Allocates a regular and optionally a high-priority NRS head (if the service
978 * handles high-priority RPCs), and then registers all available compatible
979 * policies on those NRS heads.
981 * \param[in,out] svcpt the PTLRPC service partition to setup
983 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
985 static int nrs_svcpt_setup_locked(struct ptlrpc_service_part *svcpt)
987 struct ptlrpc_nrs *nrs;
991 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
994 * Initialize the regular NRS head.
996 nrs = nrs_svcpt2nrs(svcpt, false);
997 rc = nrs_svcpt_setup_locked0(nrs, svcpt);
1002 * Optionally allocate a high-priority NRS head.
1004 if (svcpt->scp_service->srv_ops.so_hpreq_handler == NULL)
1007 OBD_CPT_ALLOC_PTR(svcpt->scp_nrs_hp,
1008 svcpt->scp_service->srv_cptable,
1010 if (svcpt->scp_nrs_hp == NULL)
1011 GOTO(out, rc = -ENOMEM);
1013 nrs = nrs_svcpt2nrs(svcpt, true);
1014 rc = nrs_svcpt_setup_locked0(nrs, svcpt);
1021 * Unregisters all policies on all available NRS heads in a service partition;
1022 * called at PTLRPC service unregistration time.
1024 * \param[in] svcpt the PTLRPC service partition
1026 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
1028 static void nrs_svcpt_cleanup_locked(struct ptlrpc_service_part *svcpt)
1030 struct ptlrpc_nrs *nrs;
1031 struct ptlrpc_nrs_policy *policy;
1032 struct ptlrpc_nrs_policy *tmp;
1037 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
1040 /* scp_nrs_hp could be NULL due to short of memory. */
1041 nrs = hp ? svcpt->scp_nrs_hp : &svcpt->scp_nrs_reg;
1042 /* check the nrs_svcpt to see if nrs is initialized. */
1043 if (!nrs || !nrs->nrs_svcpt) {
1047 nrs->nrs_stopping = 1;
1049 list_for_each_entry_safe(policy, tmp, &nrs->nrs_policy_list,
1051 rc = nrs_policy_unregister(nrs, policy->pol_desc->pd_name);
1056 * If the service partition has an HP NRS head, clean that up as well.
1058 if (!hp && nrs_svcpt_has_hp(svcpt)) {
1070 * Returns the descriptor for a policy as identified by by \a name.
1072 * \param[in] name the policy name
1074 * \retval the policy descriptor
1077 static struct ptlrpc_nrs_pol_desc *nrs_policy_find_desc_locked(const char *name)
1079 struct ptlrpc_nrs_pol_desc *tmp;
1082 list_for_each_entry(tmp, &nrs_core.nrs_policies, pd_list) {
1083 if (strncmp(tmp->pd_name, name, NRS_POL_NAME_MAX) == 0)
1090 * Removes the policy from all supported NRS heads of all partitions of all
1093 * \param[in] desc the policy descriptor to unregister
1096 * \retval 0 successfully unregistered policy on all supported NRS heads
1098 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
1099 * \pre mutex_is_locked(&ptlrpc_all_services_mutex)
1101 static int nrs_policy_unregister_locked(struct ptlrpc_nrs_pol_desc *desc)
1103 struct ptlrpc_nrs *nrs;
1104 struct ptlrpc_service *svc;
1105 struct ptlrpc_service_part *svcpt;
1110 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
1111 LASSERT(mutex_is_locked(&ptlrpc_all_services_mutex));
1113 list_for_each_entry(svc, &ptlrpc_all_services, srv_list) {
1115 if (!nrs_policy_compatible(svc, desc) ||
1116 unlikely(svc->srv_is_stopping))
1119 ptlrpc_service_for_each_part(svcpt, i, svc) {
1123 nrs = nrs_svcpt2nrs(svcpt, hp);
1124 rc = nrs_policy_unregister(nrs, desc->pd_name);
1126 * Ignore -ENOENT as the policy may not have registered
1127 * successfully on all service partitions.
1129 if (rc == -ENOENT) {
1131 } else if (rc != 0) {
1132 CERROR("Failed to unregister NRS policy %s for "
1133 "partition %d of service %s: %d\n",
1134 desc->pd_name, svcpt->scp_cpt,
1135 svcpt->scp_service->srv_name, rc);
1139 if (!hp && nrs_svc_has_hp(svc)) {
1145 if (desc->pd_ops->op_lprocfs_fini != NULL)
1146 desc->pd_ops->op_lprocfs_fini(svc);
1153 * Registers a new policy with NRS core.
1155 * The function will only succeed if policy registration with all compatible
1156 * service partitions (if any) is successful.
1158 * N.B. This function should be called either at ptlrpc module initialization
1159 * time when registering a policy that ships with NRS core, or in a
1160 * module's init() function for policies registering from other modules.
1162 * \param[in] conf configuration information for the new policy to register
1167 int ptlrpc_nrs_policy_register(struct ptlrpc_nrs_pol_conf *conf)
1169 struct ptlrpc_service *svc;
1170 struct ptlrpc_nrs_pol_desc *desc;
1174 LASSERT(conf != NULL);
1175 LASSERT(conf->nc_ops != NULL);
1176 LASSERT(conf->nc_compat != NULL);
1177 LASSERT(ergo(conf->nc_compat == nrs_policy_compat_one,
1178 conf->nc_compat_svc_name != NULL));
1179 LASSERT(ergo((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) != 0,
1180 conf->nc_owner != NULL));
1182 conf->nc_name[NRS_POL_NAME_MAX - 1] = '\0';
1185 * External policies are not allowed to start immediately upon
1186 * registration, as there is a relatively higher chance that their
1187 * registration might fail. In such a case, some policy instances may
1188 * already have requests queued wen unregistration needs to happen as
1189 * part o cleanup; since there is currently no way to drain requests
1190 * from a policy unless the service is unregistering, we just disallow
1193 if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) &&
1194 (conf->nc_flags & (PTLRPC_NRS_FL_FALLBACK |
1195 PTLRPC_NRS_FL_REG_START))) {
1196 CERROR("NRS: failing to register policy %s. Please check "
1197 "policy flags; external policies cannot act as fallback "
1198 "policies, or be started immediately upon registration "
1199 "without interaction with lprocfs\n", conf->nc_name);
1203 mutex_lock(&nrs_core.nrs_mutex);
1205 if (nrs_policy_find_desc_locked(conf->nc_name) != NULL) {
1206 CERROR("NRS: failing to register policy %s which has already "
1207 "been registered with NRS core!\n",
1209 GOTO(fail, rc = -EEXIST);
1212 OBD_ALLOC_PTR(desc);
1214 GOTO(fail, rc = -ENOMEM);
1216 if (strlcpy(desc->pd_name, conf->nc_name, sizeof(desc->pd_name)) >=
1217 sizeof(desc->pd_name)) {
1219 GOTO(fail, rc = -E2BIG);
1221 desc->pd_ops = conf->nc_ops;
1222 desc->pd_compat = conf->nc_compat;
1223 desc->pd_compat_svc_name = conf->nc_compat_svc_name;
1224 if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) != 0)
1225 desc->pd_owner = conf->nc_owner;
1226 desc->pd_flags = conf->nc_flags;
1227 atomic_set(&desc->pd_refs, 0);
1230 * For policies that are held in the same module as NRS (currently
1231 * ptlrpc), do not register the policy with all compatible services,
1232 * as the services will not have started at this point, since we are
1233 * calling from ptlrpc module initialization code. In such cases each
1234 * service will register all compatible policies later, via
1235 * ptlrpc_service_nrs_setup().
1237 if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) == 0)
1241 * Register the new policy on all compatible services
1243 mutex_lock(&ptlrpc_all_services_mutex);
1245 list_for_each_entry(svc, &ptlrpc_all_services, srv_list) {
1246 struct ptlrpc_service_part *svcpt;
1250 if (!nrs_policy_compatible(svc, desc) ||
1251 unlikely(svc->srv_is_stopping))
1254 ptlrpc_service_for_each_part(svcpt, i, svc) {
1255 struct ptlrpc_nrs *nrs;
1258 nrs = nrs_svcpt2nrs(svcpt, hp);
1259 rc = nrs_policy_register(nrs, desc);
1261 CERROR("Failed to register NRS policy %s for "
1262 "partition %d of service %s: %d\n",
1263 desc->pd_name, svcpt->scp_cpt,
1264 svcpt->scp_service->srv_name, rc);
1266 rc2 = nrs_policy_unregister_locked(desc);
1268 * Should not fail at this point
1271 mutex_unlock(&ptlrpc_all_services_mutex);
1276 if (!hp && nrs_svc_has_hp(svc)) {
1283 * No need to take a reference to other modules here, as we
1284 * will be calling from the module's init() function.
1286 if (desc->pd_ops->op_lprocfs_init != NULL) {
1287 rc = desc->pd_ops->op_lprocfs_init(svc);
1289 rc2 = nrs_policy_unregister_locked(desc);
1291 * Should not fail at this point
1294 mutex_unlock(&ptlrpc_all_services_mutex);
1301 mutex_unlock(&ptlrpc_all_services_mutex);
1303 list_add_tail(&desc->pd_list, &nrs_core.nrs_policies);
1305 mutex_unlock(&nrs_core.nrs_mutex);
1309 EXPORT_SYMBOL(ptlrpc_nrs_policy_register);
1312 * Unregisters a previously registered policy with NRS core. All instances of
1313 * the policy on all NRS heads of all supported services are removed.
1315 * N.B. This function should only be called from a module's exit() function.
1316 * Although it can be used for policies that ship alongside NRS core, the
1317 * function is primarily intended for policies that register externally,
1318 * from other modules.
1320 * \param[in] conf configuration information for the policy to unregister
1325 int ptlrpc_nrs_policy_unregister(struct ptlrpc_nrs_pol_conf *conf)
1327 struct ptlrpc_nrs_pol_desc *desc;
1331 LASSERT(conf != NULL);
1333 if (conf->nc_flags & PTLRPC_NRS_FL_FALLBACK) {
1334 CERROR("Unable to unregister a fallback policy, unless the "
1335 "PTLRPC service is stopping.\n");
1339 conf->nc_name[NRS_POL_NAME_MAX - 1] = '\0';
1341 mutex_lock(&nrs_core.nrs_mutex);
1343 desc = nrs_policy_find_desc_locked(conf->nc_name);
1345 CERROR("Failing to unregister NRS policy %s which has "
1346 "not been registered with NRS core!\n",
1348 GOTO(not_exist, rc = -ENOENT);
1351 mutex_lock(&ptlrpc_all_services_mutex);
1353 rc = nrs_policy_unregister_locked(desc);
1356 CERROR("Please first stop policy %s on all service "
1357 "partitions and then retry to unregister the "
1358 "policy.\n", conf->nc_name);
1362 CDEBUG(D_INFO, "Unregistering policy %s from NRS core.\n",
1365 list_del(&desc->pd_list);
1369 mutex_unlock(&ptlrpc_all_services_mutex);
1372 mutex_unlock(&nrs_core.nrs_mutex);
1376 EXPORT_SYMBOL(ptlrpc_nrs_policy_unregister);
1379 * Setup NRS heads on all service partitions of service \a svc, and register
1380 * all compatible policies on those NRS heads.
1382 * To be called from withing ptl
1383 * \param[in] svc the service to setup
1385 * \retval -ve error, the calling logic should eventually call
1386 * ptlrpc_service_nrs_cleanup() to undo any work performed
1389 * \see ptlrpc_register_service()
1390 * \see ptlrpc_service_nrs_cleanup()
1392 int ptlrpc_service_nrs_setup(struct ptlrpc_service *svc)
1394 struct ptlrpc_service_part *svcpt;
1395 const struct ptlrpc_nrs_pol_desc *desc;
1399 mutex_lock(&nrs_core.nrs_mutex);
1402 * Initialize NRS heads on all service CPTs.
1404 ptlrpc_service_for_each_part(svcpt, i, svc) {
1405 rc = nrs_svcpt_setup_locked(svcpt);
1411 * Set up lprocfs interfaces for all supported policies for the
1414 list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
1415 if (!nrs_policy_compatible(svc, desc))
1418 if (desc->pd_ops->op_lprocfs_init != NULL) {
1419 rc = desc->pd_ops->op_lprocfs_init(svc);
1427 mutex_unlock(&nrs_core.nrs_mutex);
1433 * Unregisters all policies on all service partitions of service \a svc.
1435 * \param[in] svc the PTLRPC service to unregister
1437 void ptlrpc_service_nrs_cleanup(struct ptlrpc_service *svc)
1439 struct ptlrpc_service_part *svcpt;
1440 const struct ptlrpc_nrs_pol_desc *desc;
1443 mutex_lock(&nrs_core.nrs_mutex);
1446 * Clean up NRS heads on all service partitions
1448 ptlrpc_service_for_each_part(svcpt, i, svc)
1449 nrs_svcpt_cleanup_locked(svcpt);
1452 * Clean up lprocfs interfaces for all supported policies for the
1455 list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
1456 if (!nrs_policy_compatible(svc, desc))
1459 if (desc->pd_ops->op_lprocfs_fini != NULL)
1460 desc->pd_ops->op_lprocfs_fini(svc);
1463 mutex_unlock(&nrs_core.nrs_mutex);
1467 * Obtains NRS head resources for request \a req.
1469 * These could be either on the regular or HP NRS head of \a svcpt; resources
1470 * taken on the regular head can later be swapped for HP head resources by
1471 * ldlm_lock_reorder_req().
1473 * \param[in] svcpt the service partition
1474 * \param[in] req the request
1475 * \param[in] hp which NRS head of \a svcpt to use
1477 void ptlrpc_nrs_req_initialize(struct ptlrpc_service_part *svcpt,
1478 struct ptlrpc_request *req, bool hp)
1480 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1482 memset(&req->rq_nrq, 0, sizeof(req->rq_nrq));
1483 nrs_resource_get_safe(nrs, &req->rq_nrq, req->rq_nrq.nr_res_ptrs,
1487 * It is fine to access \e nr_initialized without locking as there is
1488 * no contention at this early stage.
1490 req->rq_nrq.nr_initialized = 1;
1494 * Releases resources for a request; is called after the request has been
1497 * \param[in] req the request
1499 * \see ptlrpc_server_finish_request()
1501 void ptlrpc_nrs_req_finalize(struct ptlrpc_request *req)
1503 if (req->rq_nrq.nr_initialized) {
1504 nrs_resource_put_safe(req->rq_nrq.nr_res_ptrs);
1505 /* no protection on bit nr_initialized because no
1506 * contention at this late stage */
1507 req->rq_nrq.nr_finalized = 1;
1511 void ptlrpc_nrs_req_stop_nolock(struct ptlrpc_request *req)
1513 if (req->rq_nrq.nr_started)
1514 nrs_request_stop(&req->rq_nrq);
1518 * Enqueues request \a req on either the regular or high-priority NRS head
1519 * of service partition \a svcpt.
1521 * \param[in] svcpt the service partition
1522 * \param[in] req the request to be enqueued
1523 * \param[in] hp whether to enqueue the request on the regular or
1524 * high-priority NRS head.
1526 void ptlrpc_nrs_req_add(struct ptlrpc_service_part *svcpt,
1527 struct ptlrpc_request *req, bool hp)
1529 spin_lock(&svcpt->scp_req_lock);
1532 ptlrpc_nrs_hpreq_add_nolock(req);
1534 ptlrpc_nrs_req_add_nolock(req);
1536 spin_unlock(&svcpt->scp_req_lock);
1539 static void nrs_request_removed(struct ptlrpc_nrs_policy *policy)
1541 LASSERT(policy->pol_nrs->nrs_req_queued > 0);
1542 LASSERT(policy->pol_req_queued > 0);
1544 policy->pol_nrs->nrs_req_queued--;
1545 policy->pol_req_queued--;
1548 * If the policy has no more requests queued, remove it from
1549 * ptlrpc_nrs::nrs_policy_queued.
1551 if (unlikely(policy->pol_req_queued == 0)) {
1552 list_del_init(&policy->pol_list_queued);
1555 * If there are other policies with queued requests, move the
1556 * current policy to the end so that we can round robin over
1557 * all policies and drain the requests.
1559 } else if (policy->pol_req_queued != policy->pol_nrs->nrs_req_queued) {
1560 LASSERT(policy->pol_req_queued <
1561 policy->pol_nrs->nrs_req_queued);
1563 list_move_tail(&policy->pol_list_queued,
1564 &policy->pol_nrs->nrs_policy_queued);
1569 * Obtains a request for handling from an NRS head of service partition
1572 * \param[in] svcpt the service partition
1573 * \param[in] hp whether to obtain a request from the regular or
1574 * high-priority NRS head.
1575 * \param[in] peek when set, signifies that we just want to examine the
1576 * request, and not handle it, so the request is not removed
1578 * \param[in] force when set, it will force a policy to return a request if it
1581 * \retval the request to be handled
1582 * \retval NULL the head has no requests to serve
1584 struct ptlrpc_request *
1585 ptlrpc_nrs_req_get_nolock0(struct ptlrpc_service_part *svcpt, bool hp,
1586 bool peek, bool force)
1588 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1589 struct ptlrpc_nrs_policy *policy;
1590 struct ptlrpc_nrs_request *nrq;
1593 * Always try to drain requests from all NRS polices even if they are
1594 * inactive, because the user can change policy status at runtime.
1596 list_for_each_entry(policy, &nrs->nrs_policy_queued,
1598 nrq = nrs_request_get(policy, peek, force);
1600 if (likely(!peek)) {
1601 nrq->nr_started = 1;
1603 policy->pol_req_started++;
1604 policy->pol_nrs->nrs_req_started++;
1606 nrs_request_removed(policy);
1609 return container_of(nrq, struct ptlrpc_request, rq_nrq);
1617 * Dequeues request \a req from the policy it has been enqueued on.
1619 * \param[in] req the request
1621 void ptlrpc_nrs_req_del_nolock(struct ptlrpc_request *req)
1623 struct ptlrpc_nrs_policy *policy = nrs_request_policy(&req->rq_nrq);
1625 policy->pol_desc->pd_ops->op_req_dequeue(policy, &req->rq_nrq);
1627 req->rq_nrq.nr_enqueued = 0;
1629 nrs_request_removed(policy);
1633 * Returns whether there are any requests currently enqueued on any of the
1634 * policies of service partition's \a svcpt NRS head specified by \a hp. Should
1635 * be called while holding ptlrpc_service_part::scp_req_lock to get a reliable
1638 * \param[in] svcpt the service partition to enquire.
1639 * \param[in] hp whether the regular or high-priority NRS head is to be
1642 * \retval false the indicated NRS head has no enqueued requests.
1643 * \retval true the indicated NRS head has some enqueued requests.
1645 bool ptlrpc_nrs_req_pending_nolock(struct ptlrpc_service_part *svcpt, bool hp)
1647 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1649 return nrs->nrs_req_queued > 0;
1653 * Returns whether NRS policy is throttling reqeust
1655 * \param[in] svcpt the service partition to enquire.
1656 * \param[in] hp whether the regular or high-priority NRS head is to be
1659 * \retval false the indicated NRS head has no enqueued requests.
1660 * \retval true the indicated NRS head has some enqueued requests.
1662 bool ptlrpc_nrs_req_throttling_nolock(struct ptlrpc_service_part *svcpt,
1665 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1667 return !!nrs->nrs_throttling;
1671 * Moves request \a req from the regular to the high-priority NRS head.
1673 * \param[in] req the request to move
1675 void ptlrpc_nrs_req_hp_move(struct ptlrpc_request *req)
1677 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1678 struct ptlrpc_nrs_request *nrq = &req->rq_nrq;
1679 struct ptlrpc_nrs_resource *res1[NRS_RES_MAX];
1680 struct ptlrpc_nrs_resource *res2[NRS_RES_MAX];
1684 * Obtain the high-priority NRS head resources.
1686 nrs_resource_get_safe(nrs_svcpt2nrs(svcpt, true), nrq, res1, true);
1688 spin_lock(&svcpt->scp_req_lock);
1690 if (!ptlrpc_nrs_req_can_move(req))
1693 ptlrpc_nrs_req_del_nolock(req);
1695 memcpy(res2, nrq->nr_res_ptrs, NRS_RES_MAX * sizeof(res2[0]));
1696 memcpy(nrq->nr_res_ptrs, res1, NRS_RES_MAX * sizeof(res1[0]));
1698 ptlrpc_nrs_hpreq_add_nolock(req);
1700 memcpy(res1, res2, NRS_RES_MAX * sizeof(res1[0]));
1702 spin_unlock(&svcpt->scp_req_lock);
1705 * Release either the regular NRS head resources if we moved the
1706 * request, or the high-priority NRS head resources if we took a
1707 * reference earlier in this function and ptlrpc_nrs_req_can_move()
1710 nrs_resource_put_safe(res1);
1715 * Carries out a control operation \a opc on the policy identified by the
1716 * human-readable \a name, on either all partitions, or only on the first
1717 * partition of service \a svc.
1719 * \param[in] svc the service the policy belongs to.
1720 * \param[in] queue whether to carry out the command on the policy which
1721 * belongs to the regular, high-priority, or both NRS
1722 * heads of service partitions of \a svc.
1723 * \param[in] name the policy to act upon, by human-readable name
1724 * \param[in] opc the opcode of the operation to carry out
1725 * \param[in] single when set, the operation will only be carried out on the
1726 * NRS heads of the first service partition of \a svc.
1727 * This is useful for some policies which e.g. share
1728 * identical values on the same parameters of different
1729 * service partitions; when reading these parameters via
1730 * lprocfs, these policies may just want to obtain and
1731 * print out the values from the first service partition.
1732 * Storing these values centrally elsewhere then could be
1733 * another solution for this.
1734 * \param[in,out] arg can be used as a generic in/out buffer between control
1735 * operations and the user environment.
1737 *\retval -ve error condition
1738 *\retval 0 operation was carried out successfully
1740 int ptlrpc_nrs_policy_control(const struct ptlrpc_service *svc,
1741 enum ptlrpc_nrs_queue_type queue, char *name,
1742 enum ptlrpc_nrs_ctl opc, bool single, void *arg)
1744 struct ptlrpc_service_part *svcpt;
1749 LASSERT(opc != PTLRPC_NRS_CTL_INVALID);
1751 if ((queue & PTLRPC_NRS_QUEUE_BOTH) == 0)
1754 ptlrpc_service_for_each_part(svcpt, i, svc) {
1755 if ((queue & PTLRPC_NRS_QUEUE_REG) != 0) {
1756 rc = nrs_policy_ctl(nrs_svcpt2nrs(svcpt, false), name,
1758 if (rc != 0 || (queue == PTLRPC_NRS_QUEUE_REG &&
1763 if ((queue & PTLRPC_NRS_QUEUE_HP) != 0) {
1765 * XXX: We could optionally check for
1766 * nrs_svc_has_hp(svc) here, and return an error if it
1767 * is false. Right now we rely on the policies' lprocfs
1768 * handlers that call the present function to make this
1769 * check; if they fail to do so, they might hit the
1770 * assertion inside nrs_svcpt2nrs() below.
1772 rc = nrs_policy_ctl(nrs_svcpt2nrs(svcpt, true), name,
1774 if (rc != 0 || single)
1783 * Adds all policies that ship with the ptlrpc module, to NRS core's list of
1784 * policies \e nrs_core.nrs_policies.
1786 * \retval 0 all policies have been registered successfully
1789 int ptlrpc_nrs_init(void)
1794 mutex_init(&nrs_core.nrs_mutex);
1795 INIT_LIST_HEAD(&nrs_core.nrs_policies);
1797 rc = ptlrpc_nrs_policy_register(&nrs_conf_fifo);
1801 #ifdef HAVE_SERVER_SUPPORT
1802 rc = ptlrpc_nrs_policy_register(&nrs_conf_crrn);
1806 rc = ptlrpc_nrs_policy_register(&nrs_conf_orr);
1810 rc = ptlrpc_nrs_policy_register(&nrs_conf_trr);
1813 rc = ptlrpc_nrs_policy_register(&nrs_conf_tbf);
1817 rc = ptlrpc_nrs_policy_register(&nrs_conf_delay);
1820 #endif /* HAVE_SERVER_SUPPORT */
1825 * Since no PTLRPC services have been started at this point, all we need
1826 * to do for cleanup is to free the descriptors.
1834 * Removes all policy descriptors from nrs_core::nrs_policies, and frees the
1835 * policy descriptors.
1837 * Since all PTLRPC services are stopped at this point, there are no more
1838 * instances of any policies, because each service will have stopped its policy
1839 * instances in ptlrpc_service_nrs_cleanup(), so we just need to free the
1842 void ptlrpc_nrs_fini(void)
1844 struct ptlrpc_nrs_pol_desc *desc;
1845 struct ptlrpc_nrs_pol_desc *tmp;
1847 list_for_each_entry_safe(desc, tmp, &nrs_core.nrs_policies,
1849 list_del_init(&desc->pd_list);