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