]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libzpool/kernel.c
Don't assume pthread_t is uint_t for portability
[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
d1261452
JG
427/*
428 * =========================================================================
429 * procfs list
430 * =========================================================================
431 */
432
433void
434seq_printf(struct seq_file *m, const char *fmt, ...)
435{}
436
437void
438procfs_list_install(const char *module,
439 const char *name,
a887d653 440 mode_t mode,
d1261452
JG
441 procfs_list_t *procfs_list,
442 int (*show)(struct seq_file *f, void *p),
443 int (*show_header)(struct seq_file *f),
444 int (*clear)(procfs_list_t *procfs_list),
445 size_t procfs_list_node_off)
446{
447 mutex_init(&procfs_list->pl_lock, NULL, MUTEX_DEFAULT, NULL);
448 list_create(&procfs_list->pl_list,
449 procfs_list_node_off + sizeof (procfs_list_node_t),
450 procfs_list_node_off + offsetof(procfs_list_node_t, pln_link));
451 procfs_list->pl_next_id = 1;
452 procfs_list->pl_node_offset = procfs_list_node_off;
453}
454
455void
456procfs_list_uninstall(procfs_list_t *procfs_list)
457{}
458
459void
460procfs_list_destroy(procfs_list_t *procfs_list)
461{
462 ASSERT(list_is_empty(&procfs_list->pl_list));
463 list_destroy(&procfs_list->pl_list);
464 mutex_destroy(&procfs_list->pl_lock);
465}
466
467#define NODE_ID(procfs_list, obj) \
468 (((procfs_list_node_t *)(((char *)obj) + \
469 (procfs_list)->pl_node_offset))->pln_id)
470
471void
472procfs_list_add(procfs_list_t *procfs_list, void *p)
473{
474 ASSERT(MUTEX_HELD(&procfs_list->pl_lock));
475 NODE_ID(procfs_list, p) = procfs_list->pl_next_id++;
476 list_insert_tail(&procfs_list->pl_list, p);
477}
478
34dc7c2f
BB
479/*
480 * =========================================================================
481 * vnode operations
482 * =========================================================================
483 */
484/*
485 * Note: for the xxxat() versions of these functions, we assume that the
486 * starting vp is always rootdir (which is true for spa_directory.c, the only
487 * ZFS consumer of these interfaces). We assert this is true, and then emulate
488 * them by adding '/' in front of the path.
489 */
490
491/*ARGSUSED*/
492int
493vn_open(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, int x3)
494{
e2c292bb 495 int fd = -1;
496 int dump_fd = -1;
34dc7c2f 497 vnode_t *vp;
a4914d38 498 int old_umask = 0;
5ae4e2c2 499 char *realpath;
34dc7c2f 500 struct stat64 st;
4d58b69d 501 int err;
34dc7c2f 502
5ae4e2c2
BB
503 realpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
504
34dc7c2f
BB
505 /*
506 * If we're accessing a real disk from userland, we need to use
507 * the character interface to avoid caching. This is particularly
508 * important if we're trying to look at a real in-kernel storage
509 * pool from userland, e.g. via zdb, because otherwise we won't
510 * see the changes occurring under the segmap cache.
511 * On the other hand, the stupid character device returns zero
512 * for its size. So -- gag -- we open the block device to get
513 * its size, and remember it for subsequent VOP_GETATTR().
514 */
d603ed6c 515#if defined(__sun__) || defined(__sun)
34dc7c2f 516 if (strncmp(path, "/dev/", 5) == 0) {
d603ed6c
BB
517#else
518 if (0) {
519#endif
34dc7c2f
BB
520 char *dsk;
521 fd = open64(path, O_RDONLY);
5ae4e2c2
BB
522 if (fd == -1) {
523 err = errno;
524 free(realpath);
525 return (err);
526 }
34dc7c2f 527 if (fstat64(fd, &st) == -1) {
5ae4e2c2 528 err = errno;
34dc7c2f 529 close(fd);
5ae4e2c2
BB
530 free(realpath);
531 return (err);
34dc7c2f
BB
532 }
533 close(fd);
534 (void) sprintf(realpath, "%s", path);
535 dsk = strstr(path, "/dsk/");
536 if (dsk != NULL)
537 (void) sprintf(realpath + (dsk - path) + 1, "r%s",
538 dsk + 1);
539 } else {
540 (void) sprintf(realpath, "%s", path);
5ae4e2c2
BB
541 if (!(flags & FCREAT) && stat64(realpath, &st) == -1) {
542 err = errno;
543 free(realpath);
544 return (err);
545 }
34dc7c2f
BB
546 }
547
d603ed6c
BB
548 if (!(flags & FCREAT) && S_ISBLK(st.st_mode)) {
549#ifdef __linux__
550 flags |= O_DIRECT;
551#endif
d603ed6c
BB
552 }
553
34dc7c2f
BB
554 if (flags & FCREAT)
555 old_umask = umask(0);
556
557 /*
558 * The construct 'flags - FREAD' conveniently maps combinations of
559 * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR.
560 */
561 fd = open64(realpath, flags - FREAD, mode);
470f12d6
G
562 if (fd == -1) {
563 err = errno;
564 free(realpath);
565 return (err);
566 }
34dc7c2f
BB
567
568 if (flags & FCREAT)
569 (void) umask(old_umask);
570
9867e8be
MA
571 if (vn_dumpdir != NULL) {
572 char *dumppath = umem_zalloc(MAXPATHLEN, UMEM_NOFAIL);
573 (void) snprintf(dumppath, MAXPATHLEN,
574 "%s/%s", vn_dumpdir, basename(realpath));
575 dump_fd = open64(dumppath, O_CREAT | O_WRONLY, 0666);
576 umem_free(dumppath, MAXPATHLEN);
577 if (dump_fd == -1) {
578 err = errno;
579 free(realpath);
580 close(fd);
581 return (err);
582 }
583 } else {
584 dump_fd = -1;
585 }
586
587 free(realpath);
588
8d4e8140 589 if (fstat64_blk(fd, &st) == -1) {
4d58b69d 590 err = errno;
34dc7c2f 591 close(fd);
e2c292bb 592 if (dump_fd != -1)
593 close(dump_fd);
4d58b69d 594 return (err);
34dc7c2f
BB
595 }
596
597 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
598
599 *vpp = vp = umem_zalloc(sizeof (vnode_t), UMEM_NOFAIL);
600
601 vp->v_fd = fd;
602 vp->v_size = st.st_size;
603 vp->v_path = spa_strdup(path);
9867e8be 604 vp->v_dump_fd = dump_fd;
34dc7c2f
BB
605
606 return (0);
607}
608
609/*ARGSUSED*/
610int
611vn_openat(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2,
612 int x3, vnode_t *startvp, int fd)
613{
614 char *realpath = umem_alloc(strlen(path) + 2, UMEM_NOFAIL);
615 int ret;
616
617 ASSERT(startvp == rootdir);
618 (void) sprintf(realpath, "/%s", path);
619
620 /* fd ignored for now, need if want to simulate nbmand support */
621 ret = vn_open(realpath, x1, flags, mode, vpp, x2, x3);
622
623 umem_free(realpath, strlen(path) + 2);
624
625 return (ret);
626}
627
628/*ARGSUSED*/
629int
630vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, offset_t offset,
e9aa730c 631 int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp)
34dc7c2f 632{
4d58b69d 633 ssize_t rc, done = 0, split;
34dc7c2f
BB
634
635 if (uio == UIO_READ) {
4d58b69d 636 rc = pread64(vp->v_fd, addr, len, offset);
470f12d6 637 if (vp->v_dump_fd != -1 && rc != -1) {
928c58dd
BB
638 int status;
639 status = pwrite64(vp->v_dump_fd, addr, rc, offset);
9867e8be
MA
640 ASSERT(status != -1);
641 }
34dc7c2f
BB
642 } else {
643 /*
644 * To simulate partial disk writes, we split writes into two
645 * system calls so that the process can be killed in between.
646 */
9ae529ec
CS
647 int sectors = len >> SPA_MINBLOCKSHIFT;
648 split = (sectors > 0 ? rand() % sectors : 0) <<
649 SPA_MINBLOCKSHIFT;
4d58b69d
RC
650 rc = pwrite64(vp->v_fd, addr, split, offset);
651 if (rc != -1) {
652 done = rc;
653 rc = pwrite64(vp->v_fd, (char *)addr + split,
654 len - split, offset + split);
655 }
34dc7c2f
BB
656 }
657
d603ed6c
BB
658#ifdef __linux__
659 if (rc == -1 && errno == EINVAL) {
660 /*
661 * Under Linux, this most likely means an alignment issue
662 * (memory or disk) due to O_DIRECT, so we abort() in order to
663 * catch the offender.
664 */
d1d7e268 665 abort();
d603ed6c
BB
666 }
667#endif
4d58b69d 668 if (rc == -1)
34dc7c2f 669 return (errno);
4d58b69d
RC
670
671 done += rc;
672
34dc7c2f 673 if (residp)
4d58b69d
RC
674 *residp = len - done;
675 else if (done != len)
34dc7c2f
BB
676 return (EIO);
677 return (0);
678}
679
680void
681vn_close(vnode_t *vp)
682{
683 close(vp->v_fd);
9867e8be
MA
684 if (vp->v_dump_fd != -1)
685 close(vp->v_dump_fd);
34dc7c2f
BB
686 spa_strfree(vp->v_path);
687 umem_free(vp, sizeof (vnode_t));
688}
689
428870ff
BB
690/*
691 * At a minimum we need to update the size since vdev_reopen()
692 * will no longer call vn_openat().
693 */
694int
695fop_getattr(vnode_t *vp, vattr_t *vap)
696{
697 struct stat64 st;
8d4e8140 698 int err;
428870ff 699
8d4e8140
RC
700 if (fstat64_blk(vp->v_fd, &st) == -1) {
701 err = errno;
428870ff 702 close(vp->v_fd);
8d4e8140 703 return (err);
428870ff
BB
704 }
705
706 vap->va_size = st.st_size;
707 return (0);
708}
709
34dc7c2f
BB
710/*
711 * =========================================================================
712 * Figure out which debugging statements to print
713 * =========================================================================
714 */
715
716static char *dprintf_string;
717static int dprintf_print_all;
718
719int
720dprintf_find_string(const char *string)
721{
722 char *tmp_str = dprintf_string;
723 int len = strlen(string);
724
725 /*
726 * Find out if this is a string we want to print.
727 * String format: file1.c,function_name1,file2.c,file3.c
728 */
729
730 while (tmp_str != NULL) {
731 if (strncmp(tmp_str, string, len) == 0 &&
732 (tmp_str[len] == ',' || tmp_str[len] == '\0'))
733 return (1);
734 tmp_str = strchr(tmp_str, ',');
735 if (tmp_str != NULL)
736 tmp_str++; /* Get rid of , */
737 }
738 return (0);
739}
740
741void
742dprintf_setup(int *argc, char **argv)
743{
744 int i, j;
745
746 /*
747 * Debugging can be specified two ways: by setting the
748 * environment variable ZFS_DEBUG, or by including a
749 * "debug=..." argument on the command line. The command
750 * line setting overrides the environment variable.
751 */
752
753 for (i = 1; i < *argc; i++) {
754 int len = strlen("debug=");
755 /* First look for a command line argument */
756 if (strncmp("debug=", argv[i], len) == 0) {
757 dprintf_string = argv[i] + len;
758 /* Remove from args */
759 for (j = i; j < *argc; j++)
760 argv[j] = argv[j+1];
761 argv[j] = NULL;
762 (*argc)--;
763 }
764 }
765
766 if (dprintf_string == NULL) {
767 /* Look for ZFS_DEBUG environment variable */
768 dprintf_string = getenv("ZFS_DEBUG");
769 }
770
771 /*
772 * Are we just turning on all debugging?
773 */
774 if (dprintf_find_string("on"))
775 dprintf_print_all = 1;
308a451f
MA
776
777 if (dprintf_string != NULL)
778 zfs_flags |= ZFS_DEBUG_DPRINTF;
34dc7c2f
BB
779}
780
781/*
782 * =========================================================================
783 * debug printfs
784 * =========================================================================
785 */
786void
ab4c009e
TC
787__dprintf(boolean_t dprint, const char *file, const char *func,
788 int line, const char *fmt, ...)
34dc7c2f
BB
789{
790 const char *newfile;
791 va_list adx;
792
793 /*
794 * Get rid of annoying "../common/" prefix to filename.
795 */
796 newfile = strrchr(file, '/');
797 if (newfile != NULL) {
798 newfile = newfile + 1; /* Get rid of leading / */
799 } else {
800 newfile = file;
801 }
802
ab4c009e
TC
803 if (dprint) {
804 /* dprintf messages are printed immediately */
805
806 if (!dprintf_print_all &&
807 !dprintf_find_string(newfile) &&
808 !dprintf_find_string(func))
809 return;
810
34dc7c2f
BB
811 /* Print out just the function name if requested */
812 flockfile(stdout);
813 if (dprintf_find_string("pid"))
814 (void) printf("%d ", getpid());
815 if (dprintf_find_string("tid"))
5ae4e448
TK
816 (void) printf("%ju ",
817 (uintmax_t)(uintptr_t)pthread_self());
34dc7c2f
BB
818 if (dprintf_find_string("cpu"))
819 (void) printf("%u ", getcpuid());
820 if (dprintf_find_string("time"))
821 (void) printf("%llu ", gethrtime());
822 if (dprintf_find_string("long"))
823 (void) printf("%s, line %d: ", newfile, line);
ab4c009e 824 (void) printf("dprintf: %s: ", func);
34dc7c2f
BB
825 va_start(adx, fmt);
826 (void) vprintf(fmt, adx);
827 va_end(adx);
828 funlockfile(stdout);
ab4c009e
TC
829 } else {
830 /* zfs_dbgmsg is logged for dumping later */
831 size_t size;
832 char *buf;
833 int i;
834
835 size = 1024;
836 buf = umem_alloc(size, UMEM_NOFAIL);
837 i = snprintf(buf, size, "%s:%d:%s(): ", newfile, line, func);
838
839 if (i < size) {
840 va_start(adx, fmt);
841 (void) vsnprintf(buf + i, size - i, fmt, adx);
842 va_end(adx);
843 }
844
845 __zfs_dbgmsg(buf);
846
847 umem_free(buf, size);
34dc7c2f
BB
848 }
849}
850
34dc7c2f
BB
851/*
852 * =========================================================================
853 * cmn_err() and panic()
854 * =========================================================================
855 */
856static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
857static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
858
859void
860vpanic(const char *fmt, va_list adx)
861{
862 (void) fprintf(stderr, "error: ");
863 (void) vfprintf(stderr, fmt, adx);
864 (void) fprintf(stderr, "\n");
865
866 abort(); /* think of it as a "user-level crash dump" */
867}
868
869void
870panic(const char *fmt, ...)
871{
872 va_list adx;
873
874 va_start(adx, fmt);
875 vpanic(fmt, adx);
876 va_end(adx);
877}
878
879void
880vcmn_err(int ce, const char *fmt, va_list adx)
881{
882 if (ce == CE_PANIC)
883 vpanic(fmt, adx);
884 if (ce != CE_NOTE) { /* suppress noise in userland stress testing */
885 (void) fprintf(stderr, "%s", ce_prefix[ce]);
886 (void) vfprintf(stderr, fmt, adx);
887 (void) fprintf(stderr, "%s", ce_suffix[ce]);
888 }
889}
890
891/*PRINTFLIKE2*/
892void
893cmn_err(int ce, const char *fmt, ...)
894{
895 va_list adx;
896
897 va_start(adx, fmt);
898 vcmn_err(ce, fmt, adx);
899 va_end(adx);
900}
901
902/*
903 * =========================================================================
904 * kobj interfaces
905 * =========================================================================
906 */
907struct _buf *
908kobj_open_file(char *name)
909{
910 struct _buf *file;
911 vnode_t *vp;
912
913 /* set vp as the _fd field of the file */
914 if (vn_openat(name, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0, rootdir,
915 -1) != 0)
916 return ((void *)-1UL);
917
918 file = umem_zalloc(sizeof (struct _buf), UMEM_NOFAIL);
919 file->_fd = (intptr_t)vp;
920 return (file);
921}
922
923int
924kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off)
925{
689f093e 926 ssize_t resid = 0;
34dc7c2f 927
957dc932
RY
928 if (vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off,
929 UIO_SYSSPACE, 0, 0, 0, &resid) != 0)
930 return (-1);
34dc7c2f
BB
931
932 return (size - resid);
933}
934
935void
936kobj_close_file(struct _buf *file)
937{
938 vn_close((vnode_t *)file->_fd);
939 umem_free(file, sizeof (struct _buf));
940}
941
942int
943kobj_get_filesize(struct _buf *file, uint64_t *size)
944{
945 struct stat64 st;
946 vnode_t *vp = (vnode_t *)file->_fd;
947
948 if (fstat64(vp->v_fd, &st) == -1) {
949 vn_close(vp);
950 return (errno);
951 }
952 *size = st.st_size;
953 return (0);
954}
955
956/*
957 * =========================================================================
958 * misc routines
959 * =========================================================================
960 */
961
962void
963delay(clock_t ticks)
964{
af4db70f 965 (void) poll(0, 0, ticks * (1000 / hz));
34dc7c2f
BB
966}
967
968/*
969 * Find highest one bit set.
46364cb2
BB
970 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
971 * The __builtin_clzll() function is supported by both GCC and Clang.
34dc7c2f
BB
972 */
973int
9bd274dd 974highbit64(uint64_t i)
34dc7c2f 975{
34dc7c2f 976 if (i == 0)
46364cb2
BB
977 return (0);
978
979 return (NBBY * sizeof (uint64_t) - __builtin_clzll(i));
34dc7c2f
BB
980}
981
193a37cb
TH
982/*
983 * Find lowest one bit set.
984 * Returns bit number + 1 of lowest bit that is set, otherwise returns 0.
46364cb2 985 * The __builtin_ffsll() function is supported by both GCC and Clang.
193a37cb
TH
986 */
987int
988lowbit64(uint64_t i)
989{
193a37cb
TH
990 if (i == 0)
991 return (0);
992
46364cb2 993 return (__builtin_ffsll(i));
0b04990a 994}
193a37cb 995
e1a0850c
BB
996char *random_path = "/dev/random";
997char *urandom_path = "/dev/urandom";
34dc7c2f
BB
998static int random_fd = -1, urandom_fd = -1;
999
0b04990a
TC
1000void
1001random_init(void)
1002{
e1a0850c
BB
1003 VERIFY((random_fd = open(random_path, O_RDONLY)) != -1);
1004 VERIFY((urandom_fd = open(urandom_path, O_RDONLY)) != -1);
0b04990a
TC
1005}
1006
1007void
1008random_fini(void)
1009{
1010 close(random_fd);
1011 close(urandom_fd);
1012
1013 random_fd = -1;
1014 urandom_fd = -1;
1015}
1016
34dc7c2f
BB
1017static int
1018random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
1019{
1020 size_t resid = len;
1021 ssize_t bytes;
1022
1023 ASSERT(fd != -1);
1024
1025 while (resid != 0) {
1026 bytes = read(fd, ptr, resid);
1027 ASSERT3S(bytes, >=, 0);
1028 ptr += bytes;
1029 resid -= bytes;
1030 }
1031
1032 return (0);
1033}
1034
1035int
1036random_get_bytes(uint8_t *ptr, size_t len)
1037{
1038 return (random_get_bytes_common(ptr, len, random_fd));
1039}
1040
1041int
1042random_get_pseudo_bytes(uint8_t *ptr, size_t len)
1043{
1044 return (random_get_bytes_common(ptr, len, urandom_fd));
1045}
1046
1047int
1048ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result)
1049{
1050 char *end;
1051
1052 *result = strtoul(hw_serial, &end, base);
1053 if (*result == 0)
1054 return (errno);
1055 return (0);
1056}
1057
428870ff
BB
1058int
1059ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
1060{
1061 char *end;
1062
1063 *result = strtoull(str, &end, base);
1064 if (*result == 0)
1065 return (errno);
1066 return (0);
1067}
1068
f0e324f2
BB
1069utsname_t *
1070utsname(void)
1071{
1072 return (&hw_utsname);
1073}
1074
34dc7c2f
BB
1075/*
1076 * =========================================================================
1077 * kernel emulation setup & teardown
1078 * =========================================================================
1079 */
1080static int
1081umem_out_of_memory(void)
1082{
1083 char errmsg[] = "out of memory -- generating core dump\n";
1084
0e5b68e0 1085 (void) fprintf(stderr, "%s", errmsg);
34dc7c2f
BB
1086 abort();
1087 return (0);
1088}
1089
1090void
1091kernel_init(int mode)
1092{
13fe0198
MA
1093 extern uint_t rrw_tsd_key;
1094
34dc7c2f
BB
1095 umem_nofail_callback(umem_out_of_memory);
1096
1097 physmem = sysconf(_SC_PHYS_PAGES);
1098
1099 dprintf("physmem = %llu pages (%.2f GB)\n", physmem,
1100 (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30));
1101
428870ff 1102 (void) snprintf(hw_serial, sizeof (hw_serial), "%ld",
53698a45 1103 (mode & FWRITE) ? get_system_hostid() : 0);
34dc7c2f 1104
0b04990a
TC
1105 random_init();
1106
f0e324f2 1107 VERIFY0(uname(&hw_utsname));
34dc7c2f 1108
b128c09f 1109 system_taskq_init();
0b04990a 1110 icp_init();
b128c09f 1111
34dc7c2f 1112 spa_init(mode);
13fe0198 1113
1eeb4562
JX
1114 fletcher_4_init();
1115
13fe0198 1116 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
34dc7c2f
BB
1117}
1118
1119void
1120kernel_fini(void)
1121{
1eeb4562 1122 fletcher_4_fini();
34dc7c2f
BB
1123 spa_fini();
1124
0b04990a 1125 icp_fini();
428870ff
BB
1126 system_taskq_fini();
1127
0b04990a 1128 random_fini();
34dc7c2f
BB
1129}
1130
34dc7c2f
BB
1131uid_t
1132crgetuid(cred_t *cr)
1133{
1134 return (0);
1135}
1136
6f1ffb06
MA
1137uid_t
1138crgetruid(cred_t *cr)
1139{
1140 return (0);
1141}
1142
34dc7c2f
BB
1143gid_t
1144crgetgid(cred_t *cr)
1145{
1146 return (0);
1147}
1148
1149int
1150crgetngroups(cred_t *cr)
1151{
1152 return (0);
1153}
1154
1155gid_t *
1156crgetgroups(cred_t *cr)
1157{
1158 return (NULL);
1159}
1160
1161int
1162zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
1163{
1164 return (0);
1165}
1166
1167int
1168zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
1169{
1170 return (0);
1171}
1172
1173int
1174zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
1175{
1176 return (0);
1177}
1178
f74b821a
BB
1179int
1180secpolicy_zfs(const cred_t *cr)
1181{
1182 return (0);
1183}
1184
34dc7c2f
BB
1185ksiddomain_t *
1186ksid_lookupdomain(const char *dom)
1187{
1188 ksiddomain_t *kd;
1189
1190 kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL);
1191 kd->kd_name = spa_strdup(dom);
1192 return (kd);
1193}
1194
1195void
1196ksiddomain_rele(ksiddomain_t *ksid)
1197{
1198 spa_strfree(ksid->kd_name);
1199 umem_free(ksid, sizeof (ksiddomain_t));
1200}
428870ff 1201
428870ff 1202char *
00b46022 1203kmem_vasprintf(const char *fmt, va_list adx)
428870ff 1204{
00b46022
BB
1205 char *buf = NULL;
1206 va_list adx_copy;
428870ff 1207
00b46022
BB
1208 va_copy(adx_copy, adx);
1209 VERIFY(vasprintf(&buf, fmt, adx_copy) != -1);
1210 va_end(adx_copy);
428870ff 1211
00b46022
BB
1212 return (buf);
1213}
1214
1215char *
1216kmem_asprintf(const char *fmt, ...)
1217{
1218 char *buf = NULL;
1219 va_list adx;
428870ff
BB
1220
1221 va_start(adx, fmt);
00b46022 1222 VERIFY(vasprintf(&buf, fmt, adx) != -1);
428870ff
BB
1223 va_end(adx);
1224
1225 return (buf);
1226}
572e2857
BB
1227
1228/* ARGSUSED */
1229int
1230zfs_onexit_fd_hold(int fd, minor_t *minorp)
1231{
1232 *minorp = 0;
1233 return (0);
1234}
1235
1236/* ARGSUSED */
1237void
1238zfs_onexit_fd_rele(int fd)
1239{
1240}
1241
1242/* ARGSUSED */
1243int
1244zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data,
1245 uint64_t *action_handle)
1246{
1247 return (0);
1248}
1249
1250/* ARGSUSED */
1251int
1252zfs_onexit_del_cb(minor_t minor, uint64_t action_handle, boolean_t fire)
1253{
1254 return (0);
1255}
1256
1257/* ARGSUSED */
1258int
1259zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data)
1260{
1261 return (0);
1262}
92119cc2
BB
1263
1264fstrans_cookie_t
1265spl_fstrans_mark(void)
1266{
02730c33 1267 return ((fstrans_cookie_t)0);
92119cc2
BB
1268}
1269
1270void
1271spl_fstrans_unmark(fstrans_cookie_t cookie)
1272{
1273}
1274
1275int
e624cd19 1276__spl_pf_fstrans_check(void)
92119cc2
BB
1277{
1278 return (0);
1279}
a0bd735a 1280
3ec34e55
BL
1281int
1282kmem_cache_reap_active(void)
1283{
1284 return (0);
1285}
1286
47dfff3b
MA
1287void *zvol_tag = "zvol_tag";
1288
a0bd735a
BP
1289void
1290zvol_create_minors(spa_t *spa, const char *name, boolean_t async)
1291{
1292}
1293
1294void
1295zvol_remove_minor(spa_t *spa, const char *name, boolean_t async)
1296{
1297}
1298
1299void
1300zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
1301{
1302}
1303
1304void
1305zvol_rename_minors(spa_t *spa, const char *oldname, const char *newname,
1306 boolean_t async)
1307{
1308}