Whamcloud - gitweb
dc3dabdcb74fab60fa67d3a58adc0d86507da98b
[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 extern struct list_head ptlrpc_all_services;
54
55 /**
56  * NRS core object.
57  */
58 struct nrs_core nrs_core;
59
60 static int nrs_policy_init(struct ptlrpc_nrs_policy *policy)
61 {
62         return policy->pol_desc->pd_ops->op_policy_init != NULL ?
63                policy->pol_desc->pd_ops->op_policy_init(policy) : 0;
64 }
65
66 static void nrs_policy_fini(struct ptlrpc_nrs_policy *policy)
67 {
68         LASSERT(policy->pol_ref == 0);
69         LASSERT(policy->pol_req_queued == 0);
70
71         if (policy->pol_desc->pd_ops->op_policy_fini != NULL)
72                 policy->pol_desc->pd_ops->op_policy_fini(policy);
73 }
74
75 static int nrs_policy_ctl_locked(struct ptlrpc_nrs_policy *policy,
76                                  enum ptlrpc_nrs_ctl opc, void *arg)
77 {
78         /**
79          * The policy may be stopped, but the lprocfs files and
80          * ptlrpc_nrs_policy instances remain present until unregistration time.
81          * Do not perform the ctl operation if the policy is stopped, as
82          * policy->pol_private will be NULL in such a case.
83          */
84         if (policy->pol_state == NRS_POL_STATE_STOPPED)
85                 RETURN(-ENODEV);
86
87         RETURN(policy->pol_desc->pd_ops->op_policy_ctl != NULL ?
88                policy->pol_desc->pd_ops->op_policy_ctl(policy, opc, arg) :
89                -ENOSYS);
90 }
91
92 static void nrs_policy_stop0(struct ptlrpc_nrs_policy *policy)
93 {
94         struct ptlrpc_nrs *nrs = policy->pol_nrs;
95         ENTRY;
96
97         if (policy->pol_desc->pd_ops->op_policy_stop != NULL) {
98                 spin_unlock(&nrs->nrs_lock);
99
100                 policy->pol_desc->pd_ops->op_policy_stop(policy);
101
102                 spin_lock(&nrs->nrs_lock);
103         }
104
105         LASSERT(cfs_list_empty(&policy->pol_list_queued));
106         LASSERT(policy->pol_req_queued == 0 &&
107                 policy->pol_req_started == 0);
108
109         policy->pol_private = NULL;
110
111         policy->pol_state = NRS_POL_STATE_STOPPED;
112
113         if (cfs_atomic_dec_and_test(&policy->pol_desc->pd_refs))
114                 module_put(policy->pol_desc->pd_owner);
115
116         EXIT;
117 }
118
119 static int nrs_policy_stop_locked(struct ptlrpc_nrs_policy *policy)
120 {
121         struct ptlrpc_nrs *nrs = policy->pol_nrs;
122         ENTRY;
123
124         if (nrs->nrs_policy_fallback == policy && !nrs->nrs_stopping)
125                 RETURN(-EPERM);
126
127         if (policy->pol_state == NRS_POL_STATE_STARTING)
128                 RETURN(-EAGAIN);
129
130         /* In progress or already stopped */
131         if (policy->pol_state != NRS_POL_STATE_STARTED)
132                 RETURN(0);
133
134         policy->pol_state = NRS_POL_STATE_STOPPING;
135
136         /* Immediately make it invisible */
137         if (nrs->nrs_policy_primary == policy) {
138                 nrs->nrs_policy_primary = NULL;
139
140         } else {
141                 LASSERT(nrs->nrs_policy_fallback == policy);
142                 nrs->nrs_policy_fallback = NULL;
143         }
144
145         /* I have the only refcount */
146         if (policy->pol_ref == 1)
147                 nrs_policy_stop0(policy);
148
149         RETURN(0);
150 }
151
152 /**
153  * Transitions the \a nrs NRS head's primary policy to
154  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING and if the policy has no
155  * pending usage references, to ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED.
156  *
157  * \param[in] nrs the NRS head to carry out this operation on
158  */
159 static void nrs_policy_stop_primary(struct ptlrpc_nrs *nrs)
160 {
161         struct ptlrpc_nrs_policy *tmp = nrs->nrs_policy_primary;
162         ENTRY;
163
164         if (tmp == NULL) {
165                 /**
166                  * XXX: This should really be RETURN_EXIT, but the latter does
167                  * not currently print anything out, and possibly should be
168                  * fixed to do so.
169                  */
170                 EXIT;
171                 return;
172         }
173
174         nrs->nrs_policy_primary = NULL;
175
176         LASSERT(tmp->pol_state == NRS_POL_STATE_STARTED);
177         tmp->pol_state = NRS_POL_STATE_STOPPING;
178
179         if (tmp->pol_ref == 0)
180                 nrs_policy_stop0(tmp);
181         EXIT;
182 }
183
184 /**
185  * Transitions a policy across the ptlrpc_nrs_pol_state range of values, in
186  * response to an lprocfs command to start a policy.
187  *
188  * If a primary policy different to the current one is specified, this function
189  * will transition the new policy to the
190  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTING and then to
191  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED, and will then transition
192  * the old primary policy (if there is one) to
193  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING, and if there are no outstanding
194  * references on the policy to ptlrpc_nrs_pol_stae::NRS_POL_STATE_STOPPED.
195  *
196  * If the fallback policy is specified, this is taken to indicate an instruction
197  * to stop the current primary policy, without substituting it with another
198  * primary policy, so the primary policy (if any) is transitioned to
199  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING, and if there are no outstanding
200  * references on the policy to ptlrpc_nrs_pol_stae::NRS_POL_STATE_STOPPED. In
201  * this case, the fallback policy is only left active in the NRS head.
202  */
203 static int nrs_policy_start_locked(struct ptlrpc_nrs_policy *policy)
204 {
205         struct ptlrpc_nrs      *nrs = policy->pol_nrs;
206         int                     rc = 0;
207         ENTRY;
208
209         /**
210          * Don't allow multiple starting which is too complex, and has no real
211          * benefit.
212          */
213         if (nrs->nrs_policy_starting)
214                 RETURN(-EAGAIN);
215
216         LASSERT(policy->pol_state != NRS_POL_STATE_STARTING);
217
218         if (policy->pol_state == NRS_POL_STATE_STOPPING)
219                 RETURN(-EAGAIN);
220
221         if (policy->pol_flags & PTLRPC_NRS_FL_FALLBACK) {
222                 /**
223                  * This is for cases in which the user sets the policy to the
224                  * fallback policy (currently fifo for all services); i.e. the
225                  * user is resetting the policy to the default; so we stop the
226                  * primary policy, if any.
227                  */
228                 if (policy == nrs->nrs_policy_fallback) {
229                         nrs_policy_stop_primary(nrs);
230                         RETURN(0);
231                 }
232
233                 /**
234                  * If we reach here, we must be setting up the fallback policy
235                  * at service startup time, and only a single policy with the
236                  * nrs_policy_flags::PTLRPC_NRS_FL_FALLBACK flag set can
237                  * register with NRS core.
238                  */
239                 LASSERT(nrs->nrs_policy_fallback == NULL);
240         } else {
241                 /**
242                  * Shouldn't start primary policy if w/o fallback policy.
243                  */
244                 if (nrs->nrs_policy_fallback == NULL)
245                         RETURN(-EPERM);
246
247                 if (policy->pol_state == NRS_POL_STATE_STARTED)
248                         RETURN(0);
249         }
250
251         /**
252          * Increase the module usage count for policies registering from other
253          * modules.
254          */
255         if (cfs_atomic_inc_return(&policy->pol_desc->pd_refs) == 1 &&
256             !try_module_get(policy->pol_desc->pd_owner)) {
257                 cfs_atomic_dec(&policy->pol_desc->pd_refs);
258                 CERROR("NRS: cannot get module for policy %s; is it alive?\n",
259                        policy->pol_desc->pd_name);
260                 RETURN(-ENODEV);
261         }
262
263         /**
264          * Serialize policy starting across the NRS head
265          */
266         nrs->nrs_policy_starting = 1;
267
268         policy->pol_state = NRS_POL_STATE_STARTING;
269
270         if (policy->pol_desc->pd_ops->op_policy_start) {
271                 spin_unlock(&nrs->nrs_lock);
272
273                 rc = policy->pol_desc->pd_ops->op_policy_start(policy);
274
275                 spin_lock(&nrs->nrs_lock);
276                 if (rc != 0) {
277                         if (cfs_atomic_dec_and_test(&policy->pol_desc->pd_refs))
278                                 module_put(policy->pol_desc->pd_owner);
279
280                         policy->pol_state = NRS_POL_STATE_STOPPED;
281                         GOTO(out, rc);
282                 }
283         }
284
285         policy->pol_state = NRS_POL_STATE_STARTED;
286
287         if (policy->pol_flags & PTLRPC_NRS_FL_FALLBACK) {
288                 /**
289                  * This path is only used at PTLRPC service setup time.
290                  */
291                 nrs->nrs_policy_fallback = policy;
292         } else {
293                 /*
294                  * Try to stop the current primary policy if there is one.
295                  */
296                 nrs_policy_stop_primary(nrs);
297
298                 /**
299                  * And set the newly-started policy as the primary one.
300                  */
301                 nrs->nrs_policy_primary = policy;
302         }
303
304 out:
305         nrs->nrs_policy_starting = 0;
306
307         RETURN(rc);
308 }
309
310 /**
311  * Increases the policy's usage reference count.
312  */
313 static inline void nrs_policy_get_locked(struct ptlrpc_nrs_policy *policy)
314 {
315         policy->pol_ref++;
316 }
317
318 /**
319  * Decreases the policy's usage reference count, and stops the policy in case it
320  * was already stopping and have no more outstanding usage references (which
321  * indicates it has no more queued or started requests, and can be safely
322  * stopped).
323  */
324 static void nrs_policy_put_locked(struct ptlrpc_nrs_policy *policy)
325 {
326         LASSERT(policy->pol_ref > 0);
327
328         policy->pol_ref--;
329         if (unlikely(policy->pol_ref == 0 &&
330             policy->pol_state == NRS_POL_STATE_STOPPING))
331                 nrs_policy_stop0(policy);
332 }
333
334 static void nrs_policy_put(struct ptlrpc_nrs_policy *policy)
335 {
336         spin_lock(&policy->pol_nrs->nrs_lock);
337         nrs_policy_put_locked(policy);
338         spin_unlock(&policy->pol_nrs->nrs_lock);
339 }
340
341 /**
342  * Find and return a policy by name.
343  */
344 static struct ptlrpc_nrs_policy * nrs_policy_find_locked(struct ptlrpc_nrs *nrs,
345                                                          char *name)
346 {
347         struct ptlrpc_nrs_policy *tmp;
348
349         cfs_list_for_each_entry(tmp, &nrs->nrs_policy_list, pol_list) {
350                 if (strncmp(tmp->pol_desc->pd_name, name,
351                             NRS_POL_NAME_MAX) == 0) {
352                         nrs_policy_get_locked(tmp);
353                         return tmp;
354                 }
355         }
356         return NULL;
357 }
358
359 /**
360  * Release references for the resource hierarchy moving upwards towards the
361  * policy instance resource.
362  */
363 static void nrs_resource_put(struct ptlrpc_nrs_resource *res)
364 {
365         struct ptlrpc_nrs_policy *policy = res->res_policy;
366
367         if (policy->pol_desc->pd_ops->op_res_put != NULL) {
368                 struct ptlrpc_nrs_resource *parent;
369
370                 for (; res != NULL; res = parent) {
371                         parent = res->res_parent;
372                         policy->pol_desc->pd_ops->op_res_put(policy, res);
373                 }
374         }
375 }
376
377 /**
378  * Obtains references for each resource in the resource hierarchy for request
379  * \a nrq if it is to be handled by \a policy.
380  *
381  * \param[in] policy      the policy
382  * \param[in] nrq         the request
383  * \param[in] moving_req  denotes whether this is a call to the function by
384  *                        ldlm_lock_reorder_req(), in order to move \a nrq to
385  *                        the high-priority NRS head; we should not sleep when
386  *                        set.
387  *
388  * \retval NULL           resource hierarchy references not obtained
389  * \retval valid-pointer  the bottom level of the resource hierarchy
390  *
391  * \see ptlrpc_nrs_pol_ops::op_res_get()
392  */
393 static
394 struct ptlrpc_nrs_resource * nrs_resource_get(struct ptlrpc_nrs_policy *policy,
395                                               struct ptlrpc_nrs_request *nrq,
396                                               bool moving_req)
397 {
398         /**
399          * Set to NULL to traverse the resource hierarchy from the top.
400          */
401         struct ptlrpc_nrs_resource *res = NULL;
402         struct ptlrpc_nrs_resource *tmp = NULL;
403         int                         rc;
404
405         while (1) {
406                 rc = policy->pol_desc->pd_ops->op_res_get(policy, nrq, res,
407                                                           &tmp, moving_req);
408                 if (rc < 0) {
409                         if (res != NULL)
410                                 nrs_resource_put(res);
411                         return NULL;
412                 }
413
414                 LASSERT(tmp != NULL);
415                 tmp->res_parent = res;
416                 tmp->res_policy = policy;
417                 res = tmp;
418                 tmp = NULL;
419                 /**
420                  * Return once we have obtained a reference to the bottom level
421                  * of the resource hierarchy.
422                  */
423                 if (rc > 0)
424                         return res;
425         }
426 }
427
428 /**
429  * Obtains resources for the resource hierarchies and policy references for
430  * the fallback and current primary policy (if any), that will later be used
431  * to handle request \a nrq.
432  *
433  * \param[in]  nrs  the NRS head instance that will be handling request \a nrq.
434  * \param[in]  nrq  the request that is being handled.
435  * \param[out] resp the array where references to the resource hierarchy are
436  *                  stored.
437  * \param[in]  moving_req  is set when obtaining resources while moving a
438  *                         request from a policy on the regular NRS head to a
439  *                         policy on the HP NRS head (via
440  *                         ldlm_lock_reorder_req()). It signifies that
441  *                         allocations to get resources should be atomic; for
442  *                         a full explanation, see comment in
443  *                         ptlrpc_nrs_pol_ops::op_res_get().
444  */
445 static void nrs_resource_get_safe(struct ptlrpc_nrs *nrs,
446                                   struct ptlrpc_nrs_request *nrq,
447                                   struct ptlrpc_nrs_resource **resp,
448                                   bool moving_req)
449 {
450         struct ptlrpc_nrs_policy   *primary = NULL;
451         struct ptlrpc_nrs_policy   *fallback = NULL;
452
453         memset(resp, 0, sizeof(resp[0]) * NRS_RES_MAX);
454
455         /**
456          * Obtain policy references.
457          */
458         spin_lock(&nrs->nrs_lock);
459
460         fallback = nrs->nrs_policy_fallback;
461         nrs_policy_get_locked(fallback);
462
463         primary = nrs->nrs_policy_primary;
464         if (primary != NULL)
465                 nrs_policy_get_locked(primary);
466
467         spin_unlock(&nrs->nrs_lock);
468
469         /**
470          * Obtain resource hierarchy references.
471          */
472         resp[NRS_RES_FALLBACK] = nrs_resource_get(fallback, nrq, moving_req);
473         LASSERT(resp[NRS_RES_FALLBACK] != NULL);
474
475         if (primary != NULL) {
476                 resp[NRS_RES_PRIMARY] = nrs_resource_get(primary, nrq,
477                                                          moving_req);
478                 /**
479                  * A primary policy may exist which may not wish to serve a
480                  * particular request for different reasons; release the
481                  * reference on the policy as it will not be used for this
482                  * request.
483                  */
484                 if (resp[NRS_RES_PRIMARY] == NULL)
485                         nrs_policy_put(primary);
486         }
487 }
488
489 /**
490  * Releases references to resource hierarchies and policies, because they are no
491  * longer required; used when request handling has been completed, or the
492  * request is moving to the high priority NRS head.
493  *
494  * \param resp  the resource hierarchy that is being released
495  *
496  * \see ptlrpcnrs_req_hp_move()
497  * \see ptlrpc_nrs_req_finalize()
498  */
499 static void nrs_resource_put_safe(struct ptlrpc_nrs_resource **resp)
500 {
501         struct ptlrpc_nrs_policy *pols[NRS_RES_MAX];
502         struct ptlrpc_nrs        *nrs = NULL;
503         int                       i;
504
505         for (i = 0; i < NRS_RES_MAX; i++) {
506                 if (resp[i] != NULL) {
507                         pols[i] = resp[i]->res_policy;
508                         nrs_resource_put(resp[i]);
509                         resp[i] = NULL;
510                 } else {
511                         pols[i] = NULL;
512                 }
513         }
514
515         for (i = 0; i < NRS_RES_MAX; i++) {
516                 if (pols[i] == NULL)
517                         continue;
518
519                 if (nrs == NULL) {
520                         nrs = pols[i]->pol_nrs;
521                         spin_lock(&nrs->nrs_lock);
522                 }
523                 nrs_policy_put_locked(pols[i]);
524         }
525
526         if (nrs != NULL)
527                 spin_unlock(&nrs->nrs_lock);
528 }
529
530 /**
531  * Obtains an NRS request from \a policy for handling or examination; the
532  * request should be removed in the 'handling' case.
533  *
534  * Calling into this function implies we already know the policy has a request
535  * waiting to be handled.
536  *
537  * \param[in] policy the policy from which a request
538  * \param[in] peek   when set, signifies that we just want to examine the
539  *                   request, and not handle it, so the request is not removed
540  *                   from the policy.
541  * \param[in] force  when set, it will force a policy to return a request if it
542  *                   has one pending
543  *
544  * \retval the NRS request to be handled
545  */
546 static inline
547 struct ptlrpc_nrs_request * nrs_request_get(struct ptlrpc_nrs_policy *policy,
548                                             bool peek, bool force)
549 {
550         struct ptlrpc_nrs_request *nrq;
551
552         LASSERT(policy->pol_req_queued > 0);
553
554         nrq = policy->pol_desc->pd_ops->op_req_get(policy, peek, force);
555
556         LASSERT(ergo(nrq != NULL, nrs_request_policy(nrq) == policy));
557
558         return nrq;
559 }
560
561 /**
562  * Enqueues request \a nrq for later handling, via one one the policies for
563  * which resources where earlier obtained via nrs_resource_get_safe(). The
564  * function attempts to enqueue the request first on the primary policy
565  * (if any), since this is the preferred choice.
566  *
567  * \param nrq the request being enqueued
568  *
569  * \see nrs_resource_get_safe()
570  */
571 static inline void nrs_request_enqueue(struct ptlrpc_nrs_request *nrq)
572 {
573         struct ptlrpc_nrs_policy *policy;
574         int                       rc;
575         int                       i;
576
577         /**
578          * Try in descending order, because the primary policy (if any) is
579          * the preferred choice.
580          */
581         for (i = NRS_RES_MAX - 1; i >= 0; i--) {
582                 if (nrq->nr_res_ptrs[i] == NULL)
583                         continue;
584
585                 nrq->nr_res_idx = i;
586                 policy = nrq->nr_res_ptrs[i]->res_policy;
587
588                 rc = policy->pol_desc->pd_ops->op_req_enqueue(policy, nrq);
589                 if (rc == 0) {
590                         policy->pol_nrs->nrs_req_queued++;
591                         policy->pol_req_queued++;
592                         return;
593                 }
594         }
595         /**
596          * Should never get here, as at least the primary policy's
597          * ptlrpc_nrs_pol_ops::op_req_enqueue() implementation should always
598          * succeed.
599          */
600         LBUG();
601 }
602
603 /**
604  * Called when a request has been handled
605  *
606  * \param[in] nrs the request that has been handled; can be used for
607  *                job/resource control.
608  *
609  * \see ptlrpc_nrs_req_stop_nolock()
610  */
611 static inline void nrs_request_stop(struct ptlrpc_nrs_request *nrq)
612 {
613         struct ptlrpc_nrs_policy *policy = nrs_request_policy(nrq);
614
615         if (policy->pol_desc->pd_ops->op_req_stop)
616                 policy->pol_desc->pd_ops->op_req_stop(policy, nrq);
617
618         LASSERT(policy->pol_nrs->nrs_req_started > 0);
619         LASSERT(policy->pol_req_started > 0);
620
621         policy->pol_nrs->nrs_req_started--;
622         policy->pol_req_started--;
623 }
624
625 /**
626  * Handler for operations that can be carried out on policies.
627  *
628  * Handles opcodes that are common to all policy types within NRS core, and
629  * passes any unknown opcodes to the policy-specific control function.
630  *
631  * \param[in]     nrs  the NRS head this policy belongs to.
632  * \param[in]     name the human-readable policy name; should be the same as
633  *                     ptlrpc_nrs_pol_desc::pd_name.
634  * \param[in]     opc  the opcode of the operation being carried out.
635  * \param[in,out] arg  can be used to pass information in and out between when
636  *                     carrying an operation; usually data that is private to
637  *                     the policy at some level, or generic policy status
638  *                     information.
639  *
640  * \retval -ve error condition
641  * \retval   0 operation was carried out successfully
642  */
643 static int nrs_policy_ctl(struct ptlrpc_nrs *nrs, char *name,
644                           enum ptlrpc_nrs_ctl opc, void *arg)
645 {
646         struct ptlrpc_nrs_policy       *policy;
647         int                             rc = 0;
648         ENTRY;
649
650         spin_lock(&nrs->nrs_lock);
651
652         policy = nrs_policy_find_locked(nrs, name);
653         if (policy == NULL)
654                 GOTO(out, rc = -ENOENT);
655
656         switch (opc) {
657                 /**
658                  * Unknown opcode, pass it down to the policy-specific control
659                  * function for handling.
660                  */
661         default:
662                 rc = nrs_policy_ctl_locked(policy, opc, arg);
663                 break;
664
665                 /**
666                  * Start \e policy
667                  */
668         case PTLRPC_NRS_CTL_START:
669                 rc = nrs_policy_start_locked(policy);
670                 break;
671         }
672 out:
673         if (policy != NULL)
674                 nrs_policy_put_locked(policy);
675
676         spin_unlock(&nrs->nrs_lock);
677
678         RETURN(rc);
679 }
680
681 /**
682  * Unregisters a policy by name.
683  *
684  * \param[in] nrs  the NRS head this policy belongs to.
685  * \param[in] name the human-readable policy name; should be the same as
686  *                 ptlrpc_nrs_pol_desc::pd_name
687  *
688  * \retval -ve error
689  * \retval   0 success
690  */
691 static int nrs_policy_unregister(struct ptlrpc_nrs *nrs, char *name)
692 {
693         struct ptlrpc_nrs_policy *policy = NULL;
694         ENTRY;
695
696         spin_lock(&nrs->nrs_lock);
697
698         policy = nrs_policy_find_locked(nrs, name);
699         if (policy == NULL) {
700                 spin_unlock(&nrs->nrs_lock);
701
702                 CERROR("Can't find NRS policy %s\n", name);
703                 RETURN(-ENOENT);
704         }
705
706         if (policy->pol_ref > 1) {
707                 CERROR("Policy %s is busy with %d references\n", name,
708                        (int)policy->pol_ref);
709                 nrs_policy_put_locked(policy);
710
711                 spin_unlock(&nrs->nrs_lock);
712                 RETURN(-EBUSY);
713         }
714
715         LASSERT(policy->pol_req_queued == 0);
716         LASSERT(policy->pol_req_started == 0);
717
718         if (policy->pol_state != NRS_POL_STATE_STOPPED) {
719                 nrs_policy_stop_locked(policy);
720                 LASSERT(policy->pol_state == NRS_POL_STATE_STOPPED);
721         }
722
723         cfs_list_del(&policy->pol_list);
724         nrs->nrs_num_pols--;
725
726         nrs_policy_put_locked(policy);
727
728         spin_unlock(&nrs->nrs_lock);
729
730         nrs_policy_fini(policy);
731
732         LASSERT(policy->pol_private == NULL);
733         OBD_FREE_PTR(policy);
734
735         RETURN(0);
736 }
737
738 /**
739  * Register a policy from \policy descriptor \a desc with NRS head \a nrs.
740  *
741  * \param[in] nrs   the NRS head on which the policy will be registered.
742  * \param[in] desc  the policy descriptor from which the information will be
743  *                  obtained to register the policy.
744  *
745  * \retval -ve error
746  * \retval   0 success
747  */
748 static int nrs_policy_register(struct ptlrpc_nrs *nrs,
749                                struct ptlrpc_nrs_pol_desc *desc)
750 {
751         struct ptlrpc_nrs_policy       *policy;
752         struct ptlrpc_nrs_policy       *tmp;
753         struct ptlrpc_service_part     *svcpt = nrs->nrs_svcpt;
754         int                             rc;
755         ENTRY;
756
757         LASSERT(svcpt != NULL);
758         LASSERT(desc->pd_ops != NULL);
759         LASSERT(desc->pd_ops->op_res_get != NULL);
760         LASSERT(desc->pd_ops->op_req_get != NULL);
761         LASSERT(desc->pd_ops->op_req_enqueue != NULL);
762         LASSERT(desc->pd_ops->op_req_dequeue != NULL);
763         LASSERT(desc->pd_compat != NULL);
764
765         OBD_CPT_ALLOC_GFP(policy, svcpt->scp_service->srv_cptable,
766                           svcpt->scp_cpt, sizeof(*policy), __GFP_IO);
767         if (policy == NULL)
768                 RETURN(-ENOMEM);
769
770         policy->pol_nrs     = nrs;
771         policy->pol_desc    = desc;
772         policy->pol_state   = NRS_POL_STATE_STOPPED;
773         policy->pol_flags   = desc->pd_flags;
774
775         CFS_INIT_LIST_HEAD(&policy->pol_list);
776         CFS_INIT_LIST_HEAD(&policy->pol_list_queued);
777
778         rc = nrs_policy_init(policy);
779         if (rc != 0) {
780                 OBD_FREE_PTR(policy);
781                 RETURN(rc);
782         }
783
784         spin_lock(&nrs->nrs_lock);
785
786         tmp = nrs_policy_find_locked(nrs, policy->pol_desc->pd_name);
787         if (tmp != NULL) {
788                 CERROR("NRS policy %s has been registered, can't register it "
789                        "for %s\n", policy->pol_desc->pd_name,
790                        svcpt->scp_service->srv_name);
791                 nrs_policy_put_locked(tmp);
792
793                 spin_unlock(&nrs->nrs_lock);
794                 nrs_policy_fini(policy);
795                 OBD_FREE_PTR(policy);
796
797                 RETURN(-EEXIST);
798         }
799
800         cfs_list_add_tail(&policy->pol_list, &nrs->nrs_policy_list);
801         nrs->nrs_num_pols++;
802
803         if (policy->pol_flags & PTLRPC_NRS_FL_REG_START)
804                 rc = nrs_policy_start_locked(policy);
805
806         spin_unlock(&nrs->nrs_lock);
807
808         if (rc != 0)
809                 (void) nrs_policy_unregister(nrs, policy->pol_desc->pd_name);
810
811         RETURN(rc);
812 }
813
814 /**
815  * Enqueue request \a req using one of the policies its resources are referring
816  * to.
817  *
818  * \param[in] req the request to enqueue.
819  */
820 static void ptlrpc_nrs_req_add_nolock(struct ptlrpc_request *req)
821 {
822         struct ptlrpc_nrs_policy       *policy;
823
824         LASSERT(req->rq_nrq.nr_initialized);
825         LASSERT(!req->rq_nrq.nr_enqueued);
826
827         nrs_request_enqueue(&req->rq_nrq);
828         req->rq_nrq.nr_enqueued = 1;
829
830         policy = nrs_request_policy(&req->rq_nrq);
831         /**
832          * Add the policy to the NRS head's list of policies with enqueued
833          * requests, if it has not been added there.
834          */
835         if (unlikely(cfs_list_empty(&policy->pol_list_queued)))
836                 cfs_list_add_tail(&policy->pol_list_queued,
837                                   &policy->pol_nrs->nrs_policy_queued);
838 }
839
840 /**
841  * Enqueue a request on the high priority NRS head.
842  *
843  * \param req the request to enqueue.
844  */
845 static void ptlrpc_nrs_hpreq_add_nolock(struct ptlrpc_request *req)
846 {
847         int     opc = lustre_msg_get_opc(req->rq_reqmsg);
848         ENTRY;
849
850         spin_lock(&req->rq_lock);
851         req->rq_hp = 1;
852         ptlrpc_nrs_req_add_nolock(req);
853         if (opc != OBD_PING)
854                 DEBUG_REQ(D_NET, req, "high priority req");
855         spin_unlock(&req->rq_lock);
856         EXIT;
857 }
858
859 /**
860  * Returns a boolean predicate indicating whether the policy described by
861  * \a desc is adequate for use with service \a svc.
862  *
863  * \param[in] svc  the service
864  * \param[in] desc the policy descriptor
865  *
866  * \retval false the policy is not compatible with the service
867  * \retval true  the policy is compatible with the service
868  */
869 static inline bool nrs_policy_compatible(const struct ptlrpc_service *svc,
870                                          const struct ptlrpc_nrs_pol_desc *desc)
871 {
872         return desc->pd_compat(svc, desc);
873 }
874
875 /**
876  * Registers all compatible policies in nrs_core.nrs_policies, for NRS head
877  * \a nrs.
878  *
879  * \param[in] nrs the NRS head
880  *
881  * \retval -ve error
882  * \retval   0 success
883  *
884  * \pre mutex_is_locked(&nrs_core.nrs_mutex)
885  *
886  * \see ptlrpc_service_nrs_setup()
887  */
888 static int nrs_register_policies_locked(struct ptlrpc_nrs *nrs)
889 {
890         struct ptlrpc_nrs_pol_desc *desc;
891         /* for convenience */
892         struct ptlrpc_service_part       *svcpt = nrs->nrs_svcpt;
893         struct ptlrpc_service            *svc = svcpt->scp_service;
894         int                               rc = -EINVAL;
895         ENTRY;
896
897         LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
898
899         cfs_list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
900                 if (nrs_policy_compatible(svc, desc)) {
901                         rc = nrs_policy_register(nrs, desc);
902                         if (rc != 0) {
903                                 CERROR("Failed to register NRS policy %s for "
904                                        "partition %d of service %s: %d\n",
905                                        desc->pd_name, svcpt->scp_cpt,
906                                        svc->srv_name, rc);
907                                 /**
908                                  * Fail registration if any of the policies'
909                                  * registration fails.
910                                  */
911                                 break;
912                         }
913                 }
914         }
915
916         RETURN(rc);
917 }
918
919 /**
920  * Initializes NRS head \a nrs of service partition \a svcpt, and registers all
921  * compatible policies in NRS core, with the NRS head.
922  *
923  * \param[in] nrs   the NRS head
924  * \param[in] svcpt the PTLRPC service partition to setup
925  *
926  * \retval -ve error
927  * \retval   0 success
928  *
929  * \pre mutex_is_locked(&nrs_core.nrs_mutex)
930  */
931 static int nrs_svcpt_setup_locked0(struct ptlrpc_nrs *nrs,
932                                    struct ptlrpc_service_part *svcpt)
933 {
934         int                             rc;
935         enum ptlrpc_nrs_queue_type      queue;
936
937         LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
938
939         if (nrs == &svcpt->scp_nrs_reg)
940                 queue = PTLRPC_NRS_QUEUE_REG;
941         else if (nrs == svcpt->scp_nrs_hp)
942                 queue = PTLRPC_NRS_QUEUE_HP;
943         else
944                 LBUG();
945
946         nrs->nrs_svcpt = svcpt;
947         nrs->nrs_queue_type = queue;
948         spin_lock_init(&nrs->nrs_lock);
949         CFS_INIT_LIST_HEAD(&nrs->nrs_policy_list);
950         CFS_INIT_LIST_HEAD(&nrs->nrs_policy_queued);
951
952         rc = nrs_register_policies_locked(nrs);
953
954         RETURN(rc);
955 }
956
957 /**
958  * Allocates a regular and optionally a high-priority NRS head (if the service
959  * handles high-priority RPCs), and then registers all available compatible
960  * policies on those NRS heads.
961  *
962  * \param[in,out] svcpt the PTLRPC service partition to setup
963  *
964  * \pre mutex_is_locked(&nrs_core.nrs_mutex)
965  */
966 static int nrs_svcpt_setup_locked(struct ptlrpc_service_part *svcpt)
967 {
968         struct ptlrpc_nrs              *nrs;
969         int                             rc;
970         ENTRY;
971
972         LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
973
974         /**
975          * Initialize the regular NRS head.
976          */
977         nrs = nrs_svcpt2nrs(svcpt, false);
978         rc = nrs_svcpt_setup_locked0(nrs, svcpt);
979         if (rc < 0)
980                 GOTO(out, rc);
981
982         /**
983          * Optionally allocate a high-priority NRS head.
984          */
985         if (svcpt->scp_service->srv_ops.so_hpreq_handler == NULL)
986                 GOTO(out, rc);
987
988         OBD_CPT_ALLOC_PTR(svcpt->scp_nrs_hp,
989                           svcpt->scp_service->srv_cptable,
990                           svcpt->scp_cpt);
991         if (svcpt->scp_nrs_hp == NULL)
992                 GOTO(out, rc = -ENOMEM);
993
994         nrs = nrs_svcpt2nrs(svcpt, true);
995         rc = nrs_svcpt_setup_locked0(nrs, svcpt);
996
997 out:
998         RETURN(rc);
999 }
1000
1001 /**
1002  * Unregisters all policies on all available NRS heads in a service partition;
1003  * called at PTLRPC service unregistration time.
1004  *
1005  * \param[in] svcpt the PTLRPC service partition
1006  *
1007  * \pre mutex_is_locked(&nrs_core.nrs_mutex)
1008  */
1009 static void nrs_svcpt_cleanup_locked(struct ptlrpc_service_part *svcpt)
1010 {
1011         struct ptlrpc_nrs              *nrs;
1012         struct ptlrpc_nrs_policy       *policy;
1013         struct ptlrpc_nrs_policy       *tmp;
1014         int                             rc;
1015         bool                            hp = false;
1016         ENTRY;
1017
1018         LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
1019
1020 again:
1021         nrs = nrs_svcpt2nrs(svcpt, hp);
1022         nrs->nrs_stopping = 1;
1023
1024         cfs_list_for_each_entry_safe(policy, tmp, &nrs->nrs_policy_list,
1025                                      pol_list) {
1026                 rc = nrs_policy_unregister(nrs, policy->pol_desc->pd_name);
1027                 LASSERT(rc == 0);
1028         }
1029
1030         /**
1031          * If the service partition has an HP NRS head, clean that up as well.
1032          */
1033         if (!hp && nrs_svcpt_has_hp(svcpt)) {
1034                 hp = true;
1035                 goto again;
1036         }
1037
1038         if (hp)
1039                 OBD_FREE_PTR(nrs);
1040
1041         EXIT;
1042 }
1043
1044 /**
1045  * Returns the descriptor for a policy as identified by by \a name.
1046  *
1047  * \param[in] name the policy name
1048  *
1049  * \retval the policy descriptor
1050  * \retval NULL
1051  */
1052 static struct ptlrpc_nrs_pol_desc *nrs_policy_find_desc_locked(const char *name)
1053 {
1054         struct ptlrpc_nrs_pol_desc     *tmp;
1055         ENTRY;
1056
1057         cfs_list_for_each_entry(tmp, &nrs_core.nrs_policies, pd_list) {
1058                 if (strncmp(tmp->pd_name, name, NRS_POL_NAME_MAX) == 0)
1059                         RETURN(tmp);
1060         }
1061         RETURN(NULL);
1062 }
1063
1064 /**
1065  * Removes the policy from all supported NRS heads of all partitions of all
1066  * PTLRPC services.
1067  *
1068  * \param[in] desc the policy descriptor to unregister
1069  *
1070  * \retval -ve error
1071  * \retval  0  successfully unregistered policy on all supported NRS heads
1072  *
1073  * \pre mutex_is_locked(&nrs_core.nrs_mutex)
1074  * \pre mutex_is_locked(&ptlrpc_all_services_mutex)
1075  */
1076 static int nrs_policy_unregister_locked(struct ptlrpc_nrs_pol_desc *desc)
1077 {
1078         struct ptlrpc_nrs              *nrs;
1079         struct ptlrpc_service          *svc;
1080         struct ptlrpc_service_part     *svcpt;
1081         int                             i;
1082         int                             rc = 0;
1083         ENTRY;
1084
1085         LASSERT(mutex_is_locked(&nrs_core.nrs_mutex));
1086         LASSERT(mutex_is_locked(&ptlrpc_all_services_mutex));
1087
1088         cfs_list_for_each_entry(svc, &ptlrpc_all_services, srv_list) {
1089
1090                 if (!nrs_policy_compatible(svc, desc) ||
1091                     unlikely(svc->srv_is_stopping))
1092                         continue;
1093
1094                 ptlrpc_service_for_each_part(svcpt, i, svc) {
1095                         bool hp = false;
1096
1097 again:
1098                         nrs = nrs_svcpt2nrs(svcpt, hp);
1099                         rc = nrs_policy_unregister(nrs, desc->pd_name);
1100                         /**
1101                          * Ignore -ENOENT as the policy may not have registered
1102                          * successfully on all service partitions.
1103                          */
1104                         if (rc == -ENOENT) {
1105                                 rc = 0;
1106                         } else if (rc != 0) {
1107                                 CERROR("Failed to unregister NRS policy %s for "
1108                                        "partition %d of service %s: %d\n",
1109                                        desc->pd_name, svcpt->scp_cpt,
1110                                        svcpt->scp_service->srv_name, rc);
1111                                 RETURN(rc);
1112                         }
1113
1114                         if (!hp && nrs_svc_has_hp(svc)) {
1115                                 hp = true;
1116                                 goto again;
1117                         }
1118                 }
1119
1120                 if (desc->pd_ops->op_lprocfs_fini != NULL)
1121                         desc->pd_ops->op_lprocfs_fini(svc);
1122         }
1123
1124         RETURN(rc);
1125 }
1126
1127 /**
1128  * Registers a new policy with NRS core.
1129  *
1130  * The function will only succeed if policy registration with all compatible
1131  * service partitions (if any) is successful.
1132  *
1133  * N.B. This function should be called either at ptlrpc module initialization
1134  *      time when registering a policy that ships with NRS core, or in a
1135  *      module's init() function for policies registering from other modules.
1136  *
1137  * \param[in] conf configuration information for the new policy to register
1138  *
1139  * \retval -ve error
1140  * \retval   0 success
1141  */
1142 int ptlrpc_nrs_policy_register(struct ptlrpc_nrs_pol_conf *conf)
1143 {
1144         struct ptlrpc_service          *svc;
1145         struct ptlrpc_nrs_pol_desc     *desc;
1146         int                             rc = 0;
1147         ENTRY;
1148
1149         LASSERT(conf != NULL);
1150         LASSERT(conf->nc_ops != NULL);
1151         LASSERT(conf->nc_compat != NULL);
1152         LASSERT(ergo(conf->nc_compat == nrs_policy_compat_one,
1153                 conf->nc_compat_svc_name != NULL));
1154         LASSERT(ergo((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) != 0,
1155                      conf->nc_owner != NULL));
1156
1157         conf->nc_name[NRS_POL_NAME_MAX - 1] = '\0';
1158
1159         /**
1160          * External policies are not allowed to start immediately upon
1161          * registration, as there is a relatively higher chance that their
1162          * registration might fail. In such a case, some policy instances may
1163          * already have requests queued wen unregistration needs to happen as
1164          * part o cleanup; since there is currently no way to drain requests
1165          * from a policy unless the service is unregistering, we just disallow
1166          * this.
1167          */
1168         if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) &&
1169             (conf->nc_flags & (PTLRPC_NRS_FL_FALLBACK |
1170                                PTLRPC_NRS_FL_REG_START))) {
1171                 CERROR("NRS: failing to register policy %s. Please check "
1172                        "policy flags; external policies cannot act as fallback "
1173                        "policies, or be started immediately upon registration "
1174                        "without interaction with lprocfs\n", conf->nc_name);
1175                 RETURN(-EINVAL);
1176         }
1177
1178         mutex_lock(&nrs_core.nrs_mutex);
1179
1180         if (nrs_policy_find_desc_locked(conf->nc_name) != NULL) {
1181                 CERROR("NRS: failing to register policy %s which has already "
1182                        "been registered with NRS core!\n",
1183                        conf->nc_name);
1184                 GOTO(fail, rc = -EEXIST);
1185         }
1186
1187         OBD_ALLOC_PTR(desc);
1188         if (desc == NULL)
1189                 GOTO(fail, rc = -ENOMEM);
1190
1191         if (strlcpy(desc->pd_name, conf->nc_name, sizeof(desc->pd_name)) >=
1192             sizeof(desc->pd_name)) {
1193                 OBD_FREE_PTR(desc);
1194                 GOTO(fail, rc = -E2BIG);
1195         }
1196         desc->pd_ops             = conf->nc_ops;
1197         desc->pd_compat          = conf->nc_compat;
1198         desc->pd_compat_svc_name = conf->nc_compat_svc_name;
1199         if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) != 0)
1200                 desc->pd_owner   = conf->nc_owner;
1201         desc->pd_flags           = conf->nc_flags;
1202         cfs_atomic_set(&desc->pd_refs, 0);
1203
1204         /**
1205          * For policies that are held in the same module as NRS (currently
1206          * ptlrpc), do not register the policy with all compatible services,
1207          * as the services will not have started at this point, since we are
1208          * calling from ptlrpc module initialization code. In such cases each
1209          * service will register all compatible policies later, via
1210          * ptlrpc_service_nrs_setup().
1211          */
1212         if ((conf->nc_flags & PTLRPC_NRS_FL_REG_EXTERN) == 0)
1213                 goto internal;
1214
1215         /**
1216          * Register the new policy on all compatible services
1217          */
1218         mutex_lock(&ptlrpc_all_services_mutex);
1219
1220         cfs_list_for_each_entry(svc, &ptlrpc_all_services, srv_list) {
1221                 struct ptlrpc_service_part     *svcpt;
1222                 int                             i;
1223                 int                             rc2;
1224
1225                 if (!nrs_policy_compatible(svc, desc) ||
1226                     unlikely(svc->srv_is_stopping))
1227                         continue;
1228
1229                 ptlrpc_service_for_each_part(svcpt, i, svc) {
1230                         struct ptlrpc_nrs      *nrs;
1231                         bool                    hp = false;
1232 again:
1233                         nrs = nrs_svcpt2nrs(svcpt, hp);
1234                         rc = nrs_policy_register(nrs, desc);
1235                         if (rc != 0) {
1236                                 CERROR("Failed to register NRS policy %s for "
1237                                        "partition %d of service %s: %d\n",
1238                                        desc->pd_name, svcpt->scp_cpt,
1239                                        svcpt->scp_service->srv_name, rc);
1240
1241                                 rc2 = nrs_policy_unregister_locked(desc);
1242                                 /**
1243                                  * Should not fail at this point
1244                                  */
1245                                 LASSERT(rc2 == 0);
1246                                 mutex_unlock(&ptlrpc_all_services_mutex);
1247                                 OBD_FREE_PTR(desc);
1248                                 GOTO(fail, rc);
1249                         }
1250
1251                         if (!hp && nrs_svc_has_hp(svc)) {
1252                                 hp = true;
1253                                 goto again;
1254                         }
1255                 }
1256
1257                 /**
1258                  * No need to take a reference to other modules here, as we
1259                  * will be calling from the module's init() function.
1260                  */
1261                 if (desc->pd_ops->op_lprocfs_init != NULL) {
1262                         rc = desc->pd_ops->op_lprocfs_init(svc);
1263                         if (rc != 0) {
1264                                 rc2 = nrs_policy_unregister_locked(desc);
1265                                 /**
1266                                  * Should not fail at this point
1267                                  */
1268                                 LASSERT(rc2 == 0);
1269                                 mutex_unlock(&ptlrpc_all_services_mutex);
1270                                 OBD_FREE_PTR(desc);
1271                                 GOTO(fail, rc);
1272                         }
1273                 }
1274         }
1275
1276         mutex_unlock(&ptlrpc_all_services_mutex);
1277 internal:
1278         cfs_list_add_tail(&desc->pd_list, &nrs_core.nrs_policies);
1279 fail:
1280         mutex_unlock(&nrs_core.nrs_mutex);
1281
1282         RETURN(rc);
1283 }
1284 EXPORT_SYMBOL(ptlrpc_nrs_policy_register);
1285
1286 /**
1287  * Unregisters a previously registered policy with NRS core. All instances of
1288  * the policy on all NRS heads of all supported services are removed.
1289  *
1290  * N.B. This function should only be called from a module's exit() function.
1291  *      Although it can be used for policies that ship alongside NRS core, the
1292  *      function is primarily intended for policies that register externally,
1293  *      from other modules.
1294  *
1295  * \param[in] conf configuration information for the policy to unregister
1296  *
1297  * \retval -ve error
1298  * \retval   0 success
1299  */
1300 int ptlrpc_nrs_policy_unregister(struct ptlrpc_nrs_pol_conf *conf)
1301 {
1302         struct ptlrpc_nrs_pol_desc      *desc;
1303         int                              rc;
1304         ENTRY;
1305
1306         LASSERT(conf != NULL);
1307
1308         if (conf->nc_flags & PTLRPC_NRS_FL_FALLBACK) {
1309                 CERROR("Unable to unregister a fallback policy, unless the "
1310                        "PTLRPC service is stopping.\n");
1311                 RETURN(-EPERM);
1312         }
1313
1314         conf->nc_name[NRS_POL_NAME_MAX - 1] = '\0';
1315
1316         mutex_lock(&nrs_core.nrs_mutex);
1317
1318         desc = nrs_policy_find_desc_locked(conf->nc_name);
1319         if (desc == NULL) {
1320                 CERROR("Failing to unregister NRS policy %s which has "
1321                        "not been registered with NRS core!\n",
1322                        conf->nc_name);
1323                 GOTO(not_exist, rc = -ENOENT);
1324         }
1325
1326         mutex_lock(&ptlrpc_all_services_mutex);
1327
1328         rc = nrs_policy_unregister_locked(desc);
1329         if (rc < 0) {
1330                 if (rc == -EBUSY)
1331                         CERROR("Please first stop policy %s on all service "
1332                                "partitions and then retry to unregister the "
1333                                "policy.\n", conf->nc_name);
1334                 GOTO(fail, rc);
1335         }
1336
1337         CDEBUG(D_INFO, "Unregistering policy %s from NRS core.\n",
1338                conf->nc_name);
1339
1340         cfs_list_del(&desc->pd_list);
1341         OBD_FREE_PTR(desc);
1342
1343 fail:
1344         mutex_unlock(&ptlrpc_all_services_mutex);
1345
1346 not_exist:
1347         mutex_unlock(&nrs_core.nrs_mutex);
1348
1349         RETURN(rc);
1350 }
1351 EXPORT_SYMBOL(ptlrpc_nrs_policy_unregister);
1352
1353 /**
1354  * Setup NRS heads on all service partitions of service \a svc, and register
1355  * all compatible policies on those NRS heads.
1356  *
1357  * To be called from withing ptl
1358  * \param[in] svc the service to setup
1359  *
1360  * \retval -ve error, the calling logic should eventually call
1361  *                    ptlrpc_service_nrs_cleanup() to undo any work performed
1362  *                    by this function.
1363  *
1364  * \see ptlrpc_register_service()
1365  * \see ptlrpc_service_nrs_cleanup()
1366  */
1367 int ptlrpc_service_nrs_setup(struct ptlrpc_service *svc)
1368 {
1369         struct ptlrpc_service_part             *svcpt;
1370         const struct ptlrpc_nrs_pol_desc       *desc;
1371         int                                     i;
1372         int                                     rc = 0;
1373
1374         mutex_lock(&nrs_core.nrs_mutex);
1375
1376         /**
1377          * Initialize NRS heads on all service CPTs.
1378          */
1379         ptlrpc_service_for_each_part(svcpt, i, svc) {
1380                 rc = nrs_svcpt_setup_locked(svcpt);
1381                 if (rc != 0)
1382                         GOTO(failed, rc);
1383         }
1384
1385         /**
1386          * Set up lprocfs interfaces for all supported policies for the
1387          * service.
1388          */
1389         cfs_list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
1390                 if (!nrs_policy_compatible(svc, desc))
1391                         continue;
1392
1393                 if (desc->pd_ops->op_lprocfs_init != NULL) {
1394                         rc = desc->pd_ops->op_lprocfs_init(svc);
1395                         if (rc != 0)
1396                                 GOTO(failed, rc);
1397                 }
1398         }
1399
1400 failed:
1401
1402         mutex_unlock(&nrs_core.nrs_mutex);
1403
1404         RETURN(rc);
1405 }
1406
1407 /**
1408  * Unregisters all policies on all service partitions of service \a svc.
1409  *
1410  * \param[in] svc the PTLRPC service to unregister
1411  */
1412 void ptlrpc_service_nrs_cleanup(struct ptlrpc_service *svc)
1413 {
1414         struct ptlrpc_service_part           *svcpt;
1415         const struct ptlrpc_nrs_pol_desc     *desc;
1416         int                                   i;
1417
1418         mutex_lock(&nrs_core.nrs_mutex);
1419
1420         /**
1421          * Clean up NRS heads on all service partitions
1422          */
1423         ptlrpc_service_for_each_part(svcpt, i, svc)
1424                 nrs_svcpt_cleanup_locked(svcpt);
1425
1426         /**
1427          * Clean up lprocfs interfaces for all supported policies for the
1428          * service.
1429          */
1430         cfs_list_for_each_entry(desc, &nrs_core.nrs_policies, pd_list) {
1431                 if (!nrs_policy_compatible(svc, desc))
1432                         continue;
1433
1434                 if (desc->pd_ops->op_lprocfs_fini != NULL)
1435                         desc->pd_ops->op_lprocfs_fini(svc);
1436         }
1437
1438         mutex_unlock(&nrs_core.nrs_mutex);
1439 }
1440
1441 /**
1442  * Obtains NRS head resources for request \a req.
1443  *
1444  * These could be either on the regular or HP NRS head of \a svcpt; resources
1445  * taken on the regular head can later be swapped for HP head resources by
1446  * ldlm_lock_reorder_req().
1447  *
1448  * \param[in] svcpt the service partition
1449  * \param[in] req   the request
1450  * \param[in] hp    which NRS head of \a svcpt to use
1451  */
1452 void ptlrpc_nrs_req_initialize(struct ptlrpc_service_part *svcpt,
1453                                struct ptlrpc_request *req, bool hp)
1454 {
1455         struct ptlrpc_nrs       *nrs = nrs_svcpt2nrs(svcpt, hp);
1456
1457         memset(&req->rq_nrq, 0, sizeof(req->rq_nrq));
1458         nrs_resource_get_safe(nrs, &req->rq_nrq, req->rq_nrq.nr_res_ptrs,
1459                               false);
1460
1461         /**
1462          * It is fine to access \e nr_initialized without locking as there is
1463          * no contention at this early stage.
1464          */
1465         req->rq_nrq.nr_initialized = 1;
1466 }
1467
1468 /**
1469  * Releases resources for a request; is called after the request has been
1470  * handled.
1471  *
1472  * \param[in] req the request
1473  *
1474  * \see ptlrpc_server_finish_request()
1475  */
1476 void ptlrpc_nrs_req_finalize(struct ptlrpc_request *req)
1477 {
1478         if (req->rq_nrq.nr_initialized) {
1479                 nrs_resource_put_safe(req->rq_nrq.nr_res_ptrs);
1480                 /* no protection on bit nr_initialized because no
1481                  * contention at this late stage */
1482                 req->rq_nrq.nr_finalized = 1;
1483         }
1484 }
1485
1486 void ptlrpc_nrs_req_stop_nolock(struct ptlrpc_request *req)
1487 {
1488         if (req->rq_nrq.nr_started)
1489                 nrs_request_stop(&req->rq_nrq);
1490 }
1491
1492 /**
1493  * Enqueues request \a req on either the regular or high-priority NRS head
1494  * of service partition \a svcpt.
1495  *
1496  * \param[in] svcpt the service partition
1497  * \param[in] req   the request to be enqueued
1498  * \param[in] hp    whether to enqueue the request on the regular or
1499  *                  high-priority NRS head.
1500  */
1501 void ptlrpc_nrs_req_add(struct ptlrpc_service_part *svcpt,
1502                         struct ptlrpc_request *req, bool hp)
1503 {
1504         spin_lock(&svcpt->scp_req_lock);
1505
1506         if (hp)
1507                 ptlrpc_nrs_hpreq_add_nolock(req);
1508         else
1509                 ptlrpc_nrs_req_add_nolock(req);
1510
1511         spin_unlock(&svcpt->scp_req_lock);
1512 }
1513
1514 static void nrs_request_removed(struct ptlrpc_nrs_policy *policy)
1515 {
1516         LASSERT(policy->pol_nrs->nrs_req_queued > 0);
1517         LASSERT(policy->pol_req_queued > 0);
1518
1519         policy->pol_nrs->nrs_req_queued--;
1520         policy->pol_req_queued--;
1521
1522         /**
1523          * If the policy has no more requests queued, remove it from
1524          * ptlrpc_nrs::nrs_policy_queued.
1525          */
1526         if (unlikely(policy->pol_req_queued == 0)) {
1527                 cfs_list_del_init(&policy->pol_list_queued);
1528
1529                 /**
1530                  * If there are other policies with queued requests, move the
1531                  * current policy to the end so that we can round robin over
1532                  * all policies and drain the requests.
1533                  */
1534         } else if (policy->pol_req_queued != policy->pol_nrs->nrs_req_queued) {
1535                 LASSERT(policy->pol_req_queued <
1536                         policy->pol_nrs->nrs_req_queued);
1537
1538                 cfs_list_move_tail(&policy->pol_list_queued,
1539                                    &policy->pol_nrs->nrs_policy_queued);
1540         }
1541 }
1542
1543 /**
1544  * Obtains a request for handling from an NRS head of service partition
1545  * \a svcpt.
1546  *
1547  * \param[in] svcpt the service partition
1548  * \param[in] hp    whether to obtain a request from the regular or
1549  *                  high-priority NRS head.
1550  * \param[in] peek  when set, signifies that we just want to examine the
1551  *                  request, and not handle it, so the request is not removed
1552  *                  from the policy.
1553  * \param[in] force when set, it will force a policy to return a request if it
1554  *                  has one pending
1555  *
1556  * \retval the  request to be handled
1557  * \retval NULL the head has no requests to serve
1558  */
1559 struct ptlrpc_request *
1560 ptlrpc_nrs_req_get_nolock0(struct ptlrpc_service_part *svcpt, bool hp,
1561                            bool peek, bool force)
1562 {
1563         struct ptlrpc_nrs         *nrs = nrs_svcpt2nrs(svcpt, hp);
1564         struct ptlrpc_nrs_policy  *policy;
1565         struct ptlrpc_nrs_request *nrq;
1566
1567         /**
1568          * Always try to drain requests from all NRS polices even if they are
1569          * inactive, because the user can change policy status at runtime.
1570          */
1571         cfs_list_for_each_entry(policy, &nrs->nrs_policy_queued,
1572                                 pol_list_queued) {
1573                 nrq = nrs_request_get(policy, peek, force);
1574                 if (nrq != NULL) {
1575                         if (likely(!peek)) {
1576                                 nrq->nr_started = 1;
1577
1578                                 policy->pol_req_started++;
1579                                 policy->pol_nrs->nrs_req_started++;
1580
1581                                 nrs_request_removed(policy);
1582                         }
1583
1584                         return container_of(nrq, struct ptlrpc_request, rq_nrq);
1585                 }
1586         }
1587
1588         return NULL;
1589 }
1590
1591 /**
1592  * Dequeues request \a req from the policy it has been enqueued on.
1593  *
1594  * \param[in] req the request
1595  */
1596 void ptlrpc_nrs_req_del_nolock(struct ptlrpc_request *req)
1597 {
1598         struct ptlrpc_nrs_policy *policy = nrs_request_policy(&req->rq_nrq);
1599
1600         policy->pol_desc->pd_ops->op_req_dequeue(policy, &req->rq_nrq);
1601
1602         req->rq_nrq.nr_enqueued = 0;
1603
1604         nrs_request_removed(policy);
1605 }
1606
1607 /**
1608  * Returns whether there are any requests currently enqueued on any of the
1609  * policies of service partition's \a svcpt NRS head specified by \a hp. Should
1610  * be called while holding ptlrpc_service_part::scp_req_lock to get a reliable
1611  * result.
1612  *
1613  * \param[in] svcpt the service partition to enquire.
1614  * \param[in] hp    whether the regular or high-priority NRS head is to be
1615  *                  enquired.
1616  *
1617  * \retval false the indicated NRS head has no enqueued requests.
1618  * \retval true  the indicated NRS head has some enqueued requests.
1619  */
1620 bool ptlrpc_nrs_req_pending_nolock(struct ptlrpc_service_part *svcpt, bool hp)
1621 {
1622         struct ptlrpc_nrs *nrs = nrs_svcpt2nrs(svcpt, hp);
1623
1624         return nrs->nrs_req_queued > 0;
1625 };
1626
1627 /**
1628  * Moves request \a req from the regular to the high-priority NRS head.
1629  *
1630  * \param[in] req the request to move
1631  */
1632 void ptlrpc_nrs_req_hp_move(struct ptlrpc_request *req)
1633 {
1634         struct ptlrpc_service_part      *svcpt = req->rq_rqbd->rqbd_svcpt;
1635         struct ptlrpc_nrs_request       *nrq = &req->rq_nrq;
1636         struct ptlrpc_nrs_resource      *res1[NRS_RES_MAX];
1637         struct ptlrpc_nrs_resource      *res2[NRS_RES_MAX];
1638         ENTRY;
1639
1640         /**
1641          * Obtain the high-priority NRS head resources.
1642          */
1643         nrs_resource_get_safe(nrs_svcpt2nrs(svcpt, true), nrq, res1, true);
1644
1645         spin_lock(&svcpt->scp_req_lock);
1646
1647         if (!ptlrpc_nrs_req_can_move(req))
1648                 goto out;
1649
1650         ptlrpc_nrs_req_del_nolock(req);
1651
1652         memcpy(res2, nrq->nr_res_ptrs, NRS_RES_MAX * sizeof(res2[0]));
1653         memcpy(nrq->nr_res_ptrs, res1, NRS_RES_MAX * sizeof(res1[0]));
1654
1655         ptlrpc_nrs_hpreq_add_nolock(req);
1656
1657         memcpy(res1, res2, NRS_RES_MAX * sizeof(res1[0]));
1658 out:
1659         spin_unlock(&svcpt->scp_req_lock);
1660
1661         /**
1662          * Release either the regular NRS head resources if we moved the
1663          * request, or the high-priority NRS head resources if we took a
1664          * reference earlier in this function and ptlrpc_nrs_req_can_move()
1665          * returned false.
1666          */
1667         nrs_resource_put_safe(res1);
1668         EXIT;
1669 }
1670
1671 /**
1672  * Carries out a control operation \a opc on the policy identified by the
1673  * human-readable \a name, on either all partitions, or only on the first
1674  * partition of service \a svc.
1675  *
1676  * \param[in]     svc    the service the policy belongs to.
1677  * \param[in]     queue  whether to carry out the command on the policy which
1678  *                       belongs to the regular, high-priority, or both NRS
1679  *                       heads of service partitions of \a svc.
1680  * \param[in]     name   the policy to act upon, by human-readable name
1681  * \param[in]     opc    the opcode of the operation to carry out
1682  * \param[in]     single when set, the operation will only be carried out on the
1683  *                       NRS heads of the first service partition of \a svc.
1684  *                       This is useful for some policies which e.g. share
1685  *                       identical values on the same parameters of different
1686  *                       service partitions; when reading these parameters via
1687  *                       lprocfs, these policies may just want to obtain and
1688  *                       print out the values from the first service partition.
1689  *                       Storing these values centrally elsewhere then could be
1690  *                       another solution for this.
1691  * \param[in,out] arg    can be used as a generic in/out buffer between control
1692  *                       operations and the user environment.
1693  *
1694  *\retval -ve error condition
1695  *\retval   0 operation was carried out successfully
1696  */
1697 int ptlrpc_nrs_policy_control(const struct ptlrpc_service *svc,
1698                               enum ptlrpc_nrs_queue_type queue, char *name,
1699                               enum ptlrpc_nrs_ctl opc, bool single, void *arg)
1700 {
1701         struct ptlrpc_service_part     *svcpt;
1702         int                             i;
1703         int                             rc = 0;
1704         ENTRY;
1705
1706         LASSERT(opc != PTLRPC_NRS_CTL_INVALID);
1707
1708         if ((queue & PTLRPC_NRS_QUEUE_BOTH) == 0)
1709                 return -EINVAL;
1710
1711         ptlrpc_service_for_each_part(svcpt, i, svc) {
1712                 if ((queue & PTLRPC_NRS_QUEUE_REG) != 0) {
1713                         rc = nrs_policy_ctl(nrs_svcpt2nrs(svcpt, false), name,
1714                                             opc, arg);
1715                         if (rc != 0 || (queue == PTLRPC_NRS_QUEUE_REG &&
1716                                         single))
1717                                 GOTO(out, rc);
1718                 }
1719
1720                 if ((queue & PTLRPC_NRS_QUEUE_HP) != 0) {
1721                         /**
1722                          * XXX: We could optionally check for
1723                          * nrs_svc_has_hp(svc) here, and return an error if it
1724                          * is false. Right now we rely on the policies' lprocfs
1725                          * handlers that call the present function to make this
1726                          * check; if they fail to do so, they might hit the
1727                          * assertion inside nrs_svcpt2nrs() below.
1728                          */
1729                         rc = nrs_policy_ctl(nrs_svcpt2nrs(svcpt, true), name,
1730                                             opc, arg);
1731                         if (rc != 0 || single)
1732                                 GOTO(out, rc);
1733                 }
1734         }
1735 out:
1736         RETURN(rc);
1737 }
1738
1739
1740 /* ptlrpc/nrs_fifo.c */
1741 extern struct ptlrpc_nrs_pol_conf nrs_conf_fifo;
1742 #if defined HAVE_SERVER_SUPPORT && defined(__KERNEL__)
1743 /* ptlrpc/nrs_crr.c */
1744 extern struct ptlrpc_nrs_pol_conf nrs_conf_crrn;
1745 /* ptlrpc/nrs_orr.c */
1746 extern struct ptlrpc_nrs_pol_conf nrs_conf_orr;
1747 extern struct ptlrpc_nrs_pol_conf nrs_conf_trr;
1748 #endif
1749
1750 /**
1751  * Adds all policies that ship with the ptlrpc module, to NRS core's list of
1752  * policies \e nrs_core.nrs_policies.
1753  *
1754  * \retval 0 all policies have been registered successfully
1755  * \retval -ve error
1756  */
1757 int ptlrpc_nrs_init(void)
1758 {
1759         int     rc;
1760         ENTRY;
1761
1762         mutex_init(&nrs_core.nrs_mutex);
1763         CFS_INIT_LIST_HEAD(&nrs_core.nrs_policies);
1764
1765         rc = ptlrpc_nrs_policy_register(&nrs_conf_fifo);
1766         if (rc != 0)
1767                 GOTO(fail, rc);
1768
1769 #if defined HAVE_SERVER_SUPPORT && defined(__KERNEL__)
1770         rc = ptlrpc_nrs_policy_register(&nrs_conf_crrn);
1771         if (rc != 0)
1772                 GOTO(fail, rc);
1773
1774         rc = ptlrpc_nrs_policy_register(&nrs_conf_orr);
1775         if (rc != 0)
1776                 GOTO(fail, rc);
1777
1778         rc = ptlrpc_nrs_policy_register(&nrs_conf_trr);
1779         if (rc != 0)
1780                 GOTO(fail, rc);
1781 #endif
1782
1783         RETURN(rc);
1784 fail:
1785         /**
1786          * Since no PTLRPC services have been started at this point, all we need
1787          * to do for cleanup is to free the descriptors.
1788          */
1789         ptlrpc_nrs_fini();
1790
1791         RETURN(rc);
1792 }
1793
1794 /**
1795  * Removes all policy desciptors from nrs_core::nrs_policies, and frees the
1796  * policy descriptors.
1797  *
1798  * Since all PTLRPC services are stopped at this point, there are no more
1799  * instances of any policies, because each service will have stopped its policy
1800  * instances in ptlrpc_service_nrs_cleanup(), so we just need to free the
1801  * descriptors here.
1802  */
1803 void ptlrpc_nrs_fini(void)
1804 {
1805         struct ptlrpc_nrs_pol_desc *desc;
1806         struct ptlrpc_nrs_pol_desc *tmp;
1807
1808         cfs_list_for_each_entry_safe(desc, tmp, &nrs_core.nrs_policies,
1809                                      pd_list) {
1810                 cfs_list_del_init(&desc->pd_list);
1811                 OBD_FREE_PTR(desc);
1812         }
1813 }
1814
1815 /** @} nrs */