]> git.proxmox.com Git - mirror_spl-debian.git/blob - module/spl/spl-generic.c
96a14c62e1fab9b63ecc29e4f98e91a96c7db266
[mirror_spl-debian.git] / module / spl / spl-generic.c
1 /*
2 * This file is part of the SPL: Solaris Porting Layer.
3 *
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
6 * Written by:
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
10 * UCRL-CODE-235197
11 *
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
27 #include <sys/sysmacros.h>
28 #include <sys/systeminfo.h>
29 #include <sys/vmsystm.h>
30 #include <sys/vnode.h>
31 #include <sys/kmem.h>
32 #include <sys/mutex.h>
33 #include <sys/taskq.h>
34 #include <sys/debug.h>
35 #include <sys/proc.h>
36 #include <sys/kstat.h>
37 #include <sys/utsname.h>
38 #include <linux/kmod.h>
39
40 #ifdef DEBUG_SUBSYSTEM
41 #undef DEBUG_SUBSYSTEM
42 #endif
43
44 #define DEBUG_SUBSYSTEM S_GENERIC
45
46 char spl_version[16] = "SPL v" VERSION;
47
48 long spl_hostid = 0;
49 EXPORT_SYMBOL(spl_hostid);
50
51 char hw_serial[HW_HOSTID_LEN] = "<none>";
52 EXPORT_SYMBOL(hw_serial);
53
54 int p0 = 0;
55 EXPORT_SYMBOL(p0);
56
57 int
58 highbit(unsigned long i)
59 {
60 register int h = 1;
61 ENTRY;
62
63 if (i == 0)
64 RETURN(0);
65 #if BITS_PER_LONG == 64
66 if (i & 0xffffffff00000000ul) {
67 h += 32; i >>= 32;
68 }
69 #endif
70 if (i & 0xffff0000) {
71 h += 16; i >>= 16;
72 }
73 if (i & 0xff00) {
74 h += 8; i >>= 8;
75 }
76 if (i & 0xf0) {
77 h += 4; i >>= 4;
78 }
79 if (i & 0xc) {
80 h += 2; i >>= 2;
81 }
82 if (i & 0x2) {
83 h += 1;
84 }
85 RETURN(h);
86 }
87 EXPORT_SYMBOL(highbit);
88
89 /*
90 * Implementation of 64 bit division for 32-bit machines.
91 */
92 #if BITS_PER_LONG == 32
93 uint64_t __udivdi3(uint64_t dividend, uint64_t divisor)
94 {
95 #ifdef HAVE_DIV64_64
96 return div64_64(dividend, divisor);
97 #else
98 /* Taken from a 2.6.24 kernel. */
99 uint32_t high, d;
100
101 high = divisor >> 32;
102 if (high) {
103 unsigned int shift = fls(high);
104
105 d = divisor >> shift;
106 dividend >>= shift;
107 } else
108 d = divisor;
109
110 do_div(dividend, d);
111
112 return dividend;
113 #endif
114 }
115 EXPORT_SYMBOL(__udivdi3);
116
117 /*
118 * Implementation of 64 bit modulo for 32-bit machines.
119 */
120 uint64_t __umoddi3(uint64_t dividend, uint64_t divisor)
121 {
122 return dividend - divisor * (dividend / divisor);
123 }
124 EXPORT_SYMBOL(__umoddi3);
125 #endif
126
127 /* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
128 * ddi_strtol(9F) man page. I have not verified the behavior of these
129 * functions against their Solaris counterparts. It is possible that I
130 * may have misinterpretted the man page or the man page is incorrect.
131 */
132 int ddi_strtoul(const char *, char **, int, unsigned long *);
133 int ddi_strtol(const char *, char **, int, long *);
134 int ddi_strtoull(const char *, char **, int, unsigned long long *);
135 int ddi_strtoll(const char *, char **, int, long long *);
136
137 #define define_ddi_strtoux(type, valtype) \
138 int ddi_strtou##type(const char *str, char **endptr, \
139 int base, valtype *result) \
140 { \
141 valtype last_value, value = 0; \
142 char *ptr = (char *)str; \
143 int flag = 1, digit; \
144 \
145 if (strlen(ptr) == 0) \
146 return EINVAL; \
147 \
148 /* Auto-detect base based on prefix */ \
149 if (!base) { \
150 if (str[0] == '0') { \
151 if (tolower(str[1])=='x' && isxdigit(str[2])) { \
152 base = 16; /* hex */ \
153 ptr += 2; \
154 } else if (str[1] >= '0' && str[1] < 8) { \
155 base = 8; /* octal */ \
156 ptr += 1; \
157 } else { \
158 return EINVAL; \
159 } \
160 } else { \
161 base = 10; /* decimal */ \
162 } \
163 } \
164 \
165 while (1) { \
166 if (isdigit(*ptr)) \
167 digit = *ptr - '0'; \
168 else if (isalpha(*ptr)) \
169 digit = tolower(*ptr) - 'a' + 10; \
170 else \
171 break; \
172 \
173 if (digit >= base) \
174 break; \
175 \
176 last_value = value; \
177 value = value * base + digit; \
178 if (last_value > value) /* Overflow */ \
179 return ERANGE; \
180 \
181 flag = 1; \
182 ptr++; \
183 } \
184 \
185 if (flag) \
186 *result = value; \
187 \
188 if (endptr) \
189 *endptr = (char *)(flag ? ptr : str); \
190 \
191 return 0; \
192 } \
193
194 #define define_ddi_strtox(type, valtype) \
195 int ddi_strto##type(const char *str, char **endptr, \
196 int base, valtype *result) \
197 { \
198 int rc; \
199 \
200 if (*str == '-') { \
201 rc = ddi_strtou##type(str + 1, endptr, base, result); \
202 if (!rc) { \
203 if (*endptr == str + 1) \
204 *endptr = (char *)str; \
205 else \
206 *result = -*result; \
207 } \
208 } else { \
209 rc = ddi_strtou##type(str, endptr, base, result); \
210 } \
211 \
212 return rc; \
213 }
214
215 define_ddi_strtoux(l, unsigned long)
216 define_ddi_strtox(l, long)
217 define_ddi_strtoux(ll, unsigned long long)
218 define_ddi_strtox(ll, long long)
219
220 EXPORT_SYMBOL(ddi_strtoul);
221 EXPORT_SYMBOL(ddi_strtol);
222 EXPORT_SYMBOL(ddi_strtoll);
223 EXPORT_SYMBOL(ddi_strtoull);
224
225 struct new_utsname *__utsname(void)
226 {
227 #ifdef HAVE_INIT_UTSNAME
228 return init_utsname();
229 #else
230 return &system_utsname;
231 #endif
232 }
233 EXPORT_SYMBOL(__utsname);
234
235 static int
236 set_hostid(void)
237 {
238 char sh_path[] = "/bin/sh";
239 char *argv[] = { sh_path,
240 "-c",
241 "/usr/bin/hostid >/proc/sys/kernel/spl/hostid",
242 NULL };
243 char *envp[] = { "HOME=/",
244 "TERM=linux",
245 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
246 NULL };
247
248 /* Doing address resolution in the kernel is tricky and just
249 * not a good idea in general. So to set the proper 'hw_serial'
250 * use the usermodehelper support to ask '/bin/sh' to run
251 * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid
252 * for us to use. It's a horific solution but it will do for now.
253 */
254 return call_usermodehelper(sh_path, argv, envp, 1);
255 }
256
257 uint32_t
258 zone_get_hostid(void *zone)
259 {
260 unsigned long hostid;
261
262 /* Only the global zone is supported */
263 ASSERT(zone == NULL);
264
265 if (ddi_strtoul(hw_serial, NULL, HW_HOSTID_LEN-1, &hostid) != 0)
266 return HW_INVALID_HOSTID;
267
268 return (uint32_t)hostid;
269 }
270 EXPORT_SYMBOL(zone_get_hostid);
271
272 static int __init spl_init(void)
273 {
274 int rc = 0;
275
276 if ((rc = debug_init()))
277 return rc;
278
279 if ((rc = spl_kmem_init()))
280 GOTO(out , rc);
281
282 if ((rc = spl_mutex_init()))
283 GOTO(out2 , rc);
284
285 if ((rc = spl_taskq_init()))
286 GOTO(out3, rc);
287
288 if ((rc = vn_init()))
289 GOTO(out4, rc);
290
291 if ((rc = proc_init()))
292 GOTO(out5, rc);
293
294 if ((rc = kstat_init()))
295 GOTO(out6, rc);
296
297 if ((rc = set_hostid()))
298 GOTO(out7, rc = -EADDRNOTAVAIL);
299
300 printk("SPL: Loaded Solaris Porting Layer v%s\n", VERSION);
301 RETURN(rc);
302 out7:
303 kstat_fini();
304 out6:
305 proc_fini();
306 out5:
307 vn_fini();
308 out4:
309 spl_taskq_fini();
310 out3:
311 spl_mutex_fini();
312 out2:
313 spl_kmem_fini();
314 out:
315 debug_fini();
316
317 printk("SPL: Failed to Load Solaris Porting Layer v%s, "
318 "rc = %d\n", VERSION, rc);
319 return rc;
320 }
321
322 static void spl_fini(void)
323 {
324 ENTRY;
325
326 printk("SPL: Unloaded Solaris Porting Layer v%s\n", VERSION);
327 kstat_fini();
328 proc_fini();
329 vn_fini();
330 spl_taskq_fini();
331 spl_mutex_fini();
332 spl_kmem_fini();
333 debug_fini();
334 }
335
336 module_init(spl_init);
337 module_exit(spl_fini);
338
339 MODULE_AUTHOR("Lawrence Livermore National Labs");
340 MODULE_DESCRIPTION("Solaris Porting Layer");
341 MODULE_LICENSE("GPL");