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
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/lvfs/lvfs_lib.c
38 * Lustre filesystem abstraction routines
40 * Author: Andreas Dilger <adilger@clusterfs.com>
43 #include <linux/module.h>
44 #include <linux/random.h>
46 #include <liblustre.h>
48 #include <lustre_lib.h>
49 #include <lprocfs_status.h>
51 unsigned int obd_fail_val = 0;
52 unsigned long obd_fail_loc = 0;
53 unsigned int obd_alloc_fail_rate = 0;
55 int obd_alloc_fail(const void *ptr, const char *name, const char *type,
56 size_t size, const char *file, int line)
59 (ll_rand() & OBD_ALLOC_FAIL_MASK) < obd_alloc_fail_rate) {
60 CERROR("%s%salloc of %s ("LPU64" bytes) failed at %s:%d\n",
61 ptr ? "force " :"", type, name, (__u64)size, file,
63 CERROR(LPU64" total bytes and "LPU64" total pages "
64 "("LPU64" bytes) allocated by Lustre, "
65 "%d total bytes by LNET\n",
67 obd_pages_sum() << CFS_PAGE_SHIFT,
69 atomic_read(&libcfs_kmemory));
74 EXPORT_SYMBOL(obd_alloc_fail);
76 int __obd_fail_check_set(__u32 id, __u32 value, int set)
78 static atomic_t obd_fail_count = ATOMIC_INIT(0);
80 LASSERT(!(id & OBD_FAIL_ONCE));
82 if ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE)) ==
83 (OBD_FAILED | OBD_FAIL_ONCE)) {
84 atomic_set(&obd_fail_count, 0); /* paranoia */
88 /* Fail 1/obd_fail_val times */
89 if (obd_fail_loc & OBD_FAIL_RAND) {
90 if (obd_fail_val < 2 || ll_rand() % obd_fail_val > 0)
94 /* Skip the first obd_fail_val, then fail */
95 if (obd_fail_loc & OBD_FAIL_SKIP) {
96 if (atomic_inc_return(&obd_fail_count) <= obd_fail_val)
100 /* Fail obd_fail_val times, overridden by FAIL_ONCE */
101 if (obd_fail_loc & OBD_FAIL_SOME &&
102 (!(obd_fail_loc & OBD_FAIL_ONCE) || obd_fail_val <= 1)) {
103 int count = atomic_inc_return(&obd_fail_count);
105 if (count >= obd_fail_val) {
106 set_bit(OBD_FAIL_ONCE_BIT, &obd_fail_loc);
107 atomic_set(&obd_fail_count, 0);
108 /* we are lost race to increase obd_fail_count */
109 if (count > obd_fail_val)
114 if ((set == OBD_FAIL_LOC_ORSET || set == OBD_FAIL_LOC_RESET) &&
115 (value & OBD_FAIL_ONCE))
116 set_bit(OBD_FAIL_ONCE_BIT, &obd_fail_loc);
118 /* Lost race to set OBD_FAILED_BIT. */
119 if (test_and_set_bit(OBD_FAILED_BIT, &obd_fail_loc)) {
120 /* If OBD_FAIL_ONCE is valid, only one process can fail,
121 * otherwise multi-process can fail at the same time. */
122 if (obd_fail_loc & OBD_FAIL_ONCE)
127 case OBD_FAIL_LOC_NOSET:
129 case OBD_FAIL_LOC_ORSET:
130 obd_fail_loc |= value & ~(OBD_FAILED | OBD_FAIL_ONCE);
132 case OBD_FAIL_LOC_RESET:
133 obd_fail_loc = value;
136 LASSERTF(0, "called with bad set %u\n", set);
142 EXPORT_SYMBOL(__obd_fail_check_set);
144 int __obd_fail_timeout_set(__u32 id, __u32 value, int ms, int set)
148 ret = __obd_fail_check_set(id, value, set);
150 CERROR("obd_fail_timeout id %x sleeping for %dms\n",
152 cfs_schedule_timeout(CFS_TASK_UNINT,
153 cfs_time_seconds(ms) / 1000);
154 set_current_state(CFS_TASK_RUNNING);
155 CERROR("obd_fail_timeout id %x awake\n", id);
159 EXPORT_SYMBOL(__obd_fail_timeout_set);
162 void lprocfs_counter_add(struct lprocfs_stats *stats, int idx,
165 struct lprocfs_counter *percpu_cntr;
171 /* With per-client stats, statistics are allocated only for
172 * single CPU area, so the smp_id should be 0 always. */
173 smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID);
175 percpu_cntr = &(stats->ls_percpu[smp_id]->lp_cntr[idx]);
176 atomic_inc(&percpu_cntr->lc_cntl.la_entry);
177 percpu_cntr->lc_count++;
179 if (percpu_cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) {
180 percpu_cntr->lc_sum += amount;
181 if (percpu_cntr->lc_config & LPROCFS_CNTR_STDDEV)
182 percpu_cntr->lc_sumsquare += (__s64)amount * amount;
183 if (amount < percpu_cntr->lc_min)
184 percpu_cntr->lc_min = amount;
185 if (amount > percpu_cntr->lc_max)
186 percpu_cntr->lc_max = amount;
188 atomic_inc(&percpu_cntr->lc_cntl.la_exit);
189 lprocfs_stats_unlock(stats);
191 EXPORT_SYMBOL(lprocfs_counter_add);
193 void lprocfs_counter_sub(struct lprocfs_stats *stats, int idx,
196 struct lprocfs_counter *percpu_cntr;
202 /* With per-client stats, statistics are allocated only for
203 * single CPU area, so the smp_id should be 0 always. */
204 smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID);
206 percpu_cntr = &(stats->ls_percpu[smp_id]->lp_cntr[idx]);
207 atomic_inc(&percpu_cntr->lc_cntl.la_entry);
208 if (percpu_cntr->lc_config & LPROCFS_CNTR_AVGMINMAX)
209 percpu_cntr->lc_sum -= amount;
210 atomic_inc(&percpu_cntr->lc_cntl.la_exit);
211 lprocfs_stats_unlock(stats);
213 EXPORT_SYMBOL(lprocfs_counter_sub);
216 EXPORT_SYMBOL(obd_fail_loc);
217 EXPORT_SYMBOL(obd_alloc_fail_rate);
218 EXPORT_SYMBOL(obd_fail_val);