Whamcloud - gitweb
a2df45d6aac9415c4a097db665d6a46ec36fd683
[fs/lustre-release.git] / lustre / ptlrpc / nrs.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9
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.
15
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2011 Intel Corporation
24  *
25  * Copyright 2012 Xyratex Technology Limited
26  */
27 /*
28  * lustre/ptlrpc/nrs.c
29  *
30  * Network Request Scheduler (NRS)
31  *
32  * Allows to reorder the handling of RPCs at servers.
33  *
34  * Author: Liang Zhen <liang@whamcloud.com>
35  * Author: Nikitas Angelinas <nikitas_angelinas@xyratex.com>
36  */
37 /**
38  * \addtogoup nrs
39  * @{
40  */
41
42 #define DEBUG_SUBSYSTEM S_RPC
43 #ifndef __KERNEL__
44 #include <liblustre.h>
45 #endif
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"
52
53 /**
54  * NRS core object.
55  */
56 struct nrs_core nrs_core;
57
58 static int nrs_policy_init(struct ptlrpc_nrs_policy *policy)
59 {
60         return policy->pol_desc->pd_ops->op_policy_init != NULL ?
61                policy->pol_desc->pd_ops->op_policy_init(policy) : 0;
62 }
63
64 static void nrs_policy_fini(struct ptlrpc_nrs_policy *policy)
65 {
66         LASSERT(policy->pol_ref == 0);
67         LASSERT(policy->pol_req_queued == 0);
68
69         if (policy->pol_desc->pd_ops->op_policy_fini != NULL)
70                 policy->pol_desc->pd_ops->op_policy_fini(policy);
71 }
72
73 static int nrs_policy_ctl_locked(struct ptlrpc_nrs_policy *policy,
74                                  enum ptlrpc_nrs_ctl opc, void *arg)
75 {
76         /**
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.
81          */
82         if (policy->pol_state == NRS_POL_STATE_STOPPED)
83                 RETURN(-ENODEV);
84
85         RETURN(policy->pol_desc->pd_ops->op_policy_ctl != NULL ?
86                policy->pol_desc->pd_ops->op_policy_ctl(policy, opc, arg) :
87                -ENOSYS);
88 }
89
90 static void nrs_policy_stop0(struct ptlrpc_nrs_policy *policy)
91 {
92         struct ptlrpc_nrs *nrs = policy->pol_nrs;
93         ENTRY;
94
95         if (policy->pol_desc->pd_ops->op_policy_stop != NULL) {
96                 spin_unlock(&nrs->nrs_lock);
97
98                 policy->pol_desc->pd_ops->op_policy_stop(policy);
99
100                 spin_lock(&nrs->nrs_lock);
101         }
102
103         LASSERT(list_empty(&policy->pol_list_queued));
104         LASSERT(policy->pol_req_queued == 0 &&
105                 policy->pol_req_started == 0);
106
107         policy->pol_private = NULL;
108
109         policy->pol_state = NRS_POL_STATE_STOPPED;
110
111         if (atomic_dec_and_test(&policy->pol_desc->pd_refs))
112                 module_put(policy->pol_desc->pd_owner);
113
114         EXIT;
115 }
116
117 static int nrs_policy_stop_locked(struct ptlrpc_nrs_policy *policy)
118 {
119         struct ptlrpc_nrs *nrs = policy->pol_nrs;
120         ENTRY;
121
122         if (nrs->nrs_policy_fallback == policy && !nrs->nrs_stopping)
123                 RETURN(-EPERM);
124
125         if (policy->pol_state == NRS_POL_STATE_STARTING)
126                 RETURN(-EAGAIN);
127
128         /* In progress or already stopped */
129         if (policy->pol_state != NRS_POL_STATE_STARTED)
130                 RETURN(0);
131
132         policy->pol_state = NRS_POL_STATE_STOPPING;
133
134         /* Immediately make it invisible */
135         if (nrs->nrs_policy_primary == policy) {
136                 nrs->nrs_policy_primary = NULL;
137
138         } else {
139                 LASSERT(nrs->nrs_policy_fallback == policy);
140                 nrs->nrs_policy_fallback = NULL;
141         }
142
143         /* I have the only refcount */
144         if (policy->pol_ref == 1)
145                 nrs_policy_stop0(policy);
146
147         RETURN(0);
148 }
149
150 /**
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.
154  *
155  * \param[in] nrs the NRS head to carry out this operation on
156  */
157 static void nrs_policy_stop_primary(struct ptlrpc_nrs *nrs)
158 {
159         struct ptlrpc_nrs_policy *tmp = nrs->nrs_policy_primary;
160         ENTRY;
161
162         if (tmp == NULL) {
163                 /**
164                  * XXX: This should really be RETURN_EXIT, but the latter does
165                  * not currently print anything out, and possibly should be
166                  * fixed to do so.
167                  */
168                 EXIT;
169                 return;
170         }
171
172         nrs->nrs_policy_primary = NULL;
173
174         LASSERT(tmp->pol_state == NRS_POL_STATE_STARTED);
175         tmp->pol_state = NRS_POL_STATE_STOPPING;
176
177         if (tmp->pol_ref == 0)
178                 nrs_policy_stop0(tmp);
179         EXIT;
180 }
181
182 /**
183  * Transitions a policy across the ptlrpc_nrs_pol_state range of values, in
184  * response to an lprocfs command to start a policy.
185  *
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.
193  *
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.
200  */
201 static int nrs_policy_start_locked(struct ptlrpc_nrs_policy *policy, char *arg)
202 {
203         struct ptlrpc_nrs      *nrs = policy->pol_nrs;
204         int                     rc = 0;
205         ENTRY;
206
207         /**
208          * Don't allow multiple starting which is too complex, and has no real
209          * benefit.
210          */
211         if (nrs->nrs_policy_starting)
212                 RETURN(-EAGAIN);
213
214         LASSERT(policy->pol_state != NRS_POL_STATE_STARTING);
215
216         if (policy->pol_state == NRS_POL_STATE_STOPPING)
217                 RETURN(-EAGAIN);
218
219         if (policy->pol_flags & PTLRPC_NRS_FL_FALLBACK) {
220                 /**
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.
225                  */
226                 if (policy == nrs->nrs_policy_fallback) {
227                         nrs_policy_stop_primary(nrs);
228                         RETURN(0);
229                 }
230
231                 /**
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.
236                  */
237                 LASSERT(nrs->nrs_policy_fallback == NULL);
238         } else {
239                 /**
240                  * Shouldn't start primary policy if w/o fallback policy.
241                  */
242                 if (nrs->nrs_policy_fallback == NULL)
243                         RETURN(-EPERM);
244
245                 if (policy->pol_state == NRS_POL_STATE_STARTED)
246                         RETURN(0);
247         }
248
249         /**
250          * Increase the module usage count for policies registering from other
251          * modules.
252          */
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);
258                 RETURN(-ENODEV);
259         }
260
261         /**
262          * Serialize policy starting across the NRS head
263          */
264         nrs->nrs_policy_starting = 1;
265
266         policy->pol_state = NRS_POL_STATE_STARTING;
267
268         if (policy->pol_desc->pd_ops->op_policy_start) {
269                 spin_unlock(&nrs->nrs_lock);
270
271                 rc = policy->pol_desc->pd_ops->op_policy_start(policy, arg);
272
273                 spin_lock(&nrs->nrs_lock);
274                 if (rc != 0) {
275                         if (atomic_dec_and_test(&policy->pol_desc->pd_refs))
276                                 module_put(policy->pol_desc->pd_owner);
277
278                         policy->pol_state = NRS_POL_STATE_STOPPED;
279                         GOTO(out, rc);
280                 }
281         }
282
283         policy->pol_state = NRS_POL_STATE_STARTED;
284
285         if (policy->pol_flags & PTLRPC_NRS_FL_FALLBACK) {
286                 /**
287                  * This path is only used at PTLRPC service setup time.
288                  */
289                 nrs->nrs_policy_fallback = policy;
290         } else {
291                 /*
292                  * Try to stop the current primary policy if there is one.
293                  */
294                 nrs_policy_stop_primary(nrs);
295
296                 /**
297                  * And set the newly-started policy as the primary one.
298                  */
299                 nrs->nrs_policy_primary = policy;
300         }
301
302 out:
303         nrs->nrs_policy_starting = 0;
304
305         RETURN(rc);
306 }
307
308 /**
309  * Increases the policy's usage reference count.
310  */
311 static inline void nrs_policy_get_locked(struct ptlrpc_nrs_policy *policy)
312 {
313         policy->pol_ref++;
314 }
315
316 /**
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
320  * stopped).
321  */
322 static void nrs_policy_put_locked(struct ptlrpc_nrs_policy *policy)
323 {
324         LASSERT(policy->pol_ref > 0);
325
326         policy->pol_ref--;
327         if (unlikely(policy->pol_ref == 0 &&
328             policy->pol_state == NRS_POL_STATE_STOPPING))
329                 nrs_policy_stop0(policy);
330 }
331
332 static void nrs_policy_put(struct ptlrpc_nrs_policy *policy)
333 {
334         spin_lock(&policy->pol_nrs->nrs_lock);
335         nrs_policy_put_locked(policy);
336         spin_unlock(&policy->pol_nrs->nrs_lock);
337 }
338
339 /**
340  * Find and return a policy by name.
341  */
342 static struct ptlrpc_nrs_policy * nrs_policy_find_locked(struct ptlrpc_nrs *nrs,
343                                                          char *name)
344 {
345         struct ptlrpc_nrs_policy *tmp;
346
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);
351                         return tmp;
352                 }
353         }
354         return NULL;
355 }
356
357 /**
358  * Release references for the resource hierarchy moving upwards towards the
359  * policy instance resource.
360  */
361 static void nrs_resource_put(struct ptlrpc_nrs_resource *res)
362 {
363         struct ptlrpc_nrs_policy *policy = res->res_policy;
364
365         if (policy->pol_desc->pd_ops->op_res_put != NULL) {
366                 struct ptlrpc_nrs_resource *parent;
367
368                 for (; res != NULL; res = parent) {
369                         parent = res->res_parent;
370                         policy->pol_desc->pd_ops->op_res_put(policy, res);
371                 }
372         }
373 }
374
375 /**
376  * Obtains references for each resource in the resource hierarchy for request
377  * \a nrq if it is to be handled by \a policy.
378  *
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
384  *                        set.
385  *
386  * \retval NULL           resource hierarchy references not obtained
387  * \retval valid-pointer  the bottom level of the resource hierarchy
388  *
389  * \see ptlrpc_nrs_pol_ops::op_res_get()
390  */
391 static
392 struct ptlrpc_nrs_resource * nrs_resource_get(struct ptlrpc_nrs_policy *policy,
393                                               struct ptlrpc_nrs_request *nrq,
394                                               bool moving_req)
395 {
396         /**
397          * Set to NULL to traverse the resource hierarchy from the top.
398          */
399         struct ptlrpc_nrs_resource *res = NULL;
400         struct ptlrpc_nrs_resource *tmp = NULL;
401         int                         rc;
402
403         while (1) {
404                 rc = policy->pol_desc->pd_ops->op_res_get(policy, nrq, res,
405                                                           &tmp, moving_req);
406                 if (rc < 0) {
407                         if (res != NULL)
408                                 nrs_resource_put(res);
409                         return NULL;
410                 }
411
412                 LASSERT(tmp != NULL);
413                 tmp->res_parent = res;
414                 tmp->res_policy = policy;
415                 res = tmp;
416                 tmp = NULL;
417                 /**
418                  * Return once we have obtained a reference to the bottom level
419                  * of the resource hierarchy.
420                  */
421                 if (rc > 0)
422                         return res;
423         }
424 }
425
426 /**
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.
430  *
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
434  *                  stored.
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().
442  */
443 static void nrs_resource_get_safe(struct ptlrpc_nrs *nrs,
444                                   struct ptlrpc_nrs_request *nrq,
445                                   struct ptlrpc_nrs_resource **resp,
446                                   bool moving_req)
447 {
448         struct ptlrpc_nrs_policy   *primary = NULL;
449         struct ptlrpc_nrs_policy   *fallback = NULL;
450
451         memset(resp, 0, sizeof(resp[0]) * NRS_RES_MAX);
452
453         /**
454          * Obtain policy references.
455          */
456         spin_lock(&nrs->nrs_lock);
457
458         fallback = nrs->nrs_policy_fallback;
459         nrs_policy_get_locked(fallback);
460
461         primary = nrs->nrs_policy_primary;
462         if (primary != NULL)
463                 nrs_policy_get_locked(primary);
464
465         spin_unlock(&nrs->nrs_lock);
466
467         /**
468          * Obtain resource hierarchy references.
469          */
470         resp[NRS_RES_FALLBACK] = nrs_resource_get(fallback, nrq, moving_req);
471         LASSERT(resp[NRS_RES_FALLBACK] != NULL);
472
473         if (primary != NULL) {
474                 resp[NRS_RES_PRIMARY] = nrs_resource_get(primary, nrq,
475                                                          moving_req);
476                 /**
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
480                  * request.
481                  */
482                 if (resp[NRS_RES_PRIMARY] == NULL)
483                         nrs_policy_put(primary);
484         }
485 }
486
487 /**
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.
491  *
492  * \param resp  the resource hierarchy that is being released
493  *
494  * \see ptlrpcnrs_req_hp_move()
495  * \see ptlrpc_nrs_req_finalize()
496  */
497 static void nrs_resource_put_safe(struct ptlrpc_nrs_resource **resp)
498 {
499         struct ptlrpc_nrs_policy *pols[NRS_RES_MAX];
500         struct ptlrpc_nrs        *nrs = NULL;
501         int                       i;
502
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]);
507                         resp[i] = NULL;
508                 } else {
509                         pols[i] = NULL;
510                 }
511         }
512
513         for (i = 0; i < NRS_RES_MAX; i++) {
514                 if (pols[i] == NULL)
515                         continue;
516
517                 if (nrs == NULL) {
518                         nrs = pols[i]->pol_nrs;
519                         spin_lock(&nrs->nrs_lock);
520                 }
521                 nrs_policy_put_locked(pols[i]);
522         }
523
524         if (nrs != NULL)
525                 spin_unlock(&nrs->nrs_lock);
526 }
527
528 /**
529  * Obtains an NRS request from \a policy for handling or examination; the
530  * request should be removed in the 'handling' case.
531  *
532  * Calling into this function implies we already know the policy has a request
533  * waiting to be handled.
534  *
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
538  *                   from the policy.
539  * \param[in] force  when set, it will force a policy to return a request if it
540  *                   has one pending
541  *
542  * \retval the NRS request to be handled
543  */
544 static inline
545 struct ptlrpc_nrs_request * nrs_request_get(struct ptlrpc_nrs_policy *policy,
546                                             bool peek, bool force)
547 {
548         struct ptlrpc_nrs_request *nrq;
549
550         LASSERT(policy->pol_req_queued > 0);
551
552         nrq = policy->pol_desc->pd_ops->op_req_get(policy, peek, force);
553
554         LASSERT(ergo(nrq != NULL, nrs_request_policy(nrq) == policy));
555
556         return nrq;
557 }
558
559 /**
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.
564  *
565  * \param nrq the request being enqueued
566  *
567  * \see nrs_resource_get_safe()
568  */
569 static inline void nrs_request_enqueue(struct ptlrpc_nrs_request *nrq)
570 {
571         struct ptlrpc_nrs_policy *policy;
572         int                       rc;
573         int                       i;
574
575         /**
576          * Try in descending order, because the primary policy (if any) is
577          * the preferred choice.
578          */
579         for (i = NRS_RES_MAX - 1; i >= 0; i--) {
580                 if (nrq->nr_res_ptrs[i] == NULL)
581                         continue;
582
583                 nrq->nr_res_idx = i;
584                 policy = nrq->nr_res_ptrs[i]->res_policy;
585
586                 rc = policy->pol_desc->pd_ops->op_req_enqueue(policy, nrq);
587                 if (rc == 0) {
588                         policy->pol_nrs->nrs_req_queued++;
589                         policy->pol_req_queued++;
590                         return;
591                 }
592         }
593         /**
594          * Should never get here, as at least the primary policy's
595          * ptlrpc_nrs_pol_ops::op_req_enqueue() implementation should always
596          * succeed.
597          */
598         LBUG();
599 }
600
601 /**
602  * Called when a request has been handled
603  *
604  * \param[in] nrs the request that has been handled; can be used for
605  *                job/resource control.
606  *
607  * \see ptlrpc_nrs_req_stop_nolock()
608  */
609 static inline void nrs_request_stop(struct ptlrpc_nrs_request *nrq)
610 {
611         struct ptlrpc_nrs_policy *policy = nrs_request_policy(nrq);
612
613         if (policy->pol_desc->pd_ops->op_req_stop)
614                 policy->pol_desc->pd_ops->op_req_stop(policy, nrq);
615
616         LASSERT(policy->pol_nrs->nrs_req_started > 0);
617         LASSERT(policy->pol_req_started > 0);
618
619         policy->pol_nrs->nrs_req_started--;
620         policy->pol_req_started--;
621 }
622
623 /**
624  * Handler for operations that can be carried out on policies.
625  *
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.
628  *
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
636  *                     information.
637  *
638  * \retval -ve error condition
639  * \retval   0 operation was carried out successfully
640  */
641 static int nrs_policy_ctl(struct ptlrpc_nrs *nrs, char *name,
642                           enum ptlrpc_nrs_ctl opc, void *arg)
643 {
644         struct ptlrpc_nrs_policy       *policy;
645         int                             rc = 0;
646         ENTRY;
647
648         spin_lock(&nrs->nrs_lock);
649
650         policy = nrs_policy_find_locked(nrs, name);
651         if (policy == NULL)
652                 GOTO(out, rc = -ENOENT);
653
654         switch (opc) {
655                 /**
656                  * Unknown opcode, pass it down to the policy-specific control
657                  * function for handling.
658                  */
659         default:
660                 rc = nrs_policy_ctl_locked(policy, opc, arg);
661                 break;
662
663                 /**
664                  * Start \e policy
665                  */
666         case PTLRPC_NRS_CTL_START:
667                 rc = nrs_policy_start_locked(policy, arg);
668                 break;
669         }
670 out:
671         if (policy != NULL)
672                 nrs_policy_put_locked(policy);
673
674         spin_unlock(&nrs->nrs_lock);
675
676         RETURN(rc);
677 }
678
679 /**
680  * Unregisters a policy by name.
681  *
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
685  *
686  * \retval -ve error
687  * \retval   0 success
688  */
689 static int nrs_policy_unregister(struct ptlrpc_nrs *nrs, char *name)
690 {
691         struct ptlrpc_nrs_policy *policy = NULL;
692         ENTRY;
693
694         spin_lock(&nrs->nrs_lock);
695
696         policy = nrs_policy_find_locked(nrs, name);
697         if (policy == NULL) {
698                 spin_unlock(&nrs->nrs_lock);
699
700                 CERROR("Can't find NRS policy %s\n", name);
701                 RETURN(-ENOENT);
702         }
703
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);
708
709                 spin_unlock(&nrs->nrs_lock);
710                 RETURN(-EBUSY);
711         }
712
713         LASSERT(policy->pol_req_queued == 0);
714         LASSERT(policy->pol_req_started == 0);
715
716         if (policy->pol_state != NRS_POL_STATE_STOPPED) {
717                 nrs_policy_stop_locked(policy);
718                 LASSERT(policy->pol_state == NRS_POL_STATE_STOPPED);
719         }
720
721         list_del(&policy->pol_list);
722         nrs->nrs_num_pols--;
723
724         nrs_policy_put_locked(policy);
725
726         spin_unlock(&nrs->nrs_lock);
727
728         nrs_policy_fini(policy);
729
730         LASSERT(policy->pol_private == NULL);
731         OBD_FREE_PTR(policy);
732
733         RETURN(0);
734 }
735
736 /**
737  * Register a policy from \policy descriptor \a desc with NRS head \a nrs.
738  *
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.
742  *
743  * \retval -ve error
744  * \retval   0 success
745  */
746 static int nrs_policy_register(struct ptlrpc_nrs *nrs,
747                                struct ptlrpc_nrs_pol_desc *desc)
748 {
749         struct ptlrpc_nrs_policy       *policy;
750         struct ptlrpc_nrs_policy       *tmp;
751         struct ptlrpc_service_part     *svcpt = nrs->nrs_svcpt;
752         int                             rc;
753         ENTRY;
754
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);
762
763         OBD_CPT_ALLOC_GFP(policy, svcpt->scp_service->srv_cptable,
764                           svcpt->scp_cpt, sizeof(*policy), GFP_NOFS);
765         if (policy == NULL)
766                 RETURN(-ENOMEM);
767
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;
772
773         INIT_LIST_HEAD(&policy->pol_list);
774         INIT_LIST_HEAD(&policy->pol_list_queued);
775
776         rc = nrs_policy_init(policy);
777         if (rc != 0) {
778                 OBD_FREE_PTR(policy);
779                 RETURN(rc);
780         }
781
782         spin_lock(&nrs->nrs_lock);
783
784         tmp = nrs_policy_find_locked(nrs, policy->pol_desc->pd_name);
785         if (tmp != NULL) {
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);
790
791                 spin_unlock(&nrs->nrs_lock);
792                 nrs_policy_fini(policy);
793                 OBD_FREE_PTR(policy);
794
795                 RETURN(-EEXIST);
796         }
797
798         list_add_tail(&policy->pol_list, &nrs->nrs_policy_list);
799         nrs->nrs_num_pols++;
800
801         if (policy->pol_flags & PTLRPC_NRS_FL_REG_START)
802                 rc = nrs_policy_start_locked(policy, NULL);
803
804         spin_unlock(&nrs->nrs_lock);
805
806         if (rc != 0)
807                 (void) nrs_policy_unregister(nrs, policy->pol_desc->pd_name);
808
809         RETURN(rc);
810 }
811
812 /**
813  * Enqueue request \a req using one of the policies its resources are referring
814  * to.
815  *
816  * \param[in] req the request to enqueue.
817  */
818 static void ptlrpc_nrs_req_add_nolock(struct ptlrpc_request *req)
819 {
820         struct ptlrpc_nrs_policy       *policy;
821
822         LASSERT(req->rq_nrq.nr_initialized);
823         LASSERT(!req->rq_nrq.nr_enqueued);
824
825         nrs_request_enqueue(&req->rq_nrq);
826         req->rq_nrq.nr_enqueued = 1;
827
828         policy = nrs_request_policy(&req->rq_nrq);
829         /**
830          * Add the policy to the NRS head's list of policies with enqueued
831          * requests, if it has not been added there.
832          */
833         if (unlikely(list_empty(&policy->pol_list_queued)))
834                 list_add_tail(&policy->pol_list_queued,
835                                   &policy->pol_nrs->nrs_policy_queued);
836 }
837
838 /**
839  * Enqueue a request on the high priority NRS head.
840  *
841  * \param req the request to enqueue.
842  */
843 static void ptlrpc_nrs_hpreq_add_nolock(struct ptlrpc_request *req)
844 {
845         int     opc = lustre_msg_get_opc(req->rq_reqmsg);
846         ENTRY;
847
848         spin_lock(&req->rq_lock);
849         req->rq_hp = 1;
850         ptlrpc_nrs_req_add_nolock(req);
851         if (opc != OBD_PING)
852                 DEBUG_REQ(D_NET, req, "high priority req");
853         spin_unlock(&req->rq_lock);
854         EXIT;
855 }
856
857 /**
858  * Returns a boolean predicate indicating whether the policy described by
859  * \a desc is adequate for use with service \a svc.
860  *
861  * \param[in] svc  the service
862  * \param[in] desc the policy descriptor
863  *
864  * \retval false the policy is not compatible with the service
865  * \retval true  the policy is compatible with the service
866  */
867 static inline bool nrs_policy_compatible(const struct ptlrpc_service *svc,
868                                          const struct ptlrpc_nrs_pol_desc *desc)
869 {
870         return desc->pd_compat(svc, desc);
871 }
872
873 /**
874  * Registers all compatible policies in nrs_core.nrs_policies, for NRS head
875  * \a nrs.
876  *
877  * \param[in] nrs the NRS head
878  *
879  * \retval -ve error
880  * \retval   0 success
881  *
882  * \pre mutex_is_locked(&nrs_core.nrs_mutex)
883  *
884  * \see ptlrpc_service_nrs_setup()
885  */
886 static int nrs_register_policies_locked(struct ptlrpc_nrs *nrs)
887 {
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;
892         int                               rc = -EINVAL;
893         ENTRY;
894
895         LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
896
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);
900                         if (rc != 0) {
901                                 CERROR("Failed to register NRS policy %s for "
902                                        "partition %d of service %s: %d\n",
903                                        desc->pd_name, svcpt->scp_cpt,
904                                        svc->srv_name, rc);
905                                 /**
906                                  * Fail registration if any of the policies'
907                                  * registration fails.
908                                  */
909                                 break;
910                         }
911                 }
912         }
913
914         RETURN(rc);
915 }
916
917 /**
918  * Initializes NRS head \a nrs of service partition \a svcpt, and registers all
919  * compatible policies in NRS core, with the NRS head.
920  *
921  * \param[in] nrs   the NRS head
922  * \param[in] svcpt the PTLRPC service partition to setup
923  *
924  * \retval -ve error
925  * \retval   0 success
926  *
927  * \pre mutex_is_locked(&nrs_core.nrs_mutex)
928  */
929 static int nrs_svcpt_setup_locked0(struct ptlrpc_nrs *nrs,
930                                    struct ptlrpc_service_part *svcpt)
931 {
932         int                             rc;
933         enum ptlrpc_nrs_queue_type      queue;
934
935         LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
936
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;
941         else
942                 LBUG();
943
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;
950
951         rc = nrs_register_policies_locked(nrs);
952
953         RETURN(rc);
954 }
955
956 /**
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.
960  *
961  * \param[in,out] svcpt the PTLRPC service partition to setup
962  *
963  * \pre mutex_is_locked(&nrs_core.nrs_mutex)
964  */
965 static int nrs_svcpt_setup_locked(struct ptlrpc_service_part *svcpt)
966 {
967         struct ptlrpc_nrs              *nrs;
968         int                             rc;
969         ENTRY;
970
971         LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
972
973         /**
974          * Initialize the regular NRS head.
975          */
976         nrs = nrs_svcpt2nrs(svcpt, false);
977         rc = nrs_svcpt_setup_locked0(nrs, svcpt);
978         if (rc < 0)
979                 GOTO(out, rc);
980
981         /**
982          * Optionally allocate a high-priority NRS head.
983          */
984         if (svcpt->scp_service->srv_ops.so_hpreq_handler == NULL)
985                 GOTO(out, rc);
986
987         OBD_CPT_ALLOC_PTR(svcpt->scp_nrs_hp,
988                           svcpt->scp_service->srv_cptable,
989                           svcpt->scp_cpt);
990         if (svcpt->scp_nrs_hp == NULL)
991                 GOTO(out, rc = -ENOMEM);
992
993         nrs = nrs_svcpt2nrs(svcpt, true);
994         rc = nrs_svcpt_setup_locked0(nrs, svcpt);
995
996 out:
997         RETURN(rc);
998 }
999
1000 /**
1001  * Unregisters all policies on all available NRS heads in a service partition;
1002  * called at PTLRPC service unregistration time.
1003  *
1004  * \param[in] svcpt the PTLRPC service partition
1005  *
1006  * \pre mutex_is_locked(&nrs_core.nrs_mutex)
1007  */
1008 static void nrs_svcpt_cleanup_locked(struct ptlrpc_service_part *svcpt)
1009 {
1010         struct ptlrpc_nrs              *nrs;
1011         struct ptlrpc_nrs_policy       *policy;
1012         struct ptlrpc_nrs_policy       *tmp;
1013         int                             rc;
1014         bool                            hp = false;
1015         ENTRY;
1016
1017         LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
1018
1019 again:
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) {
1024                 EXIT;
1025                 return;
1026         }
1027         nrs->nrs_stopping = 1;
1028
1029         list_for_each_entry_safe(policy, tmp, &nrs->nrs_policy_list,
1030                                      pol_list) {
1031                 rc = nrs_policy_unregister(nrs, policy->pol_desc->pd_name);
1032                 LASSERT(rc == 0);
1033         }
1034
1035         /**
1036          * If the service partition has an HP NRS head, clean that up as well.
1037          */
1038         if (!hp && nrs_svcpt_has_hp(svcpt)) {
1039                 hp = true;
1040                 goto again;
1041         }
1042
1043         if (hp)
1044                 OBD_FREE_PTR(nrs);
1045
1046         EXIT;
1047 }
1048
1049 /**
1050  * Returns the descriptor for a policy as identified by by \a name.
1051  *
1052  * \param[in] name the policy name
1053  *
1054  * \retval the policy descriptor
1055  * \retval NULL
1056  */
1057 static struct ptlrpc_nrs_pol_desc *nrs_policy_find_desc_locked(const char *name)
1058 {
1059         struct ptlrpc_nrs_pol_desc     *tmp;
1060         ENTRY;
1061
1062         list_for_each_entry(tmp, &nrs_core.nrs_policies, pd_list) {
1063                 if (strncmp(tmp->pd_name, name, NRS_POL_NAME_MAX) == 0)
1064                         RETURN(tmp);
1065         }
1066         RETURN(NULL);
1067 }
1068
1069 /**
1070  * Removes the policy from all supported NRS heads of all partitions of all
1071  * PTLRPC services.
1072  *
1073  * \param[in] desc the policy descriptor to unregister
1074  *
1075  * \retval -ve error
1076  * \retval  0  successfully unregistered policy on all supported NRS heads
1077  *
1078  * \pre mutex_is_locked(&nrs_core.nrs_mutex)
1079  * \pre mutex_is_locked(&ptlrpc_all_services_mutex)
1080  */
1081 static int nrs_policy_unregister_locked(struct ptlrpc_nrs_pol_desc *desc)
1082 {
1083         struct ptlrpc_nrs              *nrs;
1084         struct ptlrpc_service          *svc;
1085         struct ptlrpc_service_part     *svcpt;
1086         int                             i;
1087         int                             rc = 0;
1088         ENTRY;
1089
1090         LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
1091         LASSERT(mutex_is_locked(&ptlrpc_all_services_mutex));
1092
1093         list_for_each_entry(svc, &ptlrpc_all_services, srv_list) {
1094
1095                 if (!nrs_policy_compatible(svc, desc) ||
1096                     unlikely(svc->srv_is_stopping))
1097                         continue;
1098
1099                 ptlrpc_service_for_each_part(svcpt, i, svc) {
1100                         bool hp = false;
1101
1102 again:
1103                         nrs = nrs_svcpt2nrs(svcpt, hp);
1104                         rc = nrs_policy_unregister(nrs, desc->pd_name);
1105                         /**
1106                          * Ignore -ENOENT as the policy may not have registered
1107                          * successfully on all service partitions.
1108                          */
1109                         if (rc == -ENOENT) {
1110                                 rc = 0;
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);
1116                                 RETURN(rc);
1117                         }
1118
1119                         if (!hp && nrs_svc_has_hp(svc)) {
1120                                 hp = true;
1121                                 goto again;
1122                         }
1123                 }
1124
1125                 if (desc->pd_ops->op_lprocfs_fini != NULL)
1126                         desc->pd_ops->op_lprocfs_fini(svc);
1127         }
1128
1129         RETURN(rc);
1130 }
1131
1132 /**
1133  * Registers a new policy with NRS core.
1134  *
1135  * The function will only succeed if policy registration with all compatible
1136  * service partitions (if any) is successful.
1137  *
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.
1141  *
1142  * \param[in] conf configuration information for the new policy to register
1143  *
1144  * \retval -ve error
1145  * \retval   0 success
1146  */
1147 int ptlrpc_nrs_policy_register(struct ptlrpc_nrs_pol_conf *conf)
1148 {
1149         struct ptlrpc_service          *svc;
1150         struct ptlrpc_nrs_pol_desc     *desc;
1151         int                             rc = 0;
1152         ENTRY;
1153
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));
1161
1162         conf->nc_name[NRS_POL_NAME_MAX - 1] = '\0';
1163
1164         /**
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
1171          * this.
1172          */
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);
1180                 RETURN(-EINVAL);
1181         }
1182
1183         mutex_lock(&nrs_core.nrs_mutex);
1184
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",
1188                        conf->nc_name);
1189                 GOTO(fail, rc = -EEXIST);
1190         }
1191
1192         OBD_ALLOC_PTR(desc);
1193         if (desc == NULL)
1194                 GOTO(fail, rc = -ENOMEM);
1195
1196         if (strlcpy(desc->pd_name, conf->nc_name, sizeof(desc->pd_name)) >=
1197             sizeof(desc->pd_name)) {
1198                 OBD_FREE_PTR(desc);
1199                 GOTO(fail, rc = -E2BIG);
1200         }
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);
1208
1209         /**
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().
1216          */
1217         if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) == 0)
1218                 goto internal;
1219
1220         /**
1221          * Register the new policy on all compatible services
1222          */
1223         mutex_lock(&ptlrpc_all_services_mutex);
1224
1225         list_for_each_entry(svc, &ptlrpc_all_services, srv_list) {
1226                 struct ptlrpc_service_part     *svcpt;
1227                 int                             i;
1228                 int                             rc2;
1229
1230                 if (!nrs_policy_compatible(svc, desc) ||
1231                     unlikely(svc->srv_is_stopping))
1232                         continue;
1233
1234                 ptlrpc_service_for_each_part(svcpt, i, svc) {
1235                         struct ptlrpc_nrs      *nrs;
1236                         bool                    hp = false;
1237 again:
1238                         nrs = nrs_svcpt2nrs(svcpt, hp);
1239                         rc = nrs_policy_register(nrs, desc);
1240                         if (rc != 0) {
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);
1245
1246                                 rc2 = nrs_policy_unregister_locked(desc);
1247                                 /**
1248                                  * Should not fail at this point
1249                                  */
1250                                 LASSERT(rc2 == 0);
1251                                 mutex_unlock(&ptlrpc_all_services_mutex);
1252                                 OBD_FREE_PTR(desc);
1253                                 GOTO(fail, rc);
1254                         }
1255
1256                         if (!hp && nrs_svc_has_hp(svc)) {
1257                                 hp = true;
1258                                 goto again;
1259                         }
1260                 }
1261
1262                 /**
1263                  * No need to take a reference to other modules here, as we
1264                  * will be calling from the module's init() function.
1265                  */
1266                 if (desc->pd_ops->op_lprocfs_init != NULL) {
1267                         rc = desc->pd_ops->op_lprocfs_init(svc);
1268                         if (rc != 0) {
1269                                 rc2 = nrs_policy_unregister_locked(desc);
1270                                 /**
1271                                  * Should not fail at this point
1272                                  */
1273                                 LASSERT(rc2 == 0);
1274                                 mutex_unlock(&ptlrpc_all_services_mutex);
1275                                 OBD_FREE_PTR(desc);
1276                                 GOTO(fail, rc);
1277                         }
1278                 }
1279         }
1280
1281         mutex_unlock(&ptlrpc_all_services_mutex);
1282 internal:
1283         list_add_tail(&desc->pd_list, &nrs_core.nrs_policies);
1284 fail:
1285         mutex_unlock(&nrs_core.nrs_mutex);
1286
1287         RETURN(rc);
1288 }
1289 EXPORT_SYMBOL(ptlrpc_nrs_policy_register);
1290
1291 /**
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.
1294  *
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.
1299  *
1300  * \param[in] conf configuration information for the policy to unregister
1301  *
1302  * \retval -ve error
1303  * \retval   0 success
1304  */
1305 int ptlrpc_nrs_policy_unregister(struct ptlrpc_nrs_pol_conf *conf)
1306 {
1307         struct ptlrpc_nrs_pol_desc      *desc;
1308         int                              rc;
1309         ENTRY;
1310
1311         LASSERT(conf != NULL);
1312
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");
1316                 RETURN(-EPERM);
1317         }
1318
1319         conf->nc_name[NRS_POL_NAME_MAX - 1] = '\0';
1320
1321         mutex_lock(&nrs_core.nrs_mutex);
1322
1323         desc = nrs_policy_find_desc_locked(conf->nc_name);
1324         if (desc == NULL) {
1325                 CERROR("Failing to unregister NRS policy %s which has "
1326                        "not been registered with NRS core!\n",
1327                        conf->nc_name);
1328                 GOTO(not_exist, rc = -ENOENT);
1329         }
1330
1331         mutex_lock(&ptlrpc_all_services_mutex);
1332
1333         rc = nrs_policy_unregister_locked(desc);
1334         if (rc < 0) {
1335                 if (rc == -EBUSY)
1336                         CERROR("Please first stop policy %s on all service "
1337                                "partitions and then retry to unregister the "
1338                                "policy.\n", conf->nc_name);
1339                 GOTO(fail, rc);
1340         }
1341
1342         CDEBUG(D_INFO, "Unregistering policy %s from NRS core.\n",
1343                conf->nc_name);
1344
1345         list_del(&desc->pd_list);
1346         OBD_FREE_PTR(desc);
1347
1348 fail:
1349         mutex_unlock(&ptlrpc_all_services_mutex);
1350
1351 not_exist:
1352         mutex_unlock(&nrs_core.nrs_mutex);
1353
1354         RETURN(rc);
1355 }
1356 EXPORT_SYMBOL(ptlrpc_nrs_policy_unregister);
1357
1358 /**
1359  * Setup NRS heads on all service partitions of service \a svc, and register
1360  * all compatible policies on those NRS heads.
1361  *
1362  * To be called from withing ptl
1363  * \param[in] svc the service to setup
1364  *
1365  * \retval -ve error, the calling logic should eventually call
1366  *                    ptlrpc_service_nrs_cleanup() to undo any work performed
1367  *                    by this function.
1368  *
1369  * \see ptlrpc_register_service()
1370  * \see ptlrpc_service_nrs_cleanup()
1371  */
1372 int ptlrpc_service_nrs_setup(struct ptlrpc_service *svc)
1373 {
1374         struct ptlrpc_service_part             *svcpt;
1375         const struct ptlrpc_nrs_pol_desc       *desc;
1376         int                                     i;
1377         int                                     rc = 0;
1378
1379         mutex_lock(&nrs_core.nrs_mutex);
1380
1381         /**
1382          * Initialize NRS heads on all service CPTs.
1383          */
1384         ptlrpc_service_for_each_part(svcpt, i, svc) {
1385                 rc = nrs_svcpt_setup_locked(svcpt);
1386                 if (rc != 0)
1387                         GOTO(failed, rc);
1388         }
1389
1390         /**
1391          * Set up lprocfs interfaces for all supported policies for the
1392          * service.
1393          */
1394         list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
1395                 if (!nrs_policy_compatible(svc, desc))
1396                         continue;
1397
1398                 if (desc->pd_ops->op_lprocfs_init != NULL) {
1399                         rc = desc->pd_ops->op_lprocfs_init(svc);
1400                         if (rc != 0)
1401                                 GOTO(failed, rc);
1402                 }
1403         }
1404
1405 failed:
1406
1407         mutex_unlock(&nrs_core.nrs_mutex);
1408
1409         RETURN(rc);
1410 }
1411
1412 /**
1413  * Unregisters all policies on all service partitions of service \a svc.
1414  *
1415  * \param[in] svc the PTLRPC service to unregister
1416  */
1417 void ptlrpc_service_nrs_cleanup(struct ptlrpc_service *svc)
1418 {
1419         struct ptlrpc_service_part           *svcpt;
1420         const struct ptlrpc_nrs_pol_desc     *desc;
1421         int                                   i;
1422
1423         mutex_lock(&nrs_core.nrs_mutex);
1424
1425         /**
1426          * Clean up NRS heads on all service partitions
1427          */
1428         ptlrpc_service_for_each_part(svcpt, i, svc)
1429                 nrs_svcpt_cleanup_locked(svcpt);
1430
1431         /**
1432          * Clean up lprocfs interfaces for all supported policies for the
1433          * service.
1434          */
1435         list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
1436                 if (!nrs_policy_compatible(svc, desc))
1437                         continue;
1438
1439                 if (desc->pd_ops->op_lprocfs_fini != NULL)
1440                         desc->pd_ops->op_lprocfs_fini(svc);
1441         }
1442
1443         mutex_unlock(&nrs_core.nrs_mutex);
1444 }
1445
1446 /**
1447  * Obtains NRS head resources for request \a req.
1448  *
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().
1452  *
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
1456  */
1457 void ptlrpc_nrs_req_initialize(struct ptlrpc_service_part *svcpt,
1458                                struct ptlrpc_request *req, bool hp)
1459 {
1460         struct ptlrpc_nrs       *nrs = nrs_svcpt2nrs(svcpt, hp);
1461
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,
1464                               false);
1465
1466         /**
1467          * It is fine to access \e nr_initialized without locking as there is
1468          * no contention at this early stage.
1469          */
1470         req->rq_nrq.nr_initialized = 1;
1471 }
1472
1473 /**
1474  * Releases resources for a request; is called after the request has been
1475  * handled.
1476  *
1477  * \param[in] req the request
1478  *
1479  * \see ptlrpc_server_finish_request()
1480  */
1481 void ptlrpc_nrs_req_finalize(struct ptlrpc_request *req)
1482 {
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;
1488         }
1489 }
1490
1491 void ptlrpc_nrs_req_stop_nolock(struct ptlrpc_request *req)
1492 {
1493         if (req->rq_nrq.nr_started)
1494                 nrs_request_stop(&req->rq_nrq);
1495 }
1496
1497 /**
1498  * Enqueues request \a req on either the regular or high-priority NRS head
1499  * of service partition \a svcpt.
1500  *
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.
1505  */
1506 void ptlrpc_nrs_req_add(struct ptlrpc_service_part *svcpt,
1507                         struct ptlrpc_request *req, bool hp)
1508 {
1509         spin_lock(&svcpt->scp_req_lock);
1510
1511         if (hp)
1512                 ptlrpc_nrs_hpreq_add_nolock(req);
1513         else
1514                 ptlrpc_nrs_req_add_nolock(req);
1515
1516         spin_unlock(&svcpt->scp_req_lock);
1517 }
1518
1519 static void nrs_request_removed(struct ptlrpc_nrs_policy *policy)
1520 {
1521         LASSERT(policy->pol_nrs->nrs_req_queued > 0);
1522         LASSERT(policy->pol_req_queued > 0);
1523
1524         policy->pol_nrs->nrs_req_queued--;
1525         policy->pol_req_queued--;
1526
1527         /**
1528          * If the policy has no more requests queued, remove it from
1529          * ptlrpc_nrs::nrs_policy_queued.
1530          */
1531         if (unlikely(policy->pol_req_queued == 0)) {
1532                 list_del_init(&policy->pol_list_queued);
1533
1534                 /**
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.
1538                  */
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);
1542
1543                 list_move_tail(&policy->pol_list_queued,
1544                                    &policy->pol_nrs->nrs_policy_queued);
1545         }
1546 }
1547
1548 /**
1549  * Obtains a request for handling from an NRS head of service partition
1550  * \a svcpt.
1551  *
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
1557  *                  from the policy.
1558  * \param[in] force when set, it will force a policy to return a request if it
1559  *                  has one pending
1560  *
1561  * \retval the  request to be handled
1562  * \retval NULL the head has no requests to serve
1563  */
1564 struct ptlrpc_request *
1565 ptlrpc_nrs_req_get_nolock0(struct ptlrpc_service_part *svcpt, bool hp,
1566                            bool peek, bool force)
1567 {
1568         struct ptlrpc_nrs         *nrs = nrs_svcpt2nrs(svcpt, hp);
1569         struct ptlrpc_nrs_policy  *policy;
1570         struct ptlrpc_nrs_request *nrq;
1571
1572         /**
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.
1575          */
1576         list_for_each_entry(policy, &nrs->nrs_policy_queued,
1577                                 pol_list_queued) {
1578                 nrq = nrs_request_get(policy, peek, force);
1579                 if (nrq != NULL) {
1580                         if (likely(!peek)) {
1581                                 nrq->nr_started = 1;
1582
1583                                 policy->pol_req_started++;
1584                                 policy->pol_nrs->nrs_req_started++;
1585
1586                                 nrs_request_removed(policy);
1587                         }
1588
1589                         return container_of(nrq, struct ptlrpc_request, rq_nrq);
1590                 }
1591         }
1592
1593         return NULL;
1594 }
1595
1596 /**
1597  * Dequeues request \a req from the policy it has been enqueued on.
1598  *
1599  * \param[in] req the request
1600  */
1601 void ptlrpc_nrs_req_del_nolock(struct ptlrpc_request *req)
1602 {
1603         struct ptlrpc_nrs_policy *policy = nrs_request_policy(&req->rq_nrq);
1604
1605         policy->pol_desc->pd_ops->op_req_dequeue(policy, &req->rq_nrq);
1606
1607         req->rq_nrq.nr_enqueued = 0;
1608
1609         nrs_request_removed(policy);
1610 }
1611
1612 /**
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
1616  * result.
1617  *
1618  * \param[in] svcpt the service partition to enquire.
1619  * \param[in] hp    whether the regular or high-priority NRS head is to be
1620  *                  enquired.
1621  *
1622  * \retval false the indicated NRS head has no enqueued requests.
1623  * \retval true  the indicated NRS head has some enqueued requests.
1624  */
1625 bool ptlrpc_nrs_req_pending_nolock(struct ptlrpc_service_part *svcpt, bool hp)
1626 {
1627         struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1628
1629         return nrs->nrs_req_queued > 0;
1630 };
1631
1632 /**
1633  * Returns whether NRS policy is throttling reqeust
1634  *
1635  * \param[in] svcpt the service partition to enquire.
1636  * \param[in] hp    whether the regular or high-priority NRS head is to be
1637  *                  enquired.
1638  *
1639  * \retval false the indicated NRS head has no enqueued requests.
1640  * \retval true  the indicated NRS head has some enqueued requests.
1641  */
1642 bool ptlrpc_nrs_req_throttling_nolock(struct ptlrpc_service_part *svcpt,
1643                                       bool hp)
1644 {
1645         struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1646
1647         return !!nrs->nrs_throttling;
1648 };
1649
1650 /**
1651  * Moves request \a req from the regular to the high-priority NRS head.
1652  *
1653  * \param[in] req the request to move
1654  */
1655 void ptlrpc_nrs_req_hp_move(struct ptlrpc_request *req)
1656 {
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];
1661         ENTRY;
1662
1663         /**
1664          * Obtain the high-priority NRS head resources.
1665          */
1666         nrs_resource_get_safe(nrs_svcpt2nrs(svcpt, true), nrq, res1, true);
1667
1668         spin_lock(&svcpt->scp_req_lock);
1669
1670         if (!ptlrpc_nrs_req_can_move(req))
1671                 goto out;
1672
1673         ptlrpc_nrs_req_del_nolock(req);
1674
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]));
1677
1678         ptlrpc_nrs_hpreq_add_nolock(req);
1679
1680         memcpy(res1, res2, NRS_RES_MAX * sizeof(res1[0]));
1681 out:
1682         spin_unlock(&svcpt->scp_req_lock);
1683
1684         /**
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()
1688          * returned false.
1689          */
1690         nrs_resource_put_safe(res1);
1691         EXIT;
1692 }
1693
1694 /**
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.
1698  *
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.
1716  *
1717  *\retval -ve error condition
1718  *\retval   0 operation was carried out successfully
1719  */
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)
1723 {
1724         struct ptlrpc_service_part     *svcpt;
1725         int                             i;
1726         int                             rc = 0;
1727         ENTRY;
1728
1729         LASSERT(opc != PTLRPC_NRS_CTL_INVALID);
1730
1731         if ((queue & PTLRPC_NRS_QUEUE_BOTH) == 0)
1732                 return -EINVAL;
1733
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,
1737                                             opc, arg);
1738                         if (rc != 0 || (queue == PTLRPC_NRS_QUEUE_REG &&
1739                                         single))
1740                                 GOTO(out, rc);
1741                 }
1742
1743                 if ((queue & PTLRPC_NRS_QUEUE_HP) != 0) {
1744                         /**
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.
1751                          */
1752                         rc = nrs_policy_ctl(nrs_svcpt2nrs(svcpt, true), name,
1753                                             opc, arg);
1754                         if (rc != 0 || single)
1755                                 GOTO(out, rc);
1756                 }
1757         }
1758 out:
1759         RETURN(rc);
1760 }
1761
1762
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;
1772 #endif
1773
1774 /**
1775  * Adds all policies that ship with the ptlrpc module, to NRS core's list of
1776  * policies \e nrs_core.nrs_policies.
1777  *
1778  * \retval 0 all policies have been registered successfully
1779  * \retval -ve error
1780  */
1781 int ptlrpc_nrs_init(void)
1782 {
1783         int     rc;
1784         ENTRY;
1785
1786         mutex_init(&nrs_core.nrs_mutex);
1787         INIT_LIST_HEAD(&nrs_core.nrs_policies);
1788
1789         rc = ptlrpc_nrs_policy_register(&nrs_conf_fifo);
1790         if (rc != 0)
1791                 GOTO(fail, rc);
1792
1793 #if defined HAVE_SERVER_SUPPORT && defined(__KERNEL__)
1794         rc = ptlrpc_nrs_policy_register(&nrs_conf_crrn);
1795         if (rc != 0)
1796                 GOTO(fail, rc);
1797
1798         rc = ptlrpc_nrs_policy_register(&nrs_conf_orr);
1799         if (rc != 0)
1800                 GOTO(fail, rc);
1801
1802         rc = ptlrpc_nrs_policy_register(&nrs_conf_trr);
1803         if (rc != 0)
1804                 GOTO(fail, rc);
1805         rc = ptlrpc_nrs_policy_register(&nrs_conf_tbf);
1806         if (rc != 0)
1807                 GOTO(fail, rc);
1808 #endif
1809
1810         RETURN(rc);
1811 fail:
1812         /**
1813          * Since no PTLRPC services have been started at this point, all we need
1814          * to do for cleanup is to free the descriptors.
1815          */
1816         ptlrpc_nrs_fini();
1817
1818         RETURN(rc);
1819 }
1820
1821 /**
1822  * Removes all policy desciptors from nrs_core::nrs_policies, and frees the
1823  * policy descriptors.
1824  *
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
1828  * descriptors here.
1829  */
1830 void ptlrpc_nrs_fini(void)
1831 {
1832         struct ptlrpc_nrs_pol_desc *desc;
1833         struct ptlrpc_nrs_pol_desc *tmp;
1834
1835         list_for_each_entry_safe(desc, tmp, &nrs_core.nrs_policies,
1836                                      pd_list) {
1837                 list_del_init(&desc->pd_list);
1838                 OBD_FREE_PTR(desc);
1839         }
1840 }
1841
1842 /** @} nrs */