1/*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2009-2019 Dell EMC Isilon http://www.isilon.com/ 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27/** 28 * @file 29 * 30 * Main header for failpoint facility. 31 */ 32#ifndef _SYS_FAIL_H_ 33#define _SYS_FAIL_H_ 34 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> 42#include <sys/lock.h> 43#include <sys/mutex.h> 44#include <sys/systm.h> 45 46/** 47 * Failpoint return codes, used internally. 48 * @ingroup failpoint_private 49 */ 50enum 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 */ 54}; 55 56struct fail_point_entry; 57struct fail_point_setting; 58 59/** 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 64 */ 65struct fail_point { 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 72 */ 73 struct fail_point_setting * volatile fp_setting; 74 int fp_flags; 75 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; 80 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; 85 86 struct callout *fp_callout; 87}; 88 89#define FAIL_POINT_DYNAMIC_NAME 0x01 /**< Must free name on destroy */ 90/**< Use timeout path for sleep instead of msleep */ 91#define FAIL_POINT_USE_TIMEOUT_PATH 0x02 92/**< If fail point is set to sleep, replace the sleep call with delay */ 93#define FAIL_POINT_NONSLEEPABLE 0x04 94 95#define FAIL_POINT_CV_DESC "fp cv no iterators" 96#define FAIL_POINT_IS_OFF(fp) (__predict_true((fp)->fp_setting == NULL) || \ 97 __predict_true(fail_point_is_off(fp))) 98 99__BEGIN_DECLS 100 101/* Private failpoint eval function -- use fail_point_eval() instead. */ 102enum fail_point_return_code fail_point_eval_nontrivial(struct fail_point *, 103 int *ret); 104 105/** 106 * @addtogroup failpoint 107 * @{ 108 */ 109/* 110 * Initialize a fail-point. The name is formed in printf-like fashion 111 * from "fmt" and the subsequent arguments. 112 * Pair with fail_point_destroy(). 113 */ 114void fail_point_init(struct fail_point *, const char *fmt, ...) 115 __printflike(2, 3); 116 117/* Return true iff this fail point is set to off, false otherwise */ 118bool fail_point_is_off(struct fail_point *fp); 119 120/** 121 * Set the pre-sleep function for a fail point 122 * If fp_post_sleep_fn is specified, then FAIL_POINT_SLEEP will result in a 123 * (*fp->fp_pre_sleep_fn)(fp->fp_pre_sleep_arg) call by the thread. 124 */ 125static inline void 126fail_point_sleep_set_pre_func(struct fail_point *fp, void (*sleep_fn)(void *)) 127{ 128 fp->fp_pre_sleep_fn = sleep_fn; 129} 130 131static inline void 132fail_point_sleep_set_pre_arg(struct fail_point *fp, void *sleep_arg) 133{ 134 fp->fp_pre_sleep_arg = sleep_arg; 135} 136 137/** 138 * Set the post-sleep function. This will be passed to timeout if we take 139 * the timeout path. This must be set if you sleep using the timeout path. 140 */ 141static inline void 142fail_point_sleep_set_post_func(struct fail_point *fp, void (*sleep_fn)(void *)) 143{ 144 fp->fp_post_sleep_fn = sleep_fn; 145} 146 147static inline void 148fail_point_sleep_set_post_arg(struct fail_point *fp, void *sleep_arg) 149{ 150 fp->fp_post_sleep_arg = sleep_arg; 151} 152 153void fail_point_alloc_callout(struct fail_point *); 154 155/** 156 * If the FAIL_POINT_USE_TIMEOUT flag is set on a failpoint, then 157 * FAIL_POINT_SLEEP will result in a call to callout_reset instead of 158 * msleep. Note that if you sleep while this flag is set, you must 159 * set fp_post_sleep_fn or an error will occur upon waking. 160 */ 161static inline void 162fail_point_use_timeout_path(struct fail_point *fp, bool use_timeout, 163 void (*post_sleep_fn)(void *)) 164{ 165 KASSERT(!use_timeout || post_sleep_fn != NULL || 166 (post_sleep_fn == NULL && fp->fp_post_sleep_fn != NULL), 167 ("Setting fp to use timeout, but not setting post_sleep_fn\n")); 168 169 if (use_timeout) { 170 fail_point_alloc_callout(fp); 171 fp->fp_flags |= FAIL_POINT_USE_TIMEOUT_PATH; 172 } else 173 fp->fp_flags &= ~FAIL_POINT_USE_TIMEOUT_PATH; 174 175 if (post_sleep_fn != NULL) 176 fp->fp_post_sleep_fn = post_sleep_fn; 177} 178 179/** 180 * Free the resources used by a fail-point. Pair with fail_point_init(). 181 */ 182void fail_point_destroy(struct fail_point *); 183 184/** 185 * Evaluate a failpoint. 186 */ 187static inline enum fail_point_return_code 188fail_point_eval(struct fail_point *fp, int *ret) 189{ 190 if (__predict_true(fp->fp_setting == NULL)) 191 return (FAIL_POINT_RC_CONTINUE); 192 return (fail_point_eval_nontrivial(fp, ret)); 193} 194 195__END_DECLS 196 197/* Declare a fail_point and its sysctl in a function. */ 198#define KFAIL_POINT_DECLARE(name) \ 199 extern struct fail_point _FAIL_POINT_NAME(name) 200#define _FAIL_POINT_NAME(name) _fail_point_##name 201#define _FAIL_POINT_LOCATION() "(" __FILE__ ":" __XSTRING(__LINE__) ")" 202#define KFAIL_POINT_DEFINE(parent, name, flags) \ 203 struct fail_point _FAIL_POINT_NAME(name) = { \ 204 .fp_name = #name, \ 205 .fp_location = _FAIL_POINT_LOCATION(), \ 206 .fp_ref_cnt = 0, \ 207 .fp_setting = NULL, \ 208 .fp_flags = (flags), \ 209 .fp_pre_sleep_fn = NULL, \ 210 .fp_pre_sleep_arg = NULL, \ 211 .fp_post_sleep_fn = NULL, \ 212 .fp_post_sleep_arg = NULL, \ 213 }; \ 214 SYSCTL_OID(parent, OID_AUTO, name, \ 215 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, \ 216 &_FAIL_POINT_NAME(name), 0, fail_point_sysctl, \ 217 "A", ""); \ 218 SYSCTL_OID(parent, OID_AUTO, status_##name, \ 219 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, \ 220 &_FAIL_POINT_NAME(name), 0, \ 221 fail_point_sysctl_status, "A", ""); 222 223#define _FAIL_POINT_INIT(parent, name, flags) \ 224 static KFAIL_POINT_DEFINE(parent, name, flags) 225#define _FAIL_POINT_EVAL(name, cond, code...) \ 226 int RETURN_VALUE; \ 227 \ 228 if (__predict_false(cond && \ 229 fail_point_eval(&_FAIL_POINT_NAME(name), &RETURN_VALUE))) { \ 230 \ 231 code; \ 232 \ 233 } 234#define KFAIL_POINT_EVAL(name, code...) \ 235 _FAIL_POINT_EVAL(name, true, code) 236 237/** 238 * Instantiate a failpoint which returns "RETURN_VALUE" from the function 239 * when triggered. 240 * @param parent The parent sysctl under which to locate the fp's sysctl 241 * @param name The name of the failpoint in the sysctl tree (and printouts) 242 * @return Instantly returns the RETURN_VALUE specified in the 243 * failpoint, if triggered. 244 */ 245#define KFAIL_POINT_RETURN(parent, name) \ 246 KFAIL_POINT_CODE(parent, name, return RETURN_VALUE) 247 248/** 249 * Instantiate a failpoint which returns (void) from the function when 250 * triggered. 251 * @param parent The parent sysctl under which to locate the sysctl 252 * @param name The name of the failpoint in the sysctl tree (and printouts) 253 * @return Instantly returns void, if triggered in the failpoint. 254 */ 255#define KFAIL_POINT_RETURN_VOID(parent, name) \ 256 KFAIL_POINT_CODE(parent, name, return) 257 258/** 259 * Instantiate a failpoint which sets an error 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 262 * printouts) 263 * @param error_var A variable to set to the failpoint's specified 264 * return-value when triggered 265 */ 266#define KFAIL_POINT_ERROR(parent, name, error_var) \ 267 KFAIL_POINT_CODE(parent, name, (error_var) = RETURN_VALUE) 268 269/** 270 * Instantiate a failpoint which sets an error and then goes to a 271 * specified label in the function when triggered. 272 * @param parent The parent sysctl under which to locate the sysctl 273 * @param name The name of the failpoint in the sysctl tree (and 274 * printouts) 275 * @param error_var A variable to set to the failpoint's specified 276 * return-value when triggered 277 * @param label The location to goto when triggered. 278 */ 279#define KFAIL_POINT_GOTO(parent, name, error_var, label) \ 280 KFAIL_POINT_CODE(parent, name, (error_var) = RETURN_VALUE; goto label) 281 282/** 283 * Instantiate a failpoint which sets its pre- and post-sleep callback 284 * mechanisms. 285 * @param parent The parent sysctl under which to locate the sysctl 286 * @param name The name of the failpoint in the sysctl tree (and 287 * printouts) 288 * @param pre_func Function pointer to the pre-sleep function, which will be 289 * called directly before going to sleep. 290 * @param pre_arg Argument to the pre-sleep function 291 * @param post_func Function pointer to the pot-sleep function, which will be 292 * called directly before going to sleep. 293 * @param post_arg Argument to the post-sleep function 294 */ 295#define KFAIL_POINT_SLEEP_CALLBACKS(parent, name, pre_func, pre_arg, \ 296 post_func, post_arg) \ 297 KFAIL_POINT_CODE_SLEEP_CALLBACKS(parent, name, pre_func, \ 298 pre_arg, post_func, post_arg, return RETURN_VALUE) 299 300/** 301 * Instantiate a failpoint which runs arbitrary code when triggered, and sets 302 * its pre- and post-sleep callback mechanisms 303 * @param parent The parent sysctl under which to locate the sysctl 304 * @param name The name of the failpoint in the sysctl tree (and 305 * printouts) 306 * @param pre_func Function pointer to the pre-sleep function, which will be 307 * called directly before going to sleep. 308 * @param pre_arg Argument to the pre-sleep function 309 * @param post_func Function pointer to the pot-sleep function, which will be 310 * called directly before going to sleep. 311 * @param post_arg Argument to the post-sleep function 312 * @param code The arbitrary code to run when triggered. Can reference 313 * "RETURN_VALUE" if desired to extract the specified 314 * user return-value when triggered. Note that this is 315 * implemented with a do-while loop so be careful of 316 * break and continue statements. 317 */ 318#define KFAIL_POINT_CODE_SLEEP_CALLBACKS(parent, name, pre_func, pre_arg, \ 319 post_func, post_arg, code...) \ 320 do { \ 321 _FAIL_POINT_INIT(parent, name) \ 322 _FAIL_POINT_NAME(name).fp_pre_sleep_fn = pre_func; \ 323 _FAIL_POINT_NAME(name).fp_pre_sleep_arg = pre_arg; \ 324 _FAIL_POINT_NAME(name).fp_post_sleep_fn = post_func; \ 325 _FAIL_POINT_NAME(name).fp_post_sleep_arg = post_arg; \ 326 _FAIL_POINT_EVAL(name, true, code) \ 327 } while (0) 328 329/** 330 * Instantiate a failpoint which runs arbitrary code when triggered. 331 * @param parent The parent sysctl under which to locate the sysctl 332 * @param name The name of the failpoint in the sysctl tree 333 * (and printouts) 334 * @param code The arbitrary code to run when triggered. Can reference 335 * "RETURN_VALUE" if desired to extract the specified 336 * user return-value when triggered. Note that this is 337 * implemented with a do-while loop so be careful of 338 * break and continue statements. 339 */ 340#define KFAIL_POINT_CODE(parent, name, code...) \ 341 do { \ 342 _FAIL_POINT_INIT(parent, name, 0) \ 343 _FAIL_POINT_EVAL(name, true, code) \ 344 } while (0) 345 346#define KFAIL_POINT_CODE_FLAGS(parent, name, flags, code...) \ 347 do { \ 348 _FAIL_POINT_INIT(parent, name, flags) \ 349 _FAIL_POINT_EVAL(name, true, code) \ 350 } while (0) 351 352#define KFAIL_POINT_CODE_COND(parent, name, cond, flags, code...) \ 353 do { \ 354 _FAIL_POINT_INIT(parent, name, flags) \ 355 _FAIL_POINT_EVAL(name, cond, code) \ 356 } while (0) 357 358/** 359 * @} 360 * (end group failpoint) 361 */ 362 363#ifdef _KERNEL 364int fail_point_sysctl(SYSCTL_HANDLER_ARGS); 365int fail_point_sysctl_status(SYSCTL_HANDLER_ARGS); 366 367/* The fail point sysctl tree. */ 368SYSCTL_DECL(_debug_fail_point); 369#define DEBUG_FP _debug_fail_point 370#endif 371 372#endif /* _SYS_FAIL_H_ */ 373