1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see http://www.gnu.org/licenses
21 * Please contact Oracle Corporation, Inc., 500 Oracle Parkway, Redwood Shores,
22 * CA 94065 USA or visit www.oracle.com if you need additional information or
28 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
29 * Use is subject to license terms.
32 * This file is part of Lustre, http://www.lustre.org/
33 * Lustre is a trademark of Oracle Corporation, Inc.
37 #include <liblustre.h>
39 #include <libcfs/libcfs.h>
42 unsigned long cfs_fail_loc = 0;
43 unsigned int cfs_fail_val = 0;
44 cfs_waitq_t cfs_race_waitq;
47 CFS_EXPORT_SYMBOL(cfs_fail_loc);
48 CFS_EXPORT_SYMBOL(cfs_fail_val);
49 CFS_EXPORT_SYMBOL(cfs_race_waitq);
50 CFS_EXPORT_SYMBOL(cfs_race_state);
52 int __cfs_fail_check_set(__u32 id, __u32 value, int set)
54 static cfs_atomic_t cfs_fail_count = CFS_ATOMIC_INIT(0);
56 LASSERT(!(id & CFS_FAIL_ONCE));
58 if ((cfs_fail_loc & (CFS_FAILED | CFS_FAIL_ONCE)) ==
59 (CFS_FAILED | CFS_FAIL_ONCE)) {
60 cfs_atomic_set(&cfs_fail_count, 0); /* paranoia */
64 /* Fail 1/cfs_fail_val times */
65 if (cfs_fail_loc & CFS_FAIL_RAND) {
66 if (cfs_fail_val < 2 || cfs_rand() % cfs_fail_val > 0)
70 /* Skip the first cfs_fail_val, then fail */
71 if (cfs_fail_loc & CFS_FAIL_SKIP) {
72 if (cfs_atomic_inc_return(&cfs_fail_count) <= cfs_fail_val)
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 = cfs_atomic_inc_return(&cfs_fail_count);
81 if (count >= cfs_fail_val) {
82 cfs_set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
83 cfs_atomic_set(&cfs_fail_count, 0);
84 /* we are lost race to increase */
85 if (count > cfs_fail_val)
90 if ((set == CFS_FAIL_LOC_ORSET || set == CFS_FAIL_LOC_RESET) &&
91 (value & CFS_FAIL_ONCE))
92 cfs_set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
93 /* Lost race to set CFS_FAILED_BIT. */
94 if (cfs_test_and_set_bit(CFS_FAILED_BIT, &cfs_fail_loc)) {
95 /* If CFS_FAIL_ONCE is valid, only one process can fail,
96 * otherwise multi-process can fail at the same time. */
97 if (cfs_fail_loc & CFS_FAIL_ONCE)
102 case CFS_FAIL_LOC_NOSET:
104 case CFS_FAIL_LOC_ORSET:
105 cfs_fail_loc |= value & ~(CFS_FAILED | CFS_FAIL_ONCE);
107 case CFS_FAIL_LOC_RESET:
108 cfs_fail_loc = value;
111 LASSERTF(0, "called with bad set %u\n", set);
117 CFS_EXPORT_SYMBOL(__cfs_fail_check_set);
119 int __cfs_fail_timeout_set(__u32 id, __u32 value, int ms, int set)
123 ret = __cfs_fail_check_set(id, value, set);
125 CERROR("cfs_fail_timeout id %x sleeping for %dms\n",
127 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT,
128 cfs_time_seconds(ms) / 1000);
129 cfs_set_current_state(CFS_TASK_RUNNING);
130 CERROR("cfs_fail_timeout id %x awake\n", id);
134 CFS_EXPORT_SYMBOL(__cfs_fail_timeout_set);