4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Use is subject to license terms.
25 * Copyright (c) 2012, 2015, Intel Corporation.
28 * This file is part of Lustre, http://www.lustre.org/
29 * Lustre is a trademark of Oracle Corporation, Inc.
32 #include <libcfs/libcfs.h>
34 unsigned long cfs_fail_loc = 0;
35 unsigned int cfs_fail_val = 0;
37 DECLARE_WAIT_QUEUE_HEAD(cfs_race_waitq);
40 EXPORT_SYMBOL(cfs_fail_loc);
41 EXPORT_SYMBOL(cfs_fail_val);
42 EXPORT_SYMBOL(cfs_fail_err);
43 EXPORT_SYMBOL(cfs_race_waitq);
44 EXPORT_SYMBOL(cfs_race_state);
46 int __cfs_fail_check_set(__u32 id, __u32 value, int set)
48 static atomic_t cfs_fail_count = ATOMIC_INIT(0);
50 LASSERT(!(id & CFS_FAIL_ONCE));
52 if ((cfs_fail_loc & (CFS_FAILED | CFS_FAIL_ONCE)) ==
53 (CFS_FAILED | CFS_FAIL_ONCE)) {
54 atomic_set(&cfs_fail_count, 0); /* paranoia */
58 /* Fail 1/cfs_fail_val times */
59 if (cfs_fail_loc & CFS_FAIL_RAND) {
60 if (cfs_fail_val < 2 || cfs_rand() % cfs_fail_val > 0)
64 /* Skip the first cfs_fail_val, then fail */
65 if (cfs_fail_loc & CFS_FAIL_SKIP) {
66 if (atomic_inc_return(&cfs_fail_count) <= cfs_fail_val)
70 /* check cfs_fail_val... */
71 if (set == CFS_FAIL_LOC_VALUE) {
72 if (cfs_fail_val != -1 && cfs_fail_val != value)
76 /* Fail cfs_fail_val times, overridden by FAIL_ONCE */
77 if (cfs_fail_loc & CFS_FAIL_SOME &&
78 (!(cfs_fail_loc & CFS_FAIL_ONCE) || cfs_fail_val <= 1)) {
79 int count = atomic_inc_return(&cfs_fail_count);
81 if (count >= cfs_fail_val) {
82 set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
83 atomic_set(&cfs_fail_count, 0);
84 /* we are lost race to increase */
85 if (count > cfs_fail_val)
90 /* Take into account the current call for FAIL_ONCE for ORSET only,
91 * as RESET is a new fail_loc, it does not change the current call */
92 if ((set == CFS_FAIL_LOC_ORSET) && (value & CFS_FAIL_ONCE))
93 set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
94 /* Lost race to set CFS_FAILED_BIT. */
95 if (test_and_set_bit(CFS_FAILED_BIT, &cfs_fail_loc)) {
96 /* If CFS_FAIL_ONCE is valid, only one process can fail,
97 * otherwise multi-process can fail at the same time. */
98 if (cfs_fail_loc & CFS_FAIL_ONCE)
103 case CFS_FAIL_LOC_NOSET:
104 case CFS_FAIL_LOC_VALUE:
106 case CFS_FAIL_LOC_ORSET:
107 cfs_fail_loc |= value & ~(CFS_FAILED | CFS_FAIL_ONCE);
109 case CFS_FAIL_LOC_RESET:
110 cfs_fail_loc = value;
111 atomic_set(&cfs_fail_count, 0);
114 LASSERTF(0, "called with bad set %u\n", set);
120 EXPORT_SYMBOL(__cfs_fail_check_set);
122 int __cfs_fail_timeout_set(__u32 id, __u32 value, int ms, int set)
126 ret = __cfs_fail_check_set(id, value, set);
127 if (ret && likely(ms > 0)) {
128 CERROR("cfs_fail_timeout id %x sleeping for %dms\n",
130 set_current_state(TASK_UNINTERRUPTIBLE);
131 schedule_timeout(cfs_time_seconds(ms) / 1000);
132 set_current_state(TASK_RUNNING);
133 CERROR("cfs_fail_timeout id %x awake\n", id);
137 EXPORT_SYMBOL(__cfs_fail_timeout_set);