4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see http://www.gnu.org/licenses
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Use is subject to license terms.
25 * Copyright (c) 2011, 2014, Intel Corporation.
28 * This file is part of Lustre, http://www.lustre.org/
29 * Lustre is a trademark of Oracle Corporation, Inc.
32 #ifndef _LIBCFS_FAIL_H
33 #define _LIBCFS_FAIL_H
35 extern unsigned long cfs_fail_loc;
36 extern unsigned int cfs_fail_val;
37 extern int cfs_fail_err;
39 extern wait_queue_head_t cfs_race_waitq;
40 extern int cfs_race_state;
42 int __cfs_fail_check_set(__u32 id, __u32 value, int set);
43 int __cfs_fail_timeout_set(const char *file, const char *func, int line,
44 __u32 id, __u32 value, int ms, int set);
47 CFS_FAIL_LOC_NOSET = 0,
48 CFS_FAIL_LOC_ORSET = 1,
49 CFS_FAIL_LOC_RESET = 2,
50 CFS_FAIL_LOC_VALUE = 3
55 * "0x0100 - 0x3fff" for Lustre
56 * "0xe000 - 0xefff" for LNet
57 * "0xf000 - 0xffff" for LNDs
60 /* Failure injection control */
61 #define CFS_FAIL_MASK_SYS 0x0000FF00
62 #define CFS_FAIL_MASK_LOC (0x000000FF | CFS_FAIL_MASK_SYS)
64 #define CFS_FAILED_BIT 30
65 /* CFS_FAILED is 0x40000000 */
66 #define CFS_FAILED BIT(CFS_FAILED_BIT)
68 #define CFS_FAIL_ONCE_BIT 31
69 /* CFS_FAIL_ONCE is 0x80000000 */
70 #define CFS_FAIL_ONCE BIT(CFS_FAIL_ONCE_BIT)
72 /* The following flags aren't made to be combined */
73 #define CFS_FAIL_SKIP 0x20000000 /* skip N times then fail */
74 #define CFS_FAIL_SOME 0x10000000 /* only fail N times */
75 #define CFS_FAIL_RAND 0x08000000 /* fail 1/N of the times */
76 #define CFS_FAIL_USR1 0x04000000 /* user flag */
78 /* CFS_FAULT may be combined with any one of the above flags. */
79 #define CFS_FAULT 0x02000000 /* match any CFS_FAULT_CHECK */
81 static inline bool CFS_FAIL_PRECHECK(__u32 id)
83 return unlikely(cfs_fail_loc != 0 &&
84 ((cfs_fail_loc & CFS_FAIL_MASK_LOC) == (id & CFS_FAIL_MASK_LOC) ||
85 (cfs_fail_loc & id & CFS_FAULT)));
88 #define UNLIKELY_CHECK_SET(id, value, set, quiet) \
89 (unlikely(cfs_fail_check_set_loc(__FILE__, __func__, __LINE__, \
90 id, value, set, quiet)))
92 static inline int cfs_fail_check_set_loc(const char *file, const char *func,
93 int line, __u32 id, __u32 value,
98 if (CFS_FAIL_PRECHECK(id)) {
99 /* set failed_once before the CFS_FAILED flag is set below */
100 unsigned long failed_once = cfs_fail_loc & CFS_FAILED;
102 ret = __cfs_fail_check_set(id, value, set);
104 CDEBUG_LIMIT_LOC(file, func, line,
105 (quiet && failed_once) ?
107 "*** cfs_fail_loc=%x, val=%u***\n",
115 * If id hit cfs_fail_loc, return 1, otherwise return 0
117 #define CFS_FAIL_CHECK(id) \
118 UNLIKELY_CHECK_SET(id, cfs_fail_val, CFS_FAIL_LOC_NOSET, 0)
119 #define CFS_FAIL_CHECK_QUIET(id) \
120 UNLIKELY_CHECK_SET(id, cfs_fail_val, CFS_FAIL_LOC_NOSET, 1)
123 * If id hit cfs_fail_loc and cfs_fail_val == (-1 or value) return 1,
126 #define CFS_FAIL_CHECK_VALUE(id, value) \
127 UNLIKELY_CHECK_SET(id, value, CFS_FAIL_LOC_VALUE, 0)
128 #define CFS_FAIL_CHECK_VALUE_QUIET(id, value) \
129 UNLIKELY_CHECK_SET(id, value, CFS_FAIL_LOC_VALUE, 1)
132 * If id hit cfs_fail_loc, cfs_fail_loc |= value and return 1,
135 #define CFS_FAIL_CHECK_ORSET(id, value) \
136 UNLIKELY_CHECK_SET(id, value, CFS_FAIL_LOC_ORSET, 0)
137 #define CFS_FAIL_CHECK_ORSET_QUIET(id, value) \
138 UNLIKELY_CHECK_SET(id, value, CFS_FAIL_LOC_ORSET, 1)
141 * If id hit cfs_fail_loc, cfs_fail_loc = value and return 1,
144 #define CFS_FAIL_CHECK_RESET(id, value) \
145 UNLIKELY_CHECK_SET(id, value, CFS_FAIL_LOC_RESET, 0)
146 #define CFS_FAIL_CHECK_RESET_QUIET(id, value) \
147 UNLIKELY_CHECK_SET(id, value, CFS_FAIL_LOC_RESET, 1)
149 #define UNLIKELY_TIMEOUT_SET(id, value, ms, set) \
150 (unlikely(cfs_fail_timeout_set_loc(__FILE__, __func__, __LINE__, \
151 id, value, ms, set)))
153 static inline int cfs_fail_timeout_set_loc(const char *file, const char *func,
154 int line, __u32 id, __u32 value,
157 if (CFS_FAIL_PRECHECK(id))
158 return __cfs_fail_timeout_set(file, func, line, id, value, ms,
163 /* If id hit cfs_fail_loc, sleep for seconds or milliseconds */
164 #define CFS_FAIL_TIMEOUT(id, secs) \
165 UNLIKELY_TIMEOUT_SET(id, cfs_fail_val, (secs)*1000, CFS_FAIL_LOC_NOSET)
166 #define CFS_FAIL_TIMEOUT_MS(id, ms) \
167 UNLIKELY_TIMEOUT_SET(id, cfs_fail_val, ms, CFS_FAIL_LOC_NOSET)
170 * If id hit cfs_fail_loc, cfs_fail_loc |= value and
171 * sleep seconds or milliseconds
173 #define CFS_FAIL_TIMEOUT_ORSET(id, value, secs) \
174 UNLIKELY_TIMEOUT_SET(id, value, (secs) * 1000, CFS_FAIL_LOC_ORSET)
176 #define CFS_FAIL_TIMEOUT_RESET(id, value, secs) \
177 UNLIKELY_TIMEOUT_SET(id, value, (secs) * 1000, CFS_FAIL_LOC_RESET)
179 #define CFS_FAIL_TIMEOUT_MS_ORSET(id, value, ms) \
180 UNLIKELY_TIMEOUT_SET(id, value, ms, CFS_FAIL_LOC_ORSET)
181 #define CFS_FAULT_CHECK(id) \
182 CFS_FAIL_CHECK(CFS_FAULT | (id))
185 * The idea here is to synchronise two threads to force a race. The
186 * first thread that calls this with a matching fail_loc is put to
187 * sleep. The next thread that calls with the same fail_loc wakes up
188 * the first and continues.
190 static inline void cfs_race_loc(const char *file, const char *func, int line,
193 if (CFS_FAIL_PRECHECK(id)) {
194 if (unlikely(__cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET))) {
197 CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
198 "cfs_race id %x sleeping\n", id);
200 * XXX: don't wait forever as there is no guarantee
201 * that this branch is executed first. for testing
202 * purposes this construction works good enough
204 rc = wait_event_interruptible_timeout(cfs_race_waitq,
206 cfs_time_seconds(5));
207 CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
208 "cfs_fail_race id %x awake: rc=%d\n",
211 CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
212 "cfs_fail_race id %x waking\n", id);
214 wake_up(&cfs_race_waitq);
218 #define CFS_RACE(id) cfs_race_loc(__FILE__, __func__, __LINE__, id)
223 * The first thread that calls this with a matching fail_loc is put to sleep,
224 * but subseqent callers of this won't sleep. Until another thread that calls
225 * cfs_race_wakeup(), the first thread will be woken up and continue.
227 static inline void cfs_race_wait_loc(const char *file, const char *func,
230 if (CFS_FAIL_PRECHECK(id)) {
231 if (unlikely(__cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET))) {
235 CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
236 "cfs_race id %x sleeping\n", id);
237 rc = wait_event_interruptible(cfs_race_waitq,
238 cfs_race_state != 0);
239 CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
240 "cfs_fail_race id %x awake: rc=%d\n",
245 #define CFS_RACE_WAIT(id) cfs_race_wait_loc(__FILE__, __func__, __LINE__, id)
248 * Wake up the thread that is waiting on the matching fail_loc.
250 static inline void cfs_race_wakeup_loc(const char *file, const char *func,
253 if (CFS_FAIL_PRECHECK(id)) {
254 if (likely(!__cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET))) {
255 CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
256 "cfs_fail_race id %x waking\n", id);
258 wake_up(&cfs_race_waitq);
262 #define CFS_RACE_WAKEUP(id) cfs_race_wakeup_loc(__FILE__, __func__, __LINE__,id)
264 #endif /* _LIBCFS_FAIL_H */