Whamcloud - gitweb
LU-6409 nodemap: don't destroy nodemaps in cfs_hash_for_each
[fs/lustre-release.git] / lustre / ptlrpc / nrs_orr.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, 2014, Intel Corporation.
24  *
25  * Copyright 2012 Xyratex Technology Limited
26  */
27 /*
28  * lustre/ptlrpc/nrs_orr.c
29  *
30  * Network Request Scheduler (NRS) ORR and TRR policies
31  *
32  * Request scheduling in a Round-Robin manner over backend-fs objects and OSTs
33  * respectively
34  *
35  * Author: Liang Zhen <liang@whamcloud.com>
36  * Author: Nikitas Angelinas <nikitas_angelinas@xyratex.com>
37  */
38 #ifdef HAVE_SERVER_SUPPORT
39
40 /**
41  * \addtogoup nrs
42  * @{
43  */
44 #define DEBUG_SUBSYSTEM S_RPC
45 #include <obd_support.h>
46 #include <obd_class.h>
47 #include <lustre_net.h>
48 #include <lustre/lustre_idl.h>
49 #include <lustre_req_layout.h>
50 #include "ptlrpc_internal.h"
51
52 /**
53  * \name ORR/TRR policy
54  *
55  * ORR/TRR (Object-based Round Robin/Target-based Round Robin) NRS policies
56  *
57  * ORR performs batched Round Robin shceduling of brw RPCs, based on the FID of
58  * the backend-fs object that the brw RPC pertains to; the TRR policy performs
59  * batched Round Robin scheduling of brw RPCs, based on the OST index that the
60  * RPC pertains to. Both policies also order RPCs in each batch in ascending
61  * offset order, which is lprocfs-tunable between logical file offsets, and
62  * physical disk offsets, as reported by fiemap.
63  *
64  * The TRR policy reuses much of the functionality of ORR. These two scheduling
65  * algorithms could alternatively be implemented under a single NRS policy, that
66  * uses an lprocfs tunable in order to switch between the two types of
67  * scheduling behaviour. The two algorithms have been implemented as separate
68  * policies for reasons of clarity to the user, and to avoid issues that would
69  * otherwise arise at the point of switching between behaviours in the case of
70  * having a single policy, such as resource cleanup for nrs_orr_object
71  * instances. It is possible that this may need to be re-examined in the future,
72  * along with potentially coalescing other policies that perform batched request
73  * scheduling in a Round-Robin manner, all into one policy.
74  *
75  * @{
76  */
77
78 #define NRS_POL_NAME_ORR        "orr"
79 #define NRS_POL_NAME_TRR        "trr"
80
81 /**
82  * Checks if the RPC type of \a nrq is currently handled by an ORR/TRR policy
83  *
84  * \param[in]  orrd   the ORR/TRR policy scheduler instance
85  * \param[in]  nrq    the request
86  * \param[out] opcode the opcode is saved here, just in order to avoid calling
87  *                    lustre_msg_get_opc() again later
88  *
89  * \retval true  request type is supported by the policy instance
90  * \retval false request type is not supported by the policy instance
91  */
92 static bool nrs_orr_req_supported(struct nrs_orr_data *orrd,
93                                   struct ptlrpc_nrs_request *nrq, __u32 *opcode)
94 {
95         struct ptlrpc_request  *req = container_of(nrq, struct ptlrpc_request,
96                                                    rq_nrq);
97         __u32                   opc = lustre_msg_get_opc(req->rq_reqmsg);
98         bool                    rc = false;
99
100         /**
101          * XXX: nrs_orr_data::od_supp accessed unlocked.
102          */
103         switch (opc) {
104         case OST_READ:
105                 rc = orrd->od_supp & NOS_OST_READ;
106                 break;
107         case OST_WRITE:
108                 rc = orrd->od_supp & NOS_OST_WRITE;
109                 break;
110         }
111
112         if (rc)
113                 *opcode = opc;
114
115         return rc;
116 }
117
118 /**
119  * Returns the ORR/TRR key fields for the request \a nrq in \a key.
120  *
121  * \param[in]  orrd the ORR/TRR policy scheduler instance
122  * \param[in]  nrq  the request
123  * \param[in]  opc  the request's opcode
124  * \param[in]  name the policy name
125  * \param[out] key  fields of the key are returned here.
126  *
127  * \retval 0   key filled successfully
128  * \retval < 0 error
129  */
130 static int nrs_orr_key_fill(struct nrs_orr_data *orrd,
131                             struct ptlrpc_nrs_request *nrq, __u32 opc,
132                             char *name, struct nrs_orr_key *key)
133 {
134         struct ptlrpc_request  *req = container_of(nrq, struct ptlrpc_request,
135                                                    rq_nrq);
136         struct ost_body        *body;
137         __u32                   ost_idx;
138         bool                    is_orr = strncmp(name, NRS_POL_NAME_ORR,
139                                                  NRS_POL_NAME_MAX) == 0;
140
141         LASSERT(req != NULL);
142
143         /**
144          * This is an attempt to fill in the request key fields while
145          * moving a request from the regular to the high-priority NRS
146          * head (via ldlm_lock_reorder_req()), but the request key has
147          * been adequately filled when nrs_orr_res_get() was called through
148          * ptlrpc_nrs_req_initialize() for the regular NRS head's ORR/TRR
149          * policy, so there is nothing to do.
150          */
151         if ((is_orr && nrq->nr_u.orr.or_orr_set) ||
152             (!is_orr && nrq->nr_u.orr.or_trr_set)) {
153                 *key = nrq->nr_u.orr.or_key;
154                 return 0;
155         }
156
157         /* Bounce unconnected requests to the default policy. */
158         if (req->rq_export == NULL)
159                 return -ENOTCONN;
160
161         if (nrq->nr_u.orr.or_orr_set || nrq->nr_u.orr.or_trr_set)
162                 memset(&nrq->nr_u.orr.or_key, 0, sizeof(nrq->nr_u.orr.or_key));
163
164         ost_idx = class_server_data(req->rq_export->exp_obd)->lsd_osd_index;
165
166         if (is_orr) {
167                 int     rc;
168                 /**
169                  * The request pill for OST_READ and OST_WRITE requests is
170                  * initialized in the ost_io service's
171                  * ptlrpc_service_ops::so_hpreq_handler, ost_io_hpreq_handler(),
172                  * so no need to redo it here.
173                  */
174                 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
175                 if (body == NULL)
176                         RETURN(-EFAULT);
177
178                 rc = ostid_to_fid(&key->ok_fid, &body->oa.o_oi, ost_idx);
179                 if (rc < 0)
180                         return rc;
181
182                 nrq->nr_u.orr.or_orr_set = 1;
183         } else {
184                 key->ok_idx = ost_idx;
185                 nrq->nr_u.orr.or_trr_set = 1;
186         }
187
188         return 0;
189 }
190
191 /**
192  * Populates the range values in \a range with logical offsets obtained via
193  * \a nb.
194  *
195  * \param[in]  nb       niobuf_remote struct array for this request
196  * \param[in]  niocount count of niobuf_remote structs for this request
197  * \param[out] range    the offset range is returned here
198  */
199 static void nrs_orr_range_fill_logical(struct niobuf_remote *nb, int niocount,
200                                        struct nrs_orr_req_range *range)
201 {
202         /* Should we do this at page boundaries ? */
203         range->or_start = nb[0].rnb_offset & PAGE_MASK;
204         range->or_end = (nb[niocount - 1].rnb_offset +
205                          nb[niocount - 1].rnb_len - 1) | ~PAGE_MASK;
206 }
207
208 /**
209  * We obtain information just for a single extent, as the request can only be in
210  * a single place in the binary heap anyway.
211  */
212 #define ORR_NUM_EXTENTS 1
213
214 /**
215  * Converts the logical file offset range in \a range, to a physical disk offset
216  * range in \a range, for a request. Uses obd_get_info() in order to carry out a
217  * fiemap call and obtain backend-fs extent information. The returned range is
218  * in physical block numbers.
219  *
220  * \param[in]     nrq   the request
221  * \param[in]     oa    obdo struct for this request
222  * \param[in,out] range the offset range in bytes; logical range in, physical
223  *                      range out
224  *
225  * \retval 0    physical offsets obtained successfully
226  * \retvall < 0 error
227  */
228 static int nrs_orr_range_fill_physical(struct ptlrpc_nrs_request *nrq,
229                                        struct obdo *oa,
230                                        struct nrs_orr_req_range *range)
231 {
232         struct ptlrpc_request     *req = container_of(nrq,
233                                                       struct ptlrpc_request,
234                                                       rq_nrq);
235         char                       fiemap_buf[offsetof(struct fiemap,
236                                                   fm_extents[ORR_NUM_EXTENTS])];
237         struct fiemap              *fiemap = (struct fiemap *)fiemap_buf;
238         struct ll_fiemap_info_key  key;
239         loff_t                     start;
240         loff_t                     end;
241         int                        rc;
242
243         key = (typeof(key)) {
244                 .lfik_name = KEY_FIEMAP,
245                 .lfik_oa = *oa,
246                 .lfik_fiemap = {
247                         .fm_start = range->or_start,
248                         .fm_length = range->or_end - range->or_start,
249                         .fm_extent_count = ORR_NUM_EXTENTS
250                 }
251         };
252
253         rc = obd_get_info(req->rq_svc_thread->t_env, req->rq_export,
254                           sizeof(key), &key, NULL, fiemap);
255         if (rc < 0)
256                 GOTO(out, rc);
257
258         if (fiemap->fm_mapped_extents == 0 ||
259             fiemap->fm_mapped_extents > ORR_NUM_EXTENTS)
260                 GOTO(out, rc = -EFAULT);
261
262         /**
263          * Calculate the physical offset ranges for the request from the extent
264          * information and the logical request offsets.
265          */
266         start = fiemap->fm_extents[0].fe_physical + range->or_start -
267                 fiemap->fm_extents[0].fe_logical;
268         end = start + range->or_end - range->or_start;
269
270         range->or_start = start;
271         range->or_end = end;
272
273         nrq->nr_u.orr.or_physical_set = 1;
274 out:
275         return rc;
276 }
277
278 /**
279  * Sets the offset range the request covers; either in logical file
280  * offsets or in physical disk offsets.
281  *
282  * \param[in] nrq        the request
283  * \param[in] orrd       the ORR/TRR policy scheduler instance
284  * \param[in] opc        the request's opcode
285  * \param[in] moving_req is the request in the process of moving onto the
286  *                       high-priority NRS head?
287  *
288  * \retval 0    range filled successfully
289  * \retval != 0 error
290  */
291 static int nrs_orr_range_fill(struct ptlrpc_nrs_request *nrq,
292                               struct nrs_orr_data *orrd, __u32 opc,
293                               bool moving_req)
294 {
295         struct ptlrpc_request       *req = container_of(nrq,
296                                                         struct ptlrpc_request,
297                                                         rq_nrq);
298         struct obd_ioobj            *ioo;
299         struct niobuf_remote        *nb;
300         struct ost_body             *body;
301         struct nrs_orr_req_range     range;
302         int                          niocount;
303         int                          rc = 0;
304
305         /**
306          * If we are scheduling using physical disk offsets, but we have filled
307          * the offset information in the request previously
308          * (i.e. ldlm_lock_reorder_req() is moving the request to the
309          * high-priority NRS head), there is no need to do anything, and we can
310          * exit. Moreover than the lack of need, we would be unable to perform
311          * the obd_get_info() call required in nrs_orr_range_fill_physical(),
312          * because ldlm_lock_reorder_lock() calls into here while holding a
313          * spinlock, and retrieving fiemap information via obd_get_info() is a
314          * potentially sleeping operation.
315          */
316         if (orrd->od_physical && nrq->nr_u.orr.or_physical_set)
317                 return 0;
318
319         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
320         if (ioo == NULL)
321                 GOTO(out, rc = -EFAULT);
322
323         niocount = ioo->ioo_bufcnt;
324
325         nb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
326         if (nb == NULL)
327                 GOTO(out, rc = -EFAULT);
328
329         /**
330          * Use logical information from niobuf_remote structures.
331          */
332         nrs_orr_range_fill_logical(nb, niocount, &range);
333
334         /**
335          * Obtain physical offsets if selected, and this is an OST_READ RPC
336          * RPC. We do not enter this block if moving_req is set which indicates
337          * that the request is being moved to the high-priority NRS head by
338          * ldlm_lock_reorder_req(), as that function calls in here while holding
339          * a spinlock, and nrs_orr_range_physical() can sleep, so we just use
340          * logical file offsets for the range values for such requests.
341          */
342         if (orrd->od_physical && opc == OST_READ && !moving_req) {
343                 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
344                 if (body == NULL)
345                         GOTO(out, rc = -EFAULT);
346
347                 /**
348                  * Translate to physical block offsets from backend filesystem
349                  * extents.
350                  * Ignore return values; if obtaining the physical offsets
351                  * fails, use the logical offsets.
352                  */
353                 nrs_orr_range_fill_physical(nrq, &body->oa, &range);
354         }
355
356         nrq->nr_u.orr.or_range = range;
357 out:
358         return rc;
359 }
360
361 /**
362  * Generates a character string that can be used in order to register uniquely
363  * named libcfs_hash and slab objects for ORR/TRR policy instances. The
364  * character string is unique per policy instance, as it includes the policy's
365  * name, the CPT number, and a {reg|hp} token, and there is one policy instance
366  * per NRS head on each CPT, and the policy is only compatible with the ost_io
367  * service.
368  *
369  * \param[in] policy the policy instance
370  * \param[out] name  the character array that will hold the generated name
371  */
372 static void nrs_orr_genobjname(struct ptlrpc_nrs_policy *policy, char *name)
373 {
374         snprintf(name, NRS_ORR_OBJ_NAME_MAX, "%s%s%s%d",
375                  "nrs_", policy->pol_desc->pd_name,
376                  policy->pol_nrs->nrs_queue_type == PTLRPC_NRS_QUEUE_REG ?
377                  "_reg_" : "_hp_", nrs_pol2cptid(policy));
378 }
379
380 /**
381  * ORR/TRR hash operations
382  */
383 #define NRS_ORR_BITS            24
384 #define NRS_ORR_BKT_BITS        12
385 #define NRS_ORR_HASH_FLAGS      (CFS_HASH_SPIN_BKTLOCK | CFS_HASH_ASSERT_EMPTY)
386
387 #define NRS_TRR_BITS            4
388 #define NRS_TRR_BKT_BITS        2
389 #define NRS_TRR_HASH_FLAGS      CFS_HASH_SPIN_BKTLOCK
390
391 static unsigned
392 nrs_orr_hop_hash(struct cfs_hash *hs, const void *key, unsigned mask)
393 {
394         return cfs_hash_djb2_hash(key, sizeof(struct nrs_orr_key), mask);
395 }
396
397 static void *nrs_orr_hop_key(struct hlist_node *hnode)
398 {
399         struct nrs_orr_object *orro = hlist_entry(hnode,
400                                                       struct nrs_orr_object,
401                                                       oo_hnode);
402         return &orro->oo_key;
403 }
404
405 static int nrs_orr_hop_keycmp(const void *key, struct hlist_node *hnode)
406 {
407         struct nrs_orr_object *orro = hlist_entry(hnode,
408                                                       struct nrs_orr_object,
409                                                       oo_hnode);
410
411         return lu_fid_eq(&orro->oo_key.ok_fid,
412                          &((struct nrs_orr_key *)key)->ok_fid);
413 }
414
415 static void *nrs_orr_hop_object(struct hlist_node *hnode)
416 {
417         return hlist_entry(hnode, struct nrs_orr_object, oo_hnode);
418 }
419
420 static void nrs_orr_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
421 {
422         struct nrs_orr_object *orro = hlist_entry(hnode,
423                                                       struct nrs_orr_object,
424                                                       oo_hnode);
425         orro->oo_ref++;
426 }
427
428 /**
429  * Removes an nrs_orr_object the hash and frees its memory, if the object has
430  * no active users.
431  */
432 static void nrs_orr_hop_put_free(struct cfs_hash *hs, struct hlist_node *hnode)
433 {
434         struct nrs_orr_object *orro = hlist_entry(hnode,
435                                                       struct nrs_orr_object,
436                                                       oo_hnode);
437         struct nrs_orr_data   *orrd = container_of(orro->oo_res.res_parent,
438                                                    struct nrs_orr_data, od_res);
439         struct cfs_hash_bd     bd;
440
441         cfs_hash_bd_get_and_lock(hs, &orro->oo_key, &bd, 1);
442
443         if (--orro->oo_ref > 1) {
444                 cfs_hash_bd_unlock(hs, &bd, 1);
445
446                 return;
447         }
448         LASSERT(orro->oo_ref == 1);
449
450         cfs_hash_bd_del_locked(hs, &bd, hnode);
451         cfs_hash_bd_unlock(hs, &bd, 1);
452
453         OBD_SLAB_FREE_PTR(orro, orrd->od_cache);
454 }
455
456 static void nrs_orr_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
457 {
458         struct nrs_orr_object *orro = hlist_entry(hnode,
459                                                       struct nrs_orr_object,
460                                                       oo_hnode);
461         orro->oo_ref--;
462 }
463
464 static int nrs_trr_hop_keycmp(const void *key, struct hlist_node *hnode)
465 {
466         struct nrs_orr_object *orro = hlist_entry(hnode,
467                                                       struct nrs_orr_object,
468                                                       oo_hnode);
469
470         return orro->oo_key.ok_idx == ((struct nrs_orr_key *)key)->ok_idx;
471 }
472
473 static void nrs_trr_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
474 {
475         struct nrs_orr_object *orro = hlist_entry(hnode,
476                                                       struct nrs_orr_object,
477                                                       oo_hnode);
478         struct nrs_orr_data   *orrd = container_of(orro->oo_res.res_parent,
479                                                    struct nrs_orr_data, od_res);
480
481         LASSERTF(orro->oo_ref == 0,
482                  "Busy NRS TRR policy object for OST with index %u, with %ld "
483                  "refs\n", orro->oo_key.ok_idx, orro->oo_ref);
484
485         OBD_SLAB_FREE_PTR(orro, orrd->od_cache);
486 }
487
488 static struct cfs_hash_ops nrs_orr_hash_ops = {
489         .hs_hash        = nrs_orr_hop_hash,
490         .hs_key         = nrs_orr_hop_key,
491         .hs_keycmp      = nrs_orr_hop_keycmp,
492         .hs_object      = nrs_orr_hop_object,
493         .hs_get         = nrs_orr_hop_get,
494         .hs_put         = nrs_orr_hop_put_free,
495         .hs_put_locked  = nrs_orr_hop_put,
496 };
497
498 static struct cfs_hash_ops nrs_trr_hash_ops = {
499         .hs_hash        = nrs_orr_hop_hash,
500         .hs_key         = nrs_orr_hop_key,
501         .hs_keycmp      = nrs_trr_hop_keycmp,
502         .hs_object      = nrs_orr_hop_object,
503         .hs_get         = nrs_orr_hop_get,
504         .hs_put         = nrs_orr_hop_put,
505         .hs_put_locked  = nrs_orr_hop_put,
506         .hs_exit        = nrs_trr_hop_exit,
507 };
508
509 #define NRS_ORR_QUANTUM_DFLT    256
510
511 /**
512  * Binary heap predicate.
513  *
514  * Uses
515  * ptlrpc_nrs_request::nr_u::orr::or_round,
516  * ptlrpc_nrs_request::nr_u::orr::or_sequence, and
517  * ptlrpc_nrs_request::nr_u::orr::or_range to compare two binheap nodes and
518  * produce a binary predicate that indicates their relative priority, so that
519  * the binary heap can perform the necessary sorting operations.
520  *
521  * \param[in] e1 the first binheap node to compare
522  * \param[in] e2 the second binheap node to compare
523  *
524  * \retval 0 e1 > e2
525  * \retval 1 e1 < e2
526  */
527 static int orr_req_compare(cfs_binheap_node_t *e1, cfs_binheap_node_t *e2)
528 {
529         struct ptlrpc_nrs_request *nrq1;
530         struct ptlrpc_nrs_request *nrq2;
531
532         nrq1 = container_of(e1, struct ptlrpc_nrs_request, nr_node);
533         nrq2 = container_of(e2, struct ptlrpc_nrs_request, nr_node);
534
535         /**
536          * Requests have been scheduled against a different scheduling round.
537          */
538         if (nrq1->nr_u.orr.or_round < nrq2->nr_u.orr.or_round)
539                 return 1;
540         else if (nrq1->nr_u.orr.or_round > nrq2->nr_u.orr.or_round)
541                 return 0;
542
543         /**
544          * Requests have been scheduled against the same scheduling round, but
545          * belong to a different batch, i.e. they pertain to a different
546          * backend-fs object (for ORR policy instances) or OST (for TRR policy
547          * instances).
548          */
549         if (nrq1->nr_u.orr.or_sequence < nrq2->nr_u.orr.or_sequence)
550                 return 1;
551         else if (nrq1->nr_u.orr.or_sequence > nrq2->nr_u.orr.or_sequence)
552                 return 0;
553
554         /**
555          * If round numbers and sequence numbers are equal, the two requests
556          * have been scheduled on the same round, and belong to the same batch,
557          * which means they pertain to the same backend-fs object (if this is an
558          * ORR policy instance), or to the same OST (if this is a TRR policy
559          * instance), so these requests should be sorted by ascending offset
560          * order.
561          */
562         if (nrq1->nr_u.orr.or_range.or_start <
563             nrq2->nr_u.orr.or_range.or_start) {
564                 return 1;
565         } else if (nrq1->nr_u.orr.or_range.or_start >
566                  nrq2->nr_u.orr.or_range.or_start) {
567                 return 0;
568         } else {
569                 /**
570                  * Requests start from the same offset; Dispatch the shorter one
571                  * first; perhaps slightly more chances of hitting caches like
572                  * this.
573                  */
574                 return nrq1->nr_u.orr.or_range.or_end <
575                        nrq2->nr_u.orr.or_range.or_end;
576         }
577 }
578
579 /**
580  * ORR binary heap operations
581  */
582 static cfs_binheap_ops_t nrs_orr_heap_ops = {
583         .hop_enter      = NULL,
584         .hop_exit       = NULL,
585         .hop_compare    = orr_req_compare,
586 };
587
588 /**
589  * Prints a warning message if an ORR/TRR policy is started on a service with
590  * more than one CPT.  Not printed on the console for now, since we don't
591  * have any performance metrics in the first place, and it is annoying.
592  *
593  * \param[in] policy the policy instance
594  *
595  * \retval 0 success
596  */
597 static int nrs_orr_init(struct ptlrpc_nrs_policy *policy)
598 {
599         if (policy->pol_nrs->nrs_svcpt->scp_service->srv_ncpts > 1)
600                 CDEBUG(D_CONFIG, "%s: The %s NRS policy was registered on a "
601                       "service with multiple service partitions. This policy "
602                       "may perform better with a single partition.\n",
603                       policy->pol_nrs->nrs_svcpt->scp_service->srv_name,
604                       policy->pol_desc->pd_name);
605
606         return 0;
607 }
608
609 /**
610  * Called when an ORR policy instance is started.
611  *
612  * \param[in] policy the policy
613  *
614  * \retval -ENOMEM OOM error
615  * \retval 0       success
616  */
617 static int nrs_orr_start(struct ptlrpc_nrs_policy *policy, char *arg)
618 {
619         struct nrs_orr_data    *orrd;
620         struct cfs_hash_ops            *ops;
621         unsigned                cur_bits;
622         unsigned                max_bits;
623         unsigned                bkt_bits;
624         unsigned                flags;
625         int                     rc = 0;
626         ENTRY;
627
628         OBD_CPT_ALLOC_PTR(orrd, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
629         if (orrd == NULL)
630                 RETURN(-ENOMEM);
631
632         /*
633          * Binary heap instance for sorted incoming requests.
634          */
635         orrd->od_binheap = cfs_binheap_create(&nrs_orr_heap_ops,
636                                               CBH_FLAG_ATOMIC_GROW, 4096, NULL,
637                                               nrs_pol2cptab(policy),
638                                               nrs_pol2cptid(policy));
639         if (orrd->od_binheap == NULL)
640                 GOTO(failed, rc = -ENOMEM);
641
642         nrs_orr_genobjname(policy, orrd->od_objname);
643
644         /**
645          * Slab cache for NRS ORR/TRR objects.
646          */
647         orrd->od_cache = kmem_cache_create(orrd->od_objname,
648                                            sizeof(struct nrs_orr_object),
649                                            0, 0, NULL);
650         if (orrd->od_cache == NULL)
651                 GOTO(failed, rc = -ENOMEM);
652
653         if (strncmp(policy->pol_desc->pd_name, NRS_POL_NAME_ORR,
654                     NRS_POL_NAME_MAX) == 0) {
655                 ops = &nrs_orr_hash_ops;
656                 cur_bits = NRS_ORR_BITS;
657                 max_bits = NRS_ORR_BITS;
658                 bkt_bits = NRS_ORR_BKT_BITS;
659                 flags = NRS_ORR_HASH_FLAGS;
660         } else {
661                 ops = &nrs_trr_hash_ops;
662                 cur_bits = NRS_TRR_BITS;
663                 max_bits = NRS_TRR_BITS;
664                 bkt_bits = NRS_TRR_BKT_BITS;
665                 flags = NRS_TRR_HASH_FLAGS;
666         }
667
668         /**
669          * Hash for finding objects by struct nrs_orr_key.
670          * XXX: For TRR, it might be better to avoid using libcfs_hash?
671          * All that needs to be resolved are OST indices, and they
672          * will stay relatively stable during an OSS node's lifetime.
673          */
674         orrd->od_obj_hash = cfs_hash_create(orrd->od_objname, cur_bits,
675                                             max_bits, bkt_bits, 0,
676                                             CFS_HASH_MIN_THETA,
677                                             CFS_HASH_MAX_THETA, ops, flags);
678         if (orrd->od_obj_hash == NULL)
679                 GOTO(failed, rc = -ENOMEM);
680
681         /* XXX: Fields accessed unlocked */
682         orrd->od_quantum = NRS_ORR_QUANTUM_DFLT;
683         orrd->od_supp = NOS_DFLT;
684         orrd->od_physical = true;
685         /**
686          * Set to 1 so that the test inside nrs_orr_req_add() can evaluate to
687          * true.
688          */
689         orrd->od_sequence = 1;
690
691         policy->pol_private = orrd;
692
693         RETURN(rc);
694
695 failed:
696         if (orrd->od_cache) {
697                 kmem_cache_destroy(orrd->od_cache);
698                 LASSERTF(rc == 0, "Could not destroy od_cache slab\n");
699         }
700         if (orrd->od_binheap != NULL)
701                 cfs_binheap_destroy(orrd->od_binheap);
702
703         OBD_FREE_PTR(orrd);
704
705         RETURN(rc);
706 }
707
708 /**
709  * Called when an ORR/TRR policy instance is stopped.
710  *
711  * Called when the policy has been instructed to transition to the
712  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state and has no more
713  * pending requests to serve.
714  *
715  * \param[in] policy the policy
716  */
717 static void nrs_orr_stop(struct ptlrpc_nrs_policy *policy)
718 {
719         struct nrs_orr_data *orrd = policy->pol_private;
720         ENTRY;
721
722         LASSERT(orrd != NULL);
723         LASSERT(orrd->od_binheap != NULL);
724         LASSERT(orrd->od_obj_hash != NULL);
725         LASSERT(orrd->od_cache != NULL);
726         LASSERT(cfs_binheap_is_empty(orrd->od_binheap));
727
728         cfs_binheap_destroy(orrd->od_binheap);
729         cfs_hash_putref(orrd->od_obj_hash);
730         kmem_cache_destroy(orrd->od_cache);
731
732         OBD_FREE_PTR(orrd);
733 }
734
735 /**
736  * Performs a policy-specific ctl function on ORR/TRR policy instances; similar
737  * to ioctl.
738  *
739  * \param[in]     policy the policy instance
740  * \param[in]     opc    the opcode
741  * \param[in,out] arg    used for passing parameters and information
742  *
743  * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
744  * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
745  *
746  * \retval 0   operation carried successfully
747  * \retval -ve error
748  */
749 static int nrs_orr_ctl(struct ptlrpc_nrs_policy *policy,
750                        enum ptlrpc_nrs_ctl opc, void *arg)
751 {
752         assert_spin_locked(&policy->pol_nrs->nrs_lock);
753
754         switch((enum nrs_ctl_orr)opc) {
755         default:
756                 RETURN(-EINVAL);
757
758         case NRS_CTL_ORR_RD_QUANTUM: {
759                 struct nrs_orr_data     *orrd = policy->pol_private;
760
761                 *(__u16 *)arg = orrd->od_quantum;
762                 }
763                 break;
764
765         case NRS_CTL_ORR_WR_QUANTUM: {
766                 struct nrs_orr_data     *orrd = policy->pol_private;
767
768                 orrd->od_quantum = *(__u16 *)arg;
769                 LASSERT(orrd->od_quantum != 0);
770                 }
771                 break;
772
773         case NRS_CTL_ORR_RD_OFF_TYPE: {
774                 struct nrs_orr_data     *orrd = policy->pol_private;
775
776                 *(bool *)arg = orrd->od_physical;
777                 }
778                 break;
779
780         case NRS_CTL_ORR_WR_OFF_TYPE: {
781                 struct nrs_orr_data     *orrd = policy->pol_private;
782
783                 orrd->od_physical = *(bool *)arg;
784                 }
785                 break;
786
787         case NRS_CTL_ORR_RD_SUPP_REQ: {
788                 struct nrs_orr_data     *orrd = policy->pol_private;
789
790                 *(enum nrs_orr_supp *)arg = orrd->od_supp;
791                 }
792                 break;
793
794         case NRS_CTL_ORR_WR_SUPP_REQ: {
795                 struct nrs_orr_data     *orrd = policy->pol_private;
796
797                 orrd->od_supp = *(enum nrs_orr_supp *)arg;
798                 LASSERT((orrd->od_supp & NOS_OST_RW) != 0);
799                 }
800                 break;
801         }
802         RETURN(0);
803 }
804
805 /**
806  * Obtains resources for ORR/TRR policy instances. The top-level resource lives
807  * inside \e nrs_orr_data and the second-level resource inside
808  * \e nrs_orr_object instances.
809  *
810  * \param[in]  policy     the policy for which resources are being taken for
811  *                        request \a nrq
812  * \param[in]  nrq        the request for which resources are being taken
813  * \param[in]  parent     parent resource, embedded in nrs_orr_data for the
814  *                        ORR/TRR policies
815  * \param[out] resp       used to return resource references
816  * \param[in]  moving_req signifies limited caller context; used to perform
817  *                        memory allocations in an atomic context in this
818  *                        policy
819  *
820  * \retval 0   we are returning a top-level, parent resource, one that is
821  *             embedded in an nrs_orr_data object
822  * \retval 1   we are returning a bottom-level resource, one that is embedded
823  *             in an nrs_orr_object object
824  *
825  * \see nrs_resource_get_safe()
826  */
827 static int nrs_orr_res_get(struct ptlrpc_nrs_policy *policy,
828                            struct ptlrpc_nrs_request *nrq,
829                            const struct ptlrpc_nrs_resource *parent,
830                            struct ptlrpc_nrs_resource **resp, bool moving_req)
831 {
832         struct nrs_orr_data            *orrd;
833         struct nrs_orr_object          *orro;
834         struct nrs_orr_object          *tmp;
835         struct nrs_orr_key              key = { { { 0 } } };
836         __u32                           opc;
837         int                             rc = 0;
838
839         /**
840          * struct nrs_orr_data is requested.
841          */
842         if (parent == NULL) {
843                 *resp = &((struct nrs_orr_data *)policy->pol_private)->od_res;
844                 return 0;
845         }
846
847         orrd = container_of(parent, struct nrs_orr_data, od_res);
848
849         /**
850          * If the request type is not supported, fail the enqueuing; the RPC
851          * will be handled by the fallback NRS policy.
852          */
853         if (!nrs_orr_req_supported(orrd, nrq, &opc))
854                 return -1;
855
856         /**
857          * Fill in the key for the request; OST FID for ORR policy instances,
858          * and OST index for TRR policy instances.
859          */
860         rc = nrs_orr_key_fill(orrd, nrq, opc, policy->pol_desc->pd_name, &key);
861         if (rc < 0)
862                 RETURN(rc);
863
864         /**
865          * Set the offset range the request covers
866          */
867         rc = nrs_orr_range_fill(nrq, orrd, opc, moving_req);
868         if (rc < 0)
869                 RETURN(rc);
870
871         orro = cfs_hash_lookup(orrd->od_obj_hash, &key);
872         if (orro != NULL)
873                 goto out;
874
875         OBD_SLAB_CPT_ALLOC_PTR_GFP(orro, orrd->od_cache,
876                                    nrs_pol2cptab(policy), nrs_pol2cptid(policy),
877                                    moving_req ? GFP_ATOMIC : GFP_NOFS);
878         if (orro == NULL)
879                 RETURN(-ENOMEM);
880
881         orro->oo_key = key;
882         orro->oo_ref = 1;
883
884         tmp = cfs_hash_findadd_unique(orrd->od_obj_hash, &orro->oo_key,
885                                       &orro->oo_hnode);
886         if (tmp != orro) {
887                 OBD_SLAB_FREE_PTR(orro, orrd->od_cache);
888                 orro = tmp;
889         }
890 out:
891         /**
892          * For debugging purposes
893          */
894         nrq->nr_u.orr.or_key = orro->oo_key;
895
896         *resp = &orro->oo_res;
897
898         return 1;
899 }
900
901 /**
902  * Called when releasing references to the resource hierachy obtained for a
903  * request for scheduling using ORR/TRR policy instances
904  *
905  * \param[in] policy   the policy the resource belongs to
906  * \param[in] res      the resource to be released
907  */
908 static void nrs_orr_res_put(struct ptlrpc_nrs_policy *policy,
909                             const struct ptlrpc_nrs_resource *res)
910 {
911         struct nrs_orr_data     *orrd;
912         struct nrs_orr_object   *orro;
913
914         /**
915          * Do nothing for freeing parent, nrs_orr_data resources.
916          */
917         if (res->res_parent == NULL)
918                 return;
919
920         orro = container_of(res, struct nrs_orr_object, oo_res);
921         orrd = container_of(res->res_parent, struct nrs_orr_data, od_res);
922
923         cfs_hash_put(orrd->od_obj_hash, &orro->oo_hnode);
924 }
925
926 /**
927  * Called when polling an ORR/TRR policy instance for a request so that it can
928  * be served. Returns the request that is at the root of the binary heap, as
929  * that is the lowest priority one (i.e. libcfs_heap is an implementation of a
930  * min-heap)
931  *
932  * \param[in] policy the policy instance being polled
933  * \param[in] peek   when set, signifies that we just want to examine the
934  *                   request, and not handle it, so the request is not removed
935  *                   from the policy.
936  * \param[in] force  force the policy to return a request; unused in this policy
937  *
938  * \retval the request to be handled
939  * \retval NULL no request available
940  *
941  * \see ptlrpc_nrs_req_get_nolock()
942  * \see nrs_request_get()
943  */
944 static
945 struct ptlrpc_nrs_request *nrs_orr_req_get(struct ptlrpc_nrs_policy *policy,
946                                            bool peek, bool force)
947 {
948         struct nrs_orr_data       *orrd = policy->pol_private;
949         cfs_binheap_node_t        *node = cfs_binheap_root(orrd->od_binheap);
950         struct ptlrpc_nrs_request *nrq;
951
952         nrq = unlikely(node == NULL) ? NULL :
953               container_of(node, struct ptlrpc_nrs_request, nr_node);
954
955         if (likely(!peek && nrq != NULL)) {
956                 struct nrs_orr_object *orro;
957
958                 orro = container_of(nrs_request_resource(nrq),
959                                     struct nrs_orr_object, oo_res);
960
961                 LASSERT(nrq->nr_u.orr.or_round <= orro->oo_round);
962
963                 cfs_binheap_remove(orrd->od_binheap, &nrq->nr_node);
964                 orro->oo_active--;
965
966                 if (strncmp(policy->pol_desc->pd_name, NRS_POL_NAME_ORR,
967                                  NRS_POL_NAME_MAX) == 0)
968                         CDEBUG(D_RPCTRACE,
969                                "NRS: starting to handle %s request for object "
970                                "with FID "DFID", from OST with index %u, with "
971                                "round "LPU64"\n", NRS_POL_NAME_ORR,
972                                PFID(&orro->oo_key.ok_fid),
973                                nrq->nr_u.orr.or_key.ok_idx,
974                                nrq->nr_u.orr.or_round);
975                 else
976                         CDEBUG(D_RPCTRACE,
977                                "NRS: starting to handle %s request from OST "
978                                "with index %u, with round "LPU64"\n",
979                                NRS_POL_NAME_TRR, nrq->nr_u.orr.or_key.ok_idx,
980                                nrq->nr_u.orr.or_round);
981
982                 /** Peek at the next request to be served */
983                 node = cfs_binheap_root(orrd->od_binheap);
984
985                 /** No more requests */
986                 if (unlikely(node == NULL)) {
987                         orrd->od_round++;
988                 } else {
989                         struct ptlrpc_nrs_request *next;
990
991                         next = container_of(node, struct ptlrpc_nrs_request,
992                                             nr_node);
993
994                         if (orrd->od_round < next->nr_u.orr.or_round)
995                                 orrd->od_round = next->nr_u.orr.or_round;
996                 }
997         }
998
999         return nrq;
1000 }
1001
1002 /**
1003  * Sort-adds request \a nrq to an ORR/TRR \a policy instance's set of queued
1004  * requests in the policy's binary heap.
1005  *
1006  * A scheduling round is a stream of requests that have been sorted in batches
1007  * according to the backend-fs object (for ORR policy instances) or OST (for TRR
1008  * policy instances) that they pertain to (as identified by its IDIF FID or OST
1009  * index respectively); there can be only one batch for each object or OST in
1010  * each round. The batches are of maximum size nrs_orr_data:od_quantum. When a
1011  * new request arrives for scheduling for an object or OST that has exhausted
1012  * its quantum in its current round, the request will be scheduled on the next
1013  * scheduling round. Requests are allowed to be scheduled against a round until
1014  * all requests for the round are serviced, so an object or OST might miss a
1015  * round if requests are not scheduled for it for a long enough period of time.
1016  * Objects or OSTs that miss a round will continue with having their next
1017  * request scheduled, starting at the round that requests are being dispatched
1018  * for, at the time of arrival of this request.
1019  *
1020  * Requests are tagged with the round number and a sequence number; the sequence
1021  * number indicates the relative ordering amongst the batches of requests in a
1022  * round, and is identical for all requests in a batch, as is the round number.
1023  * The round and sequence numbers are used by orr_req_compare() in order to use
1024  * nrs_orr_data::od_binheap in order to maintain an ordered set of rounds, with
1025  * each round consisting of an ordered set of batches of requests, and each
1026  * batch consisting of an ordered set of requests according to their logical
1027  * file or physical disk offsets.
1028  *
1029  * \param[in] policy the policy
1030  * \param[in] nrq    the request to add
1031  *
1032  * \retval 0    request successfully added
1033  * \retval != 0 error
1034  */
1035 static int nrs_orr_req_add(struct ptlrpc_nrs_policy *policy,
1036                            struct ptlrpc_nrs_request *nrq)
1037 {
1038         struct nrs_orr_data     *orrd;
1039         struct nrs_orr_object   *orro;
1040         int                      rc;
1041
1042         orro = container_of(nrs_request_resource(nrq),
1043                             struct nrs_orr_object, oo_res);
1044         orrd = container_of(nrs_request_resource(nrq)->res_parent,
1045                             struct nrs_orr_data, od_res);
1046
1047         if (orro->oo_quantum == 0 || orro->oo_round < orrd->od_round ||
1048             (orro->oo_active == 0 && orro->oo_quantum > 0)) {
1049
1050                 /**
1051                  * If there are no pending requests for the object/OST, but some
1052                  * of its quantum still remains unused, which implies we did not
1053                  * get a chance to schedule up to its maximum allowed batch size
1054                  * of requests in the previous round this object/OST
1055                  * participated in, schedule this next request on a new round;
1056                  * this avoids fragmentation of request batches caused by
1057                  * intermittent inactivity on the object/OST, at the expense of
1058                  * potentially slightly increased service time for the request
1059                  * batch this request will be a part of.
1060                  */
1061                 if (orro->oo_active == 0 && orro->oo_quantum > 0)
1062                         orro->oo_round++;
1063
1064                 /** A new scheduling round has commenced */
1065                 if (orro->oo_round < orrd->od_round)
1066                         orro->oo_round = orrd->od_round;
1067
1068                 /** I was not the last object/OST that scheduled a request */
1069                 if (orro->oo_sequence < orrd->od_sequence)
1070                         orro->oo_sequence = ++orrd->od_sequence;
1071                 /**
1072                  * Reset the quantum if we have reached the maximum quantum
1073                  * size for this batch, or even if we have not managed to
1074                  * complete a batch size up to its maximum allowed size.
1075                  * XXX: Accessed unlocked
1076                  */
1077                 orro->oo_quantum = orrd->od_quantum;
1078         }
1079
1080         nrq->nr_u.orr.or_round = orro->oo_round;
1081         nrq->nr_u.orr.or_sequence = orro->oo_sequence;
1082
1083         rc = cfs_binheap_insert(orrd->od_binheap, &nrq->nr_node);
1084         if (rc == 0) {
1085                 orro->oo_active++;
1086                 if (--orro->oo_quantum == 0)
1087                         orro->oo_round++;
1088         }
1089         return rc;
1090 }
1091
1092 /**
1093  * Removes request \a nrq from an ORR/TRR \a policy instance's set of queued
1094  * requests.
1095  *
1096  * \param[in] policy the policy
1097  * \param[in] nrq    the request to remove
1098  */
1099 static void nrs_orr_req_del(struct ptlrpc_nrs_policy *policy,
1100                             struct ptlrpc_nrs_request *nrq)
1101 {
1102         struct nrs_orr_data     *orrd;
1103         struct nrs_orr_object   *orro;
1104         bool                     is_root;
1105
1106         orro = container_of(nrs_request_resource(nrq),
1107                             struct nrs_orr_object, oo_res);
1108         orrd = container_of(nrs_request_resource(nrq)->res_parent,
1109                             struct nrs_orr_data, od_res);
1110
1111         LASSERT(nrq->nr_u.orr.or_round <= orro->oo_round);
1112
1113         is_root = &nrq->nr_node == cfs_binheap_root(orrd->od_binheap);
1114
1115         cfs_binheap_remove(orrd->od_binheap, &nrq->nr_node);
1116         orro->oo_active--;
1117
1118         /**
1119          * If we just deleted the node at the root of the binheap, we may have
1120          * to adjust round numbers.
1121          */
1122         if (unlikely(is_root)) {
1123                 /** Peek at the next request to be served */
1124                 cfs_binheap_node_t *node = cfs_binheap_root(orrd->od_binheap);
1125
1126                 /** No more requests */
1127                 if (unlikely(node == NULL)) {
1128                         orrd->od_round++;
1129                 } else {
1130                         nrq = container_of(node, struct ptlrpc_nrs_request,
1131                                            nr_node);
1132
1133                         if (orrd->od_round < nrq->nr_u.orr.or_round)
1134                                 orrd->od_round = nrq->nr_u.orr.or_round;
1135                 }
1136         }
1137 }
1138
1139 /**
1140  * Called right after the request \a nrq finishes being handled by ORR policy
1141  * instance \a policy.
1142  *
1143  * \param[in] policy the policy that handled the request
1144  * \param[in] nrq    the request that was handled
1145  */
1146 static void nrs_orr_req_stop(struct ptlrpc_nrs_policy *policy,
1147                              struct ptlrpc_nrs_request *nrq)
1148 {
1149         /** NB: resource control, credits etc can be added here */
1150         if (strncmp(policy->pol_desc->pd_name, NRS_POL_NAME_ORR,
1151                     NRS_POL_NAME_MAX) == 0)
1152                 CDEBUG(D_RPCTRACE,
1153                        "NRS: finished handling %s request for object with FID "
1154                        DFID", from OST with index %u, with round "LPU64"\n",
1155                        NRS_POL_NAME_ORR, PFID(&nrq->nr_u.orr.or_key.ok_fid),
1156                        nrq->nr_u.orr.or_key.ok_idx, nrq->nr_u.orr.or_round);
1157         else
1158                 CDEBUG(D_RPCTRACE,
1159                        "NRS: finished handling %s request from OST with index %u,"
1160                        " with round "LPU64"\n",
1161                        NRS_POL_NAME_TRR, nrq->nr_u.orr.or_key.ok_idx,
1162                        nrq->nr_u.orr.or_round);
1163 }
1164
1165 /**
1166  * lprocfs interface
1167  */
1168
1169 #ifdef CONFIG_PROC_FS
1170
1171 /**
1172  * This allows to bundle the policy name into the lprocfs_vars::data pointer
1173  * so that lprocfs read/write functions can be used by both the ORR and TRR
1174  * policies.
1175  */
1176 static struct nrs_lprocfs_orr_data {
1177         struct ptlrpc_service   *svc;
1178         char                    *name;
1179 } lprocfs_orr_data = {
1180         .name = NRS_POL_NAME_ORR
1181 }, lprocfs_trr_data = {
1182         .name = NRS_POL_NAME_TRR
1183 };
1184
1185 /**
1186  * Retrieves the value of the Round Robin quantum (i.e. the maximum batch size)
1187  * for ORR/TRR policy instances on both the regular and high-priority NRS head
1188  * of a service, as long as a policy instance is not in the
1189  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state; policy instances in this
1190  * state are skipped later by nrs_orr_ctl().
1191  *
1192  * Quantum values are in # of RPCs, and the output is in YAML format.
1193  *
1194  * For example:
1195  *
1196  *      reg_quantum:256
1197  *      hp_quantum:8
1198  *
1199  * XXX: the CRR-N version of this, ptlrpc_lprocfs_rd_nrs_crrn_quantum() is
1200  * almost identical; it can be reworked and then reused for ORR/TRR.
1201  */
1202 static int
1203 ptlrpc_lprocfs_nrs_orr_quantum_seq_show(struct seq_file *m, void *data)
1204 {
1205         struct nrs_lprocfs_orr_data *orr_data = m->private;
1206         struct ptlrpc_service       *svc = orr_data->svc;
1207         __u16                        quantum;
1208         int                          rc;
1209
1210         /**
1211          * Perform two separate calls to this as only one of the NRS heads'
1212          * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
1213          * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
1214          */
1215         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1216                                        orr_data->name,
1217                                        NRS_CTL_ORR_RD_QUANTUM,
1218                                        true, &quantum);
1219         if (rc == 0) {
1220                 seq_printf(m, NRS_LPROCFS_QUANTUM_NAME_REG "%-5d\n", quantum);
1221                 /**
1222                  * Ignore -ENODEV as the regular NRS head's policy may be in the
1223                  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1224                  */
1225         } else if (rc != -ENODEV) {
1226                 return rc;
1227         }
1228
1229         /**
1230          * We know the ost_io service which is the only one ORR/TRR policies are
1231          * compatible with, do have an HP NRS head, but it may be best to guard
1232          * against a possible change of this in the future.
1233          */
1234         if (!nrs_svc_has_hp(svc))
1235                 goto no_hp;
1236
1237         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1238                                        orr_data->name, NRS_CTL_ORR_RD_QUANTUM,
1239                                        true, &quantum);
1240         if (rc == 0) {
1241                 seq_printf(m, NRS_LPROCFS_QUANTUM_NAME_HP"%-5d\n", quantum);
1242                 /**
1243                  * Ignore -ENODEV as the high priority NRS head's policy may be
1244                  * in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1245                  */
1246         } else if (rc != -ENODEV) {
1247                 return rc;
1248         }
1249
1250 no_hp:
1251
1252         return rc;
1253 }
1254
1255 /**
1256  * Sets the value of the Round Robin quantum (i.e. the maximum batch size)
1257  * for ORR/TRR policy instances of a service. The user can set the quantum size
1258  * for the regular and high priority NRS head separately by specifying each
1259  * value, or both together in a single invocation.
1260  *
1261  * For example:
1262  *
1263  * lctl set_param ost.OSS.ost_io.nrs_orr_quantum=req_quantum:64, to set the
1264  * request quantum size of the ORR policy instance on the regular NRS head of
1265  * the ost_io service to 64
1266  *
1267  * lctl set_param ost.OSS.ost_io.nrs_trr_quantum=hp_quantum:8 to set the request
1268  * quantum size of the TRR policy instance on the high priority NRS head of the
1269  * ost_io service to 8
1270  *
1271  * lctl set_param ost.OSS.ost_io.nrs_orr_quantum=32, to set both the request
1272  * quantum size of the ORR policy instance on both the regular and the high
1273  * priority NRS head of the ost_io service to 32
1274  *
1275  * policy instances in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state
1276  * are skipped later by nrs_orr_ctl().
1277  *
1278  * XXX: the CRR-N version of this, ptlrpc_lprocfs_wr_nrs_crrn_quantum() is
1279  * almost identical; it can be reworked and then reused for ORR/TRR.
1280  */
1281 static ssize_t
1282 ptlrpc_lprocfs_nrs_orr_quantum_seq_write(struct file *file, const char *buffer,
1283                                          size_t count, loff_t *off)
1284 {
1285         struct seq_file             *m = file->private_data;
1286         struct nrs_lprocfs_orr_data *orr_data = m->private;
1287         struct ptlrpc_service       *svc = orr_data->svc;
1288         enum ptlrpc_nrs_queue_type   queue = 0;
1289         char                         kernbuf[LPROCFS_NRS_WR_QUANTUM_MAX_CMD];
1290         char                        *val;
1291         long                         quantum_reg;
1292         long                         quantum_hp;
1293         /** lprocfs_find_named_value() modifies its argument, so keep a copy */
1294         size_t                       count_copy;
1295         int                          rc = 0;
1296         int                          rc2 = 0;
1297
1298         if (count > (sizeof(kernbuf) - 1))
1299                 return -EINVAL;
1300
1301         if (copy_from_user(kernbuf, buffer, count))
1302                 return -EFAULT;
1303
1304         kernbuf[count] = '\0';
1305
1306         count_copy = count;
1307
1308         /**
1309          * Check if the regular quantum value has been specified
1310          */
1311         val = lprocfs_find_named_value(kernbuf, NRS_LPROCFS_QUANTUM_NAME_REG,
1312                                        &count_copy);
1313         if (val != kernbuf) {
1314                 quantum_reg = simple_strtol(val, NULL, 10);
1315
1316                 queue |= PTLRPC_NRS_QUEUE_REG;
1317         }
1318
1319         count_copy = count;
1320
1321         /**
1322          * Check if the high priority quantum value has been specified
1323          */
1324         val = lprocfs_find_named_value(kernbuf, NRS_LPROCFS_QUANTUM_NAME_HP,
1325                                        &count_copy);
1326         if (val != kernbuf) {
1327                 if (!nrs_svc_has_hp(svc))
1328                         return -ENODEV;
1329
1330                 quantum_hp = simple_strtol(val, NULL, 10);
1331
1332                 queue |= PTLRPC_NRS_QUEUE_HP;
1333         }
1334
1335         /**
1336          * If none of the queues has been specified, look for a valid numerical
1337          * value
1338          */
1339         if (queue == 0) {
1340                 if (!isdigit(kernbuf[0]))
1341                         return -EINVAL;
1342
1343                 quantum_reg = simple_strtol(kernbuf, NULL, 10);
1344
1345                 queue = PTLRPC_NRS_QUEUE_REG;
1346
1347                 if (nrs_svc_has_hp(svc)) {
1348                         queue |= PTLRPC_NRS_QUEUE_HP;
1349                         quantum_hp = quantum_reg;
1350                 }
1351         }
1352
1353         if ((((queue & PTLRPC_NRS_QUEUE_REG) != 0) &&
1354             ((quantum_reg > LPROCFS_NRS_QUANTUM_MAX || quantum_reg <= 0))) ||
1355             (((queue & PTLRPC_NRS_QUEUE_HP) != 0) &&
1356             ((quantum_hp > LPROCFS_NRS_QUANTUM_MAX || quantum_hp <= 0))))
1357                 return -EINVAL;
1358
1359         /**
1360          * We change the values on regular and HP NRS heads separately, so that
1361          * we do not exit early from ptlrpc_nrs_policy_control() with an error
1362          * returned by nrs_policy_ctl_locked(), in cases where the user has not
1363          * started the policy on either the regular or HP NRS head; i.e. we are
1364          * ignoring -ENODEV within nrs_policy_ctl_locked(). -ENODEV is returned
1365          * only if the operation fails with -ENODEV on all heads that have been
1366          * specified by the command; if at least one operation succeeds,
1367          * success is returned.
1368          */
1369         if ((queue & PTLRPC_NRS_QUEUE_REG) != 0) {
1370                 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1371                                                orr_data->name,
1372                                                NRS_CTL_ORR_WR_QUANTUM, false,
1373                                                &quantum_reg);
1374                 if ((rc < 0 && rc != -ENODEV) ||
1375                     (rc == -ENODEV && queue == PTLRPC_NRS_QUEUE_REG))
1376                         return rc;
1377         }
1378
1379         if ((queue & PTLRPC_NRS_QUEUE_HP) != 0) {
1380                 rc2 = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1381                                                 orr_data->name,
1382                                                 NRS_CTL_ORR_WR_QUANTUM, false,
1383                                                 &quantum_hp);
1384                 if ((rc2 < 0 && rc2 != -ENODEV) ||
1385                     (rc2 == -ENODEV && queue == PTLRPC_NRS_QUEUE_HP))
1386                         return rc2;
1387         }
1388
1389         return rc == -ENODEV && rc2 == -ENODEV ? -ENODEV : count;
1390 }
1391 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_orr_quantum);
1392
1393 #define LPROCFS_NRS_OFF_NAME_REG                "reg_offset_type:"
1394 #define LPROCFS_NRS_OFF_NAME_HP                 "hp_offset_type:"
1395
1396 #define LPROCFS_NRS_OFF_NAME_PHYSICAL           "physical"
1397 #define LPROCFS_NRS_OFF_NAME_LOGICAL            "logical"
1398
1399 /**
1400  * Retrieves the offset type used by ORR/TRR policy instances on both the
1401  * regular and high-priority NRS head of a service, as long as a policy
1402  * instance is not in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state;
1403  * policy instances in this state are skipped later by nrs_orr_ctl().
1404  *
1405  * Offset type information is a (physical|logical) string, and output is
1406  * in YAML format.
1407  *
1408  * For example:
1409  *
1410  *      reg_offset_type:physical
1411  *      hp_offset_type:logical
1412  */
1413 static int
1414 ptlrpc_lprocfs_nrs_orr_offset_type_seq_show(struct seq_file *m, void *data)
1415 {
1416         struct nrs_lprocfs_orr_data *orr_data = m->private;
1417         struct ptlrpc_service       *svc = orr_data->svc;
1418         bool                         physical;
1419         int                          rc;
1420
1421         /**
1422          * Perform two separate calls to this as only one of the NRS heads'
1423          * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED
1424          * or ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
1425          */
1426         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1427                                        orr_data->name, NRS_CTL_ORR_RD_OFF_TYPE,
1428                                        true, &physical);
1429         if (rc == 0) {
1430                 seq_printf(m, LPROCFS_NRS_OFF_NAME_REG"%s\n",
1431                            physical ? LPROCFS_NRS_OFF_NAME_PHYSICAL :
1432                            LPROCFS_NRS_OFF_NAME_LOGICAL);
1433                 /**
1434                  * Ignore -ENODEV as the regular NRS head's policy may be in the
1435                  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1436                  */
1437         } else if (rc != -ENODEV) {
1438                 return rc;
1439         }
1440
1441         /**
1442          * We know the ost_io service which is the only one ORR/TRR policies are
1443          * compatible with, do have an HP NRS head, but it may be best to guard
1444          * against a possible change of this in the future.
1445          */
1446         if (!nrs_svc_has_hp(svc))
1447                 goto no_hp;
1448
1449         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1450                                        orr_data->name, NRS_CTL_ORR_RD_OFF_TYPE,
1451                                        true, &physical);
1452         if (rc == 0) {
1453                 seq_printf(m, LPROCFS_NRS_OFF_NAME_HP"%s\n",
1454                            physical ? LPROCFS_NRS_OFF_NAME_PHYSICAL :
1455                            LPROCFS_NRS_OFF_NAME_LOGICAL);
1456                 /**
1457                  * Ignore -ENODEV as the high priority NRS head's policy may be
1458                  * in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1459                  */
1460         } else if (rc != -ENODEV) {
1461                 return rc;
1462         }
1463
1464 no_hp:
1465         return rc;
1466 }
1467
1468 /**
1469  * Max valid command string is the size of the labels, plus "physical" twice.
1470  * plus a separating ' '
1471  */
1472 #define LPROCFS_NRS_WR_OFF_TYPE_MAX_CMD                                        \
1473         sizeof(LPROCFS_NRS_OFF_NAME_REG LPROCFS_NRS_OFF_NAME_PHYSICAL " "      \
1474                LPROCFS_NRS_OFF_NAME_HP LPROCFS_NRS_OFF_NAME_PHYSICAL)
1475
1476 /**
1477  * Sets the type of offsets used to order RPCs in ORR/TRR policy instances. The
1478  * user can set offset type for the regular or high priority NRS head
1479  * separately by specifying each value, or both together in a single invocation.
1480  *
1481  * For example:
1482  *
1483  * lctl set_param ost.OSS.ost_io.nrs_orr_offset_type=
1484  * reg_offset_type:physical, to enable the ORR policy instance on the regular
1485  * NRS head of the ost_io service to use physical disk offset ordering.
1486  *
1487  * lctl set_param ost.OSS.ost_io.nrs_trr_offset_type=logical, to enable the TRR
1488  * policy instances on both the regular ang high priority NRS heads of the
1489  * ost_io service to use logical file offset ordering.
1490  *
1491  * policy instances in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state are
1492  * are skipped later by nrs_orr_ctl().
1493  */
1494 static ssize_t
1495 ptlrpc_lprocfs_nrs_orr_offset_type_seq_write(struct file *file,
1496                                              const char *buffer, size_t count,
1497                                              loff_t *off)
1498 {
1499         struct seq_file             *m = file->private_data;
1500         struct nrs_lprocfs_orr_data *orr_data = m->private;
1501         struct ptlrpc_service       *svc = orr_data->svc;
1502         enum ptlrpc_nrs_queue_type   queue = 0;
1503         char                         kernbuf[LPROCFS_NRS_WR_OFF_TYPE_MAX_CMD];
1504         char                        *val_reg;
1505         char                        *val_hp;
1506         bool                         physical_reg;
1507         bool                         physical_hp;
1508         size_t                       count_copy;
1509         int                          rc = 0;
1510         int                          rc2 = 0;
1511
1512         if (count > (sizeof(kernbuf) - 1))
1513                 return -EINVAL;
1514
1515         if (copy_from_user(kernbuf, buffer, count))
1516                 return -EFAULT;
1517
1518         kernbuf[count] = '\0';
1519
1520         count_copy = count;
1521
1522         /**
1523          * Check if the regular offset type has been specified
1524          */
1525         val_reg = lprocfs_find_named_value(kernbuf,
1526                                            LPROCFS_NRS_OFF_NAME_REG,
1527                                            &count_copy);
1528         if (val_reg != kernbuf)
1529                 queue |= PTLRPC_NRS_QUEUE_REG;
1530
1531         count_copy = count;
1532
1533         /**
1534          * Check if the high priority offset type has been specified
1535          */
1536         val_hp = lprocfs_find_named_value(kernbuf, LPROCFS_NRS_OFF_NAME_HP,
1537                                           &count_copy);
1538         if (val_hp != kernbuf) {
1539                 if (!nrs_svc_has_hp(svc))
1540                         return -ENODEV;
1541
1542                 queue |= PTLRPC_NRS_QUEUE_HP;
1543         }
1544
1545         /**
1546          * If none of the queues has been specified, there may be a valid
1547          * command string at the start of the buffer.
1548          */
1549         if (queue == 0) {
1550                 queue = PTLRPC_NRS_QUEUE_REG;
1551
1552                 if (nrs_svc_has_hp(svc))
1553                         queue |= PTLRPC_NRS_QUEUE_HP;
1554         }
1555
1556         if ((queue & PTLRPC_NRS_QUEUE_REG) != 0) {
1557                 if (strncmp(val_reg, LPROCFS_NRS_OFF_NAME_PHYSICAL,
1558                             sizeof(LPROCFS_NRS_OFF_NAME_PHYSICAL) - 1) == 0)
1559                         physical_reg = true;
1560                 else if (strncmp(val_reg, LPROCFS_NRS_OFF_NAME_LOGICAL,
1561                          sizeof(LPROCFS_NRS_OFF_NAME_LOGICAL) - 1) == 0)
1562                         physical_reg = false;
1563                 else
1564                         return -EINVAL;
1565         }
1566
1567         if ((queue & PTLRPC_NRS_QUEUE_HP) != 0) {
1568                 if (strncmp(val_hp, LPROCFS_NRS_OFF_NAME_PHYSICAL,
1569                             sizeof(LPROCFS_NRS_OFF_NAME_PHYSICAL) - 1) == 0)
1570                         physical_hp = true;
1571                 else if (strncmp(val_hp, LPROCFS_NRS_OFF_NAME_LOGICAL,
1572                                  sizeof(LPROCFS_NRS_OFF_NAME_LOGICAL) - 1) == 0)
1573                         physical_hp = false;
1574                 else
1575                         return -EINVAL;
1576         }
1577
1578         /**
1579          * We change the values on regular and HP NRS heads separately, so that
1580          * we do not exit early from ptlrpc_nrs_policy_control() with an error
1581          * returned by nrs_policy_ctl_locked(), in cases where the user has not
1582          * started the policy on either the regular or HP NRS head; i.e. we are
1583          * ignoring -ENODEV within nrs_policy_ctl_locked(). -ENODEV is returned
1584          * only if the operation fails with -ENODEV on all heads that have been
1585          * specified by the command; if at least one operation succeeds,
1586          * success is returned.
1587          */
1588         if ((queue & PTLRPC_NRS_QUEUE_REG) != 0) {
1589                 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1590                                                orr_data->name,
1591                                                NRS_CTL_ORR_WR_OFF_TYPE, false,
1592                                                &physical_reg);
1593                 if ((rc < 0 && rc != -ENODEV) ||
1594                     (rc == -ENODEV && queue == PTLRPC_NRS_QUEUE_REG))
1595                         return rc;
1596         }
1597
1598         if ((queue & PTLRPC_NRS_QUEUE_HP) != 0) {
1599                 rc2 = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1600                                                 orr_data->name,
1601                                                 NRS_CTL_ORR_WR_OFF_TYPE, false,
1602                                                 &physical_hp);
1603                 if ((rc2 < 0 && rc2 != -ENODEV) ||
1604                     (rc2 == -ENODEV && queue == PTLRPC_NRS_QUEUE_HP))
1605                         return rc2;
1606         }
1607
1608         return rc == -ENODEV && rc2 == -ENODEV ? -ENODEV : count;
1609 }
1610 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_orr_offset_type);
1611
1612 #define NRS_LPROCFS_REQ_SUPP_NAME_REG           "reg_supported:"
1613 #define NRS_LPROCFS_REQ_SUPP_NAME_HP            "hp_supported:"
1614
1615 #define LPROCFS_NRS_SUPP_NAME_READS             "reads"
1616 #define LPROCFS_NRS_SUPP_NAME_WRITES            "writes"
1617 #define LPROCFS_NRS_SUPP_NAME_READWRITES        "reads_and_writes"
1618
1619 /**
1620  * Translates enum nrs_orr_supp values to a corresponding string.
1621  */
1622 static const char *nrs_orr_supp2str(enum nrs_orr_supp supp)
1623 {
1624         switch(supp) {
1625         default:
1626                 LBUG();
1627         case NOS_OST_READ:
1628                 return LPROCFS_NRS_SUPP_NAME_READS;
1629         case NOS_OST_WRITE:
1630                 return LPROCFS_NRS_SUPP_NAME_WRITES;
1631         case NOS_OST_RW:
1632                 return LPROCFS_NRS_SUPP_NAME_READWRITES;
1633         }
1634 }
1635
1636 /**
1637  * Translates strings to the corresponding enum nrs_orr_supp value
1638  */
1639 static enum nrs_orr_supp nrs_orr_str2supp(const char *val)
1640 {
1641         if (strncmp(val, LPROCFS_NRS_SUPP_NAME_READWRITES,
1642                     sizeof(LPROCFS_NRS_SUPP_NAME_READWRITES) - 1) == 0)
1643                 return NOS_OST_RW;
1644         else if (strncmp(val, LPROCFS_NRS_SUPP_NAME_READS,
1645                          sizeof(LPROCFS_NRS_SUPP_NAME_READS) - 1) == 0)
1646                 return NOS_OST_READ;
1647         else if (strncmp(val, LPROCFS_NRS_SUPP_NAME_WRITES,
1648                          sizeof(LPROCFS_NRS_SUPP_NAME_WRITES) - 1) == 0)
1649                 return NOS_OST_WRITE;
1650         else
1651                 return -EINVAL;
1652 }
1653
1654 /**
1655  * Retrieves the type of RPCs handled at the point of invocation by ORR/TRR
1656  * policy instances on both the regular and high-priority NRS head of a service,
1657  * as long as a policy instance is not in the
1658  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state; policy instances in this
1659  * state are skipped later by nrs_orr_ctl().
1660  *
1661  * Supported RPC type information is a (reads|writes|reads_and_writes) string,
1662  * and output is in YAML format.
1663  *
1664  * For example:
1665  *
1666  *      reg_supported:reads
1667  *      hp_supported:reads_and_writes
1668  */
1669 static int
1670 ptlrpc_lprocfs_nrs_orr_supported_seq_show(struct seq_file *m, void *data)
1671 {
1672         struct nrs_lprocfs_orr_data *orr_data = m->private;
1673         struct ptlrpc_service       *svc = orr_data->svc;
1674         enum nrs_orr_supp            supported;
1675         int                          rc;
1676
1677         /**
1678          * Perform two separate calls to this as only one of the NRS heads'
1679          * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED
1680          * or ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
1681          */
1682         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1683                                        orr_data->name,
1684                                        NRS_CTL_ORR_RD_SUPP_REQ, true,
1685                                        &supported);
1686
1687         if (rc == 0) {
1688                 seq_printf(m, NRS_LPROCFS_REQ_SUPP_NAME_REG"%s\n",
1689                            nrs_orr_supp2str(supported));
1690                 /**
1691                  * Ignore -ENODEV as the regular NRS head's policy may be in the
1692                  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1693                  */
1694         } else if (rc != -ENODEV) {
1695                 return rc;
1696         }
1697
1698         /**
1699          * We know the ost_io service which is the only one ORR/TRR policies are
1700          * compatible with, do have an HP NRS head, but it may be best to guard
1701          * against a possible change of this in the future.
1702          */
1703         if (!nrs_svc_has_hp(svc))
1704                 goto no_hp;
1705
1706         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1707                                        orr_data->name,
1708                                        NRS_CTL_ORR_RD_SUPP_REQ, true,
1709                                        &supported);
1710         if (rc == 0) {
1711                 seq_printf(m, NRS_LPROCFS_REQ_SUPP_NAME_HP"%s\n",
1712                            nrs_orr_supp2str(supported));
1713                 /**
1714                  * Ignore -ENODEV as the high priority NRS head's policy may be
1715                  * in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1716                  */
1717         } else if (rc != -ENODEV) {
1718                 return rc;
1719         }
1720
1721 no_hp:
1722
1723         return rc;
1724 }
1725
1726 /**
1727  * Max valid command string is the size of the labels, plus "reads_and_writes"
1728  * twice, plus a separating ' '
1729  */
1730 #define LPROCFS_NRS_WR_REQ_SUPP_MAX_CMD                                        \
1731         sizeof(NRS_LPROCFS_REQ_SUPP_NAME_REG LPROCFS_NRS_SUPP_NAME_READWRITES  \
1732                NRS_LPROCFS_REQ_SUPP_NAME_HP LPROCFS_NRS_SUPP_NAME_READWRITES   \
1733                " ")
1734
1735 /**
1736  * Sets the type of RPCs handled by ORR/TRR policy instances. The user can
1737  * modify this setting for the regular or high priority NRS heads separately, or
1738  * both together in a single invocation.
1739  *
1740  * For example:
1741  *
1742  * lctl set_param ost.OSS.ost_io.nrs_orr_supported=
1743  * "reg_supported:reads", to enable the ORR policy instance on the regular NRS
1744  * head of the ost_io service to handle OST_READ RPCs.
1745  *
1746  * lctl set_param ost.OSS.ost_io.nrs_trr_supported=reads_and_writes, to enable
1747  * the TRR policy instances on both the regular ang high priority NRS heads of
1748  * the ost_io service to use handle OST_READ and OST_WRITE RPCs.
1749  *
1750  * policy instances in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state are
1751  * are skipped later by nrs_orr_ctl().
1752  */
1753 static ssize_t
1754 ptlrpc_lprocfs_nrs_orr_supported_seq_write(struct file *file,
1755                                            const char *buffer, size_t count,
1756                                            loff_t *off)
1757 {
1758         struct seq_file             *m = file->private_data;
1759         struct nrs_lprocfs_orr_data *orr_data = m->private;
1760         struct ptlrpc_service       *svc = orr_data->svc;
1761         enum ptlrpc_nrs_queue_type   queue = 0;
1762         char                         kernbuf[LPROCFS_NRS_WR_REQ_SUPP_MAX_CMD];
1763         char                        *val_reg;
1764         char                        *val_hp;
1765         enum nrs_orr_supp            supp_reg;
1766         enum nrs_orr_supp            supp_hp;
1767         size_t                       count_copy;
1768         int                          rc = 0;
1769         int                          rc2 = 0;
1770
1771         if (count > (sizeof(kernbuf) - 1))
1772                 return -EINVAL;
1773
1774         if (copy_from_user(kernbuf, buffer, count))
1775                 return -EFAULT;
1776
1777         kernbuf[count] = '\0';
1778
1779         count_copy = count;
1780
1781         /**
1782          * Check if the regular supported requests setting has been specified
1783          */
1784         val_reg = lprocfs_find_named_value(kernbuf,
1785                                            NRS_LPROCFS_REQ_SUPP_NAME_REG,
1786                                            &count_copy);
1787         if (val_reg != kernbuf)
1788                 queue |= PTLRPC_NRS_QUEUE_REG;
1789
1790         count_copy = count;
1791
1792         /**
1793          * Check if the high priority supported requests setting has been
1794          * specified
1795          */
1796         val_hp = lprocfs_find_named_value(kernbuf, NRS_LPROCFS_REQ_SUPP_NAME_HP,
1797                                           &count_copy);
1798         if (val_hp != kernbuf) {
1799                 if (!nrs_svc_has_hp(svc))
1800                         return -ENODEV;
1801
1802                 queue |= PTLRPC_NRS_QUEUE_HP;
1803         }
1804
1805         /**
1806          * If none of the queues has been specified, there may be a valid
1807          * command string at the start of the buffer.
1808          */
1809         if (queue == 0) {
1810                 queue = PTLRPC_NRS_QUEUE_REG;
1811
1812                 if (nrs_svc_has_hp(svc))
1813                         queue |= PTLRPC_NRS_QUEUE_HP;
1814         }
1815
1816         if ((queue & PTLRPC_NRS_QUEUE_REG) != 0) {
1817                 supp_reg = nrs_orr_str2supp(val_reg);
1818                 if (supp_reg == -EINVAL)
1819                         return -EINVAL;
1820         }
1821
1822         if ((queue & PTLRPC_NRS_QUEUE_HP) != 0) {
1823                 supp_hp = nrs_orr_str2supp(val_hp);
1824                 if (supp_hp == -EINVAL)
1825                         return -EINVAL;
1826         }
1827
1828         /**
1829          * We change the values on regular and HP NRS heads separately, so that
1830          * we do not exit early from ptlrpc_nrs_policy_control() with an error
1831          * returned by nrs_policy_ctl_locked(), in cases where the user has not
1832          * started the policy on either the regular or HP NRS head; i.e. we are
1833          * ignoring -ENODEV within nrs_policy_ctl_locked(). -ENODEV is returned
1834          * only if the operation fails with -ENODEV on all heads that have been
1835          * specified by the command; if at least one operation succeeds,
1836          * success is returned.
1837          */
1838         if ((queue & PTLRPC_NRS_QUEUE_REG) != 0) {
1839                 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1840                                                orr_data->name,
1841                                                NRS_CTL_ORR_WR_SUPP_REQ, false,
1842                                                &supp_reg);
1843                 if ((rc < 0 && rc != -ENODEV) ||
1844                     (rc == -ENODEV && queue == PTLRPC_NRS_QUEUE_REG))
1845                         return rc;
1846         }
1847
1848         if ((queue & PTLRPC_NRS_QUEUE_HP) != 0) {
1849                 rc2 = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1850                                                 orr_data->name,
1851                                                 NRS_CTL_ORR_WR_SUPP_REQ, false,
1852                                                 &supp_hp);
1853                 if ((rc2 < 0 && rc2 != -ENODEV) ||
1854                     (rc2 == -ENODEV && queue == PTLRPC_NRS_QUEUE_HP))
1855                         return rc2;
1856         }
1857
1858         return rc == -ENODEV && rc2 == -ENODEV ? -ENODEV : count;
1859 }
1860 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_orr_supported);
1861
1862 static int nrs_orr_lprocfs_init(struct ptlrpc_service *svc)
1863 {
1864         int     i;
1865
1866         struct lprocfs_vars nrs_orr_lprocfs_vars[] = {
1867                 { .name         = "nrs_orr_quantum",
1868                   .fops         = &ptlrpc_lprocfs_nrs_orr_quantum_fops  },
1869                 { .name         = "nrs_orr_offset_type",
1870                   .fops         = &ptlrpc_lprocfs_nrs_orr_offset_type_fops },
1871                 { .name         = "nrs_orr_supported",
1872                   .fops         = &ptlrpc_lprocfs_nrs_orr_supported_fops },
1873                 { NULL }
1874         };
1875
1876         if (svc->srv_procroot == NULL)
1877                 return 0;
1878
1879         lprocfs_orr_data.svc = svc;
1880
1881         for (i = 0; i < ARRAY_SIZE(nrs_orr_lprocfs_vars); i++)
1882                 nrs_orr_lprocfs_vars[i].data = &lprocfs_orr_data;
1883
1884         return lprocfs_add_vars(svc->srv_procroot, nrs_orr_lprocfs_vars, NULL);
1885 }
1886
1887 static void nrs_orr_lprocfs_fini(struct ptlrpc_service *svc)
1888 {
1889         if (svc->srv_procroot == NULL)
1890                 return;
1891
1892         lprocfs_remove_proc_entry("nrs_orr_quantum", svc->srv_procroot);
1893         lprocfs_remove_proc_entry("nrs_orr_offset_type", svc->srv_procroot);
1894         lprocfs_remove_proc_entry("nrs_orr_supported", svc->srv_procroot);
1895 }
1896
1897 #endif /* CONFIG_PROC_FS */
1898
1899 static const struct ptlrpc_nrs_pol_ops nrs_orr_ops = {
1900         .op_policy_init         = nrs_orr_init,
1901         .op_policy_start        = nrs_orr_start,
1902         .op_policy_stop         = nrs_orr_stop,
1903         .op_policy_ctl          = nrs_orr_ctl,
1904         .op_res_get             = nrs_orr_res_get,
1905         .op_res_put             = nrs_orr_res_put,
1906         .op_req_get             = nrs_orr_req_get,
1907         .op_req_enqueue         = nrs_orr_req_add,
1908         .op_req_dequeue         = nrs_orr_req_del,
1909         .op_req_stop            = nrs_orr_req_stop,
1910 #ifdef CONFIG_PROC_FS
1911         .op_lprocfs_init        = nrs_orr_lprocfs_init,
1912         .op_lprocfs_fini        = nrs_orr_lprocfs_fini,
1913 #endif
1914 };
1915
1916 struct ptlrpc_nrs_pol_conf nrs_conf_orr = {
1917         .nc_name                = NRS_POL_NAME_ORR,
1918         .nc_ops                 = &nrs_orr_ops,
1919         .nc_compat              = nrs_policy_compat_one,
1920         .nc_compat_svc_name     = "ost_io",
1921 };
1922
1923 /**
1924  * TRR, Target-based Round Robin policy
1925  *
1926  * TRR reuses much of the functions and data structures of ORR
1927  */
1928
1929 #ifdef CONFIG_PROC_FS
1930
1931 static int nrs_trr_lprocfs_init(struct ptlrpc_service *svc)
1932 {
1933         int     i;
1934
1935         struct lprocfs_vars nrs_trr_lprocfs_vars[] = {
1936                 { .name         = "nrs_trr_quantum",
1937                   .fops         = &ptlrpc_lprocfs_nrs_orr_quantum_fops },
1938                 { .name         = "nrs_trr_offset_type",
1939                   .fops         = &ptlrpc_lprocfs_nrs_orr_offset_type_fops },
1940                 { .name         = "nrs_trr_supported",
1941                   .fops         = &ptlrpc_lprocfs_nrs_orr_supported_fops },
1942                 { NULL }
1943         };
1944
1945         if (svc->srv_procroot == NULL)
1946                 return 0;
1947
1948         lprocfs_trr_data.svc = svc;
1949
1950         for (i = 0; i < ARRAY_SIZE(nrs_trr_lprocfs_vars); i++)
1951                 nrs_trr_lprocfs_vars[i].data = &lprocfs_trr_data;
1952
1953         return lprocfs_add_vars(svc->srv_procroot, nrs_trr_lprocfs_vars, NULL);
1954 }
1955
1956 static void nrs_trr_lprocfs_fini(struct ptlrpc_service *svc)
1957 {
1958         if (svc->srv_procroot == NULL)
1959                 return;
1960
1961         lprocfs_remove_proc_entry("nrs_trr_quantum", svc->srv_procroot);
1962         lprocfs_remove_proc_entry("nrs_trr_offset_type", svc->srv_procroot);
1963         lprocfs_remove_proc_entry("nrs_trr_supported", svc->srv_procroot);
1964 }
1965
1966 #endif /* CONFIG_PROC_FS */
1967
1968 /**
1969  * Reuse much of the ORR functionality for TRR.
1970  */
1971 static const struct ptlrpc_nrs_pol_ops nrs_trr_ops = {
1972         .op_policy_init         = nrs_orr_init,
1973         .op_policy_start        = nrs_orr_start,
1974         .op_policy_stop         = nrs_orr_stop,
1975         .op_policy_ctl          = nrs_orr_ctl,
1976         .op_res_get             = nrs_orr_res_get,
1977         .op_res_put             = nrs_orr_res_put,
1978         .op_req_get             = nrs_orr_req_get,
1979         .op_req_enqueue         = nrs_orr_req_add,
1980         .op_req_dequeue         = nrs_orr_req_del,
1981         .op_req_stop            = nrs_orr_req_stop,
1982 #ifdef CONFIG_PROC_FS
1983         .op_lprocfs_init        = nrs_trr_lprocfs_init,
1984         .op_lprocfs_fini        = nrs_trr_lprocfs_fini,
1985 #endif
1986 };
1987
1988 struct ptlrpc_nrs_pol_conf nrs_conf_trr = {
1989         .nc_name                = NRS_POL_NAME_TRR,
1990         .nc_ops                 = &nrs_trr_ops,
1991         .nc_compat              = nrs_policy_compat_one,
1992         .nc_compat_svc_name     = "ost_io",
1993 };
1994
1995 /** @} ORR/TRR policy */
1996
1997 /** @} nrs */
1998
1999 #endif /* HAVE_SERVER_SUPPORT */