From 3db4d9a69e4256a42a815e32f9f5c26a68e17454 Mon Sep 17 00:00:00 2001 From: Mr NeilBrown Date: Tue, 24 Nov 2020 13:45:39 +1100 Subject: [PATCH] LU-6142 libcfs: discard cfs_strrstr() cfs_strrstr() is only used in one place, and it can easily be open coded there without increasing code complexity. In particular the fact that the "needle" cannot meaningfully be at the start of the "haystack", means a simple loop does all we need. In fact, there is room to improve the code in lwp_setup() - sprintf isn't needed as the result is a constant that can be calculated at compile time - adding the nul termination is then not needed as the buffer being copied to was initialised to zeroes. Signed-off-by: Mr NeilBrown Change-Id: I52b4abb36cf809d3bd9eebcc752959b0a81bfc13 Reviewed-on: https://review.whamcloud.com/40861 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Alex Zhuravlev Reviewed-by: James Simmons Reviewed-by: Oleg Drokin --- libcfs/include/libcfs/libcfs_string.h | 1 - libcfs/libcfs/libcfs_string.c | 27 --------------------------- lustre/osp/lwp_dev.c | 13 ++++++++----- 3 files changed, 8 insertions(+), 33 deletions(-) diff --git a/libcfs/include/libcfs/libcfs_string.h b/libcfs/include/libcfs/libcfs_string.h index f74a9cd..0b9bd14 100644 --- a/libcfs/include/libcfs/libcfs_string.h +++ b/libcfs/include/libcfs/libcfs_string.h @@ -40,7 +40,6 @@ #define __LIBCFS_STRING_H__ /* libcfs_string.c */ -char *cfs_strrstr(const char *haystack, const char *needle); /* Convert a text string to a bitmask */ int cfs_str2mask(const char *str, const char *(*bit2str)(int bit), int *oldmask, int minmask, int allmask); diff --git a/libcfs/libcfs/libcfs_string.c b/libcfs/libcfs/libcfs_string.c index 1ba1c24..40f7aaf 100644 --- a/libcfs/libcfs/libcfs_string.c +++ b/libcfs/libcfs/libcfs_string.c @@ -40,33 +40,6 @@ #include #include -char *cfs_strrstr(const char *haystack, const char *needle) -{ - char *ptr; - - if (unlikely(haystack == NULL || needle == NULL)) - return NULL; - - if (strlen(needle) == 1) - return strrchr(haystack, needle[0]); - - ptr = strstr(haystack, needle); - if (ptr != NULL) { - while (1) { - char *tmp; - - tmp = strstr(&ptr[1], needle); - if (tmp == NULL) - return ptr; - - ptr = tmp; - } - } - - return NULL; -} -EXPORT_SYMBOL(cfs_strrstr); - /* Convert a text string to a bitmask */ int cfs_str2mask(const char *str, const char *(*bit2str)(int bit), int *oldmask, int minmask, int allmask) diff --git a/lustre/osp/lwp_dev.c b/lustre/osp/lwp_dev.c index 188aca0..c26968a 100644 --- a/lustre/osp/lwp_dev.c +++ b/lustre/osp/lwp_dev.c @@ -82,9 +82,11 @@ static int lwp_setup(const struct lu_env *env, struct lwp_device *lwp, char *lwp_name = lwp->lpd_obd->obd_name; char *server_uuid = NULL; char *ptr; + int uuid_len = -1; struct obd_import *imp; int len = strlen(lwp_name) + 1; int rc; + const char *lwp_marker = "-" LUSTRE_LWP_NAME "-"; ENTRY; lwp->lpd_notify_task = NULL; @@ -97,16 +99,17 @@ static int lwp_setup(const struct lu_env *env, struct lwp_device *lwp, if (server_uuid == NULL) GOTO(out, rc = -ENOMEM); - snprintf(server_uuid, len, "-%s-", LUSTRE_LWP_NAME); - ptr = cfs_strrstr(lwp_name, server_uuid); - if (ptr == NULL) { + ptr = lwp_name; + while (ptr && (ptr = strstr(ptr+1, lwp_marker)) != NULL) + uuid_len = ptr - lwp_name; + + if (uuid_len < 0) { CERROR("%s: failed to get server_uuid from lwp_name: rc = %d\n", lwp_name, -EINVAL); GOTO(out, rc = -EINVAL); } - strncpy(server_uuid, lwp_name, ptr - lwp_name); - server_uuid[ptr - lwp_name] = '\0'; + strncpy(server_uuid, lwp_name, uuid_len); strlcat(server_uuid, "_UUID", len); lustre_cfg_bufs_reset(bufs, lwp_name); lustre_cfg_bufs_set_string(bufs, 1, server_uuid); -- 1.8.3.1