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