2 * Copyright (c) 2009 Isilon Inc http://www.isilon.com/
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * @ingroup failpoint_private
33 * @defgroup failpoint fail(9) Facility
35 * Failpoints allow for injecting fake errors into running code on the fly,
36 * without modifying code or recompiling with flags. Failpoints are always
37 * present, and are very efficient when disabled. Failpoints are described
41 * @defgroup failpoint_private Private fail(9) Implementation functions
43 * Private implementations for the actual failpoint code.
48 * @addtogroup failpoint_private
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
55 #include <sys/errno.h>
57 #include <sys/kernel.h>
58 #include <sys/libkern.h>
60 #include <sys/malloc.h>
61 #include <sys/mutex.h>
64 #include <machine/stdarg.h>
66 #ifdef ILOG_DEFINE_FOR_FILE
67 ILOG_DEFINE_FOR_FILE(L_ISI_FAIL_POINT, L_ILOG, fail_point);
70 MALLOC_DEFINE(M_FAIL_POINT, "Fail Points", "fail points system");
71 #define fp_free(ptr) free(ptr, M_FAIL_POINT)
72 #define fp_malloc(size, flags) malloc((size), M_FAIL_POINT, (flags))
74 static struct mtx g_fp_mtx;
75 MTX_SYSINIT(g_fp_mtx, &g_fp_mtx, "fail point mtx", MTX_DEF);
76 #define FP_LOCK() mtx_lock(&g_fp_mtx)
77 #define FP_UNLOCK() mtx_unlock(&g_fp_mtx)
81 * Don't change these without changing fail_type_strings in fail.c.
82 * @ingroup failpoint_private
85 FAIL_POINT_OFF, /**< don't fail */
86 FAIL_POINT_PANIC, /**< panic */
87 FAIL_POINT_RETURN, /**< return an errorcode */
88 FAIL_POINT_BREAK, /**< break into the debugger */
89 FAIL_POINT_PRINT, /**< print a message */
90 FAIL_POINT_SLEEP, /**< sleep for some msecs */
91 FAIL_POINT_INVALID, /**< placeholder */
94 static const char *fail_type_strings[] = {
104 * Internal structure tracking a single term of a complete failpoint.
105 * @ingroup failpoint_private
107 struct fail_point_entry {
108 enum fail_point_t fe_type; /**< type of entry */
109 int fe_arg; /**< argument to type (e.g. return value) */
110 int fe_prob; /**< likelihood of firing in millionths */
111 int fe_count; /**< number of times to fire, 0 means always */
113 TAILQ_ENTRY(fail_point_entry) fe_entries; /**< next entry in fail point */
117 fail_point_sleep(struct fail_point *fp, struct fail_point_entry *ent,
118 int msecs, enum fail_point_return_code *pret)
120 /* convert from millisecs to ticks, rounding up */
121 int timo = ((msecs * hz) + 999) / 1000;
124 if (fp->fp_sleep_fn == NULL) {
125 msleep(fp, &g_fp_mtx, PWAIT, "failpt", timo);
127 timeout(fp->fp_sleep_fn, fp->fp_sleep_arg, timo);
128 *pret = FAIL_POINT_RC_QUEUED;
135 * Defines stating the equivalent of probablilty one (100%)
138 PROB_MAX = 1000000, /* probability between zero and this number */
139 PROB_DIGITS = 6, /* number of zero's in above number */
142 static char *parse_fail_point(struct fail_point_entries *, char *);
143 static char *parse_term(struct fail_point_entries *, char *);
144 static char *parse_number(int *out_units, int *out_decimal, char *);
145 static char *parse_type(struct fail_point_entry *, char *);
146 static void free_entry(struct fail_point_entries *, struct fail_point_entry *);
147 static void clear_entries(struct fail_point_entries *);
150 * Initialize a fail_point. The name is formed in a printf-like fashion
151 * from "fmt" and subsequent arguments. This function is generally used
152 * for custom failpoints located at odd places in the sysctl tree, and is
153 * not explicitly needed for standard in-line-declared failpoints.
158 fail_point_init(struct fail_point *fp, const char *fmt, ...)
164 TAILQ_INIT(&fp->fp_entries);
167 /* Figure out the size of the name. */
169 n = vsnprintf(NULL, 0, fmt, ap);
172 /* Allocate the name and fill it in. */
173 name = fp_malloc(n + 1, M_WAITOK);
176 vsnprintf(name, n + 1, fmt, ap);
180 fp->fp_flags |= FAIL_POINT_DYNAMIC_NAME;
181 fp->fp_sleep_fn = NULL;
182 fp->fp_sleep_arg = NULL;
186 * Free the resources held by a fail_point.
191 fail_point_destroy(struct fail_point *fp)
193 struct fail_point_entry *ent;
195 if (fp->fp_flags & FAIL_POINT_DYNAMIC_NAME && fp->fp_name != NULL) {
196 fp_free((void *)(intptr_t)fp->fp_name);
201 while (!TAILQ_EMPTY(&fp->fp_entries)) {
202 ent = TAILQ_FIRST(&fp->fp_entries);
203 TAILQ_REMOVE(&fp->fp_entries, ent, fe_entries);
209 * This does the real work of evaluating a fail point. If the fail point tells
210 * us to return a value, this function returns 1 and fills in 'return_value'
211 * (return_value is allowed to be null). If the fail point tells us to panic,
212 * we never return. Otherwise we just return 0 after doing some work, which
213 * means "keep going".
215 enum fail_point_return_code
216 fail_point_eval_nontrivial(struct fail_point *fp, int *return_value)
218 enum fail_point_return_code ret = FAIL_POINT_RC_CONTINUE;
219 struct fail_point_entry *ent, *next;
224 ent = TAILQ_FIRST(&fp->fp_entries);
226 int cont = 0; /* don't continue by default */
227 next = TAILQ_NEXT(ent, fe_entries);
229 if (ent->fe_prob < PROB_MAX &&
230 ent->fe_prob < random() % PROB_MAX) {
235 switch (ent->fe_type) {
236 case FAIL_POINT_PANIC:
237 panic("fail point %s panicking", fp->fp_name);
240 case FAIL_POINT_RETURN:
242 *return_value = ent->fe_arg;
243 ret = FAIL_POINT_RC_RETURN;
246 case FAIL_POINT_BREAK:
247 printf("fail point %s breaking to debugger\n", fp->fp_name);
251 case FAIL_POINT_PRINT:
252 printf("fail point %s executing\n", fp->fp_name);
256 case FAIL_POINT_SLEEP:
258 * Free the entry now if necessary, since
259 * we're about to drop the mutex and sleep.
262 if (ent->fe_count > 0 && --ent->fe_count == 0) {
263 free_entry(&fp->fp_entries, ent);
268 fail_point_sleep(fp, ent, msecs, &ret);
275 if (ent && ent->fe_count > 0 && --ent->fe_count == 0)
276 free_entry(&fp->fp_entries, ent);
285 /* Get rid of "off"s at the end. */
286 while ((ent = TAILQ_LAST(&fp->fp_entries, fail_point_entries)) &&
287 ent->fe_type == FAIL_POINT_OFF)
288 free_entry(&fp->fp_entries, ent);
296 * Translate internal fail_point structure into human-readable text.
299 fail_point_get(struct fail_point *fp, struct sbuf *sb)
301 struct fail_point_entry *ent;
305 TAILQ_FOREACH(ent, &fp->fp_entries, fe_entries) {
306 if (ent->fe_prob < PROB_MAX) {
307 int decimal = ent->fe_prob % (PROB_MAX / 100);
308 int units = ent->fe_prob / (PROB_MAX / 100);
309 sbuf_printf(sb, "%d", units);
311 int digits = PROB_DIGITS - 2;
312 while (!(decimal % 10)) {
316 sbuf_printf(sb, ".%0*d", digits, decimal);
318 sbuf_printf(sb, "%%");
320 if (ent->fe_count > 0)
321 sbuf_printf(sb, "%d*", ent->fe_count);
322 sbuf_printf(sb, "%s", fail_type_strings[ent->fe_type]);
324 sbuf_printf(sb, "(%d)", ent->fe_arg);
325 if (TAILQ_NEXT(ent, fe_entries))
326 sbuf_printf(sb, "->");
328 if (TAILQ_EMPTY(&fp->fp_entries))
329 sbuf_printf(sb, "off");
335 * Set an internal fail_point structure from a human-readable failpoint string
336 * in a lock-safe manner.
339 fail_point_set(struct fail_point *fp, char *buf)
342 struct fail_point_entry *ent, *ent_next;
343 struct fail_point_entries new_entries;
345 /* Parse new entries. */
346 TAILQ_INIT(&new_entries);
347 if (!parse_fail_point(&new_entries, buf)) {
348 clear_entries(&new_entries);
355 /* Move new entries in. */
356 TAILQ_SWAP(&fp->fp_entries, &new_entries, fail_point_entry, fe_entries);
357 clear_entries(&new_entries);
359 /* Get rid of useless zero probability entries. */
360 TAILQ_FOREACH_SAFE(ent, &fp->fp_entries, fe_entries, ent_next) {
361 if (ent->fe_prob == 0)
362 free_entry(&fp->fp_entries, ent);
365 /* Get rid of "off"s at the end. */
366 while ((ent = TAILQ_LAST(&fp->fp_entries, fail_point_entries)) &&
367 ent->fe_type == FAIL_POINT_OFF)
368 free_entry(&fp->fp_entries, ent);
375 IWARNING("Failed to set %s (%s) to %s",
376 fp->fp_name, fp->fp_location, buf);
378 INOTICE("Set %s (%s) to %s",
379 fp->fp_name, fp->fp_location, buf);
380 #endif /* IWARNING */
385 #define MAX_FAIL_POINT_BUF 1023
388 * Handle kernel failpoint set/get.
391 fail_point_sysctl(SYSCTL_HANDLER_ARGS)
393 struct fail_point *fp = arg1;
399 sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
400 fail_point_get(fp, &sb);
403 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
407 if (!error && req->newptr) {
408 if (req->newlen > MAX_FAIL_POINT_BUF) {
413 buf = fp_malloc(req->newlen + 1, M_WAITOK);
415 error = SYSCTL_IN(req, buf, req->newlen);
418 buf[req->newlen] = '\0';
420 error = fail_point_set(fp, buf);
430 * Internal helper function to translate a human-readable failpoint string
431 * into a internally-parsable fail_point structure.
434 parse_fail_point(struct fail_point_entries *ents, char *p)
437 * <term> ( "->" <term> )*
439 if (!(p = parse_term(ents, p)))
442 if (p[0] != '-' || p[1] != '>' || !(p = parse_term(ents, p+2)))
448 * Internal helper function to parse an individual term from a failpoint.
451 parse_term(struct fail_point_entries *ents, char *p)
453 struct fail_point_entry *ent;
455 ent = fp_malloc(sizeof *ent, M_WAITOK | M_ZERO);
456 ent->fe_prob = PROB_MAX;
457 TAILQ_INSERT_TAIL(ents, ent, fe_entries);
461 * ( (<float> "%") | (<integer> "*" ) )*
463 * [ "(" <integer> ")" ]
466 /* ( (<float> "%") | (<integer> "*" ) )* */
467 while (('0' <= *p && *p <= '9') || *p == '.') {
470 if (!(p = parse_number(&units, &decimal, p)))
474 if (units > 100) /* prevent overflow early */
476 ent->fe_prob = units * (PROB_MAX / 100) + decimal;
477 if (ent->fe_prob > PROB_MAX)
478 ent->fe_prob = PROB_MAX;
480 } else if (*p == '*') {
481 if (!units || decimal)
483 ent->fe_count = units;
493 if (!(p = parse_type(ent, p)))
498 /* [ "(" <integer> ")" ] */
502 if (('0' <= *p && *p <= '9') || *p == '-')
503 ent->fe_arg = strtol(p, &p, 0);
513 * Internal helper function to parse a numeric for a failpoint term.
516 parse_number(int *out_units, int *out_decimal, char *p)
522 * <integer> [ "." <integer> ] |
528 *out_units = strtol(p, &p, 10);
529 if (p == old_p && *p != '.')
532 /* fractional part */
537 while ('0' <= *p && *p <= '9') {
538 int digit = *p - '0';
539 if (digits < PROB_DIGITS - 2)
540 *out_decimal = *out_decimal * 10 + digit;
541 else if (digits == PROB_DIGITS - 2 && digit >= 5)
546 if (!digits) /* need at least one digit after '.' */
548 while (digits++ < PROB_DIGITS - 2) /* add implicit zeros */
552 return p; /* success */
556 * Internal helper function to parse an individual type for a failpoint term.
559 parse_type(struct fail_point_entry *ent, char *beg)
561 enum fail_point_t type;
563 while ('a' <= *end && *end <= 'z')
567 for (type = FAIL_POINT_OFF; type != FAIL_POINT_INVALID; type++) {
568 const char *p = fail_type_strings[type];
570 while (q < end && *p++ == *q++);
571 if (q == end && *p == '\0') {
580 * Internal helper function to free an individual failpoint term.
583 free_entry(struct fail_point_entries *ents, struct fail_point_entry *ent)
585 TAILQ_REMOVE(ents, ent, fe_entries);
590 * Internal helper function to clear out all failpoint terms for a single
594 clear_entries(struct fail_point_entries *ents)
596 struct fail_point_entry *ent, *ent_next;
597 TAILQ_FOREACH_SAFE(ent, ents, fe_entries, ent_next)
602 /* The fail point sysctl tree. */
603 SYSCTL_NODE(_debug, OID_AUTO, fail_point, CTLFLAG_RW, 0, "fail points");