]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libzpool/kernel.c
Fix flake 8 style warnings
[mirror_zfs.git] / lib / libzpool / kernel.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
572e2857 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
a0bd735a 23 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
34dc7c2f
BB
24 */
25
34dc7c2f
BB
26#include <assert.h>
27#include <fcntl.h>
28#include <poll.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <zlib.h>
9867e8be 33#include <libgen.h>
1e33ac1e 34#include <sys/signal.h>
34dc7c2f
BB
35#include <sys/spa.h>
36#include <sys/stat.h>
37#include <sys/processor.h>
38#include <sys/zfs_context.h>
13fe0198 39#include <sys/rrwlock.h>
34dc7c2f 40#include <sys/utsname.h>
d603ed6c 41#include <sys/time.h>
d164b209 42#include <sys/systeminfo.h>
1eeb4562 43#include <zfs_fletcher.h>
0b04990a 44#include <sys/crypto/icp.h>
34dc7c2f
BB
45
46/*
47 * Emulation of kernel services in userland.
48 */
49
428870ff 50int aok;
34dc7c2f
BB
51uint64_t physmem;
52vnode_t *rootdir = (vnode_t *)0xabcd1234;
d164b209 53char hw_serial[HW_HOSTID_LEN];
f0e324f2 54struct utsname hw_utsname;
ca67b33a 55vmem_t *zio_arena = NULL;
34dc7c2f 56
9867e8be
MA
57/* If set, all blocks read will be copied to the specified directory. */
58char *vn_dumpdir = NULL;
59
428870ff
BB
60/* this only exists to have its address taken */
61struct proc p0;
62
34dc7c2f
BB
63/*
64 * =========================================================================
65 * threads
66 * =========================================================================
c25b8f99
BB
67 *
68 * TS_STACK_MIN is dictated by the minimum allowed pthread stack size. While
69 * TS_STACK_MAX is somewhat arbitrary, it was selected to be large enough for
70 * the expected stack depth while small enough to avoid exhausting address
71 * space with high thread counts.
34dc7c2f 72 */
c25b8f99
BB
73#define TS_STACK_MIN MAX(PTHREAD_STACK_MIN, 32768)
74#define TS_STACK_MAX (256 * 1024)
1e33ac1e 75
c25b8f99 76/*ARGSUSED*/
1e33ac1e 77kthread_t *
c25b8f99 78zk_thread_create(void (*func)(void *), void *arg, size_t stksize, int state)
1e33ac1e 79{
1e33ac1e 80 pthread_attr_t attr;
c25b8f99 81 pthread_t tid;
aa0ac7ca 82 char *stkstr;
c25b8f99 83 int detachstate = PTHREAD_CREATE_DETACHED;
1e33ac1e 84
c25b8f99 85 VERIFY0(pthread_attr_init(&attr));
1e33ac1e 86
c25b8f99
BB
87 if (state & TS_JOINABLE)
88 detachstate = PTHREAD_CREATE_JOINABLE;
1e33ac1e 89
aa0ac7ca
BB
90 VERIFY0(pthread_attr_setdetachstate(&attr, detachstate));
91
1e33ac1e 92 /*
aa0ac7ca
BB
93 * We allow the default stack size in user space to be specified by
94 * setting the ZFS_STACK_SIZE environment variable. This allows us
95 * the convenience of observing and debugging stack overruns in
96 * user space. Explicitly specified stack sizes will be honored.
97 * The usage of ZFS_STACK_SIZE is discussed further in the
98 * ENVIRONMENT VARIABLES sections of the ztest(1) man page.
1e33ac1e 99 */
aa0ac7ca
BB
100 if (stksize == 0) {
101 stkstr = getenv("ZFS_STACK_SIZE");
102
103 if (stkstr == NULL)
104 stksize = TS_STACK_MAX;
105 else
106 stksize = MAX(atoi(stkstr), TS_STACK_MIN);
107 }
108
109 VERIFY3S(stksize, >, 0);
110 stksize = P2ROUNDUP(MAX(stksize, TS_STACK_MIN), PAGESIZE);
c25b8f99 111
206971d2
DR
112 /*
113 * If this ever fails, it may be because the stack size is not a
114 * multiple of system page size.
115 */
aa0ac7ca
BB
116 VERIFY0(pthread_attr_setstacksize(&attr, stksize));
117 VERIFY0(pthread_attr_setguardsize(&attr, PAGESIZE));
118
c25b8f99 119 VERIFY0(pthread_create(&tid, &attr, (void *(*)(void *))func, arg));
aa0ac7ca 120 VERIFY0(pthread_attr_destroy(&attr));
1e33ac1e 121
c25b8f99 122 return ((void *)(uintptr_t)tid);
34dc7c2f
BB
123}
124
125/*
126 * =========================================================================
127 * kstats
128 * =========================================================================
129 */
130/*ARGSUSED*/
131kstat_t *
330847ff
MA
132kstat_create(const char *module, int instance, const char *name,
133 const char *class, uchar_t type, ulong_t ndata, uchar_t ks_flag)
34dc7c2f
BB
134{
135 return (NULL);
136}
137
138/*ARGSUSED*/
139void
140kstat_install(kstat_t *ksp)
141{}
142
143/*ARGSUSED*/
144void
145kstat_delete(kstat_t *ksp)
146{}
147
1421c891 148/*ARGSUSED*/
330847ff
MA
149void
150kstat_waitq_enter(kstat_io_t *kiop)
151{}
152
153/*ARGSUSED*/
154void
155kstat_waitq_exit(kstat_io_t *kiop)
156{}
157
158/*ARGSUSED*/
159void
160kstat_runq_enter(kstat_io_t *kiop)
161{}
162
163/*ARGSUSED*/
164void
165kstat_runq_exit(kstat_io_t *kiop)
166{}
167
168/*ARGSUSED*/
169void
170kstat_waitq_to_runq(kstat_io_t *kiop)
171{}
172
173/*ARGSUSED*/
174void
175kstat_runq_back_to_waitq(kstat_io_t *kiop)
176{}
177
1421c891
PS
178void
179kstat_set_raw_ops(kstat_t *ksp,
180 int (*headers)(char *buf, size_t size),
181 int (*data)(char *buf, size_t size, void *data),
182 void *(*addr)(kstat_t *ksp, loff_t index))
183{}
184
34dc7c2f
BB
185/*
186 * =========================================================================
187 * mutexes
188 * =========================================================================
189 */
1e33ac1e 190
34dc7c2f 191void
1e33ac1e 192mutex_init(kmutex_t *mp, char *name, int type, void *cookie)
34dc7c2f 193{
c25b8f99
BB
194 VERIFY0(pthread_mutex_init(&mp->m_lock, NULL));
195 memset(&mp->m_owner, 0, sizeof (pthread_t));
34dc7c2f
BB
196}
197
198void
1e33ac1e 199mutex_destroy(kmutex_t *mp)
34dc7c2f 200{
c25b8f99 201 VERIFY0(pthread_mutex_destroy(&mp->m_lock));
34dc7c2f
BB
202}
203
204void
205mutex_enter(kmutex_t *mp)
206{
c25b8f99
BB
207 VERIFY0(pthread_mutex_lock(&mp->m_lock));
208 mp->m_owner = pthread_self();
34dc7c2f
BB
209}
210
211int
212mutex_tryenter(kmutex_t *mp)
213{
c25b8f99
BB
214 int error;
215
216 error = pthread_mutex_trylock(&mp->m_lock);
217 if (error == 0) {
218 mp->m_owner = pthread_self();
34dc7c2f
BB
219 return (1);
220 } else {
c25b8f99 221 VERIFY3S(error, ==, EBUSY);
34dc7c2f
BB
222 return (0);
223 }
224}
225
226void
227mutex_exit(kmutex_t *mp)
228{
c25b8f99
BB
229 memset(&mp->m_owner, 0, sizeof (pthread_t));
230 VERIFY0(pthread_mutex_unlock(&mp->m_lock));
1e33ac1e
BB
231}
232
34dc7c2f
BB
233/*
234 * =========================================================================
235 * rwlocks
236 * =========================================================================
237 */
1e33ac1e 238
34dc7c2f
BB
239void
240rw_init(krwlock_t *rwlp, char *name, int type, void *arg)
241{
c25b8f99 242 VERIFY0(pthread_rwlock_init(&rwlp->rw_lock, NULL));
1e33ac1e 243 rwlp->rw_readers = 0;
c25b8f99 244 rwlp->rw_owner = 0;
34dc7c2f
BB
245}
246
247void
248rw_destroy(krwlock_t *rwlp)
249{
c25b8f99 250 VERIFY0(pthread_rwlock_destroy(&rwlp->rw_lock));
34dc7c2f
BB
251}
252
253void
254rw_enter(krwlock_t *rwlp, krw_t rw)
255{
1e33ac1e 256 if (rw == RW_READER) {
c25b8f99 257 VERIFY0(pthread_rwlock_rdlock(&rwlp->rw_lock));
1e33ac1e
BB
258 atomic_inc_uint(&rwlp->rw_readers);
259 } else {
c25b8f99
BB
260 VERIFY0(pthread_rwlock_wrlock(&rwlp->rw_lock));
261 rwlp->rw_owner = pthread_self();
1e33ac1e 262 }
34dc7c2f
BB
263}
264
265void
266rw_exit(krwlock_t *rwlp)
267{
1e33ac1e
BB
268 if (RW_READ_HELD(rwlp))
269 atomic_dec_uint(&rwlp->rw_readers);
270 else
c25b8f99 271 rwlp->rw_owner = 0;
34dc7c2f 272
c25b8f99 273 VERIFY0(pthread_rwlock_unlock(&rwlp->rw_lock));
34dc7c2f
BB
274}
275
276int
277rw_tryenter(krwlock_t *rwlp, krw_t rw)
278{
c25b8f99 279 int error;
34dc7c2f
BB
280
281 if (rw == RW_READER)
c25b8f99 282 error = pthread_rwlock_tryrdlock(&rwlp->rw_lock);
34dc7c2f 283 else
c25b8f99 284 error = pthread_rwlock_trywrlock(&rwlp->rw_lock);
1e33ac1e 285
c25b8f99 286 if (error == 0) {
1e33ac1e
BB
287 if (rw == RW_READER)
288 atomic_inc_uint(&rwlp->rw_readers);
c25b8f99
BB
289 else
290 rwlp->rw_owner = pthread_self();
1e33ac1e 291
34dc7c2f
BB
292 return (1);
293 }
294
c25b8f99 295 VERIFY3S(error, ==, EBUSY);
1e33ac1e 296
34dc7c2f
BB
297 return (0);
298}
299
6cb8e530
PZ
300/* ARGSUSED */
301uint32_t
302zone_get_hostid(void *zonep)
303{
304 /*
305 * We're emulating the system's hostid in userland.
306 */
307 return (strtoul(hw_serial, NULL, 10));
308}
309
34dc7c2f
BB
310int
311rw_tryupgrade(krwlock_t *rwlp)
312{
34dc7c2f
BB
313 return (0);
314}
315
316/*
317 * =========================================================================
318 * condition variables
319 * =========================================================================
320 */
1e33ac1e 321
34dc7c2f
BB
322void
323cv_init(kcondvar_t *cv, char *name, int type, void *arg)
324{
c25b8f99 325 VERIFY0(pthread_cond_init(cv, NULL));
34dc7c2f
BB
326}
327
328void
329cv_destroy(kcondvar_t *cv)
330{
c25b8f99 331 VERIFY0(pthread_cond_destroy(cv));
34dc7c2f
BB
332}
333
334void
335cv_wait(kcondvar_t *cv, kmutex_t *mp)
336{
c25b8f99
BB
337 memset(&mp->m_owner, 0, sizeof (pthread_t));
338 VERIFY0(pthread_cond_wait(cv, &mp->m_lock));
339 mp->m_owner = pthread_self();
34dc7c2f
BB
340}
341
342clock_t
343cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime)
344{
345 int error;
1e33ac1e 346 struct timeval tv;
6413c95f 347 struct timespec ts;
34dc7c2f
BB
348 clock_t delta;
349
428870ff 350 delta = abstime - ddi_get_lbolt();
34dc7c2f
BB
351 if (delta <= 0)
352 return (-1);
353
1e33ac1e
BB
354 VERIFY(gettimeofday(&tv, NULL) == 0);
355
356 ts.tv_sec = tv.tv_sec + delta / hz;
67925abb 357 ts.tv_nsec = tv.tv_usec * NSEC_PER_USEC + (delta % hz) * (NANOSEC / hz);
1e33ac1e
BB
358 if (ts.tv_nsec >= NANOSEC) {
359 ts.tv_sec++;
360 ts.tv_nsec -= NANOSEC;
361 }
34dc7c2f 362
c25b8f99
BB
363 memset(&mp->m_owner, 0, sizeof (pthread_t));
364 error = pthread_cond_timedwait(cv, &mp->m_lock, &ts);
365 mp->m_owner = pthread_self();
34dc7c2f 366
1e33ac1e 367 if (error == ETIMEDOUT)
34dc7c2f
BB
368 return (-1);
369
206971d2 370 VERIFY0(error);
34dc7c2f
BB
371
372 return (1);
373}
374
63fd3c6c
AL
375/*ARGSUSED*/
376clock_t
377cv_timedwait_hires(kcondvar_t *cv, kmutex_t *mp, hrtime_t tim, hrtime_t res,
378 int flag)
379{
380 int error;
67925abb 381 struct timeval tv;
6413c95f 382 struct timespec ts;
63fd3c6c
AL
383 hrtime_t delta;
384
206971d2
DR
385 ASSERT(flag == 0 || flag == CALLOUT_FLAG_ABSOLUTE);
386
387 delta = tim;
388 if (flag & CALLOUT_FLAG_ABSOLUTE)
389 delta -= gethrtime();
63fd3c6c 390
63fd3c6c
AL
391 if (delta <= 0)
392 return (-1);
393
c25b8f99 394 VERIFY0(gettimeofday(&tv, NULL));
67925abb
BB
395
396 ts.tv_sec = tv.tv_sec + delta / NANOSEC;
397 ts.tv_nsec = tv.tv_usec * NSEC_PER_USEC + (delta % NANOSEC);
398 if (ts.tv_nsec >= NANOSEC) {
399 ts.tv_sec++;
400 ts.tv_nsec -= NANOSEC;
401 }
63fd3c6c 402
c25b8f99
BB
403 memset(&mp->m_owner, 0, sizeof (pthread_t));
404 error = pthread_cond_timedwait(cv, &mp->m_lock, &ts);
405 mp->m_owner = pthread_self();
63fd3c6c 406
206971d2 407 if (error == ETIMEDOUT)
63fd3c6c
AL
408 return (-1);
409
206971d2 410 VERIFY0(error);
63fd3c6c
AL
411
412 return (1);
413}
414
34dc7c2f
BB
415void
416cv_signal(kcondvar_t *cv)
417{
c25b8f99 418 VERIFY0(pthread_cond_signal(cv));
34dc7c2f
BB
419}
420
421void
422cv_broadcast(kcondvar_t *cv)
423{
c25b8f99 424 VERIFY0(pthread_cond_broadcast(cv));
34dc7c2f
BB
425}
426
427/*
428 * =========================================================================
429 * vnode operations
430 * =========================================================================
431 */
432/*
433 * Note: for the xxxat() versions of these functions, we assume that the
434 * starting vp is always rootdir (which is true for spa_directory.c, the only
435 * ZFS consumer of these interfaces). We assert this is true, and then emulate
436 * them by adding '/' in front of the path.
437 */
438
439/*ARGSUSED*/
440int
441vn_open(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, int x3)
442{
e2c292bb 443 int fd = -1;
444 int dump_fd = -1;
34dc7c2f 445 vnode_t *vp;
a4914d38 446 int old_umask = 0;
5ae4e2c2 447 char *realpath;
34dc7c2f 448 struct stat64 st;
4d58b69d 449 int err;
34dc7c2f 450
5ae4e2c2
BB
451 realpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
452
34dc7c2f
BB
453 /*
454 * If we're accessing a real disk from userland, we need to use
455 * the character interface to avoid caching. This is particularly
456 * important if we're trying to look at a real in-kernel storage
457 * pool from userland, e.g. via zdb, because otherwise we won't
458 * see the changes occurring under the segmap cache.
459 * On the other hand, the stupid character device returns zero
460 * for its size. So -- gag -- we open the block device to get
461 * its size, and remember it for subsequent VOP_GETATTR().
462 */
d603ed6c 463#if defined(__sun__) || defined(__sun)
34dc7c2f 464 if (strncmp(path, "/dev/", 5) == 0) {
d603ed6c
BB
465#else
466 if (0) {
467#endif
34dc7c2f
BB
468 char *dsk;
469 fd = open64(path, O_RDONLY);
5ae4e2c2
BB
470 if (fd == -1) {
471 err = errno;
472 free(realpath);
473 return (err);
474 }
34dc7c2f 475 if (fstat64(fd, &st) == -1) {
5ae4e2c2 476 err = errno;
34dc7c2f 477 close(fd);
5ae4e2c2
BB
478 free(realpath);
479 return (err);
34dc7c2f
BB
480 }
481 close(fd);
482 (void) sprintf(realpath, "%s", path);
483 dsk = strstr(path, "/dsk/");
484 if (dsk != NULL)
485 (void) sprintf(realpath + (dsk - path) + 1, "r%s",
486 dsk + 1);
487 } else {
488 (void) sprintf(realpath, "%s", path);
5ae4e2c2
BB
489 if (!(flags & FCREAT) && stat64(realpath, &st) == -1) {
490 err = errno;
491 free(realpath);
492 return (err);
493 }
34dc7c2f
BB
494 }
495
d603ed6c
BB
496 if (!(flags & FCREAT) && S_ISBLK(st.st_mode)) {
497#ifdef __linux__
498 flags |= O_DIRECT;
499#endif
d603ed6c
BB
500 }
501
34dc7c2f
BB
502 if (flags & FCREAT)
503 old_umask = umask(0);
504
505 /*
506 * The construct 'flags - FREAD' conveniently maps combinations of
507 * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR.
508 */
509 fd = open64(realpath, flags - FREAD, mode);
470f12d6
G
510 if (fd == -1) {
511 err = errno;
512 free(realpath);
513 return (err);
514 }
34dc7c2f
BB
515
516 if (flags & FCREAT)
517 (void) umask(old_umask);
518
9867e8be
MA
519 if (vn_dumpdir != NULL) {
520 char *dumppath = umem_zalloc(MAXPATHLEN, UMEM_NOFAIL);
521 (void) snprintf(dumppath, MAXPATHLEN,
522 "%s/%s", vn_dumpdir, basename(realpath));
523 dump_fd = open64(dumppath, O_CREAT | O_WRONLY, 0666);
524 umem_free(dumppath, MAXPATHLEN);
525 if (dump_fd == -1) {
526 err = errno;
527 free(realpath);
528 close(fd);
529 return (err);
530 }
531 } else {
532 dump_fd = -1;
533 }
534
535 free(realpath);
536
8d4e8140 537 if (fstat64_blk(fd, &st) == -1) {
4d58b69d 538 err = errno;
34dc7c2f 539 close(fd);
e2c292bb 540 if (dump_fd != -1)
541 close(dump_fd);
4d58b69d 542 return (err);
34dc7c2f
BB
543 }
544
545 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
546
547 *vpp = vp = umem_zalloc(sizeof (vnode_t), UMEM_NOFAIL);
548
549 vp->v_fd = fd;
550 vp->v_size = st.st_size;
551 vp->v_path = spa_strdup(path);
9867e8be 552 vp->v_dump_fd = dump_fd;
34dc7c2f
BB
553
554 return (0);
555}
556
557/*ARGSUSED*/
558int
559vn_openat(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2,
560 int x3, vnode_t *startvp, int fd)
561{
562 char *realpath = umem_alloc(strlen(path) + 2, UMEM_NOFAIL);
563 int ret;
564
565 ASSERT(startvp == rootdir);
566 (void) sprintf(realpath, "/%s", path);
567
568 /* fd ignored for now, need if want to simulate nbmand support */
569 ret = vn_open(realpath, x1, flags, mode, vpp, x2, x3);
570
571 umem_free(realpath, strlen(path) + 2);
572
573 return (ret);
574}
575
576/*ARGSUSED*/
577int
578vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, offset_t offset,
e9aa730c 579 int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp)
34dc7c2f 580{
4d58b69d 581 ssize_t rc, done = 0, split;
34dc7c2f
BB
582
583 if (uio == UIO_READ) {
4d58b69d 584 rc = pread64(vp->v_fd, addr, len, offset);
470f12d6 585 if (vp->v_dump_fd != -1 && rc != -1) {
928c58dd
BB
586 int status;
587 status = pwrite64(vp->v_dump_fd, addr, rc, offset);
9867e8be
MA
588 ASSERT(status != -1);
589 }
34dc7c2f
BB
590 } else {
591 /*
592 * To simulate partial disk writes, we split writes into two
593 * system calls so that the process can be killed in between.
594 */
9ae529ec
CS
595 int sectors = len >> SPA_MINBLOCKSHIFT;
596 split = (sectors > 0 ? rand() % sectors : 0) <<
597 SPA_MINBLOCKSHIFT;
4d58b69d
RC
598 rc = pwrite64(vp->v_fd, addr, split, offset);
599 if (rc != -1) {
600 done = rc;
601 rc = pwrite64(vp->v_fd, (char *)addr + split,
602 len - split, offset + split);
603 }
34dc7c2f
BB
604 }
605
d603ed6c
BB
606#ifdef __linux__
607 if (rc == -1 && errno == EINVAL) {
608 /*
609 * Under Linux, this most likely means an alignment issue
610 * (memory or disk) due to O_DIRECT, so we abort() in order to
611 * catch the offender.
612 */
d1d7e268 613 abort();
d603ed6c
BB
614 }
615#endif
4d58b69d 616 if (rc == -1)
34dc7c2f 617 return (errno);
4d58b69d
RC
618
619 done += rc;
620
34dc7c2f 621 if (residp)
4d58b69d
RC
622 *residp = len - done;
623 else if (done != len)
34dc7c2f
BB
624 return (EIO);
625 return (0);
626}
627
628void
629vn_close(vnode_t *vp)
630{
631 close(vp->v_fd);
9867e8be
MA
632 if (vp->v_dump_fd != -1)
633 close(vp->v_dump_fd);
34dc7c2f
BB
634 spa_strfree(vp->v_path);
635 umem_free(vp, sizeof (vnode_t));
636}
637
428870ff
BB
638/*
639 * At a minimum we need to update the size since vdev_reopen()
640 * will no longer call vn_openat().
641 */
642int
643fop_getattr(vnode_t *vp, vattr_t *vap)
644{
645 struct stat64 st;
8d4e8140 646 int err;
428870ff 647
8d4e8140
RC
648 if (fstat64_blk(vp->v_fd, &st) == -1) {
649 err = errno;
428870ff 650 close(vp->v_fd);
8d4e8140 651 return (err);
428870ff
BB
652 }
653
654 vap->va_size = st.st_size;
655 return (0);
656}
657
34dc7c2f
BB
658/*
659 * =========================================================================
660 * Figure out which debugging statements to print
661 * =========================================================================
662 */
663
664static char *dprintf_string;
665static int dprintf_print_all;
666
667int
668dprintf_find_string(const char *string)
669{
670 char *tmp_str = dprintf_string;
671 int len = strlen(string);
672
673 /*
674 * Find out if this is a string we want to print.
675 * String format: file1.c,function_name1,file2.c,file3.c
676 */
677
678 while (tmp_str != NULL) {
679 if (strncmp(tmp_str, string, len) == 0 &&
680 (tmp_str[len] == ',' || tmp_str[len] == '\0'))
681 return (1);
682 tmp_str = strchr(tmp_str, ',');
683 if (tmp_str != NULL)
684 tmp_str++; /* Get rid of , */
685 }
686 return (0);
687}
688
689void
690dprintf_setup(int *argc, char **argv)
691{
692 int i, j;
693
694 /*
695 * Debugging can be specified two ways: by setting the
696 * environment variable ZFS_DEBUG, or by including a
697 * "debug=..." argument on the command line. The command
698 * line setting overrides the environment variable.
699 */
700
701 for (i = 1; i < *argc; i++) {
702 int len = strlen("debug=");
703 /* First look for a command line argument */
704 if (strncmp("debug=", argv[i], len) == 0) {
705 dprintf_string = argv[i] + len;
706 /* Remove from args */
707 for (j = i; j < *argc; j++)
708 argv[j] = argv[j+1];
709 argv[j] = NULL;
710 (*argc)--;
711 }
712 }
713
714 if (dprintf_string == NULL) {
715 /* Look for ZFS_DEBUG environment variable */
716 dprintf_string = getenv("ZFS_DEBUG");
717 }
718
719 /*
720 * Are we just turning on all debugging?
721 */
722 if (dprintf_find_string("on"))
723 dprintf_print_all = 1;
308a451f
MA
724
725 if (dprintf_string != NULL)
726 zfs_flags |= ZFS_DEBUG_DPRINTF;
34dc7c2f
BB
727}
728
729/*
730 * =========================================================================
731 * debug printfs
732 * =========================================================================
733 */
734void
735__dprintf(const char *file, const char *func, int line, const char *fmt, ...)
736{
737 const char *newfile;
738 va_list adx;
739
740 /*
741 * Get rid of annoying "../common/" prefix to filename.
742 */
743 newfile = strrchr(file, '/');
744 if (newfile != NULL) {
745 newfile = newfile + 1; /* Get rid of leading / */
746 } else {
747 newfile = file;
748 }
749
750 if (dprintf_print_all ||
751 dprintf_find_string(newfile) ||
752 dprintf_find_string(func)) {
753 /* Print out just the function name if requested */
754 flockfile(stdout);
755 if (dprintf_find_string("pid"))
756 (void) printf("%d ", getpid());
757 if (dprintf_find_string("tid"))
02730c33 758 (void) printf("%u ", (uint_t)pthread_self());
34dc7c2f
BB
759 if (dprintf_find_string("cpu"))
760 (void) printf("%u ", getcpuid());
761 if (dprintf_find_string("time"))
762 (void) printf("%llu ", gethrtime());
763 if (dprintf_find_string("long"))
764 (void) printf("%s, line %d: ", newfile, line);
765 (void) printf("%s: ", func);
766 va_start(adx, fmt);
767 (void) vprintf(fmt, adx);
768 va_end(adx);
769 funlockfile(stdout);
770 }
771}
772
34dc7c2f
BB
773/*
774 * =========================================================================
775 * cmn_err() and panic()
776 * =========================================================================
777 */
778static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
779static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
780
781void
782vpanic(const char *fmt, va_list adx)
783{
784 (void) fprintf(stderr, "error: ");
785 (void) vfprintf(stderr, fmt, adx);
786 (void) fprintf(stderr, "\n");
787
788 abort(); /* think of it as a "user-level crash dump" */
789}
790
791void
792panic(const char *fmt, ...)
793{
794 va_list adx;
795
796 va_start(adx, fmt);
797 vpanic(fmt, adx);
798 va_end(adx);
799}
800
801void
802vcmn_err(int ce, const char *fmt, va_list adx)
803{
804 if (ce == CE_PANIC)
805 vpanic(fmt, adx);
806 if (ce != CE_NOTE) { /* suppress noise in userland stress testing */
807 (void) fprintf(stderr, "%s", ce_prefix[ce]);
808 (void) vfprintf(stderr, fmt, adx);
809 (void) fprintf(stderr, "%s", ce_suffix[ce]);
810 }
811}
812
813/*PRINTFLIKE2*/
814void
815cmn_err(int ce, const char *fmt, ...)
816{
817 va_list adx;
818
819 va_start(adx, fmt);
820 vcmn_err(ce, fmt, adx);
821 va_end(adx);
822}
823
824/*
825 * =========================================================================
826 * kobj interfaces
827 * =========================================================================
828 */
829struct _buf *
830kobj_open_file(char *name)
831{
832 struct _buf *file;
833 vnode_t *vp;
834
835 /* set vp as the _fd field of the file */
836 if (vn_openat(name, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0, rootdir,
837 -1) != 0)
838 return ((void *)-1UL);
839
840 file = umem_zalloc(sizeof (struct _buf), UMEM_NOFAIL);
841 file->_fd = (intptr_t)vp;
842 return (file);
843}
844
845int
846kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off)
847{
689f093e 848 ssize_t resid = 0;
34dc7c2f 849
957dc932
RY
850 if (vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off,
851 UIO_SYSSPACE, 0, 0, 0, &resid) != 0)
852 return (-1);
34dc7c2f
BB
853
854 return (size - resid);
855}
856
857void
858kobj_close_file(struct _buf *file)
859{
860 vn_close((vnode_t *)file->_fd);
861 umem_free(file, sizeof (struct _buf));
862}
863
864int
865kobj_get_filesize(struct _buf *file, uint64_t *size)
866{
867 struct stat64 st;
868 vnode_t *vp = (vnode_t *)file->_fd;
869
870 if (fstat64(vp->v_fd, &st) == -1) {
871 vn_close(vp);
872 return (errno);
873 }
874 *size = st.st_size;
875 return (0);
876}
877
878/*
879 * =========================================================================
880 * misc routines
881 * =========================================================================
882 */
883
884void
885delay(clock_t ticks)
886{
af4db70f 887 (void) poll(0, 0, ticks * (1000 / hz));
34dc7c2f
BB
888}
889
890/*
891 * Find highest one bit set.
46364cb2
BB
892 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
893 * The __builtin_clzll() function is supported by both GCC and Clang.
34dc7c2f
BB
894 */
895int
9bd274dd 896highbit64(uint64_t i)
34dc7c2f 897{
34dc7c2f 898 if (i == 0)
46364cb2
BB
899 return (0);
900
901 return (NBBY * sizeof (uint64_t) - __builtin_clzll(i));
34dc7c2f
BB
902}
903
193a37cb
TH
904/*
905 * Find lowest one bit set.
906 * Returns bit number + 1 of lowest bit that is set, otherwise returns 0.
46364cb2 907 * The __builtin_ffsll() function is supported by both GCC and Clang.
193a37cb
TH
908 */
909int
910lowbit64(uint64_t i)
911{
193a37cb
TH
912 if (i == 0)
913 return (0);
914
46364cb2 915 return (__builtin_ffsll(i));
0b04990a 916}
193a37cb 917
e1a0850c
BB
918char *random_path = "/dev/random";
919char *urandom_path = "/dev/urandom";
34dc7c2f
BB
920static int random_fd = -1, urandom_fd = -1;
921
0b04990a
TC
922void
923random_init(void)
924{
e1a0850c
BB
925 VERIFY((random_fd = open(random_path, O_RDONLY)) != -1);
926 VERIFY((urandom_fd = open(urandom_path, O_RDONLY)) != -1);
0b04990a
TC
927}
928
929void
930random_fini(void)
931{
932 close(random_fd);
933 close(urandom_fd);
934
935 random_fd = -1;
936 urandom_fd = -1;
937}
938
34dc7c2f
BB
939static int
940random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
941{
942 size_t resid = len;
943 ssize_t bytes;
944
945 ASSERT(fd != -1);
946
947 while (resid != 0) {
948 bytes = read(fd, ptr, resid);
949 ASSERT3S(bytes, >=, 0);
950 ptr += bytes;
951 resid -= bytes;
952 }
953
954 return (0);
955}
956
957int
958random_get_bytes(uint8_t *ptr, size_t len)
959{
960 return (random_get_bytes_common(ptr, len, random_fd));
961}
962
963int
964random_get_pseudo_bytes(uint8_t *ptr, size_t len)
965{
966 return (random_get_bytes_common(ptr, len, urandom_fd));
967}
968
969int
970ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result)
971{
972 char *end;
973
974 *result = strtoul(hw_serial, &end, base);
975 if (*result == 0)
976 return (errno);
977 return (0);
978}
979
428870ff
BB
980int
981ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
982{
983 char *end;
984
985 *result = strtoull(str, &end, base);
986 if (*result == 0)
987 return (errno);
988 return (0);
989}
990
f0e324f2
BB
991utsname_t *
992utsname(void)
993{
994 return (&hw_utsname);
995}
996
34dc7c2f
BB
997/*
998 * =========================================================================
999 * kernel emulation setup & teardown
1000 * =========================================================================
1001 */
1002static int
1003umem_out_of_memory(void)
1004{
1005 char errmsg[] = "out of memory -- generating core dump\n";
1006
0e5b68e0 1007 (void) fprintf(stderr, "%s", errmsg);
34dc7c2f
BB
1008 abort();
1009 return (0);
1010}
1011
1012void
1013kernel_init(int mode)
1014{
13fe0198
MA
1015 extern uint_t rrw_tsd_key;
1016
34dc7c2f
BB
1017 umem_nofail_callback(umem_out_of_memory);
1018
1019 physmem = sysconf(_SC_PHYS_PAGES);
1020
1021 dprintf("physmem = %llu pages (%.2f GB)\n", physmem,
1022 (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30));
1023
428870ff 1024 (void) snprintf(hw_serial, sizeof (hw_serial), "%ld",
53698a45 1025 (mode & FWRITE) ? get_system_hostid() : 0);
34dc7c2f 1026
0b04990a
TC
1027 random_init();
1028
f0e324f2 1029 VERIFY0(uname(&hw_utsname));
34dc7c2f 1030
b128c09f 1031 system_taskq_init();
0b04990a 1032 icp_init();
b128c09f 1033
34dc7c2f 1034 spa_init(mode);
13fe0198 1035
1eeb4562
JX
1036 fletcher_4_init();
1037
13fe0198 1038 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
34dc7c2f
BB
1039}
1040
1041void
1042kernel_fini(void)
1043{
1eeb4562 1044 fletcher_4_fini();
34dc7c2f
BB
1045 spa_fini();
1046
0b04990a 1047 icp_fini();
428870ff
BB
1048 system_taskq_fini();
1049
0b04990a 1050 random_fini();
34dc7c2f
BB
1051}
1052
34dc7c2f
BB
1053uid_t
1054crgetuid(cred_t *cr)
1055{
1056 return (0);
1057}
1058
6f1ffb06
MA
1059uid_t
1060crgetruid(cred_t *cr)
1061{
1062 return (0);
1063}
1064
34dc7c2f
BB
1065gid_t
1066crgetgid(cred_t *cr)
1067{
1068 return (0);
1069}
1070
1071int
1072crgetngroups(cred_t *cr)
1073{
1074 return (0);
1075}
1076
1077gid_t *
1078crgetgroups(cred_t *cr)
1079{
1080 return (NULL);
1081}
1082
1083int
1084zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
1085{
1086 return (0);
1087}
1088
1089int
1090zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
1091{
1092 return (0);
1093}
1094
1095int
1096zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
1097{
1098 return (0);
1099}
1100
f74b821a
BB
1101int
1102secpolicy_zfs(const cred_t *cr)
1103{
1104 return (0);
1105}
1106
34dc7c2f
BB
1107ksiddomain_t *
1108ksid_lookupdomain(const char *dom)
1109{
1110 ksiddomain_t *kd;
1111
1112 kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL);
1113 kd->kd_name = spa_strdup(dom);
1114 return (kd);
1115}
1116
1117void
1118ksiddomain_rele(ksiddomain_t *ksid)
1119{
1120 spa_strfree(ksid->kd_name);
1121 umem_free(ksid, sizeof (ksiddomain_t));
1122}
428870ff 1123
428870ff 1124char *
00b46022 1125kmem_vasprintf(const char *fmt, va_list adx)
428870ff 1126{
00b46022
BB
1127 char *buf = NULL;
1128 va_list adx_copy;
428870ff 1129
00b46022
BB
1130 va_copy(adx_copy, adx);
1131 VERIFY(vasprintf(&buf, fmt, adx_copy) != -1);
1132 va_end(adx_copy);
428870ff 1133
00b46022
BB
1134 return (buf);
1135}
1136
1137char *
1138kmem_asprintf(const char *fmt, ...)
1139{
1140 char *buf = NULL;
1141 va_list adx;
428870ff
BB
1142
1143 va_start(adx, fmt);
00b46022 1144 VERIFY(vasprintf(&buf, fmt, adx) != -1);
428870ff
BB
1145 va_end(adx);
1146
1147 return (buf);
1148}
572e2857
BB
1149
1150/* ARGSUSED */
1151int
1152zfs_onexit_fd_hold(int fd, minor_t *minorp)
1153{
1154 *minorp = 0;
1155 return (0);
1156}
1157
1158/* ARGSUSED */
1159void
1160zfs_onexit_fd_rele(int fd)
1161{
1162}
1163
1164/* ARGSUSED */
1165int
1166zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data,
1167 uint64_t *action_handle)
1168{
1169 return (0);
1170}
1171
1172/* ARGSUSED */
1173int
1174zfs_onexit_del_cb(minor_t minor, uint64_t action_handle, boolean_t fire)
1175{
1176 return (0);
1177}
1178
1179/* ARGSUSED */
1180int
1181zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data)
1182{
1183 return (0);
1184}
92119cc2
BB
1185
1186fstrans_cookie_t
1187spl_fstrans_mark(void)
1188{
02730c33 1189 return ((fstrans_cookie_t)0);
92119cc2
BB
1190}
1191
1192void
1193spl_fstrans_unmark(fstrans_cookie_t cookie)
1194{
1195}
1196
1197int
e624cd19 1198__spl_pf_fstrans_check(void)
92119cc2
BB
1199{
1200 return (0);
1201}
a0bd735a 1202
47dfff3b
MA
1203void *zvol_tag = "zvol_tag";
1204
a0bd735a
BP
1205void
1206zvol_create_minors(spa_t *spa, const char *name, boolean_t async)
1207{
1208}
1209
1210void
1211zvol_remove_minor(spa_t *spa, const char *name, boolean_t async)
1212{
1213}
1214
1215void
1216zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
1217{
1218}
1219
1220void
1221zvol_rename_minors(spa_t *spa, const char *oldname, const char *newname,
1222 boolean_t async)
1223{
1224}