GNU Linux-libre 4.14.265-gnu1
[releases.git] / drivers / staging / lustre / include / linux / 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, 2012, 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(u32 id, u32 value, int ms, int set);
44
45 enum {
46         CFS_FAIL_LOC_NOSET      = 0,
47         CFS_FAIL_LOC_ORSET      = 1,
48         CFS_FAIL_LOC_RESET      = 2,
49         CFS_FAIL_LOC_VALUE      = 3
50 };
51
52 /* Failure injection control */
53 #define CFS_FAIL_MASK_SYS    0x0000FF00
54 #define CFS_FAIL_MASK_LOC   (0x000000FF | CFS_FAIL_MASK_SYS)
55
56 #define CFS_FAILED_BIT       30
57 /* CFS_FAILED is 0x40000000 */
58 #define CFS_FAILED              BIT(CFS_FAILED_BIT)
59
60 #define CFS_FAIL_ONCE_BIT    31
61 /* CFS_FAIL_ONCE is 0x80000000 */
62 #define CFS_FAIL_ONCE           BIT(CFS_FAIL_ONCE_BIT)
63
64 /* The following flags aren't made to be combined */
65 #define CFS_FAIL_SKIP   0x20000000 /* skip N times then fail */
66 #define CFS_FAIL_SOME   0x10000000 /* only fail N times */
67 #define CFS_FAIL_RAND   0x08000000 /* fail 1/N of the times */
68 #define CFS_FAIL_USR1   0x04000000 /* user flag */
69
70 #define CFS_FAULT       0x02000000 /* match any CFS_FAULT_CHECK */
71
72 static inline bool CFS_FAIL_PRECHECK(u32 id)
73 {
74         return cfs_fail_loc &&
75                ((cfs_fail_loc & CFS_FAIL_MASK_LOC) == (id & CFS_FAIL_MASK_LOC) ||
76                 (cfs_fail_loc & id & CFS_FAULT));
77 }
78
79 static inline int cfs_fail_check_set(u32 id, u32 value,
80                                      int set, int quiet)
81 {
82         int ret = 0;
83
84         if (unlikely(CFS_FAIL_PRECHECK(id))) {
85                 ret = __cfs_fail_check_set(id, value, set);
86                 if (ret) {
87                         if (quiet) {
88                                 CDEBUG(D_INFO, "*** cfs_fail_loc=%x, val=%u***\n",
89                                        id, value);
90                         } else {
91                                 LCONSOLE_INFO("*** cfs_fail_loc=%x, val=%u***\n",
92                                               id, value);
93                         }
94                 }
95         }
96
97         return ret;
98 }
99
100 /* If id hit cfs_fail_loc, return 1, otherwise return 0 */
101 #define CFS_FAIL_CHECK(id) \
102         cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET, 0)
103 #define CFS_FAIL_CHECK_QUIET(id) \
104         cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET, 1)
105
106 /*
107  * If id hit cfs_fail_loc and cfs_fail_val == (-1 or value) return 1,
108  * otherwise return 0
109  */
110 #define CFS_FAIL_CHECK_VALUE(id, value) \
111         cfs_fail_check_set(id, value, CFS_FAIL_LOC_VALUE, 0)
112 #define CFS_FAIL_CHECK_VALUE_QUIET(id, value) \
113         cfs_fail_check_set(id, value, CFS_FAIL_LOC_VALUE, 1)
114
115 /*
116  * If id hit cfs_fail_loc, cfs_fail_loc |= value and return 1,
117  * otherwise return 0
118  */
119 #define CFS_FAIL_CHECK_ORSET(id, value) \
120         cfs_fail_check_set(id, value, CFS_FAIL_LOC_ORSET, 0)
121 #define CFS_FAIL_CHECK_ORSET_QUIET(id, value) \
122         cfs_fail_check_set(id, value, CFS_FAIL_LOC_ORSET, 1)
123
124 /*
125  * If id hit cfs_fail_loc, cfs_fail_loc = value and return 1,
126  * otherwise return 0
127  */
128 #define CFS_FAIL_CHECK_RESET(id, value) \
129         cfs_fail_check_set(id, value, CFS_FAIL_LOC_RESET, 0)
130 #define CFS_FAIL_CHECK_RESET_QUIET(id, value) \
131         cfs_fail_check_set(id, value, CFS_FAIL_LOC_RESET, 1)
132
133 static inline int cfs_fail_timeout_set(u32 id, u32 value, int ms, int set)
134 {
135         if (unlikely(CFS_FAIL_PRECHECK(id)))
136                 return __cfs_fail_timeout_set(id, value, ms, set);
137         return 0;
138 }
139
140 /* If id hit cfs_fail_loc, sleep for seconds or milliseconds */
141 #define CFS_FAIL_TIMEOUT(id, secs) \
142         cfs_fail_timeout_set(id, 0, secs * 1000, CFS_FAIL_LOC_NOSET)
143
144 #define CFS_FAIL_TIMEOUT_MS(id, ms) \
145         cfs_fail_timeout_set(id, 0, ms, CFS_FAIL_LOC_NOSET)
146
147 /*
148  * If id hit cfs_fail_loc, cfs_fail_loc |= value and
149  * sleep seconds or milliseconds
150  */
151 #define CFS_FAIL_TIMEOUT_ORSET(id, value, secs) \
152         cfs_fail_timeout_set(id, value, secs * 1000, CFS_FAIL_LOC_ORSET)
153
154 #define CFS_FAIL_TIMEOUT_RESET(id, value, secs) \
155         cfs_fail_timeout_set(id, value, secs * 1000, CFS_FAIL_LOC_RESET)
156
157 #define CFS_FAIL_TIMEOUT_MS_ORSET(id, value, ms) \
158         cfs_fail_timeout_set(id, value, ms, CFS_FAIL_LOC_ORSET)
159
160 #define CFS_FAULT_CHECK(id)                     \
161         CFS_FAIL_CHECK(CFS_FAULT | (id))
162
163 /*
164  * The idea here is to synchronise two threads to force a race. The
165  * first thread that calls this with a matching fail_loc is put to
166  * sleep. The next thread that calls with the same fail_loc wakes up
167  * the first and continues.
168  */
169 static inline void cfs_race(u32 id)
170 {
171         if (CFS_FAIL_PRECHECK(id)) {
172                 if (unlikely(__cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET))) {
173                         int rc;
174
175                         cfs_race_state = 0;
176                         CERROR("cfs_race id %x sleeping\n", id);
177                         rc = wait_event_interruptible(cfs_race_waitq,
178                                                       !!cfs_race_state);
179                         CERROR("cfs_fail_race id %x awake, rc=%d\n", id, rc);
180                 } else {
181                         CERROR("cfs_fail_race id %x waking\n", id);
182                         cfs_race_state = 1;
183                         wake_up(&cfs_race_waitq);
184                 }
185         }
186 }
187
188 #define CFS_RACE(id) cfs_race(id)
189
190 #endif /* _LIBCFS_FAIL_H */