1192908Szml/*-
2192908Szml * Copyright (c) 2009 Isilon Inc http://www.isilon.com/
3192908Szml *
4192908Szml * Redistribution and use in source and binary forms, with or without
5192908Szml * modification, are permitted provided that the following conditions
6192908Szml * are met:
7192908Szml * 1. Redistributions of source code must retain the above copyright
8192908Szml *    notice, this list of conditions and the following disclaimer.
9192908Szml * 2. Redistributions in binary form must reproduce the above copyright
10192908Szml *    notice, this list of conditions and the following disclaimer in the
11192908Szml *    documentation and/or other materials provided with the distribution.
12192908Szml *
13192908Szml * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14192908Szml * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15192908Szml * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16192908Szml * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17192908Szml * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18192908Szml * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19192908Szml * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20192908Szml * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21192908Szml * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22192908Szml * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23192908Szml * SUCH DAMAGE.
24192908Szml */
25192908Szml/**
26192908Szml * @file
27192908Szml *
28192908Szml * fail(9) Facility.
29192908Szml *
30192908Szml * @ingroup failpoint_private
31192908Szml */
32192908Szml/**
33192908Szml * @defgroup failpoint fail(9) Facility
34192908Szml *
35192908Szml * Failpoints allow for injecting fake errors into running code on the fly,
36192908Szml * without modifying code or recompiling with flags.  Failpoints are always
37192908Szml * present, and are very efficient when disabled.  Failpoints are described
38192908Szml * in man fail(9).
39192908Szml */
40192908Szml/**
41192908Szml * @defgroup failpoint_private Private fail(9) Implementation functions
42192908Szml *
43192908Szml * Private implementations for the actual failpoint code.
44192908Szml *
45192908Szml * @ingroup failpoint
46192908Szml */
47192908Szml/**
48192908Szml * @addtogroup failpoint_private
49192908Szml * @{
50192908Szml */
51192908Szml
52192908Szml#include <sys/cdefs.h>
53192908Szml__FBSDID("$FreeBSD: stable/11/sys/kern/kern_fail.c 318847 2017-05-25 01:15:53Z markj $");
54192908Szml
55296973Scem#include "opt_stack.h"
56296973Scem
57223875Smdf#include <sys/ctype.h>
58192908Szml#include <sys/errno.h>
59192908Szml#include <sys/fail.h>
60192908Szml#include <sys/kernel.h>
61192908Szml#include <sys/libkern.h>
62296927Scem#include <sys/limits.h>
63192908Szml#include <sys/lock.h>
64192908Szml#include <sys/malloc.h>
65192908Szml#include <sys/mutex.h>
66223876Smdf#include <sys/proc.h>
67192908Szml#include <sys/sbuf.h>
68296927Scem#include <sys/sleepqueue.h>
69296927Scem#include <sys/sx.h>
70296927Scem#include <sys/sysctl.h>
71296927Scem#include <sys/types.h>
72192908Szml
73296927Scem#include <machine/atomic.h>
74192908Szml#include <machine/stdarg.h>
75192908Szml
76192908Szml#ifdef ILOG_DEFINE_FOR_FILE
77192908SzmlILOG_DEFINE_FOR_FILE(L_ISI_FAIL_POINT, L_ILOG, fail_point);
78192908Szml#endif
79192908Szml
80227293Sedstatic MALLOC_DEFINE(M_FAIL_POINT, "Fail Points", "fail points system");
81192908Szml#define fp_free(ptr) free(ptr, M_FAIL_POINT)
82192908Szml#define fp_malloc(size, flags) malloc((size), M_FAIL_POINT, (flags))
83296927Scem#define fs_free(ptr) fp_free(ptr)
84296927Scem#define fs_malloc() fp_malloc(sizeof(struct fail_point_setting), \
85301727Smarkj    M_WAITOK | M_ZERO)
86192908Szml
87301727Smarkj/**
88301727Smarkj * These define the wchans that are used for sleeping, pausing respectively.
89301727Smarkj * They are chosen arbitrarily but need to be distinct to the failpoint and
90301727Smarkj * the sleep/pause distinction.
91301727Smarkj */
92296927Scem#define FP_SLEEP_CHANNEL(fp) (void*)(fp)
93296927Scem#define FP_PAUSE_CHANNEL(fp) __DEVOLATILE(void*, &fp->fp_setting)
94192908Szml
95216616Smdf/**
96296927Scem * Don't allow more than this many entries in a fail point set by sysctl.
97296927Scem * The 99.99...% case is to have 1 entry.  I can't imagine having this many
98296927Scem * entries, so it should not limit us.  Saves on re-mallocs while holding
99296927Scem * a non-sleepable lock.
100296927Scem */
101296927Scem#define FP_MAX_ENTRY_COUNT 20
102296927Scem
103296927Scem/* Used to drain sbufs to the sysctl output */
104296927Scemint fail_sysctl_drain_func(void *, const char *, int);
105296927Scem
106296927Scem/* Head of tailq of struct fail_point_entry */
107296927ScemTAILQ_HEAD(fail_point_entry_queue, fail_point_entry);
108296927Scem
109296927Scem/**
110296927Scem * fp entries garbage list; outstanding entries are cleaned up in the
111296927Scem * garbage collector
112296927Scem */
113296927ScemSTAILQ_HEAD(fail_point_setting_garbage, fail_point_setting);
114296927Scemstatic struct fail_point_setting_garbage fp_setting_garbage =
115296927Scem        STAILQ_HEAD_INITIALIZER(fp_setting_garbage);
116296927Scemstatic struct mtx mtx_garbage_list;
117296927ScemMTX_SYSINIT(mtx_garbage_list, &mtx_garbage_list, "fail point garbage mtx",
118296927Scem        MTX_SPIN);
119296927Scem
120296927Scemstatic struct sx sx_fp_set;
121296927ScemSX_SYSINIT(sx_fp_set, &sx_fp_set, "fail point set sx");
122296927Scem
123296927Scem/**
124216616Smdf * Failpoint types.
125216616Smdf * Don't change these without changing fail_type_strings in fail.c.
126216616Smdf * @ingroup failpoint_private
127216616Smdf */
128216616Smdfenum fail_point_t {
129216616Smdf	FAIL_POINT_OFF,		/**< don't fail */
130216616Smdf	FAIL_POINT_PANIC,	/**< panic */
131216616Smdf	FAIL_POINT_RETURN,	/**< return an errorcode */
132216616Smdf	FAIL_POINT_BREAK,	/**< break into the debugger */
133216616Smdf	FAIL_POINT_PRINT,	/**< print a message */
134216616Smdf	FAIL_POINT_SLEEP,	/**< sleep for some msecs */
135296927Scem	FAIL_POINT_PAUSE,	/**< sleep until failpoint is set to off */
136296927Scem	FAIL_POINT_YIELD,	/**< yield the cpu */
137296927Scem	FAIL_POINT_DELAY,	/**< busy wait the cpu */
138296927Scem	FAIL_POINT_NUMTYPES,
139296927Scem	FAIL_POINT_INVALID = -1
140216616Smdf};
141216616Smdf
142223875Smdfstatic struct {
143223875Smdf	const char *name;
144223875Smdf	int	nmlen;
145223875Smdf} fail_type_strings[] = {
146223875Smdf#define	FP_TYPE_NM_LEN(s)	{ s, sizeof(s) - 1 }
147223875Smdf	[FAIL_POINT_OFF] =	FP_TYPE_NM_LEN("off"),
148223875Smdf	[FAIL_POINT_PANIC] =	FP_TYPE_NM_LEN("panic"),
149223875Smdf	[FAIL_POINT_RETURN] =	FP_TYPE_NM_LEN("return"),
150223875Smdf	[FAIL_POINT_BREAK] =	FP_TYPE_NM_LEN("break"),
151223875Smdf	[FAIL_POINT_PRINT] =	FP_TYPE_NM_LEN("print"),
152223875Smdf	[FAIL_POINT_SLEEP] =	FP_TYPE_NM_LEN("sleep"),
153296927Scem	[FAIL_POINT_PAUSE] =	FP_TYPE_NM_LEN("pause"),
154296927Scem	[FAIL_POINT_YIELD] =	FP_TYPE_NM_LEN("yield"),
155296927Scem	[FAIL_POINT_DELAY] =	FP_TYPE_NM_LEN("delay"),
156216616Smdf};
157216616Smdf
158296927Scem#define FE_COUNT_UNTRACKED (INT_MIN)
159296927Scem
160216616Smdf/**
161216616Smdf * Internal structure tracking a single term of a complete failpoint.
162216616Smdf * @ingroup failpoint_private
163216616Smdf */
164216616Smdfstruct fail_point_entry {
165296927Scem	volatile bool	fe_stale;
166296927Scem	enum fail_point_t	fe_type;	/**< type of entry */
167216616Smdf	int		fe_arg;		/**< argument to type (e.g. return value) */
168216616Smdf	int		fe_prob;	/**< likelihood of firing in millionths */
169301727Smarkj	int32_t		fe_count;	/**< number of times to fire, -1 means infinite */
170223876Smdf	pid_t		fe_pid;		/**< only fail for this process */
171296927Scem	struct fail_point	*fe_parent;	/**< backpointer to fp */
172296927Scem	TAILQ_ENTRY(fail_point_entry)	fe_entries; /**< next entry ptr */
173216616Smdf};
174216616Smdf
175296927Scemstruct fail_point_setting {
176296927Scem	STAILQ_ENTRY(fail_point_setting) fs_garbage_link;
177296927Scem	struct fail_point_entry_queue fp_entry_queue;
178296927Scem	struct fail_point * fs_parent;
179296927Scem	struct mtx feq_mtx; /* Gives fail_point_pause something to do.  */
180296927Scem};
181296927Scem
182296927Scem/**
183296927Scem * Defines stating the equivalent of probablilty one (100%)
184296927Scem */
185296927Scemenum {
186296927Scem	PROB_MAX = 1000000,	/* probability between zero and this number */
187296927Scem	PROB_DIGITS = 6		/* number of zero's in above number */
188296927Scem};
189296927Scem
190296927Scem/* Get a ref on an fp's fp_setting */
191296927Scemstatic inline struct fail_point_setting *fail_point_setting_get_ref(
192296927Scem        struct fail_point *fp);
193296927Scem/* Release a ref on an fp_setting */
194296927Scemstatic inline void fail_point_setting_release_ref(struct fail_point *fp);
195296927Scem/* Allocate and initialize a struct fail_point_setting */
196296927Scemstatic struct fail_point_setting *fail_point_setting_new(struct
197296927Scem        fail_point *);
198296927Scem/* Free a struct fail_point_setting */
199296927Scemstatic void fail_point_setting_destroy(struct fail_point_setting *fp_setting);
200296927Scem/* Allocate and initialize a struct fail_point_entry */
201296927Scemstatic struct fail_point_entry *fail_point_entry_new(struct
202296927Scem        fail_point_setting *);
203296927Scem/* Free a struct fail_point_entry */
204296927Scemstatic void fail_point_entry_destroy(struct fail_point_entry *fp_entry);
205296927Scem/* Append fp setting to garbage list */
206296927Scemstatic inline void fail_point_setting_garbage_append(
207296927Scem        struct fail_point_setting *fp_setting);
208296927Scem/* Swap fp's setting with fp_setting_new */
209296927Scemstatic inline struct fail_point_setting *
210296927Scem        fail_point_swap_settings(struct fail_point *fp,
211296927Scem        struct fail_point_setting *fp_setting_new);
212296927Scem/* Free up any zero-ref setting in the garbage queue */
213296927Scemstatic void fail_point_garbage_collect(void);
214296927Scem/* If this fail point's setting are empty, then swap it out to NULL. */
215296927Scemstatic inline void fail_point_eval_swap_out(struct fail_point *fp,
216296927Scem        struct fail_point_setting *fp_setting);
217296927Scem
218296927Scembool
219296927Scemfail_point_is_off(struct fail_point *fp)
220296927Scem{
221296927Scem	bool return_val;
222296927Scem	struct fail_point_setting *fp_setting;
223296927Scem	struct fail_point_entry *ent;
224296927Scem
225296927Scem	return_val = true;
226296927Scem
227296927Scem	fp_setting = fail_point_setting_get_ref(fp);
228296927Scem	if (fp_setting != NULL) {
229296927Scem		TAILQ_FOREACH(ent, &fp_setting->fp_entry_queue,
230296927Scem		    fe_entries) {
231296927Scem			if (!ent->fe_stale) {
232296927Scem				return_val = false;
233296927Scem				break;
234296927Scem			}
235296927Scem		}
236296927Scem	}
237296927Scem	fail_point_setting_release_ref(fp);
238296927Scem
239296927Scem	return (return_val);
240296927Scem}
241296927Scem
242296927Scem/* Allocate and initialize a struct fail_point_setting */
243296927Scemstatic struct fail_point_setting *
244296927Scemfail_point_setting_new(struct fail_point *fp)
245296927Scem{
246296927Scem	struct fail_point_setting *fs_new;
247296927Scem
248296927Scem	fs_new = fs_malloc();
249296927Scem	fs_new->fs_parent = fp;
250296927Scem	TAILQ_INIT(&fs_new->fp_entry_queue);
251296927Scem	mtx_init(&fs_new->feq_mtx, "fail point entries", NULL, MTX_SPIN);
252296927Scem
253296927Scem	fail_point_setting_garbage_append(fs_new);
254296927Scem
255296927Scem	return (fs_new);
256296927Scem}
257296927Scem
258296927Scem/* Free a struct fail_point_setting */
259296927Scemstatic void
260296927Scemfail_point_setting_destroy(struct fail_point_setting *fp_setting)
261296927Scem{
262296927Scem	struct fail_point_entry *ent;
263296927Scem
264296927Scem	while (!TAILQ_EMPTY(&fp_setting->fp_entry_queue)) {
265296927Scem		ent = TAILQ_FIRST(&fp_setting->fp_entry_queue);
266296927Scem		TAILQ_REMOVE(&fp_setting->fp_entry_queue, ent, fe_entries);
267296927Scem		fail_point_entry_destroy(ent);
268296927Scem	}
269296927Scem
270296927Scem	fs_free(fp_setting);
271296927Scem}
272296927Scem
273296927Scem/* Allocate and initialize a struct fail_point_entry */
274296927Scemstatic struct fail_point_entry *
275296927Scemfail_point_entry_new(struct fail_point_setting *fp_setting)
276296927Scem{
277296927Scem	struct fail_point_entry *fp_entry;
278296927Scem
279296927Scem	fp_entry = fp_malloc(sizeof(struct fail_point_entry),
280296927Scem	        M_WAITOK | M_ZERO);
281296927Scem	fp_entry->fe_parent = fp_setting->fs_parent;
282296927Scem	fp_entry->fe_prob = PROB_MAX;
283296927Scem	fp_entry->fe_pid = NO_PID;
284296927Scem	fp_entry->fe_count = FE_COUNT_UNTRACKED;
285296927Scem	TAILQ_INSERT_TAIL(&fp_setting->fp_entry_queue, fp_entry,
286296927Scem	        fe_entries);
287296927Scem
288296927Scem	return (fp_entry);
289296927Scem}
290296927Scem
291296927Scem/* Free a struct fail_point_entry */
292296927Scemstatic void
293296927Scemfail_point_entry_destroy(struct fail_point_entry *fp_entry)
294296927Scem{
295296927Scem
296296927Scem	fp_free(fp_entry);
297296927Scem}
298296927Scem
299296927Scem/* Get a ref on an fp's fp_setting */
300296927Scemstatic inline struct fail_point_setting *
301296927Scemfail_point_setting_get_ref(struct fail_point *fp)
302296927Scem{
303296927Scem	struct fail_point_setting *fp_setting;
304296927Scem
305296927Scem	/* Invariant: if we have a ref, our pointer to fp_setting is safe */
306296927Scem	atomic_add_acq_32(&fp->fp_ref_cnt, 1);
307296927Scem	fp_setting = fp->fp_setting;
308296927Scem
309296927Scem	return (fp_setting);
310296927Scem}
311296927Scem
312296927Scem/* Release a ref on an fp_setting */
313192908Szmlstatic inline void
314296927Scemfail_point_setting_release_ref(struct fail_point *fp)
315192908Szml{
316192908Szml
317296927Scem	KASSERT(&fp->fp_ref_cnt > 0, ("Attempting to deref w/no refs"));
318296927Scem	atomic_subtract_rel_32(&fp->fp_ref_cnt, 1);
319296927Scem}
320296927Scem
321296927Scem/* Append fp entries to fp garbage list */
322296927Scemstatic inline void
323296927Scemfail_point_setting_garbage_append(struct fail_point_setting *fp_setting)
324296927Scem{
325296927Scem
326296927Scem	mtx_lock_spin(&mtx_garbage_list);
327296927Scem	STAILQ_INSERT_TAIL(&fp_setting_garbage, fp_setting,
328296927Scem	        fs_garbage_link);
329296927Scem	mtx_unlock_spin(&mtx_garbage_list);
330296927Scem}
331296927Scem
332296927Scem/* Swap fp's entries with fp_setting_new */
333296927Scemstatic struct fail_point_setting *
334296927Scemfail_point_swap_settings(struct fail_point *fp,
335296927Scem        struct fail_point_setting *fp_setting_new)
336296927Scem{
337296927Scem	struct fail_point_setting *fp_setting_old;
338296927Scem
339296927Scem	fp_setting_old = fp->fp_setting;
340296927Scem	fp->fp_setting = fp_setting_new;
341296927Scem
342296927Scem	return (fp_setting_old);
343296927Scem}
344296927Scem
345296927Scemstatic inline void
346296927Scemfail_point_eval_swap_out(struct fail_point *fp,
347296927Scem        struct fail_point_setting *fp_setting)
348296927Scem{
349296927Scem
350296927Scem	/* We may have already been swapped out and replaced; ignore. */
351296927Scem	if (fp->fp_setting == fp_setting)
352296927Scem		fail_point_swap_settings(fp, NULL);
353296927Scem}
354296927Scem
355296927Scem/* Free up any zero-ref entries in the garbage queue */
356296927Scemstatic void
357301727Smarkjfail_point_garbage_collect(void)
358296927Scem{
359296927Scem	struct fail_point_setting *fs_current, *fs_next;
360296927Scem	struct fail_point_setting_garbage fp_ents_free_list;
361296927Scem
362296927Scem	/**
363296927Scem	  * We will transfer the entries to free to fp_ents_free_list while holding
364296927Scem	  * the spin mutex, then free it after we drop the lock. This avoids
365296927Scem	  * triggering witness due to sleepable mutexes in the memory
366296927Scem	  * allocator.
367296927Scem	  */
368296927Scem	STAILQ_INIT(&fp_ents_free_list);
369296927Scem
370296927Scem	mtx_lock_spin(&mtx_garbage_list);
371296927Scem	STAILQ_FOREACH_SAFE(fs_current, &fp_setting_garbage, fs_garbage_link,
372296927Scem	    fs_next) {
373296927Scem		if (fs_current->fs_parent->fp_setting != fs_current &&
374296927Scem		        fs_current->fs_parent->fp_ref_cnt == 0) {
375296927Scem			STAILQ_REMOVE(&fp_setting_garbage, fs_current,
376296927Scem			        fail_point_setting, fs_garbage_link);
377296927Scem			STAILQ_INSERT_HEAD(&fp_ents_free_list, fs_current,
378296927Scem			        fs_garbage_link);
379296927Scem		}
380296927Scem	}
381296927Scem	mtx_unlock_spin(&mtx_garbage_list);
382296927Scem
383296927Scem	STAILQ_FOREACH_SAFE(fs_current, &fp_ents_free_list, fs_garbage_link,
384296927Scem	        fs_next)
385296927Scem		fail_point_setting_destroy(fs_current);
386296927Scem}
387296927Scem
388296927Scem/* Drain out all refs from this fail point */
389296927Scemstatic inline void
390296927Scemfail_point_drain(struct fail_point *fp, int expected_ref)
391296927Scem{
392296927Scem	struct fail_point_setting *entries;
393296927Scem
394296927Scem	entries = fail_point_swap_settings(fp, NULL);
395296927Scem	/**
396296927Scem	 * We have unpaused all threads; so we will wait no longer
397296927Scem	 * than the time taken for the longest remaining sleep, or
398296927Scem	 * the length of time of a long-running code block.
399296927Scem	 */
400296927Scem	while (fp->fp_ref_cnt > expected_ref) {
401296927Scem		wakeup(FP_PAUSE_CHANNEL(fp));
402296927Scem		tsleep(&fp, PWAIT, "fail_point_drain", hz / 100);
403296927Scem	}
404296927Scem	fail_point_swap_settings(fp, entries);
405296927Scem}
406296927Scem
407296927Scemstatic inline void
408296927Scemfail_point_pause(struct fail_point *fp, enum fail_point_return_code *pret,
409296927Scem        struct mtx *mtx_sleep)
410296927Scem{
411296927Scem
412296927Scem	if (fp->fp_pre_sleep_fn)
413296927Scem		fp->fp_pre_sleep_fn(fp->fp_pre_sleep_arg);
414296927Scem
415296927Scem	msleep_spin(FP_PAUSE_CHANNEL(fp), mtx_sleep, "failpt", 0);
416296927Scem
417296927Scem	if (fp->fp_post_sleep_fn)
418296927Scem		fp->fp_post_sleep_fn(fp->fp_post_sleep_arg);
419296927Scem}
420296927Scem
421296927Scemstatic inline void
422296927Scemfail_point_sleep(struct fail_point *fp, int msecs,
423296927Scem        enum fail_point_return_code *pret)
424296927Scem{
425296927Scem	int timo;
426296927Scem
427296927Scem	/* Convert from millisecs to ticks, rounding up */
428296927Scem	timo = howmany(msecs * hz, 1000);
429296927Scem
430223875Smdf	if (timo > 0) {
431296927Scem		if (!(fp->fp_flags & FAIL_POINT_USE_TIMEOUT_PATH)) {
432296927Scem			if (fp->fp_pre_sleep_fn)
433296927Scem				fp->fp_pre_sleep_fn(fp->fp_pre_sleep_arg);
434296927Scem
435296927Scem			tsleep(FP_SLEEP_CHANNEL(fp), PWAIT, "failpt", timo);
436296927Scem
437296927Scem			if (fp->fp_post_sleep_fn)
438296927Scem				fp->fp_post_sleep_fn(fp->fp_post_sleep_arg);
439192908Szml		} else {
440296927Scem			if (fp->fp_pre_sleep_fn)
441296927Scem				fp->fp_pre_sleep_fn(fp->fp_pre_sleep_arg);
442296927Scem
443296927Scem			timeout(fp->fp_post_sleep_fn, fp->fp_post_sleep_arg,
444301727Smarkj			    timo);
445192908Szml			*pret = FAIL_POINT_RC_QUEUED;
446192908Szml		}
447192908Szml	}
448192908Szml}
449192908Szml
450296927Scemstatic char *parse_fail_point(struct fail_point_setting *, char *);
451296927Scemstatic char *parse_term(struct fail_point_setting *, char *);
452192908Szmlstatic char *parse_number(int *out_units, int *out_decimal, char *);
453192908Szmlstatic char *parse_type(struct fail_point_entry *, char *);
454192908Szml
455192908Szml/**
456192908Szml * Initialize a fail_point.  The name is formed in a printf-like fashion
457192908Szml * from "fmt" and subsequent arguments.  This function is generally used
458192908Szml * for custom failpoints located at odd places in the sysctl tree, and is
459192908Szml * not explicitly needed for standard in-line-declared failpoints.
460192908Szml *
461192908Szml * @ingroup failpoint
462192908Szml */
463192908Szmlvoid
464192908Szmlfail_point_init(struct fail_point *fp, const char *fmt, ...)
465192908Szml{
466192908Szml	va_list ap;
467192908Szml	char *name;
468192908Szml	int n;
469192908Szml
470296927Scem	fp->fp_setting = NULL;
471192908Szml	fp->fp_flags = 0;
472192908Szml
473192908Szml	/* Figure out the size of the name. */
474192908Szml	va_start(ap, fmt);
475192908Szml	n = vsnprintf(NULL, 0, fmt, ap);
476192908Szml	va_end(ap);
477192908Szml
478192908Szml	/* Allocate the name and fill it in. */
479192908Szml	name = fp_malloc(n + 1, M_WAITOK);
480192908Szml	if (name != NULL) {
481192908Szml		va_start(ap, fmt);
482192908Szml		vsnprintf(name, n + 1, fmt, ap);
483192908Szml		va_end(ap);
484192908Szml	}
485192908Szml	fp->fp_name = name;
486216620Smdf	fp->fp_location = "";
487192908Szml	fp->fp_flags |= FAIL_POINT_DYNAMIC_NAME;
488296927Scem	fp->fp_pre_sleep_fn = NULL;
489296927Scem	fp->fp_pre_sleep_arg = NULL;
490296927Scem	fp->fp_post_sleep_fn = NULL;
491296927Scem	fp->fp_post_sleep_arg = NULL;
492192908Szml}
493192908Szml
494192908Szml/**
495296927Scem * Free the resources held by a fail_point, and wake any paused threads.
496296927Scem * Thou shalt not allow threads to hit this fail point after you enter this
497296927Scem * function, nor shall you call this multiple times for a given fp.
498192908Szml * @ingroup failpoint
499192908Szml */
500192908Szmlvoid
501192908Szmlfail_point_destroy(struct fail_point *fp)
502192908Szml{
503192908Szml
504296927Scem	fail_point_drain(fp, 0);
505296927Scem
506223875Smdf	if ((fp->fp_flags & FAIL_POINT_DYNAMIC_NAME) != 0) {
507223875Smdf		fp_free(__DECONST(void *, fp->fp_name));
508192908Szml		fp->fp_name = NULL;
509192908Szml	}
510192908Szml	fp->fp_flags = 0;
511296927Scem
512296927Scem	sx_xlock(&sx_fp_set);
513296927Scem	fail_point_garbage_collect();
514296927Scem	sx_xunlock(&sx_fp_set);
515192908Szml}
516192908Szml
517192908Szml/**
518192908Szml * This does the real work of evaluating a fail point. If the fail point tells
519192908Szml * us to return a value, this function returns 1 and fills in 'return_value'
520192908Szml * (return_value is allowed to be null). If the fail point tells us to panic,
521192908Szml * we never return. Otherwise we just return 0 after doing some work, which
522192908Szml * means "keep going".
523192908Szml */
524192908Szmlenum fail_point_return_code
525192908Szmlfail_point_eval_nontrivial(struct fail_point *fp, int *return_value)
526192908Szml{
527296927Scem	bool execute = false;
528296927Scem	struct fail_point_entry *ent;
529296927Scem	struct fail_point_setting *fp_setting;
530296927Scem	enum fail_point_return_code ret;
531296927Scem	int cont;
532296927Scem	int count;
533192908Szml	int msecs;
534296927Scem	int usecs;
535192908Szml
536296927Scem	ret = FAIL_POINT_RC_CONTINUE;
537296927Scem	cont = 0; /* don't continue by default */
538192908Szml
539296927Scem	fp_setting = fail_point_setting_get_ref(fp);
540296927Scem	if (fp_setting == NULL)
541296927Scem		goto abort;
542192908Szml
543296927Scem	TAILQ_FOREACH(ent, &fp_setting->fp_entry_queue, fe_entries) {
544296927Scem
545296927Scem		if (ent->fe_stale)
546296927Scem			continue;
547296927Scem
548192908Szml		if (ent->fe_prob < PROB_MAX &&
549223875Smdf		    ent->fe_prob < random() % PROB_MAX)
550223875Smdf			continue;
551296927Scem
552223876Smdf		if (ent->fe_pid != NO_PID && ent->fe_pid != curproc->p_pid)
553223876Smdf			continue;
554192908Szml
555296927Scem		if (ent->fe_count != FE_COUNT_UNTRACKED) {
556296927Scem			count = ent->fe_count;
557296927Scem			while (count > 0) {
558296927Scem				if (atomic_cmpset_32(&ent->fe_count, count, count - 1)) {
559296927Scem					count--;
560296927Scem					execute = true;
561296927Scem					break;
562296927Scem				}
563296927Scem				count = ent->fe_count;
564296927Scem			}
565296927Scem			if (execute == false)
566296927Scem				/* We lost the race; consider the entry stale and bail now */
567296927Scem				continue;
568296927Scem			if (count == 0)
569296927Scem				ent->fe_stale = true;
570296927Scem		}
571296927Scem
572192908Szml		switch (ent->fe_type) {
573192908Szml		case FAIL_POINT_PANIC:
574192908Szml			panic("fail point %s panicking", fp->fp_name);
575192908Szml			/* NOTREACHED */
576192908Szml
577192908Szml		case FAIL_POINT_RETURN:
578223875Smdf			if (return_value != NULL)
579192908Szml				*return_value = ent->fe_arg;
580192908Szml			ret = FAIL_POINT_RC_RETURN;
581192908Szml			break;
582192908Szml
583192908Szml		case FAIL_POINT_BREAK:
584223875Smdf			printf("fail point %s breaking to debugger\n",
585296927Scem			        fp->fp_name);
586192908Szml			breakpoint();
587192908Szml			break;
588192908Szml
589192908Szml		case FAIL_POINT_PRINT:
590192908Szml			printf("fail point %s executing\n", fp->fp_name);
591192908Szml			cont = ent->fe_arg;
592192908Szml			break;
593192908Szml
594192908Szml		case FAIL_POINT_SLEEP:
595192908Szml			msecs = ent->fe_arg;
596192908Szml			if (msecs)
597296927Scem				fail_point_sleep(fp, msecs, &ret);
598192908Szml			break;
599192908Szml
600296927Scem		case FAIL_POINT_PAUSE:
601296927Scem			/**
602296927Scem			 * Pausing is inherently strange with multiple
603296927Scem			 * entries given our design.  That is because some
604296927Scem			 * entries could be unreachable, for instance in cases like:
605296927Scem			 * pause->return. We can never reach the return entry.
606296927Scem			 * The sysctl layer actually truncates all entries after
607296927Scem			 * a pause for this reason.
608296927Scem			 */
609296927Scem			mtx_lock_spin(&fp_setting->feq_mtx);
610296927Scem			fail_point_pause(fp, &ret, &fp_setting->feq_mtx);
611296927Scem			mtx_unlock_spin(&fp_setting->feq_mtx);
612296927Scem			break;
613296927Scem
614296927Scem		case FAIL_POINT_YIELD:
615318847Smarkj			kern_yield(PRI_UNCHANGED);
616296927Scem			break;
617296927Scem
618296927Scem		case FAIL_POINT_DELAY:
619296927Scem			usecs = ent->fe_arg;
620296927Scem			DELAY(usecs);
621296927Scem			break;
622296927Scem
623192908Szml		default:
624192908Szml			break;
625192908Szml		}
626192908Szml
627223875Smdf		if (cont == 0)
628192908Szml			break;
629192908Szml	}
630192908Szml
631296927Scem	if (fail_point_is_off(fp))
632296927Scem		fail_point_eval_swap_out(fp, fp_setting);
633192908Szml
634296927Scemabort:
635296927Scem	fail_point_setting_release_ref(fp);
636192908Szml
637223875Smdf	return (ret);
638192908Szml}
639192908Szml
640192908Szml/**
641192908Szml * Translate internal fail_point structure into human-readable text.
642192908Szml */
643192908Szmlstatic void
644296927Scemfail_point_get(struct fail_point *fp, struct sbuf *sb,
645296927Scem        bool verbose)
646192908Szml{
647192908Szml	struct fail_point_entry *ent;
648296927Scem	struct fail_point_setting *fp_setting;
649296927Scem	struct fail_point_entry *fp_entry_cpy;
650296927Scem	int cnt_sleeping;
651296927Scem	int idx;
652296927Scem	int printed_entry_count;
653192908Szml
654296927Scem	cnt_sleeping = 0;
655296927Scem	idx = 0;
656296927Scem	printed_entry_count = 0;
657192908Szml
658296927Scem	fp_entry_cpy = fp_malloc(sizeof(struct fail_point_entry) *
659296927Scem	        (FP_MAX_ENTRY_COUNT + 1), M_WAITOK);
660296927Scem
661296927Scem	fp_setting = fail_point_setting_get_ref(fp);
662296927Scem
663296927Scem	if (fp_setting != NULL) {
664296927Scem		TAILQ_FOREACH(ent, &fp_setting->fp_entry_queue, fe_entries) {
665296927Scem			if (ent->fe_stale)
666296927Scem				continue;
667296927Scem
668296927Scem			KASSERT(printed_entry_count < FP_MAX_ENTRY_COUNT,
669296927Scem			        ("FP entry list larger than allowed"));
670296927Scem
671296927Scem			fp_entry_cpy[printed_entry_count] = *ent;
672296927Scem			++printed_entry_count;
673296927Scem		}
674296927Scem	}
675296927Scem	fail_point_setting_release_ref(fp);
676296927Scem
677296927Scem	/* This is our equivalent of a NULL terminator */
678296927Scem	fp_entry_cpy[printed_entry_count].fe_type = FAIL_POINT_INVALID;
679296927Scem
680296927Scem	while (idx < printed_entry_count) {
681296927Scem		ent = &fp_entry_cpy[idx];
682296927Scem		++idx;
683192908Szml		if (ent->fe_prob < PROB_MAX) {
684192908Szml			int decimal = ent->fe_prob % (PROB_MAX / 100);
685192908Szml			int units = ent->fe_prob / (PROB_MAX / 100);
686192908Szml			sbuf_printf(sb, "%d", units);
687192908Szml			if (decimal) {
688192908Szml				int digits = PROB_DIGITS - 2;
689192908Szml				while (!(decimal % 10)) {
690192908Szml					digits--;
691192908Szml					decimal /= 10;
692192908Szml				}
693192908Szml				sbuf_printf(sb, ".%0*d", digits, decimal);
694192908Szml			}
695192908Szml			sbuf_printf(sb, "%%");
696192908Szml		}
697296927Scem		if (ent->fe_count >= 0)
698192908Szml			sbuf_printf(sb, "%d*", ent->fe_count);
699223875Smdf		sbuf_printf(sb, "%s", fail_type_strings[ent->fe_type].name);
700192908Szml		if (ent->fe_arg)
701192908Szml			sbuf_printf(sb, "(%d)", ent->fe_arg);
702223876Smdf		if (ent->fe_pid != NO_PID)
703223876Smdf			sbuf_printf(sb, "[pid %d]", ent->fe_pid);
704192908Szml		if (TAILQ_NEXT(ent, fe_entries))
705192908Szml			sbuf_printf(sb, "->");
706192908Szml	}
707296927Scem	if (!printed_entry_count)
708192908Szml		sbuf_printf(sb, "off");
709192908Szml
710296927Scem	fp_free(fp_entry_cpy);
711296927Scem	if (verbose) {
712296973Scem#ifdef STACK
713296927Scem		/* Print number of sleeping threads. queue=0 is the argument
714296927Scem		 * used by msleep when sending our threads to sleep. */
715296927Scem		sbuf_printf(sb, "\nsleeping_thread_stacks = {\n");
716296927Scem		sleepq_sbuf_print_stacks(sb, FP_SLEEP_CHANNEL(fp), 0,
717296927Scem		        &cnt_sleeping);
718296927Scem
719296927Scem		sbuf_printf(sb, "},\n");
720296973Scem#endif
721296927Scem		sbuf_printf(sb, "sleeping_thread_count = %d,\n",
722296927Scem		        cnt_sleeping);
723296927Scem
724296973Scem#ifdef STACK
725296927Scem		sbuf_printf(sb, "paused_thread_stacks = {\n");
726296927Scem		sleepq_sbuf_print_stacks(sb, FP_PAUSE_CHANNEL(fp), 0,
727296927Scem		        &cnt_sleeping);
728296927Scem
729296927Scem		sbuf_printf(sb, "},\n");
730296973Scem#endif
731296927Scem		sbuf_printf(sb, "paused_thread_count = %d\n",
732296927Scem		        cnt_sleeping);
733296927Scem	}
734192908Szml}
735192908Szml
736192908Szml/**
737192908Szml * Set an internal fail_point structure from a human-readable failpoint string
738192908Szml * in a lock-safe manner.
739192908Szml */
740192908Szmlstatic int
741192908Szmlfail_point_set(struct fail_point *fp, char *buf)
742192908Szml{
743192908Szml	struct fail_point_entry *ent, *ent_next;
744296927Scem	struct fail_point_setting *entries;
745296927Scem	bool should_wake_paused;
746296927Scem	bool should_truncate;
747296927Scem	int error;
748192908Szml
749296927Scem	error = 0;
750296927Scem	should_wake_paused = false;
751296927Scem	should_truncate = false;
752296927Scem
753192908Szml	/* Parse new entries. */
754296927Scem	/**
755296927Scem	 * ref protects our new malloc'd stuff from being garbage collected
756296927Scem	 * before we link it.
757296927Scem	 */
758296927Scem	fail_point_setting_get_ref(fp);
759296927Scem	entries = fail_point_setting_new(fp);
760296927Scem	if (parse_fail_point(entries, buf) == NULL) {
761296927Scem		STAILQ_REMOVE(&fp_setting_garbage, entries,
762296927Scem		        fail_point_setting, fs_garbage_link);
763296927Scem		fail_point_setting_destroy(entries);
764192908Szml		error = EINVAL;
765192908Szml		goto end;
766192908Szml	}
767192908Szml
768296927Scem	/**
769296927Scem	 * Transfer the entries we are going to keep to a new list.
770296927Scem	 * Get rid of useless zero probability entries, and entries with hit
771296927Scem	 * count 0.
772296927Scem	 * If 'off' is present, and it has no hit count set, then all entries
773296927Scem	 *       after it are discarded since they are unreachable.
774296927Scem	 */
775296927Scem	TAILQ_FOREACH_SAFE(ent, &entries->fp_entry_queue, fe_entries, ent_next) {
776296927Scem		if (ent->fe_prob == 0 || ent->fe_count == 0) {
777296927Scem			printf("Discarding entry which cannot execute %s\n",
778296927Scem			        fail_type_strings[ent->fe_type].name);
779296927Scem			TAILQ_REMOVE(&entries->fp_entry_queue, ent,
780296927Scem			        fe_entries);
781296927Scem			fp_free(ent);
782296927Scem			continue;
783296927Scem		} else if (should_truncate) {
784296927Scem			printf("Discarding unreachable entry %s\n",
785296927Scem			        fail_type_strings[ent->fe_type].name);
786296927Scem			TAILQ_REMOVE(&entries->fp_entry_queue, ent,
787296927Scem			        fe_entries);
788296927Scem			fp_free(ent);
789296927Scem			continue;
790296927Scem		}
791192908Szml
792296927Scem		if (ent->fe_type == FAIL_POINT_OFF) {
793296927Scem			should_wake_paused = true;
794296927Scem			if (ent->fe_count == FE_COUNT_UNTRACKED) {
795296927Scem				should_truncate = true;
796296927Scem				TAILQ_REMOVE(&entries->fp_entry_queue, ent,
797296927Scem				        fe_entries);
798296927Scem				fp_free(ent);
799296927Scem			}
800296927Scem		} else if (ent->fe_type == FAIL_POINT_PAUSE) {
801296927Scem			should_truncate = true;
802296927Scem		} else if (ent->fe_type == FAIL_POINT_SLEEP && (fp->fp_flags &
803296927Scem		        FAIL_POINT_NONSLEEPABLE)) {
804296927Scem			/**
805296927Scem			 * If this fail point is annotated as being in a
806296927Scem			 * non-sleepable ctx, convert sleep to delay and
807296927Scem			 * convert the msec argument to usecs.
808296927Scem			 */
809296927Scem			printf("Sleep call request on fail point in "
810296927Scem			        "non-sleepable context; using delay instead "
811296927Scem			        "of sleep\n");
812296927Scem			ent->fe_type = FAIL_POINT_DELAY;
813296927Scem			ent->fe_arg *= 1000;
814296927Scem		}
815296927Scem	}
816192908Szml
817296927Scem	if (TAILQ_EMPTY(&entries->fp_entry_queue)) {
818296927Scem		entries = fail_point_swap_settings(fp, NULL);
819296927Scem		if (entries != NULL)
820296927Scem			wakeup(FP_PAUSE_CHANNEL(fp));
821296927Scem	} else {
822296927Scem		if (should_wake_paused)
823296927Scem			wakeup(FP_PAUSE_CHANNEL(fp));
824296927Scem		fail_point_swap_settings(fp, entries);
825192908Szml	}
826192908Szml
827296927Scemend:
828192908Szml#ifdef IWARNING
829192908Szml	if (error)
830216620Smdf		IWARNING("Failed to set %s %s to %s",
831192908Szml		    fp->fp_name, fp->fp_location, buf);
832192908Szml	else
833216620Smdf		INOTICE("Set %s %s to %s",
834192908Szml		    fp->fp_name, fp->fp_location, buf);
835192908Szml#endif /* IWARNING */
836192908Szml
837296927Scem	fail_point_setting_release_ref(fp);
838223875Smdf	return (error);
839192908Szml}
840192908Szml
841192908Szml#define MAX_FAIL_POINT_BUF	1023
842192908Szml
843192908Szml/**
844192908Szml * Handle kernel failpoint set/get.
845192908Szml */
846192908Szmlint
847192908Szmlfail_point_sysctl(SYSCTL_HANDLER_ARGS)
848192908Szml{
849296927Scem	struct fail_point *fp;
850296927Scem	char *buf;
851301727Smarkj	struct sbuf sb, *sb_check;
852192908Szml	int error;
853192908Szml
854301727Smarkj	buf = NULL;
855296927Scem	error = 0;
856296927Scem	fp = arg1;
857192908Szml
858296927Scem	sb_check = sbuf_new(&sb, NULL, 1024, SBUF_AUTOEXTEND);
859296927Scem	if (sb_check != &sb)
860296927Scem		return (ENOMEM);
861296927Scem
862296927Scem	sbuf_set_drain(&sb, (sbuf_drain_func *)fail_sysctl_drain_func, req);
863296927Scem
864192908Szml	/* Setting */
865296927Scem	/**
866296927Scem	 * Lock protects any new entries from being garbage collected before we
867296927Scem	 * can link them to the fail point.
868296927Scem	 */
869296927Scem	sx_xlock(&sx_fp_set);
870296927Scem	if (req->newptr) {
871192908Szml		if (req->newlen > MAX_FAIL_POINT_BUF) {
872192908Szml			error = EINVAL;
873192908Szml			goto out;
874192908Szml		}
875192908Szml
876192908Szml		buf = fp_malloc(req->newlen + 1, M_WAITOK);
877192908Szml
878192908Szml		error = SYSCTL_IN(req, buf, req->newlen);
879192908Szml		if (error)
880192908Szml			goto out;
881192908Szml		buf[req->newlen] = '\0';
882192908Szml
883192908Szml		error = fail_point_set(fp, buf);
884296927Scem	}
885192908Szml
886296927Scem	fail_point_garbage_collect();
887296927Scem	sx_xunlock(&sx_fp_set);
888296927Scem
889296927Scem	/* Retrieving. */
890296927Scem	fail_point_get(fp, &sb, false);
891296927Scem
892192908Szmlout:
893296927Scem	sbuf_finish(&sb);
894296927Scem	sbuf_delete(&sb);
895296927Scem
896296927Scem	if (buf)
897296927Scem		fp_free(buf);
898296927Scem
899223875Smdf	return (error);
900192908Szml}
901192908Szml
902296927Scemint
903296927Scemfail_point_sysctl_status(SYSCTL_HANDLER_ARGS)
904296927Scem{
905296927Scem	struct fail_point *fp;
906296927Scem	struct sbuf sb, *sb_check;
907296927Scem
908296927Scem	fp = arg1;
909296927Scem
910296927Scem	sb_check = sbuf_new(&sb, NULL, 1024, SBUF_AUTOEXTEND);
911296927Scem	if (sb_check != &sb)
912296927Scem		return (ENOMEM);
913296927Scem
914296927Scem	sbuf_set_drain(&sb, (sbuf_drain_func *)fail_sysctl_drain_func, req);
915296927Scem
916296927Scem	/* Retrieving. */
917296927Scem	fail_point_get(fp, &sb, true);
918296927Scem
919296927Scem	sbuf_finish(&sb);
920296927Scem	sbuf_delete(&sb);
921296927Scem
922296927Scem	/**
923296927Scem	 * Lock protects any new entries from being garbage collected before we
924296927Scem	 * can link them to the fail point.
925296927Scem	 */
926296927Scem	sx_xlock(&sx_fp_set);
927296927Scem	fail_point_garbage_collect();
928296927Scem	sx_xunlock(&sx_fp_set);
929296927Scem
930296927Scem	return (0);
931296927Scem}
932296927Scem
933296927Scemint
934296927Scemfail_sysctl_drain_func(void *sysctl_args, const char *buf, int len)
935296927Scem{
936296927Scem	struct sysctl_req *sa;
937296927Scem	int error;
938296927Scem
939296927Scem	sa = sysctl_args;
940296927Scem
941296927Scem	error = SYSCTL_OUT(sa, buf, len);
942296927Scem
943296927Scem	if (error == ENOMEM)
944296927Scem		return (-1);
945296927Scem	else
946296927Scem		return (len);
947296927Scem}
948296927Scem
949192908Szml/**
950192908Szml * Internal helper function to translate a human-readable failpoint string
951192908Szml * into a internally-parsable fail_point structure.
952192908Szml */
953192908Szmlstatic char *
954296927Scemparse_fail_point(struct fail_point_setting *ents, char *p)
955192908Szml{
956192908Szml	/*  <fail_point> ::
957192908Szml	 *      <term> ( "->" <term> )*
958192908Szml	 */
959296927Scem	uint8_t term_count;
960296927Scem
961296927Scem	term_count = 1;
962296927Scem
963223875Smdf	p = parse_term(ents, p);
964223875Smdf	if (p == NULL)
965223875Smdf		return (NULL);
966296927Scem
967223875Smdf	while (*p != '\0') {
968296927Scem		term_count++;
969296927Scem		if (p[0] != '-' || p[1] != '>' ||
970296927Scem		        (p = parse_term(ents, p+2)) == NULL ||
971296927Scem		        term_count > FP_MAX_ENTRY_COUNT)
972223875Smdf			return (NULL);
973223875Smdf	}
974223875Smdf	return (p);
975192908Szml}
976192908Szml
977192908Szml/**
978192908Szml * Internal helper function to parse an individual term from a failpoint.
979192908Szml */
980192908Szmlstatic char *
981296927Scemparse_term(struct fail_point_setting *ents, char *p)
982192908Szml{
983192908Szml	struct fail_point_entry *ent;
984192908Szml
985296927Scem	ent = fail_point_entry_new(ents);
986192908Szml
987192908Szml	/*
988192908Szml	 * <term> ::
989192908Szml	 *     ( (<float> "%") | (<integer> "*" ) )*
990192908Szml	 *     <type>
991192908Szml	 *     [ "(" <integer> ")" ]
992223876Smdf	 *     [ "[pid " <integer> "]" ]
993192908Szml	 */
994192908Szml
995192908Szml	/* ( (<float> "%") | (<integer> "*" ) )* */
996223875Smdf	while (isdigit(*p) || *p == '.') {
997192908Szml		int units, decimal;
998192908Szml
999223875Smdf		p = parse_number(&units, &decimal, p);
1000223875Smdf		if (p == NULL)
1001223875Smdf			return (NULL);
1002192908Szml
1003192908Szml		if (*p == '%') {
1004192908Szml			if (units > 100) /* prevent overflow early */
1005192908Szml				units = 100;
1006192908Szml			ent->fe_prob = units * (PROB_MAX / 100) + decimal;
1007192908Szml			if (ent->fe_prob > PROB_MAX)
1008192908Szml				ent->fe_prob = PROB_MAX;
1009192908Szml		} else if (*p == '*') {
1010296927Scem			if (!units || units < 0 || decimal)
1011223875Smdf				return (NULL);
1012201758Smbr			ent->fe_count = units;
1013223875Smdf		} else
1014223875Smdf			return (NULL);
1015192908Szml		p++;
1016192908Szml	}
1017192908Szml
1018192908Szml	/* <type> */
1019223875Smdf	p = parse_type(ent, p);
1020223875Smdf	if (p == NULL)
1021223875Smdf		return (NULL);
1022192908Szml	if (*p == '\0')
1023223875Smdf		return (p);
1024192908Szml
1025192908Szml	/* [ "(" <integer> ")" ] */
1026192908Szml	if (*p != '(')
1027296927Scem		return (p);
1028192908Szml	p++;
1029223875Smdf	if (!isdigit(*p) && *p != '-')
1030223875Smdf		return (NULL);
1031223875Smdf	ent->fe_arg = strtol(p, &p, 0);
1032192908Szml	if (*p++ != ')')
1033223875Smdf		return (NULL);
1034192908Szml
1035223876Smdf	/* [ "[pid " <integer> "]" ] */
1036296927Scem#define PID_STRING "[pid "
1037223876Smdf	if (strncmp(p, PID_STRING, sizeof(PID_STRING) - 1) != 0)
1038223876Smdf		return (p);
1039223876Smdf	p += sizeof(PID_STRING) - 1;
1040223876Smdf	if (!isdigit(*p))
1041223876Smdf		return (NULL);
1042223876Smdf	ent->fe_pid = strtol(p, &p, 0);
1043223876Smdf	if (*p++ != ']')
1044223876Smdf		return (NULL);
1045223876Smdf
1046223875Smdf	return (p);
1047192908Szml}
1048192908Szml
1049192908Szml/**
1050192908Szml * Internal helper function to parse a numeric for a failpoint term.
1051192908Szml */
1052192908Szmlstatic char *
1053192908Szmlparse_number(int *out_units, int *out_decimal, char *p)
1054192908Szml{
1055192908Szml	char *old_p;
1056192908Szml
1057296927Scem	/**
1058192908Szml	 *  <number> ::
1059192908Szml	 *      <integer> [ "." <integer> ] |
1060192908Szml	 *      "." <integer>
1061192908Szml	 */
1062192908Szml
1063192908Szml	/* whole part */
1064192908Szml	old_p = p;
1065201758Smbr	*out_units = strtol(p, &p, 10);
1066192908Szml	if (p == old_p && *p != '.')
1067223875Smdf		return (NULL);
1068192908Szml
1069192908Szml	/* fractional part */
1070192908Szml	*out_decimal = 0;
1071192908Szml	if (*p == '.') {
1072192908Szml		int digits = 0;
1073192908Szml		p++;
1074223875Smdf		while (isdigit(*p)) {
1075192908Szml			int digit = *p - '0';
1076192908Szml			if (digits < PROB_DIGITS - 2)
1077192908Szml				*out_decimal = *out_decimal * 10 + digit;
1078192908Szml			else if (digits == PROB_DIGITS - 2 && digit >= 5)
1079192908Szml				(*out_decimal)++;
1080192908Szml			digits++;
1081192908Szml			p++;
1082192908Szml		}
1083192908Szml		if (!digits) /* need at least one digit after '.' */
1084223875Smdf			return (NULL);
1085192908Szml		while (digits++ < PROB_DIGITS - 2) /* add implicit zeros */
1086192908Szml			*out_decimal *= 10;
1087192908Szml	}
1088192908Szml
1089223875Smdf	return (p); /* success */
1090192908Szml}
1091192908Szml
1092192908Szml/**
1093192908Szml * Internal helper function to parse an individual type for a failpoint term.
1094192908Szml */
1095192908Szmlstatic char *
1096192908Szmlparse_type(struct fail_point_entry *ent, char *beg)
1097192908Szml{
1098192908Szml	enum fail_point_t type;
1099223875Smdf	int len;
1100223875Smdf
1101223875Smdf	for (type = FAIL_POINT_OFF; type < FAIL_POINT_NUMTYPES; type++) {
1102223875Smdf		len = fail_type_strings[type].nmlen;
1103223875Smdf		if (strncmp(fail_type_strings[type].name, beg, len) == 0) {
1104192908Szml			ent->fe_type = type;
1105223875Smdf			return (beg + len);
1106192908Szml		}
1107192908Szml	}
1108223875Smdf	return (NULL);
1109192908Szml}
1110192908Szml
1111296927Scem/* The fail point sysctl tree. */
1112296927ScemSYSCTL_NODE(_debug, OID_AUTO, fail_point, CTLFLAG_RW, 0, "fail points");
1113192908Szml
1114296927Scem/* Debugging/testing stuff for fail point */
1115296927Scemstatic int
1116296927Scemsysctl_test_fail_point(SYSCTL_HANDLER_ARGS)
1117192908Szml{
1118223875Smdf
1119296927Scem	KFAIL_POINT_RETURN(DEBUG_FP, test_fail_point);
1120296927Scem	return (0);
1121192908Szml}
1122296927ScemSYSCTL_OID(_debug_fail_point, OID_AUTO, test_trigger_fail_point,
1123296927Scem        CTLTYPE_STRING | CTLFLAG_RD, NULL, 0, sysctl_test_fail_point, "A",
1124296927Scem        "Trigger test fail points");
1125