1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#include "../../include/linux/libcfs/libcfs.h"
37
38unsigned long cfs_fail_loc = 0;
39EXPORT_SYMBOL(cfs_fail_loc);
40
41unsigned int cfs_fail_val = 0;
42EXPORT_SYMBOL(cfs_fail_val);
43
44wait_queue_head_t cfs_race_waitq;
45EXPORT_SYMBOL(cfs_race_waitq);
46
47int cfs_race_state;
48EXPORT_SYMBOL(cfs_race_state);
49
50int __cfs_fail_check_set(__u32 id, __u32 value, int set)
51{
52 static atomic_t cfs_fail_count = ATOMIC_INIT(0);
53
54 LASSERT(!(id & CFS_FAIL_ONCE));
55
56 if ((cfs_fail_loc & (CFS_FAILED | CFS_FAIL_ONCE)) ==
57 (CFS_FAILED | CFS_FAIL_ONCE)) {
58 atomic_set(&cfs_fail_count, 0);
59 return 0;
60 }
61
62
63 if (cfs_fail_loc & CFS_FAIL_RAND) {
64 if (cfs_fail_val < 2 || cfs_rand() % cfs_fail_val > 0)
65 return 0;
66 }
67
68
69 if (cfs_fail_loc & CFS_FAIL_SKIP) {
70 if (atomic_inc_return(&cfs_fail_count) <= cfs_fail_val)
71 return 0;
72 }
73
74
75 if (set == CFS_FAIL_LOC_VALUE) {
76 if (cfs_fail_val != -1 && cfs_fail_val != value)
77 return 0;
78 }
79
80
81 if (cfs_fail_loc & CFS_FAIL_SOME &&
82 (!(cfs_fail_loc & CFS_FAIL_ONCE) || cfs_fail_val <= 1)) {
83 int count = atomic_inc_return(&cfs_fail_count);
84
85 if (count >= cfs_fail_val) {
86 set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
87 atomic_set(&cfs_fail_count, 0);
88
89 if (count > cfs_fail_val)
90 return 0;
91 }
92 }
93
94 if ((set == CFS_FAIL_LOC_ORSET || set == CFS_FAIL_LOC_RESET) &&
95 (value & CFS_FAIL_ONCE))
96 set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
97
98 if (test_and_set_bit(CFS_FAILED_BIT, &cfs_fail_loc)) {
99
100
101 if (cfs_fail_loc & CFS_FAIL_ONCE)
102 return 0;
103 }
104
105 switch (set) {
106 case CFS_FAIL_LOC_NOSET:
107 case CFS_FAIL_LOC_VALUE:
108 break;
109 case CFS_FAIL_LOC_ORSET:
110 cfs_fail_loc |= value & ~(CFS_FAILED | CFS_FAIL_ONCE);
111 break;
112 case CFS_FAIL_LOC_RESET:
113 cfs_fail_loc = value;
114 break;
115 default:
116 LASSERTF(0, "called with bad set %u\n", set);
117 break;
118 }
119
120 return 1;
121}
122EXPORT_SYMBOL(__cfs_fail_check_set);
123
124int __cfs_fail_timeout_set(__u32 id, __u32 value, int ms, int set)
125{
126 int ret = 0;
127
128 ret = __cfs_fail_check_set(id, value, set);
129 if (ret) {
130 CERROR("cfs_fail_timeout id %x sleeping for %dms\n",
131 id, ms);
132 set_current_state(TASK_UNINTERRUPTIBLE);
133 schedule_timeout(cfs_time_seconds(ms) / 1000);
134 CERROR("cfs_fail_timeout id %x awake\n", id);
135 }
136 return ret;
137}
138EXPORT_SYMBOL(__cfs_fail_timeout_set);
139