Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_fail.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
18  *
19  * GPL HEADER END
20  */
21 /*
22  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright (c) 2011, 2014, Intel Corporation.
26  */
27 /*
28  * This file is part of Lustre, http://www.lustre.org/
29  * Lustre is a trademark of Oracle Corporation, Inc.
30  */
31
32 #ifndef _LIBCFS_FAIL_H
33 #define _LIBCFS_FAIL_H
34
35 extern unsigned long cfs_fail_loc;
36 extern unsigned int cfs_fail_val;
37 extern int cfs_fail_err;
38
39 extern wait_queue_head_t cfs_race_waitq;
40 extern int cfs_race_state;
41
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);
45
46 enum {
47         CFS_FAIL_LOC_NOSET = 0,
48         CFS_FAIL_LOC_ORSET = 1,
49         CFS_FAIL_LOC_RESET = 2,
50         CFS_FAIL_LOC_VALUE = 3
51 };
52
53 /*
54  * Failure ranges:
55  *      "0x0100 - 0x3fff" for Lustre
56  *      "0xe000 - 0xefff" for LNet
57  *      "0xf000 - 0xffff" for LNDs
58  */
59
60 /* Failure injection control */
61 #define CFS_FAIL_MASK_SYS    0x0000FF00
62 #define CFS_FAIL_MASK_LOC   (0x000000FF | CFS_FAIL_MASK_SYS)
63
64 #define CFS_FAILED_BIT       30
65 /* CFS_FAILED is 0x40000000 */
66 #define CFS_FAILED          BIT(CFS_FAILED_BIT)
67
68 #define CFS_FAIL_ONCE_BIT    31
69 /* CFS_FAIL_ONCE is 0x80000000 */
70 #define CFS_FAIL_ONCE       BIT(CFS_FAIL_ONCE_BIT)
71
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 */
77
78 /* CFS_FAULT may be combined with any one of the above flags. */
79 #define CFS_FAULT            0x02000000 /* match any CFS_FAULT_CHECK */
80
81 static inline bool CFS_FAIL_PRECHECK(__u32 id)
82 {
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)));
86 }
87
88 #define UNLIKELY_CHECK_SET(id, value, set, quiet)                       \
89         (unlikely(cfs_fail_check_set_loc(__FILE__, __func__, __LINE__,  \
90                                          id, value, set, quiet)))
91
92 static inline int cfs_fail_check_set_loc(const char *file, const char *func,
93                                          int line, __u32 id, __u32 value,
94                                          int set, int quiet)
95 {
96         int ret = 0;
97
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;
101
102                 ret = __cfs_fail_check_set(id, value, set);
103                 if (ret)
104                         CDEBUG_LIMIT_LOC(file, func, line,
105                                          (quiet && failed_once) ?
106                                                 D_INFO : D_CONSOLE,
107                                          "*** cfs_fail_loc=%x, val=%u***\n",
108                                          id, value);
109         }
110
111         return ret;
112 }
113
114 /*
115  * If id hit cfs_fail_loc, return 1, otherwise return 0
116  */
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)
121
122 /*
123  * If id hit cfs_fail_loc and cfs_fail_val == (-1 or value) return 1,
124  * otherwise return 0
125  */
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)
130
131 /*
132  * If id hit cfs_fail_loc, cfs_fail_loc |= value and return 1,
133  * otherwise return 0
134  */
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)
139
140 /*
141  * If id hit cfs_fail_loc, cfs_fail_loc = value and return 1,
142  * otherwise return 0
143  */
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)
148
149 #define UNLIKELY_TIMEOUT_SET(id, value, ms, set) \
150         (unlikely(cfs_fail_timeout_set_loc(__FILE__, __func__, __LINE__, \
151                                            id, value, ms, set)))
152
153 static inline int cfs_fail_timeout_set_loc(const char *file, const char *func,
154                                            int line, __u32 id, __u32 value,
155                                            int ms, int set)
156 {
157         if (CFS_FAIL_PRECHECK(id))
158                 return __cfs_fail_timeout_set(file, func, line, id, value, ms,
159                                               set);
160         return 0;
161 }
162
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)
168
169 /*
170  * If id hit cfs_fail_loc, cfs_fail_loc |= value and
171  * sleep seconds or milliseconds
172  */
173 #define CFS_FAIL_TIMEOUT_ORSET(id, value, secs) \
174         UNLIKELY_TIMEOUT_SET(id, value, (secs) * 1000, CFS_FAIL_LOC_ORSET)
175
176 #define CFS_FAIL_TIMEOUT_RESET(id, value, secs) \
177         UNLIKELY_TIMEOUT_SET(id, value, (secs) * 1000, CFS_FAIL_LOC_RESET)
178
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))
183
184 /*
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.
189  */
190 static inline void cfs_race_loc(const char *file, const char *func, int line,
191                                 __u32 id)
192 {
193         if (CFS_FAIL_PRECHECK(id)) {
194                 if (unlikely(__cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET))) {
195                         int rc;
196                         cfs_race_state = 0;
197                         CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
198                                          "cfs_race id %x sleeping\n", id);
199                         /*
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
203                          */
204                         rc = wait_event_interruptible_timeout(cfs_race_waitq,
205                                                       cfs_race_state != 0,
206                                                       cfs_time_seconds(5));
207                         CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
208                                          "cfs_fail_race id %x awake: rc=%d\n",
209                                          id, rc);
210                 } else {
211                         CDEBUG_LIMIT_LOC(file, func, line, D_ERROR,
212                                          "cfs_fail_race id %x waking\n", id);
213                         cfs_race_state = 1;
214                         wake_up(&cfs_race_waitq);
215                 }
216         }
217 }
218 #define CFS_RACE(id) cfs_race_loc(__FILE__, __func__, __LINE__, id)
219
220 /**
221  * Wait on race.
222  *
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.
226  */
227 static inline void cfs_race_wait_loc(const char *file, const char *func,
228                                      int line, __u32 id)
229 {
230         if (CFS_FAIL_PRECHECK(id)) {
231                 if (unlikely(__cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET))) {
232                         int rc;
233
234                         cfs_race_state = 0;
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",
241                                          id, rc);
242                 }
243         }
244 }
245 #define CFS_RACE_WAIT(id) cfs_race_wait_loc(__FILE__, __func__, __LINE__, id)
246
247 /**
248  * Wake up the thread that is waiting on the matching fail_loc.
249  */
250 static inline void cfs_race_wakeup_loc(const char *file, const char *func,
251                                        int line, __u32 id)
252 {
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);
257                         cfs_race_state = 1;
258                         wake_up(&cfs_race_waitq);
259                 }
260         }
261 }
262 #define CFS_RACE_WAKEUP(id) cfs_race_wakeup_loc(__FILE__, __func__, __LINE__,id)
263
264 #endif /* _LIBCFS_FAIL_H */