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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
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.
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Generic Implementation.
25 \*****************************************************************************/
27 #include <sys/sysmacros.h>
28 #include <sys/systeminfo.h>
29 #include <sys/vmsystm.h>
32 #include <sys/mutex.h>
33 #include <sys/rwlock.h>
34 #include <sys/taskq.h>
37 #include <sys/debug.h>
39 #include <sys/kstat.h>
40 #include <sys/utsname.h>
42 #include <linux/kmod.h>
43 #include <linux/proc_compat.h>
44 #include <spl-debug.h>
46 #ifdef SS_DEBUG_SUBSYS
47 #undef SS_DEBUG_SUBSYS
50 #define SS_DEBUG_SUBSYS SS_GENERIC
52 char spl_version
[32] = "SPL v" SPL_META_VERSION
"-" SPL_META_RELEASE
;
53 EXPORT_SYMBOL(spl_version
);
55 unsigned long spl_hostid
= 0;
56 EXPORT_SYMBOL(spl_hostid
);
57 module_param(spl_hostid
, ulong
, 0644);
58 MODULE_PARM_DESC(spl_hostid
, "The system hostid.");
63 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
64 DECLARE_WAIT_QUEUE_HEAD(spl_kallsyms_lookup_name_waitq
);
65 kallsyms_lookup_name_t spl_kallsyms_lookup_name_fn
= SYMBOL_POISON
;
69 highbit(unsigned long i
)
76 #if BITS_PER_LONG == 64
77 if (i
& 0xffffffff00000000ul
) {
98 EXPORT_SYMBOL(highbit
);
101 highbit64(uint64_t i
)
108 if (i
& 0xffffffff00000000ull
) {
111 if (i
& 0xffff0000) {
128 EXPORT_SYMBOL(highbit64
);
130 #if BITS_PER_LONG == 32
132 * Support 64/64 => 64 division on a 32-bit platform. While the kernel
133 * provides a div64_u64() function for this we do not use it because the
134 * implementation is flawed. There are cases which return incorrect
135 * results as late as linux-2.6.35. Until this is fixed upstream the
136 * spl must provide its own implementation.
138 * This implementation is a slightly modified version of the algorithm
139 * proposed by the book 'Hacker's Delight'. The original source can be
140 * found here and is available for use without restriction.
142 * http://www.hackersdelight.org/HDcode/newCode/divDouble.c
146 * Calculate number of leading of zeros for a 64-bit value.
155 if (x
<= 0x00000000FFFFFFFFULL
) {n
= n
+ 32; x
= x
<< 32;}
156 if (x
<= 0x0000FFFFFFFFFFFFULL
) {n
= n
+ 16; x
= x
<< 16;}
157 if (x
<= 0x00FFFFFFFFFFFFFFULL
) {n
= n
+ 8; x
= x
<< 8;}
158 if (x
<= 0x0FFFFFFFFFFFFFFFULL
) {n
= n
+ 4; x
= x
<< 4;}
159 if (x
<= 0x3FFFFFFFFFFFFFFFULL
) {n
= n
+ 2; x
= x
<< 2;}
160 if (x
<= 0x7FFFFFFFFFFFFFFFULL
) {n
= n
+ 1;}
166 * Newer kernels have a div_u64() function but we define our own
167 * to simplify portibility between kernel versions.
169 static inline uint64_t
170 __div_u64(uint64_t u
, uint32_t v
)
177 * Implementation of 64-bit unsigned division for 32-bit machines.
179 * First the procedure takes care of the case in which the divisor is a
180 * 32-bit quantity. There are two subcases: (1) If the left half of the
181 * dividend is less than the divisor, one execution of do_div() is all that
182 * is required (overflow is not possible). (2) Otherwise it does two
183 * divisions, using the grade school method.
186 __udivdi3(uint64_t u
, uint64_t v
)
188 uint64_t u0
, u1
, v1
, q0
, q1
, k
;
191 if (v
>> 32 == 0) { // If v < 2**32:
192 if (u
>> 32 < v
) { // If u/v cannot overflow,
193 return __div_u64(u
, v
); // just do one division.
194 } else { // If u/v would overflow:
195 u1
= u
>> 32; // Break u into two halves.
197 q1
= __div_u64(u1
, v
); // First quotient digit.
198 k
= u1
- q1
* v
; // First remainder, < v.
200 q0
= __div_u64(u0
, v
); // Seconds quotient digit.
201 return (q1
<< 32) + q0
;
203 } else { // If v >= 2**32:
204 n
= nlz64(v
); // 0 <= n <= 31.
205 v1
= (v
<< n
) >> 32; // Normalize divisor, MSB is 1.
206 u1
= u
>> 1; // To ensure no overflow.
207 q1
= __div_u64(u1
, v1
); // Get quotient from
208 q0
= (q1
<< n
) >> 31; // Undo normalization and
209 // division of u by 2.
210 if (q0
!= 0) // Make q0 correct or
211 q0
= q0
- 1; // too small by 1.
212 if ((u
- q0
* v
) >= v
)
213 q0
= q0
+ 1; // Now q0 is correct.
218 EXPORT_SYMBOL(__udivdi3
);
221 * Implementation of 64-bit signed division for 32-bit machines.
224 __divdi3(int64_t u
, int64_t v
)
227 q
= __udivdi3(abs64(u
), abs64(v
));
228 t
= (u
^ v
) >> 63; // If u, v have different
229 return (q
^ t
) - t
; // signs, negate q.
231 EXPORT_SYMBOL(__divdi3
);
234 * Implementation of 64-bit unsigned modulo for 32-bit machines.
237 __umoddi3(uint64_t dividend
, uint64_t divisor
)
239 return (dividend
- (divisor
* __udivdi3(dividend
, divisor
)));
241 EXPORT_SYMBOL(__umoddi3
);
243 #if defined(__arm) || defined(__arm__)
245 * Implementation of 64-bit (un)signed division for 32-bit arm machines.
247 * Run-time ABI for the ARM Architecture (page 20). A pair of (unsigned)
248 * long longs is returned in {{r0, r1}, {r2,r3}}, the quotient in {r0, r1},
249 * and the remainder in {r2, r3}. The return type is specifically left
250 * set to 'void' to ensure the compiler does not overwrite these registers
251 * during the return. All results are in registers as per ABI
254 __aeabi_uldivmod(uint64_t u
, uint64_t v
)
259 res
= __udivdi3(u
, v
);
260 mod
= __umoddi3(u
, v
);
262 register uint32_t r0
asm("r0") = (res
& 0xFFFFFFFF);
263 register uint32_t r1
asm("r1") = (res
>> 32);
264 register uint32_t r2
asm("r2") = (mod
& 0xFFFFFFFF);
265 register uint32_t r3
asm("r3") = (mod
>> 32);
268 : "+r"(r0
), "+r"(r1
), "+r"(r2
),"+r"(r3
) /* output */
269 : "r"(r0
), "r"(r1
), "r"(r2
), "r"(r3
)); /* input */
274 EXPORT_SYMBOL(__aeabi_uldivmod
);
277 __aeabi_ldivmod(int64_t u
, int64_t v
)
282 res
= __divdi3(u
, v
);
283 mod
= __umoddi3(u
, v
);
285 register uint32_t r0
asm("r0") = (res
& 0xFFFFFFFF);
286 register uint32_t r1
asm("r1") = (res
>> 32);
287 register uint32_t r2
asm("r2") = (mod
& 0xFFFFFFFF);
288 register uint32_t r3
asm("r3") = (mod
>> 32);
291 : "+r"(r0
), "+r"(r1
), "+r"(r2
),"+r"(r3
) /* output */
292 : "r"(r0
), "r"(r1
), "r"(r2
), "r"(r3
)); /* input */
297 EXPORT_SYMBOL(__aeabi_ldivmod
);
298 #endif /* __arm || __arm__ */
299 #endif /* BITS_PER_LONG */
301 /* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
302 * ddi_strtol(9F) man page. I have not verified the behavior of these
303 * functions against their Solaris counterparts. It is possible that I
304 * may have misinterpreted the man page or the man page is incorrect.
306 int ddi_strtoul(const char *, char **, int, unsigned long *);
307 int ddi_strtol(const char *, char **, int, long *);
308 int ddi_strtoull(const char *, char **, int, unsigned long long *);
309 int ddi_strtoll(const char *, char **, int, long long *);
311 #define define_ddi_strtoux(type, valtype) \
312 int ddi_strtou##type(const char *str, char **endptr, \
313 int base, valtype *result) \
315 valtype last_value, value = 0; \
316 char *ptr = (char *)str; \
317 int flag = 1, digit; \
319 if (strlen(ptr) == 0) \
322 /* Auto-detect base based on prefix */ \
324 if (str[0] == '0') { \
325 if (tolower(str[1])=='x' && isxdigit(str[2])) { \
326 base = 16; /* hex */ \
328 } else if (str[1] >= '0' && str[1] < 8) { \
329 base = 8; /* octal */ \
335 base = 10; /* decimal */ \
341 digit = *ptr - '0'; \
342 else if (isalpha(*ptr)) \
343 digit = tolower(*ptr) - 'a' + 10; \
350 last_value = value; \
351 value = value * base + digit; \
352 if (last_value > value) /* Overflow */ \
363 *endptr = (char *)(flag ? ptr : str); \
368 #define define_ddi_strtox(type, valtype) \
369 int ddi_strto##type(const char *str, char **endptr, \
370 int base, valtype *result) \
375 rc = ddi_strtou##type(str + 1, endptr, base, result); \
377 if (*endptr == str + 1) \
378 *endptr = (char *)str; \
380 *result = -*result; \
383 rc = ddi_strtou##type(str, endptr, base, result); \
389 define_ddi_strtoux(l
, unsigned long)
390 define_ddi_strtox(l
, long)
391 define_ddi_strtoux(ll
, unsigned long long)
392 define_ddi_strtox(ll
, long long)
394 EXPORT_SYMBOL(ddi_strtoul
);
395 EXPORT_SYMBOL(ddi_strtol
);
396 EXPORT_SYMBOL(ddi_strtoll
);
397 EXPORT_SYMBOL(ddi_strtoull
);
400 ddi_copyin(const void *from
, void *to
, size_t len
, int flags
)
402 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
403 if (flags
& FKIOCTL
) {
404 memcpy(to
, from
, len
);
408 return copyin(from
, to
, len
);
410 EXPORT_SYMBOL(ddi_copyin
);
413 ddi_copyout(const void *from
, void *to
, size_t len
, int flags
)
415 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
416 if (flags
& FKIOCTL
) {
417 memcpy(to
, from
, len
);
421 return copyout(from
, to
, len
);
423 EXPORT_SYMBOL(ddi_copyout
);
425 #ifndef HAVE_PUT_TASK_STRUCT
427 * This is only a stub function which should never be used. The SPL should
428 * never be putting away the last reference on a task structure so this will
429 * not be called. However, we still need to define it so the module does not
430 * have undefined symbol at load time. That all said if this impossible
431 * thing does somehow happen PANIC immediately so we know about it.
434 __put_task_struct(struct task_struct
*t
)
436 PANIC("Unexpectly put last reference on task %d\n", (int)t
->pid
);
438 EXPORT_SYMBOL(__put_task_struct
);
439 #endif /* HAVE_PUT_TASK_STRUCT */
441 struct new_utsname
*__utsname(void)
443 #ifdef HAVE_INIT_UTSNAME
444 return init_utsname();
446 return &system_utsname
;
449 EXPORT_SYMBOL(__utsname
);
453 * Read the unique system identifier from the /etc/hostid file.
455 * The behavior of /usr/bin/hostid on Linux systems with the
456 * regular eglibc and coreutils is:
458 * 1. Generate the value if the /etc/hostid file does not exist
459 * or if the /etc/hostid file is less than four bytes in size.
461 * 2. If the /etc/hostid file is at least 4 bytes, then return
462 * the first four bytes [0..3] in native endian order.
464 * 3. Always ignore bytes [4..] if they exist in the file.
466 * Only the first four bytes are significant, even on systems that
467 * have a 64-bit word size.
471 * eglibc: sysdeps/unix/sysv/linux/gethostid.c
472 * coreutils: src/hostid.c
476 * The /etc/hostid file on Solaris is a text file that often reads:
481 * Directly copying this file to Linux results in a constant
482 * hostid of 4f442023 because the default comment constitutes
483 * the first four bytes of the file.
487 char *spl_hostid_path
= HW_HOSTID_PATH
;
488 module_param(spl_hostid_path
, charp
, 0444);
489 MODULE_PARM_DESC(spl_hostid_path
, "The system hostid file (/etc/hostid)");
499 file
= kobj_open_file(spl_hostid_path
);
501 if (file
== (struct _buf
*)-1)
504 result
= kobj_get_filesize(file
, &size
);
508 "SPL: kobj_get_filesize returned %i on %s\n",
509 result
, spl_hostid_path
);
510 kobj_close_file(file
);
514 if (size
< sizeof(HW_HOSTID_MASK
)) {
516 "SPL: Ignoring the %s file because it is %llu bytes; "
517 "expecting %lu bytes instead.\n", spl_hostid_path
,
518 size
, (unsigned long)sizeof(HW_HOSTID_MASK
));
519 kobj_close_file(file
);
523 /* Read directly into the variable like eglibc does. */
524 /* Short reads are okay; native behavior is preserved. */
525 result
= kobj_read_file(file
, (char *)&hostid
, sizeof(hostid
), 0);
529 "SPL: kobj_read_file returned %i on %s\n",
530 result
, spl_hostid_path
);
531 kobj_close_file(file
);
535 /* Mask down to 32 bits like coreutils does. */
536 spl_hostid
= hostid
& HW_HOSTID_MASK
;
537 kobj_close_file(file
);
542 zone_get_hostid(void *zone
)
544 static int first
= 1;
546 /* Only the global zone is supported */
547 ASSERT(zone
== NULL
);
553 * Get the hostid if it was not passed as a module parameter.
554 * Try reading the /etc/hostid file directly.
559 printk(KERN_NOTICE
"SPL: using hostid 0x%08x\n",
560 (unsigned int) spl_hostid
);
565 EXPORT_SYMBOL(zone_get_hostid
);
567 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
569 * The kallsyms_lookup_name() kernel function is not an exported symbol in
570 * Linux 2.6.19 through 2.6.32 inclusive.
572 * This function replaces the functionality by performing an upcall to user
573 * space where /proc/kallsyms is consulted for the requested address.
577 #define GET_KALLSYMS_ADDR_CMD \
578 "exec 0</dev/null " \
579 " 1>/proc/sys/kernel/spl/kallsyms_lookup_name " \
581 "awk '{ if ( $3 == \"kallsyms_lookup_name\" ) { print $1 } }' " \
585 set_kallsyms_lookup_name(void)
587 char *argv
[] = { "/bin/sh",
589 GET_KALLSYMS_ADDR_CMD
,
591 char *envp
[] = { "HOME=/",
593 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
597 rc
= call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_PROC
);
600 * Due to I/O buffering the helper may return successfully before
601 * the proc handler has a chance to execute. To catch this case
602 * wait up to 1 second to verify spl_kallsyms_lookup_name_fn was
603 * updated to a non SYMBOL_POISON value.
606 rc
= wait_event_timeout(spl_kallsyms_lookup_name_waitq
,
607 spl_kallsyms_lookup_name_fn
!= SYMBOL_POISON
, HZ
);
610 else if (spl_kallsyms_lookup_name_fn
== SYMBOL_POISON
)
617 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
618 argv
[0], argv
[1], argv
[2], rc
);
625 __init
spl_init(void)
629 if ((rc
= spl_debug_init()))
632 if ((rc
= spl_kmem_init()))
635 if ((rc
= spl_mutex_init()))
638 if ((rc
= spl_rw_init()))
641 if ((rc
= spl_taskq_init()))
644 if ((rc
= spl_vn_init()))
647 if ((rc
= spl_proc_init()))
650 if ((rc
= spl_kstat_init()))
653 if ((rc
= spl_tsd_init()))
656 if ((rc
= spl_zlib_init()))
659 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
660 if ((rc
= set_kallsyms_lookup_name()))
661 SGOTO(out10
, rc
= -EADDRNOTAVAIL
);
662 #endif /* HAVE_KALLSYMS_LOOKUP_NAME */
664 if ((rc
= spl_kmem_init_kallsyms_lookup()))
667 if ((rc
= spl_vn_init_kallsyms_lookup()))
670 printk(KERN_NOTICE
"SPL: Loaded module v%s-%s%s\n", SPL_META_VERSION
,
671 SPL_META_RELEASE
, SPL_DEBUG_STR
);
694 printk(KERN_NOTICE
"SPL: Failed to Load Solaris Porting Layer "
695 "v%s-%s%s, rc = %d\n", SPL_META_VERSION
, SPL_META_RELEASE
,
705 printk(KERN_NOTICE
"SPL: Unloaded module v%s-%s%s\n",
706 SPL_META_VERSION
, SPL_META_RELEASE
, SPL_DEBUG_STR
);
719 /* Called when a dependent module is loaded */
726 * At module load time the pwd is set to '/' on a Solaris system.
727 * On a Linux system will be set to whatever directory the caller
728 * was in when executing insmod/modprobe.
730 rc
= vn_set_pwd("/");
732 printk("SPL: Warning unable to set pwd to '/': %d\n", rc
);
734 EXPORT_SYMBOL(spl_setup
);
736 /* Called when a dependent module is unloaded */
741 EXPORT_SYMBOL(spl_cleanup
);
743 module_init(spl_init
);
744 module_exit(spl_fini
);
746 MODULE_AUTHOR("Lawrence Livermore National Labs");
747 MODULE_DESCRIPTION("Solaris Porting Layer");
748 MODULE_LICENSE("GPL");
749 MODULE_VERSION(SPL_META_VERSION
"-" SPL_META_RELEASE
);