Whamcloud - gitweb
LU-12501 utils: fix 'lfs df' printing loop
[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  * GPL HEADER END
20  */
21 /*
22  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright (c) 2012, 2015, Intel Corporation.
26  */
27 /*
28  * This file is part of Lustre, http://www.lustre.org/
29  * Lustre is a trademark of Oracle Corporation, Inc.
30  */
31
32 #include <linux/random.h>
33 #include <libcfs/libcfs.h>
34
35 unsigned long cfs_fail_loc = 0;
36 unsigned int cfs_fail_val = 0;
37 int cfs_fail_err;
38 DECLARE_WAIT_QUEUE_HEAD(cfs_race_waitq);
39 int cfs_race_state;
40
41 EXPORT_SYMBOL(cfs_fail_loc);
42 EXPORT_SYMBOL(cfs_fail_val);
43 EXPORT_SYMBOL(cfs_fail_err);
44 EXPORT_SYMBOL(cfs_race_waitq);
45 EXPORT_SYMBOL(cfs_race_state);
46
47 int __cfs_fail_check_set(__u32 id, __u32 value, int set)
48 {
49         static atomic_t cfs_fail_count = ATOMIC_INIT(0);
50
51         LASSERT(!(id & CFS_FAIL_ONCE));
52
53         if ((cfs_fail_loc & (CFS_FAILED | CFS_FAIL_ONCE)) ==
54             (CFS_FAILED | CFS_FAIL_ONCE)) {
55                 atomic_set(&cfs_fail_count, 0); /* paranoia */
56                 return 0;
57         }
58
59         /* Fail 1/cfs_fail_val times */
60         if (cfs_fail_loc & CFS_FAIL_RAND) {
61                 if (cfs_fail_val < 2 || prandom_u32_max(cfs_fail_val) > 0)
62                         return 0;
63         }
64
65         /* Skip the first cfs_fail_val, then fail */
66         if (cfs_fail_loc & CFS_FAIL_SKIP) {
67                 if (atomic_inc_return(&cfs_fail_count) <= cfs_fail_val)
68                         return 0;
69         }
70
71         /* check cfs_fail_val... */
72         if (set == CFS_FAIL_LOC_VALUE) {
73                 if (cfs_fail_val != -1 && cfs_fail_val != value)
74                         return 0;
75         }
76
77         /* Fail cfs_fail_val times, overridden by FAIL_ONCE */
78         if (cfs_fail_loc & CFS_FAIL_SOME &&
79             (!(cfs_fail_loc & CFS_FAIL_ONCE) || cfs_fail_val <= 1)) {
80                 int count = atomic_inc_return(&cfs_fail_count);
81
82                 if (count >= cfs_fail_val) {
83                         set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
84                         atomic_set(&cfs_fail_count, 0);
85                         /* we are lost race to increase  */
86                         if (count > cfs_fail_val)
87                                 return 0;
88                 }
89         }
90
91         /* Take into account the current call for FAIL_ONCE for ORSET only,
92          * as RESET is a new fail_loc, it does not change the current call */
93         if ((set == CFS_FAIL_LOC_ORSET) && (value & CFS_FAIL_ONCE))
94                 set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
95         /* Lost race to set CFS_FAILED_BIT. */
96         if (test_and_set_bit(CFS_FAILED_BIT, &cfs_fail_loc)) {
97                 /* If CFS_FAIL_ONCE is valid, only one process can fail,
98                  * otherwise multi-process can fail at the same time. */
99                 if (cfs_fail_loc & CFS_FAIL_ONCE)
100                         return 0;
101         }
102
103         switch (set) {
104                 case CFS_FAIL_LOC_NOSET:
105                 case CFS_FAIL_LOC_VALUE:
106                         break;
107                 case CFS_FAIL_LOC_ORSET:
108                         cfs_fail_loc |= value & ~(CFS_FAILED | CFS_FAIL_ONCE);
109                         break;
110                 case CFS_FAIL_LOC_RESET:
111                         cfs_fail_loc = value;
112                         atomic_set(&cfs_fail_count, 0);
113                         break;
114                 default:
115                         LASSERTF(0, "called with bad set %u\n", set);
116                         break;
117         }
118
119         return 1;
120 }
121 EXPORT_SYMBOL(__cfs_fail_check_set);
122
123 int __cfs_fail_timeout_set(__u32 id, __u32 value, int ms, int set)
124 {
125         ktime_t till = ktime_add_ms(ktime_get(), ms);
126         int ret = 0;
127
128         ret = __cfs_fail_check_set(id, value, set);
129         if (ret && likely(ms > 0)) {
130                 CERROR("cfs_fail_timeout id %x sleeping for %dms\n", id, ms);
131                 while (ktime_before(ktime_get(), till)) {
132                         set_current_state(TASK_UNINTERRUPTIBLE);
133                         schedule_timeout(msecs_to_jiffies(1000) / 10);
134                         set_current_state(TASK_RUNNING);
135                         if (!cfs_fail_loc) {
136                                 CERROR("cfs_fail_timeout interrupted\n");
137                                 break;
138                         }
139                 }
140                 if (cfs_fail_loc)
141                         CERROR("cfs_fail_timeout id %x awake\n", id);
142         }
143         return ret;
144 }
145 EXPORT_SYMBOL(__cfs_fail_timeout_set);