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