]> git.proxmox.com Git - mirror_spl.git/blob - module/spl/spl-generic.c
Linux VM integration / device special files
[mirror_spl.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/vmsystm.h>
29 #include <sys/vnode.h>
30 #include <sys/kmem.h>
31 #include <sys/mutex.h>
32 #include <sys/taskq.h>
33 #include <sys/debug.h>
34 #include <sys/proc.h>
35 #include <sys/kstat.h>
36 #include <sys/utsname.h>
37 #include <linux/kmod.h>
38
39 #ifdef DEBUG_SUBSYSTEM
40 #undef DEBUG_SUBSYSTEM
41 #endif
42
43 #define DEBUG_SUBSYSTEM S_GENERIC
44
45 char spl_version[16] = "SPL v" VERSION;
46
47 long spl_hostid = 0;
48 EXPORT_SYMBOL(spl_hostid);
49
50 char hw_serial[11] = "<none>";
51 EXPORT_SYMBOL(hw_serial);
52
53 int p0 = 0;
54 EXPORT_SYMBOL(p0);
55
56 int
57 highbit(unsigned long i)
58 {
59 register int h = 1;
60 ENTRY;
61
62 if (i == 0)
63 RETURN(0);
64 #if BITS_PER_LONG == 64
65 if (i & 0xffffffff00000000ul) {
66 h += 32; i >>= 32;
67 }
68 #endif
69 if (i & 0xffff0000) {
70 h += 16; i >>= 16;
71 }
72 if (i & 0xff00) {
73 h += 8; i >>= 8;
74 }
75 if (i & 0xf0) {
76 h += 4; i >>= 4;
77 }
78 if (i & 0xc) {
79 h += 2; i >>= 2;
80 }
81 if (i & 0x2) {
82 h += 1;
83 }
84 RETURN(h);
85 }
86 EXPORT_SYMBOL(highbit);
87
88 /*
89 * Implementation of 64 bit division for 32-bit machines.
90 */
91 #if BITS_PER_LONG == 32
92 uint64_t __udivdi3(uint64_t dividend, uint64_t divisor)
93 {
94 #ifdef HAVE_DIV64_64
95 return div64_64(dividend, divisor);
96 #else
97 /* Taken from a 2.6.24 kernel. */
98 uint32_t high, d;
99
100 high = divisor >> 32;
101 if (high) {
102 unsigned int shift = fls(high);
103
104 d = divisor >> shift;
105 dividend >>= shift;
106 } else
107 d = divisor;
108
109 do_div(dividend, d);
110
111 return dividend;
112 #endif
113 }
114 EXPORT_SYMBOL(__udivdi3);
115
116 /*
117 * Implementation of 64 bit modulo for 32-bit machines.
118 */
119 uint64_t __umoddi3(uint64_t dividend, uint64_t divisor)
120 {
121 return dividend - divisor * (dividend / divisor);
122 }
123 EXPORT_SYMBOL(__umoddi3);
124 #endif
125
126 /* NOTE: The strtoxx behavior is solely based on my reading of the Solaris
127 * ddi_strtol(9F) man page. I have not verified the behavior of these
128 * functions against their Solaris counterparts. It is possible that I
129 * may have misinterpretted the man page or the man page is incorrect.
130 */
131 int ddi_strtoul(const char *, char **, int, unsigned long *);
132 int ddi_strtol(const char *, char **, int, long *);
133 int ddi_strtoull(const char *, char **, int, unsigned long long *);
134 int ddi_strtoll(const char *, char **, int, long long *);
135
136 #define define_ddi_strtoux(type, valtype) \
137 int ddi_strtou##type(const char *str, char **endptr, \
138 int base, valtype *result) \
139 { \
140 valtype last_value, value = 0; \
141 char *ptr = (char *)str; \
142 int flag = 1, digit; \
143 \
144 if (strlen(ptr) == 0) \
145 return EINVAL; \
146 \
147 /* Auto-detect base based on prefix */ \
148 if (!base) { \
149 if (str[0] == '0') { \
150 if (tolower(str[1])=='x' && isxdigit(str[2])) { \
151 base = 16; /* hex */ \
152 ptr += 2; \
153 } else if (str[1] >= '0' && str[1] < 8) { \
154 base = 8; /* octal */ \
155 ptr += 1; \
156 } else { \
157 return EINVAL; \
158 } \
159 } else { \
160 base = 10; /* decimal */ \
161 } \
162 } \
163 \
164 while (1) { \
165 if (isdigit(*ptr)) \
166 digit = *ptr - '0'; \
167 else if (isalpha(*ptr)) \
168 digit = tolower(*ptr) - 'a' + 10; \
169 else \
170 break; \
171 \
172 if (digit >= base) \
173 break; \
174 \
175 last_value = value; \
176 value = value * base + digit; \
177 if (last_value > value) /* Overflow */ \
178 return ERANGE; \
179 \
180 flag = 1; \
181 ptr++; \
182 } \
183 \
184 if (flag) \
185 *result = value; \
186 \
187 if (endptr) \
188 *endptr = (char *)(flag ? ptr : str); \
189 \
190 return 0; \
191 } \
192
193 #define define_ddi_strtox(type, valtype) \
194 int ddi_strto##type(const char *str, char **endptr, \
195 int base, valtype *result) \
196 { \
197 int rc; \
198 \
199 if (*str == '-') { \
200 rc = ddi_strtou##type(str + 1, endptr, base, result); \
201 if (!rc) { \
202 if (*endptr == str + 1) \
203 *endptr = (char *)str; \
204 else \
205 *result = -*result; \
206 } \
207 } else { \
208 rc = ddi_strtou##type(str, endptr, base, result); \
209 } \
210 \
211 return rc; \
212 }
213
214 define_ddi_strtoux(l, unsigned long)
215 define_ddi_strtox(l, long)
216 define_ddi_strtoux(ll, unsigned long long)
217 define_ddi_strtox(ll, long long)
218
219 EXPORT_SYMBOL(ddi_strtoul);
220 EXPORT_SYMBOL(ddi_strtol);
221 EXPORT_SYMBOL(ddi_strtoll);
222 EXPORT_SYMBOL(ddi_strtoull);
223
224 struct new_utsname *__utsname(void)
225 {
226 #ifdef HAVE_INIT_UTSNAME
227 return init_utsname();
228 #else
229 return &system_utsname;
230 #endif
231 }
232 EXPORT_SYMBOL(__utsname);
233
234 static int
235 set_hostid(void)
236 {
237 char sh_path[] = "/bin/sh";
238 char *argv[] = { sh_path,
239 "-c",
240 "/usr/bin/hostid >/proc/sys/kernel/spl/hostid",
241 NULL };
242 char *envp[] = { "HOME=/",
243 "TERM=linux",
244 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
245 NULL };
246
247 /* Doing address resolution in the kernel is tricky and just
248 * not a good idea in general. So to set the proper 'hw_serial'
249 * use the usermodehelper support to ask '/bin/sh' to run
250 * '/usr/bin/hostid' and redirect the result to /proc/sys/spl/hostid
251 * for us to use. It's a horific solution but it will do for now.
252 */
253 return call_usermodehelper(sh_path, argv, envp, 1);
254 }
255
256 static int __init spl_init(void)
257 {
258 int rc = 0;
259
260 if ((rc = debug_init()))
261 return rc;
262
263 if ((rc = spl_kmem_init()))
264 GOTO(out , rc);
265
266 if ((rc = spl_mutex_init()))
267 GOTO(out2 , rc);
268
269 if ((rc = spl_taskq_init()))
270 GOTO(out3, rc);
271
272 if ((rc = vn_init()))
273 GOTO(out4, rc);
274
275 if ((rc = proc_init()))
276 GOTO(out5, rc);
277
278 if ((rc = kstat_init()))
279 GOTO(out6, rc);
280
281 if ((rc = set_hostid()))
282 GOTO(out7, rc = -EADDRNOTAVAIL);
283
284 printk("SPL: Loaded Solaris Porting Layer v%s\n", VERSION);
285 RETURN(rc);
286 out7:
287 kstat_fini();
288 out6:
289 proc_fini();
290 out5:
291 vn_fini();
292 out4:
293 spl_taskq_fini();
294 out3:
295 spl_mutex_fini();
296 out2:
297 spl_kmem_fini();
298 out:
299 debug_fini();
300
301 printk("SPL: Failed to Load Solaris Porting Layer v%s, "
302 "rc = %d\n", VERSION, rc);
303 return rc;
304 }
305
306 static void spl_fini(void)
307 {
308 ENTRY;
309
310 printk("SPL: Unloaded Solaris Porting Layer v%s\n", VERSION);
311 kstat_fini();
312 proc_fini();
313 vn_fini();
314 spl_taskq_fini();
315 spl_mutex_fini();
316 spl_kmem_fini();
317 debug_fini();
318 }
319
320 module_init(spl_init);
321 module_exit(spl_fini);
322
323 MODULE_AUTHOR("Lawrence Livermore National Labs");
324 MODULE_DESCRIPTION("Solaris Porting Layer");
325 MODULE_LICENSE("GPL");