Whamcloud - gitweb
LU-9633 ptlrpc: Add kernel doc style for ptlrpc (14)
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_fail.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 /*
4  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
5  * Use is subject to license terms.
6  *
7  * Copyright (c) 2011, 2014, Intel Corporation.
8  */
9
10 /*
11  * This file is part of Lustre, http://www.lustre.org/
12  */
13
14 #ifndef _LIBCFS_FAIL_H
15 #define _LIBCFS_FAIL_H
16
17 extern unsigned long cfs_fail_loc;
18 extern unsigned int cfs_fail_val;
19 extern int cfs_fail_err;
20
21 extern wait_queue_head_t cfs_race_waitq;
22 extern int cfs_race_state;
23
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);
27
28 enum {
29         CFS_FAIL_LOC_NOSET = 0,
30         CFS_FAIL_LOC_ORSET = 1,
31         CFS_FAIL_LOC_RESET = 2,
32         CFS_FAIL_LOC_VALUE = 3
33 };
34
35 /*
36  * Failure ranges:
37  *      "0x0100 - 0x3fff" for Lustre
38  *      "0xe000 - 0xefff" for LNet
39  *      "0xf000 - 0xffff" for LNDs
40  */
41
42 /* Failure injection control */
43 #define CFS_FAIL_MASK_SYS    0x0000FF00
44 #define CFS_FAIL_MASK_LOC   (0x000000FF | CFS_FAIL_MASK_SYS)
45
46 #define CFS_FAILED_BIT       30
47 /* CFS_FAILED is 0x40000000 */
48 #define CFS_FAILED          BIT(CFS_FAILED_BIT)
49
50 #define CFS_FAIL_ONCE_BIT    31
51 /* CFS_FAIL_ONCE is 0x80000000 */
52 #define CFS_FAIL_ONCE       BIT(CFS_FAIL_ONCE_BIT)
53
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 */
59
60 /* CFS_FAULT may be combined with any one of the above flags. */
61 #define CFS_FAULT            0x02000000 /* match any CFS_FAULT_CHECK */
62
63 static inline bool CFS_FAIL_PRECHECK(__u32 id)
64 {
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)));
68 }
69
70 #define UNLIKELY_CHECK_SET(id, value, set, quiet)                       \
71         (unlikely(cfs_fail_check_set_loc(__FILE__, __func__, __LINE__,  \
72                                          id, value, set, quiet)))
73
74 static inline int cfs_fail_check_set_loc(const char *file, const char *func,
75                                          int line, __u32 id, __u32 value,
76                                          int set, int quiet)
77 {
78         int ret = 0;
79
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;
83
84                 ret = __cfs_fail_check_set(id, value, set);
85                 if (ret)
86                         CDEBUG_LIMIT_LOC(file, func, line,
87                                          (quiet && failed_once) ?
88                                                 D_INFO : D_CONSOLE,
89                                          "*** cfs_fail_loc=%x, val=%u***\n",
90                                          id, value);
91         }
92
93         return ret;
94 }
95
96 /*
97  * If id hit cfs_fail_loc, return 1, otherwise return 0
98  */
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)
103
104 /*
105  * If id hit cfs_fail_loc and cfs_fail_val == (-1 or value) return 1,
106  * otherwise return 0
107  */
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)
112
113 /*
114  * If id hit cfs_fail_loc, cfs_fail_loc |= value and return 1,
115  * otherwise return 0
116  */
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)
121
122 /*
123  * If id hit cfs_fail_loc, cfs_fail_loc = value and return 1,
124  * otherwise return 0
125  */
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)
130
131 #define UNLIKELY_TIMEOUT_SET(id, value, ms, set) \
132         (unlikely(cfs_fail_timeout_set_loc(__FILE__, __func__, __LINE__, \
133                                            id, value, ms, set)))
134
135 static inline int cfs_fail_timeout_set_loc(const char *file, const char *func,
136                                            int line, __u32 id, __u32 value,
137                                            int ms, int set)
138 {
139         if (CFS_FAIL_PRECHECK(id))
140                 return __cfs_fail_timeout_set(file, func, line, id, value, ms,
141                                               set);
142         return 0;
143 }
144
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)
150
151 /*
152  * If id hit cfs_fail_loc, cfs_fail_loc |= value and
153  * sleep seconds or milliseconds
154  */
155 #define CFS_FAIL_TIMEOUT_ORSET(id, value, secs) \
156         UNLIKELY_TIMEOUT_SET(id, value, (secs) * 1000, CFS_FAIL_LOC_ORSET)
157
158 #define CFS_FAIL_TIMEOUT_RESET(id, value, secs) \
159         UNLIKELY_TIMEOUT_SET(id, value, (secs) * 1000, CFS_FAIL_LOC_RESET)
160
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))
165
166 /*
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.
171  */
172 static inline void cfs_race_loc(const char *file, const char *func, int line,
173                                 __u32 id)
174 {
175         if (CFS_FAIL_PRECHECK(id)) {
176                 if (unlikely(__cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET))) {
177                         int rc;
178                         cfs_race_state = 0;
179                         CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
180                                          "cfs_race id %x sleeping\n", id);
181                         /*
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
185                          */
186                         rc = wait_event_interruptible_timeout(cfs_race_waitq,
187                                                       cfs_race_state != 0,
188                                                       cfs_time_seconds(5));
189                         CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
190                                          "cfs_fail_race id %x awake: rc=%d\n",
191                                          id, rc);
192                 } else {
193                         CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
194                                          "cfs_fail_race id %x waking\n", id);
195                         cfs_race_state = 1;
196                         wake_up(&cfs_race_waitq);
197                 }
198         }
199 }
200 #define CFS_RACE(id) cfs_race_loc(__FILE__, __func__, __LINE__, id)
201
202 /**
203  * Wait on race.
204  *
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.
208  */
209 static inline void cfs_race_wait_loc(const char *file, const char *func,
210                                      int line, __u32 id)
211 {
212         if (CFS_FAIL_PRECHECK(id)) {
213                 if (unlikely(__cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET))) {
214                         int rc;
215
216                         cfs_race_state = 0;
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",
223                                          id, rc);
224                 }
225         }
226 }
227 #define CFS_RACE_WAIT(id) cfs_race_wait_loc(__FILE__, __func__, __LINE__, id)
228
229 /**
230  * Wake up the thread that is waiting on the matching fail_loc.
231  */
232 static inline void cfs_race_wakeup_loc(const char *file, const char *func,
233                                        int line, __u32 id)
234 {
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);
239                         cfs_race_state = 1;
240                         wake_up(&cfs_race_waitq);
241                 }
242         }
243 }
244 #define CFS_RACE_WAKEUP(id) cfs_race_wakeup_loc(__FILE__, __func__, __LINE__,id)
245
246 #endif /* _LIBCFS_FAIL_H */