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