]> git.proxmox.com Git - mirror_zfs.git/blame - module/spl/spl-generic.c
Stack overflow on 64-bit modulus operations on 32-bit architectures.
[mirror_zfs.git] / module / spl / spl-generic.c
CommitLineData
716154c5
BB
1/*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
715f6251
BB
6 * UCRL-CODE-235197
7 *
716154c5
BB
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
715f6251 15 *
716154c5 16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
715f6251
BB
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
716154c5
BB
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Generic Implementation.
25\*****************************************************************************/
715f6251 26
14c5326c 27#include <sys/sysmacros.h>
99639e4a 28#include <sys/systeminfo.h>
af828292
BB
29#include <sys/vmsystm.h>
30#include <sys/vnode.h>
c19c06f3 31#include <sys/kmem.h>
9ab1ac14 32#include <sys/mutex.h>
d28db80f 33#include <sys/rwlock.h>
e9cb2b4f 34#include <sys/taskq.h>
8d0f1ee9 35#include <sys/debug.h>
57d1b188 36#include <sys/proc.h>
04a479f7 37#include <sys/kstat.h>
691d2bd7 38#include <sys/utsname.h>
d3126abe 39#include <sys/file.h>
f23e92fa 40#include <linux/kmod.h>
f1b59d26 41
57d1b188
BB
42#ifdef DEBUG_SUBSYSTEM
43#undef DEBUG_SUBSYSTEM
44#endif
8d0f1ee9 45
57d1b188 46#define DEBUG_SUBSYSTEM S_GENERIC
f23e92fa 47
0cbaeb11 48char spl_version[16] = "SPL v" SPL_META_VERSION;
3561541c 49
937879f1 50long spl_hostid = 0;
f23e92fa 51EXPORT_SYMBOL(spl_hostid);
8d0f1ee9 52
99639e4a 53char hw_serial[HW_HOSTID_LEN] = "<none>";
937879f1 54EXPORT_SYMBOL(hw_serial);
f1b59d26
BB
55
56int p0 = 0;
57EXPORT_SYMBOL(p0);
70eadc19 58
d1ff2312 59#ifndef HAVE_KALLSYMS_LOOKUP_NAME
96dded38 60kallsyms_lookup_name_t spl_kallsyms_lookup_name_fn = SYMBOL_POISON;
d1ff2312
BB
61#endif
62
77b1fe8f
BB
63int
64highbit(unsigned long i)
65{
66 register int h = 1;
3d061e9d 67 ENTRY;
77b1fe8f
BB
68
69 if (i == 0)
57d1b188 70 RETURN(0);
77b1fe8f
BB
71#if BITS_PER_LONG == 64
72 if (i & 0xffffffff00000000ul) {
73 h += 32; i >>= 32;
74 }
75#endif
76 if (i & 0xffff0000) {
77 h += 16; i >>= 16;
78 }
79 if (i & 0xff00) {
80 h += 8; i >>= 8;
81 }
82 if (i & 0xf0) {
83 h += 4; i >>= 4;
84 }
85 if (i & 0xc) {
86 h += 2; i >>= 2;
87 }
88 if (i & 0x2) {
89 h += 1;
90 }
57d1b188 91 RETURN(h);
77b1fe8f
BB
92}
93EXPORT_SYMBOL(highbit);
94
b61a6e8b 95/*
550f1705 96 * Implementation of 64 bit division for 32-bit machines.
b61a6e8b 97 */
550f1705 98#if BITS_PER_LONG == 32
1b4ad25e
AZ
99uint64_t
100__udivdi3(uint64_t dividend, uint64_t divisor)
b61a6e8b 101{
96dded38 102#if defined(HAVE_DIV64_64) /* 2.6.22 - 2.6.25 API */
550f1705 103 return div64_64(dividend, divisor);
96dded38
BB
104#elif defined(HAVE_DIV64_U64) /* 2.6.26 - 2.6.x API */
105 return div64_u64(dividend, divisor);
550f1705 106#else
96dded38 107 /* Implementation from 2.6.30 kernel */
b61a6e8b
BB
108 uint32_t high, d;
109
110 high = divisor >> 32;
111 if (high) {
112 unsigned int shift = fls(high);
113
114 d = divisor >> shift;
115 dividend >>= shift;
116 } else
117 d = divisor;
118
db1aa222
BB
119 do_div(dividend, d);
120
121 return dividend;
96dded38 122#endif /* HAVE_DIV64_64, HAVE_DIV64_U64 */
550f1705
BB
123}
124EXPORT_SYMBOL(__udivdi3);
125
126/*
127 * Implementation of 64 bit modulo for 32-bit machines.
128 */
1b4ad25e
AZ
129uint64_t
130__umoddi3(uint64_t dividend, uint64_t divisor)
550f1705 131{
1b4ad25e 132 return (dividend - (divisor * __udivdi3(dividend, divisor)));
b61a6e8b 133}
550f1705 134EXPORT_SYMBOL(__umoddi3);
96dded38 135#endif /* BITS_PER_LONG */
b61a6e8b 136
b871b8cd
BB
137/* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
138 * ddi_strtol(9F) man page. I have not verified the behavior of these
139 * functions against their Solaris counterparts. It is possible that I
96dded38 140 * may have misinterpreted the man page or the man page is incorrect.
b871b8cd 141 */
2ee63a54
BB
142int ddi_strtoul(const char *, char **, int, unsigned long *);
143int ddi_strtol(const char *, char **, int, long *);
144int ddi_strtoull(const char *, char **, int, unsigned long long *);
145int ddi_strtoll(const char *, char **, int, long long *);
146
147#define define_ddi_strtoux(type, valtype) \
148int ddi_strtou##type(const char *str, char **endptr, \
b871b8cd 149 int base, valtype *result) \
2ee63a54 150{ \
b871b8cd
BB
151 valtype last_value, value = 0; \
152 char *ptr = (char *)str; \
153 int flag = 1, digit; \
154 \
155 if (strlen(ptr) == 0) \
156 return EINVAL; \
157 \
158 /* Auto-detect base based on prefix */ \
159 if (!base) { \
160 if (str[0] == '0') { \
161 if (tolower(str[1])=='x' && isxdigit(str[2])) { \
162 base = 16; /* hex */ \
163 ptr += 2; \
164 } else if (str[1] >= '0' && str[1] < 8) { \
165 base = 8; /* octal */ \
166 ptr += 1; \
167 } else { \
168 return EINVAL; \
169 } \
170 } else { \
171 base = 10; /* decimal */ \
172 } \
173 } \
174 \
175 while (1) { \
176 if (isdigit(*ptr)) \
177 digit = *ptr - '0'; \
178 else if (isalpha(*ptr)) \
179 digit = tolower(*ptr) - 'a' + 10; \
180 else \
181 break; \
182 \
183 if (digit >= base) \
184 break; \
2ee63a54 185 \
b871b8cd
BB
186 last_value = value; \
187 value = value * base + digit; \
188 if (last_value > value) /* Overflow */ \
189 return ERANGE; \
2ee63a54 190 \
b871b8cd
BB
191 flag = 1; \
192 ptr++; \
2ee63a54
BB
193 } \
194 \
b871b8cd
BB
195 if (flag) \
196 *result = value; \
197 \
198 if (endptr) \
199 *endptr = (char *)(flag ? ptr : str); \
200 \
201 return 0; \
2ee63a54
BB
202} \
203
204#define define_ddi_strtox(type, valtype) \
205int ddi_strto##type(const char *str, char **endptr, \
206 int base, valtype *result) \
b871b8cd
BB
207{ \
208 int rc; \
2ee63a54
BB
209 \
210 if (*str == '-') { \
b871b8cd
BB
211 rc = ddi_strtou##type(str + 1, endptr, base, result); \
212 if (!rc) { \
213 if (*endptr == str + 1) \
214 *endptr = (char *)str; \
215 else \
216 *result = -*result; \
217 } \
2ee63a54 218 } else { \
b871b8cd 219 rc = ddi_strtou##type(str, endptr, base, result); \
2ee63a54
BB
220 } \
221 \
b871b8cd
BB
222 return rc; \
223}
2ee63a54
BB
224
225define_ddi_strtoux(l, unsigned long)
226define_ddi_strtox(l, long)
227define_ddi_strtoux(ll, unsigned long long)
228define_ddi_strtox(ll, long long)
229
2f5d55aa 230EXPORT_SYMBOL(ddi_strtoul);
2ee63a54
BB
231EXPORT_SYMBOL(ddi_strtol);
232EXPORT_SYMBOL(ddi_strtoll);
233EXPORT_SYMBOL(ddi_strtoull);
2f5d55aa 234
d3126abe
BB
235int
236ddi_copyin(const void *from, void *to, size_t len, int flags)
237{
238 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
239 if (flags & FKIOCTL) {
240 memcpy(to, from, len);
241 return 0;
242 }
243
244 return copyin(from, to, len);
245}
246EXPORT_SYMBOL(ddi_copyin);
247
248int
249ddi_copyout(const void *from, void *to, size_t len, int flags)
250{
251 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
252 if (flags & FKIOCTL) {
253 memcpy(to, from, len);
254 return 0;
255 }
256
257 return copyout(from, to, len);
258}
259EXPORT_SYMBOL(ddi_copyout);
260
e811949a
BB
261#ifndef HAVE_PUT_TASK_STRUCT
262/*
263 * This is only a stub function which should never be used. The SPL should
264 * never be putting away the last reference on a task structure so this will
265 * not be called. However, we still need to define it so the module does not
266 * have undefined symbol at load time. That all said if this impossible
267 * thing does somehow happen SBUG() immediately so we know about it.
268 */
269void
270__put_task_struct(struct task_struct *t)
271{
272 SBUG();
273}
274EXPORT_SYMBOL(__put_task_struct);
275#endif /* HAVE_PUT_TASK_STRUCT */
276
691d2bd7
BB
277struct new_utsname *__utsname(void)
278{
3d061e9d 279#ifdef HAVE_INIT_UTSNAME
691d2bd7 280 return init_utsname();
3d061e9d
BB
281#else
282 return &system_utsname;
283#endif
691d2bd7
BB
284}
285EXPORT_SYMBOL(__utsname);
286
8d0f1ee9 287static int
57d1b188 288set_hostid(void)
8d0f1ee9 289{
f23e92fa
BB
290 char sh_path[] = "/bin/sh";
291 char *argv[] = { sh_path,
292 "-c",
57d86234 293 "/usr/bin/hostid >/proc/sys/kernel/spl/hostid",
f23e92fa
BB
294 NULL };
295 char *envp[] = { "HOME=/",
296 "TERM=linux",
297 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
298 NULL };
96dded38 299 int rc;
8d0f1ee9 300
57d1b188 301 /* Doing address resolution in the kernel is tricky and just
937879f1 302 * not a good idea in general. So to set the proper 'hw_serial'
57d1b188
BB
303 * use the usermodehelper support to ask '/bin/sh' to run
304 * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid
96dded38 305 * for us to use. It's a horrific solution but it will do for now.
57d1b188 306 */
96dded38
BB
307 rc = call_usermodehelper(sh_path, argv, envp, 1);
308 if (rc)
309 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
310 argv[0], argv[1], argv[2], rc);
311
312 return rc;
57d1b188 313}
8d0f1ee9 314
99639e4a
BB
315uint32_t
316zone_get_hostid(void *zone)
317{
318 unsigned long hostid;
319
320 /* Only the global zone is supported */
321 ASSERT(zone == NULL);
322
323 if (ddi_strtoul(hw_serial, NULL, HW_HOSTID_LEN-1, &hostid) != 0)
324 return HW_INVALID_HOSTID;
325
326 return (uint32_t)hostid;
327}
328EXPORT_SYMBOL(zone_get_hostid);
329
96dded38 330#ifndef HAVE_KALLSYMS_LOOKUP_NAME
d1ff2312
BB
331/*
332 * Because kallsyms_lookup_name() is no longer exported in the
333 * mainline kernel we are forced to resort to somewhat drastic
334 * measures. This function replaces the functionality by performing
335 * an upcall to user space where /proc/kallsyms is consulted for
336 * the requested address.
337 */
338#define GET_KALLSYMS_ADDR_CMD \
339 "awk '{ if ( $3 == \"kallsyms_lookup_name\") { print $1 } }' " \
340 "/proc/kallsyms >/proc/sys/kernel/spl/kallsyms_lookup_name"
341
342static int
343set_kallsyms_lookup_name(void)
344{
345 char sh_path[] = "/bin/sh";
346 char *argv[] = { sh_path,
347 "-c",
348 GET_KALLSYMS_ADDR_CMD,
349 NULL };
350 char *envp[] = { "HOME=/",
351 "TERM=linux",
352 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
353 NULL };
354 int rc;
355
356 rc = call_usermodehelper(sh_path, argv, envp, 1);
357 if (rc)
96dded38
BB
358 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
359 argv[0], argv[1], argv[2], rc);
d1ff2312 360
96dded38 361 return rc;
d1ff2312
BB
362}
363#endif
364
51a727e9
BB
365static int
366__init spl_init(void)
57d1b188
BB
367{
368 int rc = 0;
f23e92fa 369
57d1b188 370 if ((rc = debug_init()))
18c9eadf 371 return rc;
f23e92fa 372
2fb9b26a 373 if ((rc = spl_kmem_init()))
d28db80f 374 GOTO(out1, rc);
8d0f1ee9 375
9ab1ac14 376 if ((rc = spl_mutex_init()))
d28db80f 377 GOTO(out2, rc);
9ab1ac14 378
d28db80f 379 if ((rc = spl_rw_init()))
9ab1ac14 380 GOTO(out3, rc);
8d0f1ee9 381
d28db80f 382 if ((rc = spl_taskq_init()))
9ab1ac14 383 GOTO(out4, rc);
af828292 384
d28db80f 385 if ((rc = vn_init()))
04a479f7
BB
386 GOTO(out5, rc);
387
d28db80f 388 if ((rc = proc_init()))
e9cb2b4f
BB
389 GOTO(out6, rc);
390
d28db80f
BB
391 if ((rc = kstat_init()))
392 GOTO(out7, rc);
393
57d1b188 394 if ((rc = set_hostid()))
d28db80f 395 GOTO(out8, rc = -EADDRNOTAVAIL);
f23e92fa 396
96dded38 397#ifndef HAVE_KALLSYMS_LOOKUP_NAME
d1ff2312 398 if ((rc = set_kallsyms_lookup_name()))
d28db80f 399 GOTO(out8, rc = -EADDRNOTAVAIL);
96dded38
BB
400#endif /* HAVE_KALLSYMS_LOOKUP_NAME */
401
402 if ((rc = spl_kmem_init_kallsyms_lookup()))
d28db80f 403 GOTO(out8, rc);
d1ff2312 404
0cbaeb11 405 printk("SPL: Loaded Solaris Porting Layer v%s\n", SPL_META_VERSION);
57d1b188 406 RETURN(rc);
d28db80f 407out8:
04a479f7 408 kstat_fini();
d28db80f 409out7:
57d1b188 410 proc_fini();
d28db80f 411out6:
57d1b188 412 vn_fini();
d28db80f 413out5:
e9cb2b4f 414 spl_taskq_fini();
d28db80f
BB
415out4:
416 spl_rw_fini();
9ab1ac14
BB
417out3:
418 spl_mutex_fini();
8d0f1ee9 419out2:
2fb9b26a 420 spl_kmem_fini();
d28db80f 421out1:
57d1b188 422 debug_fini();
8d0f1ee9 423
57d1b188 424 printk("SPL: Failed to Load Solaris Porting Layer v%s, "
0cbaeb11 425 "rc = %d\n", SPL_META_VERSION, rc);
18c9eadf 426 return rc;
70eadc19
BB
427}
428
51a727e9
BB
429static void
430spl_fini(void)
70eadc19 431{
57d1b188
BB
432 ENTRY;
433
0cbaeb11 434 printk("SPL: Unloaded Solaris Porting Layer v%s\n", SPL_META_VERSION);
04a479f7 435 kstat_fini();
57d1b188 436 proc_fini();
af828292 437 vn_fini();
e9cb2b4f 438 spl_taskq_fini();
d28db80f 439 spl_rw_fini();
2fb9b26a
BB
440 spl_mutex_fini();
441 spl_kmem_fini();
57d1b188 442 debug_fini();
70eadc19
BB
443}
444
51a727e9
BB
445/* Called when a dependent module is loaded */
446void
447spl_setup(void)
448{
82a358d9
BB
449 int rc;
450
51a727e9
BB
451 /*
452 * At module load time the pwd is set to '/' on a Solaris system.
453 * On a Linux system will be set to whatever directory the caller
454 * was in when executing insmod/modprobe.
455 */
82a358d9
BB
456 rc = vn_set_pwd("/");
457 if (rc)
458 printk("SPL: Warning unable to set pwd to '/': %d\n", rc);
51a727e9
BB
459}
460EXPORT_SYMBOL(spl_setup);
461
462/* Called when a dependent module is unloaded */
463void
464spl_cleanup(void)
465{
466}
467EXPORT_SYMBOL(spl_cleanup);
468
70eadc19
BB
469module_init(spl_init);
470module_exit(spl_fini);
471
472MODULE_AUTHOR("Lawrence Livermore National Labs");
473MODULE_DESCRIPTION("Solaris Porting Layer");
474MODULE_LICENSE("GPL");