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