Whamcloud - gitweb
Branch b1_6
[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 __u64 obd_max_pages = 0;
34 __u64 obd_max_alloc = 0;
35
36 #ifdef __KERNEL__
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 */
40 #else
41 __u64 obd_alloc;
42 __u64 obd_pages;
43 #endif
44
45 unsigned int obd_fail_val = 0;
46 unsigned int obd_fail_loc = 0;
47 unsigned int obd_alloc_fail_rate = 0;
48
49 int obd_alloc_fail(const void *ptr, const char *name, const char *type,
50                    size_t size, const char *file, int line)
51 {
52         if (ptr == NULL ||
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,
56                        line);
57                 CERROR(LPU64" total bytes and "LPU64" total pages "           
58                        "("LPU64" bytes) allocated by Lustre, "                
59                        "%d total bytes by LNET\n",                            
60                        obd_memory_sum(),                                     
61                        obd_pages_sum() << CFS_PAGE_SHIFT,                                      
62                        obd_pages_sum(),           
63                        atomic_read(&libcfs_kmemory));                         
64                return 1;
65         }
66         return 0;
67 }
68
69 #ifdef __KERNEL__
70 void obd_update_maxusage()
71 {
72         __u64 max1, max2;
73
74         max1 = obd_pages_sum();
75         max2 = obd_memory_sum();
76
77         spin_lock(&obd_updatemax_lock);
78         if (max1 > obd_max_pages)
79                 obd_max_pages = max1;
80         if (max2 > obd_max_alloc)
81                 obd_max_alloc = max2;
82         spin_unlock(&obd_updatemax_lock);
83         
84 }
85
86 __u64 obd_memory_max(void)
87 {
88         __u64 ret;
89
90         spin_lock(&obd_updatemax_lock);
91         ret = obd_max_alloc;
92         spin_unlock(&obd_updatemax_lock);
93
94         return ret;
95 }
96
97 __u64 obd_pages_max(void)
98 {
99         __u64 ret;
100
101         spin_lock(&obd_updatemax_lock);
102         ret = obd_max_pages;
103         spin_unlock(&obd_updatemax_lock);
104
105         return ret;
106 }
107
108 EXPORT_SYMBOL(obd_update_maxusage);
109 EXPORT_SYMBOL(obd_pages_max);
110 EXPORT_SYMBOL(obd_memory_max);
111 EXPORT_SYMBOL(obd_memory);
112
113 #endif
114
115 #ifdef LPROCFS
116 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
117                           enum lprocfs_fields_flags field)
118 {
119         __u64 ret = 0;
120         int centry;
121
122         if (!lc)
123                 RETURN(0);
124         do {
125                 centry = atomic_read(&lc->lc_cntl.la_entry);
126
127                 switch (field) {
128                         case LPROCFS_FIELDS_FLAGS_CONFIG:
129                                 ret = lc->lc_config;
130                                 break;
131                         case LPROCFS_FIELDS_FLAGS_SUM:
132                                 ret = lc->lc_sum;
133                                 break;
134                         case LPROCFS_FIELDS_FLAGS_MIN:
135                                 ret = lc->lc_min;
136                                 break;
137                         case LPROCFS_FIELDS_FLAGS_MAX:
138                                 ret = lc->lc_max;
139                                 break;
140                         case LPROCFS_FIELDS_FLAGS_AVG:
141                                 ret = (lc->lc_max - lc->lc_min)/2;
142                                 break;
143                         case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
144                                 ret = lc->lc_sumsquare;
145                                 break;
146                         case LPROCFS_FIELDS_FLAGS_COUNT:
147                                 ret = lc->lc_count;
148                                 break;
149                         default:
150                                 break;
151                 };
152         } while (centry != atomic_read(&lc->lc_cntl.la_entry) &&
153                  centry != atomic_read(&lc->lc_cntl.la_exit));
154
155         RETURN(ret);
156 }
157 EXPORT_SYMBOL(lprocfs_read_helper);
158 #endif
159
160 EXPORT_SYMBOL(obd_alloc_fail);
161
162 EXPORT_SYMBOL(obd_fail_loc);
163 EXPORT_SYMBOL(obd_alloc_fail_rate);
164 EXPORT_SYMBOL(obd_fail_val);
165
166 int obd_fail_check(__u32 id)
167 {
168         static int count = 0;
169         if (likely((obd_fail_loc & OBD_FAIL_MASK_LOC) !=
170                    (id & OBD_FAIL_MASK_LOC)))
171                 return 0;
172
173         if ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE)) ==
174             (OBD_FAILED | OBD_FAIL_ONCE)) {
175                 count = 0; /* paranoia */
176                 return 0;
177         }
178
179         if (obd_fail_loc & OBD_FAIL_RAND) {
180                 unsigned int ll_rand(void);
181                 if (obd_fail_val < 2)
182                         return 0;
183                 if (ll_rand() % obd_fail_val > 0)
184                         return 0;
185         }
186
187         if (obd_fail_loc & OBD_FAIL_SKIP) {
188                 count++;
189                 if (count < obd_fail_val)
190                         return 0;
191                 count = 0;
192         }
193
194         /* Overridden by FAIL_ONCE */
195         if (obd_fail_loc & OBD_FAIL_SOME) {
196                 count++;
197                 if (count >= obd_fail_val) {
198                         count = 0;
199                         /* Don't fail anymore */
200                         obd_fail_loc |= OBD_FAIL_ONCE;
201                 }
202         }
203
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;
208
209         return 1;
210 }
211 EXPORT_SYMBOL(obd_fail_check);