]> git.proxmox.com Git - mirror_spl.git/blame - modules/spl/spl-generic.c
Remove u8_textprep, we will not be implementing this nightmare yet
[mirror_spl.git] / modules / spl / spl-generic.c
CommitLineData
715f6251 1/*
2 * This file is part of the SPL: Solaris Porting Layer.
3 *
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
6 * Written by:
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
10 * UCRL-CODE-235197
11 *
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
14c5326c 27#include <sys/sysmacros.h>
af828292 28#include <sys/vmsystm.h>
29#include <sys/vnode.h>
c19c06f3 30#include <sys/kmem.h>
9ab1ac14 31#include <sys/mutex.h>
8d0f1ee9 32#include <sys/debug.h>
57d1b188 33#include <sys/proc.h>
04a479f7 34#include <sys/kstat.h>
691d2bd7 35#include <sys/utsname.h>
f23e92fa 36#include <linux/kmod.h>
f1b59d26 37
57d1b188 38#ifdef DEBUG_SUBSYSTEM
39#undef DEBUG_SUBSYSTEM
40#endif
8d0f1ee9 41
57d1b188 42#define DEBUG_SUBSYSTEM S_GENERIC
f23e92fa 43
3561541c 44char spl_version[16] = "SPL v" VERSION;
45
937879f1 46long spl_hostid = 0;
f23e92fa 47EXPORT_SYMBOL(spl_hostid);
8d0f1ee9 48
937879f1 49char hw_serial[11] = "<none>";
50EXPORT_SYMBOL(hw_serial);
f1b59d26 51
52int p0 = 0;
53EXPORT_SYMBOL(p0);
70eadc19 54
af828292 55vmem_t *zio_alloc_arena = NULL;
56EXPORT_SYMBOL(zio_alloc_arena);
57
77b1fe8f 58int
59highbit(unsigned long i)
60{
61 register int h = 1;
3d061e9d 62 ENTRY;
77b1fe8f 63
64 if (i == 0)
57d1b188 65 RETURN(0);
77b1fe8f 66#if BITS_PER_LONG == 64
67 if (i & 0xffffffff00000000ul) {
68 h += 32; i >>= 32;
69 }
70#endif
71 if (i & 0xffff0000) {
72 h += 16; i >>= 16;
73 }
74 if (i & 0xff00) {
75 h += 8; i >>= 8;
76 }
77 if (i & 0xf0) {
78 h += 4; i >>= 4;
79 }
80 if (i & 0xc) {
81 h += 2; i >>= 2;
82 }
83 if (i & 0x2) {
84 h += 1;
85 }
57d1b188 86 RETURN(h);
77b1fe8f 87}
88EXPORT_SYMBOL(highbit);
89
b61a6e8b 90/*
550f1705 91 * Implementation of 64 bit division for 32-bit machines.
b61a6e8b 92 */
550f1705 93#if BITS_PER_LONG == 32
94uint64_t __udivdi3(uint64_t dividend, uint64_t divisor)
b61a6e8b 95{
550f1705 96#ifdef HAVE_DIV64_64
97 return div64_64(dividend, divisor);
98#else
99 /* Taken from a 2.6.24 kernel. */
b61a6e8b 100 uint32_t high, d;
101
102 high = divisor >> 32;
103 if (high) {
104 unsigned int shift = fls(high);
105
106 d = divisor >> shift;
107 dividend >>= shift;
108 } else
109 d = divisor;
110
111 do_div(dividend, d);
112
113 return dividend;
550f1705 114#endif
115}
116EXPORT_SYMBOL(__udivdi3);
117
118/*
119 * Implementation of 64 bit modulo for 32-bit machines.
120 */
121uint64_t __umoddi3(uint64_t dividend, uint64_t divisor)
122{
123 return dividend - divisor * (dividend / divisor);
b61a6e8b 124}
550f1705 125EXPORT_SYMBOL(__umoddi3);
126#endif
b61a6e8b 127
2ee63a54
BB
128int ddi_strtoul(const char *, char **, int, unsigned long *);
129int ddi_strtol(const char *, char **, int, long *);
130int ddi_strtoull(const char *, char **, int, unsigned long long *);
131int ddi_strtoll(const char *, char **, int, long long *);
132
133#define define_ddi_strtoux(type, valtype) \
134int ddi_strtou##type(const char *str, char **endptr, \
135 int base, valtype *result) \
136{ \
137 valtype val; \
138 size_t len; \
139 \
140 len = strlen(str); \
141 if (len == 0) \
142 return -EINVAL; \
143 \
144 val = simple_strtoul(str, endptr, (unsigned int)base); \
145 if ((**endptr == '\0') || \
146 ((len == (size_t)(*endptr-str) + 1) && (**endptr == '\n'))) {\
147 *result = val; \
148 return 0; \
149 } \
150 \
151 return -EINVAL; \
152} \
153
154#define define_ddi_strtox(type, valtype) \
155int ddi_strto##type(const char *str, char **endptr, \
156 int base, valtype *result) \
157{ \
158 int ret; \
159 \
160 if (*str == '-') { \
161 ret = ddi_strtou##type(str+1, endptr, \
162 (unsigned int)base, result); \
163 if (!ret) \
164 *result = -(*result); \
165 } else { \
166 ret = ddi_strtou##type(str, endptr, \
167 (unsigned int)base, result); \
168 } \
169 \
170 return ret; \
171} \
172
173define_ddi_strtoux(l, unsigned long)
174define_ddi_strtox(l, long)
175define_ddi_strtoux(ll, unsigned long long)
176define_ddi_strtox(ll, long long)
177
2f5d55aa 178EXPORT_SYMBOL(ddi_strtoul);
2ee63a54
BB
179EXPORT_SYMBOL(ddi_strtol);
180EXPORT_SYMBOL(ddi_strtoll);
181EXPORT_SYMBOL(ddi_strtoull);
2f5d55aa 182
691d2bd7 183struct new_utsname *__utsname(void)
184{
3d061e9d 185#ifdef HAVE_INIT_UTSNAME
691d2bd7 186 return init_utsname();
3d061e9d 187#else
188 return &system_utsname;
189#endif
691d2bd7 190}
191EXPORT_SYMBOL(__utsname);
192
8d0f1ee9 193static int
57d1b188 194set_hostid(void)
8d0f1ee9 195{
f23e92fa 196 char sh_path[] = "/bin/sh";
197 char *argv[] = { sh_path,
198 "-c",
57d86234 199 "/usr/bin/hostid >/proc/sys/kernel/spl/hostid",
f23e92fa 200 NULL };
201 char *envp[] = { "HOME=/",
202 "TERM=linux",
203 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
204 NULL };
8d0f1ee9 205
57d1b188 206 /* Doing address resolution in the kernel is tricky and just
937879f1 207 * not a good idea in general. So to set the proper 'hw_serial'
57d1b188 208 * use the usermodehelper support to ask '/bin/sh' to run
209 * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid
210 * for us to use. It's a horific solution but it will do for now.
211 */
212 return call_usermodehelper(sh_path, argv, envp, 1);
213}
8d0f1ee9 214
57d1b188 215static int __init spl_init(void)
216{
217 int rc = 0;
f23e92fa 218
57d1b188 219 if ((rc = debug_init()))
18c9eadf 220 return rc;
f23e92fa 221
2fb9b26a 222 if ((rc = spl_kmem_init()))
57d1b188 223 GOTO(out , rc);
8d0f1ee9 224
9ab1ac14 225 if ((rc = spl_mutex_init()))
226 GOTO(out2 , rc);
227
8d0f1ee9 228 if ((rc = vn_init()))
9ab1ac14 229 GOTO(out3, rc);
8d0f1ee9 230
57d1b188 231 if ((rc = proc_init()))
9ab1ac14 232 GOTO(out4, rc);
af828292 233
04a479f7 234 if ((rc = kstat_init()))
235 GOTO(out5, rc);
236
57d1b188 237 if ((rc = set_hostid()))
04a479f7 238 GOTO(out6, rc = -EADDRNOTAVAIL);
f23e92fa 239
937879f1 240 printk("SPL: Loaded Solaris Porting Layer v%s\n", VERSION);
57d1b188 241 RETURN(rc);
04a479f7 242out6:
243 kstat_fini();
9ab1ac14 244out5:
57d1b188 245 proc_fini();
9ab1ac14 246out4:
57d1b188 247 vn_fini();
9ab1ac14 248out3:
249 spl_mutex_fini();
8d0f1ee9 250out2:
2fb9b26a 251 spl_kmem_fini();
8d0f1ee9 252out:
57d1b188 253 debug_fini();
8d0f1ee9 254
57d1b188 255 printk("SPL: Failed to Load Solaris Porting Layer v%s, "
256 "rc = %d\n", VERSION, rc);
18c9eadf 257 return rc;
70eadc19 258}
259
260static void spl_fini(void)
261{
57d1b188 262 ENTRY;
263
937879f1 264 printk("SPL: Unloaded Solaris Porting Layer v%s\n", VERSION);
04a479f7 265 kstat_fini();
57d1b188 266 proc_fini();
af828292 267 vn_fini();
2fb9b26a 268 spl_mutex_fini();
269 spl_kmem_fini();
57d1b188 270 debug_fini();
70eadc19 271}
272
273module_init(spl_init);
274module_exit(spl_fini);
275
276MODULE_AUTHOR("Lawrence Livermore National Labs");
277MODULE_DESCRIPTION("Solaris Porting Layer");
278MODULE_LICENSE("GPL");