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