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