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