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