]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | /* | |
22 | * Copyright 2008 Sun Microsystems, Inc. All rights reserved. | |
23 | * Use is subject to license terms. | |
24 | */ | |
25 | ||
26 | ||
27 | ||
28 | #include <assert.h> | |
29 | #include <fcntl.h> | |
30 | #include <poll.h> | |
31 | #include <stdio.h> | |
32 | #include <stdlib.h> | |
33 | #include <string.h> | |
34 | #include <zlib.h> | |
35 | #include <sys/spa.h> | |
36 | #include <sys/stat.h> | |
37 | #include <sys/processor.h> | |
38 | #include <sys/zfs_context.h> | |
39 | #include <sys/zmod.h> | |
40 | #include <sys/utsname.h> | |
41 | ||
42 | /* | |
43 | * Emulation of kernel services in userland. | |
44 | */ | |
45 | ||
46 | uint64_t physmem; | |
47 | vnode_t *rootdir = (vnode_t *)0xabcd1234; | |
48 | char hw_serial[11]; | |
49 | ||
50 | struct utsname utsname = { | |
51 | "userland", "libzpool", "1", "1", "na" | |
52 | }; | |
53 | ||
54 | /* | |
55 | * ========================================================================= | |
56 | * threads | |
57 | * ========================================================================= | |
58 | */ | |
59 | /*ARGSUSED*/ | |
60 | kthread_t * | |
61 | zk_thread_create(void (*func)(), void *arg) | |
62 | { | |
63 | thread_t tid; | |
64 | ||
65 | VERIFY(thr_create(0, 0, (void *(*)(void *))func, arg, THR_DETACHED, | |
66 | &tid) == 0); | |
67 | ||
68 | return ((void *)(uintptr_t)tid); | |
69 | } | |
70 | ||
71 | /* | |
72 | * ========================================================================= | |
73 | * kstats | |
74 | * ========================================================================= | |
75 | */ | |
76 | /*ARGSUSED*/ | |
77 | kstat_t * | |
78 | kstat_create(char *module, int instance, char *name, char *class, | |
79 | uchar_t type, ulong_t ndata, uchar_t ks_flag) | |
80 | { | |
81 | return (NULL); | |
82 | } | |
83 | ||
84 | /*ARGSUSED*/ | |
85 | void | |
86 | kstat_install(kstat_t *ksp) | |
87 | {} | |
88 | ||
89 | /*ARGSUSED*/ | |
90 | void | |
91 | kstat_delete(kstat_t *ksp) | |
92 | {} | |
93 | ||
94 | /* | |
95 | * ========================================================================= | |
96 | * mutexes | |
97 | * ========================================================================= | |
98 | */ | |
99 | void | |
100 | zmutex_init(kmutex_t *mp) | |
101 | { | |
102 | mp->m_owner = NULL; | |
103 | mp->initialized = B_TRUE; | |
104 | (void) _mutex_init(&mp->m_lock, USYNC_THREAD, NULL); | |
105 | } | |
106 | ||
107 | void | |
108 | zmutex_destroy(kmutex_t *mp) | |
109 | { | |
110 | ASSERT(mp->initialized == B_TRUE); | |
111 | ASSERT(mp->m_owner == NULL); | |
112 | (void) _mutex_destroy(&(mp)->m_lock); | |
113 | mp->m_owner = (void *)-1UL; | |
114 | mp->initialized = B_FALSE; | |
115 | } | |
116 | ||
117 | void | |
118 | mutex_enter(kmutex_t *mp) | |
119 | { | |
120 | ASSERT(mp->initialized == B_TRUE); | |
121 | ASSERT(mp->m_owner != (void *)-1UL); | |
122 | ASSERT(mp->m_owner != curthread); | |
123 | VERIFY(mutex_lock(&mp->m_lock) == 0); | |
124 | ASSERT(mp->m_owner == NULL); | |
125 | mp->m_owner = curthread; | |
126 | } | |
127 | ||
128 | int | |
129 | mutex_tryenter(kmutex_t *mp) | |
130 | { | |
131 | ASSERT(mp->initialized == B_TRUE); | |
132 | ASSERT(mp->m_owner != (void *)-1UL); | |
133 | if (0 == mutex_trylock(&mp->m_lock)) { | |
134 | ASSERT(mp->m_owner == NULL); | |
135 | mp->m_owner = curthread; | |
136 | return (1); | |
137 | } else { | |
138 | return (0); | |
139 | } | |
140 | } | |
141 | ||
142 | void | |
143 | mutex_exit(kmutex_t *mp) | |
144 | { | |
145 | ASSERT(mp->initialized == B_TRUE); | |
146 | ASSERT(mutex_owner(mp) == curthread); | |
147 | mp->m_owner = NULL; | |
148 | VERIFY(mutex_unlock(&mp->m_lock) == 0); | |
149 | } | |
150 | ||
151 | void * | |
152 | mutex_owner(kmutex_t *mp) | |
153 | { | |
154 | ASSERT(mp->initialized == B_TRUE); | |
155 | return (mp->m_owner); | |
156 | } | |
157 | ||
158 | /* | |
159 | * ========================================================================= | |
160 | * rwlocks | |
161 | * ========================================================================= | |
162 | */ | |
163 | /*ARGSUSED*/ | |
164 | void | |
165 | rw_init(krwlock_t *rwlp, char *name, int type, void *arg) | |
166 | { | |
167 | rwlock_init(&rwlp->rw_lock, USYNC_THREAD, NULL); | |
168 | rwlp->rw_owner = NULL; | |
169 | rwlp->initialized = B_TRUE; | |
170 | } | |
171 | ||
172 | void | |
173 | rw_destroy(krwlock_t *rwlp) | |
174 | { | |
175 | rwlock_destroy(&rwlp->rw_lock); | |
176 | rwlp->rw_owner = (void *)-1UL; | |
177 | rwlp->initialized = B_FALSE; | |
178 | } | |
179 | ||
180 | void | |
181 | rw_enter(krwlock_t *rwlp, krw_t rw) | |
182 | { | |
183 | ASSERT(!RW_LOCK_HELD(rwlp)); | |
184 | ASSERT(rwlp->initialized == B_TRUE); | |
185 | ASSERT(rwlp->rw_owner != (void *)-1UL); | |
186 | ASSERT(rwlp->rw_owner != curthread); | |
187 | ||
188 | if (rw == RW_READER) | |
189 | (void) rw_rdlock(&rwlp->rw_lock); | |
190 | else | |
191 | (void) rw_wrlock(&rwlp->rw_lock); | |
192 | ||
193 | rwlp->rw_owner = curthread; | |
194 | } | |
195 | ||
196 | void | |
197 | rw_exit(krwlock_t *rwlp) | |
198 | { | |
199 | ASSERT(rwlp->initialized == B_TRUE); | |
200 | ASSERT(rwlp->rw_owner != (void *)-1UL); | |
201 | ||
202 | rwlp->rw_owner = NULL; | |
203 | (void) rw_unlock(&rwlp->rw_lock); | |
204 | } | |
205 | ||
206 | int | |
207 | rw_tryenter(krwlock_t *rwlp, krw_t rw) | |
208 | { | |
209 | int rv; | |
210 | ||
211 | ASSERT(rwlp->initialized == B_TRUE); | |
212 | ASSERT(rwlp->rw_owner != (void *)-1UL); | |
213 | ||
214 | if (rw == RW_READER) | |
215 | rv = rw_tryrdlock(&rwlp->rw_lock); | |
216 | else | |
217 | rv = rw_trywrlock(&rwlp->rw_lock); | |
218 | ||
219 | if (rv == 0) { | |
220 | rwlp->rw_owner = curthread; | |
221 | return (1); | |
222 | } | |
223 | ||
224 | return (0); | |
225 | } | |
226 | ||
227 | /*ARGSUSED*/ | |
228 | int | |
229 | rw_tryupgrade(krwlock_t *rwlp) | |
230 | { | |
231 | ASSERT(rwlp->initialized == B_TRUE); | |
232 | ASSERT(rwlp->rw_owner != (void *)-1UL); | |
233 | ||
234 | return (0); | |
235 | } | |
236 | ||
237 | /* | |
238 | * ========================================================================= | |
239 | * condition variables | |
240 | * ========================================================================= | |
241 | */ | |
242 | /*ARGSUSED*/ | |
243 | void | |
244 | cv_init(kcondvar_t *cv, char *name, int type, void *arg) | |
245 | { | |
246 | VERIFY(cond_init(cv, type, NULL) == 0); | |
247 | } | |
248 | ||
249 | void | |
250 | cv_destroy(kcondvar_t *cv) | |
251 | { | |
252 | VERIFY(cond_destroy(cv) == 0); | |
253 | } | |
254 | ||
255 | void | |
256 | cv_wait(kcondvar_t *cv, kmutex_t *mp) | |
257 | { | |
258 | ASSERT(mutex_owner(mp) == curthread); | |
259 | mp->m_owner = NULL; | |
260 | int ret = cond_wait(cv, &mp->m_lock); | |
261 | VERIFY(ret == 0 || ret == EINTR); | |
262 | mp->m_owner = curthread; | |
263 | } | |
264 | ||
265 | clock_t | |
266 | cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime) | |
267 | { | |
268 | int error; | |
269 | timestruc_t ts; | |
270 | clock_t delta; | |
271 | ||
272 | top: | |
273 | delta = abstime - lbolt; | |
274 | if (delta <= 0) | |
275 | return (-1); | |
276 | ||
277 | ts.tv_sec = delta / hz; | |
278 | ts.tv_nsec = (delta % hz) * (NANOSEC / hz); | |
279 | ||
280 | ASSERT(mutex_owner(mp) == curthread); | |
281 | mp->m_owner = NULL; | |
282 | error = cond_reltimedwait(cv, &mp->m_lock, &ts); | |
283 | mp->m_owner = curthread; | |
284 | ||
285 | if (error == ETIME) | |
286 | return (-1); | |
287 | ||
288 | if (error == EINTR) | |
289 | goto top; | |
290 | ||
291 | ASSERT(error == 0); | |
292 | ||
293 | return (1); | |
294 | } | |
295 | ||
296 | void | |
297 | cv_signal(kcondvar_t *cv) | |
298 | { | |
299 | VERIFY(cond_signal(cv) == 0); | |
300 | } | |
301 | ||
302 | void | |
303 | cv_broadcast(kcondvar_t *cv) | |
304 | { | |
305 | VERIFY(cond_broadcast(cv) == 0); | |
306 | } | |
307 | ||
308 | /* | |
309 | * ========================================================================= | |
310 | * vnode operations | |
311 | * ========================================================================= | |
312 | */ | |
313 | /* | |
314 | * Note: for the xxxat() versions of these functions, we assume that the | |
315 | * starting vp is always rootdir (which is true for spa_directory.c, the only | |
316 | * ZFS consumer of these interfaces). We assert this is true, and then emulate | |
317 | * them by adding '/' in front of the path. | |
318 | */ | |
319 | ||
320 | /*ARGSUSED*/ | |
321 | int | |
322 | vn_open(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, int x3) | |
323 | { | |
324 | int fd; | |
325 | vnode_t *vp; | |
326 | int old_umask; | |
327 | char realpath[MAXPATHLEN]; | |
328 | struct stat64 st; | |
329 | ||
330 | /* | |
331 | * If we're accessing a real disk from userland, we need to use | |
332 | * the character interface to avoid caching. This is particularly | |
333 | * important if we're trying to look at a real in-kernel storage | |
334 | * pool from userland, e.g. via zdb, because otherwise we won't | |
335 | * see the changes occurring under the segmap cache. | |
336 | * On the other hand, the stupid character device returns zero | |
337 | * for its size. So -- gag -- we open the block device to get | |
338 | * its size, and remember it for subsequent VOP_GETATTR(). | |
339 | */ | |
340 | if (strncmp(path, "/dev/", 5) == 0) { | |
341 | char *dsk; | |
342 | fd = open64(path, O_RDONLY); | |
343 | if (fd == -1) | |
344 | return (errno); | |
345 | if (fstat64(fd, &st) == -1) { | |
346 | close(fd); | |
347 | return (errno); | |
348 | } | |
349 | close(fd); | |
350 | (void) sprintf(realpath, "%s", path); | |
351 | dsk = strstr(path, "/dsk/"); | |
352 | if (dsk != NULL) | |
353 | (void) sprintf(realpath + (dsk - path) + 1, "r%s", | |
354 | dsk + 1); | |
355 | } else { | |
356 | (void) sprintf(realpath, "%s", path); | |
357 | if (!(flags & FCREAT) && stat64(realpath, &st) == -1) | |
358 | return (errno); | |
359 | } | |
360 | ||
361 | if (flags & FCREAT) | |
362 | old_umask = umask(0); | |
363 | ||
364 | /* | |
365 | * The construct 'flags - FREAD' conveniently maps combinations of | |
366 | * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR. | |
367 | */ | |
368 | fd = open64(realpath, flags - FREAD, mode); | |
369 | ||
370 | if (flags & FCREAT) | |
371 | (void) umask(old_umask); | |
372 | ||
373 | if (fd == -1) | |
374 | return (errno); | |
375 | ||
376 | if (fstat64(fd, &st) == -1) { | |
377 | close(fd); | |
378 | return (errno); | |
379 | } | |
380 | ||
381 | (void) fcntl(fd, F_SETFD, FD_CLOEXEC); | |
382 | ||
383 | *vpp = vp = umem_zalloc(sizeof (vnode_t), UMEM_NOFAIL); | |
384 | ||
385 | vp->v_fd = fd; | |
386 | vp->v_size = st.st_size; | |
387 | vp->v_path = spa_strdup(path); | |
388 | ||
389 | return (0); | |
390 | } | |
391 | ||
392 | /*ARGSUSED*/ | |
393 | int | |
394 | vn_openat(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, | |
395 | int x3, vnode_t *startvp, int fd) | |
396 | { | |
397 | char *realpath = umem_alloc(strlen(path) + 2, UMEM_NOFAIL); | |
398 | int ret; | |
399 | ||
400 | ASSERT(startvp == rootdir); | |
401 | (void) sprintf(realpath, "/%s", path); | |
402 | ||
403 | /* fd ignored for now, need if want to simulate nbmand support */ | |
404 | ret = vn_open(realpath, x1, flags, mode, vpp, x2, x3); | |
405 | ||
406 | umem_free(realpath, strlen(path) + 2); | |
407 | ||
408 | return (ret); | |
409 | } | |
410 | ||
411 | /*ARGSUSED*/ | |
412 | int | |
413 | vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, offset_t offset, | |
414 | int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp) | |
415 | { | |
416 | ssize_t iolen, split; | |
417 | ||
418 | if (uio == UIO_READ) { | |
419 | iolen = pread64(vp->v_fd, addr, len, offset); | |
420 | } else { | |
421 | /* | |
422 | * To simulate partial disk writes, we split writes into two | |
423 | * system calls so that the process can be killed in between. | |
424 | */ | |
425 | split = (len > 0 ? rand() % len : 0); | |
426 | iolen = pwrite64(vp->v_fd, addr, split, offset); | |
427 | iolen += pwrite64(vp->v_fd, (char *)addr + split, | |
428 | len - split, offset + split); | |
429 | } | |
430 | ||
431 | if (iolen < 0) | |
432 | return (errno); | |
433 | if (residp) | |
434 | *residp = len - iolen; | |
435 | else if (iolen != len) | |
436 | return (EIO); | |
437 | return (0); | |
438 | } | |
439 | ||
440 | void | |
441 | vn_close(vnode_t *vp) | |
442 | { | |
443 | close(vp->v_fd); | |
444 | spa_strfree(vp->v_path); | |
445 | umem_free(vp, sizeof (vnode_t)); | |
446 | } | |
447 | ||
448 | #ifdef ZFS_DEBUG | |
449 | ||
450 | /* | |
451 | * ========================================================================= | |
452 | * Figure out which debugging statements to print | |
453 | * ========================================================================= | |
454 | */ | |
455 | ||
456 | static char *dprintf_string; | |
457 | static int dprintf_print_all; | |
458 | ||
459 | int | |
460 | dprintf_find_string(const char *string) | |
461 | { | |
462 | char *tmp_str = dprintf_string; | |
463 | int len = strlen(string); | |
464 | ||
465 | /* | |
466 | * Find out if this is a string we want to print. | |
467 | * String format: file1.c,function_name1,file2.c,file3.c | |
468 | */ | |
469 | ||
470 | while (tmp_str != NULL) { | |
471 | if (strncmp(tmp_str, string, len) == 0 && | |
472 | (tmp_str[len] == ',' || tmp_str[len] == '\0')) | |
473 | return (1); | |
474 | tmp_str = strchr(tmp_str, ','); | |
475 | if (tmp_str != NULL) | |
476 | tmp_str++; /* Get rid of , */ | |
477 | } | |
478 | return (0); | |
479 | } | |
480 | ||
481 | void | |
482 | dprintf_setup(int *argc, char **argv) | |
483 | { | |
484 | int i, j; | |
485 | ||
486 | /* | |
487 | * Debugging can be specified two ways: by setting the | |
488 | * environment variable ZFS_DEBUG, or by including a | |
489 | * "debug=..." argument on the command line. The command | |
490 | * line setting overrides the environment variable. | |
491 | */ | |
492 | ||
493 | for (i = 1; i < *argc; i++) { | |
494 | int len = strlen("debug="); | |
495 | /* First look for a command line argument */ | |
496 | if (strncmp("debug=", argv[i], len) == 0) { | |
497 | dprintf_string = argv[i] + len; | |
498 | /* Remove from args */ | |
499 | for (j = i; j < *argc; j++) | |
500 | argv[j] = argv[j+1]; | |
501 | argv[j] = NULL; | |
502 | (*argc)--; | |
503 | } | |
504 | } | |
505 | ||
506 | if (dprintf_string == NULL) { | |
507 | /* Look for ZFS_DEBUG environment variable */ | |
508 | dprintf_string = getenv("ZFS_DEBUG"); | |
509 | } | |
510 | ||
511 | /* | |
512 | * Are we just turning on all debugging? | |
513 | */ | |
514 | if (dprintf_find_string("on")) | |
515 | dprintf_print_all = 1; | |
516 | } | |
517 | ||
518 | /* | |
519 | * ========================================================================= | |
520 | * debug printfs | |
521 | * ========================================================================= | |
522 | */ | |
523 | void | |
524 | __dprintf(const char *file, const char *func, int line, const char *fmt, ...) | |
525 | { | |
526 | const char *newfile; | |
527 | va_list adx; | |
528 | ||
529 | /* | |
530 | * Get rid of annoying "../common/" prefix to filename. | |
531 | */ | |
532 | newfile = strrchr(file, '/'); | |
533 | if (newfile != NULL) { | |
534 | newfile = newfile + 1; /* Get rid of leading / */ | |
535 | } else { | |
536 | newfile = file; | |
537 | } | |
538 | ||
539 | if (dprintf_print_all || | |
540 | dprintf_find_string(newfile) || | |
541 | dprintf_find_string(func)) { | |
542 | /* Print out just the function name if requested */ | |
543 | flockfile(stdout); | |
544 | if (dprintf_find_string("pid")) | |
545 | (void) printf("%d ", getpid()); | |
546 | if (dprintf_find_string("tid")) | |
547 | (void) printf("%u ", thr_self()); | |
548 | if (dprintf_find_string("cpu")) | |
549 | (void) printf("%u ", getcpuid()); | |
550 | if (dprintf_find_string("time")) | |
551 | (void) printf("%llu ", gethrtime()); | |
552 | if (dprintf_find_string("long")) | |
553 | (void) printf("%s, line %d: ", newfile, line); | |
554 | (void) printf("%s: ", func); | |
555 | va_start(adx, fmt); | |
556 | (void) vprintf(fmt, adx); | |
557 | va_end(adx); | |
558 | funlockfile(stdout); | |
559 | } | |
560 | } | |
561 | ||
562 | #endif /* ZFS_DEBUG */ | |
563 | ||
564 | /* | |
565 | * ========================================================================= | |
566 | * cmn_err() and panic() | |
567 | * ========================================================================= | |
568 | */ | |
569 | static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" }; | |
570 | static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" }; | |
571 | ||
572 | void | |
573 | vpanic(const char *fmt, va_list adx) | |
574 | { | |
575 | (void) fprintf(stderr, "error: "); | |
576 | (void) vfprintf(stderr, fmt, adx); | |
577 | (void) fprintf(stderr, "\n"); | |
578 | ||
579 | abort(); /* think of it as a "user-level crash dump" */ | |
580 | } | |
581 | ||
582 | void | |
583 | panic(const char *fmt, ...) | |
584 | { | |
585 | va_list adx; | |
586 | ||
587 | va_start(adx, fmt); | |
588 | vpanic(fmt, adx); | |
589 | va_end(adx); | |
590 | } | |
591 | ||
592 | void | |
593 | vcmn_err(int ce, const char *fmt, va_list adx) | |
594 | { | |
595 | if (ce == CE_PANIC) | |
596 | vpanic(fmt, adx); | |
597 | if (ce != CE_NOTE) { /* suppress noise in userland stress testing */ | |
598 | (void) fprintf(stderr, "%s", ce_prefix[ce]); | |
599 | (void) vfprintf(stderr, fmt, adx); | |
600 | (void) fprintf(stderr, "%s", ce_suffix[ce]); | |
601 | } | |
602 | } | |
603 | ||
604 | /*PRINTFLIKE2*/ | |
605 | void | |
606 | cmn_err(int ce, const char *fmt, ...) | |
607 | { | |
608 | va_list adx; | |
609 | ||
610 | va_start(adx, fmt); | |
611 | vcmn_err(ce, fmt, adx); | |
612 | va_end(adx); | |
613 | } | |
614 | ||
615 | /* | |
616 | * ========================================================================= | |
617 | * kobj interfaces | |
618 | * ========================================================================= | |
619 | */ | |
620 | struct _buf * | |
621 | kobj_open_file(char *name) | |
622 | { | |
623 | struct _buf *file; | |
624 | vnode_t *vp; | |
625 | ||
626 | /* set vp as the _fd field of the file */ | |
627 | if (vn_openat(name, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0, rootdir, | |
628 | -1) != 0) | |
629 | return ((void *)-1UL); | |
630 | ||
631 | file = umem_zalloc(sizeof (struct _buf), UMEM_NOFAIL); | |
632 | file->_fd = (intptr_t)vp; | |
633 | return (file); | |
634 | } | |
635 | ||
636 | int | |
637 | kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off) | |
638 | { | |
639 | ssize_t resid; | |
640 | ||
641 | vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off, | |
642 | UIO_SYSSPACE, 0, 0, 0, &resid); | |
643 | ||
644 | return (size - resid); | |
645 | } | |
646 | ||
647 | void | |
648 | kobj_close_file(struct _buf *file) | |
649 | { | |
650 | vn_close((vnode_t *)file->_fd); | |
651 | umem_free(file, sizeof (struct _buf)); | |
652 | } | |
653 | ||
654 | int | |
655 | kobj_get_filesize(struct _buf *file, uint64_t *size) | |
656 | { | |
657 | struct stat64 st; | |
658 | vnode_t *vp = (vnode_t *)file->_fd; | |
659 | ||
660 | if (fstat64(vp->v_fd, &st) == -1) { | |
661 | vn_close(vp); | |
662 | return (errno); | |
663 | } | |
664 | *size = st.st_size; | |
665 | return (0); | |
666 | } | |
667 | ||
668 | /* | |
669 | * ========================================================================= | |
670 | * misc routines | |
671 | * ========================================================================= | |
672 | */ | |
673 | ||
674 | void | |
675 | delay(clock_t ticks) | |
676 | { | |
677 | poll(0, 0, ticks * (1000 / hz)); | |
678 | } | |
679 | ||
680 | /* | |
681 | * Find highest one bit set. | |
682 | * Returns bit number + 1 of highest bit that is set, otherwise returns 0. | |
683 | * High order bit is 31 (or 63 in _LP64 kernel). | |
684 | */ | |
685 | int | |
686 | highbit(ulong_t i) | |
687 | { | |
688 | register int h = 1; | |
689 | ||
690 | if (i == 0) | |
691 | return (0); | |
692 | #ifdef _LP64 | |
693 | if (i & 0xffffffff00000000ul) { | |
694 | h += 32; i >>= 32; | |
695 | } | |
696 | #endif | |
697 | if (i & 0xffff0000) { | |
698 | h += 16; i >>= 16; | |
699 | } | |
700 | if (i & 0xff00) { | |
701 | h += 8; i >>= 8; | |
702 | } | |
703 | if (i & 0xf0) { | |
704 | h += 4; i >>= 4; | |
705 | } | |
706 | if (i & 0xc) { | |
707 | h += 2; i >>= 2; | |
708 | } | |
709 | if (i & 0x2) { | |
710 | h += 1; | |
711 | } | |
712 | return (h); | |
713 | } | |
714 | ||
715 | static int random_fd = -1, urandom_fd = -1; | |
716 | ||
717 | static int | |
718 | random_get_bytes_common(uint8_t *ptr, size_t len, int fd) | |
719 | { | |
720 | size_t resid = len; | |
721 | ssize_t bytes; | |
722 | ||
723 | ASSERT(fd != -1); | |
724 | ||
725 | while (resid != 0) { | |
726 | bytes = read(fd, ptr, resid); | |
727 | ASSERT3S(bytes, >=, 0); | |
728 | ptr += bytes; | |
729 | resid -= bytes; | |
730 | } | |
731 | ||
732 | return (0); | |
733 | } | |
734 | ||
735 | int | |
736 | random_get_bytes(uint8_t *ptr, size_t len) | |
737 | { | |
738 | return (random_get_bytes_common(ptr, len, random_fd)); | |
739 | } | |
740 | ||
741 | int | |
742 | random_get_pseudo_bytes(uint8_t *ptr, size_t len) | |
743 | { | |
744 | return (random_get_bytes_common(ptr, len, urandom_fd)); | |
745 | } | |
746 | ||
747 | int | |
748 | ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result) | |
749 | { | |
750 | char *end; | |
751 | ||
752 | *result = strtoul(hw_serial, &end, base); | |
753 | if (*result == 0) | |
754 | return (errno); | |
755 | return (0); | |
756 | } | |
757 | ||
758 | /* | |
759 | * ========================================================================= | |
760 | * kernel emulation setup & teardown | |
761 | * ========================================================================= | |
762 | */ | |
763 | static int | |
764 | umem_out_of_memory(void) | |
765 | { | |
766 | char errmsg[] = "out of memory -- generating core dump\n"; | |
767 | ||
768 | write(fileno(stderr), errmsg, sizeof (errmsg)); | |
769 | abort(); | |
770 | return (0); | |
771 | } | |
772 | ||
773 | void | |
774 | kernel_init(int mode) | |
775 | { | |
776 | umem_nofail_callback(umem_out_of_memory); | |
777 | ||
778 | physmem = sysconf(_SC_PHYS_PAGES); | |
779 | ||
780 | dprintf("physmem = %llu pages (%.2f GB)\n", physmem, | |
781 | (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30)); | |
782 | ||
783 | uname(&utsname); | |
784 | snprintf(hw_serial, sizeof (hw_serial), "%ld", gethostid()); | |
785 | ||
786 | VERIFY((random_fd = open("/dev/random", O_RDONLY)) != -1); | |
787 | VERIFY((urandom_fd = open("/dev/urandom", O_RDONLY)) != -1); | |
788 | ||
789 | spa_init(mode); | |
790 | } | |
791 | ||
792 | void | |
793 | kernel_fini(void) | |
794 | { | |
795 | spa_fini(); | |
796 | ||
797 | close(random_fd); | |
798 | close(urandom_fd); | |
799 | ||
800 | random_fd = -1; | |
801 | urandom_fd = -1; | |
802 | } | |
803 | ||
804 | int | |
805 | z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen) | |
806 | { | |
807 | int ret; | |
808 | uLongf len = *dstlen; | |
809 | ||
810 | if ((ret = uncompress(dst, &len, src, srclen)) == Z_OK) | |
811 | *dstlen = (size_t)len; | |
812 | ||
813 | return (ret); | |
814 | } | |
815 | ||
816 | int | |
817 | z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen, | |
818 | int level) | |
819 | { | |
820 | int ret; | |
821 | uLongf len = *dstlen; | |
822 | ||
823 | if ((ret = compress2(dst, &len, src, srclen, level)) == Z_OK) | |
824 | *dstlen = (size_t)len; | |
825 | ||
826 | return (ret); | |
827 | } | |
828 | ||
829 | /*ARGSUSED*/ | |
830 | size_t u8_textprep_str(char *i, size_t *il, char *o, size_t *ol, int nf, | |
831 | size_t vers, int *err) | |
832 | { | |
833 | *err = EINVAL; | |
834 | return ((size_t)-1); | |
835 | } | |
836 | ||
837 | uid_t | |
838 | crgetuid(cred_t *cr) | |
839 | { | |
840 | return (0); | |
841 | } | |
842 | ||
843 | gid_t | |
844 | crgetgid(cred_t *cr) | |
845 | { | |
846 | return (0); | |
847 | } | |
848 | ||
849 | int | |
850 | crgetngroups(cred_t *cr) | |
851 | { | |
852 | return (0); | |
853 | } | |
854 | ||
855 | gid_t * | |
856 | crgetgroups(cred_t *cr) | |
857 | { | |
858 | return (NULL); | |
859 | } | |
860 | ||
861 | int | |
862 | zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) | |
863 | { | |
864 | return (0); | |
865 | } | |
866 | ||
867 | int | |
868 | zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) | |
869 | { | |
870 | return (0); | |
871 | } | |
872 | ||
873 | int | |
874 | zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) | |
875 | { | |
876 | return (0); | |
877 | } | |
878 | ||
879 | ksiddomain_t * | |
880 | ksid_lookupdomain(const char *dom) | |
881 | { | |
882 | ksiddomain_t *kd; | |
883 | ||
884 | kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL); | |
885 | kd->kd_name = spa_strdup(dom); | |
886 | return (kd); | |
887 | } | |
888 | ||
889 | void | |
890 | ksiddomain_rele(ksiddomain_t *ksid) | |
891 | { | |
892 | spa_strfree(ksid->kd_name); | |
893 | umem_free(ksid, sizeof (ksiddomain_t)); | |
894 | } |