xref: /illumos-gate/usr/src/uts/common/sys/zone.h (revision bbf21555)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
210dfe541eSEvan Layton 
227c478bd9Sstevel@tonic-gate /*
23134a1f4eSCasper H.S. Dik  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
246481fd49SIgor Kozhukhov  * Copyright 2014 Igor Kozhukhov <ikozhukhov@gmail.com>.
250dfe541eSEvan Layton  * Copyright 2019 Nexenta Systems, Inc. All rights reserved.
260dfe541eSEvan Layton  * Copyright 2020 Joyent, Inc.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #ifndef _SYS_ZONE_H
307c478bd9Sstevel@tonic-gate #define	_SYS_ZONE_H
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
34b3b35633SGordon Ross #include <sys/tsol/label.h>
35b3b35633SGordon Ross #include <sys/uadmin.h>
36b3b35633SGordon Ross #include <netinet/in.h>
37b3b35633SGordon Ross 
38b3b35633SGordon Ross #ifdef _KERNEL
39b3b35633SGordon Ross /*
40b3b35633SGordon Ross  * Many includes are kernel-only to reduce namespace pollution of
41b3b35633SGordon Ross  * userland applications.
42b3b35633SGordon Ross  */
43b3b35633SGordon Ross #include <sys/mutex.h>
447c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
45824c205fSml #include <sys/ipc_rctl.h>
467c478bd9Sstevel@tonic-gate #include <sys/pset.h>
47f4b3ec61Sdh #include <sys/cred.h>
48f4b3ec61Sdh #include <sys/netstack.h>
49835ee219SRobert Harris #include <sys/ksynch.h>
50550b6e40SSowmini Varadhan #include <sys/socket_impl.h>
51d2a70789SRichard Lowe #include <sys/secflags.h>
522918c4a3SJohn Levon #include <sys/cpu_uarray.h>
53b3b35633SGordon Ross #include <sys/list.h>
54b3b35633SGordon Ross #include <sys/loadavg.h>
55b3b35633SGordon Ross #endif	/* _KERNEL */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
587c478bd9Sstevel@tonic-gate extern "C" {
597c478bd9Sstevel@tonic-gate #endif
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * NOTE
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * The contents of this file are private to the implementation of
657c478bd9Sstevel@tonic-gate  * Solaris and are subject to change at any time without notice.
667c478bd9Sstevel@tonic-gate  * Applications and drivers using these interfaces may fail to
677c478bd9Sstevel@tonic-gate  * run on future releases.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /* Available both in kernel and for user space */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /* zone id restrictions and special ids */
737c478bd9Sstevel@tonic-gate #define	MAX_ZONEID	9999
747c478bd9Sstevel@tonic-gate #define	MIN_USERZONEID	1	/* lowest user-creatable zone ID */
757c478bd9Sstevel@tonic-gate #define	MIN_ZONEID	0	/* minimum zone ID on system */
767c478bd9Sstevel@tonic-gate #define	GLOBAL_ZONEID	0
777c478bd9Sstevel@tonic-gate #define	ZONEID_WIDTH	4	/* for printf */
787c478bd9Sstevel@tonic-gate 
7945916cd2Sjpk /*
8045916cd2Sjpk  * Special zoneid_t token to refer to all zones.
8145916cd2Sjpk  */
8245916cd2Sjpk #define	ALL_ZONES	(-1)
8345916cd2Sjpk 
847c478bd9Sstevel@tonic-gate /* system call subcodes */
85f4b3ec61Sdh #define	ZONE_CREATE		0
86f4b3ec61Sdh #define	ZONE_DESTROY		1
87f4b3ec61Sdh #define	ZONE_GETATTR		2
88f4b3ec61Sdh #define	ZONE_ENTER		3
89f4b3ec61Sdh #define	ZONE_LIST		4
90f4b3ec61Sdh #define	ZONE_SHUTDOWN		5
91f4b3ec61Sdh #define	ZONE_LOOKUP		6
92f4b3ec61Sdh #define	ZONE_BOOT		7
93f4b3ec61Sdh #define	ZONE_VERSION		8
94f4b3ec61Sdh #define	ZONE_SETATTR		9
95f4b3ec61Sdh #define	ZONE_ADD_DATALINK	10
96f4b3ec61Sdh #define	ZONE_DEL_DATALINK	11
97f4b3ec61Sdh #define	ZONE_CHECK_DATALINK	12
98f4b3ec61Sdh #define	ZONE_LIST_DATALINK	13
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /* zone attributes */
1017c478bd9Sstevel@tonic-gate #define	ZONE_ATTR_ROOT		1
1027c478bd9Sstevel@tonic-gate #define	ZONE_ATTR_NAME		2
1037c478bd9Sstevel@tonic-gate #define	ZONE_ATTR_STATUS	3
1047c478bd9Sstevel@tonic-gate #define	ZONE_ATTR_PRIVSET	4
1057c478bd9Sstevel@tonic-gate #define	ZONE_ATTR_UNIQID	5
1067c478bd9Sstevel@tonic-gate #define	ZONE_ATTR_POOLID	6
1077c478bd9Sstevel@tonic-gate #define	ZONE_ATTR_INITPID	7
10845916cd2Sjpk #define	ZONE_ATTR_SLBL		8
1093f2f09c1Sdp #define	ZONE_ATTR_INITNAME	9
1103f2f09c1Sdp #define	ZONE_ATTR_BOOTARGS	10
1119acbbeafSnn #define	ZONE_ATTR_BRAND		11
1120209230bSgjelinek #define	ZONE_ATTR_PHYS_MCAP	12
1130209230bSgjelinek #define	ZONE_ATTR_SCHED_CLASS	13
114f4b3ec61Sdh #define	ZONE_ATTR_FLAGS		14
1155679c89fSjv #define	ZONE_ATTR_HOSTID	15
1160fbb751dSJohn Levon #define	ZONE_ATTR_FS_ALLOWED	16
117550b6e40SSowmini Varadhan #define	ZONE_ATTR_NETWORK	17
118bafd1f14SJerry Jelinek #define	ZONE_ATTR_INITNORESTART	20
119d2a70789SRichard Lowe #define	ZONE_ATTR_SECFLAGS	21
12055fcd84fSJerry Jelinek #define	ZONE_ATTR_INITRESTART0	22
12155fcd84fSJerry Jelinek #define	ZONE_ATTR_INITREBOOT	23
1229acbbeafSnn 
1239acbbeafSnn /* Start of the brand-specific attribute namespace */
1249acbbeafSnn #define	ZONE_ATTR_BRAND_ATTRS	32768
1257c478bd9Sstevel@tonic-gate 
1260fbb751dSJohn Levon #define	ZONE_FS_ALLOWED_MAX	1024
1270fbb751dSJohn Levon 
128cf8f45c7Sdstaff #define	ZONE_EVENT_CHANNEL	"com.sun:zones:status"
129cf8f45c7Sdstaff #define	ZONE_EVENT_STATUS_CLASS	"status"
130cf8f45c7Sdstaff #define	ZONE_EVENT_STATUS_SUBCLASS	"change"
131cf8f45c7Sdstaff 
132cf8f45c7Sdstaff #define	ZONE_EVENT_UNINITIALIZED	"uninitialized"
133bd41d0a8Snordmark #define	ZONE_EVENT_INITIALIZED		"initialized"
134cf8f45c7Sdstaff #define	ZONE_EVENT_READY		"ready"
135cf8f45c7Sdstaff #define	ZONE_EVENT_RUNNING		"running"
136cf8f45c7Sdstaff #define	ZONE_EVENT_SHUTTING_DOWN	"shutting_down"
137cf8f45c7Sdstaff 
138cf8f45c7Sdstaff #define	ZONE_CB_NAME		"zonename"
139cf8f45c7Sdstaff #define	ZONE_CB_NEWSTATE	"newstate"
140cf8f45c7Sdstaff #define	ZONE_CB_OLDSTATE	"oldstate"
141cf8f45c7Sdstaff #define	ZONE_CB_TIMESTAMP	"when"
142cf8f45c7Sdstaff #define	ZONE_CB_ZONEID		"zoneid"
143cf8f45c7Sdstaff 
1449acbbeafSnn /*
1459acbbeafSnn  * Exit values that may be returned by scripts or programs invoked by various
1469acbbeafSnn  * zone commands.
1479acbbeafSnn  *
1489acbbeafSnn  * These are defined as:
1499acbbeafSnn  *
1509acbbeafSnn  *	ZONE_SUBPROC_OK
1519acbbeafSnn  *	===============
1529acbbeafSnn  *	The subprocess completed successfully.
1539acbbeafSnn  *
1549acbbeafSnn  *	ZONE_SUBPROC_USAGE
1559acbbeafSnn  *	==================
1569acbbeafSnn  *	The subprocess failed with a usage message, or a usage message should
1579acbbeafSnn  *	be output in its behalf.
1589acbbeafSnn  *
1599acbbeafSnn  *	ZONE_SUBPROC_NOTCOMPLETE
1609acbbeafSnn  *	========================
1619acbbeafSnn  *	The subprocess did not complete, but the actions performed by the
1629acbbeafSnn  *	subprocess require no recovery actions by the user.
1639acbbeafSnn  *
1649acbbeafSnn  *	For example, if the subprocess were called by "zoneadm install," the
1659acbbeafSnn  *	installation of the zone did not succeed but the user need not perform
1669acbbeafSnn  *	a "zoneadm uninstall" before attempting another install.
1679acbbeafSnn  *
1689acbbeafSnn  *	ZONE_SUBPROC_FATAL
1699acbbeafSnn  *	==================
1709acbbeafSnn  *	The subprocess failed in a fatal manner, usually one that will require
1719acbbeafSnn  *	some type of recovery action by the user.
1729acbbeafSnn  *
1739acbbeafSnn  *	For example, if the subprocess were called by "zoneadm install," the
1749acbbeafSnn  *	installation of the zone did not succeed and the user will need to
1759acbbeafSnn  *	perform a "zoneadm uninstall" before another install attempt is
1769acbbeafSnn  *	possible.
1779acbbeafSnn  *
1789acbbeafSnn  *	The non-success exit values are large to avoid accidental collision
1799acbbeafSnn  *	with values used internally by some commands (e.g. "Z_ERR" and
1809acbbeafSnn  *	"Z_USAGE" as used by zoneadm.)
1819acbbeafSnn  */
1829acbbeafSnn #define	ZONE_SUBPROC_OK			0
1839acbbeafSnn #define	ZONE_SUBPROC_USAGE		253
1849acbbeafSnn #define	ZONE_SUBPROC_NOTCOMPLETE	254
1859acbbeafSnn #define	ZONE_SUBPROC_FATAL		255
1869acbbeafSnn 
1877c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32
1887c478bd9Sstevel@tonic-gate typedef struct {
1897c478bd9Sstevel@tonic-gate 	caddr32_t zone_name;
1907c478bd9Sstevel@tonic-gate 	caddr32_t zone_root;
1917c478bd9Sstevel@tonic-gate 	caddr32_t zone_privs;
192821c4a97Sdp 	size32_t zone_privssz;
1937c478bd9Sstevel@tonic-gate 	caddr32_t rctlbuf;
1947c478bd9Sstevel@tonic-gate 	size32_t rctlbufsz;
1957c478bd9Sstevel@tonic-gate 	caddr32_t extended_error;
196fa9e4066Sahrens 	caddr32_t zfsbuf;
197fa9e4066Sahrens 	size32_t  zfsbufsz;
19842bc57c4Srica 	int match;			/* match level */
19942bc57c4Srica 	uint32_t doi;			/* DOI for label */
20042bc57c4Srica 	caddr32_t label;		/* label associated with zone */
201f4b3ec61Sdh 	int flags;
2027c478bd9Sstevel@tonic-gate } zone_def32;
2037c478bd9Sstevel@tonic-gate #endif
2047c478bd9Sstevel@tonic-gate typedef struct {
2057c478bd9Sstevel@tonic-gate 	const char *zone_name;
2067c478bd9Sstevel@tonic-gate 	const char *zone_root;
2077c478bd9Sstevel@tonic-gate 	const struct priv_set *zone_privs;
208821c4a97Sdp 	size_t zone_privssz;
2097c478bd9Sstevel@tonic-gate 	const char *rctlbuf;
2107c478bd9Sstevel@tonic-gate 	size_t rctlbufsz;
2117c478bd9Sstevel@tonic-gate 	int *extended_error;
212fa9e4066Sahrens 	const char *zfsbuf;
213fa9e4066Sahrens 	size_t zfsbufsz;
21445916cd2Sjpk 	int match;			/* match level */
21542bc57c4Srica 	uint32_t doi;			/* DOI for label */
21645916cd2Sjpk 	const bslabel_t *label;		/* label associated with zone */
217f4b3ec61Sdh 	int flags;
2187c478bd9Sstevel@tonic-gate } zone_def;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /* extended error information */
2217c478bd9Sstevel@tonic-gate #define	ZE_UNKNOWN	0	/* No extended error info */
2227c478bd9Sstevel@tonic-gate #define	ZE_CHROOTED	1	/* tried to zone_create from chroot */
2237c478bd9Sstevel@tonic-gate #define	ZE_AREMOUNTS	2	/* there are mounts within the zone */
2248c55461bSton #define	ZE_LABELINUSE	3	/* label is already in use by some other zone */
2257c478bd9Sstevel@tonic-gate 
226a19609f8Sjv /*
227a19609f8Sjv  * zone_status values
228a19609f8Sjv  *
229*bbf21555SRichard Lowe  * You must modify zone_status_names in mdb(1)'s genunix module
230a19609f8Sjv  * (genunix/zone.c) when you modify this enum.
231a19609f8Sjv  */
2327c478bd9Sstevel@tonic-gate typedef enum {
2337c478bd9Sstevel@tonic-gate 	ZONE_IS_UNINITIALIZED = 0,
234bd41d0a8Snordmark 	ZONE_IS_INITIALIZED,
2357c478bd9Sstevel@tonic-gate 	ZONE_IS_READY,
2367c478bd9Sstevel@tonic-gate 	ZONE_IS_BOOTING,
2377c478bd9Sstevel@tonic-gate 	ZONE_IS_RUNNING,
2387c478bd9Sstevel@tonic-gate 	ZONE_IS_SHUTTING_DOWN,
2397c478bd9Sstevel@tonic-gate 	ZONE_IS_EMPTY,
2407c478bd9Sstevel@tonic-gate 	ZONE_IS_DOWN,
2417c478bd9Sstevel@tonic-gate 	ZONE_IS_DYING,
2427c478bd9Sstevel@tonic-gate 	ZONE_IS_DEAD
2437c478bd9Sstevel@tonic-gate } zone_status_t;
2447c478bd9Sstevel@tonic-gate #define	ZONE_MIN_STATE		ZONE_IS_UNINITIALIZED
2457c478bd9Sstevel@tonic-gate #define	ZONE_MAX_STATE		ZONE_IS_DEAD
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * Valid commands which may be issued by zoneadm to zoneadmd.  The kernel also
2497c478bd9Sstevel@tonic-gate  * communicates with zoneadmd, but only uses Z_REBOOT and Z_HALT.
2507c478bd9Sstevel@tonic-gate  */
2517c478bd9Sstevel@tonic-gate typedef enum zone_cmd {
2529acbbeafSnn 	Z_READY, Z_BOOT, Z_FORCEBOOT, Z_REBOOT, Z_HALT, Z_NOTE_UNINSTALLING,
2533c7284bdSAlexander Eremin 	Z_MOUNT, Z_FORCEMOUNT, Z_UNMOUNT, Z_SHUTDOWN
2547c478bd9Sstevel@tonic-gate } zone_cmd_t;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate /*
2577c478bd9Sstevel@tonic-gate  * The structure of a request to zoneadmd.
2587c478bd9Sstevel@tonic-gate  */
2597c478bd9Sstevel@tonic-gate typedef struct zone_cmd_arg {
2607c478bd9Sstevel@tonic-gate 	uint64_t	uniqid;		/* unique "generation number" */
2617c478bd9Sstevel@tonic-gate 	zone_cmd_t	cmd;		/* requested action */
2623f2f09c1Sdp 	uint32_t	_pad;		/* need consistent 32/64 bit alignmt */
2637c478bd9Sstevel@tonic-gate 	char locale[MAXPATHLEN];	/* locale in which to render messages */
2643f2f09c1Sdp 	char bootbuf[BOOTARGS_MAX];	/* arguments passed to zone_boot() */
2657c478bd9Sstevel@tonic-gate } zone_cmd_arg_t;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate  * Structure of zoneadmd's response to a request.  A NULL return value means
2697c478bd9Sstevel@tonic-gate  * the caller should attempt to restart zoneadmd and retry.
2707c478bd9Sstevel@tonic-gate  */
2717c478bd9Sstevel@tonic-gate typedef struct zone_cmd_rval {
2727c478bd9Sstevel@tonic-gate 	int rval;			/* return value of request */
2737c478bd9Sstevel@tonic-gate 	char errbuf[1];	/* variable-sized buffer containing error messages */
2747c478bd9Sstevel@tonic-gate } zone_cmd_rval_t;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /*
2777c478bd9Sstevel@tonic-gate  * The zone support infrastructure uses the zone name as a component
2787c478bd9Sstevel@tonic-gate  * of unix domain (AF_UNIX) sockets, which are limited to 108 characters
2797c478bd9Sstevel@tonic-gate  * in length, so ZONENAME_MAX is limited by that.
2807c478bd9Sstevel@tonic-gate  */
2817c478bd9Sstevel@tonic-gate #define	ZONENAME_MAX		64
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate #define	GLOBAL_ZONENAME		"global"
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate /*
286*bbf21555SRichard Lowe  * Extended Regular expression (see regex(7)) which matches all valid zone
2877c478bd9Sstevel@tonic-gate  * names.
2887c478bd9Sstevel@tonic-gate  */
2897c478bd9Sstevel@tonic-gate #define	ZONENAME_REGEXP		"[a-zA-Z0-9][-_.a-zA-Z0-9]{0,62}"
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate /*
2927c478bd9Sstevel@tonic-gate  * Where the zones support infrastructure places temporary files.
2937c478bd9Sstevel@tonic-gate  */
2947c478bd9Sstevel@tonic-gate #define	ZONES_TMPDIR		"/var/run/zones"
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * The path to the door used by clients to communicate with zoneadmd.
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate #define	ZONE_DOOR_PATH		ZONES_TMPDIR "/%s.zoneadmd_door"
3007c478bd9Sstevel@tonic-gate 
301a19609f8Sjv 
302f4b3ec61Sdh /* zone_flags */
303a19609f8Sjv /*
304a19609f8Sjv  * Threads that read or write the following flag must hold zone_lock.
305a19609f8Sjv  */
306a19609f8Sjv #define	ZF_REFCOUNTS_LOGGED	0x1	/* a thread logged the zone's refs */
307a19609f8Sjv 
308a19609f8Sjv /*
309a19609f8Sjv  * The following threads are set when the zone is created and never changed.
310a19609f8Sjv  * Threads that test for these flags don't have to hold zone_lock.
311a19609f8Sjv  */
312f4b3ec61Sdh #define	ZF_HASHED_LABEL		0x2	/* zone has a unique label */
313f4b3ec61Sdh #define	ZF_IS_SCRATCH		0x4	/* scratch zone */
314f4b3ec61Sdh #define	ZF_NET_EXCL		0x8	/* Zone has an exclusive IP stack */
315f4b3ec61Sdh 
316a19609f8Sjv 
317f4b3ec61Sdh /* zone_create flags */
318f4b3ec61Sdh #define	ZCF_NET_EXCL		0x1	/* Create a zone with exclusive IP */
319f4b3ec61Sdh 
320550b6e40SSowmini Varadhan /* zone network properties */
321550b6e40SSowmini Varadhan #define	ZONE_NETWORK_ADDRESS	1
322550b6e40SSowmini Varadhan #define	ZONE_NETWORK_DEFROUTER	2
323550b6e40SSowmini Varadhan 
324550b6e40SSowmini Varadhan #define	ZONE_NET_ADDRNAME	"address"
325550b6e40SSowmini Varadhan #define	ZONE_NET_RTRNAME	"route"
326550b6e40SSowmini Varadhan 
327550b6e40SSowmini Varadhan typedef struct zone_net_data {
328550b6e40SSowmini Varadhan 	int zn_type;
329550b6e40SSowmini Varadhan 	int zn_len;
330550b6e40SSowmini Varadhan 	datalink_id_t zn_linkid;
331550b6e40SSowmini Varadhan 	uint8_t zn_val[1];
332550b6e40SSowmini Varadhan } zone_net_data_t;
333550b6e40SSowmini Varadhan 
334550b6e40SSowmini Varadhan 
3357c478bd9Sstevel@tonic-gate #ifdef _KERNEL
336a19609f8Sjv 
3377c478bd9Sstevel@tonic-gate #define	GLOBAL_ZONEUNIQID	0	/* uniqid of the global zone */
3387c478bd9Sstevel@tonic-gate 
3392918c4a3SJohn Levon /*
3402918c4a3SJohn Levon  * Indexes into ->zone_ustate array, summing the micro state of all threads in a
3412918c4a3SJohn Levon  * particular zone.
3422918c4a3SJohn Levon  */
3432918c4a3SJohn Levon #define	ZONE_USTATE_STIME (0)
3442918c4a3SJohn Levon #define	ZONE_USTATE_UTIME (1)
3452918c4a3SJohn Levon #define	ZONE_USTATE_WTIME (2)
3462918c4a3SJohn Levon #define	ZONE_USTATE_MAX (3)
3472918c4a3SJohn Levon 
3487c478bd9Sstevel@tonic-gate struct pool;
3499acbbeafSnn struct brand;
3507c478bd9Sstevel@tonic-gate 
351a19609f8Sjv /*
352a19609f8Sjv  * Each of these constants identifies a kernel subsystem that acquires and
353a19609f8Sjv  * releases zone references.  Each subsystem that invokes
354a19609f8Sjv  * zone_hold_ref() and zone_rele_ref() should specify the
355a19609f8Sjv  * zone_ref_subsys_t constant associated with the subsystem.  Tracked holds
356a19609f8Sjv  * help users and developers quickly identify subsystems that stall zone
357a19609f8Sjv  * shutdowns indefinitely.
358a19609f8Sjv  *
359a19609f8Sjv  * NOTE: You must modify zone_ref_subsys_names in usr/src/uts/common/os/zone.c
360a19609f8Sjv  * when you modify this enumeration.
361a19609f8Sjv  */
362a19609f8Sjv typedef enum zone_ref_subsys {
363a19609f8Sjv 	ZONE_REF_NFS,			/* NFS */
364a19609f8Sjv 	ZONE_REF_NFSV4,			/* NFSv4 */
365a19609f8Sjv 	ZONE_REF_SMBFS,			/* SMBFS */
366a19609f8Sjv 	ZONE_REF_MNTFS,			/* MNTFS */
367a19609f8Sjv 	ZONE_REF_LOFI,			/* LOFI devices */
368a19609f8Sjv 	ZONE_REF_VFS,			/* VFS infrastructure */
369a19609f8Sjv 	ZONE_REF_IPC,			/* IPC infrastructure */
370a19609f8Sjv 	ZONE_REF_NUM_SUBSYS		/* This must be the last entry. */
371a19609f8Sjv } zone_ref_subsys_t;
372a19609f8Sjv 
373a19609f8Sjv /*
374a19609f8Sjv  * zone_ref represents a general-purpose references to a zone.  Each zone's
375a19609f8Sjv  * references are linked into the zone's zone_t::zone_ref_list.  This allows
376a19609f8Sjv  * debuggers to walk zones' references.
377a19609f8Sjv  */
378a19609f8Sjv typedef struct zone_ref {
379a19609f8Sjv 	struct zone	*zref_zone; /* the zone to which the reference refers */
380a19609f8Sjv 	list_node_t	zref_linkage; /* linkage for zone_t::zone_ref_list */
381a19609f8Sjv } zone_ref_t;
382a19609f8Sjv 
383fa9e4066Sahrens /*
384fa9e4066Sahrens  * Structure to record list of ZFS datasets exported to a zone.
385fa9e4066Sahrens  */
386fa9e4066Sahrens typedef struct zone_dataset {
387fa9e4066Sahrens 	char		*zd_dataset;
388fa9e4066Sahrens 	list_node_t	zd_linkage;
389fa9e4066Sahrens } zone_dataset_t;
390fa9e4066Sahrens 
3910209230bSgjelinek /*
3920209230bSgjelinek  * structure for zone kstats
3930209230bSgjelinek  */
3940209230bSgjelinek typedef struct zone_kstat {
3950209230bSgjelinek 	kstat_named_t zk_zonename;
3960209230bSgjelinek 	kstat_named_t zk_usage;
3970209230bSgjelinek 	kstat_named_t zk_value;
3980209230bSgjelinek } zone_kstat_t;
3990209230bSgjelinek 
400c97ad5cdSakolb struct cpucap;
401c97ad5cdSakolb 
4029468939eSJerry Jelinek typedef struct {
4039468939eSJerry Jelinek 	kstat_named_t	zm_zonename;
4049468939eSJerry Jelinek 	kstat_named_t	zm_pgpgin;
4059468939eSJerry Jelinek 	kstat_named_t	zm_anonpgin;
4069468939eSJerry Jelinek 	kstat_named_t	zm_execpgin;
4079468939eSJerry Jelinek 	kstat_named_t	zm_fspgin;
408d14b1d19SJerry Jelinek 	kstat_named_t	zm_anon_alloc_fail;
4099468939eSJerry Jelinek } zone_mcap_kstat_t;
4109468939eSJerry Jelinek 
411542a813cSJerry Jelinek typedef struct {
412542a813cSJerry Jelinek 	kstat_named_t	zm_zonename;	/* full name, kstat truncates name */
413542a813cSJerry Jelinek 	kstat_named_t	zm_utime;
414542a813cSJerry Jelinek 	kstat_named_t	zm_stime;
415542a813cSJerry Jelinek 	kstat_named_t	zm_wtime;
41681d43577SJerry Jelinek 	kstat_named_t	zm_avenrun1;
41781d43577SJerry Jelinek 	kstat_named_t	zm_avenrun5;
41881d43577SJerry Jelinek 	kstat_named_t	zm_avenrun15;
4192dc692e0SJerry Jelinek 	kstat_named_t	zm_ffcap;
4202dc692e0SJerry Jelinek 	kstat_named_t	zm_ffnoproc;
4212dc692e0SJerry Jelinek 	kstat_named_t	zm_ffnomem;
4222dc692e0SJerry Jelinek 	kstat_named_t	zm_ffmisc;
42393cf283aSJerry Jelinek 	kstat_named_t	zm_nested_intp;
424a48d8120SJerry Jelinek 	kstat_named_t	zm_init_pid;
425a48d8120SJerry Jelinek 	kstat_named_t	zm_boot_time;
426542a813cSJerry Jelinek } zone_misc_kstat_t;
427542a813cSJerry Jelinek 
4287c478bd9Sstevel@tonic-gate typedef struct zone {
4297c478bd9Sstevel@tonic-gate 	/*
4307c478bd9Sstevel@tonic-gate 	 * zone_name is never modified once set.
4317c478bd9Sstevel@tonic-gate 	 */
4327c478bd9Sstevel@tonic-gate 	char		*zone_name;	/* zone's configuration name */
4337c478bd9Sstevel@tonic-gate 	/*
4347c478bd9Sstevel@tonic-gate 	 * zone_nodename and zone_domain are never freed once allocated.
4357c478bd9Sstevel@tonic-gate 	 */
4367c478bd9Sstevel@tonic-gate 	char		*zone_nodename;	/* utsname.nodename equivalent */
4377c478bd9Sstevel@tonic-gate 	char		*zone_domain;	/* srpc_domain equivalent */
4385679c89fSjv 	/*
4395679c89fSjv 	 * zone_hostid is used for per-zone hostid emulation.
4405679c89fSjv 	 * Currently it isn't modified after it's set (so no locks protect
4415679c89fSjv 	 * accesses), but that might have to change when we allow
4425679c89fSjv 	 * administrators to change running zones' properties.
4435679c89fSjv 	 *
4445679c89fSjv 	 * The global zone's zone_hostid must always be HW_INVALID_HOSTID so
4455679c89fSjv 	 * that zone_get_hostid() will function correctly.
4465679c89fSjv 	 */
4475679c89fSjv 	uint32_t	zone_hostid;	/* zone's hostid, HW_INVALID_HOSTID */
4485679c89fSjv 					/* if not emulated */
4497c478bd9Sstevel@tonic-gate 	/*
4507c478bd9Sstevel@tonic-gate 	 * zone_lock protects the following fields of a zone_t:
4512918c4a3SJohn Levon 	 *	zone_ref
4522918c4a3SJohn Levon 	 *	zone_cred_ref
4532918c4a3SJohn Levon 	 *	zone_subsys_ref
4542918c4a3SJohn Levon 	 *	zone_ref_list
4552918c4a3SJohn Levon 	 *	zone_ntasks
4562918c4a3SJohn Levon 	 *	zone_flags
4572918c4a3SJohn Levon 	 *	zone_zsd
458134a1f4eSCasper H.S. Dik 	 *	zone_pfexecd
4597c478bd9Sstevel@tonic-gate 	 */
4607c478bd9Sstevel@tonic-gate 	kmutex_t	zone_lock;
4617c478bd9Sstevel@tonic-gate 	/*
4627c478bd9Sstevel@tonic-gate 	 * zone_linkage is the zone's linkage into the active or
4637c478bd9Sstevel@tonic-gate 	 * death-row list.  The field is protected by zonehash_lock.
4647c478bd9Sstevel@tonic-gate 	 */
4657c478bd9Sstevel@tonic-gate 	list_node_t	zone_linkage;
4667c478bd9Sstevel@tonic-gate 	zoneid_t	zone_id;	/* ID of zone */
4677c478bd9Sstevel@tonic-gate 	uint_t		zone_ref;	/* count of zone_hold()s on zone */
4687c478bd9Sstevel@tonic-gate 	uint_t		zone_cred_ref;	/* count of zone_hold_cred()s on zone */
469a19609f8Sjv 	/*
470a19609f8Sjv 	 * Fixed-sized array of subsystem-specific reference counts
471a19609f8Sjv 	 * The sum of all of the counts must be less than or equal to zone_ref.
472a19609f8Sjv 	 * The array is indexed by the counts' subsystems' zone_ref_subsys_t
473a19609f8Sjv 	 * constants.
474a19609f8Sjv 	 */
475a19609f8Sjv 	uint_t		zone_subsys_ref[ZONE_REF_NUM_SUBSYS];
476a19609f8Sjv 	list_t		zone_ref_list;	/* list of zone_ref_t structs */
4777c478bd9Sstevel@tonic-gate 	/*
4787c478bd9Sstevel@tonic-gate 	 * zone_rootvp and zone_rootpath can never be modified once set.
4797c478bd9Sstevel@tonic-gate 	 */
4807c478bd9Sstevel@tonic-gate 	struct vnode	*zone_rootvp;	/* zone's root vnode */
4817c478bd9Sstevel@tonic-gate 	char		*zone_rootpath;	/* Path to zone's root + '/' */
4827c478bd9Sstevel@tonic-gate 	ushort_t	zone_flags;	/* misc flags */
4837c478bd9Sstevel@tonic-gate 	zone_status_t	zone_status;	/* protected by zone_status_lock */
4847c478bd9Sstevel@tonic-gate 	uint_t		zone_ntasks;	/* number of tasks executing in zone */
4857c478bd9Sstevel@tonic-gate 	kmutex_t	zone_nlwps_lock; /* protects zone_nlwps, and *_nlwps */
4867c478bd9Sstevel@tonic-gate 					/* counters in projects and tasks */
4877c478bd9Sstevel@tonic-gate 					/* that are within the zone */
4887c478bd9Sstevel@tonic-gate 	rctl_qty_t	zone_nlwps;	/* number of lwps in zone */
4897c478bd9Sstevel@tonic-gate 	rctl_qty_t	zone_nlwps_ctl; /* protected by zone_rctls->rcs_lock */
490824c205fSml 	rctl_qty_t	zone_shmmax;	/* System V shared memory usage */
491824c205fSml 	ipc_rqty_t	zone_ipc;	/* System V IPC id resource usage */
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	uint_t		zone_rootpathlen; /* strlen(zone_rootpath) + 1 */
4947c478bd9Sstevel@tonic-gate 	uint32_t	zone_shares;	/* FSS shares allocated to zone */
4957c478bd9Sstevel@tonic-gate 	rctl_set_t	*zone_rctls;	/* zone-wide (zone.*) rctls */
4960209230bSgjelinek 	kmutex_t	zone_mem_lock;	/* protects zone_locked_mem and */
497c6939658Ssl 					/* kpd_locked_mem for all */
4980209230bSgjelinek 					/* projects in zone. */
4990209230bSgjelinek 					/* Also protects zone_max_swap */
500c6939658Ssl 					/* grab after p_lock, before rcs_lock */
5010209230bSgjelinek 	rctl_qty_t	zone_locked_mem;	/* bytes of locked memory in */
5020209230bSgjelinek 						/* zone */
5030209230bSgjelinek 	rctl_qty_t	zone_locked_mem_ctl;	/* Current locked memory */
504c6939658Ssl 						/* limit.  Protected by */
505c6939658Ssl 						/* zone_rctls->rcs_lock */
5060209230bSgjelinek 	rctl_qty_t	zone_max_swap; /* bytes of swap reserved by zone */
5070209230bSgjelinek 	rctl_qty_t	zone_max_swap_ctl;	/* current swap limit. */
5080209230bSgjelinek 						/* Protected by */
5090209230bSgjelinek 						/* zone_rctls->rcs_lock */
5100fbb751dSJohn Levon 	kmutex_t	zone_rctl_lock;	/* protects zone_max_lofi */
5110fbb751dSJohn Levon 	rctl_qty_t	zone_max_lofi; /* lofi devs for zone */
5120fbb751dSJohn Levon 	rctl_qty_t	zone_max_lofi_ctl;	/* current lofi limit. */
5130fbb751dSJohn Levon 						/* Protected by */
5140fbb751dSJohn Levon 						/* zone_rctls->rcs_lock */
5157c478bd9Sstevel@tonic-gate 	list_t		zone_zsd;	/* list of Zone-Specific Data values */
5167c478bd9Sstevel@tonic-gate 	kcondvar_t	zone_cv;	/* used to signal state changes */
5177c478bd9Sstevel@tonic-gate 	struct proc	*zone_zsched;	/* Dummy kernel "zsched" process */
5187c478bd9Sstevel@tonic-gate 	pid_t		zone_proc_initpid; /* pid of "init" for this zone */
5193f2f09c1Sdp 	char		*zone_initname;	/* fs path to 'init' */
5207c478bd9Sstevel@tonic-gate 	int		zone_boot_err;  /* for zone_boot() if boot fails */
5217c478bd9Sstevel@tonic-gate 	char		*zone_bootargs;	/* arguments passed via zone_boot() */
5220209230bSgjelinek 	uint64_t	zone_phys_mcap;	/* physical memory cap */
5237c478bd9Sstevel@tonic-gate 	/*
5247c478bd9Sstevel@tonic-gate 	 * zone_kthreads is protected by zone_status_lock.
5257c478bd9Sstevel@tonic-gate 	 */
5267c478bd9Sstevel@tonic-gate 	kthread_t	*zone_kthreads;	/* kernel threads in zone */
5277c478bd9Sstevel@tonic-gate 	struct priv_set	*zone_privset;	/* limit set for zone */
5287c478bd9Sstevel@tonic-gate 	/*
5297c478bd9Sstevel@tonic-gate 	 * zone_vfslist is protected by vfs_list_lock().
5307c478bd9Sstevel@tonic-gate 	 */
5317c478bd9Sstevel@tonic-gate 	struct vfs	*zone_vfslist;	/* list of FS's mounted in zone */
5327c478bd9Sstevel@tonic-gate 	uint64_t	zone_uniqid;	/* unique zone generation number */
5337c478bd9Sstevel@tonic-gate 	struct cred	*zone_kcred;	/* kcred-like, zone-limited cred */
5347c478bd9Sstevel@tonic-gate 	/*
5357c478bd9Sstevel@tonic-gate 	 * zone_pool is protected by pool_lock().
5367c478bd9Sstevel@tonic-gate 	 */
5377c478bd9Sstevel@tonic-gate 	struct pool	*zone_pool;	/* pool the zone is bound to */
5387c478bd9Sstevel@tonic-gate 	hrtime_t	zone_pool_mod;	/* last pool bind modification time */
5397c478bd9Sstevel@tonic-gate 	/* zone_psetid is protected by cpu_lock */
5407c478bd9Sstevel@tonic-gate 	psetid_t	zone_psetid;	/* pset the zone is bound to */
54186ad481cSJerry Jelinek 
54286ad481cSJerry Jelinek 	time_t		zone_boot_time;	/* Similar to boot_time */
54386ad481cSJerry Jelinek 
5447c478bd9Sstevel@tonic-gate 	/*
5457c478bd9Sstevel@tonic-gate 	 * The following two can be read without holding any locks.  They are
5467c478bd9Sstevel@tonic-gate 	 * updated under cpu_lock.
5477c478bd9Sstevel@tonic-gate 	 */
5487c478bd9Sstevel@tonic-gate 	int		zone_ncpus;  /* zone's idea of ncpus */
5497c478bd9Sstevel@tonic-gate 	int		zone_ncpus_online; /* zone's idea of ncpus_online */
550fa9e4066Sahrens 	/*
551fa9e4066Sahrens 	 * List of ZFS datasets exported to this zone.
552fa9e4066Sahrens 	 */
553fa9e4066Sahrens 	list_t		zone_datasets;	/* list of datasets */
55445916cd2Sjpk 
55542bc57c4Srica 	ts_label_t	*zone_slabel;	/* zone sensitivity label */
55642bc57c4Srica 	int		zone_match;	/* require label match for packets */
55745916cd2Sjpk 	tsol_mlp_list_t zone_mlps;	/* MLPs on zone-private addresses */
5583f2f09c1Sdp 
5599acbbeafSnn 	boolean_t	zone_restart_init;	/* Restart init if it dies? */
56055fcd84fSJerry Jelinek 	boolean_t	zone_reboot_on_init_exit; /* Reboot if init dies? */
56155fcd84fSJerry Jelinek 	boolean_t	zone_restart_init_0;	/* Restart only if it exits 0 */
5629acbbeafSnn 	struct brand	*zone_brand;		/* zone's brand */
5632918c4a3SJohn Levon 	void		*zone_brand_data;	/* store brand specific data */
5640209230bSgjelinek 	id_t		zone_defaultcid;	/* dflt scheduling class id */
5650209230bSgjelinek 	kstat_t		*zone_swapresv_kstat;
5660209230bSgjelinek 	kstat_t		*zone_lockedmem_kstat;
567f4b3ec61Sdh 	/*
568f4b3ec61Sdh 	 * zone_dl_list is protected by zone_lock
569f4b3ec61Sdh 	 */
5702b24ab6bSSebastien Roy 	list_t		zone_dl_list;
571f4b3ec61Sdh 	netstack_t	*zone_netstack;
572c97ad5cdSakolb 	struct cpucap	*zone_cpucap;	/* CPU caps data */
5739e9e6ab8Spaulson 	/*
5749e9e6ab8Spaulson 	 * Solaris Auditing per-zone audit context
5759e9e6ab8Spaulson 	 */
5769e9e6ab8Spaulson 	struct au_kcontext	*zone_audit_kctxt;
577835ee219SRobert Harris 	/*
578835ee219SRobert Harris 	 * For private use by mntfs.
579835ee219SRobert Harris 	 */
580835ee219SRobert Harris 	struct mntelem	*zone_mntfs_db;
581835ee219SRobert Harris 	krwlock_t	zone_mntfs_db_lock;
582134a1f4eSCasper H.S. Dik 
583134a1f4eSCasper H.S. Dik 	struct klpd_reg		*zone_pfexecd;
5840fbb751dSJohn Levon 
5850fbb751dSJohn Levon 	char		*zone_fs_allowed;
586ff19e029SMenno Lageman 	rctl_qty_t	zone_nprocs;	/* number of processes in the zone */
587ff19e029SMenno Lageman 	rctl_qty_t	zone_nprocs_ctl;	/* current limit protected by */
588ff19e029SMenno Lageman 						/* zone_rctls->rcs_lock */
589ff19e029SMenno Lageman 	kstat_t		*zone_nprocs_kstat;
590b0f673c4SBryan Cantrill 
5919468939eSJerry Jelinek 	kmutex_t	zone_mcap_lock;	/* protects mcap statistics */
5929468939eSJerry Jelinek 	kstat_t		*zone_mcap_ksp;
5939468939eSJerry Jelinek 	zone_mcap_kstat_t *zone_mcap_stats;
5949468939eSJerry Jelinek 	uint64_t	zone_pgpgin;		/* pages paged in */
5959468939eSJerry Jelinek 	uint64_t	zone_anonpgin;		/* anon pages paged in */
5969468939eSJerry Jelinek 	uint64_t	zone_execpgin;		/* exec pages paged in */
5979468939eSJerry Jelinek 	uint64_t	zone_fspgin;		/* fs pages paged in */
598d14b1d19SJerry Jelinek 	uint64_t	zone_anon_alloc_fail;	/* cnt of anon alloc fails */
5999468939eSJerry Jelinek 
600d2a70789SRichard Lowe 	psecflags_t	zone_secflags; /* default zone security-flags */
601d2a70789SRichard Lowe 
602542a813cSJerry Jelinek 	/*
603542a813cSJerry Jelinek 	 * Misc. kstats and counters for zone cpu-usage aggregation.
604542a813cSJerry Jelinek 	 */
605542a813cSJerry Jelinek 	kmutex_t	zone_misc_lock;		/* protects misc statistics */
606542a813cSJerry Jelinek 	kstat_t		*zone_misc_ksp;
607542a813cSJerry Jelinek 	zone_misc_kstat_t *zone_misc_stats;
6082918c4a3SJohn Levon 	/* Accumulated microstate for all threads in this zone. */
6092918c4a3SJohn Levon 	cpu_uarray_t	*zone_ustate;
6102dc692e0SJerry Jelinek 	/* fork-fail kstat tracking */
6112dc692e0SJerry Jelinek 	uint32_t	zone_ffcap;		/* hit an rctl cap */
6122dc692e0SJerry Jelinek 	uint32_t	zone_ffnoproc;		/* get proc/lwp error */
6132dc692e0SJerry Jelinek 	uint32_t	zone_ffnomem;		/* as_dup/memory error */
6142dc692e0SJerry Jelinek 	uint32_t	zone_ffmisc;		/* misc. other error */
615542a813cSJerry Jelinek 
61693cf283aSJerry Jelinek 	uint32_t	zone_nested_intp;	/* nested interp. kstat */
61793cf283aSJerry Jelinek 
61881d43577SJerry Jelinek 	struct loadavg_s zone_loadavg;		/* loadavg for this zone */
61981d43577SJerry Jelinek 	uint64_t	zone_hp_avenrun[3];	/* high-precision avenrun */
62081d43577SJerry Jelinek 	int		zone_avenrun[3];	/* FSCALED avg. run queue len */
62181d43577SJerry Jelinek 
622482a7749SJerry Jelinek 	/*
623482a7749SJerry Jelinek 	 * FSS stats updated once per second by fss_decay_usage.
624482a7749SJerry Jelinek 	 */
625482a7749SJerry Jelinek 	uint32_t	zone_fss_gen;		/* FSS generation cntr */
626482a7749SJerry Jelinek 	uint64_t	zone_run_ticks;		/* tot # of ticks running */
627482a7749SJerry Jelinek 
628b0f673c4SBryan Cantrill 	/*
629b0f673c4SBryan Cantrill 	 * DTrace-private per-zone state
630b0f673c4SBryan Cantrill 	 */
631b0f673c4SBryan Cantrill 	int		zone_dtrace_getf;	/* # of unprivileged getf()s */
6325fd5c689SJerry Jelinek 
6335fd5c689SJerry Jelinek 	/*
6345fd5c689SJerry Jelinek 	 * Synchronization primitives used to synchronize between mounts and
6355fd5c689SJerry Jelinek 	 * zone creation/destruction.
6365fd5c689SJerry Jelinek 	 */
6375fd5c689SJerry Jelinek 	int		zone_mounts_in_progress;
6385fd5c689SJerry Jelinek 	kcondvar_t	zone_mount_cv;
6395fd5c689SJerry Jelinek 	kmutex_t	zone_mount_lock;
6407c478bd9Sstevel@tonic-gate } zone_t;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate /*
6437c478bd9Sstevel@tonic-gate  * Special value of zone_psetid to indicate that pools are disabled.
6447c478bd9Sstevel@tonic-gate  */
6457c478bd9Sstevel@tonic-gate #define	ZONE_PS_INVAL	PS_MYID
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate extern zone_t zone0;
6487c478bd9Sstevel@tonic-gate extern zone_t *global_zone;
6497c478bd9Sstevel@tonic-gate extern uint_t maxzones;
6507c478bd9Sstevel@tonic-gate extern rctl_hndl_t rc_zone_nlwps;
651ff19e029SMenno Lageman extern rctl_hndl_t rc_zone_nprocs;
6527c478bd9Sstevel@tonic-gate 
653fa9e4066Sahrens extern long zone(int, void *, void *, void *, void *);
6547c478bd9Sstevel@tonic-gate extern void zone_zsd_init(void);
6557c478bd9Sstevel@tonic-gate extern void zone_init(void);
6567c478bd9Sstevel@tonic-gate extern void zone_hold(zone_t *);
6577c478bd9Sstevel@tonic-gate extern void zone_rele(zone_t *);
658a19609f8Sjv extern void zone_init_ref(zone_ref_t *);
659a19609f8Sjv extern void zone_hold_ref(zone_t *, zone_ref_t *, zone_ref_subsys_t);
660a19609f8Sjv extern void zone_rele_ref(zone_ref_t *, zone_ref_subsys_t);
6617c478bd9Sstevel@tonic-gate extern void zone_cred_hold(zone_t *);
6627c478bd9Sstevel@tonic-gate extern void zone_cred_rele(zone_t *);
6637c478bd9Sstevel@tonic-gate extern void zone_task_hold(zone_t *);
6647c478bd9Sstevel@tonic-gate extern void zone_task_rele(zone_t *);
6657c478bd9Sstevel@tonic-gate extern zone_t *zone_find_by_id(zoneid_t);
66645916cd2Sjpk extern zone_t *zone_find_by_label(const ts_label_t *);
6677c478bd9Sstevel@tonic-gate extern zone_t *zone_find_by_name(char *);
66845916cd2Sjpk extern zone_t *zone_find_by_any_path(const char *, boolean_t);
66948451833Scarlsonj extern zone_t *zone_find_by_path(const char *);
6707c478bd9Sstevel@tonic-gate extern zoneid_t getzoneid(void);
671f4b3ec61Sdh extern zone_t *zone_find_by_id_nolock(zoneid_t);
6722b24ab6bSSebastien Roy extern int zone_datalink_walk(zoneid_t, int (*)(datalink_id_t, void *), void *);
6732b24ab6bSSebastien Roy extern int zone_check_datalink(zoneid_t *, datalink_id_t);
67481d43577SJerry Jelinek extern void zone_loadavg_update();
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate /*
6777c478bd9Sstevel@tonic-gate  * Zone-specific data (ZSD) APIs
6787c478bd9Sstevel@tonic-gate  */
6797c478bd9Sstevel@tonic-gate /*
6807c478bd9Sstevel@tonic-gate  * The following is what code should be initializing its zone_key_t to if it
6817c478bd9Sstevel@tonic-gate  * calls zone_getspecific() without necessarily knowing that zone_key_create()
6827c478bd9Sstevel@tonic-gate  * has been called on the key.
6837c478bd9Sstevel@tonic-gate  */
6847c478bd9Sstevel@tonic-gate #define	ZONE_KEY_UNINITIALIZED	0
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate typedef uint_t zone_key_t;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate extern void	zone_key_create(zone_key_t *, void *(*)(zoneid_t),
6897c478bd9Sstevel@tonic-gate     void (*)(zoneid_t, void *), void (*)(zoneid_t, void *));
6902918c4a3SJohn Levon extern int	zone_key_delete(zone_key_t);
6917c478bd9Sstevel@tonic-gate extern void	*zone_getspecific(zone_key_t, zone_t *);
6927c478bd9Sstevel@tonic-gate extern int	zone_setspecific(zone_key_t, zone_t *, const void *);
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate /*
6957c478bd9Sstevel@tonic-gate  * The definition of a zsd_entry is truly private to zone.c and is only
6967c478bd9Sstevel@tonic-gate  * placed here so it can be shared with mdb.
697bd41d0a8Snordmark  *
698bd41d0a8Snordmark  * State maintained for each zone times each registered key, which tracks
699bd41d0a8Snordmark  * the state of the create, shutdown and destroy callbacks.
700bd41d0a8Snordmark  *
701bd41d0a8Snordmark  * zsd_flags is used to keep track of pending actions to avoid holding locks
702bd41d0a8Snordmark  * when calling the create/shutdown/destroy callbacks, since doing so
703bd41d0a8Snordmark  * could lead to deadlocks.
7047c478bd9Sstevel@tonic-gate  */
7057c478bd9Sstevel@tonic-gate struct zsd_entry {
7067c478bd9Sstevel@tonic-gate 	zone_key_t		zsd_key;	/* Key used to lookup value */
7077c478bd9Sstevel@tonic-gate 	void			*zsd_data;	/* Caller-managed value */
7087c478bd9Sstevel@tonic-gate 	/*
7097c478bd9Sstevel@tonic-gate 	 * Callbacks to be executed when a zone is created, shutdown, and
7107c478bd9Sstevel@tonic-gate 	 * destroyed, respectively.
7117c478bd9Sstevel@tonic-gate 	 */
7127c478bd9Sstevel@tonic-gate 	void			*(*zsd_create)(zoneid_t);
7137c478bd9Sstevel@tonic-gate 	void			(*zsd_shutdown)(zoneid_t, void *);
7147c478bd9Sstevel@tonic-gate 	void			(*zsd_destroy)(zoneid_t, void *);
7157c478bd9Sstevel@tonic-gate 	list_node_t		zsd_linkage;
7162918c4a3SJohn Levon 	uint16_t		zsd_flags;	/* See below */
717bd41d0a8Snordmark 	kcondvar_t		zsd_cv;
7187c478bd9Sstevel@tonic-gate };
7197c478bd9Sstevel@tonic-gate 
720bd41d0a8Snordmark /*
721bd41d0a8Snordmark  * zsd_flags
722bd41d0a8Snordmark  */
723bd41d0a8Snordmark #define	ZSD_CREATE_NEEDED	0x0001
724bd41d0a8Snordmark #define	ZSD_CREATE_INPROGRESS	0x0002
725bd41d0a8Snordmark #define	ZSD_CREATE_COMPLETED	0x0004
726bd41d0a8Snordmark #define	ZSD_SHUTDOWN_NEEDED	0x0010
727bd41d0a8Snordmark #define	ZSD_SHUTDOWN_INPROGRESS	0x0020
728bd41d0a8Snordmark #define	ZSD_SHUTDOWN_COMPLETED	0x0040
729bd41d0a8Snordmark #define	ZSD_DESTROY_NEEDED	0x0100
730bd41d0a8Snordmark #define	ZSD_DESTROY_INPROGRESS	0x0200
731bd41d0a8Snordmark #define	ZSD_DESTROY_COMPLETED	0x0400
732bd41d0a8Snordmark 
733bd41d0a8Snordmark #define	ZSD_CREATE_ALL	\
734bd41d0a8Snordmark 	(ZSD_CREATE_NEEDED|ZSD_CREATE_INPROGRESS|ZSD_CREATE_COMPLETED)
735bd41d0a8Snordmark #define	ZSD_SHUTDOWN_ALL	\
736bd41d0a8Snordmark 	(ZSD_SHUTDOWN_NEEDED|ZSD_SHUTDOWN_INPROGRESS|ZSD_SHUTDOWN_COMPLETED)
737bd41d0a8Snordmark #define	ZSD_DESTROY_ALL	\
738bd41d0a8Snordmark 	(ZSD_DESTROY_NEEDED|ZSD_DESTROY_INPROGRESS|ZSD_DESTROY_COMPLETED)
739bd41d0a8Snordmark 
740bd41d0a8Snordmark #define	ZSD_ALL_INPROGRESS \
741bd41d0a8Snordmark 	(ZSD_CREATE_INPROGRESS|ZSD_SHUTDOWN_INPROGRESS|ZSD_DESTROY_INPROGRESS)
742bd41d0a8Snordmark 
7437c478bd9Sstevel@tonic-gate /*
7447c478bd9Sstevel@tonic-gate  * Macros to help with zone visibility restrictions.
7457c478bd9Sstevel@tonic-gate  */
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate /*
7487c478bd9Sstevel@tonic-gate  * Is process in the global zone?
7497c478bd9Sstevel@tonic-gate  */
7507c478bd9Sstevel@tonic-gate #define	INGLOBALZONE(p) \
7517c478bd9Sstevel@tonic-gate 	((p)->p_zone == global_zone)
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate /*
7547c478bd9Sstevel@tonic-gate  * Can process view objects in given zone?
7557c478bd9Sstevel@tonic-gate  */
7567c478bd9Sstevel@tonic-gate #define	HASZONEACCESS(p, zoneid) \
7577c478bd9Sstevel@tonic-gate 	((p)->p_zone->zone_id == (zoneid) || INGLOBALZONE(p))
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate /*
7607c478bd9Sstevel@tonic-gate  * Convenience macro to see if a resolved path is visible from within a
7617c478bd9Sstevel@tonic-gate  * given zone.
7627c478bd9Sstevel@tonic-gate  *
7637c478bd9Sstevel@tonic-gate  * The basic idea is that the first (zone_rootpathlen - 1) bytes of the
7647c478bd9Sstevel@tonic-gate  * two strings must be equal.  Since the rootpathlen has a trailing '/',
7657c478bd9Sstevel@tonic-gate  * we want to skip everything in the path up to (but not including) the
7667c478bd9Sstevel@tonic-gate  * trailing '/'.
7677c478bd9Sstevel@tonic-gate  */
7687c478bd9Sstevel@tonic-gate #define	ZONE_PATH_VISIBLE(path, zone) \
7697c478bd9Sstevel@tonic-gate 	(strncmp((path), (zone)->zone_rootpath,		\
7707c478bd9Sstevel@tonic-gate 	    (zone)->zone_rootpathlen - 1) == 0)
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate /*
7737c478bd9Sstevel@tonic-gate  * Convenience macro to go from the global view of a path to that seen
7747c478bd9Sstevel@tonic-gate  * from within said zone.  It is the responsibility of the caller to
7757c478bd9Sstevel@tonic-gate  * ensure that the path is a resolved one (ie, no '..'s or '.'s), and is
7767c478bd9Sstevel@tonic-gate  * in fact visible from within the zone.
7777c478bd9Sstevel@tonic-gate  */
7787c478bd9Sstevel@tonic-gate #define	ZONE_PATH_TRANSLATE(path, zone)	\
7797c478bd9Sstevel@tonic-gate 	(ASSERT(ZONE_PATH_VISIBLE(path, zone)),	\
7807c478bd9Sstevel@tonic-gate 	(path) + (zone)->zone_rootpathlen - 2)
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate /*
7837c478bd9Sstevel@tonic-gate  * Special processes visible in all zones.
7847c478bd9Sstevel@tonic-gate  */
7857c478bd9Sstevel@tonic-gate #define	ZONE_SPECIALPID(x)	 ((x) == 0 || (x) == 1)
7867c478bd9Sstevel@tonic-gate 
7870dfe541eSEvan Layton /*
7880dfe541eSEvan Layton  * A root vnode of the current zone.
7890dfe541eSEvan Layton  *
7900dfe541eSEvan Layton  * NOTE: It may be necessary (initialization time for file sharing where an
7910dfe541eSEvan Layton  * NGZ loads a file-sharing kernel module that does zsd initialization) to NOT
7920dfe541eSEvan Layton  * use this macro. One should ASSERT() that curzone == active ZSD (an
7930dfe541eSEvan Layton  * ASSERTion that's not always true at ZSD initialization time) during regular
7940dfe541eSEvan Layton  * use of this macro.
7950dfe541eSEvan Layton  */
7960dfe541eSEvan Layton #define	ZONE_ROOTVP()	(curzone->zone_rootvp)
7970dfe541eSEvan Layton 
7980dfe541eSEvan Layton /*
7990dfe541eSEvan Layton  * Since a zone's root isn't necessarily an actual filesystem boundary
8000dfe541eSEvan Layton  * (i.e. VROOT may not be set on zone->zone_rootvp) we need to not assume it.
8010dfe541eSEvan Layton  * This macro helps in checking if a vnode is the current zone's rootvp.
8020dfe541eSEvan Layton  * NOTE:  Using the VN_ prefix, even though it's defined here in zone.h.
8030dfe541eSEvan Layton  * NOTE2: See above warning about ZONE_ROOTVP().
8040dfe541eSEvan Layton  */
8050dfe541eSEvan Layton #define	VN_IS_CURZONEROOT(vp)   (VN_CMP(vp, ZONE_ROOTVP()))
8060dfe541eSEvan Layton 
8077c478bd9Sstevel@tonic-gate /*
8087c478bd9Sstevel@tonic-gate  * Zone-safe version of thread_create() to be used when the caller wants to
8097c478bd9Sstevel@tonic-gate  * create a kernel thread to run within the current zone's context.
8107c478bd9Sstevel@tonic-gate  */
8117c478bd9Sstevel@tonic-gate extern kthread_t *zthread_create(caddr_t, size_t, void (*)(), void *, size_t,
8127c478bd9Sstevel@tonic-gate     pri_t);
8137c478bd9Sstevel@tonic-gate extern void zthread_exit(void);
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate /*
8167c478bd9Sstevel@tonic-gate  * Functions for an external observer to register interest in a zone's status
8177c478bd9Sstevel@tonic-gate  * change.  Observers will be woken up when the zone status equals the status
8187c478bd9Sstevel@tonic-gate  * argument passed in (in the case of zone_status_timedwait, the function may
8197c478bd9Sstevel@tonic-gate  * also return because of a timeout; zone_status_wait_sig may return early due
8207c478bd9Sstevel@tonic-gate  * to a signal being delivered; zone_status_timedwait_sig may return for any of
8217c478bd9Sstevel@tonic-gate  * the above reasons).
8227c478bd9Sstevel@tonic-gate  *
8237c478bd9Sstevel@tonic-gate  * Otherwise these behave identically to cv_timedwait(), cv_wait(), and
8247c478bd9Sstevel@tonic-gate  * cv_wait_sig() respectively.
8257c478bd9Sstevel@tonic-gate  */
8267c478bd9Sstevel@tonic-gate extern clock_t zone_status_timedwait(zone_t *, clock_t, zone_status_t);
8277c478bd9Sstevel@tonic-gate extern clock_t zone_status_timedwait_sig(zone_t *, clock_t, zone_status_t);
8287c478bd9Sstevel@tonic-gate extern void zone_status_wait(zone_t *, zone_status_t);
8297c478bd9Sstevel@tonic-gate extern int zone_status_wait_sig(zone_t *, zone_status_t);
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate /*
8327c478bd9Sstevel@tonic-gate  * Get the status  of the zone (at the time it was called).  The state may
8337c478bd9Sstevel@tonic-gate  * have progressed by the time it is returned.
8347c478bd9Sstevel@tonic-gate  */
8357c478bd9Sstevel@tonic-gate extern zone_status_t zone_status_get(zone_t *);
8367c478bd9Sstevel@tonic-gate 
8375679c89fSjv /*
8385679c89fSjv  * Safely get the hostid of the specified zone (defaults to machine's hostid
8395679c89fSjv  * if the specified zone doesn't emulate a hostid).  Passing NULL retrieves
8405679c89fSjv  * the global zone's (i.e., physical system's) hostid.
8415679c89fSjv  */
8425679c89fSjv extern uint32_t zone_get_hostid(zone_t *);
8435679c89fSjv 
8447c478bd9Sstevel@tonic-gate /*
8457c478bd9Sstevel@tonic-gate  * Get the "kcred" credentials corresponding to the given zone.
8467c478bd9Sstevel@tonic-gate  */
8477c478bd9Sstevel@tonic-gate extern struct cred *zone_get_kcred(zoneid_t);
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate /*
8507c478bd9Sstevel@tonic-gate  * Get/set the pool the zone is currently bound to.
8517c478bd9Sstevel@tonic-gate  */
8527c478bd9Sstevel@tonic-gate extern struct pool *zone_pool_get(zone_t *);
8537c478bd9Sstevel@tonic-gate extern void zone_pool_set(zone_t *, struct pool *);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate /*
8567c478bd9Sstevel@tonic-gate  * Get/set the pset the zone is currently using.
8577c478bd9Sstevel@tonic-gate  */
8587c478bd9Sstevel@tonic-gate extern psetid_t zone_pset_get(zone_t *);
8597c478bd9Sstevel@tonic-gate extern void zone_pset_set(zone_t *, psetid_t);
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate /*
8627c478bd9Sstevel@tonic-gate  * Get the number of cpus/online-cpus visible from the given zone.
8637c478bd9Sstevel@tonic-gate  */
8647c478bd9Sstevel@tonic-gate extern int zone_ncpus_get(zone_t *);
8657c478bd9Sstevel@tonic-gate extern int zone_ncpus_online_get(zone_t *);
8667c478bd9Sstevel@tonic-gate 
867fa9e4066Sahrens /*
868fa9e4066Sahrens  * Returns true if the named pool/dataset is visible in the current zone.
869fa9e4066Sahrens  */
870fa9e4066Sahrens extern int zone_dataset_visible(const char *, int *);
871fa9e4066Sahrens 
8727c478bd9Sstevel@tonic-gate /*
8733f2f09c1Sdp  * zone version of kadmin()
8747c478bd9Sstevel@tonic-gate  */
8753f2f09c1Sdp extern int zone_kadmin(int, int, const char *, cred_t *);
8767c478bd9Sstevel@tonic-gate extern void zone_shutdown_global(void);
8777c478bd9Sstevel@tonic-gate 
8785fd5c689SJerry Jelinek extern void mount_in_progress(zone_t *);
8795fd5c689SJerry Jelinek extern void mount_completed(zone_t *);
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate extern int zone_walk(int (*)(zone_t *, void *), void *);
8827c478bd9Sstevel@tonic-gate 
883c6939658Ssl extern rctl_hndl_t rc_zone_locked_mem;
8840209230bSgjelinek extern rctl_hndl_t rc_zone_max_swap;
8850fbb751dSJohn Levon extern rctl_hndl_t rc_zone_max_lofi;
886c6939658Ssl 
8877c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate #endif
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate #endif	/* _SYS_ZONE_H */
894