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