]> git.proxmox.com Git - mirror_spl-debian.git/blame - module/spl/spl-generic.c
Use Linux flock struct
[mirror_spl-debian.git] / module / spl / spl-generic.c
CommitLineData
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
BB
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.
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>
30#include <sys/vnode.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>
8d0f1ee9 36#include <sys/debug.h>
57d1b188 37#include <sys/proc.h>
04a479f7 38#include <sys/kstat.h>
691d2bd7 39#include <sys/utsname.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
0cbaeb11 51char spl_version[16] = "SPL v" SPL_META_VERSION;
1a73940d 52EXPORT_SYMBOL(spl_version);
3561541c 53
937879f1 54long spl_hostid = 0;
f23e92fa 55EXPORT_SYMBOL(spl_hostid);
8d0f1ee9 56
99639e4a 57char hw_serial[HW_HOSTID_LEN] = "<none>";
937879f1 58EXPORT_SYMBOL(hw_serial);
f1b59d26 59
ae4c36ad 60proc_t p0 = { 0 };
f1b59d26 61EXPORT_SYMBOL(p0);
70eadc19 62
d1ff2312 63#ifndef HAVE_KALLSYMS_LOOKUP_NAME
96dded38 64kallsyms_lookup_name_t spl_kallsyms_lookup_name_fn = SYMBOL_POISON;
d1ff2312
BB
65#endif
66
77b1fe8f 67int
68highbit(unsigned long i)
69{
70 register int h = 1;
b17edc10 71 SENTRY;
77b1fe8f 72
73 if (i == 0)
b17edc10 74 SRETURN(0);
77b1fe8f 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 }
b17edc10 95 SRETURN(h);
77b1fe8f 96}
97EXPORT_SYMBOL(highbit);
98
a4bfd8ea 99#if BITS_PER_LONG == 32
b61a6e8b 100/*
a4bfd8ea
BB
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 */
117static int
118nlz64(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 */
138static 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.
b61a6e8b 153 */
1b4ad25e 154uint64_t
a4bfd8ea 155__udivdi3(uint64_t u, uint64_t v)
b61a6e8b 156{
a4bfd8ea
BB
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 }
550f1705 186}
187EXPORT_SYMBOL(__udivdi3);
188
189/*
a4bfd8ea
BB
190 * Implementation of 64-bit signed division for 32-bit machines.
191 */
192int64_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}
200EXPORT_SYMBOL(__divdi3);
201
202/*
203 * Implementation of 64-bit unsigned modulo for 32-bit machines.
550f1705 204 */
1b4ad25e
AZ
205uint64_t
206__umoddi3(uint64_t dividend, uint64_t divisor)
550f1705 207{
1b4ad25e 208 return (dividend - (divisor * __udivdi3(dividend, divisor)));
b61a6e8b 209}
550f1705 210EXPORT_SYMBOL(__umoddi3);
a4bfd8ea 211
96dded38 212#endif /* BITS_PER_LONG */
b61a6e8b 213
b871b8cd
BB
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
96dded38 217 * may have misinterpreted the man page or the man page is incorrect.
b871b8cd 218 */
2ee63a54
BB
219int ddi_strtoul(const char *, char **, int, unsigned long *);
220int ddi_strtol(const char *, char **, int, long *);
221int ddi_strtoull(const char *, char **, int, unsigned long long *);
222int ddi_strtoll(const char *, char **, int, long long *);
223
224#define define_ddi_strtoux(type, valtype) \
225int ddi_strtou##type(const char *str, char **endptr, \
b871b8cd 226 int base, valtype *result) \
2ee63a54 227{ \
b871b8cd
BB
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; \
2ee63a54 262 \
b871b8cd
BB
263 last_value = value; \
264 value = value * base + digit; \
265 if (last_value > value) /* Overflow */ \
266 return ERANGE; \
2ee63a54 267 \
b871b8cd
BB
268 flag = 1; \
269 ptr++; \
2ee63a54
BB
270 } \
271 \
b871b8cd
BB
272 if (flag) \
273 *result = value; \
274 \
275 if (endptr) \
276 *endptr = (char *)(flag ? ptr : str); \
277 \
278 return 0; \
2ee63a54
BB
279} \
280
281#define define_ddi_strtox(type, valtype) \
282int ddi_strto##type(const char *str, char **endptr, \
283 int base, valtype *result) \
b871b8cd
BB
284{ \
285 int rc; \
2ee63a54
BB
286 \
287 if (*str == '-') { \
b871b8cd
BB
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 } \
2ee63a54 295 } else { \
b871b8cd 296 rc = ddi_strtou##type(str, endptr, base, result); \
2ee63a54
BB
297 } \
298 \
b871b8cd
BB
299 return rc; \
300}
2ee63a54
BB
301
302define_ddi_strtoux(l, unsigned long)
303define_ddi_strtox(l, long)
304define_ddi_strtoux(ll, unsigned long long)
305define_ddi_strtox(ll, long long)
306
2f5d55aa 307EXPORT_SYMBOL(ddi_strtoul);
2ee63a54
BB
308EXPORT_SYMBOL(ddi_strtol);
309EXPORT_SYMBOL(ddi_strtoll);
310EXPORT_SYMBOL(ddi_strtoull);
2f5d55aa 311
d3126abe
BB
312int
313ddi_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}
323EXPORT_SYMBOL(ddi_copyin);
324
325int
326ddi_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}
336EXPORT_SYMBOL(ddi_copyout);
337
e811949a
BB
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
55abb092 344 * thing does somehow happen PANIC immediately so we know about it.
e811949a
BB
345 */
346void
347__put_task_struct(struct task_struct *t)
348{
55abb092 349 PANIC("Unexpectly put last reference on task %d\n", (int)t->pid);
e811949a
BB
350}
351EXPORT_SYMBOL(__put_task_struct);
352#endif /* HAVE_PUT_TASK_STRUCT */
353
691d2bd7 354struct new_utsname *__utsname(void)
355{
3d061e9d 356#ifdef HAVE_INIT_UTSNAME
691d2bd7 357 return init_utsname();
3d061e9d 358#else
359 return &system_utsname;
360#endif
691d2bd7 361}
362EXPORT_SYMBOL(__utsname);
363
8d0f1ee9 364static int
57d1b188 365set_hostid(void)
8d0f1ee9 366{
f23e92fa 367 char sh_path[] = "/bin/sh";
368 char *argv[] = { sh_path,
369 "-c",
57d86234 370 "/usr/bin/hostid >/proc/sys/kernel/spl/hostid",
f23e92fa 371 NULL };
372 char *envp[] = { "HOME=/",
373 "TERM=linux",
374 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
375 NULL };
96dded38 376 int rc;
8d0f1ee9 377
57d1b188 378 /* Doing address resolution in the kernel is tricky and just
937879f1 379 * not a good idea in general. So to set the proper 'hw_serial'
57d1b188 380 * use the usermodehelper support to ask '/bin/sh' to run
381 * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid
96dded38 382 * for us to use. It's a horrific solution but it will do for now.
57d1b188 383 */
96dded38
BB
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;
57d1b188 390}
8d0f1ee9 391
99639e4a
BB
392uint32_t
393zone_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}
405EXPORT_SYMBOL(zone_get_hostid);
406
96dded38 407#ifndef HAVE_KALLSYMS_LOOKUP_NAME
d1ff2312
BB
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 \
18142514 416 "gawk '{ if ( $3 == \"kallsyms_lookup_name\") { print $1 } }' " \
d1ff2312
BB
417 "/proc/kallsyms >/proc/sys/kernel/spl/kallsyms_lookup_name"
418
419static int
420set_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)
96dded38
BB
435 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
436 argv[0], argv[1], argv[2], rc);
d1ff2312 437
96dded38 438 return rc;
d1ff2312
BB
439}
440#endif
441
51a727e9
BB
442static int
443__init spl_init(void)
57d1b188 444{
445 int rc = 0;
f23e92fa 446
57d1b188 447 if ((rc = debug_init()))
18c9eadf 448 return rc;
f23e92fa 449
2fb9b26a 450 if ((rc = spl_kmem_init()))
b17edc10 451 SGOTO(out1, rc);
8d0f1ee9 452
9ab1ac14 453 if ((rc = spl_mutex_init()))
b17edc10 454 SGOTO(out2, rc);
9ab1ac14 455
d28db80f 456 if ((rc = spl_rw_init()))
b17edc10 457 SGOTO(out3, rc);
8d0f1ee9 458
d28db80f 459 if ((rc = spl_taskq_init()))
b17edc10 460 SGOTO(out4, rc);
af828292 461
d28db80f 462 if ((rc = vn_init()))
b17edc10 463 SGOTO(out5, rc);
04a479f7 464
d28db80f 465 if ((rc = proc_init()))
b17edc10 466 SGOTO(out6, rc);
e9cb2b4f 467
d28db80f 468 if ((rc = kstat_init()))
b17edc10 469 SGOTO(out7, rc);
d28db80f 470
9fe45dc1
BB
471 if ((rc = tsd_init()))
472 SGOTO(out8, rc);
473
57d1b188 474 if ((rc = set_hostid()))
9fe45dc1 475 SGOTO(out9, rc = -EADDRNOTAVAIL);
f23e92fa 476
96dded38 477#ifndef HAVE_KALLSYMS_LOOKUP_NAME
d1ff2312 478 if ((rc = set_kallsyms_lookup_name()))
9fe45dc1 479 SGOTO(out9, rc = -EADDRNOTAVAIL);
96dded38
BB
480#endif /* HAVE_KALLSYMS_LOOKUP_NAME */
481
482 if ((rc = spl_kmem_init_kallsyms_lookup()))
9fe45dc1 483 SGOTO(out9, rc);
d1ff2312 484
81672c01
RC
485 printk(KERN_NOTICE "SPL: Loaded Solaris Porting Layer v%s%s\n",
486 SPL_META_VERSION, SPL_DEBUG_STR);
b17edc10 487 SRETURN(rc);
9fe45dc1
BB
488out9:
489 tsd_fini();
d28db80f 490out8:
04a479f7 491 kstat_fini();
d28db80f 492out7:
57d1b188 493 proc_fini();
d28db80f 494out6:
57d1b188 495 vn_fini();
d28db80f 496out5:
e9cb2b4f 497 spl_taskq_fini();
d28db80f
BB
498out4:
499 spl_rw_fini();
9ab1ac14 500out3:
501 spl_mutex_fini();
8d0f1ee9 502out2:
2fb9b26a 503 spl_kmem_fini();
d28db80f 504out1:
57d1b188 505 debug_fini();
8d0f1ee9 506
81672c01
RC
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);
18c9eadf 509 return rc;
70eadc19 510}
511
51a727e9
BB
512static void
513spl_fini(void)
70eadc19 514{
b17edc10 515 SENTRY;
57d1b188 516
81672c01
RC
517 printk(KERN_NOTICE "SPL: Unloaded Solaris Porting Layer v%s%s\n",
518 SPL_META_VERSION, SPL_DEBUG_STR);
9fe45dc1 519 tsd_fini();
04a479f7 520 kstat_fini();
57d1b188 521 proc_fini();
af828292 522 vn_fini();
e9cb2b4f 523 spl_taskq_fini();
d28db80f 524 spl_rw_fini();
2fb9b26a 525 spl_mutex_fini();
526 spl_kmem_fini();
57d1b188 527 debug_fini();
70eadc19 528}
529
51a727e9
BB
530/* Called when a dependent module is loaded */
531void
532spl_setup(void)
533{
82a358d9
BB
534 int rc;
535
51a727e9
BB
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 */
82a358d9
BB
541 rc = vn_set_pwd("/");
542 if (rc)
543 printk("SPL: Warning unable to set pwd to '/': %d\n", rc);
51a727e9
BB
544}
545EXPORT_SYMBOL(spl_setup);
546
547/* Called when a dependent module is unloaded */
548void
549spl_cleanup(void)
550{
551}
552EXPORT_SYMBOL(spl_cleanup);
553
70eadc19 554module_init(spl_init);
555module_exit(spl_fini);
556
557MODULE_AUTHOR("Lawrence Livermore National Labs");
558MODULE_DESCRIPTION("Solaris Porting Layer");
559MODULE_LICENSE("GPL");