]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libzpool/kernel.c
Illumos #3522
[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 *
227kstat_create(char *module, int instance, char *name, char *class,
228 uchar_t type, ulong_t ndata, uchar_t ks_flag)
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
PS
243/*ARGSUSED*/
244void
245kstat_set_raw_ops(kstat_t *ksp,
246 int (*headers)(char *buf, size_t size),
247 int (*data)(char *buf, size_t size, void *data),
248 void *(*addr)(kstat_t *ksp, loff_t index))
249{}
250
34dc7c2f
BB
251/*
252 * =========================================================================
253 * mutexes
254 * =========================================================================
255 */
1e33ac1e 256
34dc7c2f 257void
1e33ac1e 258mutex_init(kmutex_t *mp, char *name, int type, void *cookie)
34dc7c2f 259{
1e33ac1e
BB
260 ASSERT3S(type, ==, MUTEX_DEFAULT);
261 ASSERT3P(cookie, ==, NULL);
262 mp->m_owner = MTX_INIT;
263 mp->m_magic = MTX_MAGIC;
264 VERIFY3S(pthread_mutex_init(&mp->m_lock, NULL), ==, 0);
34dc7c2f
BB
265}
266
267void
1e33ac1e 268mutex_destroy(kmutex_t *mp)
34dc7c2f 269{
1e33ac1e
BB
270 ASSERT3U(mp->m_magic, ==, MTX_MAGIC);
271 ASSERT3P(mp->m_owner, ==, MTX_INIT);
272 VERIFY3S(pthread_mutex_destroy(&(mp)->m_lock), ==, 0);
273 mp->m_owner = MTX_DEST;
274 mp->m_magic = 0;
34dc7c2f
BB
275}
276
277void
278mutex_enter(kmutex_t *mp)
279{
1e33ac1e
BB
280 ASSERT3U(mp->m_magic, ==, MTX_MAGIC);
281 ASSERT3P(mp->m_owner, !=, MTX_DEST);
282 ASSERT3P(mp->m_owner, !=, curthread);
283 VERIFY3S(pthread_mutex_lock(&mp->m_lock), ==, 0);
284 ASSERT3P(mp->m_owner, ==, MTX_INIT);
34dc7c2f
BB
285 mp->m_owner = curthread;
286}
287
288int
289mutex_tryenter(kmutex_t *mp)
290{
1e33ac1e
BB
291 ASSERT3U(mp->m_magic, ==, MTX_MAGIC);
292 ASSERT3P(mp->m_owner, !=, MTX_DEST);
293 if (0 == pthread_mutex_trylock(&mp->m_lock)) {
294 ASSERT3P(mp->m_owner, ==, MTX_INIT);
34dc7c2f
BB
295 mp->m_owner = curthread;
296 return (1);
297 } else {
298 return (0);
299 }
300}
301
302void
303mutex_exit(kmutex_t *mp)
304{
1e33ac1e
BB
305 ASSERT3U(mp->m_magic, ==, MTX_MAGIC);
306 ASSERT3P(mutex_owner(mp), ==, curthread);
307 mp->m_owner = MTX_INIT;
308 VERIFY3S(pthread_mutex_unlock(&mp->m_lock), ==, 0);
34dc7c2f
BB
309}
310
311void *
312mutex_owner(kmutex_t *mp)
313{
1e33ac1e 314 ASSERT3U(mp->m_magic, ==, MTX_MAGIC);
34dc7c2f
BB
315 return (mp->m_owner);
316}
317
1e33ac1e
BB
318int
319mutex_held(kmutex_t *mp)
320{
321 return (mp->m_owner == curthread);
322}
323
34dc7c2f
BB
324/*
325 * =========================================================================
326 * rwlocks
327 * =========================================================================
328 */
1e33ac1e 329
34dc7c2f
BB
330void
331rw_init(krwlock_t *rwlp, char *name, int type, void *arg)
332{
1e33ac1e
BB
333 ASSERT3S(type, ==, RW_DEFAULT);
334 ASSERT3P(arg, ==, NULL);
335 VERIFY3S(pthread_rwlock_init(&rwlp->rw_lock, NULL), ==, 0);
336 rwlp->rw_owner = RW_INIT;
337 rwlp->rw_wr_owner = RW_INIT;
338 rwlp->rw_readers = 0;
339 rwlp->rw_magic = RW_MAGIC;
34dc7c2f
BB
340}
341
342void
343rw_destroy(krwlock_t *rwlp)
344{
1e33ac1e
BB
345 ASSERT3U(rwlp->rw_magic, ==, RW_MAGIC);
346
347 VERIFY3S(pthread_rwlock_destroy(&rwlp->rw_lock), ==, 0);
348 rwlp->rw_magic = 0;
34dc7c2f
BB
349}
350
351void
352rw_enter(krwlock_t *rwlp, krw_t rw)
353{
1e33ac1e
BB
354 ASSERT3U(rwlp->rw_magic, ==, RW_MAGIC);
355 ASSERT3P(rwlp->rw_owner, !=, curthread);
356 ASSERT3P(rwlp->rw_wr_owner, !=, curthread);
34dc7c2f 357
1e33ac1e
BB
358 if (rw == RW_READER) {
359 VERIFY3S(pthread_rwlock_rdlock(&rwlp->rw_lock), ==, 0);
360 ASSERT3P(rwlp->rw_wr_owner, ==, RW_INIT);
361
362 atomic_inc_uint(&rwlp->rw_readers);
363 } else {
364 VERIFY3S(pthread_rwlock_wrlock(&rwlp->rw_lock), ==, 0);
365 ASSERT3P(rwlp->rw_wr_owner, ==, RW_INIT);
366 ASSERT3U(rwlp->rw_readers, ==, 0);
367
368 rwlp->rw_wr_owner = curthread;
369 }
34dc7c2f
BB
370
371 rwlp->rw_owner = curthread;
372}
373
374void
375rw_exit(krwlock_t *rwlp)
376{
1e33ac1e
BB
377 ASSERT3U(rwlp->rw_magic, ==, RW_MAGIC);
378 ASSERT(RW_LOCK_HELD(rwlp));
379
380 if (RW_READ_HELD(rwlp))
381 atomic_dec_uint(&rwlp->rw_readers);
382 else
383 rwlp->rw_wr_owner = RW_INIT;
34dc7c2f 384
1e33ac1e
BB
385 rwlp->rw_owner = RW_INIT;
386 VERIFY3S(pthread_rwlock_unlock(&rwlp->rw_lock), ==, 0);
34dc7c2f
BB
387}
388
389int
390rw_tryenter(krwlock_t *rwlp, krw_t rw)
391{
392 int rv;
393
1e33ac1e 394 ASSERT3U(rwlp->rw_magic, ==, RW_MAGIC);
34dc7c2f
BB
395
396 if (rw == RW_READER)
1e33ac1e 397 rv = pthread_rwlock_tryrdlock(&rwlp->rw_lock);
34dc7c2f 398 else
1e33ac1e 399 rv = pthread_rwlock_trywrlock(&rwlp->rw_lock);
34dc7c2f
BB
400
401 if (rv == 0) {
1e33ac1e
BB
402 ASSERT3P(rwlp->rw_wr_owner, ==, RW_INIT);
403
404 if (rw == RW_READER)
405 atomic_inc_uint(&rwlp->rw_readers);
406 else {
407 ASSERT3U(rwlp->rw_readers, ==, 0);
408 rwlp->rw_wr_owner = curthread;
409 }
410
34dc7c2f
BB
411 rwlp->rw_owner = curthread;
412 return (1);
413 }
414
1e33ac1e
BB
415 VERIFY3S(rv, ==, EBUSY);
416
34dc7c2f
BB
417 return (0);
418}
419
34dc7c2f
BB
420int
421rw_tryupgrade(krwlock_t *rwlp)
422{
1e33ac1e 423 ASSERT3U(rwlp->rw_magic, ==, RW_MAGIC);
34dc7c2f
BB
424
425 return (0);
426}
427
428/*
429 * =========================================================================
430 * condition variables
431 * =========================================================================
432 */
1e33ac1e 433
34dc7c2f
BB
434void
435cv_init(kcondvar_t *cv, char *name, int type, void *arg)
436{
1e33ac1e
BB
437 ASSERT3S(type, ==, CV_DEFAULT);
438 cv->cv_magic = CV_MAGIC;
439 VERIFY3S(pthread_cond_init(&cv->cv, NULL), ==, 0);
34dc7c2f
BB
440}
441
442void
443cv_destroy(kcondvar_t *cv)
444{
1e33ac1e
BB
445 ASSERT3U(cv->cv_magic, ==, CV_MAGIC);
446 VERIFY3S(pthread_cond_destroy(&cv->cv), ==, 0);
447 cv->cv_magic = 0;
34dc7c2f
BB
448}
449
450void
451cv_wait(kcondvar_t *cv, kmutex_t *mp)
452{
1e33ac1e
BB
453 ASSERT3U(cv->cv_magic, ==, CV_MAGIC);
454 ASSERT3P(mutex_owner(mp), ==, curthread);
455 mp->m_owner = MTX_INIT;
456 int ret = pthread_cond_wait(&cv->cv, &mp->m_lock);
457 if (ret != 0)
458 VERIFY3S(ret, ==, EINTR);
34dc7c2f
BB
459 mp->m_owner = curthread;
460}
461
462clock_t
463cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime)
464{
465 int error;
1e33ac1e 466 struct timeval tv;
34dc7c2f
BB
467 timestruc_t ts;
468 clock_t delta;
469
1e33ac1e
BB
470 ASSERT3U(cv->cv_magic, ==, CV_MAGIC);
471
34dc7c2f 472top:
428870ff 473 delta = abstime - ddi_get_lbolt();
34dc7c2f
BB
474 if (delta <= 0)
475 return (-1);
476
1e33ac1e
BB
477 VERIFY(gettimeofday(&tv, NULL) == 0);
478
479 ts.tv_sec = tv.tv_sec + delta / hz;
480 ts.tv_nsec = tv.tv_usec * 1000 + (delta % hz) * (NANOSEC / hz);
481 if (ts.tv_nsec >= NANOSEC) {
482 ts.tv_sec++;
483 ts.tv_nsec -= NANOSEC;
484 }
34dc7c2f 485
1e33ac1e
BB
486 ASSERT3P(mutex_owner(mp), ==, curthread);
487 mp->m_owner = MTX_INIT;
488 error = pthread_cond_timedwait(&cv->cv, &mp->m_lock, &ts);
34dc7c2f
BB
489 mp->m_owner = curthread;
490
1e33ac1e 491 if (error == ETIMEDOUT)
34dc7c2f
BB
492 return (-1);
493
494 if (error == EINTR)
495 goto top;
496
1e33ac1e 497 VERIFY3S(error, ==, 0);
34dc7c2f
BB
498
499 return (1);
500}
501
502void
503cv_signal(kcondvar_t *cv)
504{
1e33ac1e
BB
505 ASSERT3U(cv->cv_magic, ==, CV_MAGIC);
506 VERIFY3S(pthread_cond_signal(&cv->cv), ==, 0);
34dc7c2f
BB
507}
508
509void
510cv_broadcast(kcondvar_t *cv)
511{
1e33ac1e
BB
512 ASSERT3U(cv->cv_magic, ==, CV_MAGIC);
513 VERIFY3S(pthread_cond_broadcast(&cv->cv), ==, 0);
34dc7c2f
BB
514}
515
516/*
517 * =========================================================================
518 * vnode operations
519 * =========================================================================
520 */
521/*
522 * Note: for the xxxat() versions of these functions, we assume that the
523 * starting vp is always rootdir (which is true for spa_directory.c, the only
524 * ZFS consumer of these interfaces). We assert this is true, and then emulate
525 * them by adding '/' in front of the path.
526 */
527
528/*ARGSUSED*/
529int
530vn_open(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2, int x3)
531{
532 int fd;
533 vnode_t *vp;
a4914d38 534 int old_umask = 0;
5ae4e2c2 535 char *realpath;
34dc7c2f 536 struct stat64 st;
4d58b69d 537 int err;
34dc7c2f 538
5ae4e2c2
BB
539 realpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
540
34dc7c2f
BB
541 /*
542 * If we're accessing a real disk from userland, we need to use
543 * the character interface to avoid caching. This is particularly
544 * important if we're trying to look at a real in-kernel storage
545 * pool from userland, e.g. via zdb, because otherwise we won't
546 * see the changes occurring under the segmap cache.
547 * On the other hand, the stupid character device returns zero
548 * for its size. So -- gag -- we open the block device to get
549 * its size, and remember it for subsequent VOP_GETATTR().
550 */
d603ed6c 551#if defined(__sun__) || defined(__sun)
34dc7c2f 552 if (strncmp(path, "/dev/", 5) == 0) {
d603ed6c
BB
553#else
554 if (0) {
555#endif
34dc7c2f
BB
556 char *dsk;
557 fd = open64(path, O_RDONLY);
5ae4e2c2
BB
558 if (fd == -1) {
559 err = errno;
560 free(realpath);
561 return (err);
562 }
34dc7c2f 563 if (fstat64(fd, &st) == -1) {
5ae4e2c2 564 err = errno;
34dc7c2f 565 close(fd);
5ae4e2c2
BB
566 free(realpath);
567 return (err);
34dc7c2f
BB
568 }
569 close(fd);
570 (void) sprintf(realpath, "%s", path);
571 dsk = strstr(path, "/dsk/");
572 if (dsk != NULL)
573 (void) sprintf(realpath + (dsk - path) + 1, "r%s",
574 dsk + 1);
575 } else {
576 (void) sprintf(realpath, "%s", path);
5ae4e2c2
BB
577 if (!(flags & FCREAT) && stat64(realpath, &st) == -1) {
578 err = errno;
579 free(realpath);
580 return (err);
581 }
34dc7c2f
BB
582 }
583
d603ed6c
BB
584 if (!(flags & FCREAT) && S_ISBLK(st.st_mode)) {
585#ifdef __linux__
586 flags |= O_DIRECT;
587#endif
588 /* We shouldn't be writing to block devices in userspace */
589 VERIFY(!(flags & FWRITE));
590 }
591
34dc7c2f
BB
592 if (flags & FCREAT)
593 old_umask = umask(0);
594
595 /*
596 * The construct 'flags - FREAD' conveniently maps combinations of
597 * FREAD and FWRITE to the corresponding O_RDONLY, O_WRONLY, and O_RDWR.
598 */
599 fd = open64(realpath, flags - FREAD, mode);
5ae4e2c2 600 free(realpath);
34dc7c2f
BB
601
602 if (flags & FCREAT)
603 (void) umask(old_umask);
604
605 if (fd == -1)
606 return (errno);
607
8d4e8140 608 if (fstat64_blk(fd, &st) == -1) {
4d58b69d 609 err = errno;
34dc7c2f 610 close(fd);
4d58b69d 611 return (err);
34dc7c2f
BB
612 }
613
614 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
615
616 *vpp = vp = umem_zalloc(sizeof (vnode_t), UMEM_NOFAIL);
617
618 vp->v_fd = fd;
619 vp->v_size = st.st_size;
620 vp->v_path = spa_strdup(path);
621
622 return (0);
623}
624
625/*ARGSUSED*/
626int
627vn_openat(char *path, int x1, int flags, int mode, vnode_t **vpp, int x2,
628 int x3, vnode_t *startvp, int fd)
629{
630 char *realpath = umem_alloc(strlen(path) + 2, UMEM_NOFAIL);
631 int ret;
632
633 ASSERT(startvp == rootdir);
634 (void) sprintf(realpath, "/%s", path);
635
636 /* fd ignored for now, need if want to simulate nbmand support */
637 ret = vn_open(realpath, x1, flags, mode, vpp, x2, x3);
638
639 umem_free(realpath, strlen(path) + 2);
640
641 return (ret);
642}
643
644/*ARGSUSED*/
645int
646vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len, offset_t offset,
647 int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp)
648{
4d58b69d 649 ssize_t rc, done = 0, split;
34dc7c2f
BB
650
651 if (uio == UIO_READ) {
4d58b69d 652 rc = pread64(vp->v_fd, addr, len, offset);
34dc7c2f
BB
653 } else {
654 /*
655 * To simulate partial disk writes, we split writes into two
656 * system calls so that the process can be killed in between.
657 */
9ae529ec
CS
658 int sectors = len >> SPA_MINBLOCKSHIFT;
659 split = (sectors > 0 ? rand() % sectors : 0) <<
660 SPA_MINBLOCKSHIFT;
4d58b69d
RC
661 rc = pwrite64(vp->v_fd, addr, split, offset);
662 if (rc != -1) {
663 done = rc;
664 rc = pwrite64(vp->v_fd, (char *)addr + split,
665 len - split, offset + split);
666 }
34dc7c2f
BB
667 }
668
d603ed6c
BB
669#ifdef __linux__
670 if (rc == -1 && errno == EINVAL) {
671 /*
672 * Under Linux, this most likely means an alignment issue
673 * (memory or disk) due to O_DIRECT, so we abort() in order to
674 * catch the offender.
675 */
676 abort();
677 }
678#endif
4d58b69d 679 if (rc == -1)
34dc7c2f 680 return (errno);
4d58b69d
RC
681
682 done += rc;
683
34dc7c2f 684 if (residp)
4d58b69d
RC
685 *residp = len - done;
686 else if (done != len)
34dc7c2f
BB
687 return (EIO);
688 return (0);
689}
690
691void
692vn_close(vnode_t *vp)
693{
694 close(vp->v_fd);
695 spa_strfree(vp->v_path);
696 umem_free(vp, sizeof (vnode_t));
697}
698
428870ff
BB
699/*
700 * At a minimum we need to update the size since vdev_reopen()
701 * will no longer call vn_openat().
702 */
703int
704fop_getattr(vnode_t *vp, vattr_t *vap)
705{
706 struct stat64 st;
8d4e8140 707 int err;
428870ff 708
8d4e8140
RC
709 if (fstat64_blk(vp->v_fd, &st) == -1) {
710 err = errno;
428870ff 711 close(vp->v_fd);
8d4e8140 712 return (err);
428870ff
BB
713 }
714
715 vap->va_size = st.st_size;
716 return (0);
717}
718
34dc7c2f
BB
719/*
720 * =========================================================================
721 * Figure out which debugging statements to print
722 * =========================================================================
723 */
724
725static char *dprintf_string;
726static int dprintf_print_all;
727
728int
729dprintf_find_string(const char *string)
730{
731 char *tmp_str = dprintf_string;
732 int len = strlen(string);
733
734 /*
735 * Find out if this is a string we want to print.
736 * String format: file1.c,function_name1,file2.c,file3.c
737 */
738
739 while (tmp_str != NULL) {
740 if (strncmp(tmp_str, string, len) == 0 &&
741 (tmp_str[len] == ',' || tmp_str[len] == '\0'))
742 return (1);
743 tmp_str = strchr(tmp_str, ',');
744 if (tmp_str != NULL)
745 tmp_str++; /* Get rid of , */
746 }
747 return (0);
748}
749
750void
751dprintf_setup(int *argc, char **argv)
752{
753 int i, j;
754
755 /*
756 * Debugging can be specified two ways: by setting the
757 * environment variable ZFS_DEBUG, or by including a
758 * "debug=..." argument on the command line. The command
759 * line setting overrides the environment variable.
760 */
761
762 for (i = 1; i < *argc; i++) {
763 int len = strlen("debug=");
764 /* First look for a command line argument */
765 if (strncmp("debug=", argv[i], len) == 0) {
766 dprintf_string = argv[i] + len;
767 /* Remove from args */
768 for (j = i; j < *argc; j++)
769 argv[j] = argv[j+1];
770 argv[j] = NULL;
771 (*argc)--;
772 }
773 }
774
775 if (dprintf_string == NULL) {
776 /* Look for ZFS_DEBUG environment variable */
777 dprintf_string = getenv("ZFS_DEBUG");
778 }
779
780 /*
781 * Are we just turning on all debugging?
782 */
783 if (dprintf_find_string("on"))
784 dprintf_print_all = 1;
785}
786
787/*
788 * =========================================================================
789 * debug printfs
790 * =========================================================================
791 */
792void
793__dprintf(const char *file, const char *func, int line, const char *fmt, ...)
794{
795 const char *newfile;
796 va_list adx;
797
798 /*
799 * Get rid of annoying "../common/" prefix to filename.
800 */
801 newfile = strrchr(file, '/');
802 if (newfile != NULL) {
803 newfile = newfile + 1; /* Get rid of leading / */
804 } else {
805 newfile = file;
806 }
807
808 if (dprintf_print_all ||
809 dprintf_find_string(newfile) ||
810 dprintf_find_string(func)) {
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"))
1e33ac1e 816 (void) printf("%u ", (uint_t) pthread_self());
34dc7c2f
BB
817 if (dprintf_find_string("cpu"))
818 (void) printf("%u ", getcpuid());
819 if (dprintf_find_string("time"))
820 (void) printf("%llu ", gethrtime());
821 if (dprintf_find_string("long"))
822 (void) printf("%s, line %d: ", newfile, line);
823 (void) printf("%s: ", func);
824 va_start(adx, fmt);
825 (void) vprintf(fmt, adx);
826 va_end(adx);
827 funlockfile(stdout);
828 }
829}
830
34dc7c2f
BB
831/*
832 * =========================================================================
833 * cmn_err() and panic()
834 * =========================================================================
835 */
836static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
837static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
838
839void
840vpanic(const char *fmt, va_list adx)
841{
842 (void) fprintf(stderr, "error: ");
843 (void) vfprintf(stderr, fmt, adx);
844 (void) fprintf(stderr, "\n");
845
846 abort(); /* think of it as a "user-level crash dump" */
847}
848
849void
850panic(const char *fmt, ...)
851{
852 va_list adx;
853
854 va_start(adx, fmt);
855 vpanic(fmt, adx);
856 va_end(adx);
857}
858
859void
860vcmn_err(int ce, const char *fmt, va_list adx)
861{
862 if (ce == CE_PANIC)
863 vpanic(fmt, adx);
864 if (ce != CE_NOTE) { /* suppress noise in userland stress testing */
865 (void) fprintf(stderr, "%s", ce_prefix[ce]);
866 (void) vfprintf(stderr, fmt, adx);
867 (void) fprintf(stderr, "%s", ce_suffix[ce]);
868 }
869}
870
871/*PRINTFLIKE2*/
872void
873cmn_err(int ce, const char *fmt, ...)
874{
875 va_list adx;
876
877 va_start(adx, fmt);
878 vcmn_err(ce, fmt, adx);
879 va_end(adx);
880}
881
882/*
883 * =========================================================================
884 * kobj interfaces
885 * =========================================================================
886 */
887struct _buf *
888kobj_open_file(char *name)
889{
890 struct _buf *file;
891 vnode_t *vp;
892
893 /* set vp as the _fd field of the file */
894 if (vn_openat(name, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0, rootdir,
895 -1) != 0)
896 return ((void *)-1UL);
897
898 file = umem_zalloc(sizeof (struct _buf), UMEM_NOFAIL);
899 file->_fd = (intptr_t)vp;
900 return (file);
901}
902
903int
904kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off)
905{
906 ssize_t resid;
907
908 vn_rdwr(UIO_READ, (vnode_t *)file->_fd, buf, size, (offset_t)off,
909 UIO_SYSSPACE, 0, 0, 0, &resid);
910
911 return (size - resid);
912}
913
914void
915kobj_close_file(struct _buf *file)
916{
917 vn_close((vnode_t *)file->_fd);
918 umem_free(file, sizeof (struct _buf));
919}
920
921int
922kobj_get_filesize(struct _buf *file, uint64_t *size)
923{
924 struct stat64 st;
925 vnode_t *vp = (vnode_t *)file->_fd;
926
927 if (fstat64(vp->v_fd, &st) == -1) {
928 vn_close(vp);
929 return (errno);
930 }
931 *size = st.st_size;
932 return (0);
933}
934
935/*
936 * =========================================================================
937 * misc routines
938 * =========================================================================
939 */
940
941void
942delay(clock_t ticks)
943{
944 poll(0, 0, ticks * (1000 / hz));
945}
946
947/*
948 * Find highest one bit set.
949 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
950 * High order bit is 31 (or 63 in _LP64 kernel).
951 */
952int
953highbit(ulong_t i)
954{
955 register int h = 1;
956
957 if (i == 0)
958 return (0);
959#ifdef _LP64
960 if (i & 0xffffffff00000000ul) {
961 h += 32; i >>= 32;
962 }
963#endif
964 if (i & 0xffff0000) {
965 h += 16; i >>= 16;
966 }
967 if (i & 0xff00) {
968 h += 8; i >>= 8;
969 }
970 if (i & 0xf0) {
971 h += 4; i >>= 4;
972 }
973 if (i & 0xc) {
974 h += 2; i >>= 2;
975 }
976 if (i & 0x2) {
977 h += 1;
978 }
979 return (h);
980}
981
982static int random_fd = -1, urandom_fd = -1;
983
984static int
985random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
986{
987 size_t resid = len;
988 ssize_t bytes;
989
990 ASSERT(fd != -1);
991
992 while (resid != 0) {
993 bytes = read(fd, ptr, resid);
994 ASSERT3S(bytes, >=, 0);
995 ptr += bytes;
996 resid -= bytes;
997 }
998
999 return (0);
1000}
1001
1002int
1003random_get_bytes(uint8_t *ptr, size_t len)
1004{
1005 return (random_get_bytes_common(ptr, len, random_fd));
1006}
1007
1008int
1009random_get_pseudo_bytes(uint8_t *ptr, size_t len)
1010{
1011 return (random_get_bytes_common(ptr, len, urandom_fd));
1012}
1013
1014int
1015ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result)
1016{
1017 char *end;
1018
1019 *result = strtoul(hw_serial, &end, base);
1020 if (*result == 0)
1021 return (errno);
1022 return (0);
1023}
1024
428870ff
BB
1025int
1026ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
1027{
1028 char *end;
1029
1030 *result = strtoull(str, &end, base);
1031 if (*result == 0)
1032 return (errno);
1033 return (0);
1034}
1035
34dc7c2f
BB
1036/*
1037 * =========================================================================
1038 * kernel emulation setup & teardown
1039 * =========================================================================
1040 */
1041static int
1042umem_out_of_memory(void)
1043{
1044 char errmsg[] = "out of memory -- generating core dump\n";
1045
0e5b68e0 1046 (void) fprintf(stderr, "%s", errmsg);
34dc7c2f
BB
1047 abort();
1048 return (0);
1049}
1050
1051void
1052kernel_init(int mode)
1053{
13fe0198
MA
1054 extern uint_t rrw_tsd_key;
1055
34dc7c2f
BB
1056 umem_nofail_callback(umem_out_of_memory);
1057
1058 physmem = sysconf(_SC_PHYS_PAGES);
1059
1060 dprintf("physmem = %llu pages (%.2f GB)\n", physmem,
1061 (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30));
1062
428870ff
BB
1063 (void) snprintf(hw_serial, sizeof (hw_serial), "%ld",
1064 (mode & FWRITE) ? gethostid() : 0);
34dc7c2f
BB
1065
1066 VERIFY((random_fd = open("/dev/random", O_RDONLY)) != -1);
1067 VERIFY((urandom_fd = open("/dev/urandom", O_RDONLY)) != -1);
1068
1e33ac1e 1069 thread_init();
b128c09f
BB
1070 system_taskq_init();
1071
34dc7c2f 1072 spa_init(mode);
13fe0198
MA
1073
1074 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
34dc7c2f
BB
1075}
1076
1077void
1078kernel_fini(void)
1079{
1080 spa_fini();
1081
428870ff 1082 system_taskq_fini();
1e33ac1e 1083 thread_fini();
428870ff 1084
34dc7c2f
BB
1085 close(random_fd);
1086 close(urandom_fd);
1087
1088 random_fd = -1;
1089 urandom_fd = -1;
1090}
1091
34dc7c2f
BB
1092uid_t
1093crgetuid(cred_t *cr)
1094{
1095 return (0);
1096}
1097
6f1ffb06
MA
1098uid_t
1099crgetruid(cred_t *cr)
1100{
1101 return (0);
1102}
1103
34dc7c2f
BB
1104gid_t
1105crgetgid(cred_t *cr)
1106{
1107 return (0);
1108}
1109
1110int
1111crgetngroups(cred_t *cr)
1112{
1113 return (0);
1114}
1115
1116gid_t *
1117crgetgroups(cred_t *cr)
1118{
1119 return (NULL);
1120}
1121
1122int
1123zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
1124{
1125 return (0);
1126}
1127
1128int
1129zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
1130{
1131 return (0);
1132}
1133
1134int
1135zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
1136{
1137 return (0);
1138}
1139
1140ksiddomain_t *
1141ksid_lookupdomain(const char *dom)
1142{
1143 ksiddomain_t *kd;
1144
1145 kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL);
1146 kd->kd_name = spa_strdup(dom);
1147 return (kd);
1148}
1149
1150void
1151ksiddomain_rele(ksiddomain_t *ksid)
1152{
1153 spa_strfree(ksid->kd_name);
1154 umem_free(ksid, sizeof (ksiddomain_t));
1155}
428870ff 1156
428870ff 1157char *
00b46022 1158kmem_vasprintf(const char *fmt, va_list adx)
428870ff 1159{
00b46022
BB
1160 char *buf = NULL;
1161 va_list adx_copy;
428870ff 1162
00b46022
BB
1163 va_copy(adx_copy, adx);
1164 VERIFY(vasprintf(&buf, fmt, adx_copy) != -1);
1165 va_end(adx_copy);
428870ff 1166
00b46022
BB
1167 return (buf);
1168}
1169
1170char *
1171kmem_asprintf(const char *fmt, ...)
1172{
1173 char *buf = NULL;
1174 va_list adx;
428870ff
BB
1175
1176 va_start(adx, fmt);
00b46022 1177 VERIFY(vasprintf(&buf, fmt, adx) != -1);
428870ff
BB
1178 va_end(adx);
1179
1180 return (buf);
1181}
572e2857
BB
1182
1183/* ARGSUSED */
1184int
1185zfs_onexit_fd_hold(int fd, minor_t *minorp)
1186{
1187 *minorp = 0;
1188 return (0);
1189}
1190
1191/* ARGSUSED */
1192void
1193zfs_onexit_fd_rele(int fd)
1194{
1195}
1196
1197/* ARGSUSED */
1198int
1199zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data,
1200 uint64_t *action_handle)
1201{
1202 return (0);
1203}
1204
1205/* ARGSUSED */
1206int
1207zfs_onexit_del_cb(minor_t minor, uint64_t action_handle, boolean_t fire)
1208{
1209 return (0);
1210}
1211
1212/* ARGSUSED */
1213int
1214zfs_onexit_cb_data(minor_t minor, uint64_t action_handle, void **data)
1215{
1216 return (0);
1217}