Merge branches 'acpi-ec' and 'acpi-button'
[cascardo/linux.git] / drivers / staging / lustre / lnet / 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) 2011, 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 "../../include/linux/libcfs/libcfs.h"
33
34 unsigned long cfs_fail_loc;
35 EXPORT_SYMBOL(cfs_fail_loc);
36
37 unsigned int cfs_fail_val;
38 EXPORT_SYMBOL(cfs_fail_val);
39
40 int cfs_fail_err;
41 EXPORT_SYMBOL(cfs_fail_err);
42
43 DECLARE_WAIT_QUEUE_HEAD(cfs_race_waitq);
44 EXPORT_SYMBOL(cfs_race_waitq);
45
46 int cfs_race_state;
47 EXPORT_SYMBOL(cfs_race_state);
48
49 int __cfs_fail_check_set(__u32 id, __u32 value, int set)
50 {
51         static atomic_t cfs_fail_count = ATOMIC_INIT(0);
52
53         LASSERT(!(id & CFS_FAIL_ONCE));
54
55         if ((cfs_fail_loc & (CFS_FAILED | CFS_FAIL_ONCE)) ==
56             (CFS_FAILED | CFS_FAIL_ONCE)) {
57                 atomic_set(&cfs_fail_count, 0); /* paranoia */
58                 return 0;
59         }
60
61         /* Fail 1/cfs_fail_val times */
62         if (cfs_fail_loc & CFS_FAIL_RAND) {
63                 if (cfs_fail_val < 2 || cfs_rand() % cfs_fail_val > 0)
64                         return 0;
65         }
66
67         /* Skip the first cfs_fail_val, then fail */
68         if (cfs_fail_loc & CFS_FAIL_SKIP) {
69                 if (atomic_inc_return(&cfs_fail_count) <= cfs_fail_val)
70                         return 0;
71         }
72
73         /* check cfs_fail_val... */
74         if (set == CFS_FAIL_LOC_VALUE) {
75                 if (cfs_fail_val != -1 && cfs_fail_val != value)
76                         return 0;
77         }
78
79         /* Fail cfs_fail_val times, overridden by FAIL_ONCE */
80         if (cfs_fail_loc & CFS_FAIL_SOME &&
81             (!(cfs_fail_loc & CFS_FAIL_ONCE) || cfs_fail_val <= 1)) {
82                 int count = atomic_inc_return(&cfs_fail_count);
83
84                 if (count >= cfs_fail_val) {
85                         set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
86                         atomic_set(&cfs_fail_count, 0);
87                         /* we are lost race to increase  */
88                         if (count > cfs_fail_val)
89                                 return 0;
90                 }
91         }
92
93         if ((set == CFS_FAIL_LOC_ORSET || set == CFS_FAIL_LOC_RESET) &&
94             (value & CFS_FAIL_ONCE))
95                 set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
96         /* Lost race to set CFS_FAILED_BIT. */
97         if (test_and_set_bit(CFS_FAILED_BIT, &cfs_fail_loc)) {
98                 /* If CFS_FAIL_ONCE is valid, only one process can fail,
99                  * otherwise multi-process can fail at the same time.
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 }
122 EXPORT_SYMBOL(__cfs_fail_check_set);
123
124 int __cfs_fail_timeout_set(__u32 id, __u32 value, int ms, int set)
125 {
126         int ret;
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",
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 }
138 EXPORT_SYMBOL(__cfs_fail_timeout_set);