Whamcloud - gitweb
6c7b5dce65cd6ac6458f2cae4f39768860c30de1
[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  */
26 /*
27  * lustre/ptlrpc/nrs_tbf.c
28  *
29  * Network Request Scheduler (NRS) Token Bucket Filter(TBF) policy
30  *
31  */
32
33 #ifdef HAVE_SERVER_SUPPORT
34
35 /**
36  * \addtogoup nrs
37  * @{
38  */
39
40 #define DEBUG_SUBSYSTEM S_RPC
41 #include <obd_support.h>
42 #include <obd_class.h>
43 #include <libcfs/libcfs.h>
44 #include "ptlrpc_internal.h"
45
46 /**
47  * \name tbf
48  *
49  * Token Bucket Filter over client NIDs
50  *
51  * @{
52  */
53
54 #define NRS_POL_NAME_TBF        "tbf"
55
56 int tbf_jobid_cache_size = 8192;
57 CFS_MODULE_PARM(tbf_jobid_cache_size, "i", int, 0644,
58                 "The size of jobid cache");
59
60 int tbf_rate = 10000;
61 CFS_MODULE_PARM(tbf_rate, "i", int, 0644,
62                 "Default rate limit in RPCs/s");
63
64 int tbf_depth = 3;
65 CFS_MODULE_PARM(tbf_depth, "i", int, 0644,
66                 "How many tokens that a client can save up");
67
68 static enum hrtimer_restart nrs_tbf_timer_cb(struct hrtimer *timer)
69 {
70         struct nrs_tbf_head *head = container_of(timer, struct nrs_tbf_head,
71                                                  th_timer);
72         struct ptlrpc_nrs   *nrs = head->th_res.res_policy->pol_nrs;
73         struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
74
75         spin_lock(&nrs->nrs_lock);
76         nrs->nrs_throttling = 0;
77         spin_unlock(&nrs->nrs_lock);
78         wake_up(&svcpt->scp_waitq);
79
80         return HRTIMER_NORESTART;
81 }
82
83 #define NRS_TBF_DEFAULT_RULE "default"
84
85 static void nrs_tbf_rule_fini(struct nrs_tbf_rule *rule)
86 {
87         LASSERT(atomic_read(&rule->tr_ref) == 0);
88         LASSERT(list_empty(&rule->tr_cli_list));
89         LASSERT(list_empty(&rule->tr_linkage));
90
91         rule->tr_head->th_ops->o_rule_fini(rule);
92         OBD_FREE_PTR(rule);
93 }
94
95 /**
96  * Decreases the rule's usage reference count, and stops the rule in case it
97  * was already stopping and have no more outstanding usage references (which
98  * indicates it has no more queued or started requests, and can be safely
99  * stopped).
100  */
101 static void nrs_tbf_rule_put(struct nrs_tbf_rule *rule)
102 {
103         if (atomic_dec_and_test(&rule->tr_ref))
104                 nrs_tbf_rule_fini(rule);
105 }
106
107 /**
108  * Increases the rule's usage reference count.
109  */
110 static inline void nrs_tbf_rule_get(struct nrs_tbf_rule *rule)
111 {
112         atomic_inc(&rule->tr_ref);
113 }
114
115 static void
116 nrs_tbf_cli_rule_put(struct nrs_tbf_client *cli)
117 {
118         LASSERT(!list_empty(&cli->tc_linkage));
119         LASSERT(cli->tc_rule);
120         list_del_init(&cli->tc_linkage);
121         nrs_tbf_rule_put(cli->tc_rule);
122         cli->tc_rule = NULL;
123 }
124
125 static void
126 nrs_tbf_cli_reset_value(struct nrs_tbf_head *head,
127                         struct nrs_tbf_client *cli)
128
129 {
130         struct nrs_tbf_rule *rule = cli->tc_rule;
131
132         cli->tc_rpc_rate = rule->tr_rpc_rate;
133         cli->tc_nsecs = rule->tr_nsecs;
134         cli->tc_depth = rule->tr_depth;
135         cli->tc_ntoken = rule->tr_depth;
136         cli->tc_check_time = ktime_to_ns(ktime_get());
137         cli->tc_rule_sequence = atomic_read(&head->th_rule_sequence);
138         cli->tc_rule_generation = rule->tr_generation;
139
140         if (cli->tc_in_heap)
141                 cfs_binheap_relocate(head->th_binheap,
142                                      &cli->tc_node);
143 }
144
145 static void
146 nrs_tbf_cli_reset(struct nrs_tbf_head *head,
147                   struct nrs_tbf_rule *rule,
148                   struct nrs_tbf_client *cli)
149 {
150         if (!list_empty(&cli->tc_linkage)) {
151                 LASSERT(rule != cli->tc_rule);
152                 nrs_tbf_cli_rule_put(cli);
153         }
154         LASSERT(cli->tc_rule == NULL);
155         LASSERT(list_empty(&cli->tc_linkage));
156         /* Rule's ref is added before called */
157         cli->tc_rule = rule;
158         list_add_tail(&cli->tc_linkage, &rule->tr_cli_list);
159         nrs_tbf_cli_reset_value(head, cli);
160 }
161
162 static int
163 nrs_tbf_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
164 {
165         return rule->tr_head->th_ops->o_rule_dump(rule, m);
166 }
167
168 static int
169 nrs_tbf_rule_dump_all(struct nrs_tbf_head *head, struct seq_file *m)
170 {
171         struct nrs_tbf_rule *rule;
172         int rc = 0;
173
174         LASSERT(head != NULL);
175         spin_lock(&head->th_rule_lock);
176         /* List the rules from newest to oldest */
177         list_for_each_entry(rule, &head->th_list, tr_linkage) {
178                 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
179                 rc = nrs_tbf_rule_dump(rule, m);
180                 if (rc) {
181                         rc = -ENOSPC;
182                         break;
183                 }
184         }
185         spin_unlock(&head->th_rule_lock);
186
187         return rc;
188 }
189
190 static struct nrs_tbf_rule *
191 nrs_tbf_rule_find_nolock(struct nrs_tbf_head *head,
192                          const char *name)
193 {
194         struct nrs_tbf_rule *rule;
195
196         LASSERT(head != NULL);
197         list_for_each_entry(rule, &head->th_list, tr_linkage) {
198                 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
199                 if (strcmp(rule->tr_name, name) == 0) {
200                         nrs_tbf_rule_get(rule);
201                         return rule;
202                 }
203         }
204         return NULL;
205 }
206
207 static struct nrs_tbf_rule *
208 nrs_tbf_rule_find(struct nrs_tbf_head *head,
209                   const char *name)
210 {
211         struct nrs_tbf_rule *rule;
212
213         LASSERT(head != NULL);
214         spin_lock(&head->th_rule_lock);
215         rule = nrs_tbf_rule_find_nolock(head, name);
216         spin_unlock(&head->th_rule_lock);
217         return rule;
218 }
219
220 static struct nrs_tbf_rule *
221 nrs_tbf_rule_match(struct nrs_tbf_head *head,
222                    struct nrs_tbf_client *cli)
223 {
224         struct nrs_tbf_rule *rule = NULL;
225         struct nrs_tbf_rule *tmp_rule;
226
227         spin_lock(&head->th_rule_lock);
228         /* Match the newest rule in the list */
229         list_for_each_entry(tmp_rule, &head->th_list, tr_linkage) {
230                 LASSERT((tmp_rule->tr_flags & NTRS_STOPPING) == 0);
231                 if (head->th_ops->o_rule_match(tmp_rule, cli)) {
232                         rule = tmp_rule;
233                         break;
234                 }
235         }
236
237         if (rule == NULL)
238                 rule = head->th_rule;
239
240         nrs_tbf_rule_get(rule);
241         spin_unlock(&head->th_rule_lock);
242         return rule;
243 }
244
245 static void
246 nrs_tbf_cli_init(struct nrs_tbf_head *head,
247                  struct nrs_tbf_client *cli,
248                  struct ptlrpc_request *req)
249 {
250         struct nrs_tbf_rule *rule;
251
252         cli->tc_in_heap = false;
253         head->th_ops->o_cli_init(cli, req);
254         INIT_LIST_HEAD(&cli->tc_list);
255         INIT_LIST_HEAD(&cli->tc_linkage);
256         atomic_set(&cli->tc_ref, 1);
257         rule = nrs_tbf_rule_match(head, cli);
258         nrs_tbf_cli_reset(head, rule, cli);
259 }
260
261 static void
262 nrs_tbf_cli_fini(struct nrs_tbf_client *cli)
263 {
264         LASSERT(list_empty(&cli->tc_list));
265         LASSERT(!cli->tc_in_heap);
266         LASSERT(atomic_read(&cli->tc_ref) == 0);
267         nrs_tbf_cli_rule_put(cli);
268         OBD_FREE_PTR(cli);
269 }
270
271 static int
272 nrs_tbf_rule_start(struct ptlrpc_nrs_policy *policy,
273                    struct nrs_tbf_head *head,
274                    struct nrs_tbf_cmd *start)
275 {
276         struct nrs_tbf_rule *rule, *tmp_rule;
277         int rc;
278
279         rule = nrs_tbf_rule_find(head, start->tc_name);
280         if (rule) {
281                 nrs_tbf_rule_put(rule);
282                 return -EEXIST;
283         }
284
285         OBD_CPT_ALLOC_PTR(rule, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
286         if (rule == NULL)
287                 return -ENOMEM;
288
289         memcpy(rule->tr_name, start->tc_name, strlen(start->tc_name));
290         rule->tr_rpc_rate = start->tc_rpc_rate;
291         rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
292         rule->tr_depth = tbf_depth;
293         atomic_set(&rule->tr_ref, 1);
294         INIT_LIST_HEAD(&rule->tr_cli_list);
295         INIT_LIST_HEAD(&rule->tr_nids);
296
297         rc = head->th_ops->o_rule_init(policy, rule, start);
298         if (rc) {
299                 OBD_FREE_PTR(rule);
300                 return rc;
301         }
302
303         /* Add as the newest rule */
304         spin_lock(&head->th_rule_lock);
305         tmp_rule = nrs_tbf_rule_find_nolock(head, start->tc_name);
306         if (tmp_rule) {
307                 spin_unlock(&head->th_rule_lock);
308                 nrs_tbf_rule_put(tmp_rule);
309                 nrs_tbf_rule_put(rule);
310                 return -EEXIST;
311         }
312         list_add(&rule->tr_linkage, &head->th_list);
313         rule->tr_head = head;
314         spin_unlock(&head->th_rule_lock);
315         atomic_inc(&head->th_rule_sequence);
316         if (start->tc_rule_flags & NTRS_DEFAULT) {
317                 rule->tr_flags |= NTRS_DEFAULT;
318                 LASSERT(head->th_rule == NULL);
319                 head->th_rule = rule;
320         }
321
322         return 0;
323 }
324
325 static int
326 nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
327                     struct nrs_tbf_head *head,
328                     struct nrs_tbf_cmd *change)
329 {
330         struct nrs_tbf_rule *rule;
331
332         assert_spin_locked(&policy->pol_nrs->nrs_lock);
333
334         rule = nrs_tbf_rule_find(head, change->tc_name);
335         if (rule == NULL)
336                 return -ENOENT;
337
338         rule->tr_rpc_rate = change->tc_rpc_rate;
339         rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
340         rule->tr_generation++;
341         nrs_tbf_rule_put(rule);
342
343         return 0;
344 }
345
346 static int
347 nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
348                   struct nrs_tbf_head *head,
349                   struct nrs_tbf_cmd *stop)
350 {
351         struct nrs_tbf_rule *rule;
352
353         assert_spin_locked(&policy->pol_nrs->nrs_lock);
354
355         if (strcmp(stop->tc_name, NRS_TBF_DEFAULT_RULE) == 0)
356                 return -EPERM;
357
358         rule = nrs_tbf_rule_find(head, stop->tc_name);
359         if (rule == NULL)
360                 return -ENOENT;
361
362         list_del_init(&rule->tr_linkage);
363         rule->tr_flags |= NTRS_STOPPING;
364         nrs_tbf_rule_put(rule);
365         nrs_tbf_rule_put(rule);
366
367         return 0;
368 }
369
370 static int
371 nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
372                 struct nrs_tbf_head *head,
373                 struct nrs_tbf_cmd *cmd)
374 {
375         int rc;
376
377         assert_spin_locked(&policy->pol_nrs->nrs_lock);
378
379         switch (cmd->tc_cmd) {
380         case NRS_CTL_TBF_START_RULE:
381                 if (!(cmd->tc_valid_types & head->th_type_flag))
382                         return -EINVAL;
383
384                 spin_unlock(&policy->pol_nrs->nrs_lock);
385                 rc = nrs_tbf_rule_start(policy, head, cmd);
386                 spin_lock(&policy->pol_nrs->nrs_lock);
387                 return rc;
388         case NRS_CTL_TBF_CHANGE_RATE:
389                 rc = nrs_tbf_rule_change(policy, head, cmd);
390                 return rc;
391         case NRS_CTL_TBF_STOP_RULE:
392                 rc = nrs_tbf_rule_stop(policy, head, cmd);
393                 /* Take it as a success, if not exists at all */
394                 return rc == -ENOENT ? 0 : rc;
395         default:
396                 return -EFAULT;
397         }
398 }
399
400 /**
401  * Binary heap predicate.
402  *
403  * \param[in] e1 the first binheap node to compare
404  * \param[in] e2 the second binheap node to compare
405  *
406  * \retval 0 e1 > e2
407  * \retval 1 e1 < e2
408  */
409 static int tbf_cli_compare(cfs_binheap_node_t *e1, cfs_binheap_node_t *e2)
410 {
411         struct nrs_tbf_client *cli1;
412         struct nrs_tbf_client *cli2;
413
414         cli1 = container_of(e1, struct nrs_tbf_client, tc_node);
415         cli2 = container_of(e2, struct nrs_tbf_client, tc_node);
416
417         if (cli1->tc_check_time + cli1->tc_nsecs <
418             cli2->tc_check_time + cli2->tc_nsecs)
419                 return 1;
420         else if (cli1->tc_check_time + cli1->tc_nsecs >
421                  cli2->tc_check_time + cli2->tc_nsecs)
422                 return 0;
423
424         if (cli1->tc_check_time < cli2->tc_check_time)
425                 return 1;
426         else if (cli1->tc_check_time > cli2->tc_check_time)
427                 return 0;
428
429         /* Maybe need more comparasion, e.g. request number in the rules */
430         return 1;
431 }
432
433 /**
434  * TBF binary heap operations
435  */
436 static cfs_binheap_ops_t nrs_tbf_heap_ops = {
437         .hop_enter      = NULL,
438         .hop_exit       = NULL,
439         .hop_compare    = tbf_cli_compare,
440 };
441
442 static unsigned nrs_tbf_jobid_hop_hash(cfs_hash_t *hs, const void *key,
443                                   unsigned mask)
444 {
445         return cfs_hash_djb2_hash(key, strlen(key), mask);
446 }
447
448 static int nrs_tbf_jobid_hop_keycmp(const void *key, struct hlist_node *hnode)
449 {
450         struct nrs_tbf_client *cli = hlist_entry(hnode,
451                                                      struct nrs_tbf_client,
452                                                      tc_hnode);
453
454         return (strcmp(cli->tc_jobid, key) == 0);
455 }
456
457 static void *nrs_tbf_jobid_hop_key(struct hlist_node *hnode)
458 {
459         struct nrs_tbf_client *cli = hlist_entry(hnode,
460                                                      struct nrs_tbf_client,
461                                                      tc_hnode);
462
463         return cli->tc_jobid;
464 }
465
466 static void *nrs_tbf_jobid_hop_object(struct hlist_node *hnode)
467 {
468         return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
469 }
470
471 static void nrs_tbf_jobid_hop_get(cfs_hash_t *hs, struct hlist_node *hnode)
472 {
473         struct nrs_tbf_client *cli = hlist_entry(hnode,
474                                                      struct nrs_tbf_client,
475                                                      tc_hnode);
476
477         atomic_inc(&cli->tc_ref);
478 }
479
480 static void nrs_tbf_jobid_hop_put(cfs_hash_t *hs, struct hlist_node *hnode)
481 {
482         struct nrs_tbf_client *cli = hlist_entry(hnode,
483                                                      struct nrs_tbf_client,
484                                                      tc_hnode);
485
486         atomic_dec(&cli->tc_ref);
487 }
488
489 static void nrs_tbf_jobid_hop_exit(cfs_hash_t *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 cfs_hash_ops_t 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(cfs_hash_t *hs,
517                           cfs_hash_bd_t *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         cfs_hash_t              *hs = head->th_cli_hash;
545         cfs_hash_bd_t            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         cfs_hash_t              *hs = head->th_cli_hash;
564         cfs_hash_bd_t            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         cfs_hash_bd_t            bd;
583         cfs_hash_t              *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         cfs_hash_bd_t            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 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(cfs_hash_t *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(cfs_hash_t *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(cfs_hash_t *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(cfs_hash_t *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 cfs_hash_ops_t 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 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         spin_lock(&nrs->nrs_lock);
1186         nrs->nrs_throttling = 0;
1187         spin_unlock(&nrs->nrs_lock);
1188         wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
1189 }
1190
1191 /**
1192  * Performs a policy-specific ctl function on TBF policy instances; similar
1193  * to ioctl.
1194  *
1195  * \param[in]     policy the policy instance
1196  * \param[in]     opc    the opcode
1197  * \param[in,out] arg    used for passing parameters and information
1198  *
1199  * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1200  * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1201  *
1202  * \retval 0   operation carried out successfully
1203  * \retval -ve error
1204  */
1205 int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy, enum ptlrpc_nrs_ctl opc,
1206                 void *arg)
1207 {
1208         int rc = 0;
1209         ENTRY;
1210
1211         assert_spin_locked(&policy->pol_nrs->nrs_lock);
1212
1213         switch ((enum nrs_ctl_tbf)opc) {
1214         default:
1215                 RETURN(-EINVAL);
1216
1217         /**
1218          * Read RPC rate size of a policy instance.
1219          */
1220         case NRS_CTL_TBF_RD_RULE: {
1221                 struct nrs_tbf_head *head = policy->pol_private;
1222                 struct seq_file *m = (struct seq_file *) arg;
1223                 struct ptlrpc_service_part *svcpt;
1224
1225                 svcpt = policy->pol_nrs->nrs_svcpt;
1226                 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
1227
1228                 rc = nrs_tbf_rule_dump_all(head, m);
1229                 }
1230                 break;
1231
1232         /**
1233          * Write RPC rate of a policy instance.
1234          */
1235         case NRS_CTL_TBF_WR_RULE: {
1236                 struct nrs_tbf_head *head = policy->pol_private;
1237                 struct nrs_tbf_cmd *cmd;
1238
1239                 cmd = (struct nrs_tbf_cmd *)arg;
1240                 rc = nrs_tbf_command(policy,
1241                                      head,
1242                                      cmd);
1243                 }
1244                 break;
1245         }
1246
1247         RETURN(rc);
1248 }
1249
1250 /**
1251  * Is called for obtaining a TBF policy resource.
1252  *
1253  * \param[in]  policy     The policy on which the request is being asked for
1254  * \param[in]  nrq        The request for which resources are being taken
1255  * \param[in]  parent     Parent resource, unused in this policy
1256  * \param[out] resp       Resources references are placed in this array
1257  * \param[in]  moving_req Signifies limited caller context; unused in this
1258  *                        policy
1259  *
1260  *
1261  * \see nrs_resource_get_safe()
1262  */
1263 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
1264                            struct ptlrpc_nrs_request *nrq,
1265                            const struct ptlrpc_nrs_resource *parent,
1266                            struct ptlrpc_nrs_resource **resp,
1267                            bool moving_req)
1268 {
1269         struct nrs_tbf_head   *head;
1270         struct nrs_tbf_client *cli;
1271         struct nrs_tbf_client *tmp;
1272         struct ptlrpc_request *req;
1273
1274         if (parent == NULL) {
1275                 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
1276                 return 0;
1277         }
1278
1279         head = container_of(parent, struct nrs_tbf_head, th_res);
1280         req = container_of(nrq, struct ptlrpc_request, rq_nrq);
1281         cli = head->th_ops->o_cli_find(head, req);
1282         if (cli != NULL) {
1283                 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1284                 LASSERT(cli->tc_rule);
1285                 if (cli->tc_rule_sequence !=
1286                     atomic_read(&head->th_rule_sequence) ||
1287                     cli->tc_rule->tr_flags & NTRS_STOPPING) {
1288                         struct nrs_tbf_rule *rule;
1289
1290                         rule = nrs_tbf_rule_match(head, cli);
1291                         if (rule != cli->tc_rule)
1292                                 nrs_tbf_cli_reset(head, rule, cli);
1293                         else
1294                                 nrs_tbf_rule_put(rule);
1295                 } else if (cli->tc_rule_generation !=
1296                            cli->tc_rule->tr_generation) {
1297                         nrs_tbf_cli_reset_value(head, cli);
1298                 }
1299                 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1300                 goto out;
1301         }
1302
1303         OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
1304                           sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
1305         if (cli == NULL)
1306                 return -ENOMEM;
1307         nrs_tbf_cli_init(head, cli, req);
1308         tmp = head->th_ops->o_cli_findadd(head, cli);
1309         if (tmp != cli) {
1310                 atomic_dec(&cli->tc_ref);
1311                 nrs_tbf_cli_fini(cli);
1312                 cli = tmp;
1313         }
1314 out:
1315         *resp = &cli->tc_res;
1316
1317         return 1;
1318 }
1319
1320 /**
1321  * Called when releasing references to the resource hierachy obtained for a
1322  * request for scheduling using the TBF policy.
1323  *
1324  * \param[in] policy   the policy the resource belongs to
1325  * \param[in] res      the resource to be released
1326  */
1327 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
1328                             const struct ptlrpc_nrs_resource *res)
1329 {
1330         struct nrs_tbf_head   *head;
1331         struct nrs_tbf_client *cli;
1332
1333         /**
1334          * Do nothing for freeing parent, nrs_tbf_net resources
1335          */
1336         if (res->res_parent == NULL)
1337                 return;
1338
1339         cli = container_of(res, struct nrs_tbf_client, tc_res);
1340         head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
1341
1342         head->th_ops->o_cli_put(head, cli);
1343 }
1344
1345 /**
1346  * Called when getting a request from the TBF policy for handling, or just
1347  * peeking; removes the request from the policy when it is to be handled.
1348  *
1349  * \param[in] policy The policy
1350  * \param[in] peek   When set, signifies that we just want to examine the
1351  *                   request, and not handle it, so the request is not removed
1352  *                   from the policy.
1353  * \param[in] force  Force the policy to return a request; unused in this
1354  *                   policy
1355  *
1356  * \retval The request to be handled; this is the next request in the TBF
1357  *         rule
1358  *
1359  * \see ptlrpc_nrs_req_get_nolock()
1360  * \see nrs_request_get()
1361  */
1362 static
1363 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
1364                                            bool peek, bool force)
1365 {
1366         struct nrs_tbf_head       *head = policy->pol_private;
1367         struct ptlrpc_nrs_request *nrq = NULL;
1368         struct nrs_tbf_client     *cli;
1369         cfs_binheap_node_t        *node;
1370
1371         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1372
1373         if (!peek && policy->pol_nrs->nrs_throttling)
1374                 return NULL;
1375
1376         node = cfs_binheap_root(head->th_binheap);
1377         if (unlikely(node == NULL))
1378                 return NULL;
1379
1380         cli = container_of(node, struct nrs_tbf_client, tc_node);
1381         LASSERT(cli->tc_in_heap);
1382         if (peek) {
1383                 nrq = list_entry(cli->tc_list.next,
1384                                      struct ptlrpc_nrs_request,
1385                                      nr_u.tbf.tr_list);
1386         } else {
1387                 __u64 now = ktime_to_ns(ktime_get());
1388                 __u64 passed;
1389                 long  ntoken;
1390                 __u64 deadline;
1391
1392                 deadline = cli->tc_check_time +
1393                           cli->tc_nsecs;
1394                 LASSERT(now >= cli->tc_check_time);
1395                 passed = now - cli->tc_check_time;
1396                 ntoken = (passed * cli->tc_rpc_rate) / NSEC_PER_SEC;
1397                 ntoken += cli->tc_ntoken;
1398                 if (ntoken > cli->tc_depth)
1399                         ntoken = cli->tc_depth;
1400                 if (ntoken > 0) {
1401                         struct ptlrpc_request *req;
1402                         nrq = list_entry(cli->tc_list.next,
1403                                              struct ptlrpc_nrs_request,
1404                                              nr_u.tbf.tr_list);
1405                         req = container_of(nrq,
1406                                            struct ptlrpc_request,
1407                                            rq_nrq);
1408                         ntoken--;
1409                         cli->tc_ntoken = ntoken;
1410                         cli->tc_check_time = now;
1411                         list_del_init(&nrq->nr_u.tbf.tr_list);
1412                         if (list_empty(&cli->tc_list)) {
1413                                 cfs_binheap_remove(head->th_binheap,
1414                                                    &cli->tc_node);
1415                                 cli->tc_in_heap = false;
1416                         } else {
1417                                 cfs_binheap_relocate(head->th_binheap,
1418                                                      &cli->tc_node);
1419                         }
1420                         CDEBUG(D_RPCTRACE,
1421                                "NRS start %s request from %s, "
1422                                "seq: "LPU64"\n",
1423                                policy->pol_desc->pd_name,
1424                                libcfs_id2str(req->rq_peer),
1425                                nrq->nr_u.tbf.tr_sequence);
1426                 } else {
1427                         ktime_t time;
1428
1429                         spin_lock(&policy->pol_nrs->nrs_lock);
1430                         policy->pol_nrs->nrs_throttling = 1;
1431                         spin_unlock(&policy->pol_nrs->nrs_lock);
1432                         head->th_deadline = deadline;
1433                         time = ktime_set(0, 0);
1434                         time = ktime_add_ns(time, deadline);
1435                         hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
1436                 }
1437         }
1438
1439         return nrq;
1440 }
1441
1442 /**
1443  * Adds request \a nrq to \a policy's list of queued requests
1444  *
1445  * \param[in] policy The policy
1446  * \param[in] nrq    The request to add
1447  *
1448  * \retval 0 success; nrs_request_enqueue() assumes this function will always
1449  *                    succeed
1450  */
1451 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
1452                            struct ptlrpc_nrs_request *nrq)
1453 {
1454         struct nrs_tbf_head   *head;
1455         struct nrs_tbf_client *cli;
1456         int                    rc = 0;
1457
1458         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1459
1460         cli = container_of(nrs_request_resource(nrq),
1461                            struct nrs_tbf_client, tc_res);
1462         head = container_of(nrs_request_resource(nrq)->res_parent,
1463                             struct nrs_tbf_head, th_res);
1464         if (list_empty(&cli->tc_list)) {
1465                 LASSERT(!cli->tc_in_heap);
1466                 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
1467                 if (rc == 0) {
1468                         cli->tc_in_heap = true;
1469                         nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1470                         list_add_tail(&nrq->nr_u.tbf.tr_list,
1471                                           &cli->tc_list);
1472                         if (policy->pol_nrs->nrs_throttling) {
1473                                 __u64 deadline = cli->tc_check_time +
1474                                                  cli->tc_nsecs;
1475                                 if ((head->th_deadline > deadline) &&
1476                                     (hrtimer_try_to_cancel(&head->th_timer)
1477                                      >= 0)) {
1478                                         ktime_t time;
1479                                         head->th_deadline = deadline;
1480                                         time = ktime_set(0, 0);
1481                                         time = ktime_add_ns(time, deadline);
1482                                         hrtimer_start(&head->th_timer, time,
1483                                                       HRTIMER_MODE_ABS);
1484                                 }
1485                         }
1486                 }
1487         } else {
1488                 LASSERT(cli->tc_in_heap);
1489                 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1490                 list_add_tail(&nrq->nr_u.tbf.tr_list,
1491                                   &cli->tc_list);
1492         }
1493         return rc;
1494 }
1495
1496 /**
1497  * Removes request \a nrq from \a policy's list of queued requests.
1498  *
1499  * \param[in] policy The policy
1500  * \param[in] nrq    The request to remove
1501  */
1502 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
1503                              struct ptlrpc_nrs_request *nrq)
1504 {
1505         struct nrs_tbf_head   *head;
1506         struct nrs_tbf_client *cli;
1507
1508         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1509
1510         cli = container_of(nrs_request_resource(nrq),
1511                            struct nrs_tbf_client, tc_res);
1512         head = container_of(nrs_request_resource(nrq)->res_parent,
1513                             struct nrs_tbf_head, th_res);
1514
1515         LASSERT(!list_empty(&nrq->nr_u.tbf.tr_list));
1516         list_del_init(&nrq->nr_u.tbf.tr_list);
1517         if (list_empty(&cli->tc_list)) {
1518                 cfs_binheap_remove(head->th_binheap,
1519                                    &cli->tc_node);
1520                 cli->tc_in_heap = false;
1521         } else {
1522                 cfs_binheap_relocate(head->th_binheap,
1523                                      &cli->tc_node);
1524         }
1525 }
1526
1527 /**
1528  * Prints a debug statement right before the request \a nrq stops being
1529  * handled.
1530  *
1531  * \param[in] policy The policy handling the request
1532  * \param[in] nrq    The request being handled
1533  *
1534  * \see ptlrpc_server_finish_request()
1535  * \see ptlrpc_nrs_req_stop_nolock()
1536  */
1537 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
1538                               struct ptlrpc_nrs_request *nrq)
1539 {
1540         struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
1541                                                   rq_nrq);
1542
1543         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1544
1545         CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: "LPU64"\n",
1546                policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
1547                nrq->nr_u.tbf.tr_sequence);
1548 }
1549
1550 #ifdef LPROCFS
1551
1552 /**
1553  * lprocfs interface
1554  */
1555
1556 /**
1557  * The maximum RPC rate.
1558  */
1559 #define LPROCFS_NRS_RATE_MAX            65535
1560
1561 static int
1562 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
1563 {
1564         struct ptlrpc_service       *svc = m->private;
1565         int                          rc;
1566
1567         seq_printf(m, "regular_requests:\n");
1568         /**
1569          * Perform two separate calls to this as only one of the NRS heads'
1570          * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
1571          * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
1572          */
1573         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1574                                        NRS_POL_NAME_TBF,
1575                                        NRS_CTL_TBF_RD_RULE,
1576                                        false, m);
1577         if (rc == 0) {
1578                 /**
1579                  * -ENOSPC means buf in the parameter m is overflow, return 0
1580                  * here to let upper layer function seq_read alloc a larger
1581                  * memory area and do this process again.
1582                  */
1583         } else if (rc == -ENOSPC) {
1584                 return 0;
1585
1586                 /**
1587                  * Ignore -ENODEV as the regular NRS head's policy may be in the
1588                  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1589                  */
1590         } else if (rc != -ENODEV) {
1591                 return rc;
1592         }
1593
1594         if (!nrs_svc_has_hp(svc))
1595                 goto no_hp;
1596
1597         seq_printf(m, "high_priority_requests:\n");
1598         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1599                                        NRS_POL_NAME_TBF,
1600                                        NRS_CTL_TBF_RD_RULE,
1601                                        false, m);
1602         if (rc == 0) {
1603                 /**
1604                  * -ENOSPC means buf in the parameter m is overflow, return 0
1605                  * here to let upper layer function seq_read alloc a larger
1606                  * memory area and do this process again.
1607                  */
1608         } else if (rc == -ENOSPC) {
1609                 return 0;
1610         }
1611
1612 no_hp:
1613
1614         return rc;
1615 }
1616
1617 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char **val)
1618 {
1619         int rc;
1620         char *token;
1621
1622         token = strsep(val, "}");
1623         if (*val == NULL)
1624                 GOTO(out, rc = -EINVAL);
1625
1626         if (strlen(token) <= 1 ||
1627             token[0] != '{')
1628                 GOTO(out, rc = -EINVAL);
1629         /* Skip '{' */
1630         token++;
1631
1632         /* Should be followed by ' ' or nothing */
1633         if ((*val)[0] == '\0')
1634                 *val = NULL;
1635         else if ((*val)[0] == ' ')
1636                 (*val)++;
1637         else
1638                 GOTO(out, rc = -EINVAL);
1639
1640         rc = nrs_tbf_jobid_parse(cmd, token);
1641         if (!rc)
1642                 cmd->tc_valid_types |= NRS_TBF_FLAG_JOBID;
1643
1644         rc = nrs_tbf_nid_parse(cmd, token);
1645         if (!rc)
1646                 cmd->tc_valid_types |= NRS_TBF_FLAG_NID;
1647
1648         if (!cmd->tc_valid_types)
1649                 rc = -EINVAL;
1650         else
1651                 rc = 0;
1652 out:
1653         return rc;
1654 }
1655
1656
1657 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
1658 {
1659         if (cmd->tc_valid_types & NRS_TBF_FLAG_JOBID)
1660                 nrs_tbf_jobid_cmd_fini(cmd);
1661         if (cmd->tc_valid_types & NRS_TBF_FLAG_NID)
1662                 nrs_tbf_nid_cmd_fini(cmd);
1663 }
1664
1665 static struct nrs_tbf_cmd *
1666 nrs_tbf_parse_cmd(char *buffer, unsigned long count)
1667 {
1668         static struct nrs_tbf_cmd *cmd;
1669         char                      *token;
1670         char                      *val;
1671         int                        i;
1672         int                        rc = 0;
1673
1674         OBD_ALLOC_PTR(cmd);
1675         if (cmd == NULL)
1676                 GOTO(out, rc = -ENOMEM);
1677
1678         val = buffer;
1679         token = strsep(&val, " ");
1680         if (val == NULL || strlen(val) == 0)
1681                 GOTO(out_free_cmd, rc = -EINVAL);
1682
1683         /* Type of the command */
1684         if (strcmp(token, "start") == 0)
1685                 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
1686         else if (strcmp(token, "stop") == 0)
1687                 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
1688         else if (strcmp(token, "change") == 0)
1689                 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RATE;
1690         else
1691                 GOTO(out_free_cmd, rc = -EINVAL);
1692
1693         /* Name of the rule */
1694         token = strsep(&val, " ");
1695         if (val == NULL) {
1696                 /**
1697                  * Stop comand only need name argument,
1698                  * But other commands need ID or rate argument.
1699                  */
1700                 if (cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE)
1701                         GOTO(out_free_cmd, rc = -EINVAL);
1702         }
1703
1704         for (i = 0; i < strlen(token); i++) {
1705                 if ((!isalnum(token[i])) &&
1706                     (token[i] != '_'))
1707                         GOTO(out_free_cmd, rc = -EINVAL);
1708         }
1709         cmd->tc_name = token;
1710
1711         if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
1712                 /* List of ID */
1713                 LASSERT(val);
1714                 rc = nrs_tbf_id_parse(cmd, &val);
1715                 if (rc)
1716                         GOTO(out_free_cmd, rc);
1717         }
1718
1719         if (val != NULL) {
1720                 if (cmd->tc_cmd == NRS_CTL_TBF_STOP_RULE ||
1721                     strlen(val) == 0 || !isdigit(val[0]))
1722                         GOTO(out_free_nid, rc = -EINVAL);
1723
1724                 cmd->tc_rpc_rate = simple_strtoull(val, NULL, 10);
1725                 if (cmd->tc_rpc_rate <= 0 ||
1726                     cmd->tc_rpc_rate >= LPROCFS_NRS_RATE_MAX)
1727                         GOTO(out_free_nid, rc = -EINVAL);
1728         } else {
1729                 if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RATE)
1730                         GOTO(out_free_nid, rc = -EINVAL);
1731                 /* No RPC rate given */
1732                 cmd->tc_rpc_rate = tbf_rate;
1733         }
1734         goto out;
1735 out_free_nid:
1736         nrs_tbf_cmd_fini(cmd);
1737 out_free_cmd:
1738         OBD_FREE_PTR(cmd);
1739 out:
1740         if (rc)
1741                 cmd = ERR_PTR(rc);
1742         return cmd;
1743 }
1744
1745 extern struct nrs_core nrs_core;
1746 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
1747 static ssize_t
1748 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file, const char *buffer,
1749                                       size_t count, loff_t *off)
1750 {
1751         struct seq_file           *m = file->private_data;
1752         struct ptlrpc_service     *svc = m->private;
1753         char                      *kernbuf;
1754         char                      *val;
1755         int                        rc;
1756         static struct nrs_tbf_cmd *cmd;
1757         enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
1758         unsigned long              length;
1759         char                      *token;
1760
1761         OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1762         if (kernbuf == NULL)
1763                 GOTO(out, rc = -ENOMEM);
1764
1765         if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
1766                 GOTO(out_free_kernbuff, rc = -EINVAL);
1767
1768         if (copy_from_user(kernbuf, buffer, count))
1769                 GOTO(out_free_kernbuff, rc = -EFAULT);
1770
1771         val = kernbuf;
1772         token = strsep(&val, " ");
1773         if (val == NULL)
1774                 GOTO(out_free_kernbuff, rc = -EINVAL);
1775
1776         if (strcmp(token, "reg") == 0) {
1777                 queue = PTLRPC_NRS_QUEUE_REG;
1778         } else if (strcmp(token, "hp") == 0) {
1779                 queue = PTLRPC_NRS_QUEUE_HP;
1780         } else {
1781                 kernbuf[strlen(token)] = ' ';
1782                 val = kernbuf;
1783         }
1784         length = strlen(val);
1785
1786         if (length == 0)
1787                 GOTO(out_free_kernbuff, rc = -EINVAL);
1788
1789         if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
1790                 GOTO(out_free_kernbuff, rc = -ENODEV);
1791         else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
1792                 queue = PTLRPC_NRS_QUEUE_REG;
1793
1794         cmd = nrs_tbf_parse_cmd(val, length);
1795         if (IS_ERR(cmd))
1796                 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
1797
1798         /**
1799          * Serialize NRS core lprocfs operations with policy registration/
1800          * unregistration.
1801          */
1802         mutex_lock(&nrs_core.nrs_mutex);
1803         rc = ptlrpc_nrs_policy_control(svc, queue,
1804                                        NRS_POL_NAME_TBF,
1805                                        NRS_CTL_TBF_WR_RULE,
1806                                        false, cmd);
1807         mutex_unlock(&nrs_core.nrs_mutex);
1808
1809         nrs_tbf_cmd_fini(cmd);
1810         OBD_FREE_PTR(cmd);
1811 out_free_kernbuff:
1812         OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1813 out:
1814         return rc ? rc : count;
1815 }
1816 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
1817
1818 /**
1819  * Initializes a TBF policy's lprocfs interface for service \a svc
1820  *
1821  * \param[in] svc the service
1822  *
1823  * \retval 0    success
1824  * \retval != 0 error
1825  */
1826 int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
1827 {
1828         struct lprocfs_seq_vars nrs_tbf_lprocfs_vars[] = {
1829                 { .name         = "nrs_tbf_rule",
1830                   .fops         = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
1831                   .data = svc },
1832                 { NULL }
1833         };
1834
1835         if (svc->srv_procroot == NULL)
1836                 return 0;
1837
1838         return lprocfs_seq_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars,
1839                                     NULL);
1840 }
1841
1842 /**
1843  * Cleans up a TBF policy's lprocfs interface for service \a svc
1844  *
1845  * \param[in] svc the service
1846  */
1847 void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
1848 {
1849         if (svc->srv_procroot == NULL)
1850                 return;
1851
1852         lprocfs_remove_proc_entry("nrs_tbf_rule", svc->srv_procroot);
1853 }
1854
1855 #endif /* LPROCFS */
1856
1857 /**
1858  * TBF policy operations
1859  */
1860 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
1861         .op_policy_start        = nrs_tbf_start,
1862         .op_policy_stop         = nrs_tbf_stop,
1863         .op_policy_ctl          = nrs_tbf_ctl,
1864         .op_res_get             = nrs_tbf_res_get,
1865         .op_res_put             = nrs_tbf_res_put,
1866         .op_req_get             = nrs_tbf_req_get,
1867         .op_req_enqueue         = nrs_tbf_req_add,
1868         .op_req_dequeue         = nrs_tbf_req_del,
1869         .op_req_stop            = nrs_tbf_req_stop,
1870 #ifdef LPROCFS
1871         .op_lprocfs_init        = nrs_tbf_lprocfs_init,
1872         .op_lprocfs_fini        = nrs_tbf_lprocfs_fini,
1873 #endif
1874 };
1875
1876 /**
1877  * TBF policy configuration
1878  */
1879 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
1880         .nc_name                = NRS_POL_NAME_TBF,
1881         .nc_ops                 = &nrs_tbf_ops,
1882         .nc_compat              = nrs_policy_compat_all,
1883 };
1884
1885 /** @} tbf */
1886
1887 /** @} nrs */
1888
1889 #endif /* HAVE_SERVER_SUPPORT */