Whamcloud - gitweb
LU-14291 ptlrpc: format UPDATE messages in server-only code
[fs/lustre-release.git] / lustre / ptlrpc / nrs_crr.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) 2013, 2017, Intel Corporation.
24  *
25  * Copyright 2012 Xyratex Technology Limited
26  */
27 /*
28  * lustre/ptlrpc/nrs_crr.c
29  *
30  * Network Request Scheduler (NRS) CRR-N policy
31  *
32  * Request ordering in a batched Round-Robin manner over client NIDs
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 #include <obd_support.h>
44 #include <obd_class.h>
45 #include <lustre_net.h>
46 #include <lprocfs_status.h>
47 #include "ptlrpc_internal.h"
48
49 /**
50  * \name CRR-N policy
51  *
52  * Client Round-Robin scheduling over client NIDs
53  *
54  * @{
55  *
56  */
57
58 #define NRS_POL_NAME_CRRN       "crrn"
59
60 /**
61  * Binary heap predicate.
62  *
63  * Uses ptlrpc_nrs_request::nr_u::crr::cr_round and
64  * ptlrpc_nrs_request::nr_u::crr::cr_sequence to compare two binheap nodes and
65  * produce a binary predicate that shows their relative priority, so that the
66  * binary heap can perform the necessary sorting operations.
67  *
68  * \param[in] e1 the first binheap node to compare
69  * \param[in] e2 the second binheap node to compare
70  *
71  * \retval 0 e1 > e2
72  * \retval 1 e1 <= e2
73  */
74 static int
75 crrn_req_compare(struct binheap_node *e1, struct binheap_node *e2)
76 {
77         struct ptlrpc_nrs_request *nrq1;
78         struct ptlrpc_nrs_request *nrq2;
79
80         nrq1 = container_of(e1, struct ptlrpc_nrs_request, nr_node);
81         nrq2 = container_of(e2, struct ptlrpc_nrs_request, nr_node);
82
83         if (nrq1->nr_u.crr.cr_round < nrq2->nr_u.crr.cr_round)
84                 return 1;
85         else if (nrq1->nr_u.crr.cr_round > nrq2->nr_u.crr.cr_round)
86                 return 0;
87
88         return nrq1->nr_u.crr.cr_sequence < nrq2->nr_u.crr.cr_sequence;
89 }
90
91 static struct binheap_ops nrs_crrn_heap_ops = {
92         .hop_enter      = NULL,
93         .hop_exit       = NULL,
94         .hop_compare    = crrn_req_compare,
95 };
96
97 /**
98  * rhashtable operations for nrs_crrn_net::cn_cli_hash
99  *
100  * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
101  * nrs_crrn_client objects.
102  */
103 static u32 nrs_crrn_hashfn(const void *data, u32 len, u32 seed)
104 {
105         const lnet_nid_t *nid = data;
106
107         seed ^= cfs_hash_64((u64)nid, 32);
108         return seed;
109 }
110
111 static int nrs_crrn_cmpfn(struct rhashtable_compare_arg *arg, const void *obj)
112 {
113         const struct nrs_crrn_client *cli = obj;
114         const lnet_nid_t *nid = arg->key;
115
116         return *nid != cli->cc_nid;
117 }
118
119 static const struct rhashtable_params nrs_crrn_hash_params = {
120         .key_len        = sizeof(lnet_nid_t),
121         .key_offset     = offsetof(struct nrs_crrn_client, cc_nid),
122         .head_offset    = offsetof(struct nrs_crrn_client, cc_rhead),
123         .hashfn         = nrs_crrn_hashfn,
124         .obj_cmpfn      = nrs_crrn_cmpfn,
125 };
126
127 static void nrs_crrn_exit(void *vcli, void *data)
128 {
129         struct nrs_crrn_client *cli = vcli;
130
131         LASSERTF(atomic_read(&cli->cc_ref) == 0,
132                  "Busy CRR-N object from client with NID %s, with %d refs\n",
133                  libcfs_nid2str(cli->cc_nid), atomic_read(&cli->cc_ref));
134
135         OBD_FREE_PTR(cli);
136 }
137
138 /**
139  * Called when a CRR-N policy instance is started.
140  *
141  * \param[in] policy the policy
142  *
143  * \retval -ENOMEM OOM error
144  * \retval 0       success
145  */
146 static int nrs_crrn_start(struct ptlrpc_nrs_policy *policy, char *arg)
147 {
148         struct nrs_crrn_net    *net;
149         int                     rc = 0;
150         ENTRY;
151
152         OBD_CPT_ALLOC_PTR(net, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
153         if (net == NULL)
154                 RETURN(-ENOMEM);
155
156         net->cn_binheap = binheap_create(&nrs_crrn_heap_ops,
157                                              CBH_FLAG_ATOMIC_GROW, 4096, NULL,
158                                              nrs_pol2cptab(policy),
159                                              nrs_pol2cptid(policy));
160         if (net->cn_binheap == NULL)
161                 GOTO(out_net, rc = -ENOMEM);
162
163         rc = rhashtable_init(&net->cn_cli_hash, &nrs_crrn_hash_params);
164         if (rc)
165                 GOTO(out_binheap, rc);
166
167         /**
168          * Set default quantum value to max_rpcs_in_flight for non-MDS OSCs;
169          * there may be more RPCs pending from each struct nrs_crrn_client even
170          * with the default max_rpcs_in_flight value, as we are scheduling over
171          * NIDs, and there may be more than one mount point per client.
172          */
173         net->cn_quantum = OBD_MAX_RIF_DEFAULT;
174         /**
175          * Set to 1 so that the test inside nrs_crrn_req_add() can evaluate to
176          * true.
177          */
178         net->cn_sequence = 1;
179
180         policy->pol_private = net;
181
182         RETURN(rc);
183
184 out_binheap:
185         binheap_destroy(net->cn_binheap);
186 out_net:
187         OBD_FREE_PTR(net);
188
189         RETURN(rc);
190 }
191
192 /**
193  * Called when a CRR-N policy instance is stopped.
194  *
195  * Called when the policy has been instructed to transition to the
196  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state and has no more pending
197  * requests to serve.
198  *
199  * \param[in] policy the policy
200  */
201 static void nrs_crrn_stop(struct ptlrpc_nrs_policy *policy)
202 {
203         struct nrs_crrn_net     *net = policy->pol_private;
204         ENTRY;
205
206         LASSERT(net != NULL);
207         LASSERT(net->cn_binheap != NULL);
208         LASSERT(binheap_is_empty(net->cn_binheap));
209
210         rhashtable_free_and_destroy(&net->cn_cli_hash, nrs_crrn_exit, NULL);
211         binheap_destroy(net->cn_binheap);
212
213         OBD_FREE_PTR(net);
214 }
215
216 /**
217  * Performs a policy-specific ctl function on CRR-N policy instances; similar
218  * to ioctl.
219  *
220  * \param[in]     policy the policy instance
221  * \param[in]     opc    the opcode
222  * \param[in,out] arg    used for passing parameters and information
223  *
224  * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
225  * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
226  *
227  * \retval 0   operation carried out successfully
228  * \retval -ve error
229  */
230 static int nrs_crrn_ctl(struct ptlrpc_nrs_policy *policy,
231                         enum ptlrpc_nrs_ctl opc,
232                         void *arg)
233 {
234         assert_spin_locked(&policy->pol_nrs->nrs_lock);
235
236         switch((enum nrs_ctl_crr)opc) {
237         default:
238                 RETURN(-EINVAL);
239
240         /**
241          * Read Round Robin quantum size of a policy instance.
242          */
243         case NRS_CTL_CRRN_RD_QUANTUM: {
244                 struct nrs_crrn_net     *net = policy->pol_private;
245
246                 *(__u16 *)arg = net->cn_quantum;
247                 }
248                 break;
249
250         /**
251          * Write Round Robin quantum size of a policy instance.
252          */
253         case NRS_CTL_CRRN_WR_QUANTUM: {
254                 struct nrs_crrn_net     *net = policy->pol_private;
255
256                 net->cn_quantum = *(__u16 *)arg;
257                 LASSERT(net->cn_quantum != 0);
258                 }
259                 break;
260         }
261
262         RETURN(0);
263 }
264
265 /**
266  * Obtains resources from CRR-N policy instances. The top-level resource lives
267  * inside \e nrs_crrn_net and the second-level resource inside
268  * \e nrs_crrn_client object instances.
269  *
270  * \param[in]  policy     the policy for which resources are being taken for
271  *                        request \a nrq
272  * \param[in]  nrq        the request for which resources are being taken
273  * \param[in]  parent     parent resource, embedded in nrs_crrn_net for the
274  *                        CRR-N policy
275  * \param[out] resp       resources references are placed in this array
276  * \param[in]  moving_req signifies limited caller context; used to perform
277  *                        memory allocations in an atomic context in this
278  *                        policy
279  *
280  * \retval 0   we are returning a top-level, parent resource, one that is
281  *             embedded in an nrs_crrn_net object
282  * \retval 1   we are returning a bottom-level resource, one that is embedded
283  *             in an nrs_crrn_client object
284  *
285  * \see nrs_resource_get_safe()
286  */
287 static int nrs_crrn_res_get(struct ptlrpc_nrs_policy *policy,
288                             struct ptlrpc_nrs_request *nrq,
289                             const struct ptlrpc_nrs_resource *parent,
290                             struct ptlrpc_nrs_resource **resp, bool moving_req)
291 {
292         struct nrs_crrn_net     *net;
293         struct nrs_crrn_client  *cli;
294         struct nrs_crrn_client  *tmp;
295         struct ptlrpc_request   *req;
296
297         if (parent == NULL) {
298                 *resp = &((struct nrs_crrn_net *)policy->pol_private)->cn_res;
299                 return 0;
300         }
301
302         net = container_of(parent, struct nrs_crrn_net, cn_res);
303         req = container_of(nrq, struct ptlrpc_request, rq_nrq);
304
305         cli = rhashtable_lookup_fast(&net->cn_cli_hash, &req->rq_peer.nid,
306                                      nrs_crrn_hash_params);
307         if (cli)
308                 goto out;
309
310         OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
311                           sizeof(*cli), moving_req ? GFP_ATOMIC : GFP_NOFS);
312         if (cli == NULL)
313                 return -ENOMEM;
314
315         cli->cc_nid = req->rq_peer.nid;
316
317         atomic_set(&cli->cc_ref, 0);
318
319         tmp = rhashtable_lookup_get_insert_fast(&net->cn_cli_hash,
320                                                 &cli->cc_rhead,
321                                                 nrs_crrn_hash_params);
322         if (tmp) {
323                 /* insertion failed */
324                 OBD_FREE_PTR(cli);
325                 if (IS_ERR(tmp))
326                         return PTR_ERR(tmp);
327                 cli = tmp;
328         }
329 out:
330         atomic_inc(&cli->cc_ref);
331         *resp = &cli->cc_res;
332
333         return 1;
334 }
335
336 /**
337  * Called when releasing references to the resource hierachy obtained for a
338  * request for scheduling using the CRR-N policy.
339  *
340  * \param[in] policy   the policy the resource belongs to
341  * \param[in] res      the resource to be released
342  */
343 static void nrs_crrn_res_put(struct ptlrpc_nrs_policy *policy,
344                              const struct ptlrpc_nrs_resource *res)
345 {
346         struct nrs_crrn_client *cli;
347
348         /**
349          * Do nothing for freeing parent, nrs_crrn_net resources
350          */
351         if (res->res_parent == NULL)
352                 return;
353
354         cli = container_of(res, struct nrs_crrn_client, cc_res);
355
356         atomic_dec(&cli->cc_ref);
357 }
358
359 /**
360  * Called when getting a request from the CRR-N policy for handlingso that it can be served
361  *
362  * \param[in] policy the policy being polled
363  * \param[in] peek   when set, signifies that we just want to examine the
364  *                   request, and not handle it, so the request is not removed
365  *                   from the policy.
366  * \param[in] force  force the policy to return a request; unused in this policy
367  *
368  * \retval the request to be handled
369  * \retval NULL no request available
370  *
371  * \see ptlrpc_nrs_req_get_nolock()
372  * \see nrs_request_get()
373  */
374 static
375 struct ptlrpc_nrs_request *nrs_crrn_req_get(struct ptlrpc_nrs_policy *policy,
376                                             bool peek, bool force)
377 {
378         struct nrs_crrn_net       *net = policy->pol_private;
379         struct binheap_node       *node = binheap_root(net->cn_binheap);
380         struct ptlrpc_nrs_request *nrq;
381
382         nrq = unlikely(node == NULL) ? NULL :
383               container_of(node, struct ptlrpc_nrs_request, nr_node);
384
385         if (likely(!peek && nrq != NULL)) {
386                 struct nrs_crrn_client *cli;
387                 struct ptlrpc_request *req = container_of(nrq,
388                                                           struct ptlrpc_request,
389                                                           rq_nrq);
390
391                 cli = container_of(nrs_request_resource(nrq),
392                                    struct nrs_crrn_client, cc_res);
393
394                 LASSERT(nrq->nr_u.crr.cr_round <= cli->cc_round);
395
396                 binheap_remove(net->cn_binheap, &nrq->nr_node);
397                 cli->cc_active--;
398
399                 CDEBUG(D_RPCTRACE,
400                        "NRS: starting to handle %s request from %s, with round "
401                        "%llu\n", NRS_POL_NAME_CRRN,
402                        libcfs_id2str(req->rq_peer), nrq->nr_u.crr.cr_round);
403
404                 /** Peek at the next request to be served */
405                 node = binheap_root(net->cn_binheap);
406
407                 /** No more requests */
408                 if (unlikely(node == NULL)) {
409                         net->cn_round++;
410                 } else {
411                         struct ptlrpc_nrs_request *next;
412
413                         next = container_of(node, struct ptlrpc_nrs_request,
414                                             nr_node);
415
416                         if (net->cn_round < next->nr_u.crr.cr_round)
417                                 net->cn_round = next->nr_u.crr.cr_round;
418                 }
419         }
420
421         return nrq;
422 }
423
424 /**
425  * Adds request \a nrq to a CRR-N \a policy instance's set of queued requests
426  *
427  * A scheduling round is a stream of requests that have been sorted in batches
428  * according to the client that they originate from (as identified by its NID);
429  * there can be only one batch for each client in each round. The batches are of
430  * maximum size nrs_crrn_net:cn_quantum. When a new request arrives for
431  * scheduling from a client that has exhausted its quantum in its current round,
432  * it will start scheduling requests on the next scheduling round. Clients are
433  * allowed to schedule requests against a round until all requests for the round
434  * are serviced, so a client might miss a round if it is not generating requests
435  * for a long enough period of time. Clients that miss a round will continue
436  * with scheduling the next request that they generate, starting at the round
437  * that requests are being dispatched for, at the time of arrival of this new
438  * request.
439  *
440  * Requests are tagged with the round number and a sequence number; the sequence
441  * number indicates the relative ordering amongst the batches of requests in a
442  * round, and is identical for all requests in a batch, as is the round number.
443  * The round and sequence numbers are used by crrn_req_compare() in order to
444  * maintain an ordered set of rounds, with each round consisting of an ordered
445  * set of batches of requests.
446  *
447  * \param[in] policy the policy
448  * \param[in] nrq    the request to add
449  *
450  * \retval 0    request successfully added
451  * \retval != 0 error
452  */
453 static int nrs_crrn_req_add(struct ptlrpc_nrs_policy *policy,
454                             struct ptlrpc_nrs_request *nrq)
455 {
456         struct nrs_crrn_net     *net;
457         struct nrs_crrn_client  *cli;
458         int                      rc;
459
460         cli = container_of(nrs_request_resource(nrq),
461                            struct nrs_crrn_client, cc_res);
462         net = container_of(nrs_request_resource(nrq)->res_parent,
463                            struct nrs_crrn_net, cn_res);
464
465         if (cli->cc_quantum == 0 || cli->cc_round < net->cn_round ||
466             (cli->cc_active == 0 && cli->cc_quantum > 0)) {
467
468                 /**
469                  * If the client has no pending requests, and still some of its
470                  * quantum remaining unused, which implies it has not had a
471                  * chance to schedule up to its maximum allowed batch size of
472                  * requests in the previous round it participated, schedule this
473                  * next request on a new round; this avoids fragmentation of
474                  * request batches caused by client inactivity, at the expense
475                  * of potentially slightly increased service time for the
476                  * request batch this request will be a part of.
477                  */
478                 if (cli->cc_active == 0 && cli->cc_quantum > 0)
479                         cli->cc_round++;
480
481                 /** A new scheduling round has commenced */
482                 if (cli->cc_round < net->cn_round)
483                         cli->cc_round = net->cn_round;
484
485                 /** I was not the last client through here */
486                 if (cli->cc_sequence < net->cn_sequence)
487                         cli->cc_sequence = ++net->cn_sequence;
488                 /**
489                  * Reset the quantum if we have reached the maximum quantum
490                  * size for this batch, or even if we have not managed to
491                  * complete a batch size up to its maximum allowed size.
492                  * XXX: Accessed unlocked
493                  */
494                 cli->cc_quantum = net->cn_quantum;
495         }
496
497         nrq->nr_u.crr.cr_round = cli->cc_round;
498         nrq->nr_u.crr.cr_sequence = cli->cc_sequence;
499
500         rc = binheap_insert(net->cn_binheap, &nrq->nr_node);
501         if (rc == 0) {
502                 cli->cc_active++;
503                 if (--cli->cc_quantum == 0)
504                         cli->cc_round++;
505         }
506         return rc;
507 }
508
509 /**
510  * Removes request \a nrq from a CRR-N \a policy instance's set of queued
511  * requests.
512  *
513  * \param[in] policy the policy
514  * \param[in] nrq    the request to remove
515  */
516 static void nrs_crrn_req_del(struct ptlrpc_nrs_policy *policy,
517                              struct ptlrpc_nrs_request *nrq)
518 {
519         struct nrs_crrn_net     *net;
520         struct nrs_crrn_client  *cli;
521         bool                     is_root;
522
523         cli = container_of(nrs_request_resource(nrq),
524                            struct nrs_crrn_client, cc_res);
525         net = container_of(nrs_request_resource(nrq)->res_parent,
526                            struct nrs_crrn_net, cn_res);
527
528         LASSERT(nrq->nr_u.crr.cr_round <= cli->cc_round);
529
530         is_root = &nrq->nr_node == binheap_root(net->cn_binheap);
531
532         binheap_remove(net->cn_binheap, &nrq->nr_node);
533         cli->cc_active--;
534
535         /**
536          * If we just deleted the node at the root of the binheap, we may have
537          * to adjust round numbers.
538          */
539         if (unlikely(is_root)) {
540                 /** Peek at the next request to be served */
541                 struct binheap_node *node = binheap_root(net->cn_binheap);
542
543                 /** No more requests */
544                 if (unlikely(node == NULL)) {
545                         net->cn_round++;
546                 } else {
547                         nrq = container_of(node, struct ptlrpc_nrs_request,
548                                            nr_node);
549
550                         if (net->cn_round < nrq->nr_u.crr.cr_round)
551                                 net->cn_round = nrq->nr_u.crr.cr_round;
552                 }
553         }
554 }
555
556 /**
557  * Called right after the request \a nrq finishes being handled by CRR-N policy
558  * instance \a policy.
559  *
560  * \param[in] policy the policy that handled the request
561  * \param[in] nrq    the request that was handled
562  */
563 static void nrs_crrn_req_stop(struct ptlrpc_nrs_policy *policy,
564                               struct ptlrpc_nrs_request *nrq)
565 {
566         struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
567                                                   rq_nrq);
568
569         CDEBUG(D_RPCTRACE,
570                "NRS: finished handling %s request from %s, with round %llu"
571                "\n", NRS_POL_NAME_CRRN,
572                libcfs_id2str(req->rq_peer), nrq->nr_u.crr.cr_round);
573 }
574
575 /**
576  * debugfs interface
577  */
578
579 /**
580  * Retrieves the value of the Round Robin quantum (i.e. the maximum batch size)
581  * for CRR-N policy instances on both the regular and high-priority NRS head
582  * of a service, as long as a policy instance is not in the
583  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state; policy instances in this
584  * state are skipped later by nrs_crrn_ctl().
585  *
586  * Quantum values are in # of RPCs, and output is in YAML format.
587  *
588  * For example:
589  *
590  *      reg_quantum:8
591  *      hp_quantum:4
592  */
593 static int
594 ptlrpc_lprocfs_nrs_crrn_quantum_seq_show(struct seq_file *m, void *data)
595 {
596         struct ptlrpc_service   *svc = m->private;
597         __u16                   quantum;
598         int                     rc;
599
600         /**
601          * Perform two separate calls to this as only one of the NRS heads'
602          * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
603          * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
604          */
605         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
606                                        NRS_POL_NAME_CRRN,
607                                        NRS_CTL_CRRN_RD_QUANTUM,
608                                        true, &quantum);
609         if (rc == 0) {
610                 seq_printf(m, NRS_LPROCFS_QUANTUM_NAME_REG
611                            "%-5d\n", quantum);
612                 /**
613                  * Ignore -ENODEV as the regular NRS head's policy may be in the
614                  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
615                  */
616         } else if (rc != -ENODEV) {
617                 return rc;
618         }
619
620         if (!nrs_svc_has_hp(svc))
621                 goto no_hp;
622
623         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
624                                        NRS_POL_NAME_CRRN,
625                                        NRS_CTL_CRRN_RD_QUANTUM,
626                                        true, &quantum);
627         if (rc == 0) {
628                 seq_printf(m, NRS_LPROCFS_QUANTUM_NAME_HP"%-5d\n", quantum);
629                 /**
630                  * Ignore -ENODEV as the high priority NRS head's policy may be
631                  * in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
632                  */
633         } else if (rc != -ENODEV) {
634                 return rc;
635         }
636
637 no_hp:
638         return rc;
639 }
640
641 /**
642  * Sets the value of the Round Robin quantum (i.e. the maximum batch size)
643  * for CRR-N policy instances of a service. The user can set the quantum size
644  * for the regular or high priority NRS head individually by specifying each
645  * value, or both together in a single invocation.
646  *
647  * For example:
648  *
649  * lctl set_param *.*.*.nrs_crrn_quantum=reg_quantum:32, to set the regular
650  * request quantum size on all PTLRPC services to 32
651  *
652  * lctl set_param *.*.*.nrs_crrn_quantum=hp_quantum:16, to set the high
653  * priority request quantum size on all PTLRPC services to 16, and
654  *
655  * lctl set_param *.*.ost_io.nrs_crrn_quantum=16, to set both the regular and
656  * high priority request quantum sizes of the ost_io service to 16.
657  *
658  * policy instances in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state
659  * are skipped later by nrs_crrn_ctl().
660  */
661 static ssize_t
662 ptlrpc_lprocfs_nrs_crrn_quantum_seq_write(struct file *file,
663                                           const char __user *buffer,
664                                           size_t count,
665                                           loff_t *off)
666 {
667         struct seq_file             *m = file->private_data;
668         struct ptlrpc_service       *svc = m->private;
669         enum ptlrpc_nrs_queue_type   queue = 0;
670         char                         kernbuf[LPROCFS_NRS_WR_QUANTUM_MAX_CMD];
671         char                        *val;
672         long                         quantum_reg;
673         long                         quantum_hp;
674         /** lprocfs_find_named_value() modifies its argument, so keep a copy */
675         size_t                       count_copy;
676         int                          rc = 0;
677         int                          rc2 = 0;
678
679         if (count > (sizeof(kernbuf) - 1))
680                 return -EINVAL;
681
682         if (copy_from_user(kernbuf, buffer, count))
683                 return -EFAULT;
684
685         kernbuf[count] = '\0';
686
687         count_copy = count;
688
689         /**
690          * Check if the regular quantum value has been specified
691          */
692         val = lprocfs_find_named_value(kernbuf, NRS_LPROCFS_QUANTUM_NAME_REG,
693                                        &count_copy);
694         if (val != kernbuf) {
695                 rc = kstrtol(val, 10, &quantum_reg);
696                 if (rc)
697                         return rc;
698
699                 queue |= PTLRPC_NRS_QUEUE_REG;
700         }
701
702         count_copy = count;
703
704         /**
705          * Check if the high priority quantum value has been specified
706          */
707         val = lprocfs_find_named_value(kernbuf, NRS_LPROCFS_QUANTUM_NAME_HP,
708                                        &count_copy);
709         if (val != kernbuf) {
710                 if (!nrs_svc_has_hp(svc))
711                         return -ENODEV;
712
713                 rc = kstrtol(val, 10, &quantum_hp);
714                 if (rc)
715                         return rc;
716
717                 queue |= PTLRPC_NRS_QUEUE_HP;
718         }
719
720         /**
721          * If none of the queues has been specified, look for a valid numerical
722          * value
723          */
724         if (queue == 0) {
725                 rc = kstrtol(kernbuf, 10, &quantum_reg);
726                 if (rc)
727                         return rc;
728
729                 queue = PTLRPC_NRS_QUEUE_REG;
730
731                 if (nrs_svc_has_hp(svc)) {
732                         queue |= PTLRPC_NRS_QUEUE_HP;
733                         quantum_hp = quantum_reg;
734                 }
735         }
736
737         if ((((queue & PTLRPC_NRS_QUEUE_REG) != 0) &&
738             ((quantum_reg > LPROCFS_NRS_QUANTUM_MAX || quantum_reg <= 0))) ||
739             (((queue & PTLRPC_NRS_QUEUE_HP) != 0) &&
740             ((quantum_hp > LPROCFS_NRS_QUANTUM_MAX || quantum_hp <= 0))))
741                 return -EINVAL;
742
743         /**
744          * We change the values on regular and HP NRS heads separately, so that
745          * we do not exit early from ptlrpc_nrs_policy_control() with an error
746          * returned by nrs_policy_ctl_locked(), in cases where the user has not
747          * started the policy on either the regular or HP NRS head; i.e. we are
748          * ignoring -ENODEV within nrs_policy_ctl_locked(). -ENODEV is returned
749          * only if the operation fails with -ENODEV on all heads that have been
750          * specified by the command; if at least one operation succeeds,
751          * success is returned.
752          */
753         if ((queue & PTLRPC_NRS_QUEUE_REG) != 0) {
754                 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
755                                                NRS_POL_NAME_CRRN,
756                                                NRS_CTL_CRRN_WR_QUANTUM, false,
757                                                &quantum_reg);
758                 if ((rc < 0 && rc != -ENODEV) ||
759                     (rc == -ENODEV && queue == PTLRPC_NRS_QUEUE_REG))
760                         return rc;
761         }
762
763         if ((queue & PTLRPC_NRS_QUEUE_HP) != 0) {
764                 rc2 = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
765                                                 NRS_POL_NAME_CRRN,
766                                                 NRS_CTL_CRRN_WR_QUANTUM, false,
767                                                 &quantum_hp);
768                 if ((rc2 < 0 && rc2 != -ENODEV) ||
769                     (rc2 == -ENODEV && queue == PTLRPC_NRS_QUEUE_HP))
770                         return rc2;
771         }
772
773         return rc == -ENODEV && rc2 == -ENODEV ? -ENODEV : count;
774 }
775
776 LDEBUGFS_SEQ_FOPS(ptlrpc_lprocfs_nrs_crrn_quantum);
777
778 /**
779  * Initializes a CRR-N policy's lprocfs interface for service \a svc
780  *
781  * \param[in] svc the service
782  *
783  * \retval 0    success
784  * \retval != 0 error
785  */
786 static int nrs_crrn_lprocfs_init(struct ptlrpc_service *svc)
787 {
788         struct ldebugfs_vars nrs_crrn_lprocfs_vars[] = {
789                 { .name         = "nrs_crrn_quantum",
790                   .fops         = &ptlrpc_lprocfs_nrs_crrn_quantum_fops,
791                   .data = svc },
792                 { NULL }
793         };
794
795         if (!svc->srv_debugfs_entry)
796                 return 0;
797
798         ldebugfs_add_vars(svc->srv_debugfs_entry, nrs_crrn_lprocfs_vars, NULL);
799
800         return 0;
801 }
802
803 /**
804  * CRR-N policy operations
805  */
806 static const struct ptlrpc_nrs_pol_ops nrs_crrn_ops = {
807         .op_policy_start        = nrs_crrn_start,
808         .op_policy_stop         = nrs_crrn_stop,
809         .op_policy_ctl          = nrs_crrn_ctl,
810         .op_res_get             = nrs_crrn_res_get,
811         .op_res_put             = nrs_crrn_res_put,
812         .op_req_get             = nrs_crrn_req_get,
813         .op_req_enqueue         = nrs_crrn_req_add,
814         .op_req_dequeue         = nrs_crrn_req_del,
815         .op_req_stop            = nrs_crrn_req_stop,
816         .op_lprocfs_init        = nrs_crrn_lprocfs_init,
817 };
818
819 /**
820  * CRR-N policy configuration
821  */
822 struct ptlrpc_nrs_pol_conf nrs_conf_crrn = {
823         .nc_name                = NRS_POL_NAME_CRRN,
824         .nc_ops                 = &nrs_crrn_ops,
825         .nc_compat              = nrs_policy_compat_all,
826 };
827
828 /** @} CRR-N policy */
829
830 /** @} nrs */