Whamcloud - gitweb
LU-13055 libcfs: allow comma-separated masks
[fs/lustre-release.git] / libcfs / libcfs / libcfs_string.c
index 2eb8efa..5549417 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2012, 2015, Intel Corporation.
+ * Copyright (c) 2012, 2017, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
- * Lustre is a trademark of Sun Microsystems, Inc.
  *
  * String manipulation functions.
  *
  * Author: Nathan Rutman <nathan.rutman@sun.com>
  */
 
+#include <linux/ctype.h>
 #include <libcfs/libcfs.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);
+#include <libcfs/libcfs_string.h>
 
 /* Convert a text string to a bitmask */
 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
-                 int *oldmask, int minmask, int allmask)
+                int *oldmask, int minmask, int allmask)
 {
-        const char *debugstr;
-        char op = 0;
-        int newmask = minmask, i, len, found = 0;
-        ENTRY;
-
-        /* <str> must be a list of tokens separated by whitespace
-         * and optionally an operator ('+' or '-').  If an operator
-         * appears first in <str>, '*oldmask' is used as the starting point
-         * (relative), otherwise minmask is used (absolute).  An operator
-         * applies to all following tokens up to the next operator. */
-        while (*str != 0) {
-                while (isspace(*str))
-                        str++;
-                if (*str == 0)
-                        break;
-                if (*str == '+' || *str == '-') {
-                        op = *str++;
-                        if (!found)
-                                /* only if first token is relative */
-                                newmask = *oldmask;
-                        while (isspace(*str))
-                                str++;
-                        if (*str == 0)          /* trailing op */
-                                return -EINVAL;
-                }
-
-                /* find token length */
-                for (len = 0; str[len] != 0 && !isspace(str[len]) &&
-                      str[len] != '+' && str[len] != '-'; len++);
-
-                /* match token */
-                found = 0;
-                for (i = 0; i < 32; i++) {
-                        debugstr = bit2str(i);
-                        if (debugstr != NULL &&
-                            strlen(debugstr) == len &&
+       const char *debugstr;
+       char op = 0;
+       int newmask = minmask, i, len, found = 0;
+
+       ENTRY;
+       /* <str> must be a list of tokens separated by whitespace or comma,
+        * and optionally an operator ('+' or '-').  If an operator
+        * appears first in <str>, '*oldmask' is used as the starting point
+        * (relative), otherwise minmask is used (absolute).  An operator
+        * applies to all following tokens up to the next operator.
+        */
+       while (*str != 0) {
+               while (isspace(*str) || *str == ',')
+                       str++;
+               if (*str == 0)
+                       break;
+               if (*str == '+' || *str == '-') {
+                       op = *str++;
+                       if (!found)
+                               /* only if first token is relative */
+                               newmask = *oldmask;
+                       while (isspace(*str))
+                               str++;
+                       if (*str == 0)          /* trailing op */
+                               return -EINVAL;
+               }
+
+               /* find token length */
+               for (len = 0; str[len] != 0 && !isspace(str[len]) &&
+                       str[len] != '+' && str[len] != '-' && str[len] != ',';
+                    len++);
+
+               /* match token */
+               found = 0;
+               for (i = 0; i < 32; i++) {
+                       debugstr = bit2str(i);
+                       if (debugstr != NULL &&
+                           strlen(debugstr) == len &&
                            strncasecmp(str, debugstr, len) == 0) {
-                                if (op == '-')
-                                        newmask &= ~(1 << i);
-                                else
-                                        newmask |= (1 << i);
-                                found = 1;
-                                break;
-                        }
-                }
-                if (!found && len == 3 &&
+                               if (op == '-')
+                                       newmask &= ~BIT(i);
+                               else
+                                       newmask |= BIT(i);
+                               found = 1;
+                               break;
+                       }
+               }
+               if (!found && len == 3 &&
                    (strncasecmp(str, "ALL", len) == 0)) {
-                        if (op == '-')
-                                newmask = minmask;
-                        else
-                                newmask = allmask;
-                        found = 1;
-                }
-                if (!found) {
-                        CWARN("unknown mask '%.*s'.\n"
-                              "mask usage: [+|-]<all|type> ...\n", len, str);
-                        return -EINVAL;
-                }
-                str += len;
-        }
-
-        *oldmask = newmask;
-        return 0;
-}
-EXPORT_SYMBOL(cfs_str2mask);
-
-/* get the first string out of @str */
-char *cfs_firststr(char *str, size_t size)
-{
-        size_t i = 0;
-        char  *end;
-
-        /* trim leading spaces */
-        while (i < size && *str && isspace(*str)) {
-                ++i;
-                ++str;
-        }
-
-        /* string with all spaces */
-        if (*str == '\0')
-                goto out;
-
-        end = str;
-        while (i < size && *end != '\0' && !isspace(*end)) {
-                ++i;
-                ++end;
-        }
-
-        *end= '\0';
-out:
-        return str;
-}
-EXPORT_SYMBOL(cfs_firststr);
-
-char *
-cfs_trimwhite(char *str)
-{
-       char *end;
-
-       while (isspace(*str))
-               str++;
-
-       end = str + strlen(str);
-       while (end > str) {
-               if (!isspace(end[-1]))
-                       break;
-               end--;
+                       if (op == '-')
+                               newmask = minmask;
+                       else
+                               newmask = allmask;
+                       found = 1;
+               }
+               if (!found) {
+                       CWARN("unknown mask '%.*s'.\n"
+                             "mask usage: [+|-]<all|type> ...\n", len, str);
+                       return -EINVAL;
+               }
+               str += len;
        }
 
-       *end = 0;
-       return str;
+       *oldmask = newmask;
+       return 0;
 }
-EXPORT_SYMBOL(cfs_trimwhite);
+EXPORT_SYMBOL(cfs_str2mask);
 
 /**
  * Extracts tokens from strings.
@@ -256,17 +180,47 @@ int
 cfs_str2num_check(char *str, int nob, unsigned *num,
                  unsigned min, unsigned max)
 {
-       char    *endp;
-
-       *num = simple_strtoul(str, &endp, 0);
-       if (endp == str)
-               return 0;
+       bool all_numbers = true;
+       char *endp, cache;
+       int len;
+       int rc;
+
+       endp = strim(str);
+       /**
+        * kstrouint can only handle strings composed
+        * of only numbers. We need to scan the string
+        * passed in for the first non-digit character
+        * and end the string at that location. If we
+        * don't find any non-digit character we still
+        * need to place a '\0' at position len since
+        * we are not interested in the rest of the
+        * string which is longer than len in size.
+        * After we are done the character at the
+        * position we placed '\0' must be restored.
+        */
+       len = min((int)strlen(endp), nob);
+       for (; endp < str + len; endp++) {
+               if (!isxdigit(*endp) && *endp != '-' &&
+                   *endp != '+') {
+                       all_numbers = false;
+                       break;
+               }
+       }
 
-       for (; endp < str + nob; endp++) {
-               if (!isspace(*endp))
-                       return 0;
+       /* Eat trailing space */
+       if (!all_numbers && isspace(*endp)) {
+               all_numbers = true;
+               endp--;
        }
 
+       cache = *endp;
+       *endp = '\0';
+
+       rc = kstrtouint(str, 0, num);
+       *endp = cache;
+       if (rc || !all_numbers)
+               return 0;
+
        return (*num >= min && *num <= max);
 }
 EXPORT_SYMBOL(cfs_str2num_check);
@@ -288,8 +242,8 @@ static int
 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
                     int bracketed, struct cfs_range_expr **expr)
 {
-       struct cfs_range_expr   *re;
-       struct cfs_lstr         tok;
+       struct cfs_range_expr *re;
+       struct cfs_lstr tok;
 
        LIBCFS_ALLOC(re, sizeof(*re));
        if (re == NULL)
@@ -339,11 +293,11 @@ cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
                }
        }
 
