1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 22c99e4bdcSChris Kirby * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23bf16b11eSMatthew Ahrens * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 24b3d32f0cSBryan Cantrill * Copyright (c) 2013, Joyent, Inc. All rights reserved. 25fa9e4066Sahrens */ 26fa9e4066Sahrens 27fa9e4066Sahrens #include <assert.h> 28c9431fa1Sahl #include <fcntl.h> 29fa9e4066Sahrens #include <poll.h> 30fa9e4066Sahrens #include <stdio.h> 31fa9e4066Sahrens #include <stdlib.h> 32c9431fa1Sahl #include <string.h> 33c9431fa1Sahl #include <zlib.h> 34*df15e419SMatthew Ahrens #include <libgen.h> 35fa9e4066Sahrens #include <sys/spa.h> 36c9431fa1Sahl #include <sys/stat.h> 37fa9e4066Sahrens #include <sys/processor.h> 38c9431fa1Sahl #include <sys/zfs_context.h> 393b2aab18SMatthew Ahrens #include <sys/rrwlock.h> 40c9431fa1Sahl #include <sys/zmod.h> 4195173954Sek110237 #include <sys/utsname.h> 425679c89fSjv227347 #include <sys/systeminfo.h> 435ad82045Snd150628 44fa9e4066Sahrens /* 45fa9e4066Sahrens * Emulation of kernel services in userland. 46fa9e4066Sahrens */ 47fa9e4066Sahrens 48feef89cfSVictor Latushkin int aok; 49fa9e4066Sahrens uint64_t physmem; 50fa9e4066Sahrens vnode_t *rootdir = (vnode_t *)0xabcd1234; 515679c89fSjv227347 char hw_serial[HW_HOSTID_LEN]; 52283b8460SGeorge.Wilson kmutex_t cpu_lock; 5394dd93aeSGeorge Wilson vmem_t *zio_arena = NULL; 5495173954Sek110237 55*df15e419SMatthew Ahrens /* If set, all blocks read will be copied to the specified directory. */ 56*df15e419SMatthew Ahrens char *vn_dumpdir = NULL; 57*df15e419SMatthew Ahrens 5895173954Sek110237 struct utsname utsname = { 5995173954Sek110237 "userland", "libzpool", "1", "1", "na" 6095173954Sek110237 }; 61fa9e4066Sahrens 6235a5a358SJonathan Adams /* this only exists to have its address taken */ 6335a5a358SJonathan Adams struct proc p0; 6435a5a358SJonathan Adams 65fa9e4066Sahrens /* 66fa9e4066Sahrens * ========================================================================= 67fa9e4066Sahrens * threads 68fa9e4066Sahrens * ========================================================================= 69fa9e4066Sahrens */ 70fa9e4066Sahrens /*ARGSUSED*/ 71fa9e4066Sahrens kthread_t * 72fa9e4066Sahrens zk_thread_create(void (*func)(), void *arg) 73fa9e4066Sahrens { 74fa9e4066Sahrens thread_t tid; 75fa9e4066Sahrens 76fa9e4066Sahrens VERIFY(thr_create(0, 0, (void *(*)(void *))func, arg, THR_DETACHED, 77fa9e4066Sahrens &tid) == 0); 78fa9e4066Sahrens 79fa9e4066Sahrens return ((void *)(uintptr_t)tid); 80fa9e4066Sahrens } 81fa9e4066Sahrens 82fa9e4066Sahrens /* 83fa9e4066Sahrens * ========================================================================= 8444cb6abcSbmc * kstats 8544cb6abcSbmc * ========================================================================= 8644cb6abcSbmc */ 8744cb6abcSbmc /*ARGSUSED*/ 8844cb6abcSbmc kstat_t * 89c3a66015SMatthew Ahrens kstat_create(const char *module, int instance, const char *name, 90c3a66015SMatthew Ahrens const char *class, uchar_t type, ulong_t ndata, uchar_t ks_flag) 9144cb6abcSbmc { 9244cb6abcSbmc return (NULL); 9344cb6abcSbmc } 9444cb6abcSbmc 9544cb6abcSbmc /*ARGSUSED*/ 9644cb6abcSbmc void 9744cb6abcSbmc kstat_install(kstat_t *ksp) 9844cb6abcSbmc {} 9944cb6abcSbmc 10044cb6abcSbmc /*ARGSUSED*/ 10144cb6abcSbmc void 10244cb6abcSbmc kstat_delete(kstat_t *ksp) 10344cb6abcSbmc {} 10444cb6abcSbmc 105c3a66015SMatthew Ahrens /*ARGSUSED*/ 106c3a66015SMatthew Ahrens void 107c3a66015SMatthew Ahrens kstat_waitq_enter(kstat_io_t *kiop) 108c3a66015SMatthew Ahrens {} 109c3a66015SMatthew Ahrens 110c3a66015SMatthew Ahrens /*ARGSUSED*/ 111c3a66015SMatthew Ahrens void 112c3a66015SMatthew Ahrens kstat_waitq_exit(kstat_io_t *kiop) 113c3a66015SMatthew Ahrens {} 114c3a66015SMatthew Ahrens 115c3a66015SMatthew Ahrens /*ARGSUSED*/ 116c3a66015SMatthew Ahrens void 117c3a66015SMatthew Ahrens kstat_runq_enter(kstat_io_t *kiop) 118c3a66015SMatthew Ahrens {} 119c3a66015SMatthew Ahrens 120c3a66015SMatthew Ahrens /*ARGSUSED*/ 121c3a66015SMatthew Ahrens void 122c3a66015SMatthew Ahrens kstat_runq_exit(kstat_io_t *kiop) 123c3a66015SMatthew Ahrens {} 124c3a66015SMatthew Ahrens 125c3a66015SMatthew Ahrens /*ARGSUSED*/ 126c3a66015SMatthew Ahrens void 127c3a66015SMatthew Ahrens kstat_waitq_to_runq(kstat_io_t *kiop) 128c3a66015SMatthew Ahrens {} 129c3a66015SMatthew Ahrens 130c3a66015SMatthew Ahrens /*ARGSUSED*/ 131c3a66015SMatthew Ahrens void 132c3a66015SMatthew Ahrens kstat_runq_back_to_waitq(kstat_io_t *kiop) 133c3a66015SMatthew Ahrens {} 134c3a66015SMatthew Ahrens 13544cb6abcSbmc /* 13644cb6abcSbmc * ========================================================================= 137fa9e4066Sahrens * mutexes 138fa9e4066Sahrens * ========================================================================= 139fa9e4066Sahrens */ 140fa9e4066Sahrens void 141fa9e4066Sahrens zmutex_init(kmutex_t *mp) 142fa9e4066Sahrens { 143fa9e4066Sahrens mp->m_owner = NULL; 144c25056deSgw25295 mp->initialized = B_TRUE; 145fa9e4066Sahrens (void) _mutex_init(&mp->m_lock, USYNC_THREAD, NULL); 146fa9e4066Sahrens } 147fa9e4066Sahrens 148fa9e4066Sahrens void 149fa9e4066Sahrens zmutex_destroy(kmutex_t *mp) 150fa9e4066Sahrens { 151c25056deSgw25295 ASSERT(mp->initialized == B_TRUE); 152fa9e4066Sahrens ASSERT(mp->m_owner == NULL); 153fa9e4066Sahrens (void) _mutex_destroy(&(mp)->m_lock); 154fa9e4066Sahrens mp->m_owner = (void *)-1UL; 155c25056deSgw25295 mp->initialized = B_FALSE; 156fa9e4066Sahrens } 157fa9e4066Sahrens 158fa9e4066Sahrens void 159fa9e4066Sahrens mutex_enter(kmutex_t *mp) 160fa9e4066Sahrens { 161c25056deSgw25295 ASSERT(mp->initialized == B_TRUE); 162fa9e4066Sahrens ASSERT(mp->m_owner != (void *)-1UL); 163fa9e4066Sahrens ASSERT(mp->m_owner != curthread); 1645ad82045Snd150628 VERIFY(mutex_lock(&mp->m_lock) == 0); 165fa9e4066Sahrens ASSERT(mp->m_owner == NULL); 166fa9e4066Sahrens mp->m_owner = curthread; 167fa9e4066Sahrens } 168fa9e4066Sahrens 169fa9e4066Sahrens int 170fa9e4066Sahrens mutex_tryenter(kmutex_t *mp) 171fa9e4066Sahrens { 172c25056deSgw25295 ASSERT(mp->initialized == B_TRUE); 173fa9e4066Sahrens ASSERT(mp->m_owner != (void *)-1UL); 174fa9e4066Sahrens if (0 == mutex_trylock(&mp->m_lock)) { 175fa9e4066Sahrens ASSERT(mp->m_owner == NULL); 176fa9e4066Sahrens mp->m_owner = curthread; 177fa9e4066Sahrens return (1); 178fa9e4066Sahrens } else { 179fa9e4066Sahrens return (0); 180fa9e4066Sahrens } 181fa9e4066Sahrens } 182fa9e4066Sahrens 183fa9e4066Sahrens void 184fa9e4066Sahrens mutex_exit(kmutex_t *mp) 185fa9e4066Sahrens { 186c25056deSgw25295 ASSERT(mp->initialized == B_TRUE); 187fa9e4066Sahrens ASSERT(mutex_owner(mp) == curthread); 188fa9e4066Sahrens mp->m_owner = NULL; 1895ad82045Snd150628 VERIFY(mutex_unlock(&mp->m_lock) == 0); 190fa9e4066Sahrens } 191fa9e4066Sahrens 192fa9e4066Sahrens void * 193fa9e4066Sahrens mutex_owner(kmutex_t *mp) 194fa9e4066Sahrens { 195c25056deSgw25295 ASSERT(mp->initialized == B_TRUE); 196fa9e4066Sahrens return (mp->m_owner); 197fa9e4066Sahrens } 198fa9e4066Sahrens 199fa9e4066Sahrens /* 200fa9e4066Sahrens * ========================================================================= 201fa9e4066Sahrens * rwlocks 202fa9e4066Sahrens * ========================================================================= 203fa9e4066Sahrens */ 204fa9e4066Sahrens /*ARGSUSED*/ 205fa9e4066Sahrens void 206fa9e4066Sahrens rw_init(krwlock_t *rwlp, char *name, int type, void *arg) 207fa9e4066Sahrens { 208fa9e4066Sahrens rwlock_init(&rwlp->rw_lock, USYNC_THREAD, NULL); 209fa9e4066Sahrens rwlp->rw_owner = NULL; 210c25056deSgw25295 rwlp->initialized = B_TRUE; 211fa9e4066Sahrens } 212fa9e4066Sahrens 213fa9e4066Sahrens void 214fa9e4066Sahrens rw_destroy(krwlock_t *rwlp) 215fa9e4066Sahrens { 216fa9e4066Sahrens rwlock_destroy(&rwlp->rw_lock); 217fa9e4066Sahrens rwlp->rw_owner = (void *)-1UL; 218c25056deSgw25295 rwlp->initialized = B_FALSE; 219fa9e4066Sahrens } 220fa9e4066Sahrens 221fa9e4066Sahrens void 222fa9e4066Sahrens rw_enter(krwlock_t *rwlp, krw_t rw) 223fa9e4066Sahrens { 224fa9e4066Sahrens ASSERT(!RW_LOCK_HELD(rwlp)); 225c25056deSgw25295 ASSERT(rwlp->initialized == B_TRUE); 226fa9e4066Sahrens ASSERT(rwlp->rw_owner != (void *)-1UL); 227fa9e4066Sahrens ASSERT(rwlp->rw_owner != curthread); 228fa9e4066Sahrens 229b3d32f0cSBryan Cantrill if (rw == RW_WRITER) 230745cd3c5Smaybee VERIFY(rw_wrlock(&rwlp->rw_lock) == 0); 231b3d32f0cSBryan Cantrill else 232b3d32f0cSBryan Cantrill VERIFY(rw_rdlock(&rwlp->rw_lock) == 0); 233fa9e4066Sahrens 234fa9e4066Sahrens rwlp->rw_owner = curthread; 235fa9e4066Sahrens } 236fa9e4066Sahrens 237fa9e4066Sahrens void 238fa9e4066Sahrens rw_exit(krwlock_t *rwlp) 239fa9e4066Sahrens { 240c25056deSgw25295 ASSERT(rwlp->initialized == B_TRUE); 241fa9e4066Sahrens ASSERT(rwlp->rw_owner != (void *)-1UL); 242fa9e4066Sahrens 243fa9e4066Sahrens rwlp->rw_owner = NULL; 244745cd3c5Smaybee VERIFY(rw_unlock(&rwlp->rw_lock) == 0); 245fa9e4066Sahrens } 246fa9e4066Sahrens 247fa9e4066Sahrens int 248fa9e4066Sahrens rw_tryenter(krwlock_t *rwlp, krw_t rw) 249fa9e4066Sahrens { 250fa9e4066Sahrens int rv; 251fa9e4066Sahrens 252c25056deSgw25295 ASSERT(rwlp->initialized == B_TRUE); 253fa9e4066Sahrens ASSERT(rwlp->rw_owner != (void *)-1UL); 254fa9e4066Sahrens 255b3d32f0cSBryan Cantrill if (rw == RW_WRITER) 256fa9e4066Sahrens rv = rw_trywrlock(&rwlp->rw_lock); 257b3d32f0cSBryan Cantrill else 258b3d32f0cSBryan Cantrill rv = rw_tryrdlock(&rwlp->rw_lock); 259fa9e4066Sahrens 260fa9e4066Sahrens if (rv == 0) { 261fa9e4066Sahrens rwlp->rw_owner = curthread; 262fa9e4066Sahrens return (1); 263fa9e4066Sahrens } 264fa9e4066Sahrens 265fa9e4066Sahrens return (0); 266fa9e4066Sahrens } 267fa9e4066Sahrens 268fa9e4066Sahrens /*ARGSUSED*/ 269fa9e4066Sahrens int 270fa9e4066Sahrens rw_tryupgrade(krwlock_t *rwlp) 271fa9e4066Sahrens { 272c25056deSgw25295 ASSERT(rwlp->initialized == B_TRUE); 273fa9e4066Sahrens ASSERT(rwlp->rw_owner != (void *)-1UL); 274fa9e4066Sahrens 275fa9e4066Sahrens return (0); 276fa9e4066Sahrens } 277fa9e4066Sahrens 278fa9e4066Sahrens /* 279fa9e4066Sahrens * ========================================================================= 280fa9e4066Sahrens * condition variables 281fa9e4066Sahrens * ========================================================================= 282fa9e4066Sahrens */ 283fa9e4066Sahrens /*ARGSUSED*/ 284fa9e4066Sahrens void 285fa9e4066Sahrens cv_init(kcondvar_t *cv, char *name, int type, void *arg) 286fa9e4066Sahrens { 2875ad82045Snd150628 VERIFY(cond_init(cv, type, NULL) == 0); 288fa9e4066Sahrens } 289fa9e4066Sahrens 290fa9e4066Sahrens void 291fa9e4066Sahrens cv_destroy(kcondvar_t *cv) 292fa9e4066Sahrens { 2935ad82045Snd150628 VERIFY(cond_destroy(cv) == 0); 294fa9e4066Sahrens } 295fa9e4066Sahrens 296fa9e4066Sahrens void 297fa9e4066Sahrens cv_wait(kcondvar_t *cv, kmutex_t *mp) 298fa9e4066Sahrens { 299fa9e4066Sahrens ASSERT(mutex_owner(mp) == curthread); 300fa9e4066Sahrens mp->m_owner = NULL; 3015ad82045Snd150628 int ret = cond_wait(cv, &mp->m_lock); 3025ad82045Snd150628 VERIFY(ret == 0 || ret == EINTR); 303fa9e4066Sahrens mp->m_owner = curthread; 304fa9e4066Sahrens } 305fa9e4066Sahrens 306fa9e4066Sahrens clock_t 307fa9e4066Sahrens cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime) 308fa9e4066Sahrens { 309fa9e4066Sahrens int error; 310fa9e4066Sahrens timestruc_t ts; 311fa9e4066Sahrens clock_t delta; 312fa9e4066Sahrens 313fa9e4066Sahrens top: 314d3d50737SRafael Vanoni delta = abstime - ddi_get_lbolt(); 315fa9e4066Sahrens if (delta <= 0) 316fa9e4066Sahrens return (-1); 317fa9e4066Sahrens 318fa9e4066Sahrens ts.tv_sec = delta / hz; 319fa9e4066Sahrens ts.tv_nsec = (delta % hz) * (NANOSEC / hz); 320fa9e4066Sahrens 321fa9e4066Sahrens ASSERT(mutex_owner(mp) == curthread); 322fa9e4066Sahrens mp->m_owner = NULL; 323fa9e4066Sahrens error = cond_reltimedwait(cv, &mp->m_lock, &ts); 324fa9e4066Sahrens mp->m_owner = curthread; 325fa9e4066Sahrens 326fa9e4066Sahrens if (error == ETIME) 327fa9e4066Sahrens return (-1); 328fa9e4066Sahrens 329fa9e4066Sahrens if (error == EINTR) 330fa9e4066Sahrens goto top; 331fa9e4066Sahrens 332fa9e4066Sahrens ASSERT(error == 0); 333fa9e4066Sahrens 334fa9e4066Sahrens return (1); 335fa9e4066Sahrens } 336fa9e4066Sahrens 3370689f76cSAdam Leventhal /*ARGSUSED*/ 3380689f76cSAdam Leventhal clock_t 3390689f76cSAdam Leventhal cv_timedwait_hires(kcondvar_t *cv, kmutex_t *mp, hrtime_t tim, hrtime_t res, 3400689f76cSAdam Leventhal int flag) 3410689f76cSAdam Leventhal { 3420689f76cSAdam Leventhal int error; 3430689f76cSAdam Leventhal timestruc_t ts; 3440689f76cSAdam Leventhal hrtime_t delta; 3450689f76cSAdam Leventhal 3460689f76cSAdam Leventhal ASSERT(flag == 0); 3470689f76cSAdam Leventhal 3480689f76cSAdam Leventhal top: 3490689f76cSAdam Leventhal delta = tim - gethrtime(); 3500689f76cSAdam Leventhal if (delta <= 0) 3510689f76cSAdam Leventhal return (-1); 3520689f76cSAdam Leventhal 3530689f76cSAdam Leventhal ts.tv_sec = delta / NANOSEC; 3540689f76cSAdam Leventhal ts.tv_nsec = delta % NANOSEC; 3550689f76cSAdam Leventhal 3560689f76cSAdam Leventhal ASSERT(mutex_owner(mp) == curthread); 3570689f76cSAdam Leventhal mp->m_owner = NULL; 3580689f76cSAdam Leventhal error = cond_reltimedwait(cv, &mp->m_lock, &ts); 3590689f76cSAdam Leventhal mp->m_owner = curthread; 3600689f76cSAdam Leventhal 3610689f76cSAdam Leventhal if (error == ETIME) 3620689f76cSAdam Leventhal return (-1); 3630689f76cSAdam Leventhal 3640689f76cSAdam Leventhal if (error == EINTR) 3650689f76cSAdam Leventhal goto top; 3660689f76cSAdam Leventhal 3670689f76cSAdam Leventhal ASSERT(error == 0); 3680689f76cSAdam Leventhal 3690689f76cSAdam Leventhal return (1); 3700689f76cSAdam Leventhal } 3710689f76cSAdam Leventhal 372fa9e4066Sahrens void 373fa9e4066Sahrens cv_signal(kcondvar_t *cv) 374fa9e4066Sahrens { 3755ad82045Snd150628 VERIFY(cond_signal(cv) == 0); 376fa9e4066Sahrens } 377fa9e4066Sahrens 378fa9e4066Sahrens void 379fa9e4066Sahrens cv_broadcast(kcondvar_t *cv) 380fa9e4066Sahrens { 3815ad82045Snd150628 VERIFY(cond_broadcast(cv) == 0); 382fa9e4066Sahrens } 383fa9e4066Sahrens 384fa9e4066Sahrens /* 385fa9e4066Sahrens * ========================================================================= 386fa9e4066Sahrens * vnode operations 387fa9e4066Sahrens * ========================================================================= 388fa9e4066Sahrens */ 389fa9e4066Sahrens /* 390fa9e4066Sahrens * Note: for the xxxat() versions of these functions, we assume that the 391fa9e4066Sahrens * starting vp is always rootdir (which is true for spa_directory.c, the only 392fa9e4066Sahrens * ZFS consumer of these interfaces). We assert this is true, and then emulate 393fa9e4066Sahrens * them by adding '/' in front of the path. 394fa9e4066Sahrens */ 395fa9e4066Sahrens 396fa9e4066Sahrens /*ARGSUSED*/ 397fa9e4066Sahrens int 398fa9e4066Sahrens vn_open(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, int x3) 399fa9e4066Sahrens { 400fa9e4066Sahrens int fd; 401*df15e419SMatthew Ahrens int dump_fd; 402fa9e4066Sahrens vnode_t *vp; 403fa9e4066Sahrens int old_umask; 404fa9e4066Sahrens char realpath[MAXPATHLEN]; 405fa9e4066Sahrens struct stat64 st; 406fa9e4066Sahrens 407fa9e4066Sahrens /* 408fa9e4066Sahrens * If we're accessing a real disk from userland, we need to use 409fa9e4066Sahrens * the character interface to avoid caching. This is particularly 410fa9e4066Sahrens * important if we're trying to look at a real in-kernel storage 411fa9e4066Sahrens * pool from userland, e.g. via zdb, because otherwise we won't 412fa9e4066Sahrens * see the changes occurring under the segmap cache. 413fa9e4066Sahrens * On the other hand, the stupid character device returns zero 414fa9e4066Sahrens * for its size. So -- gag -- we open the block device to get 415fa9e4066Sahrens * its size, and remember it for subsequent VOP_GETATTR(). 416fa9e4066Sahrens */ 417fa9e4066Sahrens if (strncmp(path, "/dev/", 5) == 0) { 418fa9e4066Sahrens char *dsk; 419fa9e4066Sahrens fd = open64(path, O_RDONLY); 420fa9e4066Sahrens if (fd == -1) 421fa9e4066Sahrens return (errno); 422fa9e4066Sahrens if (fstat64(fd, &st) == -1) { 423fa9e4066Sahrens close(fd); 424fa9e4066Sahrens return (errno); 425fa9e4066Sahrens } 426fa9e4066Sahrens close(fd); 427fa9e4066Sahrens (void) sprintf(realpath, "%s", path); 428fa9e4066Sahrens dsk = strstr(path, "/dsk/"); 429fa9e4066Sahrens if (dsk != NULL) 430fa9e4066Sahrens (void) sprintf(realpath + (dsk - path) + 1, "r%s", 431fa9e4066Sahrens dsk + 1); 432fa9e4066Sahrens } else { 433fa9e4066Sahrens (void) sprintf(realpath, "%s", path); 434fa9e4066Sahrens if (!(flags & FCREAT) && stat64(realpath, &st) == -1) 435fa9e4066Sahrens return (errno); 436fa9e4066Sahrens } 437fa9e4066Sahrens 438fa9e4066Sahrens if (flags & FCREAT) 439fa9e4066Sahrens old_umask = umask(0); 440fa9e4066Sahrens 441fa9e4066Sahrens /* 442fa9e4066Sahrens * The construct 'flags - FREAD' conveniently maps combinations of 443fa9e4066Sahrens * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR. 444fa9e4066Sahrens */ 445fa9e4066Sahrens fd = open64(realpath, flags - FREAD, mode); 446fa9e4066Sahrens 447fa9e4066Sahrens if (flags & FCREAT) 448fa9e4066Sahrens (void) umask(old_umask); 449fa9e4066Sahrens 450*df15e419SMatthew Ahrens if (vn_dumpdir != NULL) { 451*df15e419SMatthew Ahrens char dumppath[MAXPATHLEN]; 452*df15e419SMatthew Ahrens (void) snprintf(dumppath, sizeof (dumppath), 453*df15e419SMatthew Ahrens "%s/%s", vn_dumpdir, basename(realpath)); 454*df15e419SMatthew Ahrens dump_fd = open64(dumppath, O_CREAT | O_WRONLY, 0666); 455*df15e419SMatthew Ahrens if (dump_fd == -1) 456*df15e419SMatthew Ahrens return (errno); 457*df15e419SMatthew Ahrens } else { 458*df15e419SMatthew Ahrens dump_fd = -1; 459*df15e419SMatthew Ahrens } 460*df15e419SMatthew Ahrens 461fa9e4066Sahrens if (fd == -1) 462fa9e4066Sahrens return (errno); 463fa9e4066Sahrens 464fa9e4066Sahrens if (fstat64(fd, &st) == -1) { 465fa9e4066Sahrens close(fd); 466fa9e4066Sahrens return (errno); 467fa9e4066Sahrens } 468fa9e4066Sahrens 469fa9e4066Sahrens (void) fcntl(fd, F_SETFD, FD_CLOEXEC); 470fa9e4066Sahrens 471fa9e4066Sahrens *vpp = vp = umem_zalloc(sizeof (vnode_t), UMEM_NOFAIL); 472fa9e4066Sahrens 473fa9e4066Sahrens vp->v_fd = fd; 474fa9e4066Sahrens vp->v_size = st.st_size; 475fa9e4066Sahrens vp->v_path = spa_strdup(path); 476*df15e419SMatthew Ahrens vp->v_dump_fd = dump_fd; 477fa9e4066Sahrens 478fa9e4066Sahrens return (0); 479fa9e4066Sahrens } 480fa9e4066Sahrens 481da6c28aaSamw /*ARGSUSED*/ 482fa9e4066Sahrens int 483fa9e4066Sahrens vn_openat(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, 484da6c28aaSamw int x3, vnode_t *startvp, int fd) 485fa9e4066Sahrens { 486fa9e4066Sahrens char *realpath = umem_alloc(strlen(path) + 2, UMEM_NOFAIL); 487fa9e4066Sahrens int ret; 488fa9e4066Sahrens 489fa9e4066Sahrens ASSERT(startvp == rootdir); 490fa9e4066Sahrens (void) sprintf(realpath, "/%s", path); 491fa9e4066Sahrens 492da6c28aaSamw /* fd ignored for now, need if want to simulate nbmand support */ 493fa9e4066Sahrens ret = vn_open(realpath, x1, flags, mode, vpp, x2, x3); 494fa9e4066Sahrens 495fa9e4066Sahrens umem_free(realpath, strlen(path) + 2); 496fa9e4066Sahrens 497fa9e4066Sahrens return (ret); 498fa9e4066Sahrens } 499fa9e4066Sahrens 500fa9e4066Sahrens /*ARGSUSED*/ 501fa9e4066Sahrens int 502fa9e4066Sahrens vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, offset_t offset, 503fa9e4066Sahrens int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp) 504fa9e4066Sahrens { 505fa9e4066Sahrens ssize_t iolen, split; 506fa9e4066Sahrens 507fa9e4066Sahrens if (uio == UIO_READ) { 508fa9e4066Sahrens iolen = pread64(vp->v_fd, addr, len, offset); 509*df15e419SMatthew Ahrens if (vp->v_dump_fd != -1) { 510*df15e419SMatthew Ahrens int status = 511*df15e419SMatthew Ahrens pwrite64(vp->v_dump_fd, addr, iolen, offset); 512*df15e419SMatthew Ahrens ASSERT(status != -1); 513*df15e419SMatthew Ahrens } 514fa9e4066Sahrens } else { 515fa9e4066Sahrens /* 516fa9e4066Sahrens * To simulate partial disk writes, we split writes into two 517fa9e4066Sahrens * system calls so that the process can be killed in between. 518fa9e4066Sahrens */ 519ad135b5dSChristopher Siden int sectors = len >> SPA_MINBLOCKSHIFT; 520ad135b5dSChristopher Siden split = (sectors > 0 ? rand() % sectors : 0) << 521ad135b5dSChristopher Siden SPA_MINBLOCKSHIFT; 522fa9e4066Sahrens iolen = pwrite64(vp->v_fd, addr, split, offset); 523fa9e4066Sahrens iolen += pwrite64(vp->v_fd, (char *)addr + split, 524fa9e4066Sahrens len - split, offset + split); 525fa9e4066Sahrens } 526fa9e4066Sahrens 527fa9e4066Sahrens if (iolen == -1) 528fa9e4066Sahrens return (errno); 529fa9e4066Sahrens if (residp) 530fa9e4066Sahrens *residp = len - iolen; 531fa9e4066Sahrens else if (iolen != len) 532fa9e4066Sahrens return (EIO); 533fa9e4066Sahrens return (0); 534fa9e4066Sahrens } 535fa9e4066Sahrens 536fa9e4066Sahrens void 537fa9e4066Sahrens vn_close(vnode_t *vp) 538fa9e4066Sahrens { 539fa9e4066Sahrens close(vp->v_fd); 540*df15e419SMatthew Ahrens if (vp->v_dump_fd != -1) 541*df15e419SMatthew Ahrens close(vp->v_dump_fd); 542fa9e4066Sahrens spa_strfree(vp->v_path); 543fa9e4066Sahrens umem_free(vp, sizeof (vnode_t)); 544fa9e4066Sahrens } 545fa9e4066Sahrens 546095bcd66SGeorge Wilson /* 547095bcd66SGeorge Wilson * At a minimum we need to update the size since vdev_reopen() 548095bcd66SGeorge Wilson * will no longer call vn_openat(). 549095bcd66SGeorge Wilson */ 550095bcd66SGeorge Wilson int 551095bcd66SGeorge Wilson fop_getattr(vnode_t *vp, vattr_t *vap) 552095bcd66SGeorge Wilson { 553095bcd66SGeorge Wilson struct stat64 st; 554095bcd66SGeorge Wilson 555095bcd66SGeorge Wilson if (fstat64(vp->v_fd, &st) == -1) { 556095bcd66SGeorge Wilson close(vp->v_fd); 557095bcd66SGeorge Wilson return (errno); 558095bcd66SGeorge Wilson } 559095bcd66SGeorge Wilson 560095bcd66SGeorge Wilson vap->va_size = st.st_size; 561095bcd66SGeorge Wilson return (0); 562095bcd66SGeorge Wilson } 563095bcd66SGeorge Wilson 564fa9e4066Sahrens #ifdef ZFS_DEBUG 565fa9e4066Sahrens 566fa9e4066Sahrens /* 567fa9e4066Sahrens * ========================================================================= 568fa9e4066Sahrens * Figure out which debugging statements to print 569fa9e4066Sahrens * ========================================================================= 570fa9e4066Sahrens */ 571fa9e4066Sahrens 572fa9e4066Sahrens static char *dprintf_string; 573fa9e4066Sahrens static int dprintf_print_all; 574fa9e4066Sahrens 575fa9e4066Sahrens int 576fa9e4066Sahrens dprintf_find_string(const char *string) 577fa9e4066Sahrens { 578fa9e4066Sahrens char *tmp_str = dprintf_string; 579fa9e4066Sahrens int len = strlen(string); 580fa9e4066Sahrens 581fa9e4066Sahrens /* 582fa9e4066Sahrens * Find out if this is a string we want to print. 583fa9e4066Sahrens * String format: file1.c,function_name1,file2.c,file3.c 584fa9e4066Sahrens */ 585fa9e4066Sahrens 586fa9e4066Sahrens while (tmp_str != NULL) { 587fa9e4066Sahrens if (strncmp(tmp_str, string, len) == 0 && 588fa9e4066Sahrens (tmp_str[len] == ',' || tmp_str[len] == '\0')) 589fa9e4066Sahrens return (1); 590fa9e4066Sahrens tmp_str = strchr(tmp_str, ','); 591fa9e4066Sahrens if (tmp_str != NULL) 592fa9e4066Sahrens tmp_str++; /* Get rid of , */ 593fa9e4066Sahrens } 594fa9e4066Sahrens return (0); 595fa9e4066Sahrens } 596fa9e4066Sahrens 597fa9e4066Sahrens void 598fa9e4066Sahrens dprintf_setup(int *argc, char **argv) 599fa9e4066Sahrens { 600fa9e4066Sahrens int i, j; 601fa9e4066Sahrens 602fa9e4066Sahrens /* 603fa9e4066Sahrens * Debugging can be specified two ways: by setting the 604fa9e4066Sahrens * environment variable ZFS_DEBUG, or by including a 605fa9e4066Sahrens * "debug=..." argument on the command line. The command 606fa9e4066Sahrens * line setting overrides the environment variable. 607fa9e4066Sahrens */ 608fa9e4066Sahrens 609fa9e4066Sahrens for (i = 1; i < *argc; i++) { 610fa9e4066Sahrens int len = strlen("debug="); 611fa9e4066Sahrens /* First look for a command line argument */ 612fa9e4066Sahrens if (strncmp("debug=", argv[i], len) == 0) { 613fa9e4066Sahrens dprintf_string = argv[i] + len; 614fa9e4066Sahrens /* Remove from args */ 615fa9e4066Sahrens for (j = i; j < *argc; j++) 616fa9e4066Sahrens argv[j] = argv[j+1]; 617fa9e4066Sahrens argv[j] = NULL; 618fa9e4066Sahrens (*argc)--; 619fa9e4066Sahrens } 620fa9e4066Sahrens } 621fa9e4066Sahrens 622fa9e4066Sahrens if (dprintf_string == NULL) { 623fa9e4066Sahrens /* Look for ZFS_DEBUG environment variable */ 624fa9e4066Sahrens dprintf_string = getenv("ZFS_DEBUG"); 625fa9e4066Sahrens } 626fa9e4066Sahrens 627fa9e4066Sahrens /* 628fa9e4066Sahrens * Are we just turning on all debugging? 629fa9e4066Sahrens */ 630fa9e4066Sahrens if (dprintf_find_string("on")) 631fa9e4066Sahrens dprintf_print_all = 1; 632fa9e4066Sahrens } 633fa9e4066Sahrens 634fa9e4066Sahrens /* 635fa9e4066Sahrens * ========================================================================= 636fa9e4066Sahrens * debug printfs 637fa9e4066Sahrens * ========================================================================= 638fa9e4066Sahrens */ 639fa9e4066Sahrens void 640fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...) 641fa9e4066Sahrens { 642fa9e4066Sahrens const char *newfile; 643fa9e4066Sahrens va_list adx; 644fa9e4066Sahrens 645fa9e4066Sahrens /* 646fa9e4066Sahrens * Get rid of annoying "../common/" prefix to filename. 647fa9e4066Sahrens */ 648fa9e4066Sahrens newfile = strrchr(file, '/'); 649fa9e4066Sahrens if (newfile != NULL) { 650fa9e4066Sahrens newfile = newfile + 1; /* Get rid of leading / */ 651fa9e4066Sahrens } else { 652fa9e4066Sahrens newfile = file; 653fa9e4066Sahrens } 654fa9e4066Sahrens 655fa9e4066Sahrens if (dprintf_print_all || 656fa9e4066Sahrens dprintf_find_string(newfile) || 657fa9e4066Sahrens dprintf_find_string(func)) { 658fa9e4066Sahrens /* Print out just the function name if requested */ 659fa9e4066Sahrens flockfile(stdout); 660fa9e4066Sahrens if (dprintf_find_string("pid")) 661fa9e4066Sahrens (void) printf("%d ", getpid()); 662fa9e4066Sahrens if (dprintf_find_string("tid")) 663fa9e4066Sahrens (void) printf("%u ", thr_self()); 664fa9e4066Sahrens if (dprintf_find_string("cpu")) 665fa9e4066Sahrens (void) printf("%u ", getcpuid()); 666fa9e4066Sahrens if (dprintf_find_string("time")) 667fa9e4066Sahrens (void) printf("%llu ", gethrtime()); 668fa9e4066Sahrens if (dprintf_find_string("long")) 669fa9e4066Sahrens (void) printf("%s, line %d: ", newfile, line); 670fa9e4066Sahrens (void) printf("%s: ", func); 671fa9e4066Sahrens va_start(adx, fmt); 672fa9e4066Sahrens (void) vprintf(fmt, adx); 673fa9e4066Sahrens va_end(adx); 674fa9e4066Sahrens funlockfile(stdout); 675fa9e4066Sahrens } 676fa9e4066Sahrens } 677fa9e4066Sahrens 678fa9e4066Sahrens #endif /* ZFS_DEBUG */ 679fa9e4066Sahrens 680fa9e4066Sahrens /* 681fa9e4066Sahrens * ========================================================================= 682fa9e4066Sahrens * cmn_err() and panic() 683fa9e4066Sahrens * ========================================================================= 684fa9e4066Sahrens */ 685fa9e4066Sahrens static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" }; 686fa9e4066Sahrens static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" }; 687fa9e4066Sahrens 688fa9e4066Sahrens void 689fa9e4066Sahrens vpanic(const char *fmt, va_list adx) 690fa9e4066Sahrens { 691fa9e4066Sahrens (void) fprintf(stderr, "error: "); 692fa9e4066Sahrens (void) vfprintf(stderr, fmt, adx); 693fa9e4066Sahrens (void) fprintf(stderr, "\n"); 694fa9e4066Sahrens 695fa9e4066Sahrens abort(); /* think of it as a "user-level crash dump" */ 696fa9e4066Sahrens } 697fa9e4066Sahrens 698fa9e4066Sahrens void 699fa9e4066Sahrens panic(const char *fmt, ...) 700fa9e4066Sahrens { 701fa9e4066Sahrens va_list adx; 702fa9e4066Sahrens 703fa9e4066Sahrens va_start(adx, fmt); 704fa9e4066Sahrens vpanic(fmt, adx); 705fa9e4066Sahrens va_end(adx); 706fa9e4066Sahrens } 707fa9e4066Sahrens 708fa9e4066Sahrens void 7090125049cSahrens vcmn_err(int ce, const char *fmt, va_list adx) 710fa9e4066Sahrens { 711fa9e4066Sahrens if (ce == CE_PANIC) 712fa9e4066Sahrens vpanic(fmt, adx); 713fa9e4066Sahrens if (ce != CE_NOTE) { /* suppress noise in userland stress testing */ 714fa9e4066Sahrens (void) fprintf(stderr, "%s", ce_prefix[ce]); 715fa9e4066Sahrens (void) vfprintf(stderr, fmt, adx); 716fa9e4066Sahrens (void) fprintf(stderr, "%s", ce_suffix[ce]); 717fa9e4066Sahrens } 7180125049cSahrens } 7190125049cSahrens 7200125049cSahrens /*PRINTFLIKE2*/ 7210125049cSahrens void 7220125049cSahrens cmn_err(int ce, const char *fmt, ...) 7230125049cSahrens { 7240125049cSahrens va_list adx; 7250125049cSahrens 7260125049cSahrens va_start(adx, fmt); 7270125049cSahrens vcmn_err(ce, fmt, adx); 728fa9e4066Sahrens va_end(adx); 729fa9e4066Sahrens } 730fa9e4066Sahrens 731fa9e4066Sahrens /* 732fa9e4066Sahrens * ========================================================================= 733ea8dc4b6Seschrock * kobj interfaces 734ea8dc4b6Seschrock * ========================================================================= 735ea8dc4b6Seschrock */ 736ea8dc4b6Seschrock struct _buf * 737ea8dc4b6Seschrock kobj_open_file(char *name) 738ea8dc4b6Seschrock { 739ea8dc4b6Seschrock struct _buf *file; 740ea8dc4b6Seschrock vnode_t *vp; 741ea8dc4b6Seschrock 742ea8dc4b6Seschrock /* set vp as the _fd field of the file */ 743da6c28aaSamw if (vn_openat(name, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0, rootdir, 744da6c28aaSamw -1) != 0) 745ea8dc4b6Seschrock return ((void *)-1UL); 746ea8dc4b6Seschrock 747ea8dc4b6Seschrock file = umem_zalloc(sizeof (struct _buf), UMEM_NOFAIL); 748ea8dc4b6Seschrock file->_fd = (intptr_t)vp; 749ea8dc4b6Seschrock return (file); 750ea8dc4b6Seschrock } 751ea8dc4b6Seschrock 752ea8dc4b6Seschrock int 753ea8dc4b6Seschrock kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off) 754ea8dc4b6Seschrock { 755ea8dc4b6Seschrock ssize_t resid; 756ea8dc4b6Seschrock 757ea8dc4b6Seschrock vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off, 758ea8dc4b6Seschrock UIO_SYSSPACE, 0, 0, 0, &resid); 759ea8dc4b6Seschrock 760b1b8ab34Slling return (size - resid); 761ea8dc4b6Seschrock } 762ea8dc4b6Seschrock 763ea8dc4b6Seschrock void 764ea8dc4b6Seschrock kobj_close_file(struct _buf *file) 765ea8dc4b6Seschrock { 766ea8dc4b6Seschrock vn_close((vnode_t *)file->_fd); 767ea8dc4b6Seschrock umem_free(file, sizeof (struct _buf)); 768ea8dc4b6Seschrock } 769ea8dc4b6Seschrock 770ea8dc4b6Seschrock int 771b1b8ab34Slling kobj_get_filesize(struct _buf *file, uint64_t *size) 772ea8dc4b6Seschrock { 773ea8dc4b6Seschrock struct stat64 st; 774b1b8ab34Slling vnode_t *vp = (vnode_t *)file->_fd; 775b1b8ab34Slling 776ea8dc4b6Seschrock if (fstat64(vp->v_fd, &st) == -1) { 777ea8dc4b6Seschrock vn_close(vp); 778ea8dc4b6Seschrock return (errno); 779ea8dc4b6Seschrock } 780b1b8ab34Slling *size = st.st_size; 781ea8dc4b6Seschrock return (0); 782ea8dc4b6Seschrock } 783ea8dc4b6Seschrock 784ea8dc4b6Seschrock /* 785ea8dc4b6Seschrock * ========================================================================= 786fa9e4066Sahrens * misc routines 787fa9e4066Sahrens * ========================================================================= 788fa9e4066Sahrens */ 789fa9e4066Sahrens 790fa9e4066Sahrens void 791fa9e4066Sahrens delay(clock_t ticks) 792fa9e4066Sahrens { 793fa9e4066Sahrens poll(0, 0, ticks * (1000 / hz)); 794fa9e4066Sahrens } 795fa9e4066Sahrens 796fa9e4066Sahrens /* 797fa9e4066Sahrens * Find highest one bit set. 798fa9e4066Sahrens * Returns bit number + 1 of highest bit that is set, otherwise returns 0. 799fa9e4066Sahrens */ 800fa9e4066Sahrens int 801bf16b11eSMatthew Ahrens highbit64(uint64_t i) 802fa9e4066Sahrens { 803bf16b11eSMatthew Ahrens int h = 1; 804fa9e4066Sahrens 805fa9e4066Sahrens if (i == 0) 806fa9e4066Sahrens return (0); 807bf16b11eSMatthew Ahrens if (i & 0xffffffff00000000ULL) { 808fa9e4066Sahrens h += 32; i >>= 32; 809fa9e4066Sahrens } 810fa9e4066Sahrens if (i & 0xffff0000) { 811fa9e4066Sahrens h += 16; i >>= 16; 812fa9e4066Sahrens } 813fa9e4066Sahrens if (i & 0xff00) { 814fa9e4066Sahrens h += 8; i >>= 8; 815fa9e4066Sahrens } 816fa9e4066Sahrens if (i & 0xf0) { 817fa9e4066Sahrens h += 4; i >>= 4; 818fa9e4066Sahrens } 819fa9e4066Sahrens if (i & 0xc) { 820fa9e4066Sahrens h += 2; i >>= 2; 821fa9e4066Sahrens } 822fa9e4066Sahrens if (i & 0x2) { 823fa9e4066Sahrens h += 1; 824fa9e4066Sahrens } 825fa9e4066Sahrens return (h); 826fa9e4066Sahrens } 827fa9e4066Sahrens 82817f17c2dSbonwick static int random_fd = -1, urandom_fd = -1; 82917f17c2dSbonwick 830fa9e4066Sahrens static int 83117f17c2dSbonwick random_get_bytes_common(uint8_t *ptr, size_t len, int fd) 832fa9e4066Sahrens { 833fa9e4066Sahrens size_t resid = len; 834fa9e4066Sahrens ssize_t bytes; 835fa9e4066Sahrens 836fa9e4066Sahrens ASSERT(fd != -1); 837fa9e4066Sahrens 838fa9e4066Sahrens while (resid != 0) { 839fa9e4066Sahrens bytes = read(fd, ptr, resid); 84017f17c2dSbonwick ASSERT3S(bytes, >=, 0); 841fa9e4066Sahrens ptr += bytes; 842fa9e4066Sahrens resid -= bytes; 843fa9e4066Sahrens } 844fa9e4066Sahrens 845fa9e4066Sahrens return (0); 846fa9e4066Sahrens } 847fa9e4066Sahrens 848fa9e4066Sahrens int 849fa9e4066Sahrens random_get_bytes(uint8_t *ptr, size_t len) 850fa9e4066Sahrens { 85117f17c2dSbonwick return (random_get_bytes_common(ptr, len, random_fd)); 852fa9e4066Sahrens } 853fa9e4066Sahrens 854fa9e4066Sahrens int 855fa9e4066Sahrens random_get_pseudo_bytes(uint8_t *ptr, size_t len) 856fa9e4066Sahrens { 85717f17c2dSbonwick return (random_get_bytes_common(ptr, len, urandom_fd)); 858fa9e4066Sahrens } 859fa9e4066Sahrens 86095173954Sek110237 int 86195173954Sek110237 ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result) 86295173954Sek110237 { 86395173954Sek110237 char *end; 86495173954Sek110237 86595173954Sek110237 *result = strtoul(hw_serial, &end, base); 86695173954Sek110237 if (*result == 0) 86795173954Sek110237 return (errno); 86895173954Sek110237 return (0); 86995173954Sek110237 } 87095173954Sek110237 8710a586ceaSMark Shellenbaum int 8720a586ceaSMark Shellenbaum ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result) 8730a586ceaSMark Shellenbaum { 8740a586ceaSMark Shellenbaum char *end; 8750a586ceaSMark Shellenbaum 8760a586ceaSMark Shellenbaum *result = strtoull(str, &end, base); 8770a586ceaSMark Shellenbaum if (*result == 0) 8780a586ceaSMark Shellenbaum return (errno); 8790a586ceaSMark Shellenbaum return (0); 8800a586ceaSMark Shellenbaum } 8810a586ceaSMark Shellenbaum 882283b8460SGeorge.Wilson /* ARGSUSED */ 883283b8460SGeorge.Wilson cyclic_id_t 884283b8460SGeorge.Wilson cyclic_add(cyc_handler_t *hdlr, cyc_time_t *when) 885283b8460SGeorge.Wilson { 886283b8460SGeorge.Wilson return (1); 887283b8460SGeorge.Wilson } 888283b8460SGeorge.Wilson 889283b8460SGeorge.Wilson /* ARGSUSED */ 890283b8460SGeorge.Wilson void 891283b8460SGeorge.Wilson cyclic_remove(cyclic_id_t id) 892283b8460SGeorge.Wilson { 893283b8460SGeorge.Wilson } 894283b8460SGeorge.Wilson 895283b8460SGeorge.Wilson /* ARGSUSED */ 896283b8460SGeorge.Wilson int 897283b8460SGeorge.Wilson cyclic_reprogram(cyclic_id_t id, hrtime_t expiration) 898283b8460SGeorge.Wilson { 899283b8460SGeorge.Wilson return (1); 900283b8460SGeorge.Wilson } 901283b8460SGeorge.Wilson 902fa9e4066Sahrens /* 903fa9e4066Sahrens * ========================================================================= 904fa9e4066Sahrens * kernel emulation setup & teardown 905fa9e4066Sahrens * ========================================================================= 906fa9e4066Sahrens */ 907fa9e4066Sahrens static int 908fa9e4066Sahrens umem_out_of_memory(void) 909fa9e4066Sahrens { 910fa9e4066Sahrens char errmsg[] = "out of memory -- generating core dump\n"; 911fa9e4066Sahrens 912fa9e4066Sahrens write(fileno(stderr), errmsg, sizeof (errmsg)); 913fa9e4066Sahrens abort(); 914fa9e4066Sahrens return (0); 915fa9e4066Sahrens } 916fa9e4066Sahrens 917fa9e4066Sahrens void 918fa9e4066Sahrens kernel_init(int mode) 919fa9e4066Sahrens { 9203b2aab18SMatthew Ahrens extern uint_t rrw_tsd_key; 9213b2aab18SMatthew Ahrens 922fa9e4066Sahrens umem_nofail_callback(umem_out_of_memory); 923fa9e4066Sahrens 924fa9e4066Sahrens physmem = sysconf(_SC_PHYS_PAGES); 925fa9e4066Sahrens 926fa9e4066Sahrens dprintf("physmem = %llu pages (%.2f GB)\n", physmem, 927fa9e4066Sahrens (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30)); 928fa9e4066Sahrens 9293ad6c7f9SVictor Latushkin (void) snprintf(hw_serial, sizeof (hw_serial), "%ld", 9303ad6c7f9SVictor Latushkin (mode & FWRITE) ? gethostid() : 0); 93195173954Sek110237 93217f17c2dSbonwick VERIFY((random_fd = open("/dev/random", O_RDONLY)) != -1); 93317f17c2dSbonwick VERIFY((urandom_fd = open("/dev/urandom", O_RDONLY)) != -1); 93417f17c2dSbonwick 93588b7b0f2SMatthew Ahrens system_taskq_init(); 93688b7b0f2SMatthew Ahrens 937283b8460SGeorge.Wilson mutex_init(&cpu_lock, NULL, MUTEX_DEFAULT, NULL); 938283b8460SGeorge.Wilson 939fa9e4066Sahrens spa_init(mode); 9403b2aab18SMatthew Ahrens 9413b2aab18SMatthew Ahrens tsd_create(&rrw_tsd_key, rrw_tsd_destroy); 942fa9e4066Sahrens } 943fa9e4066Sahrens 944fa9e4066Sahrens void 945fa9e4066Sahrens kernel_fini(void) 946fa9e4066Sahrens { 947fa9e4066Sahrens spa_fini(); 94817f17c2dSbonwick 949d20e665cSRicardo M. Correia system_taskq_fini(); 950d20e665cSRicardo M. Correia 95117f17c2dSbonwick close(random_fd); 95217f17c2dSbonwick close(urandom_fd); 95317f17c2dSbonwick 95417f17c2dSbonwick random_fd = -1; 95517f17c2dSbonwick urandom_fd = -1; 956fa9e4066Sahrens } 957c9431fa1Sahl 958c9431fa1Sahl int 959c9431fa1Sahl z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen) 960c9431fa1Sahl { 961c9431fa1Sahl int ret; 962c9431fa1Sahl uLongf len = *dstlen; 963c9431fa1Sahl 964c9431fa1Sahl if ((ret = uncompress(dst, &len, src, srclen)) == Z_OK) 965c9431fa1Sahl *dstlen = (size_t)len; 966c9431fa1Sahl 967c9431fa1Sahl return (ret); 968c9431fa1Sahl } 969c9431fa1Sahl 970c9431fa1Sahl int 971c9431fa1Sahl z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen, 972c9431fa1Sahl int level) 973c9431fa1Sahl { 974c9431fa1Sahl int ret; 975c9431fa1Sahl uLongf len = *dstlen; 976c9431fa1Sahl 977c9431fa1Sahl if ((ret = compress2(dst, &len, src, srclen, level)) == Z_OK) 978c9431fa1Sahl *dstlen = (size_t)len; 979c9431fa1Sahl 980c9431fa1Sahl return (ret); 981c9431fa1Sahl } 982ecd6cf80Smarks 983ecd6cf80Smarks uid_t 984ecd6cf80Smarks crgetuid(cred_t *cr) 985ecd6cf80Smarks { 986ecd6cf80Smarks return (0); 987ecd6cf80Smarks } 988ecd6cf80Smarks 9894445fffbSMatthew Ahrens uid_t 9904445fffbSMatthew Ahrens crgetruid(cred_t *cr) 9914445fffbSMatthew Ahrens { 9924445fffbSMatthew Ahrens return (0); 9934445fffbSMatthew Ahrens } 9944445fffbSMatthew Ahrens 995ecd6cf80Smarks gid_t 996ecd6cf80Smarks crgetgid(cred_t *cr) 997ecd6cf80Smarks { 998ecd6cf80Smarks return (0); 999ecd6cf80Smarks } 1000ecd6cf80Smarks 1001ecd6cf80Smarks int 1002ecd6cf80Smarks crgetngroups(cred_t *cr) 1003ecd6cf80Smarks { 1004ecd6cf80Smarks return (0); 1005ecd6cf80Smarks } 1006ecd6cf80Smarks 1007ecd6cf80Smarks gid_t * 1008ecd6cf80Smarks crgetgroups(cred_t *cr) 1009ecd6cf80Smarks { 1010ecd6cf80Smarks return (NULL); 1011ecd6cf80Smarks } 1012ecd6cf80Smarks 1013ecd6cf80Smarks int 1014ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 1015ecd6cf80Smarks { 1016ecd6cf80Smarks return (0); 1017ecd6cf80Smarks } 1018ecd6cf80Smarks 1019ecd6cf80Smarks int 1020ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 1021ecd6cf80Smarks { 1022ecd6cf80Smarks return (0); 1023ecd6cf80Smarks } 1024ecd6cf80Smarks 1025ecd6cf80Smarks int 1026ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 1027ecd6cf80Smarks { 1028ecd6cf80Smarks return (0); 1029ecd6cf80Smarks } 1030e0d35c44Smarks 1031e0d35c44Smarks ksiddomain_t * 1032e0d35c44Smarks ksid_lookupdomain(const char *dom) 1033e0d35c44Smarks { 1034e0d35c44Smarks ksiddomain_t *kd; 1035e0d35c44Smarks 1036e0d35c44Smarks kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL); 1037e0d35c44Smarks kd->kd_name = spa_strdup(dom); 1038e0d35c44Smarks return (kd); 1039e0d35c44Smarks } 1040e0d35c44Smarks 1041e0d35c44Smarks void 1042e0d35c44Smarks ksiddomain_rele(ksiddomain_t *ksid) 1043e0d35c44Smarks { 1044e0d35c44Smarks spa_strfree(ksid->kd_name); 1045e0d35c44Smarks umem_free(ksid, sizeof (ksiddomain_t)); 1046e0d35c44Smarks } 1047ae46e4c7SMatthew Ahrens 1048ae46e4c7SMatthew Ahrens /* 1049ae46e4c7SMatthew Ahrens * Do not change the length of the returned string; it must be freed 1050ae46e4c7SMatthew Ahrens * with strfree(). 1051ae46e4c7SMatthew Ahrens */ 1052ae46e4c7SMatthew Ahrens char * 1053ae46e4c7SMatthew Ahrens kmem_asprintf(const char *fmt, ...) 1054ae46e4c7SMatthew Ahrens { 1055ae46e4c7SMatthew Ahrens int size; 1056ae46e4c7SMatthew Ahrens va_list adx; 1057ae46e4c7SMatthew Ahrens char *buf; 1058ae46e4c7SMatthew Ahrens 1059ae46e4c7SMatthew Ahrens va_start(adx, fmt); 1060ae46e4c7SMatthew Ahrens size = vsnprintf(NULL, 0, fmt, adx) + 1; 1061ae46e4c7SMatthew Ahrens va_end(adx); 1062ae46e4c7SMatthew Ahrens 1063ae46e4c7SMatthew Ahrens buf = kmem_alloc(size, KM_SLEEP); 1064ae46e4c7SMatthew Ahrens 1065ae46e4c7SMatthew Ahrens va_start(adx, fmt); 1066ae46e4c7SMatthew Ahrens size = vsnprintf(buf, size, fmt, adx); 1067ae46e4c7SMatthew Ahrens va_end(adx); 1068ae46e4c7SMatthew Ahrens 1069ae46e4c7SMatthew Ahrens return (buf); 1070ae46e4c7SMatthew Ahrens } 1071c99e4bdcSChris Kirby 1072c99e4bdcSChris Kirby /* ARGSUSED */ 1073c99e4bdcSChris Kirby int 1074a7f53a56SChris Kirby zfs_onexit_fd_hold(int fd, minor_t *minorp) 1075a7f53a56SChris Kirby { 1076a7f53a56SChris Kirby *minorp = 0; 1077a7f53a56SChris Kirby return (0); 1078a7f53a56SChris Kirby } 1079a7f53a56SChris Kirby 1080a7f53a56SChris Kirby /* ARGSUSED */ 1081a7f53a56SChris Kirby void 1082a7f53a56SChris Kirby zfs_onexit_fd_rele(int fd) 1083a7f53a56SChris Kirby { 1084a7f53a56SChris Kirby } 1085a7f53a56SChris Kirby 1086a7f53a56SChris Kirby /* ARGSUSED */ 1087a7f53a56SChris Kirby int 1088a7f53a56SChris Kirby zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data, 1089c99e4bdcSChris Kirby uint64_t *action_handle) 1090c99e4bdcSChris Kirby { 1091c99e4bdcSChris Kirby return (0); 1092c99e4bdcSChris Kirby } 1093c99e4bdcSChris Kirby 1094c99e4bdcSChris Kirby /* ARGSUSED */ 1095c99e4bdcSChris Kirby int 1096a7f53a56SChris Kirby zfs_onexit_del_cb(minor_t minor, uint64_t action_handle, boolean_t fire) 1097c99e4bdcSChris Kirby { 1098c99e4bdcSChris Kirby return (0); 1099c99e4bdcSChris Kirby } 1100c99e4bdcSChris Kirby 1101c99e4bdcSChris Kirby /* ARGSUSED */ 1102c99e4bdcSChris Kirby int 1103a7f53a56SChris Kirby zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data) 1104c99e4bdcSChris Kirby { 1105c99e4bdcSChris Kirby return (0); 1106c99e4bdcSChris Kirby } 110731d7e8faSGeorge Wilson 110831d7e8faSGeorge Wilson void 110931d7e8faSGeorge Wilson bioinit(buf_t *bp) 111031d7e8faSGeorge Wilson { 111131d7e8faSGeorge Wilson bzero(bp, sizeof (buf_t)); 111231d7e8faSGeorge Wilson } 111331d7e8faSGeorge Wilson 111431d7e8faSGeorge Wilson void 111531d7e8faSGeorge Wilson biodone(buf_t *bp) 111631d7e8faSGeorge Wilson { 111731d7e8faSGeorge Wilson if (bp->b_iodone != NULL) { 111831d7e8faSGeorge Wilson (*(bp->b_iodone))(bp); 111931d7e8faSGeorge Wilson return; 112031d7e8faSGeorge Wilson } 112131d7e8faSGeorge Wilson ASSERT((bp->b_flags & B_DONE) == 0); 112231d7e8faSGeorge Wilson bp->b_flags |= B_DONE; 112331d7e8faSGeorge Wilson } 112431d7e8faSGeorge Wilson 112531d7e8faSGeorge Wilson void 112631d7e8faSGeorge Wilson bioerror(buf_t *bp, int error) 112731d7e8faSGeorge Wilson { 112831d7e8faSGeorge Wilson ASSERT(bp != NULL); 112931d7e8faSGeorge Wilson ASSERT(error >= 0); 113031d7e8faSGeorge Wilson 113131d7e8faSGeorge Wilson if (error != 0) { 113231d7e8faSGeorge Wilson bp->b_flags |= B_ERROR; 113331d7e8faSGeorge Wilson } else { 113431d7e8faSGeorge Wilson bp->b_flags &= ~B_ERROR; 113531d7e8faSGeorge Wilson } 113631d7e8faSGeorge Wilson bp->b_error = error; 113731d7e8faSGeorge Wilson } 113831d7e8faSGeorge Wilson 113931d7e8faSGeorge Wilson 114031d7e8faSGeorge Wilson int 114131d7e8faSGeorge Wilson geterror(struct buf *bp) 114231d7e8faSGeorge Wilson { 114331d7e8faSGeorge Wilson int error = 0; 114431d7e8faSGeorge Wilson 114531d7e8faSGeorge Wilson if (bp->b_flags & B_ERROR) { 114631d7e8faSGeorge Wilson error = bp->b_error; 114731d7e8faSGeorge Wilson if (!error) 114831d7e8faSGeorge Wilson error = EIO; 114931d7e8faSGeorge Wilson } 115031d7e8faSGeorge Wilson return (error); 115131d7e8faSGeorge Wilson } 1152