From: Qian Yingjin Date: Thu, 12 Jan 2017 02:50:10 +0000 (+0800) Subject: LU-8236 nrs: Add wildcard support for JOBID TBF rule X-Git-Tag: 2.9.56~53 X-Git-Url: https://git.whamcloud.com/?a=commitdiff_plain;h=d5b5a07a8b07ab51a2215de593cb8bfce7e589d0;p=fs%2Flustre-release.git LU-8236 nrs: Add wildcard support for JOBID TBF rule This patch adds wildcard support for JOBID TBF rule. The following kind of wildcard matching rules can be used: start ruleName jobid={dd.*} rate=20 start ruleName jobid={*.500} rate=10 start ruleName jobid={prog*.10*} rate=15 Signed-off-by: Qian Yingjin Change-Id: I0a4c44fd15533dfccd9f6ab7374a0a7a24b1403e Reviewed-on: https://review.whamcloud.com/24523 Tested-by: Jenkins Tested-by: Maloo Reviewed-by: Andreas Dilger Reviewed-by: Wang Shilong --- diff --git a/lustre/include/lustre_nrs_tbf.h b/lustre/include/lustre_nrs_tbf.h index e991909..6e0c736 100644 --- a/lustre/include/lustre_nrs_tbf.h +++ b/lustre/include/lustre_nrs_tbf.h @@ -42,8 +42,12 @@ struct nrs_tbf_head; struct nrs_tbf_cmd; +#define NRS_TBF_MATCH_FULL 0x0000001 +#define NRS_TBF_MATCH_WILDCARD 0x0000002 + struct nrs_tbf_jobid { char *tj_id; + __u32 tj_match_flag; struct list_head tj_linkage; }; diff --git a/lustre/ptlrpc/nrs_tbf.c b/lustre/ptlrpc/nrs_tbf.c index 9fb43e4..d3c6e9e 100644 --- a/lustre/ptlrpc/nrs_tbf.c +++ b/lustre/ptlrpc/nrs_tbf.c @@ -809,9 +809,11 @@ nrs_tbf_jobid_list_free(struct list_head *jobid_list) } static int -nrs_tbf_jobid_list_add(const struct cfs_lstr *id, struct list_head *jobid_list) +nrs_tbf_jobid_list_add(struct cfs_lstr *id, struct list_head *jobid_list) { struct nrs_tbf_jobid *jobid; + struct cfs_lstr res; + int rc; OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid)); if (jobid == NULL) @@ -824,17 +826,62 @@ nrs_tbf_jobid_list_add(const struct cfs_lstr *id, struct list_head *jobid_list) } memcpy(jobid->tj_id, id->ls_str, id->ls_len); + rc = cfs_gettok(id, '*', &res); + if (rc == 0) + jobid->tj_match_flag = NRS_TBF_MATCH_FULL; + else + jobid->tj_match_flag = NRS_TBF_MATCH_WILDCARD; + list_add_tail(&jobid->tj_linkage, jobid_list); return 0; } +static bool +cfs_match_wildcard(const char *pattern, const char *content) +{ + if (*pattern == '\0' && *content == '\0') + return true; + + if (*pattern == '*' && *(pattern + 1) != '\0' && *content == '\0') + return false; + + while (*pattern == *content) { + pattern++; + content++; + if (*pattern == '\0' && *content == '\0') + return true; + + if (*pattern == '*' && *(pattern + 1) != '\0' && + *content == '\0') + return false; + } + + if (*pattern == '*') + return (cfs_match_wildcard(pattern + 1, content) || + cfs_match_wildcard(pattern, content + 1)); + + return false; +} + +static inline bool +nrs_tbf_jobid_match(const struct nrs_tbf_jobid *jobid, const char *id) +{ + if (jobid->tj_match_flag == NRS_TBF_MATCH_FULL) + return strcmp(jobid->tj_id, id) == 0; + + if (jobid->tj_match_flag == NRS_TBF_MATCH_WILDCARD) + return cfs_match_wildcard(jobid->tj_id, id); + + return false; +} + static int nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id) { struct nrs_tbf_jobid *jobid; list_for_each_entry(jobid, jobid_list, tj_linkage) { - if (strcmp(id, jobid->tj_id) == 0) + if (nrs_tbf_jobid_match(jobid, id)) return 1; } return 0;