xref: /onnv-gate/usr/src/uts/common/os/zone.c (revision 6073:47f6aa7a8077)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21390Sraf 
220Sstevel@tonic-gate /*
235880Snordmark  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Zones
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  *   A zone is a named collection of processes, namespace constraints,
330Sstevel@tonic-gate  *   and other system resources which comprise a secure and manageable
340Sstevel@tonic-gate  *   application containment facility.
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  *   Zones (represented by the reference counted zone_t) are tracked in
370Sstevel@tonic-gate  *   the kernel in the zonehash.  Elsewhere in the kernel, Zone IDs
380Sstevel@tonic-gate  *   (zoneid_t) are used to track zone association.  Zone IDs are
390Sstevel@tonic-gate  *   dynamically generated when the zone is created; if a persistent
400Sstevel@tonic-gate  *   identifier is needed (core files, accounting logs, audit trail,
410Sstevel@tonic-gate  *   etc.), the zone name should be used.
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  *   Global Zone:
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  *   The global zone (zoneid 0) is automatically associated with all
470Sstevel@tonic-gate  *   system resources that have not been bound to a user-created zone.
480Sstevel@tonic-gate  *   This means that even systems where zones are not in active use
490Sstevel@tonic-gate  *   have a global zone, and all processes, mounts, etc. are
500Sstevel@tonic-gate  *   associated with that zone.  The global zone is generally
510Sstevel@tonic-gate  *   unconstrained in terms of privileges and access, though the usual
520Sstevel@tonic-gate  *   credential and privilege based restrictions apply.
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  *   Zone States:
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  *   The states in which a zone may be in and the transitions are as
580Sstevel@tonic-gate  *   follows:
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  *   ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially
610Sstevel@tonic-gate  *   initialized zone is added to the list of active zones on the system but
620Sstevel@tonic-gate  *   isn't accessible.
630Sstevel@tonic-gate  *
645880Snordmark  *   ZONE_IS_INITIALIZED: Initialization complete except the ZSD callbacks are
655880Snordmark  *   not yet completed. Not possible to enter the zone, but attributes can
665880Snordmark  *   be retrieved.
675880Snordmark  *
680Sstevel@tonic-gate  *   ZONE_IS_READY: zsched (the kernel dummy process for a zone) is
690Sstevel@tonic-gate  *   ready.  The zone is made visible after the ZSD constructor callbacks are
700Sstevel@tonic-gate  *   executed.  A zone remains in this state until it transitions into
710Sstevel@tonic-gate  *   the ZONE_IS_BOOTING state as a result of a call to zone_boot().
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  *   ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start
740Sstevel@tonic-gate  *   init.  Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN
750Sstevel@tonic-gate  *   state.
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  *   ZONE_IS_RUNNING: The zone is open for business: zsched has
780Sstevel@tonic-gate  *   successfully started init.   A zone remains in this state until
790Sstevel@tonic-gate  *   zone_shutdown() is called.
800Sstevel@tonic-gate  *
810Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is
820Sstevel@tonic-gate  *   killing all processes running in the zone. The zone remains
830Sstevel@tonic-gate  *   in this state until there are no more user processes running in the zone.
840Sstevel@tonic-gate  *   zone_create(), zone_enter(), and zone_destroy() on this zone will fail.
850Sstevel@tonic-gate  *   Since zone_shutdown() is restartable, it may be called successfully
860Sstevel@tonic-gate  *   multiple times for the same zone_t.  Setting of the zone's state to
870Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check
880Sstevel@tonic-gate  *   the zone's status without worrying about it being a moving target.
890Sstevel@tonic-gate  *
900Sstevel@tonic-gate  *   ZONE_IS_EMPTY: zone_shutdown() has been called, and there
910Sstevel@tonic-gate  *   are no more user processes in the zone.  The zone remains in this
920Sstevel@tonic-gate  *   state until there are no more kernel threads associated with the
930Sstevel@tonic-gate  *   zone.  zone_create(), zone_enter(), and zone_destroy() on this zone will
940Sstevel@tonic-gate  *   fail.
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  *   ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone
970Sstevel@tonic-gate  *   have exited.  zone_shutdown() returns.  Henceforth it is not possible to
980Sstevel@tonic-gate  *   join the zone or create kernel threads therein.
990Sstevel@tonic-gate  *
1000Sstevel@tonic-gate  *   ZONE_IS_DYING: zone_destroy() has been called on the zone; zone
1010Sstevel@tonic-gate  *   remains in this state until zsched exits.  Calls to zone_find_by_*()
1020Sstevel@tonic-gate  *   return NULL from now on.
1030Sstevel@tonic-gate  *
1040Sstevel@tonic-gate  *   ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0).  There are no
1050Sstevel@tonic-gate  *   processes or threads doing work on behalf of the zone.  The zone is
1060Sstevel@tonic-gate  *   removed from the list of active zones.  zone_destroy() returns, and
1070Sstevel@tonic-gate  *   the zone can be recreated.
1080Sstevel@tonic-gate  *
1090Sstevel@tonic-gate  *   ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor
1100Sstevel@tonic-gate  *   callbacks are executed, and all memory associated with the zone is
1110Sstevel@tonic-gate  *   freed.
1120Sstevel@tonic-gate  *
1130Sstevel@tonic-gate  *   Threads can wait for the zone to enter a requested state by using
1140Sstevel@tonic-gate  *   zone_status_wait() or zone_status_timedwait() with the desired
1150Sstevel@tonic-gate  *   state passed in as an argument.  Zone state transitions are
1160Sstevel@tonic-gate  *   uni-directional; it is not possible to move back to an earlier state.
1170Sstevel@tonic-gate  *
1180Sstevel@tonic-gate  *
1190Sstevel@tonic-gate  *   Zone-Specific Data:
1200Sstevel@tonic-gate  *
1210Sstevel@tonic-gate  *   Subsystems needing to maintain zone-specific data can store that
1220Sstevel@tonic-gate  *   data using the ZSD mechanism.  This provides a zone-specific data
1230Sstevel@tonic-gate  *   store, similar to thread-specific data (see pthread_getspecific(3C)
1240Sstevel@tonic-gate  *   or the TSD code in uts/common/disp/thread.c.  Also, ZSD can be used
1250Sstevel@tonic-gate  *   to register callbacks to be invoked when a zone is created, shut
1260Sstevel@tonic-gate  *   down, or destroyed.  This can be used to initialize zone-specific
1270Sstevel@tonic-gate  *   data for new zones and to clean up when zones go away.
1280Sstevel@tonic-gate  *
1290Sstevel@tonic-gate  *
1300Sstevel@tonic-gate  *   Data Structures:
1310Sstevel@tonic-gate  *
1320Sstevel@tonic-gate  *   The per-zone structure (zone_t) is reference counted, and freed
1330Sstevel@tonic-gate  *   when all references are released.  zone_hold and zone_rele can be
1340Sstevel@tonic-gate  *   used to adjust the reference count.  In addition, reference counts
1350Sstevel@tonic-gate  *   associated with the cred_t structure are tracked separately using
1360Sstevel@tonic-gate  *   zone_cred_hold and zone_cred_rele.
1370Sstevel@tonic-gate  *
1380Sstevel@tonic-gate  *   Pointers to active zone_t's are stored in two hash tables; one
1390Sstevel@tonic-gate  *   for searching by id, the other for searching by name.  Lookups
1400Sstevel@tonic-gate  *   can be performed on either basis, using zone_find_by_id and
1410Sstevel@tonic-gate  *   zone_find_by_name.  Both return zone_t pointers with the zone
1420Sstevel@tonic-gate  *   held, so zone_rele should be called when the pointer is no longer
1430Sstevel@tonic-gate  *   needed.  Zones can also be searched by path; zone_find_by_path
1440Sstevel@tonic-gate  *   returns the zone with which a path name is associated (global
1450Sstevel@tonic-gate  *   zone if the path is not within some other zone's file system
1460Sstevel@tonic-gate  *   hierarchy).  This currently requires iterating through each zone,
1470Sstevel@tonic-gate  *   so it is slower than an id or name search via a hash table.
1480Sstevel@tonic-gate  *
1490Sstevel@tonic-gate  *
1500Sstevel@tonic-gate  *   Locking:
1510Sstevel@tonic-gate  *
1520Sstevel@tonic-gate  *   zonehash_lock: This is a top-level global lock used to protect the
1530Sstevel@tonic-gate  *       zone hash tables and lists.  Zones cannot be created or destroyed
1540Sstevel@tonic-gate  *       while this lock is held.
1550Sstevel@tonic-gate  *   zone_status_lock: This is a global lock protecting zone state.
1560Sstevel@tonic-gate  *       Zones cannot change state while this lock is held.  It also
1570Sstevel@tonic-gate  *       protects the list of kernel threads associated with a zone.
1580Sstevel@tonic-gate  *   zone_lock: This is a per-zone lock used to protect several fields of
1590Sstevel@tonic-gate  *       the zone_t (see <sys/zone.h> for details).  In addition, holding
1600Sstevel@tonic-gate  *       this lock means that the zone cannot go away.
1613247Sgjelinek  *   zone_nlwps_lock: This is a per-zone lock used to protect the fields
1623247Sgjelinek  *	 related to the zone.max-lwps rctl.
1633247Sgjelinek  *   zone_mem_lock: This is a per-zone lock used to protect the fields
1643247Sgjelinek  *	 related to the zone.max-locked-memory and zone.max-swap rctls.
1650Sstevel@tonic-gate  *   zsd_key_lock: This is a global lock protecting the key state for ZSD.
1660Sstevel@tonic-gate  *   zone_deathrow_lock: This is a global lock protecting the "deathrow"
1670Sstevel@tonic-gate  *       list (a list of zones in the ZONE_IS_DEAD state).
1680Sstevel@tonic-gate  *
1690Sstevel@tonic-gate  *   Ordering requirements:
1700Sstevel@tonic-gate  *       pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock -->
1710Sstevel@tonic-gate  *       	zone_lock --> zsd_key_lock --> pidlock --> p_lock
1720Sstevel@tonic-gate  *
1733247Sgjelinek  *   When taking zone_mem_lock or zone_nlwps_lock, the lock ordering is:
1743247Sgjelinek  *	zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock
1753247Sgjelinek  *	zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock
1763247Sgjelinek  *
1770Sstevel@tonic-gate  *   Blocking memory allocations are permitted while holding any of the
1780Sstevel@tonic-gate  *   zone locks.
1790Sstevel@tonic-gate  *
1800Sstevel@tonic-gate  *
1810Sstevel@tonic-gate  *   System Call Interface:
1820Sstevel@tonic-gate  *
1830Sstevel@tonic-gate  *   The zone subsystem can be managed and queried from user level with
1840Sstevel@tonic-gate  *   the following system calls (all subcodes of the primary "zone"
1850Sstevel@tonic-gate  *   system call):
1860Sstevel@tonic-gate  *   - zone_create: creates a zone with selected attributes (name,
187789Sahrens  *     root path, privileges, resource controls, ZFS datasets)
1880Sstevel@tonic-gate  *   - zone_enter: allows the current process to enter a zone
1890Sstevel@tonic-gate  *   - zone_getattr: reports attributes of a zone
1902267Sdp  *   - zone_setattr: set attributes of a zone
1912267Sdp  *   - zone_boot: set 'init' running for the zone
1920Sstevel@tonic-gate  *   - zone_list: lists all zones active in the system
1930Sstevel@tonic-gate  *   - zone_lookup: looks up zone id based on name
1940Sstevel@tonic-gate  *   - zone_shutdown: initiates shutdown process (see states above)
1950Sstevel@tonic-gate  *   - zone_destroy: completes shutdown process (see states above)
1960Sstevel@tonic-gate  *
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate #include <sys/priv_impl.h>
2000Sstevel@tonic-gate #include <sys/cred.h>
2010Sstevel@tonic-gate #include <c2/audit.h>
2020Sstevel@tonic-gate #include <sys/debug.h>
2030Sstevel@tonic-gate #include <sys/file.h>
2040Sstevel@tonic-gate #include <sys/kmem.h>
2053247Sgjelinek #include <sys/kstat.h>
2060Sstevel@tonic-gate #include <sys/mutex.h>
2071676Sjpk #include <sys/note.h>
2080Sstevel@tonic-gate #include <sys/pathname.h>
2090Sstevel@tonic-gate #include <sys/proc.h>
2100Sstevel@tonic-gate #include <sys/project.h>
2111166Sdstaff #include <sys/sysevent.h>
2120Sstevel@tonic-gate #include <sys/task.h>
2130Sstevel@tonic-gate #include <sys/systm.h>
2140Sstevel@tonic-gate #include <sys/types.h>
2150Sstevel@tonic-gate #include <sys/utsname.h>
2160Sstevel@tonic-gate #include <sys/vnode.h>
2170Sstevel@tonic-gate #include <sys/vfs.h>
2180Sstevel@tonic-gate #include <sys/systeminfo.h>
2190Sstevel@tonic-gate #include <sys/policy.h>
2200Sstevel@tonic-gate #include <sys/cred_impl.h>
2210Sstevel@tonic-gate #include <sys/contract_impl.h>
2220Sstevel@tonic-gate #include <sys/contract/process_impl.h>
2230Sstevel@tonic-gate #include <sys/class.h>
2240Sstevel@tonic-gate #include <sys/pool.h>
2250Sstevel@tonic-gate #include <sys/pool_pset.h>
2260Sstevel@tonic-gate #include <sys/pset.h>
2270Sstevel@tonic-gate #include <sys/sysmacros.h>
2280Sstevel@tonic-gate #include <sys/callb.h>
2290Sstevel@tonic-gate #include <sys/vmparam.h>
2300Sstevel@tonic-gate #include <sys/corectl.h>
2312677Sml93401 #include <sys/ipc_impl.h>
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate #include <sys/door.h>
2340Sstevel@tonic-gate #include <sys/cpuvar.h>
2355880Snordmark #include <sys/sdt.h>
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate #include <sys/uadmin.h>
2380Sstevel@tonic-gate #include <sys/session.h>
2390Sstevel@tonic-gate #include <sys/cmn_err.h>
2400Sstevel@tonic-gate #include <sys/modhash.h>
2412267Sdp #include <sys/sunddi.h>
2420Sstevel@tonic-gate #include <sys/nvpair.h>
2430Sstevel@tonic-gate #include <sys/rctl.h>
2440Sstevel@tonic-gate #include <sys/fss.h>
2452712Snn35248 #include <sys/brand.h>
2460Sstevel@tonic-gate #include <sys/zone.h>
2473448Sdh155122 #include <net/if.h>
2483792Sakolb #include <sys/cpucaps.h>
2493247Sgjelinek #include <vm/seg.h>
2503247Sgjelinek 
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate  * cv used to signal that all references to the zone have been released.  This
2530Sstevel@tonic-gate  * needs to be global since there may be multiple waiters, and the first to
2540Sstevel@tonic-gate  * wake up will free the zone_t, hence we cannot use zone->zone_cv.
2550Sstevel@tonic-gate  */
2560Sstevel@tonic-gate static kcondvar_t zone_destroy_cv;
2570Sstevel@tonic-gate /*
2580Sstevel@tonic-gate  * Lock used to serialize access to zone_cv.  This could have been per-zone,
2590Sstevel@tonic-gate  * but then we'd need another lock for zone_destroy_cv, and why bother?
2600Sstevel@tonic-gate  */
2610Sstevel@tonic-gate static kmutex_t zone_status_lock;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate /*
2640Sstevel@tonic-gate  * ZSD-related global variables.
2650Sstevel@tonic-gate  */
2660Sstevel@tonic-gate static kmutex_t zsd_key_lock;	/* protects the following two */
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate  * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval.
2690Sstevel@tonic-gate  */
2700Sstevel@tonic-gate static zone_key_t zsd_keyval = 0;
2710Sstevel@tonic-gate /*
2720Sstevel@tonic-gate  * Global list of registered keys.  We use this when a new zone is created.
2730Sstevel@tonic-gate  */
2740Sstevel@tonic-gate static list_t zsd_registered_keys;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate int zone_hash_size = 256;
2771676Sjpk static mod_hash_t *zonehashbyname, *zonehashbyid, *zonehashbylabel;
2780Sstevel@tonic-gate static kmutex_t zonehash_lock;
2790Sstevel@tonic-gate static uint_t zonecount;
2800Sstevel@tonic-gate static id_space_t *zoneid_space;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate /*
2830Sstevel@tonic-gate  * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the
2840Sstevel@tonic-gate  * kernel proper runs, and which manages all other zones.
2850Sstevel@tonic-gate  *
2860Sstevel@tonic-gate  * Although not declared as static, the variable "zone0" should not be used
2870Sstevel@tonic-gate  * except for by code that needs to reference the global zone early on in boot,
2880Sstevel@tonic-gate  * before it is fully initialized.  All other consumers should use
2890Sstevel@tonic-gate  * 'global_zone'.
2900Sstevel@tonic-gate  */
2910Sstevel@tonic-gate zone_t zone0;
2920Sstevel@tonic-gate zone_t *global_zone = NULL;	/* Set when the global zone is initialized */
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate  * List of active zones, protected by zonehash_lock.
2960Sstevel@tonic-gate  */
2970Sstevel@tonic-gate static list_t zone_active;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate /*
3000Sstevel@tonic-gate  * List of destroyed zones that still have outstanding cred references.
3010Sstevel@tonic-gate  * Used for debugging.  Uses a separate lock to avoid lock ordering
3020Sstevel@tonic-gate  * problems in zone_free.
3030Sstevel@tonic-gate  */
3040Sstevel@tonic-gate static list_t zone_deathrow;
3050Sstevel@tonic-gate static kmutex_t zone_deathrow_lock;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */
3080Sstevel@tonic-gate uint_t maxzones = 8192;
3090Sstevel@tonic-gate 
3101166Sdstaff /* Event channel to sent zone state change notifications */
3111166Sdstaff evchan_t *zone_event_chan;
3121166Sdstaff 
3131166Sdstaff /*
3141166Sdstaff  * This table holds the mapping from kernel zone states to
3151166Sdstaff  * states visible in the state notification API.
3161166Sdstaff  * The idea is that we only expose "obvious" states and
3171166Sdstaff  * do not expose states which are just implementation details.
3181166Sdstaff  */
3191166Sdstaff const char  *zone_status_table[] = {
3201166Sdstaff 	ZONE_EVENT_UNINITIALIZED,	/* uninitialized */
3215880Snordmark 	ZONE_EVENT_INITIALIZED,		/* initialized */
3221166Sdstaff 	ZONE_EVENT_READY,		/* ready */
3231166Sdstaff 	ZONE_EVENT_READY,		/* booting */
3241166Sdstaff 	ZONE_EVENT_RUNNING,		/* running */
3251166Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* shutting_down */
3261166Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* empty */
3271166Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* down */
3281166Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* dying */
3291166Sdstaff 	ZONE_EVENT_UNINITIALIZED,	/* dead */
3301166Sdstaff };
3311166Sdstaff 
3320Sstevel@tonic-gate /*
3330Sstevel@tonic-gate  * This isn't static so lint doesn't complain.
3340Sstevel@tonic-gate  */
3350Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares;
3362768Ssl108498 rctl_hndl_t rc_zone_locked_mem;
3373247Sgjelinek rctl_hndl_t rc_zone_max_swap;
3383792Sakolb rctl_hndl_t rc_zone_cpu_cap;
3390Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps;
3402677Sml93401 rctl_hndl_t rc_zone_shmmax;
3412677Sml93401 rctl_hndl_t rc_zone_shmmni;
3422677Sml93401 rctl_hndl_t rc_zone_semmni;
3432677Sml93401 rctl_hndl_t rc_zone_msgmni;
3440Sstevel@tonic-gate /*
3450Sstevel@tonic-gate  * Synchronization primitives used to synchronize between mounts and zone
3460Sstevel@tonic-gate  * creation/destruction.
3470Sstevel@tonic-gate  */
3480Sstevel@tonic-gate static int mounts_in_progress;
3490Sstevel@tonic-gate static kcondvar_t mount_cv;
3500Sstevel@tonic-gate static kmutex_t mount_lock;
3510Sstevel@tonic-gate 
3522267Sdp const char * const zone_default_initname = "/sbin/init";
3531676Sjpk static char * const zone_prefix = "/zone/";
3540Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid);
3553448Sdh155122 static int zone_add_datalink(zoneid_t, char *);
3563448Sdh155122 static int zone_remove_datalink(zoneid_t, char *);
3573448Sdh155122 static int zone_check_datalink(zoneid_t *, char *);
3583448Sdh155122 static int zone_list_datalink(zoneid_t, int *, char *);
3590Sstevel@tonic-gate 
3605880Snordmark typedef boolean_t zsd_applyfn_t(kmutex_t *, boolean_t, zone_t *, zone_key_t);
3615880Snordmark 
3625880Snordmark static void zsd_apply_all_zones(zsd_applyfn_t *, zone_key_t);
3635880Snordmark static void zsd_apply_all_keys(zsd_applyfn_t *, zone_t *);
3645880Snordmark static boolean_t zsd_apply_create(kmutex_t *, boolean_t, zone_t *, zone_key_t);
3655880Snordmark static boolean_t zsd_apply_shutdown(kmutex_t *, boolean_t, zone_t *,
3665880Snordmark     zone_key_t);
3675880Snordmark static boolean_t zsd_apply_destroy(kmutex_t *, boolean_t, zone_t *, zone_key_t);
3685880Snordmark static boolean_t zsd_wait_for_creator(zone_t *, struct zsd_entry *,
3695880Snordmark     kmutex_t *);
3705880Snordmark static boolean_t zsd_wait_for_inprogress(zone_t *, struct zsd_entry *,
3715880Snordmark     kmutex_t *);
3725880Snordmark 
3730Sstevel@tonic-gate /*
374813Sdp  * Bump this number when you alter the zone syscall interfaces; this is
375813Sdp  * because we need to have support for previous API versions in libc
376813Sdp  * to support patching; libc calls into the kernel to determine this number.
377813Sdp  *
378813Sdp  * Version 1 of the API is the version originally shipped with Solaris 10
379813Sdp  * Version 2 alters the zone_create system call in order to support more
380813Sdp  *     arguments by moving the args into a structure; and to do better
381813Sdp  *     error reporting when zone_create() fails.
382813Sdp  * Version 3 alters the zone_create system call in order to support the
383813Sdp  *     import of ZFS datasets to zones.
3841676Sjpk  * Version 4 alters the zone_create system call in order to support
3851676Sjpk  *     Trusted Extensions.
3862267Sdp  * Version 5 alters the zone_boot system call, and converts its old
3872267Sdp  *     bootargs parameter to be set by the zone_setattr API instead.
3883448Sdh155122  * Version 6 adds the flag argument to zone_create.
389813Sdp  */
3903448Sdh155122 static const int ZONE_SYSCALL_API_VERSION = 6;
391813Sdp 
392813Sdp /*
3930Sstevel@tonic-gate  * Certain filesystems (such as NFS and autofs) need to know which zone
3940Sstevel@tonic-gate  * the mount is being placed in.  Because of this, we need to be able to
3950Sstevel@tonic-gate  * ensure that a zone isn't in the process of being created such that
3960Sstevel@tonic-gate  * nfs_mount() thinks it is in the global zone, while by the time it
3970Sstevel@tonic-gate  * gets added the list of mounted zones, it ends up on zoneA's mount
3980Sstevel@tonic-gate  * list.
3990Sstevel@tonic-gate  *
4000Sstevel@tonic-gate  * The following functions: block_mounts()/resume_mounts() and
4010Sstevel@tonic-gate  * mount_in_progress()/mount_completed() are used by zones and the VFS
4020Sstevel@tonic-gate  * layer (respectively) to synchronize zone creation and new mounts.
4030Sstevel@tonic-gate  *
4040Sstevel@tonic-gate  * The semantics are like a reader-reader lock such that there may
4050Sstevel@tonic-gate  * either be multiple mounts (or zone creations, if that weren't
4060Sstevel@tonic-gate  * serialized by zonehash_lock) in progress at the same time, but not
4070Sstevel@tonic-gate  * both.
4080Sstevel@tonic-gate  *
4090Sstevel@tonic-gate  * We use cv's so the user can ctrl-C out of the operation if it's
4100Sstevel@tonic-gate  * taking too long.
4110Sstevel@tonic-gate  *
4120Sstevel@tonic-gate  * The semantics are such that there is unfair bias towards the
4130Sstevel@tonic-gate  * "current" operation.  This means that zone creations may starve if
4140Sstevel@tonic-gate  * there is a rapid succession of new mounts coming in to the system, or
4150Sstevel@tonic-gate  * there is a remote possibility that zones will be created at such a
4160Sstevel@tonic-gate  * rate that new mounts will not be able to proceed.
4170Sstevel@tonic-gate  */
4180Sstevel@tonic-gate /*
4190Sstevel@tonic-gate  * Prevent new mounts from progressing to the point of calling
4200Sstevel@tonic-gate  * VFS_MOUNT().  If there are already mounts in this "region", wait for
4210Sstevel@tonic-gate  * them to complete.
4220Sstevel@tonic-gate  */
4230Sstevel@tonic-gate static int
4240Sstevel@tonic-gate block_mounts(void)
4250Sstevel@tonic-gate {
4260Sstevel@tonic-gate 	int retval = 0;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	/*
4290Sstevel@tonic-gate 	 * Since it may block for a long time, block_mounts() shouldn't be
4300Sstevel@tonic-gate 	 * called with zonehash_lock held.
4310Sstevel@tonic-gate 	 */
4320Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
4330Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4340Sstevel@tonic-gate 	while (mounts_in_progress > 0) {
4350Sstevel@tonic-gate 		if (cv_wait_sig(&mount_cv, &mount_lock) == 0)
4360Sstevel@tonic-gate 			goto signaled;
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 	/*
4390Sstevel@tonic-gate 	 * A negative value of mounts_in_progress indicates that mounts
4400Sstevel@tonic-gate 	 * have been blocked by (-mounts_in_progress) different callers.
4410Sstevel@tonic-gate 	 */
4420Sstevel@tonic-gate 	mounts_in_progress--;
4430Sstevel@tonic-gate 	retval = 1;
4440Sstevel@tonic-gate signaled:
4450Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4460Sstevel@tonic-gate 	return (retval);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate /*
4500Sstevel@tonic-gate  * The VFS layer may progress with new mounts as far as we're concerned.
4510Sstevel@tonic-gate  * Allow them to progress if we were the last obstacle.
4520Sstevel@tonic-gate  */
4530Sstevel@tonic-gate static void
4540Sstevel@tonic-gate resume_mounts(void)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4570Sstevel@tonic-gate 	if (++mounts_in_progress == 0)
4580Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
4590Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate /*
4630Sstevel@tonic-gate  * The VFS layer is busy with a mount; zones should wait until all
4640Sstevel@tonic-gate  * mounts are completed to progress.
4650Sstevel@tonic-gate  */
4660Sstevel@tonic-gate void
4670Sstevel@tonic-gate mount_in_progress(void)
4680Sstevel@tonic-gate {
4690Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4700Sstevel@tonic-gate 	while (mounts_in_progress < 0)
4710Sstevel@tonic-gate 		cv_wait(&mount_cv, &mount_lock);
4720Sstevel@tonic-gate 	mounts_in_progress++;
4730Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate /*
4770Sstevel@tonic-gate  * VFS is done with one mount; wake up any waiting block_mounts()
4780Sstevel@tonic-gate  * callers if this is the last mount.
4790Sstevel@tonic-gate  */
4800Sstevel@tonic-gate void
4810Sstevel@tonic-gate mount_completed(void)
4820Sstevel@tonic-gate {
4830Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4840Sstevel@tonic-gate 	if (--mounts_in_progress == 0)
4850Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
4860Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate /*
4900Sstevel@tonic-gate  * ZSD routines.
4910Sstevel@tonic-gate  *
4920Sstevel@tonic-gate  * Zone Specific Data (ZSD) is modeled after Thread Specific Data as
4930Sstevel@tonic-gate  * defined by the pthread_key_create() and related interfaces.
4940Sstevel@tonic-gate  *
4950Sstevel@tonic-gate  * Kernel subsystems may register one or more data items and/or
4960Sstevel@tonic-gate  * callbacks to be executed when a zone is created, shutdown, or
4970Sstevel@tonic-gate  * destroyed.
4980Sstevel@tonic-gate  *
4990Sstevel@tonic-gate  * Unlike the thread counterpart, destructor callbacks will be executed
5000Sstevel@tonic-gate  * even if the data pointer is NULL and/or there are no constructor
5010Sstevel@tonic-gate  * callbacks, so it is the responsibility of such callbacks to check for
5020Sstevel@tonic-gate  * NULL data values if necessary.
5030Sstevel@tonic-gate  *
5040Sstevel@tonic-gate  * The locking strategy and overall picture is as follows:
5050Sstevel@tonic-gate  *
5060Sstevel@tonic-gate  * When someone calls zone_key_create(), a template ZSD entry is added to the
5075880Snordmark  * global list "zsd_registered_keys", protected by zsd_key_lock.  While
5085880Snordmark  * holding that lock all the existing zones are marked as
5095880Snordmark  * ZSD_CREATE_NEEDED and a copy of the ZSD entry added to the per-zone
5105880Snordmark  * zone_zsd list (protected by zone_lock). The global list is updated first
5115880Snordmark  * (under zone_key_lock) to make sure that newly created zones use the
5125880Snordmark  * most recent list of keys. Then under zonehash_lock we walk the zones
5135880Snordmark  * and mark them.  Similar locking is used in zone_key_delete().
5140Sstevel@tonic-gate  *
5155880Snordmark  * The actual create, shutdown, and destroy callbacks are done without
5165880Snordmark  * holding any lock. And zsd_flags are used to ensure that the operations
5175880Snordmark  * completed so that when zone_key_create (and zone_create) is done, as well as
5185880Snordmark  * zone_key_delete (and zone_destroy) is done, all the necessary callbacks
5195880Snordmark  * are completed.
5200Sstevel@tonic-gate  *
5210Sstevel@tonic-gate  * When new zones are created constructor callbacks for all registered ZSD
5225880Snordmark  * entries will be called. That also uses the above two phases of marking
5235880Snordmark  * what needs to be done, and then running the callbacks without holding
5245880Snordmark  * any locks.
5250Sstevel@tonic-gate  *
5260Sstevel@tonic-gate  * The framework does not provide any locking around zone_getspecific() and
5270Sstevel@tonic-gate  * zone_setspecific() apart from that needed for internal consistency, so
5280Sstevel@tonic-gate  * callers interested in atomic "test-and-set" semantics will need to provide
5290Sstevel@tonic-gate  * their own locking.
5300Sstevel@tonic-gate  */
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate /*
5330Sstevel@tonic-gate  * Helper function to find the zsd_entry associated with the key in the
5340Sstevel@tonic-gate  * given list.
5350Sstevel@tonic-gate  */
5360Sstevel@tonic-gate static struct zsd_entry *
5370Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key)
5380Sstevel@tonic-gate {
5390Sstevel@tonic-gate 	struct zsd_entry *zsd;
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) {
5420Sstevel@tonic-gate 		if (zsd->zsd_key == key) {
5435880Snordmark 			return (zsd);
5445880Snordmark 		}
5455880Snordmark 	}
5465880Snordmark 	return (NULL);
5475880Snordmark }
5485880Snordmark 
5495880Snordmark /*
5505880Snordmark  * Helper function to find the zsd_entry associated with the key in the
5515880Snordmark  * given list. Move it to the front of the list.
5525880Snordmark  */
5535880Snordmark static struct zsd_entry *
5545880Snordmark zsd_find_mru(list_t *l, zone_key_t key)
5555880Snordmark {
5565880Snordmark 	struct zsd_entry *zsd;
5575880Snordmark 
5585880Snordmark 	for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) {
5595880Snordmark 		if (zsd->zsd_key == key) {
5600Sstevel@tonic-gate 			/*
5610Sstevel@tonic-gate 			 * Move to head of list to keep list in MRU order.
5620Sstevel@tonic-gate 			 */
5630Sstevel@tonic-gate 			if (zsd != list_head(l)) {
5640Sstevel@tonic-gate 				list_remove(l, zsd);
5650Sstevel@tonic-gate 				list_insert_head(l, zsd);
5660Sstevel@tonic-gate 			}
5670Sstevel@tonic-gate 			return (zsd);
5680Sstevel@tonic-gate 		}
5690Sstevel@tonic-gate 	}
5700Sstevel@tonic-gate 	return (NULL);
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate 
5735880Snordmark void
5745880Snordmark zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t),
5755880Snordmark     void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *))
5765880Snordmark {
5775880Snordmark 	struct zsd_entry *zsdp;
5785880Snordmark 	struct zsd_entry *t;
5795880Snordmark 	struct zone *zone;
5805880Snordmark 	zone_key_t  key;
5815880Snordmark 
5825880Snordmark 	zsdp = kmem_zalloc(sizeof (*zsdp), KM_SLEEP);
5835880Snordmark 	zsdp->zsd_data = NULL;
5845880Snordmark 	zsdp->zsd_create = create;
5855880Snordmark 	zsdp->zsd_shutdown = shutdown;
5865880Snordmark 	zsdp->zsd_destroy = destroy;
5875880Snordmark 
5885880Snordmark 	/*
5895880Snordmark 	 * Insert in global list of callbacks. Makes future zone creations
5905880Snordmark 	 * see it.
5915880Snordmark 	 */
5925880Snordmark 	mutex_enter(&zsd_key_lock);
5935880Snordmark 	*keyp = key = zsdp->zsd_key = ++zsd_keyval;
5945880Snordmark 	ASSERT(zsd_keyval != 0);
5955880Snordmark 	list_insert_tail(&zsd_registered_keys, zsdp);
5965880Snordmark 	mutex_exit(&zsd_key_lock);
5975880Snordmark 
5985880Snordmark 	/*
5995880Snordmark 	 * Insert for all existing zones and mark them as needing
6005880Snordmark 	 * a create callback.
6015880Snordmark 	 */
6025880Snordmark 	mutex_enter(&zonehash_lock);	/* stop the world */
6035880Snordmark 	for (zone = list_head(&zone_active); zone != NULL;
6045880Snordmark 	    zone = list_next(&zone_active, zone)) {
6055880Snordmark 		zone_status_t status;
6065880Snordmark 
6075880Snordmark 		mutex_enter(&zone->zone_lock);
6085880Snordmark 
6095880Snordmark 		/* Skip zones that are on the way down or not yet up */
6105880Snordmark 		status = zone_status_get(zone);
6115880Snordmark 		if (status >= ZONE_IS_DOWN ||
6125880Snordmark 		    status == ZONE_IS_UNINITIALIZED) {
6135880Snordmark 			mutex_exit(&zone->zone_lock);
6145880Snordmark 			continue;
6155880Snordmark 		}
6165880Snordmark 
6175880Snordmark 		t = zsd_find_mru(&zone->zone_zsd, key);
6185880Snordmark 		if (t != NULL) {
6195880Snordmark 			/*
6205880Snordmark 			 * A zsd_configure already inserted it after
6215880Snordmark 			 * we dropped zsd_key_lock above.
6225880Snordmark 			 */
6235880Snordmark 			mutex_exit(&zone->zone_lock);
6245880Snordmark 			continue;
6255880Snordmark 		}
6265880Snordmark 		t = kmem_zalloc(sizeof (*t), KM_SLEEP);
6275880Snordmark 		t->zsd_key = key;
6285880Snordmark 		t->zsd_create = create;
6295880Snordmark 		t->zsd_shutdown = shutdown;
6305880Snordmark 		t->zsd_destroy = destroy;
6315880Snordmark 		if (create != NULL) {
6325880Snordmark 			t->zsd_flags = ZSD_CREATE_NEEDED;
6335880Snordmark 			DTRACE_PROBE2(zsd__create__needed,
6345880Snordmark 			    zone_t *, zone, zone_key_t, key);
6355880Snordmark 		}
6365880Snordmark 		list_insert_tail(&zone->zone_zsd, t);
6375880Snordmark 		mutex_exit(&zone->zone_lock);
6385880Snordmark 	}
6395880Snordmark 	mutex_exit(&zonehash_lock);
6405880Snordmark 
6415880Snordmark 	if (create != NULL) {
6425880Snordmark 		/* Now call the create callback for this key */
6435880Snordmark 		zsd_apply_all_zones(zsd_apply_create, key);
6445880Snordmark 	}
6455880Snordmark }
6465880Snordmark 
6470Sstevel@tonic-gate /*
6480Sstevel@tonic-gate  * Function called when a module is being unloaded, or otherwise wishes
6490Sstevel@tonic-gate  * to unregister its ZSD key and callbacks.
6505880Snordmark  *
6515880Snordmark  * Remove from the global list and determine the functions that need to
6525880Snordmark  * be called under a global lock. Then call the functions without
6535880Snordmark  * holding any locks. Finally free up the zone_zsd entries. (The apply
6545880Snordmark  * functions need to access the zone_zsd entries to find zsd_data etc.)
6550Sstevel@tonic-gate  */
6560Sstevel@tonic-gate int
6570Sstevel@tonic-gate zone_key_delete(zone_key_t key)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate 	struct zsd_entry *zsdp = NULL;
6600Sstevel@tonic-gate 	zone_t *zone;
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
6635880Snordmark 	zsdp = zsd_find_mru(&zsd_registered_keys, key);
6645880Snordmark 	if (zsdp == NULL) {
6655880Snordmark 		mutex_exit(&zsd_key_lock);
6665880Snordmark 		return (-1);
6675880Snordmark 	}
6680Sstevel@tonic-gate 	list_remove(&zsd_registered_keys, zsdp);
6690Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
6700Sstevel@tonic-gate 
6715880Snordmark 	mutex_enter(&zonehash_lock);
6720Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
6730Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
6740Sstevel@tonic-gate 		struct zsd_entry *del;
6755880Snordmark 
6765880Snordmark 		mutex_enter(&zone->zone_lock);
6775880Snordmark 		del = zsd_find_mru(&zone->zone_zsd, key);
6785880Snordmark 		if (del == NULL) {
6795880Snordmark 			/*
6805880Snordmark 			 * Somebody else got here first e.g the zone going
6815880Snordmark 			 * away.
6825880Snordmark 			 */
6835880Snordmark 			mutex_exit(&zone->zone_lock);
6845880Snordmark 			continue;
6855880Snordmark 		}
6865880Snordmark 		ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown);
6875880Snordmark 		ASSERT(del->zsd_destroy == zsdp->zsd_destroy);
6885880Snordmark 		if (del->zsd_shutdown != NULL &&
6895880Snordmark 		    (del->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) {
6905880Snordmark 			del->zsd_flags |= ZSD_SHUTDOWN_NEEDED;
6915880Snordmark 			DTRACE_PROBE2(zsd__shutdown__needed,
6925880Snordmark 			    zone_t *, zone, zone_key_t, key);
6935880Snordmark 		}
6945880Snordmark 		if (del->zsd_destroy != NULL &&
6955880Snordmark 		    (del->zsd_flags & ZSD_DESTROY_ALL) == 0) {
6965880Snordmark 			del->zsd_flags |= ZSD_DESTROY_NEEDED;
6975880Snordmark 			DTRACE_PROBE2(zsd__destroy__needed,
6985880Snordmark 			    zone_t *, zone, zone_key_t, key);
6990Sstevel@tonic-gate 		}
7000Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
7030Sstevel@tonic-gate 	kmem_free(zsdp, sizeof (*zsdp));
7045880Snordmark 
7055880Snordmark 	/* Now call the shutdown and destroy callback for this key */
7065880Snordmark 	zsd_apply_all_zones(zsd_apply_shutdown, key);
7075880Snordmark 	zsd_apply_all_zones(zsd_apply_destroy, key);
7085880Snordmark 
7095880Snordmark 	/* Now we can free up the zsdp structures in each zone */
7105880Snordmark 	mutex_enter(&zonehash_lock);
7110Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
7125880Snordmark 	    zone = list_next(&zone_active, zone)) {
7135880Snordmark 		struct zsd_entry *del;
7145880Snordmark 
7155880Snordmark 		mutex_enter(&zone->zone_lock);
7165880Snordmark 		del = zsd_find(&zone->zone_zsd, key);
7175880Snordmark 		if (del != NULL) {
7185880Snordmark 			list_remove(&zone->zone_zsd, del);
7195880Snordmark 			ASSERT(!(del->zsd_flags & ZSD_ALL_INPROGRESS));
7205880Snordmark 			kmem_free(del, sizeof (*del));
7215880Snordmark 		}
7220Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
7235880Snordmark 	}
7240Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
7255880Snordmark 
7265880Snordmark 	return (0);
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate /*
7300Sstevel@tonic-gate  * ZSD counterpart of pthread_setspecific().
7315880Snordmark  *
7325880Snordmark  * Since all zsd callbacks, including those with no create function,
7335880Snordmark  * have an entry in zone_zsd, if the key is registered it is part of
7345880Snordmark  * the zone_zsd list.
7355880Snordmark  * Return an error if the key wasn't registerd.
7360Sstevel@tonic-gate  */
7370Sstevel@tonic-gate int
7380Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data)
7390Sstevel@tonic-gate {
7400Sstevel@tonic-gate 	struct zsd_entry *t;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
7435880Snordmark 	t = zsd_find_mru(&zone->zone_zsd, key);
7440Sstevel@tonic-gate 	if (t != NULL) {
7450Sstevel@tonic-gate 		/*
7460Sstevel@tonic-gate 		 * Replace old value with new
7470Sstevel@tonic-gate 		 */
7480Sstevel@tonic-gate 		t->zsd_data = (void *)data;
7490Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
7500Sstevel@tonic-gate 		return (0);
7510Sstevel@tonic-gate 	}
7520Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
7535880Snordmark 	return (-1);
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate /*
7570Sstevel@tonic-gate  * ZSD counterpart of pthread_getspecific().
7580Sstevel@tonic-gate  */
7590Sstevel@tonic-gate void *
7600Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone)
7610Sstevel@tonic-gate {
7620Sstevel@tonic-gate 	struct zsd_entry *t;
7630Sstevel@tonic-gate 	void *data;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
7665880Snordmark 	t = zsd_find_mru(&zone->zone_zsd, key);
7670Sstevel@tonic-gate 	data = (t == NULL ? NULL : t->zsd_data);
7680Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
7690Sstevel@tonic-gate 	return (data);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate /*
7730Sstevel@tonic-gate  * Function used to initialize a zone's list of ZSD callbacks and data
7740Sstevel@tonic-gate  * when the zone is being created.  The callbacks are initialized from
7755880Snordmark  * the template list (zsd_registered_keys). The constructor callback is
7765880Snordmark  * executed later (once the zone exists and with locks dropped).
7770Sstevel@tonic-gate  */
7780Sstevel@tonic-gate static void
7790Sstevel@tonic-gate zone_zsd_configure(zone_t *zone)
7800Sstevel@tonic-gate {
7810Sstevel@tonic-gate 	struct zsd_entry *zsdp;
7820Sstevel@tonic-gate 	struct zsd_entry *t;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
7850Sstevel@tonic-gate 	ASSERT(list_head(&zone->zone_zsd) == NULL);
7865880Snordmark 	mutex_enter(&zone->zone_lock);
7870Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
7880Sstevel@tonic-gate 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
7890Sstevel@tonic-gate 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
7905880Snordmark 		/*
7915880Snordmark 		 * Since this zone is ZONE_IS_UNCONFIGURED, zone_key_create
7925880Snordmark 		 * should not have added anything to it.
7935880Snordmark 		 */
7945880Snordmark 		ASSERT(zsd_find(&zone->zone_zsd, zsdp->zsd_key) == NULL);
7955880Snordmark 
7965880Snordmark 		t = kmem_zalloc(sizeof (*t), KM_SLEEP);
7975880Snordmark 		t->zsd_key = zsdp->zsd_key;
7985880Snordmark 		t->zsd_create = zsdp->zsd_create;
7995880Snordmark 		t->zsd_shutdown = zsdp->zsd_shutdown;
8005880Snordmark 		t->zsd_destroy = zsdp->zsd_destroy;
8010Sstevel@tonic-gate 		if (zsdp->zsd_create != NULL) {
8025880Snordmark 			t->zsd_flags = ZSD_CREATE_NEEDED;
8035880Snordmark 			DTRACE_PROBE2(zsd__create__needed,
8045880Snordmark 			    zone_t *, zone, zone_key_t, zsdp->zsd_key);
8050Sstevel@tonic-gate 		}
8065880Snordmark 		list_insert_tail(&zone->zone_zsd, t);
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
8095880Snordmark 	mutex_exit(&zone->zone_lock);
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY };
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate /*
8150Sstevel@tonic-gate  * Helper function to execute shutdown or destructor callbacks.
8160Sstevel@tonic-gate  */
8170Sstevel@tonic-gate static void
8180Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct)
8190Sstevel@tonic-gate {
8200Sstevel@tonic-gate 	struct zsd_entry *t;
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY);
8230Sstevel@tonic-gate 	ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY);
8240Sstevel@tonic-gate 	ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN);
8250Sstevel@tonic-gate 
8265880Snordmark 	/*
8275880Snordmark 	 * Run the callback solely based on what is registered for the zone
8285880Snordmark 	 * in zone_zsd. The global list can change independently of this
8295880Snordmark 	 * as keys are registered and unregistered and we don't register new
8305880Snordmark 	 * callbacks for a zone that is in the process of going away.
8315880Snordmark 	 */
8320Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
8335880Snordmark 	for (t = list_head(&zone->zone_zsd); t != NULL;
8345880Snordmark 	    t = list_next(&zone->zone_zsd, t)) {
8355880Snordmark 		zone_key_t key = t->zsd_key;
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 		/* Skip if no callbacks registered */
8385880Snordmark 
8395880Snordmark 		if (ct == ZSD_SHUTDOWN) {
8405880Snordmark 			if (t->zsd_shutdown != NULL &&
8415880Snordmark 			    (t->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) {
8425880Snordmark 				t->zsd_flags |= ZSD_SHUTDOWN_NEEDED;
8435880Snordmark 				DTRACE_PROBE2(zsd__shutdown__needed,
8445880Snordmark 				    zone_t *, zone, zone_key_t, key);
8450Sstevel@tonic-gate 			}
8460Sstevel@tonic-gate 		} else {
8475880Snordmark 			if (t->zsd_destroy != NULL &&
8485880Snordmark 			    (t->zsd_flags & ZSD_DESTROY_ALL) == 0) {
8495880Snordmark 				t->zsd_flags |= ZSD_DESTROY_NEEDED;
8505880Snordmark 				DTRACE_PROBE2(zsd__destroy__needed,
8515880Snordmark 				    zone_t *, zone, zone_key_t, key);
8520Sstevel@tonic-gate 			}
8530Sstevel@tonic-gate 		}
8540Sstevel@tonic-gate 	}
8555880Snordmark 	mutex_exit(&zone->zone_lock);
8565880Snordmark 
8575880Snordmark 	/* Now call the shutdown and destroy callback for this key */
8585880Snordmark 	zsd_apply_all_keys(zsd_apply_shutdown, zone);
8595880Snordmark 	zsd_apply_all_keys(zsd_apply_destroy, zone);
8605880Snordmark 
8610Sstevel@tonic-gate }
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate /*
8640Sstevel@tonic-gate  * Called when the zone is going away; free ZSD-related memory, and
8650Sstevel@tonic-gate  * destroy the zone_zsd list.
8660Sstevel@tonic-gate  */
8670Sstevel@tonic-gate static void
8680Sstevel@tonic-gate zone_free_zsd(zone_t *zone)
8690Sstevel@tonic-gate {
8700Sstevel@tonic-gate 	struct zsd_entry *t, *next;
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	/*
8730Sstevel@tonic-gate 	 * Free all the zsd_entry's we had on this zone.
8740Sstevel@tonic-gate 	 */
8755880Snordmark 	mutex_enter(&zone->zone_lock);
8760Sstevel@tonic-gate 	for (t = list_head(&zone->zone_zsd); t != NULL; t = next) {
8770Sstevel@tonic-gate 		next = list_next(&zone->zone_zsd, t);
8780Sstevel@tonic-gate 		list_remove(&zone->zone_zsd, t);
8795880Snordmark 		ASSERT(!(t->zsd_flags & ZSD_ALL_INPROGRESS));
8800Sstevel@tonic-gate 		kmem_free(t, sizeof (*t));
8810Sstevel@tonic-gate 	}
8820Sstevel@tonic-gate 	list_destroy(&zone->zone_zsd);
8835880Snordmark 	mutex_exit(&zone->zone_lock);
8845880Snordmark 
8855880Snordmark }
8865880Snordmark 
8875880Snordmark /*
8885880Snordmark  * Apply a function to all zones for particular key value.
8895880Snordmark  *
8905880Snordmark  * The applyfn has to drop zonehash_lock if it does some work, and
8915880Snordmark  * then reacquire it before it returns.
8925880Snordmark  * When the lock is dropped we don't follow list_next even
8935880Snordmark  * if it is possible to do so without any hazards. This is
8945880Snordmark  * because we want the design to allow for the list of zones
8955880Snordmark  * to change in any arbitrary way during the time the
8965880Snordmark  * lock was dropped.
8975880Snordmark  *
8985880Snordmark  * It is safe to restart the loop at list_head since the applyfn
8995880Snordmark  * changes the zsd_flags as it does work, so a subsequent
9005880Snordmark  * pass through will have no effect in applyfn, hence the loop will terminate
9015880Snordmark  * in at worst O(N^2).
9025880Snordmark  */
9035880Snordmark static void
9045880Snordmark zsd_apply_all_zones(zsd_applyfn_t *applyfn, zone_key_t key)
9055880Snordmark {
9065880Snordmark 	zone_t *zone;
9075880Snordmark 
9085880Snordmark 	mutex_enter(&zonehash_lock);
9095880Snordmark 	zone = list_head(&zone_active);
9105880Snordmark 	while (zone != NULL) {
9115880Snordmark 		if ((applyfn)(&zonehash_lock, B_FALSE, zone, key)) {
9125880Snordmark 			/* Lock dropped - restart at head */
9135880Snordmark 			zone = list_head(&zone_active);
9145880Snordmark 		} else {
9155880Snordmark 			zone = list_next(&zone_active, zone);
9165880Snordmark 		}
9175880Snordmark 	}
9185880Snordmark 	mutex_exit(&zonehash_lock);
9195880Snordmark }
9205880Snordmark 
9215880Snordmark /*
9225880Snordmark  * Apply a function to all keys for a particular zone.
9235880Snordmark  *
9245880Snordmark  * The applyfn has to drop zonehash_lock if it does some work, and
9255880Snordmark  * then reacquire it before it returns.
9265880Snordmark  * When the lock is dropped we don't follow list_next even
9275880Snordmark  * if it is possible to do so without any hazards. This is
9285880Snordmark  * because we want the design to allow for the list of zsd callbacks
9295880Snordmark  * to change in any arbitrary way during the time the
9305880Snordmark  * lock was dropped.
9315880Snordmark  *
9325880Snordmark  * It is safe to restart the loop at list_head since the applyfn
9335880Snordmark  * changes the zsd_flags as it does work, so a subsequent
9345880Snordmark  * pass through will have no effect in applyfn, hence the loop will terminate
9355880Snordmark  * in at worst O(N^2).
9365880Snordmark  */
9375880Snordmark static void
9385880Snordmark zsd_apply_all_keys(zsd_applyfn_t *applyfn, zone_t *zone)
9395880Snordmark {
9405880Snordmark 	struct zsd_entry *t;
9415880Snordmark 
9425880Snordmark 	mutex_enter(&zone->zone_lock);
9435880Snordmark 	t = list_head(&zone->zone_zsd);
9445880Snordmark 	while (t != NULL) {
9455880Snordmark 		if ((applyfn)(NULL, B_TRUE, zone, t->zsd_key)) {
9465880Snordmark 			/* Lock dropped - restart at head */
9475880Snordmark 			t = list_head(&zone->zone_zsd);
9485880Snordmark 		} else {
9495880Snordmark 			t = list_next(&zone->zone_zsd, t);
9505880Snordmark 		}
9515880Snordmark 	}
9525880Snordmark 	mutex_exit(&zone->zone_lock);
9535880Snordmark }
9545880Snordmark 
9555880Snordmark /*
9565880Snordmark  * Call the create function for the zone and key if CREATE_NEEDED
9575880Snordmark  * is set.
9585880Snordmark  * If some other thread gets here first and sets CREATE_INPROGRESS, then
9595880Snordmark  * we wait for that thread to complete so that we can ensure that
9605880Snordmark  * all the callbacks are done when we've looped over all zones/keys.
9615880Snordmark  *
9625880Snordmark  * When we call the create function, we drop the global held by the
9635880Snordmark  * caller, and return true to tell the caller it needs to re-evalute the
9645880Snordmark  * state.
9655880Snordmark  * If the caller holds zone_lock then zone_lock_held is set, and zone_lock
9665880Snordmark  * remains held on exit.
9675880Snordmark  */
9685880Snordmark static boolean_t
9695880Snordmark zsd_apply_create(kmutex_t *lockp, boolean_t zone_lock_held,
9705880Snordmark     zone_t *zone, zone_key_t key)
9715880Snordmark {
9725880Snordmark 	void *result;
9735880Snordmark 	struct zsd_entry *t;
9745880Snordmark 	boolean_t dropped;
9755880Snordmark 
9765880Snordmark 	if (lockp != NULL) {
9775880Snordmark 		ASSERT(MUTEX_HELD(lockp));
9785880Snordmark 	}
9795880Snordmark 	if (zone_lock_held) {
9805880Snordmark 		ASSERT(MUTEX_HELD(&zone->zone_lock));
9815880Snordmark 	} else {
9825880Snordmark 		mutex_enter(&zone->zone_lock);
9835880Snordmark 	}
9845880Snordmark 
9855880Snordmark 	t = zsd_find(&zone->zone_zsd, key);
9865880Snordmark 	if (t == NULL) {
9875880Snordmark 		/*
9885880Snordmark 		 * Somebody else got here first e.g the zone going
9895880Snordmark 		 * away.
9905880Snordmark 		 */
9915880Snordmark 		if (!zone_lock_held)
9925880Snordmark 			mutex_exit(&zone->zone_lock);
9935880Snordmark 		return (B_FALSE);
9945880Snordmark 	}
9955880Snordmark 	dropped = B_FALSE;
9965880Snordmark 	if (zsd_wait_for_inprogress(zone, t, lockp))
9975880Snordmark 		dropped = B_TRUE;
9985880Snordmark 
9995880Snordmark 	if (t->zsd_flags & ZSD_CREATE_NEEDED) {
10005880Snordmark 		t->zsd_flags &= ~ZSD_CREATE_NEEDED;
10015880Snordmark 		t->zsd_flags |= ZSD_CREATE_INPROGRESS;
10025880Snordmark 		DTRACE_PROBE2(zsd__create__inprogress,
10035880Snordmark 		    zone_t *, zone, zone_key_t, key);
10045880Snordmark 		mutex_exit(&zone->zone_lock);
10055880Snordmark 		if (lockp != NULL)
10065880Snordmark 			mutex_exit(lockp);
10075880Snordmark 
10085880Snordmark 		dropped = B_TRUE;
10095880Snordmark 		ASSERT(t->zsd_create != NULL);
10105880Snordmark 		DTRACE_PROBE2(zsd__create__start,
10115880Snordmark 		    zone_t *, zone, zone_key_t, key);
10125880Snordmark 
10135880Snordmark 		result = (*t->zsd_create)(zone->zone_id);
10145880Snordmark 
10155880Snordmark 		DTRACE_PROBE2(zsd__create__end,
10165880Snordmark 		    zone_t *, zone, voidn *, result);
10175880Snordmark 
10185880Snordmark 		ASSERT(result != NULL);
10195880Snordmark 		if (lockp != NULL)
10205880Snordmark 			mutex_enter(lockp);
10215880Snordmark 		mutex_enter(&zone->zone_lock);
10225880Snordmark 		t->zsd_data = result;
10235880Snordmark 		t->zsd_flags &= ~ZSD_CREATE_INPROGRESS;
10245880Snordmark 		t->zsd_flags |= ZSD_CREATE_COMPLETED;
10255880Snordmark 		cv_broadcast(&t->zsd_cv);
10265880Snordmark 		DTRACE_PROBE2(zsd__create__completed,
10275880Snordmark 		    zone_t *, zone, zone_key_t, key);
10285880Snordmark 	}
10295880Snordmark 	if (!zone_lock_held)
10305880Snordmark 		mutex_exit(&zone->zone_lock);
10315880Snordmark 	return (dropped);
10325880Snordmark }
10335880Snordmark 
10345880Snordmark /*
10355880Snordmark  * Call the shutdown function for the zone and key if SHUTDOWN_NEEDED
10365880Snordmark  * is set.
10375880Snordmark  * If some other thread gets here first and sets *_INPROGRESS, then
10385880Snordmark  * we wait for that thread to complete so that we can ensure that
10395880Snordmark  * all the callbacks are done when we've looped over all zones/keys.
10405880Snordmark  *
10415880Snordmark  * When we call the shutdown function, we drop the global held by the
10425880Snordmark  * caller, and return true to tell the caller it needs to re-evalute the
10435880Snordmark  * state.
10445880Snordmark  * If the caller holds zone_lock then zone_lock_held is set, and zone_lock
10455880Snordmark  * remains held on exit.
10465880Snordmark  */
10475880Snordmark static boolean_t
10485880Snordmark zsd_apply_shutdown(kmutex_t *lockp, boolean_t zone_lock_held,
10495880Snordmark     zone_t *zone, zone_key_t key)
10505880Snordmark {
10515880Snordmark 	struct zsd_entry *t;
10525880Snordmark 	void *data;
10535880Snordmark 	boolean_t dropped;
10545880Snordmark 
10555880Snordmark 	if (lockp != NULL) {
10565880Snordmark 		ASSERT(MUTEX_HELD(lockp));
10575880Snordmark 	}
10585880Snordmark 	if (zone_lock_held) {
10595880Snordmark 		ASSERT(MUTEX_HELD(&zone->zone_lock));
10605880Snordmark 	} else {
10615880Snordmark 		mutex_enter(&zone->zone_lock);
10625880Snordmark 	}
10635880Snordmark 
10645880Snordmark 	t = zsd_find(&zone->zone_zsd, key);
10655880Snordmark 	if (t == NULL) {
10665880Snordmark 		/*
10675880Snordmark 		 * Somebody else got here first e.g the zone going
10685880Snordmark 		 * away.
10695880Snordmark 		 */
10705880Snordmark 		if (!zone_lock_held)
10715880Snordmark 			mutex_exit(&zone->zone_lock);
10725880Snordmark 		return (B_FALSE);
10735880Snordmark 	}
10745880Snordmark 	dropped = B_FALSE;
10755880Snordmark 	if (zsd_wait_for_creator(zone, t, lockp))
10765880Snordmark 		dropped = B_TRUE;
10775880Snordmark 
10785880Snordmark 	if (zsd_wait_for_inprogress(zone, t, lockp))
10795880Snordmark 		dropped = B_TRUE;
10805880Snordmark 
10815880Snordmark 	if (t->zsd_flags & ZSD_SHUTDOWN_NEEDED) {
10825880Snordmark 		t->zsd_flags &= ~ZSD_SHUTDOWN_NEEDED;
10835880Snordmark 		t->zsd_flags |= ZSD_SHUTDOWN_INPROGRESS;
10845880Snordmark 		DTRACE_PROBE2(zsd__shutdown__inprogress,
10855880Snordmark 		    zone_t *, zone, zone_key_t, key);
10865880Snordmark 		mutex_exit(&zone->zone_lock);
10875880Snordmark 		if (lockp != NULL)
10885880Snordmark 			mutex_exit(lockp);
10895880Snordmark 		dropped = B_TRUE;
10905880Snordmark 
10915880Snordmark 		ASSERT(t->zsd_shutdown != NULL);
10925880Snordmark 		data = t->zsd_data;
10935880Snordmark 
10945880Snordmark 		DTRACE_PROBE2(zsd__shutdown__start,
10955880Snordmark 		    zone_t *, zone, zone_key_t, key);
10965880Snordmark 
10975880Snordmark 		(t->zsd_shutdown)(zone->zone_id, data);
10985880Snordmark 		DTRACE_PROBE2(zsd__shutdown__end,
10995880Snordmark 		    zone_t *, zone, zone_key_t, key);
11005880Snordmark 
11015880Snordmark 		if (lockp != NULL)
11025880Snordmark 			mutex_enter(lockp);
11035880Snordmark 		mutex_enter(&zone->zone_lock);
11045880Snordmark 		t->zsd_flags &= ~ZSD_SHUTDOWN_INPROGRESS;
11055880Snordmark 		t->zsd_flags |= ZSD_SHUTDOWN_COMPLETED;
11065880Snordmark 		cv_broadcast(&t->zsd_cv);
11075880Snordmark 		DTRACE_PROBE2(zsd__shutdown__completed,
11085880Snordmark 		    zone_t *, zone, zone_key_t, key);
11095880Snordmark 	}
11105880Snordmark 	if (!zone_lock_held)
11115880Snordmark 		mutex_exit(&zone->zone_lock);
11125880Snordmark 	return (dropped);
11135880Snordmark }
11145880Snordmark 
11155880Snordmark /*
11165880Snordmark  * Call the destroy function for the zone and key if DESTROY_NEEDED
11175880Snordmark  * is set.
11185880Snordmark  * If some other thread gets here first and sets *_INPROGRESS, then
11195880Snordmark  * we wait for that thread to complete so that we can ensure that
11205880Snordmark  * all the callbacks are done when we've looped over all zones/keys.
11215880Snordmark  *
11225880Snordmark  * When we call the destroy function, we drop the global held by the
11235880Snordmark  * caller, and return true to tell the caller it needs to re-evalute the
11245880Snordmark  * state.
11255880Snordmark  * If the caller holds zone_lock then zone_lock_held is set, and zone_lock
11265880Snordmark  * remains held on exit.
11275880Snordmark  */
11285880Snordmark static boolean_t
11295880Snordmark zsd_apply_destroy(kmutex_t *lockp, boolean_t zone_lock_held,
11305880Snordmark     zone_t *zone, zone_key_t key)
11315880Snordmark {
11325880Snordmark 	struct zsd_entry *t;
11335880Snordmark 	void *data;
11345880Snordmark 	boolean_t dropped;
11355880Snordmark 
11365880Snordmark 	if (lockp != NULL) {
11375880Snordmark 		ASSERT(MUTEX_HELD(lockp));
11385880Snordmark 	}
11395880Snordmark 	if (zone_lock_held) {
11405880Snordmark 		ASSERT(MUTEX_HELD(&zone->zone_lock));
11415880Snordmark 	} else {
11425880Snordmark 		mutex_enter(&zone->zone_lock);
11435880Snordmark 	}
11445880Snordmark 
11455880Snordmark 	t = zsd_find(&zone->zone_zsd, key);
11465880Snordmark 	if (t == NULL) {
11475880Snordmark 		/*
11485880Snordmark 		 * Somebody else got here first e.g the zone going
11495880Snordmark 		 * away.
11505880Snordmark 		 */
11515880Snordmark 		if (!zone_lock_held)
11525880Snordmark 			mutex_exit(&zone->zone_lock);
11535880Snordmark 		return (B_FALSE);
11545880Snordmark 	}
11555880Snordmark 	dropped = B_FALSE;
11565880Snordmark 	if (zsd_wait_for_creator(zone, t, lockp))
11575880Snordmark 		dropped = B_TRUE;
11585880Snordmark 
11595880Snordmark 	if (zsd_wait_for_inprogress(zone, t, lockp))
11605880Snordmark 		dropped = B_TRUE;
11615880Snordmark 
11625880Snordmark 	if (t->zsd_flags & ZSD_DESTROY_NEEDED) {
11635880Snordmark 		t->zsd_flags &= ~ZSD_DESTROY_NEEDED;
11645880Snordmark 		t->zsd_flags |= ZSD_DESTROY_INPROGRESS;
11655880Snordmark 		DTRACE_PROBE2(zsd__destroy__inprogress,
11665880Snordmark 		    zone_t *, zone, zone_key_t, key);
11675880Snordmark 		mutex_exit(&zone->zone_lock);
11685880Snordmark 		if (lockp != NULL)
11695880Snordmark 			mutex_exit(lockp);
11705880Snordmark 		dropped = B_TRUE;
11715880Snordmark 
11725880Snordmark 		ASSERT(t->zsd_destroy != NULL);
11735880Snordmark 		data = t->zsd_data;
11745880Snordmark 		DTRACE_PROBE2(zsd__destroy__start,
11755880Snordmark 		    zone_t *, zone, zone_key_t, key);
11765880Snordmark 
11775880Snordmark 		(t->zsd_destroy)(zone->zone_id, data);
11785880Snordmark 		DTRACE_PROBE2(zsd__destroy__end,
11795880Snordmark 		    zone_t *, zone, zone_key_t, key);
11805880Snordmark 
11815880Snordmark 		if (lockp != NULL)
11825880Snordmark 			mutex_enter(lockp);
11835880Snordmark 		mutex_enter(&zone->zone_lock);
11845880Snordmark 		t->zsd_data = NULL;
11855880Snordmark 		t->zsd_flags &= ~ZSD_DESTROY_INPROGRESS;
11865880Snordmark 		t->zsd_flags |= ZSD_DESTROY_COMPLETED;
11875880Snordmark 		cv_broadcast(&t->zsd_cv);
11885880Snordmark 		DTRACE_PROBE2(zsd__destroy__completed,
11895880Snordmark 		    zone_t *, zone, zone_key_t, key);
11905880Snordmark 	}
11915880Snordmark 	if (!zone_lock_held)
11925880Snordmark 		mutex_exit(&zone->zone_lock);
11935880Snordmark 	return (dropped);
11945880Snordmark }
11955880Snordmark 
11965880Snordmark /*
11975880Snordmark  * Wait for any CREATE_NEEDED flag to be cleared.
11985880Snordmark  * Returns true if lockp was temporarily dropped while waiting.
11995880Snordmark  */
12005880Snordmark static boolean_t
12015880Snordmark zsd_wait_for_creator(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp)
12025880Snordmark {
12035880Snordmark 	boolean_t dropped = B_FALSE;
12045880Snordmark 
12055880Snordmark 	while (t->zsd_flags & ZSD_CREATE_NEEDED) {
12065880Snordmark 		DTRACE_PROBE2(zsd__wait__for__creator,
12075880Snordmark 		    zone_t *, zone, struct zsd_entry *, t);
12085880Snordmark 		if (lockp != NULL) {
12095880Snordmark 			dropped = B_TRUE;
12105880Snordmark 			mutex_exit(lockp);
12115880Snordmark 		}
12125880Snordmark 		cv_wait(&t->zsd_cv, &zone->zone_lock);
12135880Snordmark 		if (lockp != NULL) {
12145880Snordmark 			/* First drop zone_lock to preserve order */
12155880Snordmark 			mutex_exit(&zone->zone_lock);
12165880Snordmark 			mutex_enter(lockp);
12175880Snordmark 			mutex_enter(&zone->zone_lock);
12185880Snordmark 		}
12195880Snordmark 	}
12205880Snordmark 	return (dropped);
12215880Snordmark }
12225880Snordmark 
12235880Snordmark /*
12245880Snordmark  * Wait for any INPROGRESS flag to be cleared.
12255880Snordmark  * Returns true if lockp was temporarily dropped while waiting.
12265880Snordmark  */
12275880Snordmark static boolean_t
12285880Snordmark zsd_wait_for_inprogress(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp)
12295880Snordmark {
12305880Snordmark 	boolean_t dropped = B_FALSE;
12315880Snordmark 
12325880Snordmark 	while (t->zsd_flags & ZSD_ALL_INPROGRESS) {
12335880Snordmark 		DTRACE_PROBE2(zsd__wait__for__inprogress,
12345880Snordmark 		    zone_t *, zone, struct zsd_entry *, t);
12355880Snordmark 		if (lockp != NULL) {
12365880Snordmark 			dropped = B_TRUE;
12375880Snordmark 			mutex_exit(lockp);
12385880Snordmark 		}
12395880Snordmark 		cv_wait(&t->zsd_cv, &zone->zone_lock);
12405880Snordmark 		if (lockp != NULL) {
12415880Snordmark 			/* First drop zone_lock to preserve order */
12425880Snordmark 			mutex_exit(&zone->zone_lock);
12435880Snordmark 			mutex_enter(lockp);
12445880Snordmark 			mutex_enter(&zone->zone_lock);
12455880Snordmark 		}
12465880Snordmark 	}
12475880Snordmark 	return (dropped);
12480Sstevel@tonic-gate }
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate /*
1251789Sahrens  * Frees memory associated with the zone dataset list.
1252789Sahrens  */
1253789Sahrens static void
1254789Sahrens zone_free_datasets(zone_t *zone)
1255789Sahrens {
1256789Sahrens 	zone_dataset_t *t, *next;
1257789Sahrens 
1258789Sahrens 	for (t = list_head(&zone->zone_datasets); t != NULL; t = next) {
1259789Sahrens 		next = list_next(&zone->zone_datasets, t);
1260789Sahrens 		list_remove(&zone->zone_datasets, t);
1261789Sahrens 		kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1);
1262789Sahrens 		kmem_free(t, sizeof (*t));
1263789Sahrens 	}
1264789Sahrens 	list_destroy(&zone->zone_datasets);
1265789Sahrens }
1266789Sahrens 
1267789Sahrens /*
12680Sstevel@tonic-gate  * zone.cpu-shares resource control support.
12690Sstevel@tonic-gate  */
12700Sstevel@tonic-gate /*ARGSUSED*/
12710Sstevel@tonic-gate static rctl_qty_t
12720Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p)
12730Sstevel@tonic-gate {
12740Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
12750Sstevel@tonic-gate 	return (p->p_zone->zone_shares);
12760Sstevel@tonic-gate }
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate /*ARGSUSED*/
12790Sstevel@tonic-gate static int
12800Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
12810Sstevel@tonic-gate     rctl_qty_t nv)
12820Sstevel@tonic-gate {
12830Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
12840Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
12850Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
12860Sstevel@tonic-gate 		return (0);
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	e->rcep_p.zone->zone_shares = nv;
12890Sstevel@tonic-gate 	return (0);
12900Sstevel@tonic-gate }
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = {
12930Sstevel@tonic-gate 	rcop_no_action,
12940Sstevel@tonic-gate 	zone_cpu_shares_usage,
12950Sstevel@tonic-gate 	zone_cpu_shares_set,
12960Sstevel@tonic-gate 	rcop_no_test
12970Sstevel@tonic-gate };
12980Sstevel@tonic-gate 
12993792Sakolb /*
13003792Sakolb  * zone.cpu-cap resource control support.
13013792Sakolb  */
13023792Sakolb /*ARGSUSED*/
13033792Sakolb static rctl_qty_t
13043792Sakolb zone_cpu_cap_get(rctl_t *rctl, struct proc *p)
13053792Sakolb {
13063792Sakolb 	ASSERT(MUTEX_HELD(&p->p_lock));
13073792Sakolb 	return (cpucaps_zone_get(p->p_zone));
13083792Sakolb }
13093792Sakolb 
13103792Sakolb /*ARGSUSED*/
13113792Sakolb static int
13123792Sakolb zone_cpu_cap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
13133792Sakolb     rctl_qty_t nv)
13143792Sakolb {
13153792Sakolb 	zone_t *zone = e->rcep_p.zone;
13163792Sakolb 
13173792Sakolb 	ASSERT(MUTEX_HELD(&p->p_lock));
13183792Sakolb 	ASSERT(e->rcep_t == RCENTITY_ZONE);
13193792Sakolb 
13203792Sakolb 	if (zone == NULL)
13213792Sakolb 		return (0);
13223792Sakolb 
13233792Sakolb 	/*
13243792Sakolb 	 * set cap to the new value.
13253792Sakolb 	 */
13263792Sakolb 	return (cpucaps_zone_set(zone, nv));
13273792Sakolb }
13283792Sakolb 
13293792Sakolb static rctl_ops_t zone_cpu_cap_ops = {
13303792Sakolb 	rcop_no_action,
13313792Sakolb 	zone_cpu_cap_get,
13323792Sakolb 	zone_cpu_cap_set,
13333792Sakolb 	rcop_no_test
13343792Sakolb };
13353792Sakolb 
13360Sstevel@tonic-gate /*ARGSUSED*/
13370Sstevel@tonic-gate static rctl_qty_t
13380Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p)
13390Sstevel@tonic-gate {
13400Sstevel@tonic-gate 	rctl_qty_t nlwps;
13410Sstevel@tonic-gate 	zone_t *zone = p->p_zone;
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
13460Sstevel@tonic-gate 	nlwps = zone->zone_nlwps;
13470Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	return (nlwps);
13500Sstevel@tonic-gate }
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate /*ARGSUSED*/
13530Sstevel@tonic-gate static int
13540Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
13550Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
13560Sstevel@tonic-gate {
13570Sstevel@tonic-gate 	rctl_qty_t nlwps;
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
13600Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
13610Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
13620Sstevel@tonic-gate 		return (0);
13630Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock)));
13640Sstevel@tonic-gate 	nlwps = e->rcep_p.zone->zone_nlwps;
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	if (nlwps + incr > rcntl->rcv_value)
13670Sstevel@tonic-gate 		return (1);
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 	return (0);
13700Sstevel@tonic-gate }
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate /*ARGSUSED*/
13730Sstevel@tonic-gate static int
13742768Ssl108498 zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv)
13752768Ssl108498 {
13760Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
13770Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
13780Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
13790Sstevel@tonic-gate 		return (0);
13800Sstevel@tonic-gate 	e->rcep_p.zone->zone_nlwps_ctl = nv;
13810Sstevel@tonic-gate 	return (0);
13820Sstevel@tonic-gate }
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = {
13850Sstevel@tonic-gate 	rcop_no_action,
13860Sstevel@tonic-gate 	zone_lwps_usage,
13870Sstevel@tonic-gate 	zone_lwps_set,
13880Sstevel@tonic-gate 	zone_lwps_test,
13890Sstevel@tonic-gate };
13900Sstevel@tonic-gate 
13912677Sml93401 /*ARGSUSED*/
13922677Sml93401 static int
13932677Sml93401 zone_shmmax_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
13942677Sml93401     rctl_qty_t incr, uint_t flags)
13952677Sml93401 {
13962677Sml93401 	rctl_qty_t v;
13972677Sml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
13982677Sml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
13992677Sml93401 	v = e->rcep_p.zone->zone_shmmax + incr;
14002677Sml93401 	if (v > rval->rcv_value)
14012677Sml93401 		return (1);
14022677Sml93401 	return (0);
14032677Sml93401 }
14042677Sml93401 
14052677Sml93401 static rctl_ops_t zone_shmmax_ops = {
14062677Sml93401 	rcop_no_action,
14072677Sml93401 	rcop_no_usage,
14082677Sml93401 	rcop_no_set,
14092677Sml93401 	zone_shmmax_test
14102677Sml93401 };
14112677Sml93401 
14122677Sml93401 /*ARGSUSED*/
14132677Sml93401 static int
14142677Sml93401 zone_shmmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
14152677Sml93401     rctl_qty_t incr, uint_t flags)
14162677Sml93401 {
14172677Sml93401 	rctl_qty_t v;
14182677Sml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
14192677Sml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
14202677Sml93401 	v = e->rcep_p.zone->zone_ipc.ipcq_shmmni + incr;
14212677Sml93401 	if (v > rval->rcv_value)
14222677Sml93401 		return (1);
14232677Sml93401 	return (0);
14242677Sml93401 }
14252677Sml93401 
14262677Sml93401 static rctl_ops_t zone_shmmni_ops = {
14272677Sml93401 	rcop_no_action,
14282677Sml93401 	rcop_no_usage,
14292677Sml93401 	rcop_no_set,
14302677Sml93401 	zone_shmmni_test
14312677Sml93401 };
14322677Sml93401 
14332677Sml93401 /*ARGSUSED*/
14342677Sml93401 static int
14352677Sml93401 zone_semmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
14362677Sml93401     rctl_qty_t incr, uint_t flags)
14372677Sml93401 {
14382677Sml93401 	rctl_qty_t v;
14392677Sml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
14402677Sml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
14412677Sml93401 	v = e->rcep_p.zone->zone_ipc.ipcq_semmni + incr;
14422677Sml93401 	if (v > rval->rcv_value)
14432677Sml93401 		return (1);
14442677Sml93401 	return (0);
14452677Sml93401 }
14462677Sml93401 
14472677Sml93401 static rctl_ops_t zone_semmni_ops = {
14482677Sml93401 	rcop_no_action,
14492677Sml93401 	rcop_no_usage,
14502677Sml93401 	rcop_no_set,
14512677Sml93401 	zone_semmni_test
14522677Sml93401 };
14532677Sml93401 
14542677Sml93401 /*ARGSUSED*/
14552677Sml93401 static int
14562677Sml93401 zone_msgmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
14572677Sml93401     rctl_qty_t incr, uint_t flags)
14582677Sml93401 {
14592677Sml93401 	rctl_qty_t v;
14602677Sml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
14612677Sml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
14622677Sml93401 	v = e->rcep_p.zone->zone_ipc.ipcq_msgmni + incr;
14632677Sml93401 	if (v > rval->rcv_value)
14642677Sml93401 		return (1);
14652677Sml93401 	return (0);
14662677Sml93401 }
14672677Sml93401 
14682677Sml93401 static rctl_ops_t zone_msgmni_ops = {
14692677Sml93401 	rcop_no_action,
14702677Sml93401 	rcop_no_usage,
14712677Sml93401 	rcop_no_set,
14722677Sml93401 	zone_msgmni_test
14732677Sml93401 };
14742677Sml93401 
14752768Ssl108498 /*ARGSUSED*/
14762768Ssl108498 static rctl_qty_t
14772768Ssl108498 zone_locked_mem_usage(rctl_t *rctl, struct proc *p)
14782768Ssl108498 {
14792768Ssl108498 	rctl_qty_t q;
14802768Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
14813247Sgjelinek 	mutex_enter(&p->p_zone->zone_mem_lock);
14822768Ssl108498 	q = p->p_zone->zone_locked_mem;
14833247Sgjelinek 	mutex_exit(&p->p_zone->zone_mem_lock);
14842768Ssl108498 	return (q);
14852768Ssl108498 }
14862768Ssl108498 
14872768Ssl108498 /*ARGSUSED*/
14882768Ssl108498 static int
14892768Ssl108498 zone_locked_mem_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
14902768Ssl108498     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
14912768Ssl108498 {
14922768Ssl108498 	rctl_qty_t q;
14933247Sgjelinek 	zone_t *z;
14943247Sgjelinek 
14953247Sgjelinek 	z = e->rcep_p.zone;
14962768Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
14973247Sgjelinek 	ASSERT(MUTEX_HELD(&z->zone_mem_lock));
14983247Sgjelinek 	q = z->zone_locked_mem;
14992768Ssl108498 	if (q + incr > rcntl->rcv_value)
15002768Ssl108498 		return (1);
15012768Ssl108498 	return (0);
15022768Ssl108498 }
15032768Ssl108498 
15042768Ssl108498 /*ARGSUSED*/
15052768Ssl108498 static int
15062768Ssl108498 zone_locked_mem_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
15072768Ssl108498     rctl_qty_t nv)
15082768Ssl108498 {
15092768Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
15102768Ssl108498 	ASSERT(e->rcep_t == RCENTITY_ZONE);
15112768Ssl108498 	if (e->rcep_p.zone == NULL)
15122768Ssl108498 		return (0);
15132768Ssl108498 	e->rcep_p.zone->zone_locked_mem_ctl = nv;
15142768Ssl108498 	return (0);
15152768Ssl108498 }
15162768Ssl108498 
15172768Ssl108498 static rctl_ops_t zone_locked_mem_ops = {
15182768Ssl108498 	rcop_no_action,
15192768Ssl108498 	zone_locked_mem_usage,
15202768Ssl108498 	zone_locked_mem_set,
15212768Ssl108498 	zone_locked_mem_test
15222768Ssl108498 };
15232677Sml93401 
15243247Sgjelinek /*ARGSUSED*/
15253247Sgjelinek static rctl_qty_t
15263247Sgjelinek zone_max_swap_usage(rctl_t *rctl, struct proc *p)
15273247Sgjelinek {
15283247Sgjelinek 	rctl_qty_t q;
15293247Sgjelinek 	zone_t *z = p->p_zone;
15303247Sgjelinek 
15313247Sgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
15323247Sgjelinek 	mutex_enter(&z->zone_mem_lock);
15333247Sgjelinek 	q = z->zone_max_swap;
15343247Sgjelinek 	mutex_exit(&z->zone_mem_lock);
15353247Sgjelinek 	return (q);
15363247Sgjelinek }
15373247Sgjelinek 
15383247Sgjelinek /*ARGSUSED*/
15393247Sgjelinek static int
15403247Sgjelinek zone_max_swap_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
15413247Sgjelinek     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
15423247Sgjelinek {
15433247Sgjelinek 	rctl_qty_t q;
15443247Sgjelinek 	zone_t *z;
15453247Sgjelinek 
15463247Sgjelinek 	z = e->rcep_p.zone;
15473247Sgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
15483247Sgjelinek 	ASSERT(MUTEX_HELD(&z->zone_mem_lock));
15493247Sgjelinek 	q = z->zone_max_swap;
15503247Sgjelinek 	if (q + incr > rcntl->rcv_value)
15513247Sgjelinek 		return (1);
15523247Sgjelinek 	return (0);
15533247Sgjelinek }
15543247Sgjelinek 
15553247Sgjelinek /*ARGSUSED*/
15563247Sgjelinek static int
15573247Sgjelinek zone_max_swap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
15583247Sgjelinek     rctl_qty_t nv)
15593247Sgjelinek {
15603247Sgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
15613247Sgjelinek 	ASSERT(e->rcep_t == RCENTITY_ZONE);
15623247Sgjelinek 	if (e->rcep_p.zone == NULL)
15633247Sgjelinek 		return (0);
15643247Sgjelinek 	e->rcep_p.zone->zone_max_swap_ctl = nv;
15653247Sgjelinek 	return (0);
15663247Sgjelinek }
15673247Sgjelinek 
15683247Sgjelinek static rctl_ops_t zone_max_swap_ops = {
15693247Sgjelinek 	rcop_no_action,
15703247Sgjelinek 	zone_max_swap_usage,
15713247Sgjelinek 	zone_max_swap_set,
15723247Sgjelinek 	zone_max_swap_test
15733247Sgjelinek };
15743247Sgjelinek 
15750Sstevel@tonic-gate /*
15760Sstevel@tonic-gate  * Helper function to brand the zone with a unique ID.
15770Sstevel@tonic-gate  */
15780Sstevel@tonic-gate static void
15790Sstevel@tonic-gate zone_uniqid(zone_t *zone)
15800Sstevel@tonic-gate {
15810Sstevel@tonic-gate 	static uint64_t uniqid = 0;
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
15840Sstevel@tonic-gate 	zone->zone_uniqid = uniqid++;
15850Sstevel@tonic-gate }
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate /*
15880Sstevel@tonic-gate  * Returns a held pointer to the "kcred" for the specified zone.
15890Sstevel@tonic-gate  */
15900Sstevel@tonic-gate struct cred *
15910Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid)
15920Sstevel@tonic-gate {
15930Sstevel@tonic-gate 	zone_t *zone;
15940Sstevel@tonic-gate 	cred_t *cr;
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	if ((zone = zone_find_by_id(zoneid)) == NULL)
15970Sstevel@tonic-gate 		return (NULL);
15980Sstevel@tonic-gate 	cr = zone->zone_kcred;
15990Sstevel@tonic-gate 	crhold(cr);
16000Sstevel@tonic-gate 	zone_rele(zone);
16010Sstevel@tonic-gate 	return (cr);
16020Sstevel@tonic-gate }
16030Sstevel@tonic-gate 
16043247Sgjelinek static int
16053247Sgjelinek zone_lockedmem_kstat_update(kstat_t *ksp, int rw)
16063247Sgjelinek {
16073247Sgjelinek 	zone_t *zone = ksp->ks_private;
16083247Sgjelinek 	zone_kstat_t *zk = ksp->ks_data;
16093247Sgjelinek 
16103247Sgjelinek 	if (rw == KSTAT_WRITE)
16113247Sgjelinek 		return (EACCES);
16123247Sgjelinek 
16133247Sgjelinek 	zk->zk_usage.value.ui64 = zone->zone_locked_mem;
16143247Sgjelinek 	zk->zk_value.value.ui64 = zone->zone_locked_mem_ctl;
16153247Sgjelinek 	return (0);
16163247Sgjelinek }
16173247Sgjelinek 
16183247Sgjelinek static int
16193247Sgjelinek zone_swapresv_kstat_update(kstat_t *ksp, int rw)
16203247Sgjelinek {
16213247Sgjelinek 	zone_t *zone = ksp->ks_private;
16223247Sgjelinek 	zone_kstat_t *zk = ksp->ks_data;
16233247Sgjelinek 
16243247Sgjelinek 	if (rw == KSTAT_WRITE)
16253247Sgjelinek 		return (EACCES);
16263247Sgjelinek 
16273247Sgjelinek 	zk->zk_usage.value.ui64 = zone->zone_max_swap;
16283247Sgjelinek 	zk->zk_value.value.ui64 = zone->zone_max_swap_ctl;
16293247Sgjelinek 	return (0);
16303247Sgjelinek }
16313247Sgjelinek 
16323247Sgjelinek static void
16333247Sgjelinek zone_kstat_create(zone_t *zone)
16343247Sgjelinek {
16353247Sgjelinek 	kstat_t *ksp;
16363247Sgjelinek 	zone_kstat_t *zk;
16373247Sgjelinek 
16383247Sgjelinek 	ksp = rctl_kstat_create_zone(zone, "lockedmem", KSTAT_TYPE_NAMED,
16393247Sgjelinek 	    sizeof (zone_kstat_t) / sizeof (kstat_named_t),
16403247Sgjelinek 	    KSTAT_FLAG_VIRTUAL);
16413247Sgjelinek 
16423247Sgjelinek 	if (ksp == NULL)
16433247Sgjelinek 		return;
16443247Sgjelinek 
16453247Sgjelinek 	zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP);
16463247Sgjelinek 	ksp->ks_data_size += strlen(zone->zone_name) + 1;
16473247Sgjelinek 	kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING);
16483247Sgjelinek 	kstat_named_setstr(&zk->zk_zonename, zone->zone_name);
16493247Sgjelinek 	kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64);
16503247Sgjelinek 	kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64);
16513247Sgjelinek 	ksp->ks_update = zone_lockedmem_kstat_update;
16523247Sgjelinek 	ksp->ks_private = zone;
16533247Sgjelinek 	kstat_install(ksp);
16543247Sgjelinek 
16553247Sgjelinek 	zone->zone_lockedmem_kstat = ksp;
16563247Sgjelinek 
16573247Sgjelinek 	ksp = rctl_kstat_create_zone(zone, "swapresv", KSTAT_TYPE_NAMED,
16583247Sgjelinek 	    sizeof (zone_kstat_t) / sizeof (kstat_named_t),
16593247Sgjelinek 	    KSTAT_FLAG_VIRTUAL);
16603247Sgjelinek 
16613247Sgjelinek 	if (ksp == NULL)
16623247Sgjelinek 		return;
16633247Sgjelinek 
16643247Sgjelinek 	zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP);
16653247Sgjelinek 	ksp->ks_data_size += strlen(zone->zone_name) + 1;
16663247Sgjelinek 	kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING);
16673247Sgjelinek 	kstat_named_setstr(&zk->zk_zonename, zone->zone_name);
16683247Sgjelinek 	kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64);
16693247Sgjelinek 	kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64);
16703247Sgjelinek 	ksp->ks_update = zone_swapresv_kstat_update;
16713247Sgjelinek 	ksp->ks_private = zone;
16723247Sgjelinek 	kstat_install(ksp);
16733247Sgjelinek 
16743247Sgjelinek 	zone->zone_swapresv_kstat = ksp;
16753247Sgjelinek }
16763247Sgjelinek 
16773247Sgjelinek static void
16783247Sgjelinek zone_kstat_delete(zone_t *zone)
16793247Sgjelinek {
16803247Sgjelinek 	void *data;
16813247Sgjelinek 
16823247Sgjelinek 	if (zone->zone_lockedmem_kstat != NULL) {
16833247Sgjelinek 		data = zone->zone_lockedmem_kstat->ks_data;
16843247Sgjelinek 		kstat_delete(zone->zone_lockedmem_kstat);
16853247Sgjelinek 		kmem_free(data, sizeof (zone_kstat_t));
16863247Sgjelinek 	}
16873247Sgjelinek 	if (zone->zone_swapresv_kstat != NULL) {
16883247Sgjelinek 		data = zone->zone_swapresv_kstat->ks_data;
16893247Sgjelinek 		kstat_delete(zone->zone_swapresv_kstat);
16903247Sgjelinek 		kmem_free(data, sizeof (zone_kstat_t));
16913247Sgjelinek 	}
16923247Sgjelinek }
16933247Sgjelinek 
16940Sstevel@tonic-gate /*
16950Sstevel@tonic-gate  * Called very early on in boot to initialize the ZSD list so that
16960Sstevel@tonic-gate  * zone_key_create() can be called before zone_init().  It also initializes
16970Sstevel@tonic-gate  * portions of zone0 which may be used before zone_init() is called.  The
16980Sstevel@tonic-gate  * variable "global_zone" will be set when zone0 is fully initialized by
16990Sstevel@tonic-gate  * zone_init().
17000Sstevel@tonic-gate  */
17010Sstevel@tonic-gate void
17020Sstevel@tonic-gate zone_zsd_init(void)
17030Sstevel@tonic-gate {
17040Sstevel@tonic-gate 	mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL);
17050Sstevel@tonic-gate 	mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL);
17060Sstevel@tonic-gate 	list_create(&zsd_registered_keys, sizeof (struct zsd_entry),
17070Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
17080Sstevel@tonic-gate 	list_create(&zone_active, sizeof (zone_t),
17090Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
17100Sstevel@tonic-gate 	list_create(&zone_deathrow, sizeof (zone_t),
17110Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 	mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL);
17140Sstevel@tonic-gate 	mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
17153247Sgjelinek 	mutex_init(&zone0.zone_mem_lock, NULL, MUTEX_DEFAULT, NULL);
17160Sstevel@tonic-gate 	zone0.zone_shares = 1;
17173247Sgjelinek 	zone0.zone_nlwps = 0;
17180Sstevel@tonic-gate 	zone0.zone_nlwps_ctl = INT_MAX;
17193247Sgjelinek 	zone0.zone_locked_mem = 0;
17203247Sgjelinek 	zone0.zone_locked_mem_ctl = UINT64_MAX;
17213247Sgjelinek 	ASSERT(zone0.zone_max_swap == 0);
17223247Sgjelinek 	zone0.zone_max_swap_ctl = UINT64_MAX;
17232677Sml93401 	zone0.zone_shmmax = 0;
17242677Sml93401 	zone0.zone_ipc.ipcq_shmmni = 0;
17252677Sml93401 	zone0.zone_ipc.ipcq_semmni = 0;
17262677Sml93401 	zone0.zone_ipc.ipcq_msgmni = 0;
17270Sstevel@tonic-gate 	zone0.zone_name = GLOBAL_ZONENAME;
17280Sstevel@tonic-gate 	zone0.zone_nodename = utsname.nodename;
17290Sstevel@tonic-gate 	zone0.zone_domain = srpc_domain;
17300Sstevel@tonic-gate 	zone0.zone_ref = 1;
17310Sstevel@tonic-gate 	zone0.zone_id = GLOBAL_ZONEID;
17320Sstevel@tonic-gate 	zone0.zone_status = ZONE_IS_RUNNING;
17330Sstevel@tonic-gate 	zone0.zone_rootpath = "/";
17340Sstevel@tonic-gate 	zone0.zone_rootpathlen = 2;
17350Sstevel@tonic-gate 	zone0.zone_psetid = ZONE_PS_INVAL;
17360Sstevel@tonic-gate 	zone0.zone_ncpus = 0;
17370Sstevel@tonic-gate 	zone0.zone_ncpus_online = 0;
17380Sstevel@tonic-gate 	zone0.zone_proc_initpid = 1;
17392267Sdp 	zone0.zone_initname = initname;
17403247Sgjelinek 	zone0.zone_lockedmem_kstat = NULL;
17413247Sgjelinek 	zone0.zone_swapresv_kstat = NULL;
17420Sstevel@tonic-gate 	list_create(&zone0.zone_zsd, sizeof (struct zsd_entry),
17430Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
17440Sstevel@tonic-gate 	list_insert_head(&zone_active, &zone0);
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate 	/*
17470Sstevel@tonic-gate 	 * The root filesystem is not mounted yet, so zone_rootvp cannot be set
17480Sstevel@tonic-gate 	 * to anything meaningful.  It is assigned to be 'rootdir' in
17490Sstevel@tonic-gate 	 * vfs_mountroot().
17500Sstevel@tonic-gate 	 */
17510Sstevel@tonic-gate 	zone0.zone_rootvp = NULL;
17520Sstevel@tonic-gate 	zone0.zone_vfslist = NULL;
17532267Sdp 	zone0.zone_bootargs = initargs;
17540Sstevel@tonic-gate 	zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
17550Sstevel@tonic-gate 	/*
17560Sstevel@tonic-gate 	 * The global zone has all privileges
17570Sstevel@tonic-gate 	 */
17580Sstevel@tonic-gate 	priv_fillset(zone0.zone_privset);
17590Sstevel@tonic-gate 	/*
17600Sstevel@tonic-gate 	 * Add p0 to the global zone
17610Sstevel@tonic-gate 	 */
17620Sstevel@tonic-gate 	zone0.zone_zsched = &p0;
17630Sstevel@tonic-gate 	p0.p_zone = &zone0;
17640Sstevel@tonic-gate }
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate /*
17671676Sjpk  * Compute a hash value based on the contents of the label and the DOI.  The
17681676Sjpk  * hash algorithm is somewhat arbitrary, but is based on the observation that
17691676Sjpk  * humans will likely pick labels that differ by amounts that work out to be
17701676Sjpk  * multiples of the number of hash chains, and thus stirring in some primes
17711676Sjpk  * should help.
17721676Sjpk  */
17731676Sjpk static uint_t
17741676Sjpk hash_bylabel(void *hdata, mod_hash_key_t key)
17751676Sjpk {
17761676Sjpk 	const ts_label_t *lab = (ts_label_t *)key;
17771676Sjpk 	const uint32_t *up, *ue;
17781676Sjpk 	uint_t hash;
17791676Sjpk 	int i;
17801676Sjpk 
17811676Sjpk 	_NOTE(ARGUNUSED(hdata));
17821676Sjpk 
17831676Sjpk 	hash = lab->tsl_doi + (lab->tsl_doi << 1);
17841676Sjpk 	/* we depend on alignment of label, but not representation */
17851676Sjpk 	up = (const uint32_t *)&lab->tsl_label;
17861676Sjpk 	ue = up + sizeof (lab->tsl_label) / sizeof (*up);
17871676Sjpk 	i = 1;
17881676Sjpk 	while (up < ue) {
17891676Sjpk 		/* using 2^n + 1, 1 <= n <= 16 as source of many primes */
17901676Sjpk 		hash += *up + (*up << ((i % 16) + 1));
17911676Sjpk 		up++;
17921676Sjpk 		i++;
17931676Sjpk 	}
17941676Sjpk 	return (hash);
17951676Sjpk }
17961676Sjpk 
17971676Sjpk /*
17981676Sjpk  * All that mod_hash cares about here is zero (equal) versus non-zero (not
17991676Sjpk  * equal).  This may need to be changed if less than / greater than is ever
18001676Sjpk  * needed.
18011676Sjpk  */
18021676Sjpk static int
18031676Sjpk hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
18041676Sjpk {
18051676Sjpk 	ts_label_t *lab1 = (ts_label_t *)key1;
18061676Sjpk 	ts_label_t *lab2 = (ts_label_t *)key2;
18071676Sjpk 
18081676Sjpk 	return (label_equal(lab1, lab2) ? 0 : 1);
18091676Sjpk }
18101676Sjpk 
18111676Sjpk /*
18120Sstevel@tonic-gate  * Called by main() to initialize the zones framework.
18130Sstevel@tonic-gate  */
18140Sstevel@tonic-gate void
18150Sstevel@tonic-gate zone_init(void)
18160Sstevel@tonic-gate {
18170Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
18180Sstevel@tonic-gate 	rctl_val_t *dval;
18190Sstevel@tonic-gate 	rctl_set_t *set;
18200Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
18210Sstevel@tonic-gate 	rctl_entity_p_t e;
18221166Sdstaff 	int res;
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate 	ASSERT(curproc == &p0);
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 	/*
18270Sstevel@tonic-gate 	 * Create ID space for zone IDs.  ID 0 is reserved for the
18280Sstevel@tonic-gate 	 * global zone.
18290Sstevel@tonic-gate 	 */
18300Sstevel@tonic-gate 	zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID);
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate 	/*
18330Sstevel@tonic-gate 	 * Initialize generic zone resource controls, if any.
18340Sstevel@tonic-gate 	 */
18350Sstevel@tonic-gate 	rc_zone_cpu_shares = rctl_register("zone.cpu-shares",
18360Sstevel@tonic-gate 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER |
18371996Sml93401 	    RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER,
18383792Sakolb 	    FSS_MAXSHARES, FSS_MAXSHARES, &zone_cpu_shares_ops);
18393792Sakolb 
18403792Sakolb 	rc_zone_cpu_cap = rctl_register("zone.cpu-cap",
18413792Sakolb 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_ALWAYS |
18423792Sakolb 	    RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT |RCTL_GLOBAL_SYSLOG_NEVER |
18433792Sakolb 	    RCTL_GLOBAL_INFINITE,
18443792Sakolb 	    MAXCAP, MAXCAP, &zone_cpu_cap_ops);
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 	rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE,
18470Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
18480Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &zone_lwps_ops);
18490Sstevel@tonic-gate 	/*
18502677Sml93401 	 * System V IPC resource controls
18512677Sml93401 	 */
18522677Sml93401 	rc_zone_msgmni = rctl_register("zone.max-msg-ids",
18532677Sml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
18542677Sml93401 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_msgmni_ops);
18552677Sml93401 
18562677Sml93401 	rc_zone_semmni = rctl_register("zone.max-sem-ids",
18572677Sml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
18582677Sml93401 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_semmni_ops);
18592677Sml93401 
18602677Sml93401 	rc_zone_shmmni = rctl_register("zone.max-shm-ids",
18612677Sml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
18622677Sml93401 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_shmmni_ops);
18632677Sml93401 
18642677Sml93401 	rc_zone_shmmax = rctl_register("zone.max-shm-memory",
18652677Sml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
18662677Sml93401 	    RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &zone_shmmax_ops);
18672677Sml93401 
18682677Sml93401 	/*
18690Sstevel@tonic-gate 	 * Create a rctl_val with PRIVILEGED, NOACTION, value = 1.  Then attach
18700Sstevel@tonic-gate 	 * this at the head of the rctl_dict_entry for ``zone.cpu-shares''.
18710Sstevel@tonic-gate 	 */
18720Sstevel@tonic-gate 	dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
18730Sstevel@tonic-gate 	bzero(dval, sizeof (rctl_val_t));
18740Sstevel@tonic-gate 	dval->rcv_value = 1;
18750Sstevel@tonic-gate 	dval->rcv_privilege = RCPRIV_PRIVILEGED;
18760Sstevel@tonic-gate 	dval->rcv_flagaction = RCTL_LOCAL_NOACTION;
18770Sstevel@tonic-gate 	dval->rcv_action_recip_pid = -1;
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 	rde = rctl_dict_lookup("zone.cpu-shares");
18800Sstevel@tonic-gate 	(void) rctl_val_list_insert(&rde->rcd_default_value, dval);
18810Sstevel@tonic-gate 
18822768Ssl108498 	rc_zone_locked_mem = rctl_register("zone.max-locked-memory",
18832768Ssl108498 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES |
18842768Ssl108498 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
18852768Ssl108498 	    &zone_locked_mem_ops);
18863247Sgjelinek 
18873247Sgjelinek 	rc_zone_max_swap = rctl_register("zone.max-swap",
18883247Sgjelinek 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES |
18893247Sgjelinek 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
18903247Sgjelinek 	    &zone_max_swap_ops);
18913247Sgjelinek 
18920Sstevel@tonic-gate 	/*
18930Sstevel@tonic-gate 	 * Initialize the ``global zone''.
18940Sstevel@tonic-gate 	 */
18950Sstevel@tonic-gate 	set = rctl_set_create();
18960Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
18970Sstevel@tonic-gate 	mutex_enter(&p0.p_lock);
18980Sstevel@tonic-gate 	e.rcep_p.zone = &zone0;
18990Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
19000Sstevel@tonic-gate 	zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set,
19010Sstevel@tonic-gate 	    gp);
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 	zone0.zone_nlwps = p0.p_lwpcnt;
19040Sstevel@tonic-gate 	zone0.zone_ntasks = 1;
19050Sstevel@tonic-gate 	mutex_exit(&p0.p_lock);
19062712Snn35248 	zone0.zone_restart_init = B_TRUE;
19072712Snn35248 	zone0.zone_brand = &native_brand;
19080Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
19090Sstevel@tonic-gate 	/*
19103247Sgjelinek 	 * pool_default hasn't been initialized yet, so we let pool_init()
19113247Sgjelinek 	 * take care of making sure the global zone is in the default pool.
19120Sstevel@tonic-gate 	 */
19131676Sjpk 
19141676Sjpk 	/*
19153247Sgjelinek 	 * Initialize global zone kstats
19163247Sgjelinek 	 */
19173247Sgjelinek 	zone_kstat_create(&zone0);
19183247Sgjelinek 
19193247Sgjelinek 	/*
19201676Sjpk 	 * Initialize zone label.
19211676Sjpk 	 * mlp are initialized when tnzonecfg is loaded.
19221676Sjpk 	 */
19231676Sjpk 	zone0.zone_slabel = l_admin_low;
19241676Sjpk 	rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
19251676Sjpk 	label_hold(l_admin_low);
19261676Sjpk 
19270Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
19280Sstevel@tonic-gate 	zone_uniqid(&zone0);
19290Sstevel@tonic-gate 	ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID);
19301676Sjpk 
19310Sstevel@tonic-gate 	zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size,
19320Sstevel@tonic-gate 	    mod_hash_null_valdtor);
19330Sstevel@tonic-gate 	zonehashbyname = mod_hash_create_strhash("zone_by_name",
19340Sstevel@tonic-gate 	    zone_hash_size, mod_hash_null_valdtor);
19351676Sjpk 	/*
19361676Sjpk 	 * maintain zonehashbylabel only for labeled systems
19371676Sjpk 	 */
19381676Sjpk 	if (is_system_labeled())
19391676Sjpk 		zonehashbylabel = mod_hash_create_extended("zone_by_label",
19401676Sjpk 		    zone_hash_size, mod_hash_null_keydtor,
19411676Sjpk 		    mod_hash_null_valdtor, hash_bylabel, NULL,
19421676Sjpk 		    hash_labelkey_cmp, KM_SLEEP);
19430Sstevel@tonic-gate 	zonecount = 1;
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID,
19460Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
19470Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name,
19480Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
19491769Scarlsonj 	if (is_system_labeled()) {
19501769Scarlsonj 		zone0.zone_flags |= ZF_HASHED_LABEL;
19511676Sjpk 		(void) mod_hash_insert(zonehashbylabel,
19521676Sjpk 		    (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0);
19531769Scarlsonj 	}
19541676Sjpk 	mutex_exit(&zonehash_lock);
19551676Sjpk 
19560Sstevel@tonic-gate 	/*
19570Sstevel@tonic-gate 	 * We avoid setting zone_kcred until now, since kcred is initialized
19580Sstevel@tonic-gate 	 * sometime after zone_zsd_init() and before zone_init().
19590Sstevel@tonic-gate 	 */
19600Sstevel@tonic-gate 	zone0.zone_kcred = kcred;
19610Sstevel@tonic-gate 	/*
19620Sstevel@tonic-gate 	 * The global zone is fully initialized (except for zone_rootvp which
19630Sstevel@tonic-gate 	 * will be set when the root filesystem is mounted).
19640Sstevel@tonic-gate 	 */
19650Sstevel@tonic-gate 	global_zone = &zone0;
19661166Sdstaff 
19671166Sdstaff 	/*
19681166Sdstaff 	 * Setup an event channel to send zone status change notifications on
19691166Sdstaff 	 */
19701166Sdstaff 	res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan,
19711166Sdstaff 	    EVCH_CREAT);
19721166Sdstaff 
19731166Sdstaff 	if (res)
19741166Sdstaff 		panic("Sysevent_evc_bind failed during zone setup.\n");
19753247Sgjelinek 
19760Sstevel@tonic-gate }
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate static void
19790Sstevel@tonic-gate zone_free(zone_t *zone)
19800Sstevel@tonic-gate {
19810Sstevel@tonic-gate 	ASSERT(zone != global_zone);
19820Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks == 0);
19830Sstevel@tonic-gate 	ASSERT(zone->zone_nlwps == 0);
19840Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 0);
19850Sstevel@tonic-gate 	ASSERT(zone->zone_kcred == NULL);
19860Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_DEAD ||
19870Sstevel@tonic-gate 	    zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
19880Sstevel@tonic-gate 
19893792Sakolb 	/*
19903792Sakolb 	 * Remove any zone caps.
19913792Sakolb 	 */
19923792Sakolb 	cpucaps_zone_remove(zone);
19933792Sakolb 
19943792Sakolb 	ASSERT(zone->zone_cpucap == NULL);
19953792Sakolb 
19960Sstevel@tonic-gate 	/* remove from deathrow list */
19970Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_DEAD) {
19980Sstevel@tonic-gate 		ASSERT(zone->zone_ref == 0);
19990Sstevel@tonic-gate 		mutex_enter(&zone_deathrow_lock);
20000Sstevel@tonic-gate 		list_remove(&zone_deathrow, zone);
20010Sstevel@tonic-gate 		mutex_exit(&zone_deathrow_lock);
20020Sstevel@tonic-gate 	}
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 	zone_free_zsd(zone);
2005789Sahrens 	zone_free_datasets(zone);
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 	if (zone->zone_rootvp != NULL)
20080Sstevel@tonic-gate 		VN_RELE(zone->zone_rootvp);
20090Sstevel@tonic-gate 	if (zone->zone_rootpath)
20100Sstevel@tonic-gate 		kmem_free(zone->zone_rootpath, zone->zone_rootpathlen);
20110Sstevel@tonic-gate 	if (zone->zone_name != NULL)
20120Sstevel@tonic-gate 		kmem_free(zone->zone_name, ZONENAME_MAX);
20131676Sjpk 	if (zone->zone_slabel != NULL)
20141676Sjpk 		label_rele(zone->zone_slabel);
20150Sstevel@tonic-gate 	if (zone->zone_nodename != NULL)
20160Sstevel@tonic-gate 		kmem_free(zone->zone_nodename, _SYS_NMLN);
20170Sstevel@tonic-gate 	if (zone->zone_domain != NULL)
20180Sstevel@tonic-gate 		kmem_free(zone->zone_domain, _SYS_NMLN);
20190Sstevel@tonic-gate 	if (zone->zone_privset != NULL)
20200Sstevel@tonic-gate 		kmem_free(zone->zone_privset, sizeof (priv_set_t));
20210Sstevel@tonic-gate 	if (zone->zone_rctls != NULL)
20220Sstevel@tonic-gate 		rctl_set_free(zone->zone_rctls);
20230Sstevel@tonic-gate 	if (zone->zone_bootargs != NULL)
20242267Sdp 		kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1);
20252267Sdp 	if (zone->zone_initname != NULL)
20262267Sdp 		kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1);
20270Sstevel@tonic-gate 	id_free(zoneid_space, zone->zone_id);
20280Sstevel@tonic-gate 	mutex_destroy(&zone->zone_lock);
20290Sstevel@tonic-gate 	cv_destroy(&zone->zone_cv);
20301676Sjpk 	rw_destroy(&zone->zone_mlps.mlpl_rwlock);
20310Sstevel@tonic-gate 	kmem_free(zone, sizeof (zone_t));
20320Sstevel@tonic-gate }
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate /*
20350Sstevel@tonic-gate  * See block comment at the top of this file for information about zone
20360Sstevel@tonic-gate  * status values.
20370Sstevel@tonic-gate  */
20380Sstevel@tonic-gate /*
20390Sstevel@tonic-gate  * Convenience function for setting zone status.
20400Sstevel@tonic-gate  */
20410Sstevel@tonic-gate static void
20420Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status)
20430Sstevel@tonic-gate {
20441166Sdstaff 
20451166Sdstaff 	nvlist_t *nvl = NULL;
20460Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zone_status_lock));
20470Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE &&
20480Sstevel@tonic-gate 	    status >= zone_status_get(zone));
20491166Sdstaff 
20501166Sdstaff 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) ||
20511166Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) ||
20521166Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_NEWSTATE,
20532267Sdp 	    zone_status_table[status]) ||
20541166Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_OLDSTATE,
20552267Sdp 	    zone_status_table[zone->zone_status]) ||
20561166Sdstaff 	    nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) ||
20571166Sdstaff 	    nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) ||
20581166Sdstaff 	    sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS,
20592267Sdp 	    ZONE_EVENT_STATUS_SUBCLASS, "sun.com", "kernel", nvl, EVCH_SLEEP)) {
20601166Sdstaff #ifdef DEBUG
20611166Sdstaff 		(void) printf(
20621166Sdstaff 		    "Failed to allocate and send zone state change event.\n");
20631166Sdstaff #endif
20641166Sdstaff 	}
20651166Sdstaff 	nvlist_free(nvl);
20661166Sdstaff 
20670Sstevel@tonic-gate 	zone->zone_status = status;
20681166Sdstaff 
20690Sstevel@tonic-gate 	cv_broadcast(&zone->zone_cv);
20700Sstevel@tonic-gate }
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate /*
20730Sstevel@tonic-gate  * Public function to retrieve the zone status.  The zone status may
20740Sstevel@tonic-gate  * change after it is retrieved.
20750Sstevel@tonic-gate  */
20760Sstevel@tonic-gate zone_status_t
20770Sstevel@tonic-gate zone_status_get(zone_t *zone)
20780Sstevel@tonic-gate {
20790Sstevel@tonic-gate 	return (zone->zone_status);
20800Sstevel@tonic-gate }
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate static int
20830Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs)
20840Sstevel@tonic-gate {
20852267Sdp 	char *bootargs = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP);
20862267Sdp 	int err = 0;
20872267Sdp 
20882267Sdp 	ASSERT(zone != global_zone);
20892267Sdp 	if ((err = copyinstr(zone_bootargs, bootargs, BOOTARGS_MAX, NULL)) != 0)
20902267Sdp 		goto done;	/* EFAULT or ENAMETOOLONG */
20912267Sdp 
20922267Sdp 	if (zone->zone_bootargs != NULL)
20932267Sdp 		kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1);
20942267Sdp 
20952267Sdp 	zone->zone_bootargs = kmem_alloc(strlen(bootargs) + 1, KM_SLEEP);
20962267Sdp 	(void) strcpy(zone->zone_bootargs, bootargs);
20972267Sdp 
20982267Sdp done:
20992267Sdp 	kmem_free(bootargs, BOOTARGS_MAX);
21002267Sdp 	return (err);
21012267Sdp }
21022267Sdp 
21032267Sdp static int
21044141Sedp zone_set_brand(zone_t *zone, const char *brand)
21054141Sedp {
21064141Sedp 	struct brand_attr *attrp;
21074141Sedp 	brand_t *bp;
21084141Sedp 
21094141Sedp 	attrp = kmem_alloc(sizeof (struct brand_attr), KM_SLEEP);
21104141Sedp 	if (copyin(brand, attrp, sizeof (struct brand_attr)) != 0) {
21114141Sedp 		kmem_free(attrp, sizeof (struct brand_attr));
21124141Sedp 		return (EFAULT);
21134141Sedp 	}
21144141Sedp 
21154141Sedp 	bp = brand_register_zone(attrp);
21164141Sedp 	kmem_free(attrp, sizeof (struct brand_attr));
21174141Sedp 	if (bp == NULL)
21184141Sedp 		return (EINVAL);
21194141Sedp 
21204141Sedp 	/*
21214141Sedp 	 * This is the only place where a zone can change it's brand.
21224141Sedp 	 * We already need to hold zone_status_lock to check the zone
21234141Sedp 	 * status, so we'll just use that lock to serialize zone
21244141Sedp 	 * branding requests as well.
21254141Sedp 	 */
21264141Sedp 	mutex_enter(&zone_status_lock);
21274141Sedp 
21284141Sedp 	/* Re-Branding is not allowed and the zone can't be booted yet */
21294141Sedp 	if ((ZONE_IS_BRANDED(zone)) ||
21304141Sedp 	    (zone_status_get(zone) >= ZONE_IS_BOOTING)) {
21314141Sedp 		mutex_exit(&zone_status_lock);
21324141Sedp 		brand_unregister_zone(bp);
21334141Sedp 		return (EINVAL);
21344141Sedp 	}
21354141Sedp 
21364141Sedp 	if (is_system_labeled() &&
21374141Sedp 	    strncmp(attrp->ba_brandname, NATIVE_BRAND_NAME, MAXNAMELEN) != 0) {
21384141Sedp 		mutex_exit(&zone_status_lock);
21394141Sedp 		brand_unregister_zone(bp);
21404141Sedp 		return (EPERM);
21414141Sedp 	}
21424141Sedp 
21434888Seh208807 	/* set up the brand specific data */
21444141Sedp 	zone->zone_brand = bp;
21454888Seh208807 	ZBROP(zone)->b_init_brand_data(zone);
21464888Seh208807 
21474141Sedp 	mutex_exit(&zone_status_lock);
21484141Sedp 	return (0);
21494141Sedp }
21504141Sedp 
21514141Sedp static int
21522267Sdp zone_set_initname(zone_t *zone, const char *zone_initname)
21532267Sdp {
21542267Sdp 	char initname[INITNAME_SZ];
21550Sstevel@tonic-gate 	size_t len;
21562267Sdp 	int err = 0;
21572267Sdp 
21582267Sdp 	ASSERT(zone != global_zone);
21592267Sdp 	if ((err = copyinstr(zone_initname, initname, INITNAME_SZ, &len)) != 0)
21600Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
21612267Sdp 
21622267Sdp 	if (zone->zone_initname != NULL)
21632267Sdp 		kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1);
21642267Sdp 
21652267Sdp 	zone->zone_initname = kmem_alloc(strlen(initname) + 1, KM_SLEEP);
21662267Sdp 	(void) strcpy(zone->zone_initname, initname);
21670Sstevel@tonic-gate 	return (0);
21680Sstevel@tonic-gate }
21690Sstevel@tonic-gate 
21703247Sgjelinek static int
21713247Sgjelinek zone_set_phys_mcap(zone_t *zone, const uint64_t *zone_mcap)
21723247Sgjelinek {
21733247Sgjelinek 	uint64_t mcap;
21743247Sgjelinek 	int err = 0;
21753247Sgjelinek 
21763247Sgjelinek 	if ((err = copyin(zone_mcap, &mcap, sizeof (uint64_t))) == 0)
21773247Sgjelinek 		zone->zone_phys_mcap = mcap;
21783247Sgjelinek 
21793247Sgjelinek 	return (err);
21803247Sgjelinek }
21813247Sgjelinek 
21823247Sgjelinek static int
21833247Sgjelinek zone_set_sched_class(zone_t *zone, const char *new_class)
21843247Sgjelinek {
21853247Sgjelinek 	char sched_class[PC_CLNMSZ];
21863247Sgjelinek 	id_t classid;
21873247Sgjelinek 	int err;
21883247Sgjelinek 
21893247Sgjelinek 	ASSERT(zone != global_zone);
21903247Sgjelinek 	if ((err = copyinstr(new_class, sched_class, PC_CLNMSZ, NULL)) != 0)
21913247Sgjelinek 		return (err);	/* EFAULT or ENAMETOOLONG */
21923247Sgjelinek 
21933247Sgjelinek 	if (getcid(sched_class, &classid) != 0 || classid == syscid)
21943247Sgjelinek 		return (set_errno(EINVAL));
21953247Sgjelinek 	zone->zone_defaultcid = classid;
21963247Sgjelinek 	ASSERT(zone->zone_defaultcid > 0 &&
21973247Sgjelinek 	    zone->zone_defaultcid < loaded_classes);
21983247Sgjelinek 
21993247Sgjelinek 	return (0);
22003247Sgjelinek }
22013247Sgjelinek 
22020Sstevel@tonic-gate /*
22030Sstevel@tonic-gate  * Block indefinitely waiting for (zone_status >= status)
22040Sstevel@tonic-gate  */
22050Sstevel@tonic-gate void
22060Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status)
22070Sstevel@tonic-gate {
22080Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
22110Sstevel@tonic-gate 	while (zone->zone_status < status) {
22120Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
22130Sstevel@tonic-gate 	}
22140Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
22150Sstevel@tonic-gate }
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate /*
22180Sstevel@tonic-gate  * Private CPR-safe version of zone_status_wait().
22190Sstevel@tonic-gate  */
22200Sstevel@tonic-gate static void
22210Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str)
22220Sstevel@tonic-gate {
22230Sstevel@tonic-gate 	callb_cpr_t cprinfo;
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr,
22280Sstevel@tonic-gate 	    str);
22290Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
22300Sstevel@tonic-gate 	while (zone->zone_status < status) {
22310Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
22320Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
22330Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock);
22340Sstevel@tonic-gate 	}
22350Sstevel@tonic-gate 	/*
22360Sstevel@tonic-gate 	 * zone_status_lock is implicitly released by the following.
22370Sstevel@tonic-gate 	 */
22380Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
22390Sstevel@tonic-gate }
22400Sstevel@tonic-gate 
22410Sstevel@tonic-gate /*
22420Sstevel@tonic-gate  * Block until zone enters requested state or signal is received.  Return (0)
22430Sstevel@tonic-gate  * if signaled, non-zero otherwise.
22440Sstevel@tonic-gate  */
22450Sstevel@tonic-gate int
22460Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status)
22470Sstevel@tonic-gate {
22480Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
22510Sstevel@tonic-gate 	while (zone->zone_status < status) {
22520Sstevel@tonic-gate 		if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) {
22530Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
22540Sstevel@tonic-gate 			return (0);
22550Sstevel@tonic-gate 		}
22560Sstevel@tonic-gate 	}
22570Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
22580Sstevel@tonic-gate 	return (1);
22590Sstevel@tonic-gate }
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate /*
22620Sstevel@tonic-gate  * Block until the zone enters the requested state or the timeout expires,
22630Sstevel@tonic-gate  * whichever happens first.  Return (-1) if operation timed out, time remaining
22640Sstevel@tonic-gate  * otherwise.
22650Sstevel@tonic-gate  */
22660Sstevel@tonic-gate clock_t
22670Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status)
22680Sstevel@tonic-gate {
22690Sstevel@tonic-gate 	clock_t timeleft = 0;
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
22740Sstevel@tonic-gate 	while (zone->zone_status < status && timeleft != -1) {
22750Sstevel@tonic-gate 		timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim);
22760Sstevel@tonic-gate 	}
22770Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
22780Sstevel@tonic-gate 	return (timeleft);
22790Sstevel@tonic-gate }
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate /*
22820Sstevel@tonic-gate  * Block until the zone enters the requested state, the current process is
22830Sstevel@tonic-gate  * signaled,  or the timeout expires, whichever happens first.  Return (-1) if
22840Sstevel@tonic-gate  * operation timed out, 0 if signaled, time remaining otherwise.
22850Sstevel@tonic-gate  */
22860Sstevel@tonic-gate clock_t
22870Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status)
22880Sstevel@tonic-gate {
22890Sstevel@tonic-gate 	clock_t timeleft = tim - lbolt;
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
22940Sstevel@tonic-gate 	while (zone->zone_status < status) {
22950Sstevel@tonic-gate 		timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock,
22960Sstevel@tonic-gate 		    tim);
22970Sstevel@tonic-gate 		if (timeleft <= 0)
22980Sstevel@tonic-gate 			break;
22990Sstevel@tonic-gate 	}
23000Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
23010Sstevel@tonic-gate 	return (timeleft);
23020Sstevel@tonic-gate }
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate /*
23050Sstevel@tonic-gate  * Zones have two reference counts: one for references from credential
23060Sstevel@tonic-gate  * structures (zone_cred_ref), and one (zone_ref) for everything else.
23070Sstevel@tonic-gate  * This is so we can allow a zone to be rebooted while there are still
23080Sstevel@tonic-gate  * outstanding cred references, since certain drivers cache dblks (which
23090Sstevel@tonic-gate  * implicitly results in cached creds).  We wait for zone_ref to drop to
23100Sstevel@tonic-gate  * 0 (actually 1), but not zone_cred_ref.  The zone structure itself is
23110Sstevel@tonic-gate  * later freed when the zone_cred_ref drops to 0, though nothing other
23120Sstevel@tonic-gate  * than the zone id and privilege set should be accessed once the zone
23130Sstevel@tonic-gate  * is "dead".
23140Sstevel@tonic-gate  *
23150Sstevel@tonic-gate  * A debugging flag, zone_wait_for_cred, can be set to a non-zero value
23160Sstevel@tonic-gate  * to force halt/reboot to block waiting for the zone_cred_ref to drop
23170Sstevel@tonic-gate  * to 0.  This can be useful to flush out other sources of cached creds
23180Sstevel@tonic-gate  * that may be less innocuous than the driver case.
23190Sstevel@tonic-gate  */
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate int zone_wait_for_cred = 0;
23220Sstevel@tonic-gate 
23230Sstevel@tonic-gate static void
23240Sstevel@tonic-gate zone_hold_locked(zone_t *z)
23250Sstevel@tonic-gate {
23260Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&z->zone_lock));
23270Sstevel@tonic-gate 	z->zone_ref++;
23280Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
23290Sstevel@tonic-gate }
23300Sstevel@tonic-gate 
23310Sstevel@tonic-gate void
23320Sstevel@tonic-gate zone_hold(zone_t *z)
23330Sstevel@tonic-gate {
23340Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
23350Sstevel@tonic-gate 	zone_hold_locked(z);
23360Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
23370Sstevel@tonic-gate }
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate /*
23400Sstevel@tonic-gate  * If the non-cred ref count drops to 1 and either the cred ref count
23410Sstevel@tonic-gate  * is 0 or we aren't waiting for cred references, the zone is ready to
23420Sstevel@tonic-gate  * be destroyed.
23430Sstevel@tonic-gate  */
23440Sstevel@tonic-gate #define	ZONE_IS_UNREF(zone)	((zone)->zone_ref == 1 && \
23450Sstevel@tonic-gate 	    (!zone_wait_for_cred || (zone)->zone_cred_ref == 0))
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate void
23480Sstevel@tonic-gate zone_rele(zone_t *z)
23490Sstevel@tonic-gate {
23500Sstevel@tonic-gate 	boolean_t wakeup;
23510Sstevel@tonic-gate 
23520Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
23530Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
23540Sstevel@tonic-gate 	z->zone_ref--;
23550Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
23560Sstevel@tonic-gate 		/* no more refs, free the structure */
23570Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
23580Sstevel@tonic-gate 		zone_free(z);
23590Sstevel@tonic-gate 		return;
23600Sstevel@tonic-gate 	}
23610Sstevel@tonic-gate 	/* signal zone_destroy so the zone can finish halting */
23620Sstevel@tonic-gate 	wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD);
23630Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
23640Sstevel@tonic-gate 
23650Sstevel@tonic-gate 	if (wakeup) {
23660Sstevel@tonic-gate 		/*
23670Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
23680Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
23690Sstevel@tonic-gate 		 */
23700Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
23710Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
23720Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
23730Sstevel@tonic-gate 	}
23740Sstevel@tonic-gate }
23750Sstevel@tonic-gate 
23760Sstevel@tonic-gate void
23770Sstevel@tonic-gate zone_cred_hold(zone_t *z)
23780Sstevel@tonic-gate {
23790Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
23800Sstevel@tonic-gate 	z->zone_cred_ref++;
23810Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
23820Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
23830Sstevel@tonic-gate }
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate void
23860Sstevel@tonic-gate zone_cred_rele(zone_t *z)
23870Sstevel@tonic-gate {
23880Sstevel@tonic-gate 	boolean_t wakeup;
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
23910Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
23920Sstevel@tonic-gate 	z->zone_cred_ref--;
23930Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
23940Sstevel@tonic-gate 		/* no more refs, free the structure */
23950Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
23960Sstevel@tonic-gate 		zone_free(z);
23970Sstevel@tonic-gate 		return;
23980Sstevel@tonic-gate 	}
23990Sstevel@tonic-gate 	/*
24000Sstevel@tonic-gate 	 * If zone_destroy is waiting for the cred references to drain
24010Sstevel@tonic-gate 	 * out, and they have, signal it.
24020Sstevel@tonic-gate 	 */
24030Sstevel@tonic-gate 	wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) &&
24040Sstevel@tonic-gate 	    zone_status_get(z) >= ZONE_IS_DEAD);
24050Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 	if (wakeup) {
24080Sstevel@tonic-gate 		/*
24090Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
24100Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
24110Sstevel@tonic-gate 		 */
24120Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
24130Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
24140Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
24150Sstevel@tonic-gate 	}
24160Sstevel@tonic-gate }
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate void
24190Sstevel@tonic-gate zone_task_hold(zone_t *z)
24200Sstevel@tonic-gate {
24210Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
24220Sstevel@tonic-gate 	z->zone_ntasks++;
24230Sstevel@tonic-gate 	ASSERT(z->zone_ntasks != 0);
24240Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
24250Sstevel@tonic-gate }
24260Sstevel@tonic-gate 
24270Sstevel@tonic-gate void
24280Sstevel@tonic-gate zone_task_rele(zone_t *zone)
24290Sstevel@tonic-gate {
24300Sstevel@tonic-gate 	uint_t refcnt;
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
24330Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks != 0);
24340Sstevel@tonic-gate 	refcnt = --zone->zone_ntasks;
24350Sstevel@tonic-gate 	if (refcnt > 1)	{	/* Common case */
24360Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
24370Sstevel@tonic-gate 		return;
24380Sstevel@tonic-gate 	}
24390Sstevel@tonic-gate 	zone_hold_locked(zone);	/* so we can use the zone_t later */
24400Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
24410Sstevel@tonic-gate 	if (refcnt == 1) {
24420Sstevel@tonic-gate 		/*
24430Sstevel@tonic-gate 		 * See if the zone is shutting down.
24440Sstevel@tonic-gate 		 */
24450Sstevel@tonic-gate 		mutex_enter(&zone_status_lock);
24460Sstevel@tonic-gate 		if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) {
24470Sstevel@tonic-gate 			goto out;
24480Sstevel@tonic-gate 		}
24490Sstevel@tonic-gate 
24500Sstevel@tonic-gate 		/*
24510Sstevel@tonic-gate 		 * Make sure the ntasks didn't change since we
24520Sstevel@tonic-gate 		 * dropped zone_lock.
24530Sstevel@tonic-gate 		 */
24540Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
24550Sstevel@tonic-gate 		if (refcnt != zone->zone_ntasks) {
24560Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
24570Sstevel@tonic-gate 			goto out;
24580Sstevel@tonic-gate 		}
24590Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
24600Sstevel@tonic-gate 
24610Sstevel@tonic-gate 		/*
24620Sstevel@tonic-gate 		 * No more user processes in the zone.  The zone is empty.
24630Sstevel@tonic-gate 		 */
24640Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_EMPTY);
24650Sstevel@tonic-gate 		goto out;
24660Sstevel@tonic-gate 	}
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 	ASSERT(refcnt == 0);
24690Sstevel@tonic-gate 	/*
24700Sstevel@tonic-gate 	 * zsched has exited; the zone is dead.
24710Sstevel@tonic-gate 	 */
24720Sstevel@tonic-gate 	zone->zone_zsched = NULL;		/* paranoia */
24730Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
24740Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_DEAD);
24750Sstevel@tonic-gate out:
24760Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
24770Sstevel@tonic-gate 	zone_rele(zone);
24780Sstevel@tonic-gate }
24790Sstevel@tonic-gate 
24800Sstevel@tonic-gate zoneid_t
24810Sstevel@tonic-gate getzoneid(void)
24820Sstevel@tonic-gate {
24830Sstevel@tonic-gate 	return (curproc->p_zone->zone_id);
24840Sstevel@tonic-gate }
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate /*
24870Sstevel@tonic-gate  * Internal versions of zone_find_by_*().  These don't zone_hold() or
24880Sstevel@tonic-gate  * check the validity of a zone's state.
24890Sstevel@tonic-gate  */
24900Sstevel@tonic-gate static zone_t *
24910Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid)
24920Sstevel@tonic-gate {
24930Sstevel@tonic-gate 	mod_hash_val_t hv;
24940Sstevel@tonic-gate 	zone_t *zone = NULL;
24950Sstevel@tonic-gate 
24960Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyid,
24990Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0)
25000Sstevel@tonic-gate 		zone = (zone_t *)hv;
25010Sstevel@tonic-gate 	return (zone);
25020Sstevel@tonic-gate }
25030Sstevel@tonic-gate 
25040Sstevel@tonic-gate static zone_t *
25051676Sjpk zone_find_all_by_label(const ts_label_t *label)
25061676Sjpk {
25071676Sjpk 	mod_hash_val_t hv;
25081676Sjpk 	zone_t *zone = NULL;
25091676Sjpk 
25101676Sjpk 	ASSERT(MUTEX_HELD(&zonehash_lock));
25111676Sjpk 
25121676Sjpk 	/*
25131676Sjpk 	 * zonehashbylabel is not maintained for unlabeled systems
25141676Sjpk 	 */
25151676Sjpk 	if (!is_system_labeled())
25161676Sjpk 		return (NULL);
25171676Sjpk 	if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0)
25181676Sjpk 		zone = (zone_t *)hv;
25191676Sjpk 	return (zone);
25201676Sjpk }
25211676Sjpk 
25221676Sjpk static zone_t *
25230Sstevel@tonic-gate zone_find_all_by_name(char *name)
25240Sstevel@tonic-gate {
25250Sstevel@tonic-gate 	mod_hash_val_t hv;
25260Sstevel@tonic-gate 	zone_t *zone = NULL;
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0)
25310Sstevel@tonic-gate 		zone = (zone_t *)hv;
25320Sstevel@tonic-gate 	return (zone);
25330Sstevel@tonic-gate }
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate /*
25360Sstevel@tonic-gate  * Public interface for looking up a zone by zoneid.  Only returns the zone if
25370Sstevel@tonic-gate  * it is fully initialized, and has not yet begun the zone_destroy() sequence.
25380Sstevel@tonic-gate  * Caller must call zone_rele() once it is done with the zone.
25390Sstevel@tonic-gate  *
25400Sstevel@tonic-gate  * The zone may begin the zone_destroy() sequence immediately after this
25410Sstevel@tonic-gate  * function returns, but may be safely used until zone_rele() is called.
25420Sstevel@tonic-gate  */
25430Sstevel@tonic-gate zone_t *
25440Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid)
25450Sstevel@tonic-gate {
25460Sstevel@tonic-gate 	zone_t *zone;
25470Sstevel@tonic-gate 	zone_status_t status;
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
25500Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
25510Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
25520Sstevel@tonic-gate 		return (NULL);
25530Sstevel@tonic-gate 	}
25540Sstevel@tonic-gate 	status = zone_status_get(zone);
25550Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
25560Sstevel@tonic-gate 		/*
25570Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
25580Sstevel@tonic-gate 		 */
25590Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
25600Sstevel@tonic-gate 		return (NULL);
25610Sstevel@tonic-gate 	}
25620Sstevel@tonic-gate 	zone_hold(zone);
25630Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
25640Sstevel@tonic-gate 	return (zone);
25650Sstevel@tonic-gate }
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate /*
25681676Sjpk  * Similar to zone_find_by_id, but using zone label as the key.
25691676Sjpk  */
25701676Sjpk zone_t *
25711676Sjpk zone_find_by_label(const ts_label_t *label)
25721676Sjpk {
25731676Sjpk 	zone_t *zone;
25742110Srica 	zone_status_t status;
25751676Sjpk 
25761676Sjpk 	mutex_enter(&zonehash_lock);
25771676Sjpk 	if ((zone = zone_find_all_by_label(label)) == NULL) {
25781676Sjpk 		mutex_exit(&zonehash_lock);
25791676Sjpk 		return (NULL);
25801676Sjpk 	}
25812110Srica 
25822110Srica 	status = zone_status_get(zone);
25832110Srica 	if (status > ZONE_IS_DOWN) {
25841676Sjpk 		/*
25851676Sjpk 		 * For all practical purposes the zone doesn't exist.
25861676Sjpk 		 */
25872110Srica 		mutex_exit(&zonehash_lock);
25882110Srica 		return (NULL);
25891676Sjpk 	}
25902110Srica 	zone_hold(zone);
25911676Sjpk 	mutex_exit(&zonehash_lock);
25921676Sjpk 	return (zone);
25931676Sjpk }
25941676Sjpk 
25951676Sjpk /*
25960Sstevel@tonic-gate  * Similar to zone_find_by_id, but using zone name as the key.
25970Sstevel@tonic-gate  */
25980Sstevel@tonic-gate zone_t *
25990Sstevel@tonic-gate zone_find_by_name(char *name)
26000Sstevel@tonic-gate {
26010Sstevel@tonic-gate 	zone_t *zone;
26020Sstevel@tonic-gate 	zone_status_t status;
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
26050Sstevel@tonic-gate 	if ((zone = zone_find_all_by_name(name)) == NULL) {
26060Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
26070Sstevel@tonic-gate 		return (NULL);
26080Sstevel@tonic-gate 	}
26090Sstevel@tonic-gate 	status = zone_status_get(zone);
26100Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
26110Sstevel@tonic-gate 		/*
26120Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
26130Sstevel@tonic-gate 		 */
26140Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
26150Sstevel@tonic-gate 		return (NULL);
26160Sstevel@tonic-gate 	}
26170Sstevel@tonic-gate 	zone_hold(zone);
26180Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
26190Sstevel@tonic-gate 	return (zone);
26200Sstevel@tonic-gate }
26210Sstevel@tonic-gate 
26220Sstevel@tonic-gate /*
26230Sstevel@tonic-gate  * Similar to zone_find_by_id(), using the path as a key.  For instance,
26240Sstevel@tonic-gate  * if there is a zone "foo" rooted at /foo/root, and the path argument
26250Sstevel@tonic-gate  * is "/foo/root/proc", it will return the held zone_t corresponding to
26260Sstevel@tonic-gate  * zone "foo".
26270Sstevel@tonic-gate  *
26280Sstevel@tonic-gate  * zone_find_by_path() always returns a non-NULL value, since at the
26290Sstevel@tonic-gate  * very least every path will be contained in the global zone.
26300Sstevel@tonic-gate  *
26310Sstevel@tonic-gate  * As with the other zone_find_by_*() functions, the caller is
26320Sstevel@tonic-gate  * responsible for zone_rele()ing the return value of this function.
26330Sstevel@tonic-gate  */
26340Sstevel@tonic-gate zone_t *
26350Sstevel@tonic-gate zone_find_by_path(const char *path)
26360Sstevel@tonic-gate {
26370Sstevel@tonic-gate 	zone_t *zone;
26380Sstevel@tonic-gate 	zone_t *zret = NULL;
26390Sstevel@tonic-gate 	zone_status_t status;
26400Sstevel@tonic-gate 
26410Sstevel@tonic-gate 	if (path == NULL) {
26420Sstevel@tonic-gate 		/*
26430Sstevel@tonic-gate 		 * Call from rootconf().
26440Sstevel@tonic-gate 		 */
26450Sstevel@tonic-gate 		zone_hold(global_zone);
26460Sstevel@tonic-gate 		return (global_zone);
26470Sstevel@tonic-gate 	}
26480Sstevel@tonic-gate 	ASSERT(*path == '/');
26490Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
26500Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
26510Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
26520Sstevel@tonic-gate 		if (ZONE_PATH_VISIBLE(path, zone))
26530Sstevel@tonic-gate 			zret = zone;
26540Sstevel@tonic-gate 	}
26550Sstevel@tonic-gate 	ASSERT(zret != NULL);
26560Sstevel@tonic-gate 	status = zone_status_get(zret);
26570Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
26580Sstevel@tonic-gate 		/*
26590Sstevel@tonic-gate 		 * Zone practically doesn't exist.
26600Sstevel@tonic-gate 		 */
26610Sstevel@tonic-gate 		zret = global_zone;
26620Sstevel@tonic-gate 	}
26630Sstevel@tonic-gate 	zone_hold(zret);
26640Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
26650Sstevel@tonic-gate 	return (zret);
26660Sstevel@tonic-gate }
26670Sstevel@tonic-gate 
26680Sstevel@tonic-gate /*
26690Sstevel@tonic-gate  * Get the number of cpus visible to this zone.  The system-wide global
26700Sstevel@tonic-gate  * 'ncpus' is returned if pools are disabled, the caller is in the
26710Sstevel@tonic-gate  * global zone, or a NULL zone argument is passed in.
26720Sstevel@tonic-gate  */
26730Sstevel@tonic-gate int
26740Sstevel@tonic-gate zone_ncpus_get(zone_t *zone)
26750Sstevel@tonic-gate {
26760Sstevel@tonic-gate 	int myncpus = zone == NULL ? 0 : zone->zone_ncpus;
26770Sstevel@tonic-gate 
26780Sstevel@tonic-gate 	return (myncpus != 0 ? myncpus : ncpus);
26790Sstevel@tonic-gate }
26800Sstevel@tonic-gate 
26810Sstevel@tonic-gate /*
26820Sstevel@tonic-gate  * Get the number of online cpus visible to this zone.  The system-wide
26830Sstevel@tonic-gate  * global 'ncpus_online' is returned if pools are disabled, the caller
26840Sstevel@tonic-gate  * is in the global zone, or a NULL zone argument is passed in.
26850Sstevel@tonic-gate  */
26860Sstevel@tonic-gate int
26870Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone)
26880Sstevel@tonic-gate {
26890Sstevel@tonic-gate 	int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online;
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate 	return (myncpus_online != 0 ? myncpus_online : ncpus_online);
26920Sstevel@tonic-gate }
26930Sstevel@tonic-gate 
26940Sstevel@tonic-gate /*
26950Sstevel@tonic-gate  * Return the pool to which the zone is currently bound.
26960Sstevel@tonic-gate  */
26970Sstevel@tonic-gate pool_t *
26980Sstevel@tonic-gate zone_pool_get(zone_t *zone)
26990Sstevel@tonic-gate {
27000Sstevel@tonic-gate 	ASSERT(pool_lock_held());
27010Sstevel@tonic-gate 
27020Sstevel@tonic-gate 	return (zone->zone_pool);
27030Sstevel@tonic-gate }
27040Sstevel@tonic-gate 
27050Sstevel@tonic-gate /*
27060Sstevel@tonic-gate  * Set the zone's pool pointer and update the zone's visibility to match
27070Sstevel@tonic-gate  * the resources in the new pool.
27080Sstevel@tonic-gate  */
27090Sstevel@tonic-gate void
27100Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool)
27110Sstevel@tonic-gate {
27120Sstevel@tonic-gate 	ASSERT(pool_lock_held());
27130Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
27140Sstevel@tonic-gate 
27150Sstevel@tonic-gate 	zone->zone_pool = pool;
27160Sstevel@tonic-gate 	zone_pset_set(zone, pool->pool_pset->pset_id);
27170Sstevel@tonic-gate }
27180Sstevel@tonic-gate 
27190Sstevel@tonic-gate /*
27200Sstevel@tonic-gate  * Return the cached value of the id of the processor set to which the
27210Sstevel@tonic-gate  * zone is currently bound.  The value will be ZONE_PS_INVAL if the pools
27220Sstevel@tonic-gate  * facility is disabled.
27230Sstevel@tonic-gate  */
27240Sstevel@tonic-gate psetid_t
27250Sstevel@tonic-gate zone_pset_get(zone_t *zone)
27260Sstevel@tonic-gate {
27270Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
27280Sstevel@tonic-gate 
27290Sstevel@tonic-gate 	return (zone->zone_psetid);
27300Sstevel@tonic-gate }
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate /*
27330Sstevel@tonic-gate  * Set the cached value of the id of the processor set to which the zone
27340Sstevel@tonic-gate  * is currently bound.  Also update the zone's visibility to match the
27350Sstevel@tonic-gate  * resources in the new processor set.
27360Sstevel@tonic-gate  */
27370Sstevel@tonic-gate void
27380Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid)
27390Sstevel@tonic-gate {
27400Sstevel@tonic-gate 	psetid_t oldpsetid;
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
27430Sstevel@tonic-gate 	oldpsetid = zone_pset_get(zone);
27440Sstevel@tonic-gate 
27450Sstevel@tonic-gate 	if (oldpsetid == newpsetid)
27460Sstevel@tonic-gate 		return;
27470Sstevel@tonic-gate 	/*
27480Sstevel@tonic-gate 	 * Global zone sees all.
27490Sstevel@tonic-gate 	 */
27500Sstevel@tonic-gate 	if (zone != global_zone) {
27510Sstevel@tonic-gate 		zone->zone_psetid = newpsetid;
27520Sstevel@tonic-gate 		if (newpsetid != ZONE_PS_INVAL)
27530Sstevel@tonic-gate 			pool_pset_visibility_add(newpsetid, zone);
27540Sstevel@tonic-gate 		if (oldpsetid != ZONE_PS_INVAL)
27550Sstevel@tonic-gate 			pool_pset_visibility_remove(oldpsetid, zone);
27560Sstevel@tonic-gate 	}
27570Sstevel@tonic-gate 	/*
27580Sstevel@tonic-gate 	 * Disabling pools, so we should start using the global values
27590Sstevel@tonic-gate 	 * for ncpus and ncpus_online.
27600Sstevel@tonic-gate 	 */
27610Sstevel@tonic-gate 	if (newpsetid == ZONE_PS_INVAL) {
27620Sstevel@tonic-gate 		zone->zone_ncpus = 0;
27630Sstevel@tonic-gate 		zone->zone_ncpus_online = 0;
27640Sstevel@tonic-gate 	}
27650Sstevel@tonic-gate }
27660Sstevel@tonic-gate 
27670Sstevel@tonic-gate /*
27680Sstevel@tonic-gate  * Walk the list of active zones and issue the provided callback for
27690Sstevel@tonic-gate  * each of them.
27700Sstevel@tonic-gate  *
27710Sstevel@tonic-gate  * Caller must not be holding any locks that may be acquired under
27720Sstevel@tonic-gate  * zonehash_lock.  See comment at the beginning of the file for a list of
27730Sstevel@tonic-gate  * common locks and their interactions with zones.
27740Sstevel@tonic-gate  */
27750Sstevel@tonic-gate int
27760Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data)
27770Sstevel@tonic-gate {
27780Sstevel@tonic-gate 	zone_t *zone;
27790Sstevel@tonic-gate 	int ret = 0;
27800Sstevel@tonic-gate 	zone_status_t status;
27810Sstevel@tonic-gate 
27820Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
27830Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
27840Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
27850Sstevel@tonic-gate 		/*
27860Sstevel@tonic-gate 		 * Skip zones that shouldn't be externally visible.
27870Sstevel@tonic-gate 		 */
27880Sstevel@tonic-gate 		status = zone_status_get(zone);
27890Sstevel@tonic-gate 		if (status < ZONE_IS_READY || status > ZONE_IS_DOWN)
27900Sstevel@tonic-gate 			continue;
27910Sstevel@tonic-gate 		/*
27920Sstevel@tonic-gate 		 * Bail immediately if any callback invocation returns a
27930Sstevel@tonic-gate 		 * non-zero value.
27940Sstevel@tonic-gate 		 */
27950Sstevel@tonic-gate 		ret = (*cb)(zone, data);
27960Sstevel@tonic-gate 		if (ret != 0)
27970Sstevel@tonic-gate 			break;
27980Sstevel@tonic-gate 	}
27990Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
28000Sstevel@tonic-gate 	return (ret);
28010Sstevel@tonic-gate }
28020Sstevel@tonic-gate 
28030Sstevel@tonic-gate static int
28040Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath)
28050Sstevel@tonic-gate {
28060Sstevel@tonic-gate 	vnode_t *vp;
28070Sstevel@tonic-gate 	int trycount;
28080Sstevel@tonic-gate 	int error = 0;
28090Sstevel@tonic-gate 	char *path;
28100Sstevel@tonic-gate 	struct pathname upn, pn;
28110Sstevel@tonic-gate 	size_t pathlen;
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 	if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0)
28140Sstevel@tonic-gate 		return (error);
28150Sstevel@tonic-gate 
28160Sstevel@tonic-gate 	pn_alloc(&pn);
28170Sstevel@tonic-gate 
28180Sstevel@tonic-gate 	/* prevent infinite loop */
28190Sstevel@tonic-gate 	trycount = 10;
28200Sstevel@tonic-gate 	for (;;) {
28210Sstevel@tonic-gate 		if (--trycount <= 0) {
28220Sstevel@tonic-gate 			error = ESTALE;
28230Sstevel@tonic-gate 			goto out;
28240Sstevel@tonic-gate 		}
28250Sstevel@tonic-gate 
28260Sstevel@tonic-gate 		if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) {
28270Sstevel@tonic-gate 			/*
28280Sstevel@tonic-gate 			 * VOP_ACCESS() may cover 'vp' with a new
28290Sstevel@tonic-gate 			 * filesystem, if 'vp' is an autoFS vnode.
28300Sstevel@tonic-gate 			 * Get the new 'vp' if so.
28310Sstevel@tonic-gate 			 */
28325331Samw 			if ((error =
28335331Samw 			    VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL)) == 0 &&
28344417Seh208807 			    (!vn_ismntpt(vp) ||
28350Sstevel@tonic-gate 			    (error = traverse(&vp)) == 0)) {
28360Sstevel@tonic-gate 				pathlen = pn.pn_pathlen + 2;
28370Sstevel@tonic-gate 				path = kmem_alloc(pathlen, KM_SLEEP);
28380Sstevel@tonic-gate 				(void) strncpy(path, pn.pn_path,
28390Sstevel@tonic-gate 				    pn.pn_pathlen + 1);
28400Sstevel@tonic-gate 				path[pathlen - 2] = '/';
28410Sstevel@tonic-gate 				path[pathlen - 1] = '\0';
28420Sstevel@tonic-gate 				pn_free(&pn);
28430Sstevel@tonic-gate 				pn_free(&upn);
28440Sstevel@tonic-gate 
28450Sstevel@tonic-gate 				/* Success! */
28460Sstevel@tonic-gate 				break;
28470Sstevel@tonic-gate 			}
28480Sstevel@tonic-gate 			VN_RELE(vp);
28490Sstevel@tonic-gate 		}
28500Sstevel@tonic-gate 		if (error != ESTALE)
28510Sstevel@tonic-gate 			goto out;
28520Sstevel@tonic-gate 	}
28530Sstevel@tonic-gate 
28540Sstevel@tonic-gate 	ASSERT(error == 0);
28550Sstevel@tonic-gate 	zone->zone_rootvp = vp;		/* we hold a reference to vp */
28560Sstevel@tonic-gate 	zone->zone_rootpath = path;
28570Sstevel@tonic-gate 	zone->zone_rootpathlen = pathlen;
28581769Scarlsonj 	if (pathlen > 5 && strcmp(path + pathlen - 5, "/lu/") == 0)
28591769Scarlsonj 		zone->zone_flags |= ZF_IS_SCRATCH;
28600Sstevel@tonic-gate 	return (0);
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate out:
28630Sstevel@tonic-gate 	pn_free(&pn);
28640Sstevel@tonic-gate 	pn_free(&upn);
28650Sstevel@tonic-gate 	return (error);
28660Sstevel@tonic-gate }
28670Sstevel@tonic-gate 
28680Sstevel@tonic-gate #define	isalnum(c)	(((c) >= '0' && (c) <= '9') || \
28690Sstevel@tonic-gate 			((c) >= 'a' && (c) <= 'z') || \
28700Sstevel@tonic-gate 			((c) >= 'A' && (c) <= 'Z'))
28710Sstevel@tonic-gate 
28720Sstevel@tonic-gate static int
28730Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname)
28740Sstevel@tonic-gate {
28750Sstevel@tonic-gate 	char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
28760Sstevel@tonic-gate 	size_t len;
28770Sstevel@tonic-gate 	int i, err;
28780Sstevel@tonic-gate 
28790Sstevel@tonic-gate 	if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) {
28800Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
28810Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
28820Sstevel@tonic-gate 	}
28830Sstevel@tonic-gate 
28840Sstevel@tonic-gate 	/* must be less than ZONENAME_MAX */
28850Sstevel@tonic-gate 	if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') {
28860Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
28870Sstevel@tonic-gate 		return (EINVAL);
28880Sstevel@tonic-gate 	}
28890Sstevel@tonic-gate 
28900Sstevel@tonic-gate 	/*
28910Sstevel@tonic-gate 	 * Name must start with an alphanumeric and must contain only
28920Sstevel@tonic-gate 	 * alphanumerics, '-', '_' and '.'.
28930Sstevel@tonic-gate 	 */
28940Sstevel@tonic-gate 	if (!isalnum(kname[0])) {
28950Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
28960Sstevel@tonic-gate 		return (EINVAL);
28970Sstevel@tonic-gate 	}
28980Sstevel@tonic-gate 	for (i = 1; i < len - 1; i++) {
28990Sstevel@tonic-gate 		if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' &&
29000Sstevel@tonic-gate 		    kname[i] != '.') {
29010Sstevel@tonic-gate 			kmem_free(kname, ZONENAME_MAX);
29020Sstevel@tonic-gate 			return (EINVAL);
29030Sstevel@tonic-gate 		}
29040Sstevel@tonic-gate 	}
29050Sstevel@tonic-gate 
29060Sstevel@tonic-gate 	zone->zone_name = kname;
29070Sstevel@tonic-gate 	return (0);
29080Sstevel@tonic-gate }
29090Sstevel@tonic-gate 
29100Sstevel@tonic-gate /*
29110Sstevel@tonic-gate  * Similar to thread_create(), but makes sure the thread is in the appropriate
29120Sstevel@tonic-gate  * zone's zsched process (curproc->p_zone->zone_zsched) before returning.
29130Sstevel@tonic-gate  */
29140Sstevel@tonic-gate /*ARGSUSED*/
29150Sstevel@tonic-gate kthread_t *
29160Sstevel@tonic-gate zthread_create(
29170Sstevel@tonic-gate     caddr_t stk,
29180Sstevel@tonic-gate     size_t stksize,
29190Sstevel@tonic-gate     void (*proc)(),
29200Sstevel@tonic-gate     void *arg,
29210Sstevel@tonic-gate     size_t len,
29220Sstevel@tonic-gate     pri_t pri)
29230Sstevel@tonic-gate {
29240Sstevel@tonic-gate 	kthread_t *t;
29250Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
29260Sstevel@tonic-gate 	proc_t *pp = zone->zone_zsched;
29270Sstevel@tonic-gate 
29280Sstevel@tonic-gate 	zone_hold(zone);	/* Reference to be dropped when thread exits */
29290Sstevel@tonic-gate 
29300Sstevel@tonic-gate 	/*
29310Sstevel@tonic-gate 	 * No-one should be trying to create threads if the zone is shutting
29320Sstevel@tonic-gate 	 * down and there aren't any kernel threads around.  See comment
29330Sstevel@tonic-gate 	 * in zthread_exit().
29340Sstevel@tonic-gate 	 */
29350Sstevel@tonic-gate 	ASSERT(!(zone->zone_kthreads == NULL &&
29360Sstevel@tonic-gate 	    zone_status_get(zone) >= ZONE_IS_EMPTY));
29370Sstevel@tonic-gate 	/*
29380Sstevel@tonic-gate 	 * Create a thread, but don't let it run until we've finished setting
29390Sstevel@tonic-gate 	 * things up.
29400Sstevel@tonic-gate 	 */
29410Sstevel@tonic-gate 	t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri);
29420Sstevel@tonic-gate 	ASSERT(t->t_forw == NULL);
29430Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
29440Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL) {
29450Sstevel@tonic-gate 		t->t_forw = t->t_back = t;
29460Sstevel@tonic-gate 	} else {
29470Sstevel@tonic-gate 		kthread_t *tx = zone->zone_kthreads;
29480Sstevel@tonic-gate 
29490Sstevel@tonic-gate 		t->t_forw = tx;
29500Sstevel@tonic-gate 		t->t_back = tx->t_back;
29510Sstevel@tonic-gate 		tx->t_back->t_forw = t;
29520Sstevel@tonic-gate 		tx->t_back = t;
29530Sstevel@tonic-gate 	}
29540Sstevel@tonic-gate 	zone->zone_kthreads = t;
29550Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
29580Sstevel@tonic-gate 	t->t_proc_flag |= TP_ZTHREAD;
29590Sstevel@tonic-gate 	project_rele(t->t_proj);
29600Sstevel@tonic-gate 	t->t_proj = project_hold(pp->p_task->tk_proj);
29610Sstevel@tonic-gate 
29620Sstevel@tonic-gate 	/*
29630Sstevel@tonic-gate 	 * Setup complete, let it run.
29640Sstevel@tonic-gate 	 */
29650Sstevel@tonic-gate 	thread_lock(t);
29660Sstevel@tonic-gate 	t->t_schedflag |= TS_ALLSTART;
29670Sstevel@tonic-gate 	setrun_locked(t);
29680Sstevel@tonic-gate 	thread_unlock(t);
29690Sstevel@tonic-gate 
29700Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
29710Sstevel@tonic-gate 
29720Sstevel@tonic-gate 	return (t);
29730Sstevel@tonic-gate }
29740Sstevel@tonic-gate 
29750Sstevel@tonic-gate /*
29760Sstevel@tonic-gate  * Similar to thread_exit().  Must be called by threads created via
29770Sstevel@tonic-gate  * zthread_exit().
29780Sstevel@tonic-gate  */
29790Sstevel@tonic-gate void
29800Sstevel@tonic-gate zthread_exit(void)
29810Sstevel@tonic-gate {
29820Sstevel@tonic-gate 	kthread_t *t = curthread;
29830Sstevel@tonic-gate 	proc_t *pp = curproc;
29840Sstevel@tonic-gate 	zone_t *zone = pp->p_zone;
29850Sstevel@tonic-gate 
29860Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
29870Sstevel@tonic-gate 
29880Sstevel@tonic-gate 	/*
29890Sstevel@tonic-gate 	 * Reparent to p0
29900Sstevel@tonic-gate 	 */
29911075Sjosephb 	kpreempt_disable();
29920Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
29930Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_ZTHREAD;
29940Sstevel@tonic-gate 	t->t_procp = &p0;
29950Sstevel@tonic-gate 	hat_thread_exit(t);
29960Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
29971075Sjosephb 	kpreempt_enable();
29980Sstevel@tonic-gate 
29990Sstevel@tonic-gate 	if (t->t_back == t) {
30000Sstevel@tonic-gate 		ASSERT(t->t_forw == t);
30010Sstevel@tonic-gate 		/*
30020Sstevel@tonic-gate 		 * If the zone is empty, once the thread count
30030Sstevel@tonic-gate 		 * goes to zero no further kernel threads can be
30040Sstevel@tonic-gate 		 * created.  This is because if the creator is a process
30050Sstevel@tonic-gate 		 * in the zone, then it must have exited before the zone
30060Sstevel@tonic-gate 		 * state could be set to ZONE_IS_EMPTY.
30070Sstevel@tonic-gate 		 * Otherwise, if the creator is a kernel thread in the
30080Sstevel@tonic-gate 		 * zone, the thread count is non-zero.
30090Sstevel@tonic-gate 		 *
30100Sstevel@tonic-gate 		 * This really means that non-zone kernel threads should
30110Sstevel@tonic-gate 		 * not create zone kernel threads.
30120Sstevel@tonic-gate 		 */
30130Sstevel@tonic-gate 		zone->zone_kthreads = NULL;
30140Sstevel@tonic-gate 		if (zone_status_get(zone) == ZONE_IS_EMPTY) {
30150Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_DOWN);
30163792Sakolb 			/*
30173792Sakolb 			 * Remove any CPU caps on this zone.
30183792Sakolb 			 */
30193792Sakolb 			cpucaps_zone_remove(zone);
30200Sstevel@tonic-gate 		}
30210Sstevel@tonic-gate 	} else {
30220Sstevel@tonic-gate 		t->t_forw->t_back = t->t_back;
30230Sstevel@tonic-gate 		t->t_back->t_forw = t->t_forw;
30240Sstevel@tonic-gate 		if (zone->zone_kthreads == t)
30250Sstevel@tonic-gate 			zone->zone_kthreads = t->t_forw;
30260Sstevel@tonic-gate 	}
30270Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
30280Sstevel@tonic-gate 	zone_rele(zone);
30290Sstevel@tonic-gate 	thread_exit();
30300Sstevel@tonic-gate 	/* NOTREACHED */
30310Sstevel@tonic-gate }
30320Sstevel@tonic-gate 
30330Sstevel@tonic-gate static void
30340Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp)
30350Sstevel@tonic-gate {
30360Sstevel@tonic-gate 	vnode_t *oldvp;
30370Sstevel@tonic-gate 
30380Sstevel@tonic-gate 	/* we're going to hold a reference here to the directory */
30390Sstevel@tonic-gate 	VN_HOLD(vp);
30400Sstevel@tonic-gate 
30410Sstevel@tonic-gate 	if (audit_active)	/* update abs cwd/root path see c2audit.c */
30420Sstevel@tonic-gate 		audit_chdirec(vp, vpp);
30430Sstevel@tonic-gate 
30440Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
30450Sstevel@tonic-gate 	oldvp = *vpp;
30460Sstevel@tonic-gate 	*vpp = vp;
30470Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
30480Sstevel@tonic-gate 	if (oldvp != NULL)
30490Sstevel@tonic-gate 		VN_RELE(oldvp);
30500Sstevel@tonic-gate }
30510Sstevel@tonic-gate 
30520Sstevel@tonic-gate /*
30530Sstevel@tonic-gate  * Convert an rctl value represented by an nvlist_t into an rctl_val_t.
30540Sstevel@tonic-gate  */
30550Sstevel@tonic-gate static int
30560Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv)
30570Sstevel@tonic-gate {
30580Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
30590Sstevel@tonic-gate 	boolean_t priv_set = B_FALSE;
30600Sstevel@tonic-gate 	boolean_t limit_set = B_FALSE;
30610Sstevel@tonic-gate 	boolean_t action_set = B_FALSE;
30620Sstevel@tonic-gate 
30630Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
30640Sstevel@tonic-gate 		const char *name;
30650Sstevel@tonic-gate 		uint64_t ui64;
30660Sstevel@tonic-gate 
30670Sstevel@tonic-gate 		name = nvpair_name(nvp);
30680Sstevel@tonic-gate 		if (nvpair_type(nvp) != DATA_TYPE_UINT64)
30690Sstevel@tonic-gate 			return (EINVAL);
30700Sstevel@tonic-gate 		(void) nvpair_value_uint64(nvp, &ui64);
30710Sstevel@tonic-gate 		if (strcmp(name, "privilege") == 0) {
30720Sstevel@tonic-gate 			/*
30730Sstevel@tonic-gate 			 * Currently only privileged values are allowed, but
30740Sstevel@tonic-gate 			 * this may change in the future.
30750Sstevel@tonic-gate 			 */
30760Sstevel@tonic-gate 			if (ui64 != RCPRIV_PRIVILEGED)
30770Sstevel@tonic-gate 				return (EINVAL);
30780Sstevel@tonic-gate 			rv->rcv_privilege = ui64;
30790Sstevel@tonic-gate 			priv_set = B_TRUE;
30800Sstevel@tonic-gate 		} else if (strcmp(name, "limit") == 0) {
30810Sstevel@tonic-gate 			rv->rcv_value = ui64;
30820Sstevel@tonic-gate 			limit_set = B_TRUE;
30830Sstevel@tonic-gate 		} else if (strcmp(name, "action") == 0) {
30840Sstevel@tonic-gate 			if (ui64 != RCTL_LOCAL_NOACTION &&
30850Sstevel@tonic-gate 			    ui64 != RCTL_LOCAL_DENY)
30860Sstevel@tonic-gate 				return (EINVAL);
30870Sstevel@tonic-gate 			rv->rcv_flagaction = ui64;
30880Sstevel@tonic-gate 			action_set = B_TRUE;
30890Sstevel@tonic-gate 		} else {
30900Sstevel@tonic-gate 			return (EINVAL);
30910Sstevel@tonic-gate 		}
30920Sstevel@tonic-gate 	}
30930Sstevel@tonic-gate 
30940Sstevel@tonic-gate 	if (!(priv_set && limit_set && action_set))
30950Sstevel@tonic-gate 		return (EINVAL);
30960Sstevel@tonic-gate 	rv->rcv_action_signal = 0;
30970Sstevel@tonic-gate 	rv->rcv_action_recipient = NULL;
30980Sstevel@tonic-gate 	rv->rcv_action_recip_pid = -1;
30990Sstevel@tonic-gate 	rv->rcv_firing_time = 0;
31000Sstevel@tonic-gate 
31010Sstevel@tonic-gate 	return (0);
31020Sstevel@tonic-gate }
31030Sstevel@tonic-gate 
31042267Sdp /*
31052267Sdp  * Non-global zone version of start_init.
31062267Sdp  */
31070Sstevel@tonic-gate void
31082267Sdp zone_start_init(void)
31090Sstevel@tonic-gate {
31100Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
31112712Snn35248 	zone_t *z = p->p_zone;
31122267Sdp 
31132267Sdp 	ASSERT(!INGLOBALZONE(curproc));
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 	/*
31162712Snn35248 	 * For all purposes (ZONE_ATTR_INITPID and restart_init),
31172712Snn35248 	 * storing just the pid of init is sufficient.
31182712Snn35248 	 */
31192712Snn35248 	z->zone_proc_initpid = p->p_pid;
31202712Snn35248 
31212712Snn35248 	/*
31222267Sdp 	 * We maintain zone_boot_err so that we can return the cause of the
31232267Sdp 	 * failure back to the caller of the zone_boot syscall.
31240Sstevel@tonic-gate 	 */
31252267Sdp 	p->p_zone->zone_boot_err = start_init_common();
31260Sstevel@tonic-gate 
31270Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
31282712Snn35248 	if (z->zone_boot_err != 0) {
31290Sstevel@tonic-gate 		/*
31300Sstevel@tonic-gate 		 * Make sure we are still in the booting state-- we could have
31310Sstevel@tonic-gate 		 * raced and already be shutting down, or even further along.
31320Sstevel@tonic-gate 		 */
31333792Sakolb 		if (zone_status_get(z) == ZONE_IS_BOOTING) {
31342712Snn35248 			zone_status_set(z, ZONE_IS_SHUTTING_DOWN);
31353792Sakolb 		}
31360Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
31370Sstevel@tonic-gate 		/* It's gone bad, dispose of the process */
31382712Snn35248 		if (proc_exit(CLD_EXITED, z->zone_boot_err) != 0) {
3139390Sraf 			mutex_enter(&p->p_lock);
3140390Sraf 			ASSERT(p->p_flag & SEXITLWPS);
31410Sstevel@tonic-gate 			lwp_exit();
31420Sstevel@tonic-gate 		}
31430Sstevel@tonic-gate 	} else {
31442712Snn35248 		if (zone_status_get(z) == ZONE_IS_BOOTING)
31452712Snn35248 			zone_status_set(z, ZONE_IS_RUNNING);
31460Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
31470Sstevel@tonic-gate 		/* cause the process to return to userland. */
31480Sstevel@tonic-gate 		lwp_rtt();
31490Sstevel@tonic-gate 	}
31500Sstevel@tonic-gate }
31510Sstevel@tonic-gate 
31520Sstevel@tonic-gate struct zsched_arg {
31530Sstevel@tonic-gate 	zone_t *zone;
31540Sstevel@tonic-gate 	nvlist_t *nvlist;
31550Sstevel@tonic-gate };
31560Sstevel@tonic-gate 
31570Sstevel@tonic-gate /*
31580Sstevel@tonic-gate  * Per-zone "sched" workalike.  The similarity to "sched" doesn't have
31590Sstevel@tonic-gate  * anything to do with scheduling, but rather with the fact that
31600Sstevel@tonic-gate  * per-zone kernel threads are parented to zsched, just like regular
31610Sstevel@tonic-gate  * kernel threads are parented to sched (p0).
31620Sstevel@tonic-gate  *
31630Sstevel@tonic-gate  * zsched is also responsible for launching init for the zone.
31640Sstevel@tonic-gate  */
31650Sstevel@tonic-gate static void
31660Sstevel@tonic-gate zsched(void *arg)
31670Sstevel@tonic-gate {
31680Sstevel@tonic-gate 	struct zsched_arg *za = arg;
31690Sstevel@tonic-gate 	proc_t *pp = curproc;
31700Sstevel@tonic-gate 	proc_t *initp = proc_init;
31710Sstevel@tonic-gate 	zone_t *zone = za->zone;
31720Sstevel@tonic-gate 	cred_t *cr, *oldcred;
31730Sstevel@tonic-gate 	rctl_set_t *set;
31740Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
31750Sstevel@tonic-gate 	contract_t *ct = NULL;
31760Sstevel@tonic-gate 	task_t *tk, *oldtk;
31770Sstevel@tonic-gate 	rctl_entity_p_t e;
31780Sstevel@tonic-gate 	kproject_t *pj;
31790Sstevel@tonic-gate 
31800Sstevel@tonic-gate 	nvlist_t *nvl = za->nvlist;
31810Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
31820Sstevel@tonic-gate 
31833446Smrj 	bcopy("zsched", PTOU(pp)->u_psargs, sizeof ("zsched"));
31843446Smrj 	bcopy("zsched", PTOU(pp)->u_comm, sizeof ("zsched"));
31853446Smrj 	PTOU(pp)->u_argc = 0;
31863446Smrj 	PTOU(pp)->u_argv = NULL;
31873446Smrj 	PTOU(pp)->u_envp = NULL;
31880Sstevel@tonic-gate 	closeall(P_FINFO(pp));
31890Sstevel@tonic-gate 
31900Sstevel@tonic-gate 	/*
31910Sstevel@tonic-gate 	 * We are this zone's "zsched" process.  As the zone isn't generally
31920Sstevel@tonic-gate 	 * visible yet we don't need to grab any locks before initializing its
31930Sstevel@tonic-gate 	 * zone_proc pointer.
31940Sstevel@tonic-gate 	 */
31950Sstevel@tonic-gate 	zone_hold(zone);  /* this hold is released by zone_destroy() */
31960Sstevel@tonic-gate 	zone->zone_zsched = pp;
31970Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
31980Sstevel@tonic-gate 	pp->p_zone = zone;
31990Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
32000Sstevel@tonic-gate 
32010Sstevel@tonic-gate 	/*
32020Sstevel@tonic-gate 	 * Disassociate process from its 'parent'; parent ourselves to init
32030Sstevel@tonic-gate 	 * (pid 1) and change other values as needed.
32040Sstevel@tonic-gate 	 */
32050Sstevel@tonic-gate 	sess_create();
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate 	mutex_enter(&pidlock);
32080Sstevel@tonic-gate 	proc_detach(pp);
32090Sstevel@tonic-gate 	pp->p_ppid = 1;
32100Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
32110Sstevel@tonic-gate 	pp->p_ancpid = 1;
32120Sstevel@tonic-gate 	pp->p_parent = initp;
32130Sstevel@tonic-gate 	pp->p_psibling = NULL;
32140Sstevel@tonic-gate 	if (initp->p_child)
32150Sstevel@tonic-gate 		initp->p_child->p_psibling = pp;
32160Sstevel@tonic-gate 	pp->p_sibling = initp->p_child;
32170Sstevel@tonic-gate 	initp->p_child = pp;
32180Sstevel@tonic-gate 
32190Sstevel@tonic-gate 	/* Decrement what newproc() incremented. */
32200Sstevel@tonic-gate 	upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID);
32210Sstevel@tonic-gate 	/*
32220Sstevel@tonic-gate 	 * Our credentials are about to become kcred-like, so we don't care
32230Sstevel@tonic-gate 	 * about the caller's ruid.
32240Sstevel@tonic-gate 	 */
32250Sstevel@tonic-gate 	upcount_inc(crgetruid(kcred), zone->zone_id);
32260Sstevel@tonic-gate 	mutex_exit(&pidlock);
32270Sstevel@tonic-gate 
32280Sstevel@tonic-gate 	/*
32290Sstevel@tonic-gate 	 * getting out of global zone, so decrement lwp counts
32300Sstevel@tonic-gate 	 */
32310Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
32320Sstevel@tonic-gate 	mutex_enter(&global_zone->zone_nlwps_lock);
32330Sstevel@tonic-gate 	pj->kpj_nlwps -= pp->p_lwpcnt;
32340Sstevel@tonic-gate 	global_zone->zone_nlwps -= pp->p_lwpcnt;
32350Sstevel@tonic-gate 	mutex_exit(&global_zone->zone_nlwps_lock);
32360Sstevel@tonic-gate 
32370Sstevel@tonic-gate 	/*
32382768Ssl108498 	 * Decrement locked memory counts on old zone and project.
32392768Ssl108498 	 */
32403247Sgjelinek 	mutex_enter(&global_zone->zone_mem_lock);
32412768Ssl108498 	global_zone->zone_locked_mem -= pp->p_locked_mem;
32422768Ssl108498 	pj->kpj_data.kpd_locked_mem -= pp->p_locked_mem;
32433247Sgjelinek 	mutex_exit(&global_zone->zone_mem_lock);
32442768Ssl108498 
32452768Ssl108498 	/*
32460Sstevel@tonic-gate 	 * Create and join a new task in project '0' of this zone.
32470Sstevel@tonic-gate 	 *
32480Sstevel@tonic-gate 	 * We don't need to call holdlwps() since we know we're the only lwp in
32490Sstevel@tonic-gate 	 * this process.
32500Sstevel@tonic-gate 	 *
32510Sstevel@tonic-gate 	 * task_join() returns with p_lock held.
32520Sstevel@tonic-gate 	 */
32530Sstevel@tonic-gate 	tk = task_create(0, zone);
32540Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
32550Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
32562768Ssl108498 
32572768Ssl108498 	pj = pp->p_task->tk_proj;
32582768Ssl108498 
32593247Sgjelinek 	mutex_enter(&zone->zone_mem_lock);
32602768Ssl108498 	zone->zone_locked_mem += pp->p_locked_mem;
32612768Ssl108498 	pj->kpj_data.kpd_locked_mem += pp->p_locked_mem;
32623247Sgjelinek 	mutex_exit(&zone->zone_mem_lock);
32630Sstevel@tonic-gate 
32640Sstevel@tonic-gate 	/*
32650Sstevel@tonic-gate 	 * add lwp counts to zsched's zone, and increment project's task count
32660Sstevel@tonic-gate 	 * due to the task created in the above tasksys_settaskid
32670Sstevel@tonic-gate 	 */
32682768Ssl108498 
32690Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
32700Sstevel@tonic-gate 	pj->kpj_nlwps += pp->p_lwpcnt;
32710Sstevel@tonic-gate 	pj->kpj_ntasks += 1;
32720Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
32730Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
32740Sstevel@tonic-gate 
32752768Ssl108498 	mutex_exit(&curproc->p_lock);
32762768Ssl108498 	mutex_exit(&cpu_lock);
32772768Ssl108498 	task_rele(oldtk);
32782768Ssl108498 
32790Sstevel@tonic-gate 	/*
32800Sstevel@tonic-gate 	 * The process was created by a process in the global zone, hence the
32810Sstevel@tonic-gate 	 * credentials are wrong.  We might as well have kcred-ish credentials.
32820Sstevel@tonic-gate 	 */
32830Sstevel@tonic-gate 	cr = zone->zone_kcred;
32840Sstevel@tonic-gate 	crhold(cr);
32850Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
32860Sstevel@tonic-gate 	oldcred = pp->p_cred;
32870Sstevel@tonic-gate 	pp->p_cred = cr;
32880Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
32890Sstevel@tonic-gate 	crfree(oldcred);
32900Sstevel@tonic-gate 
32910Sstevel@tonic-gate 	/*
32920Sstevel@tonic-gate 	 * Hold credentials again (for thread)
32930Sstevel@tonic-gate 	 */
32940Sstevel@tonic-gate 	crhold(cr);
32950Sstevel@tonic-gate 
32960Sstevel@tonic-gate 	/*
32970Sstevel@tonic-gate 	 * p_lwpcnt can't change since this is a kernel process.
32980Sstevel@tonic-gate 	 */
32990Sstevel@tonic-gate 	crset(pp, cr);
33000Sstevel@tonic-gate 
33010Sstevel@tonic-gate 	/*
33020Sstevel@tonic-gate 	 * Chroot
33030Sstevel@tonic-gate 	 */
33040Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp);
33050Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp);
33060Sstevel@tonic-gate 
33070Sstevel@tonic-gate 	/*
33080Sstevel@tonic-gate 	 * Initialize zone's rctl set.
33090Sstevel@tonic-gate 	 */
33100Sstevel@tonic-gate 	set = rctl_set_create();
33110Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
33120Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
33130Sstevel@tonic-gate 	e.rcep_p.zone = zone;
33140Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
33150Sstevel@tonic-gate 	zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp);
33160Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
33170Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
33180Sstevel@tonic-gate 
33190Sstevel@tonic-gate 	/*
33200Sstevel@tonic-gate 	 * Apply the rctls passed in to zone_create().  This is basically a list
33210Sstevel@tonic-gate 	 * assignment: all of the old values are removed and the new ones
33220Sstevel@tonic-gate 	 * inserted.  That is, if an empty list is passed in, all values are
33230Sstevel@tonic-gate 	 * removed.
33240Sstevel@tonic-gate 	 */
33250Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
33260Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
33270Sstevel@tonic-gate 		rctl_hndl_t hndl;
33280Sstevel@tonic-gate 		char *name;
33290Sstevel@tonic-gate 		nvlist_t **nvlarray;
33300Sstevel@tonic-gate 		uint_t i, nelem;
33310Sstevel@tonic-gate 		int error;	/* For ASSERT()s */
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate 		name = nvpair_name(nvp);
33340Sstevel@tonic-gate 		hndl = rctl_hndl_lookup(name);
33350Sstevel@tonic-gate 		ASSERT(hndl != -1);
33360Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
33370Sstevel@tonic-gate 		ASSERT(rde != NULL);
33380Sstevel@tonic-gate 
33390Sstevel@tonic-gate 		for (; /* ever */; ) {
33400Sstevel@tonic-gate 			rctl_val_t oval;
33410Sstevel@tonic-gate 
33420Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
33430Sstevel@tonic-gate 			error = rctl_local_get(hndl, NULL, &oval, pp);
33440Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
33450Sstevel@tonic-gate 			ASSERT(error == 0);	/* Can't fail for RCTL_FIRST */
33460Sstevel@tonic-gate 			ASSERT(oval.rcv_privilege != RCPRIV_BASIC);
33470Sstevel@tonic-gate 			if (oval.rcv_privilege == RCPRIV_SYSTEM)
33480Sstevel@tonic-gate 				break;
33490Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
33500Sstevel@tonic-gate 			error = rctl_local_delete(hndl, &oval, pp);
33510Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
33520Sstevel@tonic-gate 			ASSERT(error == 0);
33530Sstevel@tonic-gate 		}
33540Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
33550Sstevel@tonic-gate 		ASSERT(error == 0);
33560Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
33570Sstevel@tonic-gate 			rctl_val_t *nvalp;
33580Sstevel@tonic-gate 
33590Sstevel@tonic-gate 			nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
33600Sstevel@tonic-gate 			error = nvlist2rctlval(nvlarray[i], nvalp);
33610Sstevel@tonic-gate 			ASSERT(error == 0);
33620Sstevel@tonic-gate 			/*
33630Sstevel@tonic-gate 			 * rctl_local_insert can fail if the value being
33640Sstevel@tonic-gate 			 * inserted is a duplicate; this is OK.
33650Sstevel@tonic-gate 			 */
33660Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
33670Sstevel@tonic-gate 			if (rctl_local_insert(hndl, nvalp, pp) != 0)
33680Sstevel@tonic-gate 				kmem_cache_free(rctl_val_cache, nvalp);
33690Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
33700Sstevel@tonic-gate 		}
33710Sstevel@tonic-gate 	}
33720Sstevel@tonic-gate 	/*
33730Sstevel@tonic-gate 	 * Tell the world that we're done setting up.
33740Sstevel@tonic-gate 	 *
33755880Snordmark 	 * At this point we want to set the zone status to ZONE_IS_INITIALIZED
33760Sstevel@tonic-gate 	 * and atomically set the zone's processor set visibility.  Once
33770Sstevel@tonic-gate 	 * we drop pool_lock() this zone will automatically get updated
33780Sstevel@tonic-gate 	 * to reflect any future changes to the pools configuration.
33795880Snordmark 	 *
33805880Snordmark 	 * Note that after we drop the locks below (zonehash_lock in
33815880Snordmark 	 * particular) other operations such as a zone_getattr call can
33825880Snordmark 	 * now proceed and observe the zone. That is the reason for doing a
33835880Snordmark 	 * state transition to the INITIALIZED state.
33840Sstevel@tonic-gate 	 */
33850Sstevel@tonic-gate 	pool_lock();
33860Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
33870Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
33880Sstevel@tonic-gate 	zone_uniqid(zone);
33890Sstevel@tonic-gate 	zone_zsd_configure(zone);
33900Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED)
33910Sstevel@tonic-gate 		zone_pset_set(zone, pool_default->pool_pset->pset_id);
33920Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
33930Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
33945880Snordmark 	zone_status_set(zone, ZONE_IS_INITIALIZED);
33950Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
33960Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
33970Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
33980Sstevel@tonic-gate 	pool_unlock();
33990Sstevel@tonic-gate 
34005880Snordmark 	/* Now call the create callback for this key */
34015880Snordmark 	zsd_apply_all_keys(zsd_apply_create, zone);
34025880Snordmark 
34035880Snordmark 	/* The callbacks are complete. Mark ZONE_IS_READY */
34045880Snordmark 	mutex_enter(&zone_status_lock);
34055880Snordmark 	ASSERT(zone_status_get(zone) == ZONE_IS_INITIALIZED);
34065880Snordmark 	zone_status_set(zone, ZONE_IS_READY);
34075880Snordmark 	mutex_exit(&zone_status_lock);
34085880Snordmark 
34090Sstevel@tonic-gate 	/*
34100Sstevel@tonic-gate 	 * Once we see the zone transition to the ZONE_IS_BOOTING state,
34110Sstevel@tonic-gate 	 * we launch init, and set the state to running.
34120Sstevel@tonic-gate 	 */
34130Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched");
34140Sstevel@tonic-gate 
34150Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_BOOTING) {
34160Sstevel@tonic-gate 		id_t cid;
34170Sstevel@tonic-gate 
34180Sstevel@tonic-gate 		/*
34190Sstevel@tonic-gate 		 * Ok, this is a little complicated.  We need to grab the
34200Sstevel@tonic-gate 		 * zone's pool's scheduling class ID; note that by now, we
34210Sstevel@tonic-gate 		 * are already bound to a pool if we need to be (zoneadmd
34220Sstevel@tonic-gate 		 * will have done that to us while we're in the READY
34230Sstevel@tonic-gate 		 * state).  *But* the scheduling class for the zone's 'init'
34240Sstevel@tonic-gate 		 * must be explicitly passed to newproc, which doesn't
34250Sstevel@tonic-gate 		 * respect pool bindings.
34260Sstevel@tonic-gate 		 *
34270Sstevel@tonic-gate 		 * We hold the pool_lock across the call to newproc() to
34280Sstevel@tonic-gate 		 * close the obvious race: the pool's scheduling class
34290Sstevel@tonic-gate 		 * could change before we manage to create the LWP with
34300Sstevel@tonic-gate 		 * classid 'cid'.
34310Sstevel@tonic-gate 		 */
34320Sstevel@tonic-gate 		pool_lock();
34333247Sgjelinek 		if (zone->zone_defaultcid > 0)
34343247Sgjelinek 			cid = zone->zone_defaultcid;
34353247Sgjelinek 		else
34363247Sgjelinek 			cid = pool_get_class(zone->zone_pool);
34370Sstevel@tonic-gate 		if (cid == -1)
34380Sstevel@tonic-gate 			cid = defaultcid;
34390Sstevel@tonic-gate 
34400Sstevel@tonic-gate 		/*
34410Sstevel@tonic-gate 		 * If this fails, zone_boot will ultimately fail.  The
34420Sstevel@tonic-gate 		 * state of the zone will be set to SHUTTING_DOWN-- userland
34430Sstevel@tonic-gate 		 * will have to tear down the zone, and fail, or try again.
34440Sstevel@tonic-gate 		 */
34452267Sdp 		if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid,
34460Sstevel@tonic-gate 		    minclsyspri - 1, &ct)) != 0) {
34470Sstevel@tonic-gate 			mutex_enter(&zone_status_lock);
34480Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
34490Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
34500Sstevel@tonic-gate 		}
34510Sstevel@tonic-gate 		pool_unlock();
34520Sstevel@tonic-gate 	}
34530Sstevel@tonic-gate 
34540Sstevel@tonic-gate 	/*
34550Sstevel@tonic-gate 	 * Wait for zone_destroy() to be called.  This is what we spend
34560Sstevel@tonic-gate 	 * most of our life doing.
34570Sstevel@tonic-gate 	 */
34580Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched");
34590Sstevel@tonic-gate 
34600Sstevel@tonic-gate 	if (ct)
34610Sstevel@tonic-gate 		/*
34620Sstevel@tonic-gate 		 * At this point the process contract should be empty.
34630Sstevel@tonic-gate 		 * (Though if it isn't, it's not the end of the world.)
34640Sstevel@tonic-gate 		 */
34650Sstevel@tonic-gate 		VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0);
34660Sstevel@tonic-gate 
34670Sstevel@tonic-gate 	/*
34680Sstevel@tonic-gate 	 * Allow kcred to be freed when all referring processes
34690Sstevel@tonic-gate 	 * (including this one) go away.  We can't just do this in
34700Sstevel@tonic-gate 	 * zone_free because we need to wait for the zone_cred_ref to
34710Sstevel@tonic-gate 	 * drop to 0 before calling zone_free, and the existence of
34720Sstevel@tonic-gate 	 * zone_kcred will prevent that.  Thus, we call crfree here to
34730Sstevel@tonic-gate 	 * balance the crdup in zone_create.  The crhold calls earlier
34740Sstevel@tonic-gate 	 * in zsched will be dropped when the thread and process exit.
34750Sstevel@tonic-gate 	 */
34760Sstevel@tonic-gate 	crfree(zone->zone_kcred);
34770Sstevel@tonic-gate 	zone->zone_kcred = NULL;
34780Sstevel@tonic-gate 
34790Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
34800Sstevel@tonic-gate }
34810Sstevel@tonic-gate 
34820Sstevel@tonic-gate /*
34830Sstevel@tonic-gate  * Helper function to determine if there are any submounts of the
34840Sstevel@tonic-gate  * provided path.  Used to make sure the zone doesn't "inherit" any
34850Sstevel@tonic-gate  * mounts from before it is created.
34860Sstevel@tonic-gate  */
34870Sstevel@tonic-gate static uint_t
34880Sstevel@tonic-gate zone_mount_count(const char *rootpath)
34890Sstevel@tonic-gate {
34900Sstevel@tonic-gate 	vfs_t *vfsp;
34910Sstevel@tonic-gate 	uint_t count = 0;
34920Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
34930Sstevel@tonic-gate 
34940Sstevel@tonic-gate 	/*
34950Sstevel@tonic-gate 	 * Holding zonehash_lock prevents race conditions with
34960Sstevel@tonic-gate 	 * vfs_list_add()/vfs_list_remove() since we serialize with
34970Sstevel@tonic-gate 	 * zone_find_by_path().
34980Sstevel@tonic-gate 	 */
34990Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
35000Sstevel@tonic-gate 	/*
35010Sstevel@tonic-gate 	 * The rootpath must end with a '/'
35020Sstevel@tonic-gate 	 */
35030Sstevel@tonic-gate 	ASSERT(rootpath[rootpathlen - 1] == '/');
35040Sstevel@tonic-gate 
35050Sstevel@tonic-gate 	/*
35060Sstevel@tonic-gate 	 * This intentionally does not count the rootpath itself if that
35070Sstevel@tonic-gate 	 * happens to be a mount point.
35080Sstevel@tonic-gate 	 */
35090Sstevel@tonic-gate 	vfs_list_read_lock();
35100Sstevel@tonic-gate 	vfsp = rootvfs;
35110Sstevel@tonic-gate 	do {
35120Sstevel@tonic-gate 		if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt),
35130Sstevel@tonic-gate 		    rootpathlen) == 0)
35140Sstevel@tonic-gate 			count++;
35150Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
35160Sstevel@tonic-gate 	} while (vfsp != rootvfs);
35170Sstevel@tonic-gate 	vfs_list_unlock();
35180Sstevel@tonic-gate 	return (count);
35190Sstevel@tonic-gate }
35200Sstevel@tonic-gate 
35210Sstevel@tonic-gate /*
35220Sstevel@tonic-gate  * Helper function to make sure that a zone created on 'rootpath'
35230Sstevel@tonic-gate  * wouldn't end up containing other zones' rootpaths.
35240Sstevel@tonic-gate  */
35250Sstevel@tonic-gate static boolean_t
35260Sstevel@tonic-gate zone_is_nested(const char *rootpath)
35270Sstevel@tonic-gate {
35280Sstevel@tonic-gate 	zone_t *zone;
35290Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
35300Sstevel@tonic-gate 	size_t len;
35310Sstevel@tonic-gate 
35320Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
35330Sstevel@tonic-gate 
35340Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
35350Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
35360Sstevel@tonic-gate 		if (zone == global_zone)
35370Sstevel@tonic-gate 			continue;
35380Sstevel@tonic-gate 		len = strlen(zone->zone_rootpath);
35390Sstevel@tonic-gate 		if (strncmp(rootpath, zone->zone_rootpath,
35400Sstevel@tonic-gate 		    MIN(rootpathlen, len)) == 0)
35410Sstevel@tonic-gate 			return (B_TRUE);
35420Sstevel@tonic-gate 	}
35430Sstevel@tonic-gate 	return (B_FALSE);
35440Sstevel@tonic-gate }
35450Sstevel@tonic-gate 
35460Sstevel@tonic-gate static int
3547813Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs,
3548813Sdp     size_t zone_privssz)
35490Sstevel@tonic-gate {
35500Sstevel@tonic-gate 	priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
35510Sstevel@tonic-gate 
3552813Sdp 	if (zone_privssz < sizeof (priv_set_t))
3553813Sdp 		return (set_errno(ENOMEM));
3554813Sdp 
35550Sstevel@tonic-gate 	if (copyin(zone_privs, privs, sizeof (priv_set_t))) {
35560Sstevel@tonic-gate 		kmem_free(privs, sizeof (priv_set_t));
35570Sstevel@tonic-gate 		return (EFAULT);
35580Sstevel@tonic-gate 	}
35590Sstevel@tonic-gate 
35600Sstevel@tonic-gate 	zone->zone_privset = privs;
35610Sstevel@tonic-gate 	return (0);
35620Sstevel@tonic-gate }
35630Sstevel@tonic-gate 
35640Sstevel@tonic-gate /*
35650Sstevel@tonic-gate  * We make creative use of nvlists to pass in rctls from userland.  The list is
35660Sstevel@tonic-gate  * a list of the following structures:
35670Sstevel@tonic-gate  *
35680Sstevel@tonic-gate  * (name = rctl_name, value = nvpair_list_array)
35690Sstevel@tonic-gate  *
35700Sstevel@tonic-gate  * Where each element of the nvpair_list_array is of the form:
35710Sstevel@tonic-gate  *
35720Sstevel@tonic-gate  * [(name = "privilege", value = RCPRIV_PRIVILEGED),
35730Sstevel@tonic-gate  * 	(name = "limit", value = uint64_t),
35740Sstevel@tonic-gate  * 	(name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))]
35750Sstevel@tonic-gate  */
35760Sstevel@tonic-gate static int
35770Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp)
35780Sstevel@tonic-gate {
35790Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
35800Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
35810Sstevel@tonic-gate 	char *kbuf;
35820Sstevel@tonic-gate 	int error;
35830Sstevel@tonic-gate 	rctl_val_t rv;
35840Sstevel@tonic-gate 
35850Sstevel@tonic-gate 	*nvlp = NULL;
35860Sstevel@tonic-gate 
35870Sstevel@tonic-gate 	if (buflen == 0)
35880Sstevel@tonic-gate 		return (0);
35890Sstevel@tonic-gate 
35900Sstevel@tonic-gate 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
35910Sstevel@tonic-gate 		return (ENOMEM);
35920Sstevel@tonic-gate 	if (copyin(ubuf, kbuf, buflen)) {
35930Sstevel@tonic-gate 		error = EFAULT;
35940Sstevel@tonic-gate 		goto out;
35950Sstevel@tonic-gate 	}
35960Sstevel@tonic-gate 	if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) {
35970Sstevel@tonic-gate 		/*
35980Sstevel@tonic-gate 		 * nvl may have been allocated/free'd, but the value set to
35990Sstevel@tonic-gate 		 * non-NULL, so we reset it here.
36000Sstevel@tonic-gate 		 */
36010Sstevel@tonic-gate 		nvl = NULL;
36020Sstevel@tonic-gate 		error = EINVAL;
36030Sstevel@tonic-gate 		goto out;
36040Sstevel@tonic-gate 	}
36050Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
36060Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
36070Sstevel@tonic-gate 		rctl_hndl_t hndl;
36080Sstevel@tonic-gate 		nvlist_t **nvlarray;
36090Sstevel@tonic-gate 		uint_t i, nelem;
36100Sstevel@tonic-gate 		char *name;
36110Sstevel@tonic-gate 
36120Sstevel@tonic-gate 		error = EINVAL;
36130Sstevel@tonic-gate 		name = nvpair_name(nvp);
36140Sstevel@tonic-gate 		if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1)
36150Sstevel@tonic-gate 		    != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) {
36160Sstevel@tonic-gate 			goto out;
36170Sstevel@tonic-gate 		}
36180Sstevel@tonic-gate 		if ((hndl = rctl_hndl_lookup(name)) == -1) {
36190Sstevel@tonic-gate 			goto out;
36200Sstevel@tonic-gate 		}
36210Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
36220Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
36230Sstevel@tonic-gate 		ASSERT(error == 0);
36240Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
36250Sstevel@tonic-gate 			if (error = nvlist2rctlval(nvlarray[i], &rv))
36260Sstevel@tonic-gate 				goto out;
36270Sstevel@tonic-gate 		}
36280Sstevel@tonic-gate 		if (rctl_invalid_value(rde, &rv)) {
36290Sstevel@tonic-gate 			error = EINVAL;
36300Sstevel@tonic-gate 			goto out;
36310Sstevel@tonic-gate 		}
36320Sstevel@tonic-gate 	}
36330Sstevel@tonic-gate 	error = 0;
36340Sstevel@tonic-gate 	*nvlp = nvl;
36350Sstevel@tonic-gate out:
36360Sstevel@tonic-gate 	kmem_free(kbuf, buflen);
36370Sstevel@tonic-gate 	if (error && nvl != NULL)
36380Sstevel@tonic-gate 		nvlist_free(nvl);
36390Sstevel@tonic-gate 	return (error);
36400Sstevel@tonic-gate }
36410Sstevel@tonic-gate 
36420Sstevel@tonic-gate int
36430Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) {
36440Sstevel@tonic-gate 	if (er_out != NULL) {
36450Sstevel@tonic-gate 		if (copyout(&er_ext, er_out, sizeof (int))) {
36460Sstevel@tonic-gate 			return (set_errno(EFAULT));
36470Sstevel@tonic-gate 		}
36480Sstevel@tonic-gate 	}
36490Sstevel@tonic-gate 	return (set_errno(er_error));
36500Sstevel@tonic-gate }
36510Sstevel@tonic-gate 
36521676Sjpk static int
36531676Sjpk zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi)
36541676Sjpk {
36551676Sjpk 	ts_label_t *tsl;
36561676Sjpk 	bslabel_t blab;
36571676Sjpk 
36581676Sjpk 	/* Get label from user */
36591676Sjpk 	if (copyin(lab, &blab, sizeof (blab)) != 0)
36601676Sjpk 		return (EFAULT);
36611676Sjpk 	tsl = labelalloc(&blab, doi, KM_NOSLEEP);
36621676Sjpk 	if (tsl == NULL)
36631676Sjpk 		return (ENOMEM);
36641676Sjpk 
36651676Sjpk 	zone->zone_slabel = tsl;
36661676Sjpk 	return (0);
36671676Sjpk }
36681676Sjpk 
36690Sstevel@tonic-gate /*
3670789Sahrens  * Parses a comma-separated list of ZFS datasets into a per-zone dictionary.
3671789Sahrens  */
3672789Sahrens static int
3673789Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen)
3674789Sahrens {
3675789Sahrens 	char *kbuf;
3676789Sahrens 	char *dataset, *next;
3677789Sahrens 	zone_dataset_t *zd;
3678789Sahrens 	size_t len;
3679789Sahrens 
3680789Sahrens 	if (ubuf == NULL || buflen == 0)
3681789Sahrens 		return (0);
3682789Sahrens 
3683789Sahrens 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
3684789Sahrens 		return (ENOMEM);
3685789Sahrens 
3686789Sahrens 	if (copyin(ubuf, kbuf, buflen) != 0) {
3687789Sahrens 		kmem_free(kbuf, buflen);
3688789Sahrens 		return (EFAULT);
3689789Sahrens 	}
3690789Sahrens 
3691789Sahrens 	dataset = next = kbuf;
3692789Sahrens 	for (;;) {
3693789Sahrens 		zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP);
3694789Sahrens 
3695789Sahrens 		next = strchr(dataset, ',');
3696789Sahrens 
3697789Sahrens 		if (next == NULL)
3698789Sahrens 			len = strlen(dataset);
3699789Sahrens 		else
3700789Sahrens 			len = next - dataset;
3701789Sahrens 
3702789Sahrens 		zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP);
3703789Sahrens 		bcopy(dataset, zd->zd_dataset, len);
3704789Sahrens 		zd->zd_dataset[len] = '\0';
3705789Sahrens 
3706789Sahrens 		list_insert_head(&zone->zone_datasets, zd);
3707789Sahrens 
3708789Sahrens 		if (next == NULL)
3709789Sahrens 			break;
3710789Sahrens 
3711789Sahrens 		dataset = next + 1;
3712789Sahrens 	}
3713789Sahrens 
3714789Sahrens 	kmem_free(kbuf, buflen);
3715789Sahrens 	return (0);
3716789Sahrens }
3717789Sahrens 
3718789Sahrens /*
37190Sstevel@tonic-gate  * System call to create/initialize a new zone named 'zone_name', rooted
37200Sstevel@tonic-gate  * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs',
37211676Sjpk  * and initialized with the zone-wide rctls described in 'rctlbuf', and
37221676Sjpk  * with labeling set by 'match', 'doi', and 'label'.
37230Sstevel@tonic-gate  *
37240Sstevel@tonic-gate  * If extended error is non-null, we may use it to return more detailed
37250Sstevel@tonic-gate  * error information.
37260Sstevel@tonic-gate  */
37270Sstevel@tonic-gate static zoneid_t
37280Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root,
3729813Sdp     const priv_set_t *zone_privs, size_t zone_privssz,
3730813Sdp     caddr_t rctlbuf, size_t rctlbufsz,
37311676Sjpk     caddr_t zfsbuf, size_t zfsbufsz, int *extended_error,
37323448Sdh155122     int match, uint32_t doi, const bslabel_t *label,
37333448Sdh155122     int flags)
37340Sstevel@tonic-gate {
37350Sstevel@tonic-gate 	struct zsched_arg zarg;
37360Sstevel@tonic-gate 	nvlist_t *rctls = NULL;
37370Sstevel@tonic-gate 	proc_t *pp = curproc;
37380Sstevel@tonic-gate 	zone_t *zone, *ztmp;
37390Sstevel@tonic-gate 	zoneid_t zoneid;
37400Sstevel@tonic-gate 	int error;
37410Sstevel@tonic-gate 	int error2 = 0;
37420Sstevel@tonic-gate 	char *str;
37430Sstevel@tonic-gate 	cred_t *zkcr;
37441769Scarlsonj 	boolean_t insert_label_hash;
37450Sstevel@tonic-gate 
37460Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
37470Sstevel@tonic-gate 		return (set_errno(EPERM));
37480Sstevel@tonic-gate 
37490Sstevel@tonic-gate 	/* can't boot zone from within chroot environment */
37500Sstevel@tonic-gate 	if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir)
37510Sstevel@tonic-gate 		return (zone_create_error(ENOTSUP, ZE_CHROOTED,
3752813Sdp 		    extended_error));
37530Sstevel@tonic-gate 
37540Sstevel@tonic-gate 	zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP);
37550Sstevel@tonic-gate 	zoneid = zone->zone_id = id_alloc(zoneid_space);
37560Sstevel@tonic-gate 	zone->zone_status = ZONE_IS_UNINITIALIZED;
37570Sstevel@tonic-gate 	zone->zone_pool = pool_default;
37580Sstevel@tonic-gate 	zone->zone_pool_mod = gethrtime();
37590Sstevel@tonic-gate 	zone->zone_psetid = ZONE_PS_INVAL;
37600Sstevel@tonic-gate 	zone->zone_ncpus = 0;
37610Sstevel@tonic-gate 	zone->zone_ncpus_online = 0;
37622712Snn35248 	zone->zone_restart_init = B_TRUE;
37632712Snn35248 	zone->zone_brand = &native_brand;
37642712Snn35248 	zone->zone_initname = NULL;
37650Sstevel@tonic-gate 	mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL);
37660Sstevel@tonic-gate 	mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
37673247Sgjelinek 	mutex_init(&zone->zone_mem_lock, NULL, MUTEX_DEFAULT, NULL);
37680Sstevel@tonic-gate 	cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL);
37690Sstevel@tonic-gate 	list_create(&zone->zone_zsd, sizeof (struct zsd_entry),
37700Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
3771789Sahrens 	list_create(&zone->zone_datasets, sizeof (zone_dataset_t),
3772789Sahrens 	    offsetof(zone_dataset_t, zd_linkage));
37731676Sjpk 	rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
37740Sstevel@tonic-gate 
37753448Sdh155122 	if (flags & ZCF_NET_EXCL) {
37763448Sdh155122 		zone->zone_flags |= ZF_NET_EXCL;
37773448Sdh155122 	}
37783448Sdh155122 
37790Sstevel@tonic-gate 	if ((error = zone_set_name(zone, zone_name)) != 0) {
37800Sstevel@tonic-gate 		zone_free(zone);
37810Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
37820Sstevel@tonic-gate 	}
37830Sstevel@tonic-gate 
37840Sstevel@tonic-gate 	if ((error = zone_set_root(zone, zone_root)) != 0) {
37850Sstevel@tonic-gate 		zone_free(zone);
37860Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
37870Sstevel@tonic-gate 	}
3788813Sdp 	if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) {
37890Sstevel@tonic-gate 		zone_free(zone);
37900Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
37910Sstevel@tonic-gate 	}
37920Sstevel@tonic-gate 
37930Sstevel@tonic-gate 	/* initialize node name to be the same as zone name */
37940Sstevel@tonic-gate 	zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP);
37950Sstevel@tonic-gate 	(void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN);
37960Sstevel@tonic-gate 	zone->zone_nodename[_SYS_NMLN - 1] = '\0';
37970Sstevel@tonic-gate 
37980Sstevel@tonic-gate 	zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP);
37990Sstevel@tonic-gate 	zone->zone_domain[0] = '\0';
38000Sstevel@tonic-gate 	zone->zone_shares = 1;
38012677Sml93401 	zone->zone_shmmax = 0;
38022677Sml93401 	zone->zone_ipc.ipcq_shmmni = 0;
38032677Sml93401 	zone->zone_ipc.ipcq_semmni = 0;
38042677Sml93401 	zone->zone_ipc.ipcq_msgmni = 0;
38050Sstevel@tonic-gate 	zone->zone_bootargs = NULL;
38062267Sdp 	zone->zone_initname =
38072267Sdp 	    kmem_alloc(strlen(zone_default_initname) + 1, KM_SLEEP);
38082267Sdp 	(void) strcpy(zone->zone_initname, zone_default_initname);
38093247Sgjelinek 	zone->zone_nlwps = 0;
38103247Sgjelinek 	zone->zone_nlwps_ctl = INT_MAX;
38112768Ssl108498 	zone->zone_locked_mem = 0;
38122768Ssl108498 	zone->zone_locked_mem_ctl = UINT64_MAX;
38133247Sgjelinek 	zone->zone_max_swap = 0;
38143247Sgjelinek 	zone->zone_max_swap_ctl = UINT64_MAX;
38153247Sgjelinek 	zone0.zone_lockedmem_kstat = NULL;
38163247Sgjelinek 	zone0.zone_swapresv_kstat = NULL;
38170Sstevel@tonic-gate 
38180Sstevel@tonic-gate 	/*
38190Sstevel@tonic-gate 	 * Zsched initializes the rctls.
38200Sstevel@tonic-gate 	 */
38210Sstevel@tonic-gate 	zone->zone_rctls = NULL;
38220Sstevel@tonic-gate 
38230Sstevel@tonic-gate 	if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) {
38240Sstevel@tonic-gate 		zone_free(zone);
38250Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
38260Sstevel@tonic-gate 	}
38270Sstevel@tonic-gate 
3828789Sahrens 	if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) {
3829789Sahrens 		zone_free(zone);
3830789Sahrens 		return (set_errno(error));
3831789Sahrens 	}
3832789Sahrens 
38330Sstevel@tonic-gate 	/*
38341676Sjpk 	 * Read in the trusted system parameters:
38351676Sjpk 	 * match flag and sensitivity label.
38361676Sjpk 	 */
38371676Sjpk 	zone->zone_match = match;
38381769Scarlsonj 	if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) {
38394462Skp158701 		/* Fail if requested to set doi to anything but system's doi */
38404462Skp158701 		if (doi != 0 && doi != default_doi) {
38414462Skp158701 			zone_free(zone);
38424462Skp158701 			return (set_errno(EINVAL));
38434462Skp158701 		}
38444462Skp158701 		/* Always apply system's doi to the zone */
38454462Skp158701 		error = zone_set_label(zone, label, default_doi);
38461676Sjpk 		if (error != 0) {
38471676Sjpk 			zone_free(zone);
38481676Sjpk 			return (set_errno(error));
38491676Sjpk 		}
38501769Scarlsonj 		insert_label_hash = B_TRUE;
38511676Sjpk 	} else {
38521676Sjpk 		/* all zones get an admin_low label if system is not labeled */
38531676Sjpk 		zone->zone_slabel = l_admin_low;
38541676Sjpk 		label_hold(l_admin_low);
38551769Scarlsonj 		insert_label_hash = B_FALSE;
38561676Sjpk 	}
38571676Sjpk 
38581676Sjpk 	/*
38590Sstevel@tonic-gate 	 * Stop all lwps since that's what normally happens as part of fork().
38600Sstevel@tonic-gate 	 * This needs to happen before we grab any locks to avoid deadlock
38610Sstevel@tonic-gate 	 * (another lwp in the process could be waiting for the held lock).
38620Sstevel@tonic-gate 	 */
38630Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) {
38640Sstevel@tonic-gate 		zone_free(zone);
38650Sstevel@tonic-gate 		if (rctls)
38660Sstevel@tonic-gate 			nvlist_free(rctls);
38670Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
38680Sstevel@tonic-gate 	}
38690Sstevel@tonic-gate 
38700Sstevel@tonic-gate 	if (block_mounts() == 0) {
38710Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
38720Sstevel@tonic-gate 		if (curthread != pp->p_agenttp)
38730Sstevel@tonic-gate 			continuelwps(pp);
38740Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
38750Sstevel@tonic-gate 		zone_free(zone);
38760Sstevel@tonic-gate 		if (rctls)
38770Sstevel@tonic-gate 			nvlist_free(rctls);
38780Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
38790Sstevel@tonic-gate 	}
38800Sstevel@tonic-gate 
38810Sstevel@tonic-gate 	/*
38820Sstevel@tonic-gate 	 * Set up credential for kernel access.  After this, any errors
38830Sstevel@tonic-gate 	 * should go through the dance in errout rather than calling
38840Sstevel@tonic-gate 	 * zone_free directly.
38850Sstevel@tonic-gate 	 */
38860Sstevel@tonic-gate 	zone->zone_kcred = crdup(kcred);
38870Sstevel@tonic-gate 	crsetzone(zone->zone_kcred, zone);
38880Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred));
38890Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred));
38900Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred));
38910Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred));
38920Sstevel@tonic-gate 
38930Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
38940Sstevel@tonic-gate 	/*
38950Sstevel@tonic-gate 	 * Make sure zone doesn't already exist.
38961676Sjpk 	 *
38971676Sjpk 	 * If the system and zone are labeled,
38981676Sjpk 	 * make sure no other zone exists that has the same label.
38990Sstevel@tonic-gate 	 */
39001676Sjpk 	if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL ||
39011769Scarlsonj 	    (insert_label_hash &&
39021676Sjpk 	    (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) {
39030Sstevel@tonic-gate 		zone_status_t status;
39040Sstevel@tonic-gate 
39050Sstevel@tonic-gate 		status = zone_status_get(ztmp);
39060Sstevel@tonic-gate 		if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING)
39070Sstevel@tonic-gate 			error = EEXIST;
39080Sstevel@tonic-gate 		else
39090Sstevel@tonic-gate 			error = EBUSY;
39104791Ston 
39114791Ston 		if (insert_label_hash)
39124791Ston 			error2 = ZE_LABELINUSE;
39134791Ston 
39140Sstevel@tonic-gate 		goto errout;
39150Sstevel@tonic-gate 	}
39160Sstevel@tonic-gate 
39170Sstevel@tonic-gate 	/*
39180Sstevel@tonic-gate 	 * Don't allow zone creations which would cause one zone's rootpath to
39190Sstevel@tonic-gate 	 * be accessible from that of another (non-global) zone.
39200Sstevel@tonic-gate 	 */
39210Sstevel@tonic-gate 	if (zone_is_nested(zone->zone_rootpath)) {
39220Sstevel@tonic-gate 		error = EBUSY;
39230Sstevel@tonic-gate 		goto errout;
39240Sstevel@tonic-gate 	}
39250Sstevel@tonic-gate 
39260Sstevel@tonic-gate 	ASSERT(zonecount != 0);		/* check for leaks */
39270Sstevel@tonic-gate 	if (zonecount + 1 > maxzones) {
39280Sstevel@tonic-gate 		error = ENOMEM;
39290Sstevel@tonic-gate 		goto errout;
39300Sstevel@tonic-gate 	}
39310Sstevel@tonic-gate 
39320Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
39330Sstevel@tonic-gate 		error = EBUSY;
39340Sstevel@tonic-gate 		error2 = ZE_AREMOUNTS;
39350Sstevel@tonic-gate 		goto errout;
39360Sstevel@tonic-gate 	}
39370Sstevel@tonic-gate 
39380Sstevel@tonic-gate 	/*
39390Sstevel@tonic-gate 	 * Zone is still incomplete, but we need to drop all locks while
39400Sstevel@tonic-gate 	 * zsched() initializes this zone's kernel process.  We
39410Sstevel@tonic-gate 	 * optimistically add the zone to the hashtable and associated
39420Sstevel@tonic-gate 	 * lists so a parallel zone_create() doesn't try to create the
39430Sstevel@tonic-gate 	 * same zone.
39440Sstevel@tonic-gate 	 */
39450Sstevel@tonic-gate 	zonecount++;
39460Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid,
39470Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id,
39480Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
39490Sstevel@tonic-gate 	str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP);
39500Sstevel@tonic-gate 	(void) strcpy(str, zone->zone_name);
39510Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str,
39520Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
39531769Scarlsonj 	if (insert_label_hash) {
39541676Sjpk 		(void) mod_hash_insert(zonehashbylabel,
39551676Sjpk 		    (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone);
39561769Scarlsonj 		zone->zone_flags |= ZF_HASHED_LABEL;
39571676Sjpk 	}
39581676Sjpk 
39590Sstevel@tonic-gate 	/*
39600Sstevel@tonic-gate 	 * Insert into active list.  At this point there are no 'hold's
39610Sstevel@tonic-gate 	 * on the zone, but everyone else knows not to use it, so we can
39620Sstevel@tonic-gate 	 * continue to use it.  zsched() will do a zone_hold() if the
39630Sstevel@tonic-gate 	 * newproc() is successful.
39640Sstevel@tonic-gate 	 */
39650Sstevel@tonic-gate 	list_insert_tail(&zone_active, zone);
39660Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
39670Sstevel@tonic-gate 
39680Sstevel@tonic-gate 	zarg.zone = zone;
39690Sstevel@tonic-gate 	zarg.nvlist = rctls;
39700Sstevel@tonic-gate 	/*
39710Sstevel@tonic-gate 	 * The process, task, and project rctls are probably wrong;
39720Sstevel@tonic-gate 	 * we need an interface to get the default values of all rctls,
39730Sstevel@tonic-gate 	 * and initialize zsched appropriately.  I'm not sure that that
39740Sstevel@tonic-gate 	 * makes much of a difference, though.
39750Sstevel@tonic-gate 	 */
39760Sstevel@tonic-gate 	if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) {
39770Sstevel@tonic-gate 		/*
39780Sstevel@tonic-gate 		 * We need to undo all globally visible state.
39790Sstevel@tonic-gate 		 */
39800Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
39810Sstevel@tonic-gate 		list_remove(&zone_active, zone);
39821769Scarlsonj 		if (zone->zone_flags & ZF_HASHED_LABEL) {
39831676Sjpk 			ASSERT(zone->zone_slabel != NULL);
39841676Sjpk 			(void) mod_hash_destroy(zonehashbylabel,
39851676Sjpk 			    (mod_hash_key_t)zone->zone_slabel);
39861676Sjpk 		}
39870Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyname,
39880Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_name);
39890Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyid,
39900Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_id);
39910Sstevel@tonic-gate 		ASSERT(zonecount > 1);
39920Sstevel@tonic-gate 		zonecount--;
39930Sstevel@tonic-gate 		goto errout;
39940Sstevel@tonic-gate 	}
39950Sstevel@tonic-gate 
39960Sstevel@tonic-gate 	/*
39970Sstevel@tonic-gate 	 * Zone creation can't fail from now on.
39980Sstevel@tonic-gate 	 */
39990Sstevel@tonic-gate 
40000Sstevel@tonic-gate 	/*
40013247Sgjelinek 	 * Create zone kstats
40023247Sgjelinek 	 */
40033247Sgjelinek 	zone_kstat_create(zone);
40043247Sgjelinek 
40053247Sgjelinek 	/*
40060Sstevel@tonic-gate 	 * Let the other lwps continue.
40070Sstevel@tonic-gate 	 */
40080Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
40090Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
40100Sstevel@tonic-gate 		continuelwps(pp);
40110Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
40120Sstevel@tonic-gate 
40130Sstevel@tonic-gate 	/*
40140Sstevel@tonic-gate 	 * Wait for zsched to finish initializing the zone.
40150Sstevel@tonic-gate 	 */
40160Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_READY);
40170Sstevel@tonic-gate 	/*
40180Sstevel@tonic-gate 	 * The zone is fully visible, so we can let mounts progress.
40190Sstevel@tonic-gate 	 */
40200Sstevel@tonic-gate 	resume_mounts();
40210Sstevel@tonic-gate 	if (rctls)
40220Sstevel@tonic-gate 		nvlist_free(rctls);
40230Sstevel@tonic-gate 
40240Sstevel@tonic-gate 	return (zoneid);
40250Sstevel@tonic-gate 
40260Sstevel@tonic-gate errout:
40270Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
40280Sstevel@tonic-gate 	/*
40290Sstevel@tonic-gate 	 * Let the other lwps continue.
40300Sstevel@tonic-gate 	 */
40310Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
40320Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
40330Sstevel@tonic-gate 		continuelwps(pp);
40340Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
40350Sstevel@tonic-gate 
40360Sstevel@tonic-gate 	resume_mounts();
40370Sstevel@tonic-gate 	if (rctls)
40380Sstevel@tonic-gate 		nvlist_free(rctls);
40390Sstevel@tonic-gate 	/*
40400Sstevel@tonic-gate 	 * There is currently one reference to the zone, a cred_ref from
40410Sstevel@tonic-gate 	 * zone_kcred.  To free the zone, we call crfree, which will call
40420Sstevel@tonic-gate 	 * zone_cred_rele, which will call zone_free.
40430Sstevel@tonic-gate 	 */
40440Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 1);	/* for zone_kcred */
40450Sstevel@tonic-gate 	ASSERT(zone->zone_kcred->cr_ref == 1);
40460Sstevel@tonic-gate 	ASSERT(zone->zone_ref == 0);
40470Sstevel@tonic-gate 	zkcr = zone->zone_kcred;
40480Sstevel@tonic-gate 	zone->zone_kcred = NULL;
40490Sstevel@tonic-gate 	crfree(zkcr);				/* triggers call to zone_free */
40500Sstevel@tonic-gate 	return (zone_create_error(error, error2, extended_error));
40510Sstevel@tonic-gate }
40520Sstevel@tonic-gate 
40530Sstevel@tonic-gate /*
40540Sstevel@tonic-gate  * Cause the zone to boot.  This is pretty simple, since we let zoneadmd do
40552267Sdp  * the heavy lifting.  initname is the path to the program to launch
40562267Sdp  * at the "top" of the zone; if this is NULL, we use the system default,
40572267Sdp  * which is stored at zone_default_initname.
40580Sstevel@tonic-gate  */
40590Sstevel@tonic-gate static int
40602267Sdp zone_boot(zoneid_t zoneid)
40610Sstevel@tonic-gate {
40620Sstevel@tonic-gate 	int err;
40630Sstevel@tonic-gate 	zone_t *zone;
40640Sstevel@tonic-gate 
40650Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
40660Sstevel@tonic-gate 		return (set_errno(EPERM));
40670Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
40680Sstevel@tonic-gate 		return (set_errno(EINVAL));
40690Sstevel@tonic-gate 
40700Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
40710Sstevel@tonic-gate 	/*
40720Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with calls to
40730Sstevel@tonic-gate 	 * zone_shutdown, zone_destroy, etc.
40740Sstevel@tonic-gate 	 */
40750Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
40760Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
40770Sstevel@tonic-gate 		return (set_errno(EINVAL));
40780Sstevel@tonic-gate 	}
40790Sstevel@tonic-gate 
40800Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
40810Sstevel@tonic-gate 	if (zone_status_get(zone) != ZONE_IS_READY) {
40820Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
40830Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
40840Sstevel@tonic-gate 		return (set_errno(EINVAL));
40850Sstevel@tonic-gate 	}
40860Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_BOOTING);
40870Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
40880Sstevel@tonic-gate 
40890Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
40900Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
40910Sstevel@tonic-gate 
40920Sstevel@tonic-gate 	if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) {
40930Sstevel@tonic-gate 		zone_rele(zone);
40940Sstevel@tonic-gate 		return (set_errno(EINTR));
40950Sstevel@tonic-gate 	}
40960Sstevel@tonic-gate 
40970Sstevel@tonic-gate 	/*
40980Sstevel@tonic-gate 	 * Boot (starting init) might have failed, in which case the zone
40990Sstevel@tonic-gate 	 * will go to the SHUTTING_DOWN state; an appropriate errno will
41000Sstevel@tonic-gate 	 * be placed in zone->zone_boot_err, and so we return that.
41010Sstevel@tonic-gate 	 */
41020Sstevel@tonic-gate 	err = zone->zone_boot_err;
41030Sstevel@tonic-gate 	zone_rele(zone);
41040Sstevel@tonic-gate 	return (err ? set_errno(err) : 0);
41050Sstevel@tonic-gate }
41060Sstevel@tonic-gate 
41070Sstevel@tonic-gate /*
41080Sstevel@tonic-gate  * Kills all user processes in the zone, waiting for them all to exit
41090Sstevel@tonic-gate  * before returning.
41100Sstevel@tonic-gate  */
41110Sstevel@tonic-gate static int
41120Sstevel@tonic-gate zone_empty(zone_t *zone)
41130Sstevel@tonic-gate {
41140Sstevel@tonic-gate 	int waitstatus;
41150Sstevel@tonic-gate 
41160Sstevel@tonic-gate 	/*
41170Sstevel@tonic-gate 	 * We need to drop zonehash_lock before killing all
41180Sstevel@tonic-gate 	 * processes, otherwise we'll deadlock with zone_find_*
41190Sstevel@tonic-gate 	 * which can be called from the exit path.
41200Sstevel@tonic-gate 	 */
41210Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
41220Sstevel@tonic-gate 	while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz,
41230Sstevel@tonic-gate 	    ZONE_IS_EMPTY)) == -1) {
41240Sstevel@tonic-gate 		killall(zone->zone_id);
41250Sstevel@tonic-gate 	}
41260Sstevel@tonic-gate 	/*
41270Sstevel@tonic-gate 	 * return EINTR if we were signaled
41280Sstevel@tonic-gate 	 */
41290Sstevel@tonic-gate 	if (waitstatus == 0)
41300Sstevel@tonic-gate 		return (EINTR);
41310Sstevel@tonic-gate 	return (0);
41320Sstevel@tonic-gate }
41330Sstevel@tonic-gate 
41340Sstevel@tonic-gate /*
41351676Sjpk  * This function implements the policy for zone visibility.
41361676Sjpk  *
41371676Sjpk  * In standard Solaris, a non-global zone can only see itself.
41381676Sjpk  *
41391676Sjpk  * In Trusted Extensions, a labeled zone can lookup any zone whose label
41401676Sjpk  * it dominates. For this test, the label of the global zone is treated as
41411676Sjpk  * admin_high so it is special-cased instead of being checked for dominance.
41421676Sjpk  *
41431676Sjpk  * Returns true if zone attributes are viewable, false otherwise.
41441676Sjpk  */
41451676Sjpk static boolean_t
41461676Sjpk zone_list_access(zone_t *zone)
41471676Sjpk {
41481676Sjpk 
41491676Sjpk 	if (curproc->p_zone == global_zone ||
41501676Sjpk 	    curproc->p_zone == zone) {
41511676Sjpk 		return (B_TRUE);
41521769Scarlsonj 	} else if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) {
41531676Sjpk 		bslabel_t *curproc_label;
41541676Sjpk 		bslabel_t *zone_label;
41551676Sjpk 
41561676Sjpk 		curproc_label = label2bslabel(curproc->p_zone->zone_slabel);
41571676Sjpk 		zone_label = label2bslabel(zone->zone_slabel);
41581676Sjpk 
41591676Sjpk 		if (zone->zone_id != GLOBAL_ZONEID &&
41601676Sjpk 		    bldominates(curproc_label, zone_label)) {
41611676Sjpk 			return (B_TRUE);
41621676Sjpk 		} else {
41631676Sjpk 			return (B_FALSE);
41641676Sjpk 		}
41651676Sjpk 	} else {
41661676Sjpk 		return (B_FALSE);
41671676Sjpk 	}
41681676Sjpk }
41691676Sjpk 
41701676Sjpk /*
41710Sstevel@tonic-gate  * Systemcall to start the zone's halt sequence.  By the time this
41720Sstevel@tonic-gate  * function successfully returns, all user processes and kernel threads
41730Sstevel@tonic-gate  * executing in it will have exited, ZSD shutdown callbacks executed,
41740Sstevel@tonic-gate  * and the zone status set to ZONE_IS_DOWN.
41750Sstevel@tonic-gate  *
41760Sstevel@tonic-gate  * It is possible that the call will interrupt itself if the caller is the
41770Sstevel@tonic-gate  * parent of any process running in the zone, and doesn't have SIGCHLD blocked.
41780Sstevel@tonic-gate  */
41790Sstevel@tonic-gate static int
41800Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid)
41810Sstevel@tonic-gate {
41820Sstevel@tonic-gate 	int error;
41830Sstevel@tonic-gate 	zone_t *zone;
41840Sstevel@tonic-gate 	zone_status_t status;
41850Sstevel@tonic-gate 
41860Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
41870Sstevel@tonic-gate 		return (set_errno(EPERM));
41880Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
41890Sstevel@tonic-gate 		return (set_errno(EINVAL));
41900Sstevel@tonic-gate 
41910Sstevel@tonic-gate 	/*
41920Sstevel@tonic-gate 	 * Block mounts so that VFS_MOUNT() can get an accurate view of
41930Sstevel@tonic-gate 	 * the zone's status with regards to ZONE_IS_SHUTTING down.
41940Sstevel@tonic-gate 	 *
41950Sstevel@tonic-gate 	 * e.g. NFS can fail the mount if it determines that the zone
41960Sstevel@tonic-gate 	 * has already begun the shutdown sequence.
41970Sstevel@tonic-gate 	 */
41980Sstevel@tonic-gate 	if (block_mounts() == 0)
41990Sstevel@tonic-gate 		return (set_errno(EINTR));
42000Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
42010Sstevel@tonic-gate 	/*
42020Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
42030Sstevel@tonic-gate 	 * calls to zone_shutdown and zone_destroy.
42040Sstevel@tonic-gate 	 */
42050Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
42060Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
42070Sstevel@tonic-gate 		resume_mounts();
42080Sstevel@tonic-gate 		return (set_errno(EINVAL));
42090Sstevel@tonic-gate 	}
42100Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
42110Sstevel@tonic-gate 	status = zone_status_get(zone);
42120Sstevel@tonic-gate 	/*
42130Sstevel@tonic-gate 	 * Fail if the zone isn't fully initialized yet.
42140Sstevel@tonic-gate 	 */
42150Sstevel@tonic-gate 	if (status < ZONE_IS_READY) {
42160Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
42170Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
42180Sstevel@tonic-gate 		resume_mounts();
42190Sstevel@tonic-gate 		return (set_errno(EINVAL));
42200Sstevel@tonic-gate 	}
42210Sstevel@tonic-gate 	/*
42220Sstevel@tonic-gate 	 * If conditions required for zone_shutdown() to return have been met,
42230Sstevel@tonic-gate 	 * return success.
42240Sstevel@tonic-gate 	 */
42250Sstevel@tonic-gate 	if (status >= ZONE_IS_DOWN) {
42260Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
42270Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
42280Sstevel@tonic-gate 		resume_mounts();
42290Sstevel@tonic-gate 		return (0);
42300Sstevel@tonic-gate 	}
42310Sstevel@tonic-gate 	/*
42320Sstevel@tonic-gate 	 * If zone_shutdown() hasn't been called before, go through the motions.
42330Sstevel@tonic-gate 	 * If it has, there's nothing to do but wait for the kernel threads to
42340Sstevel@tonic-gate 	 * drain.
42350Sstevel@tonic-gate 	 */
42360Sstevel@tonic-gate 	if (status < ZONE_IS_EMPTY) {
42370Sstevel@tonic-gate 		uint_t ntasks;
42380Sstevel@tonic-gate 
42390Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
42400Sstevel@tonic-gate 		if ((ntasks = zone->zone_ntasks) != 1) {
42410Sstevel@tonic-gate 			/*
42420Sstevel@tonic-gate 			 * There's still stuff running.
42430Sstevel@tonic-gate 			 */
42440Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
42450Sstevel@tonic-gate 		}
42460Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
42470Sstevel@tonic-gate 		if (ntasks == 1) {
42480Sstevel@tonic-gate 			/*
42490Sstevel@tonic-gate 			 * The only way to create another task is through
42500Sstevel@tonic-gate 			 * zone_enter(), which will block until we drop
42510Sstevel@tonic-gate 			 * zonehash_lock.  The zone is empty.
42520Sstevel@tonic-gate 			 */
42530Sstevel@tonic-gate 			if (zone->zone_kthreads == NULL) {
42540Sstevel@tonic-gate 				/*
42550Sstevel@tonic-gate 				 * Skip ahead to ZONE_IS_DOWN
42560Sstevel@tonic-gate 				 */
42570Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_DOWN);
42580Sstevel@tonic-gate 			} else {
42590Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_EMPTY);
42600Sstevel@tonic-gate 			}
42610Sstevel@tonic-gate 		}
42620Sstevel@tonic-gate 	}
42630Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
42640Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
42650Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
42660Sstevel@tonic-gate 	resume_mounts();
42670Sstevel@tonic-gate 
42680Sstevel@tonic-gate 	if (error = zone_empty(zone)) {
42690Sstevel@tonic-gate 		zone_rele(zone);
42700Sstevel@tonic-gate 		return (set_errno(error));
42710Sstevel@tonic-gate 	}
42720Sstevel@tonic-gate 	/*
42730Sstevel@tonic-gate 	 * After the zone status goes to ZONE_IS_DOWN this zone will no
42740Sstevel@tonic-gate 	 * longer be notified of changes to the pools configuration, so
42750Sstevel@tonic-gate 	 * in order to not end up with a stale pool pointer, we point
42760Sstevel@tonic-gate 	 * ourselves at the default pool and remove all resource
42770Sstevel@tonic-gate 	 * visibility.  This is especially important as the zone_t may
42780Sstevel@tonic-gate 	 * languish on the deathrow for a very long time waiting for
42790Sstevel@tonic-gate 	 * cred's to drain out.
42800Sstevel@tonic-gate 	 *
42810Sstevel@tonic-gate 	 * This rebinding of the zone can happen multiple times
42820Sstevel@tonic-gate 	 * (presumably due to interrupted or parallel systemcalls)
42830Sstevel@tonic-gate 	 * without any adverse effects.
42840Sstevel@tonic-gate 	 */
42850Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
42860Sstevel@tonic-gate 		zone_rele(zone);
42870Sstevel@tonic-gate 		return (set_errno(EINTR));
42880Sstevel@tonic-gate 	}
42890Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
42900Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
42910Sstevel@tonic-gate 		zone_pool_set(zone, pool_default);
42920Sstevel@tonic-gate 		/*
42930Sstevel@tonic-gate 		 * The zone no longer needs to be able to see any cpus.
42940Sstevel@tonic-gate 		 */
42950Sstevel@tonic-gate 		zone_pset_set(zone, ZONE_PS_INVAL);
42960Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
42970Sstevel@tonic-gate 	}
42980Sstevel@tonic-gate 	pool_unlock();
42990Sstevel@tonic-gate 
43000Sstevel@tonic-gate 	/*
43010Sstevel@tonic-gate 	 * ZSD shutdown callbacks can be executed multiple times, hence
43020Sstevel@tonic-gate 	 * it is safe to not be holding any locks across this call.
43030Sstevel@tonic-gate 	 */
43040Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_SHUTDOWN);
43050Sstevel@tonic-gate 
43060Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
43070Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN)
43080Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DOWN);
43090Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
43100Sstevel@tonic-gate 
43110Sstevel@tonic-gate 	/*
43120Sstevel@tonic-gate 	 * Wait for kernel threads to drain.
43130Sstevel@tonic-gate 	 */
43140Sstevel@tonic-gate 	if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) {
43150Sstevel@tonic-gate 		zone_rele(zone);
43160Sstevel@tonic-gate 		return (set_errno(EINTR));
43170Sstevel@tonic-gate 	}
43182712Snn35248 
43193671Ssl108498 	/*
43203671Ssl108498 	 * Zone can be become down/destroyable even if the above wait
43213671Ssl108498 	 * returns EINTR, so any code added here may never execute.
43223671Ssl108498 	 * (i.e. don't add code here)
43233671Ssl108498 	 */
43242712Snn35248 
43250Sstevel@tonic-gate 	zone_rele(zone);
43260Sstevel@tonic-gate 	return (0);
43270Sstevel@tonic-gate }
43280Sstevel@tonic-gate 
43290Sstevel@tonic-gate /*
43300Sstevel@tonic-gate  * Systemcall entry point to finalize the zone halt process.  The caller
43312677Sml93401  * must have already successfully called zone_shutdown().
43320Sstevel@tonic-gate  *
43330Sstevel@tonic-gate  * Upon successful completion, the zone will have been fully destroyed:
43340Sstevel@tonic-gate  * zsched will have exited, destructor callbacks executed, and the zone
43350Sstevel@tonic-gate  * removed from the list of active zones.
43360Sstevel@tonic-gate  */
43370Sstevel@tonic-gate static int
43380Sstevel@tonic-gate zone_destroy(zoneid_t zoneid)
43390Sstevel@tonic-gate {
43400Sstevel@tonic-gate 	uint64_t uniqid;
43410Sstevel@tonic-gate 	zone_t *zone;
43420Sstevel@tonic-gate 	zone_status_t status;
43430Sstevel@tonic-gate 
43440Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
43450Sstevel@tonic-gate 		return (set_errno(EPERM));
43460Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
43470Sstevel@tonic-gate 		return (set_errno(EINVAL));
43480Sstevel@tonic-gate 
43490Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
43500Sstevel@tonic-gate 	/*
43510Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
43520Sstevel@tonic-gate 	 * calls to zone_destroy.
43530Sstevel@tonic-gate 	 */
43540Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
43550Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
43560Sstevel@tonic-gate 		return (set_errno(EINVAL));
43570Sstevel@tonic-gate 	}
43580Sstevel@tonic-gate 
43590Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
43600Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
43610Sstevel@tonic-gate 		return (set_errno(EBUSY));
43620Sstevel@tonic-gate 	}
43630Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
43640Sstevel@tonic-gate 	status = zone_status_get(zone);
43650Sstevel@tonic-gate 	if (status < ZONE_IS_DOWN) {
43660Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
43670Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
43680Sstevel@tonic-gate 		return (set_errno(EBUSY));
43690Sstevel@tonic-gate 	} else if (status == ZONE_IS_DOWN) {
43700Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */
43710Sstevel@tonic-gate 	}
43720Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
43730Sstevel@tonic-gate 	zone_hold(zone);
43740Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
43750Sstevel@tonic-gate 
43760Sstevel@tonic-gate 	/*
43770Sstevel@tonic-gate 	 * wait for zsched to exit
43780Sstevel@tonic-gate 	 */
43790Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_DEAD);
43800Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_DESTROY);
43813448Sdh155122 	zone->zone_netstack = NULL;
43820Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
43830Sstevel@tonic-gate 	zone_rele(zone);
43840Sstevel@tonic-gate 	zone = NULL;	/* potentially free'd */
43850Sstevel@tonic-gate 
43860Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
43870Sstevel@tonic-gate 	for (; /* ever */; ) {
43880Sstevel@tonic-gate 		boolean_t unref;
43890Sstevel@tonic-gate 
43900Sstevel@tonic-gate 		if ((zone = zone_find_all_by_id(zoneid)) == NULL ||
43910Sstevel@tonic-gate 		    zone->zone_uniqid != uniqid) {
43920Sstevel@tonic-gate 			/*
43930Sstevel@tonic-gate 			 * The zone has gone away.  Necessary conditions
43940Sstevel@tonic-gate 			 * are met, so we return success.
43950Sstevel@tonic-gate 			 */
43960Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
43970Sstevel@tonic-gate 			return (0);
43980Sstevel@tonic-gate 		}
43990Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
44000Sstevel@tonic-gate 		unref = ZONE_IS_UNREF(zone);
44010Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
44020Sstevel@tonic-gate 		if (unref) {
44030Sstevel@tonic-gate 			/*
44040Sstevel@tonic-gate 			 * There is only one reference to the zone -- that
44050Sstevel@tonic-gate 			 * added when the zone was added to the hashtables --
44060Sstevel@tonic-gate 			 * and things will remain this way until we drop
44070Sstevel@tonic-gate 			 * zonehash_lock... we can go ahead and cleanup the
44080Sstevel@tonic-gate 			 * zone.
44090Sstevel@tonic-gate 			 */
44100Sstevel@tonic-gate 			break;
44110Sstevel@tonic-gate 		}
44120Sstevel@tonic-gate 
44130Sstevel@tonic-gate 		if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) {
44140Sstevel@tonic-gate 			/* Signaled */
44150Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
44160Sstevel@tonic-gate 			return (set_errno(EINTR));
44170Sstevel@tonic-gate 		}
44180Sstevel@tonic-gate 
44190Sstevel@tonic-gate 	}
44200Sstevel@tonic-gate 
44213792Sakolb 	/*
44223792Sakolb 	 * Remove CPU cap for this zone now since we're not going to
44233792Sakolb 	 * fail below this point.
44243792Sakolb 	 */
44253792Sakolb 	cpucaps_zone_remove(zone);
44263792Sakolb 
44273792Sakolb 	/* Get rid of the zone's kstats */
44283247Sgjelinek 	zone_kstat_delete(zone);
44293247Sgjelinek 
44304888Seh208807 	/* free brand specific data */
44314888Seh208807 	if (ZONE_IS_BRANDED(zone))
44324888Seh208807 		ZBROP(zone)->b_free_brand_data(zone);
44334888Seh208807 
44343671Ssl108498 	/* Say goodbye to brand framework. */
44353671Ssl108498 	brand_unregister_zone(zone->zone_brand);
44363671Ssl108498 
44370Sstevel@tonic-gate 	/*
44380Sstevel@tonic-gate 	 * It is now safe to let the zone be recreated; remove it from the
44390Sstevel@tonic-gate 	 * lists.  The memory will not be freed until the last cred
44400Sstevel@tonic-gate 	 * reference goes away.
44410Sstevel@tonic-gate 	 */
44420Sstevel@tonic-gate 	ASSERT(zonecount > 1);	/* must be > 1; can't destroy global zone */
44430Sstevel@tonic-gate 	zonecount--;
44440Sstevel@tonic-gate 	/* remove from active list and hash tables */
44450Sstevel@tonic-gate 	list_remove(&zone_active, zone);
44460Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyname,
44470Sstevel@tonic-gate 	    (mod_hash_key_t)zone->zone_name);
44480Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyid,
44490Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id);
44501769Scarlsonj 	if (zone->zone_flags & ZF_HASHED_LABEL)
44511676Sjpk 		(void) mod_hash_destroy(zonehashbylabel,
44521676Sjpk 		    (mod_hash_key_t)zone->zone_slabel);
44530Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
44540Sstevel@tonic-gate 
4455766Scarlsonj 	/*
4456766Scarlsonj 	 * Release the root vnode; we're not using it anymore.  Nor should any
4457766Scarlsonj 	 * other thread that might access it exist.
4458766Scarlsonj 	 */
4459766Scarlsonj 	if (zone->zone_rootvp != NULL) {
4460766Scarlsonj 		VN_RELE(zone->zone_rootvp);
4461766Scarlsonj 		zone->zone_rootvp = NULL;
4462766Scarlsonj 	}
4463766Scarlsonj 
44640Sstevel@tonic-gate 	/* add to deathrow list */
44650Sstevel@tonic-gate 	mutex_enter(&zone_deathrow_lock);
44660Sstevel@tonic-gate 	list_insert_tail(&zone_deathrow, zone);
44670Sstevel@tonic-gate 	mutex_exit(&zone_deathrow_lock);
44680Sstevel@tonic-gate 
44690Sstevel@tonic-gate 	/*
44700Sstevel@tonic-gate 	 * Drop last reference (which was added by zsched()), this will
44710Sstevel@tonic-gate 	 * free the zone unless there are outstanding cred references.
44720Sstevel@tonic-gate 	 */
44730Sstevel@tonic-gate 	zone_rele(zone);
44740Sstevel@tonic-gate 	return (0);
44750Sstevel@tonic-gate }
44760Sstevel@tonic-gate 
44770Sstevel@tonic-gate /*
44780Sstevel@tonic-gate  * Systemcall entry point for zone_getattr(2).
44790Sstevel@tonic-gate  */
44800Sstevel@tonic-gate static ssize_t
44810Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
44820Sstevel@tonic-gate {
44830Sstevel@tonic-gate 	size_t size;
44840Sstevel@tonic-gate 	int error = 0, err;
44850Sstevel@tonic-gate 	zone_t *zone;
44860Sstevel@tonic-gate 	char *zonepath;
44872267Sdp 	char *outstr;
44880Sstevel@tonic-gate 	zone_status_t zone_status;
44890Sstevel@tonic-gate 	pid_t initpid;
44903792Sakolb 	boolean_t global = (curzone == global_zone);
44913792Sakolb 	boolean_t inzone = (curzone->zone_id == zoneid);
44923448Sdh155122 	ushort_t flags;
44930Sstevel@tonic-gate 
44940Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
44950Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
44960Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
44970Sstevel@tonic-gate 		return (set_errno(EINVAL));
44980Sstevel@tonic-gate 	}
44990Sstevel@tonic-gate 	zone_status = zone_status_get(zone);
45005880Snordmark 	if (zone_status < ZONE_IS_INITIALIZED) {
45010Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
45020Sstevel@tonic-gate 		return (set_errno(EINVAL));
45030Sstevel@tonic-gate 	}
45040Sstevel@tonic-gate 	zone_hold(zone);
45050Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
45060Sstevel@tonic-gate 
45070Sstevel@tonic-gate 	/*
45081676Sjpk 	 * If not in the global zone, don't show information about other zones,
45091676Sjpk 	 * unless the system is labeled and the local zone's label dominates
45101676Sjpk 	 * the other zone.
45110Sstevel@tonic-gate 	 */
45121676Sjpk 	if (!zone_list_access(zone)) {
45130Sstevel@tonic-gate 		zone_rele(zone);
45140Sstevel@tonic-gate 		return (set_errno(EINVAL));
45150Sstevel@tonic-gate 	}
45160Sstevel@tonic-gate 
45170Sstevel@tonic-gate 	switch (attr) {
45180Sstevel@tonic-gate 	case ZONE_ATTR_ROOT:
45190Sstevel@tonic-gate 		if (global) {
45200Sstevel@tonic-gate 			/*
45210Sstevel@tonic-gate 			 * Copy the path to trim the trailing "/" (except for
45220Sstevel@tonic-gate 			 * the global zone).
45230Sstevel@tonic-gate 			 */
45240Sstevel@tonic-gate 			if (zone != global_zone)
45250Sstevel@tonic-gate 				size = zone->zone_rootpathlen - 1;
45260Sstevel@tonic-gate 			else
45270Sstevel@tonic-gate 				size = zone->zone_rootpathlen;
45280Sstevel@tonic-gate 			zonepath = kmem_alloc(size, KM_SLEEP);
45290Sstevel@tonic-gate 			bcopy(zone->zone_rootpath, zonepath, size);
45300Sstevel@tonic-gate 			zonepath[size - 1] = '\0';
45310Sstevel@tonic-gate 		} else {
45323792Sakolb 			if (inzone || !is_system_labeled()) {
45331676Sjpk 				/*
45341676Sjpk 				 * Caller is not in the global zone.
45351676Sjpk 				 * if the query is on the current zone
45361676Sjpk 				 * or the system is not labeled,
45371676Sjpk 				 * just return faked-up path for current zone.
45381676Sjpk 				 */
45391676Sjpk 				zonepath = "/";
45401676Sjpk 				size = 2;
45411676Sjpk 			} else {
45421676Sjpk 				/*
45431676Sjpk 				 * Return related path for current zone.
45441676Sjpk 				 */
45451676Sjpk 				int prefix_len = strlen(zone_prefix);
45461676Sjpk 				int zname_len = strlen(zone->zone_name);
45471676Sjpk 
45481676Sjpk 				size = prefix_len + zname_len + 1;
45491676Sjpk 				zonepath = kmem_alloc(size, KM_SLEEP);
45501676Sjpk 				bcopy(zone_prefix, zonepath, prefix_len);
45511676Sjpk 				bcopy(zone->zone_name, zonepath +
45522267Sdp 				    prefix_len, zname_len);
45531676Sjpk 				zonepath[size - 1] = '\0';
45541676Sjpk 			}
45550Sstevel@tonic-gate 		}
45560Sstevel@tonic-gate 		if (bufsize > size)
45570Sstevel@tonic-gate 			bufsize = size;
45580Sstevel@tonic-gate 		if (buf != NULL) {
45590Sstevel@tonic-gate 			err = copyoutstr(zonepath, buf, bufsize, NULL);
45600Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
45610Sstevel@tonic-gate 				error = EFAULT;
45620Sstevel@tonic-gate 		}
45633792Sakolb 		if (global || (is_system_labeled() && !inzone))
45640Sstevel@tonic-gate 			kmem_free(zonepath, size);
45650Sstevel@tonic-gate 		break;
45660Sstevel@tonic-gate 
45670Sstevel@tonic-gate 	case ZONE_ATTR_NAME:
45680Sstevel@tonic-gate 		size = strlen(zone->zone_name) + 1;
45690Sstevel@tonic-gate 		if (bufsize > size)
45700Sstevel@tonic-gate 			bufsize = size;
45710Sstevel@tonic-gate 		if (buf != NULL) {
45720Sstevel@tonic-gate 			err = copyoutstr(zone->zone_name, buf, bufsize, NULL);
45730Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
45740Sstevel@tonic-gate 				error = EFAULT;
45750Sstevel@tonic-gate 		}
45760Sstevel@tonic-gate 		break;
45770Sstevel@tonic-gate 
45780Sstevel@tonic-gate 	case ZONE_ATTR_STATUS:
45790Sstevel@tonic-gate 		/*
45800Sstevel@tonic-gate 		 * Since we're not holding zonehash_lock, the zone status
45810Sstevel@tonic-gate 		 * may be anything; leave it up to userland to sort it out.
45820Sstevel@tonic-gate 		 */
45830Sstevel@tonic-gate 		size = sizeof (zone_status);
45840Sstevel@tonic-gate 		if (bufsize > size)
45850Sstevel@tonic-gate 			bufsize = size;
45860Sstevel@tonic-gate 		zone_status = zone_status_get(zone);
45870Sstevel@tonic-gate 		if (buf != NULL &&
45880Sstevel@tonic-gate 		    copyout(&zone_status, buf, bufsize) != 0)
45890Sstevel@tonic-gate 			error = EFAULT;
45900Sstevel@tonic-gate 		break;
45913448Sdh155122 	case ZONE_ATTR_FLAGS:
45923448Sdh155122 		size = sizeof (zone->zone_flags);
45933448Sdh155122 		if (bufsize > size)
45943448Sdh155122 			bufsize = size;
45953448Sdh155122 		flags = zone->zone_flags;
45963448Sdh155122 		if (buf != NULL &&
45973448Sdh155122 		    copyout(&flags, buf, bufsize) != 0)
45983448Sdh155122 			error = EFAULT;
45993448Sdh155122 		break;
46000Sstevel@tonic-gate 	case ZONE_ATTR_PRIVSET:
46010Sstevel@tonic-gate 		size = sizeof (priv_set_t);
46020Sstevel@tonic-gate 		if (bufsize > size)
46030Sstevel@tonic-gate 			bufsize = size;
46040Sstevel@tonic-gate 		if (buf != NULL &&
46050Sstevel@tonic-gate 		    copyout(zone->zone_privset, buf, bufsize) != 0)
46060Sstevel@tonic-gate 			error = EFAULT;
46070Sstevel@tonic-gate 		break;
46080Sstevel@tonic-gate 	case ZONE_ATTR_UNIQID:
46090Sstevel@tonic-gate 		size = sizeof (zone->zone_uniqid);
46100Sstevel@tonic-gate 		if (bufsize > size)
46110Sstevel@tonic-gate 			bufsize = size;
46120Sstevel@tonic-gate 		if (buf != NULL &&
46130Sstevel@tonic-gate 		    copyout(&zone->zone_uniqid, buf, bufsize) != 0)
46140Sstevel@tonic-gate 			error = EFAULT;
46150Sstevel@tonic-gate 		break;
46160Sstevel@tonic-gate 	case ZONE_ATTR_POOLID:
46170Sstevel@tonic-gate 		{
46180Sstevel@tonic-gate 			pool_t *pool;
46190Sstevel@tonic-gate 			poolid_t poolid;
46200Sstevel@tonic-gate 
46210Sstevel@tonic-gate 			if (pool_lock_intr() != 0) {
46220Sstevel@tonic-gate 				error = EINTR;
46230Sstevel@tonic-gate 				break;
46240Sstevel@tonic-gate 			}
46250Sstevel@tonic-gate 			pool = zone_pool_get(zone);
46260Sstevel@tonic-gate 			poolid = pool->pool_id;
46270Sstevel@tonic-gate 			pool_unlock();
46280Sstevel@tonic-gate 			size = sizeof (poolid);
46290Sstevel@tonic-gate 			if (bufsize > size)
46300Sstevel@tonic-gate 				bufsize = size;
46310Sstevel@tonic-gate 			if (buf != NULL && copyout(&poolid, buf, size) != 0)
46320Sstevel@tonic-gate 				error = EFAULT;
46330Sstevel@tonic-gate 		}
46340Sstevel@tonic-gate 		break;
46351676Sjpk 	case ZONE_ATTR_SLBL:
46361676Sjpk 		size = sizeof (bslabel_t);
46371676Sjpk 		if (bufsize > size)
46381676Sjpk 			bufsize = size;
46391676Sjpk 		if (zone->zone_slabel == NULL)
46401676Sjpk 			error = EINVAL;
46411676Sjpk 		else if (buf != NULL &&
46421676Sjpk 		    copyout(label2bslabel(zone->zone_slabel), buf,
46431676Sjpk 		    bufsize) != 0)
46441676Sjpk 			error = EFAULT;
46451676Sjpk 		break;
46460Sstevel@tonic-gate 	case ZONE_ATTR_INITPID:
46470Sstevel@tonic-gate 		size = sizeof (initpid);
46480Sstevel@tonic-gate 		if (bufsize > size)
46490Sstevel@tonic-gate 			bufsize = size;
46500Sstevel@tonic-gate 		initpid = zone->zone_proc_initpid;
46510Sstevel@tonic-gate 		if (initpid == -1) {
46520Sstevel@tonic-gate 			error = ESRCH;
46530Sstevel@tonic-gate 			break;
46540Sstevel@tonic-gate 		}
46550Sstevel@tonic-gate 		if (buf != NULL &&
46560Sstevel@tonic-gate 		    copyout(&initpid, buf, bufsize) != 0)
46570Sstevel@tonic-gate 			error = EFAULT;
46580Sstevel@tonic-gate 		break;
46592712Snn35248 	case ZONE_ATTR_BRAND:
46602712Snn35248 		size = strlen(zone->zone_brand->b_name) + 1;
46612712Snn35248 
46622712Snn35248 		if (bufsize > size)
46632712Snn35248 			bufsize = size;
46642712Snn35248 		if (buf != NULL) {
46652712Snn35248 			err = copyoutstr(zone->zone_brand->b_name, buf,
46662712Snn35248 			    bufsize, NULL);
46672712Snn35248 			if (err != 0 && err != ENAMETOOLONG)
46682712Snn35248 				error = EFAULT;
46692712Snn35248 		}
46702712Snn35248 		break;
46712267Sdp 	case ZONE_ATTR_INITNAME:
46722267Sdp 		size = strlen(zone->zone_initname) + 1;
46732267Sdp 		if (bufsize > size)
46742267Sdp 			bufsize = size;
46752267Sdp 		if (buf != NULL) {
46762267Sdp 			err = copyoutstr(zone->zone_initname, buf, bufsize,
46772267Sdp 			    NULL);
46782267Sdp 			if (err != 0 && err != ENAMETOOLONG)
46792267Sdp 				error = EFAULT;
46802267Sdp 		}
46812267Sdp 		break;
46822267Sdp 	case ZONE_ATTR_BOOTARGS:
46832267Sdp 		if (zone->zone_bootargs == NULL)
46842267Sdp 			outstr = "";
46852267Sdp 		else
46862267Sdp 			outstr = zone->zone_bootargs;
46872267Sdp 		size = strlen(outstr) + 1;
46882267Sdp 		if (bufsize > size)
46892267Sdp 			bufsize = size;
46902267Sdp 		if (buf != NULL) {
46912267Sdp 			err = copyoutstr(outstr, buf, bufsize, NULL);
46922267Sdp 			if (err != 0 && err != ENAMETOOLONG)
46932267Sdp 				error = EFAULT;
46942267Sdp 		}
46952267Sdp 		break;
46963247Sgjelinek 	case ZONE_ATTR_PHYS_MCAP:
46973247Sgjelinek 		size = sizeof (zone->zone_phys_mcap);
46983247Sgjelinek 		if (bufsize > size)
46993247Sgjelinek 			bufsize = size;
47003247Sgjelinek 		if (buf != NULL &&
47013247Sgjelinek 		    copyout(&zone->zone_phys_mcap, buf, bufsize) != 0)
47023247Sgjelinek 			error = EFAULT;
47033247Sgjelinek 		break;
47043247Sgjelinek 	case ZONE_ATTR_SCHED_CLASS:
47053247Sgjelinek 		mutex_enter(&class_lock);
47063247Sgjelinek 
47073247Sgjelinek 		if (zone->zone_defaultcid >= loaded_classes)
47083247Sgjelinek 			outstr = "";
47093247Sgjelinek 		else
47103247Sgjelinek 			outstr = sclass[zone->zone_defaultcid].cl_name;
47113247Sgjelinek 		size = strlen(outstr) + 1;
47123247Sgjelinek 		if (bufsize > size)
47133247Sgjelinek 			bufsize = size;
47143247Sgjelinek 		if (buf != NULL) {
47153247Sgjelinek 			err = copyoutstr(outstr, buf, bufsize, NULL);
47163247Sgjelinek 			if (err != 0 && err != ENAMETOOLONG)
47173247Sgjelinek 				error = EFAULT;
47183247Sgjelinek 		}
47193247Sgjelinek 
47203247Sgjelinek 		mutex_exit(&class_lock);
47213247Sgjelinek 		break;
47220Sstevel@tonic-gate 	default:
47232712Snn35248 		if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) {
47242712Snn35248 			size = bufsize;
47252712Snn35248 			error = ZBROP(zone)->b_getattr(zone, attr, buf, &size);
47262712Snn35248 		} else {
47272712Snn35248 			error = EINVAL;
47282712Snn35248 		}
47290Sstevel@tonic-gate 	}
47300Sstevel@tonic-gate 	zone_rele(zone);
47310Sstevel@tonic-gate 
47320Sstevel@tonic-gate 	if (error)
47330Sstevel@tonic-gate 		return (set_errno(error));
47340Sstevel@tonic-gate 	return ((ssize_t)size);
47350Sstevel@tonic-gate }
47360Sstevel@tonic-gate 
47370Sstevel@tonic-gate /*
47382267Sdp  * Systemcall entry point for zone_setattr(2).
47392267Sdp  */
47402267Sdp /*ARGSUSED*/
47412267Sdp static int
47422267Sdp zone_setattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
47432267Sdp {
47442267Sdp 	zone_t *zone;
47452267Sdp 	zone_status_t zone_status;
47462267Sdp 	int err;
47472267Sdp 
47482267Sdp 	if (secpolicy_zone_config(CRED()) != 0)
47492267Sdp 		return (set_errno(EPERM));
47502267Sdp 
47512267Sdp 	/*
47523247Sgjelinek 	 * Only the ZONE_ATTR_PHYS_MCAP attribute can be set on the
47533247Sgjelinek 	 * global zone.
47542267Sdp 	 */
47553247Sgjelinek 	if (zoneid == GLOBAL_ZONEID && attr != ZONE_ATTR_PHYS_MCAP) {
47562267Sdp 		return (set_errno(EINVAL));
47572267Sdp 	}
47582267Sdp 
47592267Sdp 	mutex_enter(&zonehash_lock);
47602267Sdp 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
47612267Sdp 		mutex_exit(&zonehash_lock);
47622267Sdp 		return (set_errno(EINVAL));
47632267Sdp 	}
47642267Sdp 	zone_hold(zone);
47652267Sdp 	mutex_exit(&zonehash_lock);
47662267Sdp 
47673247Sgjelinek 	/*
47683247Sgjelinek 	 * At present most attributes can only be set on non-running,
47693247Sgjelinek 	 * non-global zones.
47703247Sgjelinek 	 */
47712267Sdp 	zone_status = zone_status_get(zone);
47723247Sgjelinek 	if (attr != ZONE_ATTR_PHYS_MCAP && zone_status > ZONE_IS_READY)
47732267Sdp 		goto done;
47742267Sdp 
47752267Sdp 	switch (attr) {
47762267Sdp 	case ZONE_ATTR_INITNAME:
47772267Sdp 		err = zone_set_initname(zone, (const char *)buf);
47782267Sdp 		break;
47792267Sdp 	case ZONE_ATTR_BOOTARGS:
47802267Sdp 		err = zone_set_bootargs(zone, (const char *)buf);
47812267Sdp 		break;
47822712Snn35248 	case ZONE_ATTR_BRAND:
47834141Sedp 		err = zone_set_brand(zone, (const char *)buf);
47842712Snn35248 		break;
47853247Sgjelinek 	case ZONE_ATTR_PHYS_MCAP:
47863247Sgjelinek 		err = zone_set_phys_mcap(zone, (const uint64_t *)buf);
47873247Sgjelinek 		break;
47883247Sgjelinek 	case ZONE_ATTR_SCHED_CLASS:
47893247Sgjelinek 		err = zone_set_sched_class(zone, (const char *)buf);
47903247Sgjelinek 		break;
47912267Sdp 	default:
47922712Snn35248 		if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone))
47932712Snn35248 			err = ZBROP(zone)->b_setattr(zone, attr, buf, bufsize);
47942712Snn35248 		else
47952712Snn35248 			err = EINVAL;
47962267Sdp 	}
47972267Sdp 
47982267Sdp done:
47992267Sdp 	zone_rele(zone);
48002267Sdp 	return (err != 0 ? set_errno(err) : 0);
48012267Sdp }
48022267Sdp 
48032267Sdp /*
48040Sstevel@tonic-gate  * Return zero if the process has at least one vnode mapped in to its
48050Sstevel@tonic-gate  * address space which shouldn't be allowed to change zones.
48063247Sgjelinek  *
48073247Sgjelinek  * Also return zero if the process has any shared mappings which reserve
48083247Sgjelinek  * swap.  This is because the counting for zone.max-swap does not allow swap
48095331Samw  * reservation to be shared between zones.  zone swap reservation is counted
48103247Sgjelinek  * on zone->zone_max_swap.
48110Sstevel@tonic-gate  */
48120Sstevel@tonic-gate static int
48130Sstevel@tonic-gate as_can_change_zones(void)
48140Sstevel@tonic-gate {
48150Sstevel@tonic-gate 	proc_t *pp = curproc;
48160Sstevel@tonic-gate 	struct seg *seg;
48170Sstevel@tonic-gate 	struct as *as = pp->p_as;
48180Sstevel@tonic-gate 	vnode_t *vp;
48190Sstevel@tonic-gate 	int allow = 1;
48200Sstevel@tonic-gate 
48210Sstevel@tonic-gate 	ASSERT(pp->p_as != &kas);
48223247Sgjelinek 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
48230Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
48243247Sgjelinek 
48253247Sgjelinek 		/*
48263247Sgjelinek 		 * Cannot enter zone with shared anon memory which
48273247Sgjelinek 		 * reserves swap.  See comment above.
48283247Sgjelinek 		 */
48293247Sgjelinek 		if (seg_can_change_zones(seg) == B_FALSE) {
48303247Sgjelinek 			allow = 0;
48313247Sgjelinek 			break;
48323247Sgjelinek 		}
48330Sstevel@tonic-gate 		/*
48340Sstevel@tonic-gate 		 * if we can't get a backing vnode for this segment then skip
48350Sstevel@tonic-gate 		 * it.
48360Sstevel@tonic-gate 		 */
48370Sstevel@tonic-gate 		vp = NULL;
48380Sstevel@tonic-gate 		if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL)
48390Sstevel@tonic-gate 			continue;
48400Sstevel@tonic-gate 		if (!vn_can_change_zones(vp)) { /* bail on first match */
48410Sstevel@tonic-gate 			allow = 0;
48420Sstevel@tonic-gate 			break;
48430Sstevel@tonic-gate 		}
48440Sstevel@tonic-gate 	}
48453247Sgjelinek 	AS_LOCK_EXIT(as, &as->a_lock);
48460Sstevel@tonic-gate 	return (allow);
48470Sstevel@tonic-gate }
48480Sstevel@tonic-gate 
48490Sstevel@tonic-gate /*
48503247Sgjelinek  * Count swap reserved by curproc's address space
48513247Sgjelinek  */
48523247Sgjelinek static size_t
48533247Sgjelinek as_swresv(void)
48543247Sgjelinek {
48553247Sgjelinek 	proc_t *pp = curproc;
48563247Sgjelinek 	struct seg *seg;
48573247Sgjelinek 	struct as *as = pp->p_as;
48583247Sgjelinek 	size_t swap = 0;
48593247Sgjelinek 
48603247Sgjelinek 	ASSERT(pp->p_as != &kas);
48613247Sgjelinek 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
48623247Sgjelinek 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg))
48633247Sgjelinek 		swap += seg_swresv(seg);
48643247Sgjelinek 
48653247Sgjelinek 	return (swap);
48663247Sgjelinek }
48673247Sgjelinek 
48683247Sgjelinek /*
48690Sstevel@tonic-gate  * Systemcall entry point for zone_enter().
48700Sstevel@tonic-gate  *
48710Sstevel@tonic-gate  * The current process is injected into said zone.  In the process
48720Sstevel@tonic-gate  * it will change its project membership, privileges, rootdir/cwd,
48730Sstevel@tonic-gate  * zone-wide rctls, and pool association to match those of the zone.
48740Sstevel@tonic-gate  *
48750Sstevel@tonic-gate  * The first zone_enter() called while the zone is in the ZONE_IS_READY
48760Sstevel@tonic-gate  * state will transition it to ZONE_IS_RUNNING.  Processes may only
48770Sstevel@tonic-gate  * enter a zone that is "ready" or "running".
48780Sstevel@tonic-gate  */
48790Sstevel@tonic-gate static int
48800Sstevel@tonic-gate zone_enter(zoneid_t zoneid)
48810Sstevel@tonic-gate {
48820Sstevel@tonic-gate 	zone_t *zone;
48830Sstevel@tonic-gate 	vnode_t *vp;
48840Sstevel@tonic-gate 	proc_t *pp = curproc;
48850Sstevel@tonic-gate 	contract_t *ct;
48860Sstevel@tonic-gate 	cont_process_t *ctp;
48870Sstevel@tonic-gate 	task_t *tk, *oldtk;
48880Sstevel@tonic-gate 	kproject_t *zone_proj0;
48890Sstevel@tonic-gate 	cred_t *cr, *newcr;
48900Sstevel@tonic-gate 	pool_t *oldpool, *newpool;
48910Sstevel@tonic-gate 	sess_t *sp;
48920Sstevel@tonic-gate 	uid_t uid;
48930Sstevel@tonic-gate 	zone_status_t status;
48940Sstevel@tonic-gate 	int err = 0;
48950Sstevel@tonic-gate 	rctl_entity_p_t e;
48963247Sgjelinek 	size_t swap;
48973792Sakolb 	kthread_id_t t;
48980Sstevel@tonic-gate 
48990Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
49000Sstevel@tonic-gate 		return (set_errno(EPERM));
49010Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
49020Sstevel@tonic-gate 		return (set_errno(EINVAL));
49030Sstevel@tonic-gate 
49040Sstevel@tonic-gate 	/*
49050Sstevel@tonic-gate 	 * Stop all lwps so we don't need to hold a lock to look at
49060Sstevel@tonic-gate 	 * curproc->p_zone.  This needs to happen before we grab any
49070Sstevel@tonic-gate 	 * locks to avoid deadlock (another lwp in the process could
49080Sstevel@tonic-gate 	 * be waiting for the held lock).
49090Sstevel@tonic-gate 	 */
49100Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK))
49110Sstevel@tonic-gate 		return (set_errno(EINTR));
49120Sstevel@tonic-gate 
49130Sstevel@tonic-gate 	/*
49140Sstevel@tonic-gate 	 * Make sure we're not changing zones with files open or mapped in
49150Sstevel@tonic-gate 	 * to our address space which shouldn't be changing zones.
49160Sstevel@tonic-gate 	 */
49170Sstevel@tonic-gate 	if (!files_can_change_zones()) {
49180Sstevel@tonic-gate 		err = EBADF;
49190Sstevel@tonic-gate 		goto out;
49200Sstevel@tonic-gate 	}
49210Sstevel@tonic-gate 	if (!as_can_change_zones()) {
49220Sstevel@tonic-gate 		err = EFAULT;
49230Sstevel@tonic-gate 		goto out;
49240Sstevel@tonic-gate 	}
49250Sstevel@tonic-gate 
49260Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
49270Sstevel@tonic-gate 	if (pp->p_zone != global_zone) {
49280Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
49290Sstevel@tonic-gate 		err = EINVAL;
49300Sstevel@tonic-gate 		goto out;
49310Sstevel@tonic-gate 	}
49320Sstevel@tonic-gate 
49330Sstevel@tonic-gate 	zone = zone_find_all_by_id(zoneid);
49340Sstevel@tonic-gate 	if (zone == NULL) {
49350Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
49360Sstevel@tonic-gate 		err = EINVAL;
49370Sstevel@tonic-gate 		goto out;
49380Sstevel@tonic-gate 	}
49390Sstevel@tonic-gate 
49400Sstevel@tonic-gate 	/*
49410Sstevel@tonic-gate 	 * To prevent processes in a zone from holding contracts on
49420Sstevel@tonic-gate 	 * extrazonal resources, and to avoid process contract
49430Sstevel@tonic-gate 	 * memberships which span zones, contract holders and processes
49440Sstevel@tonic-gate 	 * which aren't the sole members of their encapsulating process
49450Sstevel@tonic-gate 	 * contracts are not allowed to zone_enter.
49460Sstevel@tonic-gate 	 */
49470Sstevel@tonic-gate 	ctp = pp->p_ct_process;
49480Sstevel@tonic-gate 	ct = &ctp->conp_contract;
49490Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
49500Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
49510Sstevel@tonic-gate 	if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) {
49520Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
49530Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
49540Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
49550Sstevel@tonic-gate 		err = EINVAL;
49560Sstevel@tonic-gate 		goto out;
49570Sstevel@tonic-gate 	}
49580Sstevel@tonic-gate 
49590Sstevel@tonic-gate 	/*
49600Sstevel@tonic-gate 	 * Moreover, we don't allow processes whose encapsulating
49610Sstevel@tonic-gate 	 * process contracts have inherited extrazonal contracts.
49620Sstevel@tonic-gate 	 * While it would be easier to eliminate all process contracts
49630Sstevel@tonic-gate 	 * with inherited contracts, we need to be able to give a
49640Sstevel@tonic-gate 	 * restarted init (or other zone-penetrating process) its
49650Sstevel@tonic-gate 	 * predecessor's contracts.
49660Sstevel@tonic-gate 	 */
49670Sstevel@tonic-gate 	if (ctp->conp_ninherited != 0) {
49680Sstevel@tonic-gate 		contract_t *next;
49690Sstevel@tonic-gate 		for (next = list_head(&ctp->conp_inherited); next;
49700Sstevel@tonic-gate 		    next = list_next(&ctp->conp_inherited, next)) {
49710Sstevel@tonic-gate 			if (contract_getzuniqid(next) != zone->zone_uniqid) {
49720Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
49730Sstevel@tonic-gate 				mutex_exit(&ct->ct_lock);
49740Sstevel@tonic-gate 				mutex_exit(&zonehash_lock);
49750Sstevel@tonic-gate 				err = EINVAL;
49760Sstevel@tonic-gate 				goto out;
49770Sstevel@tonic-gate 			}
49780Sstevel@tonic-gate 		}
49790Sstevel@tonic-gate 	}
4980*6073Sacruz 
49810Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
49820Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
49830Sstevel@tonic-gate 
49840Sstevel@tonic-gate 	status = zone_status_get(zone);
49850Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) {
49860Sstevel@tonic-gate 		/*
49870Sstevel@tonic-gate 		 * Can't join
49880Sstevel@tonic-gate 		 */
49890Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
49900Sstevel@tonic-gate 		err = EINVAL;
49910Sstevel@tonic-gate 		goto out;
49920Sstevel@tonic-gate 	}
49930Sstevel@tonic-gate 
49940Sstevel@tonic-gate 	/*
49950Sstevel@tonic-gate 	 * Make sure new priv set is within the permitted set for caller
49960Sstevel@tonic-gate 	 */
49970Sstevel@tonic-gate 	if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) {
49980Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
49990Sstevel@tonic-gate 		err = EPERM;
50000Sstevel@tonic-gate 		goto out;
50010Sstevel@tonic-gate 	}
50020Sstevel@tonic-gate 	/*
50030Sstevel@tonic-gate 	 * We want to momentarily drop zonehash_lock while we optimistically
50040Sstevel@tonic-gate 	 * bind curproc to the pool it should be running in.  This is safe
50050Sstevel@tonic-gate 	 * since the zone can't disappear (we have a hold on it).
50060Sstevel@tonic-gate 	 */
50070Sstevel@tonic-gate 	zone_hold(zone);
50080Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
50090Sstevel@tonic-gate 
50100Sstevel@tonic-gate 	/*
50110Sstevel@tonic-gate 	 * Grab pool_lock to keep the pools configuration from changing
50120Sstevel@tonic-gate 	 * and to stop ourselves from getting rebound to another pool
50130Sstevel@tonic-gate 	 * until we join the zone.
50140Sstevel@tonic-gate 	 */
50150Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
50160Sstevel@tonic-gate 		zone_rele(zone);
50170Sstevel@tonic-gate 		err = EINTR;
50180Sstevel@tonic-gate 		goto out;
50190Sstevel@tonic-gate 	}
50200Sstevel@tonic-gate 	ASSERT(secpolicy_pool(CRED()) == 0);
50210Sstevel@tonic-gate 	/*
50220Sstevel@tonic-gate 	 * Bind ourselves to the pool currently associated with the zone.
50230Sstevel@tonic-gate 	 */
50240Sstevel@tonic-gate 	oldpool = curproc->p_pool;
50250Sstevel@tonic-gate 	newpool = zone_pool_get(zone);
50260Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED && newpool != oldpool &&
50270Sstevel@tonic-gate 	    (err = pool_do_bind(newpool, P_PID, P_MYID,
50280Sstevel@tonic-gate 	    POOL_BIND_ALL)) != 0) {
50290Sstevel@tonic-gate 		pool_unlock();
50300Sstevel@tonic-gate 		zone_rele(zone);
50310Sstevel@tonic-gate 		goto out;
50320Sstevel@tonic-gate 	}
50330Sstevel@tonic-gate 
50340Sstevel@tonic-gate 	/*
50350Sstevel@tonic-gate 	 * Grab cpu_lock now; we'll need it later when we call
50360Sstevel@tonic-gate 	 * task_join().
50370Sstevel@tonic-gate 	 */
50380Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
50390Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
50400Sstevel@tonic-gate 	/*
50410Sstevel@tonic-gate 	 * Make sure the zone hasn't moved on since we dropped zonehash_lock.
50420Sstevel@tonic-gate 	 */
50430Sstevel@tonic-gate 	if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
50440Sstevel@tonic-gate 		/*
50450Sstevel@tonic-gate 		 * Can't join anymore.
50460Sstevel@tonic-gate 		 */
50470Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
50480Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
50490Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED &&
50500Sstevel@tonic-gate 		    newpool != oldpool)
50510Sstevel@tonic-gate 			(void) pool_do_bind(oldpool, P_PID, P_MYID,
50520Sstevel@tonic-gate 			    POOL_BIND_ALL);
50530Sstevel@tonic-gate 		pool_unlock();
50540Sstevel@tonic-gate 		zone_rele(zone);
50550Sstevel@tonic-gate 		err = EINVAL;
50560Sstevel@tonic-gate 		goto out;
50570Sstevel@tonic-gate 	}
50580Sstevel@tonic-gate 
50593247Sgjelinek 	/*
50603247Sgjelinek 	 * a_lock must be held while transfering locked memory and swap
50613247Sgjelinek 	 * reservation from the global zone to the non global zone because
50623247Sgjelinek 	 * asynchronous faults on the processes' address space can lock
50633247Sgjelinek 	 * memory and reserve swap via MCL_FUTURE and MAP_NORESERVE
50643247Sgjelinek 	 * segments respectively.
50653247Sgjelinek 	 */
50663247Sgjelinek 	AS_LOCK_ENTER(pp->as, &pp->p_as->a_lock, RW_WRITER);
50673247Sgjelinek 	swap = as_swresv();
50680Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
50690Sstevel@tonic-gate 	zone_proj0 = zone->zone_zsched->p_task->tk_proj;
50700Sstevel@tonic-gate 	/* verify that we do not exceed and task or lwp limits */
50710Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
50720Sstevel@tonic-gate 	/* add new lwps to zone and zone's proj0 */
50730Sstevel@tonic-gate 	zone_proj0->kpj_nlwps += pp->p_lwpcnt;
50740Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
50750Sstevel@tonic-gate 	/* add 1 task to zone's proj0 */
50760Sstevel@tonic-gate 	zone_proj0->kpj_ntasks += 1;
50770Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
50780Sstevel@tonic-gate 
50793247Sgjelinek 	mutex_enter(&zone->zone_mem_lock);
50802768Ssl108498 	zone->zone_locked_mem += pp->p_locked_mem;
50812768Ssl108498 	zone_proj0->kpj_data.kpd_locked_mem += pp->p_locked_mem;
50823247Sgjelinek 	zone->zone_max_swap += swap;
50833247Sgjelinek 	mutex_exit(&zone->zone_mem_lock);
50842768Ssl108498 
50853916Skrishna 	mutex_enter(&(zone_proj0->kpj_data.kpd_crypto_lock));
50863916Skrishna 	zone_proj0->kpj_data.kpd_crypto_mem += pp->p_crypto_mem;
50873916Skrishna 	mutex_exit(&(zone_proj0->kpj_data.kpd_crypto_lock));
50883916Skrishna 
50890Sstevel@tonic-gate 	/* remove lwps from proc's old zone and old project */
50900Sstevel@tonic-gate 	mutex_enter(&pp->p_zone->zone_nlwps_lock);
50910Sstevel@tonic-gate 	pp->p_zone->zone_nlwps -= pp->p_lwpcnt;
50920Sstevel@tonic-gate 	pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt;
50930Sstevel@tonic-gate 	mutex_exit(&pp->p_zone->zone_nlwps_lock);
50940Sstevel@tonic-gate 
50953247Sgjelinek 	mutex_enter(&pp->p_zone->zone_mem_lock);
50962768Ssl108498 	pp->p_zone->zone_locked_mem -= pp->p_locked_mem;
50972768Ssl108498 	pp->p_task->tk_proj->kpj_data.kpd_locked_mem -= pp->p_locked_mem;
50983247Sgjelinek 	pp->p_zone->zone_max_swap -= swap;
50993247Sgjelinek 	mutex_exit(&pp->p_zone->zone_mem_lock);
51002768Ssl108498 
51013916Skrishna 	mutex_enter(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock));
51023916Skrishna 	pp->p_task->tk_proj->kpj_data.kpd_crypto_mem -= pp->p_crypto_mem;
51033916Skrishna 	mutex_exit(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock));
51043916Skrishna 
51052768Ssl108498 	mutex_exit(&pp->p_lock);
51063247Sgjelinek 	AS_LOCK_EXIT(pp->p_as, &pp->p_as->a_lock);
51072768Ssl108498 
51080Sstevel@tonic-gate 	/*
51090Sstevel@tonic-gate 	 * Joining the zone cannot fail from now on.
51100Sstevel@tonic-gate 	 *
51110Sstevel@tonic-gate 	 * This means that a lot of the following code can be commonized and
51120Sstevel@tonic-gate 	 * shared with zsched().
51130Sstevel@tonic-gate 	 */
51140Sstevel@tonic-gate 
51150Sstevel@tonic-gate 	/*
5116*6073Sacruz 	 * If the process contract fmri was inherited, we need to
5117*6073Sacruz 	 * flag this so that any contract status will not leak
5118*6073Sacruz 	 * extra zone information, svc_fmri in this case
5119*6073Sacruz 	 */
5120*6073Sacruz 	if (ctp->conp_svc_ctid != ct->ct_id) {
5121*6073Sacruz 		mutex_enter(&ct->ct_lock);
5122*6073Sacruz 		ctp->conp_svc_zone_enter = ct->ct_id;
5123*6073Sacruz 		mutex_exit(&ct->ct_lock);
5124*6073Sacruz 	}
5125*6073Sacruz 
5126*6073Sacruz 	/*
51270Sstevel@tonic-gate 	 * Reset the encapsulating process contract's zone.
51280Sstevel@tonic-gate 	 */
51290Sstevel@tonic-gate 	ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID);
51300Sstevel@tonic-gate 	contract_setzuniqid(ct, zone->zone_uniqid);
51310Sstevel@tonic-gate 
51320Sstevel@tonic-gate 	/*
51330Sstevel@tonic-gate 	 * Create a new task and associate the process with the project keyed
51340Sstevel@tonic-gate 	 * by (projid,zoneid).
51350Sstevel@tonic-gate 	 *
51360Sstevel@tonic-gate 	 * We might as well be in project 0; the global zone's projid doesn't
51370Sstevel@tonic-gate 	 * make much sense in a zone anyhow.
51380Sstevel@tonic-gate 	 *
51390Sstevel@tonic-gate 	 * This also increments zone_ntasks, and returns with p_lock held.
51400Sstevel@tonic-gate 	 */
51410Sstevel@tonic-gate 	tk = task_create(0, zone);
51420Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
51430Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
51440Sstevel@tonic-gate 
51450Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
51460Sstevel@tonic-gate 	pp->p_zone = zone;
51470Sstevel@tonic-gate 
51480Sstevel@tonic-gate 	/*
51490Sstevel@tonic-gate 	 * call RCTLOP_SET functions on this proc
51500Sstevel@tonic-gate 	 */
51510Sstevel@tonic-gate 	e.rcep_p.zone = zone;
51520Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
51530Sstevel@tonic-gate 	(void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL,
51540Sstevel@tonic-gate 	    RCD_CALLBACK);
51550Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
51560Sstevel@tonic-gate 
51570Sstevel@tonic-gate 	/*
51580Sstevel@tonic-gate 	 * We don't need to hold any of zsched's locks here; not only do we know
51590Sstevel@tonic-gate 	 * the process and zone aren't going away, we know its session isn't
51600Sstevel@tonic-gate 	 * changing either.
51610Sstevel@tonic-gate 	 *
51620Sstevel@tonic-gate 	 * By joining zsched's session here, we mimic the behavior in the
51630Sstevel@tonic-gate 	 * global zone of init's sid being the pid of sched.  We extend this
51640Sstevel@tonic-gate 	 * to all zlogin-like zone_enter()'ing processes as well.
51650Sstevel@tonic-gate 	 */
51660Sstevel@tonic-gate 	mutex_enter(&pidlock);
51670Sstevel@tonic-gate 	sp = zone->zone_zsched->p_sessp;
51682712Snn35248 	sess_hold(zone->zone_zsched);
51690Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
51700Sstevel@tonic-gate 	pgexit(pp);
51712712Snn35248 	sess_rele(pp->p_sessp, B_TRUE);
51720Sstevel@tonic-gate 	pp->p_sessp = sp;
51730Sstevel@tonic-gate 	pgjoin(pp, zone->zone_zsched->p_pidp);
51743247Sgjelinek 
51753247Sgjelinek 	/*
51763792Sakolb 	 * If any threads are scheduled to be placed on zone wait queue they
51773792Sakolb 	 * should abandon the idea since the wait queue is changing.
51783792Sakolb 	 * We need to be holding pidlock & p_lock to do this.
51793792Sakolb 	 */
51803792Sakolb 	if ((t = pp->p_tlist) != NULL) {
51813792Sakolb 		do {
51823792Sakolb 			thread_lock(t);
51833792Sakolb 			/*
51843792Sakolb 			 * Kick this thread so that he doesn't sit
51853792Sakolb 			 * on a wrong wait queue.
51863792Sakolb 			 */
51873792Sakolb 			if (ISWAITING(t))
51883792Sakolb 				setrun_locked(t);
51893792Sakolb 
51903792Sakolb 			if (t->t_schedflag & TS_ANYWAITQ)
51913792Sakolb 				t->t_schedflag &= ~ TS_ANYWAITQ;
51923792Sakolb 
51933792Sakolb 			thread_unlock(t);
51943792Sakolb 		} while ((t = t->t_forw) != pp->p_tlist);
51953792Sakolb 	}
51963792Sakolb 
51973792Sakolb 	/*
51983247Sgjelinek 	 * If there is a default scheduling class for the zone and it is not
51993247Sgjelinek 	 * the class we are currently in, change all of the threads in the
52003247Sgjelinek 	 * process to the new class.  We need to be holding pidlock & p_lock
52013247Sgjelinek 	 * when we call parmsset so this is a good place to do it.
52023247Sgjelinek 	 */
52033247Sgjelinek 	if (zone->zone_defaultcid > 0 &&
52043247Sgjelinek 	    zone->zone_defaultcid != curthread->t_cid) {
52053247Sgjelinek 		pcparms_t pcparms;
52063247Sgjelinek 
52073247Sgjelinek 		pcparms.pc_cid = zone->zone_defaultcid;
52083247Sgjelinek 		pcparms.pc_clparms[0] = 0;
52093247Sgjelinek 
52103247Sgjelinek 		/*
52113247Sgjelinek 		 * If setting the class fails, we still want to enter the zone.
52123247Sgjelinek 		 */
52133247Sgjelinek 		if ((t = pp->p_tlist) != NULL) {
52143247Sgjelinek 			do {
52153247Sgjelinek 				(void) parmsset(&pcparms, t);
52163247Sgjelinek 			} while ((t = t->t_forw) != pp->p_tlist);
52173247Sgjelinek 		}
52183247Sgjelinek 	}
52193247Sgjelinek 
52200Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
52210Sstevel@tonic-gate 	mutex_exit(&pidlock);
52220Sstevel@tonic-gate 
52230Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
52240Sstevel@tonic-gate 	/*
52250Sstevel@tonic-gate 	 * We're firmly in the zone; let pools progress.
52260Sstevel@tonic-gate 	 */
52270Sstevel@tonic-gate 	pool_unlock();
52280Sstevel@tonic-gate 	task_rele(oldtk);
52290Sstevel@tonic-gate 	/*
52300Sstevel@tonic-gate 	 * We don't need to retain a hold on the zone since we already
52310Sstevel@tonic-gate 	 * incremented zone_ntasks, so the zone isn't going anywhere.
52320Sstevel@tonic-gate 	 */
52330Sstevel@tonic-gate 	zone_rele(zone);
52340Sstevel@tonic-gate 
52350Sstevel@tonic-gate 	/*
52360Sstevel@tonic-gate 	 * Chroot
52370Sstevel@tonic-gate 	 */
52380Sstevel@tonic-gate 	vp = zone->zone_rootvp;
52390Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_cdir, pp);
52400Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_rdir, pp);
52410Sstevel@tonic-gate 
52420Sstevel@tonic-gate 	/*
52430Sstevel@tonic-gate 	 * Change process credentials
52440Sstevel@tonic-gate 	 */
52450Sstevel@tonic-gate 	newcr = cralloc();
52460Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
52470Sstevel@tonic-gate 	cr = pp->p_cred;
52480Sstevel@tonic-gate 	crcopy_to(cr, newcr);
52490Sstevel@tonic-gate 	crsetzone(newcr, zone);
52500Sstevel@tonic-gate 	pp->p_cred = newcr;
52510Sstevel@tonic-gate 
52520Sstevel@tonic-gate 	/*
52530Sstevel@tonic-gate 	 * Restrict all process privilege sets to zone limit
52540Sstevel@tonic-gate 	 */
52550Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(newcr));
52560Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(newcr));
52570Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(newcr));
52580Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(newcr));
52590Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
52600Sstevel@tonic-gate 	crset(pp, newcr);
52610Sstevel@tonic-gate 
52620Sstevel@tonic-gate 	/*
52630Sstevel@tonic-gate 	 * Adjust upcount to reflect zone entry.
52640Sstevel@tonic-gate 	 */
52650Sstevel@tonic-gate 	uid = crgetruid(newcr);
52660Sstevel@tonic-gate 	mutex_enter(&pidlock);
52670Sstevel@tonic-gate 	upcount_dec(uid, GLOBAL_ZONEID);
52680Sstevel@tonic-gate 	upcount_inc(uid, zoneid);
52690Sstevel@tonic-gate 	mutex_exit(&pidlock);
52700Sstevel@tonic-gate 
52710Sstevel@tonic-gate 	/*
52720Sstevel@tonic-gate 	 * Set up core file path and content.
52730Sstevel@tonic-gate 	 */
52740Sstevel@tonic-gate 	set_core_defaults();
52750Sstevel@tonic-gate 
52760Sstevel@tonic-gate out:
52770Sstevel@tonic-gate 	/*
52780Sstevel@tonic-gate 	 * Let the other lwps continue.
52790Sstevel@tonic-gate 	 */
52800Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
52810Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
52820Sstevel@tonic-gate 		continuelwps(pp);
52830Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
52840Sstevel@tonic-gate 
52850Sstevel@tonic-gate 	return (err != 0 ? set_errno(err) : 0);
52860Sstevel@tonic-gate }
52870Sstevel@tonic-gate 
52880Sstevel@tonic-gate /*
52890Sstevel@tonic-gate  * Systemcall entry point for zone_list(2).
52900Sstevel@tonic-gate  *
52910Sstevel@tonic-gate  * Processes running in a (non-global) zone only see themselves.
52921676Sjpk  * On labeled systems, they see all zones whose label they dominate.
52930Sstevel@tonic-gate  */
52940Sstevel@tonic-gate static int
52950Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones)
52960Sstevel@tonic-gate {
52970Sstevel@tonic-gate 	zoneid_t *zoneids;
52981769Scarlsonj 	zone_t *zone, *myzone;
52990Sstevel@tonic-gate 	uint_t user_nzones, real_nzones;
53001676Sjpk 	uint_t domi_nzones;
53011676Sjpk 	int error;
53020Sstevel@tonic-gate 
53030Sstevel@tonic-gate 	if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0)
53040Sstevel@tonic-gate 		return (set_errno(EFAULT));
53050Sstevel@tonic-gate 
53061769Scarlsonj 	myzone = curproc->p_zone;
53071769Scarlsonj 	if (myzone != global_zone) {
53081676Sjpk 		bslabel_t *mybslab;
53091676Sjpk 
53101676Sjpk 		if (!is_system_labeled()) {
53111676Sjpk 			/* just return current zone */
53121676Sjpk 			real_nzones = domi_nzones = 1;
53131676Sjpk 			zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP);
53141769Scarlsonj 			zoneids[0] = myzone->zone_id;
53151676Sjpk 		} else {
53161676Sjpk 			/* return all zones that are dominated */
53171676Sjpk 			mutex_enter(&zonehash_lock);
53181676Sjpk 			real_nzones = zonecount;
53191676Sjpk 			domi_nzones = 0;
53201676Sjpk 			if (real_nzones > 0) {
53211676Sjpk 				zoneids = kmem_alloc(real_nzones *
53221676Sjpk 				    sizeof (zoneid_t), KM_SLEEP);
53231769Scarlsonj 				mybslab = label2bslabel(myzone->zone_slabel);
53241676Sjpk 				for (zone = list_head(&zone_active);
53251676Sjpk 				    zone != NULL;
53261676Sjpk 				    zone = list_next(&zone_active, zone)) {
53271676Sjpk 					if (zone->zone_id == GLOBAL_ZONEID)
53281676Sjpk 						continue;
53291769Scarlsonj 					if (zone != myzone &&
53301769Scarlsonj 					    (zone->zone_flags & ZF_IS_SCRATCH))
53311769Scarlsonj 						continue;
53321769Scarlsonj 					/*
53331769Scarlsonj 					 * Note that a label always dominates
53341769Scarlsonj 					 * itself, so myzone is always included
53351769Scarlsonj 					 * in the list.
53361769Scarlsonj 					 */
53371676Sjpk 					if (bldominates(mybslab,
53381676Sjpk 					    label2bslabel(zone->zone_slabel))) {
53391676Sjpk 						zoneids[domi_nzones++] =
53401676Sjpk 						    zone->zone_id;
53411676Sjpk 					}
53421676Sjpk 				}
53431676Sjpk 			}
53441676Sjpk 			mutex_exit(&zonehash_lock);
53451676Sjpk 		}
53460Sstevel@tonic-gate 	} else {
53470Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
53480Sstevel@tonic-gate 		real_nzones = zonecount;
53491676Sjpk 		domi_nzones = 0;
53501676Sjpk 		if (real_nzones > 0) {
53510Sstevel@tonic-gate 			zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t),
53520Sstevel@tonic-gate 			    KM_SLEEP);
53530Sstevel@tonic-gate 			for (zone = list_head(&zone_active); zone != NULL;
53540Sstevel@tonic-gate 			    zone = list_next(&zone_active, zone))
53551676Sjpk 				zoneids[domi_nzones++] = zone->zone_id;
53561676Sjpk 			ASSERT(domi_nzones == real_nzones);
53570Sstevel@tonic-gate 		}
53580Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
53590Sstevel@tonic-gate 	}
53600Sstevel@tonic-gate 
53611676Sjpk 	/*
53621676Sjpk 	 * If user has allocated space for fewer entries than we found, then
53631676Sjpk 	 * return only up to his limit.  Either way, tell him exactly how many
53641676Sjpk 	 * we found.
53651676Sjpk 	 */
53661676Sjpk 	if (domi_nzones < user_nzones)
53671676Sjpk 		user_nzones = domi_nzones;
53681676Sjpk 	error = 0;
53691676Sjpk 	if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) {
53700Sstevel@tonic-gate 		error = EFAULT;
53711676Sjpk 	} else if (zoneidlist != NULL && user_nzones != 0) {
53720Sstevel@tonic-gate 		if (copyout(zoneids, zoneidlist,
53730Sstevel@tonic-gate 		    user_nzones * sizeof (zoneid_t)) != 0)
53740Sstevel@tonic-gate 			error = EFAULT;
53750Sstevel@tonic-gate 	}
53760Sstevel@tonic-gate 
53771676Sjpk 	if (real_nzones > 0)
53780Sstevel@tonic-gate 		kmem_free(zoneids, real_nzones * sizeof (zoneid_t));
53790Sstevel@tonic-gate 
53801676Sjpk 	if (error != 0)
53810Sstevel@tonic-gate 		return (set_errno(error));
53820Sstevel@tonic-gate 	else
53830Sstevel@tonic-gate 		return (0);
53840Sstevel@tonic-gate }
53850Sstevel@tonic-gate 
53860Sstevel@tonic-gate /*
53870Sstevel@tonic-gate  * Systemcall entry point for zone_lookup(2).
53880Sstevel@tonic-gate  *
53891676Sjpk  * Non-global zones are only able to see themselves and (on labeled systems)
53901676Sjpk  * the zones they dominate.
53910Sstevel@tonic-gate  */
53920Sstevel@tonic-gate static zoneid_t
53930Sstevel@tonic-gate zone_lookup(const char *zone_name)
53940Sstevel@tonic-gate {
53950Sstevel@tonic-gate 	char *kname;
53960Sstevel@tonic-gate 	zone_t *zone;
53970Sstevel@tonic-gate 	zoneid_t zoneid;
53980Sstevel@tonic-gate 	int err;
53990Sstevel@tonic-gate 
54000Sstevel@tonic-gate 	if (zone_name == NULL) {
54010Sstevel@tonic-gate 		/* return caller's zone id */
54020Sstevel@tonic-gate 		return (getzoneid());
54030Sstevel@tonic-gate 	}
54040Sstevel@tonic-gate 
54050Sstevel@tonic-gate 	kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
54060Sstevel@tonic-gate 	if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) {
54070Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
54080Sstevel@tonic-gate 		return (set_errno(err));
54090Sstevel@tonic-gate 	}
54100Sstevel@tonic-gate 
54110Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
54120Sstevel@tonic-gate 	zone = zone_find_all_by_name(kname);
54130Sstevel@tonic-gate 	kmem_free(kname, ZONENAME_MAX);
54141676Sjpk 	/*
54151676Sjpk 	 * In a non-global zone, can only lookup global and own name.
54161676Sjpk 	 * In Trusted Extensions zone label dominance rules apply.
54171676Sjpk 	 */
54181676Sjpk 	if (zone == NULL ||
54191676Sjpk 	    zone_status_get(zone) < ZONE_IS_READY ||
54201676Sjpk 	    !zone_list_access(zone)) {
54210Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
54220Sstevel@tonic-gate 		return (set_errno(EINVAL));
54231676Sjpk 	} else {
54241676Sjpk 		zoneid = zone->zone_id;
54251676Sjpk 		mutex_exit(&zonehash_lock);
54261676Sjpk 		return (zoneid);
54270Sstevel@tonic-gate 	}
54280Sstevel@tonic-gate }
54290Sstevel@tonic-gate 
5430813Sdp static int
5431813Sdp zone_version(int *version_arg)
5432813Sdp {
5433813Sdp 	int version = ZONE_SYSCALL_API_VERSION;
5434813Sdp 
5435813Sdp 	if (copyout(&version, version_arg, sizeof (int)) != 0)
5436813Sdp 		return (set_errno(EFAULT));
5437813Sdp 	return (0);
5438813Sdp }
5439813Sdp 
54400Sstevel@tonic-gate /* ARGSUSED */
54410Sstevel@tonic-gate long
5442789Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4)
54430Sstevel@tonic-gate {
54440Sstevel@tonic-gate 	zone_def zs;
54450Sstevel@tonic-gate 
54460Sstevel@tonic-gate 	switch (cmd) {
54470Sstevel@tonic-gate 	case ZONE_CREATE:
54480Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
54490Sstevel@tonic-gate 			if (copyin(arg1, &zs, sizeof (zone_def))) {
54500Sstevel@tonic-gate 				return (set_errno(EFAULT));
54510Sstevel@tonic-gate 			}
54520Sstevel@tonic-gate 		} else {
54530Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
54540Sstevel@tonic-gate 			zone_def32 zs32;
54550Sstevel@tonic-gate 
54560Sstevel@tonic-gate 			if (copyin(arg1, &zs32, sizeof (zone_def32))) {
54570Sstevel@tonic-gate 				return (set_errno(EFAULT));
54580Sstevel@tonic-gate 			}
54590Sstevel@tonic-gate 			zs.zone_name =
54600Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_name;
54610Sstevel@tonic-gate 			zs.zone_root =
54620Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_root;
54630Sstevel@tonic-gate 			zs.zone_privs =
54640Sstevel@tonic-gate 			    (const struct priv_set *)
54650Sstevel@tonic-gate 			    (unsigned long)zs32.zone_privs;
54661409Sdp 			zs.zone_privssz = zs32.zone_privssz;
54670Sstevel@tonic-gate 			zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf;
54680Sstevel@tonic-gate 			zs.rctlbufsz = zs32.rctlbufsz;
5469789Sahrens 			zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf;
5470789Sahrens 			zs.zfsbufsz = zs32.zfsbufsz;
54710Sstevel@tonic-gate 			zs.extended_error =
54720Sstevel@tonic-gate 			    (int *)(unsigned long)zs32.extended_error;
54731676Sjpk 			zs.match = zs32.match;
54741676Sjpk 			zs.doi = zs32.doi;
54751676Sjpk 			zs.label = (const bslabel_t *)(uintptr_t)zs32.label;
54763448Sdh155122 			zs.flags = zs32.flags;
54770Sstevel@tonic-gate #else
54780Sstevel@tonic-gate 			panic("get_udatamodel() returned bogus result\n");
54790Sstevel@tonic-gate #endif
54800Sstevel@tonic-gate 		}
54810Sstevel@tonic-gate 
54820Sstevel@tonic-gate 		return (zone_create(zs.zone_name, zs.zone_root,
5483813Sdp 		    zs.zone_privs, zs.zone_privssz,
5484813Sdp 		    (caddr_t)zs.rctlbuf, zs.rctlbufsz,
5485813Sdp 		    (caddr_t)zs.zfsbuf, zs.zfsbufsz,
54861676Sjpk 		    zs.extended_error, zs.match, zs.doi,
54873448Sdh155122 		    zs.label, zs.flags));
54880Sstevel@tonic-gate 	case ZONE_BOOT:
54892267Sdp 		return (zone_boot((zoneid_t)(uintptr_t)arg1));
54900Sstevel@tonic-gate 	case ZONE_DESTROY:
54910Sstevel@tonic-gate 		return (zone_destroy((zoneid_t)(uintptr_t)arg1));
54920Sstevel@tonic-gate 	case ZONE_GETATTR:
54930Sstevel@tonic-gate 		return (zone_getattr((zoneid_t)(uintptr_t)arg1,
54940Sstevel@tonic-gate 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
54952267Sdp 	case ZONE_SETATTR:
54962267Sdp 		return (zone_setattr((zoneid_t)(uintptr_t)arg1,
54972267Sdp 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
54980Sstevel@tonic-gate 	case ZONE_ENTER:
54990Sstevel@tonic-gate 		return (zone_enter((zoneid_t)(uintptr_t)arg1));
55000Sstevel@tonic-gate 	case ZONE_LIST:
55010Sstevel@tonic-gate 		return (zone_list((zoneid_t *)arg1, (uint_t *)arg2));
55020Sstevel@tonic-gate 	case ZONE_SHUTDOWN:
55030Sstevel@tonic-gate 		return (zone_shutdown((zoneid_t)(uintptr_t)arg1));
55040Sstevel@tonic-gate 	case ZONE_LOOKUP:
55050Sstevel@tonic-gate 		return (zone_lookup((const char *)arg1));
5506813Sdp 	case ZONE_VERSION:
5507813Sdp 		return (zone_version((int *)arg1));
55083448Sdh155122 	case ZONE_ADD_DATALINK:
55093448Sdh155122 		return (zone_add_datalink((zoneid_t)(uintptr_t)arg1,
55103448Sdh155122 		    (char *)arg2));
55113448Sdh155122 	case ZONE_DEL_DATALINK:
55123448Sdh155122 		return (zone_remove_datalink((zoneid_t)(uintptr_t)arg1,
55133448Sdh155122 		    (char *)arg2));
55143448Sdh155122 	case ZONE_CHECK_DATALINK:
55153448Sdh155122 		return (zone_check_datalink((zoneid_t *)arg1, (char *)arg2));
55163448Sdh155122 	case ZONE_LIST_DATALINK:
55173448Sdh155122 		return (zone_list_datalink((zoneid_t)(uintptr_t)arg1,
55183448Sdh155122 		    (int *)arg2, (char *)arg3));
55190Sstevel@tonic-gate 	default:
55200Sstevel@tonic-gate 		return (set_errno(EINVAL));
55210Sstevel@tonic-gate 	}
55220Sstevel@tonic-gate }
55230Sstevel@tonic-gate 
55240Sstevel@tonic-gate struct zarg {
55250Sstevel@tonic-gate 	zone_t *zone;
55260Sstevel@tonic-gate 	zone_cmd_arg_t arg;
55270Sstevel@tonic-gate };
55280Sstevel@tonic-gate 
55290Sstevel@tonic-gate static int
55300Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp)
55310Sstevel@tonic-gate {
55320Sstevel@tonic-gate 	char *buf;
55330Sstevel@tonic-gate 	size_t buflen;
55340Sstevel@tonic-gate 	int error;
55350Sstevel@tonic-gate 
55360Sstevel@tonic-gate 	buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name);
55370Sstevel@tonic-gate 	buf = kmem_alloc(buflen, KM_SLEEP);
55380Sstevel@tonic-gate 	(void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name);
55390Sstevel@tonic-gate 	error = door_ki_open(buf, doorp);
55400Sstevel@tonic-gate 	kmem_free(buf, buflen);
55410Sstevel@tonic-gate 	return (error);
55420Sstevel@tonic-gate }
55430Sstevel@tonic-gate 
55440Sstevel@tonic-gate static void
55450Sstevel@tonic-gate zone_release_door(door_handle_t *doorp)
55460Sstevel@tonic-gate {
55470Sstevel@tonic-gate 	door_ki_rele(*doorp);
55480Sstevel@tonic-gate 	*doorp = NULL;
55490Sstevel@tonic-gate }
55500Sstevel@tonic-gate 
55510Sstevel@tonic-gate static void
55520Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp)
55530Sstevel@tonic-gate {
55540Sstevel@tonic-gate 	door_handle_t door = NULL;
55550Sstevel@tonic-gate 	door_arg_t darg, save_arg;
55560Sstevel@tonic-gate 	char *zone_name;
55570Sstevel@tonic-gate 	size_t zone_namelen;
55580Sstevel@tonic-gate 	zoneid_t zoneid;
55590Sstevel@tonic-gate 	zone_t *zone;
55600Sstevel@tonic-gate 	zone_cmd_arg_t arg;
55610Sstevel@tonic-gate 	uint64_t uniqid;
55620Sstevel@tonic-gate 	size_t size;
55630Sstevel@tonic-gate 	int error;
55640Sstevel@tonic-gate 	int retry;
55650Sstevel@tonic-gate 
55660Sstevel@tonic-gate 	zone = zargp->zone;
55670Sstevel@tonic-gate 	arg = zargp->arg;
55680Sstevel@tonic-gate 	kmem_free(zargp, sizeof (*zargp));
55690Sstevel@tonic-gate 
55700Sstevel@tonic-gate 	zone_namelen = strlen(zone->zone_name) + 1;
55710Sstevel@tonic-gate 	zone_name = kmem_alloc(zone_namelen, KM_SLEEP);
55720Sstevel@tonic-gate 	bcopy(zone->zone_name, zone_name, zone_namelen);
55730Sstevel@tonic-gate 	zoneid = zone->zone_id;
55740Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
55750Sstevel@tonic-gate 	/*
55760Sstevel@tonic-gate 	 * zoneadmd may be down, but at least we can empty out the zone.
55770Sstevel@tonic-gate 	 * We can ignore the return value of zone_empty() since we're called
55780Sstevel@tonic-gate 	 * from a kernel thread and know we won't be delivered any signals.
55790Sstevel@tonic-gate 	 */
55800Sstevel@tonic-gate 	ASSERT(curproc == &p0);
55810Sstevel@tonic-gate 	(void) zone_empty(zone);
55820Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY);
55830Sstevel@tonic-gate 	zone_rele(zone);
55840Sstevel@tonic-gate 
55850Sstevel@tonic-gate 	size = sizeof (arg);
55860Sstevel@tonic-gate 	darg.rbuf = (char *)&arg;
55870Sstevel@tonic-gate 	darg.data_ptr = (char *)&arg;
55880Sstevel@tonic-gate 	darg.rsize = size;
55890Sstevel@tonic-gate 	darg.data_size = size;
55900Sstevel@tonic-gate 	darg.desc_ptr = NULL;
55910Sstevel@tonic-gate 	darg.desc_num = 0;
55920Sstevel@tonic-gate 
55930Sstevel@tonic-gate 	save_arg = darg;
55940Sstevel@tonic-gate 	/*
55950Sstevel@tonic-gate 	 * Since we're not holding a reference to the zone, any number of
55960Sstevel@tonic-gate 	 * things can go wrong, including the zone disappearing before we get a
55970Sstevel@tonic-gate 	 * chance to talk to zoneadmd.
55980Sstevel@tonic-gate 	 */
55990Sstevel@tonic-gate 	for (retry = 0; /* forever */; retry++) {
56000Sstevel@tonic-gate 		if (door == NULL &&
56010Sstevel@tonic-gate 		    (error = zone_lookup_door(zone_name, &door)) != 0) {
56020Sstevel@tonic-gate 			goto next;
56030Sstevel@tonic-gate 		}
56040Sstevel@tonic-gate 		ASSERT(door != NULL);
56050Sstevel@tonic-gate 
56060Sstevel@tonic-gate 		if ((error = door_ki_upcall(door, &darg)) == 0) {
56070Sstevel@tonic-gate 			break;
56080Sstevel@tonic-gate 		}
56090Sstevel@tonic-gate 		switch (error) {
56100Sstevel@tonic-gate 		case EINTR:
56110Sstevel@tonic-gate 			/* FALLTHROUGH */
56120Sstevel@tonic-gate 		case EAGAIN:	/* process may be forking */
56130Sstevel@tonic-gate 			/*
56140Sstevel@tonic-gate 			 * Back off for a bit
56150Sstevel@tonic-gate 			 */
56160Sstevel@tonic-gate 			break;
56170Sstevel@tonic-gate 		case EBADF:
56180Sstevel@tonic-gate 			zone_release_door(&door);
56190Sstevel@tonic-gate 			if (zone_lookup_door(zone_name, &door) != 0) {
56200Sstevel@tonic-gate 				/*
56210Sstevel@tonic-gate 				 * zoneadmd may be dead, but it may come back to
56220Sstevel@tonic-gate 				 * life later.
56230Sstevel@tonic-gate 				 */
56240Sstevel@tonic-gate 				break;
56250Sstevel@tonic-gate 			}
56260Sstevel@tonic-gate 			break;
56270Sstevel@tonic-gate 		default:
56280Sstevel@tonic-gate 			cmn_err(CE_WARN,
56290Sstevel@tonic-gate 			    "zone_ki_call_zoneadmd: door_ki_upcall error %d\n",
56300Sstevel@tonic-gate 			    error);
56310Sstevel@tonic-gate 			goto out;
56320Sstevel@tonic-gate 		}
56330Sstevel@tonic-gate next:
56340Sstevel@tonic-gate 		/*
56350Sstevel@tonic-gate 		 * If this isn't the same zone_t that we originally had in mind,
56360Sstevel@tonic-gate 		 * then this is the same as if two kadmin requests come in at
56370Sstevel@tonic-gate 		 * the same time: the first one wins.  This means we lose, so we
56380Sstevel@tonic-gate 		 * bail.
56390Sstevel@tonic-gate 		 */
56400Sstevel@tonic-gate 		if ((zone = zone_find_by_id(zoneid)) == NULL) {
56410Sstevel@tonic-gate 			/*
56420Sstevel@tonic-gate 			 * Problem is solved.
56430Sstevel@tonic-gate 			 */
56440Sstevel@tonic-gate 			break;
56450Sstevel@tonic-gate 		}
56460Sstevel@tonic-gate 		if (zone->zone_uniqid != uniqid) {
56470Sstevel@tonic-gate 			/*
56480Sstevel@tonic-gate 			 * zoneid recycled
56490Sstevel@tonic-gate 			 */
56500Sstevel@tonic-gate 			zone_rele(zone);
56510Sstevel@tonic-gate 			break;
56520Sstevel@tonic-gate 		}
56530Sstevel@tonic-gate 		/*
56540Sstevel@tonic-gate 		 * We could zone_status_timedwait(), but there doesn't seem to
56550Sstevel@tonic-gate 		 * be much point in doing that (plus, it would mean that
56560Sstevel@tonic-gate 		 * zone_free() isn't called until this thread exits).
56570Sstevel@tonic-gate 		 */
56580Sstevel@tonic-gate 		zone_rele(zone);
56590Sstevel@tonic-gate 		delay(hz);
56600Sstevel@tonic-gate 		darg = save_arg;
56610Sstevel@tonic-gate 	}
56620Sstevel@tonic-gate out:
56630Sstevel@tonic-gate 	if (door != NULL) {
56640Sstevel@tonic-gate 		zone_release_door(&door);
56650Sstevel@tonic-gate 	}
56660Sstevel@tonic-gate 	kmem_free(zone_name, zone_namelen);
56670Sstevel@tonic-gate 	thread_exit();
56680Sstevel@tonic-gate }
56690Sstevel@tonic-gate 
56700Sstevel@tonic-gate /*
56712267Sdp  * Entry point for uadmin() to tell the zone to go away or reboot.  Analog to
56722267Sdp  * kadmin().  The caller is a process in the zone.
56730Sstevel@tonic-gate  *
56740Sstevel@tonic-gate  * In order to shutdown the zone, we will hand off control to zoneadmd
56750Sstevel@tonic-gate  * (running in the global zone) via a door.  We do a half-hearted job at
56760Sstevel@tonic-gate  * killing all processes in the zone, create a kernel thread to contact
56770Sstevel@tonic-gate  * zoneadmd, and make note of the "uniqid" of the zone.  The uniqid is
56780Sstevel@tonic-gate  * a form of generation number used to let zoneadmd (as well as
56790Sstevel@tonic-gate  * zone_destroy()) know exactly which zone they're re talking about.
56800Sstevel@tonic-gate  */
56810Sstevel@tonic-gate int
56822267Sdp zone_kadmin(int cmd, int fcn, const char *mdep, cred_t *credp)
56830Sstevel@tonic-gate {
56840Sstevel@tonic-gate 	struct zarg *zargp;
56850Sstevel@tonic-gate 	zone_cmd_t zcmd;
56860Sstevel@tonic-gate 	zone_t *zone;
56870Sstevel@tonic-gate 
56880Sstevel@tonic-gate 	zone = curproc->p_zone;
56890Sstevel@tonic-gate 	ASSERT(getzoneid() != GLOBAL_ZONEID);
56900Sstevel@tonic-gate 
56910Sstevel@tonic-gate 	switch (cmd) {
56920Sstevel@tonic-gate 	case A_SHUTDOWN:
56930Sstevel@tonic-gate 		switch (fcn) {
56940Sstevel@tonic-gate 		case AD_HALT:
56950Sstevel@tonic-gate 		case AD_POWEROFF:
56960Sstevel@tonic-gate 			zcmd = Z_HALT;
56970Sstevel@tonic-gate 			break;
56980Sstevel@tonic-gate 		case AD_BOOT:
56990Sstevel@tonic-gate 			zcmd = Z_REBOOT;
57000Sstevel@tonic-gate 			break;
57010Sstevel@tonic-gate 		case AD_IBOOT:
57020Sstevel@tonic-gate 		case AD_SBOOT:
57030Sstevel@tonic-gate 		case AD_SIBOOT:
57040Sstevel@tonic-gate 		case AD_NOSYNC:
57050Sstevel@tonic-gate 			return (ENOTSUP);
57060Sstevel@tonic-gate 		default:
57070Sstevel@tonic-gate 			return (EINVAL);
57080Sstevel@tonic-gate 		}
57090Sstevel@tonic-gate 		break;
57100Sstevel@tonic-gate 	case A_REBOOT:
57110Sstevel@tonic-gate 		zcmd = Z_REBOOT;
57120Sstevel@tonic-gate 		break;
57130Sstevel@tonic-gate 	case A_FTRACE:
57140Sstevel@tonic-gate 	case A_REMOUNT:
57150Sstevel@tonic-gate 	case A_FREEZE:
57160Sstevel@tonic-gate 	case A_DUMP:
57170Sstevel@tonic-gate 		return (ENOTSUP);
57180Sstevel@tonic-gate 	default:
57190Sstevel@tonic-gate 		ASSERT(cmd != A_SWAPCTL);	/* handled by uadmin() */
57200Sstevel@tonic-gate 		return (EINVAL);
57210Sstevel@tonic-gate 	}
57220Sstevel@tonic-gate 
57230Sstevel@tonic-gate 	if (secpolicy_zone_admin(credp, B_FALSE))
57240Sstevel@tonic-gate 		return (EPERM);
57250Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
57262267Sdp 
57270Sstevel@tonic-gate 	/*
57280Sstevel@tonic-gate 	 * zone_status can't be ZONE_IS_EMPTY or higher since curproc
57290Sstevel@tonic-gate 	 * is in the zone.
57300Sstevel@tonic-gate 	 */
57310Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY);
57320Sstevel@tonic-gate 	if (zone_status_get(zone) > ZONE_IS_RUNNING) {
57330Sstevel@tonic-gate 		/*
57340Sstevel@tonic-gate 		 * This zone is already on its way down.
57350Sstevel@tonic-gate 		 */
57360Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
57370Sstevel@tonic-gate 		return (0);
57380Sstevel@tonic-gate 	}
57390Sstevel@tonic-gate 	/*
57400Sstevel@tonic-gate 	 * Prevent future zone_enter()s
57410Sstevel@tonic-gate 	 */
57420Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
57430Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
57440Sstevel@tonic-gate 
57450Sstevel@tonic-gate 	/*
57460Sstevel@tonic-gate 	 * Kill everyone now and call zoneadmd later.
57470Sstevel@tonic-gate 	 * zone_ki_call_zoneadmd() will do a more thorough job of this
57480Sstevel@tonic-gate 	 * later.
57490Sstevel@tonic-gate 	 */
57500Sstevel@tonic-gate 	killall(zone->zone_id);
57510Sstevel@tonic-gate 	/*
57520Sstevel@tonic-gate 	 * Now, create the thread to contact zoneadmd and do the rest of the
57530Sstevel@tonic-gate 	 * work.  This thread can't be created in our zone otherwise
57540Sstevel@tonic-gate 	 * zone_destroy() would deadlock.
57550Sstevel@tonic-gate 	 */
57562267Sdp 	zargp = kmem_zalloc(sizeof (*zargp), KM_SLEEP);
57570Sstevel@tonic-gate 	zargp->arg.cmd = zcmd;
57580Sstevel@tonic-gate 	zargp->arg.uniqid = zone->zone_uniqid;
57592267Sdp 	zargp->zone = zone;
57600Sstevel@tonic-gate 	(void) strcpy(zargp->arg.locale, "C");
57612267Sdp 	/* mdep was already copied in for us by uadmin */
57622267Sdp 	if (mdep != NULL)
57632267Sdp 		(void) strlcpy(zargp->arg.bootbuf, mdep,
57642267Sdp 		    sizeof (zargp->arg.bootbuf));
57652267Sdp 	zone_hold(zone);
57660Sstevel@tonic-gate 
57670Sstevel@tonic-gate 	(void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0,
57680Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
57690Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
57700Sstevel@tonic-gate 
57710Sstevel@tonic-gate 	return (EINVAL);
57720Sstevel@tonic-gate }
57730Sstevel@tonic-gate 
57740Sstevel@tonic-gate /*
57750Sstevel@tonic-gate  * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's
57760Sstevel@tonic-gate  * status to ZONE_IS_SHUTTING_DOWN.
57770Sstevel@tonic-gate  */
57780Sstevel@tonic-gate void
57790Sstevel@tonic-gate zone_shutdown_global(void)
57800Sstevel@tonic-gate {
57810Sstevel@tonic-gate 	ASSERT(curproc->p_zone == global_zone);
57820Sstevel@tonic-gate 
57830Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
57840Sstevel@tonic-gate 	ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING);
57850Sstevel@tonic-gate 	zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN);
57860Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
57870Sstevel@tonic-gate }
5788789Sahrens 
5789789Sahrens /*
5790789Sahrens  * Returns true if the named dataset is visible in the current zone.
5791789Sahrens  * The 'write' parameter is set to 1 if the dataset is also writable.
5792789Sahrens  */
5793789Sahrens int
5794789Sahrens zone_dataset_visible(const char *dataset, int *write)
5795789Sahrens {
5796789Sahrens 	zone_dataset_t *zd;
5797789Sahrens 	size_t len;
5798789Sahrens 	zone_t *zone = curproc->p_zone;
5799789Sahrens 
5800789Sahrens 	if (dataset[0] == '\0')
5801789Sahrens 		return (0);
5802789Sahrens 
5803789Sahrens 	/*
5804789Sahrens 	 * Walk the list once, looking for datasets which match exactly, or
5805789Sahrens 	 * specify a dataset underneath an exported dataset.  If found, return
5806789Sahrens 	 * true and note that it is writable.
5807789Sahrens 	 */
5808789Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
5809789Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
5810789Sahrens 
5811789Sahrens 		len = strlen(zd->zd_dataset);
5812789Sahrens 		if (strlen(dataset) >= len &&
5813789Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
5814816Smaybee 		    (dataset[len] == '\0' || dataset[len] == '/' ||
5815816Smaybee 		    dataset[len] == '@')) {
5816789Sahrens 			if (write)
5817789Sahrens 				*write = 1;
5818789Sahrens 			return (1);
5819789Sahrens 		}
5820789Sahrens 	}
5821789Sahrens 
5822789Sahrens 	/*
5823789Sahrens 	 * Walk the list a second time, searching for datasets which are parents
5824789Sahrens 	 * of exported datasets.  These should be visible, but read-only.
5825789Sahrens 	 *
5826789Sahrens 	 * Note that we also have to support forms such as 'pool/dataset/', with
5827789Sahrens 	 * a trailing slash.
5828789Sahrens 	 */
5829789Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
5830789Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
5831789Sahrens 
5832789Sahrens 		len = strlen(dataset);
5833789Sahrens 		if (dataset[len - 1] == '/')
5834789Sahrens 			len--;	/* Ignore trailing slash */
5835789Sahrens 		if (len < strlen(zd->zd_dataset) &&
5836789Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
5837789Sahrens 		    zd->zd_dataset[len] == '/') {
5838789Sahrens 			if (write)
5839789Sahrens 				*write = 0;
5840789Sahrens 			return (1);
5841789Sahrens 		}
5842789Sahrens 	}
5843789Sahrens 
5844789Sahrens 	return (0);
5845789Sahrens }
58461676Sjpk 
58471676Sjpk /*
58481676Sjpk  * zone_find_by_any_path() -
58491676Sjpk  *
58501676Sjpk  * kernel-private routine similar to zone_find_by_path(), but which
58511676Sjpk  * effectively compares against zone paths rather than zonerootpath
58521676Sjpk  * (i.e., the last component of zonerootpaths, which should be "root/",
58531676Sjpk  * are not compared.)  This is done in order to accurately identify all
58541676Sjpk  * paths, whether zone-visible or not, including those which are parallel
58551676Sjpk  * to /root/, such as /dev/, /home/, etc...
58561676Sjpk  *
58571676Sjpk  * If the specified path does not fall under any zone path then global
58581676Sjpk  * zone is returned.
58591676Sjpk  *
58601676Sjpk  * The treat_abs parameter indicates whether the path should be treated as
58611676Sjpk  * an absolute path although it does not begin with "/".  (This supports
58621676Sjpk  * nfs mount syntax such as host:any/path.)
58631676Sjpk  *
58641676Sjpk  * The caller is responsible for zone_rele of the returned zone.
58651676Sjpk  */
58661676Sjpk zone_t *
58671676Sjpk zone_find_by_any_path(const char *path, boolean_t treat_abs)
58681676Sjpk {
58691676Sjpk 	zone_t *zone;
58701676Sjpk 	int path_offset = 0;
58711676Sjpk 
58721676Sjpk 	if (path == NULL) {
58731676Sjpk 		zone_hold(global_zone);
58741676Sjpk 		return (global_zone);
58751676Sjpk 	}
58761676Sjpk 
58771676Sjpk 	if (*path != '/') {
58781676Sjpk 		ASSERT(treat_abs);
58791676Sjpk 		path_offset = 1;
58801676Sjpk 	}
58811676Sjpk 
58821676Sjpk 	mutex_enter(&zonehash_lock);
58831676Sjpk 	for (zone = list_head(&zone_active); zone != NULL;
58841676Sjpk 	    zone = list_next(&zone_active, zone)) {
58851676Sjpk 		char	*c;
58861676Sjpk 		size_t	pathlen;
58871876Smp46848 		char *rootpath_start;
58881676Sjpk 
58891676Sjpk 		if (zone == global_zone)	/* skip global zone */
58901676Sjpk 			continue;
58911676Sjpk 
58921676Sjpk 		/* scan backwards to find start of last component */
58931676Sjpk 		c = zone->zone_rootpath + zone->zone_rootpathlen - 2;
58941676Sjpk 		do {
58951676Sjpk 			c--;
58961676Sjpk 		} while (*c != '/');
58971676Sjpk 
58981876Smp46848 		pathlen = c - zone->zone_rootpath + 1 - path_offset;
58991876Smp46848 		rootpath_start = (zone->zone_rootpath + path_offset);
59001876Smp46848 		if (strncmp(path, rootpath_start, pathlen) == 0)
59011676Sjpk 			break;
59021676Sjpk 	}
59031676Sjpk 	if (zone == NULL)
59041676Sjpk 		zone = global_zone;
59051676Sjpk 	zone_hold(zone);
59061676Sjpk 	mutex_exit(&zonehash_lock);
59071676Sjpk 	return (zone);
59081676Sjpk }
59093448Sdh155122 
59103448Sdh155122 /* List of data link names which are accessible from the zone */
59113448Sdh155122 struct dlnamelist {
59123448Sdh155122 	char			dlnl_name[LIFNAMSIZ];
59133448Sdh155122 	struct dlnamelist	*dlnl_next;
59143448Sdh155122 };
59153448Sdh155122 
59163448Sdh155122 
59173448Sdh155122 /*
59183448Sdh155122  * Check whether the datalink name (dlname) itself is present.
59193448Sdh155122  * Return true if found.
59203448Sdh155122  */
59213448Sdh155122 static boolean_t
59223448Sdh155122 zone_dlname(zone_t *zone, char *dlname)
59233448Sdh155122 {
59243448Sdh155122 	struct dlnamelist *dlnl;
59253448Sdh155122 	boolean_t found = B_FALSE;
59263448Sdh155122 
59273448Sdh155122 	mutex_enter(&zone->zone_lock);
59283448Sdh155122 	for (dlnl = zone->zone_dl_list; dlnl != NULL; dlnl = dlnl->dlnl_next) {
59293448Sdh155122 		if (strncmp(dlnl->dlnl_name, dlname, LIFNAMSIZ) == 0) {
59303448Sdh155122 			found = B_TRUE;
59313448Sdh155122 			break;
59323448Sdh155122 		}
59333448Sdh155122 	}
59343448Sdh155122 	mutex_exit(&zone->zone_lock);
59353448Sdh155122 	return (found);
59363448Sdh155122 }
59373448Sdh155122 
59383448Sdh155122 /*
59393448Sdh155122  * Add an data link name for the zone. Does not check for duplicates.
59403448Sdh155122  */
59413448Sdh155122 static int
59423448Sdh155122 zone_add_datalink(zoneid_t zoneid, char *dlname)
59433448Sdh155122 {
59443448Sdh155122 	struct dlnamelist *dlnl;
59453448Sdh155122 	zone_t *zone;
59463448Sdh155122 	zone_t *thiszone;
59473448Sdh155122 	int err;
59483448Sdh155122 
59493448Sdh155122 	dlnl = kmem_zalloc(sizeof (struct dlnamelist), KM_SLEEP);
59503448Sdh155122 	if ((err = copyinstr(dlname, dlnl->dlnl_name, LIFNAMSIZ, NULL)) != 0) {
59513448Sdh155122 		kmem_free(dlnl, sizeof (struct dlnamelist));
59523448Sdh155122 		return (set_errno(err));
59533448Sdh155122 	}
59543448Sdh155122 
59553448Sdh155122 	thiszone = zone_find_by_id(zoneid);
59563448Sdh155122 	if (thiszone == NULL) {
59573448Sdh155122 		kmem_free(dlnl, sizeof (struct dlnamelist));
59583448Sdh155122 		return (set_errno(ENXIO));
59593448Sdh155122 	}
59603448Sdh155122 
59613448Sdh155122 	/*
59623448Sdh155122 	 * Verify that the datalink name isn't already used by a different
59633448Sdh155122 	 * zone while allowing duplicate entries for the same zone (e.g. due
59643448Sdh155122 	 * to both using IPv4 and IPv6 on an interface)
59653448Sdh155122 	 */
59663448Sdh155122 	mutex_enter(&zonehash_lock);
59673448Sdh155122 	for (zone = list_head(&zone_active); zone != NULL;
59683448Sdh155122 	    zone = list_next(&zone_active, zone)) {
59693448Sdh155122 		if (zone->zone_id == zoneid)
59703448Sdh155122 			continue;
59713448Sdh155122 
59723448Sdh155122 		if (zone_dlname(zone, dlnl->dlnl_name)) {
59733448Sdh155122 			mutex_exit(&zonehash_lock);
59743448Sdh155122 			zone_rele(thiszone);
59753448Sdh155122 			kmem_free(dlnl, sizeof (struct dlnamelist));
59763448Sdh155122 			return (set_errno(EPERM));
59773448Sdh155122 		}
59783448Sdh155122 	}
59793448Sdh155122 	mutex_enter(&thiszone->zone_lock);
59803448Sdh155122 	dlnl->dlnl_next = thiszone->zone_dl_list;
59813448Sdh155122 	thiszone->zone_dl_list = dlnl;
59823448Sdh155122 	mutex_exit(&thiszone->zone_lock);
59833448Sdh155122 	mutex_exit(&zonehash_lock);
59843448Sdh155122 	zone_rele(thiszone);
59853448Sdh155122 	return (0);
59863448Sdh155122 }
59873448Sdh155122 
59883448Sdh155122 static int
59893448Sdh155122 zone_remove_datalink(zoneid_t zoneid, char *dlname)
59903448Sdh155122 {
59913448Sdh155122 	struct dlnamelist *dlnl, *odlnl, **dlnlp;
59923448Sdh155122 	zone_t *zone;
59933448Sdh155122 	int err;
59943448Sdh155122 
59953448Sdh155122 	dlnl = kmem_zalloc(sizeof (struct dlnamelist), KM_SLEEP);
59963448Sdh155122 	if ((err = copyinstr(dlname, dlnl->dlnl_name, LIFNAMSIZ, NULL)) != 0) {
59973448Sdh155122 		kmem_free(dlnl, sizeof (struct dlnamelist));
59983448Sdh155122 		return (set_errno(err));
59993448Sdh155122 	}
60003448Sdh155122 	zone = zone_find_by_id(zoneid);
60013448Sdh155122 	if (zone == NULL) {
60023448Sdh155122 		kmem_free(dlnl, sizeof (struct dlnamelist));
60033448Sdh155122 		return (set_errno(EINVAL));
60043448Sdh155122 	}
60053448Sdh155122 
60063448Sdh155122 	mutex_enter(&zone->zone_lock);
60073448Sdh155122 	/* Look for match */
60083448Sdh155122 	dlnlp = &zone->zone_dl_list;
60093448Sdh155122 	while (*dlnlp != NULL) {
60103448Sdh155122 		if (strncmp(dlnl->dlnl_name, (*dlnlp)->dlnl_name,
60113448Sdh155122 		    LIFNAMSIZ) == 0)
60123448Sdh155122 			goto found;
60133448Sdh155122 		dlnlp = &((*dlnlp)->dlnl_next);
60143448Sdh155122 	}
60153448Sdh155122 	mutex_exit(&zone->zone_lock);
60163448Sdh155122 	zone_rele(zone);
60173448Sdh155122 	kmem_free(dlnl, sizeof (struct dlnamelist));
60183448Sdh155122 	return (set_errno(ENXIO));
60193448Sdh155122 
60203448Sdh155122 found:
60213448Sdh155122 	odlnl = *dlnlp;
60223448Sdh155122 	*dlnlp = (*dlnlp)->dlnl_next;
60233448Sdh155122 	kmem_free(odlnl, sizeof (struct dlnamelist));
60243448Sdh155122 
60253448Sdh155122 	mutex_exit(&zone->zone_lock);
60263448Sdh155122 	zone_rele(zone);
60273448Sdh155122 	kmem_free(dlnl, sizeof (struct dlnamelist));
60283448Sdh155122 	return (0);
60293448Sdh155122 }
60303448Sdh155122 
60313448Sdh155122 /*
60323448Sdh155122  * Using the zoneidp as ALL_ZONES, we can lookup which zone is using datalink
60333448Sdh155122  * name (dlname); otherwise we just check if the specified zoneidp has access
60343448Sdh155122  * to the datalink name.
60353448Sdh155122  */
60363448Sdh155122 static int
60373448Sdh155122 zone_check_datalink(zoneid_t *zoneidp, char *dlname)
60383448Sdh155122 {
60393448Sdh155122 	zoneid_t id;
60403448Sdh155122 	char *dln;
60413448Sdh155122 	zone_t *zone;
60423448Sdh155122 	int err = 0;
60433448Sdh155122 	boolean_t allzones = B_FALSE;
60443448Sdh155122 
60453448Sdh155122 	if (copyin(zoneidp, &id, sizeof (id)) != 0) {
60463448Sdh155122 		return (set_errno(EFAULT));
60473448Sdh155122 	}
60483448Sdh155122 	dln = kmem_zalloc(LIFNAMSIZ, KM_SLEEP);
60493448Sdh155122 	if ((err = copyinstr(dlname, dln, LIFNAMSIZ, NULL)) != 0) {
60503448Sdh155122 		kmem_free(dln, LIFNAMSIZ);
60513448Sdh155122 		return (set_errno(err));
60523448Sdh155122 	}
60533448Sdh155122 
60543448Sdh155122 	if (id == ALL_ZONES)
60553448Sdh155122 		allzones = B_TRUE;
60563448Sdh155122 
60573448Sdh155122 	/*
60583448Sdh155122 	 * Check whether datalink name is already used.
60593448Sdh155122 	 */
60603448Sdh155122 	mutex_enter(&zonehash_lock);
60613448Sdh155122 	for (zone = list_head(&zone_active); zone != NULL;
60623448Sdh155122 	    zone = list_next(&zone_active, zone)) {
60633448Sdh155122 		if (allzones || (id == zone->zone_id)) {
60643448Sdh155122 			if (!zone_dlname(zone, dln))
60653448Sdh155122 				continue;
60663448Sdh155122 			if (allzones)
60673448Sdh155122 				err = copyout(&zone->zone_id, zoneidp,
60683448Sdh155122 				    sizeof (*zoneidp));
60693448Sdh155122 
60703448Sdh155122 			mutex_exit(&zonehash_lock);
60713448Sdh155122 			kmem_free(dln, LIFNAMSIZ);
60723448Sdh155122 			return (err ? set_errno(EFAULT) : 0);
60733448Sdh155122 		}
60743448Sdh155122 	}
60753448Sdh155122 
60763448Sdh155122 	/* datalink name is not found in any active zone. */
60773448Sdh155122 	mutex_exit(&zonehash_lock);
60783448Sdh155122 	kmem_free(dln, LIFNAMSIZ);
60793448Sdh155122 	return (set_errno(ENXIO));
60803448Sdh155122 }
60813448Sdh155122 
60823448Sdh155122 /*
60833448Sdh155122  * Get the names of the datalinks assigned to a zone.
60843448Sdh155122  * Here *nump is the number of datalinks, and the assumption
60855331Samw  * is that the caller will guarantee that the the supplied buffer is
60863448Sdh155122  * big enough to hold at least #*nump datalink names, that is,
60873448Sdh155122  * LIFNAMSIZ X *nump
60883448Sdh155122  * On return, *nump will be the "new" number of datalinks, if it
60893448Sdh155122  * ever changed.
60903448Sdh155122  */
60913448Sdh155122 static int
60923448Sdh155122 zone_list_datalink(zoneid_t zoneid, int *nump, char *buf)
60933448Sdh155122 {
60943448Sdh155122 	int num, dlcount;
60953448Sdh155122 	zone_t *zone;
60963448Sdh155122 	struct dlnamelist *dlnl;
60973448Sdh155122 	char *ptr;
60983448Sdh155122 
60993448Sdh155122 	if (copyin(nump, &dlcount, sizeof (dlcount)) != 0)
61003448Sdh155122 		return (set_errno(EFAULT));
61013448Sdh155122 
61023448Sdh155122 	zone = zone_find_by_id(zoneid);
61033448Sdh155122 	if (zone == NULL) {
61043448Sdh155122 		return (set_errno(ENXIO));
61053448Sdh155122 	}
61063448Sdh155122 
61073448Sdh155122 	num = 0;
61083448Sdh155122 	mutex_enter(&zone->zone_lock);
61093448Sdh155122 	ptr = buf;
61103448Sdh155122 	for (dlnl = zone->zone_dl_list; dlnl != NULL; dlnl = dlnl->dlnl_next) {
61113448Sdh155122 		/*
61123448Sdh155122 		 * If the list changed and the new number is bigger
61133448Sdh155122 		 * than what the caller supplied, just count, don't
61143448Sdh155122 		 * do copyout
61153448Sdh155122 		 */
61163448Sdh155122 		if (++num > dlcount)
61173448Sdh155122 			continue;
61183448Sdh155122 		if (copyout(dlnl->dlnl_name, ptr, LIFNAMSIZ) != 0) {
61193448Sdh155122 			mutex_exit(&zone->zone_lock);
61203448Sdh155122 			zone_rele(zone);
61213448Sdh155122 			return (set_errno(EFAULT));
61223448Sdh155122 		}
61233448Sdh155122 		ptr += LIFNAMSIZ;
61243448Sdh155122 	}
61253448Sdh155122 	mutex_exit(&zone->zone_lock);
61263448Sdh155122 	zone_rele(zone);
61273448Sdh155122 
61283448Sdh155122 	/* Increased or decreased, caller should be notified. */
61293448Sdh155122 	if (num != dlcount) {
61303448Sdh155122 		if (copyout(&num, nump, sizeof (num)) != 0) {
61313448Sdh155122 			return (set_errno(EFAULT));
61323448Sdh155122 		}
61333448Sdh155122 	}
61343448Sdh155122 	return (0);
61353448Sdh155122 }
61363448Sdh155122 
61373448Sdh155122 /*
61383448Sdh155122  * Public interface for looking up a zone by zoneid. It's a customized version
61395880Snordmark  * for netstack_zone_create(). It can only be called from the zsd create
61405880Snordmark  * callbacks, since it doesn't have reference on the zone structure hence if
61415880Snordmark  * it is called elsewhere the zone could disappear after the zonehash_lock
61425880Snordmark  * is dropped.
61435880Snordmark  *
61445880Snordmark  * Furthermore it
61455880Snordmark  * 1. Doesn't check the status of the zone.
61465880Snordmark  * 2. It will be called even before zone_init is called, in that case the
61473448Sdh155122  *    address of zone0 is returned directly, and netstack_zone_create()
61483448Sdh155122  *    will only assign a value to zone0.zone_netstack, won't break anything.
61495880Snordmark  * 3. Returns without the zone being held.
61503448Sdh155122  */
61513448Sdh155122 zone_t *
61523448Sdh155122 zone_find_by_id_nolock(zoneid_t zoneid)
61533448Sdh155122 {
61545880Snordmark 	zone_t *zone;
61555880Snordmark 
61565880Snordmark 	mutex_enter(&zonehash_lock);
61573448Sdh155122 	if (zonehashbyid == NULL)
61585880Snordmark 		zone = &zone0;
61593448Sdh155122 	else
61605880Snordmark 		zone = zone_find_all_by_id(zoneid);
61615880Snordmark 	mutex_exit(&zonehash_lock);
61625880Snordmark 	return (zone);
61633448Sdh155122 }
61645895Syz147064 
61655895Syz147064 /*
61665895Syz147064  * Walk the datalinks for a given zone
61675895Syz147064  */
61685895Syz147064 int
61695895Syz147064 zone_datalink_walk(zoneid_t zoneid, int (*cb)(const char *, void *), void *data)
61705895Syz147064 {
61715895Syz147064 	zone_t *zone;
61725895Syz147064 	struct dlnamelist *dlnl;
61735895Syz147064 	int ret = 0;
61745895Syz147064 
61755895Syz147064 	if ((zone = zone_find_by_id(zoneid)) == NULL)
61765895Syz147064 		return (ENOENT);
61775895Syz147064 
61785895Syz147064 	mutex_enter(&zone->zone_lock);
61795895Syz147064 	for (dlnl = zone->zone_dl_list; dlnl != NULL; dlnl = dlnl->dlnl_next) {
61805895Syz147064 		if ((ret = (*cb)(dlnl->dlnl_name, data)) != 0)
61815895Syz147064 			break;
61825895Syz147064 	}
61835895Syz147064 	mutex_exit(&zone->zone_lock);
61845895Syz147064 	zone_rele(zone);
61855895Syz147064 	return (ret);
61865895Syz147064 }
6187