1 /* SPDX-License-Identifier: GPL-2.0 */
4 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
5 * Use is subject to license terms.
7 * Copyright (c) 2011, 2014, Intel Corporation.
11 * This file is part of Lustre, http://www.lustre.org/
14 #ifndef _LIBCFS_FAIL_H
15 #define _LIBCFS_FAIL_H
17 extern unsigned long cfs_fail_loc;
18 extern unsigned int cfs_fail_val;
19 extern int cfs_fail_err;
21 extern wait_queue_head_t cfs_race_waitq;
22 extern int cfs_race_state;
24 int __cfs_fail_check_set(__u32 id, __u32 value, int set);
25 int __cfs_fail_timeout_set(const char *file, const char *func, int line,
26 __u32 id, __u32 value, int ms, int set);
29 CFS_FAIL_LOC_NOSET = 0,
30 CFS_FAIL_LOC_ORSET = 1,
31 CFS_FAIL_LOC_RESET = 2,
32 CFS_FAIL_LOC_VALUE = 3
37 * "0x0100 - 0x3fff" for Lustre
38 * "0xe000 - 0xefff" for LNet
39 * "0xf000 - 0xffff" for LNDs
42 /* Failure injection control */
43 #define CFS_FAIL_MASK_SYS 0x0000FF00
44 #define CFS_FAIL_MASK_LOC (0x000000FF | CFS_FAIL_MASK_SYS)
46 #define CFS_FAILED_BIT 30
47 /* CFS_FAILED is 0x40000000 */
48 #define CFS_FAILED BIT(CFS_FAILED_BIT)
50 #define CFS_FAIL_ONCE_BIT 31
51 /* CFS_FAIL_ONCE is 0x80000000 */
52 #define CFS_FAIL_ONCE BIT(CFS_FAIL_ONCE_BIT)
54 /* The following flags aren't made to be combined */
55 #define CFS_FAIL_SKIP 0x20000000 /* skip N times then fail */
56 #define CFS_FAIL_SOME 0x10000000 /* only fail N times */
57 #define CFS_FAIL_RAND 0x08000000 /* fail 1/N of the times */
58 #define CFS_FAIL_USR1 0x04000000 /* user flag */
60 /* CFS_FAULT may be combined with any one of the above flags. */
61 #define CFS_FAULT 0x02000000 /* match any CFS_FAULT_CHECK */
63 static inline bool CFS_FAIL_PRECHECK(__u32 id)
65 return unlikely(cfs_fail_loc != 0 &&
66 ((cfs_fail_loc & CFS_FAIL_MASK_LOC) == (id & CFS_FAIL_MASK_LOC) ||
67 (cfs_fail_loc & id & CFS_FAULT)));
70 #define UNLIKELY_CHECK_SET(id, value, set, quiet) \
71 (unlikely(cfs_fail_check_set_loc(__FILE__, __func__, __LINE__, \
72 id, value, set, quiet)))
74 static inline int cfs_fail_check_set_loc(const char *file, const char *func,
75 int line, __u32 id, __u32 value,
80 if (CFS_FAIL_PRECHECK(id)) {
81 /* set failed_once before the CFS_FAILED flag is set below */
82 unsigned long failed_once = cfs_fail_loc & CFS_FAILED;
84 ret = __cfs_fail_check_set(id, value, set);
86 CDEBUG_LIMIT_LOC(file, func, line,
87 (quiet && failed_once) ?
89 "*** cfs_fail_loc=%x, val=%u***\n",
97 * If id hit cfs_fail_loc, return 1, otherwise return 0
99 #define CFS_FAIL_CHECK(id) \
100 UNLIKELY_CHECK_SET(id, cfs_fail_val, CFS_FAIL_LOC_NOSET, 0)
101 #define CFS_FAIL_CHECK_QUIET(id) \
102 UNLIKELY_CHECK_SET(id, cfs_fail_val, CFS_FAIL_LOC_NOSET, 1)
105 * If id hit cfs_fail_loc and cfs_fail_val == (-1 or value) return 1,
108 #define CFS_FAIL_CHECK_VALUE(id, value) \
109 UNLIKELY_CHECK_SET(id, value, CFS_FAIL_LOC_VALUE, 0)
110 #define CFS_FAIL_CHECK_VALUE_QUIET(id, value) \
111 UNLIKELY_CHECK_SET(id, value, CFS_FAIL_LOC_VALUE, 1)
114 * If id hit cfs_fail_loc, cfs_fail_loc |= value and return 1,
117 #define CFS_FAIL_CHECK_ORSET(id, value) \
118 UNLIKELY_CHECK_SET(id, value, CFS_FAIL_LOC_ORSET, 0)
119 #define CFS_FAIL_CHECK_ORSET_QUIET(id, value) \
120 UNLIKELY_CHECK_SET(id, value, CFS_FAIL_LOC_ORSET, 1)
123 * If id hit cfs_fail_loc, cfs_fail_loc = value and return 1,
126 #define CFS_FAIL_CHECK_RESET(id, value) \
127 UNLIKELY_CHECK_SET(id, value, CFS_FAIL_LOC_RESET, 0)
128 #define CFS_FAIL_CHECK_RESET_QUIET(id, value) \
129 UNLIKELY_CHECK_SET(id, value, CFS_FAIL_LOC_RESET, 1)
131 #define UNLIKELY_TIMEOUT_SET(id, value, ms, set) \
132 (unlikely(cfs_fail_timeout_set_loc(__FILE__, __func__, __LINE__, \
133 id, value, ms, set)))
135 static inline int cfs_fail_timeout_set_loc(const char *file, const char *func,
136 int line, __u32 id, __u32 value,
139 if (CFS_FAIL_PRECHECK(id))
140 return __cfs_fail_timeout_set(file, func, line, id, value, ms,
145 /* If id hit cfs_fail_loc, sleep for seconds or milliseconds */
146 #define CFS_FAIL_TIMEOUT(id, secs) \
147 UNLIKELY_TIMEOUT_SET(id, cfs_fail_val, (secs)*1000, CFS_FAIL_LOC_NOSET)
148 #define CFS_FAIL_TIMEOUT_MS(id, ms) \
149 UNLIKELY_TIMEOUT_SET(id, cfs_fail_val, ms, CFS_FAIL_LOC_NOSET)
152 * If id hit cfs_fail_loc, cfs_fail_loc |= value and
153 * sleep seconds or milliseconds
155 #define CFS_FAIL_TIMEOUT_ORSET(id, value, secs) \
156 UNLIKELY_TIMEOUT_SET(id, value, (secs) * 1000, CFS_FAIL_LOC_ORSET)
158 #define CFS_FAIL_TIMEOUT_RESET(id, value, secs) \
159 UNLIKELY_TIMEOUT_SET(id, value, (secs) * 1000, CFS_FAIL_LOC_RESET)
161 #define CFS_FAIL_TIMEOUT_MS_ORSET(id, value, ms) \
162 UNLIKELY_TIMEOUT_SET(id, value, ms, CFS_FAIL_LOC_ORSET)
163 #define CFS_FAULT_CHECK(id) \
164 CFS_FAIL_CHECK(CFS_FAULT | (id))
167 * The idea here is to synchronise two threads to force a race. The
168 * first thread that calls this with a matching fail_loc is put to
169 * sleep. The next thread that calls with the same fail_loc wakes up
170 * the first and continues.
172 static inline void cfs_race_loc(const char *file, const char *func, int line,
175 if (CFS_FAIL_PRECHECK(id)) {
176 if (unlikely(__cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET))) {
179 CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
180 "cfs_race id %x sleeping\n", id);
182 * XXX: don't wait forever as there is no guarantee
183 * that this branch is executed first. for testing
184 * purposes this construction works good enough
186 rc = wait_event_interruptible_timeout(cfs_race_waitq,
188 cfs_time_seconds(5));
189 CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
190 "cfs_fail_race id %x awake: rc=%d\n",
193 CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
194 "cfs_fail_race id %x waking\n", id);
196 wake_up(&cfs_race_waitq);
200 #define CFS_RACE(id) cfs_race_loc(__FILE__, __func__, __LINE__, id)
205 * The first thread that calls this with a matching fail_loc is put to sleep,
206 * but subseqent callers of this won't sleep. Until another thread that calls
207 * cfs_race_wakeup(), the first thread will be woken up and continue.
209 static inline void cfs_race_wait_loc(const char *file, const char *func,
212 if (CFS_FAIL_PRECHECK(id)) {
213 if (unlikely(__cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET))) {
217 CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
218 "cfs_race id %x sleeping\n", id);
219 rc = wait_event_interruptible(cfs_race_waitq,
220 cfs_race_state != 0);
221 CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
222 "cfs_fail_race id %x awake: rc=%d\n",
227 #define CFS_RACE_WAIT(id) cfs_race_wait_loc(__FILE__, __func__, __LINE__, id)
230 * Wake up the thread that is waiting on the matching fail_loc.
232 static inline void cfs_race_wakeup_loc(const char *file, const char *func,
235 if (CFS_FAIL_PRECHECK(id)) {
236 if (likely(!__cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET))) {
237 CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
238 "cfs_fail_race id %x waking\n", id);
240 wake_up(&cfs_race_waitq);
244 #define CFS_RACE_WAKEUP(id) cfs_race_wakeup_loc(__FILE__, __func__, __LINE__,id)
246 #endif /* _LIBCFS_FAIL_H */