Whamcloud - gitweb
LU-9859 ptlrpc: simplify nrs_tbf_jobid_list_parse() 38/50838/8
authorMr NeilBrown <neilb@suse.de>
Tue, 24 Nov 2020 05:52:12 +0000 (16:52 +1100)
committerOleg Drokin <green@whamcloud.com>
Wed, 19 Jul 2023 16:43:38 +0000 (16:43 +0000)
If we duplicate the string passed to nrs_tbf_jobid_list_parse(), we
can parse using standard mutating tools.

Test-Parameters: trivial
Test-Parameters: testlist=sanityn env=ONLY=77
Signed-off-by: Mr NeilBrown <neilb@suse.de>
Change-Id: Id04390c2ed0a26a0311a8bbc784979eb18f4d19d
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/50838
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lustre/ptlrpc/nrs_tbf.c

index dac9180..5b66dd9 100644 (file)
@@ -809,7 +809,7 @@ nrs_tbf_jobid_list_free(struct list_head *jobid_list)
 }
 
 static int
-nrs_tbf_jobid_list_add(struct cfs_lstr *id, struct list_head *jobid_list)
+nrs_tbf_jobid_list_add(char *id, struct list_head *jobid_list)
 {
        struct nrs_tbf_jobid *jobid;
        char *ptr;
@@ -818,14 +818,14 @@ nrs_tbf_jobid_list_add(struct cfs_lstr *id, struct list_head *jobid_list)
        if (jobid == NULL)
                return -ENOMEM;
 
-       OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
+       OBD_ALLOC(jobid->tj_id, strlen(id) + 1);
        if (jobid->tj_id == NULL) {
                OBD_FREE_PTR(jobid);
                return -ENOMEM;
        }
 
-       memcpy(jobid->tj_id, id->ls_str, id->ls_len);
-       ptr = lprocfs_strnstr(id->ls_str, "*", id->ls_len);
+       strcpy(jobid->tj_id, id);
+       ptr = strchr(id, '*');
        if (ptr == NULL)
                jobid->tj_match_flag = NRS_TBF_MATCH_FULL;
        else
@@ -887,28 +887,28 @@ nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id)
 }
 
 static int
-nrs_tbf_jobid_list_parse(char *str, int len, struct list_head *jobid_list)
+nrs_tbf_jobid_list_parse(char *orig, struct list_head *jobid_list)
 {
-       struct cfs_lstr src;
-       struct cfs_lstr res;
+       char *str, *copy;
        int rc = 0;
        ENTRY;
 
-       src.ls_str = str;
-       src.ls_len = len;
+       copy = kstrdup(orig, GFP_KERNEL);
+       if (!copy)
+               return -ENOMEM;
+       str = copy;
        INIT_LIST_HEAD(jobid_list);
-       while (src.ls_str) {
-               rc = cfs_gettok(&src, ' ', &res);
-               if (rc == 0) {
-                       rc = -EINVAL;
-                       break;
-               }
-               rc = nrs_tbf_jobid_list_add(&res, jobid_list);
-               if (rc)
-                       break;
+       while (str && rc == 0) {
+               char *tok = strsep(&str, " ");
+
+               if (*tok)
+                       rc = nrs_tbf_jobid_list_add(tok, jobid_list);
        }
+       if (list_empty(jobid_list))
+               rc = -EINVAL;
        if (rc)
                nrs_tbf_jobid_list_free(jobid_list);
+       kfree(copy);
        RETURN(rc);
 }
 
@@ -961,7 +961,6 @@ static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, char *id)
 
        /* parse jobid list */
        rc = nrs_tbf_jobid_list_parse(cmd->u.tc_start.ts_jobids_str,
-                                     strlen(cmd->u.tc_start.ts_jobids_str),
                                      &cmd->u.tc_start.ts_jobids);
        if (rc)
                nrs_tbf_jobid_cmd_fini(cmd);
@@ -988,7 +987,6 @@ static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
        INIT_LIST_HEAD(&rule->tr_jobids);
        if (!list_empty(&start->u.tc_start.ts_jobids)) {
                rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
-                                             strlen(rule->tr_jobids_str),
                                              &rule->tr_jobids);
                if (rc)
                        CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
@@ -1856,29 +1854,24 @@ nrs_tbf_expression_parse(char *str, struct list_head *cond_list)
        len -= 2;
 
        if (strcmp(field, "nid") == 0) {
-               if (cfs_parse_nidlist(str, len,
-                                     &expr->te_cond) <= 0)
+               if (cfs_parse_nidlist(str, len, &expr->te_cond) <= 0)
                        GOTO(out, rc = -EINVAL);
                expr->te_field = NRS_TBF_FIELD_NID;
        } else if (strcmp(field, "jobid") == 0) {
-               if (nrs_tbf_jobid_list_parse(str, len,
-                                            &expr->te_cond) < 0)
+               if (nrs_tbf_jobid_list_parse(str, &expr->te_cond) < 0)
                        GOTO(out, rc = -EINVAL);
                expr->te_field = NRS_TBF_FIELD_JOBID;
        } else if (strcmp(field, "opcode") == 0) {
-               if (nrs_tbf_opcode_list_parse(str, len,
-                                             &expr->te_opcodes) < 0)
+               if (nrs_tbf_opcode_list_parse(str, len, &expr->te_opcodes) < 0)
                        GOTO(out, rc = -EINVAL);
                expr->te_field = NRS_TBF_FIELD_OPCODE;
        } else if (strcmp(field, "uid") == 0) {
-               if (nrs_tbf_id_list_parse(str,
-                                         &expr->te_cond,
+               if (nrs_tbf_id_list_parse(str, &expr->te_cond,
                                          NRS_TBF_FLAG_UID) < 0)
                        GOTO(out, rc = -EINVAL);
                expr->te_field = NRS_TBF_FIELD_UID;
        } else if (strcmp(field, "gid") == 0) {
-               if (nrs_tbf_id_list_parse(str,
-                                         &expr->te_cond,
+               if (nrs_tbf_id_list_parse(str, &expr->te_cond,
                                          NRS_TBF_FLAG_GID) < 0)
                        GOTO(out, rc = -EINVAL);
                expr->te_field = NRS_TBF_FIELD_GID;