Whamcloud - gitweb
LU-506 kernel: FC15 - fix GCC 'set-but-unused' warnings
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_fail.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see http://www.gnu.org/licenses
20  *
21  * Please contact Oracle Corporation, Inc., 500 Oracle Parkway, Redwood Shores,
22  * CA 94065 USA or visit www.oracle.com if you need additional information or
23  * have any questions.
24  *
25  * GPL HEADER END
26  */
27 /*
28  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
29  * Use is subject to license terms.
30  */
31 /*
32  * This file is part of Lustre, http://www.lustre.org/
33  * Lustre is a trademark of Oracle Corporation, Inc.
34  */
35
36 #ifndef _LIBCFS_FAIL_H
37 #define _LIBCFS_FAIL_H
38
39 extern unsigned long cfs_fail_loc;
40 extern unsigned int cfs_fail_val;
41
42 extern cfs_waitq_t cfs_race_waitq;
43 extern int cfs_race_state;
44
45 int __cfs_fail_check_set(__u32 id, __u32 value, int set);
46 int __cfs_fail_timeout_set(__u32 id, __u32 value, int ms, int set);
47
48 enum {
49         CFS_FAIL_LOC_NOSET      = 0,
50         CFS_FAIL_LOC_ORSET      = 1,
51         CFS_FAIL_LOC_RESET      = 2
52 };
53
54 /* Failure injection control */
55 #define CFS_FAIL_MASK_SYS    0x0000FF00
56 #define CFS_FAIL_MASK_LOC   (0x000000FF | CFS_FAIL_MASK_SYS)
57
58 #define CFS_FAILED_BIT       30
59 /* CFS_FAILED is 0x40000000 */
60 #define CFS_FAILED          (1 << CFS_FAILED_BIT)
61
62 #define CFS_FAIL_ONCE_BIT    31
63 /* CFS_FAIL_ONCE is 0x80000000 */
64 #define CFS_FAIL_ONCE       (1 << CFS_FAIL_ONCE_BIT)
65
66 /* The following flags aren't made to be combined */
67 #define CFS_FAIL_SKIP        0x20000000 /* skip N times then fail */
68 #define CFS_FAIL_SOME        0x10000000 /* only fail N times */
69 #define CFS_FAIL_RAND        0x08000000 /* fail 1/N of the times */
70 #define CFS_FAIL_USR1        0x04000000 /* user flag */
71
72 #define CFS_FAIL_PRECHECK(id) (cfs_fail_loc &&                                \
73                               (cfs_fail_loc & CFS_FAIL_MASK_LOC) ==           \
74                               ((id) & CFS_FAIL_MASK_LOC))
75
76 static inline int cfs_fail_check_set(__u32 id, __u32 value, int set)
77 {
78         int ret = 0;
79         if (unlikely(CFS_FAIL_PRECHECK(id) &&
80             (ret = __cfs_fail_check_set(id, value, set)))) {
81                 CERROR("*** cfs_fail_loc=%x ***\n", id);
82         }
83         return ret;
84 }
85
86 /* If id hit cfs_fail_loc, return 1, otherwise return 0 */
87 #define CFS_FAIL_CHECK(id) \
88         cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET)
89
90 /* If id hit cfs_fail_loc, cfs_fail_loc |= value and return 1,
91  * otherwise return 0 */
92 #define CFS_FAIL_CHECK_ORSET(id, value) \
93         cfs_fail_check_set(id, value, CFS_FAIL_LOC_ORSET)
94
95 /* If id hit cfs_fail_loc, cfs_fail_loc = value and return 1,
96  * otherwise return 0 */
97 #define CFS_FAIL_CHECK_RESET(id, value) \
98         cfs_fail_check_set(id, value, CFS_FAIL_LOC_RESET)
99
100 static inline int cfs_fail_timeout_set(__u32 id, __u32 value, int ms, int set)
101 {
102         if (unlikely(CFS_FAIL_PRECHECK(id)))
103                 return __cfs_fail_timeout_set(id, value, ms, set);
104         else
105                 return 0;
106 }
107
108 /* If id hit cfs_fail_loc, sleep for seconds or milliseconds */
109 #define CFS_FAIL_TIMEOUT(id, secs) \
110         cfs_fail_timeout_set(id, 0, secs * 1000, CFS_FAIL_LOC_NOSET)
111
112 #define CFS_FAIL_TIMEOUT_MS(id, ms) \
113         cfs_fail_timeout_set(id, 0, ms, CFS_FAIL_LOC_NOSET)
114
115 /* If id hit cfs_fail_loc, cfs_fail_loc |= value and
116  * sleep seconds or milliseconds */
117 #define CFS_FAIL_TIMEOUT_ORSET(id, value, secs) \
118         cfs_fail_timeout_set(id, value, secs * 1000, CFS_FAIL_LOC_ORSET)
119
120 #define CFS_FAIL_TIMEOUT_MS_ORSET(id, value, ms) \
121         cfs_fail_timeout_set(id, value, ms, CFS_FAIL_LOC_ORSET)
122
123 #ifdef __KERNEL__
124 /* The idea here is to synchronise two threads to force a race. The
125  * first thread that calls this with a matching fail_loc is put to
126  * sleep. The next thread that calls with the same fail_loc wakes up
127  * the first and continues. */
128 static inline void cfs_race(__u32 id)
129 {
130
131         if (CFS_FAIL_PRECHECK(id)) {
132                 if (unlikely(__cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET))) {
133                         int rc;
134                         cfs_race_state = 0;
135                         CERROR("cfs_race id %x sleeping\n", id);
136                         cfs_wait_event_interruptible(cfs_race_waitq,
137                                                      cfs_race_state != 0, rc);
138                         CERROR("cfs_fail_race id %x awake, rc=%d\n", id, rc);
139                 } else {
140                         CERROR("cfs_fail_race id %x waking\n", id);
141                         cfs_race_state = 1;
142                         cfs_waitq_signal(&cfs_race_waitq);
143                 }
144         }
145 }
146 #define CFS_RACE(id) cfs_race(id)
147 #else
148 /* sigh.  an expedient fix until CFS_RACE is fixed up */
149 #define CFS_RACE(foo) do {} while(0)
150 #endif
151
152 #endif /* _LIBCFS_FAIL_H */