Whamcloud - gitweb
fix changelog
[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 (c) 2007, 2010, Oracle and/or its affiliates. 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 __u64 obd_max_pages = 0;
52 __u64 obd_max_alloc = 0;
53
54 #ifdef __KERNEL__
55 struct lprocfs_stats *obd_memory = NULL;
56 spinlock_t obd_updatemax_lock = SPIN_LOCK_UNLOCKED;
57 /* refine later and change to seqlock or simlar from libcfs */
58 #else
59 __u64 obd_alloc;
60 __u64 obd_pages;
61 #endif
62
63 unsigned int obd_fail_val = 0;
64 unsigned int obd_fail_loc = 0;
65 unsigned int obd_alloc_fail_rate = 0;
66
67 int obd_alloc_fail(const void *ptr, const char *name, const char *type,
68                    size_t size, const char *file, int line)
69 {
70         if (ptr == NULL ||
71             (ll_rand() & OBD_ALLOC_FAIL_MASK) < obd_alloc_fail_rate) {
72                 CERROR("%s%salloc of %s ("LPU64" bytes) failed at %s:%d\n",
73                        ptr ? "force " :"", type, name, (__u64)size, file,
74                        line);
75                 CERROR(LPU64" total bytes and "LPU64" total pages "
76                        "("LPU64" bytes) allocated by Lustre, "
77                        "%d total bytes by LNET\n",
78                        obd_memory_sum(),
79                        obd_pages_sum() << CFS_PAGE_SHIFT,
80                        obd_pages_sum(),
81                        atomic_read(&libcfs_kmemory));
82                return 1;
83         }
84         return 0;
85 }
86 EXPORT_SYMBOL(obd_alloc_fail);
87
88 #ifdef __KERNEL__
89 void obd_update_maxusage()
90 {
91         __u64 max1, max2;
92
93         max1 = obd_pages_sum();
94         max2 = obd_memory_sum();
95
96         spin_lock(&obd_updatemax_lock);
97         if (max1 > obd_max_pages)
98                 obd_max_pages = max1;
99         if (max2 > obd_max_alloc)
100                 obd_max_alloc = max2;
101         spin_unlock(&obd_updatemax_lock);
102 }
103
104 __u64 obd_memory_max(void)
105 {
106         __u64 ret;
107
108         spin_lock(&obd_updatemax_lock);
109         ret = obd_max_alloc;
110         spin_unlock(&obd_updatemax_lock);
111
112         return ret;
113 }
114
115 __u64 obd_pages_max(void)
116 {
117         __u64 ret;
118
119         spin_lock(&obd_updatemax_lock);
120         ret = obd_max_pages;
121         spin_unlock(&obd_updatemax_lock);
122
123         return ret;
124 }
125
126 EXPORT_SYMBOL(obd_update_maxusage);
127 EXPORT_SYMBOL(obd_pages_max);
128 EXPORT_SYMBOL(obd_memory_max);
129 EXPORT_SYMBOL(obd_memory);
130
131 #endif
132
133 #ifdef LPROCFS
134 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
135                           enum lprocfs_fields_flags field)
136 {
137         __s64 ret = 0;
138         int centry;
139
140         if (!lc)
141                 RETURN(0);
142         do {
143                 centry = atomic_read(&lc->lc_cntl.la_entry);
144
145                 switch (field) {
146                         case LPROCFS_FIELDS_FLAGS_CONFIG:
147                                 ret = lc->lc_config;
148                                 break;
149                         case LPROCFS_FIELDS_FLAGS_SUM:
150                                 ret = lc->lc_sum + lc->lc_sum_irq;
151                                 break;
152                         case LPROCFS_FIELDS_FLAGS_MIN:
153                                 ret = lc->lc_min;
154                                 break;
155                         case LPROCFS_FIELDS_FLAGS_MAX:
156                                 ret = lc->lc_max;
157                                 break;
158                         case LPROCFS_FIELDS_FLAGS_AVG:
159                                 ret = (lc->lc_max - lc->lc_min)/2;
160                                 break;
161                         case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
162                                 ret = lc->lc_sumsquare;
163                                 break;
164                         case LPROCFS_FIELDS_FLAGS_COUNT:
165                                 ret = lc->lc_count;
166                                 break;
167                         default:
168                                 break;
169                 };
170         } while (centry != atomic_read(&lc->lc_cntl.la_entry) &&
171                  centry != atomic_read(&lc->lc_cntl.la_exit));
172
173         RETURN(ret);
174 }
175 EXPORT_SYMBOL(lprocfs_read_helper);
176
177 void lprocfs_counter_add(struct lprocfs_stats *stats, int idx,
178                                        long amount)
179 {
180         struct lprocfs_counter *percpu_cntr;
181         int smp_id;
182
183         if (stats == NULL)
184                 return;
185
186         /* With per-client stats, statistics are allocated only for
187          * single CPU area, so the smp_id should be 0 always. */
188         smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID);
189
190         percpu_cntr = &(stats->ls_percpu[smp_id]->lp_cntr[idx]);
191         atomic_inc(&percpu_cntr->lc_cntl.la_entry);
192         percpu_cntr->lc_count++;
193
194         if (percpu_cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) {
195                 /* see comment in lprocfs_counter_sub */
196                 LASSERT(!cfs_in_interrupt());
197
198                 percpu_cntr->lc_sum += amount;
199                 if (percpu_cntr->lc_config & LPROCFS_CNTR_STDDEV)
200                         percpu_cntr->lc_sumsquare += (__u64)amount * amount;
201                 if (amount < percpu_cntr->lc_min)
202                         percpu_cntr->lc_min = amount;
203                 if (amount > percpu_cntr->lc_max)
204                         percpu_cntr->lc_max = amount;
205         }
206         atomic_inc(&percpu_cntr->lc_cntl.la_exit);
207         lprocfs_stats_unlock(stats);
208 }
209 EXPORT_SYMBOL(lprocfs_counter_add);
210
211 void lprocfs_counter_sub(struct lprocfs_stats *stats, int idx,
212                                        long amount)
213 {
214         struct lprocfs_counter *percpu_cntr;
215         int smp_id;
216
217         if (stats == NULL)
218                 return;
219
220         /* With per-client stats, statistics are allocated only for
221          * single CPU area, so the smp_id should be 0 always. */
222         smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID);
223
224         percpu_cntr = &(stats->ls_percpu[smp_id]->lp_cntr[idx]);
225         atomic_inc(&percpu_cntr->lc_cntl.la_entry);
226         if (percpu_cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) {
227                 /*
228                  * currently lprocfs_count_add() can only be called in thread
229                  * context; sometimes we use RCU callbacks to free memory
230                  * which calls lprocfs_counter_sub(), and RCU callbacks may
231                  * execute in softirq context - right now that's the only case
232                  * we're in softirq context here, use separate counter for that.
233                  * bz20650.
234                  */
235                 if (cfs_in_interrupt())
236                         percpu_cntr->lc_sum_irq -= amount;
237                 else
238                         percpu_cntr->lc_sum -= amount;
239         }
240         atomic_inc(&percpu_cntr->lc_cntl.la_exit);
241         lprocfs_stats_unlock(stats);
242 }
243 EXPORT_SYMBOL(lprocfs_counter_sub);
244 #endif  /* LPROCFS */
245
246 EXPORT_SYMBOL(obd_fail_loc);
247 EXPORT_SYMBOL(obd_alloc_fail_rate);
248 EXPORT_SYMBOL(obd_fail_val);
249
250 int obd_fail_check(__u32 id)
251 {
252         static int count = 0;
253         if (likely((obd_fail_loc & OBD_FAIL_MASK_LOC) !=
254                    (id & OBD_FAIL_MASK_LOC)))
255                 return 0;
256
257         if ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE)) ==
258             (OBD_FAILED | OBD_FAIL_ONCE)) {
259                 count = 0; /* paranoia */
260                 return 0;
261         }
262
263         if (obd_fail_loc & OBD_FAIL_RAND) {
264                 if (obd_fail_val < 2)
265                         return 0;
266                 if (ll_rand() % obd_fail_val > 0)
267                         return 0;
268         }
269
270         if (obd_fail_loc & OBD_FAIL_SKIP) {
271                 count++;
272                 if (count < obd_fail_val)
273                         return 0;
274                 count = 0;
275         }
276
277         /* Overridden by FAIL_ONCE */
278         if (obd_fail_loc & OBD_FAIL_SOME) {
279                 count++;
280                 if (count >= obd_fail_val) {
281                         count = 0;
282                         /* Don't fail anymore */
283                         obd_fail_loc |= OBD_FAIL_ONCE;
284                 }
285         }
286
287         obd_fail_loc |= OBD_FAILED;
288         /* Handle old checks that OR in this */
289         if (id & OBD_FAIL_ONCE)
290                 obd_fail_loc |= OBD_FAIL_ONCE;
291
292         return 1;
293 }
294 EXPORT_SYMBOL(obd_fail_check);