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