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