]> git.proxmox.com Git - mirror_zfs.git/blame - module/spl/spl-generic.c
Initialize the /dev/splatctl device buffer
[mirror_zfs.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
BB
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
BB
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
BB
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>
8d0f1ee9 35#include <sys/debug.h>
57d1b188 36#include <sys/proc.h>
04a479f7 37#include <sys/kstat.h>
691d2bd7 38#include <sys/utsname.h>
d3126abe 39#include <sys/file.h>
f23e92fa 40#include <linux/kmod.h>
ae4c36ad 41#include <linux/proc_compat.h>
f1b59d26 42
57d1b188
BB
43#ifdef DEBUG_SUBSYSTEM
44#undef DEBUG_SUBSYSTEM
45#endif
8d0f1ee9 46
57d1b188 47#define DEBUG_SUBSYSTEM S_GENERIC
f23e92fa 48
0cbaeb11 49char spl_version[16] = "SPL v" SPL_META_VERSION;
1a73940d 50EXPORT_SYMBOL(spl_version);
3561541c 51
937879f1 52long spl_hostid = 0;
f23e92fa 53EXPORT_SYMBOL(spl_hostid);
8d0f1ee9 54
99639e4a 55char hw_serial[HW_HOSTID_LEN] = "<none>";
937879f1 56EXPORT_SYMBOL(hw_serial);
f1b59d26 57
ae4c36ad 58proc_t p0 = { 0 };
f1b59d26 59EXPORT_SYMBOL(p0);
70eadc19 60
d1ff2312 61#ifndef HAVE_KALLSYMS_LOOKUP_NAME
96dded38 62kallsyms_lookup_name_t spl_kallsyms_lookup_name_fn = SYMBOL_POISON;
d1ff2312
BB
63#endif
64
77b1fe8f
BB
65int
66highbit(unsigned long i)
67{
68 register int h = 1;
3d061e9d 69 ENTRY;
77b1fe8f
BB
70
71 if (i == 0)
57d1b188 72 RETURN(0);
77b1fe8f
BB
73#if BITS_PER_LONG == 64
74 if (i & 0xffffffff00000000ul) {
75 h += 32; i >>= 32;
76 }
77#endif
78 if (i & 0xffff0000) {
79 h += 16; i >>= 16;
80 }
81 if (i & 0xff00) {
82 h += 8; i >>= 8;
83 }
84 if (i & 0xf0) {
85 h += 4; i >>= 4;
86 }
87 if (i & 0xc) {
88 h += 2; i >>= 2;
89 }
90 if (i & 0x2) {
91 h += 1;
92 }
57d1b188 93 RETURN(h);
77b1fe8f
BB
94}
95EXPORT_SYMBOL(highbit);
96
b61a6e8b 97/*
550f1705 98 * Implementation of 64 bit division for 32-bit machines.
b61a6e8b 99 */
550f1705 100#if BITS_PER_LONG == 32
1b4ad25e
AZ
101uint64_t
102__udivdi3(uint64_t dividend, uint64_t divisor)
b61a6e8b 103{
96dded38 104#if defined(HAVE_DIV64_64) /* 2.6.22 - 2.6.25 API */
550f1705 105 return div64_64(dividend, divisor);
96dded38
BB
106#elif defined(HAVE_DIV64_U64) /* 2.6.26 - 2.6.x API */
107 return div64_u64(dividend, divisor);
550f1705 108#else
96dded38 109 /* Implementation from 2.6.30 kernel */
b61a6e8b
BB
110 uint32_t high, d;
111
112 high = divisor >> 32;
113 if (high) {
114 unsigned int shift = fls(high);
115
116 d = divisor >> shift;
117 dividend >>= shift;
118 } else
119 d = divisor;
120
db1aa222
BB
121 do_div(dividend, d);
122
123 return dividend;
96dded38 124#endif /* HAVE_DIV64_64, HAVE_DIV64_U64 */
550f1705
BB
125}
126EXPORT_SYMBOL(__udivdi3);
127
128/*
129 * Implementation of 64 bit modulo for 32-bit machines.
130 */
1b4ad25e
AZ
131uint64_t
132__umoddi3(uint64_t dividend, uint64_t divisor)
550f1705 133{
1b4ad25e 134 return (dividend - (divisor * __udivdi3(dividend, divisor)));
b61a6e8b 135}
550f1705 136EXPORT_SYMBOL(__umoddi3);
96dded38 137#endif /* BITS_PER_LONG */
b61a6e8b 138
b871b8cd
BB
139/* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
140 * ddi_strtol(9F) man page. I have not verified the behavior of these
141 * functions against their Solaris counterparts. It is possible that I
96dded38 142 * may have misinterpreted the man page or the man page is incorrect.
b871b8cd 143 */
2ee63a54
BB
144int ddi_strtoul(const char *, char **, int, unsigned long *);
145int ddi_strtol(const char *, char **, int, long *);
146int ddi_strtoull(const char *, char **, int, unsigned long long *);
147int ddi_strtoll(const char *, char **, int, long long *);
148
149#define define_ddi_strtoux(type, valtype) \
150int ddi_strtou##type(const char *str, char **endptr, \
b871b8cd 151 int base, valtype *result) \
2ee63a54 152{ \
b871b8cd
BB
153 valtype last_value, value = 0; \
154 char *ptr = (char *)str; \
155 int flag = 1, digit; \
156 \
157 if (strlen(ptr) == 0) \
158 return EINVAL; \
159 \
160 /* Auto-detect base based on prefix */ \
161 if (!base) { \
162 if (str[0] == '0') { \
163 if (tolower(str[1])=='x' && isxdigit(str[2])) { \
164 base = 16; /* hex */ \
165 ptr += 2; \
166 } else if (str[1] >= '0' && str[1] < 8) { \
167 base = 8; /* octal */ \
168 ptr += 1; \
169 } else { \
170 return EINVAL; \
171 } \
172 } else { \
173 base = 10; /* decimal */ \
174 } \
175 } \
176 \
177 while (1) { \
178 if (isdigit(*ptr)) \
179 digit = *ptr - '0'; \
180 else if (isalpha(*ptr)) \
181 digit = tolower(*ptr) - 'a' + 10; \
182 else \
183 break; \
184 \
185 if (digit >= base) \
186 break; \
2ee63a54 187 \
b871b8cd
BB
188 last_value = value; \
189 value = value * base + digit; \
190 if (last_value > value) /* Overflow */ \
191 return ERANGE; \
2ee63a54 192 \
b871b8cd
BB
193 flag = 1; \
194 ptr++; \
2ee63a54
BB
195 } \
196 \
b871b8cd
BB
197 if (flag) \
198 *result = value; \
199 \
200 if (endptr) \
201 *endptr = (char *)(flag ? ptr : str); \
202 \
203 return 0; \
2ee63a54
BB
204} \
205
206#define define_ddi_strtox(type, valtype) \
207int ddi_strto##type(const char *str, char **endptr, \
208 int base, valtype *result) \
b871b8cd
BB
209{ \
210 int rc; \
2ee63a54
BB
211 \
212 if (*str == '-') { \
b871b8cd
BB
213 rc = ddi_strtou##type(str + 1, endptr, base, result); \
214 if (!rc) { \
215 if (*endptr == str + 1) \
216 *endptr = (char *)str; \
217 else \
218 *result = -*result; \
219 } \
2ee63a54 220 } else { \
b871b8cd 221 rc = ddi_strtou##type(str, endptr, base, result); \
2ee63a54
BB
222 } \
223 \
b871b8cd
BB
224 return rc; \
225}
2ee63a54
BB
226
227define_ddi_strtoux(l, unsigned long)
228define_ddi_strtox(l, long)
229define_ddi_strtoux(ll, unsigned long long)
230define_ddi_strtox(ll, long long)
231
2f5d55aa 232EXPORT_SYMBOL(ddi_strtoul);
2ee63a54
BB
233EXPORT_SYMBOL(ddi_strtol);
234EXPORT_SYMBOL(ddi_strtoll);
235EXPORT_SYMBOL(ddi_strtoull);
2f5d55aa 236
d3126abe
BB
237int
238ddi_copyin(const void *from, void *to, size_t len, int flags)
239{
240 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
241 if (flags & FKIOCTL) {
242 memcpy(to, from, len);
243 return 0;
244 }
245
246 return copyin(from, to, len);
247}
248EXPORT_SYMBOL(ddi_copyin);
249
250int
251ddi_copyout(const void *from, void *to, size_t len, int flags)
252{
253 /* Fake ioctl() issued by kernel, 'from' is a kernel address */
254 if (flags & FKIOCTL) {
255 memcpy(to, from, len);
256 return 0;
257 }
258
259 return copyout(from, to, len);
260}
261EXPORT_SYMBOL(ddi_copyout);
262
e811949a
BB
263#ifndef HAVE_PUT_TASK_STRUCT
264/*
265 * This is only a stub function which should never be used. The SPL should
266 * never be putting away the last reference on a task structure so this will
267 * not be called. However, we still need to define it so the module does not
268 * have undefined symbol at load time. That all said if this impossible
269 * thing does somehow happen SBUG() immediately so we know about it.
270 */
271void
272__put_task_struct(struct task_struct *t)
273{
274 SBUG();
275}
276EXPORT_SYMBOL(__put_task_struct);
277#endif /* HAVE_PUT_TASK_STRUCT */
278
691d2bd7
BB
279struct new_utsname *__utsname(void)
280{
3d061e9d 281#ifdef HAVE_INIT_UTSNAME
691d2bd7 282 return init_utsname();
3d061e9d
BB
283#else
284 return &system_utsname;
285#endif
691d2bd7
BB
286}
287EXPORT_SYMBOL(__utsname);
288
8d0f1ee9 289static int
57d1b188 290set_hostid(void)
8d0f1ee9 291{
f23e92fa
BB
292 char sh_path[] = "/bin/sh";
293 char *argv[] = { sh_path,
294 "-c",
57d86234 295 "/usr/bin/hostid >/proc/sys/kernel/spl/hostid",
f23e92fa
BB
296 NULL };
297 char *envp[] = { "HOME=/",
298 "TERM=linux",
299 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
300 NULL };
96dded38 301 int rc;
8d0f1ee9 302
57d1b188 303 /* Doing address resolution in the kernel is tricky and just
937879f1 304 * not a good idea in general. So to set the proper 'hw_serial'
57d1b188
BB
305 * use the usermodehelper support to ask '/bin/sh' to run
306 * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid
96dded38 307 * for us to use. It's a horrific solution but it will do for now.
57d1b188 308 */
96dded38
BB
309 rc = call_usermodehelper(sh_path, argv, envp, 1);
310 if (rc)
311 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
312 argv[0], argv[1], argv[2], rc);
313
314 return rc;
57d1b188 315}
8d0f1ee9 316
99639e4a
BB
317uint32_t
318zone_get_hostid(void *zone)
319{
320 unsigned long hostid;
321
322 /* Only the global zone is supported */
323 ASSERT(zone == NULL);
324
325 if (ddi_strtoul(hw_serial, NULL, HW_HOSTID_LEN-1, &hostid) != 0)
326 return HW_INVALID_HOSTID;
327
328 return (uint32_t)hostid;
329}
330EXPORT_SYMBOL(zone_get_hostid);
331
96dded38 332#ifndef HAVE_KALLSYMS_LOOKUP_NAME
d1ff2312
BB
333/*
334 * Because kallsyms_lookup_name() is no longer exported in the
335 * mainline kernel we are forced to resort to somewhat drastic
336 * measures. This function replaces the functionality by performing
337 * an upcall to user space where /proc/kallsyms is consulted for
338 * the requested address.
339 */
340#define GET_KALLSYMS_ADDR_CMD \
341 "awk '{ if ( $3 == \"kallsyms_lookup_name\") { print $1 } }' " \
342 "/proc/kallsyms >/proc/sys/kernel/spl/kallsyms_lookup_name"
343
344static int
345set_kallsyms_lookup_name(void)
346{
347 char sh_path[] = "/bin/sh";
348 char *argv[] = { sh_path,
349 "-c",
350 GET_KALLSYMS_ADDR_CMD,
351 NULL };
352 char *envp[] = { "HOME=/",
353 "TERM=linux",
354 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
355 NULL };
356 int rc;
357
358 rc = call_usermodehelper(sh_path, argv, envp, 1);
359 if (rc)
96dded38
BB
360 printk("SPL: Failed user helper '%s %s %s', rc = %d\n",
361 argv[0], argv[1], argv[2], rc);
d1ff2312 362
96dded38 363 return rc;
d1ff2312
BB
364}
365#endif
366
51a727e9
BB
367static int
368__init spl_init(void)
57d1b188
BB
369{
370 int rc = 0;
f23e92fa 371
57d1b188 372 if ((rc = debug_init()))
18c9eadf 373 return rc;
f23e92fa 374
2fb9b26a 375 if ((rc = spl_kmem_init()))
d28db80f 376 GOTO(out1, rc);
8d0f1ee9 377
9ab1ac14 378 if ((rc = spl_mutex_init()))
d28db80f 379 GOTO(out2, rc);
9ab1ac14 380
d28db80f 381 if ((rc = spl_rw_init()))
9ab1ac14 382 GOTO(out3, rc);
8d0f1ee9 383
d28db80f 384 if ((rc = spl_taskq_init()))
9ab1ac14 385 GOTO(out4, rc);
af828292 386
d28db80f 387 if ((rc = vn_init()))
04a479f7
BB
388 GOTO(out5, rc);
389
d28db80f 390 if ((rc = proc_init()))
e9cb2b4f
BB
391 GOTO(out6, rc);
392
d28db80f
BB
393 if ((rc = kstat_init()))
394 GOTO(out7, rc);
395
57d1b188 396 if ((rc = set_hostid()))
d28db80f 397 GOTO(out8, rc = -EADDRNOTAVAIL);
f23e92fa 398
96dded38 399#ifndef HAVE_KALLSYMS_LOOKUP_NAME
d1ff2312 400 if ((rc = set_kallsyms_lookup_name()))
d28db80f 401 GOTO(out8, rc = -EADDRNOTAVAIL);
96dded38
BB
402#endif /* HAVE_KALLSYMS_LOOKUP_NAME */
403
404 if ((rc = spl_kmem_init_kallsyms_lookup()))
d28db80f 405 GOTO(out8, rc);
d1ff2312 406
0cbaeb11 407 printk("SPL: Loaded Solaris Porting Layer v%s\n", SPL_META_VERSION);
57d1b188 408 RETURN(rc);
d28db80f 409out8:
04a479f7 410 kstat_fini();
d28db80f 411out7:
57d1b188 412 proc_fini();
d28db80f 413out6:
57d1b188 414 vn_fini();
d28db80f 415out5:
e9cb2b4f 416 spl_taskq_fini();
d28db80f
BB
417out4:
418 spl_rw_fini();
9ab1ac14
BB
419out3:
420 spl_mutex_fini();
8d0f1ee9 421out2:
2fb9b26a 422 spl_kmem_fini();
d28db80f 423out1:
57d1b188 424 debug_fini();
8d0f1ee9 425
57d1b188 426 printk("SPL: Failed to Load Solaris Porting Layer v%s, "
0cbaeb11 427 "rc = %d\n", SPL_META_VERSION, rc);
18c9eadf 428 return rc;
70eadc19
BB
429}
430
51a727e9
BB
431static void
432spl_fini(void)
70eadc19 433{
57d1b188
BB
434 ENTRY;
435
0cbaeb11 436 printk("SPL: Unloaded Solaris Porting Layer v%s\n", SPL_META_VERSION);
04a479f7 437 kstat_fini();
57d1b188 438 proc_fini();
af828292 439 vn_fini();
e9cb2b4f 440 spl_taskq_fini();
d28db80f 441 spl_rw_fini();
2fb9b26a
BB
442 spl_mutex_fini();
443 spl_kmem_fini();
57d1b188 444 debug_fini();
70eadc19
BB
445}
446
51a727e9
BB
447/* Called when a dependent module is loaded */
448void
449spl_setup(void)
450{
82a358d9
BB
451 int rc;
452
51a727e9
BB
453 /*
454 * At module load time the pwd is set to '/' on a Solaris system.
455 * On a Linux system will be set to whatever directory the caller
456 * was in when executing insmod/modprobe.
457 */
82a358d9
BB
458 rc = vn_set_pwd("/");
459 if (rc)
460 printk("SPL: Warning unable to set pwd to '/': %d\n", rc);
51a727e9
BB
461}
462EXPORT_SYMBOL(spl_setup);
463
464/* Called when a dependent module is unloaded */
465void
466spl_cleanup(void)
467{
468}
469EXPORT_SYMBOL(spl_cleanup);
470
70eadc19
BB
471module_init(spl_init);
472module_exit(spl_fini);
473
474MODULE_AUTHOR("Lawrence Livermore National Labs");
475MODULE_DESCRIPTION("Solaris Porting Layer");
476MODULE_LICENSE("GPL");