Whamcloud - gitweb
LU-6245 libcfs: replace CFS_MODULE_PARAM with linux kernel module api
[fs/lustre-release.git] / lustre / ptlrpc / nrs_tbf.c
index b5d00d3..282eb8a 100644 (file)
 #define NRS_POL_NAME_TBF       "tbf"
 
 static int tbf_jobid_cache_size = 8192;
-CFS_MODULE_PARM(tbf_jobid_cache_size, "i", int, 0644,
-               "The size of jobid cache");
+module_param(tbf_jobid_cache_size, int, 0644);
+MODULE_PARM_DESC(tbf_jobid_cache_size, "The size of jobid cache");
 
 static int tbf_rate = 10000;
-CFS_MODULE_PARM(tbf_rate, "i", int, 0644,
-               "Default rate limit in RPCs/s");
+module_param(tbf_rate, int, 0644);
+MODULE_PARM_DESC(tbf_rate, "Default rate limit in RPCs/s");
 
 static int tbf_depth = 3;
-CFS_MODULE_PARM(tbf_depth, "i", int, 0644,
-               "How many tokens that a client can save up");
+module_param(tbf_depth, int, 0644);
+MODULE_PARM_DESC(tbf_depth, "How many tokens that a client can save up");
 
 static enum hrtimer_restart nrs_tbf_timer_cb(struct hrtimer *timer)
 {
@@ -73,9 +73,7 @@ static enum hrtimer_restart nrs_tbf_timer_cb(struct hrtimer *timer)
        struct ptlrpc_nrs   *nrs = head->th_res.res_policy->pol_nrs;
        struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
 
-       spin_lock(&nrs->nrs_lock);
        nrs->nrs_throttling = 0;
-       spin_unlock(&nrs->nrs_lock);
        wake_up(&svcpt->scp_waitq);
 
        return HRTIMER_NORESTART;
@@ -118,7 +116,9 @@ nrs_tbf_cli_rule_put(struct nrs_tbf_client *cli)
 {
        LASSERT(!list_empty(&cli->tc_linkage));
        LASSERT(cli->tc_rule);
+       spin_lock(&cli->tc_rule->tr_rule_lock);
        list_del_init(&cli->tc_linkage);
+       spin_unlock(&cli->tc_rule->tr_rule_lock);
        nrs_tbf_rule_put(cli->tc_rule);
        cli->tc_rule = NULL;
 }
@@ -148,7 +148,8 @@ nrs_tbf_cli_reset(struct nrs_tbf_head *head,
                  struct nrs_tbf_rule *rule,
                  struct nrs_tbf_client *cli)
 {
-       if (!list_empty(&cli->tc_linkage)) {
+       spin_lock(&cli->tc_rule_lock);
+       if (cli->tc_rule != NULL && !list_empty(&cli->tc_linkage)) {
                LASSERT(rule != cli->tc_rule);
                nrs_tbf_cli_rule_put(cli);
        }
@@ -156,7 +157,10 @@ nrs_tbf_cli_reset(struct nrs_tbf_head *head,
        LASSERT(list_empty(&cli->tc_linkage));
        /* Rule's ref is added before called */
        cli->tc_rule = rule;
+       spin_lock(&rule->tr_rule_lock);
        list_add_tail(&cli->tc_linkage, &rule->tr_cli_list);
+       spin_unlock(&rule->tr_rule_lock);
+       spin_unlock(&cli->tc_rule_lock);
        nrs_tbf_cli_reset_value(head, cli);
 }
 
@@ -254,6 +258,7 @@ nrs_tbf_cli_init(struct nrs_tbf_head *head,
        head->th_ops->o_cli_init(cli, req);
        INIT_LIST_HEAD(&cli->tc_list);
        INIT_LIST_HEAD(&cli->tc_linkage);
+       spin_lock_init(&cli->tc_rule_lock);
        atomic_set(&cli->tc_ref, 1);
        rule = nrs_tbf_rule_match(head, cli);
        nrs_tbf_cli_reset(head, rule, cli);
@@ -265,7 +270,9 @@ nrs_tbf_cli_fini(struct nrs_tbf_client *cli)
        LASSERT(list_empty(&cli->tc_list));
        LASSERT(!cli->tc_in_heap);
        LASSERT(atomic_read(&cli->tc_ref) == 0);
+       spin_lock(&cli->tc_rule_lock);
        nrs_tbf_cli_rule_put(cli);
+       spin_unlock(&cli->tc_rule_lock);
        OBD_FREE_PTR(cli);
 }
 
@@ -289,11 +296,15 @@ nrs_tbf_rule_start(struct ptlrpc_nrs_policy *policy,
 
        memcpy(rule->tr_name, start->tc_name, strlen(start->tc_name));
        rule->tr_rpc_rate = start->tc_rpc_rate;
-       rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
+       rule->tr_nsecs = NSEC_PER_SEC;
+       do_div(rule->tr_nsecs, rule->tr_rpc_rate);
        rule->tr_depth = tbf_depth;
        atomic_set(&rule->tr_ref, 1);
        INIT_LIST_HEAD(&rule->tr_cli_list);
        INIT_LIST_HEAD(&rule->tr_nids);
+       INIT_LIST_HEAD(&rule->tr_linkage);
+       spin_lock_init(&rule->tr_rule_lock);
+       rule->tr_head = head;
 
        rc = head->th_ops->o_rule_init(policy, rule, start);
        if (rc) {
@@ -311,7 +322,6 @@ nrs_tbf_rule_start(struct ptlrpc_nrs_policy *policy,
                return -EEXIST;
        }
        list_add(&rule->tr_linkage, &head->th_list);
-       rule->tr_head = head;
        spin_unlock(&head->th_rule_lock);
        atomic_inc(&head->th_rule_sequence);
        if (start->tc_rule_flags & NTRS_DEFAULT) {
@@ -337,7 +347,8 @@ nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
                return -ENOENT;
 
        rule->tr_rpc_rate = change->tc_rpc_rate;
-       rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
+       rule->tr_nsecs = NSEC_PER_SEC;
+       do_div(rule->tr_nsecs, rule->tr_rpc_rate);
        rule->tr_generation++;
        nrs_tbf_rule_put(rule);
 
@@ -407,7 +418,8 @@ nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
  * \retval 0 e1 > e2
  * \retval 1 e1 < e2
  */
-static int tbf_cli_compare(cfs_binheap_node_t *e1, cfs_binheap_node_t *e2)
+static int
+tbf_cli_compare(struct cfs_binheap_node *e1, struct cfs_binheap_node *e2)
 {
        struct nrs_tbf_client *cli1;
        struct nrs_tbf_client *cli2;
@@ -434,13 +446,13 @@ static int tbf_cli_compare(cfs_binheap_node_t *e1, cfs_binheap_node_t *e2)
 /**
  * TBF binary heap operations
  */
-static cfs_binheap_ops_t nrs_tbf_heap_ops = {
+static struct cfs_binheap_ops nrs_tbf_heap_ops = {
        .hop_enter      = NULL,
        .hop_exit       = NULL,
        .hop_compare    = tbf_cli_compare,
 };
 
-static unsigned nrs_tbf_jobid_hop_hash(cfs_hash_t *hs, const void *key,
+static unsigned nrs_tbf_jobid_hop_hash(struct cfs_hash *hs, const void *key,
                                  unsigned mask)
 {
        return cfs_hash_djb2_hash(key, strlen(key), mask);
@@ -469,7 +481,7 @@ static void *nrs_tbf_jobid_hop_object(struct hlist_node *hnode)
        return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
 }
 
-static void nrs_tbf_jobid_hop_get(cfs_hash_t *hs, struct hlist_node *hnode)
+static void nrs_tbf_jobid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
 {
        struct nrs_tbf_client *cli = hlist_entry(hnode,
                                                     struct nrs_tbf_client,
@@ -478,7 +490,7 @@ static void nrs_tbf_jobid_hop_get(cfs_hash_t *hs, struct hlist_node *hnode)
        atomic_inc(&cli->tc_ref);
 }
 
-static void nrs_tbf_jobid_hop_put(cfs_hash_t *hs, struct hlist_node *hnode)
+static void nrs_tbf_jobid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
 {
        struct nrs_tbf_client *cli = hlist_entry(hnode,
                                                     struct nrs_tbf_client,
@@ -487,18 +499,19 @@ static void nrs_tbf_jobid_hop_put(cfs_hash_t *hs, struct hlist_node *hnode)
        atomic_dec(&cli->tc_ref);
 }
 
-static void nrs_tbf_jobid_hop_exit(cfs_hash_t *hs, struct hlist_node *hnode)
+static void
+nrs_tbf_jobid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
 
 {
        struct nrs_tbf_client *cli = hlist_entry(hnode,
-                                                    struct nrs_tbf_client,
-                                                    tc_hnode);
+                                                struct nrs_tbf_client,
+                                                tc_hnode);
 
        LASSERT(atomic_read(&cli->tc_ref) == 0);
        nrs_tbf_cli_fini(cli);
 }
 
-static cfs_hash_ops_t nrs_tbf_jobid_hash_ops = {
+static struct cfs_hash_ops nrs_tbf_jobid_hash_ops = {
        .hs_hash        = nrs_tbf_jobid_hop_hash,
        .hs_keycmp      = nrs_tbf_jobid_hop_keycmp,
        .hs_key         = nrs_tbf_jobid_hop_key,
@@ -514,8 +527,8 @@ static cfs_hash_ops_t nrs_tbf_jobid_hash_ops = {
                                  CFS_HASH_DEPTH)
 
 static struct nrs_tbf_client *
-nrs_tbf_jobid_hash_lookup(cfs_hash_t *hs,
-                         cfs_hash_bd_t *bd,
+nrs_tbf_jobid_hash_lookup(struct cfs_hash *hs,
+                         struct cfs_hash_bd *bd,
                          const char *jobid)
 {
        struct hlist_node *hnode;
@@ -542,8 +555,8 @@ nrs_tbf_jobid_cli_find(struct nrs_tbf_head *head,
 {
        const char              *jobid;
        struct nrs_tbf_client   *cli;
-       cfs_hash_t              *hs = head->th_cli_hash;
-       cfs_hash_bd_t            bd;
+       struct cfs_hash         *hs = head->th_cli_hash;
+       struct cfs_hash_bd               bd;
 
        jobid = lustre_msg_get_jobid(req->rq_reqmsg);
        if (jobid == NULL)
@@ -561,8 +574,8 @@ nrs_tbf_jobid_cli_findadd(struct nrs_tbf_head *head,
 {
        const char              *jobid;
        struct nrs_tbf_client   *ret;
-       cfs_hash_t              *hs = head->th_cli_hash;
-       cfs_hash_bd_t            bd;
+       struct cfs_hash         *hs = head->th_cli_hash;
+       struct cfs_hash_bd               bd;
 
        jobid = cli->tc_jobid;
        cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
@@ -580,8 +593,8 @@ static void
 nrs_tbf_jobid_cli_put(struct nrs_tbf_head *head,
                      struct nrs_tbf_client *cli)
 {
-       cfs_hash_bd_t            bd;
-       cfs_hash_t              *hs = head->th_cli_hash;
+       struct cfs_hash_bd               bd;
+       struct cfs_hash         *hs = head->th_cli_hash;
        struct nrs_tbf_bucket   *bkt;
        int                      hw;
        struct list_head        zombies;
@@ -653,7 +666,7 @@ nrs_tbf_jobid_startup(struct ptlrpc_nrs_policy *policy,
        int                      bits;
        int                      i;
        int                      rc;
-       cfs_hash_bd_t            bd;
+       struct cfs_hash_bd       bd;
 
        bits = nrs_tbf_jobid_hash_order();
        if (bits < NRS_TBF_JOBID_BKT_BITS)
@@ -822,9 +835,10 @@ static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
 static int
 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
 {
-       return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
+       seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
                          rule->tr_jobids_str, rule->tr_rpc_rate,
                          atomic_read(&rule->tr_ref) - 1);
+       return 0;
 }
 
 static int
@@ -864,7 +878,7 @@ static struct nrs_tbf_ops nrs_tbf_jobid_ops = {
 #define NRS_TBF_NID_BKT_BITS    8
 #define NRS_TBF_NID_BITS        16
 
-static unsigned nrs_tbf_nid_hop_hash(cfs_hash_t *hs, const void *key,
+static unsigned nrs_tbf_nid_hop_hash(struct cfs_hash *hs, const void *key,
                                  unsigned mask)
 {
        return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
@@ -894,7 +908,7 @@ static void *nrs_tbf_nid_hop_object(struct hlist_node *hnode)
        return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
 }
 
-static void nrs_tbf_nid_hop_get(cfs_hash_t *hs, struct hlist_node *hnode)
+static void nrs_tbf_nid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
 {
        struct nrs_tbf_client *cli = hlist_entry(hnode,
                                                     struct nrs_tbf_client,
@@ -903,7 +917,7 @@ static void nrs_tbf_nid_hop_get(cfs_hash_t *hs, struct hlist_node *hnode)
        atomic_inc(&cli->tc_ref);
 }
 
-static void nrs_tbf_nid_hop_put(cfs_hash_t *hs, struct hlist_node *hnode)
+static void nrs_tbf_nid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
 {
        struct nrs_tbf_client *cli = hlist_entry(hnode,
                                                     struct nrs_tbf_client,
@@ -912,7 +926,7 @@ static void nrs_tbf_nid_hop_put(cfs_hash_t *hs, struct hlist_node *hnode)
        atomic_dec(&cli->tc_ref);
 }
 
-static void nrs_tbf_nid_hop_exit(cfs_hash_t *hs, struct hlist_node *hnode)
+static void nrs_tbf_nid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
 {
        struct nrs_tbf_client *cli = hlist_entry(hnode,
                                                     struct nrs_tbf_client,
@@ -925,7 +939,7 @@ static void nrs_tbf_nid_hop_exit(cfs_hash_t *hs, struct hlist_node *hnode)
        nrs_tbf_cli_fini(cli);
 }
 
-static cfs_hash_ops_t nrs_tbf_nid_hash_ops = {
+static struct cfs_hash_ops nrs_tbf_nid_hash_ops = {
        .hs_hash        = nrs_tbf_nid_hop_hash,
        .hs_keycmp      = nrs_tbf_nid_hop_keycmp,
        .hs_key         = nrs_tbf_nid_hop_key,
@@ -1027,9 +1041,10 @@ static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
 static int
 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
 {
-       return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
+       seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
                          rule->tr_nids_str, rule->tr_rpc_rate,
                          atomic_read(&rule->tr_ref) - 1);
+       return 0;
 }
 
 static int
@@ -1183,9 +1198,7 @@ static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
        LASSERT(cfs_binheap_is_empty(head->th_binheap));
        cfs_binheap_destroy(head->th_binheap);
        OBD_FREE_PTR(head);
-       spin_lock(&nrs->nrs_lock);
        nrs->nrs_throttling = 0;
-       spin_unlock(&nrs->nrs_lock);
        wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
 }
 
@@ -1368,7 +1381,7 @@ struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
        struct nrs_tbf_head       *head = policy->pol_private;
        struct ptlrpc_nrs_request *nrq = NULL;
        struct nrs_tbf_client     *cli;
-       cfs_binheap_node_t        *node;
+       struct cfs_binheap_node   *node;
 
        assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
 
@@ -1388,14 +1401,15 @@ struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
        } else {
                __u64 now = ktime_to_ns(ktime_get());
                __u64 passed;
-               long  ntoken;
+               __u64 ntoken;
                __u64 deadline;
 
                deadline = cli->tc_check_time +
                          cli->tc_nsecs;
                LASSERT(now >= cli->tc_check_time);
                passed = now - cli->tc_check_time;
-               ntoken = (passed * cli->tc_rpc_rate) / NSEC_PER_SEC;
+               ntoken = passed * cli->tc_rpc_rate;
+               do_div(ntoken, NSEC_PER_SEC);
                ntoken += cli->tc_ntoken;
                if (ntoken > cli->tc_depth)
                        ntoken = cli->tc_depth;
@@ -1428,9 +1442,7 @@ struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
                } else {
                        ktime_t time;
 
-                       spin_lock(&policy->pol_nrs->nrs_lock);
                        policy->pol_nrs->nrs_throttling = 1;
-                       spin_unlock(&policy->pol_nrs->nrs_lock);
                        head->th_deadline = deadline;
                        time = ktime_set(0, 0);
                        time = ktime_add_ns(time, deadline);
@@ -1549,7 +1561,7 @@ static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
               nrq->nr_u.tbf.tr_sequence);
 }
 
-#ifdef LPROCFS
+#ifdef CONFIG_PROC_FS
 
 /**
  * lprocfs interface
@@ -1747,7 +1759,8 @@ out:
 extern struct nrs_core nrs_core;
 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
 static ssize_t
-ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file, const char *buffer,
+ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file,
+                                     const char __user *buffer,
                                      size_t count, loff_t *off)
 {
        struct seq_file           *m = file->private_data;
@@ -1827,7 +1840,7 @@ LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
  */
 static int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
 {
-       struct lprocfs_seq_vars nrs_tbf_lprocfs_vars[] = {
+       struct lprocfs_vars nrs_tbf_lprocfs_vars[] = {
                { .name         = "nrs_tbf_rule",
                  .fops         = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
                  .data = svc },
@@ -1837,8 +1850,7 @@ static int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
        if (svc->srv_procroot == NULL)
                return 0;
 
-       return lprocfs_seq_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars,
-                                   NULL);
+       return lprocfs_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars, NULL);
 }
 
 /**
@@ -1854,7 +1866,7 @@ static void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
        lprocfs_remove_proc_entry("nrs_tbf_rule", svc->srv_procroot);
 }
 
-#endif /* LPROCFS */
+#endif /* CONFIG_PROC_FS */
 
 /**
  * TBF policy operations
@@ -1869,7 +1881,7 @@ static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
        .op_req_enqueue         = nrs_tbf_req_add,
        .op_req_dequeue         = nrs_tbf_req_del,
        .op_req_stop            = nrs_tbf_req_stop,
-#ifdef LPROCFS
+#ifdef CONFIG_PROC_FS
        .op_lprocfs_init        = nrs_tbf_lprocfs_init,
        .op_lprocfs_fini        = nrs_tbf_lprocfs_fini,
 #endif