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://github.com/behlendorf/spl/>.
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
= HW_INVALID_HOSTID
;
56 EXPORT_SYMBOL(spl_hostid
);
57 module_param(spl_hostid
, ulong
, 0644);
58 MODULE_PARM_DESC(spl_hostid
, "The system hostid.");
60 char hw_serial
[HW_HOSTID_LEN
] = "<none>";
61 EXPORT_SYMBOL(hw_serial
);
66 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
67 kallsyms_lookup_name_t spl_kallsyms_lookup_name_fn
= SYMBOL_POISON
;
71 highbit(unsigned long i
)
78 #if BITS_PER_LONG == 64
79 if (i
& 0xffffffff00000000ul
) {
100 EXPORT_SYMBOL(highbit
);
102 #if BITS_PER_LONG == 32
104 * Support 64/64 => 64 division on a 32-bit platform. While the kernel
105 * provides a div64_u64() function for this we do not use it because the
106 * implementation is flawed. There are cases which return incorrect
107 * results as late as linux-2.6.35. Until this is fixed upstream the
108 * spl must provide its own implementation.
110 * This implementation is a slightly modified version of the algorithm
111 * proposed by the book 'Hacker's Delight'. The original source can be
112 * found here and is available for use without restriction.
114 * http://www.hackersdelight.org/HDcode/newCode/divDouble.c
118 * Calculate number of leading of zeros for a 64-bit value.
127 if (x
<= 0x00000000FFFFFFFFULL
) {n
= n
+ 32; x
= x
<< 32;}
128 if (x
<= 0x0000FFFFFFFFFFFFULL
) {n
= n
+ 16; x
= x
<< 16;}
129 if (x
<= 0x00FFFFFFFFFFFFFFULL
) {n
= n
+ 8; x
= x
<< 8;}
130 if (x
<= 0x0FFFFFFFFFFFFFFFULL
) {n
= n
+ 4; x
= x
<< 4;}
131 if (x
<= 0x3FFFFFFFFFFFFFFFULL
) {n
= n
+ 2; x
= x
<< 2;}
132 if (x
<= 0x7FFFFFFFFFFFFFFFULL
) {n
= n
+ 1;}
138 * Newer kernels have a div_u64() function but we define our own
139 * to simplify portibility between kernel versions.
141 static inline uint64_t
142 __div_u64(uint64_t u
, uint32_t v
)
149 * Implementation of 64-bit unsigned division for 32-bit machines.
151 * First the procedure takes care of the case in which the divisor is a
152 * 32-bit quantity. There are two subcases: (1) If the left half of the
153 * dividend is less than the divisor, one execution of do_div() is all that
154 * is required (overflow is not possible). (2) Otherwise it does two
155 * divisions, using the grade school method.
158 __udivdi3(uint64_t u
, uint64_t v
)
160 uint64_t u0
, u1
, v1
, q0
, q1
, k
;
163 if (v
>> 32 == 0) { // If v < 2**32:
164 if (u
>> 32 < v
) { // If u/v cannot overflow,
165 return __div_u64(u
, v
); // just do one division.
166 } else { // If u/v would overflow:
167 u1
= u
>> 32; // Break u into two halves.
169 q1
= __div_u64(u1
, v
); // First quotient digit.
170 k
= u1
- q1
* v
; // First remainder, < v.
172 q0
= __div_u64(u0
, v
); // Seconds quotient digit.
173 return (q1
<< 32) + q0
;
175 } else { // If v >= 2**32:
176 n
= nlz64(v
); // 0 <= n <= 31.
177 v1
= (v
<< n
) >> 32; // Normalize divisor, MSB is 1.
178 u1
= u
>> 1; // To ensure no overflow.
179 q1
= __div_u64(u1
, v1
); // Get quotient from
180 q0
= (q1
<< n
) >> 31; // Undo normalization and
181 // division of u by 2.
182 if (q0
!= 0) // Make q0 correct or
183 q0
= q0
- 1; // too small by 1.
184 if ((u
- q0
* v
) >= v
)
185 q0
= q0
+ 1; // Now q0 is correct.
190 EXPORT_SYMBOL(__udivdi3
);
193 * Implementation of 64-bit signed division for 32-bit machines.
196 __divdi3(int64_t u
, int64_t v
)
199 q
= __udivdi3(abs64(u
), abs64(v
));
200 t
= (u
^ v
) >> 63; // If u, v have different
201 return (q
^ t
) - t
; // signs, negate q.
203 EXPORT_SYMBOL(__divdi3
);
206 * Implementation of 64-bit unsigned modulo for 32-bit machines.
209 __umoddi3(uint64_t dividend
, uint64_t divisor
)
211 return (dividend
- (divisor
* __udivdi3(dividend
, divisor
)));
213 EXPORT_SYMBOL(__umoddi3
);
215 #if defined(__arm) || defined(__arm__)
217 * Implementation of 64-bit unsigned division for 32-bit arm machines.
220 __aeabi_uldivmod(uint64_t u
, uint64_t v
)
222 return __udivdi3(u
, v
);
224 EXPORT_SYMBOL(__aeabi_uldivmod
);
227 * Implementation of 64-bit signed division for 32-bit arm machines.
230 __aeabi_ldivmod(int64_t u
, int64_t v
)
232 return __divdi3(u
, v
);
234 EXPORT_SYMBOL(__aeabi_ldivmod
);
235 #endif /* __arm || __arm__ */
236 #endif /* BITS_PER_LONG */
238 /* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
239 * ddi_strtol(9F) man page. I have not verified the behavior of these
240 * functions against their Solaris counterparts. It is possible that I
241 * may have misinterpreted the man page or the man page is incorrect.
243 int ddi_strtoul(const char *, char **, int, unsigned long *);
244 int ddi_strtol(const char *, char **, int, long *);
245 int ddi_strtoull(const char *, char **, int, unsigned long long *);
246 int ddi_strtoll(const char *, char **, int, long long *);
248 #define define_ddi_strtoux(type, valtype) \
249 int ddi_strtou##type(const char *str, char **endptr, \
250 int base, valtype *result) \
252 valtype last_value, value = 0; \
253 char *ptr = (char *)str; \
254 int flag = 1, digit; \
256 if (strlen(ptr) == 0) \
259 /* Auto-detect base based on prefix */ \
261 if (str[0] == '0') { \
262 if (tolower(str[1])=='x' && isxdigit(str[2])) { \
263 base = 16; /* hex */ \
265 } else if (str[1] >= '0' && str[1] < 8) { \
266 base = 8; /* octal */ \
272 base = 10; /* decimal */ \
278 digit = *ptr - '0'; \
279 else if (isalpha(*ptr)) \
280 digit = tolower(*ptr) - 'a' + 10; \
287 last_value = value; \
288 value = value * base + digit; \
289 if (last_value > value) /* Overflow */ \
300 *endptr = (char *)(flag ? ptr : str); \
305 #define define_ddi_strtox(type, valtype) \
306 int ddi_strto##type(const char *str, char **endptr, \
307 int base, valtype *result) \
312 rc = ddi_strtou##type(str + 1, endptr, base, result); \
314 if (*endptr == str + 1) \
315 *endptr = (char *)str; \
317 *result = -*result; \
320 rc = ddi_strtou##type(str, endptr, base, result); \
326 define_ddi_strtoux(l
, unsigned long)
327 define_ddi_strtox(l
, long)
328 define_ddi_strtoux(ll
, unsigned long long)
329 define_ddi_strtox(ll
, long long)
331 EXPORT_SYMBOL(ddi_strtoul
);
332 EXPORT_SYMBOL(ddi_strtol
);
333 EXPORT_SYMBOL(ddi_strtoll
);
334 EXPORT_SYMBOL(ddi_strtoull
);
337 ddi_copyin(const void *from
, void *to
, size_t len
, int flags
)
339 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
340 if (flags
& FKIOCTL
) {
341 memcpy(to
, from
, len
);
345 return copyin(from
, to
, len
);
347 EXPORT_SYMBOL(ddi_copyin
);
350 ddi_copyout(const void *from
, void *to
, size_t len
, int flags
)
352 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
353 if (flags
& FKIOCTL
) {
354 memcpy(to
, from
, len
);
358 return copyout(from
, to
, len
);
360 EXPORT_SYMBOL(ddi_copyout
);
362 #ifndef HAVE_PUT_TASK_STRUCT
364 * This is only a stub function which should never be used. The SPL should
365 * never be putting away the last reference on a task structure so this will
366 * not be called. However, we still need to define it so the module does not
367 * have undefined symbol at load time. That all said if this impossible
368 * thing does somehow happen PANIC immediately so we know about it.
371 __put_task_struct(struct task_struct
*t
)
373 PANIC("Unexpectly put last reference on task %d\n", (int)t
->pid
);
375 EXPORT_SYMBOL(__put_task_struct
);
376 #endif /* HAVE_PUT_TASK_STRUCT */
378 struct new_utsname
*__utsname(void)
380 #ifdef HAVE_INIT_UTSNAME
381 return init_utsname();
383 return &system_utsname
;
386 EXPORT_SYMBOL(__utsname
);
390 * Read the unique system identifier from the /etc/hostid file.
392 * The behavior of /usr/bin/hostid on Linux systems with the
393 * regular eglibc and coreutils is:
395 * 1. Generate the value if the /etc/hostid file does not exist
396 * or if the /etc/hostid file is less than four bytes in size.
398 * 2. If the /etc/hostid file is at least 4 bytes, then return
399 * the first four bytes [0..3] in native endian order.
401 * 3. Always ignore bytes [4..] if they exist in the file.
403 * Only the first four bytes are significant, even on systems that
404 * have a 64-bit word size.
408 * eglibc: sysdeps/unix/sysv/linux/gethostid.c
409 * coreutils: src/hostid.c
413 * The /etc/hostid file on Solaris is a text file that often reads:
418 * Directly copying this file to Linux results in a constant
419 * hostid of 4f442023 because the default comment constitutes
420 * the first four bytes of the file.
424 char *spl_hostid_path
= HW_HOSTID_PATH
;
425 module_param(spl_hostid_path
, charp
, 0444);
426 MODULE_PARM_DESC(spl_hostid_path
, "The system hostid file (/etc/hostid)");
434 unsigned long hostid
= 0;
436 file
= kobj_open_file(spl_hostid_path
);
438 if (file
== (struct _buf
*)-1)
441 result
= kobj_get_filesize(file
, &size
);
445 "SPL: kobj_get_filesize returned %i on %s\n",
446 result
, spl_hostid_path
);
447 kobj_close_file(file
);
451 if (size
< sizeof(HW_HOSTID_MASK
)) {
453 "SPL: Ignoring the %s file because it is %llu bytes; "
454 "expecting %lu bytes instead.\n",
455 spl_hostid_path
, size
, sizeof(HW_HOSTID_MASK
));
456 kobj_close_file(file
);
460 /* Read directly into the variable like eglibc does. */
461 /* Short reads are okay; native behavior is preserved. */
462 result
= kobj_read_file(file
, (char *)&hostid
, sizeof(hostid
), 0);
466 "SPL: kobj_read_file returned %i on %s\n",
467 result
, spl_hostid_path
);
468 kobj_close_file(file
);
472 /* Mask down to 32 bits like coreutils does. */
473 spl_hostid
= hostid
& HW_HOSTID_MASK
;
474 kobj_close_file(file
);
478 #define GET_HOSTID_CMD \
479 "exec 0</dev/null " \
480 " 1>/proc/sys/kernel/spl/hostid " \
487 char *argv
[] = { "/bin/sh",
491 char *envp
[] = { "HOME=/",
493 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
497 /* Doing address resolution in the kernel is tricky and just
498 * not a good idea in general. So to set the proper 'hw_serial'
499 * use the usermodehelper support to ask '/bin/sh' to run
500 * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid
501 * for us to use. It's a horrific solution but it will do for now.
503 rc
= call_usermodehelper(argv
[0], argv
, envp
, 1);
505 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
506 argv
[0], argv
[1], argv
[2], rc
);
512 zone_get_hostid(void *zone
)
514 unsigned long hostid
;
516 /* Only the global zone is supported */
517 ASSERT(zone
== NULL
);
519 if (ddi_strtoul(hw_serial
, NULL
, HW_HOSTID_LEN
-1, &hostid
) != 0)
520 return HW_INVALID_HOSTID
;
522 return (uint32_t)hostid
;
524 EXPORT_SYMBOL(zone_get_hostid
);
526 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
528 * The kallsyms_lookup_name() kernel function is not an exported symbol in
529 * Linux 2.6.19 through 2.6.32 inclusive.
531 * This function replaces the functionality by performing an upcall to user
532 * space where /proc/kallsyms is consulted for the requested address.
536 #define GET_KALLSYMS_ADDR_CMD \
537 "exec 0</dev/null " \
538 " 1>/proc/sys/kernel/spl/kallsyms_lookup_name " \
540 "awk '{ if ( $3 == \"kallsyms_lookup_name\" ) { print $1 } }' " \
544 set_kallsyms_lookup_name(void)
546 char *argv
[] = { "/bin/sh",
548 GET_KALLSYMS_ADDR_CMD
,
550 char *envp
[] = { "HOME=/",
552 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
556 rc
= call_usermodehelper(argv
[0], argv
, envp
, 1);
558 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
559 argv
[0], argv
[1], argv
[2], rc
);
566 __init
spl_init(void)
570 if ((rc
= spl_debug_init()))
573 if ((rc
= spl_kmem_init()))
576 if ((rc
= spl_mutex_init()))
579 if ((rc
= spl_rw_init()))
582 if ((rc
= spl_taskq_init()))
585 if ((rc
= spl_vn_init()))
588 if ((rc
= spl_proc_init()))
591 if ((rc
= spl_kstat_init()))
594 if ((rc
= spl_tsd_init()))
597 if ((rc
= spl_zlib_init()))
601 * Get the hostid if it was not passed as a module parameter. Try
602 * reading the /etc/hostid file directly, and then fall back to calling
603 * the /usr/bin/hostid utility.
606 if (spl_hostid
== HW_INVALID_HOSTID
607 && (rc
= hostid_read()) && (rc
= hostid_exec()))
608 SGOTO(out10
, rc
= -EADDRNOTAVAIL
);
610 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
611 if ((rc
= set_kallsyms_lookup_name()))
612 SGOTO(out10
, rc
= -EADDRNOTAVAIL
);
613 #endif /* HAVE_KALLSYMS_LOOKUP_NAME */
615 if ((rc
= spl_kmem_init_kallsyms_lookup()))
618 if ((rc
= spl_vn_init_kallsyms_lookup()))
621 printk(KERN_NOTICE
"SPL: Loaded module v%s-%s%s, using hostid "
622 "0x%08x\n", SPL_META_VERSION
, SPL_META_RELEASE
, SPL_DEBUG_STR
,
623 (unsigned int) spl_hostid
);
646 printk(KERN_NOTICE
"SPL: Failed to Load Solaris Porting Layer "
647 "v%s-%s%s, rc = %d\n", SPL_META_VERSION
, SPL_META_RELEASE
,
657 printk(KERN_NOTICE
"SPL: Unloaded module v%s-%s%s\n",
658 SPL_META_VERSION
, SPL_META_RELEASE
, SPL_DEBUG_STR
);
671 /* Called when a dependent module is loaded */
678 * At module load time the pwd is set to '/' on a Solaris system.
679 * On a Linux system will be set to whatever directory the caller
680 * was in when executing insmod/modprobe.
682 rc
= vn_set_pwd("/");
684 printk("SPL: Warning unable to set pwd to '/': %d\n", rc
);
686 EXPORT_SYMBOL(spl_setup
);
688 /* Called when a dependent module is unloaded */
693 EXPORT_SYMBOL(spl_cleanup
);
695 module_init(spl_init
);
696 module_exit(spl_fini
);
698 MODULE_AUTHOR("Lawrence Livermore National Labs");
699 MODULE_DESCRIPTION("Solaris Porting Layer");
700 MODULE_LICENSE("GPL");