blob: fc697cdfcdaf678ab55704acf263375486da840f [file] [log] [blame]
Peng Taod7e09d02013-05-02 16:46:55 +08001/*
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
Oleg Drokin6a5b99a2016-06-14 23:33:40 -040018 * http://www.gnu.org/licenses/gpl-2.0.html
Peng Taod7e09d02013-05-02 16:46:55 +080019 *
Peng Taod7e09d02013-05-02 16:46:55 +080020 * GPL HEADER END
21 */
22/*
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
25 *
Andreas Dilger1dc563a2015-11-08 18:09:37 -050026 * Copyright (c) 2012, 2015 Intel Corporation.
Peng Taod7e09d02013-05-02 16:46:55 +080027 */
28/*
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
31 *
32 * String manipulation functions.
33 *
34 * libcfs/libcfs/libcfs_string.c
35 *
36 * Author: Nathan Rutman <nathan.rutman@sun.com>
37 */
38
Greg Kroah-Hartman9fdaf8c2014-07-11 20:51:16 -070039#include "../../include/linux/libcfs/libcfs.h"
Peng Taod7e09d02013-05-02 16:46:55 +080040
Peng Taod7e09d02013-05-02 16:46:55 +080041/* Convert a text string to a bitmask */
42int cfs_str2mask(const char *str, const char *(*bit2str)(int bit),
43 int *oldmask, int minmask, int allmask)
44{
45 const char *debugstr;
Matthew Tyler3f9773a2014-12-15 22:37:53 +080046 char op = '\0';
Peng Taod7e09d02013-05-02 16:46:55 +080047 int newmask = minmask, i, len, found = 0;
Peng Taod7e09d02013-05-02 16:46:55 +080048
49 /* <str> must be a list of tokens separated by whitespace
50 * and optionally an operator ('+' or '-'). If an operator
51 * appears first in <str>, '*oldmask' is used as the starting point
52 * (relative), otherwise minmask is used (absolute). An operator
Oleg Drokina3fbcb3c2016-02-16 00:47:08 -050053 * applies to all following tokens up to the next operator.
54 */
Matthew Tyler3f9773a2014-12-15 22:37:53 +080055 while (*str != '\0') {
Peng Taod7e09d02013-05-02 16:46:55 +080056 while (isspace(*str))
57 str++;
Matthew Tyler3f9773a2014-12-15 22:37:53 +080058 if (*str == '\0')
Peng Taod7e09d02013-05-02 16:46:55 +080059 break;
60 if (*str == '+' || *str == '-') {
61 op = *str++;
62 if (!found)
63 /* only if first token is relative */
64 newmask = *oldmask;
65 while (isspace(*str))
66 str++;
Matthew Tyler3f9773a2014-12-15 22:37:53 +080067 if (*str == '\0') /* trailing op */
Peng Taod7e09d02013-05-02 16:46:55 +080068 return -EINVAL;
69 }
70
71 /* find token length */
Matthew Tyler3f9773a2014-12-15 22:37:53 +080072 len = 0;
73 while (str[len] != '\0' && !isspace(str[len]) &&
74 str[len] != '+' && str[len] != '-')
75 len++;
Peng Taod7e09d02013-05-02 16:46:55 +080076
77 /* match token */
78 found = 0;
79 for (i = 0; i < 32; i++) {
80 debugstr = bit2str(i);
Oleg Drokin15d9f522016-02-16 00:46:44 -050081 if (debugstr && strlen(debugstr) == len &&
Peng Tao7a724582014-03-05 21:27:16 +080082 strncasecmp(str, debugstr, len) == 0) {
Peng Taod7e09d02013-05-02 16:46:55 +080083 if (op == '-')
84 newmask &= ~(1 << i);
85 else
86 newmask |= (1 << i);
87 found = 1;
88 break;
89 }
90 }
91 if (!found && len == 3 &&
Peng Tao7a724582014-03-05 21:27:16 +080092 (strncasecmp(str, "ALL", len) == 0)) {
Peng Taod7e09d02013-05-02 16:46:55 +080093 if (op == '-')
94 newmask = minmask;
95 else
96 newmask = allmask;
97 found = 1;
98 }
99 if (!found) {
100 CWARN("unknown mask '%.*s'.\n"
101 "mask usage: [+|-]<all|type> ...\n", len, str);
102 return -EINVAL;
103 }
104 str += len;
105 }
106
107 *oldmask = newmask;
108 return 0;
109}
Peng Taod7e09d02013-05-02 16:46:55 +0800110
Peng Taod7e09d02013-05-02 16:46:55 +0800111/* get the first string out of @str */
112char *cfs_firststr(char *str, size_t size)
113{
114 size_t i = 0;
115 char *end;
116
117 /* trim leading spaces */
118 while (i < size && *str && isspace(*str)) {
119 ++i;
120 ++str;
121 }
122
123 /* string with all spaces */
124 if (*str == '\0')
125 goto out;
126
127 end = str;
128 while (i < size && *end != '\0' && !isspace(*end)) {
129 ++i;
130 ++end;
131 }
132
Matthew Tyler51566472014-12-15 22:37:27 +0800133 *end = '\0';
Peng Taod7e09d02013-05-02 16:46:55 +0800134out:
135 return str;
136}
137EXPORT_SYMBOL(cfs_firststr);
138
139char *
140cfs_trimwhite(char *str)
141{
142 char *end;
143
Peng Taoe525a682014-03-05 21:27:18 +0800144 while (isspace(*str))
Peng Taod7e09d02013-05-02 16:46:55 +0800145 str++;
146
147 end = str + strlen(str);
148 while (end > str) {
Peng Taoe525a682014-03-05 21:27:18 +0800149 if (!isspace(end[-1]))
Peng Taod7e09d02013-05-02 16:46:55 +0800150 break;
151 end--;
152 }
153
154 *end = 0;
155 return str;
156}
157EXPORT_SYMBOL(cfs_trimwhite);
158
159/**
160 * Extracts tokens from strings.
161 *
162 * Looks for \a delim in string \a next, sets \a res to point to
163 * substring before the delimiter, sets \a next right after the found
164 * delimiter.
165 *
166 * \retval 1 if \a res points to a string of non-whitespace characters
167 * \retval 0 otherwise
168 */
169int
170cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res)
171{
172 char *end;
173
Oleg Drokin15d9f522016-02-16 00:46:44 -0500174 if (!next->ls_str)
Peng Taod7e09d02013-05-02 16:46:55 +0800175 return 0;
176
177 /* skip leading white spaces */
178 while (next->ls_len) {
Peng Taoe525a682014-03-05 21:27:18 +0800179 if (!isspace(*next->ls_str))
Peng Taod7e09d02013-05-02 16:46:55 +0800180 break;
181 next->ls_str++;
182 next->ls_len--;
183 }
184
185 if (next->ls_len == 0) /* whitespaces only */
186 return 0;
187
188 if (*next->ls_str == delim) {
189 /* first non-writespace is the delimiter */
190 return 0;
191 }
192
193 res->ls_str = next->ls_str;
194 end = memchr(next->ls_str, delim, next->ls_len);
Oleg Drokin15d9f522016-02-16 00:46:44 -0500195 if (!end) {
Peng Taod7e09d02013-05-02 16:46:55 +0800196 /* there is no the delimeter in the string */
197 end = next->ls_str + next->ls_len;
198 next->ls_str = NULL;
199 } else {
200 next->ls_str = end + 1;
201 next->ls_len -= (end - res->ls_str + 1);
202 }
203
204 /* skip ending whitespaces */
205 while (--end != res->ls_str) {
Peng Taoe525a682014-03-05 21:27:18 +0800206 if (!isspace(*end))
Peng Taod7e09d02013-05-02 16:46:55 +0800207 break;
208 }
209
210 res->ls_len = end - res->ls_str + 1;
211 return 1;
212}
James Simmons47ca6ec2015-10-21 21:52:40 -0400213EXPORT_SYMBOL(cfs_gettok);
Peng Taod7e09d02013-05-02 16:46:55 +0800214
215/**
216 * Converts string to integer.
217 *
218 * Accepts decimal and hexadecimal number recordings.
219 *
220 * \retval 1 if first \a nob chars of \a str convert to decimal or
221 * hexadecimal integer in the range [\a min, \a max]
222 * \retval 0 otherwise
223 */
224int
225cfs_str2num_check(char *str, int nob, unsigned *num,
226 unsigned min, unsigned max)
227{
James Simmons3ad61522016-03-03 11:00:58 -0500228 bool all_numbers = true;
229 char *endp, cache;
Oleg Drokinae0246d2016-02-16 00:47:17 -0500230 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800231
232 str = cfs_trimwhite(str);
James Simmons3ad61522016-03-03 11:00:58 -0500233
234 /**
235 * kstrouint can only handle strings composed
236 * of only numbers. We need to scan the string
237 * passed in for the first non-digit character
238 * and end the string at that location. If we
239 * don't find any non-digit character we still
240 * need to place a '\0' at position nob since
241 * we are not interested in the rest of the
242 * string which is longer than nob in size.
243 * After we are done the character at the
244 * position we placed '\0' must be restored.
245 */
246 for (endp = str; endp < str + nob; endp++) {
247 if (!isdigit(*endp)) {
248 all_numbers = false;
249 break;
250 }
251 }
252 cache = *endp;
253 *endp = '\0';
254
Oleg Drokinae0246d2016-02-16 00:47:17 -0500255 rc = kstrtouint(str, 10, num);
James Simmons3ad61522016-03-03 11:00:58 -0500256 *endp = cache;
257 if (rc || !all_numbers)
Peng Taod7e09d02013-05-02 16:46:55 +0800258 return 0;
259
Peng Taod7e09d02013-05-02 16:46:55 +0800260 return (*num >= min && *num <= max);
261}
James Simmons47ca6ec2015-10-21 21:52:40 -0400262EXPORT_SYMBOL(cfs_str2num_check);
Peng Taod7e09d02013-05-02 16:46:55 +0800263
264/**
265 * Parses \<range_expr\> token of the syntax. If \a bracketed is false,
266 * \a src should only have a single token which can be \<number\> or \*
267 *
268 * \retval pointer to allocated range_expr and initialized
269 * range_expr::re_lo, range_expr::re_hi and range_expr:re_stride if \a
270 `* src parses to
271 * \<number\> |
272 * \<number\> '-' \<number\> |
273 * \<number\> '-' \<number\> '/' \<number\>
274 * \retval 0 will be returned if it can be parsed, otherwise -EINVAL or
275 * -ENOMEM will be returned.
276 */
Peng Tao31fb6132014-03-05 21:27:17 +0800277static int
Peng Taod7e09d02013-05-02 16:46:55 +0800278cfs_range_expr_parse(struct cfs_lstr *src, unsigned min, unsigned max,
279 int bracketed, struct cfs_range_expr **expr)
280{
281 struct cfs_range_expr *re;
282 struct cfs_lstr tok;
283
284 LIBCFS_ALLOC(re, sizeof(*re));
Oleg Drokin15d9f522016-02-16 00:46:44 -0500285 if (!re)
Peng Taod7e09d02013-05-02 16:46:55 +0800286 return -ENOMEM;
287
288 if (src->ls_len == 1 && src->ls_str[0] == '*') {
289 re->re_lo = min;
290 re->re_hi = max;
291 re->re_stride = 1;
292 goto out;
293 }
294
295 if (cfs_str2num_check(src->ls_str, src->ls_len,
296 &re->re_lo, min, max)) {
297 /* <number> is parsed */
298 re->re_hi = re->re_lo;
299 re->re_stride = 1;
300 goto out;
301 }
302
303 if (!bracketed || !cfs_gettok(src, '-', &tok))
304 goto failed;
305
306 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
307 &re->re_lo, min, max))
308 goto failed;
309
310 /* <number> - */
311 if (cfs_str2num_check(src->ls_str, src->ls_len,
312 &re->re_hi, min, max)) {
313 /* <number> - <number> is parsed */
314 re->re_stride = 1;
315 goto out;
316 }
317
318 /* go to check <number> '-' <number> '/' <number> */
319 if (cfs_gettok(src, '/', &tok)) {
320 if (!cfs_str2num_check(tok.ls_str, tok.ls_len,
321 &re->re_hi, min, max))
322 goto failed;
323
324 /* <number> - <number> / ... */
325 if (cfs_str2num_check(src->ls_str, src->ls_len,
326 &re->re_stride, min, max)) {
327 /* <number> - <number> / <number> is parsed */
328 goto out;
329 }
330 }
331
332 out:
333 *expr = re;
334 return 0;
335
336 failed:
337 LIBCFS_FREE(re, sizeof(*re));
338 return -EINVAL;
339}
Peng Taod7e09d02013-05-02 16:46:55 +0800340
341/**
Gregoire Pichona1dfc932015-10-21 21:52:39 -0400342 * Print the range expression \a re into specified \a buffer.
343 * If \a bracketed is true, expression does not need additional
344 * brackets.
345 *
346 * \retval number of characters written
347 */
348static int
349cfs_range_expr_print(char *buffer, int count, struct cfs_range_expr *expr,
350 bool bracketed)
351{
352 int i;
353 char s[] = "[";
354 char e[] = "]";
355
Oleg Drokin34ec7f82016-02-16 00:47:13 -0500356 if (bracketed) {
357 s[0] = '\0';
358 e[0] = '\0';
359 }
Gregoire Pichona1dfc932015-10-21 21:52:39 -0400360
361 if (expr->re_lo == expr->re_hi)
362 i = scnprintf(buffer, count, "%u", expr->re_lo);
363 else if (expr->re_stride == 1)
364 i = scnprintf(buffer, count, "%s%u-%u%s",
Oleg Drokinae0b4832016-02-16 00:47:00 -0500365 s, expr->re_lo, expr->re_hi, e);
Gregoire Pichona1dfc932015-10-21 21:52:39 -0400366 else
367 i = scnprintf(buffer, count, "%s%u-%u/%u%s",
Oleg Drokinae0b4832016-02-16 00:47:00 -0500368 s, expr->re_lo, expr->re_hi, expr->re_stride, e);
Gregoire Pichona1dfc932015-10-21 21:52:39 -0400369 return i;
370}
371
372/**
373 * Print a list of range expressions (\a expr_list) into specified \a buffer.
374 * If the list contains several expressions, separate them with comma
375 * and surround the list with brackets.
376 *
377 * \retval number of characters written
378 */
379int
380cfs_expr_list_print(char *buffer, int count, struct cfs_expr_list *expr_list)
381{
382 struct cfs_range_expr *expr;
383 int i = 0, j = 0;
384 int numexprs = 0;
385
386 if (count <= 0)
387 return 0;
388
389 list_for_each_entry(expr, &expr_list->el_exprs, re_link)
390 numexprs++;
391
392 if (numexprs > 1)
393 i += scnprintf(buffer + i, count - i, "[");
394
395 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
396 if (j++ != 0)
397 i += scnprintf(buffer + i, count - i, ",");
398 i += cfs_range_expr_print(buffer + i, count - i, expr,
399 numexprs > 1);
400 }
401
402 if (numexprs > 1)
403 i += scnprintf(buffer + i, count - i, "]");
404
405 return i;
406}
407EXPORT_SYMBOL(cfs_expr_list_print);
408
409/**
Peng Taod7e09d02013-05-02 16:46:55 +0800410 * Matches value (\a value) against ranges expression list \a expr_list.
411 *
412 * \retval 1 if \a value matches
413 * \retval 0 otherwise
414 */
415int
416cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list)
417{
418 struct cfs_range_expr *expr;
419
420 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
421 if (value >= expr->re_lo && value <= expr->re_hi &&
422 ((value - expr->re_lo) % expr->re_stride) == 0)
423 return 1;
424 }
425
426 return 0;
427}
James Simmons47ca6ec2015-10-21 21:52:40 -0400428EXPORT_SYMBOL(cfs_expr_list_match);
Peng Taod7e09d02013-05-02 16:46:55 +0800429
430/**
431 * Convert express list (\a expr_list) to an array of all matched values
432 *
433 * \retval N N is total number of all matched values
434 * \retval 0 if expression list is empty
435 * \retval < 0 for failure
436 */
437int
438cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
439{
440 struct cfs_range_expr *expr;
441 __u32 *val;
442 int count = 0;
443 int i;
444
445 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
446 for (i = expr->re_lo; i <= expr->re_hi; i++) {
447 if (((i - expr->re_lo) % expr->re_stride) == 0)
448 count++;
449 }
450 }
451
452 if (count == 0) /* empty expression list */
453 return 0;
454
455 if (count > max) {
456 CERROR("Number of values %d exceeds max allowed %d\n",
457 max, count);
458 return -EINVAL;
459 }
460
461 LIBCFS_ALLOC(val, sizeof(val[0]) * count);
Oleg Drokin15d9f522016-02-16 00:46:44 -0500462 if (!val)
Peng Taod7e09d02013-05-02 16:46:55 +0800463 return -ENOMEM;
464
465 count = 0;
466 list_for_each_entry(expr, &expr_list->el_exprs, re_link) {
467 for (i = expr->re_lo; i <= expr->re_hi; i++) {
468 if (((i - expr->re_lo) % expr->re_stride) == 0)
469 val[count++] = i;
470 }
471 }
472
473 *valpp = val;
474 return count;
475}
476EXPORT_SYMBOL(cfs_expr_list_values);
477
478/**
479 * Frees cfs_range_expr structures of \a expr_list.
480 *
481 * \retval none
482 */
483void
484cfs_expr_list_free(struct cfs_expr_list *expr_list)
485{
486 while (!list_empty(&expr_list->el_exprs)) {
487 struct cfs_range_expr *expr;
488
489 expr = list_entry(expr_list->el_exprs.next,
Oleg Drokinae0b4832016-02-16 00:47:00 -0500490 struct cfs_range_expr, re_link);
Peng Taod7e09d02013-05-02 16:46:55 +0800491 list_del(&expr->re_link);
492 LIBCFS_FREE(expr, sizeof(*expr));
493 }
494
495 LIBCFS_FREE(expr_list, sizeof(*expr_list));
496}
497EXPORT_SYMBOL(cfs_expr_list_free);
498
Peng Taod7e09d02013-05-02 16:46:55 +0800499/**
500 * Parses \<cfs_expr_list\> token of the syntax.
501 *
Gregoire Pichona1dfc932015-10-21 21:52:39 -0400502 * \retval 0 if \a str parses to \<number\> | \<expr_list\>
503 * \retval -errno otherwise
Peng Taod7e09d02013-05-02 16:46:55 +0800504 */
505int
506cfs_expr_list_parse(char *str, int len, unsigned min, unsigned max,
507 struct cfs_expr_list **elpp)
508{
509 struct cfs_expr_list *expr_list;
510 struct cfs_range_expr *expr;
511 struct cfs_lstr src;
512 int rc;
513
514 LIBCFS_ALLOC(expr_list, sizeof(*expr_list));
Oleg Drokin15d9f522016-02-16 00:46:44 -0500515 if (!expr_list)
Peng Taod7e09d02013-05-02 16:46:55 +0800516 return -ENOMEM;
517
518 src.ls_str = str;
519 src.ls_len = len;
520
521 INIT_LIST_HEAD(&expr_list->el_exprs);
522
523 if (src.ls_str[0] == '[' &&
524 src.ls_str[src.ls_len - 1] == ']') {
525 src.ls_str++;
526 src.ls_len -= 2;
527
528 rc = -EINVAL;
Oleg Drokin15d9f522016-02-16 00:46:44 -0500529 while (src.ls_str) {
Peng Taod7e09d02013-05-02 16:46:55 +0800530 struct cfs_lstr tok;
531
532 if (!cfs_gettok(&src, ',', &tok)) {
533 rc = -EINVAL;
534 break;
535 }
536
537 rc = cfs_range_expr_parse(&tok, min, max, 1, &expr);
538 if (rc != 0)
539 break;
540
Oleg Drokinae0b4832016-02-16 00:47:00 -0500541 list_add_tail(&expr->re_link, &expr_list->el_exprs);
Peng Taod7e09d02013-05-02 16:46:55 +0800542 }
543 } else {
544 rc = cfs_range_expr_parse(&src, min, max, 0, &expr);
Oleg Drokinae0b4832016-02-16 00:47:00 -0500545 if (rc == 0)
546 list_add_tail(&expr->re_link, &expr_list->el_exprs);
Peng Taod7e09d02013-05-02 16:46:55 +0800547 }
548
549 if (rc != 0)
550 cfs_expr_list_free(expr_list);
551 else
552 *elpp = expr_list;
553
554 return rc;
555}
556EXPORT_SYMBOL(cfs_expr_list_parse);
557
558/**
559 * Frees cfs_expr_list structures of \a list.
560 *
561 * For each struct cfs_expr_list structure found on \a list it frees
562 * range_expr list attached to it and frees the cfs_expr_list itself.
563 *
564 * \retval none
565 */
566void
567cfs_expr_list_free_list(struct list_head *list)
568{
569 struct cfs_expr_list *el;
570
571 while (!list_empty(list)) {
Oleg Drokinae0b4832016-02-16 00:47:00 -0500572 el = list_entry(list->next, struct cfs_expr_list, el_link);
Peng Taod7e09d02013-05-02 16:46:55 +0800573 list_del(&el->el_link);
574 cfs_expr_list_free(el);
575 }
576}
James Simmons47ca6ec2015-10-21 21:52:40 -0400577EXPORT_SYMBOL(cfs_expr_list_free_list);