Whamcloud - gitweb
b=17491
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/lvfs/lvfs_lib.c
37  *
38  * Lustre filesystem abstraction routines
39  *
40  * Author: Andreas Dilger <adilger@clusterfs.com>
41  */
42 #ifdef __KERNEL__
43 #include <linux/module.h>
44 #include <linux/random.h>
45 #else
46 #include <liblustre.h>
47 #endif
48 #include <lustre_lib.h>
49 #include <lprocfs_status.h>
50
51 unsigned int obd_fail_val = 0;
52 unsigned long obd_fail_loc = 0;
53 unsigned int obd_alloc_fail_rate = 0;
54
55 int obd_alloc_fail(const void *ptr, const char *name, const char *type,
56                    size_t size, const char *file, int line)
57 {
58         if (ptr == NULL ||
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,
62                        line);
63                 CERROR(LPU64" total bytes and "LPU64" total pages "
64                        "("LPU64" bytes) allocated by Lustre, "
65                        "%d total bytes by LNET\n",
66                        obd_memory_sum(),
67                        obd_pages_sum() << CFS_PAGE_SHIFT,
68                        obd_pages_sum(),
69                        atomic_read(&libcfs_kmemory));
70                 return 1;
71         }
72         return 0;
73 }
74 EXPORT_SYMBOL(obd_alloc_fail);
75
76 int __obd_fail_check_set(__u32 id, __u32 value, int set)
77 {
78         static atomic_t obd_fail_count = ATOMIC_INIT(0);
79
80         LASSERT(!(id & OBD_FAIL_ONCE));
81
82         if ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE)) ==
83             (OBD_FAILED | OBD_FAIL_ONCE)) {
84                 atomic_set(&obd_fail_count, 0); /* paranoia */
85                 return 0;
86         }
87
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)
91                         return 0;
92         }
93
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)
97                         return 0;
98         }
99
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);
104
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)
110                                 return 0;
111                 }
112         }
113
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);
117
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)
123                         return 0;
124         }
125
126         switch (set) {
127                 case OBD_FAIL_LOC_NOSET:
128                         break;
129                 case OBD_FAIL_LOC_ORSET:
130                         obd_fail_loc |= value & ~(OBD_FAILED | OBD_FAIL_ONCE);
131                         break;
132                 case OBD_FAIL_LOC_RESET:
133                         obd_fail_loc = value;
134                         break;
135                 default:
136                         LASSERTF(0, "called with bad set %u\n", set);
137                         break;
138         }
139
140         return 1;
141 }
142 EXPORT_SYMBOL(__obd_fail_check_set);
143
144 int __obd_fail_timeout_set(__u32 id, __u32 value, int ms, int set)
145 {
146         int ret = 0;
147
148         ret = __obd_fail_check_set(id, value, set);
149         if (ret) {
150                 CERROR("obd_fail_timeout id %x sleeping for %dms\n",
151                        id, ms);
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);
156         }
157         return ret;
158 }
159 EXPORT_SYMBOL(__obd_fail_timeout_set);
160
161 #ifdef LPROCFS
162 void lprocfs_counter_add(struct lprocfs_stats *stats, int idx,
163                                        long amount)
164 {
165         struct lprocfs_counter *percpu_cntr;
166         int smp_id;
167
168         if (stats == NULL)
169                 return;
170
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);
174
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++;
178
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;
187         }
188         atomic_inc(&percpu_cntr->lc_cntl.la_exit);
189         lprocfs_stats_unlock(stats);
190 }
191 EXPORT_SYMBOL(lprocfs_counter_add);
192
193 void lprocfs_counter_sub(struct lprocfs_stats *stats, int idx,
194                                        long amount)
195 {
196         struct lprocfs_counter *percpu_cntr;
197         int smp_id;
198
199         if (stats == NULL)
200                 return;
201
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);
205
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);
212 }
213 EXPORT_SYMBOL(lprocfs_counter_sub);
214 #endif  /* LPROCFS */
215
216 EXPORT_SYMBOL(obd_fail_loc);
217 EXPORT_SYMBOL(obd_alloc_fail_rate);
218 EXPORT_SYMBOL(obd_fail_val);