1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
28 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
29 * Use is subject to license terms.
31 * Copyright (c) 2011, 2012, Whamcloud, Inc.
34 * This file is part of Lustre, http://www.lustre.org/
35 * Lustre is a trademark of Oracle Corporation, Inc.
38 #ifndef _LIBCFS_FAIL_H
39 #define _LIBCFS_FAIL_H
41 extern unsigned long cfs_fail_loc;
42 extern unsigned int cfs_fail_val;
44 extern cfs_waitq_t cfs_race_waitq;
45 extern int cfs_race_state;
47 int __cfs_fail_check_set(__u32 id, __u32 value, int set);
48 int __cfs_fail_timeout_set(__u32 id, __u32 value, int ms, int set);
51 CFS_FAIL_LOC_NOSET = 0,
52 CFS_FAIL_LOC_ORSET = 1,
53 CFS_FAIL_LOC_RESET = 2,
54 CFS_FAIL_LOC_VALUE = 3
57 /* Failure injection control */
58 #define CFS_FAIL_MASK_SYS 0x0000FF00
59 #define CFS_FAIL_MASK_LOC (0x000000FF | CFS_FAIL_MASK_SYS)
61 #define CFS_FAILED_BIT 30
62 /* CFS_FAILED is 0x40000000 */
63 #define CFS_FAILED (1 << CFS_FAILED_BIT)
65 #define CFS_FAIL_ONCE_BIT 31
66 /* CFS_FAIL_ONCE is 0x80000000 */
67 #define CFS_FAIL_ONCE (1 << CFS_FAIL_ONCE_BIT)
69 /* The following flags aren't made to be combined */
70 #define CFS_FAIL_SKIP 0x20000000 /* skip N times then fail */
71 #define CFS_FAIL_SOME 0x10000000 /* only fail N times */
72 #define CFS_FAIL_RAND 0x08000000 /* fail 1/N of the times */
73 #define CFS_FAIL_USR1 0x04000000 /* user flag */
75 #define CFS_FAIL_PRECHECK(id) (cfs_fail_loc && \
76 (cfs_fail_loc & CFS_FAIL_MASK_LOC) == \
77 ((id) & CFS_FAIL_MASK_LOC))
79 static inline int cfs_fail_check_set(__u32 id, __u32 value, int set)
83 if (unlikely(CFS_FAIL_PRECHECK(id) &&
84 (ret = __cfs_fail_check_set(id, value, set))))
85 LCONSOLE_INFO("*** cfs_fail_loc=%x, val=%u***\n", id, value);
90 /* If id hit cfs_fail_loc, return 1, otherwise return 0 */
91 #define CFS_FAIL_CHECK(id) \
92 cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET)
94 /* If id hit cfs_fail_loc and cfs_fail_val == (-1 or value) return 1,
95 * otherwise return 0 */
96 #define CFS_FAIL_CHECK_VALUE(id, value) \
97 cfs_fail_check_set(id, value, CFS_FAIL_LOC_VALUE)
99 /* If id hit cfs_fail_loc, cfs_fail_loc |= value and return 1,
100 * otherwise return 0 */
101 #define CFS_FAIL_CHECK_ORSET(id, value) \
102 cfs_fail_check_set(id, value, CFS_FAIL_LOC_ORSET)
104 /* If id hit cfs_fail_loc, cfs_fail_loc = value and return 1,
105 * otherwise return 0 */
106 #define CFS_FAIL_CHECK_RESET(id, value) \
107 cfs_fail_check_set(id, value, CFS_FAIL_LOC_RESET)
109 static inline int cfs_fail_timeout_set(__u32 id, __u32 value, int ms, int set)
111 if (unlikely(CFS_FAIL_PRECHECK(id)))
112 return __cfs_fail_timeout_set(id, value, ms, set);
117 /* If id hit cfs_fail_loc, sleep for seconds or milliseconds */
118 #define CFS_FAIL_TIMEOUT(id, secs) \
119 cfs_fail_timeout_set(id, 0, secs * 1000, CFS_FAIL_LOC_NOSET)
121 #define CFS_FAIL_TIMEOUT_MS(id, ms) \
122 cfs_fail_timeout_set(id, 0, ms, CFS_FAIL_LOC_NOSET)
124 /* If id hit cfs_fail_loc, cfs_fail_loc |= value and
125 * sleep seconds or milliseconds */
126 #define CFS_FAIL_TIMEOUT_ORSET(id, value, secs) \
127 cfs_fail_timeout_set(id, value, secs * 1000, CFS_FAIL_LOC_ORSET)
129 #define CFS_FAIL_TIMEOUT_MS_ORSET(id, value, ms) \
130 cfs_fail_timeout_set(id, value, ms, CFS_FAIL_LOC_ORSET)
133 /* The idea here is to synchronise two threads to force a race. The
134 * first thread that calls this with a matching fail_loc is put to
135 * sleep. The next thread that calls with the same fail_loc wakes up
136 * the first and continues. */
137 static inline void cfs_race(__u32 id)
140 if (CFS_FAIL_PRECHECK(id)) {
141 if (unlikely(__cfs_fail_check_set(id, 0, CFS_FAIL_LOC_NOSET))) {
144 CERROR("cfs_race id %x sleeping\n", id);
145 cfs_wait_event_interruptible(cfs_race_waitq,
146 cfs_race_state != 0, rc);
147 CERROR("cfs_fail_race id %x awake, rc=%d\n", id, rc);
149 CERROR("cfs_fail_race id %x waking\n", id);
151 cfs_waitq_signal(&cfs_race_waitq);
155 #define CFS_RACE(id) cfs_race(id)
157 /* sigh. an expedient fix until CFS_RACE is fixed up */
158 #define CFS_RACE(foo) do {} while(0)
161 #endif /* _LIBCFS_FAIL_H */