xref: /onnv-gate/usr/src/uts/common/os/zone.c (revision 5331:3047ad28a67b)
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 /*
233446Smrj  * Copyright 2007 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  *
640Sstevel@tonic-gate  *   ZONE_IS_READY: zsched (the kernel dummy process for a zone) is
650Sstevel@tonic-gate  *   ready.  The zone is made visible after the ZSD constructor callbacks are
660Sstevel@tonic-gate  *   executed.  A zone remains in this state until it transitions into
670Sstevel@tonic-gate  *   the ZONE_IS_BOOTING state as a result of a call to zone_boot().
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  *   ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start
700Sstevel@tonic-gate  *   init.  Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN
710Sstevel@tonic-gate  *   state.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  *   ZONE_IS_RUNNING: The zone is open for business: zsched has
740Sstevel@tonic-gate  *   successfully started init.   A zone remains in this state until
750Sstevel@tonic-gate  *   zone_shutdown() is called.
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is
780Sstevel@tonic-gate  *   killing all processes running in the zone. The zone remains
790Sstevel@tonic-gate  *   in this state until there are no more user processes running in the zone.
800Sstevel@tonic-gate  *   zone_create(), zone_enter(), and zone_destroy() on this zone will fail.
810Sstevel@tonic-gate  *   Since zone_shutdown() is restartable, it may be called successfully
820Sstevel@tonic-gate  *   multiple times for the same zone_t.  Setting of the zone's state to
830Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check
840Sstevel@tonic-gate  *   the zone's status without worrying about it being a moving target.
850Sstevel@tonic-gate  *
860Sstevel@tonic-gate  *   ZONE_IS_EMPTY: zone_shutdown() has been called, and there
870Sstevel@tonic-gate  *   are no more user processes in the zone.  The zone remains in this
880Sstevel@tonic-gate  *   state until there are no more kernel threads associated with the
890Sstevel@tonic-gate  *   zone.  zone_create(), zone_enter(), and zone_destroy() on this zone will
900Sstevel@tonic-gate  *   fail.
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  *   ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone
930Sstevel@tonic-gate  *   have exited.  zone_shutdown() returns.  Henceforth it is not possible to
940Sstevel@tonic-gate  *   join the zone or create kernel threads therein.
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  *   ZONE_IS_DYING: zone_destroy() has been called on the zone; zone
970Sstevel@tonic-gate  *   remains in this state until zsched exits.  Calls to zone_find_by_*()
980Sstevel@tonic-gate  *   return NULL from now on.
990Sstevel@tonic-gate  *
1000Sstevel@tonic-gate  *   ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0).  There are no
1010Sstevel@tonic-gate  *   processes or threads doing work on behalf of the zone.  The zone is
1020Sstevel@tonic-gate  *   removed from the list of active zones.  zone_destroy() returns, and
1030Sstevel@tonic-gate  *   the zone can be recreated.
1040Sstevel@tonic-gate  *
1050Sstevel@tonic-gate  *   ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor
1060Sstevel@tonic-gate  *   callbacks are executed, and all memory associated with the zone is
1070Sstevel@tonic-gate  *   freed.
1080Sstevel@tonic-gate  *
1090Sstevel@tonic-gate  *   Threads can wait for the zone to enter a requested state by using
1100Sstevel@tonic-gate  *   zone_status_wait() or zone_status_timedwait() with the desired
1110Sstevel@tonic-gate  *   state passed in as an argument.  Zone state transitions are
1120Sstevel@tonic-gate  *   uni-directional; it is not possible to move back to an earlier state.
1130Sstevel@tonic-gate  *
1140Sstevel@tonic-gate  *
1150Sstevel@tonic-gate  *   Zone-Specific Data:
1160Sstevel@tonic-gate  *
1170Sstevel@tonic-gate  *   Subsystems needing to maintain zone-specific data can store that
1180Sstevel@tonic-gate  *   data using the ZSD mechanism.  This provides a zone-specific data
1190Sstevel@tonic-gate  *   store, similar to thread-specific data (see pthread_getspecific(3C)
1200Sstevel@tonic-gate  *   or the TSD code in uts/common/disp/thread.c.  Also, ZSD can be used
1210Sstevel@tonic-gate  *   to register callbacks to be invoked when a zone is created, shut
1220Sstevel@tonic-gate  *   down, or destroyed.  This can be used to initialize zone-specific
1230Sstevel@tonic-gate  *   data for new zones and to clean up when zones go away.
1240Sstevel@tonic-gate  *
1250Sstevel@tonic-gate  *
1260Sstevel@tonic-gate  *   Data Structures:
1270Sstevel@tonic-gate  *
1280Sstevel@tonic-gate  *   The per-zone structure (zone_t) is reference counted, and freed
1290Sstevel@tonic-gate  *   when all references are released.  zone_hold and zone_rele can be
1300Sstevel@tonic-gate  *   used to adjust the reference count.  In addition, reference counts
1310Sstevel@tonic-gate  *   associated with the cred_t structure are tracked separately using
1320Sstevel@tonic-gate  *   zone_cred_hold and zone_cred_rele.
1330Sstevel@tonic-gate  *
1340Sstevel@tonic-gate  *   Pointers to active zone_t's are stored in two hash tables; one
1350Sstevel@tonic-gate  *   for searching by id, the other for searching by name.  Lookups
1360Sstevel@tonic-gate  *   can be performed on either basis, using zone_find_by_id and
1370Sstevel@tonic-gate  *   zone_find_by_name.  Both return zone_t pointers with the zone
1380Sstevel@tonic-gate  *   held, so zone_rele should be called when the pointer is no longer
1390Sstevel@tonic-gate  *   needed.  Zones can also be searched by path; zone_find_by_path
1400Sstevel@tonic-gate  *   returns the zone with which a path name is associated (global
1410Sstevel@tonic-gate  *   zone if the path is not within some other zone's file system
1420Sstevel@tonic-gate  *   hierarchy).  This currently requires iterating through each zone,
1430Sstevel@tonic-gate  *   so it is slower than an id or name search via a hash table.
1440Sstevel@tonic-gate  *
1450Sstevel@tonic-gate  *
1460Sstevel@tonic-gate  *   Locking:
1470Sstevel@tonic-gate  *
1480Sstevel@tonic-gate  *   zonehash_lock: This is a top-level global lock used to protect the
1490Sstevel@tonic-gate  *       zone hash tables and lists.  Zones cannot be created or destroyed
1500Sstevel@tonic-gate  *       while this lock is held.
1510Sstevel@tonic-gate  *   zone_status_lock: This is a global lock protecting zone state.
1520Sstevel@tonic-gate  *       Zones cannot change state while this lock is held.  It also
1530Sstevel@tonic-gate  *       protects the list of kernel threads associated with a zone.
1540Sstevel@tonic-gate  *   zone_lock: This is a per-zone lock used to protect several fields of
1550Sstevel@tonic-gate  *       the zone_t (see <sys/zone.h> for details).  In addition, holding
1560Sstevel@tonic-gate  *       this lock means that the zone cannot go away.
1573247Sgjelinek  *   zone_nlwps_lock: This is a per-zone lock used to protect the fields
1583247Sgjelinek  *	 related to the zone.max-lwps rctl.
1593247Sgjelinek  *   zone_mem_lock: This is a per-zone lock used to protect the fields
1603247Sgjelinek  *	 related to the zone.max-locked-memory and zone.max-swap rctls.
1610Sstevel@tonic-gate  *   zsd_key_lock: This is a global lock protecting the key state for ZSD.
1620Sstevel@tonic-gate  *   zone_deathrow_lock: This is a global lock protecting the "deathrow"
1630Sstevel@tonic-gate  *       list (a list of zones in the ZONE_IS_DEAD state).
1640Sstevel@tonic-gate  *
1650Sstevel@tonic-gate  *   Ordering requirements:
1660Sstevel@tonic-gate  *       pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock -->
1670Sstevel@tonic-gate  *       	zone_lock --> zsd_key_lock --> pidlock --> p_lock
1680Sstevel@tonic-gate  *
1693247Sgjelinek  *   When taking zone_mem_lock or zone_nlwps_lock, the lock ordering is:
1703247Sgjelinek  *	zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock
1713247Sgjelinek  *	zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock
1723247Sgjelinek  *
1730Sstevel@tonic-gate  *   Blocking memory allocations are permitted while holding any of the
1740Sstevel@tonic-gate  *   zone locks.
1750Sstevel@tonic-gate  *
1760Sstevel@tonic-gate  *
1770Sstevel@tonic-gate  *   System Call Interface:
1780Sstevel@tonic-gate  *
1790Sstevel@tonic-gate  *   The zone subsystem can be managed and queried from user level with
1800Sstevel@tonic-gate  *   the following system calls (all subcodes of the primary "zone"
1810Sstevel@tonic-gate  *   system call):
1820Sstevel@tonic-gate  *   - zone_create: creates a zone with selected attributes (name,
183789Sahrens  *     root path, privileges, resource controls, ZFS datasets)
1840Sstevel@tonic-gate  *   - zone_enter: allows the current process to enter a zone
1850Sstevel@tonic-gate  *   - zone_getattr: reports attributes of a zone
1862267Sdp  *   - zone_setattr: set attributes of a zone
1872267Sdp  *   - zone_boot: set 'init' running for the zone
1880Sstevel@tonic-gate  *   - zone_list: lists all zones active in the system
1890Sstevel@tonic-gate  *   - zone_lookup: looks up zone id based on name
1900Sstevel@tonic-gate  *   - zone_shutdown: initiates shutdown process (see states above)
1910Sstevel@tonic-gate  *   - zone_destroy: completes shutdown process (see states above)
1920Sstevel@tonic-gate  *
1930Sstevel@tonic-gate  */
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate #include <sys/priv_impl.h>
1960Sstevel@tonic-gate #include <sys/cred.h>
1970Sstevel@tonic-gate #include <c2/audit.h>
1980Sstevel@tonic-gate #include <sys/debug.h>
1990Sstevel@tonic-gate #include <sys/file.h>
2000Sstevel@tonic-gate #include <sys/kmem.h>
2013247Sgjelinek #include <sys/kstat.h>
2020Sstevel@tonic-gate #include <sys/mutex.h>
2031676Sjpk #include <sys/note.h>
2040Sstevel@tonic-gate #include <sys/pathname.h>
2050Sstevel@tonic-gate #include <sys/proc.h>
2060Sstevel@tonic-gate #include <sys/project.h>
2071166Sdstaff #include <sys/sysevent.h>
2080Sstevel@tonic-gate #include <sys/task.h>
2090Sstevel@tonic-gate #include <sys/systm.h>
2100Sstevel@tonic-gate #include <sys/types.h>
2110Sstevel@tonic-gate #include <sys/utsname.h>
2120Sstevel@tonic-gate #include <sys/vnode.h>
2130Sstevel@tonic-gate #include <sys/vfs.h>
2140Sstevel@tonic-gate #include <sys/systeminfo.h>
2150Sstevel@tonic-gate #include <sys/policy.h>
2160Sstevel@tonic-gate #include <sys/cred_impl.h>
2170Sstevel@tonic-gate #include <sys/contract_impl.h>
2180Sstevel@tonic-gate #include <sys/contract/process_impl.h>
2190Sstevel@tonic-gate #include <sys/class.h>
2200Sstevel@tonic-gate #include <sys/pool.h>
2210Sstevel@tonic-gate #include <sys/pool_pset.h>
2220Sstevel@tonic-gate #include <sys/pset.h>
2230Sstevel@tonic-gate #include <sys/sysmacros.h>
2240Sstevel@tonic-gate #include <sys/callb.h>
2250Sstevel@tonic-gate #include <sys/vmparam.h>
2260Sstevel@tonic-gate #include <sys/corectl.h>
2272677Sml93401 #include <sys/ipc_impl.h>
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate #include <sys/door.h>
2300Sstevel@tonic-gate #include <sys/cpuvar.h>
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate #include <sys/uadmin.h>
2330Sstevel@tonic-gate #include <sys/session.h>
2340Sstevel@tonic-gate #include <sys/cmn_err.h>
2350Sstevel@tonic-gate #include <sys/modhash.h>
2362267Sdp #include <sys/sunddi.h>
2370Sstevel@tonic-gate #include <sys/nvpair.h>
2380Sstevel@tonic-gate #include <sys/rctl.h>
2390Sstevel@tonic-gate #include <sys/fss.h>
2402712Snn35248 #include <sys/brand.h>
2410Sstevel@tonic-gate #include <sys/zone.h>
2423448Sdh155122 #include <net/if.h>
2433792Sakolb #include <sys/cpucaps.h>
2443247Sgjelinek #include <vm/seg.h>
2453247Sgjelinek 
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate  * cv used to signal that all references to the zone have been released.  This
2480Sstevel@tonic-gate  * needs to be global since there may be multiple waiters, and the first to
2490Sstevel@tonic-gate  * wake up will free the zone_t, hence we cannot use zone->zone_cv.
2500Sstevel@tonic-gate  */
2510Sstevel@tonic-gate static kcondvar_t zone_destroy_cv;
2520Sstevel@tonic-gate /*
2530Sstevel@tonic-gate  * Lock used to serialize access to zone_cv.  This could have been per-zone,
2540Sstevel@tonic-gate  * but then we'd need another lock for zone_destroy_cv, and why bother?
2550Sstevel@tonic-gate  */
2560Sstevel@tonic-gate static kmutex_t zone_status_lock;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate /*
2590Sstevel@tonic-gate  * ZSD-related global variables.
2600Sstevel@tonic-gate  */
2610Sstevel@tonic-gate static kmutex_t zsd_key_lock;	/* protects the following two */
2620Sstevel@tonic-gate /*
2630Sstevel@tonic-gate  * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval.
2640Sstevel@tonic-gate  */
2650Sstevel@tonic-gate static zone_key_t zsd_keyval = 0;
2660Sstevel@tonic-gate /*
2670Sstevel@tonic-gate  * Global list of registered keys.  We use this when a new zone is created.
2680Sstevel@tonic-gate  */
2690Sstevel@tonic-gate static list_t zsd_registered_keys;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate int zone_hash_size = 256;
2721676Sjpk static mod_hash_t *zonehashbyname, *zonehashbyid, *zonehashbylabel;
2730Sstevel@tonic-gate static kmutex_t zonehash_lock;
2740Sstevel@tonic-gate static uint_t zonecount;
2750Sstevel@tonic-gate static id_space_t *zoneid_space;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate /*
2780Sstevel@tonic-gate  * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the
2790Sstevel@tonic-gate  * kernel proper runs, and which manages all other zones.
2800Sstevel@tonic-gate  *
2810Sstevel@tonic-gate  * Although not declared as static, the variable "zone0" should not be used
2820Sstevel@tonic-gate  * except for by code that needs to reference the global zone early on in boot,
2830Sstevel@tonic-gate  * before it is fully initialized.  All other consumers should use
2840Sstevel@tonic-gate  * 'global_zone'.
2850Sstevel@tonic-gate  */
2860Sstevel@tonic-gate zone_t zone0;
2870Sstevel@tonic-gate zone_t *global_zone = NULL;	/* Set when the global zone is initialized */
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate /*
2900Sstevel@tonic-gate  * List of active zones, protected by zonehash_lock.
2910Sstevel@tonic-gate  */
2920Sstevel@tonic-gate static list_t zone_active;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate  * List of destroyed zones that still have outstanding cred references.
2960Sstevel@tonic-gate  * Used for debugging.  Uses a separate lock to avoid lock ordering
2970Sstevel@tonic-gate  * problems in zone_free.
2980Sstevel@tonic-gate  */
2990Sstevel@tonic-gate static list_t zone_deathrow;
3000Sstevel@tonic-gate static kmutex_t zone_deathrow_lock;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */
3030Sstevel@tonic-gate uint_t maxzones = 8192;
3040Sstevel@tonic-gate 
3051166Sdstaff /* Event channel to sent zone state change notifications */
3061166Sdstaff evchan_t *zone_event_chan;
3071166Sdstaff 
3081166Sdstaff /*
3091166Sdstaff  * This table holds the mapping from kernel zone states to
3101166Sdstaff  * states visible in the state notification API.
3111166Sdstaff  * The idea is that we only expose "obvious" states and
3121166Sdstaff  * do not expose states which are just implementation details.
3131166Sdstaff  */
3141166Sdstaff const char  *zone_status_table[] = {
3151166Sdstaff 	ZONE_EVENT_UNINITIALIZED,	/* uninitialized */
3161166Sdstaff 	ZONE_EVENT_READY,		/* ready */
3171166Sdstaff 	ZONE_EVENT_READY,		/* booting */
3181166Sdstaff 	ZONE_EVENT_RUNNING,		/* running */
3191166Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* shutting_down */
3201166Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* empty */
3211166Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* down */
3221166Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* dying */
3231166Sdstaff 	ZONE_EVENT_UNINITIALIZED,	/* dead */
3241166Sdstaff };
3251166Sdstaff 
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate  * This isn't static so lint doesn't complain.
3280Sstevel@tonic-gate  */
3290Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares;
3302768Ssl108498 rctl_hndl_t rc_zone_locked_mem;
3313247Sgjelinek rctl_hndl_t rc_zone_max_swap;
3323792Sakolb rctl_hndl_t rc_zone_cpu_cap;
3330Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps;
3342677Sml93401 rctl_hndl_t rc_zone_shmmax;
3352677Sml93401 rctl_hndl_t rc_zone_shmmni;
3362677Sml93401 rctl_hndl_t rc_zone_semmni;
3372677Sml93401 rctl_hndl_t rc_zone_msgmni;
3380Sstevel@tonic-gate /*
3390Sstevel@tonic-gate  * Synchronization primitives used to synchronize between mounts and zone
3400Sstevel@tonic-gate  * creation/destruction.
3410Sstevel@tonic-gate  */
3420Sstevel@tonic-gate static int mounts_in_progress;
3430Sstevel@tonic-gate static kcondvar_t mount_cv;
3440Sstevel@tonic-gate static kmutex_t mount_lock;
3450Sstevel@tonic-gate 
3462267Sdp const char * const zone_default_initname = "/sbin/init";
3471676Sjpk static char * const zone_prefix = "/zone/";
3480Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid);
3493448Sdh155122 static int zone_add_datalink(zoneid_t, char *);
3503448Sdh155122 static int zone_remove_datalink(zoneid_t, char *);
3513448Sdh155122 static int zone_check_datalink(zoneid_t *, char *);
3523448Sdh155122 static int zone_list_datalink(zoneid_t, int *, char *);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate /*
355813Sdp  * Bump this number when you alter the zone syscall interfaces; this is
356813Sdp  * because we need to have support for previous API versions in libc
357813Sdp  * to support patching; libc calls into the kernel to determine this number.
358813Sdp  *
359813Sdp  * Version 1 of the API is the version originally shipped with Solaris 10
360813Sdp  * Version 2 alters the zone_create system call in order to support more
361813Sdp  *     arguments by moving the args into a structure; and to do better
362813Sdp  *     error reporting when zone_create() fails.
363813Sdp  * Version 3 alters the zone_create system call in order to support the
364813Sdp  *     import of ZFS datasets to zones.
3651676Sjpk  * Version 4 alters the zone_create system call in order to support
3661676Sjpk  *     Trusted Extensions.
3672267Sdp  * Version 5 alters the zone_boot system call, and converts its old
3682267Sdp  *     bootargs parameter to be set by the zone_setattr API instead.
3693448Sdh155122  * Version 6 adds the flag argument to zone_create.
370813Sdp  */
3713448Sdh155122 static const int ZONE_SYSCALL_API_VERSION = 6;
372813Sdp 
373813Sdp /*
3740Sstevel@tonic-gate  * Certain filesystems (such as NFS and autofs) need to know which zone
3750Sstevel@tonic-gate  * the mount is being placed in.  Because of this, we need to be able to
3760Sstevel@tonic-gate  * ensure that a zone isn't in the process of being created such that
3770Sstevel@tonic-gate  * nfs_mount() thinks it is in the global zone, while by the time it
3780Sstevel@tonic-gate  * gets added the list of mounted zones, it ends up on zoneA's mount
3790Sstevel@tonic-gate  * list.
3800Sstevel@tonic-gate  *
3810Sstevel@tonic-gate  * The following functions: block_mounts()/resume_mounts() and
3820Sstevel@tonic-gate  * mount_in_progress()/mount_completed() are used by zones and the VFS
3830Sstevel@tonic-gate  * layer (respectively) to synchronize zone creation and new mounts.
3840Sstevel@tonic-gate  *
3850Sstevel@tonic-gate  * The semantics are like a reader-reader lock such that there may
3860Sstevel@tonic-gate  * either be multiple mounts (or zone creations, if that weren't
3870Sstevel@tonic-gate  * serialized by zonehash_lock) in progress at the same time, but not
3880Sstevel@tonic-gate  * both.
3890Sstevel@tonic-gate  *
3900Sstevel@tonic-gate  * We use cv's so the user can ctrl-C out of the operation if it's
3910Sstevel@tonic-gate  * taking too long.
3920Sstevel@tonic-gate  *
3930Sstevel@tonic-gate  * The semantics are such that there is unfair bias towards the
3940Sstevel@tonic-gate  * "current" operation.  This means that zone creations may starve if
3950Sstevel@tonic-gate  * there is a rapid succession of new mounts coming in to the system, or
3960Sstevel@tonic-gate  * there is a remote possibility that zones will be created at such a
3970Sstevel@tonic-gate  * rate that new mounts will not be able to proceed.
3980Sstevel@tonic-gate  */
3990Sstevel@tonic-gate /*
4000Sstevel@tonic-gate  * Prevent new mounts from progressing to the point of calling
4010Sstevel@tonic-gate  * VFS_MOUNT().  If there are already mounts in this "region", wait for
4020Sstevel@tonic-gate  * them to complete.
4030Sstevel@tonic-gate  */
4040Sstevel@tonic-gate static int
4050Sstevel@tonic-gate block_mounts(void)
4060Sstevel@tonic-gate {
4070Sstevel@tonic-gate 	int retval = 0;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	/*
4100Sstevel@tonic-gate 	 * Since it may block for a long time, block_mounts() shouldn't be
4110Sstevel@tonic-gate 	 * called with zonehash_lock held.
4120Sstevel@tonic-gate 	 */
4130Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
4140Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4150Sstevel@tonic-gate 	while (mounts_in_progress > 0) {
4160Sstevel@tonic-gate 		if (cv_wait_sig(&mount_cv, &mount_lock) == 0)
4170Sstevel@tonic-gate 			goto signaled;
4180Sstevel@tonic-gate 	}
4190Sstevel@tonic-gate 	/*
4200Sstevel@tonic-gate 	 * A negative value of mounts_in_progress indicates that mounts
4210Sstevel@tonic-gate 	 * have been blocked by (-mounts_in_progress) different callers.
4220Sstevel@tonic-gate 	 */
4230Sstevel@tonic-gate 	mounts_in_progress--;
4240Sstevel@tonic-gate 	retval = 1;
4250Sstevel@tonic-gate signaled:
4260Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4270Sstevel@tonic-gate 	return (retval);
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate /*
4310Sstevel@tonic-gate  * The VFS layer may progress with new mounts as far as we're concerned.
4320Sstevel@tonic-gate  * Allow them to progress if we were the last obstacle.
4330Sstevel@tonic-gate  */
4340Sstevel@tonic-gate static void
4350Sstevel@tonic-gate resume_mounts(void)
4360Sstevel@tonic-gate {
4370Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4380Sstevel@tonic-gate 	if (++mounts_in_progress == 0)
4390Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
4400Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate /*
4440Sstevel@tonic-gate  * The VFS layer is busy with a mount; zones should wait until all
4450Sstevel@tonic-gate  * mounts are completed to progress.
4460Sstevel@tonic-gate  */
4470Sstevel@tonic-gate void
4480Sstevel@tonic-gate mount_in_progress(void)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4510Sstevel@tonic-gate 	while (mounts_in_progress < 0)
4520Sstevel@tonic-gate 		cv_wait(&mount_cv, &mount_lock);
4530Sstevel@tonic-gate 	mounts_in_progress++;
4540Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate /*
4580Sstevel@tonic-gate  * VFS is done with one mount; wake up any waiting block_mounts()
4590Sstevel@tonic-gate  * callers if this is the last mount.
4600Sstevel@tonic-gate  */
4610Sstevel@tonic-gate void
4620Sstevel@tonic-gate mount_completed(void)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4650Sstevel@tonic-gate 	if (--mounts_in_progress == 0)
4660Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
4670Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate /*
4710Sstevel@tonic-gate  * ZSD routines.
4720Sstevel@tonic-gate  *
4730Sstevel@tonic-gate  * Zone Specific Data (ZSD) is modeled after Thread Specific Data as
4740Sstevel@tonic-gate  * defined by the pthread_key_create() and related interfaces.
4750Sstevel@tonic-gate  *
4760Sstevel@tonic-gate  * Kernel subsystems may register one or more data items and/or
4770Sstevel@tonic-gate  * callbacks to be executed when a zone is created, shutdown, or
4780Sstevel@tonic-gate  * destroyed.
4790Sstevel@tonic-gate  *
4800Sstevel@tonic-gate  * Unlike the thread counterpart, destructor callbacks will be executed
4810Sstevel@tonic-gate  * even if the data pointer is NULL and/or there are no constructor
4820Sstevel@tonic-gate  * callbacks, so it is the responsibility of such callbacks to check for
4830Sstevel@tonic-gate  * NULL data values if necessary.
4840Sstevel@tonic-gate  *
4850Sstevel@tonic-gate  * The locking strategy and overall picture is as follows:
4860Sstevel@tonic-gate  *
4870Sstevel@tonic-gate  * When someone calls zone_key_create(), a template ZSD entry is added to the
4880Sstevel@tonic-gate  * global list "zsd_registered_keys", protected by zsd_key_lock.  The
4890Sstevel@tonic-gate  * constructor callback is called immediately on all existing zones, and a
4900Sstevel@tonic-gate  * copy of the ZSD entry added to the per-zone zone_zsd list (protected by
4910Sstevel@tonic-gate  * zone_lock).  As this operation requires the list of zones, the list of
4920Sstevel@tonic-gate  * registered keys, and the per-zone list of ZSD entries to remain constant
4930Sstevel@tonic-gate  * throughout the entire operation, it must grab zonehash_lock, zone_lock for
4940Sstevel@tonic-gate  * all existing zones, and zsd_key_lock, in that order.  Similar locking is
4950Sstevel@tonic-gate  * needed when zone_key_delete() is called.  It is thus sufficient to hold
4960Sstevel@tonic-gate  * zsd_key_lock *or* zone_lock to prevent additions to or removals from the
4970Sstevel@tonic-gate  * per-zone zone_zsd list.
4980Sstevel@tonic-gate  *
4990Sstevel@tonic-gate  * Note that this implementation does not make a copy of the ZSD entry if a
5000Sstevel@tonic-gate  * constructor callback is not provided.  A zone_getspecific() on such an
5010Sstevel@tonic-gate  * uninitialized ZSD entry will return NULL.
5020Sstevel@tonic-gate  *
5030Sstevel@tonic-gate  * When new zones are created constructor callbacks for all registered ZSD
5040Sstevel@tonic-gate  * entries will be called.
5050Sstevel@tonic-gate  *
5060Sstevel@tonic-gate  * The framework does not provide any locking around zone_getspecific() and
5070Sstevel@tonic-gate  * zone_setspecific() apart from that needed for internal consistency, so
5080Sstevel@tonic-gate  * callers interested in atomic "test-and-set" semantics will need to provide
5090Sstevel@tonic-gate  * their own locking.
5100Sstevel@tonic-gate  */
5110Sstevel@tonic-gate void
5120Sstevel@tonic-gate zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t),
5130Sstevel@tonic-gate     void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *))
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate 	struct zsd_entry *zsdp;
5160Sstevel@tonic-gate 	struct zsd_entry *t;
5170Sstevel@tonic-gate 	struct zone *zone;
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	zsdp = kmem_alloc(sizeof (*zsdp), KM_SLEEP);
5200Sstevel@tonic-gate 	zsdp->zsd_data = NULL;
5210Sstevel@tonic-gate 	zsdp->zsd_create = create;
5220Sstevel@tonic-gate 	zsdp->zsd_shutdown = shutdown;
5230Sstevel@tonic-gate 	zsdp->zsd_destroy = destroy;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);	/* stop the world */
5260Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
5270Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
5280Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);	/* lock all zones */
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
5310Sstevel@tonic-gate 	*keyp = zsdp->zsd_key = ++zsd_keyval;
5320Sstevel@tonic-gate 	ASSERT(zsd_keyval != 0);
5330Sstevel@tonic-gate 	list_insert_tail(&zsd_registered_keys, zsdp);
5340Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	if (create != NULL) {
5370Sstevel@tonic-gate 		for (zone = list_head(&zone_active); zone != NULL;
5380Sstevel@tonic-gate 		    zone = list_next(&zone_active, zone)) {
5390Sstevel@tonic-gate 			t = kmem_alloc(sizeof (*t), KM_SLEEP);
5400Sstevel@tonic-gate 			t->zsd_key = *keyp;
5410Sstevel@tonic-gate 			t->zsd_data = (*create)(zone->zone_id);
5420Sstevel@tonic-gate 			t->zsd_create = create;
5430Sstevel@tonic-gate 			t->zsd_shutdown = shutdown;
5440Sstevel@tonic-gate 			t->zsd_destroy = destroy;
5450Sstevel@tonic-gate 			list_insert_tail(&zone->zone_zsd, t);
5460Sstevel@tonic-gate 		}
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
5490Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
5500Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
5510Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate /*
5550Sstevel@tonic-gate  * Helper function to find the zsd_entry associated with the key in the
5560Sstevel@tonic-gate  * given list.
5570Sstevel@tonic-gate  */
5580Sstevel@tonic-gate static struct zsd_entry *
5590Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key)
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate 	struct zsd_entry *zsd;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) {
5640Sstevel@tonic-gate 		if (zsd->zsd_key == key) {
5650Sstevel@tonic-gate 			/*
5660Sstevel@tonic-gate 			 * Move to head of list to keep list in MRU order.
5670Sstevel@tonic-gate 			 */
5680Sstevel@tonic-gate 			if (zsd != list_head(l)) {
5690Sstevel@tonic-gate 				list_remove(l, zsd);
5700Sstevel@tonic-gate 				list_insert_head(l, zsd);
5710Sstevel@tonic-gate 			}
5720Sstevel@tonic-gate 			return (zsd);
5730Sstevel@tonic-gate 		}
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 	return (NULL);
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate /*
5790Sstevel@tonic-gate  * Function called when a module is being unloaded, or otherwise wishes
5800Sstevel@tonic-gate  * to unregister its ZSD key and callbacks.
5810Sstevel@tonic-gate  */
5820Sstevel@tonic-gate int
5830Sstevel@tonic-gate zone_key_delete(zone_key_t key)
5840Sstevel@tonic-gate {
5850Sstevel@tonic-gate 	struct zsd_entry *zsdp = NULL;
5860Sstevel@tonic-gate 	zone_t *zone;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);	/* Zone create/delete waits for us */
5890Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
5900Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
5910Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);	/* lock all zones */
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
5940Sstevel@tonic-gate 	zsdp = zsd_find(&zsd_registered_keys, key);
5950Sstevel@tonic-gate 	if (zsdp == NULL)
5960Sstevel@tonic-gate 		goto notfound;
5970Sstevel@tonic-gate 	list_remove(&zsd_registered_keys, zsdp);
5980Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
6010Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
6020Sstevel@tonic-gate 		struct zsd_entry *del;
6030Sstevel@tonic-gate 		void *data;
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 		if (!(zone->zone_flags & ZF_DESTROYED)) {
6060Sstevel@tonic-gate 			del = zsd_find(&zone->zone_zsd, key);
6070Sstevel@tonic-gate 			if (del != NULL) {
6080Sstevel@tonic-gate 				data = del->zsd_data;
6090Sstevel@tonic-gate 				ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown);
6100Sstevel@tonic-gate 				ASSERT(del->zsd_destroy == zsdp->zsd_destroy);
6110Sstevel@tonic-gate 				list_remove(&zone->zone_zsd, del);
6120Sstevel@tonic-gate 				kmem_free(del, sizeof (*del));
6130Sstevel@tonic-gate 			} else {
6140Sstevel@tonic-gate 				data = NULL;
6150Sstevel@tonic-gate 			}
6160Sstevel@tonic-gate 			if (zsdp->zsd_shutdown)
6170Sstevel@tonic-gate 				zsdp->zsd_shutdown(zone->zone_id, data);
6180Sstevel@tonic-gate 			if (zsdp->zsd_destroy)
6190Sstevel@tonic-gate 				zsdp->zsd_destroy(zone->zone_id, data);
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
6240Sstevel@tonic-gate 	kmem_free(zsdp, sizeof (*zsdp));
6250Sstevel@tonic-gate 	return (0);
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate notfound:
6280Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
6290Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
6300Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
6310Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
6320Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
6330Sstevel@tonic-gate 	return (-1);
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate /*
6370Sstevel@tonic-gate  * ZSD counterpart of pthread_setspecific().
6380Sstevel@tonic-gate  */
6390Sstevel@tonic-gate int
6400Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data)
6410Sstevel@tonic-gate {
6420Sstevel@tonic-gate 	struct zsd_entry *t;
6430Sstevel@tonic-gate 	struct zsd_entry *zsdp = NULL;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
6460Sstevel@tonic-gate 	t = zsd_find(&zone->zone_zsd, key);
6470Sstevel@tonic-gate 	if (t != NULL) {
6480Sstevel@tonic-gate 		/*
6490Sstevel@tonic-gate 		 * Replace old value with new
6500Sstevel@tonic-gate 		 */
6510Sstevel@tonic-gate 		t->zsd_data = (void *)data;
6520Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
6530Sstevel@tonic-gate 		return (0);
6540Sstevel@tonic-gate 	}
6550Sstevel@tonic-gate 	/*
6560Sstevel@tonic-gate 	 * If there was no previous value, go through the list of registered
6570Sstevel@tonic-gate 	 * keys.
6580Sstevel@tonic-gate 	 *
6590Sstevel@tonic-gate 	 * We avoid grabbing zsd_key_lock until we are sure we need it; this is
6600Sstevel@tonic-gate 	 * necessary for shutdown callbacks to be able to execute without fear
6610Sstevel@tonic-gate 	 * of deadlock.
6620Sstevel@tonic-gate 	 */
6630Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
6640Sstevel@tonic-gate 	zsdp = zsd_find(&zsd_registered_keys, key);
6650Sstevel@tonic-gate 	if (zsdp == NULL) { 	/* Key was not registered */
6660Sstevel@tonic-gate 		mutex_exit(&zsd_key_lock);
6670Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
6680Sstevel@tonic-gate 		return (-1);
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	/*
6720Sstevel@tonic-gate 	 * Add a zsd_entry to this zone, using the template we just retrieved
6730Sstevel@tonic-gate 	 * to initialize the constructor and destructor(s).
6740Sstevel@tonic-gate 	 */
6750Sstevel@tonic-gate 	t = kmem_alloc(sizeof (*t), KM_SLEEP);
6760Sstevel@tonic-gate 	t->zsd_key = key;
6770Sstevel@tonic-gate 	t->zsd_data = (void *)data;
6780Sstevel@tonic-gate 	t->zsd_create = zsdp->zsd_create;
6790Sstevel@tonic-gate 	t->zsd_shutdown = zsdp->zsd_shutdown;
6800Sstevel@tonic-gate 	t->zsd_destroy = zsdp->zsd_destroy;
6810Sstevel@tonic-gate 	list_insert_tail(&zone->zone_zsd, t);
6820Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
6830Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
6840Sstevel@tonic-gate 	return (0);
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate /*
6880Sstevel@tonic-gate  * ZSD counterpart of pthread_getspecific().
6890Sstevel@tonic-gate  */
6900Sstevel@tonic-gate void *
6910Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone)
6920Sstevel@tonic-gate {
6930Sstevel@tonic-gate 	struct zsd_entry *t;
6940Sstevel@tonic-gate 	void *data;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
6970Sstevel@tonic-gate 	t = zsd_find(&zone->zone_zsd, key);
6980Sstevel@tonic-gate 	data = (t == NULL ? NULL : t->zsd_data);
6990Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
7000Sstevel@tonic-gate 	return (data);
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate /*
7040Sstevel@tonic-gate  * Function used to initialize a zone's list of ZSD callbacks and data
7050Sstevel@tonic-gate  * when the zone is being created.  The callbacks are initialized from
7060Sstevel@tonic-gate  * the template list (zsd_registered_keys), and the constructor
7070Sstevel@tonic-gate  * callback executed (if one exists).
7080Sstevel@tonic-gate  *
7090Sstevel@tonic-gate  * This is called before the zone is made publicly available, hence no
7100Sstevel@tonic-gate  * need to grab zone_lock.
7110Sstevel@tonic-gate  *
7120Sstevel@tonic-gate  * Although we grab and release zsd_key_lock, new entries cannot be
7130Sstevel@tonic-gate  * added to or removed from the zsd_registered_keys list until we
7140Sstevel@tonic-gate  * release zonehash_lock, so there isn't a window for a
7150Sstevel@tonic-gate  * zone_key_create() to come in after we've dropped zsd_key_lock but
7160Sstevel@tonic-gate  * before the zone is added to the zone list, such that the constructor
7170Sstevel@tonic-gate  * callbacks aren't executed for the new zone.
7180Sstevel@tonic-gate  */
7190Sstevel@tonic-gate static void
7200Sstevel@tonic-gate zone_zsd_configure(zone_t *zone)
7210Sstevel@tonic-gate {
7220Sstevel@tonic-gate 	struct zsd_entry *zsdp;
7230Sstevel@tonic-gate 	struct zsd_entry *t;
7240Sstevel@tonic-gate 	zoneid_t zoneid = zone->zone_id;
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
7270Sstevel@tonic-gate 	ASSERT(list_head(&zone->zone_zsd) == NULL);
7280Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
7290Sstevel@tonic-gate 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
7300Sstevel@tonic-gate 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
7310Sstevel@tonic-gate 		if (zsdp->zsd_create != NULL) {
7320Sstevel@tonic-gate 			t = kmem_alloc(sizeof (*t), KM_SLEEP);
7330Sstevel@tonic-gate 			t->zsd_key = zsdp->zsd_key;
7340Sstevel@tonic-gate 			t->zsd_create = zsdp->zsd_create;
7350Sstevel@tonic-gate 			t->zsd_data = (*t->zsd_create)(zoneid);
7360Sstevel@tonic-gate 			t->zsd_shutdown = zsdp->zsd_shutdown;
7370Sstevel@tonic-gate 			t->zsd_destroy = zsdp->zsd_destroy;
7380Sstevel@tonic-gate 			list_insert_tail(&zone->zone_zsd, t);
7390Sstevel@tonic-gate 		}
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
7420Sstevel@tonic-gate }
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY };
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate /*
7470Sstevel@tonic-gate  * Helper function to execute shutdown or destructor callbacks.
7480Sstevel@tonic-gate  */
7490Sstevel@tonic-gate static void
7500Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct)
7510Sstevel@tonic-gate {
7520Sstevel@tonic-gate 	struct zsd_entry *zsdp;
7530Sstevel@tonic-gate 	struct zsd_entry *t;
7540Sstevel@tonic-gate 	zoneid_t zoneid = zone->zone_id;
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY);
7570Sstevel@tonic-gate 	ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY);
7580Sstevel@tonic-gate 	ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN);
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
7610Sstevel@tonic-gate 	if (ct == ZSD_DESTROY) {
7620Sstevel@tonic-gate 		if (zone->zone_flags & ZF_DESTROYED) {
7630Sstevel@tonic-gate 			/*
7640Sstevel@tonic-gate 			 * Make sure destructors are only called once.
7650Sstevel@tonic-gate 			 */
7660Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
7670Sstevel@tonic-gate 			return;
7680Sstevel@tonic-gate 		}
7690Sstevel@tonic-gate 		zone->zone_flags |= ZF_DESTROYED;
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	/*
7740Sstevel@tonic-gate 	 * Both zsd_key_lock and zone_lock need to be held in order to add or
7750Sstevel@tonic-gate 	 * remove a ZSD key, (either globally as part of
7760Sstevel@tonic-gate 	 * zone_key_create()/zone_key_delete(), or on a per-zone basis, as is
7770Sstevel@tonic-gate 	 * possible through zone_setspecific()), so it's sufficient to hold
7780Sstevel@tonic-gate 	 * zsd_key_lock here.
7790Sstevel@tonic-gate 	 *
7800Sstevel@tonic-gate 	 * This is a good thing, since we don't want to recursively try to grab
7810Sstevel@tonic-gate 	 * zone_lock if a callback attempts to do something like a crfree() or
7820Sstevel@tonic-gate 	 * zone_rele().
7830Sstevel@tonic-gate 	 */
7840Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
7850Sstevel@tonic-gate 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
7860Sstevel@tonic-gate 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
7870Sstevel@tonic-gate 		zone_key_t key = zsdp->zsd_key;
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 		/* Skip if no callbacks registered */
7900Sstevel@tonic-gate 		if (ct == ZSD_SHUTDOWN && zsdp->zsd_shutdown == NULL)
7910Sstevel@tonic-gate 			continue;
7920Sstevel@tonic-gate 		if (ct == ZSD_DESTROY && zsdp->zsd_destroy == NULL)
7930Sstevel@tonic-gate 			continue;
7940Sstevel@tonic-gate 		/*
7950Sstevel@tonic-gate 		 * Call the callback with the zone-specific data if we can find
7960Sstevel@tonic-gate 		 * any, otherwise with NULL.
7970Sstevel@tonic-gate 		 */
7980Sstevel@tonic-gate 		t = zsd_find(&zone->zone_zsd, key);
7990Sstevel@tonic-gate 		if (t != NULL) {
8000Sstevel@tonic-gate 			if (ct == ZSD_SHUTDOWN) {
8010Sstevel@tonic-gate 				t->zsd_shutdown(zoneid, t->zsd_data);
8020Sstevel@tonic-gate 			} else {
8030Sstevel@tonic-gate 				ASSERT(ct == ZSD_DESTROY);
8040Sstevel@tonic-gate 				t->zsd_destroy(zoneid, t->zsd_data);
8050Sstevel@tonic-gate 			}
8060Sstevel@tonic-gate 		} else {
8070Sstevel@tonic-gate 			if (ct == ZSD_SHUTDOWN) {
8080Sstevel@tonic-gate 				zsdp->zsd_shutdown(zoneid, NULL);
8090Sstevel@tonic-gate 			} else {
8100Sstevel@tonic-gate 				ASSERT(ct == ZSD_DESTROY);
8110Sstevel@tonic-gate 				zsdp->zsd_destroy(zoneid, NULL);
8120Sstevel@tonic-gate 			}
8130Sstevel@tonic-gate 		}
8140Sstevel@tonic-gate 	}
8150Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate /*
8190Sstevel@tonic-gate  * Called when the zone is going away; free ZSD-related memory, and
8200Sstevel@tonic-gate  * destroy the zone_zsd list.
8210Sstevel@tonic-gate  */
8220Sstevel@tonic-gate static void
8230Sstevel@tonic-gate zone_free_zsd(zone_t *zone)
8240Sstevel@tonic-gate {
8250Sstevel@tonic-gate 	struct zsd_entry *t, *next;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	/*
8280Sstevel@tonic-gate 	 * Free all the zsd_entry's we had on this zone.
8290Sstevel@tonic-gate 	 */
8300Sstevel@tonic-gate 	for (t = list_head(&zone->zone_zsd); t != NULL; t = next) {
8310Sstevel@tonic-gate 		next = list_next(&zone->zone_zsd, t);
8320Sstevel@tonic-gate 		list_remove(&zone->zone_zsd, t);
8330Sstevel@tonic-gate 		kmem_free(t, sizeof (*t));
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 	list_destroy(&zone->zone_zsd);
8360Sstevel@tonic-gate }
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate /*
839789Sahrens  * Frees memory associated with the zone dataset list.
840789Sahrens  */
841789Sahrens static void
842789Sahrens zone_free_datasets(zone_t *zone)
843789Sahrens {
844789Sahrens 	zone_dataset_t *t, *next;
845789Sahrens 
846789Sahrens 	for (t = list_head(&zone->zone_datasets); t != NULL; t = next) {
847789Sahrens 		next = list_next(&zone->zone_datasets, t);
848789Sahrens 		list_remove(&zone->zone_datasets, t);
849789Sahrens 		kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1);
850789Sahrens 		kmem_free(t, sizeof (*t));
851789Sahrens 	}
852789Sahrens 	list_destroy(&zone->zone_datasets);
853789Sahrens }
854789Sahrens 
855789Sahrens /*
8560Sstevel@tonic-gate  * zone.cpu-shares resource control support.
8570Sstevel@tonic-gate  */
8580Sstevel@tonic-gate /*ARGSUSED*/
8590Sstevel@tonic-gate static rctl_qty_t
8600Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p)
8610Sstevel@tonic-gate {
8620Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8630Sstevel@tonic-gate 	return (p->p_zone->zone_shares);
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate /*ARGSUSED*/
8670Sstevel@tonic-gate static int
8680Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
8690Sstevel@tonic-gate     rctl_qty_t nv)
8700Sstevel@tonic-gate {
8710Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8720Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
8730Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
8740Sstevel@tonic-gate 		return (0);
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	e->rcep_p.zone->zone_shares = nv;
8770Sstevel@tonic-gate 	return (0);
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = {
8810Sstevel@tonic-gate 	rcop_no_action,
8820Sstevel@tonic-gate 	zone_cpu_shares_usage,
8830Sstevel@tonic-gate 	zone_cpu_shares_set,
8840Sstevel@tonic-gate 	rcop_no_test
8850Sstevel@tonic-gate };
8860Sstevel@tonic-gate 
8873792Sakolb /*
8883792Sakolb  * zone.cpu-cap resource control support.
8893792Sakolb  */
8903792Sakolb /*ARGSUSED*/
8913792Sakolb static rctl_qty_t
8923792Sakolb zone_cpu_cap_get(rctl_t *rctl, struct proc *p)
8933792Sakolb {
8943792Sakolb 	ASSERT(MUTEX_HELD(&p->p_lock));
8953792Sakolb 	return (cpucaps_zone_get(p->p_zone));
8963792Sakolb }
8973792Sakolb 
8983792Sakolb /*ARGSUSED*/
8993792Sakolb static int
9003792Sakolb zone_cpu_cap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
9013792Sakolb     rctl_qty_t nv)
9023792Sakolb {
9033792Sakolb 	zone_t *zone = e->rcep_p.zone;
9043792Sakolb 
9053792Sakolb 	ASSERT(MUTEX_HELD(&p->p_lock));
9063792Sakolb 	ASSERT(e->rcep_t == RCENTITY_ZONE);
9073792Sakolb 
9083792Sakolb 	if (zone == NULL)
9093792Sakolb 		return (0);
9103792Sakolb 
9113792Sakolb 	/*
9123792Sakolb 	 * set cap to the new value.
9133792Sakolb 	 */
9143792Sakolb 	return (cpucaps_zone_set(zone, nv));
9153792Sakolb }
9163792Sakolb 
9173792Sakolb static rctl_ops_t zone_cpu_cap_ops = {
9183792Sakolb 	rcop_no_action,
9193792Sakolb 	zone_cpu_cap_get,
9203792Sakolb 	zone_cpu_cap_set,
9213792Sakolb 	rcop_no_test
9223792Sakolb };
9233792Sakolb 
9240Sstevel@tonic-gate /*ARGSUSED*/
9250Sstevel@tonic-gate static rctl_qty_t
9260Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p)
9270Sstevel@tonic-gate {
9280Sstevel@tonic-gate 	rctl_qty_t nlwps;
9290Sstevel@tonic-gate 	zone_t *zone = p->p_zone;
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
9340Sstevel@tonic-gate 	nlwps = zone->zone_nlwps;
9350Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	return (nlwps);
9380Sstevel@tonic-gate }
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate /*ARGSUSED*/
9410Sstevel@tonic-gate static int
9420Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
9430Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
9440Sstevel@tonic-gate {
9450Sstevel@tonic-gate 	rctl_qty_t nlwps;
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
9480Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
9490Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
9500Sstevel@tonic-gate 		return (0);
9510Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock)));
9520Sstevel@tonic-gate 	nlwps = e->rcep_p.zone->zone_nlwps;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	if (nlwps + incr > rcntl->rcv_value)
9550Sstevel@tonic-gate 		return (1);
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	return (0);
9580Sstevel@tonic-gate }
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate /*ARGSUSED*/
9610Sstevel@tonic-gate static int
9622768Ssl108498 zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv)
9632768Ssl108498 {
9640Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
9650Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
9660Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
9670Sstevel@tonic-gate 		return (0);
9680Sstevel@tonic-gate 	e->rcep_p.zone->zone_nlwps_ctl = nv;
9690Sstevel@tonic-gate 	return (0);
9700Sstevel@tonic-gate }
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = {
9730Sstevel@tonic-gate 	rcop_no_action,
9740Sstevel@tonic-gate 	zone_lwps_usage,
9750Sstevel@tonic-gate 	zone_lwps_set,
9760Sstevel@tonic-gate 	zone_lwps_test,
9770Sstevel@tonic-gate };
9780Sstevel@tonic-gate 
9792677Sml93401 /*ARGSUSED*/
9802677Sml93401 static int
9812677Sml93401 zone_shmmax_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
9822677Sml93401     rctl_qty_t incr, uint_t flags)
9832677Sml93401 {
9842677Sml93401 	rctl_qty_t v;
9852677Sml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
9862677Sml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
9872677Sml93401 	v = e->rcep_p.zone->zone_shmmax + incr;
9882677Sml93401 	if (v > rval->rcv_value)
9892677Sml93401 		return (1);
9902677Sml93401 	return (0);
9912677Sml93401 }
9922677Sml93401 
9932677Sml93401 static rctl_ops_t zone_shmmax_ops = {
9942677Sml93401 	rcop_no_action,
9952677Sml93401 	rcop_no_usage,
9962677Sml93401 	rcop_no_set,
9972677Sml93401 	zone_shmmax_test
9982677Sml93401 };
9992677Sml93401 
10002677Sml93401 /*ARGSUSED*/
10012677Sml93401 static int
10022677Sml93401 zone_shmmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
10032677Sml93401     rctl_qty_t incr, uint_t flags)
10042677Sml93401 {
10052677Sml93401 	rctl_qty_t v;
10062677Sml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
10072677Sml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
10082677Sml93401 	v = e->rcep_p.zone->zone_ipc.ipcq_shmmni + incr;
10092677Sml93401 	if (v > rval->rcv_value)
10102677Sml93401 		return (1);
10112677Sml93401 	return (0);
10122677Sml93401 }
10132677Sml93401 
10142677Sml93401 static rctl_ops_t zone_shmmni_ops = {
10152677Sml93401 	rcop_no_action,
10162677Sml93401 	rcop_no_usage,
10172677Sml93401 	rcop_no_set,
10182677Sml93401 	zone_shmmni_test
10192677Sml93401 };
10202677Sml93401 
10212677Sml93401 /*ARGSUSED*/
10222677Sml93401 static int
10232677Sml93401 zone_semmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
10242677Sml93401     rctl_qty_t incr, uint_t flags)
10252677Sml93401 {
10262677Sml93401 	rctl_qty_t v;
10272677Sml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
10282677Sml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
10292677Sml93401 	v = e->rcep_p.zone->zone_ipc.ipcq_semmni + incr;
10302677Sml93401 	if (v > rval->rcv_value)
10312677Sml93401 		return (1);
10322677Sml93401 	return (0);
10332677Sml93401 }
10342677Sml93401 
10352677Sml93401 static rctl_ops_t zone_semmni_ops = {
10362677Sml93401 	rcop_no_action,
10372677Sml93401 	rcop_no_usage,
10382677Sml93401 	rcop_no_set,
10392677Sml93401 	zone_semmni_test
10402677Sml93401 };
10412677Sml93401 
10422677Sml93401 /*ARGSUSED*/
10432677Sml93401 static int
10442677Sml93401 zone_msgmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
10452677Sml93401     rctl_qty_t incr, uint_t flags)
10462677Sml93401 {
10472677Sml93401 	rctl_qty_t v;
10482677Sml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
10492677Sml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
10502677Sml93401 	v = e->rcep_p.zone->zone_ipc.ipcq_msgmni + incr;
10512677Sml93401 	if (v > rval->rcv_value)
10522677Sml93401 		return (1);
10532677Sml93401 	return (0);
10542677Sml93401 }
10552677Sml93401 
10562677Sml93401 static rctl_ops_t zone_msgmni_ops = {
10572677Sml93401 	rcop_no_action,
10582677Sml93401 	rcop_no_usage,
10592677Sml93401 	rcop_no_set,
10602677Sml93401 	zone_msgmni_test
10612677Sml93401 };
10622677Sml93401 
10632768Ssl108498 /*ARGSUSED*/
10642768Ssl108498 static rctl_qty_t
10652768Ssl108498 zone_locked_mem_usage(rctl_t *rctl, struct proc *p)
10662768Ssl108498 {
10672768Ssl108498 	rctl_qty_t q;
10682768Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
10693247Sgjelinek 	mutex_enter(&p->p_zone->zone_mem_lock);
10702768Ssl108498 	q = p->p_zone->zone_locked_mem;
10713247Sgjelinek 	mutex_exit(&p->p_zone->zone_mem_lock);
10722768Ssl108498 	return (q);
10732768Ssl108498 }
10742768Ssl108498 
10752768Ssl108498 /*ARGSUSED*/
10762768Ssl108498 static int
10772768Ssl108498 zone_locked_mem_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
10782768Ssl108498     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
10792768Ssl108498 {
10802768Ssl108498 	rctl_qty_t q;
10813247Sgjelinek 	zone_t *z;
10823247Sgjelinek 
10833247Sgjelinek 	z = e->rcep_p.zone;
10842768Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
10853247Sgjelinek 	ASSERT(MUTEX_HELD(&z->zone_mem_lock));
10863247Sgjelinek 	q = z->zone_locked_mem;
10872768Ssl108498 	if (q + incr > rcntl->rcv_value)
10882768Ssl108498 		return (1);
10892768Ssl108498 	return (0);
10902768Ssl108498 }
10912768Ssl108498 
10922768Ssl108498 /*ARGSUSED*/
10932768Ssl108498 static int
10942768Ssl108498 zone_locked_mem_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
10952768Ssl108498     rctl_qty_t nv)
10962768Ssl108498 {
10972768Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
10982768Ssl108498 	ASSERT(e->rcep_t == RCENTITY_ZONE);
10992768Ssl108498 	if (e->rcep_p.zone == NULL)
11002768Ssl108498 		return (0);
11012768Ssl108498 	e->rcep_p.zone->zone_locked_mem_ctl = nv;
11022768Ssl108498 	return (0);
11032768Ssl108498 }
11042768Ssl108498 
11052768Ssl108498 static rctl_ops_t zone_locked_mem_ops = {
11062768Ssl108498 	rcop_no_action,
11072768Ssl108498 	zone_locked_mem_usage,
11082768Ssl108498 	zone_locked_mem_set,
11092768Ssl108498 	zone_locked_mem_test
11102768Ssl108498 };
11112677Sml93401 
11123247Sgjelinek /*ARGSUSED*/
11133247Sgjelinek static rctl_qty_t
11143247Sgjelinek zone_max_swap_usage(rctl_t *rctl, struct proc *p)
11153247Sgjelinek {
11163247Sgjelinek 	rctl_qty_t q;
11173247Sgjelinek 	zone_t *z = p->p_zone;
11183247Sgjelinek 
11193247Sgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
11203247Sgjelinek 	mutex_enter(&z->zone_mem_lock);
11213247Sgjelinek 	q = z->zone_max_swap;
11223247Sgjelinek 	mutex_exit(&z->zone_mem_lock);
11233247Sgjelinek 	return (q);
11243247Sgjelinek }
11253247Sgjelinek 
11263247Sgjelinek /*ARGSUSED*/
11273247Sgjelinek static int
11283247Sgjelinek zone_max_swap_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
11293247Sgjelinek     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
11303247Sgjelinek {
11313247Sgjelinek 	rctl_qty_t q;
11323247Sgjelinek 	zone_t *z;
11333247Sgjelinek 
11343247Sgjelinek 	z = e->rcep_p.zone;
11353247Sgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
11363247Sgjelinek 	ASSERT(MUTEX_HELD(&z->zone_mem_lock));
11373247Sgjelinek 	q = z->zone_max_swap;
11383247Sgjelinek 	if (q + incr > rcntl->rcv_value)
11393247Sgjelinek 		return (1);
11403247Sgjelinek 	return (0);
11413247Sgjelinek }
11423247Sgjelinek 
11433247Sgjelinek /*ARGSUSED*/
11443247Sgjelinek static int
11453247Sgjelinek zone_max_swap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
11463247Sgjelinek     rctl_qty_t nv)
11473247Sgjelinek {
11483247Sgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
11493247Sgjelinek 	ASSERT(e->rcep_t == RCENTITY_ZONE);
11503247Sgjelinek 	if (e->rcep_p.zone == NULL)
11513247Sgjelinek 		return (0);
11523247Sgjelinek 	e->rcep_p.zone->zone_max_swap_ctl = nv;
11533247Sgjelinek 	return (0);
11543247Sgjelinek }
11553247Sgjelinek 
11563247Sgjelinek static rctl_ops_t zone_max_swap_ops = {
11573247Sgjelinek 	rcop_no_action,
11583247Sgjelinek 	zone_max_swap_usage,
11593247Sgjelinek 	zone_max_swap_set,
11603247Sgjelinek 	zone_max_swap_test
11613247Sgjelinek };
11623247Sgjelinek 
11630Sstevel@tonic-gate /*
11640Sstevel@tonic-gate  * Helper function to brand the zone with a unique ID.
11650Sstevel@tonic-gate  */
11660Sstevel@tonic-gate static void
11670Sstevel@tonic-gate zone_uniqid(zone_t *zone)
11680Sstevel@tonic-gate {
11690Sstevel@tonic-gate 	static uint64_t uniqid = 0;
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
11720Sstevel@tonic-gate 	zone->zone_uniqid = uniqid++;
11730Sstevel@tonic-gate }
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate /*
11760Sstevel@tonic-gate  * Returns a held pointer to the "kcred" for the specified zone.
11770Sstevel@tonic-gate  */
11780Sstevel@tonic-gate struct cred *
11790Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid)
11800Sstevel@tonic-gate {
11810Sstevel@tonic-gate 	zone_t *zone;
11820Sstevel@tonic-gate 	cred_t *cr;
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	if ((zone = zone_find_by_id(zoneid)) == NULL)
11850Sstevel@tonic-gate 		return (NULL);
11860Sstevel@tonic-gate 	cr = zone->zone_kcred;
11870Sstevel@tonic-gate 	crhold(cr);
11880Sstevel@tonic-gate 	zone_rele(zone);
11890Sstevel@tonic-gate 	return (cr);
11900Sstevel@tonic-gate }
11910Sstevel@tonic-gate 
11923247Sgjelinek static int
11933247Sgjelinek zone_lockedmem_kstat_update(kstat_t *ksp, int rw)
11943247Sgjelinek {
11953247Sgjelinek 	zone_t *zone = ksp->ks_private;
11963247Sgjelinek 	zone_kstat_t *zk = ksp->ks_data;
11973247Sgjelinek 
11983247Sgjelinek 	if (rw == KSTAT_WRITE)
11993247Sgjelinek 		return (EACCES);
12003247Sgjelinek 
12013247Sgjelinek 	zk->zk_usage.value.ui64 = zone->zone_locked_mem;
12023247Sgjelinek 	zk->zk_value.value.ui64 = zone->zone_locked_mem_ctl;
12033247Sgjelinek 	return (0);
12043247Sgjelinek }
12053247Sgjelinek 
12063247Sgjelinek static int
12073247Sgjelinek zone_swapresv_kstat_update(kstat_t *ksp, int rw)
12083247Sgjelinek {
12093247Sgjelinek 	zone_t *zone = ksp->ks_private;
12103247Sgjelinek 	zone_kstat_t *zk = ksp->ks_data;
12113247Sgjelinek 
12123247Sgjelinek 	if (rw == KSTAT_WRITE)
12133247Sgjelinek 		return (EACCES);
12143247Sgjelinek 
12153247Sgjelinek 	zk->zk_usage.value.ui64 = zone->zone_max_swap;
12163247Sgjelinek 	zk->zk_value.value.ui64 = zone->zone_max_swap_ctl;
12173247Sgjelinek 	return (0);
12183247Sgjelinek }
12193247Sgjelinek 
12203247Sgjelinek static void
12213247Sgjelinek zone_kstat_create(zone_t *zone)
12223247Sgjelinek {
12233247Sgjelinek 	kstat_t *ksp;
12243247Sgjelinek 	zone_kstat_t *zk;
12253247Sgjelinek 
12263247Sgjelinek 	ksp = rctl_kstat_create_zone(zone, "lockedmem", KSTAT_TYPE_NAMED,
12273247Sgjelinek 	    sizeof (zone_kstat_t) / sizeof (kstat_named_t),
12283247Sgjelinek 	    KSTAT_FLAG_VIRTUAL);
12293247Sgjelinek 
12303247Sgjelinek 	if (ksp == NULL)
12313247Sgjelinek 		return;
12323247Sgjelinek 
12333247Sgjelinek 	zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP);
12343247Sgjelinek 	ksp->ks_data_size += strlen(zone->zone_name) + 1;
12353247Sgjelinek 	kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING);
12363247Sgjelinek 	kstat_named_setstr(&zk->zk_zonename, zone->zone_name);
12373247Sgjelinek 	kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64);
12383247Sgjelinek 	kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64);
12393247Sgjelinek 	ksp->ks_update = zone_lockedmem_kstat_update;
12403247Sgjelinek 	ksp->ks_private = zone;
12413247Sgjelinek 	kstat_install(ksp);
12423247Sgjelinek 
12433247Sgjelinek 	zone->zone_lockedmem_kstat = ksp;
12443247Sgjelinek 
12453247Sgjelinek 	ksp = rctl_kstat_create_zone(zone, "swapresv", KSTAT_TYPE_NAMED,
12463247Sgjelinek 	    sizeof (zone_kstat_t) / sizeof (kstat_named_t),
12473247Sgjelinek 	    KSTAT_FLAG_VIRTUAL);
12483247Sgjelinek 
12493247Sgjelinek 	if (ksp == NULL)
12503247Sgjelinek 		return;
12513247Sgjelinek 
12523247Sgjelinek 	zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP);
12533247Sgjelinek 	ksp->ks_data_size += strlen(zone->zone_name) + 1;
12543247Sgjelinek 	kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING);
12553247Sgjelinek 	kstat_named_setstr(&zk->zk_zonename, zone->zone_name);
12563247Sgjelinek 	kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64);
12573247Sgjelinek 	kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64);
12583247Sgjelinek 	ksp->ks_update = zone_swapresv_kstat_update;
12593247Sgjelinek 	ksp->ks_private = zone;
12603247Sgjelinek 	kstat_install(ksp);
12613247Sgjelinek 
12623247Sgjelinek 	zone->zone_swapresv_kstat = ksp;
12633247Sgjelinek }
12643247Sgjelinek 
12653247Sgjelinek static void
12663247Sgjelinek zone_kstat_delete(zone_t *zone)
12673247Sgjelinek {
12683247Sgjelinek 	void *data;
12693247Sgjelinek 
12703247Sgjelinek 	if (zone->zone_lockedmem_kstat != NULL) {
12713247Sgjelinek 		data = zone->zone_lockedmem_kstat->ks_data;
12723247Sgjelinek 		kstat_delete(zone->zone_lockedmem_kstat);
12733247Sgjelinek 		kmem_free(data, sizeof (zone_kstat_t));
12743247Sgjelinek 	}
12753247Sgjelinek 	if (zone->zone_swapresv_kstat != NULL) {
12763247Sgjelinek 		data = zone->zone_swapresv_kstat->ks_data;
12773247Sgjelinek 		kstat_delete(zone->zone_swapresv_kstat);
12783247Sgjelinek 		kmem_free(data, sizeof (zone_kstat_t));
12793247Sgjelinek 	}
12803247Sgjelinek }
12813247Sgjelinek 
12820Sstevel@tonic-gate /*
12830Sstevel@tonic-gate  * Called very early on in boot to initialize the ZSD list so that
12840Sstevel@tonic-gate  * zone_key_create() can be called before zone_init().  It also initializes
12850Sstevel@tonic-gate  * portions of zone0 which may be used before zone_init() is called.  The
12860Sstevel@tonic-gate  * variable "global_zone" will be set when zone0 is fully initialized by
12870Sstevel@tonic-gate  * zone_init().
12880Sstevel@tonic-gate  */
12890Sstevel@tonic-gate void
12900Sstevel@tonic-gate zone_zsd_init(void)
12910Sstevel@tonic-gate {
12920Sstevel@tonic-gate 	mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL);
12930Sstevel@tonic-gate 	mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL);
12940Sstevel@tonic-gate 	list_create(&zsd_registered_keys, sizeof (struct zsd_entry),
12950Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
12960Sstevel@tonic-gate 	list_create(&zone_active, sizeof (zone_t),
12970Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
12980Sstevel@tonic-gate 	list_create(&zone_deathrow, sizeof (zone_t),
12990Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 	mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL);
13020Sstevel@tonic-gate 	mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
13033247Sgjelinek 	mutex_init(&zone0.zone_mem_lock, NULL, MUTEX_DEFAULT, NULL);
13040Sstevel@tonic-gate 	zone0.zone_shares = 1;
13053247Sgjelinek 	zone0.zone_nlwps = 0;
13060Sstevel@tonic-gate 	zone0.zone_nlwps_ctl = INT_MAX;
13073247Sgjelinek 	zone0.zone_locked_mem = 0;
13083247Sgjelinek 	zone0.zone_locked_mem_ctl = UINT64_MAX;
13093247Sgjelinek 	ASSERT(zone0.zone_max_swap == 0);
13103247Sgjelinek 	zone0.zone_max_swap_ctl = UINT64_MAX;
13112677Sml93401 	zone0.zone_shmmax = 0;
13122677Sml93401 	zone0.zone_ipc.ipcq_shmmni = 0;
13132677Sml93401 	zone0.zone_ipc.ipcq_semmni = 0;
13142677Sml93401 	zone0.zone_ipc.ipcq_msgmni = 0;
13150Sstevel@tonic-gate 	zone0.zone_name = GLOBAL_ZONENAME;
13160Sstevel@tonic-gate 	zone0.zone_nodename = utsname.nodename;
13170Sstevel@tonic-gate 	zone0.zone_domain = srpc_domain;
13180Sstevel@tonic-gate 	zone0.zone_ref = 1;
13190Sstevel@tonic-gate 	zone0.zone_id = GLOBAL_ZONEID;
13200Sstevel@tonic-gate 	zone0.zone_status = ZONE_IS_RUNNING;
13210Sstevel@tonic-gate 	zone0.zone_rootpath = "/";
13220Sstevel@tonic-gate 	zone0.zone_rootpathlen = 2;
13230Sstevel@tonic-gate 	zone0.zone_psetid = ZONE_PS_INVAL;
13240Sstevel@tonic-gate 	zone0.zone_ncpus = 0;
13250Sstevel@tonic-gate 	zone0.zone_ncpus_online = 0;
13260Sstevel@tonic-gate 	zone0.zone_proc_initpid = 1;
13272267Sdp 	zone0.zone_initname = initname;
13283247Sgjelinek 	zone0.zone_lockedmem_kstat = NULL;
13293247Sgjelinek 	zone0.zone_swapresv_kstat = NULL;
13300Sstevel@tonic-gate 	list_create(&zone0.zone_zsd, sizeof (struct zsd_entry),
13310Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
13320Sstevel@tonic-gate 	list_insert_head(&zone_active, &zone0);
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	/*
13350Sstevel@tonic-gate 	 * The root filesystem is not mounted yet, so zone_rootvp cannot be set
13360Sstevel@tonic-gate 	 * to anything meaningful.  It is assigned to be 'rootdir' in
13370Sstevel@tonic-gate 	 * vfs_mountroot().
13380Sstevel@tonic-gate 	 */
13390Sstevel@tonic-gate 	zone0.zone_rootvp = NULL;
13400Sstevel@tonic-gate 	zone0.zone_vfslist = NULL;
13412267Sdp 	zone0.zone_bootargs = initargs;
13420Sstevel@tonic-gate 	zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
13430Sstevel@tonic-gate 	/*
13440Sstevel@tonic-gate 	 * The global zone has all privileges
13450Sstevel@tonic-gate 	 */
13460Sstevel@tonic-gate 	priv_fillset(zone0.zone_privset);
13470Sstevel@tonic-gate 	/*
13480Sstevel@tonic-gate 	 * Add p0 to the global zone
13490Sstevel@tonic-gate 	 */
13500Sstevel@tonic-gate 	zone0.zone_zsched = &p0;
13510Sstevel@tonic-gate 	p0.p_zone = &zone0;
13520Sstevel@tonic-gate }
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate /*
13551676Sjpk  * Compute a hash value based on the contents of the label and the DOI.  The
13561676Sjpk  * hash algorithm is somewhat arbitrary, but is based on the observation that
13571676Sjpk  * humans will likely pick labels that differ by amounts that work out to be
13581676Sjpk  * multiples of the number of hash chains, and thus stirring in some primes
13591676Sjpk  * should help.
13601676Sjpk  */
13611676Sjpk static uint_t
13621676Sjpk hash_bylabel(void *hdata, mod_hash_key_t key)
13631676Sjpk {
13641676Sjpk 	const ts_label_t *lab = (ts_label_t *)key;
13651676Sjpk 	const uint32_t *up, *ue;
13661676Sjpk 	uint_t hash;
13671676Sjpk 	int i;
13681676Sjpk 
13691676Sjpk 	_NOTE(ARGUNUSED(hdata));
13701676Sjpk 
13711676Sjpk 	hash = lab->tsl_doi + (lab->tsl_doi << 1);
13721676Sjpk 	/* we depend on alignment of label, but not representation */
13731676Sjpk 	up = (const uint32_t *)&lab->tsl_label;
13741676Sjpk 	ue = up + sizeof (lab->tsl_label) / sizeof (*up);
13751676Sjpk 	i = 1;
13761676Sjpk 	while (up < ue) {
13771676Sjpk 		/* using 2^n + 1, 1 <= n <= 16 as source of many primes */
13781676Sjpk 		hash += *up + (*up << ((i % 16) + 1));
13791676Sjpk 		up++;
13801676Sjpk 		i++;
13811676Sjpk 	}
13821676Sjpk 	return (hash);
13831676Sjpk }
13841676Sjpk 
13851676Sjpk /*
13861676Sjpk  * All that mod_hash cares about here is zero (equal) versus non-zero (not
13871676Sjpk  * equal).  This may need to be changed if less than / greater than is ever
13881676Sjpk  * needed.
13891676Sjpk  */
13901676Sjpk static int
13911676Sjpk hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
13921676Sjpk {
13931676Sjpk 	ts_label_t *lab1 = (ts_label_t *)key1;
13941676Sjpk 	ts_label_t *lab2 = (ts_label_t *)key2;
13951676Sjpk 
13961676Sjpk 	return (label_equal(lab1, lab2) ? 0 : 1);
13971676Sjpk }
13981676Sjpk 
13991676Sjpk /*
14000Sstevel@tonic-gate  * Called by main() to initialize the zones framework.
14010Sstevel@tonic-gate  */
14020Sstevel@tonic-gate void
14030Sstevel@tonic-gate zone_init(void)
14040Sstevel@tonic-gate {
14050Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
14060Sstevel@tonic-gate 	rctl_val_t *dval;
14070Sstevel@tonic-gate 	rctl_set_t *set;
14080Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
14090Sstevel@tonic-gate 	rctl_entity_p_t e;
14101166Sdstaff 	int res;
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 	ASSERT(curproc == &p0);
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	/*
14150Sstevel@tonic-gate 	 * Create ID space for zone IDs.  ID 0 is reserved for the
14160Sstevel@tonic-gate 	 * global zone.
14170Sstevel@tonic-gate 	 */
14180Sstevel@tonic-gate 	zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID);
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	/*
14210Sstevel@tonic-gate 	 * Initialize generic zone resource controls, if any.
14220Sstevel@tonic-gate 	 */
14230Sstevel@tonic-gate 	rc_zone_cpu_shares = rctl_register("zone.cpu-shares",
14240Sstevel@tonic-gate 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER |
14251996Sml93401 	    RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER,
14263792Sakolb 	    FSS_MAXSHARES, FSS_MAXSHARES, &zone_cpu_shares_ops);
14273792Sakolb 
14283792Sakolb 	rc_zone_cpu_cap = rctl_register("zone.cpu-cap",
14293792Sakolb 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_ALWAYS |
14303792Sakolb 	    RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT |RCTL_GLOBAL_SYSLOG_NEVER |
14313792Sakolb 	    RCTL_GLOBAL_INFINITE,
14323792Sakolb 	    MAXCAP, MAXCAP, &zone_cpu_cap_ops);
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 	rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE,
14350Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
14360Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &zone_lwps_ops);
14370Sstevel@tonic-gate 	/*
14382677Sml93401 	 * System V IPC resource controls
14392677Sml93401 	 */
14402677Sml93401 	rc_zone_msgmni = rctl_register("zone.max-msg-ids",
14412677Sml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
14422677Sml93401 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_msgmni_ops);
14432677Sml93401 
14442677Sml93401 	rc_zone_semmni = rctl_register("zone.max-sem-ids",
14452677Sml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
14462677Sml93401 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_semmni_ops);
14472677Sml93401 
14482677Sml93401 	rc_zone_shmmni = rctl_register("zone.max-shm-ids",
14492677Sml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
14502677Sml93401 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_shmmni_ops);
14512677Sml93401 
14522677Sml93401 	rc_zone_shmmax = rctl_register("zone.max-shm-memory",
14532677Sml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
14542677Sml93401 	    RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &zone_shmmax_ops);
14552677Sml93401 
14562677Sml93401 	/*
14570Sstevel@tonic-gate 	 * Create a rctl_val with PRIVILEGED, NOACTION, value = 1.  Then attach
14580Sstevel@tonic-gate 	 * this at the head of the rctl_dict_entry for ``zone.cpu-shares''.
14590Sstevel@tonic-gate 	 */
14600Sstevel@tonic-gate 	dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
14610Sstevel@tonic-gate 	bzero(dval, sizeof (rctl_val_t));
14620Sstevel@tonic-gate 	dval->rcv_value = 1;
14630Sstevel@tonic-gate 	dval->rcv_privilege = RCPRIV_PRIVILEGED;
14640Sstevel@tonic-gate 	dval->rcv_flagaction = RCTL_LOCAL_NOACTION;
14650Sstevel@tonic-gate 	dval->rcv_action_recip_pid = -1;
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 	rde = rctl_dict_lookup("zone.cpu-shares");
14680Sstevel@tonic-gate 	(void) rctl_val_list_insert(&rde->rcd_default_value, dval);
14690Sstevel@tonic-gate 
14702768Ssl108498 	rc_zone_locked_mem = rctl_register("zone.max-locked-memory",
14712768Ssl108498 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES |
14722768Ssl108498 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
14732768Ssl108498 	    &zone_locked_mem_ops);
14743247Sgjelinek 
14753247Sgjelinek 	rc_zone_max_swap = rctl_register("zone.max-swap",
14763247Sgjelinek 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES |
14773247Sgjelinek 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
14783247Sgjelinek 	    &zone_max_swap_ops);
14793247Sgjelinek 
14800Sstevel@tonic-gate 	/*
14810Sstevel@tonic-gate 	 * Initialize the ``global zone''.
14820Sstevel@tonic-gate 	 */
14830Sstevel@tonic-gate 	set = rctl_set_create();
14840Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
14850Sstevel@tonic-gate 	mutex_enter(&p0.p_lock);
14860Sstevel@tonic-gate 	e.rcep_p.zone = &zone0;
14870Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
14880Sstevel@tonic-gate 	zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set,
14890Sstevel@tonic-gate 	    gp);
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate 	zone0.zone_nlwps = p0.p_lwpcnt;
14920Sstevel@tonic-gate 	zone0.zone_ntasks = 1;
14930Sstevel@tonic-gate 	mutex_exit(&p0.p_lock);
14942712Snn35248 	zone0.zone_restart_init = B_TRUE;
14952712Snn35248 	zone0.zone_brand = &native_brand;
14960Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
14970Sstevel@tonic-gate 	/*
14983247Sgjelinek 	 * pool_default hasn't been initialized yet, so we let pool_init()
14993247Sgjelinek 	 * take care of making sure the global zone is in the default pool.
15000Sstevel@tonic-gate 	 */
15011676Sjpk 
15021676Sjpk 	/*
15033247Sgjelinek 	 * Initialize global zone kstats
15043247Sgjelinek 	 */
15053247Sgjelinek 	zone_kstat_create(&zone0);
15063247Sgjelinek 
15073247Sgjelinek 	/*
15081676Sjpk 	 * Initialize zone label.
15091676Sjpk 	 * mlp are initialized when tnzonecfg is loaded.
15101676Sjpk 	 */
15111676Sjpk 	zone0.zone_slabel = l_admin_low;
15121676Sjpk 	rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
15131676Sjpk 	label_hold(l_admin_low);
15141676Sjpk 
15150Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
15160Sstevel@tonic-gate 	zone_uniqid(&zone0);
15170Sstevel@tonic-gate 	ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID);
15181676Sjpk 
15190Sstevel@tonic-gate 	zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size,
15200Sstevel@tonic-gate 	    mod_hash_null_valdtor);
15210Sstevel@tonic-gate 	zonehashbyname = mod_hash_create_strhash("zone_by_name",
15220Sstevel@tonic-gate 	    zone_hash_size, mod_hash_null_valdtor);
15231676Sjpk 	/*
15241676Sjpk 	 * maintain zonehashbylabel only for labeled systems
15251676Sjpk 	 */
15261676Sjpk 	if (is_system_labeled())
15271676Sjpk 		zonehashbylabel = mod_hash_create_extended("zone_by_label",
15281676Sjpk 		    zone_hash_size, mod_hash_null_keydtor,
15291676Sjpk 		    mod_hash_null_valdtor, hash_bylabel, NULL,
15301676Sjpk 		    hash_labelkey_cmp, KM_SLEEP);
15310Sstevel@tonic-gate 	zonecount = 1;
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID,
15340Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
15350Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name,
15360Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
15371769Scarlsonj 	if (is_system_labeled()) {
15381769Scarlsonj 		zone0.zone_flags |= ZF_HASHED_LABEL;
15391676Sjpk 		(void) mod_hash_insert(zonehashbylabel,
15401676Sjpk 		    (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0);
15411769Scarlsonj 	}
15421676Sjpk 	mutex_exit(&zonehash_lock);
15431676Sjpk 
15440Sstevel@tonic-gate 	/*
15450Sstevel@tonic-gate 	 * We avoid setting zone_kcred until now, since kcred is initialized
15460Sstevel@tonic-gate 	 * sometime after zone_zsd_init() and before zone_init().
15470Sstevel@tonic-gate 	 */
15480Sstevel@tonic-gate 	zone0.zone_kcred = kcred;
15490Sstevel@tonic-gate 	/*
15500Sstevel@tonic-gate 	 * The global zone is fully initialized (except for zone_rootvp which
15510Sstevel@tonic-gate 	 * will be set when the root filesystem is mounted).
15520Sstevel@tonic-gate 	 */
15530Sstevel@tonic-gate 	global_zone = &zone0;
15541166Sdstaff 
15551166Sdstaff 	/*
15561166Sdstaff 	 * Setup an event channel to send zone status change notifications on
15571166Sdstaff 	 */
15581166Sdstaff 	res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan,
15591166Sdstaff 	    EVCH_CREAT);
15601166Sdstaff 
15611166Sdstaff 	if (res)
15621166Sdstaff 		panic("Sysevent_evc_bind failed during zone setup.\n");
15633247Sgjelinek 
15640Sstevel@tonic-gate }
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate static void
15670Sstevel@tonic-gate zone_free(zone_t *zone)
15680Sstevel@tonic-gate {
15690Sstevel@tonic-gate 	ASSERT(zone != global_zone);
15700Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks == 0);
15710Sstevel@tonic-gate 	ASSERT(zone->zone_nlwps == 0);
15720Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 0);
15730Sstevel@tonic-gate 	ASSERT(zone->zone_kcred == NULL);
15740Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_DEAD ||
15750Sstevel@tonic-gate 	    zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
15760Sstevel@tonic-gate 
15773792Sakolb 	/*
15783792Sakolb 	 * Remove any zone caps.
15793792Sakolb 	 */
15803792Sakolb 	cpucaps_zone_remove(zone);
15813792Sakolb 
15823792Sakolb 	ASSERT(zone->zone_cpucap == NULL);
15833792Sakolb 
15840Sstevel@tonic-gate 	/* remove from deathrow list */
15850Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_DEAD) {
15860Sstevel@tonic-gate 		ASSERT(zone->zone_ref == 0);
15870Sstevel@tonic-gate 		mutex_enter(&zone_deathrow_lock);
15880Sstevel@tonic-gate 		list_remove(&zone_deathrow, zone);
15890Sstevel@tonic-gate 		mutex_exit(&zone_deathrow_lock);
15900Sstevel@tonic-gate 	}
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	zone_free_zsd(zone);
1593789Sahrens 	zone_free_datasets(zone);
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	if (zone->zone_rootvp != NULL)
15960Sstevel@tonic-gate 		VN_RELE(zone->zone_rootvp);
15970Sstevel@tonic-gate 	if (zone->zone_rootpath)
15980Sstevel@tonic-gate 		kmem_free(zone->zone_rootpath, zone->zone_rootpathlen);
15990Sstevel@tonic-gate 	if (zone->zone_name != NULL)
16000Sstevel@tonic-gate 		kmem_free(zone->zone_name, ZONENAME_MAX);
16011676Sjpk 	if (zone->zone_slabel != NULL)
16021676Sjpk 		label_rele(zone->zone_slabel);
16030Sstevel@tonic-gate 	if (zone->zone_nodename != NULL)
16040Sstevel@tonic-gate 		kmem_free(zone->zone_nodename, _SYS_NMLN);
16050Sstevel@tonic-gate 	if (zone->zone_domain != NULL)
16060Sstevel@tonic-gate 		kmem_free(zone->zone_domain, _SYS_NMLN);
16070Sstevel@tonic-gate 	if (zone->zone_privset != NULL)
16080Sstevel@tonic-gate 		kmem_free(zone->zone_privset, sizeof (priv_set_t));
16090Sstevel@tonic-gate 	if (zone->zone_rctls != NULL)
16100Sstevel@tonic-gate 		rctl_set_free(zone->zone_rctls);
16110Sstevel@tonic-gate 	if (zone->zone_bootargs != NULL)
16122267Sdp 		kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1);
16132267Sdp 	if (zone->zone_initname != NULL)
16142267Sdp 		kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1);
16150Sstevel@tonic-gate 	id_free(zoneid_space, zone->zone_id);
16160Sstevel@tonic-gate 	mutex_destroy(&zone->zone_lock);
16170Sstevel@tonic-gate 	cv_destroy(&zone->zone_cv);
16181676Sjpk 	rw_destroy(&zone->zone_mlps.mlpl_rwlock);
16190Sstevel@tonic-gate 	kmem_free(zone, sizeof (zone_t));
16200Sstevel@tonic-gate }
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate /*
16230Sstevel@tonic-gate  * See block comment at the top of this file for information about zone
16240Sstevel@tonic-gate  * status values.
16250Sstevel@tonic-gate  */
16260Sstevel@tonic-gate /*
16270Sstevel@tonic-gate  * Convenience function for setting zone status.
16280Sstevel@tonic-gate  */
16290Sstevel@tonic-gate static void
16300Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status)
16310Sstevel@tonic-gate {
16321166Sdstaff 
16331166Sdstaff 	nvlist_t *nvl = NULL;
16340Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zone_status_lock));
16350Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE &&
16360Sstevel@tonic-gate 	    status >= zone_status_get(zone));
16371166Sdstaff 
16381166Sdstaff 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) ||
16391166Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) ||
16401166Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_NEWSTATE,
16412267Sdp 	    zone_status_table[status]) ||
16421166Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_OLDSTATE,
16432267Sdp 	    zone_status_table[zone->zone_status]) ||
16441166Sdstaff 	    nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) ||
16451166Sdstaff 	    nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) ||
16461166Sdstaff 	    sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS,
16472267Sdp 	    ZONE_EVENT_STATUS_SUBCLASS, "sun.com", "kernel", nvl, EVCH_SLEEP)) {
16481166Sdstaff #ifdef DEBUG
16491166Sdstaff 		(void) printf(
16501166Sdstaff 		    "Failed to allocate and send zone state change event.\n");
16511166Sdstaff #endif
16521166Sdstaff 	}
16531166Sdstaff 	nvlist_free(nvl);
16541166Sdstaff 
16550Sstevel@tonic-gate 	zone->zone_status = status;
16561166Sdstaff 
16570Sstevel@tonic-gate 	cv_broadcast(&zone->zone_cv);
16580Sstevel@tonic-gate }
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate /*
16610Sstevel@tonic-gate  * Public function to retrieve the zone status.  The zone status may
16620Sstevel@tonic-gate  * change after it is retrieved.
16630Sstevel@tonic-gate  */
16640Sstevel@tonic-gate zone_status_t
16650Sstevel@tonic-gate zone_status_get(zone_t *zone)
16660Sstevel@tonic-gate {
16670Sstevel@tonic-gate 	return (zone->zone_status);
16680Sstevel@tonic-gate }
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate static int
16710Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs)
16720Sstevel@tonic-gate {
16732267Sdp 	char *bootargs = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP);
16742267Sdp 	int err = 0;
16752267Sdp 
16762267Sdp 	ASSERT(zone != global_zone);
16772267Sdp 	if ((err = copyinstr(zone_bootargs, bootargs, BOOTARGS_MAX, NULL)) != 0)
16782267Sdp 		goto done;	/* EFAULT or ENAMETOOLONG */
16792267Sdp 
16802267Sdp 	if (zone->zone_bootargs != NULL)
16812267Sdp 		kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1);
16822267Sdp 
16832267Sdp 	zone->zone_bootargs = kmem_alloc(strlen(bootargs) + 1, KM_SLEEP);
16842267Sdp 	(void) strcpy(zone->zone_bootargs, bootargs);
16852267Sdp 
16862267Sdp done:
16872267Sdp 	kmem_free(bootargs, BOOTARGS_MAX);
16882267Sdp 	return (err);
16892267Sdp }
16902267Sdp 
16912267Sdp static int
16924141Sedp zone_set_brand(zone_t *zone, const char *brand)
16934141Sedp {
16944141Sedp 	struct brand_attr *attrp;
16954141Sedp 	brand_t *bp;
16964141Sedp 
16974141Sedp 	attrp = kmem_alloc(sizeof (struct brand_attr), KM_SLEEP);
16984141Sedp 	if (copyin(brand, attrp, sizeof (struct brand_attr)) != 0) {
16994141Sedp 		kmem_free(attrp, sizeof (struct brand_attr));
17004141Sedp 		return (EFAULT);
17014141Sedp 	}
17024141Sedp 
17034141Sedp 	bp = brand_register_zone(attrp);
17044141Sedp 	kmem_free(attrp, sizeof (struct brand_attr));
17054141Sedp 	if (bp == NULL)
17064141Sedp 		return (EINVAL);
17074141Sedp 
17084141Sedp 	/*
17094141Sedp 	 * This is the only place where a zone can change it's brand.
17104141Sedp 	 * We already need to hold zone_status_lock to check the zone
17114141Sedp 	 * status, so we'll just use that lock to serialize zone
17124141Sedp 	 * branding requests as well.
17134141Sedp 	 */
17144141Sedp 	mutex_enter(&zone_status_lock);
17154141Sedp 
17164141Sedp 	/* Re-Branding is not allowed and the zone can't be booted yet */
17174141Sedp 	if ((ZONE_IS_BRANDED(zone)) ||
17184141Sedp 	    (zone_status_get(zone) >= ZONE_IS_BOOTING)) {
17194141Sedp 		mutex_exit(&zone_status_lock);
17204141Sedp 		brand_unregister_zone(bp);
17214141Sedp 		return (EINVAL);
17224141Sedp 	}
17234141Sedp 
17244141Sedp 	if (is_system_labeled() &&
17254141Sedp 	    strncmp(attrp->ba_brandname, NATIVE_BRAND_NAME, MAXNAMELEN) != 0) {
17264141Sedp 		mutex_exit(&zone_status_lock);
17274141Sedp 		brand_unregister_zone(bp);
17284141Sedp 		return (EPERM);
17294141Sedp 	}
17304141Sedp 
17314888Seh208807 	/* set up the brand specific data */
17324141Sedp 	zone->zone_brand = bp;
17334888Seh208807 	ZBROP(zone)->b_init_brand_data(zone);
17344888Seh208807 
17354141Sedp 	mutex_exit(&zone_status_lock);
17364141Sedp 	return (0);
17374141Sedp }
17384141Sedp 
17394141Sedp static int
17402267Sdp zone_set_initname(zone_t *zone, const char *zone_initname)
17412267Sdp {
17422267Sdp 	char initname[INITNAME_SZ];
17430Sstevel@tonic-gate 	size_t len;
17442267Sdp 	int err = 0;
17452267Sdp 
17462267Sdp 	ASSERT(zone != global_zone);
17472267Sdp 	if ((err = copyinstr(zone_initname, initname, INITNAME_SZ, &len)) != 0)
17480Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
17492267Sdp 
17502267Sdp 	if (zone->zone_initname != NULL)
17512267Sdp 		kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1);
17522267Sdp 
17532267Sdp 	zone->zone_initname = kmem_alloc(strlen(initname) + 1, KM_SLEEP);
17542267Sdp 	(void) strcpy(zone->zone_initname, initname);
17550Sstevel@tonic-gate 	return (0);
17560Sstevel@tonic-gate }
17570Sstevel@tonic-gate 
17583247Sgjelinek static int
17593247Sgjelinek zone_set_phys_mcap(zone_t *zone, const uint64_t *zone_mcap)
17603247Sgjelinek {
17613247Sgjelinek 	uint64_t mcap;
17623247Sgjelinek 	int err = 0;
17633247Sgjelinek 
17643247Sgjelinek 	if ((err = copyin(zone_mcap, &mcap, sizeof (uint64_t))) == 0)
17653247Sgjelinek 		zone->zone_phys_mcap = mcap;
17663247Sgjelinek 
17673247Sgjelinek 	return (err);
17683247Sgjelinek }
17693247Sgjelinek 
17703247Sgjelinek static int
17713247Sgjelinek zone_set_sched_class(zone_t *zone, const char *new_class)
17723247Sgjelinek {
17733247Sgjelinek 	char sched_class[PC_CLNMSZ];
17743247Sgjelinek 	id_t classid;
17753247Sgjelinek 	int err;
17763247Sgjelinek 
17773247Sgjelinek 	ASSERT(zone != global_zone);
17783247Sgjelinek 	if ((err = copyinstr(new_class, sched_class, PC_CLNMSZ, NULL)) != 0)
17793247Sgjelinek 		return (err);	/* EFAULT or ENAMETOOLONG */
17803247Sgjelinek 
17813247Sgjelinek 	if (getcid(sched_class, &classid) != 0 || classid == syscid)
17823247Sgjelinek 		return (set_errno(EINVAL));
17833247Sgjelinek 	zone->zone_defaultcid = classid;
17843247Sgjelinek 	ASSERT(zone->zone_defaultcid > 0 &&
17853247Sgjelinek 	    zone->zone_defaultcid < loaded_classes);
17863247Sgjelinek 
17873247Sgjelinek 	return (0);
17883247Sgjelinek }
17893247Sgjelinek 
17900Sstevel@tonic-gate /*
17910Sstevel@tonic-gate  * Block indefinitely waiting for (zone_status >= status)
17920Sstevel@tonic-gate  */
17930Sstevel@tonic-gate void
17940Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status)
17950Sstevel@tonic-gate {
17960Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
17990Sstevel@tonic-gate 	while (zone->zone_status < status) {
18000Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
18010Sstevel@tonic-gate 	}
18020Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
18030Sstevel@tonic-gate }
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate /*
18060Sstevel@tonic-gate  * Private CPR-safe version of zone_status_wait().
18070Sstevel@tonic-gate  */
18080Sstevel@tonic-gate static void
18090Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str)
18100Sstevel@tonic-gate {
18110Sstevel@tonic-gate 	callb_cpr_t cprinfo;
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr,
18160Sstevel@tonic-gate 	    str);
18170Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
18180Sstevel@tonic-gate 	while (zone->zone_status < status) {
18190Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
18200Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
18210Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock);
18220Sstevel@tonic-gate 	}
18230Sstevel@tonic-gate 	/*
18240Sstevel@tonic-gate 	 * zone_status_lock is implicitly released by the following.
18250Sstevel@tonic-gate 	 */
18260Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
18270Sstevel@tonic-gate }
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate /*
18300Sstevel@tonic-gate  * Block until zone enters requested state or signal is received.  Return (0)
18310Sstevel@tonic-gate  * if signaled, non-zero otherwise.
18320Sstevel@tonic-gate  */
18330Sstevel@tonic-gate int
18340Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status)
18350Sstevel@tonic-gate {
18360Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
18390Sstevel@tonic-gate 	while (zone->zone_status < status) {
18400Sstevel@tonic-gate 		if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) {
18410Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
18420Sstevel@tonic-gate 			return (0);
18430Sstevel@tonic-gate 		}
18440Sstevel@tonic-gate 	}
18450Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
18460Sstevel@tonic-gate 	return (1);
18470Sstevel@tonic-gate }
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate /*
18500Sstevel@tonic-gate  * Block until the zone enters the requested state or the timeout expires,
18510Sstevel@tonic-gate  * whichever happens first.  Return (-1) if operation timed out, time remaining
18520Sstevel@tonic-gate  * otherwise.
18530Sstevel@tonic-gate  */
18540Sstevel@tonic-gate clock_t
18550Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status)
18560Sstevel@tonic-gate {
18570Sstevel@tonic-gate 	clock_t timeleft = 0;
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
18620Sstevel@tonic-gate 	while (zone->zone_status < status && timeleft != -1) {
18630Sstevel@tonic-gate 		timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim);
18640Sstevel@tonic-gate 	}
18650Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
18660Sstevel@tonic-gate 	return (timeleft);
18670Sstevel@tonic-gate }
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate /*
18700Sstevel@tonic-gate  * Block until the zone enters the requested state, the current process is
18710Sstevel@tonic-gate  * signaled,  or the timeout expires, whichever happens first.  Return (-1) if
18720Sstevel@tonic-gate  * operation timed out, 0 if signaled, time remaining otherwise.
18730Sstevel@tonic-gate  */
18740Sstevel@tonic-gate clock_t
18750Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status)
18760Sstevel@tonic-gate {
18770Sstevel@tonic-gate 	clock_t timeleft = tim - lbolt;
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
18820Sstevel@tonic-gate 	while (zone->zone_status < status) {
18830Sstevel@tonic-gate 		timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock,
18840Sstevel@tonic-gate 		    tim);
18850Sstevel@tonic-gate 		if (timeleft <= 0)
18860Sstevel@tonic-gate 			break;
18870Sstevel@tonic-gate 	}
18880Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
18890Sstevel@tonic-gate 	return (timeleft);
18900Sstevel@tonic-gate }
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate /*
18930Sstevel@tonic-gate  * Zones have two reference counts: one for references from credential
18940Sstevel@tonic-gate  * structures (zone_cred_ref), and one (zone_ref) for everything else.
18950Sstevel@tonic-gate  * This is so we can allow a zone to be rebooted while there are still
18960Sstevel@tonic-gate  * outstanding cred references, since certain drivers cache dblks (which
18970Sstevel@tonic-gate  * implicitly results in cached creds).  We wait for zone_ref to drop to
18980Sstevel@tonic-gate  * 0 (actually 1), but not zone_cred_ref.  The zone structure itself is
18990Sstevel@tonic-gate  * later freed when the zone_cred_ref drops to 0, though nothing other
19000Sstevel@tonic-gate  * than the zone id and privilege set should be accessed once the zone
19010Sstevel@tonic-gate  * is "dead".
19020Sstevel@tonic-gate  *
19030Sstevel@tonic-gate  * A debugging flag, zone_wait_for_cred, can be set to a non-zero value
19040Sstevel@tonic-gate  * to force halt/reboot to block waiting for the zone_cred_ref to drop
19050Sstevel@tonic-gate  * to 0.  This can be useful to flush out other sources of cached creds
19060Sstevel@tonic-gate  * that may be less innocuous than the driver case.
19070Sstevel@tonic-gate  */
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate int zone_wait_for_cred = 0;
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate static void
19120Sstevel@tonic-gate zone_hold_locked(zone_t *z)
19130Sstevel@tonic-gate {
19140Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&z->zone_lock));
19150Sstevel@tonic-gate 	z->zone_ref++;
19160Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
19170Sstevel@tonic-gate }
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate void
19200Sstevel@tonic-gate zone_hold(zone_t *z)
19210Sstevel@tonic-gate {
19220Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
19230Sstevel@tonic-gate 	zone_hold_locked(z);
19240Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
19250Sstevel@tonic-gate }
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate /*
19280Sstevel@tonic-gate  * If the non-cred ref count drops to 1 and either the cred ref count
19290Sstevel@tonic-gate  * is 0 or we aren't waiting for cred references, the zone is ready to
19300Sstevel@tonic-gate  * be destroyed.
19310Sstevel@tonic-gate  */
19320Sstevel@tonic-gate #define	ZONE_IS_UNREF(zone)	((zone)->zone_ref == 1 && \
19330Sstevel@tonic-gate 	    (!zone_wait_for_cred || (zone)->zone_cred_ref == 0))
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate void
19360Sstevel@tonic-gate zone_rele(zone_t *z)
19370Sstevel@tonic-gate {
19380Sstevel@tonic-gate 	boolean_t wakeup;
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
19410Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
19420Sstevel@tonic-gate 	z->zone_ref--;
19430Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
19440Sstevel@tonic-gate 		/* no more refs, free the structure */
19450Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
19460Sstevel@tonic-gate 		zone_free(z);
19470Sstevel@tonic-gate 		return;
19480Sstevel@tonic-gate 	}
19490Sstevel@tonic-gate 	/* signal zone_destroy so the zone can finish halting */
19500Sstevel@tonic-gate 	wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD);
19510Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 	if (wakeup) {
19540Sstevel@tonic-gate 		/*
19550Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
19560Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
19570Sstevel@tonic-gate 		 */
19580Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
19590Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
19600Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
19610Sstevel@tonic-gate 	}
19620Sstevel@tonic-gate }
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate void
19650Sstevel@tonic-gate zone_cred_hold(zone_t *z)
19660Sstevel@tonic-gate {
19670Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
19680Sstevel@tonic-gate 	z->zone_cred_ref++;
19690Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
19700Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
19710Sstevel@tonic-gate }
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate void
19740Sstevel@tonic-gate zone_cred_rele(zone_t *z)
19750Sstevel@tonic-gate {
19760Sstevel@tonic-gate 	boolean_t wakeup;
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
19790Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
19800Sstevel@tonic-gate 	z->zone_cred_ref--;
19810Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
19820Sstevel@tonic-gate 		/* no more refs, free the structure */
19830Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
19840Sstevel@tonic-gate 		zone_free(z);
19850Sstevel@tonic-gate 		return;
19860Sstevel@tonic-gate 	}
19870Sstevel@tonic-gate 	/*
19880Sstevel@tonic-gate 	 * If zone_destroy is waiting for the cred references to drain
19890Sstevel@tonic-gate 	 * out, and they have, signal it.
19900Sstevel@tonic-gate 	 */
19910Sstevel@tonic-gate 	wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) &&
19920Sstevel@tonic-gate 	    zone_status_get(z) >= ZONE_IS_DEAD);
19930Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	if (wakeup) {
19960Sstevel@tonic-gate 		/*
19970Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
19980Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
19990Sstevel@tonic-gate 		 */
20000Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
20010Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
20020Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
20030Sstevel@tonic-gate 	}
20040Sstevel@tonic-gate }
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate void
20070Sstevel@tonic-gate zone_task_hold(zone_t *z)
20080Sstevel@tonic-gate {
20090Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
20100Sstevel@tonic-gate 	z->zone_ntasks++;
20110Sstevel@tonic-gate 	ASSERT(z->zone_ntasks != 0);
20120Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
20130Sstevel@tonic-gate }
20140Sstevel@tonic-gate 
20150Sstevel@tonic-gate void
20160Sstevel@tonic-gate zone_task_rele(zone_t *zone)
20170Sstevel@tonic-gate {
20180Sstevel@tonic-gate 	uint_t refcnt;
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
20210Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks != 0);
20220Sstevel@tonic-gate 	refcnt = --zone->zone_ntasks;
20230Sstevel@tonic-gate 	if (refcnt > 1)	{	/* Common case */
20240Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
20250Sstevel@tonic-gate 		return;
20260Sstevel@tonic-gate 	}
20270Sstevel@tonic-gate 	zone_hold_locked(zone);	/* so we can use the zone_t later */
20280Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
20290Sstevel@tonic-gate 	if (refcnt == 1) {
20300Sstevel@tonic-gate 		/*
20310Sstevel@tonic-gate 		 * See if the zone is shutting down.
20320Sstevel@tonic-gate 		 */
20330Sstevel@tonic-gate 		mutex_enter(&zone_status_lock);
20340Sstevel@tonic-gate 		if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) {
20350Sstevel@tonic-gate 			goto out;
20360Sstevel@tonic-gate 		}
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 		/*
20390Sstevel@tonic-gate 		 * Make sure the ntasks didn't change since we
20400Sstevel@tonic-gate 		 * dropped zone_lock.
20410Sstevel@tonic-gate 		 */
20420Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
20430Sstevel@tonic-gate 		if (refcnt != zone->zone_ntasks) {
20440Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
20450Sstevel@tonic-gate 			goto out;
20460Sstevel@tonic-gate 		}
20470Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 		/*
20500Sstevel@tonic-gate 		 * No more user processes in the zone.  The zone is empty.
20510Sstevel@tonic-gate 		 */
20520Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_EMPTY);
20530Sstevel@tonic-gate 		goto out;
20540Sstevel@tonic-gate 	}
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate 	ASSERT(refcnt == 0);
20570Sstevel@tonic-gate 	/*
20580Sstevel@tonic-gate 	 * zsched has exited; the zone is dead.
20590Sstevel@tonic-gate 	 */
20600Sstevel@tonic-gate 	zone->zone_zsched = NULL;		/* paranoia */
20610Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
20620Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_DEAD);
20630Sstevel@tonic-gate out:
20640Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
20650Sstevel@tonic-gate 	zone_rele(zone);
20660Sstevel@tonic-gate }
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate zoneid_t
20690Sstevel@tonic-gate getzoneid(void)
20700Sstevel@tonic-gate {
20710Sstevel@tonic-gate 	return (curproc->p_zone->zone_id);
20720Sstevel@tonic-gate }
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate /*
20750Sstevel@tonic-gate  * Internal versions of zone_find_by_*().  These don't zone_hold() or
20760Sstevel@tonic-gate  * check the validity of a zone's state.
20770Sstevel@tonic-gate  */
20780Sstevel@tonic-gate static zone_t *
20790Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid)
20800Sstevel@tonic-gate {
20810Sstevel@tonic-gate 	mod_hash_val_t hv;
20820Sstevel@tonic-gate 	zone_t *zone = NULL;
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyid,
20870Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0)
20880Sstevel@tonic-gate 		zone = (zone_t *)hv;
20890Sstevel@tonic-gate 	return (zone);
20900Sstevel@tonic-gate }
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate static zone_t *
20931676Sjpk zone_find_all_by_label(const ts_label_t *label)
20941676Sjpk {
20951676Sjpk 	mod_hash_val_t hv;
20961676Sjpk 	zone_t *zone = NULL;
20971676Sjpk 
20981676Sjpk 	ASSERT(MUTEX_HELD(&zonehash_lock));
20991676Sjpk 
21001676Sjpk 	/*
21011676Sjpk 	 * zonehashbylabel is not maintained for unlabeled systems
21021676Sjpk 	 */
21031676Sjpk 	if (!is_system_labeled())
21041676Sjpk 		return (NULL);
21051676Sjpk 	if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0)
21061676Sjpk 		zone = (zone_t *)hv;
21071676Sjpk 	return (zone);
21081676Sjpk }
21091676Sjpk 
21101676Sjpk static zone_t *
21110Sstevel@tonic-gate zone_find_all_by_name(char *name)
21120Sstevel@tonic-gate {
21130Sstevel@tonic-gate 	mod_hash_val_t hv;
21140Sstevel@tonic-gate 	zone_t *zone = NULL;
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0)
21190Sstevel@tonic-gate 		zone = (zone_t *)hv;
21200Sstevel@tonic-gate 	return (zone);
21210Sstevel@tonic-gate }
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate /*
21240Sstevel@tonic-gate  * Public interface for looking up a zone by zoneid.  Only returns the zone if
21250Sstevel@tonic-gate  * it is fully initialized, and has not yet begun the zone_destroy() sequence.
21260Sstevel@tonic-gate  * Caller must call zone_rele() once it is done with the zone.
21270Sstevel@tonic-gate  *
21280Sstevel@tonic-gate  * The zone may begin the zone_destroy() sequence immediately after this
21290Sstevel@tonic-gate  * function returns, but may be safely used until zone_rele() is called.
21300Sstevel@tonic-gate  */
21310Sstevel@tonic-gate zone_t *
21320Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid)
21330Sstevel@tonic-gate {
21340Sstevel@tonic-gate 	zone_t *zone;
21350Sstevel@tonic-gate 	zone_status_t status;
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
21380Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
21390Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
21400Sstevel@tonic-gate 		return (NULL);
21410Sstevel@tonic-gate 	}
21420Sstevel@tonic-gate 	status = zone_status_get(zone);
21430Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
21440Sstevel@tonic-gate 		/*
21450Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
21460Sstevel@tonic-gate 		 */
21470Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
21480Sstevel@tonic-gate 		return (NULL);
21490Sstevel@tonic-gate 	}
21500Sstevel@tonic-gate 	zone_hold(zone);
21510Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
21520Sstevel@tonic-gate 	return (zone);
21530Sstevel@tonic-gate }
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate /*
21561676Sjpk  * Similar to zone_find_by_id, but using zone label as the key.
21571676Sjpk  */
21581676Sjpk zone_t *
21591676Sjpk zone_find_by_label(const ts_label_t *label)
21601676Sjpk {
21611676Sjpk 	zone_t *zone;
21622110Srica 	zone_status_t status;
21631676Sjpk 
21641676Sjpk 	mutex_enter(&zonehash_lock);
21651676Sjpk 	if ((zone = zone_find_all_by_label(label)) == NULL) {
21661676Sjpk 		mutex_exit(&zonehash_lock);
21671676Sjpk 		return (NULL);
21681676Sjpk 	}
21692110Srica 
21702110Srica 	status = zone_status_get(zone);
21712110Srica 	if (status > ZONE_IS_DOWN) {
21721676Sjpk 		/*
21731676Sjpk 		 * For all practical purposes the zone doesn't exist.
21741676Sjpk 		 */
21752110Srica 		mutex_exit(&zonehash_lock);
21762110Srica 		return (NULL);
21771676Sjpk 	}
21782110Srica 	zone_hold(zone);
21791676Sjpk 	mutex_exit(&zonehash_lock);
21801676Sjpk 	return (zone);
21811676Sjpk }
21821676Sjpk 
21831676Sjpk /*
21840Sstevel@tonic-gate  * Similar to zone_find_by_id, but using zone name as the key.
21850Sstevel@tonic-gate  */
21860Sstevel@tonic-gate zone_t *
21870Sstevel@tonic-gate zone_find_by_name(char *name)
21880Sstevel@tonic-gate {
21890Sstevel@tonic-gate 	zone_t *zone;
21900Sstevel@tonic-gate 	zone_status_t status;
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
21930Sstevel@tonic-gate 	if ((zone = zone_find_all_by_name(name)) == NULL) {
21940Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
21950Sstevel@tonic-gate 		return (NULL);
21960Sstevel@tonic-gate 	}
21970Sstevel@tonic-gate 	status = zone_status_get(zone);
21980Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
21990Sstevel@tonic-gate 		/*
22000Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
22010Sstevel@tonic-gate 		 */
22020Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
22030Sstevel@tonic-gate 		return (NULL);
22040Sstevel@tonic-gate 	}
22050Sstevel@tonic-gate 	zone_hold(zone);
22060Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
22070Sstevel@tonic-gate 	return (zone);
22080Sstevel@tonic-gate }
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate /*
22110Sstevel@tonic-gate  * Similar to zone_find_by_id(), using the path as a key.  For instance,
22120Sstevel@tonic-gate  * if there is a zone "foo" rooted at /foo/root, and the path argument
22130Sstevel@tonic-gate  * is "/foo/root/proc", it will return the held zone_t corresponding to
22140Sstevel@tonic-gate  * zone "foo".
22150Sstevel@tonic-gate  *
22160Sstevel@tonic-gate  * zone_find_by_path() always returns a non-NULL value, since at the
22170Sstevel@tonic-gate  * very least every path will be contained in the global zone.
22180Sstevel@tonic-gate  *
22190Sstevel@tonic-gate  * As with the other zone_find_by_*() functions, the caller is
22200Sstevel@tonic-gate  * responsible for zone_rele()ing the return value of this function.
22210Sstevel@tonic-gate  */
22220Sstevel@tonic-gate zone_t *
22230Sstevel@tonic-gate zone_find_by_path(const char *path)
22240Sstevel@tonic-gate {
22250Sstevel@tonic-gate 	zone_t *zone;
22260Sstevel@tonic-gate 	zone_t *zret = NULL;
22270Sstevel@tonic-gate 	zone_status_t status;
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate 	if (path == NULL) {
22300Sstevel@tonic-gate 		/*
22310Sstevel@tonic-gate 		 * Call from rootconf().
22320Sstevel@tonic-gate 		 */
22330Sstevel@tonic-gate 		zone_hold(global_zone);
22340Sstevel@tonic-gate 		return (global_zone);
22350Sstevel@tonic-gate 	}
22360Sstevel@tonic-gate 	ASSERT(*path == '/');
22370Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
22380Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
22390Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
22400Sstevel@tonic-gate 		if (ZONE_PATH_VISIBLE(path, zone))
22410Sstevel@tonic-gate 			zret = zone;
22420Sstevel@tonic-gate 	}
22430Sstevel@tonic-gate 	ASSERT(zret != NULL);
22440Sstevel@tonic-gate 	status = zone_status_get(zret);
22450Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
22460Sstevel@tonic-gate 		/*
22470Sstevel@tonic-gate 		 * Zone practically doesn't exist.
22480Sstevel@tonic-gate 		 */
22490Sstevel@tonic-gate 		zret = global_zone;
22500Sstevel@tonic-gate 	}
22510Sstevel@tonic-gate 	zone_hold(zret);
22520Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
22530Sstevel@tonic-gate 	return (zret);
22540Sstevel@tonic-gate }
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate /*
22570Sstevel@tonic-gate  * Get the number of cpus visible to this zone.  The system-wide global
22580Sstevel@tonic-gate  * 'ncpus' is returned if pools are disabled, the caller is in the
22590Sstevel@tonic-gate  * global zone, or a NULL zone argument is passed in.
22600Sstevel@tonic-gate  */
22610Sstevel@tonic-gate int
22620Sstevel@tonic-gate zone_ncpus_get(zone_t *zone)
22630Sstevel@tonic-gate {
22640Sstevel@tonic-gate 	int myncpus = zone == NULL ? 0 : zone->zone_ncpus;
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate 	return (myncpus != 0 ? myncpus : ncpus);
22670Sstevel@tonic-gate }
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate /*
22700Sstevel@tonic-gate  * Get the number of online cpus visible to this zone.  The system-wide
22710Sstevel@tonic-gate  * global 'ncpus_online' is returned if pools are disabled, the caller
22720Sstevel@tonic-gate  * is in the global zone, or a NULL zone argument is passed in.
22730Sstevel@tonic-gate  */
22740Sstevel@tonic-gate int
22750Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone)
22760Sstevel@tonic-gate {
22770Sstevel@tonic-gate 	int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online;
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate 	return (myncpus_online != 0 ? myncpus_online : ncpus_online);
22800Sstevel@tonic-gate }
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate /*
22830Sstevel@tonic-gate  * Return the pool to which the zone is currently bound.
22840Sstevel@tonic-gate  */
22850Sstevel@tonic-gate pool_t *
22860Sstevel@tonic-gate zone_pool_get(zone_t *zone)
22870Sstevel@tonic-gate {
22880Sstevel@tonic-gate 	ASSERT(pool_lock_held());
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 	return (zone->zone_pool);
22910Sstevel@tonic-gate }
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate /*
22940Sstevel@tonic-gate  * Set the zone's pool pointer and update the zone's visibility to match
22950Sstevel@tonic-gate  * the resources in the new pool.
22960Sstevel@tonic-gate  */
22970Sstevel@tonic-gate void
22980Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool)
22990Sstevel@tonic-gate {
23000Sstevel@tonic-gate 	ASSERT(pool_lock_held());
23010Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
23020Sstevel@tonic-gate 
23030Sstevel@tonic-gate 	zone->zone_pool = pool;
23040Sstevel@tonic-gate 	zone_pset_set(zone, pool->pool_pset->pset_id);
23050Sstevel@tonic-gate }
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate /*
23080Sstevel@tonic-gate  * Return the cached value of the id of the processor set to which the
23090Sstevel@tonic-gate  * zone is currently bound.  The value will be ZONE_PS_INVAL if the pools
23100Sstevel@tonic-gate  * facility is disabled.
23110Sstevel@tonic-gate  */
23120Sstevel@tonic-gate psetid_t
23130Sstevel@tonic-gate zone_pset_get(zone_t *zone)
23140Sstevel@tonic-gate {
23150Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
23160Sstevel@tonic-gate 
23170Sstevel@tonic-gate 	return (zone->zone_psetid);
23180Sstevel@tonic-gate }
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate /*
23210Sstevel@tonic-gate  * Set the cached value of the id of the processor set to which the zone
23220Sstevel@tonic-gate  * is currently bound.  Also update the zone's visibility to match the
23230Sstevel@tonic-gate  * resources in the new processor set.
23240Sstevel@tonic-gate  */
23250Sstevel@tonic-gate void
23260Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid)
23270Sstevel@tonic-gate {
23280Sstevel@tonic-gate 	psetid_t oldpsetid;
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
23310Sstevel@tonic-gate 	oldpsetid = zone_pset_get(zone);
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 	if (oldpsetid == newpsetid)
23340Sstevel@tonic-gate 		return;
23350Sstevel@tonic-gate 	/*
23360Sstevel@tonic-gate 	 * Global zone sees all.
23370Sstevel@tonic-gate 	 */
23380Sstevel@tonic-gate 	if (zone != global_zone) {
23390Sstevel@tonic-gate 		zone->zone_psetid = newpsetid;
23400Sstevel@tonic-gate 		if (newpsetid != ZONE_PS_INVAL)
23410Sstevel@tonic-gate 			pool_pset_visibility_add(newpsetid, zone);
23420Sstevel@tonic-gate 		if (oldpsetid != ZONE_PS_INVAL)
23430Sstevel@tonic-gate 			pool_pset_visibility_remove(oldpsetid, zone);
23440Sstevel@tonic-gate 	}
23450Sstevel@tonic-gate 	/*
23460Sstevel@tonic-gate 	 * Disabling pools, so we should start using the global values
23470Sstevel@tonic-gate 	 * for ncpus and ncpus_online.
23480Sstevel@tonic-gate 	 */
23490Sstevel@tonic-gate 	if (newpsetid == ZONE_PS_INVAL) {
23500Sstevel@tonic-gate 		zone->zone_ncpus = 0;
23510Sstevel@tonic-gate 		zone->zone_ncpus_online = 0;
23520Sstevel@tonic-gate 	}
23530Sstevel@tonic-gate }
23540Sstevel@tonic-gate 
23550Sstevel@tonic-gate /*
23560Sstevel@tonic-gate  * Walk the list of active zones and issue the provided callback for
23570Sstevel@tonic-gate  * each of them.
23580Sstevel@tonic-gate  *
23590Sstevel@tonic-gate  * Caller must not be holding any locks that may be acquired under
23600Sstevel@tonic-gate  * zonehash_lock.  See comment at the beginning of the file for a list of
23610Sstevel@tonic-gate  * common locks and their interactions with zones.
23620Sstevel@tonic-gate  */
23630Sstevel@tonic-gate int
23640Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data)
23650Sstevel@tonic-gate {
23660Sstevel@tonic-gate 	zone_t *zone;
23670Sstevel@tonic-gate 	int ret = 0;
23680Sstevel@tonic-gate 	zone_status_t status;
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
23710Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
23720Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
23730Sstevel@tonic-gate 		/*
23740Sstevel@tonic-gate 		 * Skip zones that shouldn't be externally visible.
23750Sstevel@tonic-gate 		 */
23760Sstevel@tonic-gate 		status = zone_status_get(zone);
23770Sstevel@tonic-gate 		if (status < ZONE_IS_READY || status > ZONE_IS_DOWN)
23780Sstevel@tonic-gate 			continue;
23790Sstevel@tonic-gate 		/*
23800Sstevel@tonic-gate 		 * Bail immediately if any callback invocation returns a
23810Sstevel@tonic-gate 		 * non-zero value.
23820Sstevel@tonic-gate 		 */
23830Sstevel@tonic-gate 		ret = (*cb)(zone, data);
23840Sstevel@tonic-gate 		if (ret != 0)
23850Sstevel@tonic-gate 			break;
23860Sstevel@tonic-gate 	}
23870Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
23880Sstevel@tonic-gate 	return (ret);
23890Sstevel@tonic-gate }
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate static int
23920Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath)
23930Sstevel@tonic-gate {
23940Sstevel@tonic-gate 	vnode_t *vp;
23950Sstevel@tonic-gate 	int trycount;
23960Sstevel@tonic-gate 	int error = 0;
23970Sstevel@tonic-gate 	char *path;
23980Sstevel@tonic-gate 	struct pathname upn, pn;
23990Sstevel@tonic-gate 	size_t pathlen;
24000Sstevel@tonic-gate 
24010Sstevel@tonic-gate 	if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0)
24020Sstevel@tonic-gate 		return (error);
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate 	pn_alloc(&pn);
24050Sstevel@tonic-gate 
24060Sstevel@tonic-gate 	/* prevent infinite loop */
24070Sstevel@tonic-gate 	trycount = 10;
24080Sstevel@tonic-gate 	for (;;) {
24090Sstevel@tonic-gate 		if (--trycount <= 0) {
24100Sstevel@tonic-gate 			error = ESTALE;
24110Sstevel@tonic-gate 			goto out;
24120Sstevel@tonic-gate 		}
24130Sstevel@tonic-gate 
24140Sstevel@tonic-gate 		if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) {
24150Sstevel@tonic-gate 			/*
24160Sstevel@tonic-gate 			 * VOP_ACCESS() may cover 'vp' with a new
24170Sstevel@tonic-gate 			 * filesystem, if 'vp' is an autoFS vnode.
24180Sstevel@tonic-gate 			 * Get the new 'vp' if so.
24190Sstevel@tonic-gate 			 */
2420*5331Samw 			if ((error =
2421*5331Samw 			    VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL)) == 0 &&
24224417Seh208807 			    (!vn_ismntpt(vp) ||
24230Sstevel@tonic-gate 			    (error = traverse(&vp)) == 0)) {
24240Sstevel@tonic-gate 				pathlen = pn.pn_pathlen + 2;
24250Sstevel@tonic-gate 				path = kmem_alloc(pathlen, KM_SLEEP);
24260Sstevel@tonic-gate 				(void) strncpy(path, pn.pn_path,
24270Sstevel@tonic-gate 				    pn.pn_pathlen + 1);
24280Sstevel@tonic-gate 				path[pathlen - 2] = '/';
24290Sstevel@tonic-gate 				path[pathlen - 1] = '\0';
24300Sstevel@tonic-gate 				pn_free(&pn);
24310Sstevel@tonic-gate 				pn_free(&upn);
24320Sstevel@tonic-gate 
24330Sstevel@tonic-gate 				/* Success! */
24340Sstevel@tonic-gate 				break;
24350Sstevel@tonic-gate 			}
24360Sstevel@tonic-gate 			VN_RELE(vp);
24370Sstevel@tonic-gate 		}
24380Sstevel@tonic-gate 		if (error != ESTALE)
24390Sstevel@tonic-gate 			goto out;
24400Sstevel@tonic-gate 	}
24410Sstevel@tonic-gate 
24420Sstevel@tonic-gate 	ASSERT(error == 0);
24430Sstevel@tonic-gate 	zone->zone_rootvp = vp;		/* we hold a reference to vp */
24440Sstevel@tonic-gate 	zone->zone_rootpath = path;
24450Sstevel@tonic-gate 	zone->zone_rootpathlen = pathlen;
24461769Scarlsonj 	if (pathlen > 5 && strcmp(path + pathlen - 5, "/lu/") == 0)
24471769Scarlsonj 		zone->zone_flags |= ZF_IS_SCRATCH;
24480Sstevel@tonic-gate 	return (0);
24490Sstevel@tonic-gate 
24500Sstevel@tonic-gate out:
24510Sstevel@tonic-gate 	pn_free(&pn);
24520Sstevel@tonic-gate 	pn_free(&upn);
24530Sstevel@tonic-gate 	return (error);
24540Sstevel@tonic-gate }
24550Sstevel@tonic-gate 
24560Sstevel@tonic-gate #define	isalnum(c)	(((c) >= '0' && (c) <= '9') || \
24570Sstevel@tonic-gate 			((c) >= 'a' && (c) <= 'z') || \
24580Sstevel@tonic-gate 			((c) >= 'A' && (c) <= 'Z'))
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate static int
24610Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname)
24620Sstevel@tonic-gate {
24630Sstevel@tonic-gate 	char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
24640Sstevel@tonic-gate 	size_t len;
24650Sstevel@tonic-gate 	int i, err;
24660Sstevel@tonic-gate 
24670Sstevel@tonic-gate 	if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) {
24680Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
24690Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
24700Sstevel@tonic-gate 	}
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate 	/* must be less than ZONENAME_MAX */
24730Sstevel@tonic-gate 	if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') {
24740Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
24750Sstevel@tonic-gate 		return (EINVAL);
24760Sstevel@tonic-gate 	}
24770Sstevel@tonic-gate 
24780Sstevel@tonic-gate 	/*
24790Sstevel@tonic-gate 	 * Name must start with an alphanumeric and must contain only
24800Sstevel@tonic-gate 	 * alphanumerics, '-', '_' and '.'.
24810Sstevel@tonic-gate 	 */
24820Sstevel@tonic-gate 	if (!isalnum(kname[0])) {
24830Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
24840Sstevel@tonic-gate 		return (EINVAL);
24850Sstevel@tonic-gate 	}
24860Sstevel@tonic-gate 	for (i = 1; i < len - 1; i++) {
24870Sstevel@tonic-gate 		if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' &&
24880Sstevel@tonic-gate 		    kname[i] != '.') {
24890Sstevel@tonic-gate 			kmem_free(kname, ZONENAME_MAX);
24900Sstevel@tonic-gate 			return (EINVAL);
24910Sstevel@tonic-gate 		}
24920Sstevel@tonic-gate 	}
24930Sstevel@tonic-gate 
24940Sstevel@tonic-gate 	zone->zone_name = kname;
24950Sstevel@tonic-gate 	return (0);
24960Sstevel@tonic-gate }
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate /*
24990Sstevel@tonic-gate  * Similar to thread_create(), but makes sure the thread is in the appropriate
25000Sstevel@tonic-gate  * zone's zsched process (curproc->p_zone->zone_zsched) before returning.
25010Sstevel@tonic-gate  */
25020Sstevel@tonic-gate /*ARGSUSED*/
25030Sstevel@tonic-gate kthread_t *
25040Sstevel@tonic-gate zthread_create(
25050Sstevel@tonic-gate     caddr_t stk,
25060Sstevel@tonic-gate     size_t stksize,
25070Sstevel@tonic-gate     void (*proc)(),
25080Sstevel@tonic-gate     void *arg,
25090Sstevel@tonic-gate     size_t len,
25100Sstevel@tonic-gate     pri_t pri)
25110Sstevel@tonic-gate {
25120Sstevel@tonic-gate 	kthread_t *t;
25130Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
25140Sstevel@tonic-gate 	proc_t *pp = zone->zone_zsched;
25150Sstevel@tonic-gate 
25160Sstevel@tonic-gate 	zone_hold(zone);	/* Reference to be dropped when thread exits */
25170Sstevel@tonic-gate 
25180Sstevel@tonic-gate 	/*
25190Sstevel@tonic-gate 	 * No-one should be trying to create threads if the zone is shutting
25200Sstevel@tonic-gate 	 * down and there aren't any kernel threads around.  See comment
25210Sstevel@tonic-gate 	 * in zthread_exit().
25220Sstevel@tonic-gate 	 */
25230Sstevel@tonic-gate 	ASSERT(!(zone->zone_kthreads == NULL &&
25240Sstevel@tonic-gate 	    zone_status_get(zone) >= ZONE_IS_EMPTY));
25250Sstevel@tonic-gate 	/*
25260Sstevel@tonic-gate 	 * Create a thread, but don't let it run until we've finished setting
25270Sstevel@tonic-gate 	 * things up.
25280Sstevel@tonic-gate 	 */
25290Sstevel@tonic-gate 	t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri);
25300Sstevel@tonic-gate 	ASSERT(t->t_forw == NULL);
25310Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
25320Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL) {
25330Sstevel@tonic-gate 		t->t_forw = t->t_back = t;
25340Sstevel@tonic-gate 	} else {
25350Sstevel@tonic-gate 		kthread_t *tx = zone->zone_kthreads;
25360Sstevel@tonic-gate 
25370Sstevel@tonic-gate 		t->t_forw = tx;
25380Sstevel@tonic-gate 		t->t_back = tx->t_back;
25390Sstevel@tonic-gate 		tx->t_back->t_forw = t;
25400Sstevel@tonic-gate 		tx->t_back = t;
25410Sstevel@tonic-gate 	}
25420Sstevel@tonic-gate 	zone->zone_kthreads = t;
25430Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
25460Sstevel@tonic-gate 	t->t_proc_flag |= TP_ZTHREAD;
25470Sstevel@tonic-gate 	project_rele(t->t_proj);
25480Sstevel@tonic-gate 	t->t_proj = project_hold(pp->p_task->tk_proj);
25490Sstevel@tonic-gate 
25500Sstevel@tonic-gate 	/*
25510Sstevel@tonic-gate 	 * Setup complete, let it run.
25520Sstevel@tonic-gate 	 */
25530Sstevel@tonic-gate 	thread_lock(t);
25540Sstevel@tonic-gate 	t->t_schedflag |= TS_ALLSTART;
25550Sstevel@tonic-gate 	setrun_locked(t);
25560Sstevel@tonic-gate 	thread_unlock(t);
25570Sstevel@tonic-gate 
25580Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
25590Sstevel@tonic-gate 
25600Sstevel@tonic-gate 	return (t);
25610Sstevel@tonic-gate }
25620Sstevel@tonic-gate 
25630Sstevel@tonic-gate /*
25640Sstevel@tonic-gate  * Similar to thread_exit().  Must be called by threads created via
25650Sstevel@tonic-gate  * zthread_exit().
25660Sstevel@tonic-gate  */
25670Sstevel@tonic-gate void
25680Sstevel@tonic-gate zthread_exit(void)
25690Sstevel@tonic-gate {
25700Sstevel@tonic-gate 	kthread_t *t = curthread;
25710Sstevel@tonic-gate 	proc_t *pp = curproc;
25720Sstevel@tonic-gate 	zone_t *zone = pp->p_zone;
25730Sstevel@tonic-gate 
25740Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
25750Sstevel@tonic-gate 
25760Sstevel@tonic-gate 	/*
25770Sstevel@tonic-gate 	 * Reparent to p0
25780Sstevel@tonic-gate 	 */
25791075Sjosephb 	kpreempt_disable();
25800Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
25810Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_ZTHREAD;
25820Sstevel@tonic-gate 	t->t_procp = &p0;
25830Sstevel@tonic-gate 	hat_thread_exit(t);
25840Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
25851075Sjosephb 	kpreempt_enable();
25860Sstevel@tonic-gate 
25870Sstevel@tonic-gate 	if (t->t_back == t) {
25880Sstevel@tonic-gate 		ASSERT(t->t_forw == t);
25890Sstevel@tonic-gate 		/*
25900Sstevel@tonic-gate 		 * If the zone is empty, once the thread count
25910Sstevel@tonic-gate 		 * goes to zero no further kernel threads can be
25920Sstevel@tonic-gate 		 * created.  This is because if the creator is a process
25930Sstevel@tonic-gate 		 * in the zone, then it must have exited before the zone
25940Sstevel@tonic-gate 		 * state could be set to ZONE_IS_EMPTY.
25950Sstevel@tonic-gate 		 * Otherwise, if the creator is a kernel thread in the
25960Sstevel@tonic-gate 		 * zone, the thread count is non-zero.
25970Sstevel@tonic-gate 		 *
25980Sstevel@tonic-gate 		 * This really means that non-zone kernel threads should
25990Sstevel@tonic-gate 		 * not create zone kernel threads.
26000Sstevel@tonic-gate 		 */
26010Sstevel@tonic-gate 		zone->zone_kthreads = NULL;
26020Sstevel@tonic-gate 		if (zone_status_get(zone) == ZONE_IS_EMPTY) {
26030Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_DOWN);
26043792Sakolb 			/*
26053792Sakolb 			 * Remove any CPU caps on this zone.
26063792Sakolb 			 */
26073792Sakolb 			cpucaps_zone_remove(zone);
26080Sstevel@tonic-gate 		}
26090Sstevel@tonic-gate 	} else {
26100Sstevel@tonic-gate 		t->t_forw->t_back = t->t_back;
26110Sstevel@tonic-gate 		t->t_back->t_forw = t->t_forw;
26120Sstevel@tonic-gate 		if (zone->zone_kthreads == t)
26130Sstevel@tonic-gate 			zone->zone_kthreads = t->t_forw;
26140Sstevel@tonic-gate 	}
26150Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
26160Sstevel@tonic-gate 	zone_rele(zone);
26170Sstevel@tonic-gate 	thread_exit();
26180Sstevel@tonic-gate 	/* NOTREACHED */
26190Sstevel@tonic-gate }
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate static void
26220Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp)
26230Sstevel@tonic-gate {
26240Sstevel@tonic-gate 	vnode_t *oldvp;
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate 	/* we're going to hold a reference here to the directory */
26270Sstevel@tonic-gate 	VN_HOLD(vp);
26280Sstevel@tonic-gate 
26290Sstevel@tonic-gate #ifdef C2_AUDIT
26300Sstevel@tonic-gate 	if (audit_active)	/* update abs cwd/root path see c2audit.c */
26310Sstevel@tonic-gate 		audit_chdirec(vp, vpp);
26320Sstevel@tonic-gate #endif
26330Sstevel@tonic-gate 
26340Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
26350Sstevel@tonic-gate 	oldvp = *vpp;
26360Sstevel@tonic-gate 	*vpp = vp;
26370Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
26380Sstevel@tonic-gate 	if (oldvp != NULL)
26390Sstevel@tonic-gate 		VN_RELE(oldvp);
26400Sstevel@tonic-gate }
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate /*
26430Sstevel@tonic-gate  * Convert an rctl value represented by an nvlist_t into an rctl_val_t.
26440Sstevel@tonic-gate  */
26450Sstevel@tonic-gate static int
26460Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv)
26470Sstevel@tonic-gate {
26480Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
26490Sstevel@tonic-gate 	boolean_t priv_set = B_FALSE;
26500Sstevel@tonic-gate 	boolean_t limit_set = B_FALSE;
26510Sstevel@tonic-gate 	boolean_t action_set = B_FALSE;
26520Sstevel@tonic-gate 
26530Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
26540Sstevel@tonic-gate 		const char *name;
26550Sstevel@tonic-gate 		uint64_t ui64;
26560Sstevel@tonic-gate 
26570Sstevel@tonic-gate 		name = nvpair_name(nvp);
26580Sstevel@tonic-gate 		if (nvpair_type(nvp) != DATA_TYPE_UINT64)
26590Sstevel@tonic-gate 			return (EINVAL);
26600Sstevel@tonic-gate 		(void) nvpair_value_uint64(nvp, &ui64);
26610Sstevel@tonic-gate 		if (strcmp(name, "privilege") == 0) {
26620Sstevel@tonic-gate 			/*
26630Sstevel@tonic-gate 			 * Currently only privileged values are allowed, but
26640Sstevel@tonic-gate 			 * this may change in the future.
26650Sstevel@tonic-gate 			 */
26660Sstevel@tonic-gate 			if (ui64 != RCPRIV_PRIVILEGED)
26670Sstevel@tonic-gate 				return (EINVAL);
26680Sstevel@tonic-gate 			rv->rcv_privilege = ui64;
26690Sstevel@tonic-gate 			priv_set = B_TRUE;
26700Sstevel@tonic-gate 		} else if (strcmp(name, "limit") == 0) {
26710Sstevel@tonic-gate 			rv->rcv_value = ui64;
26720Sstevel@tonic-gate 			limit_set = B_TRUE;
26730Sstevel@tonic-gate 		} else if (strcmp(name, "action") == 0) {
26740Sstevel@tonic-gate 			if (ui64 != RCTL_LOCAL_NOACTION &&
26750Sstevel@tonic-gate 			    ui64 != RCTL_LOCAL_DENY)
26760Sstevel@tonic-gate 				return (EINVAL);
26770Sstevel@tonic-gate 			rv->rcv_flagaction = ui64;
26780Sstevel@tonic-gate 			action_set = B_TRUE;
26790Sstevel@tonic-gate 		} else {
26800Sstevel@tonic-gate 			return (EINVAL);
26810Sstevel@tonic-gate 		}
26820Sstevel@tonic-gate 	}
26830Sstevel@tonic-gate 
26840Sstevel@tonic-gate 	if (!(priv_set && limit_set && action_set))
26850Sstevel@tonic-gate 		return (EINVAL);
26860Sstevel@tonic-gate 	rv->rcv_action_signal = 0;
26870Sstevel@tonic-gate 	rv->rcv_action_recipient = NULL;
26880Sstevel@tonic-gate 	rv->rcv_action_recip_pid = -1;
26890Sstevel@tonic-gate 	rv->rcv_firing_time = 0;
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate 	return (0);
26920Sstevel@tonic-gate }
26930Sstevel@tonic-gate 
26942267Sdp /*
26952267Sdp  * Non-global zone version of start_init.
26962267Sdp  */
26970Sstevel@tonic-gate void
26982267Sdp zone_start_init(void)
26990Sstevel@tonic-gate {
27000Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
27012712Snn35248 	zone_t *z = p->p_zone;
27022267Sdp 
27032267Sdp 	ASSERT(!INGLOBALZONE(curproc));
27040Sstevel@tonic-gate 
27050Sstevel@tonic-gate 	/*
27062712Snn35248 	 * For all purposes (ZONE_ATTR_INITPID and restart_init),
27072712Snn35248 	 * storing just the pid of init is sufficient.
27082712Snn35248 	 */
27092712Snn35248 	z->zone_proc_initpid = p->p_pid;
27102712Snn35248 
27112712Snn35248 	/*
27122267Sdp 	 * We maintain zone_boot_err so that we can return the cause of the
27132267Sdp 	 * failure back to the caller of the zone_boot syscall.
27140Sstevel@tonic-gate 	 */
27152267Sdp 	p->p_zone->zone_boot_err = start_init_common();
27160Sstevel@tonic-gate 
27170Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
27182712Snn35248 	if (z->zone_boot_err != 0) {
27190Sstevel@tonic-gate 		/*
27200Sstevel@tonic-gate 		 * Make sure we are still in the booting state-- we could have
27210Sstevel@tonic-gate 		 * raced and already be shutting down, or even further along.
27220Sstevel@tonic-gate 		 */
27233792Sakolb 		if (zone_status_get(z) == ZONE_IS_BOOTING) {
27242712Snn35248 			zone_status_set(z, ZONE_IS_SHUTTING_DOWN);
27253792Sakolb 		}
27260Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
27270Sstevel@tonic-gate 		/* It's gone bad, dispose of the process */
27282712Snn35248 		if (proc_exit(CLD_EXITED, z->zone_boot_err) != 0) {
2729390Sraf 			mutex_enter(&p->p_lock);
2730390Sraf 			ASSERT(p->p_flag & SEXITLWPS);
27310Sstevel@tonic-gate 			lwp_exit();
27320Sstevel@tonic-gate 		}
27330Sstevel@tonic-gate 	} else {
27342712Snn35248 		if (zone_status_get(z) == ZONE_IS_BOOTING)
27352712Snn35248 			zone_status_set(z, ZONE_IS_RUNNING);
27360Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
27370Sstevel@tonic-gate 		/* cause the process to return to userland. */
27380Sstevel@tonic-gate 		lwp_rtt();
27390Sstevel@tonic-gate 	}
27400Sstevel@tonic-gate }
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate struct zsched_arg {
27430Sstevel@tonic-gate 	zone_t *zone;
27440Sstevel@tonic-gate 	nvlist_t *nvlist;
27450Sstevel@tonic-gate };
27460Sstevel@tonic-gate 
27470Sstevel@tonic-gate /*
27480Sstevel@tonic-gate  * Per-zone "sched" workalike.  The similarity to "sched" doesn't have
27490Sstevel@tonic-gate  * anything to do with scheduling, but rather with the fact that
27500Sstevel@tonic-gate  * per-zone kernel threads are parented to zsched, just like regular
27510Sstevel@tonic-gate  * kernel threads are parented to sched (p0).
27520Sstevel@tonic-gate  *
27530Sstevel@tonic-gate  * zsched is also responsible for launching init for the zone.
27540Sstevel@tonic-gate  */
27550Sstevel@tonic-gate static void
27560Sstevel@tonic-gate zsched(void *arg)
27570Sstevel@tonic-gate {
27580Sstevel@tonic-gate 	struct zsched_arg *za = arg;
27590Sstevel@tonic-gate 	proc_t *pp = curproc;
27600Sstevel@tonic-gate 	proc_t *initp = proc_init;
27610Sstevel@tonic-gate 	zone_t *zone = za->zone;
27620Sstevel@tonic-gate 	cred_t *cr, *oldcred;
27630Sstevel@tonic-gate 	rctl_set_t *set;
27640Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
27650Sstevel@tonic-gate 	contract_t *ct = NULL;
27660Sstevel@tonic-gate 	task_t *tk, *oldtk;
27670Sstevel@tonic-gate 	rctl_entity_p_t e;
27680Sstevel@tonic-gate 	kproject_t *pj;
27690Sstevel@tonic-gate 
27700Sstevel@tonic-gate 	nvlist_t *nvl = za->nvlist;
27710Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
27720Sstevel@tonic-gate 
27733446Smrj 	bcopy("zsched", PTOU(pp)->u_psargs, sizeof ("zsched"));
27743446Smrj 	bcopy("zsched", PTOU(pp)->u_comm, sizeof ("zsched"));
27753446Smrj 	PTOU(pp)->u_argc = 0;
27763446Smrj 	PTOU(pp)->u_argv = NULL;
27773446Smrj 	PTOU(pp)->u_envp = NULL;
27780Sstevel@tonic-gate 	closeall(P_FINFO(pp));
27790Sstevel@tonic-gate 
27800Sstevel@tonic-gate 	/*
27810Sstevel@tonic-gate 	 * We are this zone's "zsched" process.  As the zone isn't generally
27820Sstevel@tonic-gate 	 * visible yet we don't need to grab any locks before initializing its
27830Sstevel@tonic-gate 	 * zone_proc pointer.
27840Sstevel@tonic-gate 	 */
27850Sstevel@tonic-gate 	zone_hold(zone);  /* this hold is released by zone_destroy() */
27860Sstevel@tonic-gate 	zone->zone_zsched = pp;
27870Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
27880Sstevel@tonic-gate 	pp->p_zone = zone;
27890Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
27900Sstevel@tonic-gate 
27910Sstevel@tonic-gate 	/*
27920Sstevel@tonic-gate 	 * Disassociate process from its 'parent'; parent ourselves to init
27930Sstevel@tonic-gate 	 * (pid 1) and change other values as needed.
27940Sstevel@tonic-gate 	 */
27950Sstevel@tonic-gate 	sess_create();
27960Sstevel@tonic-gate 
27970Sstevel@tonic-gate 	mutex_enter(&pidlock);
27980Sstevel@tonic-gate 	proc_detach(pp);
27990Sstevel@tonic-gate 	pp->p_ppid = 1;
28000Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
28010Sstevel@tonic-gate 	pp->p_ancpid = 1;
28020Sstevel@tonic-gate 	pp->p_parent = initp;
28030Sstevel@tonic-gate 	pp->p_psibling = NULL;
28040Sstevel@tonic-gate 	if (initp->p_child)
28050Sstevel@tonic-gate 		initp->p_child->p_psibling = pp;
28060Sstevel@tonic-gate 	pp->p_sibling = initp->p_child;
28070Sstevel@tonic-gate 	initp->p_child = pp;
28080Sstevel@tonic-gate 
28090Sstevel@tonic-gate 	/* Decrement what newproc() incremented. */
28100Sstevel@tonic-gate 	upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID);
28110Sstevel@tonic-gate 	/*
28120Sstevel@tonic-gate 	 * Our credentials are about to become kcred-like, so we don't care
28130Sstevel@tonic-gate 	 * about the caller's ruid.
28140Sstevel@tonic-gate 	 */
28150Sstevel@tonic-gate 	upcount_inc(crgetruid(kcred), zone->zone_id);
28160Sstevel@tonic-gate 	mutex_exit(&pidlock);
28170Sstevel@tonic-gate 
28180Sstevel@tonic-gate 	/*
28190Sstevel@tonic-gate 	 * getting out of global zone, so decrement lwp counts
28200Sstevel@tonic-gate 	 */
28210Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
28220Sstevel@tonic-gate 	mutex_enter(&global_zone->zone_nlwps_lock);
28230Sstevel@tonic-gate 	pj->kpj_nlwps -= pp->p_lwpcnt;
28240Sstevel@tonic-gate 	global_zone->zone_nlwps -= pp->p_lwpcnt;
28250Sstevel@tonic-gate 	mutex_exit(&global_zone->zone_nlwps_lock);
28260Sstevel@tonic-gate 
28270Sstevel@tonic-gate 	/*
28282768Ssl108498 	 * Decrement locked memory counts on old zone and project.
28292768Ssl108498 	 */
28303247Sgjelinek 	mutex_enter(&global_zone->zone_mem_lock);
28312768Ssl108498 	global_zone->zone_locked_mem -= pp->p_locked_mem;
28322768Ssl108498 	pj->kpj_data.kpd_locked_mem -= pp->p_locked_mem;
28333247Sgjelinek 	mutex_exit(&global_zone->zone_mem_lock);
28342768Ssl108498 
28352768Ssl108498 	/*
28360Sstevel@tonic-gate 	 * Create and join a new task in project '0' of this zone.
28370Sstevel@tonic-gate 	 *
28380Sstevel@tonic-gate 	 * We don't need to call holdlwps() since we know we're the only lwp in
28390Sstevel@tonic-gate 	 * this process.
28400Sstevel@tonic-gate 	 *
28410Sstevel@tonic-gate 	 * task_join() returns with p_lock held.
28420Sstevel@tonic-gate 	 */
28430Sstevel@tonic-gate 	tk = task_create(0, zone);
28440Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
28450Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
28462768Ssl108498 
28472768Ssl108498 	pj = pp->p_task->tk_proj;
28482768Ssl108498 
28493247Sgjelinek 	mutex_enter(&zone->zone_mem_lock);
28502768Ssl108498 	zone->zone_locked_mem += pp->p_locked_mem;
28512768Ssl108498 	pj->kpj_data.kpd_locked_mem += pp->p_locked_mem;
28523247Sgjelinek 	mutex_exit(&zone->zone_mem_lock);
28530Sstevel@tonic-gate 
28540Sstevel@tonic-gate 	/*
28550Sstevel@tonic-gate 	 * add lwp counts to zsched's zone, and increment project's task count
28560Sstevel@tonic-gate 	 * due to the task created in the above tasksys_settaskid
28570Sstevel@tonic-gate 	 */
28582768Ssl108498 
28590Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
28600Sstevel@tonic-gate 	pj->kpj_nlwps += pp->p_lwpcnt;
28610Sstevel@tonic-gate 	pj->kpj_ntasks += 1;
28620Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
28630Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
28640Sstevel@tonic-gate 
28652768Ssl108498 	mutex_exit(&curproc->p_lock);
28662768Ssl108498 	mutex_exit(&cpu_lock);
28672768Ssl108498 	task_rele(oldtk);
28682768Ssl108498 
28690Sstevel@tonic-gate 	/*
28700Sstevel@tonic-gate 	 * The process was created by a process in the global zone, hence the
28710Sstevel@tonic-gate 	 * credentials are wrong.  We might as well have kcred-ish credentials.
28720Sstevel@tonic-gate 	 */
28730Sstevel@tonic-gate 	cr = zone->zone_kcred;
28740Sstevel@tonic-gate 	crhold(cr);
28750Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
28760Sstevel@tonic-gate 	oldcred = pp->p_cred;
28770Sstevel@tonic-gate 	pp->p_cred = cr;
28780Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
28790Sstevel@tonic-gate 	crfree(oldcred);
28800Sstevel@tonic-gate 
28810Sstevel@tonic-gate 	/*
28820Sstevel@tonic-gate 	 * Hold credentials again (for thread)
28830Sstevel@tonic-gate 	 */
28840Sstevel@tonic-gate 	crhold(cr);
28850Sstevel@tonic-gate 
28860Sstevel@tonic-gate 	/*
28870Sstevel@tonic-gate 	 * p_lwpcnt can't change since this is a kernel process.
28880Sstevel@tonic-gate 	 */
28890Sstevel@tonic-gate 	crset(pp, cr);
28900Sstevel@tonic-gate 
28910Sstevel@tonic-gate 	/*
28920Sstevel@tonic-gate 	 * Chroot
28930Sstevel@tonic-gate 	 */
28940Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp);
28950Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp);
28960Sstevel@tonic-gate 
28970Sstevel@tonic-gate 	/*
28980Sstevel@tonic-gate 	 * Initialize zone's rctl set.
28990Sstevel@tonic-gate 	 */
29000Sstevel@tonic-gate 	set = rctl_set_create();
29010Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
29020Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
29030Sstevel@tonic-gate 	e.rcep_p.zone = zone;
29040Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
29050Sstevel@tonic-gate 	zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp);
29060Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
29070Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
29080Sstevel@tonic-gate 
29090Sstevel@tonic-gate 	/*
29100Sstevel@tonic-gate 	 * Apply the rctls passed in to zone_create().  This is basically a list
29110Sstevel@tonic-gate 	 * assignment: all of the old values are removed and the new ones
29120Sstevel@tonic-gate 	 * inserted.  That is, if an empty list is passed in, all values are
29130Sstevel@tonic-gate 	 * removed.
29140Sstevel@tonic-gate 	 */
29150Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
29160Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
29170Sstevel@tonic-gate 		rctl_hndl_t hndl;
29180Sstevel@tonic-gate 		char *name;
29190Sstevel@tonic-gate 		nvlist_t **nvlarray;
29200Sstevel@tonic-gate 		uint_t i, nelem;
29210Sstevel@tonic-gate 		int error;	/* For ASSERT()s */
29220Sstevel@tonic-gate 
29230Sstevel@tonic-gate 		name = nvpair_name(nvp);
29240Sstevel@tonic-gate 		hndl = rctl_hndl_lookup(name);
29250Sstevel@tonic-gate 		ASSERT(hndl != -1);
29260Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
29270Sstevel@tonic-gate 		ASSERT(rde != NULL);
29280Sstevel@tonic-gate 
29290Sstevel@tonic-gate 		for (; /* ever */; ) {
29300Sstevel@tonic-gate 			rctl_val_t oval;
29310Sstevel@tonic-gate 
29320Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
29330Sstevel@tonic-gate 			error = rctl_local_get(hndl, NULL, &oval, pp);
29340Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
29350Sstevel@tonic-gate 			ASSERT(error == 0);	/* Can't fail for RCTL_FIRST */
29360Sstevel@tonic-gate 			ASSERT(oval.rcv_privilege != RCPRIV_BASIC);
29370Sstevel@tonic-gate 			if (oval.rcv_privilege == RCPRIV_SYSTEM)
29380Sstevel@tonic-gate 				break;
29390Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
29400Sstevel@tonic-gate 			error = rctl_local_delete(hndl, &oval, pp);
29410Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
29420Sstevel@tonic-gate 			ASSERT(error == 0);
29430Sstevel@tonic-gate 		}
29440Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
29450Sstevel@tonic-gate 		ASSERT(error == 0);
29460Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
29470Sstevel@tonic-gate 			rctl_val_t *nvalp;
29480Sstevel@tonic-gate 
29490Sstevel@tonic-gate 			nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
29500Sstevel@tonic-gate 			error = nvlist2rctlval(nvlarray[i], nvalp);
29510Sstevel@tonic-gate 			ASSERT(error == 0);
29520Sstevel@tonic-gate 			/*
29530Sstevel@tonic-gate 			 * rctl_local_insert can fail if the value being
29540Sstevel@tonic-gate 			 * inserted is a duplicate; this is OK.
29550Sstevel@tonic-gate 			 */
29560Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
29570Sstevel@tonic-gate 			if (rctl_local_insert(hndl, nvalp, pp) != 0)
29580Sstevel@tonic-gate 				kmem_cache_free(rctl_val_cache, nvalp);
29590Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
29600Sstevel@tonic-gate 		}
29610Sstevel@tonic-gate 	}
29620Sstevel@tonic-gate 	/*
29630Sstevel@tonic-gate 	 * Tell the world that we're done setting up.
29640Sstevel@tonic-gate 	 *
29650Sstevel@tonic-gate 	 * At this point we want to set the zone status to ZONE_IS_READY
29660Sstevel@tonic-gate 	 * and atomically set the zone's processor set visibility.  Once
29670Sstevel@tonic-gate 	 * we drop pool_lock() this zone will automatically get updated
29680Sstevel@tonic-gate 	 * to reflect any future changes to the pools configuration.
29690Sstevel@tonic-gate 	 */
29700Sstevel@tonic-gate 	pool_lock();
29710Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
29720Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
29730Sstevel@tonic-gate 	zone_uniqid(zone);
29740Sstevel@tonic-gate 	zone_zsd_configure(zone);
29750Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED)
29760Sstevel@tonic-gate 		zone_pset_set(zone, pool_default->pool_pset->pset_id);
29770Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
29780Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
29790Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_READY);
29800Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
29810Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
29820Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
29830Sstevel@tonic-gate 	pool_unlock();
29840Sstevel@tonic-gate 
29850Sstevel@tonic-gate 	/*
29860Sstevel@tonic-gate 	 * Once we see the zone transition to the ZONE_IS_BOOTING state,
29870Sstevel@tonic-gate 	 * we launch init, and set the state to running.
29880Sstevel@tonic-gate 	 */
29890Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched");
29900Sstevel@tonic-gate 
29910Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_BOOTING) {
29920Sstevel@tonic-gate 		id_t cid;
29930Sstevel@tonic-gate 
29940Sstevel@tonic-gate 		/*
29950Sstevel@tonic-gate 		 * Ok, this is a little complicated.  We need to grab the
29960Sstevel@tonic-gate 		 * zone's pool's scheduling class ID; note that by now, we
29970Sstevel@tonic-gate 		 * are already bound to a pool if we need to be (zoneadmd
29980Sstevel@tonic-gate 		 * will have done that to us while we're in the READY
29990Sstevel@tonic-gate 		 * state).  *But* the scheduling class for the zone's 'init'
30000Sstevel@tonic-gate 		 * must be explicitly passed to newproc, which doesn't
30010Sstevel@tonic-gate 		 * respect pool bindings.
30020Sstevel@tonic-gate 		 *
30030Sstevel@tonic-gate 		 * We hold the pool_lock across the call to newproc() to
30040Sstevel@tonic-gate 		 * close the obvious race: the pool's scheduling class
30050Sstevel@tonic-gate 		 * could change before we manage to create the LWP with
30060Sstevel@tonic-gate 		 * classid 'cid'.
30070Sstevel@tonic-gate 		 */
30080Sstevel@tonic-gate 		pool_lock();
30093247Sgjelinek 		if (zone->zone_defaultcid > 0)
30103247Sgjelinek 			cid = zone->zone_defaultcid;
30113247Sgjelinek 		else
30123247Sgjelinek 			cid = pool_get_class(zone->zone_pool);
30130Sstevel@tonic-gate 		if (cid == -1)
30140Sstevel@tonic-gate 			cid = defaultcid;
30150Sstevel@tonic-gate 
30160Sstevel@tonic-gate 		/*
30170Sstevel@tonic-gate 		 * If this fails, zone_boot will ultimately fail.  The
30180Sstevel@tonic-gate 		 * state of the zone will be set to SHUTTING_DOWN-- userland
30190Sstevel@tonic-gate 		 * will have to tear down the zone, and fail, or try again.
30200Sstevel@tonic-gate 		 */
30212267Sdp 		if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid,
30220Sstevel@tonic-gate 		    minclsyspri - 1, &ct)) != 0) {
30230Sstevel@tonic-gate 			mutex_enter(&zone_status_lock);
30240Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
30250Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
30260Sstevel@tonic-gate 		}
30270Sstevel@tonic-gate 		pool_unlock();
30280Sstevel@tonic-gate 	}
30290Sstevel@tonic-gate 
30300Sstevel@tonic-gate 	/*
30310Sstevel@tonic-gate 	 * Wait for zone_destroy() to be called.  This is what we spend
30320Sstevel@tonic-gate 	 * most of our life doing.
30330Sstevel@tonic-gate 	 */
30340Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched");
30350Sstevel@tonic-gate 
30360Sstevel@tonic-gate 	if (ct)
30370Sstevel@tonic-gate 		/*
30380Sstevel@tonic-gate 		 * At this point the process contract should be empty.
30390Sstevel@tonic-gate 		 * (Though if it isn't, it's not the end of the world.)
30400Sstevel@tonic-gate 		 */
30410Sstevel@tonic-gate 		VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0);
30420Sstevel@tonic-gate 
30430Sstevel@tonic-gate 	/*
30440Sstevel@tonic-gate 	 * Allow kcred to be freed when all referring processes
30450Sstevel@tonic-gate 	 * (including this one) go away.  We can't just do this in
30460Sstevel@tonic-gate 	 * zone_free because we need to wait for the zone_cred_ref to
30470Sstevel@tonic-gate 	 * drop to 0 before calling zone_free, and the existence of
30480Sstevel@tonic-gate 	 * zone_kcred will prevent that.  Thus, we call crfree here to
30490Sstevel@tonic-gate 	 * balance the crdup in zone_create.  The crhold calls earlier
30500Sstevel@tonic-gate 	 * in zsched will be dropped when the thread and process exit.
30510Sstevel@tonic-gate 	 */
30520Sstevel@tonic-gate 	crfree(zone->zone_kcred);
30530Sstevel@tonic-gate 	zone->zone_kcred = NULL;
30540Sstevel@tonic-gate 
30550Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
30560Sstevel@tonic-gate }
30570Sstevel@tonic-gate 
30580Sstevel@tonic-gate /*
30590Sstevel@tonic-gate  * Helper function to determine if there are any submounts of the
30600Sstevel@tonic-gate  * provided path.  Used to make sure the zone doesn't "inherit" any
30610Sstevel@tonic-gate  * mounts from before it is created.
30620Sstevel@tonic-gate  */
30630Sstevel@tonic-gate static uint_t
30640Sstevel@tonic-gate zone_mount_count(const char *rootpath)
30650Sstevel@tonic-gate {
30660Sstevel@tonic-gate 	vfs_t *vfsp;
30670Sstevel@tonic-gate 	uint_t count = 0;
30680Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate 	/*
30710Sstevel@tonic-gate 	 * Holding zonehash_lock prevents race conditions with
30720Sstevel@tonic-gate 	 * vfs_list_add()/vfs_list_remove() since we serialize with
30730Sstevel@tonic-gate 	 * zone_find_by_path().
30740Sstevel@tonic-gate 	 */
30750Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
30760Sstevel@tonic-gate 	/*
30770Sstevel@tonic-gate 	 * The rootpath must end with a '/'
30780Sstevel@tonic-gate 	 */
30790Sstevel@tonic-gate 	ASSERT(rootpath[rootpathlen - 1] == '/');
30800Sstevel@tonic-gate 
30810Sstevel@tonic-gate 	/*
30820Sstevel@tonic-gate 	 * This intentionally does not count the rootpath itself if that
30830Sstevel@tonic-gate 	 * happens to be a mount point.
30840Sstevel@tonic-gate 	 */
30850Sstevel@tonic-gate 	vfs_list_read_lock();
30860Sstevel@tonic-gate 	vfsp = rootvfs;
30870Sstevel@tonic-gate 	do {
30880Sstevel@tonic-gate 		if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt),
30890Sstevel@tonic-gate 		    rootpathlen) == 0)
30900Sstevel@tonic-gate 			count++;
30910Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
30920Sstevel@tonic-gate 	} while (vfsp != rootvfs);
30930Sstevel@tonic-gate 	vfs_list_unlock();
30940Sstevel@tonic-gate 	return (count);
30950Sstevel@tonic-gate }
30960Sstevel@tonic-gate 
30970Sstevel@tonic-gate /*
30980Sstevel@tonic-gate  * Helper function to make sure that a zone created on 'rootpath'
30990Sstevel@tonic-gate  * wouldn't end up containing other zones' rootpaths.
31000Sstevel@tonic-gate  */
31010Sstevel@tonic-gate static boolean_t
31020Sstevel@tonic-gate zone_is_nested(const char *rootpath)
31030Sstevel@tonic-gate {
31040Sstevel@tonic-gate 	zone_t *zone;
31050Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
31060Sstevel@tonic-gate 	size_t len;
31070Sstevel@tonic-gate 
31080Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
31090Sstevel@tonic-gate 
31100Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
31110Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
31120Sstevel@tonic-gate 		if (zone == global_zone)
31130Sstevel@tonic-gate 			continue;
31140Sstevel@tonic-gate 		len = strlen(zone->zone_rootpath);
31150Sstevel@tonic-gate 		if (strncmp(rootpath, zone->zone_rootpath,
31160Sstevel@tonic-gate 		    MIN(rootpathlen, len)) == 0)
31170Sstevel@tonic-gate 			return (B_TRUE);
31180Sstevel@tonic-gate 	}
31190Sstevel@tonic-gate 	return (B_FALSE);
31200Sstevel@tonic-gate }
31210Sstevel@tonic-gate 
31220Sstevel@tonic-gate static int
3123813Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs,
3124813Sdp     size_t zone_privssz)
31250Sstevel@tonic-gate {
31260Sstevel@tonic-gate 	priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
31270Sstevel@tonic-gate 
3128813Sdp 	if (zone_privssz < sizeof (priv_set_t))
3129813Sdp 		return (set_errno(ENOMEM));
3130813Sdp 
31310Sstevel@tonic-gate 	if (copyin(zone_privs, privs, sizeof (priv_set_t))) {
31320Sstevel@tonic-gate 		kmem_free(privs, sizeof (priv_set_t));
31330Sstevel@tonic-gate 		return (EFAULT);
31340Sstevel@tonic-gate 	}
31350Sstevel@tonic-gate 
31360Sstevel@tonic-gate 	zone->zone_privset = privs;
31370Sstevel@tonic-gate 	return (0);
31380Sstevel@tonic-gate }
31390Sstevel@tonic-gate 
31400Sstevel@tonic-gate /*
31410Sstevel@tonic-gate  * We make creative use of nvlists to pass in rctls from userland.  The list is
31420Sstevel@tonic-gate  * a list of the following structures:
31430Sstevel@tonic-gate  *
31440Sstevel@tonic-gate  * (name = rctl_name, value = nvpair_list_array)
31450Sstevel@tonic-gate  *
31460Sstevel@tonic-gate  * Where each element of the nvpair_list_array is of the form:
31470Sstevel@tonic-gate  *
31480Sstevel@tonic-gate  * [(name = "privilege", value = RCPRIV_PRIVILEGED),
31490Sstevel@tonic-gate  * 	(name = "limit", value = uint64_t),
31500Sstevel@tonic-gate  * 	(name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))]
31510Sstevel@tonic-gate  */
31520Sstevel@tonic-gate static int
31530Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp)
31540Sstevel@tonic-gate {
31550Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
31560Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
31570Sstevel@tonic-gate 	char *kbuf;
31580Sstevel@tonic-gate 	int error;
31590Sstevel@tonic-gate 	rctl_val_t rv;
31600Sstevel@tonic-gate 
31610Sstevel@tonic-gate 	*nvlp = NULL;
31620Sstevel@tonic-gate 
31630Sstevel@tonic-gate 	if (buflen == 0)
31640Sstevel@tonic-gate 		return (0);
31650Sstevel@tonic-gate 
31660Sstevel@tonic-gate 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
31670Sstevel@tonic-gate 		return (ENOMEM);
31680Sstevel@tonic-gate 	if (copyin(ubuf, kbuf, buflen)) {
31690Sstevel@tonic-gate 		error = EFAULT;
31700Sstevel@tonic-gate 		goto out;
31710Sstevel@tonic-gate 	}
31720Sstevel@tonic-gate 	if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) {
31730Sstevel@tonic-gate 		/*
31740Sstevel@tonic-gate 		 * nvl may have been allocated/free'd, but the value set to
31750Sstevel@tonic-gate 		 * non-NULL, so we reset it here.
31760Sstevel@tonic-gate 		 */
31770Sstevel@tonic-gate 		nvl = NULL;
31780Sstevel@tonic-gate 		error = EINVAL;
31790Sstevel@tonic-gate 		goto out;
31800Sstevel@tonic-gate 	}
31810Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
31820Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
31830Sstevel@tonic-gate 		rctl_hndl_t hndl;
31840Sstevel@tonic-gate 		nvlist_t **nvlarray;
31850Sstevel@tonic-gate 		uint_t i, nelem;
31860Sstevel@tonic-gate 		char *name;
31870Sstevel@tonic-gate 
31880Sstevel@tonic-gate 		error = EINVAL;
31890Sstevel@tonic-gate 		name = nvpair_name(nvp);
31900Sstevel@tonic-gate 		if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1)
31910Sstevel@tonic-gate 		    != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) {
31920Sstevel@tonic-gate 			goto out;
31930Sstevel@tonic-gate 		}
31940Sstevel@tonic-gate 		if ((hndl = rctl_hndl_lookup(name)) == -1) {
31950Sstevel@tonic-gate 			goto out;
31960Sstevel@tonic-gate 		}
31970Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
31980Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
31990Sstevel@tonic-gate 		ASSERT(error == 0);
32000Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
32010Sstevel@tonic-gate 			if (error = nvlist2rctlval(nvlarray[i], &rv))
32020Sstevel@tonic-gate 				goto out;
32030Sstevel@tonic-gate 		}
32040Sstevel@tonic-gate 		if (rctl_invalid_value(rde, &rv)) {
32050Sstevel@tonic-gate 			error = EINVAL;
32060Sstevel@tonic-gate 			goto out;
32070Sstevel@tonic-gate 		}
32080Sstevel@tonic-gate 	}
32090Sstevel@tonic-gate 	error = 0;
32100Sstevel@tonic-gate 	*nvlp = nvl;
32110Sstevel@tonic-gate out:
32120Sstevel@tonic-gate 	kmem_free(kbuf, buflen);
32130Sstevel@tonic-gate 	if (error && nvl != NULL)
32140Sstevel@tonic-gate 		nvlist_free(nvl);
32150Sstevel@tonic-gate 	return (error);
32160Sstevel@tonic-gate }
32170Sstevel@tonic-gate 
32180Sstevel@tonic-gate int
32190Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) {
32200Sstevel@tonic-gate 	if (er_out != NULL) {
32210Sstevel@tonic-gate 		if (copyout(&er_ext, er_out, sizeof (int))) {
32220Sstevel@tonic-gate 			return (set_errno(EFAULT));
32230Sstevel@tonic-gate 		}
32240Sstevel@tonic-gate 	}
32250Sstevel@tonic-gate 	return (set_errno(er_error));
32260Sstevel@tonic-gate }
32270Sstevel@tonic-gate 
32281676Sjpk static int
32291676Sjpk zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi)
32301676Sjpk {
32311676Sjpk 	ts_label_t *tsl;
32321676Sjpk 	bslabel_t blab;
32331676Sjpk 
32341676Sjpk 	/* Get label from user */
32351676Sjpk 	if (copyin(lab, &blab, sizeof (blab)) != 0)
32361676Sjpk 		return (EFAULT);
32371676Sjpk 	tsl = labelalloc(&blab, doi, KM_NOSLEEP);
32381676Sjpk 	if (tsl == NULL)
32391676Sjpk 		return (ENOMEM);
32401676Sjpk 
32411676Sjpk 	zone->zone_slabel = tsl;
32421676Sjpk 	return (0);
32431676Sjpk }
32441676Sjpk 
32450Sstevel@tonic-gate /*
3246789Sahrens  * Parses a comma-separated list of ZFS datasets into a per-zone dictionary.
3247789Sahrens  */
3248789Sahrens static int
3249789Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen)
3250789Sahrens {
3251789Sahrens 	char *kbuf;
3252789Sahrens 	char *dataset, *next;
3253789Sahrens 	zone_dataset_t *zd;
3254789Sahrens 	size_t len;
3255789Sahrens 
3256789Sahrens 	if (ubuf == NULL || buflen == 0)
3257789Sahrens 		return (0);
3258789Sahrens 
3259789Sahrens 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
3260789Sahrens 		return (ENOMEM);
3261789Sahrens 
3262789Sahrens 	if (copyin(ubuf, kbuf, buflen) != 0) {
3263789Sahrens 		kmem_free(kbuf, buflen);
3264789Sahrens 		return (EFAULT);
3265789Sahrens 	}
3266789Sahrens 
3267789Sahrens 	dataset = next = kbuf;
3268789Sahrens 	for (;;) {
3269789Sahrens 		zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP);
3270789Sahrens 
3271789Sahrens 		next = strchr(dataset, ',');
3272789Sahrens 
3273789Sahrens 		if (next == NULL)
3274789Sahrens 			len = strlen(dataset);
3275789Sahrens 		else
3276789Sahrens 			len = next - dataset;
3277789Sahrens 
3278789Sahrens 		zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP);
3279789Sahrens 		bcopy(dataset, zd->zd_dataset, len);
3280789Sahrens 		zd->zd_dataset[len] = '\0';
3281789Sahrens 
3282789Sahrens 		list_insert_head(&zone->zone_datasets, zd);
3283789Sahrens 
3284789Sahrens 		if (next == NULL)
3285789Sahrens 			break;
3286789Sahrens 
3287789Sahrens 		dataset = next + 1;
3288789Sahrens 	}
3289789Sahrens 
3290789Sahrens 	kmem_free(kbuf, buflen);
3291789Sahrens 	return (0);
3292789Sahrens }
3293789Sahrens 
3294789Sahrens /*
32950Sstevel@tonic-gate  * System call to create/initialize a new zone named 'zone_name', rooted
32960Sstevel@tonic-gate  * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs',
32971676Sjpk  * and initialized with the zone-wide rctls described in 'rctlbuf', and
32981676Sjpk  * with labeling set by 'match', 'doi', and 'label'.
32990Sstevel@tonic-gate  *
33000Sstevel@tonic-gate  * If extended error is non-null, we may use it to return more detailed
33010Sstevel@tonic-gate  * error information.
33020Sstevel@tonic-gate  */
33030Sstevel@tonic-gate static zoneid_t
33040Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root,
3305813Sdp     const priv_set_t *zone_privs, size_t zone_privssz,
3306813Sdp     caddr_t rctlbuf, size_t rctlbufsz,
33071676Sjpk     caddr_t zfsbuf, size_t zfsbufsz, int *extended_error,
33083448Sdh155122     int match, uint32_t doi, const bslabel_t *label,
33093448Sdh155122     int flags)
33100Sstevel@tonic-gate {
33110Sstevel@tonic-gate 	struct zsched_arg zarg;
33120Sstevel@tonic-gate 	nvlist_t *rctls = NULL;
33130Sstevel@tonic-gate 	proc_t *pp = curproc;
33140Sstevel@tonic-gate 	zone_t *zone, *ztmp;
33150Sstevel@tonic-gate 	zoneid_t zoneid;
33160Sstevel@tonic-gate 	int error;
33170Sstevel@tonic-gate 	int error2 = 0;
33180Sstevel@tonic-gate 	char *str;
33190Sstevel@tonic-gate 	cred_t *zkcr;
33201769Scarlsonj 	boolean_t insert_label_hash;
33210Sstevel@tonic-gate 
33220Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
33230Sstevel@tonic-gate 		return (set_errno(EPERM));
33240Sstevel@tonic-gate 
33250Sstevel@tonic-gate 	/* can't boot zone from within chroot environment */
33260Sstevel@tonic-gate 	if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir)
33270Sstevel@tonic-gate 		return (zone_create_error(ENOTSUP, ZE_CHROOTED,
3328813Sdp 		    extended_error));
33290Sstevel@tonic-gate 
33300Sstevel@tonic-gate 	zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP);
33310Sstevel@tonic-gate 	zoneid = zone->zone_id = id_alloc(zoneid_space);
33320Sstevel@tonic-gate 	zone->zone_status = ZONE_IS_UNINITIALIZED;
33330Sstevel@tonic-gate 	zone->zone_pool = pool_default;
33340Sstevel@tonic-gate 	zone->zone_pool_mod = gethrtime();
33350Sstevel@tonic-gate 	zone->zone_psetid = ZONE_PS_INVAL;
33360Sstevel@tonic-gate 	zone->zone_ncpus = 0;
33370Sstevel@tonic-gate 	zone->zone_ncpus_online = 0;
33382712Snn35248 	zone->zone_restart_init = B_TRUE;
33392712Snn35248 	zone->zone_brand = &native_brand;
33402712Snn35248 	zone->zone_initname = NULL;
33410Sstevel@tonic-gate 	mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL);
33420Sstevel@tonic-gate 	mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
33433247Sgjelinek 	mutex_init(&zone->zone_mem_lock, NULL, MUTEX_DEFAULT, NULL);
33440Sstevel@tonic-gate 	cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL);
33450Sstevel@tonic-gate 	list_create(&zone->zone_zsd, sizeof (struct zsd_entry),
33460Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
3347789Sahrens 	list_create(&zone->zone_datasets, sizeof (zone_dataset_t),
3348789Sahrens 	    offsetof(zone_dataset_t, zd_linkage));
33491676Sjpk 	rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
33500Sstevel@tonic-gate 
33513448Sdh155122 	if (flags & ZCF_NET_EXCL) {
33523448Sdh155122 		zone->zone_flags |= ZF_NET_EXCL;
33533448Sdh155122 	}
33543448Sdh155122 
33550Sstevel@tonic-gate 	if ((error = zone_set_name(zone, zone_name)) != 0) {
33560Sstevel@tonic-gate 		zone_free(zone);
33570Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
33580Sstevel@tonic-gate 	}
33590Sstevel@tonic-gate 
33600Sstevel@tonic-gate 	if ((error = zone_set_root(zone, zone_root)) != 0) {
33610Sstevel@tonic-gate 		zone_free(zone);
33620Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
33630Sstevel@tonic-gate 	}
3364813Sdp 	if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) {
33650Sstevel@tonic-gate 		zone_free(zone);
33660Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
33670Sstevel@tonic-gate 	}
33680Sstevel@tonic-gate 
33690Sstevel@tonic-gate 	/* initialize node name to be the same as zone name */
33700Sstevel@tonic-gate 	zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP);
33710Sstevel@tonic-gate 	(void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN);
33720Sstevel@tonic-gate 	zone->zone_nodename[_SYS_NMLN - 1] = '\0';
33730Sstevel@tonic-gate 
33740Sstevel@tonic-gate 	zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP);
33750Sstevel@tonic-gate 	zone->zone_domain[0] = '\0';
33760Sstevel@tonic-gate 	zone->zone_shares = 1;
33772677Sml93401 	zone->zone_shmmax = 0;
33782677Sml93401 	zone->zone_ipc.ipcq_shmmni = 0;
33792677Sml93401 	zone->zone_ipc.ipcq_semmni = 0;
33802677Sml93401 	zone->zone_ipc.ipcq_msgmni = 0;
33810Sstevel@tonic-gate 	zone->zone_bootargs = NULL;
33822267Sdp 	zone->zone_initname =
33832267Sdp 	    kmem_alloc(strlen(zone_default_initname) + 1, KM_SLEEP);
33842267Sdp 	(void) strcpy(zone->zone_initname, zone_default_initname);
33853247Sgjelinek 	zone->zone_nlwps = 0;
33863247Sgjelinek 	zone->zone_nlwps_ctl = INT_MAX;
33872768Ssl108498 	zone->zone_locked_mem = 0;
33882768Ssl108498 	zone->zone_locked_mem_ctl = UINT64_MAX;
33893247Sgjelinek 	zone->zone_max_swap = 0;
33903247Sgjelinek 	zone->zone_max_swap_ctl = UINT64_MAX;
33913247Sgjelinek 	zone0.zone_lockedmem_kstat = NULL;
33923247Sgjelinek 	zone0.zone_swapresv_kstat = NULL;
33930Sstevel@tonic-gate 
33940Sstevel@tonic-gate 	/*
33950Sstevel@tonic-gate 	 * Zsched initializes the rctls.
33960Sstevel@tonic-gate 	 */
33970Sstevel@tonic-gate 	zone->zone_rctls = NULL;
33980Sstevel@tonic-gate 
33990Sstevel@tonic-gate 	if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) {
34000Sstevel@tonic-gate 		zone_free(zone);
34010Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
34020Sstevel@tonic-gate 	}
34030Sstevel@tonic-gate 
3404789Sahrens 	if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) {
3405789Sahrens 		zone_free(zone);
3406789Sahrens 		return (set_errno(error));
3407789Sahrens 	}
3408789Sahrens 
34090Sstevel@tonic-gate 	/*
34101676Sjpk 	 * Read in the trusted system parameters:
34111676Sjpk 	 * match flag and sensitivity label.
34121676Sjpk 	 */
34131676Sjpk 	zone->zone_match = match;
34141769Scarlsonj 	if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) {
34154462Skp158701 		/* Fail if requested to set doi to anything but system's doi */
34164462Skp158701 		if (doi != 0 && doi != default_doi) {
34174462Skp158701 			zone_free(zone);
34184462Skp158701 			return (set_errno(EINVAL));
34194462Skp158701 		}
34204462Skp158701 		/* Always apply system's doi to the zone */
34214462Skp158701 		error = zone_set_label(zone, label, default_doi);
34221676Sjpk 		if (error != 0) {
34231676Sjpk 			zone_free(zone);
34241676Sjpk 			return (set_errno(error));
34251676Sjpk 		}
34261769Scarlsonj 		insert_label_hash = B_TRUE;
34271676Sjpk 	} else {
34281676Sjpk 		/* all zones get an admin_low label if system is not labeled */
34291676Sjpk 		zone->zone_slabel = l_admin_low;
34301676Sjpk 		label_hold(l_admin_low);
34311769Scarlsonj 		insert_label_hash = B_FALSE;
34321676Sjpk 	}
34331676Sjpk 
34341676Sjpk 	/*
34350Sstevel@tonic-gate 	 * Stop all lwps since that's what normally happens as part of fork().
34360Sstevel@tonic-gate 	 * This needs to happen before we grab any locks to avoid deadlock
34370Sstevel@tonic-gate 	 * (another lwp in the process could be waiting for the held lock).
34380Sstevel@tonic-gate 	 */
34390Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) {
34400Sstevel@tonic-gate 		zone_free(zone);
34410Sstevel@tonic-gate 		if (rctls)
34420Sstevel@tonic-gate 			nvlist_free(rctls);
34430Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
34440Sstevel@tonic-gate 	}
34450Sstevel@tonic-gate 
34460Sstevel@tonic-gate 	if (block_mounts() == 0) {
34470Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
34480Sstevel@tonic-gate 		if (curthread != pp->p_agenttp)
34490Sstevel@tonic-gate 			continuelwps(pp);
34500Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
34510Sstevel@tonic-gate 		zone_free(zone);
34520Sstevel@tonic-gate 		if (rctls)
34530Sstevel@tonic-gate 			nvlist_free(rctls);
34540Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
34550Sstevel@tonic-gate 	}
34560Sstevel@tonic-gate 
34570Sstevel@tonic-gate 	/*
34580Sstevel@tonic-gate 	 * Set up credential for kernel access.  After this, any errors
34590Sstevel@tonic-gate 	 * should go through the dance in errout rather than calling
34600Sstevel@tonic-gate 	 * zone_free directly.
34610Sstevel@tonic-gate 	 */
34620Sstevel@tonic-gate 	zone->zone_kcred = crdup(kcred);
34630Sstevel@tonic-gate 	crsetzone(zone->zone_kcred, zone);
34640Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred));
34650Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred));
34660Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred));
34670Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred));
34680Sstevel@tonic-gate 
34690Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
34700Sstevel@tonic-gate 	/*
34710Sstevel@tonic-gate 	 * Make sure zone doesn't already exist.
34721676Sjpk 	 *
34731676Sjpk 	 * If the system and zone are labeled,
34741676Sjpk 	 * make sure no other zone exists that has the same label.
34750Sstevel@tonic-gate 	 */
34761676Sjpk 	if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL ||
34771769Scarlsonj 	    (insert_label_hash &&
34781676Sjpk 	    (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) {
34790Sstevel@tonic-gate 		zone_status_t status;
34800Sstevel@tonic-gate 
34810Sstevel@tonic-gate 		status = zone_status_get(ztmp);
34820Sstevel@tonic-gate 		if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING)
34830Sstevel@tonic-gate 			error = EEXIST;
34840Sstevel@tonic-gate 		else
34850Sstevel@tonic-gate 			error = EBUSY;
34864791Ston 
34874791Ston 		if (insert_label_hash)
34884791Ston 			error2 = ZE_LABELINUSE;
34894791Ston 
34900Sstevel@tonic-gate 		goto errout;
34910Sstevel@tonic-gate 	}
34920Sstevel@tonic-gate 
34930Sstevel@tonic-gate 	/*
34940Sstevel@tonic-gate 	 * Don't allow zone creations which would cause one zone's rootpath to
34950Sstevel@tonic-gate 	 * be accessible from that of another (non-global) zone.
34960Sstevel@tonic-gate 	 */
34970Sstevel@tonic-gate 	if (zone_is_nested(zone->zone_rootpath)) {
34980Sstevel@tonic-gate 		error = EBUSY;
34990Sstevel@tonic-gate 		goto errout;
35000Sstevel@tonic-gate 	}
35010Sstevel@tonic-gate 
35020Sstevel@tonic-gate 	ASSERT(zonecount != 0);		/* check for leaks */
35030Sstevel@tonic-gate 	if (zonecount + 1 > maxzones) {
35040Sstevel@tonic-gate 		error = ENOMEM;
35050Sstevel@tonic-gate 		goto errout;
35060Sstevel@tonic-gate 	}
35070Sstevel@tonic-gate 
35080Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
35090Sstevel@tonic-gate 		error = EBUSY;
35100Sstevel@tonic-gate 		error2 = ZE_AREMOUNTS;
35110Sstevel@tonic-gate 		goto errout;
35120Sstevel@tonic-gate 	}
35130Sstevel@tonic-gate 
35140Sstevel@tonic-gate 	/*
35150Sstevel@tonic-gate 	 * Zone is still incomplete, but we need to drop all locks while
35160Sstevel@tonic-gate 	 * zsched() initializes this zone's kernel process.  We
35170Sstevel@tonic-gate 	 * optimistically add the zone to the hashtable and associated
35180Sstevel@tonic-gate 	 * lists so a parallel zone_create() doesn't try to create the
35190Sstevel@tonic-gate 	 * same zone.
35200Sstevel@tonic-gate 	 */
35210Sstevel@tonic-gate 	zonecount++;
35220Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid,
35230Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id,
35240Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
35250Sstevel@tonic-gate 	str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP);
35260Sstevel@tonic-gate 	(void) strcpy(str, zone->zone_name);
35270Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str,
35280Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
35291769Scarlsonj 	if (insert_label_hash) {
35301676Sjpk 		(void) mod_hash_insert(zonehashbylabel,
35311676Sjpk 		    (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone);
35321769Scarlsonj 		zone->zone_flags |= ZF_HASHED_LABEL;
35331676Sjpk 	}
35341676Sjpk 
35350Sstevel@tonic-gate 	/*
35360Sstevel@tonic-gate 	 * Insert into active list.  At this point there are no 'hold's
35370Sstevel@tonic-gate 	 * on the zone, but everyone else knows not to use it, so we can
35380Sstevel@tonic-gate 	 * continue to use it.  zsched() will do a zone_hold() if the
35390Sstevel@tonic-gate 	 * newproc() is successful.
35400Sstevel@tonic-gate 	 */
35410Sstevel@tonic-gate 	list_insert_tail(&zone_active, zone);
35420Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
35430Sstevel@tonic-gate 
35440Sstevel@tonic-gate 	zarg.zone = zone;
35450Sstevel@tonic-gate 	zarg.nvlist = rctls;
35460Sstevel@tonic-gate 	/*
35470Sstevel@tonic-gate 	 * The process, task, and project rctls are probably wrong;
35480Sstevel@tonic-gate 	 * we need an interface to get the default values of all rctls,
35490Sstevel@tonic-gate 	 * and initialize zsched appropriately.  I'm not sure that that
35500Sstevel@tonic-gate 	 * makes much of a difference, though.
35510Sstevel@tonic-gate 	 */
35520Sstevel@tonic-gate 	if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) {
35530Sstevel@tonic-gate 		/*
35540Sstevel@tonic-gate 		 * We need to undo all globally visible state.
35550Sstevel@tonic-gate 		 */
35560Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
35570Sstevel@tonic-gate 		list_remove(&zone_active, zone);
35581769Scarlsonj 		if (zone->zone_flags & ZF_HASHED_LABEL) {
35591676Sjpk 			ASSERT(zone->zone_slabel != NULL);
35601676Sjpk 			(void) mod_hash_destroy(zonehashbylabel,
35611676Sjpk 			    (mod_hash_key_t)zone->zone_slabel);
35621676Sjpk 		}
35630Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyname,
35640Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_name);
35650Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyid,
35660Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_id);
35670Sstevel@tonic-gate 		ASSERT(zonecount > 1);
35680Sstevel@tonic-gate 		zonecount--;
35690Sstevel@tonic-gate 		goto errout;
35700Sstevel@tonic-gate 	}
35710Sstevel@tonic-gate 
35720Sstevel@tonic-gate 	/*
35730Sstevel@tonic-gate 	 * Zone creation can't fail from now on.
35740Sstevel@tonic-gate 	 */
35750Sstevel@tonic-gate 
35760Sstevel@tonic-gate 	/*
35773247Sgjelinek 	 * Create zone kstats
35783247Sgjelinek 	 */
35793247Sgjelinek 	zone_kstat_create(zone);
35803247Sgjelinek 
35813247Sgjelinek 	/*
35820Sstevel@tonic-gate 	 * Let the other lwps continue.
35830Sstevel@tonic-gate 	 */
35840Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
35850Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
35860Sstevel@tonic-gate 		continuelwps(pp);
35870Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
35880Sstevel@tonic-gate 
35890Sstevel@tonic-gate 	/*
35900Sstevel@tonic-gate 	 * Wait for zsched to finish initializing the zone.
35910Sstevel@tonic-gate 	 */
35920Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_READY);
35930Sstevel@tonic-gate 	/*
35940Sstevel@tonic-gate 	 * The zone is fully visible, so we can let mounts progress.
35950Sstevel@tonic-gate 	 */
35960Sstevel@tonic-gate 	resume_mounts();
35970Sstevel@tonic-gate 	if (rctls)
35980Sstevel@tonic-gate 		nvlist_free(rctls);
35990Sstevel@tonic-gate 
36000Sstevel@tonic-gate 	return (zoneid);
36010Sstevel@tonic-gate 
36020Sstevel@tonic-gate errout:
36030Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
36040Sstevel@tonic-gate 	/*
36050Sstevel@tonic-gate 	 * Let the other lwps continue.
36060Sstevel@tonic-gate 	 */
36070Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
36080Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
36090Sstevel@tonic-gate 		continuelwps(pp);
36100Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
36110Sstevel@tonic-gate 
36120Sstevel@tonic-gate 	resume_mounts();
36130Sstevel@tonic-gate 	if (rctls)
36140Sstevel@tonic-gate 		nvlist_free(rctls);
36150Sstevel@tonic-gate 	/*
36160Sstevel@tonic-gate 	 * There is currently one reference to the zone, a cred_ref from
36170Sstevel@tonic-gate 	 * zone_kcred.  To free the zone, we call crfree, which will call
36180Sstevel@tonic-gate 	 * zone_cred_rele, which will call zone_free.
36190Sstevel@tonic-gate 	 */
36200Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 1);	/* for zone_kcred */
36210Sstevel@tonic-gate 	ASSERT(zone->zone_kcred->cr_ref == 1);
36220Sstevel@tonic-gate 	ASSERT(zone->zone_ref == 0);
36230Sstevel@tonic-gate 	zkcr = zone->zone_kcred;
36240Sstevel@tonic-gate 	zone->zone_kcred = NULL;
36250Sstevel@tonic-gate 	crfree(zkcr);				/* triggers call to zone_free */
36260Sstevel@tonic-gate 	return (zone_create_error(error, error2, extended_error));
36270Sstevel@tonic-gate }
36280Sstevel@tonic-gate 
36290Sstevel@tonic-gate /*
36300Sstevel@tonic-gate  * Cause the zone to boot.  This is pretty simple, since we let zoneadmd do
36312267Sdp  * the heavy lifting.  initname is the path to the program to launch
36322267Sdp  * at the "top" of the zone; if this is NULL, we use the system default,
36332267Sdp  * which is stored at zone_default_initname.
36340Sstevel@tonic-gate  */
36350Sstevel@tonic-gate static int
36362267Sdp zone_boot(zoneid_t zoneid)
36370Sstevel@tonic-gate {
36380Sstevel@tonic-gate 	int err;
36390Sstevel@tonic-gate 	zone_t *zone;
36400Sstevel@tonic-gate 
36410Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
36420Sstevel@tonic-gate 		return (set_errno(EPERM));
36430Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
36440Sstevel@tonic-gate 		return (set_errno(EINVAL));
36450Sstevel@tonic-gate 
36460Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
36470Sstevel@tonic-gate 	/*
36480Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with calls to
36490Sstevel@tonic-gate 	 * zone_shutdown, zone_destroy, etc.
36500Sstevel@tonic-gate 	 */
36510Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
36520Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
36530Sstevel@tonic-gate 		return (set_errno(EINVAL));
36540Sstevel@tonic-gate 	}
36550Sstevel@tonic-gate 
36560Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
36570Sstevel@tonic-gate 	if (zone_status_get(zone) != ZONE_IS_READY) {
36580Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
36590Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
36600Sstevel@tonic-gate 		return (set_errno(EINVAL));
36610Sstevel@tonic-gate 	}
36620Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_BOOTING);
36630Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
36640Sstevel@tonic-gate 
36650Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
36660Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
36670Sstevel@tonic-gate 
36680Sstevel@tonic-gate 	if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) {
36690Sstevel@tonic-gate 		zone_rele(zone);
36700Sstevel@tonic-gate 		return (set_errno(EINTR));
36710Sstevel@tonic-gate 	}
36720Sstevel@tonic-gate 
36730Sstevel@tonic-gate 	/*
36740Sstevel@tonic-gate 	 * Boot (starting init) might have failed, in which case the zone
36750Sstevel@tonic-gate 	 * will go to the SHUTTING_DOWN state; an appropriate errno will
36760Sstevel@tonic-gate 	 * be placed in zone->zone_boot_err, and so we return that.
36770Sstevel@tonic-gate 	 */
36780Sstevel@tonic-gate 	err = zone->zone_boot_err;
36790Sstevel@tonic-gate 	zone_rele(zone);
36800Sstevel@tonic-gate 	return (err ? set_errno(err) : 0);
36810Sstevel@tonic-gate }
36820Sstevel@tonic-gate 
36830Sstevel@tonic-gate /*
36840Sstevel@tonic-gate  * Kills all user processes in the zone, waiting for them all to exit
36850Sstevel@tonic-gate  * before returning.
36860Sstevel@tonic-gate  */
36870Sstevel@tonic-gate static int
36880Sstevel@tonic-gate zone_empty(zone_t *zone)
36890Sstevel@tonic-gate {
36900Sstevel@tonic-gate 	int waitstatus;
36910Sstevel@tonic-gate 
36920Sstevel@tonic-gate 	/*
36930Sstevel@tonic-gate 	 * We need to drop zonehash_lock before killing all
36940Sstevel@tonic-gate 	 * processes, otherwise we'll deadlock with zone_find_*
36950Sstevel@tonic-gate 	 * which can be called from the exit path.
36960Sstevel@tonic-gate 	 */
36970Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
36980Sstevel@tonic-gate 	while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz,
36990Sstevel@tonic-gate 	    ZONE_IS_EMPTY)) == -1) {
37000Sstevel@tonic-gate 		killall(zone->zone_id);
37010Sstevel@tonic-gate 	}
37020Sstevel@tonic-gate 	/*
37030Sstevel@tonic-gate 	 * return EINTR if we were signaled
37040Sstevel@tonic-gate 	 */
37050Sstevel@tonic-gate 	if (waitstatus == 0)
37060Sstevel@tonic-gate 		return (EINTR);
37070Sstevel@tonic-gate 	return (0);
37080Sstevel@tonic-gate }
37090Sstevel@tonic-gate 
37100Sstevel@tonic-gate /*
37111676Sjpk  * This function implements the policy for zone visibility.
37121676Sjpk  *
37131676Sjpk  * In standard Solaris, a non-global zone can only see itself.
37141676Sjpk  *
37151676Sjpk  * In Trusted Extensions, a labeled zone can lookup any zone whose label
37161676Sjpk  * it dominates. For this test, the label of the global zone is treated as
37171676Sjpk  * admin_high so it is special-cased instead of being checked for dominance.
37181676Sjpk  *
37191676Sjpk  * Returns true if zone attributes are viewable, false otherwise.
37201676Sjpk  */
37211676Sjpk static boolean_t
37221676Sjpk zone_list_access(zone_t *zone)
37231676Sjpk {
37241676Sjpk 
37251676Sjpk 	if (curproc->p_zone == global_zone ||
37261676Sjpk 	    curproc->p_zone == zone) {
37271676Sjpk 		return (B_TRUE);
37281769Scarlsonj 	} else if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) {
37291676Sjpk 		bslabel_t *curproc_label;
37301676Sjpk 		bslabel_t *zone_label;
37311676Sjpk 
37321676Sjpk 		curproc_label = label2bslabel(curproc->p_zone->zone_slabel);
37331676Sjpk 		zone_label = label2bslabel(zone->zone_slabel);
37341676Sjpk 
37351676Sjpk 		if (zone->zone_id != GLOBAL_ZONEID &&
37361676Sjpk 		    bldominates(curproc_label, zone_label)) {
37371676Sjpk 			return (B_TRUE);
37381676Sjpk 		} else {
37391676Sjpk 			return (B_FALSE);
37401676Sjpk 		}
37411676Sjpk 	} else {
37421676Sjpk 		return (B_FALSE);
37431676Sjpk 	}
37441676Sjpk }
37451676Sjpk 
37461676Sjpk /*
37470Sstevel@tonic-gate  * Systemcall to start the zone's halt sequence.  By the time this
37480Sstevel@tonic-gate  * function successfully returns, all user processes and kernel threads
37490Sstevel@tonic-gate  * executing in it will have exited, ZSD shutdown callbacks executed,
37500Sstevel@tonic-gate  * and the zone status set to ZONE_IS_DOWN.
37510Sstevel@tonic-gate  *
37520Sstevel@tonic-gate  * It is possible that the call will interrupt itself if the caller is the
37530Sstevel@tonic-gate  * parent of any process running in the zone, and doesn't have SIGCHLD blocked.
37540Sstevel@tonic-gate  */
37550Sstevel@tonic-gate static int
37560Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid)
37570Sstevel@tonic-gate {
37580Sstevel@tonic-gate 	int error;
37590Sstevel@tonic-gate 	zone_t *zone;
37600Sstevel@tonic-gate 	zone_status_t status;
37610Sstevel@tonic-gate 
37620Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
37630Sstevel@tonic-gate 		return (set_errno(EPERM));
37640Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
37650Sstevel@tonic-gate 		return (set_errno(EINVAL));
37660Sstevel@tonic-gate 
37670Sstevel@tonic-gate 	/*
37680Sstevel@tonic-gate 	 * Block mounts so that VFS_MOUNT() can get an accurate view of
37690Sstevel@tonic-gate 	 * the zone's status with regards to ZONE_IS_SHUTTING down.
37700Sstevel@tonic-gate 	 *
37710Sstevel@tonic-gate 	 * e.g. NFS can fail the mount if it determines that the zone
37720Sstevel@tonic-gate 	 * has already begun the shutdown sequence.
37730Sstevel@tonic-gate 	 */
37740Sstevel@tonic-gate 	if (block_mounts() == 0)
37750Sstevel@tonic-gate 		return (set_errno(EINTR));
37760Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
37770Sstevel@tonic-gate 	/*
37780Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
37790Sstevel@tonic-gate 	 * calls to zone_shutdown and zone_destroy.
37800Sstevel@tonic-gate 	 */
37810Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
37820Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
37830Sstevel@tonic-gate 		resume_mounts();
37840Sstevel@tonic-gate 		return (set_errno(EINVAL));
37850Sstevel@tonic-gate 	}
37860Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
37870Sstevel@tonic-gate 	status = zone_status_get(zone);
37880Sstevel@tonic-gate 	/*
37890Sstevel@tonic-gate 	 * Fail if the zone isn't fully initialized yet.
37900Sstevel@tonic-gate 	 */
37910Sstevel@tonic-gate 	if (status < ZONE_IS_READY) {
37920Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
37930Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
37940Sstevel@tonic-gate 		resume_mounts();
37950Sstevel@tonic-gate 		return (set_errno(EINVAL));
37960Sstevel@tonic-gate 	}
37970Sstevel@tonic-gate 	/*
37980Sstevel@tonic-gate 	 * If conditions required for zone_shutdown() to return have been met,
37990Sstevel@tonic-gate 	 * return success.
38000Sstevel@tonic-gate 	 */
38010Sstevel@tonic-gate 	if (status >= ZONE_IS_DOWN) {
38020Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
38030Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
38040Sstevel@tonic-gate 		resume_mounts();
38050Sstevel@tonic-gate 		return (0);
38060Sstevel@tonic-gate 	}
38070Sstevel@tonic-gate 	/*
38080Sstevel@tonic-gate 	 * If zone_shutdown() hasn't been called before, go through the motions.
38090Sstevel@tonic-gate 	 * If it has, there's nothing to do but wait for the kernel threads to
38100Sstevel@tonic-gate 	 * drain.
38110Sstevel@tonic-gate 	 */
38120Sstevel@tonic-gate 	if (status < ZONE_IS_EMPTY) {
38130Sstevel@tonic-gate 		uint_t ntasks;
38140Sstevel@tonic-gate 
38150Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
38160Sstevel@tonic-gate 		if ((ntasks = zone->zone_ntasks) != 1) {
38170Sstevel@tonic-gate 			/*
38180Sstevel@tonic-gate 			 * There's still stuff running.
38190Sstevel@tonic-gate 			 */
38200Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
38210Sstevel@tonic-gate 		}
38220Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
38230Sstevel@tonic-gate 		if (ntasks == 1) {
38240Sstevel@tonic-gate 			/*
38250Sstevel@tonic-gate 			 * The only way to create another task is through
38260Sstevel@tonic-gate 			 * zone_enter(), which will block until we drop
38270Sstevel@tonic-gate 			 * zonehash_lock.  The zone is empty.
38280Sstevel@tonic-gate 			 */
38290Sstevel@tonic-gate 			if (zone->zone_kthreads == NULL) {
38300Sstevel@tonic-gate 				/*
38310Sstevel@tonic-gate 				 * Skip ahead to ZONE_IS_DOWN
38320Sstevel@tonic-gate 				 */
38330Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_DOWN);
38340Sstevel@tonic-gate 			} else {
38350Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_EMPTY);
38360Sstevel@tonic-gate 			}
38370Sstevel@tonic-gate 		}
38380Sstevel@tonic-gate 	}
38390Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
38400Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
38410Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
38420Sstevel@tonic-gate 	resume_mounts();
38430Sstevel@tonic-gate 
38440Sstevel@tonic-gate 	if (error = zone_empty(zone)) {
38450Sstevel@tonic-gate 		zone_rele(zone);
38460Sstevel@tonic-gate 		return (set_errno(error));
38470Sstevel@tonic-gate 	}
38480Sstevel@tonic-gate 	/*
38490Sstevel@tonic-gate 	 * After the zone status goes to ZONE_IS_DOWN this zone will no
38500Sstevel@tonic-gate 	 * longer be notified of changes to the pools configuration, so
38510Sstevel@tonic-gate 	 * in order to not end up with a stale pool pointer, we point
38520Sstevel@tonic-gate 	 * ourselves at the default pool and remove all resource
38530Sstevel@tonic-gate 	 * visibility.  This is especially important as the zone_t may
38540Sstevel@tonic-gate 	 * languish on the deathrow for a very long time waiting for
38550Sstevel@tonic-gate 	 * cred's to drain out.
38560Sstevel@tonic-gate 	 *
38570Sstevel@tonic-gate 	 * This rebinding of the zone can happen multiple times
38580Sstevel@tonic-gate 	 * (presumably due to interrupted or parallel systemcalls)
38590Sstevel@tonic-gate 	 * without any adverse effects.
38600Sstevel@tonic-gate 	 */
38610Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
38620Sstevel@tonic-gate 		zone_rele(zone);
38630Sstevel@tonic-gate 		return (set_errno(EINTR));
38640Sstevel@tonic-gate 	}
38650Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
38660Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
38670Sstevel@tonic-gate 		zone_pool_set(zone, pool_default);
38680Sstevel@tonic-gate 		/*
38690Sstevel@tonic-gate 		 * The zone no longer needs to be able to see any cpus.
38700Sstevel@tonic-gate 		 */
38710Sstevel@tonic-gate 		zone_pset_set(zone, ZONE_PS_INVAL);
38720Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
38730Sstevel@tonic-gate 	}
38740Sstevel@tonic-gate 	pool_unlock();
38750Sstevel@tonic-gate 
38760Sstevel@tonic-gate 	/*
38770Sstevel@tonic-gate 	 * ZSD shutdown callbacks can be executed multiple times, hence
38780Sstevel@tonic-gate 	 * it is safe to not be holding any locks across this call.
38790Sstevel@tonic-gate 	 */
38800Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_SHUTDOWN);
38810Sstevel@tonic-gate 
38820Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
38830Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN)
38840Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DOWN);
38850Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
38860Sstevel@tonic-gate 
38870Sstevel@tonic-gate 	/*
38880Sstevel@tonic-gate 	 * Wait for kernel threads to drain.
38890Sstevel@tonic-gate 	 */
38900Sstevel@tonic-gate 	if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) {
38910Sstevel@tonic-gate 		zone_rele(zone);
38920Sstevel@tonic-gate 		return (set_errno(EINTR));
38930Sstevel@tonic-gate 	}
38942712Snn35248 
38953671Ssl108498 	/*
38963671Ssl108498 	 * Zone can be become down/destroyable even if the above wait
38973671Ssl108498 	 * returns EINTR, so any code added here may never execute.
38983671Ssl108498 	 * (i.e. don't add code here)
38993671Ssl108498 	 */
39002712Snn35248 
39010Sstevel@tonic-gate 	zone_rele(zone);
39020Sstevel@tonic-gate 	return (0);
39030Sstevel@tonic-gate }
39040Sstevel@tonic-gate 
39050Sstevel@tonic-gate /*
39060Sstevel@tonic-gate  * Systemcall entry point to finalize the zone halt process.  The caller
39072677Sml93401  * must have already successfully called zone_shutdown().
39080Sstevel@tonic-gate  *
39090Sstevel@tonic-gate  * Upon successful completion, the zone will have been fully destroyed:
39100Sstevel@tonic-gate  * zsched will have exited, destructor callbacks executed, and the zone
39110Sstevel@tonic-gate  * removed from the list of active zones.
39120Sstevel@tonic-gate  */
39130Sstevel@tonic-gate static int
39140Sstevel@tonic-gate zone_destroy(zoneid_t zoneid)
39150Sstevel@tonic-gate {
39160Sstevel@tonic-gate 	uint64_t uniqid;
39170Sstevel@tonic-gate 	zone_t *zone;
39180Sstevel@tonic-gate 	zone_status_t status;
39190Sstevel@tonic-gate 
39200Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
39210Sstevel@tonic-gate 		return (set_errno(EPERM));
39220Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
39230Sstevel@tonic-gate 		return (set_errno(EINVAL));
39240Sstevel@tonic-gate 
39250Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
39260Sstevel@tonic-gate 	/*
39270Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
39280Sstevel@tonic-gate 	 * calls to zone_destroy.
39290Sstevel@tonic-gate 	 */
39300Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
39310Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
39320Sstevel@tonic-gate 		return (set_errno(EINVAL));
39330Sstevel@tonic-gate 	}
39340Sstevel@tonic-gate 
39350Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
39360Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
39370Sstevel@tonic-gate 		return (set_errno(EBUSY));
39380Sstevel@tonic-gate 	}
39390Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
39400Sstevel@tonic-gate 	status = zone_status_get(zone);
39410Sstevel@tonic-gate 	if (status < ZONE_IS_DOWN) {
39420Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
39430Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
39440Sstevel@tonic-gate 		return (set_errno(EBUSY));
39450Sstevel@tonic-gate 	} else if (status == ZONE_IS_DOWN) {
39460Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */
39470Sstevel@tonic-gate 	}
39480Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
39490Sstevel@tonic-gate 	zone_hold(zone);
39500Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
39510Sstevel@tonic-gate 
39520Sstevel@tonic-gate 	/*
39530Sstevel@tonic-gate 	 * wait for zsched to exit
39540Sstevel@tonic-gate 	 */
39550Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_DEAD);
39560Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_DESTROY);
39573448Sdh155122 	zone->zone_netstack = NULL;
39580Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
39590Sstevel@tonic-gate 	zone_rele(zone);
39600Sstevel@tonic-gate 	zone = NULL;	/* potentially free'd */
39610Sstevel@tonic-gate 
39620Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
39630Sstevel@tonic-gate 	for (; /* ever */; ) {
39640Sstevel@tonic-gate 		boolean_t unref;
39650Sstevel@tonic-gate 
39660Sstevel@tonic-gate 		if ((zone = zone_find_all_by_id(zoneid)) == NULL ||
39670Sstevel@tonic-gate 		    zone->zone_uniqid != uniqid) {
39680Sstevel@tonic-gate 			/*
39690Sstevel@tonic-gate 			 * The zone has gone away.  Necessary conditions
39700Sstevel@tonic-gate 			 * are met, so we return success.
39710Sstevel@tonic-gate 			 */
39720Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
39730Sstevel@tonic-gate 			return (0);
39740Sstevel@tonic-gate 		}
39750Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
39760Sstevel@tonic-gate 		unref = ZONE_IS_UNREF(zone);
39770Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
39780Sstevel@tonic-gate 		if (unref) {
39790Sstevel@tonic-gate 			/*
39800Sstevel@tonic-gate 			 * There is only one reference to the zone -- that
39810Sstevel@tonic-gate 			 * added when the zone was added to the hashtables --
39820Sstevel@tonic-gate 			 * and things will remain this way until we drop
39830Sstevel@tonic-gate 			 * zonehash_lock... we can go ahead and cleanup the
39840Sstevel@tonic-gate 			 * zone.
39850Sstevel@tonic-gate 			 */
39860Sstevel@tonic-gate 			break;
39870Sstevel@tonic-gate 		}
39880Sstevel@tonic-gate 
39890Sstevel@tonic-gate 		if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) {
39900Sstevel@tonic-gate 			/* Signaled */
39910Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
39920Sstevel@tonic-gate 			return (set_errno(EINTR));
39930Sstevel@tonic-gate 		}
39940Sstevel@tonic-gate 
39950Sstevel@tonic-gate 	}
39960Sstevel@tonic-gate 
39973792Sakolb 	/*
39983792Sakolb 	 * Remove CPU cap for this zone now since we're not going to
39993792Sakolb 	 * fail below this point.
40003792Sakolb 	 */
40013792Sakolb 	cpucaps_zone_remove(zone);
40023792Sakolb 
40033792Sakolb 	/* Get rid of the zone's kstats */
40043247Sgjelinek 	zone_kstat_delete(zone);
40053247Sgjelinek 
40064888Seh208807 	/* free brand specific data */
40074888Seh208807 	if (ZONE_IS_BRANDED(zone))
40084888Seh208807 		ZBROP(zone)->b_free_brand_data(zone);
40094888Seh208807 
40103671Ssl108498 	/* Say goodbye to brand framework. */
40113671Ssl108498 	brand_unregister_zone(zone->zone_brand);
40123671Ssl108498 
40130Sstevel@tonic-gate 	/*
40140Sstevel@tonic-gate 	 * It is now safe to let the zone be recreated; remove it from the
40150Sstevel@tonic-gate 	 * lists.  The memory will not be freed until the last cred
40160Sstevel@tonic-gate 	 * reference goes away.
40170Sstevel@tonic-gate 	 */
40180Sstevel@tonic-gate 	ASSERT(zonecount > 1);	/* must be > 1; can't destroy global zone */
40190Sstevel@tonic-gate 	zonecount--;
40200Sstevel@tonic-gate 	/* remove from active list and hash tables */
40210Sstevel@tonic-gate 	list_remove(&zone_active, zone);
40220Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyname,
40230Sstevel@tonic-gate 	    (mod_hash_key_t)zone->zone_name);
40240Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyid,
40250Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id);
40261769Scarlsonj 	if (zone->zone_flags & ZF_HASHED_LABEL)
40271676Sjpk 		(void) mod_hash_destroy(zonehashbylabel,
40281676Sjpk 		    (mod_hash_key_t)zone->zone_slabel);
40290Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
40300Sstevel@tonic-gate 
4031766Scarlsonj 	/*
4032766Scarlsonj 	 * Release the root vnode; we're not using it anymore.  Nor should any
4033766Scarlsonj 	 * other thread that might access it exist.
4034766Scarlsonj 	 */
4035766Scarlsonj 	if (zone->zone_rootvp != NULL) {
4036766Scarlsonj 		VN_RELE(zone->zone_rootvp);
4037766Scarlsonj 		zone->zone_rootvp = NULL;
4038766Scarlsonj 	}
4039766Scarlsonj 
40400Sstevel@tonic-gate 	/* add to deathrow list */
40410Sstevel@tonic-gate 	mutex_enter(&zone_deathrow_lock);
40420Sstevel@tonic-gate 	list_insert_tail(&zone_deathrow, zone);
40430Sstevel@tonic-gate 	mutex_exit(&zone_deathrow_lock);
40440Sstevel@tonic-gate 
40450Sstevel@tonic-gate 	/*
40460Sstevel@tonic-gate 	 * Drop last reference (which was added by zsched()), this will
40470Sstevel@tonic-gate 	 * free the zone unless there are outstanding cred references.
40480Sstevel@tonic-gate 	 */
40490Sstevel@tonic-gate 	zone_rele(zone);
40500Sstevel@tonic-gate 	return (0);
40510Sstevel@tonic-gate }
40520Sstevel@tonic-gate 
40530Sstevel@tonic-gate /*
40540Sstevel@tonic-gate  * Systemcall entry point for zone_getattr(2).
40550Sstevel@tonic-gate  */
40560Sstevel@tonic-gate static ssize_t
40570Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
40580Sstevel@tonic-gate {
40590Sstevel@tonic-gate 	size_t size;
40600Sstevel@tonic-gate 	int error = 0, err;
40610Sstevel@tonic-gate 	zone_t *zone;
40620Sstevel@tonic-gate 	char *zonepath;
40632267Sdp 	char *outstr;
40640Sstevel@tonic-gate 	zone_status_t zone_status;
40650Sstevel@tonic-gate 	pid_t initpid;
40663792Sakolb 	boolean_t global = (curzone == global_zone);
40673792Sakolb 	boolean_t inzone = (curzone->zone_id == zoneid);
40683448Sdh155122 	ushort_t flags;
40690Sstevel@tonic-gate 
40700Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
40710Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
40720Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
40730Sstevel@tonic-gate 		return (set_errno(EINVAL));
40740Sstevel@tonic-gate 	}
40750Sstevel@tonic-gate 	zone_status = zone_status_get(zone);
40760Sstevel@tonic-gate 	if (zone_status < ZONE_IS_READY) {
40770Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
40780Sstevel@tonic-gate 		return (set_errno(EINVAL));
40790Sstevel@tonic-gate 	}
40800Sstevel@tonic-gate 	zone_hold(zone);
40810Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
40820Sstevel@tonic-gate 
40830Sstevel@tonic-gate 	/*
40841676Sjpk 	 * If not in the global zone, don't show information about other zones,
40851676Sjpk 	 * unless the system is labeled and the local zone's label dominates
40861676Sjpk 	 * the other zone.
40870Sstevel@tonic-gate 	 */
40881676Sjpk 	if (!zone_list_access(zone)) {
40890Sstevel@tonic-gate 		zone_rele(zone);
40900Sstevel@tonic-gate 		return (set_errno(EINVAL));
40910Sstevel@tonic-gate 	}
40920Sstevel@tonic-gate 
40930Sstevel@tonic-gate 	switch (attr) {
40940Sstevel@tonic-gate 	case ZONE_ATTR_ROOT:
40950Sstevel@tonic-gate 		if (global) {
40960Sstevel@tonic-gate 			/*
40970Sstevel@tonic-gate 			 * Copy the path to trim the trailing "/" (except for
40980Sstevel@tonic-gate 			 * the global zone).
40990Sstevel@tonic-gate 			 */
41000Sstevel@tonic-gate 			if (zone != global_zone)
41010Sstevel@tonic-gate 				size = zone->zone_rootpathlen - 1;
41020Sstevel@tonic-gate 			else
41030Sstevel@tonic-gate 				size = zone->zone_rootpathlen;
41040Sstevel@tonic-gate 			zonepath = kmem_alloc(size, KM_SLEEP);
41050Sstevel@tonic-gate 			bcopy(zone->zone_rootpath, zonepath, size);
41060Sstevel@tonic-gate 			zonepath[size - 1] = '\0';
41070Sstevel@tonic-gate 		} else {
41083792Sakolb 			if (inzone || !is_system_labeled()) {
41091676Sjpk 				/*
41101676Sjpk 				 * Caller is not in the global zone.
41111676Sjpk 				 * if the query is on the current zone
41121676Sjpk 				 * or the system is not labeled,
41131676Sjpk 				 * just return faked-up path for current zone.
41141676Sjpk 				 */
41151676Sjpk 				zonepath = "/";
41161676Sjpk 				size = 2;
41171676Sjpk 			} else {
41181676Sjpk 				/*
41191676Sjpk 				 * Return related path for current zone.
41201676Sjpk 				 */
41211676Sjpk 				int prefix_len = strlen(zone_prefix);
41221676Sjpk 				int zname_len = strlen(zone->zone_name);
41231676Sjpk 
41241676Sjpk 				size = prefix_len + zname_len + 1;
41251676Sjpk 				zonepath = kmem_alloc(size, KM_SLEEP);
41261676Sjpk 				bcopy(zone_prefix, zonepath, prefix_len);
41271676Sjpk 				bcopy(zone->zone_name, zonepath +
41282267Sdp 				    prefix_len, zname_len);
41291676Sjpk 				zonepath[size - 1] = '\0';
41301676Sjpk 			}
41310Sstevel@tonic-gate 		}
41320Sstevel@tonic-gate 		if (bufsize > size)
41330Sstevel@tonic-gate 			bufsize = size;
41340Sstevel@tonic-gate 		if (buf != NULL) {
41350Sstevel@tonic-gate 			err = copyoutstr(zonepath, buf, bufsize, NULL);
41360Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
41370Sstevel@tonic-gate 				error = EFAULT;
41380Sstevel@tonic-gate 		}
41393792Sakolb 		if (global || (is_system_labeled() && !inzone))
41400Sstevel@tonic-gate 			kmem_free(zonepath, size);
41410Sstevel@tonic-gate 		break;
41420Sstevel@tonic-gate 
41430Sstevel@tonic-gate 	case ZONE_ATTR_NAME:
41440Sstevel@tonic-gate 		size = strlen(zone->zone_name) + 1;
41450Sstevel@tonic-gate 		if (bufsize > size)
41460Sstevel@tonic-gate 			bufsize = size;
41470Sstevel@tonic-gate 		if (buf != NULL) {
41480Sstevel@tonic-gate 			err = copyoutstr(zone->zone_name, buf, bufsize, NULL);
41490Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
41500Sstevel@tonic-gate 				error = EFAULT;
41510Sstevel@tonic-gate 		}
41520Sstevel@tonic-gate 		break;
41530Sstevel@tonic-gate 
41540Sstevel@tonic-gate 	case ZONE_ATTR_STATUS:
41550Sstevel@tonic-gate 		/*
41560Sstevel@tonic-gate 		 * Since we're not holding zonehash_lock, the zone status
41570Sstevel@tonic-gate 		 * may be anything; leave it up to userland to sort it out.
41580Sstevel@tonic-gate 		 */
41590Sstevel@tonic-gate 		size = sizeof (zone_status);
41600Sstevel@tonic-gate 		if (bufsize > size)
41610Sstevel@tonic-gate 			bufsize = size;
41620Sstevel@tonic-gate 		zone_status = zone_status_get(zone);
41630Sstevel@tonic-gate 		if (buf != NULL &&
41640Sstevel@tonic-gate 		    copyout(&zone_status, buf, bufsize) != 0)
41650Sstevel@tonic-gate 			error = EFAULT;
41660Sstevel@tonic-gate 		break;
41673448Sdh155122 	case ZONE_ATTR_FLAGS:
41683448Sdh155122 		size = sizeof (zone->zone_flags);
41693448Sdh155122 		if (bufsize > size)
41703448Sdh155122 			bufsize = size;
41713448Sdh155122 		flags = zone->zone_flags;
41723448Sdh155122 		if (buf != NULL &&
41733448Sdh155122 		    copyout(&flags, buf, bufsize) != 0)
41743448Sdh155122 			error = EFAULT;
41753448Sdh155122 		break;
41760Sstevel@tonic-gate 	case ZONE_ATTR_PRIVSET:
41770Sstevel@tonic-gate 		size = sizeof (priv_set_t);
41780Sstevel@tonic-gate 		if (bufsize > size)
41790Sstevel@tonic-gate 			bufsize = size;
41800Sstevel@tonic-gate 		if (buf != NULL &&
41810Sstevel@tonic-gate 		    copyout(zone->zone_privset, buf, bufsize) != 0)
41820Sstevel@tonic-gate 			error = EFAULT;
41830Sstevel@tonic-gate 		break;
41840Sstevel@tonic-gate 	case ZONE_ATTR_UNIQID:
41850Sstevel@tonic-gate 		size = sizeof (zone->zone_uniqid);
41860Sstevel@tonic-gate 		if (bufsize > size)
41870Sstevel@tonic-gate 			bufsize = size;
41880Sstevel@tonic-gate 		if (buf != NULL &&
41890Sstevel@tonic-gate 		    copyout(&zone->zone_uniqid, buf, bufsize) != 0)
41900Sstevel@tonic-gate 			error = EFAULT;
41910Sstevel@tonic-gate 		break;
41920Sstevel@tonic-gate 	case ZONE_ATTR_POOLID:
41930Sstevel@tonic-gate 		{
41940Sstevel@tonic-gate 			pool_t *pool;
41950Sstevel@tonic-gate 			poolid_t poolid;
41960Sstevel@tonic-gate 
41970Sstevel@tonic-gate 			if (pool_lock_intr() != 0) {
41980Sstevel@tonic-gate 				error = EINTR;
41990Sstevel@tonic-gate 				break;
42000Sstevel@tonic-gate 			}
42010Sstevel@tonic-gate 			pool = zone_pool_get(zone);
42020Sstevel@tonic-gate 			poolid = pool->pool_id;
42030Sstevel@tonic-gate 			pool_unlock();
42040Sstevel@tonic-gate 			size = sizeof (poolid);
42050Sstevel@tonic-gate 			if (bufsize > size)
42060Sstevel@tonic-gate 				bufsize = size;
42070Sstevel@tonic-gate 			if (buf != NULL && copyout(&poolid, buf, size) != 0)
42080Sstevel@tonic-gate 				error = EFAULT;
42090Sstevel@tonic-gate 		}
42100Sstevel@tonic-gate 		break;
42111676Sjpk 	case ZONE_ATTR_SLBL:
42121676Sjpk 		size = sizeof (bslabel_t);
42131676Sjpk 		if (bufsize > size)
42141676Sjpk 			bufsize = size;
42151676Sjpk 		if (zone->zone_slabel == NULL)
42161676Sjpk 			error = EINVAL;
42171676Sjpk 		else if (buf != NULL &&
42181676Sjpk 		    copyout(label2bslabel(zone->zone_slabel), buf,
42191676Sjpk 		    bufsize) != 0)
42201676Sjpk 			error = EFAULT;
42211676Sjpk 		break;
42220Sstevel@tonic-gate 	case ZONE_ATTR_INITPID:
42230Sstevel@tonic-gate 		size = sizeof (initpid);
42240Sstevel@tonic-gate 		if (bufsize > size)
42250Sstevel@tonic-gate 			bufsize = size;
42260Sstevel@tonic-gate 		initpid = zone->zone_proc_initpid;
42270Sstevel@tonic-gate 		if (initpid == -1) {
42280Sstevel@tonic-gate 			error = ESRCH;
42290Sstevel@tonic-gate 			break;
42300Sstevel@tonic-gate 		}
42310Sstevel@tonic-gate 		if (buf != NULL &&
42320Sstevel@tonic-gate 		    copyout(&initpid, buf, bufsize) != 0)
42330Sstevel@tonic-gate 			error = EFAULT;
42340Sstevel@tonic-gate 		break;
42352712Snn35248 	case ZONE_ATTR_BRAND:
42362712Snn35248 		size = strlen(zone->zone_brand->b_name) + 1;
42372712Snn35248 
42382712Snn35248 		if (bufsize > size)
42392712Snn35248 			bufsize = size;
42402712Snn35248 		if (buf != NULL) {
42412712Snn35248 			err = copyoutstr(zone->zone_brand->b_name, buf,
42422712Snn35248 			    bufsize, NULL);
42432712Snn35248 			if (err != 0 && err != ENAMETOOLONG)
42442712Snn35248 				error = EFAULT;
42452712Snn35248 		}
42462712Snn35248 		break;
42472267Sdp 	case ZONE_ATTR_INITNAME:
42482267Sdp 		size = strlen(zone->zone_initname) + 1;
42492267Sdp 		if (bufsize > size)
42502267Sdp 			bufsize = size;
42512267Sdp 		if (buf != NULL) {
42522267Sdp 			err = copyoutstr(zone->zone_initname, buf, bufsize,
42532267Sdp 			    NULL);
42542267Sdp 			if (err != 0 && err != ENAMETOOLONG)
42552267Sdp 				error = EFAULT;
42562267Sdp 		}
42572267Sdp 		break;
42582267Sdp 	case ZONE_ATTR_BOOTARGS:
42592267Sdp 		if (zone->zone_bootargs == NULL)
42602267Sdp 			outstr = "";
42612267Sdp 		else
42622267Sdp 			outstr = zone->zone_bootargs;
42632267Sdp 		size = strlen(outstr) + 1;
42642267Sdp 		if (bufsize > size)
42652267Sdp 			bufsize = size;
42662267Sdp 		if (buf != NULL) {
42672267Sdp 			err = copyoutstr(outstr, buf, bufsize, NULL);
42682267Sdp 			if (err != 0 && err != ENAMETOOLONG)
42692267Sdp 				error = EFAULT;
42702267Sdp 		}
42712267Sdp 		break;
42723247Sgjelinek 	case ZONE_ATTR_PHYS_MCAP:
42733247Sgjelinek 		size = sizeof (zone->zone_phys_mcap);
42743247Sgjelinek 		if (bufsize > size)
42753247Sgjelinek 			bufsize = size;
42763247Sgjelinek 		if (buf != NULL &&
42773247Sgjelinek 		    copyout(&zone->zone_phys_mcap, buf, bufsize) != 0)
42783247Sgjelinek 			error = EFAULT;
42793247Sgjelinek 		break;
42803247Sgjelinek 	case ZONE_ATTR_SCHED_CLASS:
42813247Sgjelinek 		mutex_enter(&class_lock);
42823247Sgjelinek 
42833247Sgjelinek 		if (zone->zone_defaultcid >= loaded_classes)
42843247Sgjelinek 			outstr = "";
42853247Sgjelinek 		else
42863247Sgjelinek 			outstr = sclass[zone->zone_defaultcid].cl_name;
42873247Sgjelinek 		size = strlen(outstr) + 1;
42883247Sgjelinek 		if (bufsize > size)
42893247Sgjelinek 			bufsize = size;
42903247Sgjelinek 		if (buf != NULL) {
42913247Sgjelinek 			err = copyoutstr(outstr, buf, bufsize, NULL);
42923247Sgjelinek 			if (err != 0 && err != ENAMETOOLONG)
42933247Sgjelinek 				error = EFAULT;
42943247Sgjelinek 		}
42953247Sgjelinek 
42963247Sgjelinek 		mutex_exit(&class_lock);
42973247Sgjelinek 		break;
42980Sstevel@tonic-gate 	default:
42992712Snn35248 		if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) {
43002712Snn35248 			size = bufsize;
43012712Snn35248 			error = ZBROP(zone)->b_getattr(zone, attr, buf, &size);
43022712Snn35248 		} else {
43032712Snn35248 			error = EINVAL;
43042712Snn35248 		}
43050Sstevel@tonic-gate 	}
43060Sstevel@tonic-gate 	zone_rele(zone);
43070Sstevel@tonic-gate 
43080Sstevel@tonic-gate 	if (error)
43090Sstevel@tonic-gate 		return (set_errno(error));
43100Sstevel@tonic-gate 	return ((ssize_t)size);
43110Sstevel@tonic-gate }
43120Sstevel@tonic-gate 
43130Sstevel@tonic-gate /*
43142267Sdp  * Systemcall entry point for zone_setattr(2).
43152267Sdp  */
43162267Sdp /*ARGSUSED*/
43172267Sdp static int
43182267Sdp zone_setattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
43192267Sdp {
43202267Sdp 	zone_t *zone;
43212267Sdp 	zone_status_t zone_status;
43222267Sdp 	int err;
43232267Sdp 
43242267Sdp 	if (secpolicy_zone_config(CRED()) != 0)
43252267Sdp 		return (set_errno(EPERM));
43262267Sdp 
43272267Sdp 	/*
43283247Sgjelinek 	 * Only the ZONE_ATTR_PHYS_MCAP attribute can be set on the
43293247Sgjelinek 	 * global zone.
43302267Sdp 	 */
43313247Sgjelinek 	if (zoneid == GLOBAL_ZONEID && attr != ZONE_ATTR_PHYS_MCAP) {
43322267Sdp 		return (set_errno(EINVAL));
43332267Sdp 	}
43342267Sdp 
43352267Sdp 	mutex_enter(&zonehash_lock);
43362267Sdp 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
43372267Sdp 		mutex_exit(&zonehash_lock);
43382267Sdp 		return (set_errno(EINVAL));
43392267Sdp 	}
43402267Sdp 	zone_hold(zone);
43412267Sdp 	mutex_exit(&zonehash_lock);
43422267Sdp 
43433247Sgjelinek 	/*
43443247Sgjelinek 	 * At present most attributes can only be set on non-running,
43453247Sgjelinek 	 * non-global zones.
43463247Sgjelinek 	 */
43472267Sdp 	zone_status = zone_status_get(zone);
43483247Sgjelinek 	if (attr != ZONE_ATTR_PHYS_MCAP && zone_status > ZONE_IS_READY)
43492267Sdp 		goto done;
43502267Sdp 
43512267Sdp 	switch (attr) {
43522267Sdp 	case ZONE_ATTR_INITNAME:
43532267Sdp 		err = zone_set_initname(zone, (const char *)buf);
43542267Sdp 		break;
43552267Sdp 	case ZONE_ATTR_BOOTARGS:
43562267Sdp 		err = zone_set_bootargs(zone, (const char *)buf);
43572267Sdp 		break;
43582712Snn35248 	case ZONE_ATTR_BRAND:
43594141Sedp 		err = zone_set_brand(zone, (const char *)buf);
43602712Snn35248 		break;
43613247Sgjelinek 	case ZONE_ATTR_PHYS_MCAP:
43623247Sgjelinek 		err = zone_set_phys_mcap(zone, (const uint64_t *)buf);
43633247Sgjelinek 		break;
43643247Sgjelinek 	case ZONE_ATTR_SCHED_CLASS:
43653247Sgjelinek 		err = zone_set_sched_class(zone, (const char *)buf);
43663247Sgjelinek 		break;
43672267Sdp 	default:
43682712Snn35248 		if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone))
43692712Snn35248 			err = ZBROP(zone)->b_setattr(zone, attr, buf, bufsize);
43702712Snn35248 		else
43712712Snn35248 			err = EINVAL;
43722267Sdp 	}
43732267Sdp 
43742267Sdp done:
43752267Sdp 	zone_rele(zone);
43762267Sdp 	return (err != 0 ? set_errno(err) : 0);
43772267Sdp }
43782267Sdp 
43792267Sdp /*
43800Sstevel@tonic-gate  * Return zero if the process has at least one vnode mapped in to its
43810Sstevel@tonic-gate  * address space which shouldn't be allowed to change zones.
43823247Sgjelinek  *
43833247Sgjelinek  * Also return zero if the process has any shared mappings which reserve
43843247Sgjelinek  * swap.  This is because the counting for zone.max-swap does not allow swap
4385*5331Samw  * reservation to be shared between zones.  zone swap reservation is counted
43863247Sgjelinek  * on zone->zone_max_swap.
43870Sstevel@tonic-gate  */
43880Sstevel@tonic-gate static int
43890Sstevel@tonic-gate as_can_change_zones(void)
43900Sstevel@tonic-gate {
43910Sstevel@tonic-gate 	proc_t *pp = curproc;
43920Sstevel@tonic-gate 	struct seg *seg;
43930Sstevel@tonic-gate 	struct as *as = pp->p_as;
43940Sstevel@tonic-gate 	vnode_t *vp;
43950Sstevel@tonic-gate 	int allow = 1;
43960Sstevel@tonic-gate 
43970Sstevel@tonic-gate 	ASSERT(pp->p_as != &kas);
43983247Sgjelinek 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
43990Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
44003247Sgjelinek 
44013247Sgjelinek 		/*
44023247Sgjelinek 		 * Cannot enter zone with shared anon memory which
44033247Sgjelinek 		 * reserves swap.  See comment above.
44043247Sgjelinek 		 */
44053247Sgjelinek 		if (seg_can_change_zones(seg) == B_FALSE) {
44063247Sgjelinek 			allow = 0;
44073247Sgjelinek 			break;
44083247Sgjelinek 		}
44090Sstevel@tonic-gate 		/*
44100Sstevel@tonic-gate 		 * if we can't get a backing vnode for this segment then skip
44110Sstevel@tonic-gate 		 * it.
44120Sstevel@tonic-gate 		 */
44130Sstevel@tonic-gate 		vp = NULL;
44140Sstevel@tonic-gate 		if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL)
44150Sstevel@tonic-gate 			continue;
44160Sstevel@tonic-gate 		if (!vn_can_change_zones(vp)) { /* bail on first match */
44170Sstevel@tonic-gate 			allow = 0;
44180Sstevel@tonic-gate 			break;
44190Sstevel@tonic-gate 		}
44200Sstevel@tonic-gate 	}
44213247Sgjelinek 	AS_LOCK_EXIT(as, &as->a_lock);
44220Sstevel@tonic-gate 	return (allow);
44230Sstevel@tonic-gate }
44240Sstevel@tonic-gate 
44250Sstevel@tonic-gate /*
44263247Sgjelinek  * Count swap reserved by curproc's address space
44273247Sgjelinek  */
44283247Sgjelinek static size_t
44293247Sgjelinek as_swresv(void)
44303247Sgjelinek {
44313247Sgjelinek 	proc_t *pp = curproc;
44323247Sgjelinek 	struct seg *seg;
44333247Sgjelinek 	struct as *as = pp->p_as;
44343247Sgjelinek 	size_t swap = 0;
44353247Sgjelinek 
44363247Sgjelinek 	ASSERT(pp->p_as != &kas);
44373247Sgjelinek 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
44383247Sgjelinek 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg))
44393247Sgjelinek 		swap += seg_swresv(seg);
44403247Sgjelinek 
44413247Sgjelinek 	return (swap);
44423247Sgjelinek }
44433247Sgjelinek 
44443247Sgjelinek /*
44450Sstevel@tonic-gate  * Systemcall entry point for zone_enter().
44460Sstevel@tonic-gate  *
44470Sstevel@tonic-gate  * The current process is injected into said zone.  In the process
44480Sstevel@tonic-gate  * it will change its project membership, privileges, rootdir/cwd,
44490Sstevel@tonic-gate  * zone-wide rctls, and pool association to match those of the zone.
44500Sstevel@tonic-gate  *
44510Sstevel@tonic-gate  * The first zone_enter() called while the zone is in the ZONE_IS_READY
44520Sstevel@tonic-gate  * state will transition it to ZONE_IS_RUNNING.  Processes may only
44530Sstevel@tonic-gate  * enter a zone that is "ready" or "running".
44540Sstevel@tonic-gate  */
44550Sstevel@tonic-gate static int
44560Sstevel@tonic-gate zone_enter(zoneid_t zoneid)
44570Sstevel@tonic-gate {
44580Sstevel@tonic-gate 	zone_t *zone;
44590Sstevel@tonic-gate 	vnode_t *vp;
44600Sstevel@tonic-gate 	proc_t *pp = curproc;
44610Sstevel@tonic-gate 	contract_t *ct;
44620Sstevel@tonic-gate 	cont_process_t *ctp;
44630Sstevel@tonic-gate 	task_t *tk, *oldtk;
44640Sstevel@tonic-gate 	kproject_t *zone_proj0;
44650Sstevel@tonic-gate 	cred_t *cr, *newcr;
44660Sstevel@tonic-gate 	pool_t *oldpool, *newpool;
44670Sstevel@tonic-gate 	sess_t *sp;
44680Sstevel@tonic-gate 	uid_t uid;
44690Sstevel@tonic-gate 	zone_status_t status;
44700Sstevel@tonic-gate 	int err = 0;
44710Sstevel@tonic-gate 	rctl_entity_p_t e;
44723247Sgjelinek 	size_t swap;
44733792Sakolb 	kthread_id_t t;
44740Sstevel@tonic-gate 
44750Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
44760Sstevel@tonic-gate 		return (set_errno(EPERM));
44770Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
44780Sstevel@tonic-gate 		return (set_errno(EINVAL));
44790Sstevel@tonic-gate 
44800Sstevel@tonic-gate 	/*
44810Sstevel@tonic-gate 	 * Stop all lwps so we don't need to hold a lock to look at
44820Sstevel@tonic-gate 	 * curproc->p_zone.  This needs to happen before we grab any
44830Sstevel@tonic-gate 	 * locks to avoid deadlock (another lwp in the process could
44840Sstevel@tonic-gate 	 * be waiting for the held lock).
44850Sstevel@tonic-gate 	 */
44860Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK))
44870Sstevel@tonic-gate 		return (set_errno(EINTR));
44880Sstevel@tonic-gate 
44890Sstevel@tonic-gate 	/*
44900Sstevel@tonic-gate 	 * Make sure we're not changing zones with files open or mapped in
44910Sstevel@tonic-gate 	 * to our address space which shouldn't be changing zones.
44920Sstevel@tonic-gate 	 */
44930Sstevel@tonic-gate 	if (!files_can_change_zones()) {
44940Sstevel@tonic-gate 		err = EBADF;
44950Sstevel@tonic-gate 		goto out;
44960Sstevel@tonic-gate 	}
44970Sstevel@tonic-gate 	if (!as_can_change_zones()) {
44980Sstevel@tonic-gate 		err = EFAULT;
44990Sstevel@tonic-gate 		goto out;
45000Sstevel@tonic-gate 	}
45010Sstevel@tonic-gate 
45020Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
45030Sstevel@tonic-gate 	if (pp->p_zone != global_zone) {
45040Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
45050Sstevel@tonic-gate 		err = EINVAL;
45060Sstevel@tonic-gate 		goto out;
45070Sstevel@tonic-gate 	}
45080Sstevel@tonic-gate 
45090Sstevel@tonic-gate 	zone = zone_find_all_by_id(zoneid);
45100Sstevel@tonic-gate 	if (zone == NULL) {
45110Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
45120Sstevel@tonic-gate 		err = EINVAL;
45130Sstevel@tonic-gate 		goto out;
45140Sstevel@tonic-gate 	}
45150Sstevel@tonic-gate 
45160Sstevel@tonic-gate 	/*
45170Sstevel@tonic-gate 	 * To prevent processes in a zone from holding contracts on
45180Sstevel@tonic-gate 	 * extrazonal resources, and to avoid process contract
45190Sstevel@tonic-gate 	 * memberships which span zones, contract holders and processes
45200Sstevel@tonic-gate 	 * which aren't the sole members of their encapsulating process
45210Sstevel@tonic-gate 	 * contracts are not allowed to zone_enter.
45220Sstevel@tonic-gate 	 */
45230Sstevel@tonic-gate 	ctp = pp->p_ct_process;
45240Sstevel@tonic-gate 	ct = &ctp->conp_contract;
45250Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
45260Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
45270Sstevel@tonic-gate 	if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) {
45280Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
45290Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
45300Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
45310Sstevel@tonic-gate 		err = EINVAL;
45320Sstevel@tonic-gate 		goto out;
45330Sstevel@tonic-gate 	}
45340Sstevel@tonic-gate 
45350Sstevel@tonic-gate 	/*
45360Sstevel@tonic-gate 	 * Moreover, we don't allow processes whose encapsulating
45370Sstevel@tonic-gate 	 * process contracts have inherited extrazonal contracts.
45380Sstevel@tonic-gate 	 * While it would be easier to eliminate all process contracts
45390Sstevel@tonic-gate 	 * with inherited contracts, we need to be able to give a
45400Sstevel@tonic-gate 	 * restarted init (or other zone-penetrating process) its
45410Sstevel@tonic-gate 	 * predecessor's contracts.
45420Sstevel@tonic-gate 	 */
45430Sstevel@tonic-gate 	if (ctp->conp_ninherited != 0) {
45440Sstevel@tonic-gate 		contract_t *next;
45450Sstevel@tonic-gate 		for (next = list_head(&ctp->conp_inherited); next;
45460Sstevel@tonic-gate 		    next = list_next(&ctp->conp_inherited, next)) {
45470Sstevel@tonic-gate 			if (contract_getzuniqid(next) != zone->zone_uniqid) {
45480Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
45490Sstevel@tonic-gate 				mutex_exit(&ct->ct_lock);
45500Sstevel@tonic-gate 				mutex_exit(&zonehash_lock);
45510Sstevel@tonic-gate 				err = EINVAL;
45520Sstevel@tonic-gate 				goto out;
45530Sstevel@tonic-gate 			}
45540Sstevel@tonic-gate 		}
45550Sstevel@tonic-gate 	}
45560Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
45570Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
45580Sstevel@tonic-gate 
45590Sstevel@tonic-gate 	status = zone_status_get(zone);
45600Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) {
45610Sstevel@tonic-gate 		/*
45620Sstevel@tonic-gate 		 * Can't join
45630Sstevel@tonic-gate 		 */
45640Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
45650Sstevel@tonic-gate 		err = EINVAL;
45660Sstevel@tonic-gate 		goto out;
45670Sstevel@tonic-gate 	}
45680Sstevel@tonic-gate 
45690Sstevel@tonic-gate 	/*
45700Sstevel@tonic-gate 	 * Make sure new priv set is within the permitted set for caller
45710Sstevel@tonic-gate 	 */
45720Sstevel@tonic-gate 	if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) {
45730Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
45740Sstevel@tonic-gate 		err = EPERM;
45750Sstevel@tonic-gate 		goto out;
45760Sstevel@tonic-gate 	}
45770Sstevel@tonic-gate 	/*
45780Sstevel@tonic-gate 	 * We want to momentarily drop zonehash_lock while we optimistically
45790Sstevel@tonic-gate 	 * bind curproc to the pool it should be running in.  This is safe
45800Sstevel@tonic-gate 	 * since the zone can't disappear (we have a hold on it).
45810Sstevel@tonic-gate 	 */
45820Sstevel@tonic-gate 	zone_hold(zone);
45830Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
45840Sstevel@tonic-gate 
45850Sstevel@tonic-gate 	/*
45860Sstevel@tonic-gate 	 * Grab pool_lock to keep the pools configuration from changing
45870Sstevel@tonic-gate 	 * and to stop ourselves from getting rebound to another pool
45880Sstevel@tonic-gate 	 * until we join the zone.
45890Sstevel@tonic-gate 	 */
45900Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
45910Sstevel@tonic-gate 		zone_rele(zone);
45920Sstevel@tonic-gate 		err = EINTR;
45930Sstevel@tonic-gate 		goto out;
45940Sstevel@tonic-gate 	}
45950Sstevel@tonic-gate 	ASSERT(secpolicy_pool(CRED()) == 0);
45960Sstevel@tonic-gate 	/*
45970Sstevel@tonic-gate 	 * Bind ourselves to the pool currently associated with the zone.
45980Sstevel@tonic-gate 	 */
45990Sstevel@tonic-gate 	oldpool = curproc->p_pool;
46000Sstevel@tonic-gate 	newpool = zone_pool_get(zone);
46010Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED && newpool != oldpool &&
46020Sstevel@tonic-gate 	    (err = pool_do_bind(newpool, P_PID, P_MYID,
46030Sstevel@tonic-gate 	    POOL_BIND_ALL)) != 0) {
46040Sstevel@tonic-gate 		pool_unlock();
46050Sstevel@tonic-gate 		zone_rele(zone);
46060Sstevel@tonic-gate 		goto out;
46070Sstevel@tonic-gate 	}
46080Sstevel@tonic-gate 
46090Sstevel@tonic-gate 	/*
46100Sstevel@tonic-gate 	 * Grab cpu_lock now; we'll need it later when we call
46110Sstevel@tonic-gate 	 * task_join().
46120Sstevel@tonic-gate 	 */
46130Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
46140Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
46150Sstevel@tonic-gate 	/*
46160Sstevel@tonic-gate 	 * Make sure the zone hasn't moved on since we dropped zonehash_lock.
46170Sstevel@tonic-gate 	 */
46180Sstevel@tonic-gate 	if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
46190Sstevel@tonic-gate 		/*
46200Sstevel@tonic-gate 		 * Can't join anymore.
46210Sstevel@tonic-gate 		 */
46220Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
46230Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
46240Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED &&
46250Sstevel@tonic-gate 		    newpool != oldpool)
46260Sstevel@tonic-gate 			(void) pool_do_bind(oldpool, P_PID, P_MYID,
46270Sstevel@tonic-gate 			    POOL_BIND_ALL);
46280Sstevel@tonic-gate 		pool_unlock();
46290Sstevel@tonic-gate 		zone_rele(zone);
46300Sstevel@tonic-gate 		err = EINVAL;
46310Sstevel@tonic-gate 		goto out;
46320Sstevel@tonic-gate 	}
46330Sstevel@tonic-gate 
46343247Sgjelinek 	/*
46353247Sgjelinek 	 * a_lock must be held while transfering locked memory and swap
46363247Sgjelinek 	 * reservation from the global zone to the non global zone because
46373247Sgjelinek 	 * asynchronous faults on the processes' address space can lock
46383247Sgjelinek 	 * memory and reserve swap via MCL_FUTURE and MAP_NORESERVE
46393247Sgjelinek 	 * segments respectively.
46403247Sgjelinek 	 */
46413247Sgjelinek 	AS_LOCK_ENTER(pp->as, &pp->p_as->a_lock, RW_WRITER);
46423247Sgjelinek 	swap = as_swresv();
46430Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
46440Sstevel@tonic-gate 	zone_proj0 = zone->zone_zsched->p_task->tk_proj;
46450Sstevel@tonic-gate 	/* verify that we do not exceed and task or lwp limits */
46460Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
46470Sstevel@tonic-gate 	/* add new lwps to zone and zone's proj0 */
46480Sstevel@tonic-gate 	zone_proj0->kpj_nlwps += pp->p_lwpcnt;
46490Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
46500Sstevel@tonic-gate 	/* add 1 task to zone's proj0 */
46510Sstevel@tonic-gate 	zone_proj0->kpj_ntasks += 1;
46520Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
46530Sstevel@tonic-gate 
46543247Sgjelinek 	mutex_enter(&zone->zone_mem_lock);
46552768Ssl108498 	zone->zone_locked_mem += pp->p_locked_mem;
46562768Ssl108498 	zone_proj0->kpj_data.kpd_locked_mem += pp->p_locked_mem;
46573247Sgjelinek 	zone->zone_max_swap += swap;
46583247Sgjelinek 	mutex_exit(&zone->zone_mem_lock);
46592768Ssl108498 
46603916Skrishna 	mutex_enter(&(zone_proj0->kpj_data.kpd_crypto_lock));
46613916Skrishna 	zone_proj0->kpj_data.kpd_crypto_mem += pp->p_crypto_mem;
46623916Skrishna 	mutex_exit(&(zone_proj0->kpj_data.kpd_crypto_lock));
46633916Skrishna 
46640Sstevel@tonic-gate 	/* remove lwps from proc's old zone and old project */
46650Sstevel@tonic-gate 	mutex_enter(&pp->p_zone->zone_nlwps_lock);
46660Sstevel@tonic-gate 	pp->p_zone->zone_nlwps -= pp->p_lwpcnt;
46670Sstevel@tonic-gate 	pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt;
46680Sstevel@tonic-gate 	mutex_exit(&pp->p_zone->zone_nlwps_lock);
46690Sstevel@tonic-gate 
46703247Sgjelinek 	mutex_enter(&pp->p_zone->zone_mem_lock);
46712768Ssl108498 	pp->p_zone->zone_locked_mem -= pp->p_locked_mem;
46722768Ssl108498 	pp->p_task->tk_proj->kpj_data.kpd_locked_mem -= pp->p_locked_mem;
46733247Sgjelinek 	pp->p_zone->zone_max_swap -= swap;
46743247Sgjelinek 	mutex_exit(&pp->p_zone->zone_mem_lock);
46752768Ssl108498 
46763916Skrishna 	mutex_enter(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock));
46773916Skrishna 	pp->p_task->tk_proj->kpj_data.kpd_crypto_mem -= pp->p_crypto_mem;
46783916Skrishna 	mutex_exit(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock));
46793916Skrishna 
46802768Ssl108498 	mutex_exit(&pp->p_lock);
46813247Sgjelinek 	AS_LOCK_EXIT(pp->p_as, &pp->p_as->a_lock);
46822768Ssl108498 
46830Sstevel@tonic-gate 	/*
46840Sstevel@tonic-gate 	 * Joining the zone cannot fail from now on.
46850Sstevel@tonic-gate 	 *
46860Sstevel@tonic-gate 	 * This means that a lot of the following code can be commonized and
46870Sstevel@tonic-gate 	 * shared with zsched().
46880Sstevel@tonic-gate 	 */
46890Sstevel@tonic-gate 
46900Sstevel@tonic-gate 	/*
46910Sstevel@tonic-gate 	 * Reset the encapsulating process contract's zone.
46920Sstevel@tonic-gate 	 */
46930Sstevel@tonic-gate 	ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID);
46940Sstevel@tonic-gate 	contract_setzuniqid(ct, zone->zone_uniqid);
46950Sstevel@tonic-gate 
46960Sstevel@tonic-gate 	/*
46970Sstevel@tonic-gate 	 * Create a new task and associate the process with the project keyed
46980Sstevel@tonic-gate 	 * by (projid,zoneid).
46990Sstevel@tonic-gate 	 *
47000Sstevel@tonic-gate 	 * We might as well be in project 0; the global zone's projid doesn't
47010Sstevel@tonic-gate 	 * make much sense in a zone anyhow.
47020Sstevel@tonic-gate 	 *
47030Sstevel@tonic-gate 	 * This also increments zone_ntasks, and returns with p_lock held.
47040Sstevel@tonic-gate 	 */
47050Sstevel@tonic-gate 	tk = task_create(0, zone);
47060Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
47070Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
47080Sstevel@tonic-gate 
47090Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
47100Sstevel@tonic-gate 	pp->p_zone = zone;
47110Sstevel@tonic-gate 
47120Sstevel@tonic-gate 	/*
47130Sstevel@tonic-gate 	 * call RCTLOP_SET functions on this proc
47140Sstevel@tonic-gate 	 */
47150Sstevel@tonic-gate 	e.rcep_p.zone = zone;
47160Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
47170Sstevel@tonic-gate 	(void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL,
47180Sstevel@tonic-gate 	    RCD_CALLBACK);
47190Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
47200Sstevel@tonic-gate 
47210Sstevel@tonic-gate 	/*
47220Sstevel@tonic-gate 	 * We don't need to hold any of zsched's locks here; not only do we know
47230Sstevel@tonic-gate 	 * the process and zone aren't going away, we know its session isn't
47240Sstevel@tonic-gate 	 * changing either.
47250Sstevel@tonic-gate 	 *
47260Sstevel@tonic-gate 	 * By joining zsched's session here, we mimic the behavior in the
47270Sstevel@tonic-gate 	 * global zone of init's sid being the pid of sched.  We extend this
47280Sstevel@tonic-gate 	 * to all zlogin-like zone_enter()'ing processes as well.
47290Sstevel@tonic-gate 	 */
47300Sstevel@tonic-gate 	mutex_enter(&pidlock);
47310Sstevel@tonic-gate 	sp = zone->zone_zsched->p_sessp;
47322712Snn35248 	sess_hold(zone->zone_zsched);
47330Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
47340Sstevel@tonic-gate 	pgexit(pp);
47352712Snn35248 	sess_rele(pp->p_sessp, B_TRUE);
47360Sstevel@tonic-gate 	pp->p_sessp = sp;
47370Sstevel@tonic-gate 	pgjoin(pp, zone->zone_zsched->p_pidp);
47383247Sgjelinek 
47393247Sgjelinek 	/*
47403792Sakolb 	 * If any threads are scheduled to be placed on zone wait queue they
47413792Sakolb 	 * should abandon the idea since the wait queue is changing.
47423792Sakolb 	 * We need to be holding pidlock & p_lock to do this.
47433792Sakolb 	 */
47443792Sakolb 	if ((t = pp->p_tlist) != NULL) {
47453792Sakolb 		do {
47463792Sakolb 			thread_lock(t);
47473792Sakolb 			/*
47483792Sakolb 			 * Kick this thread so that he doesn't sit
47493792Sakolb 			 * on a wrong wait queue.
47503792Sakolb 			 */
47513792Sakolb 			if (ISWAITING(t))
47523792Sakolb 				setrun_locked(t);
47533792Sakolb 
47543792Sakolb 			if (t->t_schedflag & TS_ANYWAITQ)
47553792Sakolb 				t->t_schedflag &= ~ TS_ANYWAITQ;
47563792Sakolb 
47573792Sakolb 			thread_unlock(t);
47583792Sakolb 		} while ((t = t->t_forw) != pp->p_tlist);
47593792Sakolb 	}
47603792Sakolb 
47613792Sakolb 	/*
47623247Sgjelinek 	 * If there is a default scheduling class for the zone and it is not
47633247Sgjelinek 	 * the class we are currently in, change all of the threads in the
47643247Sgjelinek 	 * process to the new class.  We need to be holding pidlock & p_lock
47653247Sgjelinek 	 * when we call parmsset so this is a good place to do it.
47663247Sgjelinek 	 */
47673247Sgjelinek 	if (zone->zone_defaultcid > 0 &&
47683247Sgjelinek 	    zone->zone_defaultcid != curthread->t_cid) {
47693247Sgjelinek 		pcparms_t pcparms;
47703247Sgjelinek 
47713247Sgjelinek 		pcparms.pc_cid = zone->zone_defaultcid;
47723247Sgjelinek 		pcparms.pc_clparms[0] = 0;
47733247Sgjelinek 
47743247Sgjelinek 		/*
47753247Sgjelinek 		 * If setting the class fails, we still want to enter the zone.
47763247Sgjelinek 		 */
47773247Sgjelinek 		if ((t = pp->p_tlist) != NULL) {
47783247Sgjelinek 			do {
47793247Sgjelinek 				(void) parmsset(&pcparms, t);
47803247Sgjelinek 			} while ((t = t->t_forw) != pp->p_tlist);
47813247Sgjelinek 		}
47823247Sgjelinek 	}
47833247Sgjelinek 
47840Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
47850Sstevel@tonic-gate 	mutex_exit(&pidlock);
47860Sstevel@tonic-gate 
47870Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
47880Sstevel@tonic-gate 	/*
47890Sstevel@tonic-gate 	 * We're firmly in the zone; let pools progress.
47900Sstevel@tonic-gate 	 */
47910Sstevel@tonic-gate 	pool_unlock();
47920Sstevel@tonic-gate 	task_rele(oldtk);
47930Sstevel@tonic-gate 	/*
47940Sstevel@tonic-gate 	 * We don't need to retain a hold on the zone since we already
47950Sstevel@tonic-gate 	 * incremented zone_ntasks, so the zone isn't going anywhere.
47960Sstevel@tonic-gate 	 */
47970Sstevel@tonic-gate 	zone_rele(zone);
47980Sstevel@tonic-gate 
47990Sstevel@tonic-gate 	/*
48000Sstevel@tonic-gate 	 * Chroot
48010Sstevel@tonic-gate 	 */
48020Sstevel@tonic-gate 	vp = zone->zone_rootvp;
48030Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_cdir, pp);
48040Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_rdir, pp);
48050Sstevel@tonic-gate 
48060Sstevel@tonic-gate 	/*
48070Sstevel@tonic-gate 	 * Change process credentials
48080Sstevel@tonic-gate 	 */
48090Sstevel@tonic-gate 	newcr = cralloc();
48100Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
48110Sstevel@tonic-gate 	cr = pp->p_cred;
48120Sstevel@tonic-gate 	crcopy_to(cr, newcr);
48130Sstevel@tonic-gate 	crsetzone(newcr, zone);
48140Sstevel@tonic-gate 	pp->p_cred = newcr;
48150Sstevel@tonic-gate 
48160Sstevel@tonic-gate 	/*
48170Sstevel@tonic-gate 	 * Restrict all process privilege sets to zone limit
48180Sstevel@tonic-gate 	 */
48190Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(newcr));
48200Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(newcr));
48210Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(newcr));
48220Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(newcr));
48230Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
48240Sstevel@tonic-gate 	crset(pp, newcr);
48250Sstevel@tonic-gate 
48260Sstevel@tonic-gate 	/*
48270Sstevel@tonic-gate 	 * Adjust upcount to reflect zone entry.
48280Sstevel@tonic-gate 	 */
48290Sstevel@tonic-gate 	uid = crgetruid(newcr);
48300Sstevel@tonic-gate 	mutex_enter(&pidlock);
48310Sstevel@tonic-gate 	upcount_dec(uid, GLOBAL_ZONEID);
48320Sstevel@tonic-gate 	upcount_inc(uid, zoneid);
48330Sstevel@tonic-gate 	mutex_exit(&pidlock);
48340Sstevel@tonic-gate 
48350Sstevel@tonic-gate 	/*
48360Sstevel@tonic-gate 	 * Set up core file path and content.
48370Sstevel@tonic-gate 	 */
48380Sstevel@tonic-gate 	set_core_defaults();
48390Sstevel@tonic-gate 
48400Sstevel@tonic-gate out:
48410Sstevel@tonic-gate 	/*
48420Sstevel@tonic-gate 	 * Let the other lwps continue.
48430Sstevel@tonic-gate 	 */
48440Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
48450Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
48460Sstevel@tonic-gate 		continuelwps(pp);
48470Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
48480Sstevel@tonic-gate 
48490Sstevel@tonic-gate 	return (err != 0 ? set_errno(err) : 0);
48500Sstevel@tonic-gate }
48510Sstevel@tonic-gate 
48520Sstevel@tonic-gate /*
48530Sstevel@tonic-gate  * Systemcall entry point for zone_list(2).
48540Sstevel@tonic-gate  *
48550Sstevel@tonic-gate  * Processes running in a (non-global) zone only see themselves.
48561676Sjpk  * On labeled systems, they see all zones whose label they dominate.
48570Sstevel@tonic-gate  */
48580Sstevel@tonic-gate static int
48590Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones)
48600Sstevel@tonic-gate {
48610Sstevel@tonic-gate 	zoneid_t *zoneids;
48621769Scarlsonj 	zone_t *zone, *myzone;
48630Sstevel@tonic-gate 	uint_t user_nzones, real_nzones;
48641676Sjpk 	uint_t domi_nzones;
48651676Sjpk 	int error;
48660Sstevel@tonic-gate 
48670Sstevel@tonic-gate 	if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0)
48680Sstevel@tonic-gate 		return (set_errno(EFAULT));
48690Sstevel@tonic-gate 
48701769Scarlsonj 	myzone = curproc->p_zone;
48711769Scarlsonj 	if (myzone != global_zone) {
48721676Sjpk 		bslabel_t *mybslab;
48731676Sjpk 
48741676Sjpk 		if (!is_system_labeled()) {
48751676Sjpk 			/* just return current zone */
48761676Sjpk 			real_nzones = domi_nzones = 1;
48771676Sjpk 			zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP);
48781769Scarlsonj 			zoneids[0] = myzone->zone_id;
48791676Sjpk 		} else {
48801676Sjpk 			/* return all zones that are dominated */
48811676Sjpk 			mutex_enter(&zonehash_lock);
48821676Sjpk 			real_nzones = zonecount;
48831676Sjpk 			domi_nzones = 0;
48841676Sjpk 			if (real_nzones > 0) {
48851676Sjpk 				zoneids = kmem_alloc(real_nzones *
48861676Sjpk 				    sizeof (zoneid_t), KM_SLEEP);
48871769Scarlsonj 				mybslab = label2bslabel(myzone->zone_slabel);
48881676Sjpk 				for (zone = list_head(&zone_active);
48891676Sjpk 				    zone != NULL;
48901676Sjpk 				    zone = list_next(&zone_active, zone)) {
48911676Sjpk 					if (zone->zone_id == GLOBAL_ZONEID)
48921676Sjpk 						continue;
48931769Scarlsonj 					if (zone != myzone &&
48941769Scarlsonj 					    (zone->zone_flags & ZF_IS_SCRATCH))
48951769Scarlsonj 						continue;
48961769Scarlsonj 					/*
48971769Scarlsonj 					 * Note that a label always dominates
48981769Scarlsonj 					 * itself, so myzone is always included
48991769Scarlsonj 					 * in the list.
49001769Scarlsonj 					 */
49011676Sjpk 					if (bldominates(mybslab,
49021676Sjpk 					    label2bslabel(zone->zone_slabel))) {
49031676Sjpk 						zoneids[domi_nzones++] =
49041676Sjpk 						    zone->zone_id;
49051676Sjpk 					}
49061676Sjpk 				}
49071676Sjpk 			}
49081676Sjpk 			mutex_exit(&zonehash_lock);
49091676Sjpk 		}
49100Sstevel@tonic-gate 	} else {
49110Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
49120Sstevel@tonic-gate 		real_nzones = zonecount;
49131676Sjpk 		domi_nzones = 0;
49141676Sjpk 		if (real_nzones > 0) {
49150Sstevel@tonic-gate 			zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t),
49160Sstevel@tonic-gate 			    KM_SLEEP);
49170Sstevel@tonic-gate 			for (zone = list_head(&zone_active); zone != NULL;
49180Sstevel@tonic-gate 			    zone = list_next(&zone_active, zone))
49191676Sjpk 				zoneids[domi_nzones++] = zone->zone_id;
49201676Sjpk 			ASSERT(domi_nzones == real_nzones);
49210Sstevel@tonic-gate 		}
49220Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
49230Sstevel@tonic-gate 	}
49240Sstevel@tonic-gate 
49251676Sjpk 	/*
49261676Sjpk 	 * If user has allocated space for fewer entries than we found, then
49271676Sjpk 	 * return only up to his limit.  Either way, tell him exactly how many
49281676Sjpk 	 * we found.
49291676Sjpk 	 */
49301676Sjpk 	if (domi_nzones < user_nzones)
49311676Sjpk 		user_nzones = domi_nzones;
49321676Sjpk 	error = 0;
49331676Sjpk 	if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) {
49340Sstevel@tonic-gate 		error = EFAULT;
49351676Sjpk 	} else if (zoneidlist != NULL && user_nzones != 0) {
49360Sstevel@tonic-gate 		if (copyout(zoneids, zoneidlist,
49370Sstevel@tonic-gate 		    user_nzones * sizeof (zoneid_t)) != 0)
49380Sstevel@tonic-gate 			error = EFAULT;
49390Sstevel@tonic-gate 	}
49400Sstevel@tonic-gate 
49411676Sjpk 	if (real_nzones > 0)
49420Sstevel@tonic-gate 		kmem_free(zoneids, real_nzones * sizeof (zoneid_t));
49430Sstevel@tonic-gate 
49441676Sjpk 	if (error != 0)
49450Sstevel@tonic-gate 		return (set_errno(error));
49460Sstevel@tonic-gate 	else
49470Sstevel@tonic-gate 		return (0);
49480Sstevel@tonic-gate }
49490Sstevel@tonic-gate 
49500Sstevel@tonic-gate /*
49510Sstevel@tonic-gate  * Systemcall entry point for zone_lookup(2).
49520Sstevel@tonic-gate  *
49531676Sjpk  * Non-global zones are only able to see themselves and (on labeled systems)
49541676Sjpk  * the zones they dominate.
49550Sstevel@tonic-gate  */
49560Sstevel@tonic-gate static zoneid_t
49570Sstevel@tonic-gate zone_lookup(const char *zone_name)
49580Sstevel@tonic-gate {
49590Sstevel@tonic-gate 	char *kname;
49600Sstevel@tonic-gate 	zone_t *zone;
49610Sstevel@tonic-gate 	zoneid_t zoneid;
49620Sstevel@tonic-gate 	int err;
49630Sstevel@tonic-gate 
49640Sstevel@tonic-gate 	if (zone_name == NULL) {
49650Sstevel@tonic-gate 		/* return caller's zone id */
49660Sstevel@tonic-gate 		return (getzoneid());
49670Sstevel@tonic-gate 	}
49680Sstevel@tonic-gate 
49690Sstevel@tonic-gate 	kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
49700Sstevel@tonic-gate 	if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) {
49710Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
49720Sstevel@tonic-gate 		return (set_errno(err));
49730Sstevel@tonic-gate 	}
49740Sstevel@tonic-gate 
49750Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
49760Sstevel@tonic-gate 	zone = zone_find_all_by_name(kname);
49770Sstevel@tonic-gate 	kmem_free(kname, ZONENAME_MAX);
49781676Sjpk 	/*
49791676Sjpk 	 * In a non-global zone, can only lookup global and own name.
49801676Sjpk 	 * In Trusted Extensions zone label dominance rules apply.
49811676Sjpk 	 */
49821676Sjpk 	if (zone == NULL ||
49831676Sjpk 	    zone_status_get(zone) < ZONE_IS_READY ||
49841676Sjpk 	    !zone_list_access(zone)) {
49850Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
49860Sstevel@tonic-gate 		return (set_errno(EINVAL));
49871676Sjpk 	} else {
49881676Sjpk 		zoneid = zone->zone_id;
49891676Sjpk 		mutex_exit(&zonehash_lock);
49901676Sjpk 		return (zoneid);
49910Sstevel@tonic-gate 	}
49920Sstevel@tonic-gate }
49930Sstevel@tonic-gate 
4994813Sdp static int
4995813Sdp zone_version(int *version_arg)
4996813Sdp {
4997813Sdp 	int version = ZONE_SYSCALL_API_VERSION;
4998813Sdp 
4999813Sdp 	if (copyout(&version, version_arg, sizeof (int)) != 0)
5000813Sdp 		return (set_errno(EFAULT));
5001813Sdp 	return (0);
5002813Sdp }
5003813Sdp 
50040Sstevel@tonic-gate /* ARGSUSED */
50050Sstevel@tonic-gate long
5006789Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4)
50070Sstevel@tonic-gate {
50080Sstevel@tonic-gate 	zone_def zs;
50090Sstevel@tonic-gate 
50100Sstevel@tonic-gate 	switch (cmd) {
50110Sstevel@tonic-gate 	case ZONE_CREATE:
50120Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
50130Sstevel@tonic-gate 			if (copyin(arg1, &zs, sizeof (zone_def))) {
50140Sstevel@tonic-gate 				return (set_errno(EFAULT));
50150Sstevel@tonic-gate 			}
50160Sstevel@tonic-gate 		} else {
50170Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
50180Sstevel@tonic-gate 			zone_def32 zs32;
50190Sstevel@tonic-gate 
50200Sstevel@tonic-gate 			if (copyin(arg1, &zs32, sizeof (zone_def32))) {
50210Sstevel@tonic-gate 				return (set_errno(EFAULT));
50220Sstevel@tonic-gate 			}
50230Sstevel@tonic-gate 			zs.zone_name =
50240Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_name;
50250Sstevel@tonic-gate 			zs.zone_root =
50260Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_root;
50270Sstevel@tonic-gate 			zs.zone_privs =
50280Sstevel@tonic-gate 			    (const struct priv_set *)
50290Sstevel@tonic-gate 			    (unsigned long)zs32.zone_privs;
50301409Sdp 			zs.zone_privssz = zs32.zone_privssz;
50310Sstevel@tonic-gate 			zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf;
50320Sstevel@tonic-gate 			zs.rctlbufsz = zs32.rctlbufsz;
5033789Sahrens 			zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf;
5034789Sahrens 			zs.zfsbufsz = zs32.zfsbufsz;
50350Sstevel@tonic-gate 			zs.extended_error =
50360Sstevel@tonic-gate 			    (int *)(unsigned long)zs32.extended_error;
50371676Sjpk 			zs.match = zs32.match;
50381676Sjpk 			zs.doi = zs32.doi;
50391676Sjpk 			zs.label = (const bslabel_t *)(uintptr_t)zs32.label;
50403448Sdh155122 			zs.flags = zs32.flags;
50410Sstevel@tonic-gate #else
50420Sstevel@tonic-gate 			panic("get_udatamodel() returned bogus result\n");
50430Sstevel@tonic-gate #endif
50440Sstevel@tonic-gate 		}
50450Sstevel@tonic-gate 
50460Sstevel@tonic-gate 		return (zone_create(zs.zone_name, zs.zone_root,
5047813Sdp 		    zs.zone_privs, zs.zone_privssz,
5048813Sdp 		    (caddr_t)zs.rctlbuf, zs.rctlbufsz,
5049813Sdp 		    (caddr_t)zs.zfsbuf, zs.zfsbufsz,
50501676Sjpk 		    zs.extended_error, zs.match, zs.doi,
50513448Sdh155122 		    zs.label, zs.flags));
50520Sstevel@tonic-gate 	case ZONE_BOOT:
50532267Sdp 		return (zone_boot((zoneid_t)(uintptr_t)arg1));
50540Sstevel@tonic-gate 	case ZONE_DESTROY:
50550Sstevel@tonic-gate 		return (zone_destroy((zoneid_t)(uintptr_t)arg1));
50560Sstevel@tonic-gate 	case ZONE_GETATTR:
50570Sstevel@tonic-gate 		return (zone_getattr((zoneid_t)(uintptr_t)arg1,
50580Sstevel@tonic-gate 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
50592267Sdp 	case ZONE_SETATTR:
50602267Sdp 		return (zone_setattr((zoneid_t)(uintptr_t)arg1,
50612267Sdp 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
50620Sstevel@tonic-gate 	case ZONE_ENTER:
50630Sstevel@tonic-gate 		return (zone_enter((zoneid_t)(uintptr_t)arg1));
50640Sstevel@tonic-gate 	case ZONE_LIST:
50650Sstevel@tonic-gate 		return (zone_list((zoneid_t *)arg1, (uint_t *)arg2));
50660Sstevel@tonic-gate 	case ZONE_SHUTDOWN:
50670Sstevel@tonic-gate 		return (zone_shutdown((zoneid_t)(uintptr_t)arg1));
50680Sstevel@tonic-gate 	case ZONE_LOOKUP:
50690Sstevel@tonic-gate 		return (zone_lookup((const char *)arg1));
5070813Sdp 	case ZONE_VERSION:
5071813Sdp 		return (zone_version((int *)arg1));
50723448Sdh155122 	case ZONE_ADD_DATALINK:
50733448Sdh155122 		return (zone_add_datalink((zoneid_t)(uintptr_t)arg1,
50743448Sdh155122 		    (char *)arg2));
50753448Sdh155122 	case ZONE_DEL_DATALINK:
50763448Sdh155122 		return (zone_remove_datalink((zoneid_t)(uintptr_t)arg1,
50773448Sdh155122 		    (char *)arg2));
50783448Sdh155122 	case ZONE_CHECK_DATALINK:
50793448Sdh155122 		return (zone_check_datalink((zoneid_t *)arg1, (char *)arg2));
50803448Sdh155122 	case ZONE_LIST_DATALINK:
50813448Sdh155122 		return (zone_list_datalink((zoneid_t)(uintptr_t)arg1,
50823448Sdh155122 		    (int *)arg2, (char *)arg3));
50830Sstevel@tonic-gate 	default:
50840Sstevel@tonic-gate 		return (set_errno(EINVAL));
50850Sstevel@tonic-gate 	}
50860Sstevel@tonic-gate }
50870Sstevel@tonic-gate 
50880Sstevel@tonic-gate struct zarg {
50890Sstevel@tonic-gate 	zone_t *zone;
50900Sstevel@tonic-gate 	zone_cmd_arg_t arg;
50910Sstevel@tonic-gate };
50920Sstevel@tonic-gate 
50930Sstevel@tonic-gate static int
50940Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp)
50950Sstevel@tonic-gate {
50960Sstevel@tonic-gate 	char *buf;
50970Sstevel@tonic-gate 	size_t buflen;
50980Sstevel@tonic-gate 	int error;
50990Sstevel@tonic-gate 
51000Sstevel@tonic-gate 	buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name);
51010Sstevel@tonic-gate 	buf = kmem_alloc(buflen, KM_SLEEP);
51020Sstevel@tonic-gate 	(void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name);
51030Sstevel@tonic-gate 	error = door_ki_open(buf, doorp);
51040Sstevel@tonic-gate 	kmem_free(buf, buflen);
51050Sstevel@tonic-gate 	return (error);
51060Sstevel@tonic-gate }
51070Sstevel@tonic-gate 
51080Sstevel@tonic-gate static void
51090Sstevel@tonic-gate zone_release_door(door_handle_t *doorp)
51100Sstevel@tonic-gate {
51110Sstevel@tonic-gate 	door_ki_rele(*doorp);
51120Sstevel@tonic-gate 	*doorp = NULL;
51130Sstevel@tonic-gate }
51140Sstevel@tonic-gate 
51150Sstevel@tonic-gate static void
51160Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp)
51170Sstevel@tonic-gate {
51180Sstevel@tonic-gate 	door_handle_t door = NULL;
51190Sstevel@tonic-gate 	door_arg_t darg, save_arg;
51200Sstevel@tonic-gate 	char *zone_name;
51210Sstevel@tonic-gate 	size_t zone_namelen;
51220Sstevel@tonic-gate 	zoneid_t zoneid;
51230Sstevel@tonic-gate 	zone_t *zone;
51240Sstevel@tonic-gate 	zone_cmd_arg_t arg;
51250Sstevel@tonic-gate 	uint64_t uniqid;
51260Sstevel@tonic-gate 	size_t size;
51270Sstevel@tonic-gate 	int error;
51280Sstevel@tonic-gate 	int retry;
51290Sstevel@tonic-gate 
51300Sstevel@tonic-gate 	zone = zargp->zone;
51310Sstevel@tonic-gate 	arg = zargp->arg;
51320Sstevel@tonic-gate 	kmem_free(zargp, sizeof (*zargp));
51330Sstevel@tonic-gate 
51340Sstevel@tonic-gate 	zone_namelen = strlen(zone->zone_name) + 1;
51350Sstevel@tonic-gate 	zone_name = kmem_alloc(zone_namelen, KM_SLEEP);
51360Sstevel@tonic-gate 	bcopy(zone->zone_name, zone_name, zone_namelen);
51370Sstevel@tonic-gate 	zoneid = zone->zone_id;
51380Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
51390Sstevel@tonic-gate 	/*
51400Sstevel@tonic-gate 	 * zoneadmd may be down, but at least we can empty out the zone.
51410Sstevel@tonic-gate 	 * We can ignore the return value of zone_empty() since we're called
51420Sstevel@tonic-gate 	 * from a kernel thread and know we won't be delivered any signals.
51430Sstevel@tonic-gate 	 */
51440Sstevel@tonic-gate 	ASSERT(curproc == &p0);
51450Sstevel@tonic-gate 	(void) zone_empty(zone);
51460Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY);
51470Sstevel@tonic-gate 	zone_rele(zone);
51480Sstevel@tonic-gate 
51490Sstevel@tonic-gate 	size = sizeof (arg);
51500Sstevel@tonic-gate 	darg.rbuf = (char *)&arg;
51510Sstevel@tonic-gate 	darg.data_ptr = (char *)&arg;
51520Sstevel@tonic-gate 	darg.rsize = size;
51530Sstevel@tonic-gate 	darg.data_size = size;
51540Sstevel@tonic-gate 	darg.desc_ptr = NULL;
51550Sstevel@tonic-gate 	darg.desc_num = 0;
51560Sstevel@tonic-gate 
51570Sstevel@tonic-gate 	save_arg = darg;
51580Sstevel@tonic-gate 	/*
51590Sstevel@tonic-gate 	 * Since we're not holding a reference to the zone, any number of
51600Sstevel@tonic-gate 	 * things can go wrong, including the zone disappearing before we get a
51610Sstevel@tonic-gate 	 * chance to talk to zoneadmd.
51620Sstevel@tonic-gate 	 */
51630Sstevel@tonic-gate 	for (retry = 0; /* forever */; retry++) {
51640Sstevel@tonic-gate 		if (door == NULL &&
51650Sstevel@tonic-gate 		    (error = zone_lookup_door(zone_name, &door)) != 0) {
51660Sstevel@tonic-gate 			goto next;
51670Sstevel@tonic-gate 		}
51680Sstevel@tonic-gate 		ASSERT(door != NULL);
51690Sstevel@tonic-gate 
51700Sstevel@tonic-gate 		if ((error = door_ki_upcall(door, &darg)) == 0) {
51710Sstevel@tonic-gate 			break;
51720Sstevel@tonic-gate 		}
51730Sstevel@tonic-gate 		switch (error) {
51740Sstevel@tonic-gate 		case EINTR:
51750Sstevel@tonic-gate 			/* FALLTHROUGH */
51760Sstevel@tonic-gate 		case EAGAIN:	/* process may be forking */
51770Sstevel@tonic-gate 			/*
51780Sstevel@tonic-gate 			 * Back off for a bit
51790Sstevel@tonic-gate 			 */
51800Sstevel@tonic-gate 			break;
51810Sstevel@tonic-gate 		case EBADF:
51820Sstevel@tonic-gate 			zone_release_door(&door);
51830Sstevel@tonic-gate 			if (zone_lookup_door(zone_name, &door) != 0) {
51840Sstevel@tonic-gate 				/*
51850Sstevel@tonic-gate 				 * zoneadmd may be dead, but it may come back to
51860Sstevel@tonic-gate 				 * life later.
51870Sstevel@tonic-gate 				 */
51880Sstevel@tonic-gate 				break;
51890Sstevel@tonic-gate 			}
51900Sstevel@tonic-gate 			break;
51910Sstevel@tonic-gate 		default:
51920Sstevel@tonic-gate 			cmn_err(CE_WARN,
51930Sstevel@tonic-gate 			    "zone_ki_call_zoneadmd: door_ki_upcall error %d\n",
51940Sstevel@tonic-gate 			    error);
51950Sstevel@tonic-gate 			goto out;
51960Sstevel@tonic-gate 		}
51970Sstevel@tonic-gate next:
51980Sstevel@tonic-gate 		/*
51990Sstevel@tonic-gate 		 * If this isn't the same zone_t that we originally had in mind,
52000Sstevel@tonic-gate 		 * then this is the same as if two kadmin requests come in at
52010Sstevel@tonic-gate 		 * the same time: the first one wins.  This means we lose, so we
52020Sstevel@tonic-gate 		 * bail.
52030Sstevel@tonic-gate 		 */
52040Sstevel@tonic-gate 		if ((zone = zone_find_by_id(zoneid)) == NULL) {
52050Sstevel@tonic-gate 			/*
52060Sstevel@tonic-gate 			 * Problem is solved.
52070Sstevel@tonic-gate 			 */
52080Sstevel@tonic-gate 			break;
52090Sstevel@tonic-gate 		}
52100Sstevel@tonic-gate 		if (zone->zone_uniqid != uniqid) {
52110Sstevel@tonic-gate 			/*
52120Sstevel@tonic-gate 			 * zoneid recycled
52130Sstevel@tonic-gate 			 */
52140Sstevel@tonic-gate 			zone_rele(zone);
52150Sstevel@tonic-gate 			break;
52160Sstevel@tonic-gate 		}
52170Sstevel@tonic-gate 		/*
52180Sstevel@tonic-gate 		 * We could zone_status_timedwait(), but there doesn't seem to
52190Sstevel@tonic-gate 		 * be much point in doing that (plus, it would mean that
52200Sstevel@tonic-gate 		 * zone_free() isn't called until this thread exits).
52210Sstevel@tonic-gate 		 */
52220Sstevel@tonic-gate 		zone_rele(zone);
52230Sstevel@tonic-gate 		delay(hz);
52240Sstevel@tonic-gate 		darg = save_arg;
52250Sstevel@tonic-gate 	}
52260Sstevel@tonic-gate out:
52270Sstevel@tonic-gate 	if (door != NULL) {
52280Sstevel@tonic-gate 		zone_release_door(&door);
52290Sstevel@tonic-gate 	}
52300Sstevel@tonic-gate 	kmem_free(zone_name, zone_namelen);
52310Sstevel@tonic-gate 	thread_exit();
52320Sstevel@tonic-gate }
52330Sstevel@tonic-gate 
52340Sstevel@tonic-gate /*
52352267Sdp  * Entry point for uadmin() to tell the zone to go away or reboot.  Analog to
52362267Sdp  * kadmin().  The caller is a process in the zone.
52370Sstevel@tonic-gate  *
52380Sstevel@tonic-gate  * In order to shutdown the zone, we will hand off control to zoneadmd
52390Sstevel@tonic-gate  * (running in the global zone) via a door.  We do a half-hearted job at
52400Sstevel@tonic-gate  * killing all processes in the zone, create a kernel thread to contact
52410Sstevel@tonic-gate  * zoneadmd, and make note of the "uniqid" of the zone.  The uniqid is
52420Sstevel@tonic-gate  * a form of generation number used to let zoneadmd (as well as
52430Sstevel@tonic-gate  * zone_destroy()) know exactly which zone they're re talking about.
52440Sstevel@tonic-gate  */
52450Sstevel@tonic-gate int
52462267Sdp zone_kadmin(int cmd, int fcn, const char *mdep, cred_t *credp)
52470Sstevel@tonic-gate {
52480Sstevel@tonic-gate 	struct zarg *zargp;
52490Sstevel@tonic-gate 	zone_cmd_t zcmd;
52500Sstevel@tonic-gate 	zone_t *zone;
52510Sstevel@tonic-gate 
52520Sstevel@tonic-gate 	zone = curproc->p_zone;
52530Sstevel@tonic-gate 	ASSERT(getzoneid() != GLOBAL_ZONEID);
52540Sstevel@tonic-gate 
52550Sstevel@tonic-gate 	switch (cmd) {
52560Sstevel@tonic-gate 	case A_SHUTDOWN:
52570Sstevel@tonic-gate 		switch (fcn) {
52580Sstevel@tonic-gate 		case AD_HALT:
52590Sstevel@tonic-gate 		case AD_POWEROFF:
52600Sstevel@tonic-gate 			zcmd = Z_HALT;
52610Sstevel@tonic-gate 			break;
52620Sstevel@tonic-gate 		case AD_BOOT:
52630Sstevel@tonic-gate 			zcmd = Z_REBOOT;
52640Sstevel@tonic-gate 			break;
52650Sstevel@tonic-gate 		case AD_IBOOT:
52660Sstevel@tonic-gate 		case AD_SBOOT:
52670Sstevel@tonic-gate 		case AD_SIBOOT:
52680Sstevel@tonic-gate 		case AD_NOSYNC:
52690Sstevel@tonic-gate 			return (ENOTSUP);
52700Sstevel@tonic-gate 		default:
52710Sstevel@tonic-gate 			return (EINVAL);
52720Sstevel@tonic-gate 		}
52730Sstevel@tonic-gate 		break;
52740Sstevel@tonic-gate 	case A_REBOOT:
52750Sstevel@tonic-gate 		zcmd = Z_REBOOT;
52760Sstevel@tonic-gate 		break;
52770Sstevel@tonic-gate 	case A_FTRACE:
52780Sstevel@tonic-gate 	case A_REMOUNT:
52790Sstevel@tonic-gate 	case A_FREEZE:
52800Sstevel@tonic-gate 	case A_DUMP:
52810Sstevel@tonic-gate 		return (ENOTSUP);
52820Sstevel@tonic-gate 	default:
52830Sstevel@tonic-gate 		ASSERT(cmd != A_SWAPCTL);	/* handled by uadmin() */
52840Sstevel@tonic-gate 		return (EINVAL);
52850Sstevel@tonic-gate 	}
52860Sstevel@tonic-gate 
52870Sstevel@tonic-gate 	if (secpolicy_zone_admin(credp, B_FALSE))
52880Sstevel@tonic-gate 		return (EPERM);
52890Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
52902267Sdp 
52910Sstevel@tonic-gate 	/*
52920Sstevel@tonic-gate 	 * zone_status can't be ZONE_IS_EMPTY or higher since curproc
52930Sstevel@tonic-gate 	 * is in the zone.
52940Sstevel@tonic-gate 	 */
52950Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY);
52960Sstevel@tonic-gate 	if (zone_status_get(zone) > ZONE_IS_RUNNING) {
52970Sstevel@tonic-gate 		/*
52980Sstevel@tonic-gate 		 * This zone is already on its way down.
52990Sstevel@tonic-gate 		 */
53000Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
53010Sstevel@tonic-gate 		return (0);
53020Sstevel@tonic-gate 	}
53030Sstevel@tonic-gate 	/*
53040Sstevel@tonic-gate 	 * Prevent future zone_enter()s
53050Sstevel@tonic-gate 	 */
53060Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
53070Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
53080Sstevel@tonic-gate 
53090Sstevel@tonic-gate 	/*
53100Sstevel@tonic-gate 	 * Kill everyone now and call zoneadmd later.
53110Sstevel@tonic-gate 	 * zone_ki_call_zoneadmd() will do a more thorough job of this
53120Sstevel@tonic-gate 	 * later.
53130Sstevel@tonic-gate 	 */
53140Sstevel@tonic-gate 	killall(zone->zone_id);
53150Sstevel@tonic-gate 	/*
53160Sstevel@tonic-gate 	 * Now, create the thread to contact zoneadmd and do the rest of the
53170Sstevel@tonic-gate 	 * work.  This thread can't be created in our zone otherwise
53180Sstevel@tonic-gate 	 * zone_destroy() would deadlock.
53190Sstevel@tonic-gate 	 */
53202267Sdp 	zargp = kmem_zalloc(sizeof (*zargp), KM_SLEEP);
53210Sstevel@tonic-gate 	zargp->arg.cmd = zcmd;
53220Sstevel@tonic-gate 	zargp->arg.uniqid = zone->zone_uniqid;
53232267Sdp 	zargp->zone = zone;
53240Sstevel@tonic-gate 	(void) strcpy(zargp->arg.locale, "C");
53252267Sdp 	/* mdep was already copied in for us by uadmin */
53262267Sdp 	if (mdep != NULL)
53272267Sdp 		(void) strlcpy(zargp->arg.bootbuf, mdep,
53282267Sdp 		    sizeof (zargp->arg.bootbuf));
53292267Sdp 	zone_hold(zone);
53300Sstevel@tonic-gate 
53310Sstevel@tonic-gate 	(void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0,
53320Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
53330Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
53340Sstevel@tonic-gate 
53350Sstevel@tonic-gate 	return (EINVAL);
53360Sstevel@tonic-gate }
53370Sstevel@tonic-gate 
53380Sstevel@tonic-gate /*
53390Sstevel@tonic-gate  * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's
53400Sstevel@tonic-gate  * status to ZONE_IS_SHUTTING_DOWN.
53410Sstevel@tonic-gate  */
53420Sstevel@tonic-gate void
53430Sstevel@tonic-gate zone_shutdown_global(void)
53440Sstevel@tonic-gate {
53450Sstevel@tonic-gate 	ASSERT(curproc->p_zone == global_zone);
53460Sstevel@tonic-gate 
53470Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
53480Sstevel@tonic-gate 	ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING);
53490Sstevel@tonic-gate 	zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN);
53500Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
53510Sstevel@tonic-gate }
5352789Sahrens 
5353789Sahrens /*
5354789Sahrens  * Returns true if the named dataset is visible in the current zone.
5355789Sahrens  * The 'write' parameter is set to 1 if the dataset is also writable.
5356789Sahrens  */
5357789Sahrens int
5358789Sahrens zone_dataset_visible(const char *dataset, int *write)
5359789Sahrens {
5360789Sahrens 	zone_dataset_t *zd;
5361789Sahrens 	size_t len;
5362789Sahrens 	zone_t *zone = curproc->p_zone;
5363789Sahrens 
5364789Sahrens 	if (dataset[0] == '\0')
5365789Sahrens 		return (0);
5366789Sahrens 
5367789Sahrens 	/*
5368789Sahrens 	 * Walk the list once, looking for datasets which match exactly, or
5369789Sahrens 	 * specify a dataset underneath an exported dataset.  If found, return
5370789Sahrens 	 * true and note that it is writable.
5371789Sahrens 	 */
5372789Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
5373789Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
5374789Sahrens 
5375789Sahrens 		len = strlen(zd->zd_dataset);
5376789Sahrens 		if (strlen(dataset) >= len &&
5377789Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
5378816Smaybee 		    (dataset[len] == '\0' || dataset[len] == '/' ||
5379816Smaybee 		    dataset[len] == '@')) {
5380789Sahrens 			if (write)
5381789Sahrens 				*write = 1;
5382789Sahrens 			return (1);
5383789Sahrens 		}
5384789Sahrens 	}
5385789Sahrens 
5386789Sahrens 	/*
5387789Sahrens 	 * Walk the list a second time, searching for datasets which are parents
5388789Sahrens 	 * of exported datasets.  These should be visible, but read-only.
5389789Sahrens 	 *
5390789Sahrens 	 * Note that we also have to support forms such as 'pool/dataset/', with
5391789Sahrens 	 * a trailing slash.
5392789Sahrens 	 */
5393789Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
5394789Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
5395789Sahrens 
5396789Sahrens 		len = strlen(dataset);
5397789Sahrens 		if (dataset[len - 1] == '/')
5398789Sahrens 			len--;	/* Ignore trailing slash */
5399789Sahrens 		if (len < strlen(zd->zd_dataset) &&
5400789Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
5401789Sahrens 		    zd->zd_dataset[len] == '/') {
5402789Sahrens 			if (write)
5403789Sahrens 				*write = 0;
5404789Sahrens 			return (1);
5405789Sahrens 		}
5406789Sahrens 	}
5407789Sahrens 
5408789Sahrens 	return (0);
5409789Sahrens }
54101676Sjpk 
54111676Sjpk /*
54121676Sjpk  * zone_find_by_any_path() -
54131676Sjpk  *
54141676Sjpk  * kernel-private routine similar to zone_find_by_path(), but which
54151676Sjpk  * effectively compares against zone paths rather than zonerootpath
54161676Sjpk  * (i.e., the last component of zonerootpaths, which should be "root/",
54171676Sjpk  * are not compared.)  This is done in order to accurately identify all
54181676Sjpk  * paths, whether zone-visible or not, including those which are parallel
54191676Sjpk  * to /root/, such as /dev/, /home/, etc...
54201676Sjpk  *
54211676Sjpk  * If the specified path does not fall under any zone path then global
54221676Sjpk  * zone is returned.
54231676Sjpk  *
54241676Sjpk  * The treat_abs parameter indicates whether the path should be treated as
54251676Sjpk  * an absolute path although it does not begin with "/".  (This supports
54261676Sjpk  * nfs mount syntax such as host:any/path.)
54271676Sjpk  *
54281676Sjpk  * The caller is responsible for zone_rele of the returned zone.
54291676Sjpk  */
54301676Sjpk zone_t *
54311676Sjpk zone_find_by_any_path(const char *path, boolean_t treat_abs)
54321676Sjpk {
54331676Sjpk 	zone_t *zone;
54341676Sjpk 	int path_offset = 0;
54351676Sjpk 
54361676Sjpk 	if (path == NULL) {
54371676Sjpk 		zone_hold(global_zone);
54381676Sjpk 		return (global_zone);
54391676Sjpk 	}
54401676Sjpk 
54411676Sjpk 	if (*path != '/') {
54421676Sjpk 		ASSERT(treat_abs);
54431676Sjpk 		path_offset = 1;
54441676Sjpk 	}
54451676Sjpk 
54461676Sjpk 	mutex_enter(&zonehash_lock);
54471676Sjpk 	for (zone = list_head(&zone_active); zone != NULL;
54481676Sjpk 	    zone = list_next(&zone_active, zone)) {
54491676Sjpk 		char	*c;
54501676Sjpk 		size_t	pathlen;
54511876Smp46848 		char *rootpath_start;
54521676Sjpk 
54531676Sjpk 		if (zone == global_zone)	/* skip global zone */
54541676Sjpk 			continue;
54551676Sjpk 
54561676Sjpk 		/* scan backwards to find start of last component */
54571676Sjpk 		c = zone->zone_rootpath + zone->zone_rootpathlen - 2;
54581676Sjpk 		do {
54591676Sjpk 			c--;
54601676Sjpk 		} while (*c != '/');
54611676Sjpk 
54621876Smp46848 		pathlen = c - zone->zone_rootpath + 1 - path_offset;
54631876Smp46848 		rootpath_start = (zone->zone_rootpath + path_offset);
54641876Smp46848 		if (strncmp(path, rootpath_start, pathlen) == 0)
54651676Sjpk 			break;
54661676Sjpk 	}
54671676Sjpk 	if (zone == NULL)
54681676Sjpk 		zone = global_zone;
54691676Sjpk 	zone_hold(zone);
54701676Sjpk 	mutex_exit(&zonehash_lock);
54711676Sjpk 	return (zone);
54721676Sjpk }
54733448Sdh155122 
54743448Sdh155122 /* List of data link names which are accessible from the zone */
54753448Sdh155122 struct dlnamelist {
54763448Sdh155122 	char			dlnl_name[LIFNAMSIZ];
54773448Sdh155122 	struct dlnamelist	*dlnl_next;
54783448Sdh155122 };
54793448Sdh155122 
54803448Sdh155122 
54813448Sdh155122 /*
54823448Sdh155122  * Check whether the datalink name (dlname) itself is present.
54833448Sdh155122  * Return true if found.
54843448Sdh155122  */
54853448Sdh155122 static boolean_t
54863448Sdh155122 zone_dlname(zone_t *zone, char *dlname)
54873448Sdh155122 {
54883448Sdh155122 	struct dlnamelist *dlnl;
54893448Sdh155122 	boolean_t found = B_FALSE;
54903448Sdh155122 
54913448Sdh155122 	mutex_enter(&zone->zone_lock);
54923448Sdh155122 	for (dlnl = zone->zone_dl_list; dlnl != NULL; dlnl = dlnl->dlnl_next) {
54933448Sdh155122 		if (strncmp(dlnl->dlnl_name, dlname, LIFNAMSIZ) == 0) {
54943448Sdh155122 			found = B_TRUE;
54953448Sdh155122 			break;
54963448Sdh155122 		}
54973448Sdh155122 	}
54983448Sdh155122 	mutex_exit(&zone->zone_lock);
54993448Sdh155122 	return (found);
55003448Sdh155122 }
55013448Sdh155122 
55023448Sdh155122 /*
55033448Sdh155122  * Add an data link name for the zone. Does not check for duplicates.
55043448Sdh155122  */
55053448Sdh155122 static int
55063448Sdh155122 zone_add_datalink(zoneid_t zoneid, char *dlname)
55073448Sdh155122 {
55083448Sdh155122 	struct dlnamelist *dlnl;
55093448Sdh155122 	zone_t *zone;
55103448Sdh155122 	zone_t *thiszone;
55113448Sdh155122 	int err;
55123448Sdh155122 
55133448Sdh155122 	dlnl = kmem_zalloc(sizeof (struct dlnamelist), KM_SLEEP);
55143448Sdh155122 	if ((err = copyinstr(dlname, dlnl->dlnl_name, LIFNAMSIZ, NULL)) != 0) {
55153448Sdh155122 		kmem_free(dlnl, sizeof (struct dlnamelist));
55163448Sdh155122 		return (set_errno(err));
55173448Sdh155122 	}
55183448Sdh155122 
55193448Sdh155122 	thiszone = zone_find_by_id(zoneid);
55203448Sdh155122 	if (thiszone == NULL) {
55213448Sdh155122 		kmem_free(dlnl, sizeof (struct dlnamelist));
55223448Sdh155122 		return (set_errno(ENXIO));
55233448Sdh155122 	}
55243448Sdh155122 
55253448Sdh155122 	/*
55263448Sdh155122 	 * Verify that the datalink name isn't already used by a different
55273448Sdh155122 	 * zone while allowing duplicate entries for the same zone (e.g. due
55283448Sdh155122 	 * to both using IPv4 and IPv6 on an interface)
55293448Sdh155122 	 */
55303448Sdh155122 	mutex_enter(&zonehash_lock);
55313448Sdh155122 	for (zone = list_head(&zone_active); zone != NULL;
55323448Sdh155122 	    zone = list_next(&zone_active, zone)) {
55333448Sdh155122 		if (zone->zone_id == zoneid)
55343448Sdh155122 			continue;
55353448Sdh155122 
55363448Sdh155122 		if (zone_dlname(zone, dlnl->dlnl_name)) {
55373448Sdh155122 			mutex_exit(&zonehash_lock);
55383448Sdh155122 			zone_rele(thiszone);
55393448Sdh155122 			kmem_free(dlnl, sizeof (struct dlnamelist));
55403448Sdh155122 			return (set_errno(EPERM));
55413448Sdh155122 		}
55423448Sdh155122 	}
55433448Sdh155122 	mutex_enter(&thiszone->zone_lock);
55443448Sdh155122 	dlnl->dlnl_next = thiszone->zone_dl_list;
55453448Sdh155122 	thiszone->zone_dl_list = dlnl;
55463448Sdh155122 	mutex_exit(&thiszone->zone_lock);
55473448Sdh155122 	mutex_exit(&zonehash_lock);
55483448Sdh155122 	zone_rele(thiszone);
55493448Sdh155122 	return (0);
55503448Sdh155122 }
55513448Sdh155122 
55523448Sdh155122 static int
55533448Sdh155122 zone_remove_datalink(zoneid_t zoneid, char *dlname)
55543448Sdh155122 {
55553448Sdh155122 	struct dlnamelist *dlnl, *odlnl, **dlnlp;
55563448Sdh155122 	zone_t *zone;
55573448Sdh155122 	int err;
55583448Sdh155122 
55593448Sdh155122 	dlnl = kmem_zalloc(sizeof (struct dlnamelist), KM_SLEEP);
55603448Sdh155122 	if ((err = copyinstr(dlname, dlnl->dlnl_name, LIFNAMSIZ, NULL)) != 0) {
55613448Sdh155122 		kmem_free(dlnl, sizeof (struct dlnamelist));
55623448Sdh155122 		return (set_errno(err));
55633448Sdh155122 	}
55643448Sdh155122 	zone = zone_find_by_id(zoneid);
55653448Sdh155122 	if (zone == NULL) {
55663448Sdh155122 		kmem_free(dlnl, sizeof (struct dlnamelist));
55673448Sdh155122 		return (set_errno(EINVAL));
55683448Sdh155122 	}
55693448Sdh155122 
55703448Sdh155122 	mutex_enter(&zone->zone_lock);
55713448Sdh155122 	/* Look for match */
55723448Sdh155122 	dlnlp = &zone->zone_dl_list;
55733448Sdh155122 	while (*dlnlp != NULL) {
55743448Sdh155122 		if (strncmp(dlnl->dlnl_name, (*dlnlp)->dlnl_name,
55753448Sdh155122 		    LIFNAMSIZ) == 0)
55763448Sdh155122 			goto found;
55773448Sdh155122 		dlnlp = &((*dlnlp)->dlnl_next);
55783448Sdh155122 	}
55793448Sdh155122 	mutex_exit(&zone->zone_lock);
55803448Sdh155122 	zone_rele(zone);
55813448Sdh155122 	kmem_free(dlnl, sizeof (struct dlnamelist));
55823448Sdh155122 	return (set_errno(ENXIO));
55833448Sdh155122 
55843448Sdh155122 found:
55853448Sdh155122 	odlnl = *dlnlp;
55863448Sdh155122 	*dlnlp = (*dlnlp)->dlnl_next;
55873448Sdh155122 	kmem_free(odlnl, sizeof (struct dlnamelist));
55883448Sdh155122 
55893448Sdh155122 	mutex_exit(&zone->zone_lock);
55903448Sdh155122 	zone_rele(zone);
55913448Sdh155122 	kmem_free(dlnl, sizeof (struct dlnamelist));
55923448Sdh155122 	return (0);
55933448Sdh155122 }
55943448Sdh155122 
55953448Sdh155122 /*
55963448Sdh155122  * Using the zoneidp as ALL_ZONES, we can lookup which zone is using datalink
55973448Sdh155122  * name (dlname); otherwise we just check if the specified zoneidp has access
55983448Sdh155122  * to the datalink name.
55993448Sdh155122  */
56003448Sdh155122 static int
56013448Sdh155122 zone_check_datalink(zoneid_t *zoneidp, char *dlname)
56023448Sdh155122 {
56033448Sdh155122 	zoneid_t id;
56043448Sdh155122 	char *dln;
56053448Sdh155122 	zone_t *zone;
56063448Sdh155122 	int err = 0;
56073448Sdh155122 	boolean_t allzones = B_FALSE;
56083448Sdh155122 
56093448Sdh155122 	if (copyin(zoneidp, &id, sizeof (id)) != 0) {
56103448Sdh155122 		return (set_errno(EFAULT));
56113448Sdh155122 	}
56123448Sdh155122 	dln = kmem_zalloc(LIFNAMSIZ, KM_SLEEP);
56133448Sdh155122 	if ((err = copyinstr(dlname, dln, LIFNAMSIZ, NULL)) != 0) {
56143448Sdh155122 		kmem_free(dln, LIFNAMSIZ);
56153448Sdh155122 		return (set_errno(err));
56163448Sdh155122 	}
56173448Sdh155122 
56183448Sdh155122 	if (id == ALL_ZONES)
56193448Sdh155122 		allzones = B_TRUE;
56203448Sdh155122 
56213448Sdh155122 	/*
56223448Sdh155122 	 * Check whether datalink name is already used.
56233448Sdh155122 	 */
56243448Sdh155122 	mutex_enter(&zonehash_lock);
56253448Sdh155122 	for (zone = list_head(&zone_active); zone != NULL;
56263448Sdh155122 	    zone = list_next(&zone_active, zone)) {
56273448Sdh155122 		if (allzones || (id == zone->zone_id)) {
56283448Sdh155122 			if (!zone_dlname(zone, dln))
56293448Sdh155122 				continue;
56303448Sdh155122 			if (allzones)
56313448Sdh155122 				err = copyout(&zone->zone_id, zoneidp,
56323448Sdh155122 				    sizeof (*zoneidp));
56333448Sdh155122 
56343448Sdh155122 			mutex_exit(&zonehash_lock);
56353448Sdh155122 			kmem_free(dln, LIFNAMSIZ);
56363448Sdh155122 			return (err ? set_errno(EFAULT) : 0);
56373448Sdh155122 		}
56383448Sdh155122 	}
56393448Sdh155122 
56403448Sdh155122 	/* datalink name is not found in any active zone. */
56413448Sdh155122 	mutex_exit(&zonehash_lock);
56423448Sdh155122 	kmem_free(dln, LIFNAMSIZ);
56433448Sdh155122 	return (set_errno(ENXIO));
56443448Sdh155122 }
56453448Sdh155122 
56463448Sdh155122 /*
56473448Sdh155122  * Get the names of the datalinks assigned to a zone.
56483448Sdh155122  * Here *nump is the number of datalinks, and the assumption
5649*5331Samw  * is that the caller will guarantee that the the supplied buffer is
56503448Sdh155122  * big enough to hold at least #*nump datalink names, that is,
56513448Sdh155122  * LIFNAMSIZ X *nump
56523448Sdh155122  * On return, *nump will be the "new" number of datalinks, if it
56533448Sdh155122  * ever changed.
56543448Sdh155122  */
56553448Sdh155122 static int
56563448Sdh155122 zone_list_datalink(zoneid_t zoneid, int *nump, char *buf)
56573448Sdh155122 {
56583448Sdh155122 	int num, dlcount;
56593448Sdh155122 	zone_t *zone;
56603448Sdh155122 	struct dlnamelist *dlnl;
56613448Sdh155122 	char *ptr;
56623448Sdh155122 
56633448Sdh155122 	if (copyin(nump, &dlcount, sizeof (dlcount)) != 0)
56643448Sdh155122 		return (set_errno(EFAULT));
56653448Sdh155122 
56663448Sdh155122 	zone = zone_find_by_id(zoneid);
56673448Sdh155122 	if (zone == NULL) {
56683448Sdh155122 		return (set_errno(ENXIO));
56693448Sdh155122 	}
56703448Sdh155122 
56713448Sdh155122 	num = 0;
56723448Sdh155122 	mutex_enter(&zone->zone_lock);
56733448Sdh155122 	ptr = buf;
56743448Sdh155122 	for (dlnl = zone->zone_dl_list; dlnl != NULL; dlnl = dlnl->dlnl_next) {
56753448Sdh155122 		/*
56763448Sdh155122 		 * If the list changed and the new number is bigger
56773448Sdh155122 		 * than what the caller supplied, just count, don't
56783448Sdh155122 		 * do copyout
56793448Sdh155122 		 */
56803448Sdh155122 		if (++num > dlcount)
56813448Sdh155122 			continue;
56823448Sdh155122 		if (copyout(dlnl->dlnl_name, ptr, LIFNAMSIZ) != 0) {
56833448Sdh155122 			mutex_exit(&zone->zone_lock);
56843448Sdh155122 			zone_rele(zone);
56853448Sdh155122 			return (set_errno(EFAULT));
56863448Sdh155122 		}
56873448Sdh155122 		ptr += LIFNAMSIZ;
56883448Sdh155122 	}
56893448Sdh155122 	mutex_exit(&zone->zone_lock);
56903448Sdh155122 	zone_rele(zone);
56913448Sdh155122 
56923448Sdh155122 	/* Increased or decreased, caller should be notified. */
56933448Sdh155122 	if (num != dlcount) {
56943448Sdh155122 		if (copyout(&num, nump, sizeof (num)) != 0) {
56953448Sdh155122 			return (set_errno(EFAULT));
56963448Sdh155122 		}
56973448Sdh155122 	}
56983448Sdh155122 	return (0);
56993448Sdh155122 }
57003448Sdh155122 
57013448Sdh155122 /*
57023448Sdh155122  * Public interface for looking up a zone by zoneid. It's a customized version
57033448Sdh155122  * for netstack_zone_create(), it:
57043448Sdh155122  * 1. Doesn't acquire the zonehash_lock, since it is called from
57053448Sdh155122  *    zone_key_create() or zone_zsd_configure(), lock already held.
57063448Sdh155122  * 2. Doesn't check the status of the zone.
57073448Sdh155122  * 3. It will be called even before zone_init is called, in that case the
57083448Sdh155122  *    address of zone0 is returned directly, and netstack_zone_create()
57093448Sdh155122  *    will only assign a value to zone0.zone_netstack, won't break anything.
57103448Sdh155122  */
57113448Sdh155122 zone_t *
57123448Sdh155122 zone_find_by_id_nolock(zoneid_t zoneid)
57133448Sdh155122 {
57143448Sdh155122 	ASSERT(MUTEX_HELD(&zonehash_lock));
57153448Sdh155122 
57163448Sdh155122 	if (zonehashbyid == NULL)
57173448Sdh155122 		return (&zone0);
57183448Sdh155122 	else
57193448Sdh155122 		return (zone_find_all_by_id(zoneid));
57203448Sdh155122 }
5721