Whamcloud - gitweb
LU-2590 lod: magic changed after swab
[fs/lustre-release.git] / libcfs / libcfs / fail.c
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  * Please contact Oracle Corporation, Inc., 500 Oracle Parkway, Redwood Shores,
20  * CA 94065 USA or visit www.oracle.com if you need additional information or
21  * have any questions.
22  *
23  * GPL HEADER END
24  */
25 /*
26  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
27  * Use is subject to license terms.
28  *
29  * Copyright (c) 2011, 2012, Intel Corporation.
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 __KERNEL__
37 #include <liblustre.h>
38 #else
39 #include <libcfs/libcfs.h>
40 #endif
41
42 unsigned long cfs_fail_loc = 0;
43 unsigned int cfs_fail_val = 0;
44 cfs_waitq_t cfs_race_waitq;
45 int cfs_race_state;
46
47 CFS_EXPORT_SYMBOL(cfs_fail_loc);
48 CFS_EXPORT_SYMBOL(cfs_fail_val);
49 CFS_EXPORT_SYMBOL(cfs_race_waitq);
50 CFS_EXPORT_SYMBOL(cfs_race_state);
51
52 int __cfs_fail_check_set(__u32 id, __u32 value, int set)
53 {
54         static cfs_atomic_t cfs_fail_count = CFS_ATOMIC_INIT(0);
55
56         LASSERT(!(id & CFS_FAIL_ONCE));
57
58         if ((cfs_fail_loc & (CFS_FAILED | CFS_FAIL_ONCE)) ==
59             (CFS_FAILED | CFS_FAIL_ONCE)) {
60                 cfs_atomic_set(&cfs_fail_count, 0); /* paranoia */
61                 return 0;
62         }
63
64         /* Fail 1/cfs_fail_val times */
65         if (cfs_fail_loc & CFS_FAIL_RAND) {
66                 if (cfs_fail_val < 2 || cfs_rand() % cfs_fail_val > 0)
67                         return 0;
68         }
69
70         /* Skip the first cfs_fail_val, then fail */
71         if (cfs_fail_loc & CFS_FAIL_SKIP) {
72                 if (cfs_atomic_inc_return(&cfs_fail_count) <= cfs_fail_val)
73                         return 0;
74         }
75
76         /* check cfs_fail_val... */
77         if (set == CFS_FAIL_LOC_VALUE) {
78                 if (cfs_fail_val != -1 && cfs_fail_val != value)
79                         return 0;
80         }
81
82         /* Fail cfs_fail_val times, overridden by FAIL_ONCE */
83         if (cfs_fail_loc & CFS_FAIL_SOME &&
84             (!(cfs_fail_loc & CFS_FAIL_ONCE) || cfs_fail_val <= 1)) {
85                 int count = cfs_atomic_inc_return(&cfs_fail_count);
86
87                 if (count >= cfs_fail_val) {
88                         set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
89                         cfs_atomic_set(&cfs_fail_count, 0);
90                         /* we are lost race to increase  */
91                         if (count > cfs_fail_val)
92                                 return 0;
93                 }
94         }
95
96         if ((set == CFS_FAIL_LOC_ORSET || set == CFS_FAIL_LOC_RESET) &&
97             (value & CFS_FAIL_ONCE))
98                 set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
99         /* Lost race to set CFS_FAILED_BIT. */
100         if (test_and_set_bit(CFS_FAILED_BIT, &cfs_fail_loc)) {
101                 /* If CFS_FAIL_ONCE is valid, only one process can fail,
102                  * otherwise multi-process can fail at the same time. */
103                 if (cfs_fail_loc & CFS_FAIL_ONCE)
104                         return 0;
105         }
106
107         switch (set) {
108                 case CFS_FAIL_LOC_NOSET:
109                 case CFS_FAIL_LOC_VALUE:
110                         break;
111                 case CFS_FAIL_LOC_ORSET:
112                         cfs_fail_loc |= value & ~(CFS_FAILED | CFS_FAIL_ONCE);
113                         break;
114                 case CFS_FAIL_LOC_RESET:
115                         cfs_fail_loc = value;
116                         break;
117                 default:
118                         LASSERTF(0, "called with bad set %u\n", set);
119                         break;
120         }
121
122         return 1;
123 }
124 CFS_EXPORT_SYMBOL(__cfs_fail_check_set);
125
126 int __cfs_fail_timeout_set(__u32 id, __u32 value, int ms, int set)
127 {
128         int ret = 0;
129
130         ret = __cfs_fail_check_set(id, value, set);
131         if (ret) {
132                 CERROR("cfs_fail_timeout id %x sleeping for %dms\n",
133                        id, ms);
134                 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT,
135                                                    cfs_time_seconds(ms) / 1000);
136                 cfs_set_current_state(CFS_TASK_RUNNING);
137                 CERROR("cfs_fail_timeout id %x awake\n", id);
138         }
139         return ret;
140 }
141 CFS_EXPORT_SYMBOL(__cfs_fail_timeout_set);