]>
git.proxmox.com Git - mirror_spl.git/blob - module/spl/spl-generic.c
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/>.
24 * Solaris Porting Layer (SPL) Generic Implementation.
27 #include <sys/sysmacros.h>
28 #include <sys/systeminfo.h>
29 #include <sys/vmsystm.h>
32 #include <sys/kmem_cache.h>
34 #include <sys/mutex.h>
35 #include <sys/rwlock.h>
36 #include <sys/taskq.h>
39 #include <sys/debug.h>
41 #include <sys/kstat.h>
43 #include <linux/ctype.h>
45 #include <sys/random.h>
46 #include <sys/strings.h>
47 #include <linux/kmod.h>
49 char spl_version
[32] = "SPL v" SPL_META_VERSION
"-" SPL_META_RELEASE
;
50 EXPORT_SYMBOL(spl_version
);
53 unsigned long spl_hostid
= 0;
54 EXPORT_SYMBOL(spl_hostid
);
55 module_param(spl_hostid
, ulong
, 0644);
56 MODULE_PARM_DESC(spl_hostid
, "The system hostid.");
63 * Xorshift Pseudo Random Number Generator based on work by Sebastiano Vigna
65 * "Further scramblings of Marsaglia's xorshift generators"
66 * http://vigna.di.unimi.it/ftp/papers/xorshiftplus.pdf
68 * random_get_pseudo_bytes() is an API function on Illumos whose sole purpose
69 * is to provide bytes containing random numbers. It is mapped to /dev/urandom
70 * on Illumos, which uses a "FIPS 186-2 algorithm". No user of the SPL's
71 * random_get_pseudo_bytes() needs bytes that are of cryptographic quality, so
72 * we can implement it using a fast PRNG that we seed using Linux' actual
73 * equivalent to random_get_pseudo_bytes(). We do this by providing each CPU
74 * with an independent seed so that all calls to random_get_pseudo_bytes() are
75 * free of atomic instructions.
77 * A consequence of using a fast PRNG is that using random_get_pseudo_bytes()
78 * to generate words larger than 128 bits will paradoxically be limited to
79 * `2^128 - 1` possibilities. This is because we have a sequence of `2^128 - 1`
80 * 128-bit words and selecting the first will implicitly select the second. If
81 * a caller finds this behavior undesireable, random_get_bytes() should be used
84 * XXX: Linux interrupt handlers that trigger within the critical section
85 * formed by `s[1] = xp[1];` and `xp[0] = s[0];` and call this function will
86 * see the same numbers. Nothing in the code currently calls this in an
87 * interrupt handler, so this is considered to be okay. If that becomes a
88 * problem, we could create a set of per-cpu variables for interrupt handlers
89 * and use them when in_interrupt() from linux/preempt_mask.h evaluates to
92 static DEFINE_PER_CPU(uint64_t[2], spl_pseudo_entropy
);
95 * spl_rand_next()/spl_rand_jump() are copied from the following CC-0 licensed
98 * http://xorshift.di.unimi.it/xorshift128plus.c
101 static inline uint64_t
102 spl_rand_next(uint64_t *s
)
105 const uint64_t s0
= s
[1];
108 s
[1] = s1
^ s0
^ (s1
>> 18) ^ (s0
>> 5); // b, c
113 spl_rand_jump(uint64_t *s
)
115 static const uint64_t JUMP
[] =
116 { 0x8a5cd789635d2dff, 0x121fd2155c472f96 };
121 for (i
= 0; i
< sizeof (JUMP
) / sizeof (*JUMP
); i
++)
122 for (b
= 0; b
< 64; b
++) {
123 if (JUMP
[i
] & 1ULL << b
) {
127 (void) spl_rand_next(s
);
135 random_get_pseudo_bytes(uint8_t *ptr
, size_t len
)
141 xp
= get_cpu_var(spl_pseudo_entropy
);
149 uint8_t byte
[sizeof (uint64_t)];
151 int i
= MIN(len
, sizeof (uint64_t));
154 entropy
.ui64
= spl_rand_next(s
);
157 *ptr
++ = entropy
.byte
[i
];
163 put_cpu_var(spl_pseudo_entropy
);
169 EXPORT_SYMBOL(random_get_pseudo_bytes
);
171 #if BITS_PER_LONG == 32
173 * Support 64/64 => 64 division on a 32-bit platform. While the kernel
174 * provides a div64_u64() function for this we do not use it because the
175 * implementation is flawed. There are cases which return incorrect
176 * results as late as linux-2.6.35. Until this is fixed upstream the
177 * spl must provide its own implementation.
179 * This implementation is a slightly modified version of the algorithm
180 * proposed by the book 'Hacker's Delight'. The original source can be
181 * found here and is available for use without restriction.
183 * http://www.hackersdelight.org/HDcode/newCode/divDouble.c
187 * Calculate number of leading of zeros for a 64-bit value.
197 if (x
<= 0x00000000FFFFFFFFULL
) { n
= n
+ 32; x
= x
<< 32; }
198 if (x
<= 0x0000FFFFFFFFFFFFULL
) { n
= n
+ 16; x
= x
<< 16; }
199 if (x
<= 0x00FFFFFFFFFFFFFFULL
) { n
= n
+ 8; x
= x
<< 8; }
200 if (x
<= 0x0FFFFFFFFFFFFFFFULL
) { n
= n
+ 4; x
= x
<< 4; }
201 if (x
<= 0x3FFFFFFFFFFFFFFFULL
) { n
= n
+ 2; x
= x
<< 2; }
202 if (x
<= 0x7FFFFFFFFFFFFFFFULL
) { n
= n
+ 1; }
208 * Newer kernels have a div_u64() function but we define our own
209 * to simplify portibility between kernel versions.
211 static inline uint64_t
212 __div_u64(uint64_t u
, uint32_t v
)
219 * Implementation of 64-bit unsigned division for 32-bit machines.
221 * First the procedure takes care of the case in which the divisor is a
222 * 32-bit quantity. There are two subcases: (1) If the left half of the
223 * dividend is less than the divisor, one execution of do_div() is all that
224 * is required (overflow is not possible). (2) Otherwise it does two
225 * divisions, using the grade school method.
228 __udivdi3(uint64_t u
, uint64_t v
)
230 uint64_t u0
, u1
, v1
, q0
, q1
, k
;
233 if (v
>> 32 == 0) { // If v < 2**32:
234 if (u
>> 32 < v
) { // If u/v cannot overflow,
235 return (__div_u64(u
, v
)); // just do one division.
236 } else { // If u/v would overflow:
237 u1
= u
>> 32; // Break u into two halves.
239 q1
= __div_u64(u1
, v
); // First quotient digit.
240 k
= u1
- q1
* v
; // First remainder, < v.
242 q0
= __div_u64(u0
, v
); // Seconds quotient digit.
243 return ((q1
<< 32) + q0
);
245 } else { // If v >= 2**32:
246 n
= nlz64(v
); // 0 <= n <= 31.
247 v1
= (v
<< n
) >> 32; // Normalize divisor, MSB is 1.
248 u1
= u
>> 1; // To ensure no overflow.
249 q1
= __div_u64(u1
, v1
); // Get quotient from
250 q0
= (q1
<< n
) >> 31; // Undo normalization and
251 // division of u by 2.
252 if (q0
!= 0) // Make q0 correct or
253 q0
= q0
- 1; // too small by 1.
254 if ((u
- q0
* v
) >= v
)
255 q0
= q0
+ 1; // Now q0 is correct.
260 EXPORT_SYMBOL(__udivdi3
);
264 #define abs64(x) ({ uint64_t t = (x) >> 63; ((x) ^ t) - t; })
269 * Implementation of 64-bit signed division for 32-bit machines.
272 __divdi3(int64_t u
, int64_t v
)
275 q
= __udivdi3(abs64(u
), abs64(v
));
276 t
= (u
^ v
) >> 63; // If u, v have different
277 return ((q
^ t
) - t
); // signs, negate q.
279 EXPORT_SYMBOL(__divdi3
);
282 * Implementation of 64-bit unsigned modulo for 32-bit machines.
285 __umoddi3(uint64_t dividend
, uint64_t divisor
)
287 return (dividend
- (divisor
* __udivdi3(dividend
, divisor
)));
289 EXPORT_SYMBOL(__umoddi3
);
292 * Implementation of 64-bit unsigned division/modulo for 32-bit machines.
295 __udivmoddi4(uint64_t n
, uint64_t d
, uint64_t *r
)
297 uint64_t q
= __udivdi3(n
, d
);
302 EXPORT_SYMBOL(__udivmoddi4
);
305 * Implementation of 64-bit signed division/modulo for 32-bit machines.
308 __divmoddi4(int64_t n
, int64_t d
, int64_t *r
)
311 boolean_t nn
= B_FALSE
;
312 boolean_t nd
= B_FALSE
;
322 q
= __udivmoddi4(n
, d
, (uint64_t *)&rr
);
332 EXPORT_SYMBOL(__divmoddi4
);
334 #if defined(__arm) || defined(__arm__)
336 * Implementation of 64-bit (un)signed division for 32-bit arm machines.
338 * Run-time ABI for the ARM Architecture (page 20). A pair of (unsigned)
339 * long longs is returned in {{r0, r1}, {r2,r3}}, the quotient in {r0, r1},
340 * and the remainder in {r2, r3}. The return type is specifically left
341 * set to 'void' to ensure the compiler does not overwrite these registers
342 * during the return. All results are in registers as per ABI
345 __aeabi_uldivmod(uint64_t u
, uint64_t v
)
350 res
= __udivdi3(u
, v
);
351 mod
= __umoddi3(u
, v
);
353 register uint32_t r0
asm("r0") = (res
& 0xFFFFFFFF);
354 register uint32_t r1
asm("r1") = (res
>> 32);
355 register uint32_t r2
asm("r2") = (mod
& 0xFFFFFFFF);
356 register uint32_t r3
asm("r3") = (mod
>> 32);
360 : "+r"(r0
), "+r"(r1
), "+r"(r2
),"+r"(r3
) /* output */
361 : "r"(r0
), "r"(r1
), "r"(r2
), "r"(r3
)); /* input */
367 EXPORT_SYMBOL(__aeabi_uldivmod
);
370 __aeabi_ldivmod(int64_t u
, int64_t v
)
375 res
= __divdi3(u
, v
);
376 mod
= __umoddi3(u
, v
);
378 register uint32_t r0
asm("r0") = (res
& 0xFFFFFFFF);
379 register uint32_t r1
asm("r1") = (res
>> 32);
380 register uint32_t r2
asm("r2") = (mod
& 0xFFFFFFFF);
381 register uint32_t r3
asm("r3") = (mod
>> 32);
385 : "+r"(r0
), "+r"(r1
), "+r"(r2
),"+r"(r3
) /* output */
386 : "r"(r0
), "r"(r1
), "r"(r2
), "r"(r3
)); /* input */
392 EXPORT_SYMBOL(__aeabi_ldivmod
);
393 #endif /* __arm || __arm__ */
394 #endif /* BITS_PER_LONG */
397 * NOTE: The strtoxx behavior is solely based on my reading of the Solaris
398 * ddi_strtol(9F) man page. I have not verified the behavior of these
399 * functions against their Solaris counterparts. It is possible that I
400 * may have misinterpreted the man page or the man page is incorrect.
402 int ddi_strtoul(const char *, char **, int, unsigned long *);
403 int ddi_strtol(const char *, char **, int, long *);
404 int ddi_strtoull(const char *, char **, int, unsigned long long *);
405 int ddi_strtoll(const char *, char **, int, long long *);
407 #define define_ddi_strtoux(type, valtype) \
408 int ddi_strtou##type(const char *str, char **endptr, \
409 int base, valtype *result) \
411 valtype last_value, value = 0; \
412 char *ptr = (char *)str; \
413 int flag = 1, digit; \
415 if (strlen(ptr) == 0) \
418 /* Auto-detect base based on prefix */ \
420 if (str[0] == '0') { \
421 if (tolower(str[1]) == 'x' && isxdigit(str[2])) { \
422 base = 16; /* hex */ \
424 } else if (str[1] >= '0' && str[1] < 8) { \
425 base = 8; /* octal */ \
431 base = 10; /* decimal */ \
437 digit = *ptr - '0'; \
438 else if (isalpha(*ptr)) \
439 digit = tolower(*ptr) - 'a' + 10; \
446 last_value = value; \
447 value = value * base + digit; \
448 if (last_value > value) /* Overflow */ \
459 *endptr = (char *)(flag ? ptr : str); \
464 #define define_ddi_strtox(type, valtype) \
465 int ddi_strto##type(const char *str, char **endptr, \
466 int base, valtype *result) \
471 rc = ddi_strtou##type(str + 1, endptr, base, result); \
473 if (*endptr == str + 1) \
474 *endptr = (char *)str; \
476 *result = -*result; \
479 rc = ddi_strtou##type(str, endptr, base, result); \
485 define_ddi_strtoux(l
, unsigned long)
486 define_ddi_strtox(l
, long)
487 define_ddi_strtoux(ll
, unsigned long long)
488 define_ddi_strtox(ll
, long long)
490 EXPORT_SYMBOL(ddi_strtoul
);
491 EXPORT_SYMBOL(ddi_strtol
);
492 EXPORT_SYMBOL(ddi_strtoll
);
493 EXPORT_SYMBOL(ddi_strtoull
);
496 ddi_copyin(const void *from
, void *to
, size_t len
, int flags
)
498 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
499 if (flags
& FKIOCTL
) {
500 memcpy(to
, from
, len
);
504 return (copyin(from
, to
, len
));
506 EXPORT_SYMBOL(ddi_copyin
);
509 ddi_copyout(const void *from
, void *to
, size_t len
, int flags
)
511 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
512 if (flags
& FKIOCTL
) {
513 memcpy(to
, from
, len
);
517 return (copyout(from
, to
, len
));
519 EXPORT_SYMBOL(ddi_copyout
);
522 * Read the unique system identifier from the /etc/hostid file.
524 * The behavior of /usr/bin/hostid on Linux systems with the
525 * regular eglibc and coreutils is:
527 * 1. Generate the value if the /etc/hostid file does not exist
528 * or if the /etc/hostid file is less than four bytes in size.
530 * 2. If the /etc/hostid file is at least 4 bytes, then return
531 * the first four bytes [0..3] in native endian order.
533 * 3. Always ignore bytes [4..] if they exist in the file.
535 * Only the first four bytes are significant, even on systems that
536 * have a 64-bit word size.
540 * eglibc: sysdeps/unix/sysv/linux/gethostid.c
541 * coreutils: src/hostid.c
545 * The /etc/hostid file on Solaris is a text file that often reads:
550 * Directly copying this file to Linux results in a constant
551 * hostid of 4f442023 because the default comment constitutes
552 * the first four bytes of the file.
556 char *spl_hostid_path
= HW_HOSTID_PATH
;
557 module_param(spl_hostid_path
, charp
, 0444);
558 MODULE_PARM_DESC(spl_hostid_path
, "The system hostid file (/etc/hostid)");
561 hostid_read(uint32_t *hostid
)
568 file
= kobj_open_file(spl_hostid_path
);
569 if (file
== (struct _buf
*)-1)
572 error
= kobj_get_filesize(file
, &size
);
574 kobj_close_file(file
);
578 if (size
< sizeof (HW_HOSTID_MASK
)) {
579 kobj_close_file(file
);
584 * Read directly into the variable like eglibc does.
585 * Short reads are okay; native behavior is preserved.
587 error
= kobj_read_file(file
, (char *)&value
, sizeof (value
), 0);
589 kobj_close_file(file
);
593 /* Mask down to 32 bits like coreutils does. */
594 *hostid
= (value
& HW_HOSTID_MASK
);
595 kobj_close_file(file
);
601 * Return the system hostid. Preferentially use the spl_hostid module option
602 * when set, otherwise use the value in the /etc/hostid file.
605 zone_get_hostid(void *zone
)
609 ASSERT3P(zone
, ==, NULL
);
612 return ((uint32_t)(spl_hostid
& HW_HOSTID_MASK
));
614 if (hostid_read(&hostid
) == 0)
619 EXPORT_SYMBOL(zone_get_hostid
);
626 rc
= spl_kmem_init();
630 rc
= spl_vmem_init();
640 * We initialize the random number generator with 128 bits of entropy from the
641 * system random number generator. In the improbable case that we have a zero
642 * seed, we fallback to the system jiffies, unless it is also zero, in which
643 * situation we use a preprogrammed seed. We step forward by 2^64 iterations to
644 * initialize each of the per-cpu seeds so that the sequences generated on each
645 * CPU are guaranteed to never overlap in practice.
648 spl_random_init(void)
653 get_random_bytes(s
, sizeof (s
));
655 if (s
[0] == 0 && s
[1] == 0) {
660 (void) memcpy(s
, "improbable seed", sizeof (s
));
662 printk("SPL: get_random_bytes() returned 0 "
663 "when generating random seed. Setting initial seed to "
664 "0x%016llx%016llx.", cpu_to_be64(s
[0]), cpu_to_be64(s
[1]));
667 for_each_possible_cpu(i
) {
668 uint64_t *wordp
= per_cpu(spl_pseudo_entropy
, i
);
689 bzero(&p0
, sizeof (proc_t
));
692 if ((rc
= spl_kvmem_init()))
695 if ((rc
= spl_mutex_init()))
698 if ((rc
= spl_rw_init()))
701 if ((rc
= spl_tsd_init()))
704 if ((rc
= spl_taskq_init()))
707 if ((rc
= spl_kmem_cache_init()))
710 if ((rc
= spl_vn_init()))
713 if ((rc
= spl_proc_init()))
716 if ((rc
= spl_kstat_init()))
719 if ((rc
= spl_zlib_init()))
722 printk(KERN_NOTICE
"SPL: Loaded module v%s-%s%s\n", SPL_META_VERSION
,
723 SPL_META_RELEASE
, SPL_DEBUG_STR
);
733 spl_kmem_cache_fini();
745 printk(KERN_NOTICE
"SPL: Failed to Load Solaris Porting Layer "
746 "v%s-%s%s, rc = %d\n", SPL_META_VERSION
, SPL_META_RELEASE
,
755 printk(KERN_NOTICE
"SPL: Unloaded module v%s-%s%s\n",
756 SPL_META_VERSION
, SPL_META_RELEASE
, SPL_DEBUG_STR
);
761 spl_kmem_cache_fini();
769 module_init(spl_init
);
770 module_exit(spl_fini
);
772 MODULE_DESCRIPTION("Solaris Porting Layer");
773 MODULE_AUTHOR(SPL_META_AUTHOR
);
774 MODULE_LICENSE(SPL_META_LICENSE
);
775 MODULE_VERSION(SPL_META_VERSION
"-" SPL_META_RELEASE
);