These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / staging / lustre / lustre / libcfs / libcfs_string.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * String manipulation functions.
37  *
38  * libcfs/libcfs/libcfs_string.c
39  *
40  * Author: Nathan Rutman <nathan.rutman@sun.com>
41  */
42
43 #include "../../include/linux/libcfs/libcfs.h"
44
45 /* Convert a text string to a bitmask */
46 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
47                  int *oldmask, int minmask, int allmask)
48 {
49         const char *debugstr;
50         char op = '\0';
51         int newmask = minmask, i, len, found = 0;
52
53         /* <str> must be a list of tokens separated by whitespace
54          * and optionally an operator ('+' or '-').  If an operator
55          * appears first in <str>, '*oldmask' is used as the starting point
56          * (relative), otherwise minmask is used (absolute).  An operator
57          * applies to all following tokens up to the next operator. */
58         while (*str != '\0') {
59                 while (isspace(*str))
60                         str++;
61                 if (*str == '\0')
62                         break;
63                 if (*str == '+' || *str == '-') {
64                         op = *str++;
65                         if (!found)
66                                 /* only if first token is relative */
67                                 newmask = *oldmask;
68                         while (isspace(*str))
69                                 str++;
70                         if (*str == '\0')  /* trailing op */
71                                 return -EINVAL;
72                 }
73
74                 /* find token length */
75                 len = 0;
76                 while (str[len] != '\0' && !isspace(str[len]) &&
77                        str[len] != '+' && str[len] != '-')
78                         len++;
79
80                 /* match token */
81                 found = 0;
82                 for (i = 0; i < 32; i++) {
83                         debugstr = bit2str(i);
84                         if (debugstr != NULL &&
85                             strlen(debugstr) == len &&
86                             strncasecmp(str, debugstr, len) == 0) {
87                                 if (op == '-')
88                                         newmask &= ~(1 << i);
89                                 else
90                                         newmask |= (1 << i);
91                                 found = 1;
92                                 break;
93                         }
94                 }
95                 if (!found && len == 3 &&
96                     (strncasecmp(str, "ALL", len) == 0)) {
97                         if (op == '-')
98                                 newmask = minmask;
99                         else
100                                 newmask = allmask;
101                         found = 1;
102                 }
103                 if (!found) {
104                         CWARN("unknown mask '%.*s'.\n"
105                               "mask usage: [+|-]<all|type> ...\n", len, str);
106                         return -EINVAL;
107                 }
108                 str += len;
109         }
110
111         *oldmask = newmask;
112         return 0;
113 }
114
115 /* get the first string out of @str */
116 char *cfs_firststr(char *str, size_t size)
117 {
118         size_t i = 0;
119         char  *end;
120
121         /* trim leading spaces */
122         while (i < size && *str && isspace(*str)) {
123                 ++i;
124                 ++str;
125         }
126
127         /* string with all spaces */
128         if (*str == '\0')
129                 goto out;
130
131         end = str;
132         while (i < size && *end != '\0' && !isspace(*end)) {
133                 ++i;
134                 ++end;
135         }
136
137         *end = '\0';
138 out:
139         return str;
140 }
141 EXPORT_SYMBOL(cfs_firststr);
142
143 char *
144 cfs_trimwhite(char *str)
145 {
146         char *end;
147
148         while (isspace(*str))
149                 str++;
150
151         end = str + strlen(str);
152         while (end > str) {
153                 if (!isspace(end[-1]))
154                         break;
155                 end--;
156         }
157
158         *end = 0;
159         return str;
160 }
161 EXPORT_SYMBOL(cfs_trimwhite);
162
163 /**
164  * Extracts tokens from strings.
165  *
166  * Looks for \a delim in string \a next, sets \a res to point to
167  * substring before the delimiter, sets \a next right after the found
168  * delimiter.
169  *
170  * \retval 1 if \a res points to a string of non-whitespace characters
171  * \retval 0 otherwise
172  */
173 int
174 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
175 {
176         char *end;
177
178         if (next->ls_str == NULL)
179                 return 0;
180
181         /* skip leading white spaces */
182         while (next->ls_len) {
183                 if (!isspace(*next->ls_str))
184                         break;
185                 next->ls_str++;
186                 next->ls_len--;
187         }
188
189         if (next->ls_len == 0) /* whitespaces only */
190                 return 0;
191
192         if (*next->ls_str == delim) {
193                 /* first non-writespace is the delimiter */
194                 return 0;
195         }
196
197         res->ls_str = next->ls_str;
198         end = memchr(next->ls_str, delim, next->ls_len);
199         if (end == NULL) {
200                 /* there is no the delimeter in the string */
201                 end = next->ls_str + next->ls_len;
202                 next->ls_str = NULL;
203         } else {
204                 next->ls_str = end + 1;
205                 next->ls_len -= (end - res->ls_str + 1);
206         }
207
208         /* skip ending whitespaces */
209         while (--end != res->ls_str) {
210                 if (!isspace(*end))
211                         break;
212         }
213
214         res->ls_len = end - res->ls_str + 1;
215         return 1;
216 }
217 EXPORT_SYMBOL(cfs_gettok);
218
219 /**
220  * Converts string to integer.
221  *
222  * Accepts decimal and hexadecimal number recordings.
223  *
224  * \retval 1 if first \a nob chars of \a str convert to decimal or
225  * hexadecimal integer in the range [\a min, \a max]
226  * \retval 0 otherwise
227  */
228 int
229 cfs_str2num_check(char *str, int nob, unsigned *num,
230                   unsigned min, unsigned max)
231 {
232         char    *endp;
233
234         str = cfs_trimwhite(str);
235         *num = simple_strtoul(str, &endp, 0);
236         if (endp == str)
237                 return 0;
238
239         for (; endp < str + nob; endp++) {
240                 if (!isspace(*endp))
241                         return 0;
242         }
243
244         return (*num >= min && *num <= max);
245 }
246 EXPORT_SYMBOL(cfs_str2num_check);
247
248 /**
249  * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
250  * \a src should only have a single token which can be \<number\> or  \*
251  *
252  * \retval pointer to allocated range_expr and initialized
253  * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
254  `* src parses to
255  * \<number\> |
256  * \<number\> '-' \<number\> |
257  * \<number\> '-' \<number\> '/' \<number\>
258  * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
259  * -ENOMEM will be returned.
260  */
261 static int
262 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
263                      int bracketed, struct cfs_range_expr **expr)
264 {
265         struct cfs_range_expr   *re;
266         struct cfs_lstr         tok;
267
268         LIBCFS_ALLOC(re, sizeof(*re));
269         if (re == NULL)
270                 return -ENOMEM;
271
272         if (src->ls_len == 1 && src->ls_str[0] == '*') {
273                 re->re_lo = min;
274                 re->re_hi = max;
275                 re->re_stride = 1;
276                 goto out;
277         }
278
279         if (cfs_str2num_check(src->ls_str, src->ls_len,
280                               &re->re_lo, min, max)) {
281                 /* <number> is parsed */
282                 re->re_hi = re->re_lo;
283                 re->re_stride = 1;
284                 goto out;
285         }
286
287         if (!bracketed || !cfs_gettok(src, '-', &tok))
288                 goto failed;
289
290         if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
291                                &re->re_lo, min, max))
292                 goto failed;
293
294         /* <number> - */
295         if (cfs_str2num_check(src->ls_str, src->ls_len,
296                               &re->re_hi, min, max)) {
297                 /* <number> - <number> is parsed */
298                 re->re_stride = 1;
299                 goto out;
300         }
301
302         /* go to check <number> '-' <number> '/' <number> */
303         if (cfs_gettok(src, '/', &tok)) {
304                 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
305                                        &re->re_hi, min, max))
306                         goto failed;
307
308                 /* <number> - <number> / ... */
309                 if (cfs_str2num_check(src->ls_str, src->ls_len,
310                                       &re->re_stride, min, max)) {
311                         /* <number> - <number> / <number> is parsed */
312                         goto out;
313                 }
314         }
315
316  out:
317         *expr = re;
318         return 0;
319
320  failed:
321         LIBCFS_FREE(re, sizeof(*re));
322         return -EINVAL;
323 }
324
325 /**
326  * Print the range expression \a re into specified \a buffer.
327  * If \a bracketed is true, expression does not need additional
328  * brackets.
329  *
330  * \retval number of characters written
331  */
332 static int
333 cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
334                      bool bracketed)
335 {
336         int i;
337         char s[] = "[";
338         char e[] = "]";
339
340         if (bracketed)
341                 s[0] = e[0] = '\0';
342
343         if (expr->re_lo == expr->re_hi)
344                 i = scnprintf(buffer, count, "%u", expr->re_lo);
345         else if (expr->re_stride == 1)
346                 i = scnprintf(buffer, count, "%s%u-%u%s",
347                                 s, expr->re_lo, expr->re_hi, e);
348         else
349                 i = scnprintf(buffer, count, "%s%u-%u/%u%s",
350                                 s, expr->re_lo, expr->re_hi,
351                                 expr->re_stride, e);
352         return i;
353 }
354
355 /**
356  * Print a list of range expressions (\a expr_list) into specified \a buffer.
357  * If the list contains several expressions, separate them with comma
358  * and surround the list with brackets.
359  *
360  * \retval number of characters written
361  */
362 int
363 cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
364 {
365         struct cfs_range_expr *expr;
366         int i = 0, j = 0;
367         int numexprs = 0;
368
369         if (count <= 0)
370                 return 0;
371
372         list_for_each_entry(expr, &expr_list->el_exprs, re_link)
373                 numexprs++;
374
375         if (numexprs > 1)
376                 i += scnprintf(buffer + i, count - i, "[");
377
378         list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
379                 if (j++ != 0)
380                         i += scnprintf(buffer + i, count - i, ",");
381                 i += cfs_range_expr_print(buffer + i, count - i, expr,
382                                           numexprs > 1);
383         }
384
385         if (numexprs > 1)
386                 i += scnprintf(buffer + i, count - i, "]");
387
388         return i;
389 }
390 EXPORT_SYMBOL(cfs_expr_list_print);
391
392 /**
393  * Matches value (\a value) against ranges expression list \a expr_list.
394  *
395  * \retval 1 if \a value matches
396  * \retval 0 otherwise
397  */
398 int
399 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
400 {
401         struct cfs_range_expr   *expr;
402
403         list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
404                 if (value >= expr->re_lo && value <= expr->re_hi &&
405                     ((value - expr->re_lo) % expr->re_stride) == 0)
406                         return 1;
407         }
408
409         return 0;
410 }
411 EXPORT_SYMBOL(cfs_expr_list_match);
412
413 /**
414  * Convert express list (\a expr_list) to an array of all matched values
415  *
416  * \retval N N is total number of all matched values
417  * \retval 0 if expression list is empty
418  * \retval < 0 for failure
419  */
420 int
421 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
422 {
423         struct cfs_range_expr   *expr;
424         __u32                   *val;
425         int                     count = 0;
426         int                     i;
427
428         list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
429                 for (i = expr->re_lo; i <= expr->re_hi; i++) {
430                         if (((i - expr->re_lo) % expr->re_stride) == 0)
431                                 count++;
432                 }
433         }
434
435         if (count == 0) /* empty expression list */
436                 return 0;
437
438         if (count > max) {
439                 CERROR("Number of values %d exceeds max allowed %d\n",
440                        max, count);
441                 return -EINVAL;
442         }
443
444         LIBCFS_ALLOC(val, sizeof(val[0]) * count);
445         if (val == NULL)
446                 return -ENOMEM;
447
448         count = 0;
449         list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
450                 for (i = expr->re_lo; i <= expr->re_hi; i++) {
451                         if (((i - expr->re_lo) % expr->re_stride) == 0)
452                                 val[count++] = i;
453                 }
454         }
455
456         *valpp = val;
457         return count;
458 }
459 EXPORT_SYMBOL(cfs_expr_list_values);
460
461 /**
462  * Frees cfs_range_expr structures of \a expr_list.
463  *
464  * \retval none
465  */
466 void
467 cfs_expr_list_free(struct cfs_expr_list *expr_list)
468 {
469         while (!list_empty(&expr_list->el_exprs)) {
470                 struct cfs_range_expr *expr;
471
472                 expr = list_entry(expr_list->el_exprs.next,
473                                       struct cfs_range_expr, re_link);
474                 list_del(&expr->re_link);
475                 LIBCFS_FREE(expr, sizeof(*expr));
476         }
477
478         LIBCFS_FREE(expr_list, sizeof(*expr_list));
479 }
480 EXPORT_SYMBOL(cfs_expr_list_free);
481
482 /**
483  * Parses \<cfs_expr_list\> token of the syntax.
484  *
485  * \retval 0 if \a str parses to \<number\> | \<expr_list\>
486  * \retval -errno otherwise
487  */
488 int
489 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
490                     struct cfs_expr_list **elpp)
491 {
492         struct cfs_expr_list    *expr_list;
493         struct cfs_range_expr   *expr;
494         struct cfs_lstr         src;
495         int                     rc;
496
497         LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
498         if (expr_list == NULL)
499                 return -ENOMEM;
500
501         src.ls_str = str;
502         src.ls_len = len;
503
504         INIT_LIST_HEAD(&expr_list->el_exprs);
505
506         if (src.ls_str[0] == '[' &&
507             src.ls_str[src.ls_len - 1] == ']') {
508                 src.ls_str++;
509                 src.ls_len -= 2;
510
511                 rc = -EINVAL;
512                 while (src.ls_str != NULL) {
513                         struct cfs_lstr tok;
514
515                         if (!cfs_gettok(&src, ',', &tok)) {
516                                 rc = -EINVAL;
517                                 break;
518                         }
519
520                         rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
521                         if (rc != 0)
522                                 break;
523
524                         list_add_tail(&expr->re_link,
525                                           &expr_list->el_exprs);
526                 }
527         } else {
528                 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
529                 if (rc == 0) {
530                         list_add_tail(&expr->re_link,
531                                           &expr_list->el_exprs);
532                 }
533         }
534
535         if (rc != 0)
536                 cfs_expr_list_free(expr_list);
537         else
538                 *elpp = expr_list;
539
540         return rc;
541 }
542 EXPORT_SYMBOL(cfs_expr_list_parse);
543
544 /**
545  * Frees cfs_expr_list structures of \a list.
546  *
547  * For each struct cfs_expr_list structure found on \a list it frees
548  * range_expr list attached to it and frees the cfs_expr_list itself.
549  *
550  * \retval none
551  */
552 void
553 cfs_expr_list_free_list(struct list_head *list)
554 {
555         struct cfs_expr_list *el;
556
557         while (!list_empty(list)) {
558                 el = list_entry(list->next,
559                                     struct cfs_expr_list, el_link);
560                 list_del(&el->el_link);
561                 cfs_expr_list_free(el);
562         }
563 }
564 EXPORT_SYMBOL(cfs_expr_list_free_list);