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