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