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/param.h>
36 #include <sys/cdefs.h>
37 #include <sys/linker_set.h>
38 #include <sys/queue.h>
39 #include <sys/sysctl.h>
40 #include <sys/condvar.h>
41 #include <sys/kernel.h>
43 #include <sys/mutex.h>
44 #include <sys/systm.h>
47 * Failpoint return codes, used internally.
48 * @ingroup failpoint_private
50 enum fail_point_return_code {
51 FAIL_POINT_RC_CONTINUE = 0, /**< Continue with normal execution */
52 FAIL_POINT_RC_RETURN, /**< FP evaluated to 'return' */
53 FAIL_POINT_RC_QUEUED, /**< sleep_fn will be called */
56 struct fail_point_entry;
57 struct fail_point_setting;
60 * Internal failpoint structure, tracking all the current details of the
61 * failpoint. This structure is the core component shared between the
62 * failure-injection code and the user-interface.
63 * @ingroup failpoint_private
66 const char *fp_name; /* name of fail point */
67 const char *fp_location; /* file:line of fail point */
68 volatile int fp_ref_cnt; /**
69 * protects fp_setting: while holding
70 * a ref, fp_setting points to an
71 * unfreed fail_point_setting
73 struct fail_point_setting * volatile fp_setting;
76 /**< Function to call before sleep or pause */
77 void (*fp_pre_sleep_fn)(void *);
78 /**< Arg for fp_pre_sleep_fn */
79 void *fp_pre_sleep_arg;
81 /**< Function to call after waking from sleep or pause */
82 void (*fp_post_sleep_fn)(void *);
83 /**< Arg for fp_post_sleep_fn */
84 void *fp_post_sleep_arg;
87 #define FAIL_POINT_DYNAMIC_NAME 0x01 /**< Must free name on destroy */
88 /**< Use timeout path for sleep instead of msleep */
89 #define FAIL_POINT_USE_TIMEOUT_PATH 0x02
90 /**< If fail point is set to sleep, replace the sleep call with delay */
91 #define FAIL_POINT_NONSLEEPABLE 0x04
93 #define FAIL_POINT_CV_DESC "fp cv no iterators"
94 #define FAIL_POINT_IS_OFF(fp) (__predict_true((fp)->fp_setting == NULL) || \
95 __predict_true(fail_point_is_off(fp)))
99 /* Private failpoint eval function -- use fail_point_eval() instead. */
100 enum fail_point_return_code fail_point_eval_nontrivial(struct fail_point *,
104 * @addtogroup failpoint
108 * Initialize a fail-point. The name is formed in printf-like fashion
109 * from "fmt" and the subsequent arguments.
110 * Pair with fail_point_destroy().
112 void fail_point_init(struct fail_point *, const char *fmt, ...)
115 /* Return true iff this fail point is set to off, false otherwise */
116 bool fail_point_is_off(struct fail_point *fp);
119 * Set the pre-sleep function for a fail point
120 * If fp_post_sleep_fn is specified, then FAIL_POINT_SLEEP will result in a
121 * (*fp->fp_pre_sleep_fn)(fp->fp_pre_sleep_arg) call by the thread.
124 fail_point_sleep_set_pre_func(struct fail_point *fp, void (*sleep_fn)(void *))
126 fp->fp_pre_sleep_fn = sleep_fn;
130 fail_point_sleep_set_pre_arg(struct fail_point *fp, void *sleep_arg)
132 fp->fp_pre_sleep_arg = sleep_arg;
136 * Set the post-sleep function. This will be passed to timeout if we take
137 * the timeout path. This must be set if you sleep using the timeout path.
140 fail_point_sleep_set_post_func(struct fail_point *fp, void (*sleep_fn)(void *))
142 fp->fp_post_sleep_fn = sleep_fn;
146 fail_point_sleep_set_post_arg(struct fail_point *fp, void *sleep_arg)
148 fp->fp_post_sleep_arg = sleep_arg;
151 * If the FAIL_POINT_USE_TIMEOUT flag is set on a failpoint, then
152 * FAIL_POINT_SLEEP will result in a call to timeout instead of
153 * msleep. Note that if you sleep while this flag is set, you must
154 * set fp_post_sleep_fn or an error will occur upon waking.
157 fail_point_use_timeout_path(struct fail_point *fp, bool use_timeout,
158 void (*post_sleep_fn)(void *))
160 KASSERT(!use_timeout || post_sleep_fn != NULL ||
161 (post_sleep_fn == NULL && fp->fp_post_sleep_fn != NULL),
162 ("Setting fp to use timeout, but not setting post_sleep_fn\n"));
165 fp->fp_flags |= FAIL_POINT_USE_TIMEOUT_PATH;
167 fp->fp_flags &= ~FAIL_POINT_USE_TIMEOUT_PATH;
169 if (post_sleep_fn != NULL)
170 fp->fp_post_sleep_fn = post_sleep_fn;
174 * Free the resources used by a fail-point. Pair with fail_point_init().
176 void fail_point_destroy(struct fail_point *);
179 * Evaluate a failpoint.
181 static inline enum fail_point_return_code
182 fail_point_eval(struct fail_point *fp, int *ret)
184 if (__predict_true(fp->fp_setting == NULL))
185 return (FAIL_POINT_RC_CONTINUE);
186 return (fail_point_eval_nontrivial(fp, ret));
191 /* Declare a fail_point and its sysctl in a function. */
192 #define _FAIL_POINT_NAME(name) _fail_point_##name
193 #define _FAIL_POINT_LOCATION() "(" __FILE__ ":" __XSTRING(__LINE__) ")"
194 #define _FAIL_POINT_INIT(parent, name, flags) \
195 static struct fail_point _FAIL_POINT_NAME(name) = { \
197 .fp_location = _FAIL_POINT_LOCATION(), \
199 .fp_setting = NULL, \
200 .fp_flags = (flags), \
201 .fp_pre_sleep_fn = NULL, \
202 .fp_pre_sleep_arg = NULL, \
203 .fp_post_sleep_fn = NULL, \
204 .fp_post_sleep_arg = NULL, \
206 SYSCTL_OID(parent, OID_AUTO, name, \
207 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, \
208 &_FAIL_POINT_NAME(name), 0, fail_point_sysctl, \
210 SYSCTL_OID(parent, OID_AUTO, status_##name, \
211 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, \
212 &_FAIL_POINT_NAME(name), 0, \
213 fail_point_sysctl_status, "A", "");
214 #define _FAIL_POINT_EVAL(name, cond, code...) \
217 if (__predict_false(cond && \
218 fail_point_eval(&_FAIL_POINT_NAME(name), &RETURN_VALUE))) { \
226 * Instantiate a failpoint which returns "RETURN_VALUE" from the function
228 * @param parent The parent sysctl under which to locate the fp's sysctl
229 * @param name The name of the failpoint in the sysctl tree (and printouts)
230 * @return Instantly returns the RETURN_VALUE specified in the
231 * failpoint, if triggered.
233 #define KFAIL_POINT_RETURN(parent, name) \
234 KFAIL_POINT_CODE(parent, name, return RETURN_VALUE)
237 * Instantiate a failpoint which returns (void) from the function when
239 * @param parent The parent sysctl under which to locate the sysctl
240 * @param name The name of the failpoint in the sysctl tree (and printouts)
241 * @return Instantly returns void, if triggered in the failpoint.
243 #define KFAIL_POINT_RETURN_VOID(parent, name) \
244 KFAIL_POINT_CODE(parent, name, return)
247 * Instantiate a failpoint which sets an error when triggered.
248 * @param parent The parent sysctl under which to locate the sysctl
249 * @param name The name of the failpoint in the sysctl tree (and
251 * @param error_var A variable to set to the failpoint's specified
252 * return-value when triggered
254 #define KFAIL_POINT_ERROR(parent, name, error_var) \
255 KFAIL_POINT_CODE(parent, name, (error_var) = RETURN_VALUE)
258 * Instantiate a failpoint which sets an error and then goes to a
259 * specified label in the function when triggered.
260 * @param parent The parent sysctl under which to locate the sysctl
261 * @param name The name of the failpoint in the sysctl tree (and
263 * @param error_var A variable to set to the failpoint's specified
264 * return-value when triggered
265 * @param label The location to goto when triggered.
267 #define KFAIL_POINT_GOTO(parent, name, error_var, label) \
268 KFAIL_POINT_CODE(parent, name, (error_var) = RETURN_VALUE; goto label)
271 * Instantiate a failpoint which sets its pre- and post-sleep callback
273 * @param parent The parent sysctl under which to locate the sysctl
274 * @param name The name of the failpoint in the sysctl tree (and
276 * @param pre_func Function pointer to the pre-sleep function, which will be
277 * called directly before going to sleep.
278 * @param pre_arg Argument to the pre-sleep function
279 * @param post_func Function pointer to the pot-sleep function, which will be
280 * called directly before going to sleep.
281 * @param post_arg Argument to the post-sleep function
283 #define KFAIL_POINT_SLEEP_CALLBACKS(parent, name, pre_func, pre_arg, \
284 post_func, post_arg) \
285 KFAIL_POINT_CODE_SLEEP_CALLBACKS(parent, name, pre_func, \
286 pre_arg, post_func, post_arg, return RETURN_VALUE)
289 * Instantiate a failpoint which runs arbitrary code when triggered, and sets
290 * its pre- and post-sleep callback mechanisms
291 * @param parent The parent sysctl under which to locate the sysctl
292 * @param name The name of the failpoint in the sysctl tree (and
294 * @param pre_func Function pointer to the pre-sleep function, which will be
295 * called directly before going to sleep.
296 * @param pre_arg Argument to the pre-sleep function
297 * @param post_func Function pointer to the pot-sleep function, which will be
298 * called directly before going to sleep.
299 * @param post_arg Argument to the post-sleep function
300 * @param code The arbitrary code to run when triggered. Can reference
301 * "RETURN_VALUE" if desired to extract the specified
302 * user return-value when triggered. Note that this is
303 * implemented with a do-while loop so be careful of
304 * break and continue statements.
306 #define KFAIL_POINT_CODE_SLEEP_CALLBACKS(parent, name, pre_func, pre_arg, \
307 post_func, post_arg, code...) \
309 _FAIL_POINT_INIT(parent, name) \
310 _FAIL_POINT_NAME(name).fp_pre_sleep_fn = pre_func; \
311 _FAIL_POINT_NAME(name).fp_pre_sleep_arg = pre_arg; \
312 _FAIL_POINT_NAME(name).fp_post_sleep_fn = post_func; \
313 _FAIL_POINT_NAME(name).fp_post_sleep_arg = post_arg; \
314 _FAIL_POINT_EVAL(name, true, code) \
319 * Instantiate a failpoint which runs arbitrary code when triggered.
320 * @param parent The parent sysctl under which to locate the sysctl
321 * @param name The name of the failpoint in the sysctl tree
323 * @param code The arbitrary code to run when triggered. Can reference
324 * "RETURN_VALUE" if desired to extract the specified
325 * user return-value when triggered. Note that this is
326 * implemented with a do-while loop so be careful of
327 * break and continue statements.
329 #define KFAIL_POINT_CODE(parent, name, code...) \
331 _FAIL_POINT_INIT(parent, name, 0) \
332 _FAIL_POINT_EVAL(name, true, code) \
335 #define KFAIL_POINT_CODE_FLAGS(parent, name, flags, code...) \
337 _FAIL_POINT_INIT(parent, name, flags) \
338 _FAIL_POINT_EVAL(name, true, code) \
341 #define KFAIL_POINT_CODE_COND(parent, name, cond, flags, code...) \
343 _FAIL_POINT_INIT(parent, name, flags) \
344 _FAIL_POINT_EVAL(name, cond, code) \
349 * (end group failpoint)
353 int fail_point_sysctl(SYSCTL_HANDLER_ARGS);
354 int fail_point_sysctl_status(SYSCTL_HANDLER_ARGS);
356 /* The fail point sysctl tree. */
357 SYSCTL_DECL(_debug_fail_point);
358 #define DEBUG_FP _debug_fail_point
361 #endif /* _SYS_FAIL_H_ */