xref: /titanic_50/usr/src/lib/libzpool/common/sys/zfs_context.h (revision 6cd9e705a60bbefe49d1a9cfff2944eaf6312c8b)
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 2009 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 #define	TASKQ_THREADS_CPU_PCT	0x0008	/* Use dynamic thread scheduling */
320 
321 #define	TQ_SLEEP	KM_SLEEP	/* Can block for memory */
322 #define	TQ_NOSLEEP	KM_NOSLEEP	/* cannot block for memory; may fail */
323 #define	TQ_NOQUEUE	0x02	/* Do not enqueue if can't dispatch */
324 
325 extern taskq_t *system_taskq;
326 
327 extern taskq_t	*taskq_create(const char *, int, pri_t, int, int, uint_t);
328 extern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t);
329 extern void	taskq_destroy(taskq_t *);
330 extern void	taskq_wait(taskq_t *);
331 extern int	taskq_member(taskq_t *, void *);
332 extern void	system_taskq_init(void);
333 
334 #define	XVA_MAPSIZE	3
335 #define	XVA_MAGIC	0x78766174
336 
337 /*
338  * vnodes
339  */
340 typedef struct vnode {
341 	uint64_t	v_size;
342 	int		v_fd;
343 	char		*v_path;
344 } vnode_t;
345 
346 
347 typedef struct xoptattr {
348 	timestruc_t	xoa_createtime;	/* Create time of file */
349 	uint8_t		xoa_archive;
350 	uint8_t		xoa_system;
351 	uint8_t		xoa_readonly;
352 	uint8_t		xoa_hidden;
353 	uint8_t		xoa_nounlink;
354 	uint8_t		xoa_immutable;
355 	uint8_t		xoa_appendonly;
356 	uint8_t		xoa_nodump;
357 	uint8_t		xoa_settable;
358 	uint8_t		xoa_opaque;
359 	uint8_t		xoa_av_quarantined;
360 	uint8_t		xoa_av_modified;
361 } xoptattr_t;
362 
363 typedef struct vattr {
364 	uint_t		va_mask;	/* bit-mask of attributes */
365 	u_offset_t	va_size;	/* file size in bytes */
366 } vattr_t;
367 
368 
369 typedef struct xvattr {
370 	vattr_t		xva_vattr;	/* Embedded vattr structure */
371 	uint32_t	xva_magic;	/* Magic Number */
372 	uint32_t	xva_mapsize;	/* Size of attr bitmap (32-bit words) */
373 	uint32_t	*xva_rtnattrmapp;	/* Ptr to xva_rtnattrmap[] */
374 	uint32_t	xva_reqattrmap[XVA_MAPSIZE];	/* Requested attrs */
375 	uint32_t	xva_rtnattrmap[XVA_MAPSIZE];	/* Returned attrs */
376 	xoptattr_t	xva_xoptattrs;	/* Optional attributes */
377 } xvattr_t;
378 
379 typedef struct vsecattr {
380 	uint_t		vsa_mask;	/* See below */
381 	int		vsa_aclcnt;	/* ACL entry count */
382 	void		*vsa_aclentp;	/* pointer to ACL entries */
383 	int		vsa_dfaclcnt;	/* default ACL entry count */
384 	void		*vsa_dfaclentp;	/* pointer to default ACL entries */
385 	size_t		vsa_aclentsz;	/* ACE size in bytes of vsa_aclentp */
386 } vsecattr_t;
387 
388 #define	AT_TYPE		0x00001
389 #define	AT_MODE		0x00002
390 #define	AT_UID		0x00004
391 #define	AT_GID		0x00008
392 #define	AT_FSID		0x00010
393 #define	AT_NODEID	0x00020
394 #define	AT_NLINK	0x00040
395 #define	AT_SIZE		0x00080
396 #define	AT_ATIME	0x00100
397 #define	AT_MTIME	0x00200
398 #define	AT_CTIME	0x00400
399 #define	AT_RDEV		0x00800
400 #define	AT_BLKSIZE	0x01000
401 #define	AT_NBLOCKS	0x02000
402 #define	AT_SEQ		0x08000
403 #define	AT_XVATTR	0x10000
404 
405 #define	CRCREAT		0
406 
407 #define	VOP_CLOSE(vp, f, c, o, cr, ct)	0
408 #define	VOP_PUTPAGE(vp, of, sz, fl, cr, ct)	0
409 #define	VOP_GETATTR(vp, vap, fl, cr, ct)  ((vap)->va_size = (vp)->v_size, 0)
410 
411 #define	VOP_FSYNC(vp, f, cr, ct)	fsync((vp)->v_fd)
412 
413 #define	VN_RELE(vp)	vn_close(vp)
414 
415 extern int vn_open(char *path, int x1, int oflags, int mode, vnode_t **vpp,
416     int x2, int x3);
417 extern int vn_openat(char *path, int x1, int oflags, int mode, vnode_t **vpp,
418     int x2, int x3, vnode_t *vp, int fd);
419 extern int vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len,
420     offset_t offset, int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp);
421 extern void vn_close(vnode_t *vp);
422 
423 #define	vn_remove(path, x1, x2)		remove(path)
424 #define	vn_rename(from, to, seg)	rename((from), (to))
425 #define	vn_is_readonly(vp)		B_FALSE
426 
427 extern vnode_t *rootdir;
428 
429 #include <sys/file.h>		/* for FREAD, FWRITE, etc */
430 
431 /*
432  * Random stuff
433  */
434 #define	lbolt	(gethrtime() >> 23)
435 #define	lbolt64	(gethrtime() >> 23)
436 #define	hz	119	/* frequency when using gethrtime() >> 23 for lbolt */
437 
438 extern void delay(clock_t ticks);
439 
440 #define	gethrestime_sec() time(NULL)
441 
442 #define	max_ncpus	64
443 
444 #define	minclsyspri	60
445 #define	maxclsyspri	99
446 
447 #define	CPU_SEQID	(thr_self() & (max_ncpus - 1))
448 
449 #define	kcred		NULL
450 #define	CRED()		NULL
451 
452 #define	ptob(x)		((x) * PAGESIZE)
453 
454 extern uint64_t physmem;
455 
456 extern int highbit(ulong_t i);
457 extern int random_get_bytes(uint8_t *ptr, size_t len);
458 extern int random_get_pseudo_bytes(uint8_t *ptr, size_t len);
459 
460 extern void kernel_init(int);
461 extern void kernel_fini(void);
462 
463 struct spa;
464 extern void nicenum(uint64_t num, char *buf);
465 extern void show_pool_stats(struct spa *);
466 
467 typedef struct callb_cpr {
468 	kmutex_t	*cc_lockp;
469 } callb_cpr_t;
470 
471 #define	CALLB_CPR_INIT(cp, lockp, func, name)	{		\
472 	(cp)->cc_lockp = lockp;					\
473 }
474 
475 #define	CALLB_CPR_SAFE_BEGIN(cp) {				\
476 	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
477 }
478 
479 #define	CALLB_CPR_SAFE_END(cp, lockp) {				\
480 	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
481 }
482 
483 #define	CALLB_CPR_EXIT(cp) {					\
484 	ASSERT(MUTEX_HELD((cp)->cc_lockp));			\
485 	mutex_exit((cp)->cc_lockp);				\
486 }
487 
488 #define	zone_dataset_visible(x, y)	(1)
489 #define	INGLOBALZONE(z)			(1)
490 
491 /*
492  * Hostname information
493  */
494 extern char hw_serial[];	/* for userland-emulated hostid access */
495 extern int ddi_strtoul(const char *str, char **nptr, int base,
496     unsigned long *result);
497 
498 /* ZFS Boot Related stuff. */
499 
500 struct _buf {
501 	intptr_t	_fd;
502 };
503 
504 struct bootstat {
505 	uint64_t st_size;
506 };
507 
508 typedef struct ace_object {
509 	uid_t		a_who;
510 	uint32_t	a_access_mask;
511 	uint16_t	a_flags;
512 	uint16_t	a_type;
513 	uint8_t		a_obj_type[16];
514 	uint8_t		a_inherit_obj_type[16];
515 } ace_object_t;
516 
517 
518 #define	ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE	0x05
519 #define	ACE_ACCESS_DENIED_OBJECT_ACE_TYPE	0x06
520 #define	ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE	0x07
521 #define	ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE	0x08
522 
523 extern struct _buf *kobj_open_file(char *name);
524 extern int kobj_read_file(struct _buf *file, char *buf, unsigned size,
525     unsigned off);
526 extern void kobj_close_file(struct _buf *file);
527 extern int kobj_get_filesize(struct _buf *file, uint64_t *size);
528 extern int zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr);
529 extern int zfs_secpolicy_rename_perms(const char *from, const char *to,
530     cred_t *cr);
531 extern int zfs_secpolicy_destroy_perms(const char *name, cred_t *cr);
532 extern zoneid_t getzoneid(void);
533 
534 /* SID stuff */
535 typedef struct ksiddomain {
536 	uint_t	kd_ref;
537 	uint_t	kd_len;
538 	char	*kd_name;
539 } ksiddomain_t;
540 
541 ksiddomain_t *ksid_lookupdomain(const char *);
542 void ksiddomain_rele(ksiddomain_t *);
543 
544 #ifdef	__cplusplus
545 }
546 #endif
547 
548 #endif	/* _SYS_ZFS_CONTEXT_H */
549