]> Pileus Git - ~andy/linux/blob - drivers/staging/lustre/lustre/libcfs/fail.c
Merge tag 'arc-v3.14-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/vgupta/arc
[~andy/linux] / drivers / staging / lustre / lustre / 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 #include <linux/libcfs/libcfs.h>
37
38 unsigned long cfs_fail_loc = 0;
39 unsigned int cfs_fail_val = 0;
40 wait_queue_head_t cfs_race_waitq;
41 int cfs_race_state;
42
43 EXPORT_SYMBOL(cfs_fail_loc);
44 EXPORT_SYMBOL(cfs_fail_val);
45 EXPORT_SYMBOL(cfs_race_waitq);
46 EXPORT_SYMBOL(cfs_race_state);
47
48 int __cfs_fail_check_set(__u32 id, __u32 value, int set)
49 {
50         static atomic_t cfs_fail_count = ATOMIC_INIT(0);
51
52         LASSERT(!(id & CFS_FAIL_ONCE));
53
54         if ((cfs_fail_loc & (CFS_FAILED | CFS_FAIL_ONCE)) ==
55             (CFS_FAILED | CFS_FAIL_ONCE)) {
56                 atomic_set(&cfs_fail_count, 0); /* paranoia */
57                 return 0;
58         }
59
60         /* Fail 1/cfs_fail_val times */
61         if (cfs_fail_loc & CFS_FAIL_RAND) {
62                 if (cfs_fail_val < 2 || cfs_rand() % cfs_fail_val > 0)
63                         return 0;
64         }
65
66         /* Skip the first cfs_fail_val, then fail */
67         if (cfs_fail_loc & CFS_FAIL_SKIP) {
68                 if (atomic_inc_return(&cfs_fail_count) <= cfs_fail_val)
69                         return 0;
70         }
71
72         /* check cfs_fail_val... */
73         if (set == CFS_FAIL_LOC_VALUE) {
74                 if (cfs_fail_val != -1 && cfs_fail_val != value)
75                         return 0;
76         }
77
78         /* Fail cfs_fail_val times, overridden by FAIL_ONCE */
79         if (cfs_fail_loc & CFS_FAIL_SOME &&
80             (!(cfs_fail_loc & CFS_FAIL_ONCE) || cfs_fail_val <= 1)) {
81                 int count = atomic_inc_return(&cfs_fail_count);
82
83                 if (count >= cfs_fail_val) {
84                         set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
85                         atomic_set(&cfs_fail_count, 0);
86                         /* we are lost race to increase  */
87                         if (count > cfs_fail_val)
88                                 return 0;
89                 }
90         }
91
92         if ((set == CFS_FAIL_LOC_ORSET || set == CFS_FAIL_LOC_RESET) &&
93             (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                         break;
113                 default:
114                         LASSERTF(0, "called with bad set %u\n", set);
115                         break;
116         }
117
118         return 1;
119 }
120 EXPORT_SYMBOL(__cfs_fail_check_set);
121
122 int __cfs_fail_timeout_set(__u32 id, __u32 value, int ms, int set)
123 {
124         int ret = 0;
125
126         ret = __cfs_fail_check_set(id, value, set);
127         if (ret) {
128                 CERROR("cfs_fail_timeout id %x sleeping for %dms\n",
129                        id, ms);
130                 schedule_timeout_and_set_state(TASK_UNINTERRUPTIBLE,
131                                                    cfs_time_seconds(ms) / 1000);
132                 set_current_state(TASK_RUNNING);
133                 CERROR("cfs_fail_timeout id %x awake\n", id);
134         }
135         return ret;
136 }
137 EXPORT_SYMBOL(__cfs_fail_timeout_set);