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