xref: /titanic_41/usr/src/lib/libzpool/common/sys/zfs_context.h (revision 391647d5ee9b25dc5307abb55f583388e08b2dd7)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SYS_ZFS_CONTEXT_H
27 #define	_SYS_ZFS_CONTEXT_H
28 
29 #ifdef	__cplusplus
30 extern "C" {
31 #endif
32 
33 #define	_SYS_MUTEX_H
34 #define	_SYS_RWLOCK_H
35 #define	_SYS_CONDVAR_H
36 #define	_SYS_SYSTM_H
37 #define	_SYS_DEBUG_H
38 #define	_SYS_T_LOCK_H
39 #define	_SYS_VNODE_H
40 #define	_SYS_VFS_H
41 #define	_SYS_SUNDDI_H
42 #define	_SYS_CALLB_H
43 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <stddef.h>
47 #include <stdarg.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50 #include <errno.h>
51 #include <string.h>
52 #include <strings.h>
53 #include <synch.h>
54 #include <thread.h>
55 #include <assert.h>
56 #include <alloca.h>
57 #include <umem.h>
58 #include <limits.h>
59 #include <atomic.h>
60 #include <dirent.h>
61 #include <time.h>
62 #include <sys/note.h>
63 #include <sys/types.h>
64 #include <sys/cred.h>
65 #include <sys/sysmacros.h>
66 #include <sys/bitmap.h>
67 #include <sys/resource.h>
68 #include <sys/byteorder.h>
69 #include <sys/list.h>
70 #include <sys/uio.h>
71 #include <sys/zfs_debug.h>
72 #include <sys/sdt.h>
73 #include <sys/kstat.h>
74 #include <sys/u8_textprep.h>
75 #include <sys/sysevent/eventdefs.h>
76 
77 /*
78  * Debugging
79  */
80 
81 /*
82  * Note that we are not using the debugging levels.
83  */
84 
85 #define	CE_CONT		0	/* continuation		*/
86 #define	CE_NOTE		1	/* notice		*/
87 #define	CE_WARN		2	/* warning		*/
88 #define	CE_PANIC	3	/* panic		*/
89 #define	CE_IGNORE	4	/* print nothing	*/
90 
91 /*
92  * ZFS debugging
93  */
94 
95 #ifdef ZFS_DEBUG
96 extern void dprintf_setup(int *argc, char **argv);
97 #endif /* ZFS_DEBUG */
98 
99 extern void cmn_err(int, const char *, ...);
100 extern void vcmn_err(int, const char *, __va_list);
101 extern void panic(const char *, ...);
102 extern void vpanic(const char *, __va_list);
103 
104 #define	fm_panic	panic
105 
106 /* This definition is copied from assert.h. */
107 #if defined(__STDC__)
108 #if __STDC_VERSION__ - 0 >= 199901L
109 #define	verify(EX) (void)((EX) || \
110 	(__assert_c99(#EX, __FILE__, __LINE__, __func__), 0))
111 #else
112 #define	verify(EX) (void)((EX) || (__assert(#EX, __FILE__, __LINE__), 0))
113 #endif /* __STDC_VERSION__ - 0 >= 199901L */
114 #else
115 #define	verify(EX) (void)((EX) || (_assert("EX", __FILE__, __LINE__), 0))
116 #endif	/* __STDC__ */
117 
118 
119 #define	VERIFY	verify
120 #define	ASSERT	assert
121 
122 extern void __assert(const char *, const char *, int);
123 
124 #ifdef lint
125 #define	VERIFY3_IMPL(x, y, z, t)	if (x == z) ((void)0)
126 #else
127 /* BEGIN CSTYLED */
128 #define	VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE) do { \
129 	const TYPE __left = (TYPE)(LEFT); \
130 	const TYPE __right = (TYPE)(RIGHT); \
131 	if (!(__left OP __right)) { \
132 		char *__buf = alloca(256); \
133 		(void) snprintf(__buf, 256, "%s %s %s (0x%llx %s 0x%llx)", \
134 			#LEFT, #OP, #RIGHT, \
135 			(u_longlong_t)__left, #OP, (u_longlong_t)__right); \
136 		__assert(__buf, __FILE__, __LINE__); \
137 	} \
138 _NOTE(CONSTCOND) } while (0)
139 /* END CSTYLED */
140 #endif /* lint */
141 
142 #define	VERIFY3S(x, y, z)	VERIFY3_IMPL(x, y, z, int64_t)
143 #define	VERIFY3U(x, y, z)	VERIFY3_IMPL(x, y, z, uint64_t)
144 #define	VERIFY3P(x, y, z)	VERIFY3_IMPL(x, y, z, uintptr_t)
145 
146 #ifdef NDEBUG
147 #define	ASSERT3S(x, y, z)	((void)0)
148 #define	ASSERT3U(x, y, z)	((void)0)
149 #define	ASSERT3P(x, y, z)	((void)0)
150 #else
151 #define	ASSERT3S(x, y, z)	VERIFY3S(x, y, z)
152 #define	ASSERT3U(x, y, z)	VERIFY3U(x, y, z)
153 #define	ASSERT3P(x, y, z)	VERIFY3P(x, y, z)
154 #endif
155 
156 /*
157  * DTrace SDT probes have different signatures in userland than they do in
158  * kernel.  If they're being used in kernel code, re-define them out of
159  * existence for their counterparts in libzpool.
160  */
161 
162 #ifdef DTRACE_PROBE
163 #undef	DTRACE_PROBE
164 #define	DTRACE_PROBE(a)	((void)0)
165 #endif	/* DTRACE_PROBE */
166 
167 #ifdef DTRACE_PROBE1
168 #undef	DTRACE_PROBE1
169 #define	DTRACE_PROBE1(a, b, c)	((void)0)
170 #endif	/* DTRACE_PROBE1 */
171 
172 #ifdef DTRACE_PROBE2
173 #undef	DTRACE_PROBE2
174 #define	DTRACE_PROBE2(a, b, c, d, e)	((void)0)
175 #endif	/* DTRACE_PROBE2 */
176 
177 #ifdef DTRACE_PROBE3
178 #undef	DTRACE_PROBE3
179 #define	DTRACE_PROBE3(a, b, c, d, e, f, g)	((void)0)
180 #endif	/* DTRACE_PROBE3 */
181 
182 #ifdef DTRACE_PROBE4
183 #undef	DTRACE_PROBE4
184 #define	DTRACE_PROBE4(a, b, c, d, e, f, g, h, i)	((void)0)
185 #endif	/* DTRACE_PROBE4 */
186 
187 /*
188  * Threads
189  */
190 #define	curthread	((void *)(uintptr_t)thr_self())
191 
192 typedef struct kthread kthread_t;
193 
194 #define	thread_create(stk, stksize, func, arg, len, pp, state, pri)	\
195 	zk_thread_create(func, arg)
196 #define	thread_exit() thr_exit(NULL)
197 
198 extern kthread_t *zk_thread_create(void (*func)(), void *arg);
199 
200 #define	issig(why)	(FALSE)
201 #define	ISSIG(thr, why)	(FALSE)
202 
203 /*
204  * Mutexes
205  */
206 typedef struct kmutex {
207 	void		*m_owner;
208 	boolean_t	initialized;
209 	mutex_t		m_lock;
210 } kmutex_t;
211 
212 #define	MUTEX_DEFAULT	USYNC_THREAD
213 #undef MUTEX_HELD
214 #define	MUTEX_HELD(m) _mutex_held(&(m)->m_lock)
215 
216 /*
217  * Argh -- we have to get cheesy here because the kernel and userland
218  * have different signatures for the same routine.
219  */
220 extern int _mutex_init(mutex_t *mp, int type, void *arg);
221 extern int _mutex_destroy(mutex_t *mp);
222 
223 #define	mutex_init(mp, b, c, d)		zmutex_init((kmutex_t *)(mp))
224 #define	mutex_destroy(mp)		zmutex_destroy((kmutex_t *)(mp))
225 
226 extern void zmutex_init(kmutex_t *mp);
227 extern void zmutex_destroy(kmutex_t *mp);
228 extern void mutex_enter(kmutex_t *mp);
229 extern void mutex_exit(kmutex_t *mp);
230 extern int mutex_tryenter(kmutex_t *mp);
231 extern void *mutex_owner(kmutex_t *mp);
232 
233 /*
234  * RW locks
235  */
236 typedef struct krwlock {
237 	void		*rw_owner;
238 	boolean_t	initialized;
239 	rwlock_t	rw_lock;
240 } krwlock_t;
241 
242 typedef int krw_t;
243 
244 #define	RW_READER	0
245 #define	RW_WRITER	1
246 #define	RW_DEFAULT	USYNC_THREAD
247 
248 #undef RW_READ_HELD
249 #define	RW_READ_HELD(x)		_rw_read_held(&(x)->rw_lock)
250 
251 #undef RW_WRITE_HELD
252 #define	RW_WRITE_HELD(x)	_rw_write_held(&(x)->rw_lock)
253 
254 extern void rw_init(krwlock_t *rwlp, char *name, int type, void *arg);
255 extern void rw_destroy(krwlock_t *rwlp);
256 extern void rw_enter(krwlock_t *rwlp, krw_t rw);
257 extern int rw_tryenter(krwlock_t *rwlp, krw_t rw);
258 extern int rw_tryupgrade(krwlock_t *rwlp);
259 extern void rw_exit(krwlock_t *rwlp);
260 #define	rw_downgrade(rwlp) do { } while (0)
261 
262 extern uid_t crgetuid(cred_t *cr);
263 extern gid_t crgetgid(cred_t *cr);
264 extern int crgetngroups(cred_t *cr);
265 extern gid_t *crgetgroups(cred_t *cr);
266 
267 /*
268  * Condition variables
269  */
270 typedef cond_t kcondvar_t;
271 
272 #define	CV_DEFAULT	USYNC_THREAD
273 
274 extern void cv_init(kcondvar_t *cv, char *name, int type, void *arg);
275 extern void cv_destroy(kcondvar_t *cv);
276 extern void cv_wait(kcondvar_t *cv, kmutex_t *mp);
277 extern clock_t cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime);
278 extern void cv_signal(kcondvar_t *cv);
279 extern void cv_broadcast(kcondvar_t *cv);
280 
281 /*
282  * kstat creation, installation and deletion
283  */
284 extern kstat_t *kstat_create(char *, int,
285     char *, char *, uchar_t, ulong_t, uchar_t);
286 extern void kstat_install(kstat_t *);
287 extern void kstat_delete(kstat_t *);
288 
289 /*
290  * Kernel memory
291  */
292 #define	KM_SLEEP		UMEM_NOFAIL
293 #define	KM_PUSHPAGE		KM_SLEEP
294 #define	KM_NOSLEEP		UMEM_DEFAULT
295 #define	KMC_NODEBUG		UMC_NODEBUG
296 #define	kmem_alloc(_s, _f)	umem_alloc(_s, _f)
297 #define	kmem_zalloc(_s, _f)	umem_zalloc(_s, _f)
298 #define	kmem_free(_b, _s)	umem_free(_b, _s)
299 #define	kmem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i) \
300 	umem_cache_create(_a, _b, _c, _d, _e, _f, _g, _h, _i)
301 #define	kmem_cache_destroy(_c)	umem_cache_destroy(_c)
302 #define	kmem_cache_alloc(_c, _f) umem_cache_alloc(_c, _f)
303 #define	kmem_cache_free(_c, _b)	umem_cache_free(_c, _b)
304 #define	kmem_debugging()	0
305 #define	kmem_cache_reap_now(c)
306 
307 typedef umem_cache_t kmem_cache_t;
308 
309 /*
310  * Task queues
311  */
312 typedef struct taskq taskq_t;
313 typedef uintptr_t taskqid_t;
314 typedef void (task_func_t)(void *);
315 
316 #define	TASKQ_PREPOPULATE	0x0001
317 #define	TASKQ_CPR_SAFE		0x0002	/* Use CPR safe protocol */
318 #define	TASKQ_DYNAMIC		0x0004	/* Use dynamic thread scheduling */
319 
320 #define	TQ_SLEEP	KM_SLEEP	/* Can block for memory */
321 #define	TQ_NOSLEEP	KM_NOSLEEP	/* cannot block for memory; may fail */
322 #define	TQ_NOQUEUE	0x02	/* Do not enqueue if can't dispatch */
323 
324 extern taskq_t	*taskq_create(const char *, int, pri_t, int, int, uint_t);
325 extern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t);
326 extern void	taskq_destroy(taskq_t *);
327 extern void	taskq_wait(taskq_t *);
328 extern int	taskq_member(taskq_t *, void *);
329 
330 #define	XVA_MAPSIZE	3
331 #define	XVA_MAGIC	0x78766174
332 
333 /*
334  * vnodes
335  */
336 typedef struct vnode {
337 	uint64_t	v_size;
338 	int		v_fd;
339 	char		*v_path;
340 } vnode_t;
341 
342 
343 typedef struct xoptattr {
344 	timestruc_t	xoa_createtime;	/* Create time of file */
345 	uint8_t		xoa_archive;
346 	uint8_t		xoa_system;
347 	uint8_t		xoa_readonly;
348 	uint8_t		xoa_hidden;
349 	uint8_t		xoa_nounlink;
350 	uint8_t		xoa_immutable;
351 	uint8_t		xoa_appendonly;
352 	uint8_t		xoa_nodump;
353 	uint8_t		xoa_settable;
354 	uint8_t		xoa_opaque;
355 	uint8_t		xoa_av_quarantined;
356 	uint8_t		xoa_av_modified;
357 } xoptattr_t;
358 
359 typedef struct vattr {
360 	uint_t		va_mask;	/* bit-mask of attributes */
361 	u_offset_t	va_size;	/* file size in bytes */
362 } vattr_t;
363 
364 
365 typedef struct xvattr {
366 	vattr_t		xva_vattr;	/* Embedded vattr structure */
367 	uint32_t	xva_magic;	/* Magic Number */
368 	uint32_t	xva_mapsize;	/* Size of attr bitmap (32-bit words) */
369 	uint32_t	*xva_rtnattrmapp;	/* Ptr to xva_rtnattrmap[] */
370 	uint32_t	xva_reqattrmap[XVA_MAPSIZE];	/* Requested attrs */
371 	uint32_t	xva_rtnattrmap[XVA_MAPSIZE];	/* Returned attrs */
372 	xoptattr_t	xva_xoptattrs;	/* Optional attributes */
373 } xvattr_t;
374 
375 typedef struct vsecattr {
376 	uint_t		vsa_mask;	/* See below */
377 	int		vsa_aclcnt;	/* ACL entry count */
378 	void		*vsa_aclentp;	/* pointer to ACL entries */
379 	int		vsa_dfaclcnt;	/* default ACL entry count */
380 	void		*vsa_dfaclentp;	/* pointer to default ACL entries */
381 	size_t		vsa_aclentsz;	/* ACE size in bytes of vsa_aclentp */
382 } vsecattr_t;
383 
384 #define	AT_TYPE		0x00001
385 #define	AT_MODE		0x00002
386 #define	AT_UID		0x00004
387 #define	AT_GID		0x00008
388 #define	AT_FSID		0x00010
389 #define	AT_NODEID	0x00020
390 #define	AT_NLINK	0x00040
391 #define	AT_SIZE		0x00080
392 #define	AT_ATIME	0x00100
393 #define	AT_MTIME	0x00200
394 #define	AT_CTIME	0x00400
395 #define	AT_RDEV		0x00800
396 #define	AT_BLKSIZE	0x01000
397 #define	AT_NBLOCKS	0x02000
398 #define	AT_SEQ		0x08000
399 #define	AT_XVATTR	0x10000
400 
401 #define	CRCREAT		0
402 
403 #define	VOP_CLOSE(vp, f, c, o, cr, ct)	0
404 #define	VOP_PUTPAGE(vp, of, sz, fl, cr, ct)	0
405 #define	VOP_GETATTR(vp, vap, fl, cr, ct)  ((vap)->va_size = (vp)->v_size, 0)
406 
407 #define	VOP_FSYNC(vp, f, cr, ct)	fsync((vp)->v_fd)
408 
409 #define	VN_RELE(vp)	vn_close(vp)
410 
411 extern int vn_open(char *path, int x1, int oflags, int mode, vnode_t **vpp,
412     int x2, int x3);
413 extern int vn_openat(char *path, int x1, int oflags, int mode, vnode_t **vpp,
414     int x2, int x3, vnode_t *vp, int fd);
415 extern int vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len,
416     offset_t offset, int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp);
417 extern void vn_close(vnode_t *vp);
418 
419 #define	vn_remove(path, x1, x2)		remove(path)
420 #define	vn_rename(from, to, seg)	rename((from), (to))
421 #define	vn_is_readonly(vp)		B_FALSE
422 
423 extern vnode_t *rootdir;
424 
425 #include <sys/file.h>		/* for FREAD, FWRITE, etc */
426 
427 /*
428  * Random stuff
429  */
430 #define	lbolt	(gethrtime() >> 23)
431 #define	lbolt64	(gethrtime() >> 23)
432 #define	hz	119	/* frequency when using gethrtime() >> 23 for lbolt */
433 
434 extern void delay(clock_t ticks);
435 
436 #define	gethrestime_sec() time(NULL)
437 
438 #define	max_ncpus	64
439 
440 #define	minclsyspri	60
441 #define	maxclsyspri	99
442 
443 #define	CPU_SEQID	(thr_self() & (max_ncpus - 1))
444 
445 #define	kcred		NULL
446 #define	CRED()		NULL
447 
448 #define	ptob(x)		((x) * PAGESIZE)
449 
450 extern uint64_t physmem;
451 
452 extern int highbit(ulong_t i);
453 extern int random_get_bytes(uint8_t *ptr, size_t len);
454 extern int random_get_pseudo_bytes(uint8_t *ptr, size_t len);
455 
456 extern void kernel_init(int);
457 extern void kernel_fini(void);
458 
459 struct spa;
460 extern void nicenum(uint64_t num, char *buf);
461 extern void show_pool_stats(struct spa *);
462 
463 typedef struct callb_cpr {
464 	kmutex_t	*cc_lockp;
465 } callb_cpr_t;
466 
467 #define	CALLB_CPR_INIT(cp, lockp, func, name)	{		\
468 	(cp)->cc_lockp = lockp;					\
469 }
470 
471 #define	CALLB_CPR_SAFE_BEGIN(cp) {				\
472 	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
473 }
474 
475 #define	CALLB_CPR_SAFE_END(cp, lockp) {				\
476 	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
477 }
478 
479 #define	CALLB_CPR_EXIT(cp) {					\
480 	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
481 	mutex_exit((cp)->cc_lockp);				\
482 }
483 
484 #define	zone_dataset_visible(x, y)	(1)
485 #define	INGLOBALZONE(z)			(1)
486 
487 /*
488  * Hostname information
489  */
490 extern char hw_serial[];
491 extern int ddi_strtoul(const char *str, char **nptr, int base,
492     unsigned long *result);
493 
494 /* ZFS Boot Related stuff. */
495 
496 struct _buf {
497 	intptr_t	_fd;
498 };
499 
500 struct bootstat {
501 	uint64_t st_size;
502 };
503 
504 typedef struct ace_object {
505 	uid_t		a_who;
506 	uint32_t	a_access_mask;
507 	uint16_t	a_flags;
508 	uint16_t	a_type;
509 	uint8_t		a_obj_type[16];
510 	uint8_t		a_inherit_obj_type[16];
511 } ace_object_t;
512 
513 
514 #define	ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE	0x05
515 #define	ACE_ACCESS_DENIED_OBJECT_ACE_TYPE	0x06
516 #define	ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE	0x07
517 #define	ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE	0x08
518 
519 extern struct _buf *kobj_open_file(char *name);
520 extern int kobj_read_file(struct _buf *file, char *buf, unsigned size,
521     unsigned off);
522 extern void kobj_close_file(struct _buf *file);
523 extern int kobj_get_filesize(struct _buf *file, uint64_t *size);
524 extern int zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr);
525 extern int zfs_secpolicy_rename_perms(const char *from, const char *to,
526     cred_t *cr);
527 extern int zfs_secpolicy_destroy_perms(const char *name, cred_t *cr);
528 extern zoneid_t getzoneid(void);
529 
530 /* SID stuff */
531 typedef struct ksiddomain {
532 	uint_t	kd_ref;
533 	uint_t	kd_len;
534 	char	*kd_name;
535 } ksiddomain_t;
536 
537 ksiddomain_t *ksid_lookupdomain(const char *);
538 void ksiddomain_rele(ksiddomain_t *);
539 
540 #ifdef	__cplusplus
541 }
542 #endif
543 
544 #endif	/* _SYS_ZFS_CONTEXT_H */
545