Whamcloud - gitweb
LU-6142 libcfs: discard cfs_strrstr() 61/40861/2
authorMr NeilBrown <neilb@suse.de>
Tue, 24 Nov 2020 02:45:39 +0000 (13:45 +1100)
committerOleg Drokin <green@whamcloud.com>
Fri, 26 Feb 2021 22:10:58 +0000 (22:10 +0000)
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 <neilb@suse.de>
Change-Id: I52b4abb36cf809d3bd9eebcc752959b0a81bfc13
Reviewed-on: https://review.whamcloud.com/40861
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Alex Zhuravlev <bzzz@whamcloud.com>
Reviewed-by: James Simmons <jsimmons@infradead.org>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
libcfs/include/libcfs/libcfs_string.h
libcfs/libcfs/libcfs_string.c
lustre/osp/lwp_dev.c

index f74a9cd..0b9bd14 100644 (file)
@@ -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);
index 1ba1c24..40f7aaf 100644 (file)
 #include <libcfs/libcfs.h>
 #include <libcfs/libcfs_string.h>
 
-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)
index 188aca0..c26968a 100644 (file)
@@ -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);