Whamcloud - gitweb
LU-9325 obd: replace simple_strtoul() 10/49910/11
authorJames Simmons <jsimmons@infradead.org>
Sat, 29 Apr 2023 15:39:56 +0000 (11:39 -0400)
committerOleg Drokin <green@whamcloud.com>
Fri, 19 May 2023 07:04:49 +0000 (07:04 +0000)
Replace the use of simple_strtoul() in filename_is_volatile() with
sscanf(). This change also strengthens the checking of the format
of the volatile file's name.

Remove the use of simple_strtoul() from target_name2index() as well.

Change-Id: I4bf15af00d6cecdd46eba3cca5df0fbeb473d9ce
Signed-off-by: James Simmons <jsimmons@infradead.org>
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/49910
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Arshad Hussain <arshad.hussain@aeoncomputing.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lustre/include/obd.h
lustre/obdclass/obd_mount.c

index 1961ddb..efda050 100644 (file)
@@ -1364,8 +1364,8 @@ static inline const char *lu_dev_name(const struct lu_device *lu_dev)
 static inline bool filename_is_volatile(const char *name, size_t namelen,
                                        int *idx)
 {
-       const char      *start;
-       char            *end;
+       const char *start;
+       int rnd, fd, rc;
 
        if (strncmp(name, LUSTRE_VOLATILE_HDR, LUSTRE_VOLATILE_HDR_LEN) != 0)
                return false;
@@ -1387,12 +1387,11 @@ static inline bool filename_is_volatile(const char *name, size_t namelen,
        }
        /* we have an idx, read it */
        start = name + LUSTRE_VOLATILE_HDR_LEN + 1;
-       *idx = simple_strtoul(start, &end, 16);
-       /* error cases:
-        * no digit, no trailing :, negative value
+       rc = sscanf(start, "%4x:%4x:fd=%2d", idx, &rnd, &fd);
+       /* error cases: no digit or negative value
+        * rc will never be larger then 3
         */
-       if (((*idx == 0) && (end == start)) ||
-           (*end != ':') || (*idx < 0))
+       if (rc <= 0 || *idx < 0)
                goto bad_format;
 
        return true;
index e692180..e96ca5f 100644 (file)
@@ -870,16 +870,16 @@ EXPORT_SYMBOL(server_name_is_ost);
  * Get the index from the target name MDTXXXX/OSTXXXX
  * rc = server type, or rc < 0  on error
  **/
-int target_name2index(const char *tgtname, __u32 *idx, const char **endptr)
+int target_name2index(const char *tgtname, u32 *idx, const char **endptr)
 {
        const char *dash = tgtname;
-       unsigned long index;
-       int rc;
+       int type, len, rc;
+       u16 index;
 
        if (strncmp(dash, "MDT", 3) == 0)
-               rc = LDD_F_SV_TYPE_MDT;
+               type = LDD_F_SV_TYPE_MDT;
        else if (strncmp(dash, "OST", 3) == 0)
-               rc = LDD_F_SV_TYPE_OST;
+               type = LDD_F_SV_TYPE_OST;
        else
                return -EINVAL;
 
@@ -888,17 +888,34 @@ int target_name2index(const char *tgtname, __u32 *idx, const char **endptr)
        if (strncmp(dash, "all", 3) == 0) {
                if (endptr != NULL)
                        *endptr = dash + 3;
-               return rc | LDD_F_SV_ALL;
+               return type | LDD_F_SV_ALL;
        }
 
-       index = simple_strtoul(dash, (char **)endptr, 16);
-       if (idx != NULL)
+       len = strspn(dash, "0123456789ABCDEFabcdef");
+       if (len > 4)
+               return -ERANGE;
+
+       if (strlen(dash) != len) {
+               char num[5];
+
+               num[4] = '\0';
+               memcpy(num, dash, sizeof(num) - 1);
+               rc = kstrtou16(num, 16, &index);
+               if (rc < 0)
+                       return rc;
+       } else {
+               rc = kstrtou16(dash, 16, &index);
+               if (rc < 0)
+                       return rc;
+       }
+
+       if (idx)
                *idx = index;
 
-       if (index > 0xffff)
-               return -ERANGE;
+       if (endptr)
+               *endptr = dash  + len;
 
-       return rc;
+       return type;
 }
 EXPORT_SYMBOL(target_name2index);