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