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