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/*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
24 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
25 */
26
27#include <assert.h>
28#include <fcntl.h>
29#include <libgen.h>
30#include <poll.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <sys/crypto/icp.h>
35#include <sys/processor.h>
36#include <sys/rrwlock.h>
37#include <sys/spa.h>
38#include <sys/stat.h>
39#include <sys/systeminfo.h>
40#include <sys/time.h>
41#include <sys/utsname.h>
42#include <sys/zfs_context.h>
43#include <sys/zfs_onexit.h>
44#include <sys/zfs_vfsops.h>
45#include <sys/zstd/zstd.h>
46#include <sys/zvol.h>
47#include <zfs_fletcher.h>
48#include <zlib.h>
49
50/*
51 * Emulation of kernel services in userland.
52 */
53
54uint64_t physmem;
55char hw_serial[HW_HOSTID_LEN];
56struct utsname hw_utsname;
57
58/* If set, all blocks read will be copied to the specified directory. */
59char *vn_dumpdir = NULL;
60
61/* this only exists to have its address taken */
62struct proc p0;
63
64/*
65 * =========================================================================
66 * threads
67 * =========================================================================
68 *
69 * TS_STACK_MIN is dictated by the minimum allowed pthread stack size.  While
70 * TS_STACK_MAX is somewhat arbitrary, it was selected to be large enough for
71 * the expected stack depth while small enough to avoid exhausting address
72 * space with high thread counts.
73 */
74#define	TS_STACK_MIN	MAX(PTHREAD_STACK_MIN, 32768)
75#define	TS_STACK_MAX	(256 * 1024)
76
77/*ARGSUSED*/
78kthread_t *
79zk_thread_create(void (*func)(void *), void *arg, size_t stksize, int state)
80{
81	pthread_attr_t attr;
82	pthread_t tid;
83	char *stkstr;
84	int detachstate = PTHREAD_CREATE_DETACHED;
85
86	VERIFY0(pthread_attr_init(&attr));
87
88	if (state & TS_JOINABLE)
89		detachstate = PTHREAD_CREATE_JOINABLE;
90
91	VERIFY0(pthread_attr_setdetachstate(&attr, detachstate));
92
93	/*
94	 * We allow the default stack size in user space to be specified by
95	 * setting the ZFS_STACK_SIZE environment variable.  This allows us
96	 * the convenience of observing and debugging stack overruns in
97	 * user space.  Explicitly specified stack sizes will be honored.
98	 * The usage of ZFS_STACK_SIZE is discussed further in the
99	 * ENVIRONMENT VARIABLES sections of the ztest(1) man page.
100	 */
101	if (stksize == 0) {
102		stkstr = getenv("ZFS_STACK_SIZE");
103
104		if (stkstr == NULL)
105			stksize = TS_STACK_MAX;
106		else
107			stksize = MAX(atoi(stkstr), TS_STACK_MIN);
108	}
109
110	VERIFY3S(stksize, >, 0);
111	stksize = P2ROUNDUP(MAX(stksize, TS_STACK_MIN), PAGESIZE);
112
113	/*
114	 * If this ever fails, it may be because the stack size is not a
115	 * multiple of system page size.
116	 */
117	VERIFY0(pthread_attr_setstacksize(&attr, stksize));
118	VERIFY0(pthread_attr_setguardsize(&attr, PAGESIZE));
119
120	VERIFY0(pthread_create(&tid, &attr, (void *(*)(void *))func, arg));
121	VERIFY0(pthread_attr_destroy(&attr));
122
123	return ((void *)(uintptr_t)tid);
124}
125
126/*
127 * =========================================================================
128 * kstats
129 * =========================================================================
130 */
131/*ARGSUSED*/
132kstat_t *
133kstat_create(const char *module, int instance, const char *name,
134    const char *class, uchar_t type, ulong_t ndata, uchar_t ks_flag)
135{
136	return (NULL);
137}
138
139/*ARGSUSED*/
140void
141kstat_install(kstat_t *ksp)
142{}
143
144/*ARGSUSED*/
145void
146kstat_delete(kstat_t *ksp)
147{}
148
149void
150kstat_set_raw_ops(kstat_t *ksp,
151    int (*headers)(char *buf, size_t size),
152    int (*data)(char *buf, size_t size, void *data),
153    void *(*addr)(kstat_t *ksp, loff_t index))
154{}
155
156/*
157 * =========================================================================
158 * mutexes
159 * =========================================================================
160 */
161
162void
163mutex_init(kmutex_t *mp, char *name, int type, void *cookie)
164{
165	VERIFY0(pthread_mutex_init(&mp->m_lock, NULL));
166	memset(&mp->m_owner, 0, sizeof (pthread_t));
167}
168
169void
170mutex_destroy(kmutex_t *mp)
171{
172	VERIFY0(pthread_mutex_destroy(&mp->m_lock));
173}
174
175void
176mutex_enter(kmutex_t *mp)
177{
178	VERIFY0(pthread_mutex_lock(&mp->m_lock));
179	mp->m_owner = pthread_self();
180}
181
182int
183mutex_tryenter(kmutex_t *mp)
184{
185	int error;
186
187	error = pthread_mutex_trylock(&mp->m_lock);
188	if (error == 0) {
189		mp->m_owner = pthread_self();
190		return (1);
191	} else {
192		VERIFY3S(error, ==, EBUSY);
193		return (0);
194	}
195}
196
197void
198mutex_exit(kmutex_t *mp)
199{
200	memset(&mp->m_owner, 0, sizeof (pthread_t));
201	VERIFY0(pthread_mutex_unlock(&mp->m_lock));
202}
203
204/*
205 * =========================================================================
206 * rwlocks
207 * =========================================================================
208 */
209
210void
211rw_init(krwlock_t *rwlp, char *name, int type, void *arg)
212{
213	VERIFY0(pthread_rwlock_init(&rwlp->rw_lock, NULL));
214	rwlp->rw_readers = 0;
215	rwlp->rw_owner = 0;
216}
217
218void
219rw_destroy(krwlock_t *rwlp)
220{
221	VERIFY0(pthread_rwlock_destroy(&rwlp->rw_lock));
222}
223
224void
225rw_enter(krwlock_t *rwlp, krw_t rw)
226{
227	if (rw == RW_READER) {
228		VERIFY0(pthread_rwlock_rdlock(&rwlp->rw_lock));
229		atomic_inc_uint(&rwlp->rw_readers);
230	} else {
231		VERIFY0(pthread_rwlock_wrlock(&rwlp->rw_lock));
232		rwlp->rw_owner = pthread_self();
233	}
234}
235
236void
237rw_exit(krwlock_t *rwlp)
238{
239	if (RW_READ_HELD(rwlp))
240		atomic_dec_uint(&rwlp->rw_readers);
241	else
242		rwlp->rw_owner = 0;
243
244	VERIFY0(pthread_rwlock_unlock(&rwlp->rw_lock));
245}
246
247int
248rw_tryenter(krwlock_t *rwlp, krw_t rw)
249{
250	int error;
251
252	if (rw == RW_READER)
253		error = pthread_rwlock_tryrdlock(&rwlp->rw_lock);
254	else
255		error = pthread_rwlock_trywrlock(&rwlp->rw_lock);
256
257	if (error == 0) {
258		if (rw == RW_READER)
259			atomic_inc_uint(&rwlp->rw_readers);
260		else
261			rwlp->rw_owner = pthread_self();
262
263		return (1);
264	}
265
266	VERIFY3S(error, ==, EBUSY);
267
268	return (0);
269}
270
271/* ARGSUSED */
272uint32_t
273zone_get_hostid(void *zonep)
274{
275	/*
276	 * We're emulating the system's hostid in userland.
277	 */
278	return (strtoul(hw_serial, NULL, 10));
279}
280
281int
282rw_tryupgrade(krwlock_t *rwlp)
283{
284	return (0);
285}
286
287/*
288 * =========================================================================
289 * condition variables
290 * =========================================================================
291 */
292
293void
294cv_init(kcondvar_t *cv, char *name, int type, void *arg)
295{
296	VERIFY0(pthread_cond_init(cv, NULL));
297}
298
299void
300cv_destroy(kcondvar_t *cv)
301{
302	VERIFY0(pthread_cond_destroy(cv));
303}
304
305void
306cv_wait(kcondvar_t *cv, kmutex_t *mp)
307{
308	memset(&mp->m_owner, 0, sizeof (pthread_t));
309	VERIFY0(pthread_cond_wait(cv, &mp->m_lock));
310	mp->m_owner = pthread_self();
311}
312
313int
314cv_wait_sig(kcondvar_t *cv, kmutex_t *mp)
315{
316	cv_wait(cv, mp);
317	return (1);
318}
319
320int
321cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime)
322{
323	int error;
324	struct timeval tv;
325	struct timespec ts;
326	clock_t delta;
327
328	delta = abstime - ddi_get_lbolt();
329	if (delta <= 0)
330		return (-1);
331
332	VERIFY(gettimeofday(&tv, NULL) == 0);
333
334	ts.tv_sec = tv.tv_sec + delta / hz;
335	ts.tv_nsec = tv.tv_usec * NSEC_PER_USEC + (delta % hz) * (NANOSEC / hz);
336	if (ts.tv_nsec >= NANOSEC) {
337		ts.tv_sec++;
338		ts.tv_nsec -= NANOSEC;
339	}
340
341	memset(&mp->m_owner, 0, sizeof (pthread_t));
342	error = pthread_cond_timedwait(cv, &mp->m_lock, &ts);
343	mp->m_owner = pthread_self();
344
345	if (error == ETIMEDOUT)
346		return (-1);
347
348	VERIFY0(error);
349
350	return (1);
351}
352
353/*ARGSUSED*/
354int
355cv_timedwait_hires(kcondvar_t *cv, kmutex_t *mp, hrtime_t tim, hrtime_t res,
356    int flag)
357{
358	int error;
359	struct timeval tv;
360	struct timespec ts;
361	hrtime_t delta;
362
363	ASSERT(flag == 0 || flag == CALLOUT_FLAG_ABSOLUTE);
364
365	delta = tim;
366	if (flag & CALLOUT_FLAG_ABSOLUTE)
367		delta -= gethrtime();
368
369	if (delta <= 0)
370		return (-1);
371
372	VERIFY0(gettimeofday(&tv, NULL));
373
374	ts.tv_sec = tv.tv_sec + delta / NANOSEC;
375	ts.tv_nsec = tv.tv_usec * NSEC_PER_USEC + (delta % NANOSEC);
376	if (ts.tv_nsec >= NANOSEC) {
377		ts.tv_sec++;
378		ts.tv_nsec -= NANOSEC;
379	}
380
381	memset(&mp->m_owner, 0, sizeof (pthread_t));
382	error = pthread_cond_timedwait(cv, &mp->m_lock, &ts);
383	mp->m_owner = pthread_self();
384
385	if (error == ETIMEDOUT)
386		return (-1);
387
388	VERIFY0(error);
389
390	return (1);
391}
392
393void
394cv_signal(kcondvar_t *cv)
395{
396	VERIFY0(pthread_cond_signal(cv));
397}
398
399void
400cv_broadcast(kcondvar_t *cv)
401{
402	VERIFY0(pthread_cond_broadcast(cv));
403}
404
405/*
406 * =========================================================================
407 * procfs list
408 * =========================================================================
409 */
410
411void
412seq_printf(struct seq_file *m, const char *fmt, ...)
413{}
414
415void
416procfs_list_install(const char *module,
417    const char *submodule,
418    const char *name,
419    mode_t mode,
420    procfs_list_t *procfs_list,
421    int (*show)(struct seq_file *f, void *p),
422    int (*show_header)(struct seq_file *f),
423    int (*clear)(procfs_list_t *procfs_list),
424    size_t procfs_list_node_off)
425{
426	mutex_init(&procfs_list->pl_lock, NULL, MUTEX_DEFAULT, NULL);
427	list_create(&procfs_list->pl_list,
428	    procfs_list_node_off + sizeof (procfs_list_node_t),
429	    procfs_list_node_off + offsetof(procfs_list_node_t, pln_link));
430	procfs_list->pl_next_id = 1;
431	procfs_list->pl_node_offset = procfs_list_node_off;
432}
433
434void
435procfs_list_uninstall(procfs_list_t *procfs_list)
436{}
437
438void
439procfs_list_destroy(procfs_list_t *procfs_list)
440{
441	ASSERT(list_is_empty(&procfs_list->pl_list));
442	list_destroy(&procfs_list->pl_list);
443	mutex_destroy(&procfs_list->pl_lock);
444}
445
446#define	NODE_ID(procfs_list, obj) \
447		(((procfs_list_node_t *)(((char *)obj) + \
448		(procfs_list)->pl_node_offset))->pln_id)
449
450void
451procfs_list_add(procfs_list_t *procfs_list, void *p)
452{
453	ASSERT(MUTEX_HELD(&procfs_list->pl_lock));
454	NODE_ID(procfs_list, p) = procfs_list->pl_next_id++;
455	list_insert_tail(&procfs_list->pl_list, p);
456}
457
458/*
459 * =========================================================================
460 * vnode operations
461 * =========================================================================
462 */
463
464/*
465 * =========================================================================
466 * Figure out which debugging statements to print
467 * =========================================================================
468 */
469
470static char *dprintf_string;
471static int dprintf_print_all;
472
473int
474dprintf_find_string(const char *string)
475{
476	char *tmp_str = dprintf_string;
477	int len = strlen(string);
478
479	/*
480	 * Find out if this is a string we want to print.
481	 * String format: file1.c,function_name1,file2.c,file3.c
482	 */
483
484	while (tmp_str != NULL) {
485		if (strncmp(tmp_str, string, len) == 0 &&
486		    (tmp_str[len] == ',' || tmp_str[len] == '\0'))
487			return (1);
488		tmp_str = strchr(tmp_str, ',');
489		if (tmp_str != NULL)
490			tmp_str++; /* Get rid of , */
491	}
492	return (0);
493}
494
495void
496dprintf_setup(int *argc, char **argv)
497{
498	int i, j;
499
500	/*
501	 * Debugging can be specified two ways: by setting the
502	 * environment variable ZFS_DEBUG, or by including a
503	 * "debug=..."  argument on the command line.  The command
504	 * line setting overrides the environment variable.
505	 */
506
507	for (i = 1; i < *argc; i++) {
508		int len = strlen("debug=");
509		/* First look for a command line argument */
510		if (strncmp("debug=", argv[i], len) == 0) {
511			dprintf_string = argv[i] + len;
512			/* Remove from args */
513			for (j = i; j < *argc; j++)
514				argv[j] = argv[j+1];
515			argv[j] = NULL;
516			(*argc)--;
517		}
518	}
519
520	if (dprintf_string == NULL) {
521		/* Look for ZFS_DEBUG environment variable */
522		dprintf_string = getenv("ZFS_DEBUG");
523	}
524
525	/*
526	 * Are we just turning on all debugging?
527	 */
528	if (dprintf_find_string("on"))
529		dprintf_print_all = 1;
530
531	if (dprintf_string != NULL)
532		zfs_flags |= ZFS_DEBUG_DPRINTF;
533}
534
535/*
536 * =========================================================================
537 * debug printfs
538 * =========================================================================
539 */
540void
541__dprintf(boolean_t dprint, const char *file, const char *func,
542    int line, const char *fmt, ...)
543{
544	const char *newfile;
545	va_list adx;
546
547	/*
548	 * Get rid of annoying "../common/" prefix to filename.
549	 */
550	newfile = strrchr(file, '/');
551	if (newfile != NULL) {
552		newfile = newfile + 1; /* Get rid of leading / */
553	} else {
554		newfile = file;
555	}
556
557	if (dprint) {
558		/* dprintf messages are printed immediately */
559
560		if (!dprintf_print_all &&
561		    !dprintf_find_string(newfile) &&
562		    !dprintf_find_string(func))
563			return;
564
565		/* Print out just the function name if requested */
566		flockfile(stdout);
567		if (dprintf_find_string("pid"))
568			(void) printf("%d ", getpid());
569		if (dprintf_find_string("tid"))
570			(void) printf("%ju ",
571			    (uintmax_t)(uintptr_t)pthread_self());
572		if (dprintf_find_string("cpu"))
573			(void) printf("%u ", getcpuid());
574		if (dprintf_find_string("time"))
575			(void) printf("%llu ", gethrtime());
576		if (dprintf_find_string("long"))
577			(void) printf("%s, line %d: ", newfile, line);
578		(void) printf("dprintf: %s: ", func);
579		va_start(adx, fmt);
580		(void) vprintf(fmt, adx);
581		va_end(adx);
582		funlockfile(stdout);
583	} else {
584		/* zfs_dbgmsg is logged for dumping later */
585		size_t size;
586		char *buf;
587		int i;
588
589		size = 1024;
590		buf = umem_alloc(size, UMEM_NOFAIL);
591		i = snprintf(buf, size, "%s:%d:%s(): ", newfile, line, func);
592
593		if (i < size) {
594			va_start(adx, fmt);
595			(void) vsnprintf(buf + i, size - i, fmt, adx);
596			va_end(adx);
597		}
598
599		__zfs_dbgmsg(buf);
600
601		umem_free(buf, size);
602	}
603}
604
605/*
606 * =========================================================================
607 * cmn_err() and panic()
608 * =========================================================================
609 */
610static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
611static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
612
613void
614vpanic(const char *fmt, va_list adx)
615{
616	(void) fprintf(stderr, "error: ");
617	(void) vfprintf(stderr, fmt, adx);
618	(void) fprintf(stderr, "\n");
619
620	abort();	/* think of it as a "user-level crash dump" */
621}
622
623void
624panic(const char *fmt, ...)
625{
626	va_list adx;
627
628	va_start(adx, fmt);
629	vpanic(fmt, adx);
630	va_end(adx);
631}
632
633void
634vcmn_err(int ce, const char *fmt, va_list adx)
635{
636	if (ce == CE_PANIC)
637		vpanic(fmt, adx);
638	if (ce != CE_NOTE) {	/* suppress noise in userland stress testing */
639		(void) fprintf(stderr, "%s", ce_prefix[ce]);
640		(void) vfprintf(stderr, fmt, adx);
641		(void) fprintf(stderr, "%s", ce_suffix[ce]);
642	}
643}
644
645/*PRINTFLIKE2*/
646void
647cmn_err(int ce, const char *fmt, ...)
648{
649	va_list adx;
650
651	va_start(adx, fmt);
652	vcmn_err(ce, fmt, adx);
653	va_end(adx);
654}
655
656/*
657 * =========================================================================
658 * misc routines
659 * =========================================================================
660 */
661
662void
663delay(clock_t ticks)
664{
665	(void) poll(0, 0, ticks * (1000 / hz));
666}
667
668/*
669 * Find highest one bit set.
670 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
671 * The __builtin_clzll() function is supported by both GCC and Clang.
672 */
673int
674highbit64(uint64_t i)
675{
676	if (i == 0)
677	return (0);
678
679	return (NBBY * sizeof (uint64_t) - __builtin_clzll(i));
680}
681
682/*
683 * Find lowest one bit set.
684 * Returns bit number + 1 of lowest bit that is set, otherwise returns 0.
685 * The __builtin_ffsll() function is supported by both GCC and Clang.
686 */
687int
688lowbit64(uint64_t i)
689{
690	if (i == 0)
691		return (0);
692
693	return (__builtin_ffsll(i));
694}
695
696const char *random_path = "/dev/random";
697const char *urandom_path = "/dev/urandom";
698static int random_fd = -1, urandom_fd = -1;
699
700void
701random_init(void)
702{
703	VERIFY((random_fd = open(random_path, O_RDONLY | O_CLOEXEC)) != -1);
704	VERIFY((urandom_fd = open(urandom_path, O_RDONLY | O_CLOEXEC)) != -1);
705}
706
707void
708random_fini(void)
709{
710	close(random_fd);
711	close(urandom_fd);
712
713	random_fd = -1;
714	urandom_fd = -1;
715}
716
717static int
718random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
719{
720	size_t resid = len;
721	ssize_t bytes;
722
723	ASSERT(fd != -1);
724
725	while (resid != 0) {
726		bytes = read(fd, ptr, resid);
727		ASSERT3S(bytes, >=, 0);
728		ptr += bytes;
729		resid -= bytes;
730	}
731
732	return (0);
733}
734
735int
736random_get_bytes(uint8_t *ptr, size_t len)
737{
738	return (random_get_bytes_common(ptr, len, random_fd));
739}
740
741int
742random_get_pseudo_bytes(uint8_t *ptr, size_t len)
743{
744	return (random_get_bytes_common(ptr, len, urandom_fd));
745}
746
747int
748ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result)
749{
750	char *end;
751
752	*result = strtoul(hw_serial, &end, base);
753	if (*result == 0)
754		return (errno);
755	return (0);
756}
757
758int
759ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
760{
761	char *end;
762
763	*result = strtoull(str, &end, base);
764	if (*result == 0)
765		return (errno);
766	return (0);
767}
768
769utsname_t *
770utsname(void)
771{
772	return (&hw_utsname);
773}
774
775/*
776 * =========================================================================
777 * kernel emulation setup & teardown
778 * =========================================================================
779 */
780static int
781umem_out_of_memory(void)
782{
783	char errmsg[] = "out of memory -- generating core dump\n";
784
785	(void) fprintf(stderr, "%s", errmsg);
786	abort();
787	return (0);
788}
789
790void
791kernel_init(int mode)
792{
793	extern uint_t rrw_tsd_key;
794
795	umem_nofail_callback(umem_out_of_memory);
796
797	physmem = sysconf(_SC_PHYS_PAGES);
798
799	dprintf("physmem = %llu pages (%.2f GB)\n", physmem,
800	    (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30));
801
802	(void) snprintf(hw_serial, sizeof (hw_serial), "%ld",
803	    (mode & SPA_MODE_WRITE) ? get_system_hostid() : 0);
804
805	random_init();
806
807	VERIFY0(uname(&hw_utsname));
808
809	system_taskq_init();
810	icp_init();
811
812	zstd_init();
813
814	spa_init((spa_mode_t)mode);
815
816	fletcher_4_init();
817
818	tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
819}
820
821void
822kernel_fini(void)
823{
824	fletcher_4_fini();
825	spa_fini();
826
827	zstd_fini();
828
829	icp_fini();
830	system_taskq_fini();
831
832	random_fini();
833}
834
835uid_t
836crgetuid(cred_t *cr)
837{
838	return (0);
839}
840
841uid_t
842crgetruid(cred_t *cr)
843{
844	return (0);
845}
846
847gid_t
848crgetgid(cred_t *cr)
849{
850	return (0);
851}
852
853int
854crgetngroups(cred_t *cr)
855{
856	return (0);
857}
858
859gid_t *
860crgetgroups(cred_t *cr)
861{
862	return (NULL);
863}
864
865int
866zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
867{
868	return (0);
869}
870
871int
872zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
873{
874	return (0);
875}
876
877int
878zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
879{
880	return (0);
881}
882
883int
884secpolicy_zfs(const cred_t *cr)
885{
886	return (0);
887}
888
889int
890secpolicy_zfs_proc(const cred_t *cr, proc_t *proc)
891{
892	return (0);
893}
894
895ksiddomain_t *
896ksid_lookupdomain(const char *dom)
897{
898	ksiddomain_t *kd;
899
900	kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL);
901	kd->kd_name = spa_strdup(dom);
902	return (kd);
903}
904
905void
906ksiddomain_rele(ksiddomain_t *ksid)
907{
908	spa_strfree(ksid->kd_name);
909	umem_free(ksid, sizeof (ksiddomain_t));
910}
911
912char *
913kmem_vasprintf(const char *fmt, va_list adx)
914{
915	char *buf = NULL;
916	va_list adx_copy;
917
918	va_copy(adx_copy, adx);
919	VERIFY(vasprintf(&buf, fmt, adx_copy) != -1);
920	va_end(adx_copy);
921
922	return (buf);
923}
924
925char *
926kmem_asprintf(const char *fmt, ...)
927{
928	char *buf = NULL;
929	va_list adx;
930
931	va_start(adx, fmt);
932	VERIFY(vasprintf(&buf, fmt, adx) != -1);
933	va_end(adx);
934
935	return (buf);
936}
937
938/* ARGSUSED */
939int
940zfs_onexit_fd_hold(int fd, minor_t *minorp)
941{
942	*minorp = 0;
943	return (0);
944}
945
946/* ARGSUSED */
947void
948zfs_onexit_fd_rele(int fd)
949{
950}
951
952/* ARGSUSED */
953int
954zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data,
955    uint64_t *action_handle)
956{
957	return (0);
958}
959
960fstrans_cookie_t
961spl_fstrans_mark(void)
962{
963	return ((fstrans_cookie_t)0);
964}
965
966void
967spl_fstrans_unmark(fstrans_cookie_t cookie)
968{
969}
970
971int
972__spl_pf_fstrans_check(void)
973{
974	return (0);
975}
976
977int
978kmem_cache_reap_active(void)
979{
980	return (0);
981}
982
983void *zvol_tag = "zvol_tag";
984
985void
986zvol_create_minor(const char *name)
987{
988}
989
990void
991zvol_create_minors_recursive(const char *name)
992{
993}
994
995void
996zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
997{
998}
999
1000void
1001zvol_rename_minors(spa_t *spa, const char *oldname, const char *newname,
1002    boolean_t async)
1003{
1004}
1005
1006/*
1007 * Open file
1008 *
1009 * path - fully qualified path to file
1010 * flags - file attributes O_READ / O_WRITE / O_EXCL
1011 * fpp - pointer to return file pointer
1012 *
1013 * Returns 0 on success underlying error on failure.
1014 */
1015int
1016zfs_file_open(const char *path, int flags, int mode, zfs_file_t **fpp)
1017{
1018	int fd = -1;
1019	int dump_fd = -1;
1020	int err;
1021	int old_umask = 0;
1022	zfs_file_t *fp;
1023	struct stat64 st;
1024
1025	if (!(flags & O_CREAT) && stat64(path, &st) == -1)
1026		return (errno);
1027
1028	if (!(flags & O_CREAT) && S_ISBLK(st.st_mode))
1029		flags |= O_DIRECT;
1030
1031	if (flags & O_CREAT)
1032		old_umask = umask(0);
1033
1034	fd = open64(path, flags, mode);
1035	if (fd == -1)
1036		return (errno);
1037
1038	if (flags & O_CREAT)
1039		(void) umask(old_umask);
1040
1041	if (vn_dumpdir != NULL) {
1042		char *dumppath = umem_zalloc(MAXPATHLEN, UMEM_NOFAIL);
1043		char *inpath = basename((char *)(uintptr_t)path);
1044
1045		(void) snprintf(dumppath, MAXPATHLEN,
1046		    "%s/%s", vn_dumpdir, inpath);
1047		dump_fd = open64(dumppath, O_CREAT | O_WRONLY, 0666);
1048		umem_free(dumppath, MAXPATHLEN);
1049		if (dump_fd == -1) {
1050			err = errno;
1051			close(fd);
1052			return (err);
1053		}
1054	} else {
1055		dump_fd = -1;
1056	}
1057
1058	(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
1059
1060	fp = umem_zalloc(sizeof (zfs_file_t), UMEM_NOFAIL);
1061	fp->f_fd = fd;
1062	fp->f_dump_fd = dump_fd;
1063	*fpp = fp;
1064
1065	return (0);
1066}
1067
1068void
1069zfs_file_close(zfs_file_t *fp)
1070{
1071	close(fp->f_fd);
1072	if (fp->f_dump_fd != -1)
1073		close(fp->f_dump_fd);
1074
1075	umem_free(fp, sizeof (zfs_file_t));
1076}
1077
1078/*
1079 * Stateful write - use os internal file pointer to determine where to
1080 * write and update on successful completion.
1081 *
1082 * fp -  pointer to file (pipe, socket, etc) to write to
1083 * buf - buffer to write
1084 * count - # of bytes to write
1085 * resid -  pointer to count of unwritten bytes  (if short write)
1086 *
1087 * Returns 0 on success errno on failure.
1088 */
1089int
1090zfs_file_write(zfs_file_t *fp, const void *buf, size_t count, ssize_t *resid)
1091{
1092	ssize_t rc;
1093
1094	rc = write(fp->f_fd, buf, count);
1095	if (rc < 0)
1096		return (errno);
1097
1098	if (resid) {
1099		*resid = count - rc;
1100	} else if (rc != count) {
1101		return (EIO);
1102	}
1103
1104	return (0);
1105}
1106
1107/*
1108 * Stateless write - os internal file pointer is not updated.
1109 *
1110 * fp -  pointer to file (pipe, socket, etc) to write to
1111 * buf - buffer to write
1112 * count - # of bytes to write
1113 * off - file offset to write to (only valid for seekable types)
1114 * resid -  pointer to count of unwritten bytes
1115 *
1116 * Returns 0 on success errno on failure.
1117 */
1118int
1119zfs_file_pwrite(zfs_file_t *fp, const void *buf,
1120    size_t count, loff_t pos, ssize_t *resid)
1121{
1122	ssize_t rc, split, done;
1123	int sectors;
1124
1125	/*
1126	 * To simulate partial disk writes, we split writes into two
1127	 * system calls so that the process can be killed in between.
1128	 * This is used by ztest to simulate realistic failure modes.
1129	 */
1130	sectors = count >> SPA_MINBLOCKSHIFT;
1131	split = (sectors > 0 ? rand() % sectors : 0) << SPA_MINBLOCKSHIFT;
1132	rc = pwrite64(fp->f_fd, buf, split, pos);
1133	if (rc != -1) {
1134		done = rc;
1135		rc = pwrite64(fp->f_fd, (char *)buf + split,
1136		    count - split, pos + split);
1137	}
1138#ifdef __linux__
1139	if (rc == -1 && errno == EINVAL) {
1140		/*
1141		 * Under Linux, this most likely means an alignment issue
1142		 * (memory or disk) due to O_DIRECT, so we abort() in order
1143		 * to catch the offender.
1144		 */
1145		abort();
1146	}
1147#endif
1148
1149	if (rc < 0)
1150		return (errno);
1151
1152	done += rc;
1153
1154	if (resid) {
1155		*resid = count - done;
1156	} else if (done != count) {
1157		return (EIO);
1158	}
1159
1160	return (0);
1161}
1162
1163/*
1164 * Stateful read - use os internal file pointer to determine where to
1165 * read and update on successful completion.
1166 *
1167 * fp -  pointer to file (pipe, socket, etc) to read from
1168 * buf - buffer to write
1169 * count - # of bytes to read
1170 * resid -  pointer to count of unread bytes (if short read)
1171 *
1172 * Returns 0 on success errno on failure.
1173 */
1174int
1175zfs_file_read(zfs_file_t *fp, void *buf, size_t count, ssize_t *resid)
1176{
1177	int rc;
1178
1179	rc = read(fp->f_fd, buf, count);
1180	if (rc < 0)
1181		return (errno);
1182
1183	if (resid) {
1184		*resid = count - rc;
1185	} else if (rc != count) {
1186		return (EIO);
1187	}
1188
1189	return (0);
1190}
1191
1192/*
1193 * Stateless read - os internal file pointer is not updated.
1194 *
1195 * fp -  pointer to file (pipe, socket, etc) to read from
1196 * buf - buffer to write
1197 * count - # of bytes to write
1198 * off - file offset to read from (only valid for seekable types)
1199 * resid -  pointer to count of unwritten bytes (if short write)
1200 *
1201 * Returns 0 on success errno on failure.
1202 */
1203int
1204zfs_file_pread(zfs_file_t *fp, void *buf, size_t count, loff_t off,
1205    ssize_t *resid)
1206{
1207	ssize_t rc;
1208
1209	rc = pread64(fp->f_fd, buf, count, off);
1210	if (rc < 0) {
1211#ifdef __linux__
1212		/*
1213		 * Under Linux, this most likely means an alignment issue
1214		 * (memory or disk) due to O_DIRECT, so we abort() in order to
1215		 * catch the offender.
1216		 */
1217		if (errno == EINVAL)
1218			abort();
1219#endif
1220		return (errno);
1221	}
1222
1223	if (fp->f_dump_fd != -1) {
1224		int status;
1225
1226		status = pwrite64(fp->f_dump_fd, buf, rc, off);
1227		ASSERT(status != -1);
1228	}
1229
1230	if (resid) {
1231		*resid = count - rc;
1232	} else if (rc != count) {
1233		return (EIO);
1234	}
1235
1236	return (0);
1237}
1238
1239/*
1240 * lseek - set / get file pointer
1241 *
1242 * fp -  pointer to file (pipe, socket, etc) to read from
1243 * offp - value to seek to, returns current value plus passed offset
1244 * whence - see man pages for standard lseek whence values
1245 *
1246 * Returns 0 on success errno on failure (ESPIPE for non seekable types)
1247 */
1248int
1249zfs_file_seek(zfs_file_t *fp, loff_t *offp, int whence)
1250{
1251	loff_t rc;
1252
1253	rc = lseek(fp->f_fd, *offp, whence);
1254	if (rc < 0)
1255		return (errno);
1256
1257	*offp = rc;
1258
1259	return (0);
1260}
1261
1262/*
1263 * Get file attributes
1264 *
1265 * filp - file pointer
1266 * zfattr - pointer to file attr structure
1267 *
1268 * Currently only used for fetching size and file mode
1269 *
1270 * Returns 0 on success or error code of underlying getattr call on failure.
1271 */
1272int
1273zfs_file_getattr(zfs_file_t *fp, zfs_file_attr_t *zfattr)
1274{
1275	struct stat64 st;
1276
1277	if (fstat64_blk(fp->f_fd, &st) == -1)
1278		return (errno);
1279
1280	zfattr->zfa_size = st.st_size;
1281	zfattr->zfa_mode = st.st_mode;
1282
1283	return (0);
1284}
1285
1286/*
1287 * Sync file to disk
1288 *
1289 * filp - file pointer
1290 * flags - O_SYNC and or O_DSYNC
1291 *
1292 * Returns 0 on success or error code of underlying sync call on failure.
1293 */
1294int
1295zfs_file_fsync(zfs_file_t *fp, int flags)
1296{
1297	int rc;
1298
1299	rc = fsync(fp->f_fd);
1300	if (rc < 0)
1301		return (errno);
1302
1303	return (0);
1304}
1305
1306/*
1307 * fallocate - allocate or free space on disk
1308 *
1309 * fp - file pointer
1310 * mode (non-standard options for hole punching etc)
1311 * offset - offset to start allocating or freeing from
1312 * len - length to free / allocate
1313 *
1314 * OPTIONAL
1315 */
1316int
1317zfs_file_fallocate(zfs_file_t *fp, int mode, loff_t offset, loff_t len)
1318{
1319#ifdef __linux__
1320	return (fallocate(fp->f_fd, mode, offset, len));
1321#else
1322	return (EOPNOTSUPP);
1323#endif
1324}
1325
1326/*
1327 * Request current file pointer offset
1328 *
1329 * fp - pointer to file
1330 *
1331 * Returns current file offset.
1332 */
1333loff_t
1334zfs_file_off(zfs_file_t *fp)
1335{
1336	return (lseek(fp->f_fd, SEEK_CUR, 0));
1337}
1338
1339/*
1340 * unlink file
1341 *
1342 * path - fully qualified file path
1343 *
1344 * Returns 0 on success.
1345 *
1346 * OPTIONAL
1347 */
1348int
1349zfs_file_unlink(const char *path)
1350{
1351	return (remove(path));
1352}
1353
1354/*
1355 * Get reference to file pointer
1356 *
1357 * fd - input file descriptor
1358 * fpp - pointer to file pointer
1359 *
1360 * Returns 0 on success EBADF on failure.
1361 * Unsupported in user space.
1362 */
1363int
1364zfs_file_get(int fd, zfs_file_t **fpp)
1365{
1366	abort();
1367
1368	return (EOPNOTSUPP);
1369}
1370
1371/*
1372 * Drop reference to file pointer
1373 *
1374 * fd - input file descriptor
1375 *
1376 * Unsupported in user space.
1377 */
1378void
1379zfs_file_put(int fd)
1380{
1381	abort();
1382}
1383
1384void
1385zfsvfs_update_fromname(const char *oldname, const char *newname)
1386{
1387}
1388