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 * Main header for failpoint facility.
35 #include <sys/types.h>
37 #include <sys/linker_set.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/sysctl.h>
45 * Don't change these without changing fail_type_strings in fail.c.
46 * @ingroup failpoint_private
49 FAIL_POINT_OFF, /**< don't fail */
50 FAIL_POINT_PANIC, /**< panic */
51 FAIL_POINT_RETURN, /**< return an errorcode */
52 FAIL_POINT_BREAK, /**< break into the debugger */
53 FAIL_POINT_PRINT, /**< print a message */
54 FAIL_POINT_SLEEP, /**< sleep for some msecs */
55 FAIL_POINT_INVALID, /**< placeholder */
59 * Failpoint return codes, used internally.
60 * @ingroup failpoint_private
62 enum fail_point_return_code {
63 FAIL_POINT_RC_CONTINUE = 0, /**< Continue with normal execution */
64 FAIL_POINT_RC_RETURN, /**< FP evaluated to 'return' */
65 FAIL_POINT_RC_QUEUED, /**< sleep_fn will be called */
68 TAILQ_HEAD(fail_point_entries, fail_point_entry);
70 * Internal failpoint structure, tracking all the current details of the
71 * failpoint. This structure is the core component shared between the
72 * failure-injection code and the user-interface.
73 * @ingroup failpoint_private
76 const char *fp_name; /**< name of fail point */
77 const char *fp_location; /**< file:line of fail point */
78 struct fail_point_entries fp_entries; /**< list of entries */
80 void (*fp_sleep_fn)(void *); /**< Function to call at end of
81 * sleep for sleep failpoints */
82 void *fp_sleep_arg; /**< Arg for sleep_fn */
85 #define FAIL_POINT_DYNAMIC_NAME 0x01 /**< Must free name on destroy */
88 * Internal structure tracking a single term of a complete failpoint.
89 * @ingroup failpoint_private
91 struct fail_point_entry {
92 enum fail_point_t fe_type; /**< type of entry */
93 int fe_arg; /**< argument to type (e.g. return value) */
94 int fe_prob; /**< likelihood of firing in millionths */
95 int fe_count; /**< number of times to fire, 0 means always */
97 TAILQ_ENTRY(fail_point_entry) fe_entries; /**< next entry in fail point */
100 /* Private failpoint eval function -- use fail_point_eval() instead. */
101 enum fail_point_return_code fail_point_eval_nontrivial(struct fail_point *,
105 * @addtogroup failpoint
109 * Initialize a fail-point. The name is formed in printf-like fashion
110 * from "fmt" and the subsequent arguments.
111 * Pair with fail_point_destroy().
113 void fail_point_init(struct fail_point *, const char *fmt, ...)
117 * Set the sleep function for a fail point
118 * If sleep_fn is specified, then FAIL_POINT_SLEEP will result in a
119 * (*fp->sleep_fn)(fp->sleep_arg) call by the timer thread. Otherwise,
120 * if sleep_fn is NULL (default), then FAIL_POINT_SLEEP will result in the
121 * fail_point_eval() call sleeping.
124 fail_point_sleep_set_func(struct fail_point *fp, void (*sleep_fn)(void *))
126 fp->fp_sleep_fn = sleep_fn;
130 * Set the argument for the sleep function for a fail point
133 fail_point_sleep_set_arg(struct fail_point *fp, void *sleep_arg)
135 fp->fp_sleep_arg = sleep_arg;
139 * Free the resources used by a fail-point. Pair with fail_point_init().
141 void fail_point_destroy(struct fail_point *);
144 * Evaluate a failpoint.
146 static __inline enum fail_point_return_code
147 fail_point_eval(struct fail_point *fp, int *ret)
149 if (TAILQ_EMPTY(&fp->fp_entries)) {
150 return (FAIL_POINT_RC_CONTINUE);
152 return (fail_point_eval_nontrivial(fp, ret));
155 /* Declare a fail_point and its sysctl in a function. */
156 #define _FAIL_POINT_NAME(name) _fail_point_##name
157 #define _STRINGIFY_HELPER(x) #x
158 #define _STRINGIFY(x) _STRINGIFY_HELPER(x)
159 #define _FAIL_POINT_LOCATION() __FILE__ ":" _STRINGIFY(__LINE__)
162 * Instantiate a failpoint which returns "value" from the function when triggered.
163 * @param parent The parent sysctl under which to locate the sysctl
164 * @param name The name of the failpoint in the sysctl tree (and printouts)
165 * @return Instantly returns the return("value") specified in the
166 * failpoint, if triggered.
168 #define KFAIL_POINT_RETURN(parent, name) \
169 KFAIL_POINT_CODE(parent, name, return RETURN_VALUE)
172 * Instantiate a failpoint which returns (void) from the function when triggered.
173 * @param parent The parent sysctl under which to locate the sysctl
174 * @param name The name of the failpoint in the sysctl tree (and printouts)
175 * @return Instantly returns void, if triggered in the failpoint.
177 #define KFAIL_POINT_RETURN_VOID(parent, name) \
178 KFAIL_POINT_CODE(parent, name, return)
181 * Instantiate a failpoint which sets an error when triggered.
182 * @param parent The parent sysctl under which to locate the sysctl
183 * @param name The name of the failpoint in the sysctl tree (and printouts)
184 * @param error_var A variable to set to the failpoint's specified
185 * return-value when triggered
187 #define KFAIL_POINT_ERROR(parent, name, error_var) \
188 KFAIL_POINT_CODE(parent, name, (error_var) = RETURN_VALUE)
191 * Instantiate a failpoint which sets an error and then goes to a
192 * specified label in the function when triggered.
193 * @param parent The parent sysctl under which to locate the sysctl
194 * @param name The name of the failpoint in the sysctl tree (and printouts)
195 * @param error_var A variable to set to the failpoint's specified
196 * return-value when triggered
197 * @param label The location to goto when triggered.
199 #define KFAIL_POINT_GOTO(parent, name, error_var, label) \
200 KFAIL_POINT_CODE(parent, name, (error_var) = RETURN_VALUE; goto label)
203 * Instantiate a failpoint which runs arbitrary code when triggered.
204 * @param parent The parent sysctl under which to locate the sysctl
205 * @param name The name of the failpoint in the sysctl tree (and printouts)
206 * @param code The arbitrary code to run when triggered. Can reference
207 * "RETURN_VALUE" if desired to extract the specified user
208 * return-value when triggered
210 #define KFAIL_POINT_CODE(parent, name, code) \
211 KFAIL_POINT_START(parent, name) { \
217 * (end group failpoint)
221 * Internal macro to implement above #defines -- should not be used directly.
222 * @ingroup failpoint_private
224 #define KFAIL_POINT_START(parent, name) \
227 static struct fail_point _FAIL_POINT_NAME(name) = { \
229 _FAIL_POINT_LOCATION(), \
230 TAILQ_HEAD_INITIALIZER( \
231 _FAIL_POINT_NAME(name).fp_entries), \
235 SYSCTL_OID(parent, OID_AUTO, name, \
236 CTLTYPE_STRING | CTLFLAG_RW, \
237 &_FAIL_POINT_NAME(name), 0, fail_point_sysctl, \
240 if (__predict_false( \
241 fail_point_eval(&_FAIL_POINT_NAME(name), \
245 * Internal macro to implement above #defines -- should not be used directly.
246 * @ingroup failpoint_private
248 #define FAIL_POINT_END \
253 int fail_point_sysctl(SYSCTL_HANDLER_ARGS);
255 /* The fail point sysctl tree. */
256 SYSCTL_DECL(_debug_fail_point);
258 #define DEBUG_FP _debug_fail_point
260 #endif /* _SYS_FAIL_H_ */