1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21fa9e406ahrens/*
22744947dTom Erickson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
235aeb947Garrett D'Amore * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
240e60744Pavel Zakharov * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
25084fd14Brian Behlendorf * Copyright 2019 Joyent, Inc.
26f06dce2Andrew Stormont * Copyright 2017 RackTop Systems.
275aeb947Garrett D'Amore */
28fa9e406ahrens
29fa9e406ahrens#ifndef _SYS_ZFS_CONTEXT_H
30fa9e406ahrens#define	_SYS_ZFS_CONTEXT_H
31fa9e406ahrens
32fa9e406ahrens#ifdef	__cplusplus
33fa9e406ahrensextern "C" {
34fa9e406ahrens#endif
35fa9e406ahrens
36f06dce2Andrew Stormont#define	_SYNCH_H
37f06dce2Andrew Stormont
38fa9e406ahrens#define	_SYS_VNODE_H
39fa9e406ahrens#define	_SYS_VFS_H
40fa9e406ahrens#define	_SYS_CALLB_H
41fa9e406ahrens
42fa9e406ahrens#include <stdio.h>
43fa9e406ahrens#include <stdlib.h>
44fa9e406ahrens#include <stddef.h>
45fa9e406ahrens#include <stdarg.h>
46fa9e406ahrens#include <fcntl.h>
47fa9e406ahrens#include <unistd.h>
48fa9e406ahrens#include <errno.h>
49fa9e406ahrens#include <string.h>
50fa9e406ahrens#include <strings.h>
51fa9e406ahrens#include <thread.h>
52fa9e406ahrens#include <assert.h>
53fa9e406ahrens#include <alloca.h>
54fa9e406ahrens#include <umem.h>
55fa9e406ahrens#include <limits.h>
56fa9e406ahrens#include <atomic.h>
57fa9e406ahrens#include <dirent.h>
58fa9e406ahrens#include <time.h>
59cd1c8b8Matthew Ahrens#include <procfs.h>
603b2aab1Matthew Ahrens#include <pthread.h>
61dfc1153Chris Williamson#include <setjmp.h>
623b2aab1Matthew Ahrens#include <sys/debug.h>
63573ca77George Wilson#include <libsysevent.h>
64fa9e406ahrens#include <sys/note.h>
65fa9e406ahrens#include <sys/types.h>
66ecd6cf8marks#include <sys/cred.h>
67fa9e406ahrens#include <sys/sysmacros.h>
68fa9e406ahrens#include <sys/bitmap.h>
69fa9e406ahrens#include <sys/resource.h>
70fa9e406ahrens#include <sys/byteorder.h>
71fa9e406ahrens#include <sys/list.h>
72fa9e406ahrens#include <sys/uio.h>
73fa9e406ahrens#include <sys/zfs_debug.h>
74fa9e406ahrens#include <sys/sdt.h>
7544cb6abbmc#include <sys/kstat.h>
76de8267etimh#include <sys/u8_textprep.h>
773d7072feschrock#include <sys/sysevent/eventdefs.h>
78573ca77George Wilson#include <sys/sysevent/dev.h>
79fb09f5aMadhav Suresh#include <sys/debug.h>
80f06dce2Andrew Stormont#include <sys/taskq.h>
81f06dce2Andrew Stormont#include <sys/taskq_impl.h>
82f06dce2Andrew Stormont#include <sys/mutex.h>
83f06dce2Andrew Stormont#include <sys/proc.h>
84f06dce2Andrew Stormont#include <sys/condvar.h>
85f06dce2Andrew Stormont#include <sys/cmn_err.h>
86f06dce2Andrew Stormont#include <sys/kmem.h>
87f06dce2Andrew Stormont#include <sys/systm.h>
88f06dce2Andrew Stormont#include <sys/random.h>
89f06dce2Andrew Stormont#include <sys/buf.h>
90f06dce2Andrew Stormont#include <sys/sid.h>
91f06dce2Andrew Stormont#include <sys/acl.h>
92f06dce2Andrew Stormont#include <sys/bitmap.h>
93f06dce2Andrew Stormont#include <sys/systeminfo.h>
94f06dce2Andrew Stormont#include <sys/cpuvar.h>
95f06dce2Andrew Stormont#include <sys/pset.h>
96f06dce2Andrew Stormont#include <sys/kobj.h>
97f06dce2Andrew Stormont#include <sys/fm/util.h>
98be6fd75Matthew Ahrens#include "zfs.h"
99fa9e406ahrens
100fa9e406ahrens/*
101fa9e406ahrens * ZFS debugging
102fa9e406ahrens */
103fa9e406ahrens
104fa9e406ahrens#ifdef ZFS_DEBUG
105fa9e406ahrensextern void dprintf_setup(int *argc, char **argv);
106fa9e406ahrens#endif /* ZFS_DEBUG */
107fa9e406ahrens
108fa9e406ahrens/*
1093d7072feschrock * DTrace SDT probes have different signatures in userland than they do in
110a2cdcddPaul Dagnelie * the kernel.  If they're being used in kernel code, re-define them out of
111fa9e406ahrens * existence for their counterparts in libzpool.
112a2cdcddPaul Dagnelie *
113a2cdcddPaul Dagnelie * Here's an example of how to use the set-error probes in userland:
114a2cdcddPaul Dagnelie * zfs$target:::set-error /arg0 == EBUSY/ {stack();}
115a2cdcddPaul Dagnelie *
116a2cdcddPaul Dagnelie * Here's an example of how to use DTRACE_PROBE probes in userland:
117a2cdcddPaul Dagnelie * If there is a probe declared as follows:
118a2cdcddPaul Dagnelie * DTRACE_PROBE2(zfs__probe_name, uint64_t, blkid, dnode_t *, dn);
119a2cdcddPaul Dagnelie * Then you can use it as follows:
120a2cdcddPaul Dagnelie * zfs$target:::probe2 /copyinstr(arg0) == "zfs__probe_name"/
121a2cdcddPaul Dagnelie *     {printf("%u %p\n", arg1, arg2);}
122fa9e406ahrens */
123fa9e406ahrens
12405715f9Mark Maybee#ifdef DTRACE_PROBE
12505715f9Mark Maybee#undef	DTRACE_PROBE
12605715f9Mark Maybee#endif	/* DTRACE_PROBE */
127be6fd75Matthew Ahrens#define	DTRACE_PROBE(a) \
128be6fd75Matthew Ahrens	ZFS_PROBE0(#a)
12905715f9Mark Maybee
130fa9e406ahrens#ifdef DTRACE_PROBE1
131fa9e406ahrens#undef	DTRACE_PROBE1
132fa9e406ahrens#endif	/* DTRACE_PROBE1 */
133be6fd75Matthew Ahrens#define	DTRACE_PROBE1(a, b, c) \
134be6fd75Matthew Ahrens	ZFS_PROBE1(#a, (unsigned long)c)
135fa9e406ahrens
136fa9e406ahrens#ifdef DTRACE_PROBE2
137fa9e406ahrens#undef	DTRACE_PROBE2
138fa9e406ahrens#endif	/* DTRACE_PROBE2 */
139be6fd75Matthew Ahrens#define	DTRACE_PROBE2(a, b, c, d, e) \
140be6fd75Matthew Ahrens	ZFS_PROBE2(#a, (unsigned long)c, (unsigned long)e)
141fa9e406ahrens
142c543ec0ahrens#ifdef DTRACE_PROBE3
143c543ec0ahrens#undef	DTRACE_PROBE3
144c543ec0ahrens#endif	/* DTRACE_PROBE3 */
145be6fd75Matthew Ahrens#define	DTRACE_PROBE3(a, b, c, d, e, f, g) \
146be6fd75Matthew Ahrens	ZFS_PROBE3(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g)
147c543ec0ahrens
148faafa6eahrens#ifdef DTRACE_PROBE4
149faafa6eahrens#undef	DTRACE_PROBE4
150faafa6eahrens#endif	/* DTRACE_PROBE4 */
151be6fd75Matthew Ahrens#define	DTRACE_PROBE4(a, b, c, d, e, f, g, h, i) \
152be6fd75Matthew Ahrens	ZFS_PROBE4(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g, \
153be6fd75Matthew Ahrens	(unsigned long)i)
154be6fd75Matthew Ahrens
155be6fd75Matthew Ahrens/*
156be6fd75Matthew Ahrens * We use the comma operator so that this macro can be used without much
157be6fd75Matthew Ahrens * additional code.  For example, "return (EINVAL);" becomes
158be6fd75Matthew Ahrens * "return (SET_ERROR(EINVAL));".  Note that the argument will be evaluated
159be6fd75Matthew Ahrens * twice, so it should not have side effects (e.g. something like:
160be6fd75Matthew Ahrens * "return (SET_ERROR(log_error(EINVAL, info)));" would log the error twice).
161be6fd75Matthew Ahrens */
162be6fd75Matthew Ahrens#define	SET_ERROR(err) (ZFS_SET_ERROR(err), err)
163faafa6eahrens
164fa9e406ahrens/*
165fa9e406ahrens * Threads
166fa9e406ahrens */
16769962b5Matthew Ahrens#define	kpreempt(x)	yield()
16835a5a35Jonathan Adams#define	newproc(f, a, cid, pri, ctp, pid)	(ENOSYS)
16935a5a35Jonathan Adams
1703b2aab1Matthew Ahrens/*
171f06dce2Andrew Stormont * vnodes
172fa9e406ahrens */
173da6c28aamw#define	XVA_MAPSIZE	3
174da6c28aamw#define	XVA_MAGIC	0x78766174
175da6c28aamw
176fa9e406ahrenstypedef struct vnode {
177fa9e406ahrens	uint64_t	v_size;
178fa9e406ahrens	int		v_fd;
179fa9e406ahrens	char		*v_path;
180df15e41Matthew Ahrens	int		v_dump_fd;
181fa9e406ahrens} vnode_t;
182fa9e406ahrens
183df15e41Matthew Ahrensextern char *vn_dumpdir;
1847a286c4Dai Ngo#define	AV_SCANSTAMP_SZ	32		/* length of anti-virus scanstamp */
185da6c28aamw
186da6c28aamwtypedef struct xoptattr {
187da6c28aamw	timestruc_t	xoa_createtime;	/* Create time of file */
188da6c28aamw	uint8_t		xoa_archive;
189da6c28aamw	uint8_t		xoa_system;
190da6c28aamw	uint8_t		xoa_readonly;
191da6c28aamw	uint8_t		xoa_hidden;
192da6c28aamw	uint8_t		xoa_nounlink;
193da6c28aamw	uint8_t		xoa_immutable;
194da6c28aamw	uint8_t		xoa_appendonly;
195da6c28aamw	uint8_t		xoa_nodump;
196da6c28aamw	uint8_t		xoa_settable;
197da6c28aamw	uint8_t		xoa_opaque;
198da6c28aamw	uint8_t		xoa_av_quarantined;
199da6c28aamw	uint8_t		xoa_av_modified;
2007a286c4Dai Ngo	uint8_t		xoa_av_scanstamp[AV_SCANSTAMP_SZ];
2017a286c4Dai Ngo	uint8_t		xoa_reparse;
202fd9ee8bjoyce mcintosh	uint8_t		xoa_offline;
203fd9ee8bjoyce mcintosh	uint8_t		xoa_sparse;
204da6c28aamw} xoptattr_t;
205da6c28aamw
206fa9e406ahrenstypedef struct vattr {
207fa9e406ahrens	uint_t		va_mask;	/* bit-mask of attributes */
208fa9e406ahrens	u_offset_t	va_size;	/* file size in bytes */
209fa9e406ahrens} vattr_t;
210fa9e406ahrens
211da6c28aamw
212da6c28aamwtypedef struct xvattr {
213da6c28aamw	vattr_t		xva_vattr;	/* Embedded vattr structure */
214da6c28aamw	uint32_t	xva_magic;	/* Magic Number */
215da6c28aamw	uint32_t	xva_mapsize;	/* Size of attr bitmap (32-bit words) */
216da6c28aamw	uint32_t	*xva_rtnattrmapp;	/* Ptr to xva_rtnattrmap[] */
217da6c28aamw	uint32_t	xva_reqattrmap[XVA_MAPSIZE];	/* Requested attrs */
218da6c28aamw	uint32_t	xva_rtnattrmap[XVA_MAPSIZE];	/* Returned attrs */
219da6c28aamw	xoptattr_t	xva_xoptattrs;	/* Optional attributes */
220da6c28aamw} xvattr_t;
221da6c28aamw
222da6c28aamwtypedef struct vsecattr {
223da6c28aamw	uint_t		vsa_mask;	/* See below */
224da6c28aamw	int		vsa_aclcnt;	/* ACL entry count */
225da6c28aamw	void		*vsa_aclentp;	/* pointer to ACL entries */
226da6c28aamw	int		vsa_dfaclcnt;	/* default ACL entry count */
227da6c28aamw	void		*vsa_dfaclentp;	/* pointer to default ACL entries */
228da6c28aamw	size_t		vsa_aclentsz;	/* ACE size in bytes of vsa_aclentp */
229da6c28aamw} vsecattr_t;
230da6c28aamw
231da6c28aamw#define	AT_TYPE		0x00001
232da6c28aamw#define	AT_MODE		0x00002
233da6c28aamw#define	AT_UID		0x00004
234da6c28aamw#define	AT_GID		0x00008
235da6c28aamw#define	AT_FSID		0x00010
236da6c28aamw#define	AT_NODEID	0x00020
237da6c28aamw#define	AT_NLINK	0x00040
238da6c28aamw#define	AT_SIZE		0x00080
239da6c28aamw#define	AT_ATIME	0x00100
240da6c28aamw#define	AT_MTIME	0x00200
241da6c28aamw#define	AT_CTIME	0x00400
242da6c28aamw#define	AT_RDEV		0x00800
243da6c28aamw#define	AT_BLKSIZE	0x01000
244da6c28aamw#define	AT_NBLOCKS	0x02000
245da6c28aamw#define	AT_SEQ		0x08000
246da6c28aamw#define	AT_XVATTR	0x10000
247fa9e406ahrens
248fa9e406ahrens#define	CRCREAT		0
249fa9e406ahrens
250095bcd6George Wilsonextern int fop_getattr(vnode_t *vp, vattr_t *vap);
251095bcd6George Wilson
252da6c28aamw#define	VOP_CLOSE(vp, f, c, o, cr, ct)	0
253da6c28aamw#define	VOP_PUTPAGE(vp, of, sz, fl, cr, ct)	0
254095bcd6George Wilson#define	VOP_GETATTR(vp, vap, fl, cr, ct)  fop_getattr((vp), (vap));
255084fd14Brian Behlendorf#define	VOP_SPACE(vp, cmd, a, f, o, cr, ct)  0
256fa9e406ahrens
257da6c28aamw#define	VOP_FSYNC(vp, f, cr, ct)	fsync((vp)->v_fd)
258fa9e406ahrens
259fa9e406ahrens#define	VN_RELE(vp)	vn_close(vp)
260fa9e406ahrens
261fa9e406ahrensextern int vn_open(char *path, int x1, int oflags, int mode, vnode_t **vpp,
262fa9e406ahrens    int x2, int x3);
263fa9e406ahrensextern int vn_openat(char *path, int x1, int oflags, int mode, vnode_t **vpp,
264da6c28aamw    int x2, int x3, vnode_t *vp, int fd);
265fa9e406ahrensextern int vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len,
266fa9e406ahrens    offset_t offset, int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp);
267fa9e406ahrensextern void vn_close(vnode_t *vp);
268fa9e406ahrens
269fa9e406ahrens#define	vn_remove(path, x1, x2)		remove(path)
270fa9e406ahrens#define	vn_rename(from, to, seg)	rename((from), (to))
2710373e76bonwick#define	vn_is_readonly(vp)		B_FALSE
272fa9e406ahrens
273fa9e406ahrensextern vnode_t *rootdir;
274fa9e406ahrens
275fa9e406ahrens#include <sys/file.h>		/* for FREAD, FWRITE, etc */
276f06dce2Andrew Stormont#include <sys/sunddi.h>		/* for ddi_strtoul, ddi_strtoull, etc */
277f06dce2Andrew Stormont#include <sys/cyclic.h>		/* for cyclic_add, cyclic remove, etc */
278f06dce2Andrew Stormont#include <vm/seg_kmem.h>	/* for zio_arena */
279fa9e406ahrens
280fa9e406ahrens/*
281fa9e406ahrens * Random stuff
282fa9e406ahrens */
283fa9e406ahrens#define	max_ncpus	64
284244781fPrakash Surya#define	boot_ncpus	(sysconf(_SC_NPROCESSORS_ONLN))
285fa9e406ahrens
286fa9e406ahrens#define	minclsyspri	60
287fa9e406ahrens#define	maxclsyspri	99
288fa9e406ahrens
289c4ab0d3Gvozden Neskovic#if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__)
290c4ab0d3Gvozden Neskovic#define	_zfs_expect(expr, value)    (__builtin_expect((expr), (value)))
291c4ab0d3Gvozden Neskovic#else
292c4ab0d3Gvozden Neskovic#define	_zfs_expect(expr, value)    (expr)
293c4ab0d3Gvozden Neskovic#endif
294c4ab0d3Gvozden Neskovic
295c4ab0d3Gvozden Neskovic#define	likely(x)	_zfs_expect((x) != 0, 1)
296c4ab0d3Gvozden Neskovic
297fa9e406ahrens#define	CPU_SEQID	(thr_self() & (max_ncpus - 1))
298fa9e406ahrens
299fa9e406ahrensextern void kernel_init(int);
300fa9e406ahrensextern void kernel_fini(void);
301fa9e406ahrens
302fa9e406ahrensstruct spa;
303fa9e406ahrensextern void show_pool_stats(struct spa *);
3040e60744Pavel Zakharovextern int set_global_var(char *arg);
305fa9e406ahrens
306fa9e406ahrenstypedef struct callb_cpr {
307fa9e406ahrens	kmutex_t	*cc_lockp;
308fa9e406ahrens} callb_cpr_t;
309fa9e406ahrens
310fa9e406ahrens#define	CALLB_CPR_INIT(cp, lockp, func, name)	{		\
311fa9e406ahrens	(cp)->cc_lockp = lockp;					\
312fa9e406ahrens}
313fa9e406ahrens
314fa9e406ahrens#define	CALLB_CPR_SAFE_BEGIN(cp) {				\
315fa9e406ahrens	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
316fa9e406ahrens}
317fa9e406ahrens
318fa9e406ahrens#define	CALLB_CPR_SAFE_END(cp, lockp) {				\
319fa9e406ahrens	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
320fa9e406ahrens}
321fa9e406ahrens
322fa9e406ahrens#define	CALLB_CPR_EXIT(cp) {					\
323fa9e406ahrens	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
324fa9e406ahrens	mutex_exit((cp)->cc_lockp);				\
325fa9e406ahrens}
326fa9e406ahrens
327fa9e406ahrens#define	zone_dataset_visible(x, y)	(1)
328fa9e406ahrens#define	INGLOBALZONE(z)			(1)
3296f79381Pavel Zakharovextern uint32_t zone_get_hostid(void *zonep);
330fa9e406ahrens
3314d7988dPaul Dagnelie/*
3324d7988dPaul Dagnelie * In ZoL the following defines were added to their sys/avl.h header, but
3334d7988dPaul Dagnelie * we want to limit these to the ZFS code on illumos.
3344d7988dPaul Dagnelie */
3354d7988dPaul Dagnelie#define	TREE_ISIGN(a)	(((a) > 0) - ((a) < 0))
3364d7988dPaul Dagnelie#define	TREE_CMP(a, b)	(((a) > (b)) - ((a) < (b)))
3374d7988dPaul Dagnelie#define	TREE_PCMP(a, b)	\
3384d7988dPaul Dagnelie	(((uintptr_t)(a) > (uintptr_t)(b)) - ((uintptr_t)(a) < (uintptr_t)(b)))
3394d7988dPaul Dagnelie
340ecd6cf8marksextern int zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr);
341ecd6cf8marksextern int zfs_secpolicy_rename_perms(const char *from, const char *to,
342ecd6cf8marks    cred_t *cr);
343ecd6cf8marksextern int zfs_secpolicy_destroy_perms(const char *name, cred_t *cr);
344e0d35c4marks
345573ca77George Wilson#define	ddi_log_sysevent(_a, _b, _c, _d, _e, _f, _g) \
346573ca77George Wilson	sysevent_post_event(_c, _d, _b, "libzpool", _e, _f)
347573ca77George Wilson
348f054716lling#ifdef	__cplusplus
349f054716lling}
350f054716lling#endif
351f054716lling
352fa9e406ahrens#endif	/* _SYS_ZFS_CONTEXT_H */
353