From 7955578d771ceb1ec01388f9daaa51b9ea03d7bb Mon Sep 17 00:00:00 2001 From: Qian Yingjin Date: Mon, 2 Sep 2024 16:48:12 +0800 Subject: [PATCH] LU-18192 nrs: move nid/uid/gid/opcode/jobid into generic key A more generic data struct ("nrs_tbf_key") is defined to store the nid/uid/gid/opcode/jobid information of a TBF bucket (data struct nrs_tbc_client). Remove the old tc_key for the generic NRS TBF type. And replace with this new data struct (nrs_tbf_key) instead. Signed-off-by: Qian Yingjin Change-Id: I0f62f02eb358d1ef697bbacd2e8225956deff0ec Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/56227 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Andreas Dilger Reviewed-by: Etienne AUJAMES Reviewed-by: James Simmons Reviewed-by: Oleg Drokin --- lustre/include/lustre_nrs_tbf.h | 42 +++++++++++++-------------- lustre/ptlrpc/nrs_tbf.c | 63 ++++++++++++++++++----------------------- 2 files changed, 49 insertions(+), 56 deletions(-) diff --git a/lustre/include/lustre_nrs_tbf.h b/lustre/include/lustre_nrs_tbf.h index d0535f8..3858c72 100644 --- a/lustre/include/lustre_nrs_tbf.h +++ b/lustre/include/lustre_nrs_tbf.h @@ -34,18 +34,14 @@ struct nrs_tbf_jobid { struct list_head tj_linkage; }; -#define MAX_U32_STR_LEN 10 -#define NRS_TBF_KEY_LEN (LNET_NIDSTR_SIZE + LUSTRE_JOBID_SIZE + \ - MAX_U32_STR_LEN + MAX_U32_STR_LEN + 3 + 2) - enum nrs_tbf_flag { - NRS_TBF_FLAG_INVALID = 0x0000000, - NRS_TBF_FLAG_JOBID = 0x0000001, - NRS_TBF_FLAG_NID = 0x0000002, - NRS_TBF_FLAG_OPCODE = 0x0000004, - NRS_TBF_FLAG_GENERIC = 0x0000008, - NRS_TBF_FLAG_UID = 0x0000010, - NRS_TBF_FLAG_GID = 0x0000020, + NRS_TBF_FLAG_INVALID = 0x0000000, + NRS_TBF_FLAG_JOBID = 0x0000001, + NRS_TBF_FLAG_NID = 0x0000002, + NRS_TBF_FLAG_OPCODE = 0x0000004, + NRS_TBF_FLAG_UID = 0x0000008, + NRS_TBF_FLAG_GID = 0x0000010, + NRS_TBF_FLAG_GENERIC = 0x0000020, }; struct tbf_id { @@ -59,22 +55,21 @@ struct nrs_tbf_id { struct list_head nti_linkage; }; +struct nrs_tbf_key { + struct lnet_nid tk_nid; + __u32 tk_opcode; + struct tbf_id tk_id; /* UID and GID */ + char tk_jobid[LUSTRE_JOBID_SIZE]; +}; + struct nrs_tbf_client { /** Resource object for policy instance. */ struct ptlrpc_nrs_resource tc_res; /** Node in the hash table. */ struct rhash_head tc_rhash; struct hlist_node tc_hnode; - /** NID of the client. */ - struct lnet_nid tc_nid; - /** Jobid of the client. */ - char tc_jobid[LUSTRE_JOBID_SIZE]; - /** opcode of the client. */ - __u32 tc_opcode; - /** gid or uid of the client. */ - struct tbf_id tc_id; - /** Hash key of the client. */ - char tc_key[NRS_TBF_KEY_LEN]; + /** Key of the TBF cli. */ + struct nrs_tbf_key tc_key; /** Reference number of the client. */ refcount_t tc_ref; /** Lock to protect rule and linkage. */ @@ -121,6 +116,11 @@ struct nrs_tbf_client { struct rcu_head tc_rcu_head; }; +#define tc_nid tc_key.tk_nid +#define tc_opcode tc_key.tk_opcode +#define tc_id tc_key.tk_id +#define tc_jobid tc_key.tk_jobid + #define MAX_TBF_NAME (16) enum nrs_rule_flags { diff --git a/lustre/ptlrpc/nrs_tbf.c b/lustre/ptlrpc/nrs_tbf.c index aba3be4..b24d7e4 100644 --- a/lustre/ptlrpc/nrs_tbf.c +++ b/lustre/ptlrpc/nrs_tbf.c @@ -1290,16 +1290,21 @@ static unsigned int nrs_tbf_hop_hash(struct cfs_hash *hs, const void *key, const unsigned int bits) { - return cfs_hash_djb2_hash(key, strlen(key), bits); + return cfs_hash_djb2_hash(key, sizeof(struct nrs_tbf_key), bits); } -static int nrs_tbf_hop_keycmp(const void *key, struct hlist_node *hnode) +static int nrs_tbf_hop_keycmp(const void *data, struct hlist_node *hnode) { + struct nrs_tbf_key *key = (struct nrs_tbf_key *)data; struct nrs_tbf_client *cli = hlist_entry(hnode, struct nrs_tbf_client, tc_hnode); - return (strcmp(cli->tc_key, key) == 0); + return nid_same(&cli->tc_nid, &key->tk_nid) && + cli->tc_opcode == key->tk_opcode && + cli->tc_id.ti_uid == key->tk_id.ti_uid && + cli->tc_id.ti_gid == key->tk_id.ti_gid && + strcmp(cli->tc_jobid, key->tk_jobid) == 0; } static void *nrs_tbf_hop_key(struct hlist_node *hnode) @@ -1307,7 +1312,7 @@ static void *nrs_tbf_hop_key(struct hlist_node *hnode) struct nrs_tbf_client *cli = hlist_entry(hnode, struct nrs_tbf_client, tc_hnode); - return cli->tc_key; + return &cli->tc_key; } static void nrs_tbf_hop_get(struct cfs_hash *hs, struct hlist_node *hnode) @@ -1397,12 +1402,12 @@ nrs_tbf_startup(struct ptlrpc_nrs_policy *policy, struct nrs_tbf_head *head) static struct nrs_tbf_client * nrs_tbf_cli_hash_lookup(struct cfs_hash *hs, struct cfs_hash_bd *bd, - const char *key) + struct nrs_tbf_key *key) { struct hlist_node *hnode; struct nrs_tbf_client *cli; - hnode = cfs_hash_bd_lookup_locked(hs, bd, (void *)key); + hnode = cfs_hash_bd_lookup_locked(hs, bd, key); if (hnode == NULL) return NULL; @@ -1642,31 +1647,19 @@ static int nrs_tbf_id_cli_set(struct ptlrpc_request *req, struct tbf_id *id, return rc; } -static inline void nrs_tbf_cli_gen_key(struct nrs_tbf_client *cli, - struct ptlrpc_request *req, - char *keystr, size_t keystr_sz) +static inline void nrs_tbf_cli_gen_key(struct ptlrpc_request *req, + struct nrs_tbf_key *key) { const char *jobid; - u32 opc = lustre_msg_get_opc(req->rq_reqmsg); - struct tbf_id id; - nrs_tbf_id_cli_set(req, &id, NRS_TBF_FLAG_UID | NRS_TBF_FLAG_GID); + key->tk_nid = req->rq_peer.nid; + key->tk_opcode = lustre_msg_get_opc(req->rq_reqmsg); + nrs_tbf_id_cli_set(req, &key->tk_id, NRS_TBF_FLAG_UID | NRS_TBF_FLAG_GID); + jobid = lustre_msg_get_jobid(req->rq_reqmsg); if (jobid == NULL) jobid = NRS_TBF_JOBID_NULL; - - snprintf(keystr, keystr_sz, "%s_%s_%d_%u_%u", jobid, - libcfs_nidstr(&req->rq_peer.nid), opc, id.ti_uid, - id.ti_gid); - - if (cli) { - INIT_LIST_HEAD(&cli->tc_lru); - strscpy(cli->tc_key, keystr, sizeof(cli->tc_key)); - strscpy(cli->tc_jobid, jobid, sizeof(cli->tc_jobid)); - cli->tc_nid = req->rq_peer.nid; - cli->tc_opcode = opc; - cli->tc_id = id; - } + strscpy(key->tk_jobid, jobid, sizeof(key->tk_jobid)); } static struct nrs_tbf_client * @@ -1675,11 +1668,12 @@ nrs_tbf_cli_find(struct nrs_tbf_head *head, struct ptlrpc_request *req) struct nrs_tbf_client *cli; struct cfs_hash *hs = head->th_cli_hash; struct cfs_hash_bd bd; - char keystr[NRS_TBF_KEY_LEN]; + struct nrs_tbf_key key; - nrs_tbf_cli_gen_key(NULL, req, keystr, sizeof(keystr)); - cfs_hash_bd_get_and_lock(hs, (void *)keystr, &bd, 1); - cli = nrs_tbf_cli_hash_lookup(hs, &bd, keystr); + memset(&key, 0, sizeof(key)); + nrs_tbf_cli_gen_key(req, &key); + cfs_hash_bd_get_and_lock(hs, &key, &bd, 1); + cli = nrs_tbf_cli_hash_lookup(hs, &bd, &key); cfs_hash_bd_unlock(hs, &bd, 1); return cli; @@ -1689,13 +1683,13 @@ static struct nrs_tbf_client * nrs_tbf_cli_findadd(struct nrs_tbf_head *head, struct nrs_tbf_client *cli) { - const char *key; + struct nrs_tbf_key *key; struct nrs_tbf_client *ret; struct cfs_hash *hs = head->th_cli_hash; struct cfs_hash_bd bd; - key = cli->tc_key; - cfs_hash_bd_get_and_lock(hs, (void *)key, &bd, 1); + key = &cli->tc_key; + cfs_hash_bd_get_and_lock(hs, key, &bd, 1); ret = nrs_tbf_cli_hash_lookup(hs, &bd, key); if (ret == NULL) { cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode); @@ -1749,9 +1743,8 @@ static void nrs_tbf_generic_cli_init(struct nrs_tbf_client *cli, struct ptlrpc_request *req) { - char keystr[NRS_TBF_KEY_LEN]; - - nrs_tbf_cli_gen_key(cli, req, keystr, sizeof(keystr)); + nrs_tbf_cli_gen_key(req, &cli->tc_key); + INIT_LIST_HEAD(&cli->tc_lru); } static void -- 1.8.3.1