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