]> git.proxmox.com Git - mirror_zfs.git/blob - module/spl/spl-generic.c
Make the SPL kernel messages consistent with ZFS.
[mirror_zfs.git] / module / spl / spl-generic.c
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>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
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.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
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
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Generic Implementation.
25 \*****************************************************************************/
26
27 #include <sys/sysmacros.h>
28 #include <sys/systeminfo.h>
29 #include <sys/vmsystm.h>
30 #include <sys/vnode.h>
31 #include <sys/kmem.h>
32 #include <sys/mutex.h>
33 #include <sys/rwlock.h>
34 #include <sys/taskq.h>
35 #include <sys/tsd.h>
36 #include <sys/zmod.h>
37 #include <sys/debug.h>
38 #include <sys/proc.h>
39 #include <sys/kstat.h>
40 #include <sys/utsname.h>
41 #include <sys/file.h>
42 #include <linux/kmod.h>
43 #include <linux/proc_compat.h>
44 #include <spl-debug.h>
45
46 #ifdef SS_DEBUG_SUBSYS
47 #undef SS_DEBUG_SUBSYS
48 #endif
49
50 #define SS_DEBUG_SUBSYS SS_GENERIC
51
52 char spl_version[16] = "SPL v" SPL_META_VERSION;
53 EXPORT_SYMBOL(spl_version);
54
55 unsigned long spl_hostid = 0;
56 EXPORT_SYMBOL(spl_hostid);
57 module_param(spl_hostid, ulong, 0644);
58 MODULE_PARM_DESC(spl_hostid, "The system hostid.");
59
60 char hw_serial[HW_HOSTID_LEN] = "<none>";
61 EXPORT_SYMBOL(hw_serial);
62
63 proc_t p0 = { 0 };
64 EXPORT_SYMBOL(p0);
65
66 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
67 kallsyms_lookup_name_t spl_kallsyms_lookup_name_fn = SYMBOL_POISON;
68 #endif
69
70 int
71 highbit(unsigned long i)
72 {
73 register int h = 1;
74 SENTRY;
75
76 if (i == 0)
77 SRETURN(0);
78 #if BITS_PER_LONG == 64
79 if (i & 0xffffffff00000000ul) {
80 h += 32; i >>= 32;
81 }
82 #endif
83 if (i & 0xffff0000) {
84 h += 16; i >>= 16;
85 }
86 if (i & 0xff00) {
87 h += 8; i >>= 8;
88 }
89 if (i & 0xf0) {
90 h += 4; i >>= 4;
91 }
92 if (i & 0xc) {
93 h += 2; i >>= 2;
94 }
95 if (i & 0x2) {
96 h += 1;
97 }
98 SRETURN(h);
99 }
100 EXPORT_SYMBOL(highbit);
101
102 #if BITS_PER_LONG == 32
103 /*
104 * Support 64/64 => 64 division on a 32-bit platform. While the kernel
105 * provides a div64_u64() function for this we do not use it because the
106 * implementation is flawed. There are cases which return incorrect
107 * results as late as linux-2.6.35. Until this is fixed upstream the
108 * spl must provide its own implementation.
109 *
110 * This implementation is a slightly modified version of the algorithm
111 * proposed by the book 'Hacker's Delight'. The original source can be
112 * found here and is available for use without restriction.
113 *
114 * http://www.hackersdelight.org/HDcode/newCode/divDouble.c
115 */
116
117 /*
118 * Calculate number of leading of zeros for a 64-bit value.
119 */
120 static int
121 nlz64(uint64_t x) {
122 register int n = 0;
123
124 if (x == 0)
125 return 64;
126
127 if (x <= 0x00000000FFFFFFFFULL) {n = n + 32; x = x << 32;}
128 if (x <= 0x0000FFFFFFFFFFFFULL) {n = n + 16; x = x << 16;}
129 if (x <= 0x00FFFFFFFFFFFFFFULL) {n = n + 8; x = x << 8;}
130 if (x <= 0x0FFFFFFFFFFFFFFFULL) {n = n + 4; x = x << 4;}
131 if (x <= 0x3FFFFFFFFFFFFFFFULL) {n = n + 2; x = x << 2;}
132 if (x <= 0x7FFFFFFFFFFFFFFFULL) {n = n + 1;}
133
134 return n;
135 }
136
137 /*
138 * Newer kernels have a div_u64() function but we define our own
139 * to simplify portibility between kernel versions.
140 */
141 static inline uint64_t
142 __div_u64(uint64_t u, uint32_t v)
143 {
144 (void) do_div(u, v);
145 return u;
146 }
147
148 /*
149 * Implementation of 64-bit unsigned division for 32-bit machines.
150 *
151 * First the procedure takes care of the case in which the divisor is a
152 * 32-bit quantity. There are two subcases: (1) If the left half of the
153 * dividend is less than the divisor, one execution of do_div() is all that
154 * is required (overflow is not possible). (2) Otherwise it does two
155 * divisions, using the grade school method.
156 */
157 uint64_t
158 __udivdi3(uint64_t u, uint64_t v)
159 {
160 uint64_t u0, u1, v1, q0, q1, k;
161 int n;
162
163 if (v >> 32 == 0) { // If v < 2**32:
164 if (u >> 32 < v) { // If u/v cannot overflow,
165 return __div_u64(u, v); // just do one division.
166 } else { // If u/v would overflow:
167 u1 = u >> 32; // Break u into two halves.
168 u0 = u & 0xFFFFFFFF;
169 q1 = __div_u64(u1, v); // First quotient digit.
170 k = u1 - q1 * v; // First remainder, < v.
171 u0 += (k << 32);
172 q0 = __div_u64(u0, v); // Seconds quotient digit.
173 return (q1 << 32) + q0;
174 }
175 } else { // If v >= 2**32:
176 n = nlz64(v); // 0 <= n <= 31.
177 v1 = (v << n) >> 32; // Normalize divisor, MSB is 1.
178 u1 = u >> 1; // To ensure no overflow.
179 q1 = __div_u64(u1, v1); // Get quotient from
180 q0 = (q1 << n) >> 31; // Undo normalization and
181 // division of u by 2.
182 if (q0 != 0) // Make q0 correct or
183 q0 = q0 - 1; // too small by 1.
184 if ((u - q0 * v) >= v)
185 q0 = q0 + 1; // Now q0 is correct.
186
187 return q0;
188 }
189 }
190 EXPORT_SYMBOL(__udivdi3);
191
192 /*
193 * Implementation of 64-bit signed division for 32-bit machines.
194 */
195 int64_t
196 __divdi3(int64_t u, int64_t v)
197 {
198 int64_t q, t;
199 q = __udivdi3(abs64(u), abs64(v));
200 t = (u ^ v) >> 63; // If u, v have different
201 return (q ^ t) - t; // signs, negate q.
202 }
203 EXPORT_SYMBOL(__divdi3);
204
205 /*
206 * Implementation of 64-bit unsigned modulo for 32-bit machines.
207 */
208 uint64_t
209 __umoddi3(uint64_t dividend, uint64_t divisor)
210 {
211 return (dividend - (divisor * __udivdi3(dividend, divisor)));
212 }
213 EXPORT_SYMBOL(__umoddi3);
214
215 #endif /* BITS_PER_LONG */
216
217 /* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
218 * ddi_strtol(9F) man page. I have not verified the behavior of these
219 * functions against their Solaris counterparts. It is possible that I
220 * may have misinterpreted the man page or the man page is incorrect.
221 */
222 int ddi_strtoul(const char *, char **, int, unsigned long *);
223 int ddi_strtol(const char *, char **, int, long *);
224 int ddi_strtoull(const char *, char **, int, unsigned long long *);
225 int ddi_strtoll(const char *, char **, int, long long *);
226
227 #define define_ddi_strtoux(type, valtype) \
228 int ddi_strtou##type(const char *str, char **endptr, \
229 int base, valtype *result) \
230 { \
231 valtype last_value, value = 0; \
232 char *ptr = (char *)str; \
233 int flag = 1, digit; \
234 \
235 if (strlen(ptr) == 0) \
236 return EINVAL; \
237 \
238 /* Auto-detect base based on prefix */ \
239 if (!base) { \
240 if (str[0] == '0') { \
241 if (tolower(str[1])=='x' && isxdigit(str[2])) { \
242 base = 16; /* hex */ \
243 ptr += 2; \
244 } else if (str[1] >= '0' && str[1] < 8) { \
245 base = 8; /* octal */ \
246 ptr += 1; \
247 } else { \
248 return EINVAL; \
249 } \
250 } else { \
251 base = 10; /* decimal */ \
252 } \
253 } \
254 \
255 while (1) { \
256 if (isdigit(*ptr)) \
257 digit = *ptr - '0'; \
258 else if (isalpha(*ptr)) \
259 digit = tolower(*ptr) - 'a' + 10; \
260 else \
261 break; \
262 \
263 if (digit >= base) \
264 break; \
265 \
266 last_value = value; \
267 value = value * base + digit; \
268 if (last_value > value) /* Overflow */ \
269 return ERANGE; \
270 \
271 flag = 1; \
272 ptr++; \
273 } \
274 \
275 if (flag) \
276 *result = value; \
277 \
278 if (endptr) \
279 *endptr = (char *)(flag ? ptr : str); \
280 \
281 return 0; \
282 } \
283
284 #define define_ddi_strtox(type, valtype) \
285 int ddi_strto##type(const char *str, char **endptr, \
286 int base, valtype *result) \
287 { \
288 int rc; \
289 \
290 if (*str == '-') { \
291 rc = ddi_strtou##type(str + 1, endptr, base, result); \
292 if (!rc) { \
293 if (*endptr == str + 1) \
294 *endptr = (char *)str; \
295 else \
296 *result = -*result; \
297 } \
298 } else { \
299 rc = ddi_strtou##type(str, endptr, base, result); \
300 } \
301 \
302 return rc; \
303 }
304
305 define_ddi_strtoux(l, unsigned long)
306 define_ddi_strtox(l, long)
307 define_ddi_strtoux(ll, unsigned long long)
308 define_ddi_strtox(ll, long long)
309
310 EXPORT_SYMBOL(ddi_strtoul);
311 EXPORT_SYMBOL(ddi_strtol);
312 EXPORT_SYMBOL(ddi_strtoll);
313 EXPORT_SYMBOL(ddi_strtoull);
314
315 int
316 ddi_copyin(const void *from, void *to, size_t len, int flags)
317 {
318 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
319 if (flags & FKIOCTL) {
320 memcpy(to, from, len);
321 return 0;
322 }
323
324 return copyin(from, to, len);
325 }
326 EXPORT_SYMBOL(ddi_copyin);
327
328 int
329 ddi_copyout(const void *from, void *to, size_t len, int flags)
330 {
331 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
332 if (flags & FKIOCTL) {
333 memcpy(to, from, len);
334 return 0;
335 }
336
337 return copyout(from, to, len);
338 }
339 EXPORT_SYMBOL(ddi_copyout);
340
341 #ifndef HAVE_PUT_TASK_STRUCT
342 /*
343 * This is only a stub function which should never be used. The SPL should
344 * never be putting away the last reference on a task structure so this will
345 * not be called. However, we still need to define it so the module does not
346 * have undefined symbol at load time. That all said if this impossible
347 * thing does somehow happen PANIC immediately so we know about it.
348 */
349 void
350 __put_task_struct(struct task_struct *t)
351 {
352 PANIC("Unexpectly put last reference on task %d\n", (int)t->pid);
353 }
354 EXPORT_SYMBOL(__put_task_struct);
355 #endif /* HAVE_PUT_TASK_STRUCT */
356
357 struct new_utsname *__utsname(void)
358 {
359 #ifdef HAVE_INIT_UTSNAME
360 return init_utsname();
361 #else
362 return &system_utsname;
363 #endif
364 }
365 EXPORT_SYMBOL(__utsname);
366
367 #define GET_HOSTID_CMD \
368 "exec 0</dev/null " \
369 " 1>/proc/sys/kernel/spl/hostid " \
370 " 2>/dev/null; " \
371 "hostid"
372
373 static int
374 set_hostid(void)
375 {
376 char *argv[] = { "/bin/sh",
377 "-c",
378 GET_HOSTID_CMD,
379 NULL };
380 char *envp[] = { "HOME=/",
381 "TERM=linux",
382 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
383 NULL };
384 int rc;
385
386 /* Doing address resolution in the kernel is tricky and just
387 * not a good idea in general. So to set the proper 'hw_serial'
388 * use the usermodehelper support to ask '/bin/sh' to run
389 * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid
390 * for us to use. It's a horrific solution but it will do for now.
391 */
392 rc = call_usermodehelper(argv[0], argv, envp, 1);
393 if (rc)
394 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
395 argv[0], argv[1], argv[2], rc);
396
397 return rc;
398 }
399
400 uint32_t
401 zone_get_hostid(void *zone)
402 {
403 unsigned long hostid;
404
405 /* Only the global zone is supported */
406 ASSERT(zone == NULL);
407
408 if (ddi_strtoul(hw_serial, NULL, HW_HOSTID_LEN-1, &hostid) != 0)
409 return HW_INVALID_HOSTID;
410
411 return (uint32_t)hostid;
412 }
413 EXPORT_SYMBOL(zone_get_hostid);
414
415 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
416 /*
417 * The kallsyms_lookup_name() kernel function is not an exported symbol in
418 * Linux 2.6.19 through 2.6.32 inclusive.
419 *
420 * This function replaces the functionality by performing an upcall to user
421 * space where /proc/kallsyms is consulted for the requested address.
422 *
423 */
424
425 #define GET_KALLSYMS_ADDR_CMD \
426 "exec 0</dev/null " \
427 " 1>/proc/sys/kernel/spl/kallsyms_lookup_name " \
428 " 2>/dev/null; " \
429 "awk '{ if ( $3 == \"kallsyms_lookup_name\" ) { print $1 } }' " \
430 " /proc/kallsyms "
431
432 static int
433 set_kallsyms_lookup_name(void)
434 {
435 char *argv[] = { "/bin/sh",
436 "-c",
437 GET_KALLSYMS_ADDR_CMD,
438 NULL };
439 char *envp[] = { "HOME=/",
440 "TERM=linux",
441 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
442 NULL };
443 int rc;
444
445 rc = call_usermodehelper(argv[0], argv, envp, 1);
446 if (rc)
447 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
448 argv[0], argv[1], argv[2], rc);
449
450 return rc;
451 }
452 #endif
453
454 static int
455 __init spl_init(void)
456 {
457 int rc = 0;
458
459 if ((rc = debug_init()))
460 return rc;
461
462 if ((rc = spl_kmem_init()))
463 SGOTO(out1, rc);
464
465 if ((rc = spl_mutex_init()))
466 SGOTO(out2, rc);
467
468 if ((rc = spl_rw_init()))
469 SGOTO(out3, rc);
470
471 if ((rc = spl_taskq_init()))
472 SGOTO(out4, rc);
473
474 if ((rc = vn_init()))
475 SGOTO(out5, rc);
476
477 if ((rc = proc_init()))
478 SGOTO(out6, rc);
479
480 if ((rc = kstat_init()))
481 SGOTO(out7, rc);
482
483 if ((rc = tsd_init()))
484 SGOTO(out8, rc);
485
486 if ((rc = zlib_init()))
487 SGOTO(out9, rc);
488
489 /* Get the hostid if it was not passed as a module parameter. */
490 if (spl_hostid == 0 && (rc = set_hostid()))
491 SGOTO(out10, rc = -EADDRNOTAVAIL);
492
493 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
494 if ((rc = set_kallsyms_lookup_name()))
495 SGOTO(out10, rc = -EADDRNOTAVAIL);
496 #endif /* HAVE_KALLSYMS_LOOKUP_NAME */
497
498 if ((rc = spl_kmem_init_kallsyms_lookup()))
499 SGOTO(out10, rc);
500
501 printk(KERN_NOTICE "SPL: Loaded module v%s%s, using hostid 0x%08x\n",
502 SPL_META_VERSION, SPL_DEBUG_STR, (unsigned int) spl_hostid);
503 SRETURN(rc);
504 out10:
505 zlib_fini();
506 out9:
507 tsd_fini();
508 out8:
509 kstat_fini();
510 out7:
511 proc_fini();
512 out6:
513 vn_fini();
514 out5:
515 spl_taskq_fini();
516 out4:
517 spl_rw_fini();
518 out3:
519 spl_mutex_fini();
520 out2:
521 spl_kmem_fini();
522 out1:
523 debug_fini();
524
525 printk(KERN_NOTICE "SPL: Failed to Load Solaris Porting Layer v%s%s"
526 ", rc = %d\n", SPL_META_VERSION, SPL_DEBUG_STR, rc);
527 return rc;
528 }
529
530 static void
531 spl_fini(void)
532 {
533 SENTRY;
534
535 printk(KERN_NOTICE "SPL: Unloaded module v%s%s\n",
536 SPL_META_VERSION, SPL_DEBUG_STR);
537 zlib_fini();
538 tsd_fini();
539 kstat_fini();
540 proc_fini();
541 vn_fini();
542 spl_taskq_fini();
543 spl_rw_fini();
544 spl_mutex_fini();
545 spl_kmem_fini();
546 debug_fini();
547 }
548
549 /* Called when a dependent module is loaded */
550 void
551 spl_setup(void)
552 {
553 int rc;
554
555 /*
556 * At module load time the pwd is set to '/' on a Solaris system.
557 * On a Linux system will be set to whatever directory the caller
558 * was in when executing insmod/modprobe.
559 */
560 rc = vn_set_pwd("/");
561 if (rc)
562 printk("SPL: Warning unable to set pwd to '/': %d\n", rc);
563 }
564 EXPORT_SYMBOL(spl_setup);
565
566 /* Called when a dependent module is unloaded */
567 void
568 spl_cleanup(void)
569 {
570 }
571 EXPORT_SYMBOL(spl_cleanup);
572
573 module_init(spl_init);
574 module_exit(spl_fini);
575
576 MODULE_AUTHOR("Lawrence Livermore National Labs");
577 MODULE_DESCRIPTION("Solaris Porting Layer");
578 MODULE_LICENSE("GPL");