Whamcloud - gitweb
LU-7623 Add __user to seq_write buffer arguments
[fs/lustre-release.git] / lustre / ptlrpc / nrs_tbf.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 DataDirect Networks, Inc.
24  *
25  * Copyright (c) 2014, Intel Corporation.
26  */
27 /*
28  * lustre/ptlrpc/nrs_tbf.c
29  *
30  * Network Request Scheduler (NRS) Token Bucket Filter(TBF) policy
31  *
32  */
33
34 #ifdef HAVE_SERVER_SUPPORT
35
36 /**
37  * \addtogoup nrs
38  * @{
39  */
40
41 #define DEBUG_SUBSYSTEM S_RPC
42 #include <obd_support.h>
43 #include <obd_class.h>
44 #include <libcfs/libcfs.h>
45 #include "ptlrpc_internal.h"
46
47 /**
48  * \name tbf
49  *
50  * Token Bucket Filter over client NIDs
51  *
52  * @{
53  */
54
55 #define NRS_POL_NAME_TBF        "tbf"
56
57 static int tbf_jobid_cache_size = 8192;
58 CFS_MODULE_PARM(tbf_jobid_cache_size, "i", int, 0644,
59                 "The size of jobid cache");
60
61 static int tbf_rate = 10000;
62 CFS_MODULE_PARM(tbf_rate, "i", int, 0644,
63                 "Default rate limit in RPCs/s");
64
65 static int tbf_depth = 3;
66 CFS_MODULE_PARM(tbf_depth, "i", int, 0644,
67                 "How many tokens that a client can save up");
68
69 static enum hrtimer_restart nrs_tbf_timer_cb(struct hrtimer *timer)
70 {
71         struct nrs_tbf_head *head = container_of(timer, struct nrs_tbf_head,
72                                                  th_timer);
73         struct ptlrpc_nrs   *nrs = head->th_res.res_policy->pol_nrs;
74         struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
75
76         spin_lock(&nrs->nrs_lock);
77         nrs->nrs_throttling = 0;
78         spin_unlock(&nrs->nrs_lock);
79         wake_up(&svcpt->scp_waitq);
80
81         return HRTIMER_NORESTART;
82 }
83
84 #define NRS_TBF_DEFAULT_RULE "default"
85
86 static void nrs_tbf_rule_fini(struct nrs_tbf_rule *rule)
87 {
88         LASSERT(atomic_read(&rule->tr_ref) == 0);
89         LASSERT(list_empty(&rule->tr_cli_list));
90         LASSERT(list_empty(&rule->tr_linkage));
91
92         rule->tr_head->th_ops->o_rule_fini(rule);
93         OBD_FREE_PTR(rule);
94 }
95
96 /**
97  * Decreases the rule's usage reference count, and stops the rule in case it
98  * was already stopping and have no more outstanding usage references (which
99  * indicates it has no more queued or started requests, and can be safely
100  * stopped).
101  */
102 static void nrs_tbf_rule_put(struct nrs_tbf_rule *rule)
103 {
104         if (atomic_dec_and_test(&rule->tr_ref))
105                 nrs_tbf_rule_fini(rule);
106 }
107
108 /**
109  * Increases the rule's usage reference count.
110  */
111 static inline void nrs_tbf_rule_get(struct nrs_tbf_rule *rule)
112 {
113         atomic_inc(&rule->tr_ref);
114 }
115
116 static void
117 nrs_tbf_cli_rule_put(struct nrs_tbf_client *cli)
118 {
119         LASSERT(!list_empty(&cli->tc_linkage));
120         LASSERT(cli->tc_rule);
121         list_del_init(&cli->tc_linkage);
122         nrs_tbf_rule_put(cli->tc_rule);
123         cli->tc_rule = NULL;
124 }
125
126 static void
127 nrs_tbf_cli_reset_value(struct nrs_tbf_head *head,
128                         struct nrs_tbf_client *cli)
129
130 {
131         struct nrs_tbf_rule *rule = cli->tc_rule;
132
133         cli->tc_rpc_rate = rule->tr_rpc_rate;
134         cli->tc_nsecs = rule->tr_nsecs;
135         cli->tc_depth = rule->tr_depth;
136         cli->tc_ntoken = rule->tr_depth;
137         cli->tc_check_time = ktime_to_ns(ktime_get());
138         cli->tc_rule_sequence = atomic_read(&head->th_rule_sequence);
139         cli->tc_rule_generation = rule->tr_generation;
140
141         if (cli->tc_in_heap)
142                 cfs_binheap_relocate(head->th_binheap,
143                                      &cli->tc_node);
144 }
145
146 static void
147 nrs_tbf_cli_reset(struct nrs_tbf_head *head,
148                   struct nrs_tbf_rule *rule,
149                   struct nrs_tbf_client *cli)
150 {
151         if (!list_empty(&cli->tc_linkage)) {
152                 LASSERT(rule != cli->tc_rule);
153                 nrs_tbf_cli_rule_put(cli);
154         }
155         LASSERT(cli->tc_rule == NULL);
156         LASSERT(list_empty(&cli->tc_linkage));
157         /* Rule's ref is added before called */
158         cli->tc_rule = rule;
159         list_add_tail(&cli->tc_linkage, &rule->tr_cli_list);
160         nrs_tbf_cli_reset_value(head, cli);
161 }
162
163 static int
164 nrs_tbf_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
165 {
166         return rule->tr_head->th_ops->o_rule_dump(rule, m);
167 }
168
169 static int
170 nrs_tbf_rule_dump_all(struct nrs_tbf_head *head, struct seq_file *m)
171 {
172         struct nrs_tbf_rule *rule;
173         int rc = 0;
174
175         LASSERT(head != NULL);
176         spin_lock(&head->th_rule_lock);
177         /* List the rules from newest to oldest */
178         list_for_each_entry(rule, &head->th_list, tr_linkage) {
179                 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
180                 rc = nrs_tbf_rule_dump(rule, m);
181                 if (rc) {
182                         rc = -ENOSPC;
183                         break;
184                 }
185         }
186         spin_unlock(&head->th_rule_lock);
187
188         return rc;
189 }
190
191 static struct nrs_tbf_rule *
192 nrs_tbf_rule_find_nolock(struct nrs_tbf_head *head,
193                          const char *name)
194 {
195         struct nrs_tbf_rule *rule;
196
197         LASSERT(head != NULL);
198         list_for_each_entry(rule, &head->th_list, tr_linkage) {
199                 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
200                 if (strcmp(rule->tr_name, name) == 0) {
201                         nrs_tbf_rule_get(rule);
202                         return rule;
203                 }
204         }
205         return NULL;
206 }
207
208 static struct nrs_tbf_rule *
209 nrs_tbf_rule_find(struct nrs_tbf_head *head,
210                   const char *name)
211 {
212         struct nrs_tbf_rule *rule;
213
214         LASSERT(head != NULL);
215         spin_lock(&head->th_rule_lock);
216         rule = nrs_tbf_rule_find_nolock(head, name);
217         spin_unlock(&head->th_rule_lock);
218         return rule;
219 }
220
221 static struct nrs_tbf_rule *
222 nrs_tbf_rule_match(struct nrs_tbf_head *head,
223                    struct nrs_tbf_client *cli)
224 {
225         struct nrs_tbf_rule *rule = NULL;
226         struct nrs_tbf_rule *tmp_rule;
227
228         spin_lock(&head->th_rule_lock);
229         /* Match the newest rule in the list */
230         list_for_each_entry(tmp_rule, &head->th_list, tr_linkage) {
231                 LASSERT((tmp_rule->tr_flags & NTRS_STOPPING) == 0);
232                 if (head->th_ops->o_rule_match(tmp_rule, cli)) {
233                         rule = tmp_rule;
234                         break;
235                 }
236         }
237
238         if (rule == NULL)
239                 rule = head->th_rule;
240
241         nrs_tbf_rule_get(rule);
242         spin_unlock(&head->th_rule_lock);
243         return rule;
244 }
245
246 static void
247 nrs_tbf_cli_init(struct nrs_tbf_head *head,
248                  struct nrs_tbf_client *cli,
249                  struct ptlrpc_request *req)
250 {
251         struct nrs_tbf_rule *rule;
252
253         cli->tc_in_heap = false;
254         head->th_ops->o_cli_init(cli, req);
255         INIT_LIST_HEAD(&cli->tc_list);
256         INIT_LIST_HEAD(&cli->tc_linkage);
257         atomic_set(&cli->tc_ref, 1);
258         rule = nrs_tbf_rule_match(head, cli);
259         nrs_tbf_cli_reset(head, rule, cli);
260 }
261
262 static void
263 nrs_tbf_cli_fini(struct nrs_tbf_client *cli)
264 {
265         LASSERT(list_empty(&cli->tc_list));
266         LASSERT(!cli->tc_in_heap);
267         LASSERT(atomic_read(&cli->tc_ref) == 0);
268         nrs_tbf_cli_rule_put(cli);
269         OBD_FREE_PTR(cli);
270 }
271
272 static int
273 nrs_tbf_rule_start(struct ptlrpc_nrs_policy *policy,
274                    struct nrs_tbf_head *head,
275                    struct nrs_tbf_cmd *start)
276 {
277         struct nrs_tbf_rule *rule, *tmp_rule;
278         int rc;
279
280         rule = nrs_tbf_rule_find(head, start->tc_name);
281         if (rule) {
282                 nrs_tbf_rule_put(rule);
283                 return -EEXIST;
284         }
285
286         OBD_CPT_ALLOC_PTR(rule, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
287         if (rule == NULL)
288                 return -ENOMEM;
289
290         memcpy(rule->tr_name, start->tc_name, strlen(start->tc_name));
291         rule->tr_rpc_rate = start->tc_rpc_rate;
292         rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
293         rule->tr_depth = tbf_depth;
294         atomic_set(&rule->tr_ref, 1);
295         INIT_LIST_HEAD(&rule->tr_cli_list);
296         INIT_LIST_HEAD(&rule->tr_nids);
297
298         rc = head->th_ops->o_rule_init(policy, rule, start);
299         if (rc) {
300                 OBD_FREE_PTR(rule);
301                 return rc;
302         }
303
304         /* Add as the newest rule */
305         spin_lock(&head->th_rule_lock);
306         tmp_rule = nrs_tbf_rule_find_nolock(head, start->tc_name);
307         if (tmp_rule) {
308                 spin_unlock(&head->th_rule_lock);
309                 nrs_tbf_rule_put(tmp_rule);
310                 nrs_tbf_rule_put(rule);
311                 return -EEXIST;
312         }
313         list_add(&rule->tr_linkage, &head->th_list);
314         rule->tr_head = head;
315         spin_unlock(&head->th_rule_lock);
316         atomic_inc(&head->th_rule_sequence);
317         if (start->tc_rule_flags & NTRS_DEFAULT) {
318                 rule->tr_flags |= NTRS_DEFAULT;
319                 LASSERT(head->th_rule == NULL);
320                 head->th_rule = rule;
321         }
322
323         return 0;
324 }
325
326 static int
327 nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
328                     struct nrs_tbf_head *head,
329                     struct nrs_tbf_cmd *change)
330 {
331         struct nrs_tbf_rule *rule;
332
333         assert_spin_locked(&policy->pol_nrs->nrs_lock);
334
335         rule = nrs_tbf_rule_find(head, change->tc_name);
336         if (rule == NULL)
337                 return -ENOENT;
338
339         rule->tr_rpc_rate = change->tc_rpc_rate;
340         rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
341         rule->tr_generation++;
342         nrs_tbf_rule_put(rule);
343
344         return 0;
345 }
346
347 static int
348 nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
349                   struct nrs_tbf_head *head,
350                   struct nrs_tbf_cmd *stop)
351 {
352         struct nrs_tbf_rule *rule;
353
354         assert_spin_locked(&policy->pol_nrs->nrs_lock);
355
356         if (strcmp(stop->tc_name, NRS_TBF_DEFAULT_RULE) == 0)
357                 return -EPERM;
358
359         rule = nrs_tbf_rule_find(head, stop->tc_name);
360         if (rule == NULL)
361                 return -ENOENT;
362
363         list_del_init(&rule->tr_linkage);
364         rule->tr_flags |= NTRS_STOPPING;
365         nrs_tbf_rule_put(rule);
366         nrs_tbf_rule_put(rule);
367
368         return 0;
369 }
370
371 static int
372 nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
373                 struct nrs_tbf_head *head,
374                 struct nrs_tbf_cmd *cmd)
375 {
376         int rc;
377
378         assert_spin_locked(&policy->pol_nrs->nrs_lock);
379
380         switch (cmd->tc_cmd) {
381         case NRS_CTL_TBF_START_RULE:
382                 if (!(cmd->tc_valid_types & head->th_type_flag))
383                         return -EINVAL;
384
385                 spin_unlock(&policy->pol_nrs->nrs_lock);
386                 rc = nrs_tbf_rule_start(policy, head, cmd);
387                 spin_lock(&policy->pol_nrs->nrs_lock);
388                 return rc;
389         case NRS_CTL_TBF_CHANGE_RATE:
390                 rc = nrs_tbf_rule_change(policy, head, cmd);
391                 return rc;
392         case NRS_CTL_TBF_STOP_RULE:
393                 rc = nrs_tbf_rule_stop(policy, head, cmd);
394                 /* Take it as a success, if not exists at all */
395                 return rc == -ENOENT ? 0 : rc;
396         default:
397                 return -EFAULT;
398         }
399 }
400
401 /**
402  * Binary heap predicate.
403  *
404  * \param[in] e1 the first binheap node to compare
405  * \param[in] e2 the second binheap node to compare
406  *
407  * \retval 0 e1 > e2
408  * \retval 1 e1 < e2
409  */
410 static int tbf_cli_compare(cfs_binheap_node_t *e1, cfs_binheap_node_t *e2)
411 {
412         struct nrs_tbf_client *cli1;
413         struct nrs_tbf_client *cli2;
414
415         cli1 = container_of(e1, struct nrs_tbf_client, tc_node);
416         cli2 = container_of(e2, struct nrs_tbf_client, tc_node);
417
418         if (cli1->tc_check_time + cli1->tc_nsecs <
419             cli2->tc_check_time + cli2->tc_nsecs)
420                 return 1;
421         else if (cli1->tc_check_time + cli1->tc_nsecs >
422                  cli2->tc_check_time + cli2->tc_nsecs)
423                 return 0;
424
425         if (cli1->tc_check_time < cli2->tc_check_time)
426                 return 1;
427         else if (cli1->tc_check_time > cli2->tc_check_time)
428                 return 0;
429
430         /* Maybe need more comparasion, e.g. request number in the rules */
431         return 1;
432 }
433
434 /**
435  * TBF binary heap operations
436  */
437 static cfs_binheap_ops_t nrs_tbf_heap_ops = {
438         .hop_enter      = NULL,
439         .hop_exit       = NULL,
440         .hop_compare    = tbf_cli_compare,
441 };
442
443 static unsigned nrs_tbf_jobid_hop_hash(struct cfs_hash *hs, const void *key,
444                                   unsigned mask)
445 {
446         return cfs_hash_djb2_hash(key, strlen(key), mask);
447 }
448
449 static int nrs_tbf_jobid_hop_keycmp(const void *key, struct hlist_node *hnode)
450 {
451         struct nrs_tbf_client *cli = hlist_entry(hnode,
452                                                      struct nrs_tbf_client,
453                                                      tc_hnode);
454
455         return (strcmp(cli->tc_jobid, key) == 0);
456 }
457
458 static void *nrs_tbf_jobid_hop_key(struct hlist_node *hnode)
459 {
460         struct nrs_tbf_client *cli = hlist_entry(hnode,
461                                                      struct nrs_tbf_client,
462                                                      tc_hnode);
463
464         return cli->tc_jobid;
465 }
466
467 static void *nrs_tbf_jobid_hop_object(struct hlist_node *hnode)
468 {
469         return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
470 }
471
472 static void nrs_tbf_jobid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
473 {
474         struct nrs_tbf_client *cli = hlist_entry(hnode,
475                                                      struct nrs_tbf_client,
476                                                      tc_hnode);
477
478         atomic_inc(&cli->tc_ref);
479 }
480
481 static void nrs_tbf_jobid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
482 {
483         struct nrs_tbf_client *cli = hlist_entry(hnode,
484                                                      struct nrs_tbf_client,
485                                                      tc_hnode);
486
487         atomic_dec(&cli->tc_ref);
488 }
489
490 static void
491 nrs_tbf_jobid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
492
493 {
494         struct nrs_tbf_client *cli = hlist_entry(hnode,
495                                                  struct nrs_tbf_client,
496                                                  tc_hnode);
497
498         LASSERT(atomic_read(&cli->tc_ref) == 0);
499         nrs_tbf_cli_fini(cli);
500 }
501
502 static struct cfs_hash_ops nrs_tbf_jobid_hash_ops = {
503         .hs_hash        = nrs_tbf_jobid_hop_hash,
504         .hs_keycmp      = nrs_tbf_jobid_hop_keycmp,
505         .hs_key         = nrs_tbf_jobid_hop_key,
506         .hs_object      = nrs_tbf_jobid_hop_object,
507         .hs_get         = nrs_tbf_jobid_hop_get,
508         .hs_put         = nrs_tbf_jobid_hop_put,
509         .hs_put_locked  = nrs_tbf_jobid_hop_put,
510         .hs_exit        = nrs_tbf_jobid_hop_exit,
511 };
512
513 #define NRS_TBF_JOBID_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
514                                   CFS_HASH_NO_ITEMREF | \
515                                   CFS_HASH_DEPTH)
516
517 static struct nrs_tbf_client *
518 nrs_tbf_jobid_hash_lookup(struct cfs_hash *hs,
519                           struct cfs_hash_bd *bd,
520                           const char *jobid)
521 {
522         struct hlist_node *hnode;
523         struct nrs_tbf_client *cli;
524
525         /* cfs_hash_bd_peek_locked is a somehow "internal" function
526          * of cfs_hash, it doesn't add refcount on object. */
527         hnode = cfs_hash_bd_peek_locked(hs, bd, (void *)jobid);
528         if (hnode == NULL)
529                 return NULL;
530
531         cfs_hash_get(hs, hnode);
532         cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
533         if (!list_empty(&cli->tc_lru))
534                 list_del_init(&cli->tc_lru);
535         return cli;
536 }
537
538 #define NRS_TBF_JOBID_NULL ""
539
540 static struct nrs_tbf_client *
541 nrs_tbf_jobid_cli_find(struct nrs_tbf_head *head,
542                        struct ptlrpc_request *req)
543 {
544         const char              *jobid;
545         struct nrs_tbf_client   *cli;
546         struct cfs_hash         *hs = head->th_cli_hash;
547         struct cfs_hash_bd               bd;
548
549         jobid = lustre_msg_get_jobid(req->rq_reqmsg);
550         if (jobid == NULL)
551                 jobid = NRS_TBF_JOBID_NULL;
552         cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
553         cli = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
554         cfs_hash_bd_unlock(hs, &bd, 1);
555
556         return cli;
557 }
558
559 static struct nrs_tbf_client *
560 nrs_tbf_jobid_cli_findadd(struct nrs_tbf_head *head,
561                           struct nrs_tbf_client *cli)
562 {
563         const char              *jobid;
564         struct nrs_tbf_client   *ret;
565         struct cfs_hash         *hs = head->th_cli_hash;
566         struct cfs_hash_bd               bd;
567
568         jobid = cli->tc_jobid;
569         cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
570         ret = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
571         if (ret == NULL) {
572                 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
573                 ret = cli;
574         }
575         cfs_hash_bd_unlock(hs, &bd, 1);
576
577         return ret;
578 }
579
580 static void
581 nrs_tbf_jobid_cli_put(struct nrs_tbf_head *head,
582                       struct nrs_tbf_client *cli)
583 {
584         struct cfs_hash_bd               bd;
585         struct cfs_hash         *hs = head->th_cli_hash;
586         struct nrs_tbf_bucket   *bkt;
587         int                      hw;
588         struct list_head        zombies;
589
590         INIT_LIST_HEAD(&zombies);
591         cfs_hash_bd_get(hs, &cli->tc_jobid, &bd);
592         bkt = cfs_hash_bd_extra_get(hs, &bd);
593         if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
594                 return;
595         LASSERT(list_empty(&cli->tc_lru));
596         list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
597
598         /*
599          * Check and purge the LRU, there is at least one client in the LRU.
600          */
601         hw = tbf_jobid_cache_size >>
602              (hs->hs_cur_bits - hs->hs_bkt_bits);
603         while (cfs_hash_bd_count_get(&bd) > hw) {
604                 if (unlikely(list_empty(&bkt->ntb_lru)))
605                         break;
606                 cli = list_entry(bkt->ntb_lru.next,
607                                      struct nrs_tbf_client,
608                                      tc_lru);
609                 LASSERT(atomic_read(&cli->tc_ref) == 0);
610                 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
611                 list_move(&cli->tc_lru, &zombies);
612         }
613         cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
614
615         while (!list_empty(&zombies)) {
616                 cli = container_of0(zombies.next,
617                                     struct nrs_tbf_client, tc_lru);
618                 list_del_init(&cli->tc_lru);
619                 nrs_tbf_cli_fini(cli);
620         }
621 }
622
623 static void
624 nrs_tbf_jobid_cli_init(struct nrs_tbf_client *cli,
625                        struct ptlrpc_request *req)
626 {
627         char *jobid = lustre_msg_get_jobid(req->rq_reqmsg);
628
629         if (jobid == NULL)
630                 jobid = NRS_TBF_JOBID_NULL;
631         LASSERT(strlen(jobid) < LUSTRE_JOBID_SIZE);
632         INIT_LIST_HEAD(&cli->tc_lru);
633         memcpy(cli->tc_jobid, jobid, strlen(jobid));
634 }
635
636 static int nrs_tbf_jobid_hash_order(void)
637 {
638         int bits;
639
640         for (bits = 1; (1 << bits) < tbf_jobid_cache_size; ++bits)
641                 ;
642
643         return bits;
644 }
645
646 #define NRS_TBF_JOBID_BKT_BITS 10
647
648 static int
649 nrs_tbf_jobid_startup(struct ptlrpc_nrs_policy *policy,
650                       struct nrs_tbf_head *head)
651 {
652         struct nrs_tbf_cmd       start;
653         struct nrs_tbf_bucket   *bkt;
654         int                      bits;
655         int                      i;
656         int                      rc;
657         struct cfs_hash_bd       bd;
658
659         bits = nrs_tbf_jobid_hash_order();
660         if (bits < NRS_TBF_JOBID_BKT_BITS)
661                 bits = NRS_TBF_JOBID_BKT_BITS;
662         head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
663                                             bits,
664                                             bits,
665                                             NRS_TBF_JOBID_BKT_BITS,
666                                             sizeof(*bkt),
667                                             0,
668                                             0,
669                                             &nrs_tbf_jobid_hash_ops,
670                                             NRS_TBF_JOBID_HASH_FLAGS);
671         if (head->th_cli_hash == NULL)
672                 return -ENOMEM;
673
674         cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
675                 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
676                 INIT_LIST_HEAD(&bkt->ntb_lru);
677         }
678
679         memset(&start, 0, sizeof(start));
680         start.tc_jobids_str = "*";
681
682         start.tc_rpc_rate = tbf_rate;
683         start.tc_rule_flags = NTRS_DEFAULT;
684         start.tc_name = NRS_TBF_DEFAULT_RULE;
685         INIT_LIST_HEAD(&start.tc_jobids);
686         rc = nrs_tbf_rule_start(policy, head, &start);
687
688         return rc;
689 }
690
691 /**
692  * Frees jobid of \a list.
693  *
694  */
695 static void
696 nrs_tbf_jobid_list_free(struct list_head *jobid_list)
697 {
698         struct nrs_tbf_jobid *jobid, *n;
699
700         list_for_each_entry_safe(jobid, n, jobid_list, tj_linkage) {
701                 OBD_FREE(jobid->tj_id, strlen(jobid->tj_id) + 1);
702                 list_del(&jobid->tj_linkage);
703                 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
704         }
705 }
706
707 static int
708 nrs_tbf_jobid_list_add(const struct cfs_lstr *id, struct list_head *jobid_list)
709 {
710         struct nrs_tbf_jobid *jobid;
711
712         OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
713         if (jobid == NULL)
714                 return -ENOMEM;
715
716         OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
717         if (jobid->tj_id == NULL) {
718                 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
719                 return -ENOMEM;
720         }
721
722         memcpy(jobid->tj_id, id->ls_str, id->ls_len);
723         list_add_tail(&jobid->tj_linkage, jobid_list);
724         return 0;
725 }
726
727 static int
728 nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id)
729 {
730         struct nrs_tbf_jobid *jobid;
731
732         list_for_each_entry(jobid, jobid_list, tj_linkage) {
733                 if (strcmp(id, jobid->tj_id) == 0)
734                         return 1;
735         }
736         return 0;
737 }
738
739 static int
740 nrs_tbf_jobid_list_parse(char *str, int len, struct list_head *jobid_list)
741 {
742         struct cfs_lstr src;
743         struct cfs_lstr res;
744         int rc = 0;
745         ENTRY;
746
747         src.ls_str = str;
748         src.ls_len = len;
749         INIT_LIST_HEAD(jobid_list);
750         while (src.ls_str) {
751                 rc = cfs_gettok(&src, ' ', &res);
752                 if (rc == 0) {
753                         rc = -EINVAL;
754                         break;
755                 }
756                 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
757                 if (rc)
758                         break;
759         }
760         if (rc)
761                 nrs_tbf_jobid_list_free(jobid_list);
762         RETURN(rc);
763 }
764
765 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
766 {
767         if (!list_empty(&cmd->tc_jobids))
768                 nrs_tbf_jobid_list_free(&cmd->tc_jobids);
769         if (cmd->tc_jobids_str)
770                 OBD_FREE(cmd->tc_jobids_str, strlen(cmd->tc_jobids_str) + 1);
771 }
772
773 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, const char *id)
774 {
775         int rc;
776
777         OBD_ALLOC(cmd->tc_jobids_str, strlen(id) + 1);
778         if (cmd->tc_jobids_str == NULL)
779                 return -ENOMEM;
780
781         memcpy(cmd->tc_jobids_str, id, strlen(id));
782
783         /* parse jobid list */
784         rc = nrs_tbf_jobid_list_parse(cmd->tc_jobids_str,
785                                       strlen(cmd->tc_jobids_str),
786                                       &cmd->tc_jobids);
787         if (rc)
788                 nrs_tbf_jobid_cmd_fini(cmd);
789
790         return rc;
791 }
792
793 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
794                                    struct nrs_tbf_rule *rule,
795                                    struct nrs_tbf_cmd *start)
796 {
797         int rc = 0;
798
799         LASSERT(start->tc_jobids_str);
800         OBD_ALLOC(rule->tr_jobids_str,
801                   strlen(start->tc_jobids_str) + 1);
802         if (rule->tr_jobids_str == NULL)
803                 return -ENOMEM;
804
805         memcpy(rule->tr_jobids_str,
806                start->tc_jobids_str,
807                strlen(start->tc_jobids_str));
808
809         INIT_LIST_HEAD(&rule->tr_jobids);
810         if (!list_empty(&start->tc_jobids)) {
811                 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
812                                               strlen(rule->tr_jobids_str),
813                                               &rule->tr_jobids);
814                 if (rc)
815                         CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
816         }
817         if (rc)
818                 OBD_FREE(rule->tr_jobids_str,
819                          strlen(start->tc_jobids_str) + 1);
820         return rc;
821 }
822
823 static int
824 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
825 {
826         return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
827                           rule->tr_jobids_str, rule->tr_rpc_rate,
828                           atomic_read(&rule->tr_ref) - 1);
829 }
830
831 static int
832 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
833                          struct nrs_tbf_client *cli)
834 {
835         return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
836 }
837
838 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
839 {
840         if (!list_empty(&rule->tr_jobids))
841                 nrs_tbf_jobid_list_free(&rule->tr_jobids);
842         LASSERT(rule->tr_jobids_str != NULL);
843         OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
844 }
845
846 static struct nrs_tbf_ops nrs_tbf_jobid_ops = {
847         .o_name = NRS_TBF_TYPE_JOBID,
848         .o_startup = nrs_tbf_jobid_startup,
849         .o_cli_find = nrs_tbf_jobid_cli_find,
850         .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
851         .o_cli_put = nrs_tbf_jobid_cli_put,
852         .o_cli_init = nrs_tbf_jobid_cli_init,
853         .o_rule_init = nrs_tbf_jobid_rule_init,
854         .o_rule_dump = nrs_tbf_jobid_rule_dump,
855         .o_rule_match = nrs_tbf_jobid_rule_match,
856         .o_rule_fini = nrs_tbf_jobid_rule_fini,
857 };
858
859 /**
860  * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
861  *
862  * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
863  * nrs_tbf_client objects.
864  */
865 #define NRS_TBF_NID_BKT_BITS    8
866 #define NRS_TBF_NID_BITS        16
867
868 static unsigned nrs_tbf_nid_hop_hash(struct cfs_hash *hs, const void *key,
869                                   unsigned mask)
870 {
871         return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
872 }
873
874 static int nrs_tbf_nid_hop_keycmp(const void *key, struct hlist_node *hnode)
875 {
876         lnet_nid_t            *nid = (lnet_nid_t *)key;
877         struct nrs_tbf_client *cli = hlist_entry(hnode,
878                                                      struct nrs_tbf_client,
879                                                      tc_hnode);
880
881         return *nid == cli->tc_nid;
882 }
883
884 static void *nrs_tbf_nid_hop_key(struct hlist_node *hnode)
885 {
886         struct nrs_tbf_client *cli = hlist_entry(hnode,
887                                                      struct nrs_tbf_client,
888                                                      tc_hnode);
889
890         return &cli->tc_nid;
891 }
892
893 static void *nrs_tbf_nid_hop_object(struct hlist_node *hnode)
894 {
895         return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
896 }
897
898 static void nrs_tbf_nid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
899 {
900         struct nrs_tbf_client *cli = hlist_entry(hnode,
901                                                      struct nrs_tbf_client,
902                                                      tc_hnode);
903
904         atomic_inc(&cli->tc_ref);
905 }
906
907 static void nrs_tbf_nid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
908 {
909         struct nrs_tbf_client *cli = hlist_entry(hnode,
910                                                      struct nrs_tbf_client,
911                                                      tc_hnode);
912
913         atomic_dec(&cli->tc_ref);
914 }
915
916 static void nrs_tbf_nid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
917 {
918         struct nrs_tbf_client *cli = hlist_entry(hnode,
919                                                      struct nrs_tbf_client,
920                                                      tc_hnode);
921
922         LASSERTF(atomic_read(&cli->tc_ref) == 0,
923                  "Busy TBF object from client with NID %s, with %d refs\n",
924                  libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
925
926         nrs_tbf_cli_fini(cli);
927 }
928
929 static struct cfs_hash_ops nrs_tbf_nid_hash_ops = {
930         .hs_hash        = nrs_tbf_nid_hop_hash,
931         .hs_keycmp      = nrs_tbf_nid_hop_keycmp,
932         .hs_key         = nrs_tbf_nid_hop_key,
933         .hs_object      = nrs_tbf_nid_hop_object,
934         .hs_get         = nrs_tbf_nid_hop_get,
935         .hs_put         = nrs_tbf_nid_hop_put,
936         .hs_put_locked  = nrs_tbf_nid_hop_put,
937         .hs_exit        = nrs_tbf_nid_hop_exit,
938 };
939
940 static struct nrs_tbf_client *
941 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
942                      struct ptlrpc_request *req)
943 {
944         return cfs_hash_lookup(head->th_cli_hash, &req->rq_peer.nid);
945 }
946
947 static struct nrs_tbf_client *
948 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
949                         struct nrs_tbf_client *cli)
950 {
951         return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
952                                        &cli->tc_hnode);
953 }
954
955 static void
956 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
957                       struct nrs_tbf_client *cli)
958 {
959         cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
960 }
961
962 static int
963 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
964                     struct nrs_tbf_head *head)
965 {
966         struct nrs_tbf_cmd      start;
967         int rc;
968
969         head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
970                                             NRS_TBF_NID_BITS,
971                                             NRS_TBF_NID_BITS,
972                                             NRS_TBF_NID_BKT_BITS, 0,
973                                             CFS_HASH_MIN_THETA,
974                                             CFS_HASH_MAX_THETA,
975                                             &nrs_tbf_nid_hash_ops,
976                                             CFS_HASH_RW_BKTLOCK);
977         if (head->th_cli_hash == NULL)
978                 return -ENOMEM;
979
980         memset(&start, 0, sizeof(start));
981         start.tc_nids_str = "*";
982
983         start.tc_rpc_rate = tbf_rate;
984         start.tc_rule_flags = NTRS_DEFAULT;
985         start.tc_name = NRS_TBF_DEFAULT_RULE;
986         INIT_LIST_HEAD(&start.tc_nids);
987         rc = nrs_tbf_rule_start(policy, head, &start);
988
989         return rc;
990 }
991
992 static void
993 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
994                              struct ptlrpc_request *req)
995 {
996         cli->tc_nid = req->rq_peer.nid;
997 }
998
999 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
1000                                  struct nrs_tbf_rule *rule,
1001                                  struct nrs_tbf_cmd *start)
1002 {
1003         LASSERT(start->tc_nids_str);
1004         OBD_ALLOC(rule->tr_nids_str,
1005                   strlen(start->tc_nids_str) + 1);
1006         if (rule->tr_nids_str == NULL)
1007                 return -ENOMEM;
1008
1009         memcpy(rule->tr_nids_str,
1010                start->tc_nids_str,
1011                strlen(start->tc_nids_str));
1012
1013         INIT_LIST_HEAD(&rule->tr_nids);
1014         if (!list_empty(&start->tc_nids)) {
1015                 if (cfs_parse_nidlist(rule->tr_nids_str,
1016                                       strlen(rule->tr_nids_str),
1017                                       &rule->tr_nids) <= 0) {
1018                         CERROR("nids {%s} illegal\n",
1019                                rule->tr_nids_str);
1020                         OBD_FREE(rule->tr_nids_str,
1021                                  strlen(start->tc_nids_str) + 1);
1022                         return -EINVAL;
1023                 }
1024         }
1025         return 0;
1026 }
1027
1028 static int
1029 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1030 {
1031         return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1032                           rule->tr_nids_str, rule->tr_rpc_rate,
1033                           atomic_read(&rule->tr_ref) - 1);
1034 }
1035
1036 static int
1037 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1038                        struct nrs_tbf_client *cli)
1039 {
1040         return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1041 }
1042
1043 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1044 {
1045         if (!list_empty(&rule->tr_nids))
1046                 cfs_free_nidlist(&rule->tr_nids);
1047         LASSERT(rule->tr_nids_str != NULL);
1048         OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1049 }
1050
1051 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1052 {
1053         if (!list_empty(&cmd->tc_nids))
1054                 cfs_free_nidlist(&cmd->tc_nids);
1055         if (cmd->tc_nids_str)
1056                 OBD_FREE(cmd->tc_nids_str, strlen(cmd->tc_nids_str) + 1);
1057 }
1058
1059 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, const char *id)
1060 {
1061         OBD_ALLOC(cmd->tc_nids_str, strlen(id) + 1);
1062         if (cmd->tc_nids_str == NULL)
1063                 return -ENOMEM;
1064
1065         memcpy(cmd->tc_nids_str, id, strlen(id));
1066
1067         /* parse NID list */
1068         if (cfs_parse_nidlist(cmd->tc_nids_str,
1069                               strlen(cmd->tc_nids_str),
1070                               &cmd->tc_nids) <= 0) {
1071                 nrs_tbf_nid_cmd_fini(cmd);
1072                 return -EINVAL;
1073         }
1074
1075         return 0;
1076 }
1077
1078 static struct nrs_tbf_ops nrs_tbf_nid_ops = {
1079         .o_name = NRS_TBF_TYPE_NID,
1080         .o_startup = nrs_tbf_nid_startup,
1081         .o_cli_find = nrs_tbf_nid_cli_find,
1082         .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1083         .o_cli_put = nrs_tbf_nid_cli_put,
1084         .o_cli_init = nrs_tbf_nid_cli_init,
1085         .o_rule_init = nrs_tbf_nid_rule_init,
1086         .o_rule_dump = nrs_tbf_nid_rule_dump,
1087         .o_rule_match = nrs_tbf_nid_rule_match,
1088         .o_rule_fini = nrs_tbf_nid_rule_fini,
1089 };
1090
1091 /**
1092  * Is called before the policy transitions into
1093  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
1094  * policy-specific private data structure.
1095  *
1096  * \param[in] policy The policy to start
1097  *
1098  * \retval -ENOMEM OOM error
1099  * \retval  0      success
1100  *
1101  * \see nrs_policy_register()
1102  * \see nrs_policy_ctl()
1103  */
1104 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
1105 {
1106         struct nrs_tbf_head     *head;
1107         struct nrs_tbf_ops      *ops;
1108         __u32                    type;
1109         int rc = 0;
1110
1111         if (arg == NULL || strlen(arg) > NRS_TBF_TYPE_MAX_LEN)
1112                 GOTO(out, rc = -EINVAL);
1113
1114         if (strcmp(arg, NRS_TBF_TYPE_NID) == 0) {
1115                 ops = &nrs_tbf_nid_ops;
1116                 type = NRS_TBF_FLAG_NID;
1117         } else if (strcmp(arg, NRS_TBF_TYPE_JOBID) == 0) {
1118                 ops = &nrs_tbf_jobid_ops;
1119                 type = NRS_TBF_FLAG_JOBID;
1120         } else
1121                 GOTO(out, rc = -ENOTSUPP);
1122
1123         OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
1124         if (head == NULL)
1125                 GOTO(out, rc = -ENOMEM);
1126
1127         memcpy(head->th_type, arg, strlen(arg));
1128         head->th_type[strlen(arg)] = '\0';
1129         head->th_ops = ops;
1130         head->th_type_flag = type;
1131
1132         head->th_binheap = cfs_binheap_create(&nrs_tbf_heap_ops,
1133                                               CBH_FLAG_ATOMIC_GROW, 4096, NULL,
1134                                               nrs_pol2cptab(policy),
1135                                               nrs_pol2cptid(policy));
1136         if (head->th_binheap == NULL)
1137                 GOTO(out_free_head, rc = -ENOMEM);
1138
1139         atomic_set(&head->th_rule_sequence, 0);
1140         spin_lock_init(&head->th_rule_lock);
1141         INIT_LIST_HEAD(&head->th_list);
1142         hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1143         head->th_timer.function = nrs_tbf_timer_cb;
1144         rc = head->th_ops->o_startup(policy, head);
1145         if (rc)
1146                 GOTO(out_free_heap, rc);
1147
1148         policy->pol_private = head;
1149         return 0;
1150 out_free_heap:
1151         cfs_binheap_destroy(head->th_binheap);
1152 out_free_head:
1153         OBD_FREE_PTR(head);
1154 out:
1155         return rc;
1156 }
1157
1158 /**
1159  * Is called before the policy transitions into
1160  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
1161  * private data structure.
1162  *
1163  * \param[in] policy The policy to stop
1164  *
1165  * \see nrs_policy_stop0()
1166  */
1167 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
1168 {
1169         struct nrs_tbf_head *head = policy->pol_private;
1170         struct ptlrpc_nrs *nrs = policy->pol_nrs;
1171         struct nrs_tbf_rule *rule, *n;
1172
1173         LASSERT(head != NULL);
1174         LASSERT(head->th_cli_hash != NULL);
1175         hrtimer_cancel(&head->th_timer);
1176         /* Should cleanup hash first before free rules */
1177         cfs_hash_putref(head->th_cli_hash);
1178         list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
1179                 list_del_init(&rule->tr_linkage);
1180                 nrs_tbf_rule_put(rule);
1181         }
1182         LASSERT(list_empty(&head->th_list));
1183         LASSERT(head->th_binheap != NULL);
1184         LASSERT(cfs_binheap_is_empty(head->th_binheap));
1185         cfs_binheap_destroy(head->th_binheap);
1186         OBD_FREE_PTR(head);
1187         nrs->nrs_throttling = 0;
1188         wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
1189 }
1190
1191 /**
1192  * Performs a policy-specific ctl function on TBF policy instances; similar
1193  * to ioctl.
1194  *
1195  * \param[in]     policy the policy instance
1196  * \param[in]     opc    the opcode
1197  * \param[in,out] arg    used for passing parameters and information
1198  *
1199  * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1200  * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1201  *
1202  * \retval 0   operation carried out successfully
1203  * \retval -ve error
1204  */
1205 static int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy,
1206                        enum ptlrpc_nrs_ctl opc,
1207                        void *arg)
1208 {
1209         int rc = 0;
1210         ENTRY;
1211
1212         assert_spin_locked(&policy->pol_nrs->nrs_lock);
1213
1214         switch ((enum nrs_ctl_tbf)opc) {
1215         default:
1216                 RETURN(-EINVAL);
1217
1218         /**
1219          * Read RPC rate size of a policy instance.
1220          */
1221         case NRS_CTL_TBF_RD_RULE: {
1222                 struct nrs_tbf_head *head = policy->pol_private;
1223                 struct seq_file *m = (struct seq_file *) arg;
1224                 struct ptlrpc_service_part *svcpt;
1225
1226                 svcpt = policy->pol_nrs->nrs_svcpt;
1227                 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
1228
1229                 rc = nrs_tbf_rule_dump_all(head, m);
1230                 }
1231                 break;
1232
1233         /**
1234          * Write RPC rate of a policy instance.
1235          */
1236         case NRS_CTL_TBF_WR_RULE: {
1237                 struct nrs_tbf_head *head = policy->pol_private;
1238                 struct nrs_tbf_cmd *cmd;
1239
1240                 cmd = (struct nrs_tbf_cmd *)arg;
1241                 rc = nrs_tbf_command(policy,
1242                                      head,
1243                                      cmd);
1244                 }
1245                 break;
1246         }
1247
1248         RETURN(rc);
1249 }
1250
1251 /**
1252  * Is called for obtaining a TBF policy resource.
1253  *
1254  * \param[in]  policy     The policy on which the request is being asked for
1255  * \param[in]  nrq        The request for which resources are being taken
1256  * \param[in]  parent     Parent resource, unused in this policy
1257  * \param[out] resp       Resources references are placed in this array
1258  * \param[in]  moving_req Signifies limited caller context; unused in this
1259  *                        policy
1260  *
1261  *
1262  * \see nrs_resource_get_safe()
1263  */
1264 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
1265                            struct ptlrpc_nrs_request *nrq,
1266                            const struct ptlrpc_nrs_resource *parent,
1267                            struct ptlrpc_nrs_resource **resp,
1268                            bool moving_req)
1269 {
1270         struct nrs_tbf_head   *head;
1271         struct nrs_tbf_client *cli;
1272         struct nrs_tbf_client *tmp;
1273         struct ptlrpc_request *req;
1274
1275         if (parent == NULL) {
1276                 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
1277                 return 0;
1278         }
1279
1280         head = container_of(parent, struct nrs_tbf_head, th_res);
1281         req = container_of(nrq, struct ptlrpc_request, rq_nrq);
1282         cli = head->th_ops->o_cli_find(head, req);
1283         if (cli != NULL) {
1284                 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1285                 LASSERT(cli->tc_rule);
1286                 if (cli->tc_rule_sequence !=
1287                     atomic_read(&head->th_rule_sequence) ||
1288                     cli->tc_rule->tr_flags & NTRS_STOPPING) {
1289                         struct nrs_tbf_rule *rule;
1290
1291                         rule = nrs_tbf_rule_match(head, cli);
1292                         if (rule != cli->tc_rule)
1293                                 nrs_tbf_cli_reset(head, rule, cli);
1294                         else
1295                                 nrs_tbf_rule_put(rule);
1296                 } else if (cli->tc_rule_generation !=
1297                            cli->tc_rule->tr_generation) {
1298                         nrs_tbf_cli_reset_value(head, cli);
1299                 }
1300                 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1301                 goto out;
1302         }
1303
1304         OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
1305                           sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
1306         if (cli == NULL)
1307                 return -ENOMEM;
1308         nrs_tbf_cli_init(head, cli, req);
1309         tmp = head->th_ops->o_cli_findadd(head, cli);
1310         if (tmp != cli) {
1311                 atomic_dec(&cli->tc_ref);
1312                 nrs_tbf_cli_fini(cli);
1313                 cli = tmp;
1314         }
1315 out:
1316         *resp = &cli->tc_res;
1317
1318         return 1;
1319 }
1320
1321 /**
1322  * Called when releasing references to the resource hierachy obtained for a
1323  * request for scheduling using the TBF policy.
1324  *
1325  * \param[in] policy   the policy the resource belongs to
1326  * \param[in] res      the resource to be released
1327  */
1328 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
1329                             const struct ptlrpc_nrs_resource *res)
1330 {
1331         struct nrs_tbf_head   *head;
1332         struct nrs_tbf_client *cli;
1333
1334         /**
1335          * Do nothing for freeing parent, nrs_tbf_net resources
1336          */
1337         if (res->res_parent == NULL)
1338                 return;
1339
1340         cli = container_of(res, struct nrs_tbf_client, tc_res);
1341         head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
1342
1343         head->th_ops->o_cli_put(head, cli);
1344 }
1345
1346 /**
1347  * Called when getting a request from the TBF policy for handling, or just
1348  * peeking; removes the request from the policy when it is to be handled.
1349  *
1350  * \param[in] policy The policy
1351  * \param[in] peek   When set, signifies that we just want to examine the
1352  *                   request, and not handle it, so the request is not removed
1353  *                   from the policy.
1354  * \param[in] force  Force the policy to return a request; unused in this
1355  *                   policy
1356  *
1357  * \retval The request to be handled; this is the next request in the TBF
1358  *         rule
1359  *
1360  * \see ptlrpc_nrs_req_get_nolock()
1361  * \see nrs_request_get()
1362  */
1363 static
1364 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
1365                                            bool peek, bool force)
1366 {
1367         struct nrs_tbf_head       *head = policy->pol_private;
1368         struct ptlrpc_nrs_request *nrq = NULL;
1369         struct nrs_tbf_client     *cli;
1370         cfs_binheap_node_t        *node;
1371
1372         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1373
1374         if (!peek && policy->pol_nrs->nrs_throttling)
1375                 return NULL;
1376
1377         node = cfs_binheap_root(head->th_binheap);
1378         if (unlikely(node == NULL))
1379                 return NULL;
1380
1381         cli = container_of(node, struct nrs_tbf_client, tc_node);
1382         LASSERT(cli->tc_in_heap);
1383         if (peek) {
1384                 nrq = list_entry(cli->tc_list.next,
1385                                      struct ptlrpc_nrs_request,
1386                                      nr_u.tbf.tr_list);
1387         } else {
1388                 __u64 now = ktime_to_ns(ktime_get());
1389                 __u64 passed;
1390                 long  ntoken;
1391                 __u64 deadline;
1392
1393                 deadline = cli->tc_check_time +
1394                           cli->tc_nsecs;
1395                 LASSERT(now >= cli->tc_check_time);
1396                 passed = now - cli->tc_check_time;
1397                 ntoken = (passed * cli->tc_rpc_rate) / NSEC_PER_SEC;
1398                 ntoken += cli->tc_ntoken;
1399                 if (ntoken > cli->tc_depth)
1400                         ntoken = cli->tc_depth;
1401                 if (ntoken > 0) {
1402                         struct ptlrpc_request *req;
1403                         nrq = list_entry(cli->tc_list.next,
1404                                              struct ptlrpc_nrs_request,
1405                                              nr_u.tbf.tr_list);
1406                         req = container_of(nrq,
1407                                            struct ptlrpc_request,
1408                                            rq_nrq);
1409                         ntoken--;
1410                         cli->tc_ntoken = ntoken;
1411                         cli->tc_check_time = now;
1412                         list_del_init(&nrq->nr_u.tbf.tr_list);
1413                         if (list_empty(&cli->tc_list)) {
1414                                 cfs_binheap_remove(head->th_binheap,
1415                                                    &cli->tc_node);
1416                                 cli->tc_in_heap = false;
1417                         } else {
1418                                 cfs_binheap_relocate(head->th_binheap,
1419                                                      &cli->tc_node);
1420                         }
1421                         CDEBUG(D_RPCTRACE,
1422                                "NRS start %s request from %s, "
1423                                "seq: "LPU64"\n",
1424                                policy->pol_desc->pd_name,
1425                                libcfs_id2str(req->rq_peer),
1426                                nrq->nr_u.tbf.tr_sequence);
1427                 } else {
1428                         ktime_t time;
1429
1430                         spin_lock(&policy->pol_nrs->nrs_lock);
1431                         policy->pol_nrs->nrs_throttling = 1;
1432                         spin_unlock(&policy->pol_nrs->nrs_lock);
1433                         head->th_deadline = deadline;
1434                         time = ktime_set(0, 0);
1435                         time = ktime_add_ns(time, deadline);
1436                         hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
1437                 }
1438         }
1439
1440         return nrq;
1441 }
1442
1443 /**
1444  * Adds request \a nrq to \a policy's list of queued requests
1445  *
1446  * \param[in] policy The policy
1447  * \param[in] nrq    The request to add
1448  *
1449  * \retval 0 success; nrs_request_enqueue() assumes this function will always
1450  *                    succeed
1451  */
1452 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
1453                            struct ptlrpc_nrs_request *nrq)
1454 {
1455         struct nrs_tbf_head   *head;
1456         struct nrs_tbf_client *cli;
1457         int                    rc = 0;
1458
1459         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1460
1461         cli = container_of(nrs_request_resource(nrq),
1462                            struct nrs_tbf_client, tc_res);
1463         head = container_of(nrs_request_resource(nrq)->res_parent,
1464                             struct nrs_tbf_head, th_res);
1465         if (list_empty(&cli->tc_list)) {
1466                 LASSERT(!cli->tc_in_heap);
1467                 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
1468                 if (rc == 0) {
1469                         cli->tc_in_heap = true;
1470                         nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1471                         list_add_tail(&nrq->nr_u.tbf.tr_list,
1472                                           &cli->tc_list);
1473                         if (policy->pol_nrs->nrs_throttling) {
1474                                 __u64 deadline = cli->tc_check_time +
1475                                                  cli->tc_nsecs;
1476                                 if ((head->th_deadline > deadline) &&
1477                                     (hrtimer_try_to_cancel(&head->th_timer)
1478                                      >= 0)) {
1479                                         ktime_t time;
1480                                         head->th_deadline = deadline;
1481                                         time = ktime_set(0, 0);
1482                                         time = ktime_add_ns(time, deadline);
1483                                         hrtimer_start(&head->th_timer, time,
1484                                                       HRTIMER_MODE_ABS);
1485                                 }
1486                         }
1487                 }
1488         } else {
1489                 LASSERT(cli->tc_in_heap);
1490                 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1491                 list_add_tail(&nrq->nr_u.tbf.tr_list,
1492                                   &cli->tc_list);
1493         }
1494         return rc;
1495 }
1496
1497 /**
1498  * Removes request \a nrq from \a policy's list of queued requests.
1499  *
1500  * \param[in] policy The policy
1501  * \param[in] nrq    The request to remove
1502  */
1503 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
1504                              struct ptlrpc_nrs_request *nrq)
1505 {
1506         struct nrs_tbf_head   *head;
1507         struct nrs_tbf_client *cli;
1508
1509         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1510
1511         cli = container_of(nrs_request_resource(nrq),
1512                            struct nrs_tbf_client, tc_res);
1513         head = container_of(nrs_request_resource(nrq)->res_parent,
1514                             struct nrs_tbf_head, th_res);
1515
1516         LASSERT(!list_empty(&nrq->nr_u.tbf.tr_list));
1517         list_del_init(&nrq->nr_u.tbf.tr_list);
1518         if (list_empty(&cli->tc_list)) {
1519                 cfs_binheap_remove(head->th_binheap,
1520                                    &cli->tc_node);
1521                 cli->tc_in_heap = false;
1522         } else {
1523                 cfs_binheap_relocate(head->th_binheap,
1524                                      &cli->tc_node);
1525         }
1526 }
1527
1528 /**
1529  * Prints a debug statement right before the request \a nrq stops being
1530  * handled.
1531  *
1532  * \param[in] policy The policy handling the request
1533  * \param[in] nrq    The request being handled
1534  *
1535  * \see ptlrpc_server_finish_request()
1536  * \see ptlrpc_nrs_req_stop_nolock()
1537  */
1538 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
1539                               struct ptlrpc_nrs_request *nrq)
1540 {
1541         struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
1542                                                   rq_nrq);
1543
1544         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1545
1546         CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: "LPU64"\n",
1547                policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
1548                nrq->nr_u.tbf.tr_sequence);
1549 }
1550
1551 #ifdef CONFIG_PROC_FS
1552
1553 /**
1554  * lprocfs interface
1555  */
1556
1557 /**
1558  * The maximum RPC rate.
1559  */
1560 #define LPROCFS_NRS_RATE_MAX            65535
1561
1562 static int
1563 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
1564 {
1565         struct ptlrpc_service       *svc = m->private;
1566         int                          rc;
1567
1568         seq_printf(m, "regular_requests:\n");
1569         /**
1570          * Perform two separate calls to this as only one of the NRS heads'
1571          * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
1572          * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
1573          */
1574         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1575                                        NRS_POL_NAME_TBF,
1576                                        NRS_CTL_TBF_RD_RULE,
1577                                        false, m);
1578         if (rc == 0) {
1579                 /**
1580                  * -ENOSPC means buf in the parameter m is overflow, return 0
1581                  * here to let upper layer function seq_read alloc a larger
1582                  * memory area and do this process again.
1583                  */
1584         } else if (rc == -ENOSPC) {
1585                 return 0;
1586
1587                 /**
1588                  * Ignore -ENODEV as the regular NRS head's policy may be in the
1589                  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1590                  */
1591         } else if (rc != -ENODEV) {
1592                 return rc;
1593         }
1594
1595         if (!nrs_svc_has_hp(svc))
1596                 goto no_hp;
1597
1598         seq_printf(m, "high_priority_requests:\n");
1599         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1600                                        NRS_POL_NAME_TBF,
1601                                        NRS_CTL_TBF_RD_RULE,
1602                                        false, m);
1603         if (rc == 0) {
1604                 /**
1605                  * -ENOSPC means buf in the parameter m is overflow, return 0
1606                  * here to let upper layer function seq_read alloc a larger
1607                  * memory area and do this process again.
1608                  */
1609         } else if (rc == -ENOSPC) {
1610                 return 0;
1611         }
1612
1613 no_hp:
1614
1615         return rc;
1616 }
1617
1618 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char **val)
1619 {
1620         int rc;
1621         char *token;
1622
1623         token = strsep(val, "}");
1624         if (*val == NULL)
1625                 GOTO(out, rc = -EINVAL);
1626
1627         if (strlen(token) <= 1 ||
1628             token[0] != '{')
1629                 GOTO(out, rc = -EINVAL);
1630         /* Skip '{' */
1631         token++;
1632
1633         /* Should be followed by ' ' or nothing */
1634         if ((*val)[0] == '\0')
1635                 *val = NULL;
1636         else if ((*val)[0] == ' ')
1637                 (*val)++;
1638         else
1639                 GOTO(out, rc = -EINVAL);
1640
1641         rc = nrs_tbf_jobid_parse(cmd, token);
1642         if (!rc)
1643                 cmd->tc_valid_types |= NRS_TBF_FLAG_JOBID;
1644
1645         rc = nrs_tbf_nid_parse(cmd, token);
1646         if (!rc)
1647                 cmd->tc_valid_types |= NRS_TBF_FLAG_NID;
1648
1649         if (!cmd->tc_valid_types)
1650                 rc = -EINVAL;
1651         else
1652                 rc = 0;
1653 out:
1654         return rc;
1655 }
1656
1657
1658 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
1659 {
1660         if (cmd->tc_valid_types & NRS_TBF_FLAG_JOBID)
1661                 nrs_tbf_jobid_cmd_fini(cmd);
1662         if (cmd->tc_valid_types & NRS_TBF_FLAG_NID)
1663                 nrs_tbf_nid_cmd_fini(cmd);
1664 }
1665
1666 static struct nrs_tbf_cmd *
1667 nrs_tbf_parse_cmd(char *buffer, unsigned long count)
1668 {
1669         static struct nrs_tbf_cmd *cmd;
1670         char                      *token;
1671         char                      *val;
1672         int                        i;
1673         int                        rc = 0;
1674
1675         OBD_ALLOC_PTR(cmd);
1676         if (cmd == NULL)
1677                 GOTO(out, rc = -ENOMEM);
1678
1679         val = buffer;
1680         token = strsep(&val, " ");
1681         if (val == NULL || strlen(val) == 0)
1682                 GOTO(out_free_cmd, rc = -EINVAL);
1683
1684         /* Type of the command */
1685         if (strcmp(token, "start") == 0)
1686                 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
1687         else if (strcmp(token, "stop") == 0)
1688                 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
1689         else if (strcmp(token, "change") == 0)
1690                 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RATE;
1691         else
1692                 GOTO(out_free_cmd, rc = -EINVAL);
1693
1694         /* Name of the rule */
1695         token = strsep(&val, " ");
1696         if (val == NULL) {
1697                 /**
1698                  * Stop comand only need name argument,
1699                  * But other commands need ID or rate argument.
1700                  */
1701                 if (cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE)
1702                         GOTO(out_free_cmd, rc = -EINVAL);
1703         }
1704
1705         for (i = 0; i < strlen(token); i++) {
1706                 if ((!isalnum(token[i])) &&
1707                     (token[i] != '_'))
1708                         GOTO(out_free_cmd, rc = -EINVAL);
1709         }
1710         cmd->tc_name = token;
1711
1712         if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
1713                 /* List of ID */
1714                 LASSERT(val);
1715                 rc = nrs_tbf_id_parse(cmd, &val);
1716                 if (rc)
1717                         GOTO(out_free_cmd, rc);
1718         }
1719
1720         if (val != NULL) {
1721                 if (cmd->tc_cmd == NRS_CTL_TBF_STOP_RULE ||
1722                     strlen(val) == 0 || !isdigit(val[0]))
1723                         GOTO(out_free_nid, rc = -EINVAL);
1724
1725                 cmd->tc_rpc_rate = simple_strtoull(val, NULL, 10);
1726                 if (cmd->tc_rpc_rate <= 0 ||
1727                     cmd->tc_rpc_rate >= LPROCFS_NRS_RATE_MAX)
1728                         GOTO(out_free_nid, rc = -EINVAL);
1729         } else {
1730                 if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RATE)
1731                         GOTO(out_free_nid, rc = -EINVAL);
1732                 /* No RPC rate given */
1733                 cmd->tc_rpc_rate = tbf_rate;
1734         }
1735         goto out;
1736 out_free_nid:
1737         nrs_tbf_cmd_fini(cmd);
1738 out_free_cmd:
1739         OBD_FREE_PTR(cmd);
1740 out:
1741         if (rc)
1742                 cmd = ERR_PTR(rc);
1743         return cmd;
1744 }
1745
1746 extern struct nrs_core nrs_core;
1747 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
1748 static ssize_t
1749 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file,
1750                                       const char __user *buffer,
1751                                       size_t count, loff_t *off)
1752 {
1753         struct seq_file           *m = file->private_data;
1754         struct ptlrpc_service     *svc = m->private;
1755         char                      *kernbuf;
1756         char                      *val;
1757         int                        rc;
1758         static struct nrs_tbf_cmd *cmd;
1759         enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
1760         unsigned long              length;
1761         char                      *token;
1762
1763         OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1764         if (kernbuf == NULL)
1765                 GOTO(out, rc = -ENOMEM);
1766
1767         if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
1768                 GOTO(out_free_kernbuff, rc = -EINVAL);
1769
1770         if (copy_from_user(kernbuf, buffer, count))
1771                 GOTO(out_free_kernbuff, rc = -EFAULT);
1772
1773         val = kernbuf;
1774         token = strsep(&val, " ");
1775         if (val == NULL)
1776                 GOTO(out_free_kernbuff, rc = -EINVAL);
1777
1778         if (strcmp(token, "reg") == 0) {
1779                 queue = PTLRPC_NRS_QUEUE_REG;
1780         } else if (strcmp(token, "hp") == 0) {
1781                 queue = PTLRPC_NRS_QUEUE_HP;
1782         } else {
1783                 kernbuf[strlen(token)] = ' ';
1784                 val = kernbuf;
1785         }
1786         length = strlen(val);
1787
1788         if (length == 0)
1789                 GOTO(out_free_kernbuff, rc = -EINVAL);
1790
1791         if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
1792                 GOTO(out_free_kernbuff, rc = -ENODEV);
1793         else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
1794                 queue = PTLRPC_NRS_QUEUE_REG;
1795
1796         cmd = nrs_tbf_parse_cmd(val, length);
1797         if (IS_ERR(cmd))
1798                 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
1799
1800         /**
1801          * Serialize NRS core lprocfs operations with policy registration/
1802          * unregistration.
1803          */
1804         mutex_lock(&nrs_core.nrs_mutex);
1805         rc = ptlrpc_nrs_policy_control(svc, queue,
1806                                        NRS_POL_NAME_TBF,
1807                                        NRS_CTL_TBF_WR_RULE,
1808                                        false, cmd);
1809         mutex_unlock(&nrs_core.nrs_mutex);
1810
1811         nrs_tbf_cmd_fini(cmd);
1812         OBD_FREE_PTR(cmd);
1813 out_free_kernbuff:
1814         OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1815 out:
1816         return rc ? rc : count;
1817 }
1818 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
1819
1820 /**
1821  * Initializes a TBF policy's lprocfs interface for service \a svc
1822  *
1823  * \param[in] svc the service
1824  *
1825  * \retval 0    success
1826  * \retval != 0 error
1827  */
1828 static int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
1829 {
1830         struct lprocfs_vars nrs_tbf_lprocfs_vars[] = {
1831                 { .name         = "nrs_tbf_rule",
1832                   .fops         = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
1833                   .data = svc },
1834                 { NULL }
1835         };
1836
1837         if (svc->srv_procroot == NULL)
1838                 return 0;
1839
1840         return lprocfs_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars, NULL);
1841 }
1842
1843 /**
1844  * Cleans up a TBF policy's lprocfs interface for service \a svc
1845  *
1846  * \param[in] svc the service
1847  */
1848 static void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
1849 {
1850         if (svc->srv_procroot == NULL)
1851                 return;
1852
1853         lprocfs_remove_proc_entry("nrs_tbf_rule", svc->srv_procroot);
1854 }
1855
1856 #endif /* CONFIG_PROC_FS */
1857
1858 /**
1859  * TBF policy operations
1860  */
1861 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
1862         .op_policy_start        = nrs_tbf_start,
1863         .op_policy_stop         = nrs_tbf_stop,
1864         .op_policy_ctl          = nrs_tbf_ctl,
1865         .op_res_get             = nrs_tbf_res_get,
1866         .op_res_put             = nrs_tbf_res_put,
1867         .op_req_get             = nrs_tbf_req_get,
1868         .op_req_enqueue         = nrs_tbf_req_add,
1869         .op_req_dequeue         = nrs_tbf_req_del,
1870         .op_req_stop            = nrs_tbf_req_stop,
1871 #ifdef CONFIG_PROC_FS
1872         .op_lprocfs_init        = nrs_tbf_lprocfs_init,
1873         .op_lprocfs_fini        = nrs_tbf_lprocfs_fini,
1874 #endif
1875 };
1876
1877 /**
1878  * TBF policy configuration
1879  */
1880 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
1881         .nc_name                = NRS_POL_NAME_TBF,
1882         .nc_ops                 = &nrs_tbf_ops,
1883         .nc_compat              = nrs_policy_compat_all,
1884 };
1885
1886 /** @} tbf */
1887
1888 /** @} nrs */
1889
1890 #endif /* HAVE_SERVER_SUPPORT */