]> Pileus Git - ~andy/linux/blob - drivers/staging/lustre/lustre/libcfs/libcfs_string.c
Merge branch 'timers/core' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
[~andy/linux] / 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 <linux/libcfs/libcfs.h>
44
45 /* non-0 = don't match */
46 int cfs_strncasecmp(const char *s1, const char *s2, size_t n)
47 {
48         if (s1 == NULL || s2 == NULL)
49                 return 1;
50
51         if (n == 0)
52                 return 0;
53
54         while (n-- != 0 && tolower(*s1) == tolower(*s2)) {
55                 if (n == 0 || *s1 == '\0' || *s2 == '\0')
56                         break;
57                 s1++;
58                 s2++;
59         }
60
61         return tolower(*(unsigned char *)s1) - tolower(*(unsigned char *)s2);
62 }
63 EXPORT_SYMBOL(cfs_strncasecmp);
64
65 /* Convert a text string to a bitmask */
66 int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
67                  int *oldmask, int minmask, int allmask)
68 {
69         const char *debugstr;
70         char op = 0;
71         int newmask = minmask, i, len, found = 0;
72
73         /* <str> must be a list of tokens separated by whitespace
74          * and optionally an operator ('+' or '-').  If an operator
75          * appears first in <str>, '*oldmask' is used as the starting point
76          * (relative), otherwise minmask is used (absolute).  An operator
77          * applies to all following tokens up to the next operator. */
78         while (*str != 0) {
79                 while (isspace(*str))
80                         str++;
81                 if (*str == 0)
82                         break;
83                 if (*str == '+' || *str == '-') {
84                         op = *str++;
85                         if (!found)
86                                 /* only if first token is relative */
87                                 newmask = *oldmask;
88                         while (isspace(*str))
89                                 str++;
90                         if (*str == 0)    /* trailing op */
91                                 return -EINVAL;
92                 }
93
94                 /* find token length */
95                 for (len = 0; str[len] != 0 && !isspace(str[len]) &&
96                       str[len] != '+' && str[len] != '-'; len++);
97
98                 /* match token */
99                 found = 0;
100                 for (i = 0; i < 32; i++) {
101                         debugstr = bit2str(i);
102                         if (debugstr != NULL &&
103                             strlen(debugstr) == len &&
104                             cfs_strncasecmp(str, debugstr, len) == 0) {
105                                 if (op == '-')
106                                         newmask &= ~(1 << i);
107                                 else
108                                         newmask |= (1 << i);
109                                 found = 1;
110                                 break;
111                         }
112                 }
113                 if (!found && len == 3 &&
114                     (cfs_strncasecmp(str, "ALL", len) == 0)) {
115                         if (op == '-')
116                                 newmask = minmask;
117                         else
118                                 newmask = allmask;
119                         found = 1;
120                 }
121                 if (!found) {
122                         CWARN("unknown mask '%.*s'.\n"
123                               "mask usage: [+|-]<all|type> ...\n", len, str);
124                         return -EINVAL;
125                 }
126                 str += len;
127         }
128
129         *oldmask = newmask;
130         return 0;
131 }
132 EXPORT_SYMBOL(cfs_str2mask);
133
134 /* get the first string out of @str */
135 char *cfs_firststr(char *str, size_t size)
136 {
137         size_t i = 0;
138         char  *end;
139
140         /* trim leading spaces */
141         while (i < size && *str && isspace(*str)) {
142                 ++i;
143                 ++str;
144         }
145
146         /* string with all spaces */
147         if (*str == '\0')
148                 goto out;
149
150         end = str;
151         while (i < size && *end != '\0' && !isspace(*end)) {
152                 ++i;
153                 ++end;
154         }
155
156         *end= '\0';
157 out:
158         return str;
159 }
160 EXPORT_SYMBOL(cfs_firststr);
161
162 char *
163 cfs_trimwhite(char *str)
164 {
165         char *end;
166
167         while (cfs_iswhite(*str))
168                 str++;
169
170         end = str + strlen(str);
171         while (end > str) {
172                 if (!cfs_iswhite(end[-1]))
173                         break;
174                 end--;
175         }
176
177         *end = 0;
178         return str;
179 }
180 EXPORT_SYMBOL(cfs_trimwhite);
181
182 /**
183  * Extracts tokens from strings.
184  *
185  * Looks for \a delim in string \a next, sets \a res to point to
186  * substring before the delimiter, sets \a next right after the found
187  * delimiter.
188  *
189  * \retval 1 if \a res points to a string of non-whitespace characters
190  * \retval 0 otherwise
191  */
192 int
193 cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
194 {
195         char *end;
196
197         if (next->ls_str == NULL)
198                 return 0;
199
200         /* skip leading white spaces */
201         while (next->ls_len) {
202                 if (!cfs_iswhite(*next->ls_str))
203                         break;
204                 next->ls_str++;
205                 next->ls_len--;
206         }
207
208         if (next->ls_len == 0) /* whitespaces only */
209                 return 0;
210
211         if (*next->ls_str == delim) {
212                 /* first non-writespace is the delimiter */
213                 return 0;
214         }
215
216         res->ls_str = next->ls_str;
217         end = memchr(next->ls_str, delim, next->ls_len);
218         if (end == NULL) {
219                 /* there is no the delimeter in the string */
220                 end = next->ls_str + next->ls_len;
221                 next->ls_str = NULL;
222         } else {
223                 next->ls_str = end + 1;
224                 next->ls_len -= (end - res->ls_str + 1);
225         }
226
227         /* skip ending whitespaces */
228         while (--end != res->ls_str) {
229                 if (!cfs_iswhite(*end))
230                         break;
231         }
232
233         res->ls_len = end - res->ls_str + 1;
234         return 1;
235 }
236 EXPORT_SYMBOL(cfs_gettok);
237
238 /**
239  * Converts string to integer.
240  *
241  * Accepts decimal and hexadecimal number recordings.
242  *
243  * \retval 1 if first \a nob chars of \a str convert to decimal or
244  * hexadecimal integer in the range [\a min, \a max]
245  * \retval 0 otherwise
246  */
247 int
248 cfs_str2num_check(char *str, int nob, unsigned *num,
249                   unsigned min, unsigned max)
250 {
251         char    *endp;
252
253         str = cfs_trimwhite(str);
254         *num = strtoul(str, &endp, 0);
255         if (endp == str)
256                 return 0;
257
258         for (; endp < str + nob; endp++) {
259                 if (!cfs_iswhite(*endp))
260                         return 0;
261         }
262
263         return (*num >= min && *num <= max);
264 }
265 EXPORT_SYMBOL(cfs_str2num_check);
266
267 /**
268  * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
269  * \a src should only have a single token which can be \<number\> or  \*
270  *
271  * \retval pointer to allocated range_expr and initialized
272  * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
273  `* src parses to
274  * \<number\> |
275  * \<number\> '-' \<number\> |
276  * \<number\> '-' \<number\> '/' \<number\>
277  * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
278  * -ENOMEM will be returned.
279  */
280 int
281 cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
282                      int bracketed, struct cfs_range_expr **expr)
283 {
284         struct cfs_range_expr   *re;
285         struct cfs_lstr         tok;
286
287         LIBCFS_ALLOC(re, sizeof(*re));
288         if (re == NULL)
289                 return -ENOMEM;
290
291         if (src->ls_len == 1 && src->ls_str[0] == '*') {
292                 re->re_lo = min;
293                 re->re_hi = max;
294                 re->re_stride = 1;
295                 goto out;
296         }
297
298         if (cfs_str2num_check(src->ls_str, src->ls_len,
299                               &re->re_lo, min, max)) {
300                 /* <number> is parsed */
301                 re->re_hi = re->re_lo;
302                 re->re_stride = 1;
303                 goto out;
304         }
305
306         if (!bracketed || !cfs_gettok(src, '-', &tok))
307                 goto failed;
308
309         if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
310                                &re->re_lo, min, max))
311                 goto failed;
312
313         /* <number> - */
314         if (cfs_str2num_check(src->ls_str, src->ls_len,
315                               &re->re_hi, min, max)) {
316                 /* <number> - <number> is parsed */
317                 re->re_stride = 1;
318                 goto out;
319         }
320
321         /* go to check <number> '-' <number> '/' <number> */
322         if (cfs_gettok(src, '/', &tok)) {
323                 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
324                                        &re->re_hi, min, max))
325                         goto failed;
326
327                 /* <number> - <number> / ... */
328                 if (cfs_str2num_check(src->ls_str, src->ls_len,
329                                       &re->re_stride, min, max)) {
330                         /* <number> - <number> / <number> is parsed */
331                         goto out;
332                 }
333         }
334
335  out:
336         *expr = re;
337         return 0;
338
339  failed:
340         LIBCFS_FREE(re, sizeof(*re));
341         return -EINVAL;
342 }
343 EXPORT_SYMBOL(cfs_range_expr_parse);
344
345 /**
346  * Matches value (\a value) against ranges expression list \a expr_list.
347  *
348  * \retval 1 if \a value matches
349  * \retval 0 otherwise
350  */
351 int
352 cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
353 {
354         struct cfs_range_expr   *expr;
355
356         list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
357                 if (value >= expr->re_lo && value <= expr->re_hi &&
358                     ((value - expr->re_lo) % expr->re_stride) == 0)
359                         return 1;
360         }
361
362         return 0;
363 }
364 EXPORT_SYMBOL(cfs_expr_list_match);
365
366 /**
367  * Convert express list (\a expr_list) to an array of all matched values
368  *
369  * \retval N N is total number of all matched values
370  * \retval 0 if expression list is empty
371  * \retval < 0 for failure
372  */
373 int
374 cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
375 {
376         struct cfs_range_expr   *expr;
377         __u32                   *val;
378         int                     count = 0;
379         int                     i;
380
381         list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
382                 for (i = expr->re_lo; i <= expr->re_hi; i++) {
383                         if (((i - expr->re_lo) % expr->re_stride) == 0)
384                                 count++;
385                 }
386         }
387
388         if (count == 0) /* empty expression list */
389                 return 0;
390
391         if (count > max) {
392                 CERROR("Number of values %d exceeds max allowed %d\n",
393                        max, count);
394                 return -EINVAL;
395         }
396
397         LIBCFS_ALLOC(val, sizeof(val[0]) * count);
398         if (val == NULL)
399                 return -ENOMEM;
400
401         count = 0;
402         list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
403                 for (i = expr->re_lo; i <= expr->re_hi; i++) {
404                         if (((i - expr->re_lo) % expr->re_stride) == 0)
405                                 val[count++] = i;
406                 }
407         }
408
409         *valpp = val;
410         return count;
411 }
412 EXPORT_SYMBOL(cfs_expr_list_values);
413
414 /**
415  * Frees cfs_range_expr structures of \a expr_list.
416  *
417  * \retval none
418  */
419 void
420 cfs_expr_list_free(struct cfs_expr_list *expr_list)
421 {
422         while (!list_empty(&expr_list->el_exprs)) {
423                 struct cfs_range_expr *expr;
424
425                 expr = list_entry(expr_list->el_exprs.next,
426                                       struct cfs_range_expr, re_link),
427                 list_del(&expr->re_link);
428                 LIBCFS_FREE(expr, sizeof(*expr));
429         }
430
431         LIBCFS_FREE(expr_list, sizeof(*expr_list));
432 }
433 EXPORT_SYMBOL(cfs_expr_list_free);
434
435 void
436 cfs_expr_list_print(struct cfs_expr_list *expr_list)
437 {
438         struct cfs_range_expr *expr;
439
440         list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
441                 CDEBUG(D_WARNING, "%d-%d/%d\n",
442                        expr->re_lo, expr->re_hi, expr->re_stride);
443         }
444 }
445 EXPORT_SYMBOL(cfs_expr_list_print);
446
447 /**
448  * Parses \<cfs_expr_list\> token of the syntax.
449  *
450  * \retval 1 if \a str parses to \<number\> | \<expr_list\>
451  * \retval 0 otherwise
452  */
453 int
454 cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
455                     struct cfs_expr_list **elpp)
456 {
457         struct cfs_expr_list    *expr_list;
458         struct cfs_range_expr   *expr;
459         struct cfs_lstr         src;
460         int                     rc;
461
462         LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
463         if (expr_list == NULL)
464                 return -ENOMEM;
465
466         src.ls_str = str;
467         src.ls_len = len;
468
469         INIT_LIST_HEAD(&expr_list->el_exprs);
470
471         if (src.ls_str[0] == '[' &&
472             src.ls_str[src.ls_len - 1] == ']') {
473                 src.ls_str++;
474                 src.ls_len -= 2;
475
476                 rc = -EINVAL;
477                 while (src.ls_str != NULL) {
478                         struct cfs_lstr tok;
479
480                         if (!cfs_gettok(&src, ',', &tok)) {
481                                 rc = -EINVAL;
482                                 break;
483                         }
484
485                         rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
486                         if (rc != 0)
487                                 break;
488
489                         list_add_tail(&expr->re_link,
490                                           &expr_list->el_exprs);
491                 }
492         } else {
493                 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
494                 if (rc == 0) {
495                         list_add_tail(&expr->re_link,
496                                           &expr_list->el_exprs);
497                 }
498         }
499
500         if (rc != 0)
501                 cfs_expr_list_free(expr_list);
502         else
503                 *elpp = expr_list;
504
505         return rc;
506 }
507 EXPORT_SYMBOL(cfs_expr_list_parse);
508
509 /**
510  * Frees cfs_expr_list structures of \a list.
511  *
512  * For each struct cfs_expr_list structure found on \a list it frees
513  * range_expr list attached to it and frees the cfs_expr_list itself.
514  *
515  * \retval none
516  */
517 void
518 cfs_expr_list_free_list(struct list_head *list)
519 {
520         struct cfs_expr_list *el;
521
522         while (!list_empty(list)) {
523                 el = list_entry(list->next,
524                                     struct cfs_expr_list, el_link);
525                 list_del(&el->el_link);
526                 cfs_expr_list_free(el);
527         }
528 }
529 EXPORT_SYMBOL(cfs_expr_list_free_list);
530
531 int
532 cfs_ip_addr_parse(char *str, int len, struct list_head *list)
533 {
534         struct cfs_expr_list    *el;
535         struct cfs_lstr         src;
536         int                     rc;
537         int                     i;
538
539         src.ls_str = str;
540         src.ls_len = len;
541         i = 0;
542
543         while (src.ls_str != NULL) {
544                 struct cfs_lstr res;
545
546                 if (!cfs_gettok(&src, '.', &res)) {
547                         rc = -EINVAL;
548                         goto out;
549                 }
550
551                 rc = cfs_expr_list_parse(res.ls_str, res.ls_len, 0, 255, &el);
552                 if (rc != 0)
553                         goto out;
554
555                 list_add_tail(&el->el_link, list);
556                 i++;
557         }
558
559         if (i == 4)
560                 return 0;
561
562         rc = -EINVAL;
563  out:
564         cfs_expr_list_free_list(list);
565
566         return rc;
567 }
568 EXPORT_SYMBOL(cfs_ip_addr_parse);
569
570 /**
571  * Matches address (\a addr) against address set encoded in \a list.
572  *
573  * \retval 1 if \a addr matches
574  * \retval 0 otherwise
575  */
576 int
577 cfs_ip_addr_match(__u32 addr, struct list_head *list)
578 {
579         struct cfs_expr_list *el;
580         int i = 0;
581
582         list_for_each_entry_reverse(el, list, el_link) {
583                 if (!cfs_expr_list_match(addr & 0xff, el))
584                         return 0;
585                 addr >>= 8;
586                 i++;
587         }
588
589         return i == 4;
590 }
591 EXPORT_SYMBOL(cfs_ip_addr_match);
592
593 void
594 cfs_ip_addr_free(struct list_head *list)
595 {
596         cfs_expr_list_free_list(list);
597 }
598 EXPORT_SYMBOL(cfs_ip_addr_free);