]>
Commit | Line | Data |
---|---|---|
715f6251 BB |
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> |
99639e4a | 28 | #include <sys/systeminfo.h> |
af828292 BB |
29 | #include <sys/vmsystm.h> |
30 | #include <sys/vnode.h> | |
c19c06f3 | 31 | #include <sys/kmem.h> |
9ab1ac14 | 32 | #include <sys/mutex.h> |
e9cb2b4f | 33 | #include <sys/taskq.h> |
8d0f1ee9 | 34 | #include <sys/debug.h> |
57d1b188 | 35 | #include <sys/proc.h> |
04a479f7 | 36 | #include <sys/kstat.h> |
691d2bd7 | 37 | #include <sys/utsname.h> |
f23e92fa | 38 | #include <linux/kmod.h> |
f1b59d26 | 39 | |
57d1b188 BB |
40 | #ifdef DEBUG_SUBSYSTEM |
41 | #undef DEBUG_SUBSYSTEM | |
42 | #endif | |
8d0f1ee9 | 43 | |
57d1b188 | 44 | #define DEBUG_SUBSYSTEM S_GENERIC |
f23e92fa | 45 | |
0cbaeb11 | 46 | char spl_version[16] = "SPL v" SPL_META_VERSION; |
3561541c | 47 | |
937879f1 | 48 | long spl_hostid = 0; |
f23e92fa | 49 | EXPORT_SYMBOL(spl_hostid); |
8d0f1ee9 | 50 | |
99639e4a | 51 | char hw_serial[HW_HOSTID_LEN] = "<none>"; |
937879f1 | 52 | EXPORT_SYMBOL(hw_serial); |
f1b59d26 BB |
53 | |
54 | int p0 = 0; | |
55 | EXPORT_SYMBOL(p0); | |
70eadc19 | 56 | |
d1ff2312 | 57 | #ifndef HAVE_KALLSYMS_LOOKUP_NAME |
96dded38 | 58 | kallsyms_lookup_name_t spl_kallsyms_lookup_name_fn = SYMBOL_POISON; |
d1ff2312 BB |
59 | #endif |
60 | ||
77b1fe8f BB |
61 | int |
62 | highbit(unsigned long i) | |
63 | { | |
64 | register int h = 1; | |
3d061e9d | 65 | ENTRY; |
77b1fe8f BB |
66 | |
67 | if (i == 0) | |
57d1b188 | 68 | RETURN(0); |
77b1fe8f BB |
69 | #if BITS_PER_LONG == 64 |
70 | if (i & 0xffffffff00000000ul) { | |
71 | h += 32; i >>= 32; | |
72 | } | |
73 | #endif | |
74 | if (i & 0xffff0000) { | |
75 | h += 16; i >>= 16; | |
76 | } | |
77 | if (i & 0xff00) { | |
78 | h += 8; i >>= 8; | |
79 | } | |
80 | if (i & 0xf0) { | |
81 | h += 4; i >>= 4; | |
82 | } | |
83 | if (i & 0xc) { | |
84 | h += 2; i >>= 2; | |
85 | } | |
86 | if (i & 0x2) { | |
87 | h += 1; | |
88 | } | |
57d1b188 | 89 | RETURN(h); |
77b1fe8f BB |
90 | } |
91 | EXPORT_SYMBOL(highbit); | |
92 | ||
b61a6e8b | 93 | /* |
550f1705 | 94 | * Implementation of 64 bit division for 32-bit machines. |
b61a6e8b | 95 | */ |
550f1705 BB |
96 | #if BITS_PER_LONG == 32 |
97 | uint64_t __udivdi3(uint64_t dividend, uint64_t divisor) | |
b61a6e8b | 98 | { |
96dded38 | 99 | #if defined(HAVE_DIV64_64) /* 2.6.22 - 2.6.25 API */ |
550f1705 | 100 | return div64_64(dividend, divisor); |
96dded38 BB |
101 | #elif defined(HAVE_DIV64_U64) /* 2.6.26 - 2.6.x API */ |
102 | return div64_u64(dividend, divisor); | |
550f1705 | 103 | #else |
96dded38 | 104 | /* Implementation from 2.6.30 kernel */ |
b61a6e8b BB |
105 | uint32_t high, d; |
106 | ||
107 | high = divisor >> 32; | |
108 | if (high) { | |
109 | unsigned int shift = fls(high); | |
110 | ||
111 | d = divisor >> shift; | |
112 | dividend >>= shift; | |
113 | } else | |
114 | d = divisor; | |
115 | ||
c0517c35 | 116 | return do_div(dividend, d); |
96dded38 | 117 | #endif /* HAVE_DIV64_64, HAVE_DIV64_U64 */ |
550f1705 BB |
118 | } |
119 | EXPORT_SYMBOL(__udivdi3); | |
120 | ||
121 | /* | |
122 | * Implementation of 64 bit modulo for 32-bit machines. | |
123 | */ | |
124 | uint64_t __umoddi3(uint64_t dividend, uint64_t divisor) | |
125 | { | |
126 | return dividend - divisor * (dividend / divisor); | |
b61a6e8b | 127 | } |
550f1705 | 128 | EXPORT_SYMBOL(__umoddi3); |
96dded38 | 129 | #endif /* BITS_PER_LONG */ |
b61a6e8b | 130 | |
b871b8cd BB |
131 | /* NOTE: The strtoxx behavior is solely based on my reading of the Solaris |
132 | * ddi_strtol(9F) man page. I have not verified the behavior of these | |
133 | * functions against their Solaris counterparts. It is possible that I | |
96dded38 | 134 | * may have misinterpreted the man page or the man page is incorrect. |
b871b8cd | 135 | */ |
2ee63a54 BB |
136 | int ddi_strtoul(const char *, char **, int, unsigned long *); |
137 | int ddi_strtol(const char *, char **, int, long *); | |
138 | int ddi_strtoull(const char *, char **, int, unsigned long long *); | |
139 | int ddi_strtoll(const char *, char **, int, long long *); | |
140 | ||
141 | #define define_ddi_strtoux(type, valtype) \ | |
142 | int ddi_strtou##type(const char *str, char **endptr, \ | |
b871b8cd | 143 | int base, valtype *result) \ |
2ee63a54 | 144 | { \ |
b871b8cd BB |
145 | valtype last_value, value = 0; \ |
146 | char *ptr = (char *)str; \ | |
147 | int flag = 1, digit; \ | |
148 | \ | |
149 | if (strlen(ptr) == 0) \ | |
150 | return EINVAL; \ | |
151 | \ | |
152 | /* Auto-detect base based on prefix */ \ | |
153 | if (!base) { \ | |
154 | if (str[0] == '0') { \ | |
155 | if (tolower(str[1])=='x' && isxdigit(str[2])) { \ | |
156 | base = 16; /* hex */ \ | |
157 | ptr += 2; \ | |
158 | } else if (str[1] >= '0' && str[1] < 8) { \ | |
159 | base = 8; /* octal */ \ | |
160 | ptr += 1; \ | |
161 | } else { \ | |
162 | return EINVAL; \ | |
163 | } \ | |
164 | } else { \ | |
165 | base = 10; /* decimal */ \ | |
166 | } \ | |
167 | } \ | |
168 | \ | |
169 | while (1) { \ | |
170 | if (isdigit(*ptr)) \ | |
171 | digit = *ptr - '0'; \ | |
172 | else if (isalpha(*ptr)) \ | |
173 | digit = tolower(*ptr) - 'a' + 10; \ | |
174 | else \ | |
175 | break; \ | |
176 | \ | |
177 | if (digit >= base) \ | |
178 | break; \ | |
2ee63a54 | 179 | \ |
b871b8cd BB |
180 | last_value = value; \ |
181 | value = value * base + digit; \ | |
182 | if (last_value > value) /* Overflow */ \ | |
183 | return ERANGE; \ | |
2ee63a54 | 184 | \ |
b871b8cd BB |
185 | flag = 1; \ |
186 | ptr++; \ | |
2ee63a54 BB |
187 | } \ |
188 | \ | |
b871b8cd BB |
189 | if (flag) \ |
190 | *result = value; \ | |
191 | \ | |
192 | if (endptr) \ | |
193 | *endptr = (char *)(flag ? ptr : str); \ | |
194 | \ | |
195 | return 0; \ | |
2ee63a54 BB |
196 | } \ |
197 | ||
198 | #define define_ddi_strtox(type, valtype) \ | |
199 | int ddi_strto##type(const char *str, char **endptr, \ | |
200 | int base, valtype *result) \ | |
b871b8cd BB |
201 | { \ |
202 | int rc; \ | |
2ee63a54 BB |
203 | \ |
204 | if (*str == '-') { \ | |
b871b8cd BB |
205 | rc = ddi_strtou##type(str + 1, endptr, base, result); \ |
206 | if (!rc) { \ | |
207 | if (*endptr == str + 1) \ | |
208 | *endptr = (char *)str; \ | |
209 | else \ | |
210 | *result = -*result; \ | |
211 | } \ | |
2ee63a54 | 212 | } else { \ |
b871b8cd | 213 | rc = ddi_strtou##type(str, endptr, base, result); \ |
2ee63a54 BB |
214 | } \ |
215 | \ | |
b871b8cd BB |
216 | return rc; \ |
217 | } | |
2ee63a54 BB |
218 | |
219 | define_ddi_strtoux(l, unsigned long) | |
220 | define_ddi_strtox(l, long) | |
221 | define_ddi_strtoux(ll, unsigned long long) | |
222 | define_ddi_strtox(ll, long long) | |
223 | ||
2f5d55aa | 224 | EXPORT_SYMBOL(ddi_strtoul); |
2ee63a54 BB |
225 | EXPORT_SYMBOL(ddi_strtol); |
226 | EXPORT_SYMBOL(ddi_strtoll); | |
227 | EXPORT_SYMBOL(ddi_strtoull); | |
2f5d55aa | 228 | |
691d2bd7 BB |
229 | struct new_utsname *__utsname(void) |
230 | { | |
3d061e9d | 231 | #ifdef HAVE_INIT_UTSNAME |
691d2bd7 | 232 | return init_utsname(); |
3d061e9d BB |
233 | #else |
234 | return &system_utsname; | |
235 | #endif | |
691d2bd7 BB |
236 | } |
237 | EXPORT_SYMBOL(__utsname); | |
238 | ||
8d0f1ee9 | 239 | static int |
57d1b188 | 240 | set_hostid(void) |
8d0f1ee9 | 241 | { |
f23e92fa BB |
242 | char sh_path[] = "/bin/sh"; |
243 | char *argv[] = { sh_path, | |
244 | "-c", | |
57d86234 | 245 | "/usr/bin/hostid >/proc/sys/kernel/spl/hostid", |
f23e92fa BB |
246 | NULL }; |
247 | char *envp[] = { "HOME=/", | |
248 | "TERM=linux", | |
249 | "PATH=/sbin:/usr/sbin:/bin:/usr/bin", | |
250 | NULL }; | |
96dded38 | 251 | int rc; |
8d0f1ee9 | 252 | |
57d1b188 | 253 | /* Doing address resolution in the kernel is tricky and just |
937879f1 | 254 | * not a good idea in general. So to set the proper 'hw_serial' |
57d1b188 BB |
255 | * use the usermodehelper support to ask '/bin/sh' to run |
256 | * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid | |
96dded38 | 257 | * for us to use. It's a horrific solution but it will do for now. |
57d1b188 | 258 | */ |
96dded38 BB |
259 | rc = call_usermodehelper(sh_path, argv, envp, 1); |
260 | if (rc) | |
261 | printk("SPL: Failed user helper '%s %s %s', rc = %d\n", | |
262 | argv[0], argv[1], argv[2], rc); | |
263 | ||
264 | return rc; | |
57d1b188 | 265 | } |
8d0f1ee9 | 266 | |
99639e4a BB |
267 | uint32_t |
268 | zone_get_hostid(void *zone) | |
269 | { | |
270 | unsigned long hostid; | |
271 | ||
272 | /* Only the global zone is supported */ | |
273 | ASSERT(zone == NULL); | |
274 | ||
275 | if (ddi_strtoul(hw_serial, NULL, HW_HOSTID_LEN-1, &hostid) != 0) | |
276 | return HW_INVALID_HOSTID; | |
277 | ||
278 | return (uint32_t)hostid; | |
279 | } | |
280 | EXPORT_SYMBOL(zone_get_hostid); | |
281 | ||
96dded38 | 282 | #ifndef HAVE_KALLSYMS_LOOKUP_NAME |
d1ff2312 BB |
283 | /* |
284 | * Because kallsyms_lookup_name() is no longer exported in the | |
285 | * mainline kernel we are forced to resort to somewhat drastic | |
286 | * measures. This function replaces the functionality by performing | |
287 | * an upcall to user space where /proc/kallsyms is consulted for | |
288 | * the requested address. | |
289 | */ | |
290 | #define GET_KALLSYMS_ADDR_CMD \ | |
291 | "awk '{ if ( $3 == \"kallsyms_lookup_name\") { print $1 } }' " \ | |
292 | "/proc/kallsyms >/proc/sys/kernel/spl/kallsyms_lookup_name" | |
293 | ||
294 | static int | |
295 | set_kallsyms_lookup_name(void) | |
296 | { | |
297 | char sh_path[] = "/bin/sh"; | |
298 | char *argv[] = { sh_path, | |
299 | "-c", | |
300 | GET_KALLSYMS_ADDR_CMD, | |
301 | NULL }; | |
302 | char *envp[] = { "HOME=/", | |
303 | "TERM=linux", | |
304 | "PATH=/sbin:/usr/sbin:/bin:/usr/bin", | |
305 | NULL }; | |
306 | int rc; | |
307 | ||
308 | rc = call_usermodehelper(sh_path, argv, envp, 1); | |
309 | if (rc) | |
96dded38 BB |
310 | printk("SPL: Failed user helper '%s %s %s', rc = %d\n", |
311 | argv[0], argv[1], argv[2], rc); | |
d1ff2312 | 312 | |
96dded38 | 313 | return rc; |
d1ff2312 BB |
314 | } |
315 | #endif | |
316 | ||
57d1b188 BB |
317 | static int __init spl_init(void) |
318 | { | |
319 | int rc = 0; | |
f23e92fa | 320 | |
57d1b188 | 321 | if ((rc = debug_init())) |
18c9eadf | 322 | return rc; |
f23e92fa | 323 | |
2fb9b26a | 324 | if ((rc = spl_kmem_init())) |
57d1b188 | 325 | GOTO(out , rc); |
8d0f1ee9 | 326 | |
9ab1ac14 BB |
327 | if ((rc = spl_mutex_init())) |
328 | GOTO(out2 , rc); | |
329 | ||
e9cb2b4f | 330 | if ((rc = spl_taskq_init())) |
9ab1ac14 | 331 | GOTO(out3, rc); |
8d0f1ee9 | 332 | |
e9cb2b4f | 333 | if ((rc = vn_init())) |
9ab1ac14 | 334 | GOTO(out4, rc); |
af828292 | 335 | |
e9cb2b4f | 336 | if ((rc = proc_init())) |
04a479f7 BB |
337 | GOTO(out5, rc); |
338 | ||
e9cb2b4f BB |
339 | if ((rc = kstat_init())) |
340 | GOTO(out6, rc); | |
341 | ||
57d1b188 | 342 | if ((rc = set_hostid())) |
e9cb2b4f | 343 | GOTO(out7, rc = -EADDRNOTAVAIL); |
f23e92fa | 344 | |
96dded38 | 345 | #ifndef HAVE_KALLSYMS_LOOKUP_NAME |
d1ff2312 BB |
346 | if ((rc = set_kallsyms_lookup_name())) |
347 | GOTO(out7, rc = -EADDRNOTAVAIL); | |
96dded38 BB |
348 | #endif /* HAVE_KALLSYMS_LOOKUP_NAME */ |
349 | ||
350 | if ((rc = spl_kmem_init_kallsyms_lookup())) | |
351 | GOTO(out7, rc); | |
d1ff2312 | 352 | |
0cbaeb11 | 353 | printk("SPL: Loaded Solaris Porting Layer v%s\n", SPL_META_VERSION); |
57d1b188 | 354 | RETURN(rc); |
e9cb2b4f | 355 | out7: |
04a479f7 | 356 | kstat_fini(); |
e9cb2b4f | 357 | out6: |
57d1b188 | 358 | proc_fini(); |
e9cb2b4f | 359 | out5: |
57d1b188 | 360 | vn_fini(); |
e9cb2b4f BB |
361 | out4: |
362 | spl_taskq_fini(); | |
9ab1ac14 BB |
363 | out3: |
364 | spl_mutex_fini(); | |
8d0f1ee9 | 365 | out2: |
2fb9b26a | 366 | spl_kmem_fini(); |
8d0f1ee9 | 367 | out: |
57d1b188 | 368 | debug_fini(); |
8d0f1ee9 | 369 | |
57d1b188 | 370 | printk("SPL: Failed to Load Solaris Porting Layer v%s, " |
0cbaeb11 | 371 | "rc = %d\n", SPL_META_VERSION, rc); |
18c9eadf | 372 | return rc; |
70eadc19 BB |
373 | } |
374 | ||
375 | static void spl_fini(void) | |
376 | { | |
57d1b188 BB |
377 | ENTRY; |
378 | ||
0cbaeb11 | 379 | printk("SPL: Unloaded Solaris Porting Layer v%s\n", SPL_META_VERSION); |
04a479f7 | 380 | kstat_fini(); |
57d1b188 | 381 | proc_fini(); |
af828292 | 382 | vn_fini(); |
e9cb2b4f | 383 | spl_taskq_fini(); |
2fb9b26a BB |
384 | spl_mutex_fini(); |
385 | spl_kmem_fini(); | |
57d1b188 | 386 | debug_fini(); |
70eadc19 BB |
387 | } |
388 | ||
389 | module_init(spl_init); | |
390 | module_exit(spl_fini); | |
391 | ||
392 | MODULE_AUTHOR("Lawrence Livermore National Labs"); | |
393 | MODULE_DESCRIPTION("Solaris Porting Layer"); | |
394 | MODULE_LICENSE("GPL"); |