xref: /onnv-gate/usr/src/uts/common/os/zone.c (revision 3792:57ba782523b7)
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>
243*3792Sakolb #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;
332*3792Sakolb 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 
887*3792Sakolb /*
888*3792Sakolb  * zone.cpu-cap resource control support.
889*3792Sakolb  */
890*3792Sakolb /*ARGSUSED*/
891*3792Sakolb static rctl_qty_t
892*3792Sakolb zone_cpu_cap_get(rctl_t *rctl, struct proc *p)
893*3792Sakolb {
894*3792Sakolb 	ASSERT(MUTEX_HELD(&p->p_lock));
895*3792Sakolb 	return (cpucaps_zone_get(p->p_zone));
896*3792Sakolb }
897*3792Sakolb 
898*3792Sakolb /*ARGSUSED*/
899*3792Sakolb static int
900*3792Sakolb zone_cpu_cap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
901*3792Sakolb     rctl_qty_t nv)
902*3792Sakolb {
903*3792Sakolb 	zone_t *zone = e->rcep_p.zone;
904*3792Sakolb 
905*3792Sakolb 	ASSERT(MUTEX_HELD(&p->p_lock));
906*3792Sakolb 	ASSERT(e->rcep_t == RCENTITY_ZONE);
907*3792Sakolb 
908*3792Sakolb 	if (zone == NULL)
909*3792Sakolb 		return (0);
910*3792Sakolb 
911*3792Sakolb 	/*
912*3792Sakolb 	 * set cap to the new value.
913*3792Sakolb 	 */
914*3792Sakolb 	return (cpucaps_zone_set(zone, nv));
915*3792Sakolb }
916*3792Sakolb 
917*3792Sakolb static rctl_ops_t zone_cpu_cap_ops = {
918*3792Sakolb 	rcop_no_action,
919*3792Sakolb 	zone_cpu_cap_get,
920*3792Sakolb 	zone_cpu_cap_set,
921*3792Sakolb 	rcop_no_test
922*3792Sakolb };
923*3792Sakolb 
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,
1426*3792Sakolb 	    FSS_MAXSHARES, FSS_MAXSHARES, &zone_cpu_shares_ops);
1427*3792Sakolb 
1428*3792Sakolb 	rc_zone_cpu_cap = rctl_register("zone.cpu-cap",
1429*3792Sakolb 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_ALWAYS |
1430*3792Sakolb 	    RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT |RCTL_GLOBAL_SYSLOG_NEVER |
1431*3792Sakolb 	    RCTL_GLOBAL_INFINITE,
1432*3792Sakolb 	    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 
1577*3792Sakolb 	/*
1578*3792Sakolb 	 * Remove any zone caps.
1579*3792Sakolb 	 */
1580*3792Sakolb 	cpucaps_zone_remove(zone);
1581*3792Sakolb 
1582*3792Sakolb 	ASSERT(zone->zone_cpucap == NULL);
1583*3792Sakolb 
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
16922267Sdp zone_set_initname(zone_t *zone, const char *zone_initname)
16932267Sdp {
16942267Sdp 	char initname[INITNAME_SZ];
16950Sstevel@tonic-gate 	size_t len;
16962267Sdp 	int err = 0;
16972267Sdp 
16982267Sdp 	ASSERT(zone != global_zone);
16992267Sdp 	if ((err = copyinstr(zone_initname, initname, INITNAME_SZ, &len)) != 0)
17000Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
17012267Sdp 
17022267Sdp 	if (zone->zone_initname != NULL)
17032267Sdp 		kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1);
17042267Sdp 
17052267Sdp 	zone->zone_initname = kmem_alloc(strlen(initname) + 1, KM_SLEEP);
17062267Sdp 	(void) strcpy(zone->zone_initname, initname);
17070Sstevel@tonic-gate 	return (0);
17080Sstevel@tonic-gate }
17090Sstevel@tonic-gate 
17103247Sgjelinek static int
17113247Sgjelinek zone_set_phys_mcap(zone_t *zone, const uint64_t *zone_mcap)
17123247Sgjelinek {
17133247Sgjelinek 	uint64_t mcap;
17143247Sgjelinek 	int err = 0;
17153247Sgjelinek 
17163247Sgjelinek 	if ((err = copyin(zone_mcap, &mcap, sizeof (uint64_t))) == 0)
17173247Sgjelinek 		zone->zone_phys_mcap = mcap;
17183247Sgjelinek 
17193247Sgjelinek 	return (err);
17203247Sgjelinek }
17213247Sgjelinek 
17223247Sgjelinek static int
17233247Sgjelinek zone_set_sched_class(zone_t *zone, const char *new_class)
17243247Sgjelinek {
17253247Sgjelinek 	char sched_class[PC_CLNMSZ];
17263247Sgjelinek 	id_t classid;
17273247Sgjelinek 	int err;
17283247Sgjelinek 
17293247Sgjelinek 	ASSERT(zone != global_zone);
17303247Sgjelinek 	if ((err = copyinstr(new_class, sched_class, PC_CLNMSZ, NULL)) != 0)
17313247Sgjelinek 		return (err);	/* EFAULT or ENAMETOOLONG */
17323247Sgjelinek 
17333247Sgjelinek 	if (getcid(sched_class, &classid) != 0 || classid == syscid)
17343247Sgjelinek 		return (set_errno(EINVAL));
17353247Sgjelinek 	zone->zone_defaultcid = classid;
17363247Sgjelinek 	ASSERT(zone->zone_defaultcid > 0 &&
17373247Sgjelinek 	    zone->zone_defaultcid < loaded_classes);
17383247Sgjelinek 
17393247Sgjelinek 	return (0);
17403247Sgjelinek }
17413247Sgjelinek 
17420Sstevel@tonic-gate /*
17430Sstevel@tonic-gate  * Block indefinitely waiting for (zone_status >= status)
17440Sstevel@tonic-gate  */
17450Sstevel@tonic-gate void
17460Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status)
17470Sstevel@tonic-gate {
17480Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
17510Sstevel@tonic-gate 	while (zone->zone_status < status) {
17520Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
17530Sstevel@tonic-gate 	}
17540Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
17550Sstevel@tonic-gate }
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate /*
17580Sstevel@tonic-gate  * Private CPR-safe version of zone_status_wait().
17590Sstevel@tonic-gate  */
17600Sstevel@tonic-gate static void
17610Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str)
17620Sstevel@tonic-gate {
17630Sstevel@tonic-gate 	callb_cpr_t cprinfo;
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr,
17680Sstevel@tonic-gate 	    str);
17690Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
17700Sstevel@tonic-gate 	while (zone->zone_status < status) {
17710Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
17720Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
17730Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock);
17740Sstevel@tonic-gate 	}
17750Sstevel@tonic-gate 	/*
17760Sstevel@tonic-gate 	 * zone_status_lock is implicitly released by the following.
17770Sstevel@tonic-gate 	 */
17780Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
17790Sstevel@tonic-gate }
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate /*
17820Sstevel@tonic-gate  * Block until zone enters requested state or signal is received.  Return (0)
17830Sstevel@tonic-gate  * if signaled, non-zero otherwise.
17840Sstevel@tonic-gate  */
17850Sstevel@tonic-gate int
17860Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status)
17870Sstevel@tonic-gate {
17880Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
17910Sstevel@tonic-gate 	while (zone->zone_status < status) {
17920Sstevel@tonic-gate 		if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) {
17930Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
17940Sstevel@tonic-gate 			return (0);
17950Sstevel@tonic-gate 		}
17960Sstevel@tonic-gate 	}
17970Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
17980Sstevel@tonic-gate 	return (1);
17990Sstevel@tonic-gate }
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate /*
18020Sstevel@tonic-gate  * Block until the zone enters the requested state or the timeout expires,
18030Sstevel@tonic-gate  * whichever happens first.  Return (-1) if operation timed out, time remaining
18040Sstevel@tonic-gate  * otherwise.
18050Sstevel@tonic-gate  */
18060Sstevel@tonic-gate clock_t
18070Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status)
18080Sstevel@tonic-gate {
18090Sstevel@tonic-gate 	clock_t timeleft = 0;
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
18140Sstevel@tonic-gate 	while (zone->zone_status < status && timeleft != -1) {
18150Sstevel@tonic-gate 		timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim);
18160Sstevel@tonic-gate 	}
18170Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
18180Sstevel@tonic-gate 	return (timeleft);
18190Sstevel@tonic-gate }
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate /*
18220Sstevel@tonic-gate  * Block until the zone enters the requested state, the current process is
18230Sstevel@tonic-gate  * signaled,  or the timeout expires, whichever happens first.  Return (-1) if
18240Sstevel@tonic-gate  * operation timed out, 0 if signaled, time remaining otherwise.
18250Sstevel@tonic-gate  */
18260Sstevel@tonic-gate clock_t
18270Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status)
18280Sstevel@tonic-gate {
18290Sstevel@tonic-gate 	clock_t timeleft = tim - lbolt;
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
18340Sstevel@tonic-gate 	while (zone->zone_status < status) {
18350Sstevel@tonic-gate 		timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock,
18360Sstevel@tonic-gate 		    tim);
18370Sstevel@tonic-gate 		if (timeleft <= 0)
18380Sstevel@tonic-gate 			break;
18390Sstevel@tonic-gate 	}
18400Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
18410Sstevel@tonic-gate 	return (timeleft);
18420Sstevel@tonic-gate }
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate /*
18450Sstevel@tonic-gate  * Zones have two reference counts: one for references from credential
18460Sstevel@tonic-gate  * structures (zone_cred_ref), and one (zone_ref) for everything else.
18470Sstevel@tonic-gate  * This is so we can allow a zone to be rebooted while there are still
18480Sstevel@tonic-gate  * outstanding cred references, since certain drivers cache dblks (which
18490Sstevel@tonic-gate  * implicitly results in cached creds).  We wait for zone_ref to drop to
18500Sstevel@tonic-gate  * 0 (actually 1), but not zone_cred_ref.  The zone structure itself is
18510Sstevel@tonic-gate  * later freed when the zone_cred_ref drops to 0, though nothing other
18520Sstevel@tonic-gate  * than the zone id and privilege set should be accessed once the zone
18530Sstevel@tonic-gate  * is "dead".
18540Sstevel@tonic-gate  *
18550Sstevel@tonic-gate  * A debugging flag, zone_wait_for_cred, can be set to a non-zero value
18560Sstevel@tonic-gate  * to force halt/reboot to block waiting for the zone_cred_ref to drop
18570Sstevel@tonic-gate  * to 0.  This can be useful to flush out other sources of cached creds
18580Sstevel@tonic-gate  * that may be less innocuous than the driver case.
18590Sstevel@tonic-gate  */
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate int zone_wait_for_cred = 0;
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate static void
18640Sstevel@tonic-gate zone_hold_locked(zone_t *z)
18650Sstevel@tonic-gate {
18660Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&z->zone_lock));
18670Sstevel@tonic-gate 	z->zone_ref++;
18680Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
18690Sstevel@tonic-gate }
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate void
18720Sstevel@tonic-gate zone_hold(zone_t *z)
18730Sstevel@tonic-gate {
18740Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
18750Sstevel@tonic-gate 	zone_hold_locked(z);
18760Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
18770Sstevel@tonic-gate }
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate /*
18800Sstevel@tonic-gate  * If the non-cred ref count drops to 1 and either the cred ref count
18810Sstevel@tonic-gate  * is 0 or we aren't waiting for cred references, the zone is ready to
18820Sstevel@tonic-gate  * be destroyed.
18830Sstevel@tonic-gate  */
18840Sstevel@tonic-gate #define	ZONE_IS_UNREF(zone)	((zone)->zone_ref == 1 && \
18850Sstevel@tonic-gate 	    (!zone_wait_for_cred || (zone)->zone_cred_ref == 0))
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate void
18880Sstevel@tonic-gate zone_rele(zone_t *z)
18890Sstevel@tonic-gate {
18900Sstevel@tonic-gate 	boolean_t wakeup;
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
18930Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
18940Sstevel@tonic-gate 	z->zone_ref--;
18950Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
18960Sstevel@tonic-gate 		/* no more refs, free the structure */
18970Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
18980Sstevel@tonic-gate 		zone_free(z);
18990Sstevel@tonic-gate 		return;
19000Sstevel@tonic-gate 	}
19010Sstevel@tonic-gate 	/* signal zone_destroy so the zone can finish halting */
19020Sstevel@tonic-gate 	wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD);
19030Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 	if (wakeup) {
19060Sstevel@tonic-gate 		/*
19070Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
19080Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
19090Sstevel@tonic-gate 		 */
19100Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
19110Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
19120Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
19130Sstevel@tonic-gate 	}
19140Sstevel@tonic-gate }
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate void
19170Sstevel@tonic-gate zone_cred_hold(zone_t *z)
19180Sstevel@tonic-gate {
19190Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
19200Sstevel@tonic-gate 	z->zone_cred_ref++;
19210Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
19220Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
19230Sstevel@tonic-gate }
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate void
19260Sstevel@tonic-gate zone_cred_rele(zone_t *z)
19270Sstevel@tonic-gate {
19280Sstevel@tonic-gate 	boolean_t wakeup;
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
19310Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
19320Sstevel@tonic-gate 	z->zone_cred_ref--;
19330Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
19340Sstevel@tonic-gate 		/* no more refs, free the structure */
19350Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
19360Sstevel@tonic-gate 		zone_free(z);
19370Sstevel@tonic-gate 		return;
19380Sstevel@tonic-gate 	}
19390Sstevel@tonic-gate 	/*
19400Sstevel@tonic-gate 	 * If zone_destroy is waiting for the cred references to drain
19410Sstevel@tonic-gate 	 * out, and they have, signal it.
19420Sstevel@tonic-gate 	 */
19430Sstevel@tonic-gate 	wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) &&
19440Sstevel@tonic-gate 	    zone_status_get(z) >= ZONE_IS_DEAD);
19450Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 	if (wakeup) {
19480Sstevel@tonic-gate 		/*
19490Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
19500Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
19510Sstevel@tonic-gate 		 */
19520Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
19530Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
19540Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
19550Sstevel@tonic-gate 	}
19560Sstevel@tonic-gate }
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate void
19590Sstevel@tonic-gate zone_task_hold(zone_t *z)
19600Sstevel@tonic-gate {
19610Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
19620Sstevel@tonic-gate 	z->zone_ntasks++;
19630Sstevel@tonic-gate 	ASSERT(z->zone_ntasks != 0);
19640Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
19650Sstevel@tonic-gate }
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate void
19680Sstevel@tonic-gate zone_task_rele(zone_t *zone)
19690Sstevel@tonic-gate {
19700Sstevel@tonic-gate 	uint_t refcnt;
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
19730Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks != 0);
19740Sstevel@tonic-gate 	refcnt = --zone->zone_ntasks;
19750Sstevel@tonic-gate 	if (refcnt > 1)	{	/* Common case */
19760Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
19770Sstevel@tonic-gate 		return;
19780Sstevel@tonic-gate 	}
19790Sstevel@tonic-gate 	zone_hold_locked(zone);	/* so we can use the zone_t later */
19800Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
19810Sstevel@tonic-gate 	if (refcnt == 1) {
19820Sstevel@tonic-gate 		/*
19830Sstevel@tonic-gate 		 * See if the zone is shutting down.
19840Sstevel@tonic-gate 		 */
19850Sstevel@tonic-gate 		mutex_enter(&zone_status_lock);
19860Sstevel@tonic-gate 		if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) {
19870Sstevel@tonic-gate 			goto out;
19880Sstevel@tonic-gate 		}
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 		/*
19910Sstevel@tonic-gate 		 * Make sure the ntasks didn't change since we
19920Sstevel@tonic-gate 		 * dropped zone_lock.
19930Sstevel@tonic-gate 		 */
19940Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
19950Sstevel@tonic-gate 		if (refcnt != zone->zone_ntasks) {
19960Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
19970Sstevel@tonic-gate 			goto out;
19980Sstevel@tonic-gate 		}
19990Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate 		/*
20020Sstevel@tonic-gate 		 * No more user processes in the zone.  The zone is empty.
20030Sstevel@tonic-gate 		 */
20040Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_EMPTY);
20050Sstevel@tonic-gate 		goto out;
20060Sstevel@tonic-gate 	}
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate 	ASSERT(refcnt == 0);
20090Sstevel@tonic-gate 	/*
20100Sstevel@tonic-gate 	 * zsched has exited; the zone is dead.
20110Sstevel@tonic-gate 	 */
20120Sstevel@tonic-gate 	zone->zone_zsched = NULL;		/* paranoia */
20130Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
20140Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_DEAD);
20150Sstevel@tonic-gate out:
20160Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
20170Sstevel@tonic-gate 	zone_rele(zone);
20180Sstevel@tonic-gate }
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate zoneid_t
20210Sstevel@tonic-gate getzoneid(void)
20220Sstevel@tonic-gate {
20230Sstevel@tonic-gate 	return (curproc->p_zone->zone_id);
20240Sstevel@tonic-gate }
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate /*
20270Sstevel@tonic-gate  * Internal versions of zone_find_by_*().  These don't zone_hold() or
20280Sstevel@tonic-gate  * check the validity of a zone's state.
20290Sstevel@tonic-gate  */
20300Sstevel@tonic-gate static zone_t *
20310Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid)
20320Sstevel@tonic-gate {
20330Sstevel@tonic-gate 	mod_hash_val_t hv;
20340Sstevel@tonic-gate 	zone_t *zone = NULL;
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyid,
20390Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0)
20400Sstevel@tonic-gate 		zone = (zone_t *)hv;
20410Sstevel@tonic-gate 	return (zone);
20420Sstevel@tonic-gate }
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate static zone_t *
20451676Sjpk zone_find_all_by_label(const ts_label_t *label)
20461676Sjpk {
20471676Sjpk 	mod_hash_val_t hv;
20481676Sjpk 	zone_t *zone = NULL;
20491676Sjpk 
20501676Sjpk 	ASSERT(MUTEX_HELD(&zonehash_lock));
20511676Sjpk 
20521676Sjpk 	/*
20531676Sjpk 	 * zonehashbylabel is not maintained for unlabeled systems
20541676Sjpk 	 */
20551676Sjpk 	if (!is_system_labeled())
20561676Sjpk 		return (NULL);
20571676Sjpk 	if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0)
20581676Sjpk 		zone = (zone_t *)hv;
20591676Sjpk 	return (zone);
20601676Sjpk }
20611676Sjpk 
20621676Sjpk static zone_t *
20630Sstevel@tonic-gate zone_find_all_by_name(char *name)
20640Sstevel@tonic-gate {
20650Sstevel@tonic-gate 	mod_hash_val_t hv;
20660Sstevel@tonic-gate 	zone_t *zone = NULL;
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0)
20710Sstevel@tonic-gate 		zone = (zone_t *)hv;
20720Sstevel@tonic-gate 	return (zone);
20730Sstevel@tonic-gate }
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate /*
20760Sstevel@tonic-gate  * Public interface for looking up a zone by zoneid.  Only returns the zone if
20770Sstevel@tonic-gate  * it is fully initialized, and has not yet begun the zone_destroy() sequence.
20780Sstevel@tonic-gate  * Caller must call zone_rele() once it is done with the zone.
20790Sstevel@tonic-gate  *
20800Sstevel@tonic-gate  * The zone may begin the zone_destroy() sequence immediately after this
20810Sstevel@tonic-gate  * function returns, but may be safely used until zone_rele() is called.
20820Sstevel@tonic-gate  */
20830Sstevel@tonic-gate zone_t *
20840Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid)
20850Sstevel@tonic-gate {
20860Sstevel@tonic-gate 	zone_t *zone;
20870Sstevel@tonic-gate 	zone_status_t status;
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
20900Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
20910Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
20920Sstevel@tonic-gate 		return (NULL);
20930Sstevel@tonic-gate 	}
20940Sstevel@tonic-gate 	status = zone_status_get(zone);
20950Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
20960Sstevel@tonic-gate 		/*
20970Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
20980Sstevel@tonic-gate 		 */
20990Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
21000Sstevel@tonic-gate 		return (NULL);
21010Sstevel@tonic-gate 	}
21020Sstevel@tonic-gate 	zone_hold(zone);
21030Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
21040Sstevel@tonic-gate 	return (zone);
21050Sstevel@tonic-gate }
21060Sstevel@tonic-gate 
21070Sstevel@tonic-gate /*
21081676Sjpk  * Similar to zone_find_by_id, but using zone label as the key.
21091676Sjpk  */
21101676Sjpk zone_t *
21111676Sjpk zone_find_by_label(const ts_label_t *label)
21121676Sjpk {
21131676Sjpk 	zone_t *zone;
21142110Srica 	zone_status_t status;
21151676Sjpk 
21161676Sjpk 	mutex_enter(&zonehash_lock);
21171676Sjpk 	if ((zone = zone_find_all_by_label(label)) == NULL) {
21181676Sjpk 		mutex_exit(&zonehash_lock);
21191676Sjpk 		return (NULL);
21201676Sjpk 	}
21212110Srica 
21222110Srica 	status = zone_status_get(zone);
21232110Srica 	if (status > ZONE_IS_DOWN) {
21241676Sjpk 		/*
21251676Sjpk 		 * For all practical purposes the zone doesn't exist.
21261676Sjpk 		 */
21272110Srica 		mutex_exit(&zonehash_lock);
21282110Srica 		return (NULL);
21291676Sjpk 	}
21302110Srica 	zone_hold(zone);
21311676Sjpk 	mutex_exit(&zonehash_lock);
21321676Sjpk 	return (zone);
21331676Sjpk }
21341676Sjpk 
21351676Sjpk /*
21360Sstevel@tonic-gate  * Similar to zone_find_by_id, but using zone name as the key.
21370Sstevel@tonic-gate  */
21380Sstevel@tonic-gate zone_t *
21390Sstevel@tonic-gate zone_find_by_name(char *name)
21400Sstevel@tonic-gate {
21410Sstevel@tonic-gate 	zone_t *zone;
21420Sstevel@tonic-gate 	zone_status_t status;
21430Sstevel@tonic-gate 
21440Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
21450Sstevel@tonic-gate 	if ((zone = zone_find_all_by_name(name)) == NULL) {
21460Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
21470Sstevel@tonic-gate 		return (NULL);
21480Sstevel@tonic-gate 	}
21490Sstevel@tonic-gate 	status = zone_status_get(zone);
21500Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
21510Sstevel@tonic-gate 		/*
21520Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
21530Sstevel@tonic-gate 		 */
21540Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
21550Sstevel@tonic-gate 		return (NULL);
21560Sstevel@tonic-gate 	}
21570Sstevel@tonic-gate 	zone_hold(zone);
21580Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
21590Sstevel@tonic-gate 	return (zone);
21600Sstevel@tonic-gate }
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate /*
21630Sstevel@tonic-gate  * Similar to zone_find_by_id(), using the path as a key.  For instance,
21640Sstevel@tonic-gate  * if there is a zone "foo" rooted at /foo/root, and the path argument
21650Sstevel@tonic-gate  * is "/foo/root/proc", it will return the held zone_t corresponding to
21660Sstevel@tonic-gate  * zone "foo".
21670Sstevel@tonic-gate  *
21680Sstevel@tonic-gate  * zone_find_by_path() always returns a non-NULL value, since at the
21690Sstevel@tonic-gate  * very least every path will be contained in the global zone.
21700Sstevel@tonic-gate  *
21710Sstevel@tonic-gate  * As with the other zone_find_by_*() functions, the caller is
21720Sstevel@tonic-gate  * responsible for zone_rele()ing the return value of this function.
21730Sstevel@tonic-gate  */
21740Sstevel@tonic-gate zone_t *
21750Sstevel@tonic-gate zone_find_by_path(const char *path)
21760Sstevel@tonic-gate {
21770Sstevel@tonic-gate 	zone_t *zone;
21780Sstevel@tonic-gate 	zone_t *zret = NULL;
21790Sstevel@tonic-gate 	zone_status_t status;
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 	if (path == NULL) {
21820Sstevel@tonic-gate 		/*
21830Sstevel@tonic-gate 		 * Call from rootconf().
21840Sstevel@tonic-gate 		 */
21850Sstevel@tonic-gate 		zone_hold(global_zone);
21860Sstevel@tonic-gate 		return (global_zone);
21870Sstevel@tonic-gate 	}
21880Sstevel@tonic-gate 	ASSERT(*path == '/');
21890Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
21900Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
21910Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
21920Sstevel@tonic-gate 		if (ZONE_PATH_VISIBLE(path, zone))
21930Sstevel@tonic-gate 			zret = zone;
21940Sstevel@tonic-gate 	}
21950Sstevel@tonic-gate 	ASSERT(zret != NULL);
21960Sstevel@tonic-gate 	status = zone_status_get(zret);
21970Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
21980Sstevel@tonic-gate 		/*
21990Sstevel@tonic-gate 		 * Zone practically doesn't exist.
22000Sstevel@tonic-gate 		 */
22010Sstevel@tonic-gate 		zret = global_zone;
22020Sstevel@tonic-gate 	}
22030Sstevel@tonic-gate 	zone_hold(zret);
22040Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
22050Sstevel@tonic-gate 	return (zret);
22060Sstevel@tonic-gate }
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate /*
22090Sstevel@tonic-gate  * Get the number of cpus visible to this zone.  The system-wide global
22100Sstevel@tonic-gate  * 'ncpus' is returned if pools are disabled, the caller is in the
22110Sstevel@tonic-gate  * global zone, or a NULL zone argument is passed in.
22120Sstevel@tonic-gate  */
22130Sstevel@tonic-gate int
22140Sstevel@tonic-gate zone_ncpus_get(zone_t *zone)
22150Sstevel@tonic-gate {
22160Sstevel@tonic-gate 	int myncpus = zone == NULL ? 0 : zone->zone_ncpus;
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate 	return (myncpus != 0 ? myncpus : ncpus);
22190Sstevel@tonic-gate }
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate /*
22220Sstevel@tonic-gate  * Get the number of online cpus visible to this zone.  The system-wide
22230Sstevel@tonic-gate  * global 'ncpus_online' is returned if pools are disabled, the caller
22240Sstevel@tonic-gate  * is in the global zone, or a NULL zone argument is passed in.
22250Sstevel@tonic-gate  */
22260Sstevel@tonic-gate int
22270Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone)
22280Sstevel@tonic-gate {
22290Sstevel@tonic-gate 	int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online;
22300Sstevel@tonic-gate 
22310Sstevel@tonic-gate 	return (myncpus_online != 0 ? myncpus_online : ncpus_online);
22320Sstevel@tonic-gate }
22330Sstevel@tonic-gate 
22340Sstevel@tonic-gate /*
22350Sstevel@tonic-gate  * Return the pool to which the zone is currently bound.
22360Sstevel@tonic-gate  */
22370Sstevel@tonic-gate pool_t *
22380Sstevel@tonic-gate zone_pool_get(zone_t *zone)
22390Sstevel@tonic-gate {
22400Sstevel@tonic-gate 	ASSERT(pool_lock_held());
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate 	return (zone->zone_pool);
22430Sstevel@tonic-gate }
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate /*
22460Sstevel@tonic-gate  * Set the zone's pool pointer and update the zone's visibility to match
22470Sstevel@tonic-gate  * the resources in the new pool.
22480Sstevel@tonic-gate  */
22490Sstevel@tonic-gate void
22500Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool)
22510Sstevel@tonic-gate {
22520Sstevel@tonic-gate 	ASSERT(pool_lock_held());
22530Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 	zone->zone_pool = pool;
22560Sstevel@tonic-gate 	zone_pset_set(zone, pool->pool_pset->pset_id);
22570Sstevel@tonic-gate }
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate /*
22600Sstevel@tonic-gate  * Return the cached value of the id of the processor set to which the
22610Sstevel@tonic-gate  * zone is currently bound.  The value will be ZONE_PS_INVAL if the pools
22620Sstevel@tonic-gate  * facility is disabled.
22630Sstevel@tonic-gate  */
22640Sstevel@tonic-gate psetid_t
22650Sstevel@tonic-gate zone_pset_get(zone_t *zone)
22660Sstevel@tonic-gate {
22670Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 	return (zone->zone_psetid);
22700Sstevel@tonic-gate }
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate /*
22730Sstevel@tonic-gate  * Set the cached value of the id of the processor set to which the zone
22740Sstevel@tonic-gate  * is currently bound.  Also update the zone's visibility to match the
22750Sstevel@tonic-gate  * resources in the new processor set.
22760Sstevel@tonic-gate  */
22770Sstevel@tonic-gate void
22780Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid)
22790Sstevel@tonic-gate {
22800Sstevel@tonic-gate 	psetid_t oldpsetid;
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
22830Sstevel@tonic-gate 	oldpsetid = zone_pset_get(zone);
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 	if (oldpsetid == newpsetid)
22860Sstevel@tonic-gate 		return;
22870Sstevel@tonic-gate 	/*
22880Sstevel@tonic-gate 	 * Global zone sees all.
22890Sstevel@tonic-gate 	 */
22900Sstevel@tonic-gate 	if (zone != global_zone) {
22910Sstevel@tonic-gate 		zone->zone_psetid = newpsetid;
22920Sstevel@tonic-gate 		if (newpsetid != ZONE_PS_INVAL)
22930Sstevel@tonic-gate 			pool_pset_visibility_add(newpsetid, zone);
22940Sstevel@tonic-gate 		if (oldpsetid != ZONE_PS_INVAL)
22950Sstevel@tonic-gate 			pool_pset_visibility_remove(oldpsetid, zone);
22960Sstevel@tonic-gate 	}
22970Sstevel@tonic-gate 	/*
22980Sstevel@tonic-gate 	 * Disabling pools, so we should start using the global values
22990Sstevel@tonic-gate 	 * for ncpus and ncpus_online.
23000Sstevel@tonic-gate 	 */
23010Sstevel@tonic-gate 	if (newpsetid == ZONE_PS_INVAL) {
23020Sstevel@tonic-gate 		zone->zone_ncpus = 0;
23030Sstevel@tonic-gate 		zone->zone_ncpus_online = 0;
23040Sstevel@tonic-gate 	}
23050Sstevel@tonic-gate }
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate /*
23080Sstevel@tonic-gate  * Walk the list of active zones and issue the provided callback for
23090Sstevel@tonic-gate  * each of them.
23100Sstevel@tonic-gate  *
23110Sstevel@tonic-gate  * Caller must not be holding any locks that may be acquired under
23120Sstevel@tonic-gate  * zonehash_lock.  See comment at the beginning of the file for a list of
23130Sstevel@tonic-gate  * common locks and their interactions with zones.
23140Sstevel@tonic-gate  */
23150Sstevel@tonic-gate int
23160Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data)
23170Sstevel@tonic-gate {
23180Sstevel@tonic-gate 	zone_t *zone;
23190Sstevel@tonic-gate 	int ret = 0;
23200Sstevel@tonic-gate 	zone_status_t status;
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
23230Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
23240Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
23250Sstevel@tonic-gate 		/*
23260Sstevel@tonic-gate 		 * Skip zones that shouldn't be externally visible.
23270Sstevel@tonic-gate 		 */
23280Sstevel@tonic-gate 		status = zone_status_get(zone);
23290Sstevel@tonic-gate 		if (status < ZONE_IS_READY || status > ZONE_IS_DOWN)
23300Sstevel@tonic-gate 			continue;
23310Sstevel@tonic-gate 		/*
23320Sstevel@tonic-gate 		 * Bail immediately if any callback invocation returns a
23330Sstevel@tonic-gate 		 * non-zero value.
23340Sstevel@tonic-gate 		 */
23350Sstevel@tonic-gate 		ret = (*cb)(zone, data);
23360Sstevel@tonic-gate 		if (ret != 0)
23370Sstevel@tonic-gate 			break;
23380Sstevel@tonic-gate 	}
23390Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
23400Sstevel@tonic-gate 	return (ret);
23410Sstevel@tonic-gate }
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate static int
23440Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath)
23450Sstevel@tonic-gate {
23460Sstevel@tonic-gate 	vnode_t *vp;
23470Sstevel@tonic-gate 	int trycount;
23480Sstevel@tonic-gate 	int error = 0;
23490Sstevel@tonic-gate 	char *path;
23500Sstevel@tonic-gate 	struct pathname upn, pn;
23510Sstevel@tonic-gate 	size_t pathlen;
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate 	if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0)
23540Sstevel@tonic-gate 		return (error);
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate 	pn_alloc(&pn);
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate 	/* prevent infinite loop */
23590Sstevel@tonic-gate 	trycount = 10;
23600Sstevel@tonic-gate 	for (;;) {
23610Sstevel@tonic-gate 		if (--trycount <= 0) {
23620Sstevel@tonic-gate 			error = ESTALE;
23630Sstevel@tonic-gate 			goto out;
23640Sstevel@tonic-gate 		}
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate 		if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) {
23670Sstevel@tonic-gate 			/*
23680Sstevel@tonic-gate 			 * VOP_ACCESS() may cover 'vp' with a new
23690Sstevel@tonic-gate 			 * filesystem, if 'vp' is an autoFS vnode.
23700Sstevel@tonic-gate 			 * Get the new 'vp' if so.
23710Sstevel@tonic-gate 			 */
23720Sstevel@tonic-gate 			if ((error = VOP_ACCESS(vp, VEXEC, 0, CRED())) == 0 &&
23730Sstevel@tonic-gate 			    (vp->v_vfsmountedhere == NULL ||
23740Sstevel@tonic-gate 			    (error = traverse(&vp)) == 0)) {
23750Sstevel@tonic-gate 				pathlen = pn.pn_pathlen + 2;
23760Sstevel@tonic-gate 				path = kmem_alloc(pathlen, KM_SLEEP);
23770Sstevel@tonic-gate 				(void) strncpy(path, pn.pn_path,
23780Sstevel@tonic-gate 				    pn.pn_pathlen + 1);
23790Sstevel@tonic-gate 				path[pathlen - 2] = '/';
23800Sstevel@tonic-gate 				path[pathlen - 1] = '\0';
23810Sstevel@tonic-gate 				pn_free(&pn);
23820Sstevel@tonic-gate 				pn_free(&upn);
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate 				/* Success! */
23850Sstevel@tonic-gate 				break;
23860Sstevel@tonic-gate 			}
23870Sstevel@tonic-gate 			VN_RELE(vp);
23880Sstevel@tonic-gate 		}
23890Sstevel@tonic-gate 		if (error != ESTALE)
23900Sstevel@tonic-gate 			goto out;
23910Sstevel@tonic-gate 	}
23920Sstevel@tonic-gate 
23930Sstevel@tonic-gate 	ASSERT(error == 0);
23940Sstevel@tonic-gate 	zone->zone_rootvp = vp;		/* we hold a reference to vp */
23950Sstevel@tonic-gate 	zone->zone_rootpath = path;
23960Sstevel@tonic-gate 	zone->zone_rootpathlen = pathlen;
23971769Scarlsonj 	if (pathlen > 5 && strcmp(path + pathlen - 5, "/lu/") == 0)
23981769Scarlsonj 		zone->zone_flags |= ZF_IS_SCRATCH;
23990Sstevel@tonic-gate 	return (0);
24000Sstevel@tonic-gate 
24010Sstevel@tonic-gate out:
24020Sstevel@tonic-gate 	pn_free(&pn);
24030Sstevel@tonic-gate 	pn_free(&upn);
24040Sstevel@tonic-gate 	return (error);
24050Sstevel@tonic-gate }
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate #define	isalnum(c)	(((c) >= '0' && (c) <= '9') || \
24080Sstevel@tonic-gate 			((c) >= 'a' && (c) <= 'z') || \
24090Sstevel@tonic-gate 			((c) >= 'A' && (c) <= 'Z'))
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate static int
24120Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname)
24130Sstevel@tonic-gate {
24140Sstevel@tonic-gate 	char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
24150Sstevel@tonic-gate 	size_t len;
24160Sstevel@tonic-gate 	int i, err;
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate 	if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) {
24190Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
24200Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
24210Sstevel@tonic-gate 	}
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 	/* must be less than ZONENAME_MAX */
24240Sstevel@tonic-gate 	if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') {
24250Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
24260Sstevel@tonic-gate 		return (EINVAL);
24270Sstevel@tonic-gate 	}
24280Sstevel@tonic-gate 
24290Sstevel@tonic-gate 	/*
24300Sstevel@tonic-gate 	 * Name must start with an alphanumeric and must contain only
24310Sstevel@tonic-gate 	 * alphanumerics, '-', '_' and '.'.
24320Sstevel@tonic-gate 	 */
24330Sstevel@tonic-gate 	if (!isalnum(kname[0])) {
24340Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
24350Sstevel@tonic-gate 		return (EINVAL);
24360Sstevel@tonic-gate 	}
24370Sstevel@tonic-gate 	for (i = 1; i < len - 1; i++) {
24380Sstevel@tonic-gate 		if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' &&
24390Sstevel@tonic-gate 		    kname[i] != '.') {
24400Sstevel@tonic-gate 			kmem_free(kname, ZONENAME_MAX);
24410Sstevel@tonic-gate 			return (EINVAL);
24420Sstevel@tonic-gate 		}
24430Sstevel@tonic-gate 	}
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate 	zone->zone_name = kname;
24460Sstevel@tonic-gate 	return (0);
24470Sstevel@tonic-gate }
24480Sstevel@tonic-gate 
24490Sstevel@tonic-gate /*
24500Sstevel@tonic-gate  * Similar to thread_create(), but makes sure the thread is in the appropriate
24510Sstevel@tonic-gate  * zone's zsched process (curproc->p_zone->zone_zsched) before returning.
24520Sstevel@tonic-gate  */
24530Sstevel@tonic-gate /*ARGSUSED*/
24540Sstevel@tonic-gate kthread_t *
24550Sstevel@tonic-gate zthread_create(
24560Sstevel@tonic-gate     caddr_t stk,
24570Sstevel@tonic-gate     size_t stksize,
24580Sstevel@tonic-gate     void (*proc)(),
24590Sstevel@tonic-gate     void *arg,
24600Sstevel@tonic-gate     size_t len,
24610Sstevel@tonic-gate     pri_t pri)
24620Sstevel@tonic-gate {
24630Sstevel@tonic-gate 	kthread_t *t;
24640Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
24650Sstevel@tonic-gate 	proc_t *pp = zone->zone_zsched;
24660Sstevel@tonic-gate 
24670Sstevel@tonic-gate 	zone_hold(zone);	/* Reference to be dropped when thread exits */
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 	/*
24700Sstevel@tonic-gate 	 * No-one should be trying to create threads if the zone is shutting
24710Sstevel@tonic-gate 	 * down and there aren't any kernel threads around.  See comment
24720Sstevel@tonic-gate 	 * in zthread_exit().
24730Sstevel@tonic-gate 	 */
24740Sstevel@tonic-gate 	ASSERT(!(zone->zone_kthreads == NULL &&
24750Sstevel@tonic-gate 	    zone_status_get(zone) >= ZONE_IS_EMPTY));
24760Sstevel@tonic-gate 	/*
24770Sstevel@tonic-gate 	 * Create a thread, but don't let it run until we've finished setting
24780Sstevel@tonic-gate 	 * things up.
24790Sstevel@tonic-gate 	 */
24800Sstevel@tonic-gate 	t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri);
24810Sstevel@tonic-gate 	ASSERT(t->t_forw == NULL);
24820Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
24830Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL) {
24840Sstevel@tonic-gate 		t->t_forw = t->t_back = t;
24850Sstevel@tonic-gate 	} else {
24860Sstevel@tonic-gate 		kthread_t *tx = zone->zone_kthreads;
24870Sstevel@tonic-gate 
24880Sstevel@tonic-gate 		t->t_forw = tx;
24890Sstevel@tonic-gate 		t->t_back = tx->t_back;
24900Sstevel@tonic-gate 		tx->t_back->t_forw = t;
24910Sstevel@tonic-gate 		tx->t_back = t;
24920Sstevel@tonic-gate 	}
24930Sstevel@tonic-gate 	zone->zone_kthreads = t;
24940Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
24950Sstevel@tonic-gate 
24960Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
24970Sstevel@tonic-gate 	t->t_proc_flag |= TP_ZTHREAD;
24980Sstevel@tonic-gate 	project_rele(t->t_proj);
24990Sstevel@tonic-gate 	t->t_proj = project_hold(pp->p_task->tk_proj);
25000Sstevel@tonic-gate 
25010Sstevel@tonic-gate 	/*
25020Sstevel@tonic-gate 	 * Setup complete, let it run.
25030Sstevel@tonic-gate 	 */
25040Sstevel@tonic-gate 	thread_lock(t);
25050Sstevel@tonic-gate 	t->t_schedflag |= TS_ALLSTART;
25060Sstevel@tonic-gate 	setrun_locked(t);
25070Sstevel@tonic-gate 	thread_unlock(t);
25080Sstevel@tonic-gate 
25090Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
25100Sstevel@tonic-gate 
25110Sstevel@tonic-gate 	return (t);
25120Sstevel@tonic-gate }
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate /*
25150Sstevel@tonic-gate  * Similar to thread_exit().  Must be called by threads created via
25160Sstevel@tonic-gate  * zthread_exit().
25170Sstevel@tonic-gate  */
25180Sstevel@tonic-gate void
25190Sstevel@tonic-gate zthread_exit(void)
25200Sstevel@tonic-gate {
25210Sstevel@tonic-gate 	kthread_t *t = curthread;
25220Sstevel@tonic-gate 	proc_t *pp = curproc;
25230Sstevel@tonic-gate 	zone_t *zone = pp->p_zone;
25240Sstevel@tonic-gate 
25250Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
25260Sstevel@tonic-gate 
25270Sstevel@tonic-gate 	/*
25280Sstevel@tonic-gate 	 * Reparent to p0
25290Sstevel@tonic-gate 	 */
25301075Sjosephb 	kpreempt_disable();
25310Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
25320Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_ZTHREAD;
25330Sstevel@tonic-gate 	t->t_procp = &p0;
25340Sstevel@tonic-gate 	hat_thread_exit(t);
25350Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
25361075Sjosephb 	kpreempt_enable();
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 	if (t->t_back == t) {
25390Sstevel@tonic-gate 		ASSERT(t->t_forw == t);
25400Sstevel@tonic-gate 		/*
25410Sstevel@tonic-gate 		 * If the zone is empty, once the thread count
25420Sstevel@tonic-gate 		 * goes to zero no further kernel threads can be
25430Sstevel@tonic-gate 		 * created.  This is because if the creator is a process
25440Sstevel@tonic-gate 		 * in the zone, then it must have exited before the zone
25450Sstevel@tonic-gate 		 * state could be set to ZONE_IS_EMPTY.
25460Sstevel@tonic-gate 		 * Otherwise, if the creator is a kernel thread in the
25470Sstevel@tonic-gate 		 * zone, the thread count is non-zero.
25480Sstevel@tonic-gate 		 *
25490Sstevel@tonic-gate 		 * This really means that non-zone kernel threads should
25500Sstevel@tonic-gate 		 * not create zone kernel threads.
25510Sstevel@tonic-gate 		 */
25520Sstevel@tonic-gate 		zone->zone_kthreads = NULL;
25530Sstevel@tonic-gate 		if (zone_status_get(zone) == ZONE_IS_EMPTY) {
25540Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_DOWN);
2555*3792Sakolb 			/*
2556*3792Sakolb 			 * Remove any CPU caps on this zone.
2557*3792Sakolb 			 */
2558*3792Sakolb 			cpucaps_zone_remove(zone);
25590Sstevel@tonic-gate 		}
25600Sstevel@tonic-gate 	} else {
25610Sstevel@tonic-gate 		t->t_forw->t_back = t->t_back;
25620Sstevel@tonic-gate 		t->t_back->t_forw = t->t_forw;
25630Sstevel@tonic-gate 		if (zone->zone_kthreads == t)
25640Sstevel@tonic-gate 			zone->zone_kthreads = t->t_forw;
25650Sstevel@tonic-gate 	}
25660Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
25670Sstevel@tonic-gate 	zone_rele(zone);
25680Sstevel@tonic-gate 	thread_exit();
25690Sstevel@tonic-gate 	/* NOTREACHED */
25700Sstevel@tonic-gate }
25710Sstevel@tonic-gate 
25720Sstevel@tonic-gate static void
25730Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp)
25740Sstevel@tonic-gate {
25750Sstevel@tonic-gate 	vnode_t *oldvp;
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate 	/* we're going to hold a reference here to the directory */
25780Sstevel@tonic-gate 	VN_HOLD(vp);
25790Sstevel@tonic-gate 
25800Sstevel@tonic-gate #ifdef C2_AUDIT
25810Sstevel@tonic-gate 	if (audit_active)	/* update abs cwd/root path see c2audit.c */
25820Sstevel@tonic-gate 		audit_chdirec(vp, vpp);
25830Sstevel@tonic-gate #endif
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
25860Sstevel@tonic-gate 	oldvp = *vpp;
25870Sstevel@tonic-gate 	*vpp = vp;
25880Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
25890Sstevel@tonic-gate 	if (oldvp != NULL)
25900Sstevel@tonic-gate 		VN_RELE(oldvp);
25910Sstevel@tonic-gate }
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate /*
25940Sstevel@tonic-gate  * Convert an rctl value represented by an nvlist_t into an rctl_val_t.
25950Sstevel@tonic-gate  */
25960Sstevel@tonic-gate static int
25970Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv)
25980Sstevel@tonic-gate {
25990Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
26000Sstevel@tonic-gate 	boolean_t priv_set = B_FALSE;
26010Sstevel@tonic-gate 	boolean_t limit_set = B_FALSE;
26020Sstevel@tonic-gate 	boolean_t action_set = B_FALSE;
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
26050Sstevel@tonic-gate 		const char *name;
26060Sstevel@tonic-gate 		uint64_t ui64;
26070Sstevel@tonic-gate 
26080Sstevel@tonic-gate 		name = nvpair_name(nvp);
26090Sstevel@tonic-gate 		if (nvpair_type(nvp) != DATA_TYPE_UINT64)
26100Sstevel@tonic-gate 			return (EINVAL);
26110Sstevel@tonic-gate 		(void) nvpair_value_uint64(nvp, &ui64);
26120Sstevel@tonic-gate 		if (strcmp(name, "privilege") == 0) {
26130Sstevel@tonic-gate 			/*
26140Sstevel@tonic-gate 			 * Currently only privileged values are allowed, but
26150Sstevel@tonic-gate 			 * this may change in the future.
26160Sstevel@tonic-gate 			 */
26170Sstevel@tonic-gate 			if (ui64 != RCPRIV_PRIVILEGED)
26180Sstevel@tonic-gate 				return (EINVAL);
26190Sstevel@tonic-gate 			rv->rcv_privilege = ui64;
26200Sstevel@tonic-gate 			priv_set = B_TRUE;
26210Sstevel@tonic-gate 		} else if (strcmp(name, "limit") == 0) {
26220Sstevel@tonic-gate 			rv->rcv_value = ui64;
26230Sstevel@tonic-gate 			limit_set = B_TRUE;
26240Sstevel@tonic-gate 		} else if (strcmp(name, "action") == 0) {
26250Sstevel@tonic-gate 			if (ui64 != RCTL_LOCAL_NOACTION &&
26260Sstevel@tonic-gate 			    ui64 != RCTL_LOCAL_DENY)
26270Sstevel@tonic-gate 				return (EINVAL);
26280Sstevel@tonic-gate 			rv->rcv_flagaction = ui64;
26290Sstevel@tonic-gate 			action_set = B_TRUE;
26300Sstevel@tonic-gate 		} else {
26310Sstevel@tonic-gate 			return (EINVAL);
26320Sstevel@tonic-gate 		}
26330Sstevel@tonic-gate 	}
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 	if (!(priv_set && limit_set && action_set))
26360Sstevel@tonic-gate 		return (EINVAL);
26370Sstevel@tonic-gate 	rv->rcv_action_signal = 0;
26380Sstevel@tonic-gate 	rv->rcv_action_recipient = NULL;
26390Sstevel@tonic-gate 	rv->rcv_action_recip_pid = -1;
26400Sstevel@tonic-gate 	rv->rcv_firing_time = 0;
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 	return (0);
26430Sstevel@tonic-gate }
26440Sstevel@tonic-gate 
26452267Sdp /*
26462267Sdp  * Non-global zone version of start_init.
26472267Sdp  */
26480Sstevel@tonic-gate void
26492267Sdp zone_start_init(void)
26500Sstevel@tonic-gate {
26510Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
26522712Snn35248 	zone_t *z = p->p_zone;
26532267Sdp 
26542267Sdp 	ASSERT(!INGLOBALZONE(curproc));
26550Sstevel@tonic-gate 
26560Sstevel@tonic-gate 	/*
26572712Snn35248 	 * For all purposes (ZONE_ATTR_INITPID and restart_init),
26582712Snn35248 	 * storing just the pid of init is sufficient.
26592712Snn35248 	 */
26602712Snn35248 	z->zone_proc_initpid = p->p_pid;
26612712Snn35248 
26622712Snn35248 	/*
26632267Sdp 	 * We maintain zone_boot_err so that we can return the cause of the
26642267Sdp 	 * failure back to the caller of the zone_boot syscall.
26650Sstevel@tonic-gate 	 */
26662267Sdp 	p->p_zone->zone_boot_err = start_init_common();
26670Sstevel@tonic-gate 
26680Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
26692712Snn35248 	if (z->zone_boot_err != 0) {
26700Sstevel@tonic-gate 		/*
26710Sstevel@tonic-gate 		 * Make sure we are still in the booting state-- we could have
26720Sstevel@tonic-gate 		 * raced and already be shutting down, or even further along.
26730Sstevel@tonic-gate 		 */
2674*3792Sakolb 		if (zone_status_get(z) == ZONE_IS_BOOTING) {
26752712Snn35248 			zone_status_set(z, ZONE_IS_SHUTTING_DOWN);
2676*3792Sakolb 		}
26770Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
26780Sstevel@tonic-gate 		/* It's gone bad, dispose of the process */
26792712Snn35248 		if (proc_exit(CLD_EXITED, z->zone_boot_err) != 0) {
2680390Sraf 			mutex_enter(&p->p_lock);
2681390Sraf 			ASSERT(p->p_flag & SEXITLWPS);
26820Sstevel@tonic-gate 			lwp_exit();
26830Sstevel@tonic-gate 		}
26840Sstevel@tonic-gate 	} else {
26852712Snn35248 		if (zone_status_get(z) == ZONE_IS_BOOTING)
26862712Snn35248 			zone_status_set(z, ZONE_IS_RUNNING);
26870Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
26880Sstevel@tonic-gate 		/* cause the process to return to userland. */
26890Sstevel@tonic-gate 		lwp_rtt();
26900Sstevel@tonic-gate 	}
26910Sstevel@tonic-gate }
26920Sstevel@tonic-gate 
26930Sstevel@tonic-gate struct zsched_arg {
26940Sstevel@tonic-gate 	zone_t *zone;
26950Sstevel@tonic-gate 	nvlist_t *nvlist;
26960Sstevel@tonic-gate };
26970Sstevel@tonic-gate 
26980Sstevel@tonic-gate /*
26990Sstevel@tonic-gate  * Per-zone "sched" workalike.  The similarity to "sched" doesn't have
27000Sstevel@tonic-gate  * anything to do with scheduling, but rather with the fact that
27010Sstevel@tonic-gate  * per-zone kernel threads are parented to zsched, just like regular
27020Sstevel@tonic-gate  * kernel threads are parented to sched (p0).
27030Sstevel@tonic-gate  *
27040Sstevel@tonic-gate  * zsched is also responsible for launching init for the zone.
27050Sstevel@tonic-gate  */
27060Sstevel@tonic-gate static void
27070Sstevel@tonic-gate zsched(void *arg)
27080Sstevel@tonic-gate {
27090Sstevel@tonic-gate 	struct zsched_arg *za = arg;
27100Sstevel@tonic-gate 	proc_t *pp = curproc;
27110Sstevel@tonic-gate 	proc_t *initp = proc_init;
27120Sstevel@tonic-gate 	zone_t *zone = za->zone;
27130Sstevel@tonic-gate 	cred_t *cr, *oldcred;
27140Sstevel@tonic-gate 	rctl_set_t *set;
27150Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
27160Sstevel@tonic-gate 	contract_t *ct = NULL;
27170Sstevel@tonic-gate 	task_t *tk, *oldtk;
27180Sstevel@tonic-gate 	rctl_entity_p_t e;
27190Sstevel@tonic-gate 	kproject_t *pj;
27200Sstevel@tonic-gate 
27210Sstevel@tonic-gate 	nvlist_t *nvl = za->nvlist;
27220Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
27230Sstevel@tonic-gate 
27243446Smrj 	bcopy("zsched", PTOU(pp)->u_psargs, sizeof ("zsched"));
27253446Smrj 	bcopy("zsched", PTOU(pp)->u_comm, sizeof ("zsched"));
27263446Smrj 	PTOU(pp)->u_argc = 0;
27273446Smrj 	PTOU(pp)->u_argv = NULL;
27283446Smrj 	PTOU(pp)->u_envp = NULL;
27290Sstevel@tonic-gate 	closeall(P_FINFO(pp));
27300Sstevel@tonic-gate 
27310Sstevel@tonic-gate 	/*
27320Sstevel@tonic-gate 	 * We are this zone's "zsched" process.  As the zone isn't generally
27330Sstevel@tonic-gate 	 * visible yet we don't need to grab any locks before initializing its
27340Sstevel@tonic-gate 	 * zone_proc pointer.
27350Sstevel@tonic-gate 	 */
27360Sstevel@tonic-gate 	zone_hold(zone);  /* this hold is released by zone_destroy() */
27370Sstevel@tonic-gate 	zone->zone_zsched = pp;
27380Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
27390Sstevel@tonic-gate 	pp->p_zone = zone;
27400Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate 	/*
27430Sstevel@tonic-gate 	 * Disassociate process from its 'parent'; parent ourselves to init
27440Sstevel@tonic-gate 	 * (pid 1) and change other values as needed.
27450Sstevel@tonic-gate 	 */
27460Sstevel@tonic-gate 	sess_create();
27470Sstevel@tonic-gate 
27480Sstevel@tonic-gate 	mutex_enter(&pidlock);
27490Sstevel@tonic-gate 	proc_detach(pp);
27500Sstevel@tonic-gate 	pp->p_ppid = 1;
27510Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
27520Sstevel@tonic-gate 	pp->p_ancpid = 1;
27530Sstevel@tonic-gate 	pp->p_parent = initp;
27540Sstevel@tonic-gate 	pp->p_psibling = NULL;
27550Sstevel@tonic-gate 	if (initp->p_child)
27560Sstevel@tonic-gate 		initp->p_child->p_psibling = pp;
27570Sstevel@tonic-gate 	pp->p_sibling = initp->p_child;
27580Sstevel@tonic-gate 	initp->p_child = pp;
27590Sstevel@tonic-gate 
27600Sstevel@tonic-gate 	/* Decrement what newproc() incremented. */
27610Sstevel@tonic-gate 	upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID);
27620Sstevel@tonic-gate 	/*
27630Sstevel@tonic-gate 	 * Our credentials are about to become kcred-like, so we don't care
27640Sstevel@tonic-gate 	 * about the caller's ruid.
27650Sstevel@tonic-gate 	 */
27660Sstevel@tonic-gate 	upcount_inc(crgetruid(kcred), zone->zone_id);
27670Sstevel@tonic-gate 	mutex_exit(&pidlock);
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate 	/*
27700Sstevel@tonic-gate 	 * getting out of global zone, so decrement lwp counts
27710Sstevel@tonic-gate 	 */
27720Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
27730Sstevel@tonic-gate 	mutex_enter(&global_zone->zone_nlwps_lock);
27740Sstevel@tonic-gate 	pj->kpj_nlwps -= pp->p_lwpcnt;
27750Sstevel@tonic-gate 	global_zone->zone_nlwps -= pp->p_lwpcnt;
27760Sstevel@tonic-gate 	mutex_exit(&global_zone->zone_nlwps_lock);
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 	/*
27792768Ssl108498 	 * Decrement locked memory counts on old zone and project.
27802768Ssl108498 	 */
27813247Sgjelinek 	mutex_enter(&global_zone->zone_mem_lock);
27822768Ssl108498 	global_zone->zone_locked_mem -= pp->p_locked_mem;
27832768Ssl108498 	pj->kpj_data.kpd_locked_mem -= pp->p_locked_mem;
27843247Sgjelinek 	mutex_exit(&global_zone->zone_mem_lock);
27852768Ssl108498 
27862768Ssl108498 	/*
27870Sstevel@tonic-gate 	 * Create and join a new task in project '0' of this zone.
27880Sstevel@tonic-gate 	 *
27890Sstevel@tonic-gate 	 * We don't need to call holdlwps() since we know we're the only lwp in
27900Sstevel@tonic-gate 	 * this process.
27910Sstevel@tonic-gate 	 *
27920Sstevel@tonic-gate 	 * task_join() returns with p_lock held.
27930Sstevel@tonic-gate 	 */
27940Sstevel@tonic-gate 	tk = task_create(0, zone);
27950Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
27960Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
27972768Ssl108498 
27982768Ssl108498 	pj = pp->p_task->tk_proj;
27992768Ssl108498 
28003247Sgjelinek 	mutex_enter(&zone->zone_mem_lock);
28012768Ssl108498 	zone->zone_locked_mem += pp->p_locked_mem;
28022768Ssl108498 	pj->kpj_data.kpd_locked_mem += pp->p_locked_mem;
28033247Sgjelinek 	mutex_exit(&zone->zone_mem_lock);
28040Sstevel@tonic-gate 
28050Sstevel@tonic-gate 	/*
28060Sstevel@tonic-gate 	 * add lwp counts to zsched's zone, and increment project's task count
28070Sstevel@tonic-gate 	 * due to the task created in the above tasksys_settaskid
28080Sstevel@tonic-gate 	 */
28092768Ssl108498 
28100Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
28110Sstevel@tonic-gate 	pj->kpj_nlwps += pp->p_lwpcnt;
28120Sstevel@tonic-gate 	pj->kpj_ntasks += 1;
28130Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
28140Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
28150Sstevel@tonic-gate 
28162768Ssl108498 	mutex_exit(&curproc->p_lock);
28172768Ssl108498 	mutex_exit(&cpu_lock);
28182768Ssl108498 	task_rele(oldtk);
28192768Ssl108498 
28200Sstevel@tonic-gate 	/*
28210Sstevel@tonic-gate 	 * The process was created by a process in the global zone, hence the
28220Sstevel@tonic-gate 	 * credentials are wrong.  We might as well have kcred-ish credentials.
28230Sstevel@tonic-gate 	 */
28240Sstevel@tonic-gate 	cr = zone->zone_kcred;
28250Sstevel@tonic-gate 	crhold(cr);
28260Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
28270Sstevel@tonic-gate 	oldcred = pp->p_cred;
28280Sstevel@tonic-gate 	pp->p_cred = cr;
28290Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
28300Sstevel@tonic-gate 	crfree(oldcred);
28310Sstevel@tonic-gate 
28320Sstevel@tonic-gate 	/*
28330Sstevel@tonic-gate 	 * Hold credentials again (for thread)
28340Sstevel@tonic-gate 	 */
28350Sstevel@tonic-gate 	crhold(cr);
28360Sstevel@tonic-gate 
28370Sstevel@tonic-gate 	/*
28380Sstevel@tonic-gate 	 * p_lwpcnt can't change since this is a kernel process.
28390Sstevel@tonic-gate 	 */
28400Sstevel@tonic-gate 	crset(pp, cr);
28410Sstevel@tonic-gate 
28420Sstevel@tonic-gate 	/*
28430Sstevel@tonic-gate 	 * Chroot
28440Sstevel@tonic-gate 	 */
28450Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp);
28460Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp);
28470Sstevel@tonic-gate 
28480Sstevel@tonic-gate 	/*
28490Sstevel@tonic-gate 	 * Initialize zone's rctl set.
28500Sstevel@tonic-gate 	 */
28510Sstevel@tonic-gate 	set = rctl_set_create();
28520Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
28530Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
28540Sstevel@tonic-gate 	e.rcep_p.zone = zone;
28550Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
28560Sstevel@tonic-gate 	zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp);
28570Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
28580Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate 	/*
28610Sstevel@tonic-gate 	 * Apply the rctls passed in to zone_create().  This is basically a list
28620Sstevel@tonic-gate 	 * assignment: all of the old values are removed and the new ones
28630Sstevel@tonic-gate 	 * inserted.  That is, if an empty list is passed in, all values are
28640Sstevel@tonic-gate 	 * removed.
28650Sstevel@tonic-gate 	 */
28660Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
28670Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
28680Sstevel@tonic-gate 		rctl_hndl_t hndl;
28690Sstevel@tonic-gate 		char *name;
28700Sstevel@tonic-gate 		nvlist_t **nvlarray;
28710Sstevel@tonic-gate 		uint_t i, nelem;
28720Sstevel@tonic-gate 		int error;	/* For ASSERT()s */
28730Sstevel@tonic-gate 
28740Sstevel@tonic-gate 		name = nvpair_name(nvp);
28750Sstevel@tonic-gate 		hndl = rctl_hndl_lookup(name);
28760Sstevel@tonic-gate 		ASSERT(hndl != -1);
28770Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
28780Sstevel@tonic-gate 		ASSERT(rde != NULL);
28790Sstevel@tonic-gate 
28800Sstevel@tonic-gate 		for (; /* ever */; ) {
28810Sstevel@tonic-gate 			rctl_val_t oval;
28820Sstevel@tonic-gate 
28830Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
28840Sstevel@tonic-gate 			error = rctl_local_get(hndl, NULL, &oval, pp);
28850Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
28860Sstevel@tonic-gate 			ASSERT(error == 0);	/* Can't fail for RCTL_FIRST */
28870Sstevel@tonic-gate 			ASSERT(oval.rcv_privilege != RCPRIV_BASIC);
28880Sstevel@tonic-gate 			if (oval.rcv_privilege == RCPRIV_SYSTEM)
28890Sstevel@tonic-gate 				break;
28900Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
28910Sstevel@tonic-gate 			error = rctl_local_delete(hndl, &oval, pp);
28920Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
28930Sstevel@tonic-gate 			ASSERT(error == 0);
28940Sstevel@tonic-gate 		}
28950Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
28960Sstevel@tonic-gate 		ASSERT(error == 0);
28970Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
28980Sstevel@tonic-gate 			rctl_val_t *nvalp;
28990Sstevel@tonic-gate 
29000Sstevel@tonic-gate 			nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
29010Sstevel@tonic-gate 			error = nvlist2rctlval(nvlarray[i], nvalp);
29020Sstevel@tonic-gate 			ASSERT(error == 0);
29030Sstevel@tonic-gate 			/*
29040Sstevel@tonic-gate 			 * rctl_local_insert can fail if the value being
29050Sstevel@tonic-gate 			 * inserted is a duplicate; this is OK.
29060Sstevel@tonic-gate 			 */
29070Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
29080Sstevel@tonic-gate 			if (rctl_local_insert(hndl, nvalp, pp) != 0)
29090Sstevel@tonic-gate 				kmem_cache_free(rctl_val_cache, nvalp);
29100Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
29110Sstevel@tonic-gate 		}
29120Sstevel@tonic-gate 	}
29130Sstevel@tonic-gate 	/*
29140Sstevel@tonic-gate 	 * Tell the world that we're done setting up.
29150Sstevel@tonic-gate 	 *
29160Sstevel@tonic-gate 	 * At this point we want to set the zone status to ZONE_IS_READY
29170Sstevel@tonic-gate 	 * and atomically set the zone's processor set visibility.  Once
29180Sstevel@tonic-gate 	 * we drop pool_lock() this zone will automatically get updated
29190Sstevel@tonic-gate 	 * to reflect any future changes to the pools configuration.
29200Sstevel@tonic-gate 	 */
29210Sstevel@tonic-gate 	pool_lock();
29220Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
29230Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
29240Sstevel@tonic-gate 	zone_uniqid(zone);
29250Sstevel@tonic-gate 	zone_zsd_configure(zone);
29260Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED)
29270Sstevel@tonic-gate 		zone_pset_set(zone, pool_default->pool_pset->pset_id);
29280Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
29290Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
29300Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_READY);
29310Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
29320Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
29330Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
29340Sstevel@tonic-gate 	pool_unlock();
29350Sstevel@tonic-gate 
29360Sstevel@tonic-gate 	/*
29370Sstevel@tonic-gate 	 * Once we see the zone transition to the ZONE_IS_BOOTING state,
29380Sstevel@tonic-gate 	 * we launch init, and set the state to running.
29390Sstevel@tonic-gate 	 */
29400Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched");
29410Sstevel@tonic-gate 
29420Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_BOOTING) {
29430Sstevel@tonic-gate 		id_t cid;
29440Sstevel@tonic-gate 
29450Sstevel@tonic-gate 		/*
29460Sstevel@tonic-gate 		 * Ok, this is a little complicated.  We need to grab the
29470Sstevel@tonic-gate 		 * zone's pool's scheduling class ID; note that by now, we
29480Sstevel@tonic-gate 		 * are already bound to a pool if we need to be (zoneadmd
29490Sstevel@tonic-gate 		 * will have done that to us while we're in the READY
29500Sstevel@tonic-gate 		 * state).  *But* the scheduling class for the zone's 'init'
29510Sstevel@tonic-gate 		 * must be explicitly passed to newproc, which doesn't
29520Sstevel@tonic-gate 		 * respect pool bindings.
29530Sstevel@tonic-gate 		 *
29540Sstevel@tonic-gate 		 * We hold the pool_lock across the call to newproc() to
29550Sstevel@tonic-gate 		 * close the obvious race: the pool's scheduling class
29560Sstevel@tonic-gate 		 * could change before we manage to create the LWP with
29570Sstevel@tonic-gate 		 * classid 'cid'.
29580Sstevel@tonic-gate 		 */
29590Sstevel@tonic-gate 		pool_lock();
29603247Sgjelinek 		if (zone->zone_defaultcid > 0)
29613247Sgjelinek 			cid = zone->zone_defaultcid;
29623247Sgjelinek 		else
29633247Sgjelinek 			cid = pool_get_class(zone->zone_pool);
29640Sstevel@tonic-gate 		if (cid == -1)
29650Sstevel@tonic-gate 			cid = defaultcid;
29660Sstevel@tonic-gate 
29670Sstevel@tonic-gate 		/*
29680Sstevel@tonic-gate 		 * If this fails, zone_boot will ultimately fail.  The
29690Sstevel@tonic-gate 		 * state of the zone will be set to SHUTTING_DOWN-- userland
29700Sstevel@tonic-gate 		 * will have to tear down the zone, and fail, or try again.
29710Sstevel@tonic-gate 		 */
29722267Sdp 		if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid,
29730Sstevel@tonic-gate 		    minclsyspri - 1, &ct)) != 0) {
29740Sstevel@tonic-gate 			mutex_enter(&zone_status_lock);
29750Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
29760Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
29770Sstevel@tonic-gate 		}
29780Sstevel@tonic-gate 		pool_unlock();
29790Sstevel@tonic-gate 	}
29800Sstevel@tonic-gate 
29810Sstevel@tonic-gate 	/*
29820Sstevel@tonic-gate 	 * Wait for zone_destroy() to be called.  This is what we spend
29830Sstevel@tonic-gate 	 * most of our life doing.
29840Sstevel@tonic-gate 	 */
29850Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched");
29860Sstevel@tonic-gate 
29870Sstevel@tonic-gate 	if (ct)
29880Sstevel@tonic-gate 		/*
29890Sstevel@tonic-gate 		 * At this point the process contract should be empty.
29900Sstevel@tonic-gate 		 * (Though if it isn't, it's not the end of the world.)
29910Sstevel@tonic-gate 		 */
29920Sstevel@tonic-gate 		VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0);
29930Sstevel@tonic-gate 
29940Sstevel@tonic-gate 	/*
29950Sstevel@tonic-gate 	 * Allow kcred to be freed when all referring processes
29960Sstevel@tonic-gate 	 * (including this one) go away.  We can't just do this in
29970Sstevel@tonic-gate 	 * zone_free because we need to wait for the zone_cred_ref to
29980Sstevel@tonic-gate 	 * drop to 0 before calling zone_free, and the existence of
29990Sstevel@tonic-gate 	 * zone_kcred will prevent that.  Thus, we call crfree here to
30000Sstevel@tonic-gate 	 * balance the crdup in zone_create.  The crhold calls earlier
30010Sstevel@tonic-gate 	 * in zsched will be dropped when the thread and process exit.
30020Sstevel@tonic-gate 	 */
30030Sstevel@tonic-gate 	crfree(zone->zone_kcred);
30040Sstevel@tonic-gate 	zone->zone_kcred = NULL;
30050Sstevel@tonic-gate 
30060Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
30070Sstevel@tonic-gate }
30080Sstevel@tonic-gate 
30090Sstevel@tonic-gate /*
30100Sstevel@tonic-gate  * Helper function to determine if there are any submounts of the
30110Sstevel@tonic-gate  * provided path.  Used to make sure the zone doesn't "inherit" any
30120Sstevel@tonic-gate  * mounts from before it is created.
30130Sstevel@tonic-gate  */
30140Sstevel@tonic-gate static uint_t
30150Sstevel@tonic-gate zone_mount_count(const char *rootpath)
30160Sstevel@tonic-gate {
30170Sstevel@tonic-gate 	vfs_t *vfsp;
30180Sstevel@tonic-gate 	uint_t count = 0;
30190Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
30200Sstevel@tonic-gate 
30210Sstevel@tonic-gate 	/*
30220Sstevel@tonic-gate 	 * Holding zonehash_lock prevents race conditions with
30230Sstevel@tonic-gate 	 * vfs_list_add()/vfs_list_remove() since we serialize with
30240Sstevel@tonic-gate 	 * zone_find_by_path().
30250Sstevel@tonic-gate 	 */
30260Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
30270Sstevel@tonic-gate 	/*
30280Sstevel@tonic-gate 	 * The rootpath must end with a '/'
30290Sstevel@tonic-gate 	 */
30300Sstevel@tonic-gate 	ASSERT(rootpath[rootpathlen - 1] == '/');
30310Sstevel@tonic-gate 
30320Sstevel@tonic-gate 	/*
30330Sstevel@tonic-gate 	 * This intentionally does not count the rootpath itself if that
30340Sstevel@tonic-gate 	 * happens to be a mount point.
30350Sstevel@tonic-gate 	 */
30360Sstevel@tonic-gate 	vfs_list_read_lock();
30370Sstevel@tonic-gate 	vfsp = rootvfs;
30380Sstevel@tonic-gate 	do {
30390Sstevel@tonic-gate 		if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt),
30400Sstevel@tonic-gate 		    rootpathlen) == 0)
30410Sstevel@tonic-gate 			count++;
30420Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
30430Sstevel@tonic-gate 	} while (vfsp != rootvfs);
30440Sstevel@tonic-gate 	vfs_list_unlock();
30450Sstevel@tonic-gate 	return (count);
30460Sstevel@tonic-gate }
30470Sstevel@tonic-gate 
30480Sstevel@tonic-gate /*
30490Sstevel@tonic-gate  * Helper function to make sure that a zone created on 'rootpath'
30500Sstevel@tonic-gate  * wouldn't end up containing other zones' rootpaths.
30510Sstevel@tonic-gate  */
30520Sstevel@tonic-gate static boolean_t
30530Sstevel@tonic-gate zone_is_nested(const char *rootpath)
30540Sstevel@tonic-gate {
30550Sstevel@tonic-gate 	zone_t *zone;
30560Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
30570Sstevel@tonic-gate 	size_t len;
30580Sstevel@tonic-gate 
30590Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
30600Sstevel@tonic-gate 
30610Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
30620Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
30630Sstevel@tonic-gate 		if (zone == global_zone)
30640Sstevel@tonic-gate 			continue;
30650Sstevel@tonic-gate 		len = strlen(zone->zone_rootpath);
30660Sstevel@tonic-gate 		if (strncmp(rootpath, zone->zone_rootpath,
30670Sstevel@tonic-gate 		    MIN(rootpathlen, len)) == 0)
30680Sstevel@tonic-gate 			return (B_TRUE);
30690Sstevel@tonic-gate 	}
30700Sstevel@tonic-gate 	return (B_FALSE);
30710Sstevel@tonic-gate }
30720Sstevel@tonic-gate 
30730Sstevel@tonic-gate static int
3074813Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs,
3075813Sdp     size_t zone_privssz)
30760Sstevel@tonic-gate {
30770Sstevel@tonic-gate 	priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
30780Sstevel@tonic-gate 
3079813Sdp 	if (zone_privssz < sizeof (priv_set_t))
3080813Sdp 		return (set_errno(ENOMEM));
3081813Sdp 
30820Sstevel@tonic-gate 	if (copyin(zone_privs, privs, sizeof (priv_set_t))) {
30830Sstevel@tonic-gate 		kmem_free(privs, sizeof (priv_set_t));
30840Sstevel@tonic-gate 		return (EFAULT);
30850Sstevel@tonic-gate 	}
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate 	zone->zone_privset = privs;
30880Sstevel@tonic-gate 	return (0);
30890Sstevel@tonic-gate }
30900Sstevel@tonic-gate 
30910Sstevel@tonic-gate /*
30920Sstevel@tonic-gate  * We make creative use of nvlists to pass in rctls from userland.  The list is
30930Sstevel@tonic-gate  * a list of the following structures:
30940Sstevel@tonic-gate  *
30950Sstevel@tonic-gate  * (name = rctl_name, value = nvpair_list_array)
30960Sstevel@tonic-gate  *
30970Sstevel@tonic-gate  * Where each element of the nvpair_list_array is of the form:
30980Sstevel@tonic-gate  *
30990Sstevel@tonic-gate  * [(name = "privilege", value = RCPRIV_PRIVILEGED),
31000Sstevel@tonic-gate  * 	(name = "limit", value = uint64_t),
31010Sstevel@tonic-gate  * 	(name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))]
31020Sstevel@tonic-gate  */
31030Sstevel@tonic-gate static int
31040Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp)
31050Sstevel@tonic-gate {
31060Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
31070Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
31080Sstevel@tonic-gate 	char *kbuf;
31090Sstevel@tonic-gate 	int error;
31100Sstevel@tonic-gate 	rctl_val_t rv;
31110Sstevel@tonic-gate 
31120Sstevel@tonic-gate 	*nvlp = NULL;
31130Sstevel@tonic-gate 
31140Sstevel@tonic-gate 	if (buflen == 0)
31150Sstevel@tonic-gate 		return (0);
31160Sstevel@tonic-gate 
31170Sstevel@tonic-gate 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
31180Sstevel@tonic-gate 		return (ENOMEM);
31190Sstevel@tonic-gate 	if (copyin(ubuf, kbuf, buflen)) {
31200Sstevel@tonic-gate 		error = EFAULT;
31210Sstevel@tonic-gate 		goto out;
31220Sstevel@tonic-gate 	}
31230Sstevel@tonic-gate 	if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) {
31240Sstevel@tonic-gate 		/*
31250Sstevel@tonic-gate 		 * nvl may have been allocated/free'd, but the value set to
31260Sstevel@tonic-gate 		 * non-NULL, so we reset it here.
31270Sstevel@tonic-gate 		 */
31280Sstevel@tonic-gate 		nvl = NULL;
31290Sstevel@tonic-gate 		error = EINVAL;
31300Sstevel@tonic-gate 		goto out;
31310Sstevel@tonic-gate 	}
31320Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
31330Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
31340Sstevel@tonic-gate 		rctl_hndl_t hndl;
31350Sstevel@tonic-gate 		nvlist_t **nvlarray;
31360Sstevel@tonic-gate 		uint_t i, nelem;
31370Sstevel@tonic-gate 		char *name;
31380Sstevel@tonic-gate 
31390Sstevel@tonic-gate 		error = EINVAL;
31400Sstevel@tonic-gate 		name = nvpair_name(nvp);
31410Sstevel@tonic-gate 		if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1)
31420Sstevel@tonic-gate 		    != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) {
31430Sstevel@tonic-gate 			goto out;
31440Sstevel@tonic-gate 		}
31450Sstevel@tonic-gate 		if ((hndl = rctl_hndl_lookup(name)) == -1) {
31460Sstevel@tonic-gate 			goto out;
31470Sstevel@tonic-gate 		}
31480Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
31490Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
31500Sstevel@tonic-gate 		ASSERT(error == 0);
31510Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
31520Sstevel@tonic-gate 			if (error = nvlist2rctlval(nvlarray[i], &rv))
31530Sstevel@tonic-gate 				goto out;
31540Sstevel@tonic-gate 		}
31550Sstevel@tonic-gate 		if (rctl_invalid_value(rde, &rv)) {
31560Sstevel@tonic-gate 			error = EINVAL;
31570Sstevel@tonic-gate 			goto out;
31580Sstevel@tonic-gate 		}
31590Sstevel@tonic-gate 	}
31600Sstevel@tonic-gate 	error = 0;
31610Sstevel@tonic-gate 	*nvlp = nvl;
31620Sstevel@tonic-gate out:
31630Sstevel@tonic-gate 	kmem_free(kbuf, buflen);
31640Sstevel@tonic-gate 	if (error && nvl != NULL)
31650Sstevel@tonic-gate 		nvlist_free(nvl);
31660Sstevel@tonic-gate 	return (error);
31670Sstevel@tonic-gate }
31680Sstevel@tonic-gate 
31690Sstevel@tonic-gate int
31700Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) {
31710Sstevel@tonic-gate 	if (er_out != NULL) {
31720Sstevel@tonic-gate 		if (copyout(&er_ext, er_out, sizeof (int))) {
31730Sstevel@tonic-gate 			return (set_errno(EFAULT));
31740Sstevel@tonic-gate 		}
31750Sstevel@tonic-gate 	}
31760Sstevel@tonic-gate 	return (set_errno(er_error));
31770Sstevel@tonic-gate }
31780Sstevel@tonic-gate 
31791676Sjpk static int
31801676Sjpk zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi)
31811676Sjpk {
31821676Sjpk 	ts_label_t *tsl;
31831676Sjpk 	bslabel_t blab;
31841676Sjpk 
31851676Sjpk 	/* Get label from user */
31861676Sjpk 	if (copyin(lab, &blab, sizeof (blab)) != 0)
31871676Sjpk 		return (EFAULT);
31881676Sjpk 	tsl = labelalloc(&blab, doi, KM_NOSLEEP);
31891676Sjpk 	if (tsl == NULL)
31901676Sjpk 		return (ENOMEM);
31911676Sjpk 
31921676Sjpk 	zone->zone_slabel = tsl;
31931676Sjpk 	return (0);
31941676Sjpk }
31951676Sjpk 
31960Sstevel@tonic-gate /*
3197789Sahrens  * Parses a comma-separated list of ZFS datasets into a per-zone dictionary.
3198789Sahrens  */
3199789Sahrens static int
3200789Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen)
3201789Sahrens {
3202789Sahrens 	char *kbuf;
3203789Sahrens 	char *dataset, *next;
3204789Sahrens 	zone_dataset_t *zd;
3205789Sahrens 	size_t len;
3206789Sahrens 
3207789Sahrens 	if (ubuf == NULL || buflen == 0)
3208789Sahrens 		return (0);
3209789Sahrens 
3210789Sahrens 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
3211789Sahrens 		return (ENOMEM);
3212789Sahrens 
3213789Sahrens 	if (copyin(ubuf, kbuf, buflen) != 0) {
3214789Sahrens 		kmem_free(kbuf, buflen);
3215789Sahrens 		return (EFAULT);
3216789Sahrens 	}
3217789Sahrens 
3218789Sahrens 	dataset = next = kbuf;
3219789Sahrens 	for (;;) {
3220789Sahrens 		zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP);
3221789Sahrens 
3222789Sahrens 		next = strchr(dataset, ',');
3223789Sahrens 
3224789Sahrens 		if (next == NULL)
3225789Sahrens 			len = strlen(dataset);
3226789Sahrens 		else
3227789Sahrens 			len = next - dataset;
3228789Sahrens 
3229789Sahrens 		zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP);
3230789Sahrens 		bcopy(dataset, zd->zd_dataset, len);
3231789Sahrens 		zd->zd_dataset[len] = '\0';
3232789Sahrens 
3233789Sahrens 		list_insert_head(&zone->zone_datasets, zd);
3234789Sahrens 
3235789Sahrens 		if (next == NULL)
3236789Sahrens 			break;
3237789Sahrens 
3238789Sahrens 		dataset = next + 1;
3239789Sahrens 	}
3240789Sahrens 
3241789Sahrens 	kmem_free(kbuf, buflen);
3242789Sahrens 	return (0);
3243789Sahrens }
3244789Sahrens 
3245789Sahrens /*
32460Sstevel@tonic-gate  * System call to create/initialize a new zone named 'zone_name', rooted
32470Sstevel@tonic-gate  * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs',
32481676Sjpk  * and initialized with the zone-wide rctls described in 'rctlbuf', and
32491676Sjpk  * with labeling set by 'match', 'doi', and 'label'.
32500Sstevel@tonic-gate  *
32510Sstevel@tonic-gate  * If extended error is non-null, we may use it to return more detailed
32520Sstevel@tonic-gate  * error information.
32530Sstevel@tonic-gate  */
32540Sstevel@tonic-gate static zoneid_t
32550Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root,
3256813Sdp     const priv_set_t *zone_privs, size_t zone_privssz,
3257813Sdp     caddr_t rctlbuf, size_t rctlbufsz,
32581676Sjpk     caddr_t zfsbuf, size_t zfsbufsz, int *extended_error,
32593448Sdh155122     int match, uint32_t doi, const bslabel_t *label,
32603448Sdh155122     int flags)
32610Sstevel@tonic-gate {
32620Sstevel@tonic-gate 	struct zsched_arg zarg;
32630Sstevel@tonic-gate 	nvlist_t *rctls = NULL;
32640Sstevel@tonic-gate 	proc_t *pp = curproc;
32650Sstevel@tonic-gate 	zone_t *zone, *ztmp;
32660Sstevel@tonic-gate 	zoneid_t zoneid;
32670Sstevel@tonic-gate 	int error;
32680Sstevel@tonic-gate 	int error2 = 0;
32690Sstevel@tonic-gate 	char *str;
32700Sstevel@tonic-gate 	cred_t *zkcr;
32711769Scarlsonj 	boolean_t insert_label_hash;
32720Sstevel@tonic-gate 
32730Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
32740Sstevel@tonic-gate 		return (set_errno(EPERM));
32750Sstevel@tonic-gate 
32760Sstevel@tonic-gate 	/* can't boot zone from within chroot environment */
32770Sstevel@tonic-gate 	if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir)
32780Sstevel@tonic-gate 		return (zone_create_error(ENOTSUP, ZE_CHROOTED,
3279813Sdp 		    extended_error));
32800Sstevel@tonic-gate 
32810Sstevel@tonic-gate 	zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP);
32820Sstevel@tonic-gate 	zoneid = zone->zone_id = id_alloc(zoneid_space);
32830Sstevel@tonic-gate 	zone->zone_status = ZONE_IS_UNINITIALIZED;
32840Sstevel@tonic-gate 	zone->zone_pool = pool_default;
32850Sstevel@tonic-gate 	zone->zone_pool_mod = gethrtime();
32860Sstevel@tonic-gate 	zone->zone_psetid = ZONE_PS_INVAL;
32870Sstevel@tonic-gate 	zone->zone_ncpus = 0;
32880Sstevel@tonic-gate 	zone->zone_ncpus_online = 0;
32892712Snn35248 	zone->zone_restart_init = B_TRUE;
32902712Snn35248 	zone->zone_brand = &native_brand;
32912712Snn35248 	zone->zone_initname = NULL;
32920Sstevel@tonic-gate 	mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL);
32930Sstevel@tonic-gate 	mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
32943247Sgjelinek 	mutex_init(&zone->zone_mem_lock, NULL, MUTEX_DEFAULT, NULL);
32950Sstevel@tonic-gate 	cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL);
32960Sstevel@tonic-gate 	list_create(&zone->zone_zsd, sizeof (struct zsd_entry),
32970Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
3298789Sahrens 	list_create(&zone->zone_datasets, sizeof (zone_dataset_t),
3299789Sahrens 	    offsetof(zone_dataset_t, zd_linkage));
33001676Sjpk 	rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
33010Sstevel@tonic-gate 
33023448Sdh155122 	if (flags & ZCF_NET_EXCL) {
33033448Sdh155122 		zone->zone_flags |= ZF_NET_EXCL;
33043448Sdh155122 	}
33053448Sdh155122 
33060Sstevel@tonic-gate 	if ((error = zone_set_name(zone, zone_name)) != 0) {
33070Sstevel@tonic-gate 		zone_free(zone);
33080Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
33090Sstevel@tonic-gate 	}
33100Sstevel@tonic-gate 
33110Sstevel@tonic-gate 	if ((error = zone_set_root(zone, zone_root)) != 0) {
33120Sstevel@tonic-gate 		zone_free(zone);
33130Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
33140Sstevel@tonic-gate 	}
3315813Sdp 	if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) {
33160Sstevel@tonic-gate 		zone_free(zone);
33170Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
33180Sstevel@tonic-gate 	}
33190Sstevel@tonic-gate 
33200Sstevel@tonic-gate 	/* initialize node name to be the same as zone name */
33210Sstevel@tonic-gate 	zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP);
33220Sstevel@tonic-gate 	(void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN);
33230Sstevel@tonic-gate 	zone->zone_nodename[_SYS_NMLN - 1] = '\0';
33240Sstevel@tonic-gate 
33250Sstevel@tonic-gate 	zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP);
33260Sstevel@tonic-gate 	zone->zone_domain[0] = '\0';
33270Sstevel@tonic-gate 	zone->zone_shares = 1;
33282677Sml93401 	zone->zone_shmmax = 0;
33292677Sml93401 	zone->zone_ipc.ipcq_shmmni = 0;
33302677Sml93401 	zone->zone_ipc.ipcq_semmni = 0;
33312677Sml93401 	zone->zone_ipc.ipcq_msgmni = 0;
33320Sstevel@tonic-gate 	zone->zone_bootargs = NULL;
33332267Sdp 	zone->zone_initname =
33342267Sdp 	    kmem_alloc(strlen(zone_default_initname) + 1, KM_SLEEP);
33352267Sdp 	(void) strcpy(zone->zone_initname, zone_default_initname);
33363247Sgjelinek 	zone->zone_nlwps = 0;
33373247Sgjelinek 	zone->zone_nlwps_ctl = INT_MAX;
33382768Ssl108498 	zone->zone_locked_mem = 0;
33392768Ssl108498 	zone->zone_locked_mem_ctl = UINT64_MAX;
33403247Sgjelinek 	zone->zone_max_swap = 0;
33413247Sgjelinek 	zone->zone_max_swap_ctl = UINT64_MAX;
33423247Sgjelinek 	zone0.zone_lockedmem_kstat = NULL;
33433247Sgjelinek 	zone0.zone_swapresv_kstat = NULL;
33440Sstevel@tonic-gate 
33450Sstevel@tonic-gate 	/*
33460Sstevel@tonic-gate 	 * Zsched initializes the rctls.
33470Sstevel@tonic-gate 	 */
33480Sstevel@tonic-gate 	zone->zone_rctls = NULL;
33490Sstevel@tonic-gate 
33500Sstevel@tonic-gate 	if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) {
33510Sstevel@tonic-gate 		zone_free(zone);
33520Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
33530Sstevel@tonic-gate 	}
33540Sstevel@tonic-gate 
3355789Sahrens 	if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) {
3356789Sahrens 		zone_free(zone);
3357789Sahrens 		return (set_errno(error));
3358789Sahrens 	}
3359789Sahrens 
33600Sstevel@tonic-gate 	/*
33611676Sjpk 	 * Read in the trusted system parameters:
33621676Sjpk 	 * match flag and sensitivity label.
33631676Sjpk 	 */
33641676Sjpk 	zone->zone_match = match;
33651769Scarlsonj 	if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) {
33661676Sjpk 		error = zone_set_label(zone, label, doi);
33671676Sjpk 		if (error != 0) {
33681676Sjpk 			zone_free(zone);
33691676Sjpk 			return (set_errno(error));
33701676Sjpk 		}
33711769Scarlsonj 		insert_label_hash = B_TRUE;
33721676Sjpk 	} else {
33731676Sjpk 		/* all zones get an admin_low label if system is not labeled */
33741676Sjpk 		zone->zone_slabel = l_admin_low;
33751676Sjpk 		label_hold(l_admin_low);
33761769Scarlsonj 		insert_label_hash = B_FALSE;
33771676Sjpk 	}
33781676Sjpk 
33791676Sjpk 	/*
33800Sstevel@tonic-gate 	 * Stop all lwps since that's what normally happens as part of fork().
33810Sstevel@tonic-gate 	 * This needs to happen before we grab any locks to avoid deadlock
33820Sstevel@tonic-gate 	 * (another lwp in the process could be waiting for the held lock).
33830Sstevel@tonic-gate 	 */
33840Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) {
33850Sstevel@tonic-gate 		zone_free(zone);
33860Sstevel@tonic-gate 		if (rctls)
33870Sstevel@tonic-gate 			nvlist_free(rctls);
33880Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
33890Sstevel@tonic-gate 	}
33900Sstevel@tonic-gate 
33910Sstevel@tonic-gate 	if (block_mounts() == 0) {
33920Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
33930Sstevel@tonic-gate 		if (curthread != pp->p_agenttp)
33940Sstevel@tonic-gate 			continuelwps(pp);
33950Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
33960Sstevel@tonic-gate 		zone_free(zone);
33970Sstevel@tonic-gate 		if (rctls)
33980Sstevel@tonic-gate 			nvlist_free(rctls);
33990Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
34000Sstevel@tonic-gate 	}
34010Sstevel@tonic-gate 
34020Sstevel@tonic-gate 	/*
34030Sstevel@tonic-gate 	 * Set up credential for kernel access.  After this, any errors
34040Sstevel@tonic-gate 	 * should go through the dance in errout rather than calling
34050Sstevel@tonic-gate 	 * zone_free directly.
34060Sstevel@tonic-gate 	 */
34070Sstevel@tonic-gate 	zone->zone_kcred = crdup(kcred);
34080Sstevel@tonic-gate 	crsetzone(zone->zone_kcred, zone);
34090Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred));
34100Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred));
34110Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred));
34120Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred));
34130Sstevel@tonic-gate 
34140Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
34150Sstevel@tonic-gate 	/*
34160Sstevel@tonic-gate 	 * Make sure zone doesn't already exist.
34171676Sjpk 	 *
34181676Sjpk 	 * If the system and zone are labeled,
34191676Sjpk 	 * make sure no other zone exists that has the same label.
34200Sstevel@tonic-gate 	 */
34211676Sjpk 	if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL ||
34221769Scarlsonj 	    (insert_label_hash &&
34231676Sjpk 	    (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) {
34240Sstevel@tonic-gate 		zone_status_t status;
34250Sstevel@tonic-gate 
34260Sstevel@tonic-gate 		status = zone_status_get(ztmp);
34270Sstevel@tonic-gate 		if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING)
34280Sstevel@tonic-gate 			error = EEXIST;
34290Sstevel@tonic-gate 		else
34300Sstevel@tonic-gate 			error = EBUSY;
34310Sstevel@tonic-gate 		goto errout;
34320Sstevel@tonic-gate 	}
34330Sstevel@tonic-gate 
34340Sstevel@tonic-gate 	/*
34350Sstevel@tonic-gate 	 * Don't allow zone creations which would cause one zone's rootpath to
34360Sstevel@tonic-gate 	 * be accessible from that of another (non-global) zone.
34370Sstevel@tonic-gate 	 */
34380Sstevel@tonic-gate 	if (zone_is_nested(zone->zone_rootpath)) {
34390Sstevel@tonic-gate 		error = EBUSY;
34400Sstevel@tonic-gate 		goto errout;
34410Sstevel@tonic-gate 	}
34420Sstevel@tonic-gate 
34430Sstevel@tonic-gate 	ASSERT(zonecount != 0);		/* check for leaks */
34440Sstevel@tonic-gate 	if (zonecount + 1 > maxzones) {
34450Sstevel@tonic-gate 		error = ENOMEM;
34460Sstevel@tonic-gate 		goto errout;
34470Sstevel@tonic-gate 	}
34480Sstevel@tonic-gate 
34490Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
34500Sstevel@tonic-gate 		error = EBUSY;
34510Sstevel@tonic-gate 		error2 = ZE_AREMOUNTS;
34520Sstevel@tonic-gate 		goto errout;
34530Sstevel@tonic-gate 	}
34540Sstevel@tonic-gate 
34550Sstevel@tonic-gate 	/*
34560Sstevel@tonic-gate 	 * Zone is still incomplete, but we need to drop all locks while
34570Sstevel@tonic-gate 	 * zsched() initializes this zone's kernel process.  We
34580Sstevel@tonic-gate 	 * optimistically add the zone to the hashtable and associated
34590Sstevel@tonic-gate 	 * lists so a parallel zone_create() doesn't try to create the
34600Sstevel@tonic-gate 	 * same zone.
34610Sstevel@tonic-gate 	 */
34620Sstevel@tonic-gate 	zonecount++;
34630Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid,
34640Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id,
34650Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
34660Sstevel@tonic-gate 	str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP);
34670Sstevel@tonic-gate 	(void) strcpy(str, zone->zone_name);
34680Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str,
34690Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
34701769Scarlsonj 	if (insert_label_hash) {
34711676Sjpk 		(void) mod_hash_insert(zonehashbylabel,
34721676Sjpk 		    (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone);
34731769Scarlsonj 		zone->zone_flags |= ZF_HASHED_LABEL;
34741676Sjpk 	}
34751676Sjpk 
34760Sstevel@tonic-gate 	/*
34770Sstevel@tonic-gate 	 * Insert into active list.  At this point there are no 'hold's
34780Sstevel@tonic-gate 	 * on the zone, but everyone else knows not to use it, so we can
34790Sstevel@tonic-gate 	 * continue to use it.  zsched() will do a zone_hold() if the
34800Sstevel@tonic-gate 	 * newproc() is successful.
34810Sstevel@tonic-gate 	 */
34820Sstevel@tonic-gate 	list_insert_tail(&zone_active, zone);
34830Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
34840Sstevel@tonic-gate 
34850Sstevel@tonic-gate 	zarg.zone = zone;
34860Sstevel@tonic-gate 	zarg.nvlist = rctls;
34870Sstevel@tonic-gate 	/*
34880Sstevel@tonic-gate 	 * The process, task, and project rctls are probably wrong;
34890Sstevel@tonic-gate 	 * we need an interface to get the default values of all rctls,
34900Sstevel@tonic-gate 	 * and initialize zsched appropriately.  I'm not sure that that
34910Sstevel@tonic-gate 	 * makes much of a difference, though.
34920Sstevel@tonic-gate 	 */
34930Sstevel@tonic-gate 	if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) {
34940Sstevel@tonic-gate 		/*
34950Sstevel@tonic-gate 		 * We need to undo all globally visible state.
34960Sstevel@tonic-gate 		 */
34970Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
34980Sstevel@tonic-gate 		list_remove(&zone_active, zone);
34991769Scarlsonj 		if (zone->zone_flags & ZF_HASHED_LABEL) {
35001676Sjpk 			ASSERT(zone->zone_slabel != NULL);
35011676Sjpk 			(void) mod_hash_destroy(zonehashbylabel,
35021676Sjpk 			    (mod_hash_key_t)zone->zone_slabel);
35031676Sjpk 		}
35040Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyname,
35050Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_name);
35060Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyid,
35070Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_id);
35080Sstevel@tonic-gate 		ASSERT(zonecount > 1);
35090Sstevel@tonic-gate 		zonecount--;
35100Sstevel@tonic-gate 		goto errout;
35110Sstevel@tonic-gate 	}
35120Sstevel@tonic-gate 
35130Sstevel@tonic-gate 	/*
35140Sstevel@tonic-gate 	 * Zone creation can't fail from now on.
35150Sstevel@tonic-gate 	 */
35160Sstevel@tonic-gate 
35170Sstevel@tonic-gate 	/*
35183247Sgjelinek 	 * Create zone kstats
35193247Sgjelinek 	 */
35203247Sgjelinek 	zone_kstat_create(zone);
35213247Sgjelinek 
35223247Sgjelinek 	/*
35230Sstevel@tonic-gate 	 * Let the other lwps continue.
35240Sstevel@tonic-gate 	 */
35250Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
35260Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
35270Sstevel@tonic-gate 		continuelwps(pp);
35280Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
35290Sstevel@tonic-gate 
35300Sstevel@tonic-gate 	/*
35310Sstevel@tonic-gate 	 * Wait for zsched to finish initializing the zone.
35320Sstevel@tonic-gate 	 */
35330Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_READY);
35340Sstevel@tonic-gate 	/*
35350Sstevel@tonic-gate 	 * The zone is fully visible, so we can let mounts progress.
35360Sstevel@tonic-gate 	 */
35370Sstevel@tonic-gate 	resume_mounts();
35380Sstevel@tonic-gate 	if (rctls)
35390Sstevel@tonic-gate 		nvlist_free(rctls);
35400Sstevel@tonic-gate 
35410Sstevel@tonic-gate 	return (zoneid);
35420Sstevel@tonic-gate 
35430Sstevel@tonic-gate errout:
35440Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
35450Sstevel@tonic-gate 	/*
35460Sstevel@tonic-gate 	 * Let the other lwps continue.
35470Sstevel@tonic-gate 	 */
35480Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
35490Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
35500Sstevel@tonic-gate 		continuelwps(pp);
35510Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
35520Sstevel@tonic-gate 
35530Sstevel@tonic-gate 	resume_mounts();
35540Sstevel@tonic-gate 	if (rctls)
35550Sstevel@tonic-gate 		nvlist_free(rctls);
35560Sstevel@tonic-gate 	/*
35570Sstevel@tonic-gate 	 * There is currently one reference to the zone, a cred_ref from
35580Sstevel@tonic-gate 	 * zone_kcred.  To free the zone, we call crfree, which will call
35590Sstevel@tonic-gate 	 * zone_cred_rele, which will call zone_free.
35600Sstevel@tonic-gate 	 */
35610Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 1);	/* for zone_kcred */
35620Sstevel@tonic-gate 	ASSERT(zone->zone_kcred->cr_ref == 1);
35630Sstevel@tonic-gate 	ASSERT(zone->zone_ref == 0);
35640Sstevel@tonic-gate 	zkcr = zone->zone_kcred;
35650Sstevel@tonic-gate 	zone->zone_kcred = NULL;
35660Sstevel@tonic-gate 	crfree(zkcr);				/* triggers call to zone_free */
35670Sstevel@tonic-gate 	return (zone_create_error(error, error2, extended_error));
35680Sstevel@tonic-gate }
35690Sstevel@tonic-gate 
35700Sstevel@tonic-gate /*
35710Sstevel@tonic-gate  * Cause the zone to boot.  This is pretty simple, since we let zoneadmd do
35722267Sdp  * the heavy lifting.  initname is the path to the program to launch
35732267Sdp  * at the "top" of the zone; if this is NULL, we use the system default,
35742267Sdp  * which is stored at zone_default_initname.
35750Sstevel@tonic-gate  */
35760Sstevel@tonic-gate static int
35772267Sdp zone_boot(zoneid_t zoneid)
35780Sstevel@tonic-gate {
35790Sstevel@tonic-gate 	int err;
35800Sstevel@tonic-gate 	zone_t *zone;
35810Sstevel@tonic-gate 
35820Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
35830Sstevel@tonic-gate 		return (set_errno(EPERM));
35840Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
35850Sstevel@tonic-gate 		return (set_errno(EINVAL));
35860Sstevel@tonic-gate 
35870Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
35880Sstevel@tonic-gate 	/*
35890Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with calls to
35900Sstevel@tonic-gate 	 * zone_shutdown, zone_destroy, etc.
35910Sstevel@tonic-gate 	 */
35920Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
35930Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
35940Sstevel@tonic-gate 		return (set_errno(EINVAL));
35950Sstevel@tonic-gate 	}
35960Sstevel@tonic-gate 
35970Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
35980Sstevel@tonic-gate 	if (zone_status_get(zone) != ZONE_IS_READY) {
35990Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
36000Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
36010Sstevel@tonic-gate 		return (set_errno(EINVAL));
36020Sstevel@tonic-gate 	}
36030Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_BOOTING);
36040Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
36050Sstevel@tonic-gate 
36060Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
36070Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
36080Sstevel@tonic-gate 
36090Sstevel@tonic-gate 	if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) {
36100Sstevel@tonic-gate 		zone_rele(zone);
36110Sstevel@tonic-gate 		return (set_errno(EINTR));
36120Sstevel@tonic-gate 	}
36130Sstevel@tonic-gate 
36140Sstevel@tonic-gate 	/*
36150Sstevel@tonic-gate 	 * Boot (starting init) might have failed, in which case the zone
36160Sstevel@tonic-gate 	 * will go to the SHUTTING_DOWN state; an appropriate errno will
36170Sstevel@tonic-gate 	 * be placed in zone->zone_boot_err, and so we return that.
36180Sstevel@tonic-gate 	 */
36190Sstevel@tonic-gate 	err = zone->zone_boot_err;
36200Sstevel@tonic-gate 	zone_rele(zone);
36210Sstevel@tonic-gate 	return (err ? set_errno(err) : 0);
36220Sstevel@tonic-gate }
36230Sstevel@tonic-gate 
36240Sstevel@tonic-gate /*
36250Sstevel@tonic-gate  * Kills all user processes in the zone, waiting for them all to exit
36260Sstevel@tonic-gate  * before returning.
36270Sstevel@tonic-gate  */
36280Sstevel@tonic-gate static int
36290Sstevel@tonic-gate zone_empty(zone_t *zone)
36300Sstevel@tonic-gate {
36310Sstevel@tonic-gate 	int waitstatus;
36320Sstevel@tonic-gate 
36330Sstevel@tonic-gate 	/*
36340Sstevel@tonic-gate 	 * We need to drop zonehash_lock before killing all
36350Sstevel@tonic-gate 	 * processes, otherwise we'll deadlock with zone_find_*
36360Sstevel@tonic-gate 	 * which can be called from the exit path.
36370Sstevel@tonic-gate 	 */
36380Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
36390Sstevel@tonic-gate 	while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz,
36400Sstevel@tonic-gate 	    ZONE_IS_EMPTY)) == -1) {
36410Sstevel@tonic-gate 		killall(zone->zone_id);
36420Sstevel@tonic-gate 	}
36430Sstevel@tonic-gate 	/*
36440Sstevel@tonic-gate 	 * return EINTR if we were signaled
36450Sstevel@tonic-gate 	 */
36460Sstevel@tonic-gate 	if (waitstatus == 0)
36470Sstevel@tonic-gate 		return (EINTR);
36480Sstevel@tonic-gate 	return (0);
36490Sstevel@tonic-gate }
36500Sstevel@tonic-gate 
36510Sstevel@tonic-gate /*
36521676Sjpk  * This function implements the policy for zone visibility.
36531676Sjpk  *
36541676Sjpk  * In standard Solaris, a non-global zone can only see itself.
36551676Sjpk  *
36561676Sjpk  * In Trusted Extensions, a labeled zone can lookup any zone whose label
36571676Sjpk  * it dominates. For this test, the label of the global zone is treated as
36581676Sjpk  * admin_high so it is special-cased instead of being checked for dominance.
36591676Sjpk  *
36601676Sjpk  * Returns true if zone attributes are viewable, false otherwise.
36611676Sjpk  */
36621676Sjpk static boolean_t
36631676Sjpk zone_list_access(zone_t *zone)
36641676Sjpk {
36651676Sjpk 
36661676Sjpk 	if (curproc->p_zone == global_zone ||
36671676Sjpk 	    curproc->p_zone == zone) {
36681676Sjpk 		return (B_TRUE);
36691769Scarlsonj 	} else if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) {
36701676Sjpk 		bslabel_t *curproc_label;
36711676Sjpk 		bslabel_t *zone_label;
36721676Sjpk 
36731676Sjpk 		curproc_label = label2bslabel(curproc->p_zone->zone_slabel);
36741676Sjpk 		zone_label = label2bslabel(zone->zone_slabel);
36751676Sjpk 
36761676Sjpk 		if (zone->zone_id != GLOBAL_ZONEID &&
36771676Sjpk 		    bldominates(curproc_label, zone_label)) {
36781676Sjpk 			return (B_TRUE);
36791676Sjpk 		} else {
36801676Sjpk 			return (B_FALSE);
36811676Sjpk 		}
36821676Sjpk 	} else {
36831676Sjpk 		return (B_FALSE);
36841676Sjpk 	}
36851676Sjpk }
36861676Sjpk 
36871676Sjpk /*
36880Sstevel@tonic-gate  * Systemcall to start the zone's halt sequence.  By the time this
36890Sstevel@tonic-gate  * function successfully returns, all user processes and kernel threads
36900Sstevel@tonic-gate  * executing in it will have exited, ZSD shutdown callbacks executed,
36910Sstevel@tonic-gate  * and the zone status set to ZONE_IS_DOWN.
36920Sstevel@tonic-gate  *
36930Sstevel@tonic-gate  * It is possible that the call will interrupt itself if the caller is the
36940Sstevel@tonic-gate  * parent of any process running in the zone, and doesn't have SIGCHLD blocked.
36950Sstevel@tonic-gate  */
36960Sstevel@tonic-gate static int
36970Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid)
36980Sstevel@tonic-gate {
36990Sstevel@tonic-gate 	int error;
37000Sstevel@tonic-gate 	zone_t *zone;
37010Sstevel@tonic-gate 	zone_status_t status;
37020Sstevel@tonic-gate 
37030Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
37040Sstevel@tonic-gate 		return (set_errno(EPERM));
37050Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
37060Sstevel@tonic-gate 		return (set_errno(EINVAL));
37070Sstevel@tonic-gate 
37080Sstevel@tonic-gate 	/*
37090Sstevel@tonic-gate 	 * Block mounts so that VFS_MOUNT() can get an accurate view of
37100Sstevel@tonic-gate 	 * the zone's status with regards to ZONE_IS_SHUTTING down.
37110Sstevel@tonic-gate 	 *
37120Sstevel@tonic-gate 	 * e.g. NFS can fail the mount if it determines that the zone
37130Sstevel@tonic-gate 	 * has already begun the shutdown sequence.
37140Sstevel@tonic-gate 	 */
37150Sstevel@tonic-gate 	if (block_mounts() == 0)
37160Sstevel@tonic-gate 		return (set_errno(EINTR));
37170Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
37180Sstevel@tonic-gate 	/*
37190Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
37200Sstevel@tonic-gate 	 * calls to zone_shutdown and zone_destroy.
37210Sstevel@tonic-gate 	 */
37220Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
37230Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
37240Sstevel@tonic-gate 		resume_mounts();
37250Sstevel@tonic-gate 		return (set_errno(EINVAL));
37260Sstevel@tonic-gate 	}
37270Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
37280Sstevel@tonic-gate 	status = zone_status_get(zone);
37290Sstevel@tonic-gate 	/*
37300Sstevel@tonic-gate 	 * Fail if the zone isn't fully initialized yet.
37310Sstevel@tonic-gate 	 */
37320Sstevel@tonic-gate 	if (status < ZONE_IS_READY) {
37330Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
37340Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
37350Sstevel@tonic-gate 		resume_mounts();
37360Sstevel@tonic-gate 		return (set_errno(EINVAL));
37370Sstevel@tonic-gate 	}
37380Sstevel@tonic-gate 	/*
37390Sstevel@tonic-gate 	 * If conditions required for zone_shutdown() to return have been met,
37400Sstevel@tonic-gate 	 * return success.
37410Sstevel@tonic-gate 	 */
37420Sstevel@tonic-gate 	if (status >= ZONE_IS_DOWN) {
37430Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
37440Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
37450Sstevel@tonic-gate 		resume_mounts();
37460Sstevel@tonic-gate 		return (0);
37470Sstevel@tonic-gate 	}
37480Sstevel@tonic-gate 	/*
37490Sstevel@tonic-gate 	 * If zone_shutdown() hasn't been called before, go through the motions.
37500Sstevel@tonic-gate 	 * If it has, there's nothing to do but wait for the kernel threads to
37510Sstevel@tonic-gate 	 * drain.
37520Sstevel@tonic-gate 	 */
37530Sstevel@tonic-gate 	if (status < ZONE_IS_EMPTY) {
37540Sstevel@tonic-gate 		uint_t ntasks;
37550Sstevel@tonic-gate 
37560Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
37570Sstevel@tonic-gate 		if ((ntasks = zone->zone_ntasks) != 1) {
37580Sstevel@tonic-gate 			/*
37590Sstevel@tonic-gate 			 * There's still stuff running.
37600Sstevel@tonic-gate 			 */
37610Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
37620Sstevel@tonic-gate 		}
37630Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
37640Sstevel@tonic-gate 		if (ntasks == 1) {
37650Sstevel@tonic-gate 			/*
37660Sstevel@tonic-gate 			 * The only way to create another task is through
37670Sstevel@tonic-gate 			 * zone_enter(), which will block until we drop
37680Sstevel@tonic-gate 			 * zonehash_lock.  The zone is empty.
37690Sstevel@tonic-gate 			 */
37700Sstevel@tonic-gate 			if (zone->zone_kthreads == NULL) {
37710Sstevel@tonic-gate 				/*
37720Sstevel@tonic-gate 				 * Skip ahead to ZONE_IS_DOWN
37730Sstevel@tonic-gate 				 */
37740Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_DOWN);
37750Sstevel@tonic-gate 			} else {
37760Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_EMPTY);
37770Sstevel@tonic-gate 			}
37780Sstevel@tonic-gate 		}
37790Sstevel@tonic-gate 	}
37800Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
37810Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
37820Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
37830Sstevel@tonic-gate 	resume_mounts();
37840Sstevel@tonic-gate 
37850Sstevel@tonic-gate 	if (error = zone_empty(zone)) {
37860Sstevel@tonic-gate 		zone_rele(zone);
37870Sstevel@tonic-gate 		return (set_errno(error));
37880Sstevel@tonic-gate 	}
37890Sstevel@tonic-gate 	/*
37900Sstevel@tonic-gate 	 * After the zone status goes to ZONE_IS_DOWN this zone will no
37910Sstevel@tonic-gate 	 * longer be notified of changes to the pools configuration, so
37920Sstevel@tonic-gate 	 * in order to not end up with a stale pool pointer, we point
37930Sstevel@tonic-gate 	 * ourselves at the default pool and remove all resource
37940Sstevel@tonic-gate 	 * visibility.  This is especially important as the zone_t may
37950Sstevel@tonic-gate 	 * languish on the deathrow for a very long time waiting for
37960Sstevel@tonic-gate 	 * cred's to drain out.
37970Sstevel@tonic-gate 	 *
37980Sstevel@tonic-gate 	 * This rebinding of the zone can happen multiple times
37990Sstevel@tonic-gate 	 * (presumably due to interrupted or parallel systemcalls)
38000Sstevel@tonic-gate 	 * without any adverse effects.
38010Sstevel@tonic-gate 	 */
38020Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
38030Sstevel@tonic-gate 		zone_rele(zone);
38040Sstevel@tonic-gate 		return (set_errno(EINTR));
38050Sstevel@tonic-gate 	}
38060Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
38070Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
38080Sstevel@tonic-gate 		zone_pool_set(zone, pool_default);
38090Sstevel@tonic-gate 		/*
38100Sstevel@tonic-gate 		 * The zone no longer needs to be able to see any cpus.
38110Sstevel@tonic-gate 		 */
38120Sstevel@tonic-gate 		zone_pset_set(zone, ZONE_PS_INVAL);
38130Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
38140Sstevel@tonic-gate 	}
38150Sstevel@tonic-gate 	pool_unlock();
38160Sstevel@tonic-gate 
38170Sstevel@tonic-gate 	/*
38180Sstevel@tonic-gate 	 * ZSD shutdown callbacks can be executed multiple times, hence
38190Sstevel@tonic-gate 	 * it is safe to not be holding any locks across this call.
38200Sstevel@tonic-gate 	 */
38210Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_SHUTDOWN);
38220Sstevel@tonic-gate 
38230Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
38240Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN)
38250Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DOWN);
38260Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
38270Sstevel@tonic-gate 
38280Sstevel@tonic-gate 	/*
38290Sstevel@tonic-gate 	 * Wait for kernel threads to drain.
38300Sstevel@tonic-gate 	 */
38310Sstevel@tonic-gate 	if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) {
38320Sstevel@tonic-gate 		zone_rele(zone);
38330Sstevel@tonic-gate 		return (set_errno(EINTR));
38340Sstevel@tonic-gate 	}
38352712Snn35248 
38363671Ssl108498 	/*
38373671Ssl108498 	 * Zone can be become down/destroyable even if the above wait
38383671Ssl108498 	 * returns EINTR, so any code added here may never execute.
38393671Ssl108498 	 * (i.e. don't add code here)
38403671Ssl108498 	 */
38412712Snn35248 
38420Sstevel@tonic-gate 	zone_rele(zone);
38430Sstevel@tonic-gate 	return (0);
38440Sstevel@tonic-gate }
38450Sstevel@tonic-gate 
38460Sstevel@tonic-gate /*
38470Sstevel@tonic-gate  * Systemcall entry point to finalize the zone halt process.  The caller
38482677Sml93401  * must have already successfully called zone_shutdown().
38490Sstevel@tonic-gate  *
38500Sstevel@tonic-gate  * Upon successful completion, the zone will have been fully destroyed:
38510Sstevel@tonic-gate  * zsched will have exited, destructor callbacks executed, and the zone
38520Sstevel@tonic-gate  * removed from the list of active zones.
38530Sstevel@tonic-gate  */
38540Sstevel@tonic-gate static int
38550Sstevel@tonic-gate zone_destroy(zoneid_t zoneid)
38560Sstevel@tonic-gate {
38570Sstevel@tonic-gate 	uint64_t uniqid;
38580Sstevel@tonic-gate 	zone_t *zone;
38590Sstevel@tonic-gate 	zone_status_t status;
38600Sstevel@tonic-gate 
38610Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
38620Sstevel@tonic-gate 		return (set_errno(EPERM));
38630Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
38640Sstevel@tonic-gate 		return (set_errno(EINVAL));
38650Sstevel@tonic-gate 
38660Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
38670Sstevel@tonic-gate 	/*
38680Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
38690Sstevel@tonic-gate 	 * calls to zone_destroy.
38700Sstevel@tonic-gate 	 */
38710Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
38720Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
38730Sstevel@tonic-gate 		return (set_errno(EINVAL));
38740Sstevel@tonic-gate 	}
38750Sstevel@tonic-gate 
38760Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
38770Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
38780Sstevel@tonic-gate 		return (set_errno(EBUSY));
38790Sstevel@tonic-gate 	}
38800Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
38810Sstevel@tonic-gate 	status = zone_status_get(zone);
38820Sstevel@tonic-gate 	if (status < ZONE_IS_DOWN) {
38830Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
38840Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
38850Sstevel@tonic-gate 		return (set_errno(EBUSY));
38860Sstevel@tonic-gate 	} else if (status == ZONE_IS_DOWN) {
38870Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */
38880Sstevel@tonic-gate 	}
38890Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
38900Sstevel@tonic-gate 	zone_hold(zone);
38910Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
38920Sstevel@tonic-gate 
38930Sstevel@tonic-gate 	/*
38940Sstevel@tonic-gate 	 * wait for zsched to exit
38950Sstevel@tonic-gate 	 */
38960Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_DEAD);
38970Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_DESTROY);
38983448Sdh155122 	zone->zone_netstack = NULL;
38990Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
39000Sstevel@tonic-gate 	zone_rele(zone);
39010Sstevel@tonic-gate 	zone = NULL;	/* potentially free'd */
39020Sstevel@tonic-gate 
39030Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
39040Sstevel@tonic-gate 	for (; /* ever */; ) {
39050Sstevel@tonic-gate 		boolean_t unref;
39060Sstevel@tonic-gate 
39070Sstevel@tonic-gate 		if ((zone = zone_find_all_by_id(zoneid)) == NULL ||
39080Sstevel@tonic-gate 		    zone->zone_uniqid != uniqid) {
39090Sstevel@tonic-gate 			/*
39100Sstevel@tonic-gate 			 * The zone has gone away.  Necessary conditions
39110Sstevel@tonic-gate 			 * are met, so we return success.
39120Sstevel@tonic-gate 			 */
39130Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
39140Sstevel@tonic-gate 			return (0);
39150Sstevel@tonic-gate 		}
39160Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
39170Sstevel@tonic-gate 		unref = ZONE_IS_UNREF(zone);
39180Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
39190Sstevel@tonic-gate 		if (unref) {
39200Sstevel@tonic-gate 			/*
39210Sstevel@tonic-gate 			 * There is only one reference to the zone -- that
39220Sstevel@tonic-gate 			 * added when the zone was added to the hashtables --
39230Sstevel@tonic-gate 			 * and things will remain this way until we drop
39240Sstevel@tonic-gate 			 * zonehash_lock... we can go ahead and cleanup the
39250Sstevel@tonic-gate 			 * zone.
39260Sstevel@tonic-gate 			 */
39270Sstevel@tonic-gate 			break;
39280Sstevel@tonic-gate 		}
39290Sstevel@tonic-gate 
39300Sstevel@tonic-gate 		if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) {
39310Sstevel@tonic-gate 			/* Signaled */
39320Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
39330Sstevel@tonic-gate 			return (set_errno(EINTR));
39340Sstevel@tonic-gate 		}
39350Sstevel@tonic-gate 
39360Sstevel@tonic-gate 	}
39370Sstevel@tonic-gate 
3938*3792Sakolb 	/*
3939*3792Sakolb 	 * Remove CPU cap for this zone now since we're not going to
3940*3792Sakolb 	 * fail below this point.
3941*3792Sakolb 	 */
3942*3792Sakolb 	cpucaps_zone_remove(zone);
3943*3792Sakolb 
3944*3792Sakolb 	/* Get rid of the zone's kstats */
39453247Sgjelinek 	zone_kstat_delete(zone);
39463247Sgjelinek 
39473671Ssl108498 	/* Say goodbye to brand framework. */
39483671Ssl108498 	brand_unregister_zone(zone->zone_brand);
39493671Ssl108498 
39500Sstevel@tonic-gate 	/*
39510Sstevel@tonic-gate 	 * It is now safe to let the zone be recreated; remove it from the
39520Sstevel@tonic-gate 	 * lists.  The memory will not be freed until the last cred
39530Sstevel@tonic-gate 	 * reference goes away.
39540Sstevel@tonic-gate 	 */
39550Sstevel@tonic-gate 	ASSERT(zonecount > 1);	/* must be > 1; can't destroy global zone */
39560Sstevel@tonic-gate 	zonecount--;
39570Sstevel@tonic-gate 	/* remove from active list and hash tables */
39580Sstevel@tonic-gate 	list_remove(&zone_active, zone);
39590Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyname,
39600Sstevel@tonic-gate 	    (mod_hash_key_t)zone->zone_name);
39610Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyid,
39620Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id);
39631769Scarlsonj 	if (zone->zone_flags & ZF_HASHED_LABEL)
39641676Sjpk 		(void) mod_hash_destroy(zonehashbylabel,
39651676Sjpk 		    (mod_hash_key_t)zone->zone_slabel);
39660Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
39670Sstevel@tonic-gate 
3968766Scarlsonj 	/*
3969766Scarlsonj 	 * Release the root vnode; we're not using it anymore.  Nor should any
3970766Scarlsonj 	 * other thread that might access it exist.
3971766Scarlsonj 	 */
3972766Scarlsonj 	if (zone->zone_rootvp != NULL) {
3973766Scarlsonj 		VN_RELE(zone->zone_rootvp);
3974766Scarlsonj 		zone->zone_rootvp = NULL;
3975766Scarlsonj 	}
3976766Scarlsonj 
39770Sstevel@tonic-gate 	/* add to deathrow list */
39780Sstevel@tonic-gate 	mutex_enter(&zone_deathrow_lock);
39790Sstevel@tonic-gate 	list_insert_tail(&zone_deathrow, zone);
39800Sstevel@tonic-gate 	mutex_exit(&zone_deathrow_lock);
39810Sstevel@tonic-gate 
39820Sstevel@tonic-gate 	/*
39830Sstevel@tonic-gate 	 * Drop last reference (which was added by zsched()), this will
39840Sstevel@tonic-gate 	 * free the zone unless there are outstanding cred references.
39850Sstevel@tonic-gate 	 */
39860Sstevel@tonic-gate 	zone_rele(zone);
39870Sstevel@tonic-gate 	return (0);
39880Sstevel@tonic-gate }
39890Sstevel@tonic-gate 
39900Sstevel@tonic-gate /*
39910Sstevel@tonic-gate  * Systemcall entry point for zone_getattr(2).
39920Sstevel@tonic-gate  */
39930Sstevel@tonic-gate static ssize_t
39940Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
39950Sstevel@tonic-gate {
39960Sstevel@tonic-gate 	size_t size;
39970Sstevel@tonic-gate 	int error = 0, err;
39980Sstevel@tonic-gate 	zone_t *zone;
39990Sstevel@tonic-gate 	char *zonepath;
40002267Sdp 	char *outstr;
40010Sstevel@tonic-gate 	zone_status_t zone_status;
40020Sstevel@tonic-gate 	pid_t initpid;
4003*3792Sakolb 	boolean_t global = (curzone == global_zone);
4004*3792Sakolb 	boolean_t inzone = (curzone->zone_id == zoneid);
40053448Sdh155122 	ushort_t flags;
40060Sstevel@tonic-gate 
40070Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
40080Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
40090Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
40100Sstevel@tonic-gate 		return (set_errno(EINVAL));
40110Sstevel@tonic-gate 	}
40120Sstevel@tonic-gate 	zone_status = zone_status_get(zone);
40130Sstevel@tonic-gate 	if (zone_status < ZONE_IS_READY) {
40140Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
40150Sstevel@tonic-gate 		return (set_errno(EINVAL));
40160Sstevel@tonic-gate 	}
40170Sstevel@tonic-gate 	zone_hold(zone);
40180Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
40190Sstevel@tonic-gate 
40200Sstevel@tonic-gate 	/*
40211676Sjpk 	 * If not in the global zone, don't show information about other zones,
40221676Sjpk 	 * unless the system is labeled and the local zone's label dominates
40231676Sjpk 	 * the other zone.
40240Sstevel@tonic-gate 	 */
40251676Sjpk 	if (!zone_list_access(zone)) {
40260Sstevel@tonic-gate 		zone_rele(zone);
40270Sstevel@tonic-gate 		return (set_errno(EINVAL));
40280Sstevel@tonic-gate 	}
40290Sstevel@tonic-gate 
40300Sstevel@tonic-gate 	switch (attr) {
40310Sstevel@tonic-gate 	case ZONE_ATTR_ROOT:
40320Sstevel@tonic-gate 		if (global) {
40330Sstevel@tonic-gate 			/*
40340Sstevel@tonic-gate 			 * Copy the path to trim the trailing "/" (except for
40350Sstevel@tonic-gate 			 * the global zone).
40360Sstevel@tonic-gate 			 */
40370Sstevel@tonic-gate 			if (zone != global_zone)
40380Sstevel@tonic-gate 				size = zone->zone_rootpathlen - 1;
40390Sstevel@tonic-gate 			else
40400Sstevel@tonic-gate 				size = zone->zone_rootpathlen;
40410Sstevel@tonic-gate 			zonepath = kmem_alloc(size, KM_SLEEP);
40420Sstevel@tonic-gate 			bcopy(zone->zone_rootpath, zonepath, size);
40430Sstevel@tonic-gate 			zonepath[size - 1] = '\0';
40440Sstevel@tonic-gate 		} else {
4045*3792Sakolb 			if (inzone || !is_system_labeled()) {
40461676Sjpk 				/*
40471676Sjpk 				 * Caller is not in the global zone.
40481676Sjpk 				 * if the query is on the current zone
40491676Sjpk 				 * or the system is not labeled,
40501676Sjpk 				 * just return faked-up path for current zone.
40511676Sjpk 				 */
40521676Sjpk 				zonepath = "/";
40531676Sjpk 				size = 2;
40541676Sjpk 			} else {
40551676Sjpk 				/*
40561676Sjpk 				 * Return related path for current zone.
40571676Sjpk 				 */
40581676Sjpk 				int prefix_len = strlen(zone_prefix);
40591676Sjpk 				int zname_len = strlen(zone->zone_name);
40601676Sjpk 
40611676Sjpk 				size = prefix_len + zname_len + 1;
40621676Sjpk 				zonepath = kmem_alloc(size, KM_SLEEP);
40631676Sjpk 				bcopy(zone_prefix, zonepath, prefix_len);
40641676Sjpk 				bcopy(zone->zone_name, zonepath +
40652267Sdp 				    prefix_len, zname_len);
40661676Sjpk 				zonepath[size - 1] = '\0';
40671676Sjpk 			}
40680Sstevel@tonic-gate 		}
40690Sstevel@tonic-gate 		if (bufsize > size)
40700Sstevel@tonic-gate 			bufsize = size;
40710Sstevel@tonic-gate 		if (buf != NULL) {
40720Sstevel@tonic-gate 			err = copyoutstr(zonepath, buf, bufsize, NULL);
40730Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
40740Sstevel@tonic-gate 				error = EFAULT;
40750Sstevel@tonic-gate 		}
4076*3792Sakolb 		if (global || (is_system_labeled() && !inzone))
40770Sstevel@tonic-gate 			kmem_free(zonepath, size);
40780Sstevel@tonic-gate 		break;
40790Sstevel@tonic-gate 
40800Sstevel@tonic-gate 	case ZONE_ATTR_NAME:
40810Sstevel@tonic-gate 		size = strlen(zone->zone_name) + 1;
40820Sstevel@tonic-gate 		if (bufsize > size)
40830Sstevel@tonic-gate 			bufsize = size;
40840Sstevel@tonic-gate 		if (buf != NULL) {
40850Sstevel@tonic-gate 			err = copyoutstr(zone->zone_name, buf, bufsize, NULL);
40860Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
40870Sstevel@tonic-gate 				error = EFAULT;
40880Sstevel@tonic-gate 		}
40890Sstevel@tonic-gate 		break;
40900Sstevel@tonic-gate 
40910Sstevel@tonic-gate 	case ZONE_ATTR_STATUS:
40920Sstevel@tonic-gate 		/*
40930Sstevel@tonic-gate 		 * Since we're not holding zonehash_lock, the zone status
40940Sstevel@tonic-gate 		 * may be anything; leave it up to userland to sort it out.
40950Sstevel@tonic-gate 		 */
40960Sstevel@tonic-gate 		size = sizeof (zone_status);
40970Sstevel@tonic-gate 		if (bufsize > size)
40980Sstevel@tonic-gate 			bufsize = size;
40990Sstevel@tonic-gate 		zone_status = zone_status_get(zone);
41000Sstevel@tonic-gate 		if (buf != NULL &&
41010Sstevel@tonic-gate 		    copyout(&zone_status, buf, bufsize) != 0)
41020Sstevel@tonic-gate 			error = EFAULT;
41030Sstevel@tonic-gate 		break;
41043448Sdh155122 	case ZONE_ATTR_FLAGS:
41053448Sdh155122 		size = sizeof (zone->zone_flags);
41063448Sdh155122 		if (bufsize > size)
41073448Sdh155122 			bufsize = size;
41083448Sdh155122 		flags = zone->zone_flags;
41093448Sdh155122 		if (buf != NULL &&
41103448Sdh155122 		    copyout(&flags, buf, bufsize) != 0)
41113448Sdh155122 			error = EFAULT;
41123448Sdh155122 		break;
41130Sstevel@tonic-gate 	case ZONE_ATTR_PRIVSET:
41140Sstevel@tonic-gate 		size = sizeof (priv_set_t);
41150Sstevel@tonic-gate 		if (bufsize > size)
41160Sstevel@tonic-gate 			bufsize = size;
41170Sstevel@tonic-gate 		if (buf != NULL &&
41180Sstevel@tonic-gate 		    copyout(zone->zone_privset, buf, bufsize) != 0)
41190Sstevel@tonic-gate 			error = EFAULT;
41200Sstevel@tonic-gate 		break;
41210Sstevel@tonic-gate 	case ZONE_ATTR_UNIQID:
41220Sstevel@tonic-gate 		size = sizeof (zone->zone_uniqid);
41230Sstevel@tonic-gate 		if (bufsize > size)
41240Sstevel@tonic-gate 			bufsize = size;
41250Sstevel@tonic-gate 		if (buf != NULL &&
41260Sstevel@tonic-gate 		    copyout(&zone->zone_uniqid, buf, bufsize) != 0)
41270Sstevel@tonic-gate 			error = EFAULT;
41280Sstevel@tonic-gate 		break;
41290Sstevel@tonic-gate 	case ZONE_ATTR_POOLID:
41300Sstevel@tonic-gate 		{
41310Sstevel@tonic-gate 			pool_t *pool;
41320Sstevel@tonic-gate 			poolid_t poolid;
41330Sstevel@tonic-gate 
41340Sstevel@tonic-gate 			if (pool_lock_intr() != 0) {
41350Sstevel@tonic-gate 				error = EINTR;
41360Sstevel@tonic-gate 				break;
41370Sstevel@tonic-gate 			}
41380Sstevel@tonic-gate 			pool = zone_pool_get(zone);
41390Sstevel@tonic-gate 			poolid = pool->pool_id;
41400Sstevel@tonic-gate 			pool_unlock();
41410Sstevel@tonic-gate 			size = sizeof (poolid);
41420Sstevel@tonic-gate 			if (bufsize > size)
41430Sstevel@tonic-gate 				bufsize = size;
41440Sstevel@tonic-gate 			if (buf != NULL && copyout(&poolid, buf, size) != 0)
41450Sstevel@tonic-gate 				error = EFAULT;
41460Sstevel@tonic-gate 		}
41470Sstevel@tonic-gate 		break;
41481676Sjpk 	case ZONE_ATTR_SLBL:
41491676Sjpk 		size = sizeof (bslabel_t);
41501676Sjpk 		if (bufsize > size)
41511676Sjpk 			bufsize = size;
41521676Sjpk 		if (zone->zone_slabel == NULL)
41531676Sjpk 			error = EINVAL;
41541676Sjpk 		else if (buf != NULL &&
41551676Sjpk 		    copyout(label2bslabel(zone->zone_slabel), buf,
41561676Sjpk 		    bufsize) != 0)
41571676Sjpk 			error = EFAULT;
41581676Sjpk 		break;
41590Sstevel@tonic-gate 	case ZONE_ATTR_INITPID:
41600Sstevel@tonic-gate 		size = sizeof (initpid);
41610Sstevel@tonic-gate 		if (bufsize > size)
41620Sstevel@tonic-gate 			bufsize = size;
41630Sstevel@tonic-gate 		initpid = zone->zone_proc_initpid;
41640Sstevel@tonic-gate 		if (initpid == -1) {
41650Sstevel@tonic-gate 			error = ESRCH;
41660Sstevel@tonic-gate 			break;
41670Sstevel@tonic-gate 		}
41680Sstevel@tonic-gate 		if (buf != NULL &&
41690Sstevel@tonic-gate 		    copyout(&initpid, buf, bufsize) != 0)
41700Sstevel@tonic-gate 			error = EFAULT;
41710Sstevel@tonic-gate 		break;
41722712Snn35248 	case ZONE_ATTR_BRAND:
41732712Snn35248 		size = strlen(zone->zone_brand->b_name) + 1;
41742712Snn35248 
41752712Snn35248 		if (bufsize > size)
41762712Snn35248 			bufsize = size;
41772712Snn35248 		if (buf != NULL) {
41782712Snn35248 			err = copyoutstr(zone->zone_brand->b_name, buf,
41792712Snn35248 			    bufsize, NULL);
41802712Snn35248 			if (err != 0 && err != ENAMETOOLONG)
41812712Snn35248 				error = EFAULT;
41822712Snn35248 		}
41832712Snn35248 		break;
41842267Sdp 	case ZONE_ATTR_INITNAME:
41852267Sdp 		size = strlen(zone->zone_initname) + 1;
41862267Sdp 		if (bufsize > size)
41872267Sdp 			bufsize = size;
41882267Sdp 		if (buf != NULL) {
41892267Sdp 			err = copyoutstr(zone->zone_initname, buf, bufsize,
41902267Sdp 			    NULL);
41912267Sdp 			if (err != 0 && err != ENAMETOOLONG)
41922267Sdp 				error = EFAULT;
41932267Sdp 		}
41942267Sdp 		break;
41952267Sdp 	case ZONE_ATTR_BOOTARGS:
41962267Sdp 		if (zone->zone_bootargs == NULL)
41972267Sdp 			outstr = "";
41982267Sdp 		else
41992267Sdp 			outstr = zone->zone_bootargs;
42002267Sdp 		size = strlen(outstr) + 1;
42012267Sdp 		if (bufsize > size)
42022267Sdp 			bufsize = size;
42032267Sdp 		if (buf != NULL) {
42042267Sdp 			err = copyoutstr(outstr, buf, bufsize, NULL);
42052267Sdp 			if (err != 0 && err != ENAMETOOLONG)
42062267Sdp 				error = EFAULT;
42072267Sdp 		}
42082267Sdp 		break;
42093247Sgjelinek 	case ZONE_ATTR_PHYS_MCAP:
42103247Sgjelinek 		size = sizeof (zone->zone_phys_mcap);
42113247Sgjelinek 		if (bufsize > size)
42123247Sgjelinek 			bufsize = size;
42133247Sgjelinek 		if (buf != NULL &&
42143247Sgjelinek 		    copyout(&zone->zone_phys_mcap, buf, bufsize) != 0)
42153247Sgjelinek 			error = EFAULT;
42163247Sgjelinek 		break;
42173247Sgjelinek 	case ZONE_ATTR_SCHED_CLASS:
42183247Sgjelinek 		mutex_enter(&class_lock);
42193247Sgjelinek 
42203247Sgjelinek 		if (zone->zone_defaultcid >= loaded_classes)
42213247Sgjelinek 			outstr = "";
42223247Sgjelinek 		else
42233247Sgjelinek 			outstr = sclass[zone->zone_defaultcid].cl_name;
42243247Sgjelinek 		size = strlen(outstr) + 1;
42253247Sgjelinek 		if (bufsize > size)
42263247Sgjelinek 			bufsize = size;
42273247Sgjelinek 		if (buf != NULL) {
42283247Sgjelinek 			err = copyoutstr(outstr, buf, bufsize, NULL);
42293247Sgjelinek 			if (err != 0 && err != ENAMETOOLONG)
42303247Sgjelinek 				error = EFAULT;
42313247Sgjelinek 		}
42323247Sgjelinek 
42333247Sgjelinek 		mutex_exit(&class_lock);
42343247Sgjelinek 		break;
42350Sstevel@tonic-gate 	default:
42362712Snn35248 		if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) {
42372712Snn35248 			size = bufsize;
42382712Snn35248 			error = ZBROP(zone)->b_getattr(zone, attr, buf, &size);
42392712Snn35248 		} else {
42402712Snn35248 			error = EINVAL;
42412712Snn35248 		}
42420Sstevel@tonic-gate 	}
42430Sstevel@tonic-gate 	zone_rele(zone);
42440Sstevel@tonic-gate 
42450Sstevel@tonic-gate 	if (error)
42460Sstevel@tonic-gate 		return (set_errno(error));
42470Sstevel@tonic-gate 	return ((ssize_t)size);
42480Sstevel@tonic-gate }
42490Sstevel@tonic-gate 
42500Sstevel@tonic-gate /*
42512267Sdp  * Systemcall entry point for zone_setattr(2).
42522267Sdp  */
42532267Sdp /*ARGSUSED*/
42542267Sdp static int
42552267Sdp zone_setattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
42562267Sdp {
42572267Sdp 	zone_t *zone;
42582267Sdp 	zone_status_t zone_status;
42592712Snn35248 	struct brand_attr *attrp;
42602267Sdp 	int err;
42612267Sdp 
42622267Sdp 	if (secpolicy_zone_config(CRED()) != 0)
42632267Sdp 		return (set_errno(EPERM));
42642267Sdp 
42652267Sdp 	/*
42663247Sgjelinek 	 * Only the ZONE_ATTR_PHYS_MCAP attribute can be set on the
42673247Sgjelinek 	 * global zone.
42682267Sdp 	 */
42693247Sgjelinek 	if (zoneid == GLOBAL_ZONEID && attr != ZONE_ATTR_PHYS_MCAP) {
42702267Sdp 		return (set_errno(EINVAL));
42712267Sdp 	}
42722267Sdp 
42732267Sdp 	mutex_enter(&zonehash_lock);
42742267Sdp 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
42752267Sdp 		mutex_exit(&zonehash_lock);
42762267Sdp 		return (set_errno(EINVAL));
42772267Sdp 	}
42782267Sdp 	zone_hold(zone);
42792267Sdp 	mutex_exit(&zonehash_lock);
42802267Sdp 
42813247Sgjelinek 	/*
42823247Sgjelinek 	 * At present most attributes can only be set on non-running,
42833247Sgjelinek 	 * non-global zones.
42843247Sgjelinek 	 */
42852267Sdp 	zone_status = zone_status_get(zone);
42863247Sgjelinek 	if (attr != ZONE_ATTR_PHYS_MCAP && zone_status > ZONE_IS_READY)
42872267Sdp 		goto done;
42882267Sdp 
42892267Sdp 	switch (attr) {
42902267Sdp 	case ZONE_ATTR_INITNAME:
42912267Sdp 		err = zone_set_initname(zone, (const char *)buf);
42922267Sdp 		break;
42932267Sdp 	case ZONE_ATTR_BOOTARGS:
42942267Sdp 		err = zone_set_bootargs(zone, (const char *)buf);
42952267Sdp 		break;
42962712Snn35248 	case ZONE_ATTR_BRAND:
42972712Snn35248 		ASSERT(!ZONE_IS_BRANDED(zone));
42982712Snn35248 		err = 0;
42992712Snn35248 		attrp = kmem_alloc(sizeof (struct brand_attr), KM_SLEEP);
43002712Snn35248 		if ((buf == NULL) ||
43012712Snn35248 		    (copyin(buf, attrp, sizeof (struct brand_attr)) != 0)) {
43022712Snn35248 			kmem_free(attrp, sizeof (struct brand_attr));
43032712Snn35248 			err = EFAULT;
43042712Snn35248 			break;
43052712Snn35248 		}
43062712Snn35248 
43072712Snn35248 		if (is_system_labeled() && strncmp(attrp->ba_brandname,
43082712Snn35248 		    NATIVE_BRAND_NAME, MAXNAMELEN) != 0) {
43092712Snn35248 			err = EPERM;
43102712Snn35248 			break;
43112712Snn35248 		}
43122712Snn35248 
43132712Snn35248 		zone->zone_brand = brand_register_zone(attrp);
43142712Snn35248 		kmem_free(attrp, sizeof (struct brand_attr));
43152712Snn35248 		if (zone->zone_brand == NULL)
43162712Snn35248 			err = EINVAL;
43172712Snn35248 		break;
43183247Sgjelinek 	case ZONE_ATTR_PHYS_MCAP:
43193247Sgjelinek 		err = zone_set_phys_mcap(zone, (const uint64_t *)buf);
43203247Sgjelinek 		break;
43213247Sgjelinek 	case ZONE_ATTR_SCHED_CLASS:
43223247Sgjelinek 		err = zone_set_sched_class(zone, (const char *)buf);
43233247Sgjelinek 		break;
43242267Sdp 	default:
43252712Snn35248 		if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone))
43262712Snn35248 			err = ZBROP(zone)->b_setattr(zone, attr, buf, bufsize);
43272712Snn35248 		else
43282712Snn35248 			err = EINVAL;
43292267Sdp 	}
43302267Sdp 
43312267Sdp done:
43322267Sdp 	zone_rele(zone);
43332267Sdp 	return (err != 0 ? set_errno(err) : 0);
43342267Sdp }
43352267Sdp 
43362267Sdp /*
43370Sstevel@tonic-gate  * Return zero if the process has at least one vnode mapped in to its
43380Sstevel@tonic-gate  * address space which shouldn't be allowed to change zones.
43393247Sgjelinek  *
43403247Sgjelinek  * Also return zero if the process has any shared mappings which reserve
43413247Sgjelinek  * swap.  This is because the counting for zone.max-swap does not allow swap
43423247Sgjelinek  * revervation to be shared between zones.  zone swap reservation is counted
43433247Sgjelinek  * on zone->zone_max_swap.
43440Sstevel@tonic-gate  */
43450Sstevel@tonic-gate static int
43460Sstevel@tonic-gate as_can_change_zones(void)
43470Sstevel@tonic-gate {
43480Sstevel@tonic-gate 	proc_t *pp = curproc;
43490Sstevel@tonic-gate 	struct seg *seg;
43500Sstevel@tonic-gate 	struct as *as = pp->p_as;
43510Sstevel@tonic-gate 	vnode_t *vp;
43520Sstevel@tonic-gate 	int allow = 1;
43530Sstevel@tonic-gate 
43540Sstevel@tonic-gate 	ASSERT(pp->p_as != &kas);
43553247Sgjelinek 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
43560Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
43573247Sgjelinek 
43583247Sgjelinek 		/*
43593247Sgjelinek 		 * Cannot enter zone with shared anon memory which
43603247Sgjelinek 		 * reserves swap.  See comment above.
43613247Sgjelinek 		 */
43623247Sgjelinek 		if (seg_can_change_zones(seg) == B_FALSE) {
43633247Sgjelinek 			allow = 0;
43643247Sgjelinek 			break;
43653247Sgjelinek 		}
43660Sstevel@tonic-gate 		/*
43670Sstevel@tonic-gate 		 * if we can't get a backing vnode for this segment then skip
43680Sstevel@tonic-gate 		 * it.
43690Sstevel@tonic-gate 		 */
43700Sstevel@tonic-gate 		vp = NULL;
43710Sstevel@tonic-gate 		if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL)
43720Sstevel@tonic-gate 			continue;
43730Sstevel@tonic-gate 		if (!vn_can_change_zones(vp)) { /* bail on first match */
43740Sstevel@tonic-gate 			allow = 0;
43750Sstevel@tonic-gate 			break;
43760Sstevel@tonic-gate 		}
43770Sstevel@tonic-gate 	}
43783247Sgjelinek 	AS_LOCK_EXIT(as, &as->a_lock);
43790Sstevel@tonic-gate 	return (allow);
43800Sstevel@tonic-gate }
43810Sstevel@tonic-gate 
43820Sstevel@tonic-gate /*
43833247Sgjelinek  * Count swap reserved by curproc's address space
43843247Sgjelinek  */
43853247Sgjelinek static size_t
43863247Sgjelinek as_swresv(void)
43873247Sgjelinek {
43883247Sgjelinek 	proc_t *pp = curproc;
43893247Sgjelinek 	struct seg *seg;
43903247Sgjelinek 	struct as *as = pp->p_as;
43913247Sgjelinek 	size_t swap = 0;
43923247Sgjelinek 
43933247Sgjelinek 	ASSERT(pp->p_as != &kas);
43943247Sgjelinek 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
43953247Sgjelinek 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg))
43963247Sgjelinek 		swap += seg_swresv(seg);
43973247Sgjelinek 
43983247Sgjelinek 	return (swap);
43993247Sgjelinek }
44003247Sgjelinek 
44013247Sgjelinek /*
44020Sstevel@tonic-gate  * Systemcall entry point for zone_enter().
44030Sstevel@tonic-gate  *
44040Sstevel@tonic-gate  * The current process is injected into said zone.  In the process
44050Sstevel@tonic-gate  * it will change its project membership, privileges, rootdir/cwd,
44060Sstevel@tonic-gate  * zone-wide rctls, and pool association to match those of the zone.
44070Sstevel@tonic-gate  *
44080Sstevel@tonic-gate  * The first zone_enter() called while the zone is in the ZONE_IS_READY
44090Sstevel@tonic-gate  * state will transition it to ZONE_IS_RUNNING.  Processes may only
44100Sstevel@tonic-gate  * enter a zone that is "ready" or "running".
44110Sstevel@tonic-gate  */
44120Sstevel@tonic-gate static int
44130Sstevel@tonic-gate zone_enter(zoneid_t zoneid)
44140Sstevel@tonic-gate {
44150Sstevel@tonic-gate 	zone_t *zone;
44160Sstevel@tonic-gate 	vnode_t *vp;
44170Sstevel@tonic-gate 	proc_t *pp = curproc;
44180Sstevel@tonic-gate 	contract_t *ct;
44190Sstevel@tonic-gate 	cont_process_t *ctp;
44200Sstevel@tonic-gate 	task_t *tk, *oldtk;
44210Sstevel@tonic-gate 	kproject_t *zone_proj0;
44220Sstevel@tonic-gate 	cred_t *cr, *newcr;
44230Sstevel@tonic-gate 	pool_t *oldpool, *newpool;
44240Sstevel@tonic-gate 	sess_t *sp;
44250Sstevel@tonic-gate 	uid_t uid;
44260Sstevel@tonic-gate 	zone_status_t status;
44270Sstevel@tonic-gate 	int err = 0;
44280Sstevel@tonic-gate 	rctl_entity_p_t e;
44293247Sgjelinek 	size_t swap;
4430*3792Sakolb 	kthread_id_t t;
44310Sstevel@tonic-gate 
44320Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
44330Sstevel@tonic-gate 		return (set_errno(EPERM));
44340Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
44350Sstevel@tonic-gate 		return (set_errno(EINVAL));
44360Sstevel@tonic-gate 
44370Sstevel@tonic-gate 	/*
44380Sstevel@tonic-gate 	 * Stop all lwps so we don't need to hold a lock to look at
44390Sstevel@tonic-gate 	 * curproc->p_zone.  This needs to happen before we grab any
44400Sstevel@tonic-gate 	 * locks to avoid deadlock (another lwp in the process could
44410Sstevel@tonic-gate 	 * be waiting for the held lock).
44420Sstevel@tonic-gate 	 */
44430Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK))
44440Sstevel@tonic-gate 		return (set_errno(EINTR));
44450Sstevel@tonic-gate 
44460Sstevel@tonic-gate 	/*
44470Sstevel@tonic-gate 	 * Make sure we're not changing zones with files open or mapped in
44480Sstevel@tonic-gate 	 * to our address space which shouldn't be changing zones.
44490Sstevel@tonic-gate 	 */
44500Sstevel@tonic-gate 	if (!files_can_change_zones()) {
44510Sstevel@tonic-gate 		err = EBADF;
44520Sstevel@tonic-gate 		goto out;
44530Sstevel@tonic-gate 	}
44540Sstevel@tonic-gate 	if (!as_can_change_zones()) {
44550Sstevel@tonic-gate 		err = EFAULT;
44560Sstevel@tonic-gate 		goto out;
44570Sstevel@tonic-gate 	}
44580Sstevel@tonic-gate 
44590Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
44600Sstevel@tonic-gate 	if (pp->p_zone != global_zone) {
44610Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
44620Sstevel@tonic-gate 		err = EINVAL;
44630Sstevel@tonic-gate 		goto out;
44640Sstevel@tonic-gate 	}
44650Sstevel@tonic-gate 
44660Sstevel@tonic-gate 	zone = zone_find_all_by_id(zoneid);
44670Sstevel@tonic-gate 	if (zone == NULL) {
44680Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
44690Sstevel@tonic-gate 		err = EINVAL;
44700Sstevel@tonic-gate 		goto out;
44710Sstevel@tonic-gate 	}
44720Sstevel@tonic-gate 
44730Sstevel@tonic-gate 	/*
44740Sstevel@tonic-gate 	 * To prevent processes in a zone from holding contracts on
44750Sstevel@tonic-gate 	 * extrazonal resources, and to avoid process contract
44760Sstevel@tonic-gate 	 * memberships which span zones, contract holders and processes
44770Sstevel@tonic-gate 	 * which aren't the sole members of their encapsulating process
44780Sstevel@tonic-gate 	 * contracts are not allowed to zone_enter.
44790Sstevel@tonic-gate 	 */
44800Sstevel@tonic-gate 	ctp = pp->p_ct_process;
44810Sstevel@tonic-gate 	ct = &ctp->conp_contract;
44820Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
44830Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
44840Sstevel@tonic-gate 	if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) {
44850Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
44860Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
44870Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
44880Sstevel@tonic-gate 		pool_unlock();
44890Sstevel@tonic-gate 		err = EINVAL;
44900Sstevel@tonic-gate 		goto out;
44910Sstevel@tonic-gate 	}
44920Sstevel@tonic-gate 
44930Sstevel@tonic-gate 	/*
44940Sstevel@tonic-gate 	 * Moreover, we don't allow processes whose encapsulating
44950Sstevel@tonic-gate 	 * process contracts have inherited extrazonal contracts.
44960Sstevel@tonic-gate 	 * While it would be easier to eliminate all process contracts
44970Sstevel@tonic-gate 	 * with inherited contracts, we need to be able to give a
44980Sstevel@tonic-gate 	 * restarted init (or other zone-penetrating process) its
44990Sstevel@tonic-gate 	 * predecessor's contracts.
45000Sstevel@tonic-gate 	 */
45010Sstevel@tonic-gate 	if (ctp->conp_ninherited != 0) {
45020Sstevel@tonic-gate 		contract_t *next;
45030Sstevel@tonic-gate 		for (next = list_head(&ctp->conp_inherited); next;
45040Sstevel@tonic-gate 		    next = list_next(&ctp->conp_inherited, next)) {
45050Sstevel@tonic-gate 			if (contract_getzuniqid(next) != zone->zone_uniqid) {
45060Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
45070Sstevel@tonic-gate 				mutex_exit(&ct->ct_lock);
45080Sstevel@tonic-gate 				mutex_exit(&zonehash_lock);
45090Sstevel@tonic-gate 				pool_unlock();
45100Sstevel@tonic-gate 				err = EINVAL;
45110Sstevel@tonic-gate 				goto out;
45120Sstevel@tonic-gate 			}
45130Sstevel@tonic-gate 		}
45140Sstevel@tonic-gate 	}
45150Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
45160Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
45170Sstevel@tonic-gate 
45180Sstevel@tonic-gate 	status = zone_status_get(zone);
45190Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) {
45200Sstevel@tonic-gate 		/*
45210Sstevel@tonic-gate 		 * Can't join
45220Sstevel@tonic-gate 		 */
45230Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
45240Sstevel@tonic-gate 		err = EINVAL;
45250Sstevel@tonic-gate 		goto out;
45260Sstevel@tonic-gate 	}
45270Sstevel@tonic-gate 
45280Sstevel@tonic-gate 	/*
45290Sstevel@tonic-gate 	 * Make sure new priv set is within the permitted set for caller
45300Sstevel@tonic-gate 	 */
45310Sstevel@tonic-gate 	if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) {
45320Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
45330Sstevel@tonic-gate 		err = EPERM;
45340Sstevel@tonic-gate 		goto out;
45350Sstevel@tonic-gate 	}
45360Sstevel@tonic-gate 	/*
45370Sstevel@tonic-gate 	 * We want to momentarily drop zonehash_lock while we optimistically
45380Sstevel@tonic-gate 	 * bind curproc to the pool it should be running in.  This is safe
45390Sstevel@tonic-gate 	 * since the zone can't disappear (we have a hold on it).
45400Sstevel@tonic-gate 	 */
45410Sstevel@tonic-gate 	zone_hold(zone);
45420Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
45430Sstevel@tonic-gate 
45440Sstevel@tonic-gate 	/*
45450Sstevel@tonic-gate 	 * Grab pool_lock to keep the pools configuration from changing
45460Sstevel@tonic-gate 	 * and to stop ourselves from getting rebound to another pool
45470Sstevel@tonic-gate 	 * until we join the zone.
45480Sstevel@tonic-gate 	 */
45490Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
45500Sstevel@tonic-gate 		zone_rele(zone);
45510Sstevel@tonic-gate 		err = EINTR;
45520Sstevel@tonic-gate 		goto out;
45530Sstevel@tonic-gate 	}
45540Sstevel@tonic-gate 	ASSERT(secpolicy_pool(CRED()) == 0);
45550Sstevel@tonic-gate 	/*
45560Sstevel@tonic-gate 	 * Bind ourselves to the pool currently associated with the zone.
45570Sstevel@tonic-gate 	 */
45580Sstevel@tonic-gate 	oldpool = curproc->p_pool;
45590Sstevel@tonic-gate 	newpool = zone_pool_get(zone);
45600Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED && newpool != oldpool &&
45610Sstevel@tonic-gate 	    (err = pool_do_bind(newpool, P_PID, P_MYID,
45620Sstevel@tonic-gate 	    POOL_BIND_ALL)) != 0) {
45630Sstevel@tonic-gate 		pool_unlock();
45640Sstevel@tonic-gate 		zone_rele(zone);
45650Sstevel@tonic-gate 		goto out;
45660Sstevel@tonic-gate 	}
45670Sstevel@tonic-gate 
45680Sstevel@tonic-gate 	/*
45690Sstevel@tonic-gate 	 * Grab cpu_lock now; we'll need it later when we call
45700Sstevel@tonic-gate 	 * task_join().
45710Sstevel@tonic-gate 	 */
45720Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
45730Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
45740Sstevel@tonic-gate 	/*
45750Sstevel@tonic-gate 	 * Make sure the zone hasn't moved on since we dropped zonehash_lock.
45760Sstevel@tonic-gate 	 */
45770Sstevel@tonic-gate 	if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
45780Sstevel@tonic-gate 		/*
45790Sstevel@tonic-gate 		 * Can't join anymore.
45800Sstevel@tonic-gate 		 */
45810Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
45820Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
45830Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED &&
45840Sstevel@tonic-gate 		    newpool != oldpool)
45850Sstevel@tonic-gate 			(void) pool_do_bind(oldpool, P_PID, P_MYID,
45860Sstevel@tonic-gate 			    POOL_BIND_ALL);
45870Sstevel@tonic-gate 		pool_unlock();
45880Sstevel@tonic-gate 		zone_rele(zone);
45890Sstevel@tonic-gate 		err = EINVAL;
45900Sstevel@tonic-gate 		goto out;
45910Sstevel@tonic-gate 	}
45920Sstevel@tonic-gate 
45933247Sgjelinek 	/*
45943247Sgjelinek 	 * a_lock must be held while transfering locked memory and swap
45953247Sgjelinek 	 * reservation from the global zone to the non global zone because
45963247Sgjelinek 	 * asynchronous faults on the processes' address space can lock
45973247Sgjelinek 	 * memory and reserve swap via MCL_FUTURE and MAP_NORESERVE
45983247Sgjelinek 	 * segments respectively.
45993247Sgjelinek 	 */
46003247Sgjelinek 	AS_LOCK_ENTER(pp->as, &pp->p_as->a_lock, RW_WRITER);
46013247Sgjelinek 	swap = as_swresv();
46020Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
46030Sstevel@tonic-gate 	zone_proj0 = zone->zone_zsched->p_task->tk_proj;
46040Sstevel@tonic-gate 	/* verify that we do not exceed and task or lwp limits */
46050Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
46060Sstevel@tonic-gate 	/* add new lwps to zone and zone's proj0 */
46070Sstevel@tonic-gate 	zone_proj0->kpj_nlwps += pp->p_lwpcnt;
46080Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
46090Sstevel@tonic-gate 	/* add 1 task to zone's proj0 */
46100Sstevel@tonic-gate 	zone_proj0->kpj_ntasks += 1;
46110Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
46120Sstevel@tonic-gate 
46133247Sgjelinek 	mutex_enter(&zone->zone_mem_lock);
46142768Ssl108498 	zone->zone_locked_mem += pp->p_locked_mem;
46152768Ssl108498 	zone_proj0->kpj_data.kpd_locked_mem += pp->p_locked_mem;
46163247Sgjelinek 	zone->zone_max_swap += swap;
46173247Sgjelinek 	mutex_exit(&zone->zone_mem_lock);
46182768Ssl108498 
46190Sstevel@tonic-gate 	/* remove lwps from proc's old zone and old project */
46200Sstevel@tonic-gate 	mutex_enter(&pp->p_zone->zone_nlwps_lock);
46210Sstevel@tonic-gate 	pp->p_zone->zone_nlwps -= pp->p_lwpcnt;
46220Sstevel@tonic-gate 	pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt;
46230Sstevel@tonic-gate 	mutex_exit(&pp->p_zone->zone_nlwps_lock);
46240Sstevel@tonic-gate 
46253247Sgjelinek 	mutex_enter(&pp->p_zone->zone_mem_lock);
46262768Ssl108498 	pp->p_zone->zone_locked_mem -= pp->p_locked_mem;
46272768Ssl108498 	pp->p_task->tk_proj->kpj_data.kpd_locked_mem -= pp->p_locked_mem;
46283247Sgjelinek 	pp->p_zone->zone_max_swap -= swap;
46293247Sgjelinek 	mutex_exit(&pp->p_zone->zone_mem_lock);
46302768Ssl108498 
46312768Ssl108498 	mutex_exit(&pp->p_lock);
46323247Sgjelinek 	AS_LOCK_EXIT(pp->p_as, &pp->p_as->a_lock);
46332768Ssl108498 
46340Sstevel@tonic-gate 	/*
46350Sstevel@tonic-gate 	 * Joining the zone cannot fail from now on.
46360Sstevel@tonic-gate 	 *
46370Sstevel@tonic-gate 	 * This means that a lot of the following code can be commonized and
46380Sstevel@tonic-gate 	 * shared with zsched().
46390Sstevel@tonic-gate 	 */
46400Sstevel@tonic-gate 
46410Sstevel@tonic-gate 	/*
46420Sstevel@tonic-gate 	 * Reset the encapsulating process contract's zone.
46430Sstevel@tonic-gate 	 */
46440Sstevel@tonic-gate 	ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID);
46450Sstevel@tonic-gate 	contract_setzuniqid(ct, zone->zone_uniqid);
46460Sstevel@tonic-gate 
46470Sstevel@tonic-gate 	/*
46480Sstevel@tonic-gate 	 * Create a new task and associate the process with the project keyed
46490Sstevel@tonic-gate 	 * by (projid,zoneid).
46500Sstevel@tonic-gate 	 *
46510Sstevel@tonic-gate 	 * We might as well be in project 0; the global zone's projid doesn't
46520Sstevel@tonic-gate 	 * make much sense in a zone anyhow.
46530Sstevel@tonic-gate 	 *
46540Sstevel@tonic-gate 	 * This also increments zone_ntasks, and returns with p_lock held.
46550Sstevel@tonic-gate 	 */
46560Sstevel@tonic-gate 	tk = task_create(0, zone);
46570Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
46580Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
46590Sstevel@tonic-gate 
46600Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
46610Sstevel@tonic-gate 	pp->p_zone = zone;
46620Sstevel@tonic-gate 
46630Sstevel@tonic-gate 	/*
46640Sstevel@tonic-gate 	 * call RCTLOP_SET functions on this proc
46650Sstevel@tonic-gate 	 */
46660Sstevel@tonic-gate 	e.rcep_p.zone = zone;
46670Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
46680Sstevel@tonic-gate 	(void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL,
46690Sstevel@tonic-gate 	    RCD_CALLBACK);
46700Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
46710Sstevel@tonic-gate 
46720Sstevel@tonic-gate 	/*
46730Sstevel@tonic-gate 	 * We don't need to hold any of zsched's locks here; not only do we know
46740Sstevel@tonic-gate 	 * the process and zone aren't going away, we know its session isn't
46750Sstevel@tonic-gate 	 * changing either.
46760Sstevel@tonic-gate 	 *
46770Sstevel@tonic-gate 	 * By joining zsched's session here, we mimic the behavior in the
46780Sstevel@tonic-gate 	 * global zone of init's sid being the pid of sched.  We extend this
46790Sstevel@tonic-gate 	 * to all zlogin-like zone_enter()'ing processes as well.
46800Sstevel@tonic-gate 	 */
46810Sstevel@tonic-gate 	mutex_enter(&pidlock);
46820Sstevel@tonic-gate 	sp = zone->zone_zsched->p_sessp;
46832712Snn35248 	sess_hold(zone->zone_zsched);
46840Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
46850Sstevel@tonic-gate 	pgexit(pp);
46862712Snn35248 	sess_rele(pp->p_sessp, B_TRUE);
46870Sstevel@tonic-gate 	pp->p_sessp = sp;
46880Sstevel@tonic-gate 	pgjoin(pp, zone->zone_zsched->p_pidp);
46893247Sgjelinek 
46903247Sgjelinek 	/*
4691*3792Sakolb 	 * If any threads are scheduled to be placed on zone wait queue they
4692*3792Sakolb 	 * should abandon the idea since the wait queue is changing.
4693*3792Sakolb 	 * We need to be holding pidlock & p_lock to do this.
4694*3792Sakolb 	 */
4695*3792Sakolb 	if ((t = pp->p_tlist) != NULL) {
4696*3792Sakolb 		do {
4697*3792Sakolb 			thread_lock(t);
4698*3792Sakolb 			/*
4699*3792Sakolb 			 * Kick this thread so that he doesn't sit
4700*3792Sakolb 			 * on a wrong wait queue.
4701*3792Sakolb 			 */
4702*3792Sakolb 			if (ISWAITING(t))
4703*3792Sakolb 				setrun_locked(t);
4704*3792Sakolb 
4705*3792Sakolb 			if (t->t_schedflag & TS_ANYWAITQ)
4706*3792Sakolb 				t->t_schedflag &= ~ TS_ANYWAITQ;
4707*3792Sakolb 
4708*3792Sakolb 			thread_unlock(t);
4709*3792Sakolb 		} while ((t = t->t_forw) != pp->p_tlist);
4710*3792Sakolb 	}
4711*3792Sakolb 
4712*3792Sakolb 	/*
47133247Sgjelinek 	 * If there is a default scheduling class for the zone and it is not
47143247Sgjelinek 	 * the class we are currently in, change all of the threads in the
47153247Sgjelinek 	 * process to the new class.  We need to be holding pidlock & p_lock
47163247Sgjelinek 	 * when we call parmsset so this is a good place to do it.
47173247Sgjelinek 	 */
47183247Sgjelinek 	if (zone->zone_defaultcid > 0 &&
47193247Sgjelinek 	    zone->zone_defaultcid != curthread->t_cid) {
47203247Sgjelinek 		pcparms_t pcparms;
47213247Sgjelinek 
47223247Sgjelinek 		pcparms.pc_cid = zone->zone_defaultcid;
47233247Sgjelinek 		pcparms.pc_clparms[0] = 0;
47243247Sgjelinek 
47253247Sgjelinek 		/*
47263247Sgjelinek 		 * If setting the class fails, we still want to enter the zone.
47273247Sgjelinek 		 */
47283247Sgjelinek 		if ((t = pp->p_tlist) != NULL) {
47293247Sgjelinek 			do {
47303247Sgjelinek 				(void) parmsset(&pcparms, t);
47313247Sgjelinek 			} while ((t = t->t_forw) != pp->p_tlist);
47323247Sgjelinek 		}
47333247Sgjelinek 	}
47343247Sgjelinek 
47350Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
47360Sstevel@tonic-gate 	mutex_exit(&pidlock);
47370Sstevel@tonic-gate 
47380Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
47390Sstevel@tonic-gate 	/*
47400Sstevel@tonic-gate 	 * We're firmly in the zone; let pools progress.
47410Sstevel@tonic-gate 	 */
47420Sstevel@tonic-gate 	pool_unlock();
47430Sstevel@tonic-gate 	task_rele(oldtk);
47440Sstevel@tonic-gate 	/*
47450Sstevel@tonic-gate 	 * We don't need to retain a hold on the zone since we already
47460Sstevel@tonic-gate 	 * incremented zone_ntasks, so the zone isn't going anywhere.
47470Sstevel@tonic-gate 	 */
47480Sstevel@tonic-gate 	zone_rele(zone);
47490Sstevel@tonic-gate 
47500Sstevel@tonic-gate 	/*
47510Sstevel@tonic-gate 	 * Chroot
47520Sstevel@tonic-gate 	 */
47530Sstevel@tonic-gate 	vp = zone->zone_rootvp;
47540Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_cdir, pp);
47550Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_rdir, pp);
47560Sstevel@tonic-gate 
47570Sstevel@tonic-gate 	/*
47580Sstevel@tonic-gate 	 * Change process credentials
47590Sstevel@tonic-gate 	 */
47600Sstevel@tonic-gate 	newcr = cralloc();
47610Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
47620Sstevel@tonic-gate 	cr = pp->p_cred;
47630Sstevel@tonic-gate 	crcopy_to(cr, newcr);
47640Sstevel@tonic-gate 	crsetzone(newcr, zone);
47650Sstevel@tonic-gate 	pp->p_cred = newcr;
47660Sstevel@tonic-gate 
47670Sstevel@tonic-gate 	/*
47680Sstevel@tonic-gate 	 * Restrict all process privilege sets to zone limit
47690Sstevel@tonic-gate 	 */
47700Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(newcr));
47710Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(newcr));
47720Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(newcr));
47730Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(newcr));
47740Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
47750Sstevel@tonic-gate 	crset(pp, newcr);
47760Sstevel@tonic-gate 
47770Sstevel@tonic-gate 	/*
47780Sstevel@tonic-gate 	 * Adjust upcount to reflect zone entry.
47790Sstevel@tonic-gate 	 */
47800Sstevel@tonic-gate 	uid = crgetruid(newcr);
47810Sstevel@tonic-gate 	mutex_enter(&pidlock);
47820Sstevel@tonic-gate 	upcount_dec(uid, GLOBAL_ZONEID);
47830Sstevel@tonic-gate 	upcount_inc(uid, zoneid);
47840Sstevel@tonic-gate 	mutex_exit(&pidlock);
47850Sstevel@tonic-gate 
47860Sstevel@tonic-gate 	/*
47870Sstevel@tonic-gate 	 * Set up core file path and content.
47880Sstevel@tonic-gate 	 */
47890Sstevel@tonic-gate 	set_core_defaults();
47900Sstevel@tonic-gate 
47910Sstevel@tonic-gate out:
47920Sstevel@tonic-gate 	/*
47930Sstevel@tonic-gate 	 * Let the other lwps continue.
47940Sstevel@tonic-gate 	 */
47950Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
47960Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
47970Sstevel@tonic-gate 		continuelwps(pp);
47980Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
47990Sstevel@tonic-gate 
48000Sstevel@tonic-gate 	return (err != 0 ? set_errno(err) : 0);
48010Sstevel@tonic-gate }
48020Sstevel@tonic-gate 
48030Sstevel@tonic-gate /*
48040Sstevel@tonic-gate  * Systemcall entry point for zone_list(2).
48050Sstevel@tonic-gate  *
48060Sstevel@tonic-gate  * Processes running in a (non-global) zone only see themselves.
48071676Sjpk  * On labeled systems, they see all zones whose label they dominate.
48080Sstevel@tonic-gate  */
48090Sstevel@tonic-gate static int
48100Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones)
48110Sstevel@tonic-gate {
48120Sstevel@tonic-gate 	zoneid_t *zoneids;
48131769Scarlsonj 	zone_t *zone, *myzone;
48140Sstevel@tonic-gate 	uint_t user_nzones, real_nzones;
48151676Sjpk 	uint_t domi_nzones;
48161676Sjpk 	int error;
48170Sstevel@tonic-gate 
48180Sstevel@tonic-gate 	if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0)
48190Sstevel@tonic-gate 		return (set_errno(EFAULT));
48200Sstevel@tonic-gate 
48211769Scarlsonj 	myzone = curproc->p_zone;
48221769Scarlsonj 	if (myzone != global_zone) {
48231676Sjpk 		bslabel_t *mybslab;
48241676Sjpk 
48251676Sjpk 		if (!is_system_labeled()) {
48261676Sjpk 			/* just return current zone */
48271676Sjpk 			real_nzones = domi_nzones = 1;
48281676Sjpk 			zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP);
48291769Scarlsonj 			zoneids[0] = myzone->zone_id;
48301676Sjpk 		} else {
48311676Sjpk 			/* return all zones that are dominated */
48321676Sjpk 			mutex_enter(&zonehash_lock);
48331676Sjpk 			real_nzones = zonecount;
48341676Sjpk 			domi_nzones = 0;
48351676Sjpk 			if (real_nzones > 0) {
48361676Sjpk 				zoneids = kmem_alloc(real_nzones *
48371676Sjpk 				    sizeof (zoneid_t), KM_SLEEP);
48381769Scarlsonj 				mybslab = label2bslabel(myzone->zone_slabel);
48391676Sjpk 				for (zone = list_head(&zone_active);
48401676Sjpk 				    zone != NULL;
48411676Sjpk 				    zone = list_next(&zone_active, zone)) {
48421676Sjpk 					if (zone->zone_id == GLOBAL_ZONEID)
48431676Sjpk 						continue;
48441769Scarlsonj 					if (zone != myzone &&
48451769Scarlsonj 					    (zone->zone_flags & ZF_IS_SCRATCH))
48461769Scarlsonj 						continue;
48471769Scarlsonj 					/*
48481769Scarlsonj 					 * Note that a label always dominates
48491769Scarlsonj 					 * itself, so myzone is always included
48501769Scarlsonj 					 * in the list.
48511769Scarlsonj 					 */
48521676Sjpk 					if (bldominates(mybslab,
48531676Sjpk 					    label2bslabel(zone->zone_slabel))) {
48541676Sjpk 						zoneids[domi_nzones++] =
48551676Sjpk 						    zone->zone_id;
48561676Sjpk 					}
48571676Sjpk 				}
48581676Sjpk 			}
48591676Sjpk 			mutex_exit(&zonehash_lock);
48601676Sjpk 		}
48610Sstevel@tonic-gate 	} else {
48620Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
48630Sstevel@tonic-gate 		real_nzones = zonecount;
48641676Sjpk 		domi_nzones = 0;
48651676Sjpk 		if (real_nzones > 0) {
48660Sstevel@tonic-gate 			zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t),
48670Sstevel@tonic-gate 			    KM_SLEEP);
48680Sstevel@tonic-gate 			for (zone = list_head(&zone_active); zone != NULL;
48690Sstevel@tonic-gate 			    zone = list_next(&zone_active, zone))
48701676Sjpk 				zoneids[domi_nzones++] = zone->zone_id;
48711676Sjpk 			ASSERT(domi_nzones == real_nzones);
48720Sstevel@tonic-gate 		}
48730Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
48740Sstevel@tonic-gate 	}
48750Sstevel@tonic-gate 
48761676Sjpk 	/*
48771676Sjpk 	 * If user has allocated space for fewer entries than we found, then
48781676Sjpk 	 * return only up to his limit.  Either way, tell him exactly how many
48791676Sjpk 	 * we found.
48801676Sjpk 	 */
48811676Sjpk 	if (domi_nzones < user_nzones)
48821676Sjpk 		user_nzones = domi_nzones;
48831676Sjpk 	error = 0;
48841676Sjpk 	if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) {
48850Sstevel@tonic-gate 		error = EFAULT;
48861676Sjpk 	} else if (zoneidlist != NULL && user_nzones != 0) {
48870Sstevel@tonic-gate 		if (copyout(zoneids, zoneidlist,
48880Sstevel@tonic-gate 		    user_nzones * sizeof (zoneid_t)) != 0)
48890Sstevel@tonic-gate 			error = EFAULT;
48900Sstevel@tonic-gate 	}
48910Sstevel@tonic-gate 
48921676Sjpk 	if (real_nzones > 0)
48930Sstevel@tonic-gate 		kmem_free(zoneids, real_nzones * sizeof (zoneid_t));
48940Sstevel@tonic-gate 
48951676Sjpk 	if (error != 0)
48960Sstevel@tonic-gate 		return (set_errno(error));
48970Sstevel@tonic-gate 	else
48980Sstevel@tonic-gate 		return (0);
48990Sstevel@tonic-gate }
49000Sstevel@tonic-gate 
49010Sstevel@tonic-gate /*
49020Sstevel@tonic-gate  * Systemcall entry point for zone_lookup(2).
49030Sstevel@tonic-gate  *
49041676Sjpk  * Non-global zones are only able to see themselves and (on labeled systems)
49051676Sjpk  * the zones they dominate.
49060Sstevel@tonic-gate  */
49070Sstevel@tonic-gate static zoneid_t
49080Sstevel@tonic-gate zone_lookup(const char *zone_name)
49090Sstevel@tonic-gate {
49100Sstevel@tonic-gate 	char *kname;
49110Sstevel@tonic-gate 	zone_t *zone;
49120Sstevel@tonic-gate 	zoneid_t zoneid;
49130Sstevel@tonic-gate 	int err;
49140Sstevel@tonic-gate 
49150Sstevel@tonic-gate 	if (zone_name == NULL) {
49160Sstevel@tonic-gate 		/* return caller's zone id */
49170Sstevel@tonic-gate 		return (getzoneid());
49180Sstevel@tonic-gate 	}
49190Sstevel@tonic-gate 
49200Sstevel@tonic-gate 	kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
49210Sstevel@tonic-gate 	if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) {
49220Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
49230Sstevel@tonic-gate 		return (set_errno(err));
49240Sstevel@tonic-gate 	}
49250Sstevel@tonic-gate 
49260Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
49270Sstevel@tonic-gate 	zone = zone_find_all_by_name(kname);
49280Sstevel@tonic-gate 	kmem_free(kname, ZONENAME_MAX);
49291676Sjpk 	/*
49301676Sjpk 	 * In a non-global zone, can only lookup global and own name.
49311676Sjpk 	 * In Trusted Extensions zone label dominance rules apply.
49321676Sjpk 	 */
49331676Sjpk 	if (zone == NULL ||
49341676Sjpk 	    zone_status_get(zone) < ZONE_IS_READY ||
49351676Sjpk 	    !zone_list_access(zone)) {
49360Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
49370Sstevel@tonic-gate 		return (set_errno(EINVAL));
49381676Sjpk 	} else {
49391676Sjpk 		zoneid = zone->zone_id;
49401676Sjpk 		mutex_exit(&zonehash_lock);
49411676Sjpk 		return (zoneid);
49420Sstevel@tonic-gate 	}
49430Sstevel@tonic-gate }
49440Sstevel@tonic-gate 
4945813Sdp static int
4946813Sdp zone_version(int *version_arg)
4947813Sdp {
4948813Sdp 	int version = ZONE_SYSCALL_API_VERSION;
4949813Sdp 
4950813Sdp 	if (copyout(&version, version_arg, sizeof (int)) != 0)
4951813Sdp 		return (set_errno(EFAULT));
4952813Sdp 	return (0);
4953813Sdp }
4954813Sdp 
49550Sstevel@tonic-gate /* ARGSUSED */
49560Sstevel@tonic-gate long
4957789Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4)
49580Sstevel@tonic-gate {
49590Sstevel@tonic-gate 	zone_def zs;
49600Sstevel@tonic-gate 
49610Sstevel@tonic-gate 	switch (cmd) {
49620Sstevel@tonic-gate 	case ZONE_CREATE:
49630Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
49640Sstevel@tonic-gate 			if (copyin(arg1, &zs, sizeof (zone_def))) {
49650Sstevel@tonic-gate 				return (set_errno(EFAULT));
49660Sstevel@tonic-gate 			}
49670Sstevel@tonic-gate 		} else {
49680Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
49690Sstevel@tonic-gate 			zone_def32 zs32;
49700Sstevel@tonic-gate 
49710Sstevel@tonic-gate 			if (copyin(arg1, &zs32, sizeof (zone_def32))) {
49720Sstevel@tonic-gate 				return (set_errno(EFAULT));
49730Sstevel@tonic-gate 			}
49740Sstevel@tonic-gate 			zs.zone_name =
49750Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_name;
49760Sstevel@tonic-gate 			zs.zone_root =
49770Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_root;
49780Sstevel@tonic-gate 			zs.zone_privs =
49790Sstevel@tonic-gate 			    (const struct priv_set *)
49800Sstevel@tonic-gate 			    (unsigned long)zs32.zone_privs;
49811409Sdp 			zs.zone_privssz = zs32.zone_privssz;
49820Sstevel@tonic-gate 			zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf;
49830Sstevel@tonic-gate 			zs.rctlbufsz = zs32.rctlbufsz;
4984789Sahrens 			zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf;
4985789Sahrens 			zs.zfsbufsz = zs32.zfsbufsz;
49860Sstevel@tonic-gate 			zs.extended_error =
49870Sstevel@tonic-gate 			    (int *)(unsigned long)zs32.extended_error;
49881676Sjpk 			zs.match = zs32.match;
49891676Sjpk 			zs.doi = zs32.doi;
49901676Sjpk 			zs.label = (const bslabel_t *)(uintptr_t)zs32.label;
49913448Sdh155122 			zs.flags = zs32.flags;
49920Sstevel@tonic-gate #else
49930Sstevel@tonic-gate 			panic("get_udatamodel() returned bogus result\n");
49940Sstevel@tonic-gate #endif
49950Sstevel@tonic-gate 		}
49960Sstevel@tonic-gate 
49970Sstevel@tonic-gate 		return (zone_create(zs.zone_name, zs.zone_root,
4998813Sdp 		    zs.zone_privs, zs.zone_privssz,
4999813Sdp 		    (caddr_t)zs.rctlbuf, zs.rctlbufsz,
5000813Sdp 		    (caddr_t)zs.zfsbuf, zs.zfsbufsz,
50011676Sjpk 		    zs.extended_error, zs.match, zs.doi,
50023448Sdh155122 		    zs.label, zs.flags));
50030Sstevel@tonic-gate 	case ZONE_BOOT:
50042267Sdp 		return (zone_boot((zoneid_t)(uintptr_t)arg1));
50050Sstevel@tonic-gate 	case ZONE_DESTROY:
50060Sstevel@tonic-gate 		return (zone_destroy((zoneid_t)(uintptr_t)arg1));
50070Sstevel@tonic-gate 	case ZONE_GETATTR:
50080Sstevel@tonic-gate 		return (zone_getattr((zoneid_t)(uintptr_t)arg1,
50090Sstevel@tonic-gate 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
50102267Sdp 	case ZONE_SETATTR:
50112267Sdp 		return (zone_setattr((zoneid_t)(uintptr_t)arg1,
50122267Sdp 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
50130Sstevel@tonic-gate 	case ZONE_ENTER:
50140Sstevel@tonic-gate 		return (zone_enter((zoneid_t)(uintptr_t)arg1));
50150Sstevel@tonic-gate 	case ZONE_LIST:
50160Sstevel@tonic-gate 		return (zone_list((zoneid_t *)arg1, (uint_t *)arg2));
50170Sstevel@tonic-gate 	case ZONE_SHUTDOWN:
50180Sstevel@tonic-gate 		return (zone_shutdown((zoneid_t)(uintptr_t)arg1));
50190Sstevel@tonic-gate 	case ZONE_LOOKUP:
50200Sstevel@tonic-gate 		return (zone_lookup((const char *)arg1));
5021813Sdp 	case ZONE_VERSION:
5022813Sdp 		return (zone_version((int *)arg1));
50233448Sdh155122 	case ZONE_ADD_DATALINK:
50243448Sdh155122 		return (zone_add_datalink((zoneid_t)(uintptr_t)arg1,
50253448Sdh155122 		    (char *)arg2));
50263448Sdh155122 	case ZONE_DEL_DATALINK:
50273448Sdh155122 		return (zone_remove_datalink((zoneid_t)(uintptr_t)arg1,
50283448Sdh155122 		    (char *)arg2));
50293448Sdh155122 	case ZONE_CHECK_DATALINK:
50303448Sdh155122 		return (zone_check_datalink((zoneid_t *)arg1, (char *)arg2));
50313448Sdh155122 	case ZONE_LIST_DATALINK:
50323448Sdh155122 		return (zone_list_datalink((zoneid_t)(uintptr_t)arg1,
50333448Sdh155122 		    (int *)arg2, (char *)arg3));
50340Sstevel@tonic-gate 	default:
50350Sstevel@tonic-gate 		return (set_errno(EINVAL));
50360Sstevel@tonic-gate 	}
50370Sstevel@tonic-gate }
50380Sstevel@tonic-gate 
50390Sstevel@tonic-gate struct zarg {
50400Sstevel@tonic-gate 	zone_t *zone;
50410Sstevel@tonic-gate 	zone_cmd_arg_t arg;
50420Sstevel@tonic-gate };
50430Sstevel@tonic-gate 
50440Sstevel@tonic-gate static int
50450Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp)
50460Sstevel@tonic-gate {
50470Sstevel@tonic-gate 	char *buf;
50480Sstevel@tonic-gate 	size_t buflen;
50490Sstevel@tonic-gate 	int error;
50500Sstevel@tonic-gate 
50510Sstevel@tonic-gate 	buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name);
50520Sstevel@tonic-gate 	buf = kmem_alloc(buflen, KM_SLEEP);
50530Sstevel@tonic-gate 	(void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name);
50540Sstevel@tonic-gate 	error = door_ki_open(buf, doorp);
50550Sstevel@tonic-gate 	kmem_free(buf, buflen);
50560Sstevel@tonic-gate 	return (error);
50570Sstevel@tonic-gate }
50580Sstevel@tonic-gate 
50590Sstevel@tonic-gate static void
50600Sstevel@tonic-gate zone_release_door(door_handle_t *doorp)
50610Sstevel@tonic-gate {
50620Sstevel@tonic-gate 	door_ki_rele(*doorp);
50630Sstevel@tonic-gate 	*doorp = NULL;
50640Sstevel@tonic-gate }
50650Sstevel@tonic-gate 
50660Sstevel@tonic-gate static void
50670Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp)
50680Sstevel@tonic-gate {
50690Sstevel@tonic-gate 	door_handle_t door = NULL;
50700Sstevel@tonic-gate 	door_arg_t darg, save_arg;
50710Sstevel@tonic-gate 	char *zone_name;
50720Sstevel@tonic-gate 	size_t zone_namelen;
50730Sstevel@tonic-gate 	zoneid_t zoneid;
50740Sstevel@tonic-gate 	zone_t *zone;
50750Sstevel@tonic-gate 	zone_cmd_arg_t arg;
50760Sstevel@tonic-gate 	uint64_t uniqid;
50770Sstevel@tonic-gate 	size_t size;
50780Sstevel@tonic-gate 	int error;
50790Sstevel@tonic-gate 	int retry;
50800Sstevel@tonic-gate 
50810Sstevel@tonic-gate 	zone = zargp->zone;
50820Sstevel@tonic-gate 	arg = zargp->arg;
50830Sstevel@tonic-gate 	kmem_free(zargp, sizeof (*zargp));
50840Sstevel@tonic-gate 
50850Sstevel@tonic-gate 	zone_namelen = strlen(zone->zone_name) + 1;
50860Sstevel@tonic-gate 	zone_name = kmem_alloc(zone_namelen, KM_SLEEP);
50870Sstevel@tonic-gate 	bcopy(zone->zone_name, zone_name, zone_namelen);
50880Sstevel@tonic-gate 	zoneid = zone->zone_id;
50890Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
50900Sstevel@tonic-gate 	/*
50910Sstevel@tonic-gate 	 * zoneadmd may be down, but at least we can empty out the zone.
50920Sstevel@tonic-gate 	 * We can ignore the return value of zone_empty() since we're called
50930Sstevel@tonic-gate 	 * from a kernel thread and know we won't be delivered any signals.
50940Sstevel@tonic-gate 	 */
50950Sstevel@tonic-gate 	ASSERT(curproc == &p0);
50960Sstevel@tonic-gate 	(void) zone_empty(zone);
50970Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY);
50980Sstevel@tonic-gate 	zone_rele(zone);
50990Sstevel@tonic-gate 
51000Sstevel@tonic-gate 	size = sizeof (arg);
51010Sstevel@tonic-gate 	darg.rbuf = (char *)&arg;
51020Sstevel@tonic-gate 	darg.data_ptr = (char *)&arg;
51030Sstevel@tonic-gate 	darg.rsize = size;
51040Sstevel@tonic-gate 	darg.data_size = size;
51050Sstevel@tonic-gate 	darg.desc_ptr = NULL;
51060Sstevel@tonic-gate 	darg.desc_num = 0;
51070Sstevel@tonic-gate 
51080Sstevel@tonic-gate 	save_arg = darg;
51090Sstevel@tonic-gate 	/*
51100Sstevel@tonic-gate 	 * Since we're not holding a reference to the zone, any number of
51110Sstevel@tonic-gate 	 * things can go wrong, including the zone disappearing before we get a
51120Sstevel@tonic-gate 	 * chance to talk to zoneadmd.
51130Sstevel@tonic-gate 	 */
51140Sstevel@tonic-gate 	for (retry = 0; /* forever */; retry++) {
51150Sstevel@tonic-gate 		if (door == NULL &&
51160Sstevel@tonic-gate 		    (error = zone_lookup_door(zone_name, &door)) != 0) {
51170Sstevel@tonic-gate 			goto next;
51180Sstevel@tonic-gate 		}
51190Sstevel@tonic-gate 		ASSERT(door != NULL);
51200Sstevel@tonic-gate 
51210Sstevel@tonic-gate 		if ((error = door_ki_upcall(door, &darg)) == 0) {
51220Sstevel@tonic-gate 			break;
51230Sstevel@tonic-gate 		}
51240Sstevel@tonic-gate 		switch (error) {
51250Sstevel@tonic-gate 		case EINTR:
51260Sstevel@tonic-gate 			/* FALLTHROUGH */
51270Sstevel@tonic-gate 		case EAGAIN:	/* process may be forking */
51280Sstevel@tonic-gate 			/*
51290Sstevel@tonic-gate 			 * Back off for a bit
51300Sstevel@tonic-gate 			 */
51310Sstevel@tonic-gate 			break;
51320Sstevel@tonic-gate 		case EBADF:
51330Sstevel@tonic-gate 			zone_release_door(&door);
51340Sstevel@tonic-gate 			if (zone_lookup_door(zone_name, &door) != 0) {
51350Sstevel@tonic-gate 				/*
51360Sstevel@tonic-gate 				 * zoneadmd may be dead, but it may come back to
51370Sstevel@tonic-gate 				 * life later.
51380Sstevel@tonic-gate 				 */
51390Sstevel@tonic-gate 				break;
51400Sstevel@tonic-gate 			}
51410Sstevel@tonic-gate 			break;
51420Sstevel@tonic-gate 		default:
51430Sstevel@tonic-gate 			cmn_err(CE_WARN,
51440Sstevel@tonic-gate 			    "zone_ki_call_zoneadmd: door_ki_upcall error %d\n",
51450Sstevel@tonic-gate 			    error);
51460Sstevel@tonic-gate 			goto out;
51470Sstevel@tonic-gate 		}
51480Sstevel@tonic-gate next:
51490Sstevel@tonic-gate 		/*
51500Sstevel@tonic-gate 		 * If this isn't the same zone_t that we originally had in mind,
51510Sstevel@tonic-gate 		 * then this is the same as if two kadmin requests come in at
51520Sstevel@tonic-gate 		 * the same time: the first one wins.  This means we lose, so we
51530Sstevel@tonic-gate 		 * bail.
51540Sstevel@tonic-gate 		 */
51550Sstevel@tonic-gate 		if ((zone = zone_find_by_id(zoneid)) == NULL) {
51560Sstevel@tonic-gate 			/*
51570Sstevel@tonic-gate 			 * Problem is solved.
51580Sstevel@tonic-gate 			 */
51590Sstevel@tonic-gate 			break;
51600Sstevel@tonic-gate 		}
51610Sstevel@tonic-gate 		if (zone->zone_uniqid != uniqid) {
51620Sstevel@tonic-gate 			/*
51630Sstevel@tonic-gate 			 * zoneid recycled
51640Sstevel@tonic-gate 			 */
51650Sstevel@tonic-gate 			zone_rele(zone);
51660Sstevel@tonic-gate 			break;
51670Sstevel@tonic-gate 		}
51680Sstevel@tonic-gate 		/*
51690Sstevel@tonic-gate 		 * We could zone_status_timedwait(), but there doesn't seem to
51700Sstevel@tonic-gate 		 * be much point in doing that (plus, it would mean that
51710Sstevel@tonic-gate 		 * zone_free() isn't called until this thread exits).
51720Sstevel@tonic-gate 		 */
51730Sstevel@tonic-gate 		zone_rele(zone);
51740Sstevel@tonic-gate 		delay(hz);
51750Sstevel@tonic-gate 		darg = save_arg;
51760Sstevel@tonic-gate 	}
51770Sstevel@tonic-gate out:
51780Sstevel@tonic-gate 	if (door != NULL) {
51790Sstevel@tonic-gate 		zone_release_door(&door);
51800Sstevel@tonic-gate 	}
51810Sstevel@tonic-gate 	kmem_free(zone_name, zone_namelen);
51820Sstevel@tonic-gate 	thread_exit();
51830Sstevel@tonic-gate }
51840Sstevel@tonic-gate 
51850Sstevel@tonic-gate /*
51862267Sdp  * Entry point for uadmin() to tell the zone to go away or reboot.  Analog to
51872267Sdp  * kadmin().  The caller is a process in the zone.
51880Sstevel@tonic-gate  *
51890Sstevel@tonic-gate  * In order to shutdown the zone, we will hand off control to zoneadmd
51900Sstevel@tonic-gate  * (running in the global zone) via a door.  We do a half-hearted job at
51910Sstevel@tonic-gate  * killing all processes in the zone, create a kernel thread to contact
51920Sstevel@tonic-gate  * zoneadmd, and make note of the "uniqid" of the zone.  The uniqid is
51930Sstevel@tonic-gate  * a form of generation number used to let zoneadmd (as well as
51940Sstevel@tonic-gate  * zone_destroy()) know exactly which zone they're re talking about.
51950Sstevel@tonic-gate  */
51960Sstevel@tonic-gate int
51972267Sdp zone_kadmin(int cmd, int fcn, const char *mdep, cred_t *credp)
51980Sstevel@tonic-gate {
51990Sstevel@tonic-gate 	struct zarg *zargp;
52000Sstevel@tonic-gate 	zone_cmd_t zcmd;
52010Sstevel@tonic-gate 	zone_t *zone;
52020Sstevel@tonic-gate 
52030Sstevel@tonic-gate 	zone = curproc->p_zone;
52040Sstevel@tonic-gate 	ASSERT(getzoneid() != GLOBAL_ZONEID);
52050Sstevel@tonic-gate 
52060Sstevel@tonic-gate 	switch (cmd) {
52070Sstevel@tonic-gate 	case A_SHUTDOWN:
52080Sstevel@tonic-gate 		switch (fcn) {
52090Sstevel@tonic-gate 		case AD_HALT:
52100Sstevel@tonic-gate 		case AD_POWEROFF:
52110Sstevel@tonic-gate 			zcmd = Z_HALT;
52120Sstevel@tonic-gate 			break;
52130Sstevel@tonic-gate 		case AD_BOOT:
52140Sstevel@tonic-gate 			zcmd = Z_REBOOT;
52150Sstevel@tonic-gate 			break;
52160Sstevel@tonic-gate 		case AD_IBOOT:
52170Sstevel@tonic-gate 		case AD_SBOOT:
52180Sstevel@tonic-gate 		case AD_SIBOOT:
52190Sstevel@tonic-gate 		case AD_NOSYNC:
52200Sstevel@tonic-gate 			return (ENOTSUP);
52210Sstevel@tonic-gate 		default:
52220Sstevel@tonic-gate 			return (EINVAL);
52230Sstevel@tonic-gate 		}
52240Sstevel@tonic-gate 		break;
52250Sstevel@tonic-gate 	case A_REBOOT:
52260Sstevel@tonic-gate 		zcmd = Z_REBOOT;
52270Sstevel@tonic-gate 		break;
52280Sstevel@tonic-gate 	case A_FTRACE:
52290Sstevel@tonic-gate 	case A_REMOUNT:
52300Sstevel@tonic-gate 	case A_FREEZE:
52310Sstevel@tonic-gate 	case A_DUMP:
52320Sstevel@tonic-gate 		return (ENOTSUP);
52330Sstevel@tonic-gate 	default:
52340Sstevel@tonic-gate 		ASSERT(cmd != A_SWAPCTL);	/* handled by uadmin() */
52350Sstevel@tonic-gate 		return (EINVAL);
52360Sstevel@tonic-gate 	}
52370Sstevel@tonic-gate 
52380Sstevel@tonic-gate 	if (secpolicy_zone_admin(credp, B_FALSE))
52390Sstevel@tonic-gate 		return (EPERM);
52400Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
52412267Sdp 
52420Sstevel@tonic-gate 	/*
52430Sstevel@tonic-gate 	 * zone_status can't be ZONE_IS_EMPTY or higher since curproc
52440Sstevel@tonic-gate 	 * is in the zone.
52450Sstevel@tonic-gate 	 */
52460Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY);
52470Sstevel@tonic-gate 	if (zone_status_get(zone) > ZONE_IS_RUNNING) {
52480Sstevel@tonic-gate 		/*
52490Sstevel@tonic-gate 		 * This zone is already on its way down.
52500Sstevel@tonic-gate 		 */
52510Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
52520Sstevel@tonic-gate 		return (0);
52530Sstevel@tonic-gate 	}
52540Sstevel@tonic-gate 	/*
52550Sstevel@tonic-gate 	 * Prevent future zone_enter()s
52560Sstevel@tonic-gate 	 */
52570Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
52580Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
52590Sstevel@tonic-gate 
52600Sstevel@tonic-gate 	/*
52610Sstevel@tonic-gate 	 * Kill everyone now and call zoneadmd later.
52620Sstevel@tonic-gate 	 * zone_ki_call_zoneadmd() will do a more thorough job of this
52630Sstevel@tonic-gate 	 * later.
52640Sstevel@tonic-gate 	 */
52650Sstevel@tonic-gate 	killall(zone->zone_id);
52660Sstevel@tonic-gate 	/*
52670Sstevel@tonic-gate 	 * Now, create the thread to contact zoneadmd and do the rest of the
52680Sstevel@tonic-gate 	 * work.  This thread can't be created in our zone otherwise
52690Sstevel@tonic-gate 	 * zone_destroy() would deadlock.
52700Sstevel@tonic-gate 	 */
52712267Sdp 	zargp = kmem_zalloc(sizeof (*zargp), KM_SLEEP);
52720Sstevel@tonic-gate 	zargp->arg.cmd = zcmd;
52730Sstevel@tonic-gate 	zargp->arg.uniqid = zone->zone_uniqid;
52742267Sdp 	zargp->zone = zone;
52750Sstevel@tonic-gate 	(void) strcpy(zargp->arg.locale, "C");
52762267Sdp 	/* mdep was already copied in for us by uadmin */
52772267Sdp 	if (mdep != NULL)
52782267Sdp 		(void) strlcpy(zargp->arg.bootbuf, mdep,
52792267Sdp 		    sizeof (zargp->arg.bootbuf));
52802267Sdp 	zone_hold(zone);
52810Sstevel@tonic-gate 
52820Sstevel@tonic-gate 	(void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0,
52830Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
52840Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
52850Sstevel@tonic-gate 
52860Sstevel@tonic-gate 	return (EINVAL);
52870Sstevel@tonic-gate }
52880Sstevel@tonic-gate 
52890Sstevel@tonic-gate /*
52900Sstevel@tonic-gate  * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's
52910Sstevel@tonic-gate  * status to ZONE_IS_SHUTTING_DOWN.
52920Sstevel@tonic-gate  */
52930Sstevel@tonic-gate void
52940Sstevel@tonic-gate zone_shutdown_global(void)
52950Sstevel@tonic-gate {
52960Sstevel@tonic-gate 	ASSERT(curproc->p_zone == global_zone);
52970Sstevel@tonic-gate 
52980Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
52990Sstevel@tonic-gate 	ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING);
53000Sstevel@tonic-gate 	zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN);
53010Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
53020Sstevel@tonic-gate }
5303789Sahrens 
5304789Sahrens /*
5305789Sahrens  * Returns true if the named dataset is visible in the current zone.
5306789Sahrens  * The 'write' parameter is set to 1 if the dataset is also writable.
5307789Sahrens  */
5308789Sahrens int
5309789Sahrens zone_dataset_visible(const char *dataset, int *write)
5310789Sahrens {
5311789Sahrens 	zone_dataset_t *zd;
5312789Sahrens 	size_t len;
5313789Sahrens 	zone_t *zone = curproc->p_zone;
5314789Sahrens 
5315789Sahrens 	if (dataset[0] == '\0')
5316789Sahrens 		return (0);
5317789Sahrens 
5318789Sahrens 	/*
5319789Sahrens 	 * Walk the list once, looking for datasets which match exactly, or
5320789Sahrens 	 * specify a dataset underneath an exported dataset.  If found, return
5321789Sahrens 	 * true and note that it is writable.
5322789Sahrens 	 */
5323789Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
5324789Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
5325789Sahrens 
5326789Sahrens 		len = strlen(zd->zd_dataset);
5327789Sahrens 		if (strlen(dataset) >= len &&
5328789Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
5329816Smaybee 		    (dataset[len] == '\0' || dataset[len] == '/' ||
5330816Smaybee 		    dataset[len] == '@')) {
5331789Sahrens 			if (write)
5332789Sahrens 				*write = 1;
5333789Sahrens 			return (1);
5334789Sahrens 		}
5335789Sahrens 	}
5336789Sahrens 
5337789Sahrens 	/*
5338789Sahrens 	 * Walk the list a second time, searching for datasets which are parents
5339789Sahrens 	 * of exported datasets.  These should be visible, but read-only.
5340789Sahrens 	 *
5341789Sahrens 	 * Note that we also have to support forms such as 'pool/dataset/', with
5342789Sahrens 	 * a trailing slash.
5343789Sahrens 	 */
5344789Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
5345789Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
5346789Sahrens 
5347789Sahrens 		len = strlen(dataset);
5348789Sahrens 		if (dataset[len - 1] == '/')
5349789Sahrens 			len--;	/* Ignore trailing slash */
5350789Sahrens 		if (len < strlen(zd->zd_dataset) &&
5351789Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
5352789Sahrens 		    zd->zd_dataset[len] == '/') {
5353789Sahrens 			if (write)
5354789Sahrens 				*write = 0;
5355789Sahrens 			return (1);
5356789Sahrens 		}
5357789Sahrens 	}
5358789Sahrens 
5359789Sahrens 	return (0);
5360789Sahrens }
53611676Sjpk 
53621676Sjpk /*
53631676Sjpk  * zone_find_by_any_path() -
53641676Sjpk  *
53651676Sjpk  * kernel-private routine similar to zone_find_by_path(), but which
53661676Sjpk  * effectively compares against zone paths rather than zonerootpath
53671676Sjpk  * (i.e., the last component of zonerootpaths, which should be "root/",
53681676Sjpk  * are not compared.)  This is done in order to accurately identify all
53691676Sjpk  * paths, whether zone-visible or not, including those which are parallel
53701676Sjpk  * to /root/, such as /dev/, /home/, etc...
53711676Sjpk  *
53721676Sjpk  * If the specified path does not fall under any zone path then global
53731676Sjpk  * zone is returned.
53741676Sjpk  *
53751676Sjpk  * The treat_abs parameter indicates whether the path should be treated as
53761676Sjpk  * an absolute path although it does not begin with "/".  (This supports
53771676Sjpk  * nfs mount syntax such as host:any/path.)
53781676Sjpk  *
53791676Sjpk  * The caller is responsible for zone_rele of the returned zone.
53801676Sjpk  */
53811676Sjpk zone_t *
53821676Sjpk zone_find_by_any_path(const char *path, boolean_t treat_abs)
53831676Sjpk {
53841676Sjpk 	zone_t *zone;
53851676Sjpk 	int path_offset = 0;
53861676Sjpk 
53871676Sjpk 	if (path == NULL) {
53881676Sjpk 		zone_hold(global_zone);
53891676Sjpk 		return (global_zone);
53901676Sjpk 	}
53911676Sjpk 
53921676Sjpk 	if (*path != '/') {
53931676Sjpk 		ASSERT(treat_abs);
53941676Sjpk 		path_offset = 1;
53951676Sjpk 	}
53961676Sjpk 
53971676Sjpk 	mutex_enter(&zonehash_lock);
53981676Sjpk 	for (zone = list_head(&zone_active); zone != NULL;
53991676Sjpk 	    zone = list_next(&zone_active, zone)) {
54001676Sjpk 		char	*c;
54011676Sjpk 		size_t	pathlen;
54021876Smp46848 		char *rootpath_start;
54031676Sjpk 
54041676Sjpk 		if (zone == global_zone)	/* skip global zone */
54051676Sjpk 			continue;
54061676Sjpk 
54071676Sjpk 		/* scan backwards to find start of last component */
54081676Sjpk 		c = zone->zone_rootpath + zone->zone_rootpathlen - 2;
54091676Sjpk 		do {
54101676Sjpk 			c--;
54111676Sjpk 		} while (*c != '/');
54121676Sjpk 
54131876Smp46848 		pathlen = c - zone->zone_rootpath + 1 - path_offset;
54141876Smp46848 		rootpath_start = (zone->zone_rootpath + path_offset);
54151876Smp46848 		if (strncmp(path, rootpath_start, pathlen) == 0)
54161676Sjpk 			break;
54171676Sjpk 	}
54181676Sjpk 	if (zone == NULL)
54191676Sjpk 		zone = global_zone;
54201676Sjpk 	zone_hold(zone);
54211676Sjpk 	mutex_exit(&zonehash_lock);
54221676Sjpk 	return (zone);
54231676Sjpk }
54243448Sdh155122 
54253448Sdh155122 /* List of data link names which are accessible from the zone */
54263448Sdh155122 struct dlnamelist {
54273448Sdh155122 	char			dlnl_name[LIFNAMSIZ];
54283448Sdh155122 	struct dlnamelist	*dlnl_next;
54293448Sdh155122 };
54303448Sdh155122 
54313448Sdh155122 
54323448Sdh155122 /*
54333448Sdh155122  * Check whether the datalink name (dlname) itself is present.
54343448Sdh155122  * Return true if found.
54353448Sdh155122  */
54363448Sdh155122 static boolean_t
54373448Sdh155122 zone_dlname(zone_t *zone, char *dlname)
54383448Sdh155122 {
54393448Sdh155122 	struct dlnamelist *dlnl;
54403448Sdh155122 	boolean_t found = B_FALSE;
54413448Sdh155122 
54423448Sdh155122 	mutex_enter(&zone->zone_lock);
54433448Sdh155122 	for (dlnl = zone->zone_dl_list; dlnl != NULL; dlnl = dlnl->dlnl_next) {
54443448Sdh155122 		if (strncmp(dlnl->dlnl_name, dlname, LIFNAMSIZ) == 0) {
54453448Sdh155122 			found = B_TRUE;
54463448Sdh155122 			break;
54473448Sdh155122 		}
54483448Sdh155122 	}
54493448Sdh155122 	mutex_exit(&zone->zone_lock);
54503448Sdh155122 	return (found);
54513448Sdh155122 }
54523448Sdh155122 
54533448Sdh155122 /*
54543448Sdh155122  * Add an data link name for the zone. Does not check for duplicates.
54553448Sdh155122  */
54563448Sdh155122 static int
54573448Sdh155122 zone_add_datalink(zoneid_t zoneid, char *dlname)
54583448Sdh155122 {
54593448Sdh155122 	struct dlnamelist *dlnl;
54603448Sdh155122 	zone_t *zone;
54613448Sdh155122 	zone_t *thiszone;
54623448Sdh155122 	int err;
54633448Sdh155122 
54643448Sdh155122 	dlnl = kmem_zalloc(sizeof (struct dlnamelist), KM_SLEEP);
54653448Sdh155122 	if ((err = copyinstr(dlname, dlnl->dlnl_name, LIFNAMSIZ, NULL)) != 0) {
54663448Sdh155122 		kmem_free(dlnl, sizeof (struct dlnamelist));
54673448Sdh155122 		return (set_errno(err));
54683448Sdh155122 	}
54693448Sdh155122 
54703448Sdh155122 	thiszone = zone_find_by_id(zoneid);
54713448Sdh155122 	if (thiszone == NULL) {
54723448Sdh155122 		kmem_free(dlnl, sizeof (struct dlnamelist));
54733448Sdh155122 		return (set_errno(ENXIO));
54743448Sdh155122 	}
54753448Sdh155122 
54763448Sdh155122 	/*
54773448Sdh155122 	 * Verify that the datalink name isn't already used by a different
54783448Sdh155122 	 * zone while allowing duplicate entries for the same zone (e.g. due
54793448Sdh155122 	 * to both using IPv4 and IPv6 on an interface)
54803448Sdh155122 	 */
54813448Sdh155122 	mutex_enter(&zonehash_lock);
54823448Sdh155122 	for (zone = list_head(&zone_active); zone != NULL;
54833448Sdh155122 	    zone = list_next(&zone_active, zone)) {
54843448Sdh155122 		if (zone->zone_id == zoneid)
54853448Sdh155122 			continue;
54863448Sdh155122 
54873448Sdh155122 		if (zone_dlname(zone, dlnl->dlnl_name)) {
54883448Sdh155122 			mutex_exit(&zonehash_lock);
54893448Sdh155122 			zone_rele(thiszone);
54903448Sdh155122 			kmem_free(dlnl, sizeof (struct dlnamelist));
54913448Sdh155122 			return (set_errno(EPERM));
54923448Sdh155122 		}
54933448Sdh155122 	}
54943448Sdh155122 	mutex_enter(&thiszone->zone_lock);
54953448Sdh155122 	dlnl->dlnl_next = thiszone->zone_dl_list;
54963448Sdh155122 	thiszone->zone_dl_list = dlnl;
54973448Sdh155122 	mutex_exit(&thiszone->zone_lock);
54983448Sdh155122 	mutex_exit(&zonehash_lock);
54993448Sdh155122 	zone_rele(thiszone);
55003448Sdh155122 	return (0);
55013448Sdh155122 }
55023448Sdh155122 
55033448Sdh155122 static int
55043448Sdh155122 zone_remove_datalink(zoneid_t zoneid, char *dlname)
55053448Sdh155122 {
55063448Sdh155122 	struct dlnamelist *dlnl, *odlnl, **dlnlp;
55073448Sdh155122 	zone_t *zone;
55083448Sdh155122 	int err;
55093448Sdh155122 
55103448Sdh155122 	dlnl = kmem_zalloc(sizeof (struct dlnamelist), KM_SLEEP);
55113448Sdh155122 	if ((err = copyinstr(dlname, dlnl->dlnl_name, LIFNAMSIZ, NULL)) != 0) {
55123448Sdh155122 		kmem_free(dlnl, sizeof (struct dlnamelist));
55133448Sdh155122 		return (set_errno(err));
55143448Sdh155122 	}
55153448Sdh155122 	zone = zone_find_by_id(zoneid);
55163448Sdh155122 	if (zone == NULL) {
55173448Sdh155122 		kmem_free(dlnl, sizeof (struct dlnamelist));
55183448Sdh155122 		return (set_errno(EINVAL));
55193448Sdh155122 	}
55203448Sdh155122 
55213448Sdh155122 	mutex_enter(&zone->zone_lock);
55223448Sdh155122 	/* Look for match */
55233448Sdh155122 	dlnlp = &zone->zone_dl_list;
55243448Sdh155122 	while (*dlnlp != NULL) {
55253448Sdh155122 		if (strncmp(dlnl->dlnl_name, (*dlnlp)->dlnl_name,
55263448Sdh155122 		    LIFNAMSIZ) == 0)
55273448Sdh155122 			goto found;
55283448Sdh155122 		dlnlp = &((*dlnlp)->dlnl_next);
55293448Sdh155122 	}
55303448Sdh155122 	mutex_exit(&zone->zone_lock);
55313448Sdh155122 	zone_rele(zone);
55323448Sdh155122 	kmem_free(dlnl, sizeof (struct dlnamelist));
55333448Sdh155122 	return (set_errno(ENXIO));
55343448Sdh155122 
55353448Sdh155122 found:
55363448Sdh155122 	odlnl = *dlnlp;
55373448Sdh155122 	*dlnlp = (*dlnlp)->dlnl_next;
55383448Sdh155122 	kmem_free(odlnl, sizeof (struct dlnamelist));
55393448Sdh155122 
55403448Sdh155122 	mutex_exit(&zone->zone_lock);
55413448Sdh155122 	zone_rele(zone);
55423448Sdh155122 	kmem_free(dlnl, sizeof (struct dlnamelist));
55433448Sdh155122 	return (0);
55443448Sdh155122 }
55453448Sdh155122 
55463448Sdh155122 /*
55473448Sdh155122  * Using the zoneidp as ALL_ZONES, we can lookup which zone is using datalink
55483448Sdh155122  * name (dlname); otherwise we just check if the specified zoneidp has access
55493448Sdh155122  * to the datalink name.
55503448Sdh155122  */
55513448Sdh155122 static int
55523448Sdh155122 zone_check_datalink(zoneid_t *zoneidp, char *dlname)
55533448Sdh155122 {
55543448Sdh155122 	zoneid_t id;
55553448Sdh155122 	char *dln;
55563448Sdh155122 	zone_t *zone;
55573448Sdh155122 	int err = 0;
55583448Sdh155122 	boolean_t allzones = B_FALSE;
55593448Sdh155122 
55603448Sdh155122 	if (copyin(zoneidp, &id, sizeof (id)) != 0) {
55613448Sdh155122 		return (set_errno(EFAULT));
55623448Sdh155122 	}
55633448Sdh155122 	dln = kmem_zalloc(LIFNAMSIZ, KM_SLEEP);
55643448Sdh155122 	if ((err = copyinstr(dlname, dln, LIFNAMSIZ, NULL)) != 0) {
55653448Sdh155122 		kmem_free(dln, LIFNAMSIZ);
55663448Sdh155122 		return (set_errno(err));
55673448Sdh155122 	}
55683448Sdh155122 
55693448Sdh155122 	if (id == ALL_ZONES)
55703448Sdh155122 		allzones = B_TRUE;
55713448Sdh155122 
55723448Sdh155122 	/*
55733448Sdh155122 	 * Check whether datalink name is already used.
55743448Sdh155122 	 */
55753448Sdh155122 	mutex_enter(&zonehash_lock);
55763448Sdh155122 	for (zone = list_head(&zone_active); zone != NULL;
55773448Sdh155122 	    zone = list_next(&zone_active, zone)) {
55783448Sdh155122 		if (allzones || (id == zone->zone_id)) {
55793448Sdh155122 			if (!zone_dlname(zone, dln))
55803448Sdh155122 				continue;
55813448Sdh155122 			if (allzones)
55823448Sdh155122 				err = copyout(&zone->zone_id, zoneidp,
55833448Sdh155122 				    sizeof (*zoneidp));
55843448Sdh155122 
55853448Sdh155122 			mutex_exit(&zonehash_lock);
55863448Sdh155122 			kmem_free(dln, LIFNAMSIZ);
55873448Sdh155122 			return (err ? set_errno(EFAULT) : 0);
55883448Sdh155122 		}
55893448Sdh155122 	}
55903448Sdh155122 
55913448Sdh155122 	/* datalink name is not found in any active zone. */
55923448Sdh155122 	mutex_exit(&zonehash_lock);
55933448Sdh155122 	kmem_free(dln, LIFNAMSIZ);
55943448Sdh155122 	return (set_errno(ENXIO));
55953448Sdh155122 }
55963448Sdh155122 
55973448Sdh155122 /*
55983448Sdh155122  * Get the names of the datalinks assigned to a zone.
55993448Sdh155122  * Here *nump is the number of datalinks, and the assumption
56003448Sdh155122  * is that the caller will gurantee that the the supplied buffer is
56013448Sdh155122  * big enough to hold at least #*nump datalink names, that is,
56023448Sdh155122  * LIFNAMSIZ X *nump
56033448Sdh155122  * On return, *nump will be the "new" number of datalinks, if it
56043448Sdh155122  * ever changed.
56053448Sdh155122  */
56063448Sdh155122 static int
56073448Sdh155122 zone_list_datalink(zoneid_t zoneid, int *nump, char *buf)
56083448Sdh155122 {
56093448Sdh155122 	int num, dlcount;
56103448Sdh155122 	zone_t *zone;
56113448Sdh155122 	struct dlnamelist *dlnl;
56123448Sdh155122 	char *ptr;
56133448Sdh155122 
56143448Sdh155122 	if (copyin(nump, &dlcount, sizeof (dlcount)) != 0)
56153448Sdh155122 		return (set_errno(EFAULT));
56163448Sdh155122 
56173448Sdh155122 	zone = zone_find_by_id(zoneid);
56183448Sdh155122 	if (zone == NULL) {
56193448Sdh155122 		return (set_errno(ENXIO));
56203448Sdh155122 	}
56213448Sdh155122 
56223448Sdh155122 	num = 0;
56233448Sdh155122 	mutex_enter(&zone->zone_lock);
56243448Sdh155122 	ptr = buf;
56253448Sdh155122 	for (dlnl = zone->zone_dl_list; dlnl != NULL; dlnl = dlnl->dlnl_next) {
56263448Sdh155122 		/*
56273448Sdh155122 		 * If the list changed and the new number is bigger
56283448Sdh155122 		 * than what the caller supplied, just count, don't
56293448Sdh155122 		 * do copyout
56303448Sdh155122 		 */
56313448Sdh155122 		if (++num > dlcount)
56323448Sdh155122 			continue;
56333448Sdh155122 		if (copyout(dlnl->dlnl_name, ptr, LIFNAMSIZ) != 0) {
56343448Sdh155122 			mutex_exit(&zone->zone_lock);
56353448Sdh155122 			zone_rele(zone);
56363448Sdh155122 			return (set_errno(EFAULT));
56373448Sdh155122 		}
56383448Sdh155122 		ptr += LIFNAMSIZ;
56393448Sdh155122 	}
56403448Sdh155122 	mutex_exit(&zone->zone_lock);
56413448Sdh155122 	zone_rele(zone);
56423448Sdh155122 
56433448Sdh155122 	/* Increased or decreased, caller should be notified. */
56443448Sdh155122 	if (num != dlcount) {
56453448Sdh155122 		if (copyout(&num, nump, sizeof (num)) != 0) {
56463448Sdh155122 			return (set_errno(EFAULT));
56473448Sdh155122 		}
56483448Sdh155122 	}
56493448Sdh155122 	return (0);
56503448Sdh155122 }
56513448Sdh155122 
56523448Sdh155122 /*
56533448Sdh155122  * Public interface for looking up a zone by zoneid. It's a customized version
56543448Sdh155122  * for netstack_zone_create(), it:
56553448Sdh155122  * 1. Doesn't acquire the zonehash_lock, since it is called from
56563448Sdh155122  *    zone_key_create() or zone_zsd_configure(), lock already held.
56573448Sdh155122  * 2. Doesn't check the status of the zone.
56583448Sdh155122  * 3. It will be called even before zone_init is called, in that case the
56593448Sdh155122  *    address of zone0 is returned directly, and netstack_zone_create()
56603448Sdh155122  *    will only assign a value to zone0.zone_netstack, won't break anything.
56613448Sdh155122  */
56623448Sdh155122 zone_t *
56633448Sdh155122 zone_find_by_id_nolock(zoneid_t zoneid)
56643448Sdh155122 {
56653448Sdh155122 	ASSERT(MUTEX_HELD(&zonehash_lock));
56663448Sdh155122 
56673448Sdh155122 	if (zonehashbyid == NULL)
56683448Sdh155122 		return (&zone0);
56693448Sdh155122 	else
56703448Sdh155122 		return (zone_find_all_by_id(zoneid));
56713448Sdh155122 }
5672