2025-01-04 12:37:45 +11:00
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-10-04 12:33:43 -04:00
|
|
|
|
/*
|
2010-05-17 15:18:00 -07:00
|
|
|
|
* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
|
|
|
|
|
|
* Copyright (C) 2007 The Regents of the University of California.
|
|
|
|
|
|
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
|
|
|
|
|
|
* Written by Brian Behlendorf <behlendorf1@llnl.gov>.
|
2008-05-26 04:38:26 +00:00
|
|
|
|
* UCRL-CODE-235197
|
|
|
|
|
|
*
|
2010-05-17 15:18:00 -07:00
|
|
|
|
* This file is part of the SPL, Solaris Porting Layer.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The SPL is free software; you can redistribute it and/or modify it
|
|
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
|
|
* option) any later version.
|
2008-05-26 04:38:26 +00:00
|
|
|
|
*
|
2010-05-17 15:18:00 -07:00
|
|
|
|
* The SPL is distributed in the hope that it will be useful, but WITHOUT
|
2008-05-26 04:38:26 +00:00
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
|
|
* for more details.
|
|
|
|
|
|
*
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2010-05-17 15:18:00 -07:00
|
|
|
|
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
|
2018-02-07 11:49:38 -08:00
|
|
|
|
*
|
2010-05-17 15:18:00 -07:00
|
|
|
|
* Solaris Porting Layer (SPL) Generic Implementation.
|
2017-10-04 12:33:43 -04:00
|
|
|
|
*/
|
2008-05-26 04:38:26 +00:00
|
|
|
|
|
2022-10-20 17:14:42 -04:00
|
|
|
|
#include <sys/isa_defs.h>
|
2008-03-04 18:22:31 +00:00
|
|
|
|
#include <sys/sysmacros.h>
|
2009-02-19 11:26:17 -08:00
|
|
|
|
#include <sys/systeminfo.h>
|
2008-03-14 00:04:01 +00:00
|
|
|
|
#include <sys/vmsystm.h>
|
2008-03-14 20:56:26 +00:00
|
|
|
|
#include <sys/kmem.h>
|
2014-12-08 13:04:42 -05:00
|
|
|
|
#include <sys/kmem_cache.h>
|
|
|
|
|
|
#include <sys/vmem.h>
|
2008-05-05 20:18:49 +00:00
|
|
|
|
#include <sys/mutex.h>
|
2009-09-25 14:14:35 -07:00
|
|
|
|
#include <sys/rwlock.h>
|
2009-01-05 15:08:03 -08:00
|
|
|
|
#include <sys/taskq.h>
|
2010-11-30 09:51:46 -08:00
|
|
|
|
#include <sys/tsd.h>
|
2011-02-25 13:26:19 -08:00
|
|
|
|
#include <sys/zmod.h>
|
2008-03-31 20:42:36 +00:00
|
|
|
|
#include <sys/debug.h>
|
2008-04-18 23:39:58 +00:00
|
|
|
|
#include <sys/proc.h>
|
2008-05-08 23:21:47 +00:00
|
|
|
|
#include <sys/kstat.h>
|
2009-07-10 10:56:32 -07:00
|
|
|
|
#include <sys/file.h>
|
2020-06-11 16:25:39 -04:00
|
|
|
|
#include <sys/sunddi.h>
|
2014-12-08 13:04:42 -05:00
|
|
|
|
#include <linux/ctype.h>
|
2014-07-11 18:36:28 -04:00
|
|
|
|
#include <sys/disp.h>
|
|
|
|
|
|
#include <sys/random.h>
|
2022-01-22 01:56:46 +01:00
|
|
|
|
#include <sys/string.h>
|
2008-04-12 04:27:59 +00:00
|
|
|
|
#include <linux/kmod.h>
|
2019-11-01 10:41:03 -07:00
|
|
|
|
#include <linux/mod_compat.h>
|
2019-11-21 09:32:57 -08:00
|
|
|
|
#include <sys/cred.h>
|
|
|
|
|
|
#include <sys/vnode.h>
|
2022-09-28 21:48:46 +05:00
|
|
|
|
#include <sys/misc.h>
|
2022-10-03 15:06:54 -04:00
|
|
|
|
#include <linux/mod_compat.h>
|
2008-04-12 04:27:59 +00:00
|
|
|
|
|
2013-03-11 21:16:36 -04:00
|
|
|
|
unsigned long spl_hostid = 0;
|
2008-04-12 04:27:59 +00:00
|
|
|
|
EXPORT_SYMBOL(spl_hostid);
|
2022-01-15 00:37:55 +01:00
|
|
|
|
|
2011-04-11 14:49:50 -05:00
|
|
|
|
module_param(spl_hostid, ulong, 0644);
|
|
|
|
|
|
MODULE_PARM_DESC(spl_hostid, "The system hostid.");
|
2008-03-31 20:42:36 +00:00
|
|
|
|
|
2016-10-04 17:26:36 -07:00
|
|
|
|
proc_t p0;
|
2008-02-27 19:09:51 +00:00
|
|
|
|
EXPORT_SYMBOL(p0);
|
2008-02-27 21:56:51 +00:00
|
|
|
|
|
2014-07-11 18:36:28 -04:00
|
|
|
|
/*
|
2022-10-20 17:14:42 -04:00
|
|
|
|
* xoshiro256++ 1.0 PRNG by David Blackman and Sebastiano Vigna
|
2014-07-11 18:36:28 -04:00
|
|
|
|
*
|
2022-10-20 17:14:42 -04:00
|
|
|
|
* "Scrambled Linear Pseudorandom Number Generators∗"
|
|
|
|
|
|
* https://vigna.di.unimi.it/ftp/papers/ScrambledLinear.pdf
|
2014-07-11 18:36:28 -04:00
|
|
|
|
*
|
|
|
|
|
|
* random_get_pseudo_bytes() is an API function on Illumos whose sole purpose
|
|
|
|
|
|
* is to provide bytes containing random numbers. It is mapped to /dev/urandom
|
|
|
|
|
|
* on Illumos, which uses a "FIPS 186-2 algorithm". No user of the SPL's
|
|
|
|
|
|
* random_get_pseudo_bytes() needs bytes that are of cryptographic quality, so
|
|
|
|
|
|
* we can implement it using a fast PRNG that we seed using Linux' actual
|
|
|
|
|
|
* equivalent to random_get_pseudo_bytes(). We do this by providing each CPU
|
|
|
|
|
|
* with an independent seed so that all calls to random_get_pseudo_bytes() are
|
|
|
|
|
|
* free of atomic instructions.
|
|
|
|
|
|
*
|
|
|
|
|
|
* A consequence of using a fast PRNG is that using random_get_pseudo_bytes()
|
2022-10-20 17:14:42 -04:00
|
|
|
|
* to generate words larger than 256 bits will paradoxically be limited to
|
|
|
|
|
|
* `2^256 - 1` possibilities. This is because we have a sequence of `2^256 - 1`
|
|
|
|
|
|
* 256-bit words and selecting the first will implicitly select the second. If
|
2019-08-30 23:32:18 +02:00
|
|
|
|
* a caller finds this behavior undesirable, random_get_bytes() should be used
|
2014-07-11 18:36:28 -04:00
|
|
|
|
* instead.
|
|
|
|
|
|
*
|
|
|
|
|
|
* XXX: Linux interrupt handlers that trigger within the critical section
|
2022-10-20 17:14:42 -04:00
|
|
|
|
* formed by `s[3] = xp[3];` and `xp[0] = s[0];` and call this function will
|
2014-07-11 18:36:28 -04:00
|
|
|
|
* see the same numbers. Nothing in the code currently calls this in an
|
|
|
|
|
|
* interrupt handler, so this is considered to be okay. If that becomes a
|
|
|
|
|
|
* problem, we could create a set of per-cpu variables for interrupt handlers
|
|
|
|
|
|
* and use them when in_interrupt() from linux/preempt_mask.h evaluates to
|
|
|
|
|
|
* true.
|
|
|
|
|
|
*/
|
2022-10-18 20:05:32 +02:00
|
|
|
|
static void __percpu *spl_pseudo_entropy;
|
2014-07-11 18:36:28 -04:00
|
|
|
|
|
|
|
|
|
|
/*
|
2022-10-20 17:14:42 -04:00
|
|
|
|
* rotl()/spl_rand_next()/spl_rand_jump() are copied from the following CC-0
|
|
|
|
|
|
* licensed file:
|
2014-07-11 18:36:28 -04:00
|
|
|
|
*
|
2022-10-20 17:14:42 -04:00
|
|
|
|
* https://prng.di.unimi.it/xoshiro256plusplus.c
|
2014-07-11 18:36:28 -04:00
|
|
|
|
*/
|
|
|
|
|
|
|
2022-10-20 17:14:42 -04:00
|
|
|
|
static inline uint64_t rotl(const uint64_t x, int k)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ((x << k) | (x >> (64 - k)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-11 18:36:28 -04:00
|
|
|
|
static inline uint64_t
|
2018-02-24 10:05:37 -08:00
|
|
|
|
spl_rand_next(uint64_t *s)
|
|
|
|
|
|
{
|
2022-10-20 17:14:42 -04:00
|
|
|
|
const uint64_t result = rotl(s[0] + s[3], 23) + s[0];
|
|
|
|
|
|
|
|
|
|
|
|
const uint64_t t = s[1] << 17;
|
|
|
|
|
|
|
|
|
|
|
|
s[2] ^= s[0];
|
|
|
|
|
|
s[3] ^= s[1];
|
|
|
|
|
|
s[1] ^= s[2];
|
|
|
|
|
|
s[0] ^= s[3];
|
|
|
|
|
|
|
|
|
|
|
|
s[2] ^= t;
|
|
|
|
|
|
|
|
|
|
|
|
s[3] = rotl(s[3], 45);
|
|
|
|
|
|
|
|
|
|
|
|
return (result);
|
2014-07-11 18:36:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
2018-02-24 10:05:37 -08:00
|
|
|
|
spl_rand_jump(uint64_t *s)
|
|
|
|
|
|
{
|
2022-10-20 17:14:42 -04:00
|
|
|
|
static const uint64_t JUMP[] = { 0x180ec6d33cfd0aba,
|
|
|
|
|
|
0xd5a61266f0c9392c, 0xa9582618e03fc9aa, 0x39abdc4529b1661c };
|
2014-07-11 18:36:28 -04:00
|
|
|
|
|
|
|
|
|
|
uint64_t s0 = 0;
|
|
|
|
|
|
uint64_t s1 = 0;
|
2022-10-20 17:14:42 -04:00
|
|
|
|
uint64_t s2 = 0;
|
|
|
|
|
|
uint64_t s3 = 0;
|
2014-07-11 18:36:28 -04:00
|
|
|
|
int i, b;
|
2018-02-07 11:49:38 -08:00
|
|
|
|
for (i = 0; i < sizeof (JUMP) / sizeof (*JUMP); i++)
|
|
|
|
|
|
for (b = 0; b < 64; b++) {
|
2014-07-11 18:36:28 -04:00
|
|
|
|
if (JUMP[i] & 1ULL << b) {
|
|
|
|
|
|
s0 ^= s[0];
|
|
|
|
|
|
s1 ^= s[1];
|
2022-10-20 17:14:42 -04:00
|
|
|
|
s2 ^= s[2];
|
|
|
|
|
|
s3 ^= s[3];
|
2014-07-11 18:36:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
(void) spl_rand_next(s);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
s[0] = s0;
|
|
|
|
|
|
s[1] = s1;
|
2022-10-20 17:14:42 -04:00
|
|
|
|
s[2] = s2;
|
|
|
|
|
|
s[3] = s3;
|
2014-07-11 18:36:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
|
random_get_pseudo_bytes(uint8_t *ptr, size_t len)
|
|
|
|
|
|
{
|
2022-10-20 17:14:42 -04:00
|
|
|
|
uint64_t *xp, s[4];
|
2014-07-11 18:36:28 -04:00
|
|
|
|
|
|
|
|
|
|
ASSERT(ptr);
|
|
|
|
|
|
|
2020-02-06 18:26:13 +01:00
|
|
|
|
xp = get_cpu_ptr(spl_pseudo_entropy);
|
2014-07-11 18:36:28 -04:00
|
|
|
|
|
|
|
|
|
|
s[0] = xp[0];
|
|
|
|
|
|
s[1] = xp[1];
|
2022-10-20 17:14:42 -04:00
|
|
|
|
s[2] = xp[2];
|
|
|
|
|
|
s[3] = xp[3];
|
2014-07-11 18:36:28 -04:00
|
|
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
|
|
union {
|
|
|
|
|
|
uint64_t ui64;
|
|
|
|
|
|
uint8_t byte[sizeof (uint64_t)];
|
|
|
|
|
|
}entropy;
|
|
|
|
|
|
int i = MIN(len, sizeof (uint64_t));
|
|
|
|
|
|
|
|
|
|
|
|
len -= i;
|
|
|
|
|
|
entropy.ui64 = spl_rand_next(s);
|
|
|
|
|
|
|
2022-10-20 17:14:42 -04:00
|
|
|
|
/*
|
|
|
|
|
|
* xoshiro256++ has low entropy lower bytes, so we copy the
|
|
|
|
|
|
* higher order bytes first.
|
|
|
|
|
|
*/
|
2014-07-11 18:36:28 -04:00
|
|
|
|
while (i--)
|
2022-10-20 17:14:42 -04:00
|
|
|
|
#ifdef _ZFS_BIG_ENDIAN
|
2014-07-11 18:36:28 -04:00
|
|
|
|
*ptr++ = entropy.byte[i];
|
2022-10-20 17:14:42 -04:00
|
|
|
|
#else
|
|
|
|
|
|
*ptr++ = entropy.byte[7 - i];
|
|
|
|
|
|
#endif
|
2014-07-11 18:36:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xp[0] = s[0];
|
|
|
|
|
|
xp[1] = s[1];
|
2022-10-20 17:14:42 -04:00
|
|
|
|
xp[2] = s[2];
|
|
|
|
|
|
xp[3] = s[3];
|
2014-07-11 18:36:28 -04:00
|
|
|
|
|
2020-02-06 18:26:13 +01:00
|
|
|
|
put_cpu_ptr(spl_pseudo_entropy);
|
2014-07-11 18:36:28 -04:00
|
|
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
|
|
}
|
|
|
|
|
|
EXPORT_SYMBOL(random_get_pseudo_bytes);
|
|
|
|
|
|
|
2018-02-07 11:49:38 -08:00
|
|
|
|
/*
|
|
|
|
|
|
* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
|
2009-01-13 09:30:59 -08:00
|
|
|
|
* ddi_strtol(9F) man page. I have not verified the behavior of these
|
|
|
|
|
|
* functions against their Solaris counterparts. It is possible that I
|
2009-05-20 10:08:37 -07:00
|
|
|
|
* may have misinterpreted the man page or the man page is incorrect.
|
2009-01-13 09:30:59 -08:00
|
|
|
|
*/
|
2008-12-05 16:23:57 -08:00
|
|
|
|
int ddi_strtol(const char *, char **, int, long *);
|
|
|
|
|
|
int ddi_strtoull(const char *, char **, int, unsigned long long *);
|
|
|
|
|
|
int ddi_strtoll(const char *, char **, int, long long *);
|
|
|
|
|
|
|
2022-05-07 19:54:29 +02:00
|
|
|
|
#define define_ddi_strtox(type, valtype) \
|
|
|
|
|
|
int ddi_strto##type(const char *str, char **endptr, \
|
2018-02-07 11:49:38 -08:00
|
|
|
|
int base, valtype *result) \
|
2008-12-05 16:23:57 -08:00
|
|
|
|
{ \
|
2009-01-13 09:30:59 -08:00
|
|
|
|
valtype last_value, value = 0; \
|
|
|
|
|
|
char *ptr = (char *)str; \
|
2022-05-07 19:54:29 +02:00
|
|
|
|
int digit, minus = 0; \
|
|
|
|
|
|
\
|
|
|
|
|
|
while (strchr(" \t\n\r\f", *ptr)) \
|
|
|
|
|
|
++ptr; \
|
2009-01-13 09:30:59 -08:00
|
|
|
|
\
|
|
|
|
|
|
if (strlen(ptr) == 0) \
|
2018-02-07 11:49:38 -08:00
|
|
|
|
return (EINVAL); \
|
2009-01-13 09:30:59 -08:00
|
|
|
|
\
|
2022-05-07 19:54:29 +02:00
|
|
|
|
switch (*ptr) { \
|
|
|
|
|
|
case '-': \
|
|
|
|
|
|
minus = 1; \
|
|
|
|
|
|
zfs_fallthrough; \
|
|
|
|
|
|
case '+': \
|
|
|
|
|
|
++ptr; \
|
|
|
|
|
|
break; \
|
|
|
|
|
|
} \
|
|
|
|
|
|
\
|
2009-01-13 09:30:59 -08:00
|
|
|
|
/* Auto-detect base based on prefix */ \
|
|
|
|
|
|
if (!base) { \
|
|
|
|
|
|
if (str[0] == '0') { \
|
2018-02-07 11:49:38 -08:00
|
|
|
|
if (tolower(str[1]) == 'x' && isxdigit(str[2])) { \
|
2009-01-13 09:30:59 -08:00
|
|
|
|
base = 16; /* hex */ \
|
|
|
|
|
|
ptr += 2; \
|
2023-03-05 02:01:58 -05:00
|
|
|
|
} else if (str[1] >= '0' && str[1] < '8') { \
|
2009-01-13 09:30:59 -08:00
|
|
|
|
base = 8; /* octal */ \
|
|
|
|
|
|
ptr += 1; \
|
|
|
|
|
|
} else { \
|
2018-02-07 11:49:38 -08:00
|
|
|
|
return (EINVAL); \
|
2009-01-13 09:30:59 -08:00
|
|
|
|
} \
|
|
|
|
|
|
} else { \
|
|
|
|
|
|
base = 10; /* decimal */ \
|
|
|
|
|
|
} \
|
|
|
|
|
|
} \
|
|
|
|
|
|
\
|
|
|
|
|
|
while (1) { \
|
|
|
|
|
|
if (isdigit(*ptr)) \
|
|
|
|
|
|
digit = *ptr - '0'; \
|
|
|
|
|
|
else if (isalpha(*ptr)) \
|
|
|
|
|
|
digit = tolower(*ptr) - 'a' + 10; \
|
|
|
|
|
|
else \
|
|
|
|
|
|
break; \
|
|
|
|
|
|
\
|
|
|
|
|
|
if (digit >= base) \
|
|
|
|
|
|
break; \
|
2008-12-05 16:23:57 -08:00
|
|
|
|
\
|
2009-01-13 09:30:59 -08:00
|
|
|
|
last_value = value; \
|
|
|
|
|
|
value = value * base + digit; \
|
|
|
|
|
|
if (last_value > value) /* Overflow */ \
|
2018-02-07 11:49:38 -08:00
|
|
|
|
return (ERANGE); \
|
2008-12-05 16:23:57 -08:00
|
|
|
|
\
|
2009-01-13 09:30:59 -08:00
|
|
|
|
ptr++; \
|
2008-12-05 16:23:57 -08:00
|
|
|
|
} \
|
|
|
|
|
|
\
|
2022-05-07 19:54:29 +02:00
|
|
|
|
*result = minus ? -value : value; \
|
2009-01-13 09:30:59 -08:00
|
|
|
|
\
|
|
|
|
|
|
if (endptr) \
|
2022-05-07 19:23:28 +02:00
|
|
|
|
*endptr = ptr; \
|
2009-01-13 09:30:59 -08:00
|
|
|
|
\
|
2018-02-07 11:49:38 -08:00
|
|
|
|
return (0); \
|
2008-12-05 16:23:57 -08:00
|
|
|
|
} \
|
|
|
|
|
|
|
|
|
|
|
|
define_ddi_strtox(l, long)
|
2022-05-07 19:54:29 +02:00
|
|
|
|
define_ddi_strtox(ull, unsigned long long)
|
2008-12-05 16:23:57 -08:00
|
|
|
|
define_ddi_strtox(ll, long long)
|
|
|
|
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(ddi_strtol);
|
|
|
|
|
|
EXPORT_SYMBOL(ddi_strtoll);
|
|
|
|
|
|
EXPORT_SYMBOL(ddi_strtoull);
|
2008-03-12 21:33:28 +00:00
|
|
|
|
|
2009-07-10 10:56:32 -07:00
|
|
|
|
int
|
|
|
|
|
|
ddi_copyin(const void *from, void *to, size_t len, int flags)
|
|
|
|
|
|
{
|
|
|
|
|
|
/* Fake ioctl() issued by kernel, 'from' is a kernel address */
|
|
|
|
|
|
if (flags & FKIOCTL) {
|
|
|
|
|
|
memcpy(to, from, len);
|
2018-02-07 11:49:38 -08:00
|
|
|
|
return (0);
|
2009-07-10 10:56:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-07 11:49:38 -08:00
|
|
|
|
return (copyin(from, to, len));
|
2009-07-10 10:56:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
EXPORT_SYMBOL(ddi_copyin);
|
|
|
|
|
|
|
2022-09-28 21:48:46 +05:00
|
|
|
|
/*
|
|
|
|
|
|
* Post a uevent to userspace whenever a new vdev adds to the pool. It is
|
|
|
|
|
|
* necessary to sync blkid information with udev, which zed daemon uses
|
|
|
|
|
|
* during device hotplug to identify the vdev.
|
|
|
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
|
|
|
spl_signal_kobj_evt(struct block_device *bdev)
|
|
|
|
|
|
{
|
|
|
|
|
|
#if defined(HAVE_BDEV_KOBJ) || defined(HAVE_PART_TO_DEV)
|
|
|
|
|
|
#ifdef HAVE_BDEV_KOBJ
|
|
|
|
|
|
struct kobject *disk_kobj = bdev_kobj(bdev);
|
|
|
|
|
|
#else
|
|
|
|
|
|
struct kobject *disk_kobj = &part_to_dev(bdev->bd_part)->kobj;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
if (disk_kobj) {
|
|
|
|
|
|
int ret = kobject_uevent(disk_kobj, KOBJ_CHANGE);
|
|
|
|
|
|
if (ret) {
|
|
|
|
|
|
pr_warn("ZFS: Sending event '%d' to kobject: '%s'"
|
|
|
|
|
|
" (%p): failed(ret:%d)\n", KOBJ_CHANGE,
|
|
|
|
|
|
kobject_name(disk_kobj), disk_kobj, ret);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#else
|
|
|
|
|
|
/*
|
|
|
|
|
|
* This is encountered if neither bdev_kobj() nor part_to_dev() is available
|
|
|
|
|
|
* in the kernel - likely due to an API change that needs to be chased down.
|
|
|
|
|
|
*/
|
|
|
|
|
|
#error "Unsupported kernel: unable to get struct kobj from bdev"
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
EXPORT_SYMBOL(spl_signal_kobj_evt);
|
|
|
|
|
|
|
2009-07-10 10:56:32 -07:00
|
|
|
|
int
|
|
|
|
|
|
ddi_copyout(const void *from, void *to, size_t len, int flags)
|
|
|
|
|
|
{
|
|
|
|
|
|
/* Fake ioctl() issued by kernel, 'from' is a kernel address */
|
|
|
|
|
|
if (flags & FKIOCTL) {
|
|
|
|
|
|
memcpy(to, from, len);
|
2018-02-07 11:49:38 -08:00
|
|
|
|
return (0);
|
2009-07-10 10:56:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-07 11:49:38 -08:00
|
|
|
|
return (copyout(from, to, len));
|
2009-07-10 10:56:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
EXPORT_SYMBOL(ddi_copyout);
|
|
|
|
|
|
|
2020-06-15 14:30:37 -04:00
|
|
|
|
static int
|
2019-11-21 09:32:57 -08:00
|
|
|
|
spl_getattr(struct file *filp, struct kstat *stat)
|
|
|
|
|
|
{
|
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
|
|
ASSERT(filp);
|
|
|
|
|
|
ASSERT(stat);
|
|
|
|
|
|
|
|
|
|
|
|
rc = vfs_getattr(&filp->f_path, stat, STATX_BASIC_STATS,
|
|
|
|
|
|
AT_STATX_SYNC_AS_STAT);
|
|
|
|
|
|
if (rc)
|
|
|
|
|
|
return (-rc);
|
|
|
|
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-06-20 14:53:56 -05:00
|
|
|
|
/*
|
|
|
|
|
|
* Read the unique system identifier from the /etc/hostid file.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The behavior of /usr/bin/hostid on Linux systems with the
|
|
|
|
|
|
* regular eglibc and coreutils is:
|
|
|
|
|
|
*
|
|
|
|
|
|
* 1. Generate the value if the /etc/hostid file does not exist
|
|
|
|
|
|
* or if the /etc/hostid file is less than four bytes in size.
|
|
|
|
|
|
*
|
|
|
|
|
|
* 2. If the /etc/hostid file is at least 4 bytes, then return
|
|
|
|
|
|
* the first four bytes [0..3] in native endian order.
|
|
|
|
|
|
*
|
|
|
|
|
|
* 3. Always ignore bytes [4..] if they exist in the file.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Only the first four bytes are significant, even on systems that
|
|
|
|
|
|
* have a 64-bit word size.
|
|
|
|
|
|
*
|
|
|
|
|
|
* See:
|
|
|
|
|
|
*
|
|
|
|
|
|
* eglibc: sysdeps/unix/sysv/linux/gethostid.c
|
|
|
|
|
|
* coreutils: src/hostid.c
|
|
|
|
|
|
*
|
|
|
|
|
|
* Notes:
|
|
|
|
|
|
*
|
|
|
|
|
|
* The /etc/hostid file on Solaris is a text file that often reads:
|
|
|
|
|
|
*
|
|
|
|
|
|
* # DO NOT EDIT
|
|
|
|
|
|
* "0123456789"
|
|
|
|
|
|
*
|
|
|
|
|
|
* Directly copying this file to Linux results in a constant
|
|
|
|
|
|
* hostid of 4f442023 because the default comment constitutes
|
|
|
|
|
|
* the first four bytes of the file.
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2022-01-15 00:37:55 +01:00
|
|
|
|
static char *spl_hostid_path = HW_HOSTID_PATH;
|
2011-06-20 14:53:56 -05:00
|
|
|
|
module_param(spl_hostid_path, charp, 0444);
|
|
|
|
|
|
MODULE_PARM_DESC(spl_hostid_path, "The system hostid file (/etc/hostid)");
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2017-07-10 15:24:52 -04:00
|
|
|
|
hostid_read(uint32_t *hostid)
|
2011-06-20 14:53:56 -05:00
|
|
|
|
{
|
|
|
|
|
|
uint64_t size;
|
2017-07-10 15:24:52 -04:00
|
|
|
|
uint32_t value = 0;
|
|
|
|
|
|
int error;
|
2019-11-21 09:32:57 -08:00
|
|
|
|
loff_t off;
|
|
|
|
|
|
struct file *filp;
|
|
|
|
|
|
struct kstat stat;
|
|
|
|
|
|
|
|
|
|
|
|
filp = filp_open(spl_hostid_path, 0, 0);
|
2011-06-20 14:53:56 -05:00
|
|
|
|
|
2019-11-21 09:32:57 -08:00
|
|
|
|
if (IS_ERR(filp))
|
2017-07-10 15:24:52 -04:00
|
|
|
|
return (ENOENT);
|
2011-06-20 14:53:56 -05:00
|
|
|
|
|
2019-11-21 09:32:57 -08:00
|
|
|
|
error = spl_getattr(filp, &stat);
|
2017-07-10 15:24:52 -04:00
|
|
|
|
if (error) {
|
2019-11-21 09:32:57 -08:00
|
|
|
|
filp_close(filp, 0);
|
2017-07-10 15:24:52 -04:00
|
|
|
|
return (error);
|
2011-06-20 14:53:56 -05:00
|
|
|
|
}
|
2019-11-21 09:32:57 -08:00
|
|
|
|
size = stat.size;
|
2021-11-02 21:02:57 +01:00
|
|
|
|
// cppcheck-suppress sizeofwithnumericparameter
|
2018-02-07 11:49:38 -08:00
|
|
|
|
if (size < sizeof (HW_HOSTID_MASK)) {
|
2019-11-21 09:32:57 -08:00
|
|
|
|
filp_close(filp, 0);
|
2017-07-10 15:24:52 -04:00
|
|
|
|
return (EINVAL);
|
2011-06-20 14:53:56 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-11-21 09:32:57 -08:00
|
|
|
|
off = 0;
|
2017-07-10 15:24:52 -04:00
|
|
|
|
/*
|
|
|
|
|
|
* Read directly into the variable like eglibc does.
|
|
|
|
|
|
* Short reads are okay; native behavior is preserved.
|
|
|
|
|
|
*/
|
2024-08-24 20:46:34 +10:00
|
|
|
|
error = kernel_read(filp, &value, sizeof (value), &off);
|
2017-07-10 15:24:52 -04:00
|
|
|
|
if (error < 0) {
|
2019-11-21 09:32:57 -08:00
|
|
|
|
filp_close(filp, 0);
|
2017-07-10 15:24:52 -04:00
|
|
|
|
return (EIO);
|
2011-06-20 14:53:56 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Mask down to 32 bits like coreutils does. */
|
2017-07-10 15:24:52 -04:00
|
|
|
|
*hostid = (value & HW_HOSTID_MASK);
|
2019-11-21 09:32:57 -08:00
|
|
|
|
filp_close(filp, 0);
|
2017-07-10 15:24:52 -04:00
|
|
|
|
|
2018-02-07 11:49:38 -08:00
|
|
|
|
return (0);
|
2011-06-20 14:53:56 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-10 15:24:52 -04:00
|
|
|
|
/*
|
|
|
|
|
|
* Return the system hostid. Preferentially use the spl_hostid module option
|
|
|
|
|
|
* when set, otherwise use the value in the /etc/hostid file.
|
|
|
|
|
|
*/
|
2009-02-19 11:26:17 -08:00
|
|
|
|
uint32_t
|
|
|
|
|
|
zone_get_hostid(void *zone)
|
|
|
|
|
|
{
|
2017-07-10 15:24:52 -04:00
|
|
|
|
uint32_t hostid;
|
2009-02-19 11:26:17 -08:00
|
|
|
|
|
2025-08-04 20:00:19 +10:00
|
|
|
|
ASSERT0P(zone);
|
2012-07-05 09:22:03 +02:00
|
|
|
|
|
2017-07-10 15:24:52 -04:00
|
|
|
|
if (spl_hostid != 0)
|
|
|
|
|
|
return ((uint32_t)(spl_hostid & HW_HOSTID_MASK));
|
2012-07-05 09:22:03 +02:00
|
|
|
|
|
2017-07-10 15:24:52 -04:00
|
|
|
|
if (hostid_read(&hostid) == 0)
|
|
|
|
|
|
return (hostid);
|
2015-01-23 15:01:03 +08:00
|
|
|
|
|
2017-07-10 15:24:52 -04:00
|
|
|
|
return (0);
|
2009-02-19 11:26:17 -08:00
|
|
|
|
}
|
|
|
|
|
|
EXPORT_SYMBOL(zone_get_hostid);
|
|
|
|
|
|
|
2014-12-08 13:04:42 -05:00
|
|
|
|
static int
|
|
|
|
|
|
spl_kvmem_init(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
|
|
|
|
rc = spl_kmem_init();
|
|
|
|
|
|
if (rc)
|
2015-12-02 14:52:46 -08:00
|
|
|
|
return (rc);
|
2014-12-08 13:04:42 -05:00
|
|
|
|
|
|
|
|
|
|
rc = spl_vmem_init();
|
2015-12-02 14:52:46 -08:00
|
|
|
|
if (rc) {
|
|
|
|
|
|
spl_kmem_fini();
|
|
|
|
|
|
return (rc);
|
|
|
|
|
|
}
|
2014-12-08 13:04:42 -05:00
|
|
|
|
|
|
|
|
|
|
return (rc);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-11 18:36:28 -04:00
|
|
|
|
/*
|
|
|
|
|
|
* We initialize the random number generator with 128 bits of entropy from the
|
|
|
|
|
|
* system random number generator. In the improbable case that we have a zero
|
|
|
|
|
|
* seed, we fallback to the system jiffies, unless it is also zero, in which
|
|
|
|
|
|
* situation we use a preprogrammed seed. We step forward by 2^64 iterations to
|
|
|
|
|
|
* initialize each of the per-cpu seeds so that the sequences generated on each
|
|
|
|
|
|
* CPU are guaranteed to never overlap in practice.
|
|
|
|
|
|
*/
|
2022-09-08 13:28:20 -04:00
|
|
|
|
static int __init
|
2014-07-11 18:36:28 -04:00
|
|
|
|
spl_random_init(void)
|
|
|
|
|
|
{
|
2022-10-20 17:14:42 -04:00
|
|
|
|
uint64_t s[4];
|
2019-12-14 00:07:48 +00:00
|
|
|
|
int i = 0;
|
2014-07-11 18:36:28 -04:00
|
|
|
|
|
2022-10-20 17:14:42 -04:00
|
|
|
|
spl_pseudo_entropy = __alloc_percpu(4 * sizeof (uint64_t),
|
2020-02-06 18:26:13 +01:00
|
|
|
|
sizeof (uint64_t));
|
|
|
|
|
|
|
2022-09-08 13:28:20 -04:00
|
|
|
|
if (!spl_pseudo_entropy)
|
|
|
|
|
|
return (-ENOMEM);
|
|
|
|
|
|
|
2014-07-11 18:36:28 -04:00
|
|
|
|
get_random_bytes(s, sizeof (s));
|
|
|
|
|
|
|
2022-10-20 17:14:42 -04:00
|
|
|
|
if (s[0] == 0 && s[1] == 0 && s[2] == 0 && s[3] == 0) {
|
2014-07-11 18:36:28 -04:00
|
|
|
|
if (jiffies != 0) {
|
|
|
|
|
|
s[0] = jiffies;
|
|
|
|
|
|
s[1] = ~0 - jiffies;
|
2022-10-20 17:14:42 -04:00
|
|
|
|
s[2] = ~jiffies;
|
|
|
|
|
|
s[3] = jiffies - ~0;
|
2014-07-11 18:36:28 -04:00
|
|
|
|
} else {
|
2022-10-20 17:14:42 -04:00
|
|
|
|
(void) memcpy(s, "improbable seed", 16);
|
2014-07-11 18:36:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
printk("SPL: get_random_bytes() returned 0 "
|
|
|
|
|
|
"when generating random seed. Setting initial seed to "
|
2022-10-20 17:14:42 -04:00
|
|
|
|
"0x%016llx%016llx%016llx%016llx.\n", cpu_to_be64(s[0]),
|
|
|
|
|
|
cpu_to_be64(s[1]), cpu_to_be64(s[2]), cpu_to_be64(s[3]));
|
2014-07-11 18:36:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-07 20:59:46 -07:00
|
|
|
|
for_each_possible_cpu(i) {
|
2020-02-06 18:26:13 +01:00
|
|
|
|
uint64_t *wordp = per_cpu_ptr(spl_pseudo_entropy, i);
|
2014-07-11 18:36:28 -04:00
|
|
|
|
|
|
|
|
|
|
spl_rand_jump(s);
|
|
|
|
|
|
|
|
|
|
|
|
wordp[0] = s[0];
|
|
|
|
|
|
wordp[1] = s[1];
|
2022-10-20 17:14:42 -04:00
|
|
|
|
wordp[2] = s[2];
|
|
|
|
|
|
wordp[3] = s[3];
|
2014-07-11 18:36:28 -04:00
|
|
|
|
}
|
2022-09-08 13:28:20 -04:00
|
|
|
|
|
|
|
|
|
|
return (0);
|
2014-07-11 18:36:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-06 18:26:13 +01:00
|
|
|
|
static void
|
|
|
|
|
|
spl_random_fini(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
free_percpu(spl_pseudo_entropy);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-12-08 13:04:42 -05:00
|
|
|
|
static void
|
|
|
|
|
|
spl_kvmem_fini(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
spl_vmem_fini();
|
|
|
|
|
|
spl_kmem_fini();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-18 15:24:15 -08:00
|
|
|
|
static int __init
|
|
|
|
|
|
spl_init(void)
|
2008-04-18 23:39:58 +00:00
|
|
|
|
{
|
|
|
|
|
|
int rc = 0;
|
2008-04-12 04:27:59 +00:00
|
|
|
|
|
2022-09-08 13:28:20 -04:00
|
|
|
|
if ((rc = spl_random_init()))
|
|
|
|
|
|
goto out0;
|
2014-07-11 18:36:28 -04:00
|
|
|
|
|
2014-12-08 13:04:42 -05:00
|
|
|
|
if ((rc = spl_kvmem_init()))
|
2014-11-05 17:30:35 -05:00
|
|
|
|
goto out1;
|
2008-03-31 20:42:36 +00:00
|
|
|
|
|
2015-12-02 14:52:46 -08:00
|
|
|
|
if ((rc = spl_tsd_init()))
|
2019-07-12 14:40:15 -07:00
|
|
|
|
goto out2;
|
2008-03-14 00:04:01 +00:00
|
|
|
|
|
2024-05-07 10:18:22 +10:00
|
|
|
|
if ((rc = spl_proc_init()))
|
2019-07-12 14:40:15 -07:00
|
|
|
|
goto out3;
|
2008-05-08 23:21:47 +00:00
|
|
|
|
|
2024-05-07 10:18:22 +10:00
|
|
|
|
if ((rc = spl_kstat_init()))
|
2019-07-12 14:40:15 -07:00
|
|
|
|
goto out4;
|
2009-01-05 15:08:03 -08:00
|
|
|
|
|
2024-05-07 10:18:22 +10:00
|
|
|
|
if ((rc = spl_taskq_init()))
|
2019-11-21 09:32:57 -08:00
|
|
|
|
goto out5;
|
2010-11-30 09:51:46 -08:00
|
|
|
|
|
2024-05-07 10:18:22 +10:00
|
|
|
|
if ((rc = spl_kmem_cache_init()))
|
2019-11-21 09:32:57 -08:00
|
|
|
|
goto out6;
|
2011-02-25 13:26:19 -08:00
|
|
|
|
|
2015-12-02 14:52:46 -08:00
|
|
|
|
if ((rc = spl_zlib_init()))
|
2019-11-21 09:32:57 -08:00
|
|
|
|
goto out7;
|
2015-12-02 14:52:46 -08:00
|
|
|
|
|
2021-02-21 10:19:43 -06:00
|
|
|
|
if ((rc = spl_zone_init()))
|
|
|
|
|
|
goto out8;
|
|
|
|
|
|
|
2014-11-05 17:30:35 -05:00
|
|
|
|
return (rc);
|
2014-10-01 19:58:11 -04:00
|
|
|
|
|
2021-02-21 10:19:43 -06:00
|
|
|
|
out8:
|
|
|
|
|
|
spl_zlib_fini();
|
2009-09-25 14:14:35 -07:00
|
|
|
|
out7:
|
2024-05-07 10:18:22 +10:00
|
|
|
|
spl_kmem_cache_fini();
|
2009-09-25 14:14:35 -07:00
|
|
|
|
out6:
|
2024-05-07 10:18:22 +10:00
|
|
|
|
spl_taskq_fini();
|
2015-12-02 14:52:46 -08:00
|
|
|
|
out5:
|
2024-05-07 10:18:22 +10:00
|
|
|
|
spl_kstat_fini();
|
2009-09-25 14:14:35 -07:00
|
|
|
|
out4:
|
2024-05-07 10:18:22 +10:00
|
|
|
|
spl_proc_fini();
|
2008-05-05 20:18:49 +00:00
|
|
|
|
out3:
|
2019-07-12 14:40:15 -07:00
|
|
|
|
spl_tsd_fini();
|
2008-03-31 20:42:36 +00:00
|
|
|
|
out2:
|
2014-12-08 13:04:42 -05:00
|
|
|
|
spl_kvmem_fini();
|
2009-09-25 14:14:35 -07:00
|
|
|
|
out1:
|
2022-09-08 13:28:20 -04:00
|
|
|
|
spl_random_fini();
|
|
|
|
|
|
out0:
|
2015-02-18 15:24:15 -08:00
|
|
|
|
return (rc);
|
2008-02-27 21:56:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-18 15:24:15 -08:00
|
|
|
|
static void __exit
|
2009-10-01 16:06:15 -07:00
|
|
|
|
spl_fini(void)
|
2008-02-27 21:56:51 +00:00
|
|
|
|
{
|
2021-02-21 10:19:43 -06:00
|
|
|
|
spl_zone_fini();
|
2011-11-11 09:03:31 -08:00
|
|
|
|
spl_zlib_fini();
|
2015-12-02 14:52:46 -08:00
|
|
|
|
spl_kmem_cache_fini();
|
2009-01-05 15:08:03 -08:00
|
|
|
|
spl_taskq_fini();
|
2024-05-07 10:18:22 +10:00
|
|
|
|
spl_kstat_fini();
|
|
|
|
|
|
spl_proc_fini();
|
2015-12-02 14:52:46 -08:00
|
|
|
|
spl_tsd_fini();
|
2014-12-08 13:04:42 -05:00
|
|
|
|
spl_kvmem_fini();
|
2020-02-06 18:26:13 +01:00
|
|
|
|
spl_random_fini();
|
2008-02-27 21:56:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module_init(spl_init);
|
|
|
|
|
|
module_exit(spl_fini);
|
|
|
|
|
|
|
2022-03-31 17:20:50 +02:00
|
|
|
|
MODULE_DESCRIPTION("Solaris Porting Layer");
|
|
|
|
|
|
MODULE_AUTHOR(ZFS_META_AUTHOR);
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
|
MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);
|