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