1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/lvfs/lvfs_lib.c
5 * Lustre filesystem abstraction routines
7 * Copyright (C) 2007 Cluster File Systems, Inc.
8 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * This file is part of Lustre, http://www.lustre.org.
12 * Lustre is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Lustre is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Lustre; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/module.h>
27 #include <linux/random.h>
29 #include <liblustre.h>
31 #include <lustre_lib.h>
33 __u64 obd_max_pages = 0;
34 __u64 obd_max_alloc = 0;
37 struct lprocfs_stats *obd_memory = NULL;
38 spinlock_t obd_updatemax_lock = SPIN_LOCK_UNLOCKED;
39 /* refine later and change to seqlock or simlar from libcfs */
45 unsigned int obd_fail_val = 0;
46 unsigned int obd_fail_loc = 0;
47 unsigned int obd_alloc_fail_rate = 0;
49 int obd_alloc_fail(const void *ptr, const char *name, const char *type,
50 size_t size, const char *file, int line)
53 (ll_rand() & OBD_ALLOC_FAIL_MASK) < obd_alloc_fail_rate) {
54 CERROR("%s%salloc of %s ("LPU64" bytes) failed at %s:%d\n",
55 ptr ? "force " :"", type, name, (__u64)size, file,
57 CERROR(LPU64" total bytes and "LPU64" total pages "
58 "("LPU64" bytes) allocated by Lustre, "
59 "%d total bytes by LNET\n",
61 obd_pages_sum() << CFS_PAGE_SHIFT,
63 atomic_read(&libcfs_kmemory));
70 void obd_update_maxusage()
74 max1 = obd_pages_sum();
75 max2 = obd_memory_sum();
77 spin_lock(&obd_updatemax_lock);
78 if (max1 > obd_max_pages)
80 if (max2 > obd_max_alloc)
82 spin_unlock(&obd_updatemax_lock);
86 __u64 obd_memory_max(void)
90 spin_lock(&obd_updatemax_lock);
92 spin_unlock(&obd_updatemax_lock);
97 __u64 obd_pages_max(void)
101 spin_lock(&obd_updatemax_lock);
103 spin_unlock(&obd_updatemax_lock);
108 EXPORT_SYMBOL(obd_update_maxusage);
109 EXPORT_SYMBOL(obd_pages_max);
110 EXPORT_SYMBOL(obd_memory_max);
111 EXPORT_SYMBOL(obd_memory);
116 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
117 enum lprocfs_fields_flags field)
125 centry = atomic_read(&lc->lc_cntl.la_entry);
128 case LPROCFS_FIELDS_FLAGS_CONFIG:
131 case LPROCFS_FIELDS_FLAGS_SUM:
134 case LPROCFS_FIELDS_FLAGS_MIN:
137 case LPROCFS_FIELDS_FLAGS_MAX:
140 case LPROCFS_FIELDS_FLAGS_AVG:
141 ret = (lc->lc_max - lc->lc_min)/2;
143 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
144 ret = lc->lc_sumsquare;
146 case LPROCFS_FIELDS_FLAGS_COUNT:
152 } while (centry != atomic_read(&lc->lc_cntl.la_entry) &&
153 centry != atomic_read(&lc->lc_cntl.la_exit));
157 EXPORT_SYMBOL(lprocfs_read_helper);
160 EXPORT_SYMBOL(obd_alloc_fail);
162 EXPORT_SYMBOL(obd_fail_loc);
163 EXPORT_SYMBOL(obd_alloc_fail_rate);
164 EXPORT_SYMBOL(obd_fail_val);
166 int obd_fail_check(__u32 id)
168 static int count = 0;
169 if (likely((obd_fail_loc & OBD_FAIL_MASK_LOC) !=
170 (id & OBD_FAIL_MASK_LOC)))
173 if ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE)) ==
174 (OBD_FAILED | OBD_FAIL_ONCE)) {
175 count = 0; /* paranoia */
179 if (obd_fail_loc & OBD_FAIL_RAND) {
180 unsigned int ll_rand(void);
181 if (obd_fail_val < 2)
183 if (ll_rand() % obd_fail_val > 0)
187 if (obd_fail_loc & OBD_FAIL_SKIP) {
189 if (count < obd_fail_val)
194 /* Overridden by FAIL_ONCE */
195 if (obd_fail_loc & OBD_FAIL_SOME) {
197 if (count >= obd_fail_val) {
199 /* Don't fail anymore */
200 obd_fail_loc |= OBD_FAIL_ONCE;
204 obd_fail_loc |= OBD_FAILED;
205 /* Handle old checks that OR in this */
206 if (id & OBD_FAIL_ONCE)
207 obd_fail_loc |= OBD_FAIL_ONCE;
211 EXPORT_SYMBOL(obd_fail_check);