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