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