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