]> git.proxmox.com Git - mirror_spl.git/blame - module/spl/spl-generic.c
Remove kallsyms_lookup_name() wrapper
[mirror_spl.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 6 * UCRL-CODE-235197
7 *
716154c5 8 * This file is part of the SPL, Solaris Porting Layer.
3d6af2dd 9 * For details, see <http://zfsonlinux.org/>.
716154c5
BB
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 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 29#include <sys/vmsystm.h>
0d54dcb5 30#include <sys/kobj.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>
9fe45dc1 35#include <sys/tsd.h>
5c1967eb 36#include <sys/zmod.h>
8d0f1ee9 37#include <sys/debug.h>
57d1b188 38#include <sys/proc.h>
04a479f7 39#include <sys/kstat.h>
d3126abe 40#include <sys/file.h>
f23e92fa 41#include <linux/kmod.h>
ae4c36ad 42#include <linux/proc_compat.h>
55abb092 43#include <spl-debug.h>
f1b59d26 44
b17edc10
BB
45#ifdef SS_DEBUG_SUBSYS
46#undef SS_DEBUG_SUBSYS
57d1b188 47#endif
8d0f1ee9 48
b17edc10 49#define SS_DEBUG_SUBSYS SS_GENERIC
f23e92fa 50
0835057e 51char spl_version[32] = "SPL v" SPL_META_VERSION "-" SPL_META_RELEASE;
1a73940d 52EXPORT_SYMBOL(spl_version);
3561541c 53
acf0ade3 54unsigned long spl_hostid = 0;
f23e92fa 55EXPORT_SYMBOL(spl_hostid);
fa6f7d8f
DH
56module_param(spl_hostid, ulong, 0644);
57MODULE_PARM_DESC(spl_hostid, "The system hostid.");
8d0f1ee9 58
ae4c36ad 59proc_t p0 = { 0 };
f1b59d26 60EXPORT_SYMBOL(p0);
70eadc19 61
a4bfd8ea 62#if BITS_PER_LONG == 32
b61a6e8b 63/*
a4bfd8ea
BB
64 * Support 64/64 => 64 division on a 32-bit platform. While the kernel
65 * provides a div64_u64() function for this we do not use it because the
66 * implementation is flawed. There are cases which return incorrect
67 * results as late as linux-2.6.35. Until this is fixed upstream the
68 * spl must provide its own implementation.
69 *
70 * This implementation is a slightly modified version of the algorithm
71 * proposed by the book 'Hacker's Delight'. The original source can be
72 * found here and is available for use without restriction.
73 *
74 * http://www.hackersdelight.org/HDcode/newCode/divDouble.c
75 */
76
77/*
78 * Calculate number of leading of zeros for a 64-bit value.
79 */
80static int
81nlz64(uint64_t x) {
82 register int n = 0;
83
84 if (x == 0)
85 return 64;
86
87 if (x <= 0x00000000FFFFFFFFULL) {n = n + 32; x = x << 32;}
88 if (x <= 0x0000FFFFFFFFFFFFULL) {n = n + 16; x = x << 16;}
89 if (x <= 0x00FFFFFFFFFFFFFFULL) {n = n + 8; x = x << 8;}
90 if (x <= 0x0FFFFFFFFFFFFFFFULL) {n = n + 4; x = x << 4;}
91 if (x <= 0x3FFFFFFFFFFFFFFFULL) {n = n + 2; x = x << 2;}
92 if (x <= 0x7FFFFFFFFFFFFFFFULL) {n = n + 1;}
93
94 return n;
95}
96
97/*
98 * Newer kernels have a div_u64() function but we define our own
99 * to simplify portibility between kernel versions.
100 */
101static inline uint64_t
102__div_u64(uint64_t u, uint32_t v)
103{
104 (void) do_div(u, v);
105 return u;
106}
107
108/*
109 * Implementation of 64-bit unsigned division for 32-bit machines.
110 *
111 * First the procedure takes care of the case in which the divisor is a
112 * 32-bit quantity. There are two subcases: (1) If the left half of the
113 * dividend is less than the divisor, one execution of do_div() is all that
114 * is required (overflow is not possible). (2) Otherwise it does two
115 * divisions, using the grade school method.
b61a6e8b 116 */
1b4ad25e 117uint64_t
a4bfd8ea 118__udivdi3(uint64_t u, uint64_t v)
b61a6e8b 119{
a4bfd8ea
BB
120 uint64_t u0, u1, v1, q0, q1, k;
121 int n;
122
123 if (v >> 32 == 0) { // If v < 2**32:
124 if (u >> 32 < v) { // If u/v cannot overflow,
125 return __div_u64(u, v); // just do one division.
126 } else { // If u/v would overflow:
127 u1 = u >> 32; // Break u into two halves.
128 u0 = u & 0xFFFFFFFF;
129 q1 = __div_u64(u1, v); // First quotient digit.
130 k = u1 - q1 * v; // First remainder, < v.
131 u0 += (k << 32);
132 q0 = __div_u64(u0, v); // Seconds quotient digit.
133 return (q1 << 32) + q0;
134 }
135 } else { // If v >= 2**32:
136 n = nlz64(v); // 0 <= n <= 31.
137 v1 = (v << n) >> 32; // Normalize divisor, MSB is 1.
138 u1 = u >> 1; // To ensure no overflow.
139 q1 = __div_u64(u1, v1); // Get quotient from
140 q0 = (q1 << n) >> 31; // Undo normalization and
141 // division of u by 2.
142 if (q0 != 0) // Make q0 correct or
143 q0 = q0 - 1; // too small by 1.
144 if ((u - q0 * v) >= v)
145 q0 = q0 + 1; // Now q0 is correct.
ef6f91ce 146
a4bfd8ea
BB
147 return q0;
148 }
550f1705 149}
150EXPORT_SYMBOL(__udivdi3);
151
152/*
a4bfd8ea
BB
153 * Implementation of 64-bit signed division for 32-bit machines.
154 */
155int64_t
156__divdi3(int64_t u, int64_t v)
157{
158 int64_t q, t;
159 q = __udivdi3(abs64(u), abs64(v));
160 t = (u ^ v) >> 63; // If u, v have different
161 return (q ^ t) - t; // signs, negate q.
162}
163EXPORT_SYMBOL(__divdi3);
164
165/*
166 * Implementation of 64-bit unsigned modulo for 32-bit machines.
550f1705 167 */
1b4ad25e
AZ
168uint64_t
169__umoddi3(uint64_t dividend, uint64_t divisor)
550f1705 170{
1b4ad25e 171 return (dividend - (divisor * __udivdi3(dividend, divisor)));
b61a6e8b 172}
550f1705 173EXPORT_SYMBOL(__umoddi3);
a4bfd8ea 174
ef6f91ce
JL
175#if defined(__arm) || defined(__arm__)
176/*
93b0dc92
JL
177 * Implementation of 64-bit (un)signed division for 32-bit arm machines.
178 *
179 * Run-time ABI for the ARM Architecture (page 20). A pair of (unsigned)
180 * long longs is returned in {{r0, r1}, {r2,r3}}, the quotient in {r0, r1},
181 * and the remainder in {r2, r3}. The return type is specifically left
182 * set to 'void' to ensure the compiler does not overwrite these registers
183 * during the return. All results are in registers as per ABI
ef6f91ce 184 */
93b0dc92 185void
ef6f91ce
JL
186__aeabi_uldivmod(uint64_t u, uint64_t v)
187{
93b0dc92
JL
188 uint64_t res;
189 uint64_t mod;
190
191 res = __udivdi3(u, v);
192 mod = __umoddi3(u, v);
193 {
194 register uint32_t r0 asm("r0") = (res & 0xFFFFFFFF);
195 register uint32_t r1 asm("r1") = (res >> 32);
196 register uint32_t r2 asm("r2") = (mod & 0xFFFFFFFF);
197 register uint32_t r3 asm("r3") = (mod >> 32);
198
199 asm volatile(""
200 : "+r"(r0), "+r"(r1), "+r"(r2),"+r"(r3) /* output */
201 : "r"(r0), "r"(r1), "r"(r2), "r"(r3)); /* input */
202
203 return; /* r0; */
204 }
ef6f91ce
JL
205}
206EXPORT_SYMBOL(__aeabi_uldivmod);
207
93b0dc92 208void
ef6f91ce
JL
209__aeabi_ldivmod(int64_t u, int64_t v)
210{
93b0dc92
JL
211 int64_t res;
212 uint64_t mod;
213
214 res = __divdi3(u, v);
215 mod = __umoddi3(u, v);
216 {
217 register uint32_t r0 asm("r0") = (res & 0xFFFFFFFF);
218 register uint32_t r1 asm("r1") = (res >> 32);
219 register uint32_t r2 asm("r2") = (mod & 0xFFFFFFFF);
220 register uint32_t r3 asm("r3") = (mod >> 32);
221
222 asm volatile(""
223 : "+r"(r0), "+r"(r1), "+r"(r2),"+r"(r3) /* output */
224 : "r"(r0), "r"(r1), "r"(r2), "r"(r3)); /* input */
225
226 return; /* r0; */
227 }
ef6f91ce
JL
228}
229EXPORT_SYMBOL(__aeabi_ldivmod);
230#endif /* __arm || __arm__ */
96dded38 231#endif /* BITS_PER_LONG */
b61a6e8b 232
b871b8cd
BB
233/* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
234 * ddi_strtol(9F) man page. I have not verified the behavior of these
235 * functions against their Solaris counterparts. It is possible that I
96dded38 236 * may have misinterpreted the man page or the man page is incorrect.
b871b8cd 237 */
2ee63a54
BB
238int ddi_strtoul(const char *, char **, int, unsigned long *);
239int ddi_strtol(const char *, char **, int, long *);
240int ddi_strtoull(const char *, char **, int, unsigned long long *);
241int ddi_strtoll(const char *, char **, int, long long *);
242
243#define define_ddi_strtoux(type, valtype) \
244int ddi_strtou##type(const char *str, char **endptr, \
b871b8cd 245 int base, valtype *result) \
2ee63a54 246{ \
b871b8cd
BB
247 valtype last_value, value = 0; \
248 char *ptr = (char *)str; \
249 int flag = 1, digit; \
250 \
251 if (strlen(ptr) == 0) \
252 return EINVAL; \
253 \
254 /* Auto-detect base based on prefix */ \
255 if (!base) { \
256 if (str[0] == '0') { \
257 if (tolower(str[1])=='x' && isxdigit(str[2])) { \
258 base = 16; /* hex */ \
259 ptr += 2; \
260 } else if (str[1] >= '0' && str[1] < 8) { \
261 base = 8; /* octal */ \
262 ptr += 1; \
263 } else { \
264 return EINVAL; \
265 } \
266 } else { \
267 base = 10; /* decimal */ \
268 } \
269 } \
270 \
271 while (1) { \
272 if (isdigit(*ptr)) \
273 digit = *ptr - '0'; \
274 else if (isalpha(*ptr)) \
275 digit = tolower(*ptr) - 'a' + 10; \
276 else \
277 break; \
278 \
279 if (digit >= base) \
280 break; \
2ee63a54 281 \
b871b8cd
BB
282 last_value = value; \
283 value = value * base + digit; \
284 if (last_value > value) /* Overflow */ \
285 return ERANGE; \
2ee63a54 286 \
b871b8cd
BB
287 flag = 1; \
288 ptr++; \
2ee63a54
BB
289 } \
290 \
b871b8cd
BB
291 if (flag) \
292 *result = value; \
293 \
294 if (endptr) \
295 *endptr = (char *)(flag ? ptr : str); \
296 \
297 return 0; \
2ee63a54
BB
298} \
299
300#define define_ddi_strtox(type, valtype) \
301int ddi_strto##type(const char *str, char **endptr, \
302 int base, valtype *result) \
b871b8cd
BB
303{ \
304 int rc; \
2ee63a54
BB
305 \
306 if (*str == '-') { \
b871b8cd
BB
307 rc = ddi_strtou##type(str + 1, endptr, base, result); \
308 if (!rc) { \
309 if (*endptr == str + 1) \
310 *endptr = (char *)str; \
311 else \
312 *result = -*result; \
313 } \
2ee63a54 314 } else { \
b871b8cd 315 rc = ddi_strtou##type(str, endptr, base, result); \
2ee63a54
BB
316 } \
317 \
b871b8cd
BB
318 return rc; \
319}
2ee63a54
BB
320
321define_ddi_strtoux(l, unsigned long)
322define_ddi_strtox(l, long)
323define_ddi_strtoux(ll, unsigned long long)
324define_ddi_strtox(ll, long long)
325
2f5d55aa 326EXPORT_SYMBOL(ddi_strtoul);
2ee63a54
BB
327EXPORT_SYMBOL(ddi_strtol);
328EXPORT_SYMBOL(ddi_strtoll);
329EXPORT_SYMBOL(ddi_strtoull);
2f5d55aa 330
d3126abe
BB
331int
332ddi_copyin(const void *from, void *to, size_t len, int flags)
333{
334 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
335 if (flags & FKIOCTL) {
336 memcpy(to, from, len);
337 return 0;
338 }
339
340 return copyin(from, to, len);
341}
342EXPORT_SYMBOL(ddi_copyin);
343
344int
345ddi_copyout(const void *from, void *to, size_t len, int flags)
346{
347 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
348 if (flags & FKIOCTL) {
349 memcpy(to, from, len);
350 return 0;
351 }
352
353 return copyout(from, to, len);
354}
355EXPORT_SYMBOL(ddi_copyout);
356
e811949a
BB
357#ifndef HAVE_PUT_TASK_STRUCT
358/*
359 * This is only a stub function which should never be used. The SPL should
360 * never be putting away the last reference on a task structure so this will
361 * not be called. However, we still need to define it so the module does not
362 * have undefined symbol at load time. That all said if this impossible
55abb092 363 * thing does somehow happen PANIC immediately so we know about it.
e811949a
BB
364 */
365void
366__put_task_struct(struct task_struct *t)
367{
55abb092 368 PANIC("Unexpectly put last reference on task %d\n", (int)t->pid);
e811949a
BB
369}
370EXPORT_SYMBOL(__put_task_struct);
371#endif /* HAVE_PUT_TASK_STRUCT */
372
0d54dcb5
DH
373/*
374 * Read the unique system identifier from the /etc/hostid file.
375 *
376 * The behavior of /usr/bin/hostid on Linux systems with the
377 * regular eglibc and coreutils is:
378 *
379 * 1. Generate the value if the /etc/hostid file does not exist
380 * or if the /etc/hostid file is less than four bytes in size.
381 *
382 * 2. If the /etc/hostid file is at least 4 bytes, then return
383 * the first four bytes [0..3] in native endian order.
384 *
385 * 3. Always ignore bytes [4..] if they exist in the file.
386 *
387 * Only the first four bytes are significant, even on systems that
388 * have a 64-bit word size.
389 *
390 * See:
391 *
392 * eglibc: sysdeps/unix/sysv/linux/gethostid.c
393 * coreutils: src/hostid.c
394 *
395 * Notes:
396 *
397 * The /etc/hostid file on Solaris is a text file that often reads:
398 *
399 * # DO NOT EDIT
400 * "0123456789"
401 *
402 * Directly copying this file to Linux results in a constant
403 * hostid of 4f442023 because the default comment constitutes
404 * the first four bytes of the file.
405 *
406 */
407
408char *spl_hostid_path = HW_HOSTID_PATH;
409module_param(spl_hostid_path, charp, 0444);
410MODULE_PARM_DESC(spl_hostid_path, "The system hostid file (/etc/hostid)");
411
412static int
413hostid_read(void)
414{
415 int result;
416 uint64_t size;
417 struct _buf *file;
acf0ade3 418 uint32_t hostid = 0;
0d54dcb5
DH
419
420 file = kobj_open_file(spl_hostid_path);
421
6b3b569d 422 if (file == (struct _buf *)-1)
0d54dcb5 423 return -1;
0d54dcb5
DH
424
425 result = kobj_get_filesize(file, &size);
426
427 if (result != 0) {
428 printk(KERN_WARNING
429 "SPL: kobj_get_filesize returned %i on %s\n",
430 result, spl_hostid_path);
431 kobj_close_file(file);
432 return -2;
433 }
434
435 if (size < sizeof(HW_HOSTID_MASK)) {
436 printk(KERN_WARNING
437 "SPL: Ignoring the %s file because it is %llu bytes; "
e8267acd
BB
438 "expecting %lu bytes instead.\n", spl_hostid_path,
439 size, (unsigned long)sizeof(HW_HOSTID_MASK));
0d54dcb5
DH
440 kobj_close_file(file);
441 return -3;
442 }
443
444 /* Read directly into the variable like eglibc does. */
445 /* Short reads are okay; native behavior is preserved. */
446 result = kobj_read_file(file, (char *)&hostid, sizeof(hostid), 0);
447
448 if (result < 0) {
449 printk(KERN_WARNING
450 "SPL: kobj_read_file returned %i on %s\n",
451 result, spl_hostid_path);
452 kobj_close_file(file);
453 return -4;
454 }
455
456 /* Mask down to 32 bits like coreutils does. */
457 spl_hostid = hostid & HW_HOSTID_MASK;
458 kobj_close_file(file);
459 return 0;
460}
461
99639e4a
BB
462uint32_t
463zone_get_hostid(void *zone)
464{
a9f2397e 465 static int first = 1;
99639e4a
BB
466
467 /* Only the global zone is supported */
468 ASSERT(zone == NULL);
469
a9f2397e
ED
470 if (first) {
471 first = 0;
472
473 /*
474 * Get the hostid if it was not passed as a module parameter.
acf0ade3 475 * Try reading the /etc/hostid file directly.
a9f2397e 476 */
acf0ade3
RY
477 if (hostid_read())
478 spl_hostid = 0;
a9f2397e
ED
479
480 printk(KERN_NOTICE "SPL: using hostid 0x%08x\n",
481 (unsigned int) spl_hostid);
482 }
483
acf0ade3 484 return spl_hostid;
99639e4a
BB
485}
486EXPORT_SYMBOL(zone_get_hostid);
487
51a727e9
BB
488static int
489__init spl_init(void)
57d1b188 490{
491 int rc = 0;
f23e92fa 492
1114ae6a 493 if ((rc = spl_debug_init()))
18c9eadf 494 return rc;
f23e92fa 495
2fb9b26a 496 if ((rc = spl_kmem_init()))
b17edc10 497 SGOTO(out1, rc);
8d0f1ee9 498
9ab1ac14 499 if ((rc = spl_mutex_init()))
b17edc10 500 SGOTO(out2, rc);
9ab1ac14 501
d28db80f 502 if ((rc = spl_rw_init()))
b17edc10 503 SGOTO(out3, rc);
8d0f1ee9 504
d28db80f 505 if ((rc = spl_taskq_init()))
b17edc10 506 SGOTO(out4, rc);
af828292 507
12ff95ff 508 if ((rc = spl_vn_init()))
b17edc10 509 SGOTO(out5, rc);
04a479f7 510
1114ae6a 511 if ((rc = spl_proc_init()))
b17edc10 512 SGOTO(out6, rc);
e9cb2b4f 513
1114ae6a 514 if ((rc = spl_kstat_init()))
b17edc10 515 SGOTO(out7, rc);
d28db80f 516
1114ae6a 517 if ((rc = spl_tsd_init()))
9fe45dc1
BB
518 SGOTO(out8, rc);
519
1114ae6a 520 if ((rc = spl_zlib_init()))
5c1967eb
BB
521 SGOTO(out9, rc);
522
a9f2397e
ED
523 printk(KERN_NOTICE "SPL: Loaded module v%s-%s%s\n", SPL_META_VERSION,
524 SPL_META_RELEASE, SPL_DEBUG_STR);
b17edc10 525 SRETURN(rc);
44778f41 526
9fe45dc1 527out9:
1114ae6a 528 spl_tsd_fini();
d28db80f 529out8:
1114ae6a 530 spl_kstat_fini();
d28db80f 531out7:
1114ae6a 532 spl_proc_fini();
d28db80f 533out6:
12ff95ff 534 spl_vn_fini();
d28db80f 535out5:
e9cb2b4f 536 spl_taskq_fini();
d28db80f
BB
537out4:
538 spl_rw_fini();
9ab1ac14 539out3:
540 spl_mutex_fini();
8d0f1ee9 541out2:
2fb9b26a 542 spl_kmem_fini();
d28db80f 543out1:
1114ae6a 544 spl_debug_fini();
8d0f1ee9 545
0835057e
BB
546 printk(KERN_NOTICE "SPL: Failed to Load Solaris Porting Layer "
547 "v%s-%s%s, rc = %d\n", SPL_META_VERSION, SPL_META_RELEASE,
548 SPL_DEBUG_STR, rc);
18c9eadf 549 return rc;
70eadc19 550}
551
51a727e9
BB
552static void
553spl_fini(void)
70eadc19 554{
b17edc10 555 SENTRY;
57d1b188 556
0835057e
BB
557 printk(KERN_NOTICE "SPL: Unloaded module v%s-%s%s\n",
558 SPL_META_VERSION, SPL_META_RELEASE, SPL_DEBUG_STR);
1114ae6a
BB
559 spl_zlib_fini();
560 spl_tsd_fini();
561 spl_kstat_fini();
562 spl_proc_fini();
12ff95ff 563 spl_vn_fini();
e9cb2b4f 564 spl_taskq_fini();
d28db80f 565 spl_rw_fini();
2fb9b26a 566 spl_mutex_fini();
567 spl_kmem_fini();
1114ae6a 568 spl_debug_fini();
70eadc19 569}
570
51a727e9
BB
571/* Called when a dependent module is loaded */
572void
573spl_setup(void)
574{
82a358d9
BB
575 int rc;
576
51a727e9
BB
577 /*
578 * At module load time the pwd is set to '/' on a Solaris system.
579 * On a Linux system will be set to whatever directory the caller
580 * was in when executing insmod/modprobe.
581 */
82a358d9
BB
582 rc = vn_set_pwd("/");
583 if (rc)
584 printk("SPL: Warning unable to set pwd to '/': %d\n", rc);
51a727e9
BB
585}
586EXPORT_SYMBOL(spl_setup);
587
588/* Called when a dependent module is unloaded */
589void
590spl_cleanup(void)
591{
592}
593EXPORT_SYMBOL(spl_cleanup);
594
70eadc19 595module_init(spl_init);
596module_exit(spl_fini);
597
70eadc19 598MODULE_DESCRIPTION("Solaris Porting Layer");
62032954
BB
599MODULE_AUTHOR(SPL_META_AUTHOR);
600MODULE_LICENSE(SPL_META_LICENSE);
921a35ad 601MODULE_VERSION(SPL_META_VERSION "-" SPL_META_RELEASE);