]>
Commit | Line | Data |
---|---|---|
716154c5 BB |
1 | /*****************************************************************************\ |
2 | * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC. | |
3 | * Copyright (C) 2007 The Regents of the University of California. | |
4 | * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). | |
5 | * Written by Brian Behlendorf <behlendorf1@llnl.gov>. | |
715f6251 BB |
6 | * UCRL-CODE-235197 |
7 | * | |
716154c5 | 8 | * This file is part of the SPL, Solaris Porting Layer. |
3d6af2dd | 9 | * For details, see <http://zfsonlinux.org/>. |
716154c5 BB |
10 | * |
11 | * The SPL is free software; you can redistribute it and/or modify it | |
12 | * under the terms of the GNU General Public License as published by the | |
13 | * Free Software Foundation; either version 2 of the License, or (at your | |
14 | * option) any later version. | |
715f6251 | 15 | * |
716154c5 | 16 | * The SPL is distributed in the hope that it will be useful, but WITHOUT |
715f6251 BB |
17 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
18 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
19 | * for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License along | |
716154c5 BB |
22 | * with the SPL. If not, see <http://www.gnu.org/licenses/>. |
23 | ***************************************************************************** | |
24 | * Solaris Porting Layer (SPL) Generic Implementation. | |
25 | \*****************************************************************************/ | |
715f6251 | 26 | |
14c5326c | 27 | #include <sys/sysmacros.h> |
99639e4a | 28 | #include <sys/systeminfo.h> |
af828292 | 29 | #include <sys/vmsystm.h> |
0d54dcb5 | 30 | #include <sys/kobj.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> |
9fe45dc1 | 35 | #include <sys/tsd.h> |
5c1967eb | 36 | #include <sys/zmod.h> |
8d0f1ee9 | 37 | #include <sys/debug.h> |
57d1b188 | 38 | #include <sys/proc.h> |
04a479f7 | 39 | #include <sys/kstat.h> |
691d2bd7 | 40 | #include <sys/utsname.h> |
d3126abe | 41 | #include <sys/file.h> |
f23e92fa | 42 | #include <linux/kmod.h> |
ae4c36ad | 43 | #include <linux/proc_compat.h> |
55abb092 | 44 | #include <spl-debug.h> |
f1b59d26 | 45 | |
b17edc10 BB |
46 | #ifdef SS_DEBUG_SUBSYS |
47 | #undef SS_DEBUG_SUBSYS | |
57d1b188 | 48 | #endif |
8d0f1ee9 | 49 | |
b17edc10 | 50 | #define SS_DEBUG_SUBSYS SS_GENERIC |
f23e92fa | 51 | |
0835057e | 52 | char spl_version[32] = "SPL v" SPL_META_VERSION "-" SPL_META_RELEASE; |
1a73940d | 53 | EXPORT_SYMBOL(spl_version); |
3561541c | 54 | |
acf0ade3 | 55 | unsigned long spl_hostid = 0; |
f23e92fa | 56 | EXPORT_SYMBOL(spl_hostid); |
fa6f7d8f DH |
57 | module_param(spl_hostid, ulong, 0644); |
58 | MODULE_PARM_DESC(spl_hostid, "The system hostid."); | |
8d0f1ee9 | 59 | |
ae4c36ad | 60 | proc_t p0 = { 0 }; |
f1b59d26 | 61 | EXPORT_SYMBOL(p0); |
70eadc19 | 62 | |
d1ff2312 | 63 | #ifndef HAVE_KALLSYMS_LOOKUP_NAME |
034f1b33 | 64 | DECLARE_WAIT_QUEUE_HEAD(spl_kallsyms_lookup_name_waitq); |
96dded38 | 65 | kallsyms_lookup_name_t spl_kallsyms_lookup_name_fn = SYMBOL_POISON; |
d1ff2312 BB |
66 | #endif |
67 | ||
77b1fe8f BB |
68 | int |
69 | highbit(unsigned long i) | |
70 | { | |
71 | register int h = 1; | |
b17edc10 | 72 | SENTRY; |
77b1fe8f BB |
73 | |
74 | if (i == 0) | |
b17edc10 | 75 | SRETURN(0); |
77b1fe8f BB |
76 | #if BITS_PER_LONG == 64 |
77 | if (i & 0xffffffff00000000ul) { | |
78 | h += 32; i >>= 32; | |
79 | } | |
80 | #endif | |
81 | if (i & 0xffff0000) { | |
82 | h += 16; i >>= 16; | |
83 | } | |
84 | if (i & 0xff00) { | |
85 | h += 8; i >>= 8; | |
86 | } | |
87 | if (i & 0xf0) { | |
88 | h += 4; i >>= 4; | |
89 | } | |
90 | if (i & 0xc) { | |
91 | h += 2; i >>= 2; | |
92 | } | |
93 | if (i & 0x2) { | |
94 | h += 1; | |
95 | } | |
b17edc10 | 96 | SRETURN(h); |
77b1fe8f BB |
97 | } |
98 | EXPORT_SYMBOL(highbit); | |
99 | ||
7f23e001 TC |
100 | int |
101 | highbit64(uint64_t i) | |
102 | { | |
103 | register int h = 1; | |
104 | SENTRY; | |
105 | ||
106 | if (i == 0) | |
107 | SRETURN(0); | |
108 | if (i & 0xffffffff00000000ull) { | |
109 | h += 32; i >>= 32; | |
110 | } | |
111 | if (i & 0xffff0000) { | |
112 | h += 16; i >>= 16; | |
113 | } | |
114 | if (i & 0xff00) { | |
115 | h += 8; i >>= 8; | |
116 | } | |
117 | if (i & 0xf0) { | |
118 | h += 4; i >>= 4; | |
119 | } | |
120 | if (i & 0xc) { | |
121 | h += 2; i >>= 2; | |
122 | } | |
123 | if (i & 0x2) { | |
124 | h += 1; | |
125 | } | |
126 | SRETURN(h); | |
127 | } | |
128 | EXPORT_SYMBOL(highbit64); | |
129 | ||
a4bfd8ea | 130 | #if BITS_PER_LONG == 32 |
b61a6e8b | 131 | /* |
a4bfd8ea BB |
132 | * Support 64/64 => 64 division on a 32-bit platform. While the kernel |
133 | * provides a div64_u64() function for this we do not use it because the | |
134 | * implementation is flawed. There are cases which return incorrect | |
135 | * results as late as linux-2.6.35. Until this is fixed upstream the | |
136 | * spl must provide its own implementation. | |
137 | * | |
138 | * This implementation is a slightly modified version of the algorithm | |
139 | * proposed by the book 'Hacker's Delight'. The original source can be | |
140 | * found here and is available for use without restriction. | |
141 | * | |
142 | * http://www.hackersdelight.org/HDcode/newCode/divDouble.c | |
143 | */ | |
144 | ||
145 | /* | |
146 | * Calculate number of leading of zeros for a 64-bit value. | |
147 | */ | |
148 | static int | |
149 | nlz64(uint64_t x) { | |
150 | register int n = 0; | |
151 | ||
152 | if (x == 0) | |
153 | return 64; | |
154 | ||
155 | if (x <= 0x00000000FFFFFFFFULL) {n = n + 32; x = x << 32;} | |
156 | if (x <= 0x0000FFFFFFFFFFFFULL) {n = n + 16; x = x << 16;} | |
157 | if (x <= 0x00FFFFFFFFFFFFFFULL) {n = n + 8; x = x << 8;} | |
158 | if (x <= 0x0FFFFFFFFFFFFFFFULL) {n = n + 4; x = x << 4;} | |
159 | if (x <= 0x3FFFFFFFFFFFFFFFULL) {n = n + 2; x = x << 2;} | |
160 | if (x <= 0x7FFFFFFFFFFFFFFFULL) {n = n + 1;} | |
161 | ||
162 | return n; | |
163 | } | |
164 | ||
165 | /* | |
166 | * Newer kernels have a div_u64() function but we define our own | |
167 | * to simplify portibility between kernel versions. | |
168 | */ | |
169 | static inline uint64_t | |
170 | __div_u64(uint64_t u, uint32_t v) | |
171 | { | |
172 | (void) do_div(u, v); | |
173 | return u; | |
174 | } | |
175 | ||
176 | /* | |
177 | * Implementation of 64-bit unsigned division for 32-bit machines. | |
178 | * | |
179 | * First the procedure takes care of the case in which the divisor is a | |
180 | * 32-bit quantity. There are two subcases: (1) If the left half of the | |
181 | * dividend is less than the divisor, one execution of do_div() is all that | |
182 | * is required (overflow is not possible). (2) Otherwise it does two | |
183 | * divisions, using the grade school method. | |
b61a6e8b | 184 | */ |
1b4ad25e | 185 | uint64_t |
a4bfd8ea | 186 | __udivdi3(uint64_t u, uint64_t v) |
b61a6e8b | 187 | { |
a4bfd8ea BB |
188 | uint64_t u0, u1, v1, q0, q1, k; |
189 | int n; | |
190 | ||
191 | if (v >> 32 == 0) { // If v < 2**32: | |
192 | if (u >> 32 < v) { // If u/v cannot overflow, | |
193 | return __div_u64(u, v); // just do one division. | |
194 | } else { // If u/v would overflow: | |
195 | u1 = u >> 32; // Break u into two halves. | |
196 | u0 = u & 0xFFFFFFFF; | |
197 | q1 = __div_u64(u1, v); // First quotient digit. | |
198 | k = u1 - q1 * v; // First remainder, < v. | |
199 | u0 += (k << 32); | |
200 | q0 = __div_u64(u0, v); // Seconds quotient digit. | |
201 | return (q1 << 32) + q0; | |
202 | } | |
203 | } else { // If v >= 2**32: | |
204 | n = nlz64(v); // 0 <= n <= 31. | |
205 | v1 = (v << n) >> 32; // Normalize divisor, MSB is 1. | |
206 | u1 = u >> 1; // To ensure no overflow. | |
207 | q1 = __div_u64(u1, v1); // Get quotient from | |
208 | q0 = (q1 << n) >> 31; // Undo normalization and | |
209 | // division of u by 2. | |
210 | if (q0 != 0) // Make q0 correct or | |
211 | q0 = q0 - 1; // too small by 1. | |
212 | if ((u - q0 * v) >= v) | |
213 | q0 = q0 + 1; // Now q0 is correct. | |
ef6f91ce | 214 | |
a4bfd8ea BB |
215 | return q0; |
216 | } | |
550f1705 BB |
217 | } |
218 | EXPORT_SYMBOL(__udivdi3); | |
219 | ||
220 | /* | |
a4bfd8ea BB |
221 | * Implementation of 64-bit signed division for 32-bit machines. |
222 | */ | |
223 | int64_t | |
224 | __divdi3(int64_t u, int64_t v) | |
225 | { | |
226 | int64_t q, t; | |
227 | q = __udivdi3(abs64(u), abs64(v)); | |
228 | t = (u ^ v) >> 63; // If u, v have different | |
229 | return (q ^ t) - t; // signs, negate q. | |
230 | } | |
231 | EXPORT_SYMBOL(__divdi3); | |
232 | ||
233 | /* | |
234 | * Implementation of 64-bit unsigned modulo for 32-bit machines. | |
550f1705 | 235 | */ |
1b4ad25e AZ |
236 | uint64_t |
237 | __umoddi3(uint64_t dividend, uint64_t divisor) | |
550f1705 | 238 | { |
1b4ad25e | 239 | return (dividend - (divisor * __udivdi3(dividend, divisor))); |
b61a6e8b | 240 | } |
550f1705 | 241 | EXPORT_SYMBOL(__umoddi3); |
a4bfd8ea | 242 | |
ef6f91ce JL |
243 | #if defined(__arm) || defined(__arm__) |
244 | /* | |
93b0dc92 JL |
245 | * Implementation of 64-bit (un)signed division for 32-bit arm machines. |
246 | * | |
247 | * Run-time ABI for the ARM Architecture (page 20). A pair of (unsigned) | |
248 | * long longs is returned in {{r0, r1}, {r2,r3}}, the quotient in {r0, r1}, | |
249 | * and the remainder in {r2, r3}. The return type is specifically left | |
250 | * set to 'void' to ensure the compiler does not overwrite these registers | |
251 | * during the return. All results are in registers as per ABI | |
ef6f91ce | 252 | */ |
93b0dc92 | 253 | void |
ef6f91ce JL |
254 | __aeabi_uldivmod(uint64_t u, uint64_t v) |
255 | { | |
93b0dc92 JL |
256 | uint64_t res; |
257 | uint64_t mod; | |
258 | ||
259 | res = __udivdi3(u, v); | |
260 | mod = __umoddi3(u, v); | |
261 | { | |
262 | register uint32_t r0 asm("r0") = (res & 0xFFFFFFFF); | |
263 | register uint32_t r1 asm("r1") = (res >> 32); | |
264 | register uint32_t r2 asm("r2") = (mod & 0xFFFFFFFF); | |
265 | register uint32_t r3 asm("r3") = (mod >> 32); | |
266 | ||
267 | asm volatile("" | |
268 | : "+r"(r0), "+r"(r1), "+r"(r2),"+r"(r3) /* output */ | |
269 | : "r"(r0), "r"(r1), "r"(r2), "r"(r3)); /* input */ | |
270 | ||
271 | return; /* r0; */ | |
272 | } | |
ef6f91ce JL |
273 | } |
274 | EXPORT_SYMBOL(__aeabi_uldivmod); | |
275 | ||
93b0dc92 | 276 | void |
ef6f91ce JL |
277 | __aeabi_ldivmod(int64_t u, int64_t v) |
278 | { | |
93b0dc92 JL |
279 | int64_t res; |
280 | uint64_t mod; | |
281 | ||
282 | res = __divdi3(u, v); | |
283 | mod = __umoddi3(u, v); | |
284 | { | |
285 | register uint32_t r0 asm("r0") = (res & 0xFFFFFFFF); | |
286 | register uint32_t r1 asm("r1") = (res >> 32); | |
287 | register uint32_t r2 asm("r2") = (mod & 0xFFFFFFFF); | |
288 | register uint32_t r3 asm("r3") = (mod >> 32); | |
289 | ||
290 | asm volatile("" | |
291 | : "+r"(r0), "+r"(r1), "+r"(r2),"+r"(r3) /* output */ | |
292 | : "r"(r0), "r"(r1), "r"(r2), "r"(r3)); /* input */ | |
293 | ||
294 | return; /* r0; */ | |
295 | } | |
ef6f91ce JL |
296 | } |
297 | EXPORT_SYMBOL(__aeabi_ldivmod); | |
298 | #endif /* __arm || __arm__ */ | |
96dded38 | 299 | #endif /* BITS_PER_LONG */ |
b61a6e8b | 300 | |
b871b8cd BB |
301 | /* NOTE: The strtoxx behavior is solely based on my reading of the Solaris |
302 | * ddi_strtol(9F) man page. I have not verified the behavior of these | |
303 | * functions against their Solaris counterparts. It is possible that I | |
96dded38 | 304 | * may have misinterpreted the man page or the man page is incorrect. |
b871b8cd | 305 | */ |
2ee63a54 BB |
306 | int ddi_strtoul(const char *, char **, int, unsigned long *); |
307 | int ddi_strtol(const char *, char **, int, long *); | |
308 | int ddi_strtoull(const char *, char **, int, unsigned long long *); | |
309 | int ddi_strtoll(const char *, char **, int, long long *); | |
310 | ||
311 | #define define_ddi_strtoux(type, valtype) \ | |
312 | int ddi_strtou##type(const char *str, char **endptr, \ | |
b871b8cd | 313 | int base, valtype *result) \ |
2ee63a54 | 314 | { \ |
b871b8cd BB |
315 | valtype last_value, value = 0; \ |
316 | char *ptr = (char *)str; \ | |
317 | int flag = 1, digit; \ | |
318 | \ | |
319 | if (strlen(ptr) == 0) \ | |
320 | return EINVAL; \ | |
321 | \ | |
322 | /* Auto-detect base based on prefix */ \ | |
323 | if (!base) { \ | |
324 | if (str[0] == '0') { \ | |
325 | if (tolower(str[1])=='x' && isxdigit(str[2])) { \ | |
326 | base = 16; /* hex */ \ | |
327 | ptr += 2; \ | |
328 | } else if (str[1] >= '0' && str[1] < 8) { \ | |
329 | base = 8; /* octal */ \ | |
330 | ptr += 1; \ | |
331 | } else { \ | |
332 | return EINVAL; \ | |
333 | } \ | |
334 | } else { \ | |
335 | base = 10; /* decimal */ \ | |
336 | } \ | |
337 | } \ | |
338 | \ | |
339 | while (1) { \ | |
340 | if (isdigit(*ptr)) \ | |
341 | digit = *ptr - '0'; \ | |
342 | else if (isalpha(*ptr)) \ | |
343 | digit = tolower(*ptr) - 'a' + 10; \ | |
344 | else \ | |
345 | break; \ | |
346 | \ | |
347 | if (digit >= base) \ | |
348 | break; \ | |
2ee63a54 | 349 | \ |
b871b8cd BB |
350 | last_value = value; \ |
351 | value = value * base + digit; \ | |
352 | if (last_value > value) /* Overflow */ \ | |
353 | return ERANGE; \ | |
2ee63a54 | 354 | \ |
b871b8cd BB |
355 | flag = 1; \ |
356 | ptr++; \ | |
2ee63a54 BB |
357 | } \ |
358 | \ | |
b871b8cd BB |
359 | if (flag) \ |
360 | *result = value; \ | |
361 | \ | |
362 | if (endptr) \ | |
363 | *endptr = (char *)(flag ? ptr : str); \ | |
364 | \ | |
365 | return 0; \ | |
2ee63a54 BB |
366 | } \ |
367 | ||
368 | #define define_ddi_strtox(type, valtype) \ | |
369 | int ddi_strto##type(const char *str, char **endptr, \ | |
370 | int base, valtype *result) \ | |
b871b8cd BB |
371 | { \ |
372 | int rc; \ | |
2ee63a54 BB |
373 | \ |
374 | if (*str == '-') { \ | |
b871b8cd BB |
375 | rc = ddi_strtou##type(str + 1, endptr, base, result); \ |
376 | if (!rc) { \ | |
377 | if (*endptr == str + 1) \ | |
378 | *endptr = (char *)str; \ | |
379 | else \ | |
380 | *result = -*result; \ | |
381 | } \ | |
2ee63a54 | 382 | } else { \ |
b871b8cd | 383 | rc = ddi_strtou##type(str, endptr, base, result); \ |
2ee63a54 BB |
384 | } \ |
385 | \ | |
b871b8cd BB |
386 | return rc; \ |
387 | } | |
2ee63a54 BB |
388 | |
389 | define_ddi_strtoux(l, unsigned long) | |
390 | define_ddi_strtox(l, long) | |
391 | define_ddi_strtoux(ll, unsigned long long) | |
392 | define_ddi_strtox(ll, long long) | |
393 | ||
2f5d55aa | 394 | EXPORT_SYMBOL(ddi_strtoul); |
2ee63a54 BB |
395 | EXPORT_SYMBOL(ddi_strtol); |
396 | EXPORT_SYMBOL(ddi_strtoll); | |
397 | EXPORT_SYMBOL(ddi_strtoull); | |
2f5d55aa | 398 | |
d3126abe BB |
399 | int |
400 | ddi_copyin(const void *from, void *to, size_t len, int flags) | |
401 | { | |
402 | /* Fake ioctl() issued by kernel, 'from' is a kernel address */ | |
403 | if (flags & FKIOCTL) { | |
404 | memcpy(to, from, len); | |
405 | return 0; | |
406 | } | |
407 | ||
408 | return copyin(from, to, len); | |
409 | } | |
410 | EXPORT_SYMBOL(ddi_copyin); | |
411 | ||
412 | int | |
413 | ddi_copyout(const void *from, void *to, size_t len, int flags) | |
414 | { | |
415 | /* Fake ioctl() issued by kernel, 'from' is a kernel address */ | |
416 | if (flags & FKIOCTL) { | |
417 | memcpy(to, from, len); | |
418 | return 0; | |
419 | } | |
420 | ||
421 | return copyout(from, to, len); | |
422 | } | |
423 | EXPORT_SYMBOL(ddi_copyout); | |
424 | ||
e811949a BB |
425 | #ifndef HAVE_PUT_TASK_STRUCT |
426 | /* | |
427 | * This is only a stub function which should never be used. The SPL should | |
428 | * never be putting away the last reference on a task structure so this will | |
429 | * not be called. However, we still need to define it so the module does not | |
430 | * have undefined symbol at load time. That all said if this impossible | |
55abb092 | 431 | * thing does somehow happen PANIC immediately so we know about it. |
e811949a BB |
432 | */ |
433 | void | |
434 | __put_task_struct(struct task_struct *t) | |
435 | { | |
55abb092 | 436 | PANIC("Unexpectly put last reference on task %d\n", (int)t->pid); |
e811949a BB |
437 | } |
438 | EXPORT_SYMBOL(__put_task_struct); | |
439 | #endif /* HAVE_PUT_TASK_STRUCT */ | |
440 | ||
691d2bd7 BB |
441 | struct new_utsname *__utsname(void) |
442 | { | |
3d061e9d | 443 | #ifdef HAVE_INIT_UTSNAME |
691d2bd7 | 444 | return init_utsname(); |
3d061e9d BB |
445 | #else |
446 | return &system_utsname; | |
447 | #endif | |
691d2bd7 BB |
448 | } |
449 | EXPORT_SYMBOL(__utsname); | |
450 | ||
0d54dcb5 DH |
451 | |
452 | /* | |
453 | * Read the unique system identifier from the /etc/hostid file. | |
454 | * | |
455 | * The behavior of /usr/bin/hostid on Linux systems with the | |
456 | * regular eglibc and coreutils is: | |
457 | * | |
458 | * 1. Generate the value if the /etc/hostid file does not exist | |
459 | * or if the /etc/hostid file is less than four bytes in size. | |
460 | * | |
461 | * 2. If the /etc/hostid file is at least 4 bytes, then return | |
462 | * the first four bytes [0..3] in native endian order. | |
463 | * | |
464 | * 3. Always ignore bytes [4..] if they exist in the file. | |
465 | * | |
466 | * Only the first four bytes are significant, even on systems that | |
467 | * have a 64-bit word size. | |
468 | * | |
469 | * See: | |
470 | * | |
471 | * eglibc: sysdeps/unix/sysv/linux/gethostid.c | |
472 | * coreutils: src/hostid.c | |
473 | * | |
474 | * Notes: | |
475 | * | |
476 | * The /etc/hostid file on Solaris is a text file that often reads: | |
477 | * | |
478 | * # DO NOT EDIT | |
479 | * "0123456789" | |
480 | * | |
481 | * Directly copying this file to Linux results in a constant | |
482 | * hostid of 4f442023 because the default comment constitutes | |
483 | * the first four bytes of the file. | |
484 | * | |
485 | */ | |
486 | ||
487 | char *spl_hostid_path = HW_HOSTID_PATH; | |
488 | module_param(spl_hostid_path, charp, 0444); | |
489 | MODULE_PARM_DESC(spl_hostid_path, "The system hostid file (/etc/hostid)"); | |
490 | ||
491 | static int | |
492 | hostid_read(void) | |
493 | { | |
494 | int result; | |
495 | uint64_t size; | |
496 | struct _buf *file; | |
acf0ade3 | 497 | uint32_t hostid = 0; |
0d54dcb5 DH |
498 | |
499 | file = kobj_open_file(spl_hostid_path); | |
500 | ||
6b3b569d | 501 | if (file == (struct _buf *)-1) |
0d54dcb5 | 502 | return -1; |
0d54dcb5 DH |
503 | |
504 | result = kobj_get_filesize(file, &size); | |
505 | ||
506 | if (result != 0) { | |
507 | printk(KERN_WARNING | |
508 | "SPL: kobj_get_filesize returned %i on %s\n", | |
509 | result, spl_hostid_path); | |
510 | kobj_close_file(file); | |
511 | return -2; | |
512 | } | |
513 | ||
514 | if (size < sizeof(HW_HOSTID_MASK)) { | |
515 | printk(KERN_WARNING | |
516 | "SPL: Ignoring the %s file because it is %llu bytes; " | |
e8267acd BB |
517 | "expecting %lu bytes instead.\n", spl_hostid_path, |
518 | size, (unsigned long)sizeof(HW_HOSTID_MASK)); | |
0d54dcb5 DH |
519 | kobj_close_file(file); |
520 | return -3; | |
521 | } | |
522 | ||
523 | /* Read directly into the variable like eglibc does. */ | |
524 | /* Short reads are okay; native behavior is preserved. */ | |
525 | result = kobj_read_file(file, (char *)&hostid, sizeof(hostid), 0); | |
526 | ||
527 | if (result < 0) { | |
528 | printk(KERN_WARNING | |
529 | "SPL: kobj_read_file returned %i on %s\n", | |
530 | result, spl_hostid_path); | |
531 | kobj_close_file(file); | |
532 | return -4; | |
533 | } | |
534 | ||
535 | /* Mask down to 32 bits like coreutils does. */ | |
536 | spl_hostid = hostid & HW_HOSTID_MASK; | |
537 | kobj_close_file(file); | |
538 | return 0; | |
539 | } | |
540 | ||
99639e4a BB |
541 | uint32_t |
542 | zone_get_hostid(void *zone) | |
543 | { | |
a9f2397e | 544 | static int first = 1; |
99639e4a BB |
545 | |
546 | /* Only the global zone is supported */ | |
547 | ASSERT(zone == NULL); | |
548 | ||
a9f2397e ED |
549 | if (first) { |
550 | first = 0; | |
551 | ||
552 | /* | |
553 | * Get the hostid if it was not passed as a module parameter. | |
acf0ade3 | 554 | * Try reading the /etc/hostid file directly. |
a9f2397e | 555 | */ |
acf0ade3 RY |
556 | if (hostid_read()) |
557 | spl_hostid = 0; | |
a9f2397e ED |
558 | |
559 | printk(KERN_NOTICE "SPL: using hostid 0x%08x\n", | |
560 | (unsigned int) spl_hostid); | |
561 | } | |
562 | ||
acf0ade3 | 563 | return spl_hostid; |
99639e4a BB |
564 | } |
565 | EXPORT_SYMBOL(zone_get_hostid); | |
566 | ||
96dded38 | 567 | #ifndef HAVE_KALLSYMS_LOOKUP_NAME |
d1ff2312 | 568 | /* |
ad35b6a6 DH |
569 | * The kallsyms_lookup_name() kernel function is not an exported symbol in |
570 | * Linux 2.6.19 through 2.6.32 inclusive. | |
571 | * | |
572 | * This function replaces the functionality by performing an upcall to user | |
573 | * space where /proc/kallsyms is consulted for the requested address. | |
574 | * | |
d1ff2312 | 575 | */ |
ad35b6a6 DH |
576 | |
577 | #define GET_KALLSYMS_ADDR_CMD \ | |
578 | "exec 0</dev/null " \ | |
579 | " 1>/proc/sys/kernel/spl/kallsyms_lookup_name " \ | |
580 | " 2>/dev/null; " \ | |
581 | "awk '{ if ( $3 == \"kallsyms_lookup_name\" ) { print $1 } }' " \ | |
582 | " /proc/kallsyms " | |
d1ff2312 BB |
583 | |
584 | static int | |
585 | set_kallsyms_lookup_name(void) | |
586 | { | |
ad35b6a6 | 587 | char *argv[] = { "/bin/sh", |
d1ff2312 BB |
588 | "-c", |
589 | GET_KALLSYMS_ADDR_CMD, | |
590 | NULL }; | |
591 | char *envp[] = { "HOME=/", | |
592 | "TERM=linux", | |
593 | "PATH=/sbin:/usr/sbin:/bin:/usr/bin", | |
594 | NULL }; | |
595 | int rc; | |
596 | ||
8842263b | 597 | rc = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC); |
034f1b33 BB |
598 | |
599 | /* | |
600 | * Due to I/O buffering the helper may return successfully before | |
601 | * the proc handler has a chance to execute. To catch this case | |
602 | * wait up to 1 second to verify spl_kallsyms_lookup_name_fn was | |
603 | * updated to a non SYMBOL_POISON value. | |
604 | */ | |
605 | if (rc == 0) { | |
606 | rc = wait_event_timeout(spl_kallsyms_lookup_name_waitq, | |
607 | spl_kallsyms_lookup_name_fn != SYMBOL_POISON, HZ); | |
608 | if (rc == 0) | |
609 | rc = -ETIMEDOUT; | |
610 | else if (spl_kallsyms_lookup_name_fn == SYMBOL_POISON) | |
611 | rc = -EFAULT; | |
612 | else | |
613 | rc = 0; | |
614 | } | |
615 | ||
d1ff2312 | 616 | if (rc) |
96dded38 BB |
617 | printk("SPL: Failed user helper '%s %s %s', rc = %d\n", |
618 | argv[0], argv[1], argv[2], rc); | |
d1ff2312 | 619 | |
96dded38 | 620 | return rc; |
d1ff2312 BB |
621 | } |
622 | #endif | |
623 | ||
51a727e9 BB |
624 | static int |
625 | __init spl_init(void) | |
57d1b188 BB |
626 | { |
627 | int rc = 0; | |
f23e92fa | 628 | |
1114ae6a | 629 | if ((rc = spl_debug_init())) |
18c9eadf | 630 | return rc; |
f23e92fa | 631 | |
2fb9b26a | 632 | if ((rc = spl_kmem_init())) |
b17edc10 | 633 | SGOTO(out1, rc); |
8d0f1ee9 | 634 | |
9ab1ac14 | 635 | if ((rc = spl_mutex_init())) |
b17edc10 | 636 | SGOTO(out2, rc); |
9ab1ac14 | 637 | |
d28db80f | 638 | if ((rc = spl_rw_init())) |
b17edc10 | 639 | SGOTO(out3, rc); |
8d0f1ee9 | 640 | |
d28db80f | 641 | if ((rc = spl_taskq_init())) |
b17edc10 | 642 | SGOTO(out4, rc); |
af828292 | 643 | |
12ff95ff | 644 | if ((rc = spl_vn_init())) |
b17edc10 | 645 | SGOTO(out5, rc); |
04a479f7 | 646 | |
1114ae6a | 647 | if ((rc = spl_proc_init())) |
b17edc10 | 648 | SGOTO(out6, rc); |
e9cb2b4f | 649 | |
1114ae6a | 650 | if ((rc = spl_kstat_init())) |
b17edc10 | 651 | SGOTO(out7, rc); |
d28db80f | 652 | |
1114ae6a | 653 | if ((rc = spl_tsd_init())) |
9fe45dc1 BB |
654 | SGOTO(out8, rc); |
655 | ||
1114ae6a | 656 | if ((rc = spl_zlib_init())) |
5c1967eb BB |
657 | SGOTO(out9, rc); |
658 | ||
96dded38 | 659 | #ifndef HAVE_KALLSYMS_LOOKUP_NAME |
d1ff2312 | 660 | if ((rc = set_kallsyms_lookup_name())) |
5c1967eb | 661 | SGOTO(out10, rc = -EADDRNOTAVAIL); |
96dded38 BB |
662 | #endif /* HAVE_KALLSYMS_LOOKUP_NAME */ |
663 | ||
664 | if ((rc = spl_kmem_init_kallsyms_lookup())) | |
5c1967eb | 665 | SGOTO(out10, rc); |
d1ff2312 | 666 | |
a9f2397e ED |
667 | printk(KERN_NOTICE "SPL: Loaded module v%s-%s%s\n", SPL_META_VERSION, |
668 | SPL_META_RELEASE, SPL_DEBUG_STR); | |
b17edc10 | 669 | SRETURN(rc); |
5c1967eb | 670 | out10: |
1114ae6a | 671 | spl_zlib_fini(); |
9fe45dc1 | 672 | out9: |
1114ae6a | 673 | spl_tsd_fini(); |
d28db80f | 674 | out8: |
1114ae6a | 675 | spl_kstat_fini(); |
d28db80f | 676 | out7: |
1114ae6a | 677 | spl_proc_fini(); |
d28db80f | 678 | out6: |
12ff95ff | 679 | spl_vn_fini(); |
d28db80f | 680 | out5: |
e9cb2b4f | 681 | spl_taskq_fini(); |
d28db80f BB |
682 | out4: |
683 | spl_rw_fini(); | |
9ab1ac14 BB |
684 | out3: |
685 | spl_mutex_fini(); | |
8d0f1ee9 | 686 | out2: |
2fb9b26a | 687 | spl_kmem_fini(); |
d28db80f | 688 | out1: |
1114ae6a | 689 | spl_debug_fini(); |
8d0f1ee9 | 690 | |
0835057e BB |
691 | printk(KERN_NOTICE "SPL: Failed to Load Solaris Porting Layer " |
692 | "v%s-%s%s, rc = %d\n", SPL_META_VERSION, SPL_META_RELEASE, | |
693 | SPL_DEBUG_STR, rc); | |
18c9eadf | 694 | return rc; |
70eadc19 BB |
695 | } |
696 | ||
51a727e9 BB |
697 | static void |
698 | spl_fini(void) | |
70eadc19 | 699 | { |
b17edc10 | 700 | SENTRY; |
57d1b188 | 701 | |
0835057e BB |
702 | printk(KERN_NOTICE "SPL: Unloaded module v%s-%s%s\n", |
703 | SPL_META_VERSION, SPL_META_RELEASE, SPL_DEBUG_STR); | |
1114ae6a BB |
704 | spl_zlib_fini(); |
705 | spl_tsd_fini(); | |
706 | spl_kstat_fini(); | |
707 | spl_proc_fini(); | |
12ff95ff | 708 | spl_vn_fini(); |
e9cb2b4f | 709 | spl_taskq_fini(); |
d28db80f | 710 | spl_rw_fini(); |
2fb9b26a BB |
711 | spl_mutex_fini(); |
712 | spl_kmem_fini(); | |
1114ae6a | 713 | spl_debug_fini(); |
70eadc19 BB |
714 | } |
715 | ||
51a727e9 BB |
716 | /* Called when a dependent module is loaded */ |
717 | void | |
718 | spl_setup(void) | |
719 | { | |
82a358d9 BB |
720 | int rc; |
721 | ||
51a727e9 BB |
722 | /* |
723 | * At module load time the pwd is set to '/' on a Solaris system. | |
724 | * On a Linux system will be set to whatever directory the caller | |
725 | * was in when executing insmod/modprobe. | |
726 | */ | |
82a358d9 BB |
727 | rc = vn_set_pwd("/"); |
728 | if (rc) | |
729 | printk("SPL: Warning unable to set pwd to '/': %d\n", rc); | |
51a727e9 BB |
730 | } |
731 | EXPORT_SYMBOL(spl_setup); | |
732 | ||
733 | /* Called when a dependent module is unloaded */ | |
734 | void | |
735 | spl_cleanup(void) | |
736 | { | |
737 | } | |
738 | EXPORT_SYMBOL(spl_cleanup); | |
739 | ||
70eadc19 BB |
740 | module_init(spl_init); |
741 | module_exit(spl_fini); | |
742 | ||
70eadc19 | 743 | MODULE_DESCRIPTION("Solaris Porting Layer"); |
62032954 BB |
744 | MODULE_AUTHOR(SPL_META_AUTHOR); |
745 | MODULE_LICENSE(SPL_META_LICENSE); | |
921a35ad | 746 | MODULE_VERSION(SPL_META_VERSION "-" SPL_META_RELEASE); |