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