Whamcloud - gitweb
LU-1146 build: batch update copyright messages
[fs/lustre-release.git] / libcfs / libcfs / fail.c
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  * Copyright (c) 2012, Whamcloud, Inc.
32  */
33 /*
34  * This file is part of Lustre, http://www.lustre.org/
35  * Lustre is a trademark of Oracle Corporation, Inc.
36  */
37
38 #ifndef __KERNEL__
39 #include <liblustre.h>
40 #else
41 #include <libcfs/libcfs.h>
42 #endif
43
44 unsigned long cfs_fail_loc = 0;
45 unsigned int cfs_fail_val = 0;
46 cfs_waitq_t cfs_race_waitq;
47 int cfs_race_state;
48
49 CFS_EXPORT_SYMBOL(cfs_fail_loc);
50 CFS_EXPORT_SYMBOL(cfs_fail_val);
51 CFS_EXPORT_SYMBOL(cfs_race_waitq);
52 CFS_EXPORT_SYMBOL(cfs_race_state);
53
54 int __cfs_fail_check_set(__u32 id, __u32 value, int set)
55 {
56         static cfs_atomic_t cfs_fail_count = CFS_ATOMIC_INIT(0);
57
58         LASSERT(!(id & CFS_FAIL_ONCE));
59
60         if ((cfs_fail_loc & (CFS_FAILED | CFS_FAIL_ONCE)) ==
61             (CFS_FAILED | CFS_FAIL_ONCE)) {
62                 cfs_atomic_set(&cfs_fail_count, 0); /* paranoia */
63                 return 0;
64         }
65
66         /* Fail 1/cfs_fail_val times */
67         if (cfs_fail_loc & CFS_FAIL_RAND) {
68                 if (cfs_fail_val < 2 || cfs_rand() % cfs_fail_val > 0)
69                         return 0;
70         }
71
72         /* Skip the first cfs_fail_val, then fail */
73         if (cfs_fail_loc & CFS_FAIL_SKIP) {
74                 if (cfs_atomic_inc_return(&cfs_fail_count) <= cfs_fail_val)
75                         return 0;
76         }
77
78         /* check cfs_fail_val... */
79         if (set == CFS_FAIL_LOC_VALUE) {
80                 if (cfs_fail_val != -1 && cfs_fail_val != value)
81                         return 0;
82         }
83
84         /* Fail cfs_fail_val times, overridden by FAIL_ONCE */
85         if (cfs_fail_loc & CFS_FAIL_SOME &&
86             (!(cfs_fail_loc & CFS_FAIL_ONCE) || cfs_fail_val <= 1)) {
87                 int count = cfs_atomic_inc_return(&cfs_fail_count);
88
89                 if (count >= cfs_fail_val) {
90                         cfs_set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
91                         cfs_atomic_set(&cfs_fail_count, 0);
92                         /* we are lost race to increase  */
93                         if (count > cfs_fail_val)
94                                 return 0;
95                 }
96         }
97
98         if ((set == CFS_FAIL_LOC_ORSET || set == CFS_FAIL_LOC_RESET) &&
99             (value & CFS_FAIL_ONCE))
100                 cfs_set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
101         /* Lost race to set CFS_FAILED_BIT. */
102         if (cfs_test_and_set_bit(CFS_FAILED_BIT, &cfs_fail_loc)) {
103                 /* If CFS_FAIL_ONCE is valid, only one process can fail,
104                  * otherwise multi-process can fail at the same time. */
105                 if (cfs_fail_loc & CFS_FAIL_ONCE)
106                         return 0;
107         }
108
109         switch (set) {
110                 case CFS_FAIL_LOC_NOSET:
111                 case CFS_FAIL_LOC_VALUE:
112                         break;
113                 case CFS_FAIL_LOC_ORSET:
114                         cfs_fail_loc |= value & ~(CFS_FAILED | CFS_FAIL_ONCE);
115                         break;
116                 case CFS_FAIL_LOC_RESET:
117                         cfs_fail_loc = value;
118                         break;
119                 default:
120                         LASSERTF(0, "called with bad set %u\n", set);
121                         break;
122         }
123
124         return 1;
125 }
126 CFS_EXPORT_SYMBOL(__cfs_fail_check_set);
127
128 int __cfs_fail_timeout_set(__u32 id, __u32 value, int ms, int set)
129 {
130         int ret = 0;
131
132         ret = __cfs_fail_check_set(id, value, set);
133         if (ret) {
134                 CERROR("cfs_fail_timeout id %x sleeping for %dms\n",
135                        id, ms);
136                 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT,
137                                                    cfs_time_seconds(ms) / 1000);
138                 cfs_set_current_state(CFS_TASK_RUNNING);
139                 CERROR("cfs_fail_timeout id %x awake\n", id);
140         }
141         return ret;
142 }
143 CFS_EXPORT_SYMBOL(__cfs_fail_timeout_set);