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 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
44 #include <liblustre.h>
46 #include <obd_support.h>
47 #include <obd_class.h>
48 #include <lustre_net.h>
49 #include <lprocfs_status.h>
50 #include <libcfs/libcfs.h>
51 #include "ptlrpc_internal.h"
56 struct nrs_core nrs_core;
58 static int nrs_policy_init(struct ptlrpc_nrs_policy *policy)
60 return policy->pol_desc->pd_ops->op_policy_init != NULL ?
61 policy->pol_desc->pd_ops->op_policy_init(policy) : 0;
64 static void nrs_policy_fini(struct ptlrpc_nrs_policy *policy)
66 LASSERT(policy->pol_ref == 0);
67 LASSERT(policy->pol_req_queued == 0);
69 if (policy->pol_desc->pd_ops->op_policy_fini != NULL)
70 policy->pol_desc->pd_ops->op_policy_fini(policy);
73 static int nrs_policy_ctl_locked(struct ptlrpc_nrs_policy *policy,
74 enum ptlrpc_nrs_ctl opc, void *arg)
77 * The policy may be stopped, but the lprocfs files and
78 * ptlrpc_nrs_policy instances remain present until unregistration time.
79 * Do not perform the ctl operation if the policy is stopped, as
80 * policy->pol_private will be NULL in such a case.
82 if (policy->pol_state == NRS_POL_STATE_STOPPED)
85 RETURN(policy->pol_desc->pd_ops->op_policy_ctl != NULL ?
86 policy->pol_desc->pd_ops->op_policy_ctl(policy, opc, arg) :
90 static void nrs_policy_stop0(struct ptlrpc_nrs_policy *policy)
92 struct ptlrpc_nrs *nrs = policy->pol_nrs;
95 if (policy->pol_desc->pd_ops->op_policy_stop != NULL) {
96 spin_unlock(&nrs->nrs_lock);
98 policy->pol_desc->pd_ops->op_policy_stop(policy);
100 spin_lock(&nrs->nrs_lock);
103 LASSERT(list_empty(&policy->pol_list_queued));
104 LASSERT(policy->pol_req_queued == 0 &&
105 policy->pol_req_started == 0);
107 policy->pol_private = NULL;
109 policy->pol_state = NRS_POL_STATE_STOPPED;
111 if (atomic_dec_and_test(&policy->pol_desc->pd_refs))
112 module_put(policy->pol_desc->pd_owner);
117 static int nrs_policy_stop_locked(struct ptlrpc_nrs_policy *policy)
119 struct ptlrpc_nrs *nrs = policy->pol_nrs;
122 if (nrs->nrs_policy_fallback == policy && !nrs->nrs_stopping)
125 if (policy->pol_state == NRS_POL_STATE_STARTING)
128 /* In progress or already stopped */
129 if (policy->pol_state != NRS_POL_STATE_STARTED)
132 policy->pol_state = NRS_POL_STATE_STOPPING;
134 /* Immediately make it invisible */
135 if (nrs->nrs_policy_primary == policy) {
136 nrs->nrs_policy_primary = NULL;
139 LASSERT(nrs->nrs_policy_fallback == policy);
140 nrs->nrs_policy_fallback = NULL;
143 /* I have the only refcount */
144 if (policy->pol_ref == 1)
145 nrs_policy_stop0(policy);
151 * Transitions the \a nrs NRS head's primary policy to
152 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING and if the policy has no
153 * pending usage references, to ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED.
155 * \param[in] nrs the NRS head to carry out this operation on
157 static void nrs_policy_stop_primary(struct ptlrpc_nrs *nrs)
159 struct ptlrpc_nrs_policy *tmp = nrs->nrs_policy_primary;
164 * XXX: This should really be RETURN_EXIT, but the latter does
165 * not currently print anything out, and possibly should be
172 nrs->nrs_policy_primary = NULL;
174 LASSERT(tmp->pol_state == NRS_POL_STATE_STARTED);
175 tmp->pol_state = NRS_POL_STATE_STOPPING;
177 if (tmp->pol_ref == 0)
178 nrs_policy_stop0(tmp);
183 * Transitions a policy across the ptlrpc_nrs_pol_state range of values, in
184 * response to an lprocfs command to start a policy.
186 * If a primary policy different to the current one is specified, this function
187 * will transition the new policy to the
188 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTING and then to
189 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED, and will then transition
190 * the old primary policy (if there is one) to
191 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING, and if there are no outstanding
192 * references on the policy to ptlrpc_nrs_pol_stae::NRS_POL_STATE_STOPPED.
194 * If the fallback policy is specified, this is taken to indicate an instruction
195 * to stop the current primary policy, without substituting it with another
196 * primary policy, so the primary policy (if any) is transitioned to
197 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING, and if there are no outstanding
198 * references on the policy to ptlrpc_nrs_pol_stae::NRS_POL_STATE_STOPPED. In
199 * this case, the fallback policy is only left active in the NRS head.
201 static int nrs_policy_start_locked(struct ptlrpc_nrs_policy *policy, char *arg)
203 struct ptlrpc_nrs *nrs = policy->pol_nrs;
208 * Don't allow multiple starting which is too complex, and has no real
211 if (nrs->nrs_policy_starting)
214 LASSERT(policy->pol_state != NRS_POL_STATE_STARTING);
216 if (policy->pol_state == NRS_POL_STATE_STOPPING)
219 if (policy->pol_flags & PTLRPC_NRS_FL_FALLBACK) {
221 * This is for cases in which the user sets the policy to the
222 * fallback policy (currently fifo for all services); i.e. the
223 * user is resetting the policy to the default; so we stop the
224 * primary policy, if any.
226 if (policy == nrs->nrs_policy_fallback) {
227 nrs_policy_stop_primary(nrs);
232 * If we reach here, we must be setting up the fallback policy
233 * at service startup time, and only a single policy with the
234 * nrs_policy_flags::PTLRPC_NRS_FL_FALLBACK flag set can
235 * register with NRS core.
237 LASSERT(nrs->nrs_policy_fallback == NULL);
240 * Shouldn't start primary policy if w/o fallback policy.
242 if (nrs->nrs_policy_fallback == NULL)
245 if (policy->pol_state == NRS_POL_STATE_STARTED)
250 * Increase the module usage count for policies registering from other
253 if (atomic_inc_return(&policy->pol_desc->pd_refs) == 1 &&
254 !try_module_get(policy->pol_desc->pd_owner)) {
255 atomic_dec(&policy->pol_desc->pd_refs);
256 CERROR("NRS: cannot get module for policy %s; is it alive?\n",
257 policy->pol_desc->pd_name);
262 * Serialize policy starting across the NRS head
264 nrs->nrs_policy_starting = 1;
266 policy->pol_state = NRS_POL_STATE_STARTING;
268 if (policy->pol_desc->pd_ops->op_policy_start) {
269 spin_unlock(&nrs->nrs_lock);
271 rc = policy->pol_desc->pd_ops->op_policy_start(policy, arg);
273 spin_lock(&nrs->nrs_lock);
275 if (atomic_dec_and_test(&policy->pol_desc->pd_refs))
276 module_put(policy->pol_desc->pd_owner);
278 policy->pol_state = NRS_POL_STATE_STOPPED;
283 policy->pol_state = NRS_POL_STATE_STARTED;
285 if (policy->pol_flags & PTLRPC_NRS_FL_FALLBACK) {
287 * This path is only used at PTLRPC service setup time.
289 nrs->nrs_policy_fallback = policy;
292 * Try to stop the current primary policy if there is one.
294 nrs_policy_stop_primary(nrs);
297 * And set the newly-started policy as the primary one.
299 nrs->nrs_policy_primary = policy;
303 nrs->nrs_policy_starting = 0;
309 * Increases the policy's usage reference count.
311 static inline void nrs_policy_get_locked(struct ptlrpc_nrs_policy *policy)
317 * Decreases the policy's usage reference count, and stops the policy in case it
318 * was already stopping and have no more outstanding usage references (which
319 * indicates it has no more queued or started requests, and can be safely
322 static void nrs_policy_put_locked(struct ptlrpc_nrs_policy *policy)
324 LASSERT(policy->pol_ref > 0);
327 if (unlikely(policy->pol_ref == 0 &&
328 policy->pol_state == NRS_POL_STATE_STOPPING))
329 nrs_policy_stop0(policy);
332 static void nrs_policy_put(struct ptlrpc_nrs_policy *policy)
334 spin_lock(&policy->pol_nrs->nrs_lock);
335 nrs_policy_put_locked(policy);
336 spin_unlock(&policy->pol_nrs->nrs_lock);
340 * Find and return a policy by name.
342 static struct ptlrpc_nrs_policy * nrs_policy_find_locked(struct ptlrpc_nrs *nrs,
345 struct ptlrpc_nrs_policy *tmp;
347 list_for_each_entry(tmp, &nrs->nrs_policy_list, pol_list) {
348 if (strncmp(tmp->pol_desc->pd_name, name,
349 NRS_POL_NAME_MAX) == 0) {
350 nrs_policy_get_locked(tmp);
358 * Release references for the resource hierarchy moving upwards towards the
359 * policy instance resource.
361 static void nrs_resource_put(struct ptlrpc_nrs_resource *res)
363 struct ptlrpc_nrs_policy *policy = res->res_policy;
365 if (policy->pol_desc->pd_ops->op_res_put != NULL) {
366 struct ptlrpc_nrs_resource *parent;
368 for (; res != NULL; res = parent) {
369 parent = res->res_parent;
370 policy->pol_desc->pd_ops->op_res_put(policy, res);
376 * Obtains references for each resource in the resource hierarchy for request
377 * \a nrq if it is to be handled by \a policy.
379 * \param[in] policy the policy
380 * \param[in] nrq the request
381 * \param[in] moving_req denotes whether this is a call to the function by
382 * ldlm_lock_reorder_req(), in order to move \a nrq to
383 * the high-priority NRS head; we should not sleep when
386 * \retval NULL resource hierarchy references not obtained
387 * \retval valid-pointer the bottom level of the resource hierarchy
389 * \see ptlrpc_nrs_pol_ops::op_res_get()
392 struct ptlrpc_nrs_resource * nrs_resource_get(struct ptlrpc_nrs_policy *policy,
393 struct ptlrpc_nrs_request *nrq,
397 * Set to NULL to traverse the resource hierarchy from the top.
399 struct ptlrpc_nrs_resource *res = NULL;
400 struct ptlrpc_nrs_resource *tmp = NULL;
404 rc = policy->pol_desc->pd_ops->op_res_get(policy, nrq, res,
408 nrs_resource_put(res);
412 LASSERT(tmp != NULL);
413 tmp->res_parent = res;
414 tmp->res_policy = policy;
418 * Return once we have obtained a reference to the bottom level
419 * of the resource hierarchy.
427 * Obtains resources for the resource hierarchies and policy references for
428 * the fallback and current primary policy (if any), that will later be used
429 * to handle request \a nrq.
431 * \param[in] nrs the NRS head instance that will be handling request \a nrq.
432 * \param[in] nrq the request that is being handled.
433 * \param[out] resp the array where references to the resource hierarchy are
435 * \param[in] moving_req is set when obtaining resources while moving a
436 * request from a policy on the regular NRS head to a
437 * policy on the HP NRS head (via
438 * ldlm_lock_reorder_req()). It signifies that
439 * allocations to get resources should be atomic; for
440 * a full explanation, see comment in
441 * ptlrpc_nrs_pol_ops::op_res_get().
443 static void nrs_resource_get_safe(struct ptlrpc_nrs *nrs,
444 struct ptlrpc_nrs_request *nrq,
445 struct ptlrpc_nrs_resource **resp,
448 struct ptlrpc_nrs_policy *primary = NULL;
449 struct ptlrpc_nrs_policy *fallback = NULL;
451 memset(resp, 0, sizeof(resp[0]) * NRS_RES_MAX);
454 * Obtain policy references.
456 spin_lock(&nrs->nrs_lock);
458 fallback = nrs->nrs_policy_fallback;
459 nrs_policy_get_locked(fallback);
461 primary = nrs->nrs_policy_primary;
463 nrs_policy_get_locked(primary);
465 spin_unlock(&nrs->nrs_lock);
468 * Obtain resource hierarchy references.
470 resp[NRS_RES_FALLBACK] = nrs_resource_get(fallback, nrq, moving_req);
471 LASSERT(resp[NRS_RES_FALLBACK] != NULL);
473 if (primary != NULL) {
474 resp[NRS_RES_PRIMARY] = nrs_resource_get(primary, nrq,
477 * A primary policy may exist which may not wish to serve a
478 * particular request for different reasons; release the
479 * reference on the policy as it will not be used for this
482 if (resp[NRS_RES_PRIMARY] == NULL)
483 nrs_policy_put(primary);
488 * Releases references to resource hierarchies and policies, because they are no
489 * longer required; used when request handling has been completed, or the
490 * request is moving to the high priority NRS head.
492 * \param resp the resource hierarchy that is being released
494 * \see ptlrpcnrs_req_hp_move()
495 * \see ptlrpc_nrs_req_finalize()
497 static void nrs_resource_put_safe(struct ptlrpc_nrs_resource **resp)
499 struct ptlrpc_nrs_policy *pols[NRS_RES_MAX];
500 struct ptlrpc_nrs *nrs = NULL;
503 for (i = 0; i < NRS_RES_MAX; i++) {
504 if (resp[i] != NULL) {
505 pols[i] = resp[i]->res_policy;
506 nrs_resource_put(resp[i]);
513 for (i = 0; i < NRS_RES_MAX; i++) {
518 nrs = pols[i]->pol_nrs;
519 spin_lock(&nrs->nrs_lock);
521 nrs_policy_put_locked(pols[i]);
525 spin_unlock(&nrs->nrs_lock);
529 * Obtains an NRS request from \a policy for handling or examination; the
530 * request should be removed in the 'handling' case.
532 * Calling into this function implies we already know the policy has a request
533 * waiting to be handled.
535 * \param[in] policy the policy from which a request
536 * \param[in] peek when set, signifies that we just want to examine the
537 * request, and not handle it, so the request is not removed
539 * \param[in] force when set, it will force a policy to return a request if it
542 * \retval the NRS request to be handled
545 struct ptlrpc_nrs_request * nrs_request_get(struct ptlrpc_nrs_policy *policy,
546 bool peek, bool force)
548 struct ptlrpc_nrs_request *nrq;
550 LASSERT(policy->pol_req_queued > 0);
552 nrq = policy->pol_desc->pd_ops->op_req_get(policy, peek, force);
554 LASSERT(ergo(nrq != NULL, nrs_request_policy(nrq) == policy));
560 * Enqueues request \a nrq for later handling, via one one the policies for
561 * which resources where earlier obtained via nrs_resource_get_safe(). The
562 * function attempts to enqueue the request first on the primary policy
563 * (if any), since this is the preferred choice.
565 * \param nrq the request being enqueued
567 * \see nrs_resource_get_safe()
569 static inline void nrs_request_enqueue(struct ptlrpc_nrs_request *nrq)
571 struct ptlrpc_nrs_policy *policy;
576 * Try in descending order, because the primary policy (if any) is
577 * the preferred choice.
579 for (i = NRS_RES_MAX - 1; i >= 0; i--) {
580 if (nrq->nr_res_ptrs[i] == NULL)
584 policy = nrq->nr_res_ptrs[i]->res_policy;
586 rc = policy->pol_desc->pd_ops->op_req_enqueue(policy, nrq);
588 policy->pol_nrs->nrs_req_queued++;
589 policy->pol_req_queued++;
594 * Should never get here, as at least the primary policy's
595 * ptlrpc_nrs_pol_ops::op_req_enqueue() implementation should always
602 * Called when a request has been handled
604 * \param[in] nrs the request that has been handled; can be used for
605 * job/resource control.
607 * \see ptlrpc_nrs_req_stop_nolock()
609 static inline void nrs_request_stop(struct ptlrpc_nrs_request *nrq)
611 struct ptlrpc_nrs_policy *policy = nrs_request_policy(nrq);
613 if (policy->pol_desc->pd_ops->op_req_stop)
614 policy->pol_desc->pd_ops->op_req_stop(policy, nrq);
616 LASSERT(policy->pol_nrs->nrs_req_started > 0);
617 LASSERT(policy->pol_req_started > 0);
619 policy->pol_nrs->nrs_req_started--;
620 policy->pol_req_started--;
624 * Handler for operations that can be carried out on policies.
626 * Handles opcodes that are common to all policy types within NRS core, and
627 * passes any unknown opcodes to the policy-specific control function.
629 * \param[in] nrs the NRS head this policy belongs to.
630 * \param[in] name the human-readable policy name; should be the same as
631 * ptlrpc_nrs_pol_desc::pd_name.
632 * \param[in] opc the opcode of the operation being carried out.
633 * \param[in,out] arg can be used to pass information in and out between when
634 * carrying an operation; usually data that is private to
635 * the policy at some level, or generic policy status
638 * \retval -ve error condition
639 * \retval 0 operation was carried out successfully
641 static int nrs_policy_ctl(struct ptlrpc_nrs *nrs, char *name,
642 enum ptlrpc_nrs_ctl opc, void *arg)
644 struct ptlrpc_nrs_policy *policy;
648 spin_lock(&nrs->nrs_lock);
650 policy = nrs_policy_find_locked(nrs, name);
652 GOTO(out, rc = -ENOENT);
656 * Unknown opcode, pass it down to the policy-specific control
657 * function for handling.
660 rc = nrs_policy_ctl_locked(policy, opc, arg);
666 case PTLRPC_NRS_CTL_START:
667 rc = nrs_policy_start_locked(policy, arg);
672 nrs_policy_put_locked(policy);
674 spin_unlock(&nrs->nrs_lock);
680 * Unregisters a policy by name.
682 * \param[in] nrs the NRS head this policy belongs to.
683 * \param[in] name the human-readable policy name; should be the same as
684 * ptlrpc_nrs_pol_desc::pd_name
689 static int nrs_policy_unregister(struct ptlrpc_nrs *nrs, char *name)
691 struct ptlrpc_nrs_policy *policy = NULL;
694 spin_lock(&nrs->nrs_lock);
696 policy = nrs_policy_find_locked(nrs, name);
697 if (policy == NULL) {
698 spin_unlock(&nrs->nrs_lock);
700 CERROR("Can't find NRS policy %s\n", name);
704 if (policy->pol_ref > 1) {
705 CERROR("Policy %s is busy with %d references\n", name,
706 (int)policy->pol_ref);
707 nrs_policy_put_locked(policy);
709 spin_unlock(&nrs->nrs_lock);
713 LASSERT(policy->pol_req_queued == 0);
714 LASSERT(policy->pol_req_started == 0);
716 if (policy->pol_state != NRS_POL_STATE_STOPPED) {
717 nrs_policy_stop_locked(policy);
718 LASSERT(policy->pol_state == NRS_POL_STATE_STOPPED);
721 list_del(&policy->pol_list);
724 nrs_policy_put_locked(policy);
726 spin_unlock(&nrs->nrs_lock);
728 nrs_policy_fini(policy);
730 LASSERT(policy->pol_private == NULL);
731 OBD_FREE_PTR(policy);
737 * Register a policy from \policy descriptor \a desc with NRS head \a nrs.
739 * \param[in] nrs the NRS head on which the policy will be registered.
740 * \param[in] desc the policy descriptor from which the information will be
741 * obtained to register the policy.
746 static int nrs_policy_register(struct ptlrpc_nrs *nrs,
747 struct ptlrpc_nrs_pol_desc *desc)
749 struct ptlrpc_nrs_policy *policy;
750 struct ptlrpc_nrs_policy *tmp;
751 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
755 LASSERT(svcpt != NULL);
756 LASSERT(desc->pd_ops != NULL);
757 LASSERT(desc->pd_ops->op_res_get != NULL);
758 LASSERT(desc->pd_ops->op_req_get != NULL);
759 LASSERT(desc->pd_ops->op_req_enqueue != NULL);
760 LASSERT(desc->pd_ops->op_req_dequeue != NULL);
761 LASSERT(desc->pd_compat != NULL);
763 OBD_CPT_ALLOC_GFP(policy, svcpt->scp_service->srv_cptable,
764 svcpt->scp_cpt, sizeof(*policy), GFP_NOFS);
768 policy->pol_nrs = nrs;
769 policy->pol_desc = desc;
770 policy->pol_state = NRS_POL_STATE_STOPPED;
771 policy->pol_flags = desc->pd_flags;
773 INIT_LIST_HEAD(&policy->pol_list);
774 INIT_LIST_HEAD(&policy->pol_list_queued);
776 rc = nrs_policy_init(policy);
778 OBD_FREE_PTR(policy);
782 spin_lock(&nrs->nrs_lock);
784 tmp = nrs_policy_find_locked(nrs, policy->pol_desc->pd_name);
786 CERROR("NRS policy %s has been registered, can't register it "
787 "for %s\n", policy->pol_desc->pd_name,
788 svcpt->scp_service->srv_name);
789 nrs_policy_put_locked(tmp);
791 spin_unlock(&nrs->nrs_lock);
792 nrs_policy_fini(policy);
793 OBD_FREE_PTR(policy);
798 list_add_tail(&policy->pol_list, &nrs->nrs_policy_list);
801 if (policy->pol_flags & PTLRPC_NRS_FL_REG_START)
802 rc = nrs_policy_start_locked(policy, NULL);
804 spin_unlock(&nrs->nrs_lock);
807 (void) nrs_policy_unregister(nrs, policy->pol_desc->pd_name);
813 * Enqueue request \a req using one of the policies its resources are referring
816 * \param[in] req the request to enqueue.
818 static void ptlrpc_nrs_req_add_nolock(struct ptlrpc_request *req)
820 struct ptlrpc_nrs_policy *policy;
822 LASSERT(req->rq_nrq.nr_initialized);
823 LASSERT(!req->rq_nrq.nr_enqueued);
825 nrs_request_enqueue(&req->rq_nrq);
826 req->rq_nrq.nr_enqueued = 1;
828 policy = nrs_request_policy(&req->rq_nrq);
830 * Add the policy to the NRS head's list of policies with enqueued
831 * requests, if it has not been added there.
833 if (unlikely(list_empty(&policy->pol_list_queued)))
834 list_add_tail(&policy->pol_list_queued,
835 &policy->pol_nrs->nrs_policy_queued);
839 * Enqueue a request on the high priority NRS head.
841 * \param req the request to enqueue.
843 static void ptlrpc_nrs_hpreq_add_nolock(struct ptlrpc_request *req)
845 int opc = lustre_msg_get_opc(req->rq_reqmsg);
848 spin_lock(&req->rq_lock);
850 ptlrpc_nrs_req_add_nolock(req);
852 DEBUG_REQ(D_NET, req, "high priority req");
853 spin_unlock(&req->rq_lock);
858 * Returns a boolean predicate indicating whether the policy described by
859 * \a desc is adequate for use with service \a svc.
861 * \param[in] svc the service
862 * \param[in] desc the policy descriptor
864 * \retval false the policy is not compatible with the service
865 * \retval true the policy is compatible with the service
867 static inline bool nrs_policy_compatible(const struct ptlrpc_service *svc,
868 const struct ptlrpc_nrs_pol_desc *desc)
870 return desc->pd_compat(svc, desc);
874 * Registers all compatible policies in nrs_core.nrs_policies, for NRS head
877 * \param[in] nrs the NRS head
882 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
884 * \see ptlrpc_service_nrs_setup()
886 static int nrs_register_policies_locked(struct ptlrpc_nrs *nrs)
888 struct ptlrpc_nrs_pol_desc *desc;
889 /* for convenience */
890 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
891 struct ptlrpc_service *svc = svcpt->scp_service;
895 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
897 list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
898 if (nrs_policy_compatible(svc, desc)) {
899 rc = nrs_policy_register(nrs, desc);
901 CERROR("Failed to register NRS policy %s for "
902 "partition %d of service %s: %d\n",
903 desc->pd_name, svcpt->scp_cpt,
906 * Fail registration if any of the policies'
907 * registration fails.
918 * Initializes NRS head \a nrs of service partition \a svcpt, and registers all
919 * compatible policies in NRS core, with the NRS head.
921 * \param[in] nrs the NRS head
922 * \param[in] svcpt the PTLRPC service partition to setup
927 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
929 static int nrs_svcpt_setup_locked0(struct ptlrpc_nrs *nrs,
930 struct ptlrpc_service_part *svcpt)
933 enum ptlrpc_nrs_queue_type queue;
935 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
937 if (nrs == &svcpt->scp_nrs_reg)
938 queue = PTLRPC_NRS_QUEUE_REG;
939 else if (nrs == svcpt->scp_nrs_hp)
940 queue = PTLRPC_NRS_QUEUE_HP;
944 nrs->nrs_svcpt = svcpt;
945 nrs->nrs_queue_type = queue;
946 spin_lock_init(&nrs->nrs_lock);
947 INIT_LIST_HEAD(&nrs->nrs_policy_list);
948 INIT_LIST_HEAD(&nrs->nrs_policy_queued);
949 nrs->nrs_throttling = 0;
951 rc = nrs_register_policies_locked(nrs);
957 * Allocates a regular and optionally a high-priority NRS head (if the service
958 * handles high-priority RPCs), and then registers all available compatible
959 * policies on those NRS heads.
961 * \param[in,out] svcpt the PTLRPC service partition to setup
963 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
965 static int nrs_svcpt_setup_locked(struct ptlrpc_service_part *svcpt)
967 struct ptlrpc_nrs *nrs;
971 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
974 * Initialize the regular NRS head.
976 nrs = nrs_svcpt2nrs(svcpt, false);
977 rc = nrs_svcpt_setup_locked0(nrs, svcpt);
982 * Optionally allocate a high-priority NRS head.
984 if (svcpt->scp_service->srv_ops.so_hpreq_handler == NULL)
987 OBD_CPT_ALLOC_PTR(svcpt->scp_nrs_hp,
988 svcpt->scp_service->srv_cptable,
990 if (svcpt->scp_nrs_hp == NULL)
991 GOTO(out, rc = -ENOMEM);
993 nrs = nrs_svcpt2nrs(svcpt, true);
994 rc = nrs_svcpt_setup_locked0(nrs, svcpt);
1001 * Unregisters all policies on all available NRS heads in a service partition;
1002 * called at PTLRPC service unregistration time.
1004 * \param[in] svcpt the PTLRPC service partition
1006 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
1008 static void nrs_svcpt_cleanup_locked(struct ptlrpc_service_part *svcpt)
1010 struct ptlrpc_nrs *nrs;
1011 struct ptlrpc_nrs_policy *policy;
1012 struct ptlrpc_nrs_policy *tmp;
1017 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
1020 /* scp_nrs_hp could be NULL due to short of memory. */
1021 nrs = hp ? svcpt->scp_nrs_hp : &svcpt->scp_nrs_reg;
1022 /* check the nrs_svcpt to see if nrs is initialized. */
1023 if (!nrs || !nrs->nrs_svcpt) {
1027 nrs->nrs_stopping = 1;
1029 list_for_each_entry_safe(policy, tmp, &nrs->nrs_policy_list,
1031 rc = nrs_policy_unregister(nrs, policy->pol_desc->pd_name);
1036 * If the service partition has an HP NRS head, clean that up as well.
1038 if (!hp && nrs_svcpt_has_hp(svcpt)) {
1050 * Returns the descriptor for a policy as identified by by \a name.
1052 * \param[in] name the policy name
1054 * \retval the policy descriptor
1057 static struct ptlrpc_nrs_pol_desc *nrs_policy_find_desc_locked(const char *name)
1059 struct ptlrpc_nrs_pol_desc *tmp;
1062 list_for_each_entry(tmp, &nrs_core.nrs_policies, pd_list) {
1063 if (strncmp(tmp->pd_name, name, NRS_POL_NAME_MAX) == 0)
1070 * Removes the policy from all supported NRS heads of all partitions of all
1073 * \param[in] desc the policy descriptor to unregister
1076 * \retval 0 successfully unregistered policy on all supported NRS heads
1078 * \pre mutex_is_locked(&nrs_core.nrs_mutex)
1079 * \pre mutex_is_locked(&ptlrpc_all_services_mutex)
1081 static int nrs_policy_unregister_locked(struct ptlrpc_nrs_pol_desc *desc)
1083 struct ptlrpc_nrs *nrs;
1084 struct ptlrpc_service *svc;
1085 struct ptlrpc_service_part *svcpt;
1090 LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
1091 LASSERT(mutex_is_locked(&ptlrpc_all_services_mutex));
1093 list_for_each_entry(svc, &ptlrpc_all_services, srv_list) {
1095 if (!nrs_policy_compatible(svc, desc) ||
1096 unlikely(svc->srv_is_stopping))
1099 ptlrpc_service_for_each_part(svcpt, i, svc) {
1103 nrs = nrs_svcpt2nrs(svcpt, hp);
1104 rc = nrs_policy_unregister(nrs, desc->pd_name);
1106 * Ignore -ENOENT as the policy may not have registered
1107 * successfully on all service partitions.
1109 if (rc == -ENOENT) {
1111 } else if (rc != 0) {
1112 CERROR("Failed to unregister NRS policy %s for "
1113 "partition %d of service %s: %d\n",
1114 desc->pd_name, svcpt->scp_cpt,
1115 svcpt->scp_service->srv_name, rc);
1119 if (!hp && nrs_svc_has_hp(svc)) {
1125 if (desc->pd_ops->op_lprocfs_fini != NULL)
1126 desc->pd_ops->op_lprocfs_fini(svc);
1133 * Registers a new policy with NRS core.
1135 * The function will only succeed if policy registration with all compatible
1136 * service partitions (if any) is successful.
1138 * N.B. This function should be called either at ptlrpc module initialization
1139 * time when registering a policy that ships with NRS core, or in a
1140 * module's init() function for policies registering from other modules.
1142 * \param[in] conf configuration information for the new policy to register
1147 int ptlrpc_nrs_policy_register(struct ptlrpc_nrs_pol_conf *conf)
1149 struct ptlrpc_service *svc;
1150 struct ptlrpc_nrs_pol_desc *desc;
1154 LASSERT(conf != NULL);
1155 LASSERT(conf->nc_ops != NULL);
1156 LASSERT(conf->nc_compat != NULL);
1157 LASSERT(ergo(conf->nc_compat == nrs_policy_compat_one,
1158 conf->nc_compat_svc_name != NULL));
1159 LASSERT(ergo((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) != 0,
1160 conf->nc_owner != NULL));
1162 conf->nc_name[NRS_POL_NAME_MAX - 1] = '\0';
1165 * External policies are not allowed to start immediately upon
1166 * registration, as there is a relatively higher chance that their
1167 * registration might fail. In such a case, some policy instances may
1168 * already have requests queued wen unregistration needs to happen as
1169 * part o cleanup; since there is currently no way to drain requests
1170 * from a policy unless the service is unregistering, we just disallow
1173 if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) &&
1174 (conf->nc_flags & (PTLRPC_NRS_FL_FALLBACK |
1175 PTLRPC_NRS_FL_REG_START))) {
1176 CERROR("NRS: failing to register policy %s. Please check "
1177 "policy flags; external policies cannot act as fallback "
1178 "policies, or be started immediately upon registration "
1179 "without interaction with lprocfs\n", conf->nc_name);
1183 mutex_lock(&nrs_core.nrs_mutex);
1185 if (nrs_policy_find_desc_locked(conf->nc_name) != NULL) {
1186 CERROR("NRS: failing to register policy %s which has already "
1187 "been registered with NRS core!\n",
1189 GOTO(fail, rc = -EEXIST);
1192 OBD_ALLOC_PTR(desc);
1194 GOTO(fail, rc = -ENOMEM);
1196 if (strlcpy(desc->pd_name, conf->nc_name, sizeof(desc->pd_name)) >=
1197 sizeof(desc->pd_name)) {
1199 GOTO(fail, rc = -E2BIG);
1201 desc->pd_ops = conf->nc_ops;
1202 desc->pd_compat = conf->nc_compat;
1203 desc->pd_compat_svc_name = conf->nc_compat_svc_name;
1204 if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) != 0)
1205 desc->pd_owner = conf->nc_owner;
1206 desc->pd_flags = conf->nc_flags;
1207 atomic_set(&desc->pd_refs, 0);
1210 * For policies that are held in the same module as NRS (currently
1211 * ptlrpc), do not register the policy with all compatible services,
1212 * as the services will not have started at this point, since we are
1213 * calling from ptlrpc module initialization code. In such cases each
1214 * service will register all compatible policies later, via
1215 * ptlrpc_service_nrs_setup().
1217 if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) == 0)
1221 * Register the new policy on all compatible services
1223 mutex_lock(&ptlrpc_all_services_mutex);
1225 list_for_each_entry(svc, &ptlrpc_all_services, srv_list) {
1226 struct ptlrpc_service_part *svcpt;
1230 if (!nrs_policy_compatible(svc, desc) ||
1231 unlikely(svc->srv_is_stopping))
1234 ptlrpc_service_for_each_part(svcpt, i, svc) {
1235 struct ptlrpc_nrs *nrs;
1238 nrs = nrs_svcpt2nrs(svcpt, hp);
1239 rc = nrs_policy_register(nrs, desc);
1241 CERROR("Failed to register NRS policy %s for "
1242 "partition %d of service %s: %d\n",
1243 desc->pd_name, svcpt->scp_cpt,
1244 svcpt->scp_service->srv_name, rc);
1246 rc2 = nrs_policy_unregister_locked(desc);
1248 * Should not fail at this point
1251 mutex_unlock(&ptlrpc_all_services_mutex);
1256 if (!hp && nrs_svc_has_hp(svc)) {
1263 * No need to take a reference to other modules here, as we
1264 * will be calling from the module's init() function.
1266 if (desc->pd_ops->op_lprocfs_init != NULL) {
1267 rc = desc->pd_ops->op_lprocfs_init(svc);
1269 rc2 = nrs_policy_unregister_locked(desc);
1271 * Should not fail at this point
1274 mutex_unlock(&ptlrpc_all_services_mutex);
1281 mutex_unlock(&ptlrpc_all_services_mutex);
1283 list_add_tail(&desc->pd_list, &nrs_core.nrs_policies);
1285 mutex_unlock(&nrs_core.nrs_mutex);
1289 EXPORT_SYMBOL(ptlrpc_nrs_policy_register);
1292 * Unregisters a previously registered policy with NRS core. All instances of
1293 * the policy on all NRS heads of all supported services are removed.
1295 * N.B. This function should only be called from a module's exit() function.
1296 * Although it can be used for policies that ship alongside NRS core, the
1297 * function is primarily intended for policies that register externally,
1298 * from other modules.
1300 * \param[in] conf configuration information for the policy to unregister
1305 int ptlrpc_nrs_policy_unregister(struct ptlrpc_nrs_pol_conf *conf)
1307 struct ptlrpc_nrs_pol_desc *desc;
1311 LASSERT(conf != NULL);
1313 if (conf->nc_flags & PTLRPC_NRS_FL_FALLBACK) {
1314 CERROR("Unable to unregister a fallback policy, unless the "
1315 "PTLRPC service is stopping.\n");
1319 conf->nc_name[NRS_POL_NAME_MAX - 1] = '\0';
1321 mutex_lock(&nrs_core.nrs_mutex);
1323 desc = nrs_policy_find_desc_locked(conf->nc_name);
1325 CERROR("Failing to unregister NRS policy %s which has "
1326 "not been registered with NRS core!\n",
1328 GOTO(not_exist, rc = -ENOENT);
1331 mutex_lock(&ptlrpc_all_services_mutex);
1333 rc = nrs_policy_unregister_locked(desc);
1336 CERROR("Please first stop policy %s on all service "
1337 "partitions and then retry to unregister the "
1338 "policy.\n", conf->nc_name);
1342 CDEBUG(D_INFO, "Unregistering policy %s from NRS core.\n",
1345 list_del(&desc->pd_list);
1349 mutex_unlock(&ptlrpc_all_services_mutex);
1352 mutex_unlock(&nrs_core.nrs_mutex);
1356 EXPORT_SYMBOL(ptlrpc_nrs_policy_unregister);
1359 * Setup NRS heads on all service partitions of service \a svc, and register
1360 * all compatible policies on those NRS heads.
1362 * To be called from withing ptl
1363 * \param[in] svc the service to setup
1365 * \retval -ve error, the calling logic should eventually call
1366 * ptlrpc_service_nrs_cleanup() to undo any work performed
1369 * \see ptlrpc_register_service()
1370 * \see ptlrpc_service_nrs_cleanup()
1372 int ptlrpc_service_nrs_setup(struct ptlrpc_service *svc)
1374 struct ptlrpc_service_part *svcpt;
1375 const struct ptlrpc_nrs_pol_desc *desc;
1379 mutex_lock(&nrs_core.nrs_mutex);
1382 * Initialize NRS heads on all service CPTs.
1384 ptlrpc_service_for_each_part(svcpt, i, svc) {
1385 rc = nrs_svcpt_setup_locked(svcpt);
1391 * Set up lprocfs interfaces for all supported policies for the
1394 list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
1395 if (!nrs_policy_compatible(svc, desc))
1398 if (desc->pd_ops->op_lprocfs_init != NULL) {
1399 rc = desc->pd_ops->op_lprocfs_init(svc);
1407 mutex_unlock(&nrs_core.nrs_mutex);
1413 * Unregisters all policies on all service partitions of service \a svc.
1415 * \param[in] svc the PTLRPC service to unregister
1417 void ptlrpc_service_nrs_cleanup(struct ptlrpc_service *svc)
1419 struct ptlrpc_service_part *svcpt;
1420 const struct ptlrpc_nrs_pol_desc *desc;
1423 mutex_lock(&nrs_core.nrs_mutex);
1426 * Clean up NRS heads on all service partitions
1428 ptlrpc_service_for_each_part(svcpt, i, svc)
1429 nrs_svcpt_cleanup_locked(svcpt);
1432 * Clean up lprocfs interfaces for all supported policies for the
1435 list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
1436 if (!nrs_policy_compatible(svc, desc))
1439 if (desc->pd_ops->op_lprocfs_fini != NULL)
1440 desc->pd_ops->op_lprocfs_fini(svc);
1443 mutex_unlock(&nrs_core.nrs_mutex);
1447 * Obtains NRS head resources for request \a req.
1449 * These could be either on the regular or HP NRS head of \a svcpt; resources
1450 * taken on the regular head can later be swapped for HP head resources by
1451 * ldlm_lock_reorder_req().
1453 * \param[in] svcpt the service partition
1454 * \param[in] req the request
1455 * \param[in] hp which NRS head of \a svcpt to use
1457 void ptlrpc_nrs_req_initialize(struct ptlrpc_service_part *svcpt,
1458 struct ptlrpc_request *req, bool hp)
1460 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1462 memset(&req->rq_nrq, 0, sizeof(req->rq_nrq));
1463 nrs_resource_get_safe(nrs, &req->rq_nrq, req->rq_nrq.nr_res_ptrs,
1467 * It is fine to access \e nr_initialized without locking as there is
1468 * no contention at this early stage.
1470 req->rq_nrq.nr_initialized = 1;
1474 * Releases resources for a request; is called after the request has been
1477 * \param[in] req the request
1479 * \see ptlrpc_server_finish_request()
1481 void ptlrpc_nrs_req_finalize(struct ptlrpc_request *req)
1483 if (req->rq_nrq.nr_initialized) {
1484 nrs_resource_put_safe(req->rq_nrq.nr_res_ptrs);
1485 /* no protection on bit nr_initialized because no
1486 * contention at this late stage */
1487 req->rq_nrq.nr_finalized = 1;
1491 void ptlrpc_nrs_req_stop_nolock(struct ptlrpc_request *req)
1493 if (req->rq_nrq.nr_started)
1494 nrs_request_stop(&req->rq_nrq);
1498 * Enqueues request \a req on either the regular or high-priority NRS head
1499 * of service partition \a svcpt.
1501 * \param[in] svcpt the service partition
1502 * \param[in] req the request to be enqueued
1503 * \param[in] hp whether to enqueue the request on the regular or
1504 * high-priority NRS head.
1506 void ptlrpc_nrs_req_add(struct ptlrpc_service_part *svcpt,
1507 struct ptlrpc_request *req, bool hp)
1509 spin_lock(&svcpt->scp_req_lock);
1512 ptlrpc_nrs_hpreq_add_nolock(req);
1514 ptlrpc_nrs_req_add_nolock(req);
1516 spin_unlock(&svcpt->scp_req_lock);
1519 static void nrs_request_removed(struct ptlrpc_nrs_policy *policy)
1521 LASSERT(policy->pol_nrs->nrs_req_queued > 0);
1522 LASSERT(policy->pol_req_queued > 0);
1524 policy->pol_nrs->nrs_req_queued--;
1525 policy->pol_req_queued--;
1528 * If the policy has no more requests queued, remove it from
1529 * ptlrpc_nrs::nrs_policy_queued.
1531 if (unlikely(policy->pol_req_queued == 0)) {
1532 list_del_init(&policy->pol_list_queued);
1535 * If there are other policies with queued requests, move the
1536 * current policy to the end so that we can round robin over
1537 * all policies and drain the requests.
1539 } else if (policy->pol_req_queued != policy->pol_nrs->nrs_req_queued) {
1540 LASSERT(policy->pol_req_queued <
1541 policy->pol_nrs->nrs_req_queued);
1543 list_move_tail(&policy->pol_list_queued,
1544 &policy->pol_nrs->nrs_policy_queued);
1549 * Obtains a request for handling from an NRS head of service partition
1552 * \param[in] svcpt the service partition
1553 * \param[in] hp whether to obtain a request from the regular or
1554 * high-priority NRS head.
1555 * \param[in] peek when set, signifies that we just want to examine the
1556 * request, and not handle it, so the request is not removed
1558 * \param[in] force when set, it will force a policy to return a request if it
1561 * \retval the request to be handled
1562 * \retval NULL the head has no requests to serve
1564 struct ptlrpc_request *
1565 ptlrpc_nrs_req_get_nolock0(struct ptlrpc_service_part *svcpt, bool hp,
1566 bool peek, bool force)
1568 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1569 struct ptlrpc_nrs_policy *policy;
1570 struct ptlrpc_nrs_request *nrq;
1573 * Always try to drain requests from all NRS polices even if they are
1574 * inactive, because the user can change policy status at runtime.
1576 list_for_each_entry(policy, &nrs->nrs_policy_queued,
1578 nrq = nrs_request_get(policy, peek, force);
1580 if (likely(!peek)) {
1581 nrq->nr_started = 1;
1583 policy->pol_req_started++;
1584 policy->pol_nrs->nrs_req_started++;
1586 nrs_request_removed(policy);
1589 return container_of(nrq, struct ptlrpc_request, rq_nrq);
1597 * Dequeues request \a req from the policy it has been enqueued on.
1599 * \param[in] req the request
1601 void ptlrpc_nrs_req_del_nolock(struct ptlrpc_request *req)
1603 struct ptlrpc_nrs_policy *policy = nrs_request_policy(&req->rq_nrq);
1605 policy->pol_desc->pd_ops->op_req_dequeue(policy, &req->rq_nrq);
1607 req->rq_nrq.nr_enqueued = 0;
1609 nrs_request_removed(policy);
1613 * Returns whether there are any requests currently enqueued on any of the
1614 * policies of service partition's \a svcpt NRS head specified by \a hp. Should
1615 * be called while holding ptlrpc_service_part::scp_req_lock to get a reliable
1618 * \param[in] svcpt the service partition to enquire.
1619 * \param[in] hp whether the regular or high-priority NRS head is to be
1622 * \retval false the indicated NRS head has no enqueued requests.
1623 * \retval true the indicated NRS head has some enqueued requests.
1625 bool ptlrpc_nrs_req_pending_nolock(struct ptlrpc_service_part *svcpt, bool hp)
1627 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1629 return nrs->nrs_req_queued > 0;
1633 * Returns whether NRS policy is throttling reqeust
1635 * \param[in] svcpt the service partition to enquire.
1636 * \param[in] hp whether the regular or high-priority NRS head is to be
1639 * \retval false the indicated NRS head has no enqueued requests.
1640 * \retval true the indicated NRS head has some enqueued requests.
1642 bool ptlrpc_nrs_req_throttling_nolock(struct ptlrpc_service_part *svcpt,
1645 struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1647 return !!nrs->nrs_throttling;
1651 * Moves request \a req from the regular to the high-priority NRS head.
1653 * \param[in] req the request to move
1655 void ptlrpc_nrs_req_hp_move(struct ptlrpc_request *req)
1657 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1658 struct ptlrpc_nrs_request *nrq = &req->rq_nrq;
1659 struct ptlrpc_nrs_resource *res1[NRS_RES_MAX];
1660 struct ptlrpc_nrs_resource *res2[NRS_RES_MAX];
1664 * Obtain the high-priority NRS head resources.
1666 nrs_resource_get_safe(nrs_svcpt2nrs(svcpt, true), nrq, res1, true);
1668 spin_lock(&svcpt->scp_req_lock);
1670 if (!ptlrpc_nrs_req_can_move(req))
1673 ptlrpc_nrs_req_del_nolock(req);
1675 memcpy(res2, nrq->nr_res_ptrs, NRS_RES_MAX * sizeof(res2[0]));
1676 memcpy(nrq->nr_res_ptrs, res1, NRS_RES_MAX * sizeof(res1[0]));
1678 ptlrpc_nrs_hpreq_add_nolock(req);
1680 memcpy(res1, res2, NRS_RES_MAX * sizeof(res1[0]));
1682 spin_unlock(&svcpt->scp_req_lock);
1685 * Release either the regular NRS head resources if we moved the
1686 * request, or the high-priority NRS head resources if we took a
1687 * reference earlier in this function and ptlrpc_nrs_req_can_move()
1690 nrs_resource_put_safe(res1);
1695 * Carries out a control operation \a opc on the policy identified by the
1696 * human-readable \a name, on either all partitions, or only on the first
1697 * partition of service \a svc.
1699 * \param[in] svc the service the policy belongs to.
1700 * \param[in] queue whether to carry out the command on the policy which
1701 * belongs to the regular, high-priority, or both NRS
1702 * heads of service partitions of \a svc.
1703 * \param[in] name the policy to act upon, by human-readable name
1704 * \param[in] opc the opcode of the operation to carry out
1705 * \param[in] single when set, the operation will only be carried out on the
1706 * NRS heads of the first service partition of \a svc.
1707 * This is useful for some policies which e.g. share
1708 * identical values on the same parameters of different
1709 * service partitions; when reading these parameters via
1710 * lprocfs, these policies may just want to obtain and
1711 * print out the values from the first service partition.
1712 * Storing these values centrally elsewhere then could be
1713 * another solution for this.
1714 * \param[in,out] arg can be used as a generic in/out buffer between control
1715 * operations and the user environment.
1717 *\retval -ve error condition
1718 *\retval 0 operation was carried out successfully
1720 int ptlrpc_nrs_policy_control(const struct ptlrpc_service *svc,
1721 enum ptlrpc_nrs_queue_type queue, char *name,
1722 enum ptlrpc_nrs_ctl opc, bool single, void *arg)
1724 struct ptlrpc_service_part *svcpt;
1729 LASSERT(opc != PTLRPC_NRS_CTL_INVALID);
1731 if ((queue & PTLRPC_NRS_QUEUE_BOTH) == 0)
1734 ptlrpc_service_for_each_part(svcpt, i, svc) {
1735 if ((queue & PTLRPC_NRS_QUEUE_REG) != 0) {
1736 rc = nrs_policy_ctl(nrs_svcpt2nrs(svcpt, false), name,
1738 if (rc != 0 || (queue == PTLRPC_NRS_QUEUE_REG &&
1743 if ((queue & PTLRPC_NRS_QUEUE_HP) != 0) {
1745 * XXX: We could optionally check for
1746 * nrs_svc_has_hp(svc) here, and return an error if it
1747 * is false. Right now we rely on the policies' lprocfs
1748 * handlers that call the present function to make this
1749 * check; if they fail to do so, they might hit the
1750 * assertion inside nrs_svcpt2nrs() below.
1752 rc = nrs_policy_ctl(nrs_svcpt2nrs(svcpt, true), name,
1754 if (rc != 0 || single)
1763 /* ptlrpc/nrs_fifo.c */
1764 extern struct ptlrpc_nrs_pol_conf nrs_conf_fifo;
1765 #if defined HAVE_SERVER_SUPPORT && defined(__KERNEL__)
1766 /* ptlrpc/nrs_crr.c */
1767 extern struct ptlrpc_nrs_pol_conf nrs_conf_crrn;
1768 /* ptlrpc/nrs_orr.c */
1769 extern struct ptlrpc_nrs_pol_conf nrs_conf_orr;
1770 extern struct ptlrpc_nrs_pol_conf nrs_conf_trr;
1771 extern struct ptlrpc_nrs_pol_conf nrs_conf_tbf;
1775 * Adds all policies that ship with the ptlrpc module, to NRS core's list of
1776 * policies \e nrs_core.nrs_policies.
1778 * \retval 0 all policies have been registered successfully
1781 int ptlrpc_nrs_init(void)
1786 mutex_init(&nrs_core.nrs_mutex);
1787 INIT_LIST_HEAD(&nrs_core.nrs_policies);
1789 rc = ptlrpc_nrs_policy_register(&nrs_conf_fifo);
1793 #if defined HAVE_SERVER_SUPPORT && defined(__KERNEL__)
1794 rc = ptlrpc_nrs_policy_register(&nrs_conf_crrn);
1798 rc = ptlrpc_nrs_policy_register(&nrs_conf_orr);
1802 rc = ptlrpc_nrs_policy_register(&nrs_conf_trr);
1805 rc = ptlrpc_nrs_policy_register(&nrs_conf_tbf);
1813 * Since no PTLRPC services have been started at this point, all we need
1814 * to do for cleanup is to free the descriptors.
1822 * Removes all policy desciptors from nrs_core::nrs_policies, and frees the
1823 * policy descriptors.
1825 * Since all PTLRPC services are stopped at this point, there are no more
1826 * instances of any policies, because each service will have stopped its policy
1827 * instances in ptlrpc_service_nrs_cleanup(), so we just need to free the
1830 void ptlrpc_nrs_fini(void)
1832 struct ptlrpc_nrs_pol_desc *desc;
1833 struct ptlrpc_nrs_pol_desc *tmp;
1835 list_for_each_entry_safe(desc, tmp, &nrs_core.nrs_policies,
1837 list_del_init(&desc->pd_list);