Whamcloud - gitweb
1306c068b73bd4cef2c1c2a24f8df108c0d6be1d
[fs/lustre-release.git] / lustre / lvfs / lvfs_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/lvfs/lvfs_lib.c
5  *  Lustre filesystem abstraction routines
6  *
7  *  Copyright (C) 2007 Cluster File Systems, Inc.
8  *   Author: Andreas Dilger <adilger@clusterfs.com>
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
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.
15  *
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.
20  *
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.
24  */
25 #ifdef __KERNEL__
26 #include <linux/module.h>
27 #include <linux/random.h>
28 #else
29 #include <liblustre.h>
30 #endif
31 #include <lustre_lib.h>
32
33 unsigned int obd_fail_val = 0;
34 unsigned long obd_fail_loc = 0;
35 unsigned int obd_alloc_fail_rate = 0;
36
37 int obd_alloc_fail(const void *ptr, const char *name, const char *type,
38                    size_t size, const char *file, int line)
39 {
40         if (ptr == NULL ||
41             (ll_rand() & OBD_ALLOC_FAIL_MASK) < obd_alloc_fail_rate) {
42                 CERROR("%s%salloc of %s ("LPU64" bytes) failed at %s:%d\n",
43                        ptr ? "force " :"", type, name, (__u64)size, file,
44                        line);
45                 CERROR(LPU64" total bytes and "LPU64" total pages "
46                        "("LPU64" bytes) allocated by Lustre, "
47                        "%d total bytes by LNET\n",
48                        obd_memory_sum(),
49                        obd_pages_sum() << CFS_PAGE_SHIFT,
50                        obd_pages_sum(),
51                        atomic_read(&libcfs_kmemory));                
52                 return 1;
53         }
54         return 0;
55 }
56 EXPORT_SYMBOL(obd_alloc_fail);
57
58 int __obd_fail_check_set(__u32 id, __u32 value, int set)
59 {
60         static atomic_t obd_fail_count = ATOMIC_INIT(0);
61
62         LASSERT(!(id & OBD_FAIL_ONCE));
63
64         if ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE)) ==
65             (OBD_FAILED | OBD_FAIL_ONCE)) {
66                 atomic_set(&obd_fail_count, 0); /* paranoia */
67                 return 0;
68         }
69
70         /* Fail 1/obd_fail_val times */
71         if (obd_fail_loc & OBD_FAIL_RAND) {
72                 if (obd_fail_val < 2 || ll_rand() % obd_fail_val > 0)
73                         return 0;
74         }
75
76         /* Skip the first obd_fail_val, then fail */
77         if (obd_fail_loc & OBD_FAIL_SKIP) {
78                 if (atomic_inc_return(&obd_fail_count) <= obd_fail_val)
79                         return 0;
80         }
81
82         /* Fail obd_fail_val times, overridden by FAIL_ONCE */
83         if (obd_fail_loc & OBD_FAIL_SOME &&
84             (!(obd_fail_loc & OBD_FAIL_ONCE) || obd_fail_val <= 1)) { 
85                 int count = atomic_inc_return(&obd_fail_count);
86
87                 if (count >= obd_fail_val) {
88                         set_bit(OBD_FAIL_ONCE_BIT, &obd_fail_loc);
89                         atomic_set(&obd_fail_count, 0);
90                         /* we are lost race to increase obd_fail_count */
91                         if (count > obd_fail_val)
92                                 return 0;
93                 }
94         }
95
96         if ((set == OBD_FAIL_LOC_ORSET || set == OBD_FAIL_LOC_RESET) &&
97             (value & OBD_FAIL_ONCE))
98                 set_bit(OBD_FAIL_ONCE_BIT, &obd_fail_loc);
99
100         /* Lost race to set OBD_FAILED_BIT. */
101         if (test_and_set_bit(OBD_FAILED_BIT, &obd_fail_loc)) {
102                 /* If OBD_FAIL_ONCE is valid, only one process can fail,
103                  * otherwise multi-process can fail at the same time. */
104                 if (obd_fail_loc & OBD_FAIL_ONCE)
105                         return 0;
106         }
107
108         switch (set) {
109                 case OBD_FAIL_LOC_NOSET:
110                         break;
111                 case OBD_FAIL_LOC_ORSET:
112                         obd_fail_loc |= value & ~(OBD_FAILED | OBD_FAIL_ONCE);
113                         break;
114                 case OBD_FAIL_LOC_RESET:
115                         obd_fail_loc = value;
116                         break;
117                 default:
118                         LASSERTF(0, "called with bad set %u\n", set);
119                         break;
120         }
121
122         return 1;
123 }
124 EXPORT_SYMBOL(__obd_fail_check_set);
125
126 EXPORT_SYMBOL(obd_fail_loc);
127 EXPORT_SYMBOL(obd_alloc_fail_rate);
128 EXPORT_SYMBOL(obd_fail_val);