Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 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 |
Oleg Drokin | 6a5b99a | 2016-06-14 23:33:40 -0400 | [diff] [blame] | 18 | * http://www.gnu.org/licenses/gpl-2.0.html |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 19 | * |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 20 | * 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 Dilger | 1dc563a | 2015-11-08 18:09:37 -0500 | [diff] [blame] | 26 | * Copyright (c) 2012, 2015 Intel Corporation. |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 27 | */ |
| 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-Hartman | 9fdaf8c | 2014-07-11 20:51:16 -0700 | [diff] [blame] | 39 | #include "../../include/linux/libcfs/libcfs.h" |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 40 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 41 | /* Convert a text string to a bitmask */ |
| 42 | int cfs_str2mask(const char *str, const char *(*bit2str)(int bit), |
| 43 | int *oldmask, int minmask, int allmask) |
| 44 | { |
| 45 | const char *debugstr; |
Matthew Tyler | 3f9773a | 2014-12-15 22:37:53 +0800 | [diff] [blame] | 46 | char op = '\0'; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 47 | int newmask = minmask, i, len, found = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 48 | |
| 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 Drokin | a3fbcb3c | 2016-02-16 00:47:08 -0500 | [diff] [blame] | 53 | * applies to all following tokens up to the next operator. |
| 54 | */ |
Matthew Tyler | 3f9773a | 2014-12-15 22:37:53 +0800 | [diff] [blame] | 55 | while (*str != '\0') { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 56 | while (isspace(*str)) |
| 57 | str++; |
Matthew Tyler | 3f9773a | 2014-12-15 22:37:53 +0800 | [diff] [blame] | 58 | if (*str == '\0') |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 59 | 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 Tyler | 3f9773a | 2014-12-15 22:37:53 +0800 | [diff] [blame] | 67 | if (*str == '\0') /* trailing op */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 68 | return -EINVAL; |
| 69 | } |
| 70 | |
| 71 | /* find token length */ |
Matthew Tyler | 3f9773a | 2014-12-15 22:37:53 +0800 | [diff] [blame] | 72 | len = 0; |
| 73 | while (str[len] != '\0' && !isspace(str[len]) && |
| 74 | str[len] != '+' && str[len] != '-') |
| 75 | len++; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 76 | |
| 77 | /* match token */ |
| 78 | found = 0; |
| 79 | for (i = 0; i < 32; i++) { |
| 80 | debugstr = bit2str(i); |
Oleg Drokin | 15d9f52 | 2016-02-16 00:46:44 -0500 | [diff] [blame] | 81 | if (debugstr && strlen(debugstr) == len && |
Peng Tao | 7a72458 | 2014-03-05 21:27:16 +0800 | [diff] [blame] | 82 | strncasecmp(str, debugstr, len) == 0) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 83 | 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 Tao | 7a72458 | 2014-03-05 21:27:16 +0800 | [diff] [blame] | 92 | (strncasecmp(str, "ALL", len) == 0)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 93 | 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 Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 110 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 111 | /* get the first string out of @str */ |
| 112 | char *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 Tyler | 5156647 | 2014-12-15 22:37:27 +0800 | [diff] [blame] | 133 | *end = '\0'; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 134 | out: |
| 135 | return str; |
| 136 | } |
| 137 | EXPORT_SYMBOL(cfs_firststr); |
| 138 | |
| 139 | char * |
| 140 | cfs_trimwhite(char *str) |
| 141 | { |
| 142 | char *end; |
| 143 | |
Peng Tao | e525a68 | 2014-03-05 21:27:18 +0800 | [diff] [blame] | 144 | while (isspace(*str)) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 145 | str++; |
| 146 | |
| 147 | end = str + strlen(str); |
| 148 | while (end > str) { |
Peng Tao | e525a68 | 2014-03-05 21:27:18 +0800 | [diff] [blame] | 149 | if (!isspace(end[-1])) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 150 | break; |
| 151 | end--; |
| 152 | } |
| 153 | |
| 154 | *end = 0; |
| 155 | return str; |
| 156 | } |
| 157 | EXPORT_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 | */ |
| 169 | int |
| 170 | cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res) |
| 171 | { |
| 172 | char *end; |
| 173 | |
Oleg Drokin | 15d9f52 | 2016-02-16 00:46:44 -0500 | [diff] [blame] | 174 | if (!next->ls_str) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 175 | return 0; |
| 176 | |
| 177 | /* skip leading white spaces */ |
| 178 | while (next->ls_len) { |
Peng Tao | e525a68 | 2014-03-05 21:27:18 +0800 | [diff] [blame] | 179 | if (!isspace(*next->ls_str)) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 180 | 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 Drokin | 15d9f52 | 2016-02-16 00:46:44 -0500 | [diff] [blame] | 195 | if (!end) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 196 | /* 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 Tao | e525a68 | 2014-03-05 21:27:18 +0800 | [diff] [blame] | 206 | if (!isspace(*end)) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 207 | break; |
| 208 | } |
| 209 | |
| 210 | res->ls_len = end - res->ls_str + 1; |
| 211 | return 1; |
| 212 | } |
James Simmons | 47ca6ec | 2015-10-21 21:52:40 -0400 | [diff] [blame] | 213 | EXPORT_SYMBOL(cfs_gettok); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 214 | |
| 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 | */ |
| 224 | int |
| 225 | cfs_str2num_check(char *str, int nob, unsigned *num, |
| 226 | unsigned min, unsigned max) |
| 227 | { |
James Simmons | 3ad6152 | 2016-03-03 11:00:58 -0500 | [diff] [blame] | 228 | bool all_numbers = true; |
| 229 | char *endp, cache; |
Oleg Drokin | ae0246d | 2016-02-16 00:47:17 -0500 | [diff] [blame] | 230 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 231 | |
| 232 | str = cfs_trimwhite(str); |
James Simmons | 3ad6152 | 2016-03-03 11:00:58 -0500 | [diff] [blame] | 233 | |
| 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 Drokin | ae0246d | 2016-02-16 00:47:17 -0500 | [diff] [blame] | 255 | rc = kstrtouint(str, 10, num); |
James Simmons | 3ad6152 | 2016-03-03 11:00:58 -0500 | [diff] [blame] | 256 | *endp = cache; |
| 257 | if (rc || !all_numbers) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 258 | return 0; |
| 259 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 260 | return (*num >= min && *num <= max); |
| 261 | } |
James Simmons | 47ca6ec | 2015-10-21 21:52:40 -0400 | [diff] [blame] | 262 | EXPORT_SYMBOL(cfs_str2num_check); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 263 | |
| 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 Tao | 31fb613 | 2014-03-05 21:27:17 +0800 | [diff] [blame] | 277 | static int |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 278 | cfs_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 Drokin | 15d9f52 | 2016-02-16 00:46:44 -0500 | [diff] [blame] | 285 | if (!re) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 286 | 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 Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 340 | |
| 341 | /** |
Gregoire Pichon | a1dfc93 | 2015-10-21 21:52:39 -0400 | [diff] [blame] | 342 | * 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 | */ |
| 348 | static int |
| 349 | cfs_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 Drokin | 34ec7f8 | 2016-02-16 00:47:13 -0500 | [diff] [blame] | 356 | if (bracketed) { |
| 357 | s[0] = '\0'; |
| 358 | e[0] = '\0'; |
| 359 | } |
Gregoire Pichon | a1dfc93 | 2015-10-21 21:52:39 -0400 | [diff] [blame] | 360 | |
| 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 Drokin | ae0b483 | 2016-02-16 00:47:00 -0500 | [diff] [blame] | 365 | s, expr->re_lo, expr->re_hi, e); |
Gregoire Pichon | a1dfc93 | 2015-10-21 21:52:39 -0400 | [diff] [blame] | 366 | else |
| 367 | i = scnprintf(buffer, count, "%s%u-%u/%u%s", |
Oleg Drokin | ae0b483 | 2016-02-16 00:47:00 -0500 | [diff] [blame] | 368 | s, expr->re_lo, expr->re_hi, expr->re_stride, e); |
Gregoire Pichon | a1dfc93 | 2015-10-21 21:52:39 -0400 | [diff] [blame] | 369 | 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 | */ |
| 379 | int |
| 380 | cfs_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 | } |
| 407 | EXPORT_SYMBOL(cfs_expr_list_print); |
| 408 | |
| 409 | /** |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 410 | * Matches value (\a value) against ranges expression list \a expr_list. |
| 411 | * |
| 412 | * \retval 1 if \a value matches |
| 413 | * \retval 0 otherwise |
| 414 | */ |
| 415 | int |
| 416 | cfs_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 Simmons | 47ca6ec | 2015-10-21 21:52:40 -0400 | [diff] [blame] | 428 | EXPORT_SYMBOL(cfs_expr_list_match); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 429 | |
| 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 | */ |
| 437 | int |
| 438 | cfs_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 Drokin | 15d9f52 | 2016-02-16 00:46:44 -0500 | [diff] [blame] | 462 | if (!val) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 463 | 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 | } |
| 476 | EXPORT_SYMBOL(cfs_expr_list_values); |
| 477 | |
| 478 | /** |
| 479 | * Frees cfs_range_expr structures of \a expr_list. |
| 480 | * |
| 481 | * \retval none |
| 482 | */ |
| 483 | void |
| 484 | cfs_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 Drokin | ae0b483 | 2016-02-16 00:47:00 -0500 | [diff] [blame] | 490 | struct cfs_range_expr, re_link); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 491 | list_del(&expr->re_link); |
| 492 | LIBCFS_FREE(expr, sizeof(*expr)); |
| 493 | } |
| 494 | |
| 495 | LIBCFS_FREE(expr_list, sizeof(*expr_list)); |
| 496 | } |
| 497 | EXPORT_SYMBOL(cfs_expr_list_free); |
| 498 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 499 | /** |
| 500 | * Parses \<cfs_expr_list\> token of the syntax. |
| 501 | * |
Gregoire Pichon | a1dfc93 | 2015-10-21 21:52:39 -0400 | [diff] [blame] | 502 | * \retval 0 if \a str parses to \<number\> | \<expr_list\> |
| 503 | * \retval -errno otherwise |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 504 | */ |
| 505 | int |
| 506 | cfs_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 Drokin | 15d9f52 | 2016-02-16 00:46:44 -0500 | [diff] [blame] | 515 | if (!expr_list) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 516 | 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 Drokin | 15d9f52 | 2016-02-16 00:46:44 -0500 | [diff] [blame] | 529 | while (src.ls_str) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 530 | 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 Drokin | ae0b483 | 2016-02-16 00:47:00 -0500 | [diff] [blame] | 541 | list_add_tail(&expr->re_link, &expr_list->el_exprs); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 542 | } |
| 543 | } else { |
| 544 | rc = cfs_range_expr_parse(&src, min, max, 0, &expr); |
Oleg Drokin | ae0b483 | 2016-02-16 00:47:00 -0500 | [diff] [blame] | 545 | if (rc == 0) |
| 546 | list_add_tail(&expr->re_link, &expr_list->el_exprs); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | if (rc != 0) |
| 550 | cfs_expr_list_free(expr_list); |
| 551 | else |
| 552 | *elpp = expr_list; |
| 553 | |
| 554 | return rc; |
| 555 | } |
| 556 | EXPORT_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 | */ |
| 566 | void |
| 567 | cfs_expr_list_free_list(struct list_head *list) |
| 568 | { |
| 569 | struct cfs_expr_list *el; |
| 570 | |
| 571 | while (!list_empty(list)) { |
Oleg Drokin | ae0b483 | 2016-02-16 00:47:00 -0500 | [diff] [blame] | 572 | el = list_entry(list->next, struct cfs_expr_list, el_link); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 573 | list_del(&el->el_link); |
| 574 | cfs_expr_list_free(el); |
| 575 | } |
| 576 | } |
James Simmons | 47ca6ec | 2015-10-21 21:52:40 -0400 | [diff] [blame] | 577 | EXPORT_SYMBOL(cfs_expr_list_free_list); |