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. 23*9a686fbcSPaul Dagnelie * Copyright (c) 2012, 2015 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> 34df15e419SMatthew 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 55df15e419SMatthew Ahrens /* If set, all blocks read will be copied to the specified directory. */ 56df15e419SMatthew Ahrens char *vn_dumpdir = NULL; 57df15e419SMatthew 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 1590d045c0dSRobert Mustacchi zmutex_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 1840d045c0dSRobert Mustacchi zmutex_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; 401df15e419SMatthew 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 450df15e419SMatthew Ahrens if (vn_dumpdir != NULL) { 451df15e419SMatthew Ahrens char dumppath[MAXPATHLEN]; 452df15e419SMatthew Ahrens (void) snprintf(dumppath, sizeof (dumppath), 453df15e419SMatthew Ahrens "%s/%s", vn_dumpdir, basename(realpath)); 454df15e419SMatthew Ahrens dump_fd = open64(dumppath, O_CREAT | O_WRONLY, 0666); 455df15e419SMatthew Ahrens if (dump_fd == -1) 456df15e419SMatthew Ahrens return (errno); 457df15e419SMatthew Ahrens } else { 458df15e419SMatthew Ahrens dump_fd = -1; 459df15e419SMatthew Ahrens } 460df15e419SMatthew 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); 476df15e419SMatthew 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); 509df15e419SMatthew Ahrens if (vp->v_dump_fd != -1) { 510df15e419SMatthew Ahrens int status = 511df15e419SMatthew Ahrens pwrite64(vp->v_dump_fd, addr, iolen, offset); 512df15e419SMatthew Ahrens ASSERT(status != -1); 513df15e419SMatthew 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); 540df15e419SMatthew Ahrens if (vp->v_dump_fd != -1) 541df15e419SMatthew 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; 6327fa49ea5SMatthew Ahrens 6337fa49ea5SMatthew Ahrens if (dprintf_string != NULL) 6347fa49ea5SMatthew Ahrens zfs_flags |= ZFS_DEBUG_DPRINTF; 635fa9e4066Sahrens } 636fa9e4066Sahrens 637fa9e4066Sahrens /* 638fa9e4066Sahrens * ========================================================================= 639fa9e4066Sahrens * debug printfs 640fa9e4066Sahrens * ========================================================================= 641fa9e4066Sahrens */ 642fa9e4066Sahrens void 643fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...) 644fa9e4066Sahrens { 645fa9e4066Sahrens const char *newfile; 646fa9e4066Sahrens va_list adx; 647fa9e4066Sahrens 648fa9e4066Sahrens /* 649fa9e4066Sahrens * Get rid of annoying "../common/" prefix to filename. 650fa9e4066Sahrens */ 651fa9e4066Sahrens newfile = strrchr(file, '/'); 652fa9e4066Sahrens if (newfile != NULL) { 653fa9e4066Sahrens newfile = newfile + 1; /* Get rid of leading / */ 654fa9e4066Sahrens } else { 655fa9e4066Sahrens newfile = file; 656fa9e4066Sahrens } 657fa9e4066Sahrens 658fa9e4066Sahrens if (dprintf_print_all || 659fa9e4066Sahrens dprintf_find_string(newfile) || 660fa9e4066Sahrens dprintf_find_string(func)) { 661fa9e4066Sahrens /* Print out just the function name if requested */ 662fa9e4066Sahrens flockfile(stdout); 663fa9e4066Sahrens if (dprintf_find_string("pid")) 664fa9e4066Sahrens (void) printf("%d ", getpid()); 665fa9e4066Sahrens if (dprintf_find_string("tid")) 666fa9e4066Sahrens (void) printf("%u ", thr_self()); 667fa9e4066Sahrens if (dprintf_find_string("cpu")) 668fa9e4066Sahrens (void) printf("%u ", getcpuid()); 669fa9e4066Sahrens if (dprintf_find_string("time")) 670fa9e4066Sahrens (void) printf("%llu ", gethrtime()); 671fa9e4066Sahrens if (dprintf_find_string("long")) 672fa9e4066Sahrens (void) printf("%s, line %d: ", newfile, line); 673fa9e4066Sahrens (void) printf("%s: ", func); 674fa9e4066Sahrens va_start(adx, fmt); 675fa9e4066Sahrens (void) vprintf(fmt, adx); 676fa9e4066Sahrens va_end(adx); 677fa9e4066Sahrens funlockfile(stdout); 678fa9e4066Sahrens } 679fa9e4066Sahrens } 680fa9e4066Sahrens 681fa9e4066Sahrens #endif /* ZFS_DEBUG */ 682fa9e4066Sahrens 683fa9e4066Sahrens /* 684fa9e4066Sahrens * ========================================================================= 685fa9e4066Sahrens * cmn_err() and panic() 686fa9e4066Sahrens * ========================================================================= 687fa9e4066Sahrens */ 688fa9e4066Sahrens static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" }; 689fa9e4066Sahrens static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" }; 690fa9e4066Sahrens 691fa9e4066Sahrens void 692fa9e4066Sahrens vpanic(const char *fmt, va_list adx) 693fa9e4066Sahrens { 694fae63477SPrakash Surya char buf[512]; 695fae63477SPrakash Surya (void) vsnprintf(buf, 512, fmt, adx); 696fae63477SPrakash Surya assfail(buf, NULL, 0); 697fa9e4066Sahrens } 698fa9e4066Sahrens 699fa9e4066Sahrens void 700fa9e4066Sahrens panic(const char *fmt, ...) 701fa9e4066Sahrens { 702fa9e4066Sahrens va_list adx; 703fa9e4066Sahrens 704fa9e4066Sahrens va_start(adx, fmt); 705fa9e4066Sahrens vpanic(fmt, adx); 706fa9e4066Sahrens va_end(adx); 707fa9e4066Sahrens } 708fa9e4066Sahrens 709fa9e4066Sahrens void 7100125049cSahrens vcmn_err(int ce, const char *fmt, va_list adx) 711fa9e4066Sahrens { 712fa9e4066Sahrens if (ce == CE_PANIC) 713fa9e4066Sahrens vpanic(fmt, adx); 714fa9e4066Sahrens if (ce != CE_NOTE) { /* suppress noise in userland stress testing */ 715fa9e4066Sahrens (void) fprintf(stderr, "%s", ce_prefix[ce]); 716fa9e4066Sahrens (void) vfprintf(stderr, fmt, adx); 717fa9e4066Sahrens (void) fprintf(stderr, "%s", ce_suffix[ce]); 718fa9e4066Sahrens } 7190125049cSahrens } 7200125049cSahrens 7210125049cSahrens /*PRINTFLIKE2*/ 7220125049cSahrens void 7230125049cSahrens cmn_err(int ce, const char *fmt, ...) 7240125049cSahrens { 7250125049cSahrens va_list adx; 7260125049cSahrens 7270125049cSahrens va_start(adx, fmt); 7280125049cSahrens vcmn_err(ce, fmt, adx); 729fa9e4066Sahrens va_end(adx); 730fa9e4066Sahrens } 731fa9e4066Sahrens 732fa9e4066Sahrens /* 733fa9e4066Sahrens * ========================================================================= 734ea8dc4b6Seschrock * kobj interfaces 735ea8dc4b6Seschrock * ========================================================================= 736ea8dc4b6Seschrock */ 737ea8dc4b6Seschrock struct _buf * 738ea8dc4b6Seschrock kobj_open_file(char *name) 739ea8dc4b6Seschrock { 740ea8dc4b6Seschrock struct _buf *file; 741ea8dc4b6Seschrock vnode_t *vp; 742ea8dc4b6Seschrock 743ea8dc4b6Seschrock /* set vp as the _fd field of the file */ 744da6c28aaSamw if (vn_openat(name, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0, rootdir, 745da6c28aaSamw -1) != 0) 746ea8dc4b6Seschrock return ((void *)-1UL); 747ea8dc4b6Seschrock 748ea8dc4b6Seschrock file = umem_zalloc(sizeof (struct _buf), UMEM_NOFAIL); 749ea8dc4b6Seschrock file->_fd = (intptr_t)vp; 750ea8dc4b6Seschrock return (file); 751ea8dc4b6Seschrock } 752ea8dc4b6Seschrock 753ea8dc4b6Seschrock int 754ea8dc4b6Seschrock kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off) 755ea8dc4b6Seschrock { 756ea8dc4b6Seschrock ssize_t resid; 757ea8dc4b6Seschrock 758ea8dc4b6Seschrock vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off, 759ea8dc4b6Seschrock UIO_SYSSPACE, 0, 0, 0, &resid); 760ea8dc4b6Seschrock 761b1b8ab34Slling return (size - resid); 762ea8dc4b6Seschrock } 763ea8dc4b6Seschrock 764ea8dc4b6Seschrock void 765ea8dc4b6Seschrock kobj_close_file(struct _buf *file) 766ea8dc4b6Seschrock { 767ea8dc4b6Seschrock vn_close((vnode_t *)file->_fd); 768ea8dc4b6Seschrock umem_free(file, sizeof (struct _buf)); 769ea8dc4b6Seschrock } 770ea8dc4b6Seschrock 771ea8dc4b6Seschrock int 772b1b8ab34Slling kobj_get_filesize(struct _buf *file, uint64_t *size) 773ea8dc4b6Seschrock { 774ea8dc4b6Seschrock struct stat64 st; 775b1b8ab34Slling vnode_t *vp = (vnode_t *)file->_fd; 776b1b8ab34Slling 777ea8dc4b6Seschrock if (fstat64(vp->v_fd, &st) == -1) { 778ea8dc4b6Seschrock vn_close(vp); 779ea8dc4b6Seschrock return (errno); 780ea8dc4b6Seschrock } 781b1b8ab34Slling *size = st.st_size; 782ea8dc4b6Seschrock return (0); 783ea8dc4b6Seschrock } 784ea8dc4b6Seschrock 785ea8dc4b6Seschrock /* 786ea8dc4b6Seschrock * ========================================================================= 787fa9e4066Sahrens * misc routines 788fa9e4066Sahrens * ========================================================================= 789fa9e4066Sahrens */ 790fa9e4066Sahrens 791fa9e4066Sahrens void 792fa9e4066Sahrens delay(clock_t ticks) 793fa9e4066Sahrens { 794fa9e4066Sahrens poll(0, 0, ticks * (1000 / hz)); 795fa9e4066Sahrens } 796fa9e4066Sahrens 797fa9e4066Sahrens /* 798fa9e4066Sahrens * Find highest one bit set. 799fa9e4066Sahrens * Returns bit number + 1 of highest bit that is set, otherwise returns 0. 800fa9e4066Sahrens */ 801fa9e4066Sahrens int 802bf16b11eSMatthew Ahrens highbit64(uint64_t i) 803fa9e4066Sahrens { 804bf16b11eSMatthew Ahrens int h = 1; 805fa9e4066Sahrens 806fa9e4066Sahrens if (i == 0) 807fa9e4066Sahrens return (0); 808bf16b11eSMatthew Ahrens if (i & 0xffffffff00000000ULL) { 809fa9e4066Sahrens h += 32; i >>= 32; 810fa9e4066Sahrens } 811fa9e4066Sahrens if (i & 0xffff0000) { 812fa9e4066Sahrens h += 16; i >>= 16; 813fa9e4066Sahrens } 814fa9e4066Sahrens if (i & 0xff00) { 815fa9e4066Sahrens h += 8; i >>= 8; 816fa9e4066Sahrens } 817fa9e4066Sahrens if (i & 0xf0) { 818fa9e4066Sahrens h += 4; i >>= 4; 819fa9e4066Sahrens } 820fa9e4066Sahrens if (i & 0xc) { 821fa9e4066Sahrens h += 2; i >>= 2; 822fa9e4066Sahrens } 823fa9e4066Sahrens if (i & 0x2) { 824fa9e4066Sahrens h += 1; 825fa9e4066Sahrens } 826fa9e4066Sahrens return (h); 827fa9e4066Sahrens } 828fa9e4066Sahrens 82917f17c2dSbonwick static int random_fd = -1, urandom_fd = -1; 83017f17c2dSbonwick 831fa9e4066Sahrens static int 83217f17c2dSbonwick random_get_bytes_common(uint8_t *ptr, size_t len, int fd) 833fa9e4066Sahrens { 834fa9e4066Sahrens size_t resid = len; 835fa9e4066Sahrens ssize_t bytes; 836fa9e4066Sahrens 837fa9e4066Sahrens ASSERT(fd != -1); 838fa9e4066Sahrens 839fa9e4066Sahrens while (resid != 0) { 840fa9e4066Sahrens bytes = read(fd, ptr, resid); 84117f17c2dSbonwick ASSERT3S(bytes, >=, 0); 842fa9e4066Sahrens ptr += bytes; 843fa9e4066Sahrens resid -= bytes; 844fa9e4066Sahrens } 845fa9e4066Sahrens 846fa9e4066Sahrens return (0); 847fa9e4066Sahrens } 848fa9e4066Sahrens 849fa9e4066Sahrens int 850fa9e4066Sahrens random_get_bytes(uint8_t *ptr, size_t len) 851fa9e4066Sahrens { 85217f17c2dSbonwick return (random_get_bytes_common(ptr, len, random_fd)); 853fa9e4066Sahrens } 854fa9e4066Sahrens 855fa9e4066Sahrens int 856fa9e4066Sahrens random_get_pseudo_bytes(uint8_t *ptr, size_t len) 857fa9e4066Sahrens { 85817f17c2dSbonwick return (random_get_bytes_common(ptr, len, urandom_fd)); 859fa9e4066Sahrens } 860fa9e4066Sahrens 86195173954Sek110237 int 86295173954Sek110237 ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result) 86395173954Sek110237 { 86495173954Sek110237 char *end; 86595173954Sek110237 86695173954Sek110237 *result = strtoul(hw_serial, &end, base); 86795173954Sek110237 if (*result == 0) 86895173954Sek110237 return (errno); 86995173954Sek110237 return (0); 87095173954Sek110237 } 87195173954Sek110237 8720a586ceaSMark Shellenbaum int 8730a586ceaSMark Shellenbaum ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result) 8740a586ceaSMark Shellenbaum { 8750a586ceaSMark Shellenbaum char *end; 8760a586ceaSMark Shellenbaum 8770a586ceaSMark Shellenbaum *result = strtoull(str, &end, base); 8780a586ceaSMark Shellenbaum if (*result == 0) 8790a586ceaSMark Shellenbaum return (errno); 8800a586ceaSMark Shellenbaum return (0); 8810a586ceaSMark Shellenbaum } 8820a586ceaSMark Shellenbaum 883283b8460SGeorge.Wilson /* ARGSUSED */ 884283b8460SGeorge.Wilson cyclic_id_t 885283b8460SGeorge.Wilson cyclic_add(cyc_handler_t *hdlr, cyc_time_t *when) 886283b8460SGeorge.Wilson { 887283b8460SGeorge.Wilson return (1); 888283b8460SGeorge.Wilson } 889283b8460SGeorge.Wilson 890283b8460SGeorge.Wilson /* ARGSUSED */ 891283b8460SGeorge.Wilson void 892283b8460SGeorge.Wilson cyclic_remove(cyclic_id_t id) 893283b8460SGeorge.Wilson { 894283b8460SGeorge.Wilson } 895283b8460SGeorge.Wilson 896283b8460SGeorge.Wilson /* ARGSUSED */ 897283b8460SGeorge.Wilson int 898283b8460SGeorge.Wilson cyclic_reprogram(cyclic_id_t id, hrtime_t expiration) 899283b8460SGeorge.Wilson { 900283b8460SGeorge.Wilson return (1); 901283b8460SGeorge.Wilson } 902283b8460SGeorge.Wilson 903fa9e4066Sahrens /* 904fa9e4066Sahrens * ========================================================================= 905fa9e4066Sahrens * kernel emulation setup & teardown 906fa9e4066Sahrens * ========================================================================= 907fa9e4066Sahrens */ 908fa9e4066Sahrens static int 909fa9e4066Sahrens umem_out_of_memory(void) 910fa9e4066Sahrens { 911fa9e4066Sahrens char errmsg[] = "out of memory -- generating core dump\n"; 912fa9e4066Sahrens 913fa9e4066Sahrens write(fileno(stderr), errmsg, sizeof (errmsg)); 914fa9e4066Sahrens abort(); 915fa9e4066Sahrens return (0); 916fa9e4066Sahrens } 917fa9e4066Sahrens 918fa9e4066Sahrens void 919fa9e4066Sahrens kernel_init(int mode) 920fa9e4066Sahrens { 9213b2aab18SMatthew Ahrens extern uint_t rrw_tsd_key; 9223b2aab18SMatthew Ahrens 923fa9e4066Sahrens umem_nofail_callback(umem_out_of_memory); 924fa9e4066Sahrens 925fa9e4066Sahrens physmem = sysconf(_SC_PHYS_PAGES); 926fa9e4066Sahrens 927fa9e4066Sahrens dprintf("physmem = %llu pages (%.2f GB)\n", physmem, 928fa9e4066Sahrens (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30)); 929fa9e4066Sahrens 9303ad6c7f9SVictor Latushkin (void) snprintf(hw_serial, sizeof (hw_serial), "%ld", 9313ad6c7f9SVictor Latushkin (mode & FWRITE) ? gethostid() : 0); 93295173954Sek110237 93317f17c2dSbonwick VERIFY((random_fd = open("/dev/random", O_RDONLY)) != -1); 93417f17c2dSbonwick VERIFY((urandom_fd = open("/dev/urandom", O_RDONLY)) != -1); 93517f17c2dSbonwick 93688b7b0f2SMatthew Ahrens system_taskq_init(); 93788b7b0f2SMatthew Ahrens 938283b8460SGeorge.Wilson mutex_init(&cpu_lock, NULL, MUTEX_DEFAULT, NULL); 939283b8460SGeorge.Wilson 940fa9e4066Sahrens spa_init(mode); 9413b2aab18SMatthew Ahrens 9423b2aab18SMatthew Ahrens tsd_create(&rrw_tsd_key, rrw_tsd_destroy); 943fa9e4066Sahrens } 944fa9e4066Sahrens 945fa9e4066Sahrens void 946fa9e4066Sahrens kernel_fini(void) 947fa9e4066Sahrens { 948fa9e4066Sahrens spa_fini(); 94917f17c2dSbonwick 950d20e665cSRicardo M. Correia system_taskq_fini(); 951d20e665cSRicardo M. Correia 95217f17c2dSbonwick close(random_fd); 95317f17c2dSbonwick close(urandom_fd); 95417f17c2dSbonwick 95517f17c2dSbonwick random_fd = -1; 95617f17c2dSbonwick urandom_fd = -1; 957fa9e4066Sahrens } 958c9431fa1Sahl 959c9431fa1Sahl int 960c9431fa1Sahl z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen) 961c9431fa1Sahl { 962c9431fa1Sahl int ret; 963c9431fa1Sahl uLongf len = *dstlen; 964c9431fa1Sahl 965c9431fa1Sahl if ((ret = uncompress(dst, &len, src, srclen)) == Z_OK) 966c9431fa1Sahl *dstlen = (size_t)len; 967c9431fa1Sahl 968c9431fa1Sahl return (ret); 969c9431fa1Sahl } 970c9431fa1Sahl 971c9431fa1Sahl int 972c9431fa1Sahl z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen, 973c9431fa1Sahl int level) 974c9431fa1Sahl { 975c9431fa1Sahl int ret; 976c9431fa1Sahl uLongf len = *dstlen; 977c9431fa1Sahl 978c9431fa1Sahl if ((ret = compress2(dst, &len, src, srclen, level)) == Z_OK) 979c9431fa1Sahl *dstlen = (size_t)len; 980c9431fa1Sahl 981c9431fa1Sahl return (ret); 982c9431fa1Sahl } 983ecd6cf80Smarks 984ecd6cf80Smarks uid_t 985ecd6cf80Smarks crgetuid(cred_t *cr) 986ecd6cf80Smarks { 987ecd6cf80Smarks return (0); 988ecd6cf80Smarks } 989ecd6cf80Smarks 9904445fffbSMatthew Ahrens uid_t 9914445fffbSMatthew Ahrens crgetruid(cred_t *cr) 9924445fffbSMatthew Ahrens { 9934445fffbSMatthew Ahrens return (0); 9944445fffbSMatthew Ahrens } 9954445fffbSMatthew Ahrens 996ecd6cf80Smarks gid_t 997ecd6cf80Smarks crgetgid(cred_t *cr) 998ecd6cf80Smarks { 999ecd6cf80Smarks return (0); 1000ecd6cf80Smarks } 1001ecd6cf80Smarks 1002ecd6cf80Smarks int 1003ecd6cf80Smarks crgetngroups(cred_t *cr) 1004ecd6cf80Smarks { 1005ecd6cf80Smarks return (0); 1006ecd6cf80Smarks } 1007ecd6cf80Smarks 1008ecd6cf80Smarks gid_t * 1009ecd6cf80Smarks crgetgroups(cred_t *cr) 1010ecd6cf80Smarks { 1011ecd6cf80Smarks return (NULL); 1012ecd6cf80Smarks } 1013ecd6cf80Smarks 1014ecd6cf80Smarks int 1015ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 1016ecd6cf80Smarks { 1017ecd6cf80Smarks return (0); 1018ecd6cf80Smarks } 1019ecd6cf80Smarks 1020ecd6cf80Smarks int 1021ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 1022ecd6cf80Smarks { 1023ecd6cf80Smarks return (0); 1024ecd6cf80Smarks } 1025ecd6cf80Smarks 1026ecd6cf80Smarks int 1027ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 1028ecd6cf80Smarks { 1029ecd6cf80Smarks return (0); 1030ecd6cf80Smarks } 1031e0d35c44Smarks 1032e0d35c44Smarks ksiddomain_t * 1033e0d35c44Smarks ksid_lookupdomain(const char *dom) 1034e0d35c44Smarks { 1035e0d35c44Smarks ksiddomain_t *kd; 1036e0d35c44Smarks 1037e0d35c44Smarks kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL); 1038e0d35c44Smarks kd->kd_name = spa_strdup(dom); 1039e0d35c44Smarks return (kd); 1040e0d35c44Smarks } 1041e0d35c44Smarks 1042e0d35c44Smarks void 1043e0d35c44Smarks ksiddomain_rele(ksiddomain_t *ksid) 1044e0d35c44Smarks { 1045e0d35c44Smarks spa_strfree(ksid->kd_name); 1046e0d35c44Smarks umem_free(ksid, sizeof (ksiddomain_t)); 1047e0d35c44Smarks } 1048ae46e4c7SMatthew Ahrens 1049ae46e4c7SMatthew Ahrens /* 1050ae46e4c7SMatthew Ahrens * Do not change the length of the returned string; it must be freed 1051ae46e4c7SMatthew Ahrens * with strfree(). 1052ae46e4c7SMatthew Ahrens */ 1053ae46e4c7SMatthew Ahrens char * 1054ae46e4c7SMatthew Ahrens kmem_asprintf(const char *fmt, ...) 1055ae46e4c7SMatthew Ahrens { 1056ae46e4c7SMatthew Ahrens int size; 1057ae46e4c7SMatthew Ahrens va_list adx; 1058ae46e4c7SMatthew Ahrens char *buf; 1059ae46e4c7SMatthew Ahrens 1060ae46e4c7SMatthew Ahrens va_start(adx, fmt); 1061ae46e4c7SMatthew Ahrens size = vsnprintf(NULL, 0, fmt, adx) + 1; 1062ae46e4c7SMatthew Ahrens va_end(adx); 1063ae46e4c7SMatthew Ahrens 1064ae46e4c7SMatthew Ahrens buf = kmem_alloc(size, KM_SLEEP); 1065ae46e4c7SMatthew Ahrens 1066ae46e4c7SMatthew Ahrens va_start(adx, fmt); 1067ae46e4c7SMatthew Ahrens size = vsnprintf(buf, size, fmt, adx); 1068ae46e4c7SMatthew Ahrens va_end(adx); 1069ae46e4c7SMatthew Ahrens 1070ae46e4c7SMatthew Ahrens return (buf); 1071ae46e4c7SMatthew Ahrens } 1072c99e4bdcSChris Kirby 1073c99e4bdcSChris Kirby /* ARGSUSED */ 1074c99e4bdcSChris Kirby int 1075a7f53a56SChris Kirby zfs_onexit_fd_hold(int fd, minor_t *minorp) 1076a7f53a56SChris Kirby { 1077a7f53a56SChris Kirby *minorp = 0; 1078a7f53a56SChris Kirby return (0); 1079a7f53a56SChris Kirby } 1080a7f53a56SChris Kirby 1081a7f53a56SChris Kirby /* ARGSUSED */ 1082a7f53a56SChris Kirby void 1083a7f53a56SChris Kirby zfs_onexit_fd_rele(int fd) 1084a7f53a56SChris Kirby { 1085a7f53a56SChris Kirby } 1086a7f53a56SChris Kirby 1087a7f53a56SChris Kirby /* ARGSUSED */ 1088a7f53a56SChris Kirby int 1089a7f53a56SChris Kirby zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data, 1090c99e4bdcSChris Kirby uint64_t *action_handle) 1091c99e4bdcSChris Kirby { 1092c99e4bdcSChris Kirby return (0); 1093c99e4bdcSChris Kirby } 1094c99e4bdcSChris Kirby 1095c99e4bdcSChris Kirby /* ARGSUSED */ 1096c99e4bdcSChris Kirby int 1097a7f53a56SChris Kirby zfs_onexit_del_cb(minor_t minor, uint64_t action_handle, boolean_t fire) 1098c99e4bdcSChris Kirby { 1099c99e4bdcSChris Kirby return (0); 1100c99e4bdcSChris Kirby } 1101c99e4bdcSChris Kirby 1102c99e4bdcSChris Kirby /* ARGSUSED */ 1103c99e4bdcSChris Kirby int 1104a7f53a56SChris Kirby zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data) 1105c99e4bdcSChris Kirby { 1106c99e4bdcSChris Kirby return (0); 1107c99e4bdcSChris Kirby } 110831d7e8faSGeorge Wilson 110931d7e8faSGeorge Wilson void 111031d7e8faSGeorge Wilson bioinit(buf_t *bp) 111131d7e8faSGeorge Wilson { 111231d7e8faSGeorge Wilson bzero(bp, sizeof (buf_t)); 111331d7e8faSGeorge Wilson } 111431d7e8faSGeorge Wilson 111531d7e8faSGeorge Wilson void 111631d7e8faSGeorge Wilson biodone(buf_t *bp) 111731d7e8faSGeorge Wilson { 111831d7e8faSGeorge Wilson if (bp->b_iodone != NULL) { 111931d7e8faSGeorge Wilson (*(bp->b_iodone))(bp); 112031d7e8faSGeorge Wilson return; 112131d7e8faSGeorge Wilson } 112231d7e8faSGeorge Wilson ASSERT((bp->b_flags & B_DONE) == 0); 112331d7e8faSGeorge Wilson bp->b_flags |= B_DONE; 112431d7e8faSGeorge Wilson } 112531d7e8faSGeorge Wilson 112631d7e8faSGeorge Wilson void 112731d7e8faSGeorge Wilson bioerror(buf_t *bp, int error) 112831d7e8faSGeorge Wilson { 112931d7e8faSGeorge Wilson ASSERT(bp != NULL); 113031d7e8faSGeorge Wilson ASSERT(error >= 0); 113131d7e8faSGeorge Wilson 113231d7e8faSGeorge Wilson if (error != 0) { 113331d7e8faSGeorge Wilson bp->b_flags |= B_ERROR; 113431d7e8faSGeorge Wilson } else { 113531d7e8faSGeorge Wilson bp->b_flags &= ~B_ERROR; 113631d7e8faSGeorge Wilson } 113731d7e8faSGeorge Wilson bp->b_error = error; 113831d7e8faSGeorge Wilson } 113931d7e8faSGeorge Wilson 114031d7e8faSGeorge Wilson 114131d7e8faSGeorge Wilson int 114231d7e8faSGeorge Wilson geterror(struct buf *bp) 114331d7e8faSGeorge Wilson { 114431d7e8faSGeorge Wilson int error = 0; 114531d7e8faSGeorge Wilson 114631d7e8faSGeorge Wilson if (bp->b_flags & B_ERROR) { 114731d7e8faSGeorge Wilson error = bp->b_error; 114831d7e8faSGeorge Wilson if (!error) 114931d7e8faSGeorge Wilson error = EIO; 115031d7e8faSGeorge Wilson } 115131d7e8faSGeorge Wilson return (error); 115231d7e8faSGeorge Wilson } 1153