- out:
+out:
        *expr = re;
        return 0;
 
- failed:
+failed:
        LIBCFS_FREE(re, sizeof(*re));
        return -EINVAL;
 }
@@ -370,11 +324,11 @@ cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
                i = scnprintf(buffer, count, "%u", expr->re_lo);
        else if (expr->re_stride == 1)
                i = scnprintf(buffer, count, "%s%u-%u%s",
-                                 s, expr->re_lo, expr->re_hi, e);
+                             s, expr->re_lo, expr->re_hi, e);
        else
                i = scnprintf(buffer, count, "%s%u-%u/%u%s",
-                                 s, expr->re_lo, expr->re_hi,
-                                 expr->re_stride, e);
+                             s, expr->re_lo, expr->re_hi,
+                             expr->re_stride, e);
        return i;
 }
 
@@ -424,7 +378,7 @@ EXPORT_SYMBOL(cfs_expr_list_print);
 int
 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
 {
-       struct cfs_range_expr   *expr;
+       struct cfs_range_expr *expr;
 
        list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
                if (value >= expr->re_lo && value <= expr->re_hi &&
@@ -446,10 +400,10 @@ EXPORT_SYMBOL(cfs_expr_list_match);
 int
 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
 {
-       struct cfs_range_expr   *expr;
-       __u32                   *val;
-       int                     count = 0;
-       int                     i;
+       struct cfs_range_expr *expr;
+       __u32 *val;
+       int count = 0;
+       int i;
 
        list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
                for (i = expr->re_lo; i <= expr->re_hi; i++) {
@@ -467,7 +421,7 @@ cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
                return -EINVAL;
        }
 
-       LIBCFS_ALLOC(val, sizeof(val[0]) * count);
+       CFS_ALLOC_PTR_ARRAY(val, count);
        if (val == NULL)
                return -ENOMEM;
 
@@ -484,6 +438,16 @@ cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
 }
 EXPORT_SYMBOL(cfs_expr_list_values);
 
+void
+cfs_expr_list_values_free(__u32 *values, int num)
+{
+       /* This array is allocated by LIBCFS_ALLOC(), so it shouldn't be freed
+        * by OBD_FREE() if it's called by module other than libcfs & LNet,
+        * otherwise we will see fake memory leak */
+       CFS_FREE_PTR_ARRAY(values, num);
+}
+EXPORT_SYMBOL(cfs_expr_list_values_free);
+
 /**
  * Frees cfs_range_expr structures of \a expr_list.
  *
@@ -515,10 +479,10 @@ int
 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
                    struct cfs_expr_list **elpp)
 {
-       struct cfs_expr_list    *expr_list;
-       struct cfs_range_expr   *expr;
-       struct cfs_lstr         src;
-       int                     rc;
+       struct cfs_expr_list *expr_list;
+       struct cfs_range_expr *expr;
+       struct cfs_lstr src;
+       int rc;
 
        LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
        if (expr_list == NULL)
@@ -547,15 +511,12 @@ cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
                        if (rc != 0)
                                break;
 
-                       list_add_tail(&expr->re_link,
-                                         &expr_list->el_exprs);
+                       list_add_tail(&expr->re_link, &expr_list->el_exprs);
                }
        } else {
                rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
-               if (rc == 0) {
-                       list_add_tail(&expr->re_link,
-                                         &expr_list->el_exprs);
-               }
+               if (rc == 0)
+                       list_add_tail(&expr->re_link, &expr_list->el_exprs);
        }
 
        if (rc != 0)