don't access dlclose'd dynamic ioengine object after close
[fio.git] / parse.c
1 /*
2  * This file contains the ini and command liner parser main.
3  */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <ctype.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <limits.h>
10 #include <float.h>
11
12 #include "compiler/compiler.h"
13 #include "parse.h"
14 #include "debug.h"
15 #include "log.h"
16 #include "options.h"
17 #include "optgroup.h"
18 #include "minmax.h"
19 #include "lib/ieee754.h"
20 #include "lib/pow2.h"
21
22 #ifdef CONFIG_ARITHMETIC
23 #include "y.tab.h"
24 #endif
25
26 static const char *opt_type_names[] = {
27         "OPT_INVALID",
28         "OPT_STR",
29         "OPT_STR_ULL",
30         "OPT_STR_MULTI",
31         "OPT_STR_VAL",
32         "OPT_STR_VAL_TIME",
33         "OPT_STR_STORE",
34         "OPT_RANGE",
35         "OPT_INT",
36         "OPT_ULL",
37         "OPT_BOOL",
38         "OPT_FLOAT_LIST",
39         "OPT_STR_SET",
40         "OPT_STR_VAL_ZONE",
41         "OPT_DEPRECATED",
42         "OPT_SOFT_DEPRECATED",
43         "OPT_UNSUPPORTED",
44 };
45
46 static const struct fio_option *__fio_options;
47
48 static int vp_cmp(const void *p1, const void *p2)
49 {
50         const struct value_pair *vp1 = p1;
51         const struct value_pair *vp2 = p2;
52
53         return strlen(vp2->ival) - strlen(vp1->ival);
54 }
55
56 static void posval_sort(const struct fio_option *o, struct value_pair *vpmap)
57 {
58         const struct value_pair *vp;
59         int entries;
60
61         memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
62
63         for (entries = 0; entries < PARSE_MAX_VP; entries++) {
64                 vp = &o->posval[entries];
65                 if (!vp->ival || vp->ival[0] == '\0')
66                         break;
67
68                 memcpy(&vpmap[entries], vp, sizeof(*vp));
69         }
70
71         qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
72 }
73
74 static void show_option_range(const struct fio_option *o,
75                               ssize_t (*logger)(const char *format, ...))
76 {
77         if (o->type == FIO_OPT_FLOAT_LIST) {
78                 const char *sep = "";
79                 if (!o->minfp && !o->maxfp)
80                         return;
81
82                 logger("%20s: ", "range");
83                 if (o->minfp != DBL_MIN) {
84                         logger("min=%f", o->minfp);
85                         sep = ", ";
86                 }
87                 if (o->maxfp != DBL_MAX)
88                         logger("%smax=%f", sep, o->maxfp);
89                 logger("\n");
90         } else if (!o->posval[0].ival) {
91                 if (!o->minval && !o->maxval)
92                         return;
93
94                 logger("%20s: min=%d", "range", o->minval);
95                 if (o->maxval)
96                         logger(", max=%d", o->maxval);
97                 logger("\n");
98         }
99 }
100
101 static void show_option_values(const struct fio_option *o)
102 {
103         int i;
104
105         for (i = 0; i < PARSE_MAX_VP; i++) {
106                 const struct value_pair *vp = &o->posval[i];
107
108                 if (!vp->ival)
109                         continue;
110
111                 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
112                 if (vp->help)
113                         log_info(" %s", vp->help);
114                 log_info("\n");
115         }
116
117         if (i)
118                 log_info("\n");
119 }
120
121 static void show_option_help(const struct fio_option *o, int is_err)
122 {
123         const char *typehelp[] = {
124                 [FIO_OPT_INVALID]         = "invalid",
125                 [FIO_OPT_STR]             = "string (opt=bla)",
126                 [FIO_OPT_STR_ULL]         = "string (opt=bla)",
127                 [FIO_OPT_STR_MULTI]       = "string with possible k/m/g postfix (opt=4k)",
128                 [FIO_OPT_STR_VAL]         = "string (opt=bla)",
129                 [FIO_OPT_STR_VAL_TIME]    = "string with time postfix (opt=10s)",
130                 [FIO_OPT_STR_STORE]       = "string (opt=bla)",
131                 [FIO_OPT_RANGE]           = "one to three ranges (opt=1k-4k[,4k-8k[,1k-8k]])",
132                 [FIO_OPT_INT]             = "integer value (opt=100)",
133                 [FIO_OPT_ULL]             = "integer value (opt=100)",
134                 [FIO_OPT_BOOL]            = "boolean value (opt=1)",
135                 [FIO_OPT_FLOAT_LIST]      = "list of floating point values separated by ':' (opt=5.9:7.8)",
136                 [FIO_OPT_STR_SET]         = "empty or boolean value ([0|1])",
137                 [FIO_OPT_DEPRECATED]      = "deprecated",
138                 [FIO_OPT_SOFT_DEPRECATED] = "deprecated",
139                 [FIO_OPT_UNSUPPORTED]     = "unsupported",
140         };
141         ssize_t (*logger)(const char *format, ...);
142
143         if (is_err)
144                 logger = log_err;
145         else
146                 logger = log_info;
147
148         if (o->alias)
149                 logger("%20s: %s\n", "alias", o->alias);
150
151         logger("%20s: %s\n", "type", typehelp[o->type]);
152         logger("%20s: %s\n", "default", o->def ? o->def : "no default");
153         if (o->prof_name)
154                 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
155         show_option_range(o, logger);
156         show_option_values(o);
157 }
158
159 static unsigned long long get_mult_time(const char *str, int len,
160                                         int is_seconds)
161 {
162         const char *p = str;
163         char *c;
164         unsigned long long mult = 1;
165         int i;
166
167         /*
168          * Go forward until we hit a non-digit, or +/- sign
169          */
170         while ((p - str) <= len) {
171                 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
172                         break;
173                 p++;
174         }
175
176         if (!isalpha((int) *p)) {
177                 if (is_seconds)
178                         return 1000000UL;
179                 else
180                         return 1;
181         }
182
183         c = strdup(p);
184         for (i = 0; i < strlen(c); i++)
185                 c[i] = tolower((unsigned char)c[i]);
186
187         if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
188                 mult = 1;
189         else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
190                 mult = 1000;
191         else if (!strcmp("s", c))
192                 mult = 1000000;
193         else if (!strcmp("m", c))
194                 mult = 60 * 1000000UL;
195         else if (!strcmp("h", c))
196                 mult = 60 * 60 * 1000000UL;
197         else if (!strcmp("d", c))
198                 mult = 24 * 60 * 60 * 1000000ULL;
199
200         free(c);
201         return mult;
202 }
203
204 static int is_separator(char c)
205 {
206         switch (c) {
207         case ':':
208         case '-':
209         case ',':
210         case '/':
211                 return 1;
212         default:
213                 return 0;
214         }
215 }
216
217 static unsigned long long __get_mult_bytes(const char *p, void *data,
218                                            int *percent)
219 {
220         unsigned int kb_base = fio_get_kb_base(data);
221         unsigned long long ret = 1;
222         unsigned int i, pow = 0, mult = kb_base;
223         char *c;
224
225         if (!p)
226                 return 1;
227
228         c = strdup(p);
229
230         for (i = 0; i < strlen(c); i++) {
231                 c[i] = tolower((unsigned char)c[i]);
232                 if (is_separator(c[i])) {
233                         c[i] = '\0';
234                         break;
235                 }
236         }
237
238         /* If kb_base is 1000, use true units.
239          * If kb_base is 1024, use opposite units.
240          */
241         if (!strncmp("pib", c, 3)) {
242                 pow = 5;
243                 if (kb_base == 1000)
244                         mult = 1024;
245                 else if (kb_base == 1024)
246                         mult = 1000;
247         } else if (!strncmp("tib", c, 3)) {
248                 pow = 4;
249                 if (kb_base == 1000)
250                         mult = 1024;
251                 else if (kb_base == 1024)
252                         mult = 1000;
253         } else if (!strncmp("gib", c, 3)) {
254                 pow = 3;
255                 if (kb_base == 1000)
256                         mult = 1024;
257                 else if (kb_base == 1024)
258                         mult = 1000;
259         } else if (!strncmp("mib", c, 3)) {
260                 pow = 2;
261                 if (kb_base == 1000)
262                         mult = 1024;
263                 else if (kb_base == 1024)
264                         mult = 1000;
265         } else if (!strncmp("kib", c, 3)) {
266                 pow = 1;
267                 if (kb_base == 1000)
268                         mult = 1024;
269                 else if (kb_base == 1024)
270                         mult = 1000;
271         } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2)) {
272                 pow = 5;
273         } else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2)) {
274                 pow = 4;
275         } else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2)) {
276                 pow = 3;
277         } else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2)) {
278                 pow = 2;
279         } else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2)) {
280                 pow = 1;
281         } else if (!strncmp("%", c, 1)) {
282                 *percent = 1;
283                 free(c);
284                 return ret;
285         }
286
287         while (pow--)
288                 ret *= (unsigned long long) mult;
289
290         free(c);
291         return ret;
292 }
293
294 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
295                                          int *percent)
296 {
297         const char *p = str;
298         int digit_seen = 0;
299
300         if (len < 2)
301                 return __get_mult_bytes(str, data, percent);
302
303         /*
304          * Go forward until we hit a non-digit, or +/- sign
305          */
306         while ((p - str) <= len) {
307                 if (!isdigit((int) *p) &&
308                     (((*p != '+') && (*p != '-')) || digit_seen))
309                         break;
310                 digit_seen |= isdigit((int) *p);
311                 p++;
312         }
313
314         if (!isalpha((int) *p) && (*p != '%'))
315                 p = NULL;
316
317         return __get_mult_bytes(p, data, percent);
318 }
319
320 extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
321                                           double *dval, double implied_units,
322                                           int is_time);
323
324 /*
325  * Convert string into a floating number. Return 1 for success and 0 otherwise.
326  */
327 int str_to_float(const char *str, double *val, int is_time)
328 {
329 #ifdef CONFIG_ARITHMETIC
330         int rc;
331         long long ival;
332         double dval;
333
334         if (str[0] == '(') {
335                 rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0, is_time);
336                 if (!rc) {
337                         *val = dval;
338                         return 1;
339                 }
340         }
341 #endif
342         return 1 == sscanf(str, "%lf", val);
343 }
344
345 /*
346  * convert string into decimal value, noting any size suffix
347  */
348 int str_to_decimal(const char *str, long long *val, int kilo, void *data,
349                    int is_seconds, int is_time)
350 {
351         int len, base;
352         int rc = 1;
353 #ifdef CONFIG_ARITHMETIC
354         long long ival;
355         double dval;
356         double implied_units = 1.0;
357 #endif
358
359         len = strlen(str);
360         if (!len)
361                 return 1;
362
363 #ifdef CONFIG_ARITHMETIC
364         if (is_seconds)
365                 implied_units = 1000000.0;
366         if (str[0] == '(')
367                 rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time);
368         if (str[0] == '(' && !rc) {
369                 if (!kilo && is_seconds)
370                         *val = ival / 1000000LL;
371                 else
372                         *val = ival;
373         }
374 #endif
375
376         if (rc == 1) {
377                 char *endptr;
378
379                 if (strstr(str, "0x") || strstr(str, "0X"))
380                         base = 16;
381                 else
382                         base = 10;
383
384                 *val = strtoll(str, &endptr, base);
385                 if (*val == 0 && endptr == str)
386                         return 1;
387                 if (*val == LONG_MAX && errno == ERANGE)
388                         return 1;
389         }
390
391         if (kilo) {
392                 unsigned long long mult;
393                 int perc = 0;
394
395                 mult = get_mult_bytes(str, len, data, &perc);
396                 if (perc)
397                         *val = -1ULL - *val;
398                 else
399                         *val *= mult;
400         } else
401                 *val *= get_mult_time(str, len, is_seconds);
402
403         return 0;
404 }
405
406 int check_str_bytes(const char *p, long long *val, void *data)
407 {
408         return str_to_decimal(p, val, 1, data, 0, 0);
409 }
410
411 int check_str_time(const char *p, long long *val, int is_seconds)
412 {
413         return str_to_decimal(p, val, 0, NULL, is_seconds, 1);
414 }
415
416 void strip_blank_front(char **p)
417 {
418         char *s = *p;
419
420         if (!strlen(s))
421                 return;
422         while (isspace((int) *s))
423                 s++;
424
425         *p = s;
426 }
427
428 void strip_blank_end(char *p)
429 {
430         char *start = p, *s;
431
432         if (!strlen(p))
433                 return;
434
435         s = strchr(p, ';');
436         if (s)
437                 *s = '\0';
438         s = strchr(p, '#');
439         if (s)
440                 *s = '\0';
441         if (s)
442                 p = s;
443
444         s = p + strlen(p);
445         while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
446                 s--;
447
448         *(s + 1) = '\0';
449 }
450
451 static int check_range_bytes(const char *str, long long *val, void *data)
452 {
453         long long __val;
454
455         if (!str_to_decimal(str, &__val, 1, data, 0, 0)) {
456                 *val = __val;
457                 return 0;
458         }
459
460         return 1;
461 }
462
463 static int check_int(const char *p, int *val)
464 {
465         if (!strlen(p))
466                 return 1;
467         if (strstr(p, "0x") || strstr(p, "0X")) {
468                 if (sscanf(p, "%x", val) == 1)
469                         return 0;
470         } else {
471                 if (sscanf(p, "%u", val) == 1)
472                         return 0;
473         }
474
475         return 1;
476 }
477
478 static size_t opt_len(const char *str)
479 {
480         char *postfix;
481
482         postfix = strchr(str, ':');
483         if (!postfix)
484                 return strlen(str);
485
486         return (int)(postfix - str);
487 }
488
489 static int str_match_len(const struct value_pair *vp, const char *str)
490 {
491         return max(strlen(vp->ival), opt_len(str));
492 }
493
494 #define val_store(ptr, val, off, or, data, o)           \
495         do {                                            \
496                 ptr = td_var((data), (o), (off));       \
497                 if ((or))                               \
498                         *ptr |= (val);                  \
499                 else                                    \
500                         *ptr = (val);                   \
501         } while (0)
502
503 static const char *opt_type_name(const struct fio_option *o)
504 {
505         compiletime_assert(FIO_ARRAY_SIZE(opt_type_names) - 1 == FIO_OPT_UNSUPPORTED,
506                                 "opt_type_names[] index");
507
508         if (o->type <= FIO_OPT_UNSUPPORTED)
509                 return opt_type_names[o->type];
510
511         return "OPT_UNKNOWN?";
512 }
513
514 static bool val_too_large(const struct fio_option *o, unsigned long long val,
515                           bool is_uint)
516 {
517         if (!o->maxval)
518                 return false;
519
520         if (is_uint) {
521                 if ((int) val < 0)
522                         return (int) val > (int) o->maxval;
523                 return (unsigned int) val > o->maxval;
524         }
525
526         return val > o->maxval;
527 }
528
529 static bool val_too_small(const struct fio_option *o, unsigned long long val,
530                           bool is_uint)
531 {
532         if (!o->minval)
533                 return false;
534
535         if (is_uint)
536                 return (int) val < o->minval;
537
538         return val < o->minval;
539 }
540
541 static int __handle_option(const struct fio_option *o, const char *ptr,
542                            void *data, int first, int more, int curr)
543 {
544         int il=0, *ilp;
545         fio_fp64_t *flp;
546         long long ull, *ullp;
547         long ul2;
548         long long ull1, ull2;
549         double uf;
550         char **cp = NULL;
551         int ret = 0, is_time = 0;
552         const struct value_pair *vp;
553         struct value_pair posval[PARSE_MAX_VP];
554         int i, all_skipped = 1;
555
556         dprint(FD_PARSE, "__handle_option=%s, type=%s, ptr=%s\n", o->name,
557                                                         opt_type_name(o), ptr);
558
559         if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
560                 log_err("Option %s requires an argument\n", o->name);
561                 return 1;
562         }
563
564         switch (o->type) {
565         case FIO_OPT_STR:
566         case FIO_OPT_STR_ULL:
567         case FIO_OPT_STR_MULTI: {
568                 fio_opt_str_fn *fn = o->cb;
569
570                 posval_sort(o, posval);
571
572                 ret = 1;
573                 for (i = 0; i < PARSE_MAX_VP; i++) {
574                         vp = &posval[i];
575                         if (!vp->ival || vp->ival[0] == '\0')
576                                 continue;
577                         all_skipped = 0;
578                         if (!ptr)
579                                 break;
580                         if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
581                                 ret = 0;
582                                 if (!o->off1)
583                                         continue;
584                                 if (o->type == FIO_OPT_STR_ULL)
585                                         val_store(ullp, vp->oval, o->off1, vp->orval, data, o);
586                                 else
587                                         val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
588                                 continue;
589                         }
590                 }
591
592                 if (ret && !all_skipped)
593                         show_option_values(o);
594                 else if (fn)
595                         ret = fn(data, ptr);
596                 break;
597         }
598         case FIO_OPT_STR_VAL_TIME:
599                 is_time = 1;
600                 fallthrough;
601         case FIO_OPT_ULL:
602         case FIO_OPT_INT:
603         case FIO_OPT_STR_VAL:
604         case FIO_OPT_STR_VAL_ZONE:
605         {
606                 fio_opt_str_val_fn *fn = o->cb;
607                 char tmp[128], *p;
608                 size_t len = strlen(ptr);
609
610                 if (len > 0 && ptr[len - 1] == 'z') {
611                         if (o->type == FIO_OPT_STR_VAL_ZONE) {
612                                 char *ep;
613                                 unsigned long long val;
614
615                                 errno = 0;
616                                 val = strtoul(ptr, &ep, 10);
617                                 if (errno == 0 && ep != ptr && *ep == 'z') {
618                                         ull = ZONE_BASE_VAL + (uint32_t)val;
619                                         ret = 0;
620                                         goto store_option_value;
621                                 } else {
622                                         log_err("%s: unexpected zone value '%s'\n",
623                                                 o->name, ptr);
624                                         return 1;
625                                 }
626                         } else {
627                                 log_err("%s: 'z' suffix isn't applicable\n",
628                                         o->name);
629                                 return 1;
630                         }
631                 }
632
633                 if (!is_time && o->is_time)
634                         is_time = o->is_time;
635
636                 snprintf(tmp, sizeof(tmp), "%s", ptr);
637                 p = strchr(tmp, ',');
638                 if (p)
639                         *p = '\0';
640
641                 if (is_time)
642                         ret = check_str_time(tmp, &ull, o->is_seconds);
643                 else
644                         ret = check_str_bytes(tmp, &ull, data);
645
646                 dprint(FD_PARSE, "  ret=%d, out=%llu\n", ret, ull);
647
648                 if (ret)
649                         break;
650                 if (o->pow2 && !is_power_of_2(ull)) {
651                         log_err("%s: must be a power-of-2\n", o->name);
652                         return 1;
653                 }
654
655                 if (val_too_large(o, ull, o->type == FIO_OPT_INT)) {
656                         log_err("%s: max value out of range: %llu"
657                                 " (%llu max)\n", o->name, ull, o->maxval);
658                         return 1;
659                 }
660                 if (val_too_small(o, ull, o->type == FIO_OPT_INT)) {
661                         log_err("%s: min value out of range: %lld"
662                                 " (%d min)\n", o->name, ull, o->minval);
663                         return 1;
664                 }
665                 if (o->posval[0].ival) {
666                         posval_sort(o, posval);
667
668                         ret = 1;
669                         for (i = 0; i < PARSE_MAX_VP; i++) {
670                                 vp = &posval[i];
671                                 if (!vp->ival || vp->ival[0] == '\0')
672                                         continue;
673                                 if (vp->oval == ull) {
674                                         ret = 0;
675                                         break;
676                                 }
677                         }
678                         if (ret) {
679                                 log_err("fio: value %llu not allowed:\n", ull);
680                                 show_option_values(o);
681                                 return 1;
682                         }
683                 }
684
685 store_option_value:
686                 if (fn)
687                         ret = fn(data, &ull);
688                 else {
689                         if (o->type == FIO_OPT_INT) {
690                                 if (first)
691                                         val_store(ilp, ull, o->off1, 0, data, o);
692                                 if (curr == 1) {
693                                         if (o->off2)
694                                                 val_store(ilp, ull, o->off2, 0, data, o);
695                                 }
696                                 if (curr == 2) {
697                                         if (o->off3)
698                                                 val_store(ilp, ull, o->off3, 0, data, o);
699                                 }
700                                 if (!more) {
701                                         if (curr < 1) {
702                                                 if (o->off2)
703                                                         val_store(ilp, ull, o->off2, 0, data, o);
704                                         }
705                                         if (curr < 2) {
706                                                 if (o->off3)
707                                                         val_store(ilp, ull, o->off3, 0, data, o);
708                                         }
709                                 }
710                         } else if (o->type == FIO_OPT_ULL) {
711                                 if (first)
712                                         val_store(ullp, ull, o->off1, 0, data, o);
713                                 if (curr == 1) {
714                                         if (o->off2)
715                                                 val_store(ullp, ull, o->off2, 0, data, o);
716                                 }
717                                 if (curr == 2) {
718                                         if (o->off3)
719                                                 val_store(ullp, ull, o->off3, 0, data, o);
720                                 }
721                                 if (!more) {
722                                         if (curr < 1) {
723                                                 if (o->off2)
724                                                         val_store(ullp, ull, o->off2, 0, data, o);
725                                         }
726                                         if (curr < 2) {
727                                                 if (o->off3)
728                                                         val_store(ullp, ull, o->off3, 0, data, o);
729                                         }
730                                 }
731                         } else {
732                                 if (first)
733                                         val_store(ullp, ull, o->off1, 0, data, o);
734                                 if (!more) {
735                                         if (o->off2)
736                                                 val_store(ullp, ull, o->off2, 0, data, o);
737                                 }
738                         }
739                 }
740                 break;
741         }
742         case FIO_OPT_FLOAT_LIST: {
743                 char *cp2;
744
745                 if (first) {
746                         /*
747                         ** Initialize precision to 0 and zero out list
748                         ** in case specified list is shorter than default
749                         */
750                         if (o->off2) {
751                                 ul2 = 0;
752                                 ilp = td_var(data, o, o->off2);
753                                 *ilp = ul2;
754                         }
755
756                         flp = td_var(data, o, o->off1);
757                         for(i = 0; i < o->maxlen; i++)
758                                 flp[i].u.f = 0.0;
759                 }
760                 if (curr >= o->maxlen) {
761                         log_err("the list exceeding max length %d\n",
762                                         o->maxlen);
763                         return 1;
764                 }
765                 if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */
766                         log_err("not a floating point value: %s\n", ptr);
767                         return 1;
768                 }
769                 if (o->minfp || o->maxfp) {
770                         if (uf > o->maxfp) {
771                                 log_err("value out of range: %f"
772                                         " (range max: %f)\n", uf, o->maxfp);
773                                 return 1;
774                         }
775                         if (uf < o->minfp) {
776                                 log_err("value out of range: %f"
777                                         " (range min: %f)\n", uf, o->minfp);
778                                 return 1;
779                         }
780                 }
781
782                 flp = td_var(data, o, o->off1);
783                 flp[curr].u.f = uf;
784
785                 dprint(FD_PARSE, "  out=%f\n", uf);
786
787                 /*
788                 ** Calculate precision for output by counting
789                 ** number of digits after period. Find first
790                 ** period in entire remaining list each time
791                 */
792                 cp2 = strchr(ptr, '.');
793                 if (cp2 != NULL) {
794                         int len = 0;
795
796                         while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
797                                 len++;
798
799                         if (o->off2) {
800                                 ilp = td_var(data, o, o->off2);
801                                 if (len > *ilp)
802                                         *ilp = len;
803                         }
804                 }
805
806                 break;
807         }
808         case FIO_OPT_STR_STORE: {
809                 fio_opt_str_fn *fn = o->cb;
810
811                 if (!strlen(ptr))
812                         return 1;
813
814                 if (o->off1) {
815                         cp = td_var(data, o, o->off1);
816                         *cp = strdup(ptr);
817                         if (strlen(ptr) > o->maxlen - 1) {
818                                 log_err("value exceeds max length of %d\n",
819                                         o->maxlen);
820                                 return 1;
821                         }
822                 }
823
824                 if (fn)
825                         ret = fn(data, ptr);
826                 else if (o->posval[0].ival) {
827                         posval_sort(o, posval);
828
829                         ret = 1;
830                         for (i = 0; i < PARSE_MAX_VP; i++) {
831                                 vp = &posval[i];
832                                 if (!vp->ival || vp->ival[0] == '\0' || !cp)
833                                         continue;
834                                 all_skipped = 0;
835                                 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
836                                         char *rest;
837
838                                         ret = 0;
839                                         if (vp->cb)
840                                                 fn = vp->cb;
841                                         rest = strstr(*cp ?: ptr, ":");
842                                         if (rest) {
843                                                 if (*cp)
844                                                         *rest = '\0';
845                                                 ptr = rest + 1;
846                                         } else
847                                                 ptr = NULL;
848                                         break;
849                                 }
850                         }
851                 }
852
853                 if (!all_skipped) {
854                         if (ret && !*cp)
855                                 show_option_values(o);
856                         else if (ret && *cp)
857                                 ret = 0;
858                         else if (fn && ptr)
859                                 ret = fn(data, ptr);
860                 }
861
862                 break;
863         }
864         case FIO_OPT_RANGE: {
865                 char tmp[128];
866                 char *p1, *p2;
867
868                 snprintf(tmp, sizeof(tmp), "%s", ptr);
869
870                 /* Handle bsrange with separate read,write values: */
871                 p1 = strchr(tmp, ',');
872                 if (p1)
873                         *p1 = '\0';
874
875                 p1 = strchr(tmp, '-');
876                 if (!p1) {
877                         p1 = strchr(tmp, ':');
878                         if (!p1) {
879                                 ret = 1;
880                                 break;
881                         }
882                 }
883
884                 p2 = p1 + 1;
885                 *p1 = '\0';
886                 p1 = tmp;
887
888                 ret = 1;
889                 if (!check_range_bytes(p1, &ull1, data) &&
890                         !check_range_bytes(p2, &ull2, data)) {
891                         ret = 0;
892                         if (ull1 > ull2) {
893                                 unsigned long long foo = ull1;
894
895                                 ull1 = ull2;
896                                 ull2 = foo;
897                         }
898
899                         if (first) {
900                                 val_store(ullp, ull1, o->off1, 0, data, o);
901                                 val_store(ullp, ull2, o->off2, 0, data, o);
902                         }
903                         if (curr == 1) {
904                                 if (o->off3 && o->off4) {
905                                         val_store(ullp, ull1, o->off3, 0, data, o);
906                                         val_store(ullp, ull2, o->off4, 0, data, o);
907                                 }
908                         }
909                         if (curr == 2) {
910                                 if (o->off5 && o->off6) {
911                                         val_store(ullp, ull1, o->off5, 0, data, o);
912                                         val_store(ullp, ull2, o->off6, 0, data, o);
913                                 }
914                         }
915                         if (!more) {
916                                 if (curr < 1) {
917                                         if (o->off3 && o->off4) {
918                                                 val_store(ullp, ull1, o->off3, 0, data, o);
919                                                 val_store(ullp, ull2, o->off4, 0, data, o);
920                                         }
921                                 }
922                                 if (curr < 2) {
923                                         if (o->off5 && o->off6) {
924                                                 val_store(ullp, ull1, o->off5, 0, data, o);
925                                                 val_store(ullp, ull2, o->off6, 0, data, o);
926                                         }
927                                 }
928                         }
929                 }
930
931                 break;
932         }
933         case FIO_OPT_BOOL:
934         case FIO_OPT_STR_SET: {
935                 fio_opt_int_fn *fn = o->cb;
936
937                 if (ptr)
938                         ret = check_int(ptr, &il);
939                 else if (o->type == FIO_OPT_BOOL)
940                         ret = 1;
941                 else
942                         il = 1;
943
944                 dprint(FD_PARSE, "  ret=%d, out=%d\n", ret, il);
945
946                 if (ret)
947                         break;
948
949                 if (o->maxval && il > (int) o->maxval) {
950                         log_err("max value out of range: %d (%llu max)\n",
951                                                                 il, o->maxval);
952                         return 1;
953                 }
954                 if (o->minval && il < o->minval) {
955                         log_err("min value out of range: %d (%d min)\n",
956                                                                 il, o->minval);
957                         return 1;
958                 }
959
960                 if (o->neg)
961                         il = !il;
962
963                 if (fn)
964                         ret = fn(data, &il);
965                 else {
966                         if (first)
967                                 val_store(ilp, il, o->off1, 0, data, o);
968                         if (!more) {
969                                 if (o->off2)
970                                         val_store(ilp, il, o->off2, 0, data, o);
971                         }
972                 }
973                 break;
974         }
975         case FIO_OPT_DEPRECATED:
976                 ret = 1;
977                 fallthrough;
978         case FIO_OPT_SOFT_DEPRECATED:
979                 log_info("Option %s is deprecated\n", o->name);
980                 break;
981         default:
982                 log_err("Bad option type %u\n", o->type);
983                 ret = 1;
984         }
985
986         if (ret)
987                 return ret;
988
989         if (o->verify) {
990                 ret = o->verify(o, data);
991                 if (ret) {
992                         log_err("Correct format for offending option\n");
993                         log_err("%20s: %s\n", o->name, o->help);
994                         show_option_help(o, 1);
995                 }
996         }
997
998         return ret;
999 }
1000
1001 static int handle_option(const struct fio_option *o, const char *__ptr,
1002                          void *data)
1003 {
1004         char *o_ptr, *ptr, *ptr2;
1005         int ret, done;
1006
1007         dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
1008
1009         o_ptr = ptr = NULL;
1010         if (__ptr)
1011                 o_ptr = ptr = strdup(__ptr);
1012
1013         /*
1014          * See if we have another set of parameters, hidden after a comma.
1015          * Do this before parsing this round, to check if we should
1016          * copy set 1 options to set 2.
1017          */
1018         done = 0;
1019         ret = 1;
1020         do {
1021                 int __ret;
1022
1023                 ptr2 = NULL;
1024                 if (ptr &&
1025                     (o->type != FIO_OPT_STR_STORE) &&
1026                     (o->type != FIO_OPT_STR) &&
1027                     (o->type != FIO_OPT_STR_ULL) &&
1028                     (o->type != FIO_OPT_FLOAT_LIST)) {
1029                         ptr2 = strchr(ptr, ',');
1030                         if (ptr2 && *(ptr2 + 1) == '\0')
1031                                 *ptr2 = '\0';
1032                         if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
1033                                 if (!ptr2)
1034                                         ptr2 = strchr(ptr, ':');
1035                                 if (!ptr2)
1036                                         ptr2 = strchr(ptr, '-');
1037                         }
1038                 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
1039                         ptr2 = strchr(ptr, ':');
1040                 }
1041
1042                 /*
1043                  * Don't return early if parsing the first option fails - if
1044                  * we are doing multiple arguments, we can allow the first one
1045                  * being empty.
1046                  */
1047                 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
1048                 if (ret)
1049                         ret = __ret;
1050
1051                 if (!ptr2)
1052                         break;
1053
1054                 ptr = ptr2 + 1;
1055                 done++;
1056         } while (1);
1057
1058         if (o_ptr)
1059                 free(o_ptr);
1060         return ret;
1061 }
1062
1063 struct fio_option *find_option(struct fio_option *options, const char *opt)
1064 {
1065         struct fio_option *o;
1066
1067         for (o = &options[0]; o->name; o++) {
1068                 if (!o_match(o, opt))
1069                         continue;
1070                 if (o->type == FIO_OPT_UNSUPPORTED) {
1071                         log_err("Option <%s>: %s\n", o->name, o->help);
1072                         continue;
1073                 }
1074
1075                 return o;
1076         }
1077
1078         return NULL;
1079 }
1080
1081 const struct fio_option *
1082 find_option_c(const struct fio_option *options, const char *opt)
1083 {
1084         const struct fio_option *o;
1085
1086         for (o = &options[0]; o->name; o++) {
1087                 if (!o_match(o, opt))
1088                         continue;
1089                 if (o->type == FIO_OPT_UNSUPPORTED) {
1090                         log_err("Option <%s>: %s\n", o->name, o->help);
1091                         continue;
1092                 }
1093
1094                 return o;
1095         }
1096
1097         return NULL;
1098 }
1099
1100 static const struct fio_option *
1101 get_option(char *opt, const struct fio_option *options, char **post)
1102 {
1103         const struct fio_option *o;
1104         char *ret;
1105
1106         ret = strchr(opt, '=');
1107         if (ret) {
1108                 *post = ret;
1109                 *ret = '\0';
1110                 ret = opt;
1111                 (*post)++;
1112                 strip_blank_end(ret);
1113                 o = find_option_c(options, ret);
1114         } else {
1115                 o = find_option_c(options, opt);
1116                 *post = NULL;
1117         }
1118
1119         return o;
1120 }
1121
1122 static int opt_cmp(const void *p1, const void *p2)
1123 {
1124         const struct fio_option *o;
1125         char *s, *foo;
1126         int prio1, prio2;
1127
1128         prio1 = prio2 = 0;
1129
1130         if (*(char **)p1) {
1131                 s = strdup(*((char **) p1));
1132                 o = get_option(s, __fio_options, &foo);
1133                 if (o)
1134                         prio1 = o->prio;
1135                 free(s);
1136         }
1137         if (*(char **)p2) {
1138                 s = strdup(*((char **) p2));
1139                 o = get_option(s, __fio_options, &foo);
1140                 if (o)
1141                         prio2 = o->prio;
1142                 free(s);
1143         }
1144
1145         return prio2 - prio1;
1146 }
1147
1148 void sort_options(char **opts, const struct fio_option *options, int num_opts)
1149 {
1150         __fio_options = options;
1151         qsort(opts, num_opts, sizeof(char *), opt_cmp);
1152         __fio_options = NULL;
1153 }
1154
1155 static void add_to_dump_list(const struct fio_option *o,
1156                              struct flist_head *dump_list, const char *post)
1157 {
1158         struct print_option *p;
1159
1160         if (!dump_list)
1161                 return;
1162
1163         p = malloc(sizeof(*p));
1164         p->name = strdup(o->name);
1165         if (post)
1166                 p->value = strdup(post);
1167         else
1168                 p->value = NULL;
1169
1170         flist_add_tail(&p->list, dump_list);
1171 }
1172
1173 int parse_cmd_option(const char *opt, const char *val,
1174                      const struct fio_option *options, void *data,
1175                      struct flist_head *dump_list)
1176 {
1177         const struct fio_option *o;
1178
1179         o = find_option_c(options, opt);
1180         if (!o) {
1181                 log_err("Bad option <%s>\n", opt);
1182                 return 1;
1183         }
1184
1185         if (handle_option(o, val, data)) {
1186                 log_err("fio: failed parsing %s=%s\n", opt, val);
1187                 return 1;
1188         }
1189
1190         add_to_dump_list(o, dump_list, val);
1191         return 0;
1192 }
1193
1194 int parse_option(char *opt, const char *input, const struct fio_option *options,
1195                  const struct fio_option **o, void *data,
1196                  struct flist_head *dump_list)
1197 {
1198         char *post;
1199
1200         if (!opt) {
1201                 log_err("fio: failed parsing %s\n", input);
1202                 *o = NULL;
1203                 return 1;
1204         }
1205
1206         *o = get_option(opt, options, &post);
1207         if (!*o) {
1208                 if (post) {
1209                         int len = strlen(opt);
1210                         if (opt + len + 1 != post)
1211                                 memmove(opt + len + 1, post, strlen(post));
1212                         opt[len] = '=';
1213                 }
1214                 return 1;
1215         }
1216
1217         if (handle_option(*o, post, data)) {
1218                 log_err("fio: failed parsing %s\n", input);
1219                 return 1;
1220         }
1221
1222         add_to_dump_list(*o, dump_list, post);
1223         return 0;
1224 }
1225
1226 /*
1227  * Option match, levenshtein distance. Handy for not quite remembering what
1228  * the option name is.
1229  */
1230 int string_distance(const char *s1, const char *s2)
1231 {
1232         unsigned int s1_len = strlen(s1);
1233         unsigned int s2_len = strlen(s2);
1234         unsigned int *p, *q, *r;
1235         unsigned int i, j;
1236
1237         p = malloc(sizeof(unsigned int) * (s2_len + 1));
1238         q = malloc(sizeof(unsigned int) * (s2_len + 1));
1239
1240         p[0] = 0;
1241         for (i = 1; i <= s2_len; i++)
1242                 p[i] = p[i - 1] + 1;
1243
1244         for (i = 1; i <= s1_len; i++) {
1245                 q[0] = p[0] + 1;
1246                 for (j = 1; j <= s2_len; j++) {
1247                         unsigned int sub = p[j - 1];
1248                         unsigned int pmin;
1249
1250                         if (s1[i - 1] != s2[j - 1])
1251                                 sub++;
1252
1253                         pmin = min(q[j - 1] + 1, sub);
1254                         q[j] = min(p[j] + 1, pmin);
1255                 }
1256                 r = p;
1257                 p = q;
1258                 q = r;
1259         }
1260
1261         i = p[s2_len];
1262         free(p);
1263         free(q);
1264         return i;
1265 }
1266
1267 /*
1268  * Make a guess of whether the distance from 's1' is significant enough
1269  * to warrant printing the guess. We set this to a 1/2 match.
1270  */
1271 int string_distance_ok(const char *opt, int distance)
1272 {
1273         size_t len;
1274
1275         len = strlen(opt);
1276         len = (len + 1) / 2;
1277         return distance <= len;
1278 }
1279
1280 static const struct fio_option *find_child(const struct fio_option *options,
1281                                            const struct fio_option *o)
1282 {
1283         const struct fio_option *__o;
1284
1285         for (__o = options + 1; __o->name; __o++)
1286                 if (__o->parent && !strcmp(__o->parent, o->name))
1287                         return __o;
1288
1289         return NULL;
1290 }
1291
1292 static void __print_option(const struct fio_option *o,
1293                            const struct fio_option *org,
1294                            int level)
1295 {
1296         char name[256], *p;
1297         int depth;
1298
1299         if (!o)
1300                 return;
1301
1302         p = name;
1303         depth = level;
1304         while (depth--)
1305                 p += sprintf(p, "%s", "  ");
1306
1307         sprintf(p, "%s", o->name);
1308
1309         log_info("%-24s: %s\n", name, o->help);
1310 }
1311
1312 static void print_option(const struct fio_option *o)
1313 {
1314         const struct fio_option *parent;
1315         const struct fio_option *__o;
1316         unsigned int printed;
1317         unsigned int level;
1318
1319         __print_option(o, NULL, 0);
1320         parent = o;
1321         level = 0;
1322         do {
1323                 level++;
1324                 printed = 0;
1325
1326                 while ((__o = find_child(o, parent)) != NULL) {
1327                         __print_option(__o, o, level);
1328                         o = __o;
1329                         printed++;
1330                 }
1331
1332                 parent = o;
1333         } while (printed);
1334 }
1335
1336 int show_cmd_help(const struct fio_option *options, const char *name)
1337 {
1338         const struct fio_option *o, *closest;
1339         unsigned int best_dist = -1U;
1340         int found = 0;
1341         int show_all = 0;
1342
1343         if (!name || !strcmp(name, "all"))
1344                 show_all = 1;
1345
1346         closest = NULL;
1347         best_dist = -1;
1348         for (o = &options[0]; o->name; o++) {
1349                 int match = 0;
1350
1351                 if (o->type == FIO_OPT_DEPRECATED ||
1352                     o->type == FIO_OPT_SOFT_DEPRECATED)
1353                         continue;
1354                 if (!exec_profile && o->prof_name)
1355                         continue;
1356                 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1357                         continue;
1358
1359                 if (name) {
1360                         if (!strcmp(name, o->name) ||
1361                             (o->alias && !strcmp(name, o->alias)))
1362                                 match = 1;
1363                         else {
1364                                 unsigned int dist;
1365
1366                                 dist = string_distance(name, o->name);
1367                                 if (dist < best_dist) {
1368                                         best_dist = dist;
1369                                         closest = o;
1370                                 }
1371                         }
1372                 }
1373
1374                 if (show_all || match) {
1375                         found = 1;
1376                         if (match)
1377                                 log_info("%20s: %s\n", o->name, o->help);
1378                         if (show_all) {
1379                                 if (!o->parent)
1380                                         print_option(o);
1381                                 continue;
1382                         }
1383                 }
1384
1385                 if (!match)
1386                         continue;
1387
1388                 show_option_help(o, 0);
1389         }
1390
1391         if (found)
1392                 return 0;
1393
1394         log_err("No such command: %s", name);
1395
1396         /*
1397          * Only print an appropriately close option, one where the edit
1398          * distance isn't too big. Otherwise we get crazy matches.
1399          */
1400         if (closest && best_dist < 3) {
1401                 log_info(" - showing closest match\n");
1402                 log_info("%20s: %s\n", closest->name, closest->help);
1403                 show_option_help(closest, 0);
1404         } else
1405                 log_info("\n");
1406
1407         return 1;
1408 }
1409
1410 /*
1411  * Handle parsing of default parameters.
1412  */
1413 void fill_default_options(void *data, const struct fio_option *options)
1414 {
1415         const struct fio_option *o;
1416
1417         dprint(FD_PARSE, "filling default options\n");
1418
1419         for (o = &options[0]; o->name; o++)
1420                 if (o->def)
1421                         handle_option(o, o->def, data);
1422 }
1423
1424 static void option_init(struct fio_option *o)
1425 {
1426         if (o->type == FIO_OPT_DEPRECATED || o->type == FIO_OPT_UNSUPPORTED ||
1427             o->type == FIO_OPT_SOFT_DEPRECATED)
1428                 return;
1429         if (o->name && !o->lname)
1430                 log_err("Option %s: missing long option name\n", o->name);
1431         if (o->type == FIO_OPT_BOOL) {
1432                 o->minval = 0;
1433                 o->maxval = 1;
1434         }
1435         if (o->type == FIO_OPT_INT) {
1436                 if (!o->maxval)
1437                         o->maxval = UINT_MAX;
1438         }
1439         if (o->type == FIO_OPT_ULL) {
1440                 if (!o->maxval)
1441                         o->maxval = ULLONG_MAX;
1442         }
1443         if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
1444                 log_err("Option %s: string set option with"
1445                                 " default will always be true\n", o->name);
1446         }
1447         if (!o->cb && !o->off1)
1448                 log_err("Option %s: neither cb nor offset given\n", o->name);
1449         if (!o->category) {
1450                 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1451                 o->category = FIO_OPT_C_GENERAL;
1452                 o->group = FIO_OPT_G_INVALID;
1453         }
1454 }
1455
1456 /*
1457  * Sanitize the options structure. For now it just sets min/max for bool
1458  * values and whether both callback and offsets are given.
1459  */
1460 void options_init(struct fio_option *options)
1461 {
1462         struct fio_option *o;
1463
1464         dprint(FD_PARSE, "init options\n");
1465
1466         for (o = &options[0]; o->name; o++) {
1467                 option_init(o);
1468                 if (o->inverse)
1469                         o->inv_opt = find_option(options, o->inverse);
1470         }
1471 }
1472
1473 void options_mem_dupe(const struct fio_option *options, void *data)
1474 {
1475         const struct fio_option *o;
1476         char **ptr;
1477
1478         dprint(FD_PARSE, "dup options\n");
1479
1480         for (o = &options[0]; o->name; o++) {
1481                 if (o->type != FIO_OPT_STR_STORE)
1482                         continue;
1483
1484                 ptr = td_var(data, o, o->off1);
1485                 if (*ptr)
1486                         *ptr = strdup(*ptr);
1487         }
1488 }
1489
1490 void options_free(const struct fio_option *options, void *data)
1491 {
1492         const struct fio_option *o;
1493         char **ptr;
1494
1495         dprint(FD_PARSE, "free options\n");
1496
1497         for (o = &options[0]; o->name; o++) {
1498                 if (o->type != FIO_OPT_STR_STORE || !o->off1 || o->no_free)
1499                         continue;
1500
1501                 ptr = td_var(data, o, o->off1);
1502                 if (*ptr) {
1503                         free(*ptr);
1504                         *ptr = NULL;
1505                 }
1506         }
1507 }