xref: /onnv-gate/usr/src/uts/common/os/zone.c (revision 1676:37f4a3e2bd99)
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
5*1676Sjpk  * Common Development and Distribution License (the "License").
6*1676Sjpk  * 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 /*
231409Sdp  * Copyright 2006 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.
1570Sstevel@tonic-gate  *   zsd_key_lock: This is a global lock protecting the key state for ZSD.
1580Sstevel@tonic-gate  *   zone_deathrow_lock: This is a global lock protecting the "deathrow"
1590Sstevel@tonic-gate  *       list (a list of zones in the ZONE_IS_DEAD state).
1600Sstevel@tonic-gate  *
1610Sstevel@tonic-gate  *   Ordering requirements:
1620Sstevel@tonic-gate  *       pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock -->
1630Sstevel@tonic-gate  *       	zone_lock --> zsd_key_lock --> pidlock --> p_lock
1640Sstevel@tonic-gate  *
1650Sstevel@tonic-gate  *   Blocking memory allocations are permitted while holding any of the
1660Sstevel@tonic-gate  *   zone locks.
1670Sstevel@tonic-gate  *
1680Sstevel@tonic-gate  *
1690Sstevel@tonic-gate  *   System Call Interface:
1700Sstevel@tonic-gate  *
1710Sstevel@tonic-gate  *   The zone subsystem can be managed and queried from user level with
1720Sstevel@tonic-gate  *   the following system calls (all subcodes of the primary "zone"
1730Sstevel@tonic-gate  *   system call):
1740Sstevel@tonic-gate  *   - zone_create: creates a zone with selected attributes (name,
175789Sahrens  *     root path, privileges, resource controls, ZFS datasets)
1760Sstevel@tonic-gate  *   - zone_enter: allows the current process to enter a zone
1770Sstevel@tonic-gate  *   - zone_getattr: reports attributes of a zone
1780Sstevel@tonic-gate  *   - zone_list: lists all zones active in the system
1790Sstevel@tonic-gate  *   - zone_lookup: looks up zone id based on name
1800Sstevel@tonic-gate  *   - zone_shutdown: initiates shutdown process (see states above)
1810Sstevel@tonic-gate  *   - zone_destroy: completes shutdown process (see states above)
1820Sstevel@tonic-gate  *
1830Sstevel@tonic-gate  */
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate #include <sys/priv_impl.h>
1860Sstevel@tonic-gate #include <sys/cred.h>
1870Sstevel@tonic-gate #include <c2/audit.h>
1880Sstevel@tonic-gate #include <sys/debug.h>
1890Sstevel@tonic-gate #include <sys/file.h>
1900Sstevel@tonic-gate #include <sys/kmem.h>
1910Sstevel@tonic-gate #include <sys/mutex.h>
192*1676Sjpk #include <sys/note.h>
1930Sstevel@tonic-gate #include <sys/pathname.h>
1940Sstevel@tonic-gate #include <sys/proc.h>
1950Sstevel@tonic-gate #include <sys/project.h>
1961166Sdstaff #include <sys/sysevent.h>
1970Sstevel@tonic-gate #include <sys/task.h>
1980Sstevel@tonic-gate #include <sys/systm.h>
1990Sstevel@tonic-gate #include <sys/types.h>
2000Sstevel@tonic-gate #include <sys/utsname.h>
2010Sstevel@tonic-gate #include <sys/vnode.h>
2020Sstevel@tonic-gate #include <sys/vfs.h>
2030Sstevel@tonic-gate #include <sys/systeminfo.h>
2040Sstevel@tonic-gate #include <sys/policy.h>
2050Sstevel@tonic-gate #include <sys/cred_impl.h>
2060Sstevel@tonic-gate #include <sys/contract_impl.h>
2070Sstevel@tonic-gate #include <sys/contract/process_impl.h>
2080Sstevel@tonic-gate #include <sys/class.h>
2090Sstevel@tonic-gate #include <sys/pool.h>
2100Sstevel@tonic-gate #include <sys/pool_pset.h>
2110Sstevel@tonic-gate #include <sys/pset.h>
2120Sstevel@tonic-gate #include <sys/sysmacros.h>
2130Sstevel@tonic-gate #include <sys/callb.h>
2140Sstevel@tonic-gate #include <sys/vmparam.h>
2150Sstevel@tonic-gate #include <sys/corectl.h>
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate #include <sys/door.h>
2180Sstevel@tonic-gate #include <sys/cpuvar.h>
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate #include <sys/uadmin.h>
2210Sstevel@tonic-gate #include <sys/session.h>
2220Sstevel@tonic-gate #include <sys/cmn_err.h>
2230Sstevel@tonic-gate #include <sys/modhash.h>
2240Sstevel@tonic-gate #include <sys/nvpair.h>
2250Sstevel@tonic-gate #include <sys/rctl.h>
2260Sstevel@tonic-gate #include <sys/fss.h>
2270Sstevel@tonic-gate #include <sys/zone.h>
228*1676Sjpk #include <sys/tsol/label.h>
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate /*
2310Sstevel@tonic-gate  * cv used to signal that all references to the zone have been released.  This
2320Sstevel@tonic-gate  * needs to be global since there may be multiple waiters, and the first to
2330Sstevel@tonic-gate  * wake up will free the zone_t, hence we cannot use zone->zone_cv.
2340Sstevel@tonic-gate  */
2350Sstevel@tonic-gate static kcondvar_t zone_destroy_cv;
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate  * Lock used to serialize access to zone_cv.  This could have been per-zone,
2380Sstevel@tonic-gate  * but then we'd need another lock for zone_destroy_cv, and why bother?
2390Sstevel@tonic-gate  */
2400Sstevel@tonic-gate static kmutex_t zone_status_lock;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate /*
2430Sstevel@tonic-gate  * ZSD-related global variables.
2440Sstevel@tonic-gate  */
2450Sstevel@tonic-gate static kmutex_t zsd_key_lock;	/* protects the following two */
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate  * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval.
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate static zone_key_t zsd_keyval = 0;
2500Sstevel@tonic-gate /*
2510Sstevel@tonic-gate  * Global list of registered keys.  We use this when a new zone is created.
2520Sstevel@tonic-gate  */
2530Sstevel@tonic-gate static list_t zsd_registered_keys;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate int zone_hash_size = 256;
256*1676Sjpk static mod_hash_t *zonehashbyname, *zonehashbyid, *zonehashbylabel;
2570Sstevel@tonic-gate static kmutex_t zonehash_lock;
2580Sstevel@tonic-gate static uint_t zonecount;
2590Sstevel@tonic-gate static id_space_t *zoneid_space;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate  * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the
2630Sstevel@tonic-gate  * kernel proper runs, and which manages all other zones.
2640Sstevel@tonic-gate  *
2650Sstevel@tonic-gate  * Although not declared as static, the variable "zone0" should not be used
2660Sstevel@tonic-gate  * except for by code that needs to reference the global zone early on in boot,
2670Sstevel@tonic-gate  * before it is fully initialized.  All other consumers should use
2680Sstevel@tonic-gate  * 'global_zone'.
2690Sstevel@tonic-gate  */
2700Sstevel@tonic-gate zone_t zone0;
2710Sstevel@tonic-gate zone_t *global_zone = NULL;	/* Set when the global zone is initialized */
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate  * List of active zones, protected by zonehash_lock.
2750Sstevel@tonic-gate  */
2760Sstevel@tonic-gate static list_t zone_active;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate  * List of destroyed zones that still have outstanding cred references.
2800Sstevel@tonic-gate  * Used for debugging.  Uses a separate lock to avoid lock ordering
2810Sstevel@tonic-gate  * problems in zone_free.
2820Sstevel@tonic-gate  */
2830Sstevel@tonic-gate static list_t zone_deathrow;
2840Sstevel@tonic-gate static kmutex_t zone_deathrow_lock;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */
2870Sstevel@tonic-gate uint_t maxzones = 8192;
2880Sstevel@tonic-gate 
2891166Sdstaff /* Event channel to sent zone state change notifications */
2901166Sdstaff evchan_t *zone_event_chan;
2911166Sdstaff 
2921166Sdstaff /*
2931166Sdstaff  * This table holds the mapping from kernel zone states to
2941166Sdstaff  * states visible in the state notification API.
2951166Sdstaff  * The idea is that we only expose "obvious" states and
2961166Sdstaff  * do not expose states which are just implementation details.
2971166Sdstaff  */
2981166Sdstaff const char  *zone_status_table[] = {
2991166Sdstaff 	ZONE_EVENT_UNINITIALIZED,	/* uninitialized */
3001166Sdstaff 	ZONE_EVENT_READY,		/* ready */
3011166Sdstaff 	ZONE_EVENT_READY,		/* booting */
3021166Sdstaff 	ZONE_EVENT_RUNNING,		/* running */
3031166Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* shutting_down */
3041166Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* empty */
3051166Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* down */
3061166Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* dying */
3071166Sdstaff 	ZONE_EVENT_UNINITIALIZED,	/* dead */
3081166Sdstaff };
3091166Sdstaff 
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate  * This isn't static so lint doesn't complain.
3120Sstevel@tonic-gate  */
3130Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares;
3140Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps;
3150Sstevel@tonic-gate /*
3160Sstevel@tonic-gate  * Synchronization primitives used to synchronize between mounts and zone
3170Sstevel@tonic-gate  * creation/destruction.
3180Sstevel@tonic-gate  */
3190Sstevel@tonic-gate static int mounts_in_progress;
3200Sstevel@tonic-gate static kcondvar_t mount_cv;
3210Sstevel@tonic-gate static kmutex_t mount_lock;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate const char * const zone_initname = "/sbin/init";
324*1676Sjpk static char * const zone_prefix = "/zone/";
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid);
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate /*
329813Sdp  * Bump this number when you alter the zone syscall interfaces; this is
330813Sdp  * because we need to have support for previous API versions in libc
331813Sdp  * to support patching; libc calls into the kernel to determine this number.
332813Sdp  *
333813Sdp  * Version 1 of the API is the version originally shipped with Solaris 10
334813Sdp  * Version 2 alters the zone_create system call in order to support more
335813Sdp  *     arguments by moving the args into a structure; and to do better
336813Sdp  *     error reporting when zone_create() fails.
337813Sdp  * Version 3 alters the zone_create system call in order to support the
338813Sdp  *     import of ZFS datasets to zones.
339*1676Sjpk  * Version 4 alters the zone_create system call in order to support
340*1676Sjpk  *     Trusted Extensions.
341813Sdp  */
342*1676Sjpk static const int ZONE_SYSCALL_API_VERSION = 4;
343813Sdp 
344813Sdp /*
3450Sstevel@tonic-gate  * Certain filesystems (such as NFS and autofs) need to know which zone
3460Sstevel@tonic-gate  * the mount is being placed in.  Because of this, we need to be able to
3470Sstevel@tonic-gate  * ensure that a zone isn't in the process of being created such that
3480Sstevel@tonic-gate  * nfs_mount() thinks it is in the global zone, while by the time it
3490Sstevel@tonic-gate  * gets added the list of mounted zones, it ends up on zoneA's mount
3500Sstevel@tonic-gate  * list.
3510Sstevel@tonic-gate  *
3520Sstevel@tonic-gate  * The following functions: block_mounts()/resume_mounts() and
3530Sstevel@tonic-gate  * mount_in_progress()/mount_completed() are used by zones and the VFS
3540Sstevel@tonic-gate  * layer (respectively) to synchronize zone creation and new mounts.
3550Sstevel@tonic-gate  *
3560Sstevel@tonic-gate  * The semantics are like a reader-reader lock such that there may
3570Sstevel@tonic-gate  * either be multiple mounts (or zone creations, if that weren't
3580Sstevel@tonic-gate  * serialized by zonehash_lock) in progress at the same time, but not
3590Sstevel@tonic-gate  * both.
3600Sstevel@tonic-gate  *
3610Sstevel@tonic-gate  * We use cv's so the user can ctrl-C out of the operation if it's
3620Sstevel@tonic-gate  * taking too long.
3630Sstevel@tonic-gate  *
3640Sstevel@tonic-gate  * The semantics are such that there is unfair bias towards the
3650Sstevel@tonic-gate  * "current" operation.  This means that zone creations may starve if
3660Sstevel@tonic-gate  * there is a rapid succession of new mounts coming in to the system, or
3670Sstevel@tonic-gate  * there is a remote possibility that zones will be created at such a
3680Sstevel@tonic-gate  * rate that new mounts will not be able to proceed.
3690Sstevel@tonic-gate  */
3700Sstevel@tonic-gate /*
3710Sstevel@tonic-gate  * Prevent new mounts from progressing to the point of calling
3720Sstevel@tonic-gate  * VFS_MOUNT().  If there are already mounts in this "region", wait for
3730Sstevel@tonic-gate  * them to complete.
3740Sstevel@tonic-gate  */
3750Sstevel@tonic-gate static int
3760Sstevel@tonic-gate block_mounts(void)
3770Sstevel@tonic-gate {
3780Sstevel@tonic-gate 	int retval = 0;
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	/*
3810Sstevel@tonic-gate 	 * Since it may block for a long time, block_mounts() shouldn't be
3820Sstevel@tonic-gate 	 * called with zonehash_lock held.
3830Sstevel@tonic-gate 	 */
3840Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
3850Sstevel@tonic-gate 	mutex_enter(&mount_lock);
3860Sstevel@tonic-gate 	while (mounts_in_progress > 0) {
3870Sstevel@tonic-gate 		if (cv_wait_sig(&mount_cv, &mount_lock) == 0)
3880Sstevel@tonic-gate 			goto signaled;
3890Sstevel@tonic-gate 	}
3900Sstevel@tonic-gate 	/*
3910Sstevel@tonic-gate 	 * A negative value of mounts_in_progress indicates that mounts
3920Sstevel@tonic-gate 	 * have been blocked by (-mounts_in_progress) different callers.
3930Sstevel@tonic-gate 	 */
3940Sstevel@tonic-gate 	mounts_in_progress--;
3950Sstevel@tonic-gate 	retval = 1;
3960Sstevel@tonic-gate signaled:
3970Sstevel@tonic-gate 	mutex_exit(&mount_lock);
3980Sstevel@tonic-gate 	return (retval);
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate /*
4020Sstevel@tonic-gate  * The VFS layer may progress with new mounts as far as we're concerned.
4030Sstevel@tonic-gate  * Allow them to progress if we were the last obstacle.
4040Sstevel@tonic-gate  */
4050Sstevel@tonic-gate static void
4060Sstevel@tonic-gate resume_mounts(void)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4090Sstevel@tonic-gate 	if (++mounts_in_progress == 0)
4100Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
4110Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate /*
4150Sstevel@tonic-gate  * The VFS layer is busy with a mount; zones should wait until all
4160Sstevel@tonic-gate  * mounts are completed to progress.
4170Sstevel@tonic-gate  */
4180Sstevel@tonic-gate void
4190Sstevel@tonic-gate mount_in_progress(void)
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4220Sstevel@tonic-gate 	while (mounts_in_progress < 0)
4230Sstevel@tonic-gate 		cv_wait(&mount_cv, &mount_lock);
4240Sstevel@tonic-gate 	mounts_in_progress++;
4250Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate /*
4290Sstevel@tonic-gate  * VFS is done with one mount; wake up any waiting block_mounts()
4300Sstevel@tonic-gate  * callers if this is the last mount.
4310Sstevel@tonic-gate  */
4320Sstevel@tonic-gate void
4330Sstevel@tonic-gate mount_completed(void)
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4360Sstevel@tonic-gate 	if (--mounts_in_progress == 0)
4370Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
4380Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate /*
4420Sstevel@tonic-gate  * ZSD routines.
4430Sstevel@tonic-gate  *
4440Sstevel@tonic-gate  * Zone Specific Data (ZSD) is modeled after Thread Specific Data as
4450Sstevel@tonic-gate  * defined by the pthread_key_create() and related interfaces.
4460Sstevel@tonic-gate  *
4470Sstevel@tonic-gate  * Kernel subsystems may register one or more data items and/or
4480Sstevel@tonic-gate  * callbacks to be executed when a zone is created, shutdown, or
4490Sstevel@tonic-gate  * destroyed.
4500Sstevel@tonic-gate  *
4510Sstevel@tonic-gate  * Unlike the thread counterpart, destructor callbacks will be executed
4520Sstevel@tonic-gate  * even if the data pointer is NULL and/or there are no constructor
4530Sstevel@tonic-gate  * callbacks, so it is the responsibility of such callbacks to check for
4540Sstevel@tonic-gate  * NULL data values if necessary.
4550Sstevel@tonic-gate  *
4560Sstevel@tonic-gate  * The locking strategy and overall picture is as follows:
4570Sstevel@tonic-gate  *
4580Sstevel@tonic-gate  * When someone calls zone_key_create(), a template ZSD entry is added to the
4590Sstevel@tonic-gate  * global list "zsd_registered_keys", protected by zsd_key_lock.  The
4600Sstevel@tonic-gate  * constructor callback is called immediately on all existing zones, and a
4610Sstevel@tonic-gate  * copy of the ZSD entry added to the per-zone zone_zsd list (protected by
4620Sstevel@tonic-gate  * zone_lock).  As this operation requires the list of zones, the list of
4630Sstevel@tonic-gate  * registered keys, and the per-zone list of ZSD entries to remain constant
4640Sstevel@tonic-gate  * throughout the entire operation, it must grab zonehash_lock, zone_lock for
4650Sstevel@tonic-gate  * all existing zones, and zsd_key_lock, in that order.  Similar locking is
4660Sstevel@tonic-gate  * needed when zone_key_delete() is called.  It is thus sufficient to hold
4670Sstevel@tonic-gate  * zsd_key_lock *or* zone_lock to prevent additions to or removals from the
4680Sstevel@tonic-gate  * per-zone zone_zsd list.
4690Sstevel@tonic-gate  *
4700Sstevel@tonic-gate  * Note that this implementation does not make a copy of the ZSD entry if a
4710Sstevel@tonic-gate  * constructor callback is not provided.  A zone_getspecific() on such an
4720Sstevel@tonic-gate  * uninitialized ZSD entry will return NULL.
4730Sstevel@tonic-gate  *
4740Sstevel@tonic-gate  * When new zones are created constructor callbacks for all registered ZSD
4750Sstevel@tonic-gate  * entries will be called.
4760Sstevel@tonic-gate  *
4770Sstevel@tonic-gate  * The framework does not provide any locking around zone_getspecific() and
4780Sstevel@tonic-gate  * zone_setspecific() apart from that needed for internal consistency, so
4790Sstevel@tonic-gate  * callers interested in atomic "test-and-set" semantics will need to provide
4800Sstevel@tonic-gate  * their own locking.
4810Sstevel@tonic-gate  */
4820Sstevel@tonic-gate void
4830Sstevel@tonic-gate zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t),
4840Sstevel@tonic-gate     void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *))
4850Sstevel@tonic-gate {
4860Sstevel@tonic-gate 	struct zsd_entry *zsdp;
4870Sstevel@tonic-gate 	struct zsd_entry *t;
4880Sstevel@tonic-gate 	struct zone *zone;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	zsdp = kmem_alloc(sizeof (*zsdp), KM_SLEEP);
4910Sstevel@tonic-gate 	zsdp->zsd_data = NULL;
4920Sstevel@tonic-gate 	zsdp->zsd_create = create;
4930Sstevel@tonic-gate 	zsdp->zsd_shutdown = shutdown;
4940Sstevel@tonic-gate 	zsdp->zsd_destroy = destroy;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);	/* stop the world */
4970Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
4980Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
4990Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);	/* lock all zones */
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
5020Sstevel@tonic-gate 	*keyp = zsdp->zsd_key = ++zsd_keyval;
5030Sstevel@tonic-gate 	ASSERT(zsd_keyval != 0);
5040Sstevel@tonic-gate 	list_insert_tail(&zsd_registered_keys, zsdp);
5050Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	if (create != NULL) {
5080Sstevel@tonic-gate 		for (zone = list_head(&zone_active); zone != NULL;
5090Sstevel@tonic-gate 		    zone = list_next(&zone_active, zone)) {
5100Sstevel@tonic-gate 			t = kmem_alloc(sizeof (*t), KM_SLEEP);
5110Sstevel@tonic-gate 			t->zsd_key = *keyp;
5120Sstevel@tonic-gate 			t->zsd_data = (*create)(zone->zone_id);
5130Sstevel@tonic-gate 			t->zsd_create = create;
5140Sstevel@tonic-gate 			t->zsd_shutdown = shutdown;
5150Sstevel@tonic-gate 			t->zsd_destroy = destroy;
5160Sstevel@tonic-gate 			list_insert_tail(&zone->zone_zsd, t);
5170Sstevel@tonic-gate 		}
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
5200Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
5210Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
5220Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate /*
5260Sstevel@tonic-gate  * Helper function to find the zsd_entry associated with the key in the
5270Sstevel@tonic-gate  * given list.
5280Sstevel@tonic-gate  */
5290Sstevel@tonic-gate static struct zsd_entry *
5300Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key)
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate 	struct zsd_entry *zsd;
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) {
5350Sstevel@tonic-gate 		if (zsd->zsd_key == key) {
5360Sstevel@tonic-gate 			/*
5370Sstevel@tonic-gate 			 * Move to head of list to keep list in MRU order.
5380Sstevel@tonic-gate 			 */
5390Sstevel@tonic-gate 			if (zsd != list_head(l)) {
5400Sstevel@tonic-gate 				list_remove(l, zsd);
5410Sstevel@tonic-gate 				list_insert_head(l, zsd);
5420Sstevel@tonic-gate 			}
5430Sstevel@tonic-gate 			return (zsd);
5440Sstevel@tonic-gate 		}
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 	return (NULL);
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate /*
5500Sstevel@tonic-gate  * Function called when a module is being unloaded, or otherwise wishes
5510Sstevel@tonic-gate  * to unregister its ZSD key and callbacks.
5520Sstevel@tonic-gate  */
5530Sstevel@tonic-gate int
5540Sstevel@tonic-gate zone_key_delete(zone_key_t key)
5550Sstevel@tonic-gate {
5560Sstevel@tonic-gate 	struct zsd_entry *zsdp = NULL;
5570Sstevel@tonic-gate 	zone_t *zone;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);	/* Zone create/delete waits for us */
5600Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
5610Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
5620Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);	/* lock all zones */
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
5650Sstevel@tonic-gate 	zsdp = zsd_find(&zsd_registered_keys, key);
5660Sstevel@tonic-gate 	if (zsdp == NULL)
5670Sstevel@tonic-gate 		goto notfound;
5680Sstevel@tonic-gate 	list_remove(&zsd_registered_keys, zsdp);
5690Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
5720Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
5730Sstevel@tonic-gate 		struct zsd_entry *del;
5740Sstevel@tonic-gate 		void *data;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 		if (!(zone->zone_flags & ZF_DESTROYED)) {
5770Sstevel@tonic-gate 			del = zsd_find(&zone->zone_zsd, key);
5780Sstevel@tonic-gate 			if (del != NULL) {
5790Sstevel@tonic-gate 				data = del->zsd_data;
5800Sstevel@tonic-gate 				ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown);
5810Sstevel@tonic-gate 				ASSERT(del->zsd_destroy == zsdp->zsd_destroy);
5820Sstevel@tonic-gate 				list_remove(&zone->zone_zsd, del);
5830Sstevel@tonic-gate 				kmem_free(del, sizeof (*del));
5840Sstevel@tonic-gate 			} else {
5850Sstevel@tonic-gate 				data = NULL;
5860Sstevel@tonic-gate 			}
5870Sstevel@tonic-gate 			if (zsdp->zsd_shutdown)
5880Sstevel@tonic-gate 				zsdp->zsd_shutdown(zone->zone_id, data);
5890Sstevel@tonic-gate 			if (zsdp->zsd_destroy)
5900Sstevel@tonic-gate 				zsdp->zsd_destroy(zone->zone_id, data);
5910Sstevel@tonic-gate 		}
5920Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
5950Sstevel@tonic-gate 	kmem_free(zsdp, sizeof (*zsdp));
5960Sstevel@tonic-gate 	return (0);
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate notfound:
5990Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
6000Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
6010Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
6020Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
6030Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
6040Sstevel@tonic-gate 	return (-1);
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate /*
6080Sstevel@tonic-gate  * ZSD counterpart of pthread_setspecific().
6090Sstevel@tonic-gate  */
6100Sstevel@tonic-gate int
6110Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data)
6120Sstevel@tonic-gate {
6130Sstevel@tonic-gate 	struct zsd_entry *t;
6140Sstevel@tonic-gate 	struct zsd_entry *zsdp = NULL;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
6170Sstevel@tonic-gate 	t = zsd_find(&zone->zone_zsd, key);
6180Sstevel@tonic-gate 	if (t != NULL) {
6190Sstevel@tonic-gate 		/*
6200Sstevel@tonic-gate 		 * Replace old value with new
6210Sstevel@tonic-gate 		 */
6220Sstevel@tonic-gate 		t->zsd_data = (void *)data;
6230Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
6240Sstevel@tonic-gate 		return (0);
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 	/*
6270Sstevel@tonic-gate 	 * If there was no previous value, go through the list of registered
6280Sstevel@tonic-gate 	 * keys.
6290Sstevel@tonic-gate 	 *
6300Sstevel@tonic-gate 	 * We avoid grabbing zsd_key_lock until we are sure we need it; this is
6310Sstevel@tonic-gate 	 * necessary for shutdown callbacks to be able to execute without fear
6320Sstevel@tonic-gate 	 * of deadlock.
6330Sstevel@tonic-gate 	 */
6340Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
6350Sstevel@tonic-gate 	zsdp = zsd_find(&zsd_registered_keys, key);
6360Sstevel@tonic-gate 	if (zsdp == NULL) { 	/* Key was not registered */
6370Sstevel@tonic-gate 		mutex_exit(&zsd_key_lock);
6380Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
6390Sstevel@tonic-gate 		return (-1);
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	/*
6430Sstevel@tonic-gate 	 * Add a zsd_entry to this zone, using the template we just retrieved
6440Sstevel@tonic-gate 	 * to initialize the constructor and destructor(s).
6450Sstevel@tonic-gate 	 */
6460Sstevel@tonic-gate 	t = kmem_alloc(sizeof (*t), KM_SLEEP);
6470Sstevel@tonic-gate 	t->zsd_key = key;
6480Sstevel@tonic-gate 	t->zsd_data = (void *)data;
6490Sstevel@tonic-gate 	t->zsd_create = zsdp->zsd_create;
6500Sstevel@tonic-gate 	t->zsd_shutdown = zsdp->zsd_shutdown;
6510Sstevel@tonic-gate 	t->zsd_destroy = zsdp->zsd_destroy;
6520Sstevel@tonic-gate 	list_insert_tail(&zone->zone_zsd, t);
6530Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
6540Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
6550Sstevel@tonic-gate 	return (0);
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate /*
6590Sstevel@tonic-gate  * ZSD counterpart of pthread_getspecific().
6600Sstevel@tonic-gate  */
6610Sstevel@tonic-gate void *
6620Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone)
6630Sstevel@tonic-gate {
6640Sstevel@tonic-gate 	struct zsd_entry *t;
6650Sstevel@tonic-gate 	void *data;
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
6680Sstevel@tonic-gate 	t = zsd_find(&zone->zone_zsd, key);
6690Sstevel@tonic-gate 	data = (t == NULL ? NULL : t->zsd_data);
6700Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
6710Sstevel@tonic-gate 	return (data);
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate /*
6750Sstevel@tonic-gate  * Function used to initialize a zone's list of ZSD callbacks and data
6760Sstevel@tonic-gate  * when the zone is being created.  The callbacks are initialized from
6770Sstevel@tonic-gate  * the template list (zsd_registered_keys), and the constructor
6780Sstevel@tonic-gate  * callback executed (if one exists).
6790Sstevel@tonic-gate  *
6800Sstevel@tonic-gate  * This is called before the zone is made publicly available, hence no
6810Sstevel@tonic-gate  * need to grab zone_lock.
6820Sstevel@tonic-gate  *
6830Sstevel@tonic-gate  * Although we grab and release zsd_key_lock, new entries cannot be
6840Sstevel@tonic-gate  * added to or removed from the zsd_registered_keys list until we
6850Sstevel@tonic-gate  * release zonehash_lock, so there isn't a window for a
6860Sstevel@tonic-gate  * zone_key_create() to come in after we've dropped zsd_key_lock but
6870Sstevel@tonic-gate  * before the zone is added to the zone list, such that the constructor
6880Sstevel@tonic-gate  * callbacks aren't executed for the new zone.
6890Sstevel@tonic-gate  */
6900Sstevel@tonic-gate static void
6910Sstevel@tonic-gate zone_zsd_configure(zone_t *zone)
6920Sstevel@tonic-gate {
6930Sstevel@tonic-gate 	struct zsd_entry *zsdp;
6940Sstevel@tonic-gate 	struct zsd_entry *t;
6950Sstevel@tonic-gate 	zoneid_t zoneid = zone->zone_id;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
6980Sstevel@tonic-gate 	ASSERT(list_head(&zone->zone_zsd) == NULL);
6990Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
7000Sstevel@tonic-gate 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
7010Sstevel@tonic-gate 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
7020Sstevel@tonic-gate 		if (zsdp->zsd_create != NULL) {
7030Sstevel@tonic-gate 			t = kmem_alloc(sizeof (*t), KM_SLEEP);
7040Sstevel@tonic-gate 			t->zsd_key = zsdp->zsd_key;
7050Sstevel@tonic-gate 			t->zsd_create = zsdp->zsd_create;
7060Sstevel@tonic-gate 			t->zsd_data = (*t->zsd_create)(zoneid);
7070Sstevel@tonic-gate 			t->zsd_shutdown = zsdp->zsd_shutdown;
7080Sstevel@tonic-gate 			t->zsd_destroy = zsdp->zsd_destroy;
7090Sstevel@tonic-gate 			list_insert_tail(&zone->zone_zsd, t);
7100Sstevel@tonic-gate 		}
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY };
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate /*
7180Sstevel@tonic-gate  * Helper function to execute shutdown or destructor callbacks.
7190Sstevel@tonic-gate  */
7200Sstevel@tonic-gate static void
7210Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct)
7220Sstevel@tonic-gate {
7230Sstevel@tonic-gate 	struct zsd_entry *zsdp;
7240Sstevel@tonic-gate 	struct zsd_entry *t;
7250Sstevel@tonic-gate 	zoneid_t zoneid = zone->zone_id;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY);
7280Sstevel@tonic-gate 	ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY);
7290Sstevel@tonic-gate 	ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN);
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
7320Sstevel@tonic-gate 	if (ct == ZSD_DESTROY) {
7330Sstevel@tonic-gate 		if (zone->zone_flags & ZF_DESTROYED) {
7340Sstevel@tonic-gate 			/*
7350Sstevel@tonic-gate 			 * Make sure destructors are only called once.
7360Sstevel@tonic-gate 			 */
7370Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
7380Sstevel@tonic-gate 			return;
7390Sstevel@tonic-gate 		}
7400Sstevel@tonic-gate 		zone->zone_flags |= ZF_DESTROYED;
7410Sstevel@tonic-gate 	}
7420Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	/*
7450Sstevel@tonic-gate 	 * Both zsd_key_lock and zone_lock need to be held in order to add or
7460Sstevel@tonic-gate 	 * remove a ZSD key, (either globally as part of
7470Sstevel@tonic-gate 	 * zone_key_create()/zone_key_delete(), or on a per-zone basis, as is
7480Sstevel@tonic-gate 	 * possible through zone_setspecific()), so it's sufficient to hold
7490Sstevel@tonic-gate 	 * zsd_key_lock here.
7500Sstevel@tonic-gate 	 *
7510Sstevel@tonic-gate 	 * This is a good thing, since we don't want to recursively try to grab
7520Sstevel@tonic-gate 	 * zone_lock if a callback attempts to do something like a crfree() or
7530Sstevel@tonic-gate 	 * zone_rele().
7540Sstevel@tonic-gate 	 */
7550Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
7560Sstevel@tonic-gate 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
7570Sstevel@tonic-gate 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
7580Sstevel@tonic-gate 		zone_key_t key = zsdp->zsd_key;
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 		/* Skip if no callbacks registered */
7610Sstevel@tonic-gate 		if (ct == ZSD_SHUTDOWN && zsdp->zsd_shutdown == NULL)
7620Sstevel@tonic-gate 			continue;
7630Sstevel@tonic-gate 		if (ct == ZSD_DESTROY && zsdp->zsd_destroy == NULL)
7640Sstevel@tonic-gate 			continue;
7650Sstevel@tonic-gate 		/*
7660Sstevel@tonic-gate 		 * Call the callback with the zone-specific data if we can find
7670Sstevel@tonic-gate 		 * any, otherwise with NULL.
7680Sstevel@tonic-gate 		 */
7690Sstevel@tonic-gate 		t = zsd_find(&zone->zone_zsd, key);
7700Sstevel@tonic-gate 		if (t != NULL) {
7710Sstevel@tonic-gate 			if (ct == ZSD_SHUTDOWN) {
7720Sstevel@tonic-gate 				t->zsd_shutdown(zoneid, t->zsd_data);
7730Sstevel@tonic-gate 			} else {
7740Sstevel@tonic-gate 				ASSERT(ct == ZSD_DESTROY);
7750Sstevel@tonic-gate 				t->zsd_destroy(zoneid, t->zsd_data);
7760Sstevel@tonic-gate 			}
7770Sstevel@tonic-gate 		} else {
7780Sstevel@tonic-gate 			if (ct == ZSD_SHUTDOWN) {
7790Sstevel@tonic-gate 				zsdp->zsd_shutdown(zoneid, NULL);
7800Sstevel@tonic-gate 			} else {
7810Sstevel@tonic-gate 				ASSERT(ct == ZSD_DESTROY);
7820Sstevel@tonic-gate 				zsdp->zsd_destroy(zoneid, NULL);
7830Sstevel@tonic-gate 			}
7840Sstevel@tonic-gate 		}
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate /*
7900Sstevel@tonic-gate  * Called when the zone is going away; free ZSD-related memory, and
7910Sstevel@tonic-gate  * destroy the zone_zsd list.
7920Sstevel@tonic-gate  */
7930Sstevel@tonic-gate static void
7940Sstevel@tonic-gate zone_free_zsd(zone_t *zone)
7950Sstevel@tonic-gate {
7960Sstevel@tonic-gate 	struct zsd_entry *t, *next;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	/*
7990Sstevel@tonic-gate 	 * Free all the zsd_entry's we had on this zone.
8000Sstevel@tonic-gate 	 */
8010Sstevel@tonic-gate 	for (t = list_head(&zone->zone_zsd); t != NULL; t = next) {
8020Sstevel@tonic-gate 		next = list_next(&zone->zone_zsd, t);
8030Sstevel@tonic-gate 		list_remove(&zone->zone_zsd, t);
8040Sstevel@tonic-gate 		kmem_free(t, sizeof (*t));
8050Sstevel@tonic-gate 	}
8060Sstevel@tonic-gate 	list_destroy(&zone->zone_zsd);
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate /*
810789Sahrens  * Frees memory associated with the zone dataset list.
811789Sahrens  */
812789Sahrens static void
813789Sahrens zone_free_datasets(zone_t *zone)
814789Sahrens {
815789Sahrens 	zone_dataset_t *t, *next;
816789Sahrens 
817789Sahrens 	for (t = list_head(&zone->zone_datasets); t != NULL; t = next) {
818789Sahrens 		next = list_next(&zone->zone_datasets, t);
819789Sahrens 		list_remove(&zone->zone_datasets, t);
820789Sahrens 		kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1);
821789Sahrens 		kmem_free(t, sizeof (*t));
822789Sahrens 	}
823789Sahrens 	list_destroy(&zone->zone_datasets);
824789Sahrens }
825789Sahrens 
826789Sahrens /*
8270Sstevel@tonic-gate  * zone.cpu-shares resource control support.
8280Sstevel@tonic-gate  */
8290Sstevel@tonic-gate /*ARGSUSED*/
8300Sstevel@tonic-gate static rctl_qty_t
8310Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p)
8320Sstevel@tonic-gate {
8330Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8340Sstevel@tonic-gate 	return (p->p_zone->zone_shares);
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate /*ARGSUSED*/
8380Sstevel@tonic-gate static int
8390Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
8400Sstevel@tonic-gate     rctl_qty_t nv)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8430Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
8440Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
8450Sstevel@tonic-gate 		return (0);
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	e->rcep_p.zone->zone_shares = nv;
8480Sstevel@tonic-gate 	return (0);
8490Sstevel@tonic-gate }
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = {
8520Sstevel@tonic-gate 	rcop_no_action,
8530Sstevel@tonic-gate 	zone_cpu_shares_usage,
8540Sstevel@tonic-gate 	zone_cpu_shares_set,
8550Sstevel@tonic-gate 	rcop_no_test
8560Sstevel@tonic-gate };
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate /*ARGSUSED*/
8590Sstevel@tonic-gate static rctl_qty_t
8600Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p)
8610Sstevel@tonic-gate {
8620Sstevel@tonic-gate 	rctl_qty_t nlwps;
8630Sstevel@tonic-gate 	zone_t *zone = p->p_zone;
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
8680Sstevel@tonic-gate 	nlwps = zone->zone_nlwps;
8690Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	return (nlwps);
8720Sstevel@tonic-gate }
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate /*ARGSUSED*/
8750Sstevel@tonic-gate static int
8760Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
8770Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate 	rctl_qty_t nlwps;
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8820Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
8830Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
8840Sstevel@tonic-gate 		return (0);
8850Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock)));
8860Sstevel@tonic-gate 	nlwps = e->rcep_p.zone->zone_nlwps;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	if (nlwps + incr > rcntl->rcv_value)
8890Sstevel@tonic-gate 		return (1);
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	return (0);
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate /*ARGSUSED*/
8950Sstevel@tonic-gate static int
8960Sstevel@tonic-gate zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) {
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8990Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
9000Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
9010Sstevel@tonic-gate 		return (0);
9020Sstevel@tonic-gate 	e->rcep_p.zone->zone_nlwps_ctl = nv;
9030Sstevel@tonic-gate 	return (0);
9040Sstevel@tonic-gate }
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = {
9070Sstevel@tonic-gate 	rcop_no_action,
9080Sstevel@tonic-gate 	zone_lwps_usage,
9090Sstevel@tonic-gate 	zone_lwps_set,
9100Sstevel@tonic-gate 	zone_lwps_test,
9110Sstevel@tonic-gate };
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate /*
9140Sstevel@tonic-gate  * Helper function to brand the zone with a unique ID.
9150Sstevel@tonic-gate  */
9160Sstevel@tonic-gate static void
9170Sstevel@tonic-gate zone_uniqid(zone_t *zone)
9180Sstevel@tonic-gate {
9190Sstevel@tonic-gate 	static uint64_t uniqid = 0;
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
9220Sstevel@tonic-gate 	zone->zone_uniqid = uniqid++;
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate /*
9260Sstevel@tonic-gate  * Returns a held pointer to the "kcred" for the specified zone.
9270Sstevel@tonic-gate  */
9280Sstevel@tonic-gate struct cred *
9290Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid)
9300Sstevel@tonic-gate {
9310Sstevel@tonic-gate 	zone_t *zone;
9320Sstevel@tonic-gate 	cred_t *cr;
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	if ((zone = zone_find_by_id(zoneid)) == NULL)
9350Sstevel@tonic-gate 		return (NULL);
9360Sstevel@tonic-gate 	cr = zone->zone_kcred;
9370Sstevel@tonic-gate 	crhold(cr);
9380Sstevel@tonic-gate 	zone_rele(zone);
9390Sstevel@tonic-gate 	return (cr);
9400Sstevel@tonic-gate }
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate /*
9430Sstevel@tonic-gate  * Called very early on in boot to initialize the ZSD list so that
9440Sstevel@tonic-gate  * zone_key_create() can be called before zone_init().  It also initializes
9450Sstevel@tonic-gate  * portions of zone0 which may be used before zone_init() is called.  The
9460Sstevel@tonic-gate  * variable "global_zone" will be set when zone0 is fully initialized by
9470Sstevel@tonic-gate  * zone_init().
9480Sstevel@tonic-gate  */
9490Sstevel@tonic-gate void
9500Sstevel@tonic-gate zone_zsd_init(void)
9510Sstevel@tonic-gate {
9520Sstevel@tonic-gate 	mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL);
9530Sstevel@tonic-gate 	mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL);
9540Sstevel@tonic-gate 	list_create(&zsd_registered_keys, sizeof (struct zsd_entry),
9550Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
9560Sstevel@tonic-gate 	list_create(&zone_active, sizeof (zone_t),
9570Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
9580Sstevel@tonic-gate 	list_create(&zone_deathrow, sizeof (zone_t),
9590Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL);
9620Sstevel@tonic-gate 	mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
9630Sstevel@tonic-gate 	zone0.zone_shares = 1;
9640Sstevel@tonic-gate 	zone0.zone_nlwps_ctl = INT_MAX;
9650Sstevel@tonic-gate 	zone0.zone_name = GLOBAL_ZONENAME;
9660Sstevel@tonic-gate 	zone0.zone_nodename = utsname.nodename;
9670Sstevel@tonic-gate 	zone0.zone_domain = srpc_domain;
9680Sstevel@tonic-gate 	zone0.zone_ref = 1;
9690Sstevel@tonic-gate 	zone0.zone_id = GLOBAL_ZONEID;
9700Sstevel@tonic-gate 	zone0.zone_status = ZONE_IS_RUNNING;
9710Sstevel@tonic-gate 	zone0.zone_rootpath = "/";
9720Sstevel@tonic-gate 	zone0.zone_rootpathlen = 2;
9730Sstevel@tonic-gate 	zone0.zone_psetid = ZONE_PS_INVAL;
9740Sstevel@tonic-gate 	zone0.zone_ncpus = 0;
9750Sstevel@tonic-gate 	zone0.zone_ncpus_online = 0;
9760Sstevel@tonic-gate 	zone0.zone_proc_initpid = 1;
9770Sstevel@tonic-gate 	list_create(&zone0.zone_zsd, sizeof (struct zsd_entry),
9780Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
9790Sstevel@tonic-gate 	list_insert_head(&zone_active, &zone0);
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	/*
9820Sstevel@tonic-gate 	 * The root filesystem is not mounted yet, so zone_rootvp cannot be set
9830Sstevel@tonic-gate 	 * to anything meaningful.  It is assigned to be 'rootdir' in
9840Sstevel@tonic-gate 	 * vfs_mountroot().
9850Sstevel@tonic-gate 	 */
9860Sstevel@tonic-gate 	zone0.zone_rootvp = NULL;
9870Sstevel@tonic-gate 	zone0.zone_vfslist = NULL;
9880Sstevel@tonic-gate 	zone0.zone_bootargs = NULL;
9890Sstevel@tonic-gate 	zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
9900Sstevel@tonic-gate 	/*
9910Sstevel@tonic-gate 	 * The global zone has all privileges
9920Sstevel@tonic-gate 	 */
9930Sstevel@tonic-gate 	priv_fillset(zone0.zone_privset);
9940Sstevel@tonic-gate 	/*
9950Sstevel@tonic-gate 	 * Add p0 to the global zone
9960Sstevel@tonic-gate 	 */
9970Sstevel@tonic-gate 	zone0.zone_zsched = &p0;
9980Sstevel@tonic-gate 	p0.p_zone = &zone0;
9990Sstevel@tonic-gate }
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate /*
1002*1676Sjpk  * Compute a hash value based on the contents of the label and the DOI.  The
1003*1676Sjpk  * hash algorithm is somewhat arbitrary, but is based on the observation that
1004*1676Sjpk  * humans will likely pick labels that differ by amounts that work out to be
1005*1676Sjpk  * multiples of the number of hash chains, and thus stirring in some primes
1006*1676Sjpk  * should help.
1007*1676Sjpk  */
1008*1676Sjpk static uint_t
1009*1676Sjpk hash_bylabel(void *hdata, mod_hash_key_t key)
1010*1676Sjpk {
1011*1676Sjpk 	const ts_label_t *lab = (ts_label_t *)key;
1012*1676Sjpk 	const uint32_t *up, *ue;
1013*1676Sjpk 	uint_t hash;
1014*1676Sjpk 	int i;
1015*1676Sjpk 
1016*1676Sjpk 	_NOTE(ARGUNUSED(hdata));
1017*1676Sjpk 
1018*1676Sjpk 	hash = lab->tsl_doi + (lab->tsl_doi << 1);
1019*1676Sjpk 	/* we depend on alignment of label, but not representation */
1020*1676Sjpk 	up = (const uint32_t *)&lab->tsl_label;
1021*1676Sjpk 	ue = up + sizeof (lab->tsl_label) / sizeof (*up);
1022*1676Sjpk 	i = 1;
1023*1676Sjpk 	while (up < ue) {
1024*1676Sjpk 		/* using 2^n + 1, 1 <= n <= 16 as source of many primes */
1025*1676Sjpk 		hash += *up + (*up << ((i % 16) + 1));
1026*1676Sjpk 		up++;
1027*1676Sjpk 		i++;
1028*1676Sjpk 	}
1029*1676Sjpk 	return (hash);
1030*1676Sjpk }
1031*1676Sjpk 
1032*1676Sjpk /*
1033*1676Sjpk  * All that mod_hash cares about here is zero (equal) versus non-zero (not
1034*1676Sjpk  * equal).  This may need to be changed if less than / greater than is ever
1035*1676Sjpk  * needed.
1036*1676Sjpk  */
1037*1676Sjpk static int
1038*1676Sjpk hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
1039*1676Sjpk {
1040*1676Sjpk 	ts_label_t *lab1 = (ts_label_t *)key1;
1041*1676Sjpk 	ts_label_t *lab2 = (ts_label_t *)key2;
1042*1676Sjpk 
1043*1676Sjpk 	return (label_equal(lab1, lab2) ? 0 : 1);
1044*1676Sjpk }
1045*1676Sjpk 
1046*1676Sjpk /*
10470Sstevel@tonic-gate  * Called by main() to initialize the zones framework.
10480Sstevel@tonic-gate  */
10490Sstevel@tonic-gate void
10500Sstevel@tonic-gate zone_init(void)
10510Sstevel@tonic-gate {
10520Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
10530Sstevel@tonic-gate 	rctl_val_t *dval;
10540Sstevel@tonic-gate 	rctl_set_t *set;
10550Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
10560Sstevel@tonic-gate 	rctl_entity_p_t e;
10571166Sdstaff 	int res;
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	ASSERT(curproc == &p0);
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	/*
10620Sstevel@tonic-gate 	 * Create ID space for zone IDs.  ID 0 is reserved for the
10630Sstevel@tonic-gate 	 * global zone.
10640Sstevel@tonic-gate 	 */
10650Sstevel@tonic-gate 	zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID);
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	/*
10680Sstevel@tonic-gate 	 * Initialize generic zone resource controls, if any.
10690Sstevel@tonic-gate 	 */
10700Sstevel@tonic-gate 	rc_zone_cpu_shares = rctl_register("zone.cpu-shares",
10710Sstevel@tonic-gate 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER |
10720Sstevel@tonic-gate 	    RCTL_GLOBAL_NOBASIC |
10730Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, FSS_MAXSHARES, FSS_MAXSHARES,
10740Sstevel@tonic-gate 	    &zone_cpu_shares_ops);
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE,
10770Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
10780Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &zone_lwps_ops);
10790Sstevel@tonic-gate 	/*
10800Sstevel@tonic-gate 	 * Create a rctl_val with PRIVILEGED, NOACTION, value = 1.  Then attach
10810Sstevel@tonic-gate 	 * this at the head of the rctl_dict_entry for ``zone.cpu-shares''.
10820Sstevel@tonic-gate 	 */
10830Sstevel@tonic-gate 	dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
10840Sstevel@tonic-gate 	bzero(dval, sizeof (rctl_val_t));
10850Sstevel@tonic-gate 	dval->rcv_value = 1;
10860Sstevel@tonic-gate 	dval->rcv_privilege = RCPRIV_PRIVILEGED;
10870Sstevel@tonic-gate 	dval->rcv_flagaction = RCTL_LOCAL_NOACTION;
10880Sstevel@tonic-gate 	dval->rcv_action_recip_pid = -1;
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	rde = rctl_dict_lookup("zone.cpu-shares");
10910Sstevel@tonic-gate 	(void) rctl_val_list_insert(&rde->rcd_default_value, dval);
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	/*
10940Sstevel@tonic-gate 	 * Initialize the ``global zone''.
10950Sstevel@tonic-gate 	 */
10960Sstevel@tonic-gate 	set = rctl_set_create();
10970Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
10980Sstevel@tonic-gate 	mutex_enter(&p0.p_lock);
10990Sstevel@tonic-gate 	e.rcep_p.zone = &zone0;
11000Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
11010Sstevel@tonic-gate 	zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set,
11020Sstevel@tonic-gate 	    gp);
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 	zone0.zone_nlwps = p0.p_lwpcnt;
11050Sstevel@tonic-gate 	zone0.zone_ntasks = 1;
11060Sstevel@tonic-gate 	mutex_exit(&p0.p_lock);
11070Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
11080Sstevel@tonic-gate 	/*
11090Sstevel@tonic-gate 	 * pool_default hasn't been initialized yet, so we let pool_init() take
11100Sstevel@tonic-gate 	 * care of making the global zone is in the default pool.
11110Sstevel@tonic-gate 	 */
1112*1676Sjpk 
1113*1676Sjpk 	/*
1114*1676Sjpk 	 * Initialize zone label.
1115*1676Sjpk 	 * mlp are initialized when tnzonecfg is loaded.
1116*1676Sjpk 	 */
1117*1676Sjpk 	zone0.zone_slabel = l_admin_low;
1118*1676Sjpk 	rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
1119*1676Sjpk 	label_hold(l_admin_low);
1120*1676Sjpk 
11210Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
11220Sstevel@tonic-gate 	zone_uniqid(&zone0);
11230Sstevel@tonic-gate 	ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID);
1124*1676Sjpk 
11250Sstevel@tonic-gate 	zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size,
11260Sstevel@tonic-gate 	    mod_hash_null_valdtor);
11270Sstevel@tonic-gate 	zonehashbyname = mod_hash_create_strhash("zone_by_name",
11280Sstevel@tonic-gate 	    zone_hash_size, mod_hash_null_valdtor);
1129*1676Sjpk 	/*
1130*1676Sjpk 	 * maintain zonehashbylabel only for labeled systems
1131*1676Sjpk 	 */
1132*1676Sjpk 	if (is_system_labeled())
1133*1676Sjpk 		zonehashbylabel = mod_hash_create_extended("zone_by_label",
1134*1676Sjpk 		    zone_hash_size, mod_hash_null_keydtor,
1135*1676Sjpk 		    mod_hash_null_valdtor, hash_bylabel, NULL,
1136*1676Sjpk 		    hash_labelkey_cmp, KM_SLEEP);
11370Sstevel@tonic-gate 	zonecount = 1;
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID,
11400Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
11410Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name,
11420Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
1143*1676Sjpk 	if (is_system_labeled())
1144*1676Sjpk 		(void) mod_hash_insert(zonehashbylabel,
1145*1676Sjpk 		    (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0);
1146*1676Sjpk 	mutex_exit(&zonehash_lock);
1147*1676Sjpk 
11480Sstevel@tonic-gate 	/*
11490Sstevel@tonic-gate 	 * We avoid setting zone_kcred until now, since kcred is initialized
11500Sstevel@tonic-gate 	 * sometime after zone_zsd_init() and before zone_init().
11510Sstevel@tonic-gate 	 */
11520Sstevel@tonic-gate 	zone0.zone_kcred = kcred;
11530Sstevel@tonic-gate 	/*
11540Sstevel@tonic-gate 	 * The global zone is fully initialized (except for zone_rootvp which
11550Sstevel@tonic-gate 	 * will be set when the root filesystem is mounted).
11560Sstevel@tonic-gate 	 */
11570Sstevel@tonic-gate 	global_zone = &zone0;
11581166Sdstaff 
11591166Sdstaff 	/*
11601166Sdstaff 	 * Setup an event channel to send zone status change notifications on
11611166Sdstaff 	 */
11621166Sdstaff 	res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan,
11631166Sdstaff 	    EVCH_CREAT);
11641166Sdstaff 
11651166Sdstaff 	if (res)
11661166Sdstaff 		panic("Sysevent_evc_bind failed during zone setup.\n");
11670Sstevel@tonic-gate }
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate static void
11700Sstevel@tonic-gate zone_free(zone_t *zone)
11710Sstevel@tonic-gate {
11720Sstevel@tonic-gate 	ASSERT(zone != global_zone);
11730Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks == 0);
11740Sstevel@tonic-gate 	ASSERT(zone->zone_nlwps == 0);
11750Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 0);
11760Sstevel@tonic-gate 	ASSERT(zone->zone_kcred == NULL);
11770Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_DEAD ||
11780Sstevel@tonic-gate 	    zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	/* remove from deathrow list */
11810Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_DEAD) {
11820Sstevel@tonic-gate 		ASSERT(zone->zone_ref == 0);
11830Sstevel@tonic-gate 		mutex_enter(&zone_deathrow_lock);
11840Sstevel@tonic-gate 		list_remove(&zone_deathrow, zone);
11850Sstevel@tonic-gate 		mutex_exit(&zone_deathrow_lock);
11860Sstevel@tonic-gate 	}
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	zone_free_zsd(zone);
1189789Sahrens 	zone_free_datasets(zone);
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	if (zone->zone_rootvp != NULL)
11920Sstevel@tonic-gate 		VN_RELE(zone->zone_rootvp);
11930Sstevel@tonic-gate 	if (zone->zone_rootpath)
11940Sstevel@tonic-gate 		kmem_free(zone->zone_rootpath, zone->zone_rootpathlen);
11950Sstevel@tonic-gate 	if (zone->zone_name != NULL)
11960Sstevel@tonic-gate 		kmem_free(zone->zone_name, ZONENAME_MAX);
1197*1676Sjpk 	if (zone->zone_slabel != NULL)
1198*1676Sjpk 		label_rele(zone->zone_slabel);
11990Sstevel@tonic-gate 	if (zone->zone_nodename != NULL)
12000Sstevel@tonic-gate 		kmem_free(zone->zone_nodename, _SYS_NMLN);
12010Sstevel@tonic-gate 	if (zone->zone_domain != NULL)
12020Sstevel@tonic-gate 		kmem_free(zone->zone_domain, _SYS_NMLN);
12030Sstevel@tonic-gate 	if (zone->zone_privset != NULL)
12040Sstevel@tonic-gate 		kmem_free(zone->zone_privset, sizeof (priv_set_t));
12050Sstevel@tonic-gate 	if (zone->zone_rctls != NULL)
12060Sstevel@tonic-gate 		rctl_set_free(zone->zone_rctls);
12070Sstevel@tonic-gate 	if (zone->zone_bootargs != NULL)
12080Sstevel@tonic-gate 		kmem_free(zone->zone_bootargs, ZONEBOOTARGS_MAX);
12090Sstevel@tonic-gate 	id_free(zoneid_space, zone->zone_id);
12100Sstevel@tonic-gate 	mutex_destroy(&zone->zone_lock);
12110Sstevel@tonic-gate 	cv_destroy(&zone->zone_cv);
1212*1676Sjpk 	rw_destroy(&zone->zone_mlps.mlpl_rwlock);
12130Sstevel@tonic-gate 	kmem_free(zone, sizeof (zone_t));
12140Sstevel@tonic-gate }
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate /*
12170Sstevel@tonic-gate  * See block comment at the top of this file for information about zone
12180Sstevel@tonic-gate  * status values.
12190Sstevel@tonic-gate  */
12200Sstevel@tonic-gate /*
12210Sstevel@tonic-gate  * Convenience function for setting zone status.
12220Sstevel@tonic-gate  */
12230Sstevel@tonic-gate static void
12240Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status)
12250Sstevel@tonic-gate {
12261166Sdstaff 
12271166Sdstaff 	nvlist_t *nvl = NULL;
12280Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zone_status_lock));
12290Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE &&
12300Sstevel@tonic-gate 	    status >= zone_status_get(zone));
12311166Sdstaff 
12321166Sdstaff 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) ||
12331166Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) ||
12341166Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_NEWSTATE,
12351166Sdstaff 		zone_status_table[status]) ||
12361166Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_OLDSTATE,
12371166Sdstaff 		zone_status_table[zone->zone_status]) ||
12381166Sdstaff 	    nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) ||
12391166Sdstaff 	    nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) ||
12401166Sdstaff 	    sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS,
12411166Sdstaff 		ZONE_EVENT_STATUS_SUBCLASS,
12421166Sdstaff 		"sun.com", "kernel", nvl, EVCH_SLEEP)) {
12431166Sdstaff #ifdef DEBUG
12441166Sdstaff 		(void) printf(
12451166Sdstaff 		    "Failed to allocate and send zone state change event.\n");
12461166Sdstaff #endif
12471166Sdstaff 	}
12481166Sdstaff 	nvlist_free(nvl);
12491166Sdstaff 
12500Sstevel@tonic-gate 	zone->zone_status = status;
12511166Sdstaff 
12520Sstevel@tonic-gate 	cv_broadcast(&zone->zone_cv);
12530Sstevel@tonic-gate }
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate /*
12560Sstevel@tonic-gate  * Public function to retrieve the zone status.  The zone status may
12570Sstevel@tonic-gate  * change after it is retrieved.
12580Sstevel@tonic-gate  */
12590Sstevel@tonic-gate zone_status_t
12600Sstevel@tonic-gate zone_status_get(zone_t *zone)
12610Sstevel@tonic-gate {
12620Sstevel@tonic-gate 	return (zone->zone_status);
12630Sstevel@tonic-gate }
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate static int
12660Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs)
12670Sstevel@tonic-gate {
12680Sstevel@tonic-gate 	char *bootargs = kmem_zalloc(ZONEBOOTARGS_MAX, KM_SLEEP);
12690Sstevel@tonic-gate 	size_t len;
12700Sstevel@tonic-gate 	int err;
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 	err = copyinstr(zone_bootargs, bootargs, ZONEBOOTARGS_MAX - 1, &len);
12730Sstevel@tonic-gate 	if (err != 0) {
12740Sstevel@tonic-gate 		kmem_free(bootargs, ZONEBOOTARGS_MAX);
12750Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
12760Sstevel@tonic-gate 	}
12770Sstevel@tonic-gate 	bootargs[len] = '\0';
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	ASSERT(zone->zone_bootargs == NULL);
12800Sstevel@tonic-gate 	zone->zone_bootargs = bootargs;
12810Sstevel@tonic-gate 	return (0);
12820Sstevel@tonic-gate }
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate /*
12850Sstevel@tonic-gate  * Block indefinitely waiting for (zone_status >= status)
12860Sstevel@tonic-gate  */
12870Sstevel@tonic-gate void
12880Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status)
12890Sstevel@tonic-gate {
12900Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
12930Sstevel@tonic-gate 	while (zone->zone_status < status) {
12940Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
12950Sstevel@tonic-gate 	}
12960Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
12970Sstevel@tonic-gate }
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate /*
13000Sstevel@tonic-gate  * Private CPR-safe version of zone_status_wait().
13010Sstevel@tonic-gate  */
13020Sstevel@tonic-gate static void
13030Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str)
13040Sstevel@tonic-gate {
13050Sstevel@tonic-gate 	callb_cpr_t cprinfo;
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr,
13100Sstevel@tonic-gate 	    str);
13110Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
13120Sstevel@tonic-gate 	while (zone->zone_status < status) {
13130Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
13140Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
13150Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock);
13160Sstevel@tonic-gate 	}
13170Sstevel@tonic-gate 	/*
13180Sstevel@tonic-gate 	 * zone_status_lock is implicitly released by the following.
13190Sstevel@tonic-gate 	 */
13200Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
13210Sstevel@tonic-gate }
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate /*
13240Sstevel@tonic-gate  * Block until zone enters requested state or signal is received.  Return (0)
13250Sstevel@tonic-gate  * if signaled, non-zero otherwise.
13260Sstevel@tonic-gate  */
13270Sstevel@tonic-gate int
13280Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status)
13290Sstevel@tonic-gate {
13300Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
13330Sstevel@tonic-gate 	while (zone->zone_status < status) {
13340Sstevel@tonic-gate 		if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) {
13350Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
13360Sstevel@tonic-gate 			return (0);
13370Sstevel@tonic-gate 		}
13380Sstevel@tonic-gate 	}
13390Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
13400Sstevel@tonic-gate 	return (1);
13410Sstevel@tonic-gate }
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate /*
13440Sstevel@tonic-gate  * Block until the zone enters the requested state or the timeout expires,
13450Sstevel@tonic-gate  * whichever happens first.  Return (-1) if operation timed out, time remaining
13460Sstevel@tonic-gate  * otherwise.
13470Sstevel@tonic-gate  */
13480Sstevel@tonic-gate clock_t
13490Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status)
13500Sstevel@tonic-gate {
13510Sstevel@tonic-gate 	clock_t timeleft = 0;
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
13560Sstevel@tonic-gate 	while (zone->zone_status < status && timeleft != -1) {
13570Sstevel@tonic-gate 		timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim);
13580Sstevel@tonic-gate 	}
13590Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
13600Sstevel@tonic-gate 	return (timeleft);
13610Sstevel@tonic-gate }
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate /*
13640Sstevel@tonic-gate  * Block until the zone enters the requested state, the current process is
13650Sstevel@tonic-gate  * signaled,  or the timeout expires, whichever happens first.  Return (-1) if
13660Sstevel@tonic-gate  * operation timed out, 0 if signaled, time remaining otherwise.
13670Sstevel@tonic-gate  */
13680Sstevel@tonic-gate clock_t
13690Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status)
13700Sstevel@tonic-gate {
13710Sstevel@tonic-gate 	clock_t timeleft = tim - lbolt;
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
13760Sstevel@tonic-gate 	while (zone->zone_status < status) {
13770Sstevel@tonic-gate 		timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock,
13780Sstevel@tonic-gate 		    tim);
13790Sstevel@tonic-gate 		if (timeleft <= 0)
13800Sstevel@tonic-gate 			break;
13810Sstevel@tonic-gate 	}
13820Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
13830Sstevel@tonic-gate 	return (timeleft);
13840Sstevel@tonic-gate }
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate /*
13870Sstevel@tonic-gate  * Zones have two reference counts: one for references from credential
13880Sstevel@tonic-gate  * structures (zone_cred_ref), and one (zone_ref) for everything else.
13890Sstevel@tonic-gate  * This is so we can allow a zone to be rebooted while there are still
13900Sstevel@tonic-gate  * outstanding cred references, since certain drivers cache dblks (which
13910Sstevel@tonic-gate  * implicitly results in cached creds).  We wait for zone_ref to drop to
13920Sstevel@tonic-gate  * 0 (actually 1), but not zone_cred_ref.  The zone structure itself is
13930Sstevel@tonic-gate  * later freed when the zone_cred_ref drops to 0, though nothing other
13940Sstevel@tonic-gate  * than the zone id and privilege set should be accessed once the zone
13950Sstevel@tonic-gate  * is "dead".
13960Sstevel@tonic-gate  *
13970Sstevel@tonic-gate  * A debugging flag, zone_wait_for_cred, can be set to a non-zero value
13980Sstevel@tonic-gate  * to force halt/reboot to block waiting for the zone_cred_ref to drop
13990Sstevel@tonic-gate  * to 0.  This can be useful to flush out other sources of cached creds
14000Sstevel@tonic-gate  * that may be less innocuous than the driver case.
14010Sstevel@tonic-gate  */
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate int zone_wait_for_cred = 0;
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate static void
14060Sstevel@tonic-gate zone_hold_locked(zone_t *z)
14070Sstevel@tonic-gate {
14080Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&z->zone_lock));
14090Sstevel@tonic-gate 	z->zone_ref++;
14100Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
14110Sstevel@tonic-gate }
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate void
14140Sstevel@tonic-gate zone_hold(zone_t *z)
14150Sstevel@tonic-gate {
14160Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
14170Sstevel@tonic-gate 	zone_hold_locked(z);
14180Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
14190Sstevel@tonic-gate }
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate /*
14220Sstevel@tonic-gate  * If the non-cred ref count drops to 1 and either the cred ref count
14230Sstevel@tonic-gate  * is 0 or we aren't waiting for cred references, the zone is ready to
14240Sstevel@tonic-gate  * be destroyed.
14250Sstevel@tonic-gate  */
14260Sstevel@tonic-gate #define	ZONE_IS_UNREF(zone)	((zone)->zone_ref == 1 && \
14270Sstevel@tonic-gate 	    (!zone_wait_for_cred || (zone)->zone_cred_ref == 0))
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate void
14300Sstevel@tonic-gate zone_rele(zone_t *z)
14310Sstevel@tonic-gate {
14320Sstevel@tonic-gate 	boolean_t wakeup;
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
14350Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
14360Sstevel@tonic-gate 	z->zone_ref--;
14370Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
14380Sstevel@tonic-gate 		/* no more refs, free the structure */
14390Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
14400Sstevel@tonic-gate 		zone_free(z);
14410Sstevel@tonic-gate 		return;
14420Sstevel@tonic-gate 	}
14430Sstevel@tonic-gate 	/* signal zone_destroy so the zone can finish halting */
14440Sstevel@tonic-gate 	wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD);
14450Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	if (wakeup) {
14480Sstevel@tonic-gate 		/*
14490Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
14500Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
14510Sstevel@tonic-gate 		 */
14520Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
14530Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
14540Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
14550Sstevel@tonic-gate 	}
14560Sstevel@tonic-gate }
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate void
14590Sstevel@tonic-gate zone_cred_hold(zone_t *z)
14600Sstevel@tonic-gate {
14610Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
14620Sstevel@tonic-gate 	z->zone_cred_ref++;
14630Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
14640Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
14650Sstevel@tonic-gate }
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate void
14680Sstevel@tonic-gate zone_cred_rele(zone_t *z)
14690Sstevel@tonic-gate {
14700Sstevel@tonic-gate 	boolean_t wakeup;
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
14730Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
14740Sstevel@tonic-gate 	z->zone_cred_ref--;
14750Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
14760Sstevel@tonic-gate 		/* no more refs, free the structure */
14770Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
14780Sstevel@tonic-gate 		zone_free(z);
14790Sstevel@tonic-gate 		return;
14800Sstevel@tonic-gate 	}
14810Sstevel@tonic-gate 	/*
14820Sstevel@tonic-gate 	 * If zone_destroy is waiting for the cred references to drain
14830Sstevel@tonic-gate 	 * out, and they have, signal it.
14840Sstevel@tonic-gate 	 */
14850Sstevel@tonic-gate 	wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) &&
14860Sstevel@tonic-gate 	    zone_status_get(z) >= ZONE_IS_DEAD);
14870Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 	if (wakeup) {
14900Sstevel@tonic-gate 		/*
14910Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
14920Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
14930Sstevel@tonic-gate 		 */
14940Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
14950Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
14960Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
14970Sstevel@tonic-gate 	}
14980Sstevel@tonic-gate }
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate void
15010Sstevel@tonic-gate zone_task_hold(zone_t *z)
15020Sstevel@tonic-gate {
15030Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
15040Sstevel@tonic-gate 	z->zone_ntasks++;
15050Sstevel@tonic-gate 	ASSERT(z->zone_ntasks != 0);
15060Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
15070Sstevel@tonic-gate }
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate void
15100Sstevel@tonic-gate zone_task_rele(zone_t *zone)
15110Sstevel@tonic-gate {
15120Sstevel@tonic-gate 	uint_t refcnt;
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
15150Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks != 0);
15160Sstevel@tonic-gate 	refcnt = --zone->zone_ntasks;
15170Sstevel@tonic-gate 	if (refcnt > 1)	{	/* Common case */
15180Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
15190Sstevel@tonic-gate 		return;
15200Sstevel@tonic-gate 	}
15210Sstevel@tonic-gate 	zone_hold_locked(zone);	/* so we can use the zone_t later */
15220Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
15230Sstevel@tonic-gate 	if (refcnt == 1) {
15240Sstevel@tonic-gate 		/*
15250Sstevel@tonic-gate 		 * See if the zone is shutting down.
15260Sstevel@tonic-gate 		 */
15270Sstevel@tonic-gate 		mutex_enter(&zone_status_lock);
15280Sstevel@tonic-gate 		if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) {
15290Sstevel@tonic-gate 			goto out;
15300Sstevel@tonic-gate 		}
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 		/*
15330Sstevel@tonic-gate 		 * Make sure the ntasks didn't change since we
15340Sstevel@tonic-gate 		 * dropped zone_lock.
15350Sstevel@tonic-gate 		 */
15360Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
15370Sstevel@tonic-gate 		if (refcnt != zone->zone_ntasks) {
15380Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
15390Sstevel@tonic-gate 			goto out;
15400Sstevel@tonic-gate 		}
15410Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 		/*
15440Sstevel@tonic-gate 		 * No more user processes in the zone.  The zone is empty.
15450Sstevel@tonic-gate 		 */
15460Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_EMPTY);
15470Sstevel@tonic-gate 		goto out;
15480Sstevel@tonic-gate 	}
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate 	ASSERT(refcnt == 0);
15510Sstevel@tonic-gate 	/*
15520Sstevel@tonic-gate 	 * zsched has exited; the zone is dead.
15530Sstevel@tonic-gate 	 */
15540Sstevel@tonic-gate 	zone->zone_zsched = NULL;		/* paranoia */
15550Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
15560Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_DEAD);
15570Sstevel@tonic-gate out:
15580Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
15590Sstevel@tonic-gate 	zone_rele(zone);
15600Sstevel@tonic-gate }
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate zoneid_t
15630Sstevel@tonic-gate getzoneid(void)
15640Sstevel@tonic-gate {
15650Sstevel@tonic-gate 	return (curproc->p_zone->zone_id);
15660Sstevel@tonic-gate }
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate /*
15690Sstevel@tonic-gate  * Internal versions of zone_find_by_*().  These don't zone_hold() or
15700Sstevel@tonic-gate  * check the validity of a zone's state.
15710Sstevel@tonic-gate  */
15720Sstevel@tonic-gate static zone_t *
15730Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid)
15740Sstevel@tonic-gate {
15750Sstevel@tonic-gate 	mod_hash_val_t hv;
15760Sstevel@tonic-gate 	zone_t *zone = NULL;
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyid,
15810Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0)
15820Sstevel@tonic-gate 		zone = (zone_t *)hv;
15830Sstevel@tonic-gate 	return (zone);
15840Sstevel@tonic-gate }
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate static zone_t *
1587*1676Sjpk zone_find_all_by_label(const ts_label_t *label)
1588*1676Sjpk {
1589*1676Sjpk 	mod_hash_val_t hv;
1590*1676Sjpk 	zone_t *zone = NULL;
1591*1676Sjpk 
1592*1676Sjpk 	ASSERT(MUTEX_HELD(&zonehash_lock));
1593*1676Sjpk 
1594*1676Sjpk 	/*
1595*1676Sjpk 	 * zonehashbylabel is not maintained for unlabeled systems
1596*1676Sjpk 	 */
1597*1676Sjpk 	if (!is_system_labeled())
1598*1676Sjpk 		return (NULL);
1599*1676Sjpk 	if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0)
1600*1676Sjpk 		zone = (zone_t *)hv;
1601*1676Sjpk 	return (zone);
1602*1676Sjpk }
1603*1676Sjpk 
1604*1676Sjpk static zone_t *
16050Sstevel@tonic-gate zone_find_all_by_name(char *name)
16060Sstevel@tonic-gate {
16070Sstevel@tonic-gate 	mod_hash_val_t hv;
16080Sstevel@tonic-gate 	zone_t *zone = NULL;
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0)
16130Sstevel@tonic-gate 		zone = (zone_t *)hv;
16140Sstevel@tonic-gate 	return (zone);
16150Sstevel@tonic-gate }
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate /*
16180Sstevel@tonic-gate  * Public interface for looking up a zone by zoneid.  Only returns the zone if
16190Sstevel@tonic-gate  * it is fully initialized, and has not yet begun the zone_destroy() sequence.
16200Sstevel@tonic-gate  * Caller must call zone_rele() once it is done with the zone.
16210Sstevel@tonic-gate  *
16220Sstevel@tonic-gate  * The zone may begin the zone_destroy() sequence immediately after this
16230Sstevel@tonic-gate  * function returns, but may be safely used until zone_rele() is called.
16240Sstevel@tonic-gate  */
16250Sstevel@tonic-gate zone_t *
16260Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid)
16270Sstevel@tonic-gate {
16280Sstevel@tonic-gate 	zone_t *zone;
16290Sstevel@tonic-gate 	zone_status_t status;
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
16320Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
16330Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
16340Sstevel@tonic-gate 		return (NULL);
16350Sstevel@tonic-gate 	}
16360Sstevel@tonic-gate 	status = zone_status_get(zone);
16370Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
16380Sstevel@tonic-gate 		/*
16390Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
16400Sstevel@tonic-gate 		 */
16410Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
16420Sstevel@tonic-gate 		return (NULL);
16430Sstevel@tonic-gate 	}
16440Sstevel@tonic-gate 	zone_hold(zone);
16450Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
16460Sstevel@tonic-gate 	return (zone);
16470Sstevel@tonic-gate }
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate /*
1650*1676Sjpk  * Similar to zone_find_by_id, but using zone label as the key.
1651*1676Sjpk  */
1652*1676Sjpk zone_t *
1653*1676Sjpk zone_find_by_label(const ts_label_t *label)
1654*1676Sjpk {
1655*1676Sjpk 	zone_t *zone;
1656*1676Sjpk 
1657*1676Sjpk 	mutex_enter(&zonehash_lock);
1658*1676Sjpk 	if ((zone = zone_find_all_by_label(label)) == NULL) {
1659*1676Sjpk 		mutex_exit(&zonehash_lock);
1660*1676Sjpk 		return (NULL);
1661*1676Sjpk 	}
1662*1676Sjpk 	mutex_enter(&zone_status_lock);
1663*1676Sjpk 	if (zone_status_get(zone) > ZONE_IS_DOWN) {
1664*1676Sjpk 		/*
1665*1676Sjpk 		 * For all practical purposes the zone doesn't exist.
1666*1676Sjpk 		 */
1667*1676Sjpk 		mutex_exit(&zone_status_lock);
1668*1676Sjpk 		zone = NULL;
1669*1676Sjpk 	} else {
1670*1676Sjpk 		mutex_exit(&zone_status_lock);
1671*1676Sjpk 		zone_hold(zone);
1672*1676Sjpk 	}
1673*1676Sjpk 	mutex_exit(&zonehash_lock);
1674*1676Sjpk 	return (zone);
1675*1676Sjpk }
1676*1676Sjpk 
1677*1676Sjpk /*
16780Sstevel@tonic-gate  * Similar to zone_find_by_id, but using zone name as the key.
16790Sstevel@tonic-gate  */
16800Sstevel@tonic-gate zone_t *
16810Sstevel@tonic-gate zone_find_by_name(char *name)
16820Sstevel@tonic-gate {
16830Sstevel@tonic-gate 	zone_t *zone;
16840Sstevel@tonic-gate 	zone_status_t status;
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
16870Sstevel@tonic-gate 	if ((zone = zone_find_all_by_name(name)) == NULL) {
16880Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
16890Sstevel@tonic-gate 		return (NULL);
16900Sstevel@tonic-gate 	}
16910Sstevel@tonic-gate 	status = zone_status_get(zone);
16920Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
16930Sstevel@tonic-gate 		/*
16940Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
16950Sstevel@tonic-gate 		 */
16960Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
16970Sstevel@tonic-gate 		return (NULL);
16980Sstevel@tonic-gate 	}
16990Sstevel@tonic-gate 	zone_hold(zone);
17000Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
17010Sstevel@tonic-gate 	return (zone);
17020Sstevel@tonic-gate }
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate /*
17050Sstevel@tonic-gate  * Similar to zone_find_by_id(), using the path as a key.  For instance,
17060Sstevel@tonic-gate  * if there is a zone "foo" rooted at /foo/root, and the path argument
17070Sstevel@tonic-gate  * is "/foo/root/proc", it will return the held zone_t corresponding to
17080Sstevel@tonic-gate  * zone "foo".
17090Sstevel@tonic-gate  *
17100Sstevel@tonic-gate  * zone_find_by_path() always returns a non-NULL value, since at the
17110Sstevel@tonic-gate  * very least every path will be contained in the global zone.
17120Sstevel@tonic-gate  *
17130Sstevel@tonic-gate  * As with the other zone_find_by_*() functions, the caller is
17140Sstevel@tonic-gate  * responsible for zone_rele()ing the return value of this function.
17150Sstevel@tonic-gate  */
17160Sstevel@tonic-gate zone_t *
17170Sstevel@tonic-gate zone_find_by_path(const char *path)
17180Sstevel@tonic-gate {
17190Sstevel@tonic-gate 	zone_t *zone;
17200Sstevel@tonic-gate 	zone_t *zret = NULL;
17210Sstevel@tonic-gate 	zone_status_t status;
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	if (path == NULL) {
17240Sstevel@tonic-gate 		/*
17250Sstevel@tonic-gate 		 * Call from rootconf().
17260Sstevel@tonic-gate 		 */
17270Sstevel@tonic-gate 		zone_hold(global_zone);
17280Sstevel@tonic-gate 		return (global_zone);
17290Sstevel@tonic-gate 	}
17300Sstevel@tonic-gate 	ASSERT(*path == '/');
17310Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
17320Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
17330Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
17340Sstevel@tonic-gate 		if (ZONE_PATH_VISIBLE(path, zone))
17350Sstevel@tonic-gate 			zret = zone;
17360Sstevel@tonic-gate 	}
17370Sstevel@tonic-gate 	ASSERT(zret != NULL);
17380Sstevel@tonic-gate 	status = zone_status_get(zret);
17390Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
17400Sstevel@tonic-gate 		/*
17410Sstevel@tonic-gate 		 * Zone practically doesn't exist.
17420Sstevel@tonic-gate 		 */
17430Sstevel@tonic-gate 		zret = global_zone;
17440Sstevel@tonic-gate 	}
17450Sstevel@tonic-gate 	zone_hold(zret);
17460Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
17470Sstevel@tonic-gate 	return (zret);
17480Sstevel@tonic-gate }
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate /*
17510Sstevel@tonic-gate  * Get the number of cpus visible to this zone.  The system-wide global
17520Sstevel@tonic-gate  * 'ncpus' is returned if pools are disabled, the caller is in the
17530Sstevel@tonic-gate  * global zone, or a NULL zone argument is passed in.
17540Sstevel@tonic-gate  */
17550Sstevel@tonic-gate int
17560Sstevel@tonic-gate zone_ncpus_get(zone_t *zone)
17570Sstevel@tonic-gate {
17580Sstevel@tonic-gate 	int myncpus = zone == NULL ? 0 : zone->zone_ncpus;
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 	return (myncpus != 0 ? myncpus : ncpus);
17610Sstevel@tonic-gate }
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate /*
17640Sstevel@tonic-gate  * Get the number of online cpus visible to this zone.  The system-wide
17650Sstevel@tonic-gate  * global 'ncpus_online' is returned if pools are disabled, the caller
17660Sstevel@tonic-gate  * is in the global zone, or a NULL zone argument is passed in.
17670Sstevel@tonic-gate  */
17680Sstevel@tonic-gate int
17690Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone)
17700Sstevel@tonic-gate {
17710Sstevel@tonic-gate 	int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online;
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate 	return (myncpus_online != 0 ? myncpus_online : ncpus_online);
17740Sstevel@tonic-gate }
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate /*
17770Sstevel@tonic-gate  * Return the pool to which the zone is currently bound.
17780Sstevel@tonic-gate  */
17790Sstevel@tonic-gate pool_t *
17800Sstevel@tonic-gate zone_pool_get(zone_t *zone)
17810Sstevel@tonic-gate {
17820Sstevel@tonic-gate 	ASSERT(pool_lock_held());
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 	return (zone->zone_pool);
17850Sstevel@tonic-gate }
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate /*
17880Sstevel@tonic-gate  * Set the zone's pool pointer and update the zone's visibility to match
17890Sstevel@tonic-gate  * the resources in the new pool.
17900Sstevel@tonic-gate  */
17910Sstevel@tonic-gate void
17920Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool)
17930Sstevel@tonic-gate {
17940Sstevel@tonic-gate 	ASSERT(pool_lock_held());
17950Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 	zone->zone_pool = pool;
17980Sstevel@tonic-gate 	zone_pset_set(zone, pool->pool_pset->pset_id);
17990Sstevel@tonic-gate }
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate /*
18020Sstevel@tonic-gate  * Return the cached value of the id of the processor set to which the
18030Sstevel@tonic-gate  * zone is currently bound.  The value will be ZONE_PS_INVAL if the pools
18040Sstevel@tonic-gate  * facility is disabled.
18050Sstevel@tonic-gate  */
18060Sstevel@tonic-gate psetid_t
18070Sstevel@tonic-gate zone_pset_get(zone_t *zone)
18080Sstevel@tonic-gate {
18090Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 	return (zone->zone_psetid);
18120Sstevel@tonic-gate }
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate /*
18150Sstevel@tonic-gate  * Set the cached value of the id of the processor set to which the zone
18160Sstevel@tonic-gate  * is currently bound.  Also update the zone's visibility to match the
18170Sstevel@tonic-gate  * resources in the new processor set.
18180Sstevel@tonic-gate  */
18190Sstevel@tonic-gate void
18200Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid)
18210Sstevel@tonic-gate {
18220Sstevel@tonic-gate 	psetid_t oldpsetid;
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
18250Sstevel@tonic-gate 	oldpsetid = zone_pset_get(zone);
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 	if (oldpsetid == newpsetid)
18280Sstevel@tonic-gate 		return;
18290Sstevel@tonic-gate 	/*
18300Sstevel@tonic-gate 	 * Global zone sees all.
18310Sstevel@tonic-gate 	 */
18320Sstevel@tonic-gate 	if (zone != global_zone) {
18330Sstevel@tonic-gate 		zone->zone_psetid = newpsetid;
18340Sstevel@tonic-gate 		if (newpsetid != ZONE_PS_INVAL)
18350Sstevel@tonic-gate 			pool_pset_visibility_add(newpsetid, zone);
18360Sstevel@tonic-gate 		if (oldpsetid != ZONE_PS_INVAL)
18370Sstevel@tonic-gate 			pool_pset_visibility_remove(oldpsetid, zone);
18380Sstevel@tonic-gate 	}
18390Sstevel@tonic-gate 	/*
18400Sstevel@tonic-gate 	 * Disabling pools, so we should start using the global values
18410Sstevel@tonic-gate 	 * for ncpus and ncpus_online.
18420Sstevel@tonic-gate 	 */
18430Sstevel@tonic-gate 	if (newpsetid == ZONE_PS_INVAL) {
18440Sstevel@tonic-gate 		zone->zone_ncpus = 0;
18450Sstevel@tonic-gate 		zone->zone_ncpus_online = 0;
18460Sstevel@tonic-gate 	}
18470Sstevel@tonic-gate }
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate /*
18500Sstevel@tonic-gate  * Walk the list of active zones and issue the provided callback for
18510Sstevel@tonic-gate  * each of them.
18520Sstevel@tonic-gate  *
18530Sstevel@tonic-gate  * Caller must not be holding any locks that may be acquired under
18540Sstevel@tonic-gate  * zonehash_lock.  See comment at the beginning of the file for a list of
18550Sstevel@tonic-gate  * common locks and their interactions with zones.
18560Sstevel@tonic-gate  */
18570Sstevel@tonic-gate int
18580Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data)
18590Sstevel@tonic-gate {
18600Sstevel@tonic-gate 	zone_t *zone;
18610Sstevel@tonic-gate 	int ret = 0;
18620Sstevel@tonic-gate 	zone_status_t status;
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
18650Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
18660Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
18670Sstevel@tonic-gate 		/*
18680Sstevel@tonic-gate 		 * Skip zones that shouldn't be externally visible.
18690Sstevel@tonic-gate 		 */
18700Sstevel@tonic-gate 		status = zone_status_get(zone);
18710Sstevel@tonic-gate 		if (status < ZONE_IS_READY || status > ZONE_IS_DOWN)
18720Sstevel@tonic-gate 			continue;
18730Sstevel@tonic-gate 		/*
18740Sstevel@tonic-gate 		 * Bail immediately if any callback invocation returns a
18750Sstevel@tonic-gate 		 * non-zero value.
18760Sstevel@tonic-gate 		 */
18770Sstevel@tonic-gate 		ret = (*cb)(zone, data);
18780Sstevel@tonic-gate 		if (ret != 0)
18790Sstevel@tonic-gate 			break;
18800Sstevel@tonic-gate 	}
18810Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
18820Sstevel@tonic-gate 	return (ret);
18830Sstevel@tonic-gate }
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate static int
18860Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath)
18870Sstevel@tonic-gate {
18880Sstevel@tonic-gate 	vnode_t *vp;
18890Sstevel@tonic-gate 	int trycount;
18900Sstevel@tonic-gate 	int error = 0;
18910Sstevel@tonic-gate 	char *path;
18920Sstevel@tonic-gate 	struct pathname upn, pn;
18930Sstevel@tonic-gate 	size_t pathlen;
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 	if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0)
18960Sstevel@tonic-gate 		return (error);
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	pn_alloc(&pn);
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	/* prevent infinite loop */
19010Sstevel@tonic-gate 	trycount = 10;
19020Sstevel@tonic-gate 	for (;;) {
19030Sstevel@tonic-gate 		if (--trycount <= 0) {
19040Sstevel@tonic-gate 			error = ESTALE;
19050Sstevel@tonic-gate 			goto out;
19060Sstevel@tonic-gate 		}
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 		if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) {
19090Sstevel@tonic-gate 			/*
19100Sstevel@tonic-gate 			 * VOP_ACCESS() may cover 'vp' with a new
19110Sstevel@tonic-gate 			 * filesystem, if 'vp' is an autoFS vnode.
19120Sstevel@tonic-gate 			 * Get the new 'vp' if so.
19130Sstevel@tonic-gate 			 */
19140Sstevel@tonic-gate 			if ((error = VOP_ACCESS(vp, VEXEC, 0, CRED())) == 0 &&
19150Sstevel@tonic-gate 			    (vp->v_vfsmountedhere == NULL ||
19160Sstevel@tonic-gate 			    (error = traverse(&vp)) == 0)) {
19170Sstevel@tonic-gate 				pathlen = pn.pn_pathlen + 2;
19180Sstevel@tonic-gate 				path = kmem_alloc(pathlen, KM_SLEEP);
19190Sstevel@tonic-gate 				(void) strncpy(path, pn.pn_path,
19200Sstevel@tonic-gate 				    pn.pn_pathlen + 1);
19210Sstevel@tonic-gate 				path[pathlen - 2] = '/';
19220Sstevel@tonic-gate 				path[pathlen - 1] = '\0';
19230Sstevel@tonic-gate 				pn_free(&pn);
19240Sstevel@tonic-gate 				pn_free(&upn);
19250Sstevel@tonic-gate 
19260Sstevel@tonic-gate 				/* Success! */
19270Sstevel@tonic-gate 				break;
19280Sstevel@tonic-gate 			}
19290Sstevel@tonic-gate 			VN_RELE(vp);
19300Sstevel@tonic-gate 		}
19310Sstevel@tonic-gate 		if (error != ESTALE)
19320Sstevel@tonic-gate 			goto out;
19330Sstevel@tonic-gate 	}
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 	ASSERT(error == 0);
19360Sstevel@tonic-gate 	zone->zone_rootvp = vp;		/* we hold a reference to vp */
19370Sstevel@tonic-gate 	zone->zone_rootpath = path;
19380Sstevel@tonic-gate 	zone->zone_rootpathlen = pathlen;
19390Sstevel@tonic-gate 	return (0);
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate out:
19420Sstevel@tonic-gate 	pn_free(&pn);
19430Sstevel@tonic-gate 	pn_free(&upn);
19440Sstevel@tonic-gate 	return (error);
19450Sstevel@tonic-gate }
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate #define	isalnum(c)	(((c) >= '0' && (c) <= '9') || \
19480Sstevel@tonic-gate 			((c) >= 'a' && (c) <= 'z') || \
19490Sstevel@tonic-gate 			((c) >= 'A' && (c) <= 'Z'))
19500Sstevel@tonic-gate 
19510Sstevel@tonic-gate static int
19520Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname)
19530Sstevel@tonic-gate {
19540Sstevel@tonic-gate 	char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
19550Sstevel@tonic-gate 	size_t len;
19560Sstevel@tonic-gate 	int i, err;
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) {
19590Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
19600Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
19610Sstevel@tonic-gate 	}
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 	/* must be less than ZONENAME_MAX */
19640Sstevel@tonic-gate 	if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') {
19650Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
19660Sstevel@tonic-gate 		return (EINVAL);
19670Sstevel@tonic-gate 	}
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	/*
19700Sstevel@tonic-gate 	 * Name must start with an alphanumeric and must contain only
19710Sstevel@tonic-gate 	 * alphanumerics, '-', '_' and '.'.
19720Sstevel@tonic-gate 	 */
19730Sstevel@tonic-gate 	if (!isalnum(kname[0])) {
19740Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
19750Sstevel@tonic-gate 		return (EINVAL);
19760Sstevel@tonic-gate 	}
19770Sstevel@tonic-gate 	for (i = 1; i < len - 1; i++) {
19780Sstevel@tonic-gate 		if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' &&
19790Sstevel@tonic-gate 		    kname[i] != '.') {
19800Sstevel@tonic-gate 			kmem_free(kname, ZONENAME_MAX);
19810Sstevel@tonic-gate 			return (EINVAL);
19820Sstevel@tonic-gate 		}
19830Sstevel@tonic-gate 	}
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 	zone->zone_name = kname;
19860Sstevel@tonic-gate 	return (0);
19870Sstevel@tonic-gate }
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate /*
19900Sstevel@tonic-gate  * Similar to thread_create(), but makes sure the thread is in the appropriate
19910Sstevel@tonic-gate  * zone's zsched process (curproc->p_zone->zone_zsched) before returning.
19920Sstevel@tonic-gate  */
19930Sstevel@tonic-gate /*ARGSUSED*/
19940Sstevel@tonic-gate kthread_t *
19950Sstevel@tonic-gate zthread_create(
19960Sstevel@tonic-gate     caddr_t stk,
19970Sstevel@tonic-gate     size_t stksize,
19980Sstevel@tonic-gate     void (*proc)(),
19990Sstevel@tonic-gate     void *arg,
20000Sstevel@tonic-gate     size_t len,
20010Sstevel@tonic-gate     pri_t pri)
20020Sstevel@tonic-gate {
20030Sstevel@tonic-gate 	kthread_t *t;
20040Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
20050Sstevel@tonic-gate 	proc_t *pp = zone->zone_zsched;
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 	zone_hold(zone);	/* Reference to be dropped when thread exits */
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 	/*
20100Sstevel@tonic-gate 	 * No-one should be trying to create threads if the zone is shutting
20110Sstevel@tonic-gate 	 * down and there aren't any kernel threads around.  See comment
20120Sstevel@tonic-gate 	 * in zthread_exit().
20130Sstevel@tonic-gate 	 */
20140Sstevel@tonic-gate 	ASSERT(!(zone->zone_kthreads == NULL &&
20150Sstevel@tonic-gate 	    zone_status_get(zone) >= ZONE_IS_EMPTY));
20160Sstevel@tonic-gate 	/*
20170Sstevel@tonic-gate 	 * Create a thread, but don't let it run until we've finished setting
20180Sstevel@tonic-gate 	 * things up.
20190Sstevel@tonic-gate 	 */
20200Sstevel@tonic-gate 	t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri);
20210Sstevel@tonic-gate 	ASSERT(t->t_forw == NULL);
20220Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
20230Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL) {
20240Sstevel@tonic-gate 		t->t_forw = t->t_back = t;
20250Sstevel@tonic-gate 	} else {
20260Sstevel@tonic-gate 		kthread_t *tx = zone->zone_kthreads;
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 		t->t_forw = tx;
20290Sstevel@tonic-gate 		t->t_back = tx->t_back;
20300Sstevel@tonic-gate 		tx->t_back->t_forw = t;
20310Sstevel@tonic-gate 		tx->t_back = t;
20320Sstevel@tonic-gate 	}
20330Sstevel@tonic-gate 	zone->zone_kthreads = t;
20340Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
20370Sstevel@tonic-gate 	t->t_proc_flag |= TP_ZTHREAD;
20380Sstevel@tonic-gate 	project_rele(t->t_proj);
20390Sstevel@tonic-gate 	t->t_proj = project_hold(pp->p_task->tk_proj);
20400Sstevel@tonic-gate 
20410Sstevel@tonic-gate 	/*
20420Sstevel@tonic-gate 	 * Setup complete, let it run.
20430Sstevel@tonic-gate 	 */
20440Sstevel@tonic-gate 	thread_lock(t);
20450Sstevel@tonic-gate 	t->t_schedflag |= TS_ALLSTART;
20460Sstevel@tonic-gate 	setrun_locked(t);
20470Sstevel@tonic-gate 	thread_unlock(t);
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 	return (t);
20520Sstevel@tonic-gate }
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate /*
20550Sstevel@tonic-gate  * Similar to thread_exit().  Must be called by threads created via
20560Sstevel@tonic-gate  * zthread_exit().
20570Sstevel@tonic-gate  */
20580Sstevel@tonic-gate void
20590Sstevel@tonic-gate zthread_exit(void)
20600Sstevel@tonic-gate {
20610Sstevel@tonic-gate 	kthread_t *t = curthread;
20620Sstevel@tonic-gate 	proc_t *pp = curproc;
20630Sstevel@tonic-gate 	zone_t *zone = pp->p_zone;
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 	/*
20680Sstevel@tonic-gate 	 * Reparent to p0
20690Sstevel@tonic-gate 	 */
20701075Sjosephb 	kpreempt_disable();
20710Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
20720Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_ZTHREAD;
20730Sstevel@tonic-gate 	t->t_procp = &p0;
20740Sstevel@tonic-gate 	hat_thread_exit(t);
20750Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
20761075Sjosephb 	kpreempt_enable();
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 	if (t->t_back == t) {
20790Sstevel@tonic-gate 		ASSERT(t->t_forw == t);
20800Sstevel@tonic-gate 		/*
20810Sstevel@tonic-gate 		 * If the zone is empty, once the thread count
20820Sstevel@tonic-gate 		 * goes to zero no further kernel threads can be
20830Sstevel@tonic-gate 		 * created.  This is because if the creator is a process
20840Sstevel@tonic-gate 		 * in the zone, then it must have exited before the zone
20850Sstevel@tonic-gate 		 * state could be set to ZONE_IS_EMPTY.
20860Sstevel@tonic-gate 		 * Otherwise, if the creator is a kernel thread in the
20870Sstevel@tonic-gate 		 * zone, the thread count is non-zero.
20880Sstevel@tonic-gate 		 *
20890Sstevel@tonic-gate 		 * This really means that non-zone kernel threads should
20900Sstevel@tonic-gate 		 * not create zone kernel threads.
20910Sstevel@tonic-gate 		 */
20920Sstevel@tonic-gate 		zone->zone_kthreads = NULL;
20930Sstevel@tonic-gate 		if (zone_status_get(zone) == ZONE_IS_EMPTY) {
20940Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_DOWN);
20950Sstevel@tonic-gate 		}
20960Sstevel@tonic-gate 	} else {
20970Sstevel@tonic-gate 		t->t_forw->t_back = t->t_back;
20980Sstevel@tonic-gate 		t->t_back->t_forw = t->t_forw;
20990Sstevel@tonic-gate 		if (zone->zone_kthreads == t)
21000Sstevel@tonic-gate 			zone->zone_kthreads = t->t_forw;
21010Sstevel@tonic-gate 	}
21020Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
21030Sstevel@tonic-gate 	zone_rele(zone);
21040Sstevel@tonic-gate 	thread_exit();
21050Sstevel@tonic-gate 	/* NOTREACHED */
21060Sstevel@tonic-gate }
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate static void
21090Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp)
21100Sstevel@tonic-gate {
21110Sstevel@tonic-gate 	vnode_t *oldvp;
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 	/* we're going to hold a reference here to the directory */
21140Sstevel@tonic-gate 	VN_HOLD(vp);
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate #ifdef C2_AUDIT
21170Sstevel@tonic-gate 	if (audit_active)	/* update abs cwd/root path see c2audit.c */
21180Sstevel@tonic-gate 		audit_chdirec(vp, vpp);
21190Sstevel@tonic-gate #endif
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
21220Sstevel@tonic-gate 	oldvp = *vpp;
21230Sstevel@tonic-gate 	*vpp = vp;
21240Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
21250Sstevel@tonic-gate 	if (oldvp != NULL)
21260Sstevel@tonic-gate 		VN_RELE(oldvp);
21270Sstevel@tonic-gate }
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate /*
21300Sstevel@tonic-gate  * Convert an rctl value represented by an nvlist_t into an rctl_val_t.
21310Sstevel@tonic-gate  */
21320Sstevel@tonic-gate static int
21330Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv)
21340Sstevel@tonic-gate {
21350Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
21360Sstevel@tonic-gate 	boolean_t priv_set = B_FALSE;
21370Sstevel@tonic-gate 	boolean_t limit_set = B_FALSE;
21380Sstevel@tonic-gate 	boolean_t action_set = B_FALSE;
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
21410Sstevel@tonic-gate 		const char *name;
21420Sstevel@tonic-gate 		uint64_t ui64;
21430Sstevel@tonic-gate 
21440Sstevel@tonic-gate 		name = nvpair_name(nvp);
21450Sstevel@tonic-gate 		if (nvpair_type(nvp) != DATA_TYPE_UINT64)
21460Sstevel@tonic-gate 			return (EINVAL);
21470Sstevel@tonic-gate 		(void) nvpair_value_uint64(nvp, &ui64);
21480Sstevel@tonic-gate 		if (strcmp(name, "privilege") == 0) {
21490Sstevel@tonic-gate 			/*
21500Sstevel@tonic-gate 			 * Currently only privileged values are allowed, but
21510Sstevel@tonic-gate 			 * this may change in the future.
21520Sstevel@tonic-gate 			 */
21530Sstevel@tonic-gate 			if (ui64 != RCPRIV_PRIVILEGED)
21540Sstevel@tonic-gate 				return (EINVAL);
21550Sstevel@tonic-gate 			rv->rcv_privilege = ui64;
21560Sstevel@tonic-gate 			priv_set = B_TRUE;
21570Sstevel@tonic-gate 		} else if (strcmp(name, "limit") == 0) {
21580Sstevel@tonic-gate 			rv->rcv_value = ui64;
21590Sstevel@tonic-gate 			limit_set = B_TRUE;
21600Sstevel@tonic-gate 		} else if (strcmp(name, "action") == 0) {
21610Sstevel@tonic-gate 			if (ui64 != RCTL_LOCAL_NOACTION &&
21620Sstevel@tonic-gate 			    ui64 != RCTL_LOCAL_DENY)
21630Sstevel@tonic-gate 				return (EINVAL);
21640Sstevel@tonic-gate 			rv->rcv_flagaction = ui64;
21650Sstevel@tonic-gate 			action_set = B_TRUE;
21660Sstevel@tonic-gate 		} else {
21670Sstevel@tonic-gate 			return (EINVAL);
21680Sstevel@tonic-gate 		}
21690Sstevel@tonic-gate 	}
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 	if (!(priv_set && limit_set && action_set))
21720Sstevel@tonic-gate 		return (EINVAL);
21730Sstevel@tonic-gate 	rv->rcv_action_signal = 0;
21740Sstevel@tonic-gate 	rv->rcv_action_recipient = NULL;
21750Sstevel@tonic-gate 	rv->rcv_action_recip_pid = -1;
21760Sstevel@tonic-gate 	rv->rcv_firing_time = 0;
21770Sstevel@tonic-gate 
21780Sstevel@tonic-gate 	return (0);
21790Sstevel@tonic-gate }
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate void
21820Sstevel@tonic-gate zone_icode(void)
21830Sstevel@tonic-gate {
21840Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
21850Sstevel@tonic-gate 	struct core_globals	*cg;
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate 	/*
21880Sstevel@tonic-gate 	 * For all purposes (ZONE_ATTR_INITPID and restart_init),
21890Sstevel@tonic-gate 	 * storing just the pid of init is sufficient.
21900Sstevel@tonic-gate 	 */
21910Sstevel@tonic-gate 	p->p_zone->zone_proc_initpid = p->p_pid;
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate 	/*
21940Sstevel@tonic-gate 	 * Allocate user address space and stack segment
21950Sstevel@tonic-gate 	 */
21960Sstevel@tonic-gate 
21970Sstevel@tonic-gate 	p->p_cstime = p->p_stime = p->p_cutime = p->p_utime = 0;
21980Sstevel@tonic-gate 	p->p_usrstack = (caddr_t)USRSTACK32;
21990Sstevel@tonic-gate 	p->p_model = DATAMODEL_ILP32;
22000Sstevel@tonic-gate 	p->p_stkprot = PROT_ZFOD & ~PROT_EXEC;
22010Sstevel@tonic-gate 	p->p_datprot = PROT_ZFOD & ~PROT_EXEC;
22020Sstevel@tonic-gate 	p->p_stk_ctl = INT32_MAX;
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate 	p->p_as = as_alloc();
22050Sstevel@tonic-gate 	p->p_as->a_userlimit = (caddr_t)USERLIMIT32;
22060Sstevel@tonic-gate 	(void) hat_setup(p->p_as->a_hat, HAT_INIT);
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 	cg = zone_getspecific(core_zone_key, p->p_zone);
22090Sstevel@tonic-gate 	ASSERT(cg != NULL);
22100Sstevel@tonic-gate 	corectl_path_hold(cg->core_default_path);
22110Sstevel@tonic-gate 	corectl_content_hold(cg->core_default_content);
22120Sstevel@tonic-gate 	p->p_corefile = cg->core_default_path;
22130Sstevel@tonic-gate 	p->p_content = cg->core_default_content;
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 	init_mstate(curthread, LMS_SYSTEM);
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 	p->p_zone->zone_boot_err = exec_init(zone_initname, 0,
22180Sstevel@tonic-gate 	    p->p_zone->zone_bootargs);
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
22210Sstevel@tonic-gate 	if (p->p_zone->zone_boot_err != 0) {
22220Sstevel@tonic-gate 		/*
22230Sstevel@tonic-gate 		 * Make sure we are still in the booting state-- we could have
22240Sstevel@tonic-gate 		 * raced and already be shutting down, or even further along.
22250Sstevel@tonic-gate 		 */
22260Sstevel@tonic-gate 		if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING)
22270Sstevel@tonic-gate 			zone_status_set(p->p_zone, ZONE_IS_SHUTTING_DOWN);
22280Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
22290Sstevel@tonic-gate 		/* It's gone bad, dispose of the process */
22300Sstevel@tonic-gate 		if (proc_exit(CLD_EXITED, p->p_zone->zone_boot_err) != 0) {
2231390Sraf 			mutex_enter(&p->p_lock);
2232390Sraf 			ASSERT(p->p_flag & SEXITLWPS);
22330Sstevel@tonic-gate 			lwp_exit();
22340Sstevel@tonic-gate 		}
22350Sstevel@tonic-gate 	} else {
22360Sstevel@tonic-gate 		if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING)
22370Sstevel@tonic-gate 			zone_status_set(p->p_zone, ZONE_IS_RUNNING);
22380Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
22390Sstevel@tonic-gate 		/* cause the process to return to userland. */
22400Sstevel@tonic-gate 		lwp_rtt();
22410Sstevel@tonic-gate 	}
22420Sstevel@tonic-gate }
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate struct zsched_arg {
22450Sstevel@tonic-gate 	zone_t *zone;
22460Sstevel@tonic-gate 	nvlist_t *nvlist;
22470Sstevel@tonic-gate };
22480Sstevel@tonic-gate 
22490Sstevel@tonic-gate /*
22500Sstevel@tonic-gate  * Per-zone "sched" workalike.  The similarity to "sched" doesn't have
22510Sstevel@tonic-gate  * anything to do with scheduling, but rather with the fact that
22520Sstevel@tonic-gate  * per-zone kernel threads are parented to zsched, just like regular
22530Sstevel@tonic-gate  * kernel threads are parented to sched (p0).
22540Sstevel@tonic-gate  *
22550Sstevel@tonic-gate  * zsched is also responsible for launching init for the zone.
22560Sstevel@tonic-gate  */
22570Sstevel@tonic-gate static void
22580Sstevel@tonic-gate zsched(void *arg)
22590Sstevel@tonic-gate {
22600Sstevel@tonic-gate 	struct zsched_arg *za = arg;
22610Sstevel@tonic-gate 	proc_t *pp = curproc;
22620Sstevel@tonic-gate 	proc_t *initp = proc_init;
22630Sstevel@tonic-gate 	zone_t *zone = za->zone;
22640Sstevel@tonic-gate 	cred_t *cr, *oldcred;
22650Sstevel@tonic-gate 	rctl_set_t *set;
22660Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
22670Sstevel@tonic-gate 	contract_t *ct = NULL;
22680Sstevel@tonic-gate 	task_t *tk, *oldtk;
22690Sstevel@tonic-gate 	rctl_entity_p_t e;
22700Sstevel@tonic-gate 	kproject_t *pj;
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate 	nvlist_t *nvl = za->nvlist;
22730Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate 	bcopy("zsched", u.u_psargs, sizeof ("zsched"));
22760Sstevel@tonic-gate 	bcopy("zsched", u.u_comm, sizeof ("zsched"));
22770Sstevel@tonic-gate 	u.u_argc = 0;
22780Sstevel@tonic-gate 	u.u_argv = NULL;
22790Sstevel@tonic-gate 	u.u_envp = NULL;
22800Sstevel@tonic-gate 	closeall(P_FINFO(pp));
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate 	/*
22830Sstevel@tonic-gate 	 * We are this zone's "zsched" process.  As the zone isn't generally
22840Sstevel@tonic-gate 	 * visible yet we don't need to grab any locks before initializing its
22850Sstevel@tonic-gate 	 * zone_proc pointer.
22860Sstevel@tonic-gate 	 */
22870Sstevel@tonic-gate 	zone_hold(zone);  /* this hold is released by zone_destroy() */
22880Sstevel@tonic-gate 	zone->zone_zsched = pp;
22890Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
22900Sstevel@tonic-gate 	pp->p_zone = zone;
22910Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate 	/*
22940Sstevel@tonic-gate 	 * Disassociate process from its 'parent'; parent ourselves to init
22950Sstevel@tonic-gate 	 * (pid 1) and change other values as needed.
22960Sstevel@tonic-gate 	 */
22970Sstevel@tonic-gate 	sess_create();
22980Sstevel@tonic-gate 
22990Sstevel@tonic-gate 	mutex_enter(&pidlock);
23000Sstevel@tonic-gate 	proc_detach(pp);
23010Sstevel@tonic-gate 	pp->p_ppid = 1;
23020Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
23030Sstevel@tonic-gate 	pp->p_ancpid = 1;
23040Sstevel@tonic-gate 	pp->p_parent = initp;
23050Sstevel@tonic-gate 	pp->p_psibling = NULL;
23060Sstevel@tonic-gate 	if (initp->p_child)
23070Sstevel@tonic-gate 		initp->p_child->p_psibling = pp;
23080Sstevel@tonic-gate 	pp->p_sibling = initp->p_child;
23090Sstevel@tonic-gate 	initp->p_child = pp;
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 	/* Decrement what newproc() incremented. */
23120Sstevel@tonic-gate 	upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID);
23130Sstevel@tonic-gate 	/*
23140Sstevel@tonic-gate 	 * Our credentials are about to become kcred-like, so we don't care
23150Sstevel@tonic-gate 	 * about the caller's ruid.
23160Sstevel@tonic-gate 	 */
23170Sstevel@tonic-gate 	upcount_inc(crgetruid(kcred), zone->zone_id);
23180Sstevel@tonic-gate 	mutex_exit(&pidlock);
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 	/*
23210Sstevel@tonic-gate 	 * getting out of global zone, so decrement lwp counts
23220Sstevel@tonic-gate 	 */
23230Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
23240Sstevel@tonic-gate 	mutex_enter(&global_zone->zone_nlwps_lock);
23250Sstevel@tonic-gate 	pj->kpj_nlwps -= pp->p_lwpcnt;
23260Sstevel@tonic-gate 	global_zone->zone_nlwps -= pp->p_lwpcnt;
23270Sstevel@tonic-gate 	mutex_exit(&global_zone->zone_nlwps_lock);
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate 	/*
23300Sstevel@tonic-gate 	 * Create and join a new task in project '0' of this zone.
23310Sstevel@tonic-gate 	 *
23320Sstevel@tonic-gate 	 * We don't need to call holdlwps() since we know we're the only lwp in
23330Sstevel@tonic-gate 	 * this process.
23340Sstevel@tonic-gate 	 *
23350Sstevel@tonic-gate 	 * task_join() returns with p_lock held.
23360Sstevel@tonic-gate 	 */
23370Sstevel@tonic-gate 	tk = task_create(0, zone);
23380Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
23390Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
23400Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
23410Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
23420Sstevel@tonic-gate 	task_rele(oldtk);
23430Sstevel@tonic-gate 
23440Sstevel@tonic-gate 	/*
23450Sstevel@tonic-gate 	 * add lwp counts to zsched's zone, and increment project's task count
23460Sstevel@tonic-gate 	 * due to the task created in the above tasksys_settaskid
23470Sstevel@tonic-gate 	 */
23480Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
23490Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
23500Sstevel@tonic-gate 	pj->kpj_nlwps += pp->p_lwpcnt;
23510Sstevel@tonic-gate 	pj->kpj_ntasks += 1;
23520Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
23530Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
23540Sstevel@tonic-gate 
23550Sstevel@tonic-gate 	/*
23560Sstevel@tonic-gate 	 * The process was created by a process in the global zone, hence the
23570Sstevel@tonic-gate 	 * credentials are wrong.  We might as well have kcred-ish credentials.
23580Sstevel@tonic-gate 	 */
23590Sstevel@tonic-gate 	cr = zone->zone_kcred;
23600Sstevel@tonic-gate 	crhold(cr);
23610Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
23620Sstevel@tonic-gate 	oldcred = pp->p_cred;
23630Sstevel@tonic-gate 	pp->p_cred = cr;
23640Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
23650Sstevel@tonic-gate 	crfree(oldcred);
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 	/*
23680Sstevel@tonic-gate 	 * Hold credentials again (for thread)
23690Sstevel@tonic-gate 	 */
23700Sstevel@tonic-gate 	crhold(cr);
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate 	/*
23730Sstevel@tonic-gate 	 * p_lwpcnt can't change since this is a kernel process.
23740Sstevel@tonic-gate 	 */
23750Sstevel@tonic-gate 	crset(pp, cr);
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate 	/*
23780Sstevel@tonic-gate 	 * Chroot
23790Sstevel@tonic-gate 	 */
23800Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp);
23810Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp);
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate 	/*
23840Sstevel@tonic-gate 	 * Initialize zone's rctl set.
23850Sstevel@tonic-gate 	 */
23860Sstevel@tonic-gate 	set = rctl_set_create();
23870Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
23880Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
23890Sstevel@tonic-gate 	e.rcep_p.zone = zone;
23900Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
23910Sstevel@tonic-gate 	zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp);
23920Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
23930Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate 	/*
23960Sstevel@tonic-gate 	 * Apply the rctls passed in to zone_create().  This is basically a list
23970Sstevel@tonic-gate 	 * assignment: all of the old values are removed and the new ones
23980Sstevel@tonic-gate 	 * inserted.  That is, if an empty list is passed in, all values are
23990Sstevel@tonic-gate 	 * removed.
24000Sstevel@tonic-gate 	 */
24010Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
24020Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
24030Sstevel@tonic-gate 		rctl_hndl_t hndl;
24040Sstevel@tonic-gate 		char *name;
24050Sstevel@tonic-gate 		nvlist_t **nvlarray;
24060Sstevel@tonic-gate 		uint_t i, nelem;
24070Sstevel@tonic-gate 		int error;	/* For ASSERT()s */
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate 		name = nvpair_name(nvp);
24100Sstevel@tonic-gate 		hndl = rctl_hndl_lookup(name);
24110Sstevel@tonic-gate 		ASSERT(hndl != -1);
24120Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
24130Sstevel@tonic-gate 		ASSERT(rde != NULL);
24140Sstevel@tonic-gate 
24150Sstevel@tonic-gate 		for (; /* ever */; ) {
24160Sstevel@tonic-gate 			rctl_val_t oval;
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
24190Sstevel@tonic-gate 			error = rctl_local_get(hndl, NULL, &oval, pp);
24200Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
24210Sstevel@tonic-gate 			ASSERT(error == 0);	/* Can't fail for RCTL_FIRST */
24220Sstevel@tonic-gate 			ASSERT(oval.rcv_privilege != RCPRIV_BASIC);
24230Sstevel@tonic-gate 			if (oval.rcv_privilege == RCPRIV_SYSTEM)
24240Sstevel@tonic-gate 				break;
24250Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
24260Sstevel@tonic-gate 			error = rctl_local_delete(hndl, &oval, pp);
24270Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
24280Sstevel@tonic-gate 			ASSERT(error == 0);
24290Sstevel@tonic-gate 		}
24300Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
24310Sstevel@tonic-gate 		ASSERT(error == 0);
24320Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
24330Sstevel@tonic-gate 			rctl_val_t *nvalp;
24340Sstevel@tonic-gate 
24350Sstevel@tonic-gate 			nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
24360Sstevel@tonic-gate 			error = nvlist2rctlval(nvlarray[i], nvalp);
24370Sstevel@tonic-gate 			ASSERT(error == 0);
24380Sstevel@tonic-gate 			/*
24390Sstevel@tonic-gate 			 * rctl_local_insert can fail if the value being
24400Sstevel@tonic-gate 			 * inserted is a duplicate; this is OK.
24410Sstevel@tonic-gate 			 */
24420Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
24430Sstevel@tonic-gate 			if (rctl_local_insert(hndl, nvalp, pp) != 0)
24440Sstevel@tonic-gate 				kmem_cache_free(rctl_val_cache, nvalp);
24450Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
24460Sstevel@tonic-gate 		}
24470Sstevel@tonic-gate 	}
24480Sstevel@tonic-gate 	/*
24490Sstevel@tonic-gate 	 * Tell the world that we're done setting up.
24500Sstevel@tonic-gate 	 *
24510Sstevel@tonic-gate 	 * At this point we want to set the zone status to ZONE_IS_READY
24520Sstevel@tonic-gate 	 * and atomically set the zone's processor set visibility.  Once
24530Sstevel@tonic-gate 	 * we drop pool_lock() this zone will automatically get updated
24540Sstevel@tonic-gate 	 * to reflect any future changes to the pools configuration.
24550Sstevel@tonic-gate 	 */
24560Sstevel@tonic-gate 	pool_lock();
24570Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
24580Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
24590Sstevel@tonic-gate 	zone_uniqid(zone);
24600Sstevel@tonic-gate 	zone_zsd_configure(zone);
24610Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED)
24620Sstevel@tonic-gate 		zone_pset_set(zone, pool_default->pool_pset->pset_id);
24630Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
24640Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
24650Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_READY);
24660Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
24670Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
24680Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
24690Sstevel@tonic-gate 	pool_unlock();
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate 	/*
24720Sstevel@tonic-gate 	 * Once we see the zone transition to the ZONE_IS_BOOTING state,
24730Sstevel@tonic-gate 	 * we launch init, and set the state to running.
24740Sstevel@tonic-gate 	 */
24750Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched");
24760Sstevel@tonic-gate 
24770Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_BOOTING) {
24780Sstevel@tonic-gate 		id_t cid;
24790Sstevel@tonic-gate 
24800Sstevel@tonic-gate 		/*
24810Sstevel@tonic-gate 		 * Ok, this is a little complicated.  We need to grab the
24820Sstevel@tonic-gate 		 * zone's pool's scheduling class ID; note that by now, we
24830Sstevel@tonic-gate 		 * are already bound to a pool if we need to be (zoneadmd
24840Sstevel@tonic-gate 		 * will have done that to us while we're in the READY
24850Sstevel@tonic-gate 		 * state).  *But* the scheduling class for the zone's 'init'
24860Sstevel@tonic-gate 		 * must be explicitly passed to newproc, which doesn't
24870Sstevel@tonic-gate 		 * respect pool bindings.
24880Sstevel@tonic-gate 		 *
24890Sstevel@tonic-gate 		 * We hold the pool_lock across the call to newproc() to
24900Sstevel@tonic-gate 		 * close the obvious race: the pool's scheduling class
24910Sstevel@tonic-gate 		 * could change before we manage to create the LWP with
24920Sstevel@tonic-gate 		 * classid 'cid'.
24930Sstevel@tonic-gate 		 */
24940Sstevel@tonic-gate 		pool_lock();
24950Sstevel@tonic-gate 		cid = pool_get_class(zone->zone_pool);
24960Sstevel@tonic-gate 		if (cid == -1)
24970Sstevel@tonic-gate 			cid = defaultcid;
24980Sstevel@tonic-gate 
24990Sstevel@tonic-gate 		/*
25000Sstevel@tonic-gate 		 * If this fails, zone_boot will ultimately fail.  The
25010Sstevel@tonic-gate 		 * state of the zone will be set to SHUTTING_DOWN-- userland
25020Sstevel@tonic-gate 		 * will have to tear down the zone, and fail, or try again.
25030Sstevel@tonic-gate 		 */
25040Sstevel@tonic-gate 		if ((zone->zone_boot_err = newproc(zone_icode, NULL, cid,
25050Sstevel@tonic-gate 		    minclsyspri - 1, &ct)) != 0) {
25060Sstevel@tonic-gate 			mutex_enter(&zone_status_lock);
25070Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
25080Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
25090Sstevel@tonic-gate 		}
25100Sstevel@tonic-gate 		pool_unlock();
25110Sstevel@tonic-gate 	}
25120Sstevel@tonic-gate 
25130Sstevel@tonic-gate 	/*
25140Sstevel@tonic-gate 	 * Wait for zone_destroy() to be called.  This is what we spend
25150Sstevel@tonic-gate 	 * most of our life doing.
25160Sstevel@tonic-gate 	 */
25170Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched");
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 	if (ct)
25200Sstevel@tonic-gate 		/*
25210Sstevel@tonic-gate 		 * At this point the process contract should be empty.
25220Sstevel@tonic-gate 		 * (Though if it isn't, it's not the end of the world.)
25230Sstevel@tonic-gate 		 */
25240Sstevel@tonic-gate 		VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0);
25250Sstevel@tonic-gate 
25260Sstevel@tonic-gate 	/*
25270Sstevel@tonic-gate 	 * Allow kcred to be freed when all referring processes
25280Sstevel@tonic-gate 	 * (including this one) go away.  We can't just do this in
25290Sstevel@tonic-gate 	 * zone_free because we need to wait for the zone_cred_ref to
25300Sstevel@tonic-gate 	 * drop to 0 before calling zone_free, and the existence of
25310Sstevel@tonic-gate 	 * zone_kcred will prevent that.  Thus, we call crfree here to
25320Sstevel@tonic-gate 	 * balance the crdup in zone_create.  The crhold calls earlier
25330Sstevel@tonic-gate 	 * in zsched will be dropped when the thread and process exit.
25340Sstevel@tonic-gate 	 */
25350Sstevel@tonic-gate 	crfree(zone->zone_kcred);
25360Sstevel@tonic-gate 	zone->zone_kcred = NULL;
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
25390Sstevel@tonic-gate }
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate /*
25420Sstevel@tonic-gate  * Helper function to determine if there are any submounts of the
25430Sstevel@tonic-gate  * provided path.  Used to make sure the zone doesn't "inherit" any
25440Sstevel@tonic-gate  * mounts from before it is created.
25450Sstevel@tonic-gate  */
25460Sstevel@tonic-gate static uint_t
25470Sstevel@tonic-gate zone_mount_count(const char *rootpath)
25480Sstevel@tonic-gate {
25490Sstevel@tonic-gate 	vfs_t *vfsp;
25500Sstevel@tonic-gate 	uint_t count = 0;
25510Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate 	/*
25540Sstevel@tonic-gate 	 * Holding zonehash_lock prevents race conditions with
25550Sstevel@tonic-gate 	 * vfs_list_add()/vfs_list_remove() since we serialize with
25560Sstevel@tonic-gate 	 * zone_find_by_path().
25570Sstevel@tonic-gate 	 */
25580Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
25590Sstevel@tonic-gate 	/*
25600Sstevel@tonic-gate 	 * The rootpath must end with a '/'
25610Sstevel@tonic-gate 	 */
25620Sstevel@tonic-gate 	ASSERT(rootpath[rootpathlen - 1] == '/');
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate 	/*
25650Sstevel@tonic-gate 	 * This intentionally does not count the rootpath itself if that
25660Sstevel@tonic-gate 	 * happens to be a mount point.
25670Sstevel@tonic-gate 	 */
25680Sstevel@tonic-gate 	vfs_list_read_lock();
25690Sstevel@tonic-gate 	vfsp = rootvfs;
25700Sstevel@tonic-gate 	do {
25710Sstevel@tonic-gate 		if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt),
25720Sstevel@tonic-gate 		    rootpathlen) == 0)
25730Sstevel@tonic-gate 			count++;
25740Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
25750Sstevel@tonic-gate 	} while (vfsp != rootvfs);
25760Sstevel@tonic-gate 	vfs_list_unlock();
25770Sstevel@tonic-gate 	return (count);
25780Sstevel@tonic-gate }
25790Sstevel@tonic-gate 
25800Sstevel@tonic-gate /*
25810Sstevel@tonic-gate  * Helper function to make sure that a zone created on 'rootpath'
25820Sstevel@tonic-gate  * wouldn't end up containing other zones' rootpaths.
25830Sstevel@tonic-gate  */
25840Sstevel@tonic-gate static boolean_t
25850Sstevel@tonic-gate zone_is_nested(const char *rootpath)
25860Sstevel@tonic-gate {
25870Sstevel@tonic-gate 	zone_t *zone;
25880Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
25890Sstevel@tonic-gate 	size_t len;
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
25940Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
25950Sstevel@tonic-gate 		if (zone == global_zone)
25960Sstevel@tonic-gate 			continue;
25970Sstevel@tonic-gate 		len = strlen(zone->zone_rootpath);
25980Sstevel@tonic-gate 		if (strncmp(rootpath, zone->zone_rootpath,
25990Sstevel@tonic-gate 		    MIN(rootpathlen, len)) == 0)
26000Sstevel@tonic-gate 			return (B_TRUE);
26010Sstevel@tonic-gate 	}
26020Sstevel@tonic-gate 	return (B_FALSE);
26030Sstevel@tonic-gate }
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate static int
2606813Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs,
2607813Sdp     size_t zone_privssz)
26080Sstevel@tonic-gate {
26090Sstevel@tonic-gate 	priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
26100Sstevel@tonic-gate 
2611813Sdp 	if (zone_privssz < sizeof (priv_set_t))
2612813Sdp 		return (set_errno(ENOMEM));
2613813Sdp 
26140Sstevel@tonic-gate 	if (copyin(zone_privs, privs, sizeof (priv_set_t))) {
26150Sstevel@tonic-gate 		kmem_free(privs, sizeof (priv_set_t));
26160Sstevel@tonic-gate 		return (EFAULT);
26170Sstevel@tonic-gate 	}
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate 	zone->zone_privset = privs;
26200Sstevel@tonic-gate 	return (0);
26210Sstevel@tonic-gate }
26220Sstevel@tonic-gate 
26230Sstevel@tonic-gate /*
26240Sstevel@tonic-gate  * We make creative use of nvlists to pass in rctls from userland.  The list is
26250Sstevel@tonic-gate  * a list of the following structures:
26260Sstevel@tonic-gate  *
26270Sstevel@tonic-gate  * (name = rctl_name, value = nvpair_list_array)
26280Sstevel@tonic-gate  *
26290Sstevel@tonic-gate  * Where each element of the nvpair_list_array is of the form:
26300Sstevel@tonic-gate  *
26310Sstevel@tonic-gate  * [(name = "privilege", value = RCPRIV_PRIVILEGED),
26320Sstevel@tonic-gate  * 	(name = "limit", value = uint64_t),
26330Sstevel@tonic-gate  * 	(name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))]
26340Sstevel@tonic-gate  */
26350Sstevel@tonic-gate static int
26360Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp)
26370Sstevel@tonic-gate {
26380Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
26390Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
26400Sstevel@tonic-gate 	char *kbuf;
26410Sstevel@tonic-gate 	int error;
26420Sstevel@tonic-gate 	rctl_val_t rv;
26430Sstevel@tonic-gate 
26440Sstevel@tonic-gate 	*nvlp = NULL;
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate 	if (buflen == 0)
26470Sstevel@tonic-gate 		return (0);
26480Sstevel@tonic-gate 
26490Sstevel@tonic-gate 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
26500Sstevel@tonic-gate 		return (ENOMEM);
26510Sstevel@tonic-gate 	if (copyin(ubuf, kbuf, buflen)) {
26520Sstevel@tonic-gate 		error = EFAULT;
26530Sstevel@tonic-gate 		goto out;
26540Sstevel@tonic-gate 	}
26550Sstevel@tonic-gate 	if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) {
26560Sstevel@tonic-gate 		/*
26570Sstevel@tonic-gate 		 * nvl may have been allocated/free'd, but the value set to
26580Sstevel@tonic-gate 		 * non-NULL, so we reset it here.
26590Sstevel@tonic-gate 		 */
26600Sstevel@tonic-gate 		nvl = NULL;
26610Sstevel@tonic-gate 		error = EINVAL;
26620Sstevel@tonic-gate 		goto out;
26630Sstevel@tonic-gate 	}
26640Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
26650Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
26660Sstevel@tonic-gate 		rctl_hndl_t hndl;
26670Sstevel@tonic-gate 		nvlist_t **nvlarray;
26680Sstevel@tonic-gate 		uint_t i, nelem;
26690Sstevel@tonic-gate 		char *name;
26700Sstevel@tonic-gate 
26710Sstevel@tonic-gate 		error = EINVAL;
26720Sstevel@tonic-gate 		name = nvpair_name(nvp);
26730Sstevel@tonic-gate 		if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1)
26740Sstevel@tonic-gate 		    != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) {
26750Sstevel@tonic-gate 			goto out;
26760Sstevel@tonic-gate 		}
26770Sstevel@tonic-gate 		if ((hndl = rctl_hndl_lookup(name)) == -1) {
26780Sstevel@tonic-gate 			goto out;
26790Sstevel@tonic-gate 		}
26800Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
26810Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
26820Sstevel@tonic-gate 		ASSERT(error == 0);
26830Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
26840Sstevel@tonic-gate 			if (error = nvlist2rctlval(nvlarray[i], &rv))
26850Sstevel@tonic-gate 				goto out;
26860Sstevel@tonic-gate 		}
26870Sstevel@tonic-gate 		if (rctl_invalid_value(rde, &rv)) {
26880Sstevel@tonic-gate 			error = EINVAL;
26890Sstevel@tonic-gate 			goto out;
26900Sstevel@tonic-gate 		}
26910Sstevel@tonic-gate 	}
26920Sstevel@tonic-gate 	error = 0;
26930Sstevel@tonic-gate 	*nvlp = nvl;
26940Sstevel@tonic-gate out:
26950Sstevel@tonic-gate 	kmem_free(kbuf, buflen);
26960Sstevel@tonic-gate 	if (error && nvl != NULL)
26970Sstevel@tonic-gate 		nvlist_free(nvl);
26980Sstevel@tonic-gate 	return (error);
26990Sstevel@tonic-gate }
27000Sstevel@tonic-gate 
27010Sstevel@tonic-gate int
27020Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) {
27030Sstevel@tonic-gate 	if (er_out != NULL) {
27040Sstevel@tonic-gate 		if (copyout(&er_ext, er_out, sizeof (int))) {
27050Sstevel@tonic-gate 			return (set_errno(EFAULT));
27060Sstevel@tonic-gate 		}
27070Sstevel@tonic-gate 	}
27080Sstevel@tonic-gate 	return (set_errno(er_error));
27090Sstevel@tonic-gate }
27100Sstevel@tonic-gate 
2711*1676Sjpk static int
2712*1676Sjpk zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi)
2713*1676Sjpk {
2714*1676Sjpk 	ts_label_t *tsl;
2715*1676Sjpk 	bslabel_t blab;
2716*1676Sjpk 
2717*1676Sjpk 	/* Get label from user */
2718*1676Sjpk 	if (copyin(lab, &blab, sizeof (blab)) != 0)
2719*1676Sjpk 		return (EFAULT);
2720*1676Sjpk 	tsl = labelalloc(&blab, doi, KM_NOSLEEP);
2721*1676Sjpk 	if (tsl == NULL)
2722*1676Sjpk 		return (ENOMEM);
2723*1676Sjpk 
2724*1676Sjpk 	zone->zone_slabel = tsl;
2725*1676Sjpk 	return (0);
2726*1676Sjpk }
2727*1676Sjpk 
27280Sstevel@tonic-gate /*
2729789Sahrens  * Parses a comma-separated list of ZFS datasets into a per-zone dictionary.
2730789Sahrens  */
2731789Sahrens static int
2732789Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen)
2733789Sahrens {
2734789Sahrens 	char *kbuf;
2735789Sahrens 	char *dataset, *next;
2736789Sahrens 	zone_dataset_t *zd;
2737789Sahrens 	size_t len;
2738789Sahrens 
2739789Sahrens 	if (ubuf == NULL || buflen == 0)
2740789Sahrens 		return (0);
2741789Sahrens 
2742789Sahrens 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
2743789Sahrens 		return (ENOMEM);
2744789Sahrens 
2745789Sahrens 	if (copyin(ubuf, kbuf, buflen) != 0) {
2746789Sahrens 		kmem_free(kbuf, buflen);
2747789Sahrens 		return (EFAULT);
2748789Sahrens 	}
2749789Sahrens 
2750789Sahrens 	dataset = next = kbuf;
2751789Sahrens 	for (;;) {
2752789Sahrens 		zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP);
2753789Sahrens 
2754789Sahrens 		next = strchr(dataset, ',');
2755789Sahrens 
2756789Sahrens 		if (next == NULL)
2757789Sahrens 			len = strlen(dataset);
2758789Sahrens 		else
2759789Sahrens 			len = next - dataset;
2760789Sahrens 
2761789Sahrens 		zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP);
2762789Sahrens 		bcopy(dataset, zd->zd_dataset, len);
2763789Sahrens 		zd->zd_dataset[len] = '\0';
2764789Sahrens 
2765789Sahrens 		list_insert_head(&zone->zone_datasets, zd);
2766789Sahrens 
2767789Sahrens 		if (next == NULL)
2768789Sahrens 			break;
2769789Sahrens 
2770789Sahrens 		dataset = next + 1;
2771789Sahrens 	}
2772789Sahrens 
2773789Sahrens 	kmem_free(kbuf, buflen);
2774789Sahrens 	return (0);
2775789Sahrens }
2776789Sahrens 
2777789Sahrens /*
27780Sstevel@tonic-gate  * System call to create/initialize a new zone named 'zone_name', rooted
27790Sstevel@tonic-gate  * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs',
2780*1676Sjpk  * and initialized with the zone-wide rctls described in 'rctlbuf', and
2781*1676Sjpk  * with labeling set by 'match', 'doi', and 'label'.
27820Sstevel@tonic-gate  *
27830Sstevel@tonic-gate  * If extended error is non-null, we may use it to return more detailed
27840Sstevel@tonic-gate  * error information.
27850Sstevel@tonic-gate  */
27860Sstevel@tonic-gate static zoneid_t
27870Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root,
2788813Sdp     const priv_set_t *zone_privs, size_t zone_privssz,
2789813Sdp     caddr_t rctlbuf, size_t rctlbufsz,
2790*1676Sjpk     caddr_t zfsbuf, size_t zfsbufsz, int *extended_error,
2791*1676Sjpk     int match, uint32_t doi, const bslabel_t *label)
27920Sstevel@tonic-gate {
27930Sstevel@tonic-gate 	struct zsched_arg zarg;
27940Sstevel@tonic-gate 	nvlist_t *rctls = NULL;
27950Sstevel@tonic-gate 	proc_t *pp = curproc;
27960Sstevel@tonic-gate 	zone_t *zone, *ztmp;
27970Sstevel@tonic-gate 	zoneid_t zoneid;
27980Sstevel@tonic-gate 	int error;
27990Sstevel@tonic-gate 	int error2 = 0;
28000Sstevel@tonic-gate 	char *str;
28010Sstevel@tonic-gate 	cred_t *zkcr;
28020Sstevel@tonic-gate 
28030Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
28040Sstevel@tonic-gate 		return (set_errno(EPERM));
28050Sstevel@tonic-gate 
28060Sstevel@tonic-gate 	/* can't boot zone from within chroot environment */
28070Sstevel@tonic-gate 	if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir)
28080Sstevel@tonic-gate 		return (zone_create_error(ENOTSUP, ZE_CHROOTED,
2809813Sdp 		    extended_error));
28100Sstevel@tonic-gate 
28110Sstevel@tonic-gate 	zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP);
28120Sstevel@tonic-gate 	zoneid = zone->zone_id = id_alloc(zoneid_space);
28130Sstevel@tonic-gate 	zone->zone_status = ZONE_IS_UNINITIALIZED;
28140Sstevel@tonic-gate 	zone->zone_pool = pool_default;
28150Sstevel@tonic-gate 	zone->zone_pool_mod = gethrtime();
28160Sstevel@tonic-gate 	zone->zone_psetid = ZONE_PS_INVAL;
28170Sstevel@tonic-gate 	zone->zone_ncpus = 0;
28180Sstevel@tonic-gate 	zone->zone_ncpus_online = 0;
28190Sstevel@tonic-gate 	mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL);
28200Sstevel@tonic-gate 	mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
28210Sstevel@tonic-gate 	cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL);
28220Sstevel@tonic-gate 	list_create(&zone->zone_zsd, sizeof (struct zsd_entry),
28230Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
2824789Sahrens 	list_create(&zone->zone_datasets, sizeof (zone_dataset_t),
2825789Sahrens 	    offsetof(zone_dataset_t, zd_linkage));
2826*1676Sjpk 	rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
28270Sstevel@tonic-gate 
28280Sstevel@tonic-gate 	if ((error = zone_set_name(zone, zone_name)) != 0) {
28290Sstevel@tonic-gate 		zone_free(zone);
28300Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
28310Sstevel@tonic-gate 	}
28320Sstevel@tonic-gate 
28330Sstevel@tonic-gate 	if ((error = zone_set_root(zone, zone_root)) != 0) {
28340Sstevel@tonic-gate 		zone_free(zone);
28350Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
28360Sstevel@tonic-gate 	}
2837813Sdp 	if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) {
28380Sstevel@tonic-gate 		zone_free(zone);
28390Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
28400Sstevel@tonic-gate 	}
28410Sstevel@tonic-gate 
28420Sstevel@tonic-gate 	/* initialize node name to be the same as zone name */
28430Sstevel@tonic-gate 	zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP);
28440Sstevel@tonic-gate 	(void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN);
28450Sstevel@tonic-gate 	zone->zone_nodename[_SYS_NMLN - 1] = '\0';
28460Sstevel@tonic-gate 
28470Sstevel@tonic-gate 	zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP);
28480Sstevel@tonic-gate 	zone->zone_domain[0] = '\0';
28490Sstevel@tonic-gate 	zone->zone_shares = 1;
28500Sstevel@tonic-gate 	zone->zone_bootargs = NULL;
28510Sstevel@tonic-gate 
28520Sstevel@tonic-gate 	/*
28530Sstevel@tonic-gate 	 * Zsched initializes the rctls.
28540Sstevel@tonic-gate 	 */
28550Sstevel@tonic-gate 	zone->zone_rctls = NULL;
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate 	if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) {
28580Sstevel@tonic-gate 		zone_free(zone);
28590Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
28600Sstevel@tonic-gate 	}
28610Sstevel@tonic-gate 
2862789Sahrens 	if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) {
2863789Sahrens 		zone_free(zone);
2864789Sahrens 		return (set_errno(error));
2865789Sahrens 	}
2866789Sahrens 
28670Sstevel@tonic-gate 	/*
2868*1676Sjpk 	 * Read in the trusted system parameters:
2869*1676Sjpk 	 * match flag and sensitivity label.
2870*1676Sjpk 	 */
2871*1676Sjpk 	zone->zone_match = match;
2872*1676Sjpk 	if (is_system_labeled()) {
2873*1676Sjpk 		error = zone_set_label(zone, label, doi);
2874*1676Sjpk 		if (error != 0) {
2875*1676Sjpk 			zone_free(zone);
2876*1676Sjpk 			return (set_errno(error));
2877*1676Sjpk 		}
2878*1676Sjpk 	} else {
2879*1676Sjpk 		/* all zones get an admin_low label if system is not labeled */
2880*1676Sjpk 		zone->zone_slabel = l_admin_low;
2881*1676Sjpk 		label_hold(l_admin_low);
2882*1676Sjpk 	}
2883*1676Sjpk 
2884*1676Sjpk 	/*
28850Sstevel@tonic-gate 	 * Stop all lwps since that's what normally happens as part of fork().
28860Sstevel@tonic-gate 	 * This needs to happen before we grab any locks to avoid deadlock
28870Sstevel@tonic-gate 	 * (another lwp in the process could be waiting for the held lock).
28880Sstevel@tonic-gate 	 */
28890Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) {
28900Sstevel@tonic-gate 		zone_free(zone);
28910Sstevel@tonic-gate 		if (rctls)
28920Sstevel@tonic-gate 			nvlist_free(rctls);
28930Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
28940Sstevel@tonic-gate 	}
28950Sstevel@tonic-gate 
28960Sstevel@tonic-gate 	if (block_mounts() == 0) {
28970Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
28980Sstevel@tonic-gate 		if (curthread != pp->p_agenttp)
28990Sstevel@tonic-gate 			continuelwps(pp);
29000Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
29010Sstevel@tonic-gate 		zone_free(zone);
29020Sstevel@tonic-gate 		if (rctls)
29030Sstevel@tonic-gate 			nvlist_free(rctls);
29040Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
29050Sstevel@tonic-gate 	}
29060Sstevel@tonic-gate 
29070Sstevel@tonic-gate 	/*
29080Sstevel@tonic-gate 	 * Set up credential for kernel access.  After this, any errors
29090Sstevel@tonic-gate 	 * should go through the dance in errout rather than calling
29100Sstevel@tonic-gate 	 * zone_free directly.
29110Sstevel@tonic-gate 	 */
29120Sstevel@tonic-gate 	zone->zone_kcred = crdup(kcred);
29130Sstevel@tonic-gate 	crsetzone(zone->zone_kcred, zone);
29140Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred));
29150Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred));
29160Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred));
29170Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred));
29180Sstevel@tonic-gate 
29190Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
29200Sstevel@tonic-gate 	/*
29210Sstevel@tonic-gate 	 * Make sure zone doesn't already exist.
2922*1676Sjpk 	 *
2923*1676Sjpk 	 * If the system and zone are labeled,
2924*1676Sjpk 	 * make sure no other zone exists that has the same label.
29250Sstevel@tonic-gate 	 */
2926*1676Sjpk 	if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL ||
2927*1676Sjpk 	    (zone->zone_slabel != NULL &&
2928*1676Sjpk 	    (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) {
29290Sstevel@tonic-gate 		zone_status_t status;
29300Sstevel@tonic-gate 
29310Sstevel@tonic-gate 		status = zone_status_get(ztmp);
29320Sstevel@tonic-gate 		if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING)
29330Sstevel@tonic-gate 			error = EEXIST;
29340Sstevel@tonic-gate 		else
29350Sstevel@tonic-gate 			error = EBUSY;
29360Sstevel@tonic-gate 		goto errout;
29370Sstevel@tonic-gate 	}
29380Sstevel@tonic-gate 
29390Sstevel@tonic-gate 	/*
29400Sstevel@tonic-gate 	 * Don't allow zone creations which would cause one zone's rootpath to
29410Sstevel@tonic-gate 	 * be accessible from that of another (non-global) zone.
29420Sstevel@tonic-gate 	 */
29430Sstevel@tonic-gate 	if (zone_is_nested(zone->zone_rootpath)) {
29440Sstevel@tonic-gate 		error = EBUSY;
29450Sstevel@tonic-gate 		goto errout;
29460Sstevel@tonic-gate 	}
29470Sstevel@tonic-gate 
29480Sstevel@tonic-gate 	ASSERT(zonecount != 0);		/* check for leaks */
29490Sstevel@tonic-gate 	if (zonecount + 1 > maxzones) {
29500Sstevel@tonic-gate 		error = ENOMEM;
29510Sstevel@tonic-gate 		goto errout;
29520Sstevel@tonic-gate 	}
29530Sstevel@tonic-gate 
29540Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
29550Sstevel@tonic-gate 		error = EBUSY;
29560Sstevel@tonic-gate 		error2 = ZE_AREMOUNTS;
29570Sstevel@tonic-gate 		goto errout;
29580Sstevel@tonic-gate 	}
29590Sstevel@tonic-gate 
29600Sstevel@tonic-gate 	/*
29610Sstevel@tonic-gate 	 * Zone is still incomplete, but we need to drop all locks while
29620Sstevel@tonic-gate 	 * zsched() initializes this zone's kernel process.  We
29630Sstevel@tonic-gate 	 * optimistically add the zone to the hashtable and associated
29640Sstevel@tonic-gate 	 * lists so a parallel zone_create() doesn't try to create the
29650Sstevel@tonic-gate 	 * same zone.
29660Sstevel@tonic-gate 	 */
29670Sstevel@tonic-gate 	zonecount++;
29680Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid,
29690Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id,
29700Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
29710Sstevel@tonic-gate 	str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP);
29720Sstevel@tonic-gate 	(void) strcpy(str, zone->zone_name);
29730Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str,
29740Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
2975*1676Sjpk 	if (is_system_labeled()) {
2976*1676Sjpk 		(void) mod_hash_insert(zonehashbylabel,
2977*1676Sjpk 		    (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone);
2978*1676Sjpk 	}
2979*1676Sjpk 
29800Sstevel@tonic-gate 	/*
29810Sstevel@tonic-gate 	 * Insert into active list.  At this point there are no 'hold's
29820Sstevel@tonic-gate 	 * on the zone, but everyone else knows not to use it, so we can
29830Sstevel@tonic-gate 	 * continue to use it.  zsched() will do a zone_hold() if the
29840Sstevel@tonic-gate 	 * newproc() is successful.
29850Sstevel@tonic-gate 	 */
29860Sstevel@tonic-gate 	list_insert_tail(&zone_active, zone);
29870Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
29880Sstevel@tonic-gate 
29890Sstevel@tonic-gate 	zarg.zone = zone;
29900Sstevel@tonic-gate 	zarg.nvlist = rctls;
29910Sstevel@tonic-gate 	/*
29920Sstevel@tonic-gate 	 * The process, task, and project rctls are probably wrong;
29930Sstevel@tonic-gate 	 * we need an interface to get the default values of all rctls,
29940Sstevel@tonic-gate 	 * and initialize zsched appropriately.  I'm not sure that that
29950Sstevel@tonic-gate 	 * makes much of a difference, though.
29960Sstevel@tonic-gate 	 */
29970Sstevel@tonic-gate 	if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) {
29980Sstevel@tonic-gate 		/*
29990Sstevel@tonic-gate 		 * We need to undo all globally visible state.
30000Sstevel@tonic-gate 		 */
30010Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
30020Sstevel@tonic-gate 		list_remove(&zone_active, zone);
3003*1676Sjpk 		if (is_system_labeled()) {
3004*1676Sjpk 			ASSERT(zone->zone_slabel != NULL);
3005*1676Sjpk 			(void) mod_hash_destroy(zonehashbylabel,
3006*1676Sjpk 			    (mod_hash_key_t)zone->zone_slabel);
3007*1676Sjpk 		}
30080Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyname,
30090Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_name);
30100Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyid,
30110Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_id);
30120Sstevel@tonic-gate 		ASSERT(zonecount > 1);
30130Sstevel@tonic-gate 		zonecount--;
30140Sstevel@tonic-gate 		goto errout;
30150Sstevel@tonic-gate 	}
30160Sstevel@tonic-gate 
30170Sstevel@tonic-gate 	/*
30180Sstevel@tonic-gate 	 * Zone creation can't fail from now on.
30190Sstevel@tonic-gate 	 */
30200Sstevel@tonic-gate 
30210Sstevel@tonic-gate 	/*
30220Sstevel@tonic-gate 	 * Let the other lwps continue.
30230Sstevel@tonic-gate 	 */
30240Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
30250Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
30260Sstevel@tonic-gate 		continuelwps(pp);
30270Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
30280Sstevel@tonic-gate 
30290Sstevel@tonic-gate 	/*
30300Sstevel@tonic-gate 	 * Wait for zsched to finish initializing the zone.
30310Sstevel@tonic-gate 	 */
30320Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_READY);
30330Sstevel@tonic-gate 	/*
30340Sstevel@tonic-gate 	 * The zone is fully visible, so we can let mounts progress.
30350Sstevel@tonic-gate 	 */
30360Sstevel@tonic-gate 	resume_mounts();
30370Sstevel@tonic-gate 	if (rctls)
30380Sstevel@tonic-gate 		nvlist_free(rctls);
30390Sstevel@tonic-gate 
30400Sstevel@tonic-gate 	return (zoneid);
30410Sstevel@tonic-gate 
30420Sstevel@tonic-gate errout:
30430Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
30440Sstevel@tonic-gate 	/*
30450Sstevel@tonic-gate 	 * Let the other lwps continue.
30460Sstevel@tonic-gate 	 */
30470Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
30480Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
30490Sstevel@tonic-gate 		continuelwps(pp);
30500Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
30510Sstevel@tonic-gate 
30520Sstevel@tonic-gate 	resume_mounts();
30530Sstevel@tonic-gate 	if (rctls)
30540Sstevel@tonic-gate 		nvlist_free(rctls);
30550Sstevel@tonic-gate 	/*
30560Sstevel@tonic-gate 	 * There is currently one reference to the zone, a cred_ref from
30570Sstevel@tonic-gate 	 * zone_kcred.  To free the zone, we call crfree, which will call
30580Sstevel@tonic-gate 	 * zone_cred_rele, which will call zone_free.
30590Sstevel@tonic-gate 	 */
30600Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 1);	/* for zone_kcred */
30610Sstevel@tonic-gate 	ASSERT(zone->zone_kcred->cr_ref == 1);
30620Sstevel@tonic-gate 	ASSERT(zone->zone_ref == 0);
30630Sstevel@tonic-gate 	zkcr = zone->zone_kcred;
30640Sstevel@tonic-gate 	zone->zone_kcred = NULL;
30650Sstevel@tonic-gate 	crfree(zkcr);				/* triggers call to zone_free */
30660Sstevel@tonic-gate 	return (zone_create_error(error, error2, extended_error));
30670Sstevel@tonic-gate }
30680Sstevel@tonic-gate 
30690Sstevel@tonic-gate /*
30700Sstevel@tonic-gate  * Cause the zone to boot.  This is pretty simple, since we let zoneadmd do
30710Sstevel@tonic-gate  * the heavy lifting.
30720Sstevel@tonic-gate  */
30730Sstevel@tonic-gate static int
30740Sstevel@tonic-gate zone_boot(zoneid_t zoneid, const char *bootargs)
30750Sstevel@tonic-gate {
30760Sstevel@tonic-gate 	int err;
30770Sstevel@tonic-gate 	zone_t *zone;
30780Sstevel@tonic-gate 
30790Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
30800Sstevel@tonic-gate 		return (set_errno(EPERM));
30810Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
30820Sstevel@tonic-gate 		return (set_errno(EINVAL));
30830Sstevel@tonic-gate 
30840Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
30850Sstevel@tonic-gate 	/*
30860Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with calls to
30870Sstevel@tonic-gate 	 * zone_shutdown, zone_destroy, etc.
30880Sstevel@tonic-gate 	 */
30890Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
30900Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
30910Sstevel@tonic-gate 		return (set_errno(EINVAL));
30920Sstevel@tonic-gate 	}
30930Sstevel@tonic-gate 
30940Sstevel@tonic-gate 	if ((err = zone_set_bootargs(zone, bootargs)) != 0) {
30950Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
30960Sstevel@tonic-gate 		return (set_errno(err));
30970Sstevel@tonic-gate 	}
30980Sstevel@tonic-gate 
30990Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
31000Sstevel@tonic-gate 	if (zone_status_get(zone) != ZONE_IS_READY) {
31010Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
31020Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
31030Sstevel@tonic-gate 		return (set_errno(EINVAL));
31040Sstevel@tonic-gate 	}
31050Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_BOOTING);
31060Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
31070Sstevel@tonic-gate 
31080Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
31090Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
31100Sstevel@tonic-gate 
31110Sstevel@tonic-gate 	if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) {
31120Sstevel@tonic-gate 		zone_rele(zone);
31130Sstevel@tonic-gate 		return (set_errno(EINTR));
31140Sstevel@tonic-gate 	}
31150Sstevel@tonic-gate 
31160Sstevel@tonic-gate 	/*
31170Sstevel@tonic-gate 	 * Boot (starting init) might have failed, in which case the zone
31180Sstevel@tonic-gate 	 * will go to the SHUTTING_DOWN state; an appropriate errno will
31190Sstevel@tonic-gate 	 * be placed in zone->zone_boot_err, and so we return that.
31200Sstevel@tonic-gate 	 */
31210Sstevel@tonic-gate 	err = zone->zone_boot_err;
31220Sstevel@tonic-gate 	zone_rele(zone);
31230Sstevel@tonic-gate 	return (err ? set_errno(err) : 0);
31240Sstevel@tonic-gate }
31250Sstevel@tonic-gate 
31260Sstevel@tonic-gate /*
31270Sstevel@tonic-gate  * Kills all user processes in the zone, waiting for them all to exit
31280Sstevel@tonic-gate  * before returning.
31290Sstevel@tonic-gate  */
31300Sstevel@tonic-gate static int
31310Sstevel@tonic-gate zone_empty(zone_t *zone)
31320Sstevel@tonic-gate {
31330Sstevel@tonic-gate 	int waitstatus;
31340Sstevel@tonic-gate 
31350Sstevel@tonic-gate 	/*
31360Sstevel@tonic-gate 	 * We need to drop zonehash_lock before killing all
31370Sstevel@tonic-gate 	 * processes, otherwise we'll deadlock with zone_find_*
31380Sstevel@tonic-gate 	 * which can be called from the exit path.
31390Sstevel@tonic-gate 	 */
31400Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
31410Sstevel@tonic-gate 	while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz,
31420Sstevel@tonic-gate 	    ZONE_IS_EMPTY)) == -1) {
31430Sstevel@tonic-gate 		killall(zone->zone_id);
31440Sstevel@tonic-gate 	}
31450Sstevel@tonic-gate 	/*
31460Sstevel@tonic-gate 	 * return EINTR if we were signaled
31470Sstevel@tonic-gate 	 */
31480Sstevel@tonic-gate 	if (waitstatus == 0)
31490Sstevel@tonic-gate 		return (EINTR);
31500Sstevel@tonic-gate 	return (0);
31510Sstevel@tonic-gate }
31520Sstevel@tonic-gate 
31530Sstevel@tonic-gate /*
3154*1676Sjpk  * This function implements the policy for zone visibility.
3155*1676Sjpk  *
3156*1676Sjpk  * In standard Solaris, a non-global zone can only see itself.
3157*1676Sjpk  *
3158*1676Sjpk  * In Trusted Extensions, a labeled zone can lookup any zone whose label
3159*1676Sjpk  * it dominates. For this test, the label of the global zone is treated as
3160*1676Sjpk  * admin_high so it is special-cased instead of being checked for dominance.
3161*1676Sjpk  *
3162*1676Sjpk  * Returns true if zone attributes are viewable, false otherwise.
3163*1676Sjpk  */
3164*1676Sjpk static boolean_t
3165*1676Sjpk zone_list_access(zone_t *zone)
3166*1676Sjpk {
3167*1676Sjpk 
3168*1676Sjpk 	if (curproc->p_zone == global_zone ||
3169*1676Sjpk 	    curproc->p_zone == zone) {
3170*1676Sjpk 		return (B_TRUE);
3171*1676Sjpk 	} else if (is_system_labeled()) {
3172*1676Sjpk 		bslabel_t *curproc_label;
3173*1676Sjpk 		bslabel_t *zone_label;
3174*1676Sjpk 
3175*1676Sjpk 		curproc_label = label2bslabel(curproc->p_zone->zone_slabel);
3176*1676Sjpk 		zone_label = label2bslabel(zone->zone_slabel);
3177*1676Sjpk 
3178*1676Sjpk 		if (zone->zone_id != GLOBAL_ZONEID &&
3179*1676Sjpk 		    bldominates(curproc_label, zone_label)) {
3180*1676Sjpk 			return (B_TRUE);
3181*1676Sjpk 		} else {
3182*1676Sjpk 			return (B_FALSE);
3183*1676Sjpk 		}
3184*1676Sjpk 	} else {
3185*1676Sjpk 		return (B_FALSE);
3186*1676Sjpk 	}
3187*1676Sjpk }
3188*1676Sjpk 
3189*1676Sjpk /*
31900Sstevel@tonic-gate  * Systemcall to start the zone's halt sequence.  By the time this
31910Sstevel@tonic-gate  * function successfully returns, all user processes and kernel threads
31920Sstevel@tonic-gate  * executing in it will have exited, ZSD shutdown callbacks executed,
31930Sstevel@tonic-gate  * and the zone status set to ZONE_IS_DOWN.
31940Sstevel@tonic-gate  *
31950Sstevel@tonic-gate  * It is possible that the call will interrupt itself if the caller is the
31960Sstevel@tonic-gate  * parent of any process running in the zone, and doesn't have SIGCHLD blocked.
31970Sstevel@tonic-gate  */
31980Sstevel@tonic-gate static int
31990Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid)
32000Sstevel@tonic-gate {
32010Sstevel@tonic-gate 	int error;
32020Sstevel@tonic-gate 	zone_t *zone;
32030Sstevel@tonic-gate 	zone_status_t status;
32040Sstevel@tonic-gate 
32050Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
32060Sstevel@tonic-gate 		return (set_errno(EPERM));
32070Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
32080Sstevel@tonic-gate 		return (set_errno(EINVAL));
32090Sstevel@tonic-gate 
32100Sstevel@tonic-gate 	/*
32110Sstevel@tonic-gate 	 * Block mounts so that VFS_MOUNT() can get an accurate view of
32120Sstevel@tonic-gate 	 * the zone's status with regards to ZONE_IS_SHUTTING down.
32130Sstevel@tonic-gate 	 *
32140Sstevel@tonic-gate 	 * e.g. NFS can fail the mount if it determines that the zone
32150Sstevel@tonic-gate 	 * has already begun the shutdown sequence.
32160Sstevel@tonic-gate 	 */
32170Sstevel@tonic-gate 	if (block_mounts() == 0)
32180Sstevel@tonic-gate 		return (set_errno(EINTR));
32190Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
32200Sstevel@tonic-gate 	/*
32210Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
32220Sstevel@tonic-gate 	 * calls to zone_shutdown and zone_destroy.
32230Sstevel@tonic-gate 	 */
32240Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
32250Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
32260Sstevel@tonic-gate 		resume_mounts();
32270Sstevel@tonic-gate 		return (set_errno(EINVAL));
32280Sstevel@tonic-gate 	}
32290Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
32300Sstevel@tonic-gate 	status = zone_status_get(zone);
32310Sstevel@tonic-gate 	/*
32320Sstevel@tonic-gate 	 * Fail if the zone isn't fully initialized yet.
32330Sstevel@tonic-gate 	 */
32340Sstevel@tonic-gate 	if (status < ZONE_IS_READY) {
32350Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
32360Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
32370Sstevel@tonic-gate 		resume_mounts();
32380Sstevel@tonic-gate 		return (set_errno(EINVAL));
32390Sstevel@tonic-gate 	}
32400Sstevel@tonic-gate 	/*
32410Sstevel@tonic-gate 	 * If conditions required for zone_shutdown() to return have been met,
32420Sstevel@tonic-gate 	 * return success.
32430Sstevel@tonic-gate 	 */
32440Sstevel@tonic-gate 	if (status >= ZONE_IS_DOWN) {
32450Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
32460Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
32470Sstevel@tonic-gate 		resume_mounts();
32480Sstevel@tonic-gate 		return (0);
32490Sstevel@tonic-gate 	}
32500Sstevel@tonic-gate 	/*
32510Sstevel@tonic-gate 	 * If zone_shutdown() hasn't been called before, go through the motions.
32520Sstevel@tonic-gate 	 * If it has, there's nothing to do but wait for the kernel threads to
32530Sstevel@tonic-gate 	 * drain.
32540Sstevel@tonic-gate 	 */
32550Sstevel@tonic-gate 	if (status < ZONE_IS_EMPTY) {
32560Sstevel@tonic-gate 		uint_t ntasks;
32570Sstevel@tonic-gate 
32580Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
32590Sstevel@tonic-gate 		if ((ntasks = zone->zone_ntasks) != 1) {
32600Sstevel@tonic-gate 			/*
32610Sstevel@tonic-gate 			 * There's still stuff running.
32620Sstevel@tonic-gate 			 */
32630Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
32640Sstevel@tonic-gate 		}
32650Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
32660Sstevel@tonic-gate 		if (ntasks == 1) {
32670Sstevel@tonic-gate 			/*
32680Sstevel@tonic-gate 			 * The only way to create another task is through
32690Sstevel@tonic-gate 			 * zone_enter(), which will block until we drop
32700Sstevel@tonic-gate 			 * zonehash_lock.  The zone is empty.
32710Sstevel@tonic-gate 			 */
32720Sstevel@tonic-gate 			if (zone->zone_kthreads == NULL) {
32730Sstevel@tonic-gate 				/*
32740Sstevel@tonic-gate 				 * Skip ahead to ZONE_IS_DOWN
32750Sstevel@tonic-gate 				 */
32760Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_DOWN);
32770Sstevel@tonic-gate 			} else {
32780Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_EMPTY);
32790Sstevel@tonic-gate 			}
32800Sstevel@tonic-gate 		}
32810Sstevel@tonic-gate 	}
32820Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
32830Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
32840Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
32850Sstevel@tonic-gate 	resume_mounts();
32860Sstevel@tonic-gate 
32870Sstevel@tonic-gate 	if (error = zone_empty(zone)) {
32880Sstevel@tonic-gate 		zone_rele(zone);
32890Sstevel@tonic-gate 		return (set_errno(error));
32900Sstevel@tonic-gate 	}
32910Sstevel@tonic-gate 	/*
32920Sstevel@tonic-gate 	 * After the zone status goes to ZONE_IS_DOWN this zone will no
32930Sstevel@tonic-gate 	 * longer be notified of changes to the pools configuration, so
32940Sstevel@tonic-gate 	 * in order to not end up with a stale pool pointer, we point
32950Sstevel@tonic-gate 	 * ourselves at the default pool and remove all resource
32960Sstevel@tonic-gate 	 * visibility.  This is especially important as the zone_t may
32970Sstevel@tonic-gate 	 * languish on the deathrow for a very long time waiting for
32980Sstevel@tonic-gate 	 * cred's to drain out.
32990Sstevel@tonic-gate 	 *
33000Sstevel@tonic-gate 	 * This rebinding of the zone can happen multiple times
33010Sstevel@tonic-gate 	 * (presumably due to interrupted or parallel systemcalls)
33020Sstevel@tonic-gate 	 * without any adverse effects.
33030Sstevel@tonic-gate 	 */
33040Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
33050Sstevel@tonic-gate 		zone_rele(zone);
33060Sstevel@tonic-gate 		return (set_errno(EINTR));
33070Sstevel@tonic-gate 	}
33080Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
33090Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
33100Sstevel@tonic-gate 		zone_pool_set(zone, pool_default);
33110Sstevel@tonic-gate 		/*
33120Sstevel@tonic-gate 		 * The zone no longer needs to be able to see any cpus.
33130Sstevel@tonic-gate 		 */
33140Sstevel@tonic-gate 		zone_pset_set(zone, ZONE_PS_INVAL);
33150Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
33160Sstevel@tonic-gate 	}
33170Sstevel@tonic-gate 	pool_unlock();
33180Sstevel@tonic-gate 
33190Sstevel@tonic-gate 	/*
33200Sstevel@tonic-gate 	 * ZSD shutdown callbacks can be executed multiple times, hence
33210Sstevel@tonic-gate 	 * it is safe to not be holding any locks across this call.
33220Sstevel@tonic-gate 	 */
33230Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_SHUTDOWN);
33240Sstevel@tonic-gate 
33250Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
33260Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN)
33270Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DOWN);
33280Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
33290Sstevel@tonic-gate 
33300Sstevel@tonic-gate 	/*
33310Sstevel@tonic-gate 	 * Wait for kernel threads to drain.
33320Sstevel@tonic-gate 	 */
33330Sstevel@tonic-gate 	if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) {
33340Sstevel@tonic-gate 		zone_rele(zone);
33350Sstevel@tonic-gate 		return (set_errno(EINTR));
33360Sstevel@tonic-gate 	}
33370Sstevel@tonic-gate 	zone_rele(zone);
33380Sstevel@tonic-gate 	return (0);
33390Sstevel@tonic-gate }
33400Sstevel@tonic-gate 
33410Sstevel@tonic-gate /*
33420Sstevel@tonic-gate  * Systemcall entry point to finalize the zone halt process.  The caller
33430Sstevel@tonic-gate  * must have already successfully callefd zone_shutdown().
33440Sstevel@tonic-gate  *
33450Sstevel@tonic-gate  * Upon successful completion, the zone will have been fully destroyed:
33460Sstevel@tonic-gate  * zsched will have exited, destructor callbacks executed, and the zone
33470Sstevel@tonic-gate  * removed from the list of active zones.
33480Sstevel@tonic-gate  */
33490Sstevel@tonic-gate static int
33500Sstevel@tonic-gate zone_destroy(zoneid_t zoneid)
33510Sstevel@tonic-gate {
33520Sstevel@tonic-gate 	uint64_t uniqid;
33530Sstevel@tonic-gate 	zone_t *zone;
33540Sstevel@tonic-gate 	zone_status_t status;
33550Sstevel@tonic-gate 
33560Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
33570Sstevel@tonic-gate 		return (set_errno(EPERM));
33580Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
33590Sstevel@tonic-gate 		return (set_errno(EINVAL));
33600Sstevel@tonic-gate 
33610Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
33620Sstevel@tonic-gate 	/*
33630Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
33640Sstevel@tonic-gate 	 * calls to zone_destroy.
33650Sstevel@tonic-gate 	 */
33660Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
33670Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
33680Sstevel@tonic-gate 		return (set_errno(EINVAL));
33690Sstevel@tonic-gate 	}
33700Sstevel@tonic-gate 
33710Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
33720Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
33730Sstevel@tonic-gate 		return (set_errno(EBUSY));
33740Sstevel@tonic-gate 	}
33750Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
33760Sstevel@tonic-gate 	status = zone_status_get(zone);
33770Sstevel@tonic-gate 	if (status < ZONE_IS_DOWN) {
33780Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
33790Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
33800Sstevel@tonic-gate 		return (set_errno(EBUSY));
33810Sstevel@tonic-gate 	} else if (status == ZONE_IS_DOWN) {
33820Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */
33830Sstevel@tonic-gate 	}
33840Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
33850Sstevel@tonic-gate 	zone_hold(zone);
33860Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
33870Sstevel@tonic-gate 
33880Sstevel@tonic-gate 	/*
33890Sstevel@tonic-gate 	 * wait for zsched to exit
33900Sstevel@tonic-gate 	 */
33910Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_DEAD);
33920Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_DESTROY);
33930Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
33940Sstevel@tonic-gate 	zone_rele(zone);
33950Sstevel@tonic-gate 	zone = NULL;	/* potentially free'd */
33960Sstevel@tonic-gate 
33970Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
33980Sstevel@tonic-gate 	for (; /* ever */; ) {
33990Sstevel@tonic-gate 		boolean_t unref;
34000Sstevel@tonic-gate 
34010Sstevel@tonic-gate 		if ((zone = zone_find_all_by_id(zoneid)) == NULL ||
34020Sstevel@tonic-gate 		    zone->zone_uniqid != uniqid) {
34030Sstevel@tonic-gate 			/*
34040Sstevel@tonic-gate 			 * The zone has gone away.  Necessary conditions
34050Sstevel@tonic-gate 			 * are met, so we return success.
34060Sstevel@tonic-gate 			 */
34070Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
34080Sstevel@tonic-gate 			return (0);
34090Sstevel@tonic-gate 		}
34100Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
34110Sstevel@tonic-gate 		unref = ZONE_IS_UNREF(zone);
34120Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
34130Sstevel@tonic-gate 		if (unref) {
34140Sstevel@tonic-gate 			/*
34150Sstevel@tonic-gate 			 * There is only one reference to the zone -- that
34160Sstevel@tonic-gate 			 * added when the zone was added to the hashtables --
34170Sstevel@tonic-gate 			 * and things will remain this way until we drop
34180Sstevel@tonic-gate 			 * zonehash_lock... we can go ahead and cleanup the
34190Sstevel@tonic-gate 			 * zone.
34200Sstevel@tonic-gate 			 */
34210Sstevel@tonic-gate 			break;
34220Sstevel@tonic-gate 		}
34230Sstevel@tonic-gate 
34240Sstevel@tonic-gate 		if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) {
34250Sstevel@tonic-gate 			/* Signaled */
34260Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
34270Sstevel@tonic-gate 			return (set_errno(EINTR));
34280Sstevel@tonic-gate 		}
34290Sstevel@tonic-gate 
34300Sstevel@tonic-gate 	}
34310Sstevel@tonic-gate 
34320Sstevel@tonic-gate 	/*
34330Sstevel@tonic-gate 	 * It is now safe to let the zone be recreated; remove it from the
34340Sstevel@tonic-gate 	 * lists.  The memory will not be freed until the last cred
34350Sstevel@tonic-gate 	 * reference goes away.
34360Sstevel@tonic-gate 	 */
34370Sstevel@tonic-gate 	ASSERT(zonecount > 1);	/* must be > 1; can't destroy global zone */
34380Sstevel@tonic-gate 	zonecount--;
34390Sstevel@tonic-gate 	/* remove from active list and hash tables */
34400Sstevel@tonic-gate 	list_remove(&zone_active, zone);
34410Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyname,
34420Sstevel@tonic-gate 	    (mod_hash_key_t)zone->zone_name);
34430Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyid,
34440Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id);
3445*1676Sjpk 	if (is_system_labeled() && zone->zone_slabel != NULL)
3446*1676Sjpk 		(void) mod_hash_destroy(zonehashbylabel,
3447*1676Sjpk 		    (mod_hash_key_t)zone->zone_slabel);
34480Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
34490Sstevel@tonic-gate 
3450766Scarlsonj 	/*
3451766Scarlsonj 	 * Release the root vnode; we're not using it anymore.  Nor should any
3452766Scarlsonj 	 * other thread that might access it exist.
3453766Scarlsonj 	 */
3454766Scarlsonj 	if (zone->zone_rootvp != NULL) {
3455766Scarlsonj 		VN_RELE(zone->zone_rootvp);
3456766Scarlsonj 		zone->zone_rootvp = NULL;
3457766Scarlsonj 	}
3458766Scarlsonj 
34590Sstevel@tonic-gate 	/* add to deathrow list */
34600Sstevel@tonic-gate 	mutex_enter(&zone_deathrow_lock);
34610Sstevel@tonic-gate 	list_insert_tail(&zone_deathrow, zone);
34620Sstevel@tonic-gate 	mutex_exit(&zone_deathrow_lock);
34630Sstevel@tonic-gate 
34640Sstevel@tonic-gate 	/*
34650Sstevel@tonic-gate 	 * Drop last reference (which was added by zsched()), this will
34660Sstevel@tonic-gate 	 * free the zone unless there are outstanding cred references.
34670Sstevel@tonic-gate 	 */
34680Sstevel@tonic-gate 	zone_rele(zone);
34690Sstevel@tonic-gate 	return (0);
34700Sstevel@tonic-gate }
34710Sstevel@tonic-gate 
34720Sstevel@tonic-gate /*
34730Sstevel@tonic-gate  * Systemcall entry point for zone_getattr(2).
34740Sstevel@tonic-gate  */
34750Sstevel@tonic-gate static ssize_t
34760Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
34770Sstevel@tonic-gate {
34780Sstevel@tonic-gate 	size_t size;
34790Sstevel@tonic-gate 	int error = 0, err;
34800Sstevel@tonic-gate 	zone_t *zone;
34810Sstevel@tonic-gate 	char *zonepath;
34820Sstevel@tonic-gate 	zone_status_t zone_status;
34830Sstevel@tonic-gate 	pid_t initpid;
34840Sstevel@tonic-gate 	boolean_t global = (curproc->p_zone == global_zone);
3485*1676Sjpk 	boolean_t curzone = (curproc->p_zone->zone_id == zoneid);
34860Sstevel@tonic-gate 
34870Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
34880Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
34890Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
34900Sstevel@tonic-gate 		return (set_errno(EINVAL));
34910Sstevel@tonic-gate 	}
34920Sstevel@tonic-gate 	zone_status = zone_status_get(zone);
34930Sstevel@tonic-gate 	if (zone_status < ZONE_IS_READY) {
34940Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
34950Sstevel@tonic-gate 		return (set_errno(EINVAL));
34960Sstevel@tonic-gate 	}
34970Sstevel@tonic-gate 	zone_hold(zone);
34980Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
34990Sstevel@tonic-gate 
35000Sstevel@tonic-gate 	/*
3501*1676Sjpk 	 * If not in the global zone, don't show information about other zones,
3502*1676Sjpk 	 * unless the system is labeled and the local zone's label dominates
3503*1676Sjpk 	 * the other zone.
35040Sstevel@tonic-gate 	 */
3505*1676Sjpk 	if (!zone_list_access(zone)) {
35060Sstevel@tonic-gate 		zone_rele(zone);
35070Sstevel@tonic-gate 		return (set_errno(EINVAL));
35080Sstevel@tonic-gate 	}
35090Sstevel@tonic-gate 
35100Sstevel@tonic-gate 	switch (attr) {
35110Sstevel@tonic-gate 	case ZONE_ATTR_ROOT:
35120Sstevel@tonic-gate 		if (global) {
35130Sstevel@tonic-gate 			/*
35140Sstevel@tonic-gate 			 * Copy the path to trim the trailing "/" (except for
35150Sstevel@tonic-gate 			 * the global zone).
35160Sstevel@tonic-gate 			 */
35170Sstevel@tonic-gate 			if (zone != global_zone)
35180Sstevel@tonic-gate 				size = zone->zone_rootpathlen - 1;
35190Sstevel@tonic-gate 			else
35200Sstevel@tonic-gate 				size = zone->zone_rootpathlen;
35210Sstevel@tonic-gate 			zonepath = kmem_alloc(size, KM_SLEEP);
35220Sstevel@tonic-gate 			bcopy(zone->zone_rootpath, zonepath, size);
35230Sstevel@tonic-gate 			zonepath[size - 1] = '\0';
35240Sstevel@tonic-gate 		} else {
3525*1676Sjpk 			if (curzone || !is_system_labeled()) {
3526*1676Sjpk 				/*
3527*1676Sjpk 				 * Caller is not in the global zone.
3528*1676Sjpk 				 * if the query is on the current zone
3529*1676Sjpk 				 * or the system is not labeled,
3530*1676Sjpk 				 * just return faked-up path for current zone.
3531*1676Sjpk 				 */
3532*1676Sjpk 				zonepath = "/";
3533*1676Sjpk 				size = 2;
3534*1676Sjpk 			} else {
3535*1676Sjpk 				/*
3536*1676Sjpk 				 * Return related path for current zone.
3537*1676Sjpk 				 */
3538*1676Sjpk 				int prefix_len = strlen(zone_prefix);
3539*1676Sjpk 				int zname_len = strlen(zone->zone_name);
3540*1676Sjpk 
3541*1676Sjpk 				size = prefix_len + zname_len + 1;
3542*1676Sjpk 				zonepath = kmem_alloc(size, KM_SLEEP);
3543*1676Sjpk 				bcopy(zone_prefix, zonepath, prefix_len);
3544*1676Sjpk 				bcopy(zone->zone_name, zonepath +
3545*1676Sjpk 					prefix_len, zname_len);
3546*1676Sjpk 				zonepath[size - 1] = '\0';
3547*1676Sjpk 			}
35480Sstevel@tonic-gate 		}
35490Sstevel@tonic-gate 		if (bufsize > size)
35500Sstevel@tonic-gate 			bufsize = size;
35510Sstevel@tonic-gate 		if (buf != NULL) {
35520Sstevel@tonic-gate 			err = copyoutstr(zonepath, buf, bufsize, NULL);
35530Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
35540Sstevel@tonic-gate 				error = EFAULT;
35550Sstevel@tonic-gate 		}
3556*1676Sjpk 		if (global || (is_system_labeled() && !curzone))
35570Sstevel@tonic-gate 			kmem_free(zonepath, size);
35580Sstevel@tonic-gate 		break;
35590Sstevel@tonic-gate 
35600Sstevel@tonic-gate 	case ZONE_ATTR_NAME:
35610Sstevel@tonic-gate 		size = strlen(zone->zone_name) + 1;
35620Sstevel@tonic-gate 		if (bufsize > size)
35630Sstevel@tonic-gate 			bufsize = size;
35640Sstevel@tonic-gate 		if (buf != NULL) {
35650Sstevel@tonic-gate 			err = copyoutstr(zone->zone_name, buf, bufsize, NULL);
35660Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
35670Sstevel@tonic-gate 				error = EFAULT;
35680Sstevel@tonic-gate 		}
35690Sstevel@tonic-gate 		break;
35700Sstevel@tonic-gate 
35710Sstevel@tonic-gate 	case ZONE_ATTR_STATUS:
35720Sstevel@tonic-gate 		/*
35730Sstevel@tonic-gate 		 * Since we're not holding zonehash_lock, the zone status
35740Sstevel@tonic-gate 		 * may be anything; leave it up to userland to sort it out.
35750Sstevel@tonic-gate 		 */
35760Sstevel@tonic-gate 		size = sizeof (zone_status);
35770Sstevel@tonic-gate 		if (bufsize > size)
35780Sstevel@tonic-gate 			bufsize = size;
35790Sstevel@tonic-gate 		zone_status = zone_status_get(zone);
35800Sstevel@tonic-gate 		if (buf != NULL &&
35810Sstevel@tonic-gate 		    copyout(&zone_status, buf, bufsize) != 0)
35820Sstevel@tonic-gate 			error = EFAULT;
35830Sstevel@tonic-gate 		break;
35840Sstevel@tonic-gate 	case ZONE_ATTR_PRIVSET:
35850Sstevel@tonic-gate 		size = sizeof (priv_set_t);
35860Sstevel@tonic-gate 		if (bufsize > size)
35870Sstevel@tonic-gate 			bufsize = size;
35880Sstevel@tonic-gate 		if (buf != NULL &&
35890Sstevel@tonic-gate 		    copyout(zone->zone_privset, buf, bufsize) != 0)
35900Sstevel@tonic-gate 			error = EFAULT;
35910Sstevel@tonic-gate 		break;
35920Sstevel@tonic-gate 	case ZONE_ATTR_UNIQID:
35930Sstevel@tonic-gate 		size = sizeof (zone->zone_uniqid);
35940Sstevel@tonic-gate 		if (bufsize > size)
35950Sstevel@tonic-gate 			bufsize = size;
35960Sstevel@tonic-gate 		if (buf != NULL &&
35970Sstevel@tonic-gate 		    copyout(&zone->zone_uniqid, buf, bufsize) != 0)
35980Sstevel@tonic-gate 			error = EFAULT;
35990Sstevel@tonic-gate 		break;
36000Sstevel@tonic-gate 	case ZONE_ATTR_POOLID:
36010Sstevel@tonic-gate 		{
36020Sstevel@tonic-gate 			pool_t *pool;
36030Sstevel@tonic-gate 			poolid_t poolid;
36040Sstevel@tonic-gate 
36050Sstevel@tonic-gate 			if (pool_lock_intr() != 0) {
36060Sstevel@tonic-gate 				error = EINTR;
36070Sstevel@tonic-gate 				break;
36080Sstevel@tonic-gate 			}
36090Sstevel@tonic-gate 			pool = zone_pool_get(zone);
36100Sstevel@tonic-gate 			poolid = pool->pool_id;
36110Sstevel@tonic-gate 			pool_unlock();
36120Sstevel@tonic-gate 			size = sizeof (poolid);
36130Sstevel@tonic-gate 			if (bufsize > size)
36140Sstevel@tonic-gate 				bufsize = size;
36150Sstevel@tonic-gate 			if (buf != NULL && copyout(&poolid, buf, size) != 0)
36160Sstevel@tonic-gate 				error = EFAULT;
36170Sstevel@tonic-gate 		}
36180Sstevel@tonic-gate 		break;
3619*1676Sjpk 	case ZONE_ATTR_SLBL:
3620*1676Sjpk 		size = sizeof (bslabel_t);
3621*1676Sjpk 		if (bufsize > size)
3622*1676Sjpk 			bufsize = size;
3623*1676Sjpk 		if (zone->zone_slabel == NULL)
3624*1676Sjpk 			error = EINVAL;
3625*1676Sjpk 		else if (buf != NULL &&
3626*1676Sjpk 		    copyout(label2bslabel(zone->zone_slabel), buf,
3627*1676Sjpk 		    bufsize) != 0)
3628*1676Sjpk 			error = EFAULT;
3629*1676Sjpk 		break;
36300Sstevel@tonic-gate 	case ZONE_ATTR_INITPID:
36310Sstevel@tonic-gate 		size = sizeof (initpid);
36320Sstevel@tonic-gate 		if (bufsize > size)
36330Sstevel@tonic-gate 			bufsize = size;
36340Sstevel@tonic-gate 		initpid = zone->zone_proc_initpid;
36350Sstevel@tonic-gate 		if (initpid == -1) {
36360Sstevel@tonic-gate 			error = ESRCH;
36370Sstevel@tonic-gate 			break;
36380Sstevel@tonic-gate 		}
36390Sstevel@tonic-gate 		if (buf != NULL &&
36400Sstevel@tonic-gate 		    copyout(&initpid, buf, bufsize) != 0)
36410Sstevel@tonic-gate 			error = EFAULT;
36420Sstevel@tonic-gate 		break;
36430Sstevel@tonic-gate 	default:
36440Sstevel@tonic-gate 		error = EINVAL;
36450Sstevel@tonic-gate 	}
36460Sstevel@tonic-gate 	zone_rele(zone);
36470Sstevel@tonic-gate 
36480Sstevel@tonic-gate 	if (error)
36490Sstevel@tonic-gate 		return (set_errno(error));
36500Sstevel@tonic-gate 	return ((ssize_t)size);
36510Sstevel@tonic-gate }
36520Sstevel@tonic-gate 
36530Sstevel@tonic-gate /*
36540Sstevel@tonic-gate  * Return zero if the process has at least one vnode mapped in to its
36550Sstevel@tonic-gate  * address space which shouldn't be allowed to change zones.
36560Sstevel@tonic-gate  */
36570Sstevel@tonic-gate static int
36580Sstevel@tonic-gate as_can_change_zones(void)
36590Sstevel@tonic-gate {
36600Sstevel@tonic-gate 	proc_t *pp = curproc;
36610Sstevel@tonic-gate 	struct seg *seg;
36620Sstevel@tonic-gate 	struct as *as = pp->p_as;
36630Sstevel@tonic-gate 	vnode_t *vp;
36640Sstevel@tonic-gate 	int allow = 1;
36650Sstevel@tonic-gate 
36660Sstevel@tonic-gate 	ASSERT(pp->p_as != &kas);
36670Sstevel@tonic-gate 	AS_LOCK_ENTER(&as, &as->a_lock, RW_READER);
36680Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
36690Sstevel@tonic-gate 		/*
36700Sstevel@tonic-gate 		 * if we can't get a backing vnode for this segment then skip
36710Sstevel@tonic-gate 		 * it.
36720Sstevel@tonic-gate 		 */
36730Sstevel@tonic-gate 		vp = NULL;
36740Sstevel@tonic-gate 		if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL)
36750Sstevel@tonic-gate 			continue;
36760Sstevel@tonic-gate 		if (!vn_can_change_zones(vp)) { /* bail on first match */
36770Sstevel@tonic-gate 			allow = 0;
36780Sstevel@tonic-gate 			break;
36790Sstevel@tonic-gate 		}
36800Sstevel@tonic-gate 	}
36810Sstevel@tonic-gate 	AS_LOCK_EXIT(&as, &as->a_lock);
36820Sstevel@tonic-gate 	return (allow);
36830Sstevel@tonic-gate }
36840Sstevel@tonic-gate 
36850Sstevel@tonic-gate /*
36860Sstevel@tonic-gate  * Systemcall entry point for zone_enter().
36870Sstevel@tonic-gate  *
36880Sstevel@tonic-gate  * The current process is injected into said zone.  In the process
36890Sstevel@tonic-gate  * it will change its project membership, privileges, rootdir/cwd,
36900Sstevel@tonic-gate  * zone-wide rctls, and pool association to match those of the zone.
36910Sstevel@tonic-gate  *
36920Sstevel@tonic-gate  * The first zone_enter() called while the zone is in the ZONE_IS_READY
36930Sstevel@tonic-gate  * state will transition it to ZONE_IS_RUNNING.  Processes may only
36940Sstevel@tonic-gate  * enter a zone that is "ready" or "running".
36950Sstevel@tonic-gate  */
36960Sstevel@tonic-gate static int
36970Sstevel@tonic-gate zone_enter(zoneid_t zoneid)
36980Sstevel@tonic-gate {
36990Sstevel@tonic-gate 	zone_t *zone;
37000Sstevel@tonic-gate 	vnode_t *vp;
37010Sstevel@tonic-gate 	proc_t *pp = curproc;
37020Sstevel@tonic-gate 	contract_t *ct;
37030Sstevel@tonic-gate 	cont_process_t *ctp;
37040Sstevel@tonic-gate 	task_t *tk, *oldtk;
37050Sstevel@tonic-gate 	kproject_t *zone_proj0;
37060Sstevel@tonic-gate 	cred_t *cr, *newcr;
37070Sstevel@tonic-gate 	pool_t *oldpool, *newpool;
37080Sstevel@tonic-gate 	sess_t *sp;
37090Sstevel@tonic-gate 	uid_t uid;
37100Sstevel@tonic-gate 	zone_status_t status;
37110Sstevel@tonic-gate 	int err = 0;
37120Sstevel@tonic-gate 	rctl_entity_p_t e;
37130Sstevel@tonic-gate 
37140Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
37150Sstevel@tonic-gate 		return (set_errno(EPERM));
37160Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
37170Sstevel@tonic-gate 		return (set_errno(EINVAL));
37180Sstevel@tonic-gate 
37190Sstevel@tonic-gate 	/*
37200Sstevel@tonic-gate 	 * Stop all lwps so we don't need to hold a lock to look at
37210Sstevel@tonic-gate 	 * curproc->p_zone.  This needs to happen before we grab any
37220Sstevel@tonic-gate 	 * locks to avoid deadlock (another lwp in the process could
37230Sstevel@tonic-gate 	 * be waiting for the held lock).
37240Sstevel@tonic-gate 	 */
37250Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK))
37260Sstevel@tonic-gate 		return (set_errno(EINTR));
37270Sstevel@tonic-gate 
37280Sstevel@tonic-gate 	/*
37290Sstevel@tonic-gate 	 * Make sure we're not changing zones with files open or mapped in
37300Sstevel@tonic-gate 	 * to our address space which shouldn't be changing zones.
37310Sstevel@tonic-gate 	 */
37320Sstevel@tonic-gate 	if (!files_can_change_zones()) {
37330Sstevel@tonic-gate 		err = EBADF;
37340Sstevel@tonic-gate 		goto out;
37350Sstevel@tonic-gate 	}
37360Sstevel@tonic-gate 	if (!as_can_change_zones()) {
37370Sstevel@tonic-gate 		err = EFAULT;
37380Sstevel@tonic-gate 		goto out;
37390Sstevel@tonic-gate 	}
37400Sstevel@tonic-gate 
37410Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
37420Sstevel@tonic-gate 	if (pp->p_zone != global_zone) {
37430Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
37440Sstevel@tonic-gate 		err = EINVAL;
37450Sstevel@tonic-gate 		goto out;
37460Sstevel@tonic-gate 	}
37470Sstevel@tonic-gate 
37480Sstevel@tonic-gate 	zone = zone_find_all_by_id(zoneid);
37490Sstevel@tonic-gate 	if (zone == NULL) {
37500Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
37510Sstevel@tonic-gate 		err = EINVAL;
37520Sstevel@tonic-gate 		goto out;
37530Sstevel@tonic-gate 	}
37540Sstevel@tonic-gate 
37550Sstevel@tonic-gate 	/*
37560Sstevel@tonic-gate 	 * To prevent processes in a zone from holding contracts on
37570Sstevel@tonic-gate 	 * extrazonal resources, and to avoid process contract
37580Sstevel@tonic-gate 	 * memberships which span zones, contract holders and processes
37590Sstevel@tonic-gate 	 * which aren't the sole members of their encapsulating process
37600Sstevel@tonic-gate 	 * contracts are not allowed to zone_enter.
37610Sstevel@tonic-gate 	 */
37620Sstevel@tonic-gate 	ctp = pp->p_ct_process;
37630Sstevel@tonic-gate 	ct = &ctp->conp_contract;
37640Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
37650Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
37660Sstevel@tonic-gate 	if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) {
37670Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
37680Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
37690Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
37700Sstevel@tonic-gate 		pool_unlock();
37710Sstevel@tonic-gate 		err = EINVAL;
37720Sstevel@tonic-gate 		goto out;
37730Sstevel@tonic-gate 	}
37740Sstevel@tonic-gate 
37750Sstevel@tonic-gate 	/*
37760Sstevel@tonic-gate 	 * Moreover, we don't allow processes whose encapsulating
37770Sstevel@tonic-gate 	 * process contracts have inherited extrazonal contracts.
37780Sstevel@tonic-gate 	 * While it would be easier to eliminate all process contracts
37790Sstevel@tonic-gate 	 * with inherited contracts, we need to be able to give a
37800Sstevel@tonic-gate 	 * restarted init (or other zone-penetrating process) its
37810Sstevel@tonic-gate 	 * predecessor's contracts.
37820Sstevel@tonic-gate 	 */
37830Sstevel@tonic-gate 	if (ctp->conp_ninherited != 0) {
37840Sstevel@tonic-gate 		contract_t *next;
37850Sstevel@tonic-gate 		for (next = list_head(&ctp->conp_inherited); next;
37860Sstevel@tonic-gate 		    next = list_next(&ctp->conp_inherited, next)) {
37870Sstevel@tonic-gate 			if (contract_getzuniqid(next) != zone->zone_uniqid) {
37880Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
37890Sstevel@tonic-gate 				mutex_exit(&ct->ct_lock);
37900Sstevel@tonic-gate 				mutex_exit(&zonehash_lock);
37910Sstevel@tonic-gate 				pool_unlock();
37920Sstevel@tonic-gate 				err = EINVAL;
37930Sstevel@tonic-gate 				goto out;
37940Sstevel@tonic-gate 			}
37950Sstevel@tonic-gate 		}
37960Sstevel@tonic-gate 	}
37970Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
37980Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
37990Sstevel@tonic-gate 
38000Sstevel@tonic-gate 	status = zone_status_get(zone);
38010Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) {
38020Sstevel@tonic-gate 		/*
38030Sstevel@tonic-gate 		 * Can't join
38040Sstevel@tonic-gate 		 */
38050Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
38060Sstevel@tonic-gate 		err = EINVAL;
38070Sstevel@tonic-gate 		goto out;
38080Sstevel@tonic-gate 	}
38090Sstevel@tonic-gate 
38100Sstevel@tonic-gate 	/*
38110Sstevel@tonic-gate 	 * Make sure new priv set is within the permitted set for caller
38120Sstevel@tonic-gate 	 */
38130Sstevel@tonic-gate 	if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) {
38140Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
38150Sstevel@tonic-gate 		err = EPERM;
38160Sstevel@tonic-gate 		goto out;
38170Sstevel@tonic-gate 	}
38180Sstevel@tonic-gate 	/*
38190Sstevel@tonic-gate 	 * We want to momentarily drop zonehash_lock while we optimistically
38200Sstevel@tonic-gate 	 * bind curproc to the pool it should be running in.  This is safe
38210Sstevel@tonic-gate 	 * since the zone can't disappear (we have a hold on it).
38220Sstevel@tonic-gate 	 */
38230Sstevel@tonic-gate 	zone_hold(zone);
38240Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
38250Sstevel@tonic-gate 
38260Sstevel@tonic-gate 	/*
38270Sstevel@tonic-gate 	 * Grab pool_lock to keep the pools configuration from changing
38280Sstevel@tonic-gate 	 * and to stop ourselves from getting rebound to another pool
38290Sstevel@tonic-gate 	 * until we join the zone.
38300Sstevel@tonic-gate 	 */
38310Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
38320Sstevel@tonic-gate 		zone_rele(zone);
38330Sstevel@tonic-gate 		err = EINTR;
38340Sstevel@tonic-gate 		goto out;
38350Sstevel@tonic-gate 	}
38360Sstevel@tonic-gate 	ASSERT(secpolicy_pool(CRED()) == 0);
38370Sstevel@tonic-gate 	/*
38380Sstevel@tonic-gate 	 * Bind ourselves to the pool currently associated with the zone.
38390Sstevel@tonic-gate 	 */
38400Sstevel@tonic-gate 	oldpool = curproc->p_pool;
38410Sstevel@tonic-gate 	newpool = zone_pool_get(zone);
38420Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED && newpool != oldpool &&
38430Sstevel@tonic-gate 	    (err = pool_do_bind(newpool, P_PID, P_MYID,
38440Sstevel@tonic-gate 	    POOL_BIND_ALL)) != 0) {
38450Sstevel@tonic-gate 		pool_unlock();
38460Sstevel@tonic-gate 		zone_rele(zone);
38470Sstevel@tonic-gate 		goto out;
38480Sstevel@tonic-gate 	}
38490Sstevel@tonic-gate 
38500Sstevel@tonic-gate 	/*
38510Sstevel@tonic-gate 	 * Grab cpu_lock now; we'll need it later when we call
38520Sstevel@tonic-gate 	 * task_join().
38530Sstevel@tonic-gate 	 */
38540Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
38550Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
38560Sstevel@tonic-gate 	/*
38570Sstevel@tonic-gate 	 * Make sure the zone hasn't moved on since we dropped zonehash_lock.
38580Sstevel@tonic-gate 	 */
38590Sstevel@tonic-gate 	if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
38600Sstevel@tonic-gate 		/*
38610Sstevel@tonic-gate 		 * Can't join anymore.
38620Sstevel@tonic-gate 		 */
38630Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
38640Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
38650Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED &&
38660Sstevel@tonic-gate 		    newpool != oldpool)
38670Sstevel@tonic-gate 			(void) pool_do_bind(oldpool, P_PID, P_MYID,
38680Sstevel@tonic-gate 			    POOL_BIND_ALL);
38690Sstevel@tonic-gate 		pool_unlock();
38700Sstevel@tonic-gate 		zone_rele(zone);
38710Sstevel@tonic-gate 		err = EINVAL;
38720Sstevel@tonic-gate 		goto out;
38730Sstevel@tonic-gate 	}
38740Sstevel@tonic-gate 
38750Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
38760Sstevel@tonic-gate 	zone_proj0 = zone->zone_zsched->p_task->tk_proj;
38770Sstevel@tonic-gate 	/* verify that we do not exceed and task or lwp limits */
38780Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
38790Sstevel@tonic-gate 	/* add new lwps to zone and zone's proj0 */
38800Sstevel@tonic-gate 	zone_proj0->kpj_nlwps += pp->p_lwpcnt;
38810Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
38820Sstevel@tonic-gate 	/* add 1 task to zone's proj0 */
38830Sstevel@tonic-gate 	zone_proj0->kpj_ntasks += 1;
38840Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
38850Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
38860Sstevel@tonic-gate 
38870Sstevel@tonic-gate 	/* remove lwps from proc's old zone and old project */
38880Sstevel@tonic-gate 	mutex_enter(&pp->p_zone->zone_nlwps_lock);
38890Sstevel@tonic-gate 	pp->p_zone->zone_nlwps -= pp->p_lwpcnt;
38900Sstevel@tonic-gate 	pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt;
38910Sstevel@tonic-gate 	mutex_exit(&pp->p_zone->zone_nlwps_lock);
38920Sstevel@tonic-gate 
38930Sstevel@tonic-gate 	/*
38940Sstevel@tonic-gate 	 * Joining the zone cannot fail from now on.
38950Sstevel@tonic-gate 	 *
38960Sstevel@tonic-gate 	 * This means that a lot of the following code can be commonized and
38970Sstevel@tonic-gate 	 * shared with zsched().
38980Sstevel@tonic-gate 	 */
38990Sstevel@tonic-gate 
39000Sstevel@tonic-gate 	/*
39010Sstevel@tonic-gate 	 * Reset the encapsulating process contract's zone.
39020Sstevel@tonic-gate 	 */
39030Sstevel@tonic-gate 	ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID);
39040Sstevel@tonic-gate 	contract_setzuniqid(ct, zone->zone_uniqid);
39050Sstevel@tonic-gate 
39060Sstevel@tonic-gate 	/*
39070Sstevel@tonic-gate 	 * Create a new task and associate the process with the project keyed
39080Sstevel@tonic-gate 	 * by (projid,zoneid).
39090Sstevel@tonic-gate 	 *
39100Sstevel@tonic-gate 	 * We might as well be in project 0; the global zone's projid doesn't
39110Sstevel@tonic-gate 	 * make much sense in a zone anyhow.
39120Sstevel@tonic-gate 	 *
39130Sstevel@tonic-gate 	 * This also increments zone_ntasks, and returns with p_lock held.
39140Sstevel@tonic-gate 	 */
39150Sstevel@tonic-gate 	tk = task_create(0, zone);
39160Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
39170Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
39180Sstevel@tonic-gate 
39190Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
39200Sstevel@tonic-gate 	pp->p_zone = zone;
39210Sstevel@tonic-gate 
39220Sstevel@tonic-gate 	/*
39230Sstevel@tonic-gate 	 * call RCTLOP_SET functions on this proc
39240Sstevel@tonic-gate 	 */
39250Sstevel@tonic-gate 	e.rcep_p.zone = zone;
39260Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
39270Sstevel@tonic-gate 	(void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL,
39280Sstevel@tonic-gate 	    RCD_CALLBACK);
39290Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
39300Sstevel@tonic-gate 
39310Sstevel@tonic-gate 	/*
39320Sstevel@tonic-gate 	 * We don't need to hold any of zsched's locks here; not only do we know
39330Sstevel@tonic-gate 	 * the process and zone aren't going away, we know its session isn't
39340Sstevel@tonic-gate 	 * changing either.
39350Sstevel@tonic-gate 	 *
39360Sstevel@tonic-gate 	 * By joining zsched's session here, we mimic the behavior in the
39370Sstevel@tonic-gate 	 * global zone of init's sid being the pid of sched.  We extend this
39380Sstevel@tonic-gate 	 * to all zlogin-like zone_enter()'ing processes as well.
39390Sstevel@tonic-gate 	 */
39400Sstevel@tonic-gate 	mutex_enter(&pidlock);
39410Sstevel@tonic-gate 	sp = zone->zone_zsched->p_sessp;
39420Sstevel@tonic-gate 	SESS_HOLD(sp);
39430Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
39440Sstevel@tonic-gate 	pgexit(pp);
39450Sstevel@tonic-gate 	SESS_RELE(pp->p_sessp);
39460Sstevel@tonic-gate 	pp->p_sessp = sp;
39470Sstevel@tonic-gate 	pgjoin(pp, zone->zone_zsched->p_pidp);
39480Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
39490Sstevel@tonic-gate 	mutex_exit(&pidlock);
39500Sstevel@tonic-gate 
39510Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
39520Sstevel@tonic-gate 	/*
39530Sstevel@tonic-gate 	 * We're firmly in the zone; let pools progress.
39540Sstevel@tonic-gate 	 */
39550Sstevel@tonic-gate 	pool_unlock();
39560Sstevel@tonic-gate 	task_rele(oldtk);
39570Sstevel@tonic-gate 	/*
39580Sstevel@tonic-gate 	 * We don't need to retain a hold on the zone since we already
39590Sstevel@tonic-gate 	 * incremented zone_ntasks, so the zone isn't going anywhere.
39600Sstevel@tonic-gate 	 */
39610Sstevel@tonic-gate 	zone_rele(zone);
39620Sstevel@tonic-gate 
39630Sstevel@tonic-gate 	/*
39640Sstevel@tonic-gate 	 * Chroot
39650Sstevel@tonic-gate 	 */
39660Sstevel@tonic-gate 	vp = zone->zone_rootvp;
39670Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_cdir, pp);
39680Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_rdir, pp);
39690Sstevel@tonic-gate 
39700Sstevel@tonic-gate 	/*
39710Sstevel@tonic-gate 	 * Change process credentials
39720Sstevel@tonic-gate 	 */
39730Sstevel@tonic-gate 	newcr = cralloc();
39740Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
39750Sstevel@tonic-gate 	cr = pp->p_cred;
39760Sstevel@tonic-gate 	crcopy_to(cr, newcr);
39770Sstevel@tonic-gate 	crsetzone(newcr, zone);
39780Sstevel@tonic-gate 	pp->p_cred = newcr;
39790Sstevel@tonic-gate 
39800Sstevel@tonic-gate 	/*
39810Sstevel@tonic-gate 	 * Restrict all process privilege sets to zone limit
39820Sstevel@tonic-gate 	 */
39830Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(newcr));
39840Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(newcr));
39850Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(newcr));
39860Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(newcr));
39870Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
39880Sstevel@tonic-gate 	crset(pp, newcr);
39890Sstevel@tonic-gate 
39900Sstevel@tonic-gate 	/*
39910Sstevel@tonic-gate 	 * Adjust upcount to reflect zone entry.
39920Sstevel@tonic-gate 	 */
39930Sstevel@tonic-gate 	uid = crgetruid(newcr);
39940Sstevel@tonic-gate 	mutex_enter(&pidlock);
39950Sstevel@tonic-gate 	upcount_dec(uid, GLOBAL_ZONEID);
39960Sstevel@tonic-gate 	upcount_inc(uid, zoneid);
39970Sstevel@tonic-gate 	mutex_exit(&pidlock);
39980Sstevel@tonic-gate 
39990Sstevel@tonic-gate 	/*
40000Sstevel@tonic-gate 	 * Set up core file path and content.
40010Sstevel@tonic-gate 	 */
40020Sstevel@tonic-gate 	set_core_defaults();
40030Sstevel@tonic-gate 
40040Sstevel@tonic-gate out:
40050Sstevel@tonic-gate 	/*
40060Sstevel@tonic-gate 	 * Let the other lwps continue.
40070Sstevel@tonic-gate 	 */
40080Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
40090Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
40100Sstevel@tonic-gate 		continuelwps(pp);
40110Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
40120Sstevel@tonic-gate 
40130Sstevel@tonic-gate 	return (err != 0 ? set_errno(err) : 0);
40140Sstevel@tonic-gate }
40150Sstevel@tonic-gate 
40160Sstevel@tonic-gate /*
40170Sstevel@tonic-gate  * Systemcall entry point for zone_list(2).
40180Sstevel@tonic-gate  *
40190Sstevel@tonic-gate  * Processes running in a (non-global) zone only see themselves.
4020*1676Sjpk  * On labeled systems, they see all zones whose label they dominate.
40210Sstevel@tonic-gate  */
40220Sstevel@tonic-gate static int
40230Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones)
40240Sstevel@tonic-gate {
40250Sstevel@tonic-gate 	zoneid_t *zoneids;
40260Sstevel@tonic-gate 	zone_t *zone;
40270Sstevel@tonic-gate 	uint_t user_nzones, real_nzones;
4028*1676Sjpk 	uint_t domi_nzones;
4029*1676Sjpk 	int error;
40300Sstevel@tonic-gate 
40310Sstevel@tonic-gate 	if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0)
40320Sstevel@tonic-gate 		return (set_errno(EFAULT));
40330Sstevel@tonic-gate 
40340Sstevel@tonic-gate 	if (curproc->p_zone != global_zone) {
4035*1676Sjpk 		bslabel_t *mybslab;
4036*1676Sjpk 
4037*1676Sjpk 		if (!is_system_labeled()) {
4038*1676Sjpk 			/* just return current zone */
4039*1676Sjpk 			real_nzones = domi_nzones = 1;
4040*1676Sjpk 			zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP);
4041*1676Sjpk 			zoneids[0] = curproc->p_zone->zone_id;
4042*1676Sjpk 		} else {
4043*1676Sjpk 			/* return all zones that are dominated */
4044*1676Sjpk 			mutex_enter(&zonehash_lock);
4045*1676Sjpk 			real_nzones = zonecount;
4046*1676Sjpk 			domi_nzones = 0;
4047*1676Sjpk 			if (real_nzones > 0) {
4048*1676Sjpk 				zoneids = kmem_alloc(real_nzones *
4049*1676Sjpk 				    sizeof (zoneid_t), KM_SLEEP);
4050*1676Sjpk 				mybslab = label2bslabel(curproc->p_zone->
4051*1676Sjpk 				    zone_slabel);
4052*1676Sjpk 				for (zone = list_head(&zone_active);
4053*1676Sjpk 				    zone != NULL;
4054*1676Sjpk 				    zone = list_next(&zone_active, zone)) {
4055*1676Sjpk 					if (zone->zone_id == GLOBAL_ZONEID)
4056*1676Sjpk 						continue;
4057*1676Sjpk 					if (bldominates(mybslab,
4058*1676Sjpk 					    label2bslabel(zone->zone_slabel))) {
4059*1676Sjpk 						zoneids[domi_nzones++] =
4060*1676Sjpk 						    zone->zone_id;
4061*1676Sjpk 					}
4062*1676Sjpk 				}
4063*1676Sjpk 			}
4064*1676Sjpk 			mutex_exit(&zonehash_lock);
4065*1676Sjpk 		}
40660Sstevel@tonic-gate 	} else {
40670Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
40680Sstevel@tonic-gate 		real_nzones = zonecount;
4069*1676Sjpk 		domi_nzones = 0;
4070*1676Sjpk 		if (real_nzones > 0) {
40710Sstevel@tonic-gate 			zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t),
40720Sstevel@tonic-gate 			    KM_SLEEP);
40730Sstevel@tonic-gate 			for (zone = list_head(&zone_active); zone != NULL;
40740Sstevel@tonic-gate 			    zone = list_next(&zone_active, zone))
4075*1676Sjpk 				zoneids[domi_nzones++] = zone->zone_id;
4076*1676Sjpk 			ASSERT(domi_nzones == real_nzones);
40770Sstevel@tonic-gate 		}
40780Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
40790Sstevel@tonic-gate 	}
40800Sstevel@tonic-gate 
4081*1676Sjpk 	/*
4082*1676Sjpk 	 * If user has allocated space for fewer entries than we found, then
4083*1676Sjpk 	 * return only up to his limit.  Either way, tell him exactly how many
4084*1676Sjpk 	 * we found.
4085*1676Sjpk 	 */
4086*1676Sjpk 	if (domi_nzones < user_nzones)
4087*1676Sjpk 		user_nzones = domi_nzones;
4088*1676Sjpk 	error = 0;
4089*1676Sjpk 	if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) {
40900Sstevel@tonic-gate 		error = EFAULT;
4091*1676Sjpk 	} else if (zoneidlist != NULL && user_nzones != 0) {
40920Sstevel@tonic-gate 		if (copyout(zoneids, zoneidlist,
40930Sstevel@tonic-gate 		    user_nzones * sizeof (zoneid_t)) != 0)
40940Sstevel@tonic-gate 			error = EFAULT;
40950Sstevel@tonic-gate 	}
40960Sstevel@tonic-gate 
4097*1676Sjpk 	if (real_nzones > 0)
40980Sstevel@tonic-gate 		kmem_free(zoneids, real_nzones * sizeof (zoneid_t));
40990Sstevel@tonic-gate 
4100*1676Sjpk 	if (error != 0)
41010Sstevel@tonic-gate 		return (set_errno(error));
41020Sstevel@tonic-gate 	else
41030Sstevel@tonic-gate 		return (0);
41040Sstevel@tonic-gate }
41050Sstevel@tonic-gate 
41060Sstevel@tonic-gate /*
41070Sstevel@tonic-gate  * Systemcall entry point for zone_lookup(2).
41080Sstevel@tonic-gate  *
4109*1676Sjpk  * Non-global zones are only able to see themselves and (on labeled systems)
4110*1676Sjpk  * the zones they dominate.
41110Sstevel@tonic-gate  */
41120Sstevel@tonic-gate static zoneid_t
41130Sstevel@tonic-gate zone_lookup(const char *zone_name)
41140Sstevel@tonic-gate {
41150Sstevel@tonic-gate 	char *kname;
41160Sstevel@tonic-gate 	zone_t *zone;
41170Sstevel@tonic-gate 	zoneid_t zoneid;
41180Sstevel@tonic-gate 	int err;
41190Sstevel@tonic-gate 
41200Sstevel@tonic-gate 	if (zone_name == NULL) {
41210Sstevel@tonic-gate 		/* return caller's zone id */
41220Sstevel@tonic-gate 		return (getzoneid());
41230Sstevel@tonic-gate 	}
41240Sstevel@tonic-gate 
41250Sstevel@tonic-gate 	kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
41260Sstevel@tonic-gate 	if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) {
41270Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
41280Sstevel@tonic-gate 		return (set_errno(err));
41290Sstevel@tonic-gate 	}
41300Sstevel@tonic-gate 
41310Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
41320Sstevel@tonic-gate 	zone = zone_find_all_by_name(kname);
41330Sstevel@tonic-gate 	kmem_free(kname, ZONENAME_MAX);
4134*1676Sjpk 	/*
4135*1676Sjpk 	 * In a non-global zone, can only lookup global and own name.
4136*1676Sjpk 	 * In Trusted Extensions zone label dominance rules apply.
4137*1676Sjpk 	 */
4138*1676Sjpk 	if (zone == NULL ||
4139*1676Sjpk 	    zone_status_get(zone) < ZONE_IS_READY ||
4140*1676Sjpk 	    !zone_list_access(zone)) {
41410Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
41420Sstevel@tonic-gate 		return (set_errno(EINVAL));
4143*1676Sjpk 	} else {
4144*1676Sjpk 		zoneid = zone->zone_id;
4145*1676Sjpk 		mutex_exit(&zonehash_lock);
4146*1676Sjpk 		return (zoneid);
41470Sstevel@tonic-gate 	}
41480Sstevel@tonic-gate }
41490Sstevel@tonic-gate 
4150813Sdp static int
4151813Sdp zone_version(int *version_arg)
4152813Sdp {
4153813Sdp 	int version = ZONE_SYSCALL_API_VERSION;
4154813Sdp 
4155813Sdp 	if (copyout(&version, version_arg, sizeof (int)) != 0)
4156813Sdp 		return (set_errno(EFAULT));
4157813Sdp 	return (0);
4158813Sdp }
4159813Sdp 
41600Sstevel@tonic-gate /* ARGSUSED */
41610Sstevel@tonic-gate long
4162789Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4)
41630Sstevel@tonic-gate {
41640Sstevel@tonic-gate 	zone_def zs;
41650Sstevel@tonic-gate 
41660Sstevel@tonic-gate 	switch (cmd) {
41670Sstevel@tonic-gate 	case ZONE_CREATE:
41680Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
41690Sstevel@tonic-gate 			if (copyin(arg1, &zs, sizeof (zone_def))) {
41700Sstevel@tonic-gate 				return (set_errno(EFAULT));
41710Sstevel@tonic-gate 			}
41720Sstevel@tonic-gate 		} else {
41730Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
41740Sstevel@tonic-gate 			zone_def32 zs32;
41750Sstevel@tonic-gate 
41760Sstevel@tonic-gate 			if (copyin(arg1, &zs32, sizeof (zone_def32))) {
41770Sstevel@tonic-gate 				return (set_errno(EFAULT));
41780Sstevel@tonic-gate 			}
41790Sstevel@tonic-gate 			zs.zone_name =
41800Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_name;
41810Sstevel@tonic-gate 			zs.zone_root =
41820Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_root;
41830Sstevel@tonic-gate 			zs.zone_privs =
41840Sstevel@tonic-gate 			    (const struct priv_set *)
41850Sstevel@tonic-gate 			    (unsigned long)zs32.zone_privs;
41861409Sdp 			zs.zone_privssz = zs32.zone_privssz;
41870Sstevel@tonic-gate 			zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf;
41880Sstevel@tonic-gate 			zs.rctlbufsz = zs32.rctlbufsz;
4189789Sahrens 			zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf;
4190789Sahrens 			zs.zfsbufsz = zs32.zfsbufsz;
41910Sstevel@tonic-gate 			zs.extended_error =
41920Sstevel@tonic-gate 			    (int *)(unsigned long)zs32.extended_error;
4193*1676Sjpk 			zs.match = zs32.match;
4194*1676Sjpk 			zs.doi = zs32.doi;
4195*1676Sjpk 			zs.label = (const bslabel_t *)(uintptr_t)zs32.label;
41960Sstevel@tonic-gate #else
41970Sstevel@tonic-gate 			panic("get_udatamodel() returned bogus result\n");
41980Sstevel@tonic-gate #endif
41990Sstevel@tonic-gate 		}
42000Sstevel@tonic-gate 
42010Sstevel@tonic-gate 		return (zone_create(zs.zone_name, zs.zone_root,
4202813Sdp 		    zs.zone_privs, zs.zone_privssz,
4203813Sdp 		    (caddr_t)zs.rctlbuf, zs.rctlbufsz,
4204813Sdp 		    (caddr_t)zs.zfsbuf, zs.zfsbufsz,
4205*1676Sjpk 		    zs.extended_error, zs.match, zs.doi,
4206*1676Sjpk 		    zs.label));
42070Sstevel@tonic-gate 	case ZONE_BOOT:
42080Sstevel@tonic-gate 		return (zone_boot((zoneid_t)(uintptr_t)arg1,
42090Sstevel@tonic-gate 		    (const char *)arg2));
42100Sstevel@tonic-gate 	case ZONE_DESTROY:
42110Sstevel@tonic-gate 		return (zone_destroy((zoneid_t)(uintptr_t)arg1));
42120Sstevel@tonic-gate 	case ZONE_GETATTR:
42130Sstevel@tonic-gate 		return (zone_getattr((zoneid_t)(uintptr_t)arg1,
42140Sstevel@tonic-gate 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
42150Sstevel@tonic-gate 	case ZONE_ENTER:
42160Sstevel@tonic-gate 		return (zone_enter((zoneid_t)(uintptr_t)arg1));
42170Sstevel@tonic-gate 	case ZONE_LIST:
42180Sstevel@tonic-gate 		return (zone_list((zoneid_t *)arg1, (uint_t *)arg2));
42190Sstevel@tonic-gate 	case ZONE_SHUTDOWN:
42200Sstevel@tonic-gate 		return (zone_shutdown((zoneid_t)(uintptr_t)arg1));
42210Sstevel@tonic-gate 	case ZONE_LOOKUP:
42220Sstevel@tonic-gate 		return (zone_lookup((const char *)arg1));
4223813Sdp 	case ZONE_VERSION:
4224813Sdp 		return (zone_version((int *)arg1));
42250Sstevel@tonic-gate 	default:
42260Sstevel@tonic-gate 		return (set_errno(EINVAL));
42270Sstevel@tonic-gate 	}
42280Sstevel@tonic-gate }
42290Sstevel@tonic-gate 
42300Sstevel@tonic-gate struct zarg {
42310Sstevel@tonic-gate 	zone_t *zone;
42320Sstevel@tonic-gate 	zone_cmd_arg_t arg;
42330Sstevel@tonic-gate };
42340Sstevel@tonic-gate 
42350Sstevel@tonic-gate static int
42360Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp)
42370Sstevel@tonic-gate {
42380Sstevel@tonic-gate 	char *buf;
42390Sstevel@tonic-gate 	size_t buflen;
42400Sstevel@tonic-gate 	int error;
42410Sstevel@tonic-gate 
42420Sstevel@tonic-gate 	buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name);
42430Sstevel@tonic-gate 	buf = kmem_alloc(buflen, KM_SLEEP);
42440Sstevel@tonic-gate 	(void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name);
42450Sstevel@tonic-gate 	error = door_ki_open(buf, doorp);
42460Sstevel@tonic-gate 	kmem_free(buf, buflen);
42470Sstevel@tonic-gate 	return (error);
42480Sstevel@tonic-gate }
42490Sstevel@tonic-gate 
42500Sstevel@tonic-gate static void
42510Sstevel@tonic-gate zone_release_door(door_handle_t *doorp)
42520Sstevel@tonic-gate {
42530Sstevel@tonic-gate 	door_ki_rele(*doorp);
42540Sstevel@tonic-gate 	*doorp = NULL;
42550Sstevel@tonic-gate }
42560Sstevel@tonic-gate 
42570Sstevel@tonic-gate static void
42580Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp)
42590Sstevel@tonic-gate {
42600Sstevel@tonic-gate 	door_handle_t door = NULL;
42610Sstevel@tonic-gate 	door_arg_t darg, save_arg;
42620Sstevel@tonic-gate 	char *zone_name;
42630Sstevel@tonic-gate 	size_t zone_namelen;
42640Sstevel@tonic-gate 	zoneid_t zoneid;
42650Sstevel@tonic-gate 	zone_t *zone;
42660Sstevel@tonic-gate 	zone_cmd_arg_t arg;
42670Sstevel@tonic-gate 	uint64_t uniqid;
42680Sstevel@tonic-gate 	size_t size;
42690Sstevel@tonic-gate 	int error;
42700Sstevel@tonic-gate 	int retry;
42710Sstevel@tonic-gate 
42720Sstevel@tonic-gate 	zone = zargp->zone;
42730Sstevel@tonic-gate 	arg = zargp->arg;
42740Sstevel@tonic-gate 	kmem_free(zargp, sizeof (*zargp));
42750Sstevel@tonic-gate 
42760Sstevel@tonic-gate 	zone_namelen = strlen(zone->zone_name) + 1;
42770Sstevel@tonic-gate 	zone_name = kmem_alloc(zone_namelen, KM_SLEEP);
42780Sstevel@tonic-gate 	bcopy(zone->zone_name, zone_name, zone_namelen);
42790Sstevel@tonic-gate 	zoneid = zone->zone_id;
42800Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
42810Sstevel@tonic-gate 	/*
42820Sstevel@tonic-gate 	 * zoneadmd may be down, but at least we can empty out the zone.
42830Sstevel@tonic-gate 	 * We can ignore the return value of zone_empty() since we're called
42840Sstevel@tonic-gate 	 * from a kernel thread and know we won't be delivered any signals.
42850Sstevel@tonic-gate 	 */
42860Sstevel@tonic-gate 	ASSERT(curproc == &p0);
42870Sstevel@tonic-gate 	(void) zone_empty(zone);
42880Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY);
42890Sstevel@tonic-gate 	zone_rele(zone);
42900Sstevel@tonic-gate 
42910Sstevel@tonic-gate 	size = sizeof (arg);
42920Sstevel@tonic-gate 	darg.rbuf = (char *)&arg;
42930Sstevel@tonic-gate 	darg.data_ptr = (char *)&arg;
42940Sstevel@tonic-gate 	darg.rsize = size;
42950Sstevel@tonic-gate 	darg.data_size = size;
42960Sstevel@tonic-gate 	darg.desc_ptr = NULL;
42970Sstevel@tonic-gate 	darg.desc_num = 0;
42980Sstevel@tonic-gate 
42990Sstevel@tonic-gate 	save_arg = darg;
43000Sstevel@tonic-gate 	/*
43010Sstevel@tonic-gate 	 * Since we're not holding a reference to the zone, any number of
43020Sstevel@tonic-gate 	 * things can go wrong, including the zone disappearing before we get a
43030Sstevel@tonic-gate 	 * chance to talk to zoneadmd.
43040Sstevel@tonic-gate 	 */
43050Sstevel@tonic-gate 	for (retry = 0; /* forever */; retry++) {
43060Sstevel@tonic-gate 		if (door == NULL &&
43070Sstevel@tonic-gate 		    (error = zone_lookup_door(zone_name, &door)) != 0) {
43080Sstevel@tonic-gate 			goto next;
43090Sstevel@tonic-gate 		}
43100Sstevel@tonic-gate 		ASSERT(door != NULL);
43110Sstevel@tonic-gate 
43120Sstevel@tonic-gate 		if ((error = door_ki_upcall(door, &darg)) == 0) {
43130Sstevel@tonic-gate 			break;
43140Sstevel@tonic-gate 		}
43150Sstevel@tonic-gate 		switch (error) {
43160Sstevel@tonic-gate 		case EINTR:
43170Sstevel@tonic-gate 			/* FALLTHROUGH */
43180Sstevel@tonic-gate 		case EAGAIN:	/* process may be forking */
43190Sstevel@tonic-gate 			/*
43200Sstevel@tonic-gate 			 * Back off for a bit
43210Sstevel@tonic-gate 			 */
43220Sstevel@tonic-gate 			break;
43230Sstevel@tonic-gate 		case EBADF:
43240Sstevel@tonic-gate 			zone_release_door(&door);
43250Sstevel@tonic-gate 			if (zone_lookup_door(zone_name, &door) != 0) {
43260Sstevel@tonic-gate 				/*
43270Sstevel@tonic-gate 				 * zoneadmd may be dead, but it may come back to
43280Sstevel@tonic-gate 				 * life later.
43290Sstevel@tonic-gate 				 */
43300Sstevel@tonic-gate 				break;
43310Sstevel@tonic-gate 			}
43320Sstevel@tonic-gate 			break;
43330Sstevel@tonic-gate 		default:
43340Sstevel@tonic-gate 			cmn_err(CE_WARN,
43350Sstevel@tonic-gate 			    "zone_ki_call_zoneadmd: door_ki_upcall error %d\n",
43360Sstevel@tonic-gate 			    error);
43370Sstevel@tonic-gate 			goto out;
43380Sstevel@tonic-gate 		}
43390Sstevel@tonic-gate next:
43400Sstevel@tonic-gate 		/*
43410Sstevel@tonic-gate 		 * If this isn't the same zone_t that we originally had in mind,
43420Sstevel@tonic-gate 		 * then this is the same as if two kadmin requests come in at
43430Sstevel@tonic-gate 		 * the same time: the first one wins.  This means we lose, so we
43440Sstevel@tonic-gate 		 * bail.
43450Sstevel@tonic-gate 		 */
43460Sstevel@tonic-gate 		if ((zone = zone_find_by_id(zoneid)) == NULL) {
43470Sstevel@tonic-gate 			/*
43480Sstevel@tonic-gate 			 * Problem is solved.
43490Sstevel@tonic-gate 			 */
43500Sstevel@tonic-gate 			break;
43510Sstevel@tonic-gate 		}
43520Sstevel@tonic-gate 		if (zone->zone_uniqid != uniqid) {
43530Sstevel@tonic-gate 			/*
43540Sstevel@tonic-gate 			 * zoneid recycled
43550Sstevel@tonic-gate 			 */
43560Sstevel@tonic-gate 			zone_rele(zone);
43570Sstevel@tonic-gate 			break;
43580Sstevel@tonic-gate 		}
43590Sstevel@tonic-gate 		/*
43600Sstevel@tonic-gate 		 * We could zone_status_timedwait(), but there doesn't seem to
43610Sstevel@tonic-gate 		 * be much point in doing that (plus, it would mean that
43620Sstevel@tonic-gate 		 * zone_free() isn't called until this thread exits).
43630Sstevel@tonic-gate 		 */
43640Sstevel@tonic-gate 		zone_rele(zone);
43650Sstevel@tonic-gate 		delay(hz);
43660Sstevel@tonic-gate 		darg = save_arg;
43670Sstevel@tonic-gate 	}
43680Sstevel@tonic-gate out:
43690Sstevel@tonic-gate 	if (door != NULL) {
43700Sstevel@tonic-gate 		zone_release_door(&door);
43710Sstevel@tonic-gate 	}
43720Sstevel@tonic-gate 	kmem_free(zone_name, zone_namelen);
43730Sstevel@tonic-gate 	thread_exit();
43740Sstevel@tonic-gate }
43750Sstevel@tonic-gate 
43760Sstevel@tonic-gate /*
43770Sstevel@tonic-gate  * Entry point for uadmin() to tell the zone to go away or reboot.  The caller
43780Sstevel@tonic-gate  * is a process in the zone to be modified.
43790Sstevel@tonic-gate  *
43800Sstevel@tonic-gate  * In order to shutdown the zone, we will hand off control to zoneadmd
43810Sstevel@tonic-gate  * (running in the global zone) via a door.  We do a half-hearted job at
43820Sstevel@tonic-gate  * killing all processes in the zone, create a kernel thread to contact
43830Sstevel@tonic-gate  * zoneadmd, and make note of the "uniqid" of the zone.  The uniqid is
43840Sstevel@tonic-gate  * a form of generation number used to let zoneadmd (as well as
43850Sstevel@tonic-gate  * zone_destroy()) know exactly which zone they're re talking about.
43860Sstevel@tonic-gate  */
43870Sstevel@tonic-gate int
43880Sstevel@tonic-gate zone_uadmin(int cmd, int fcn, cred_t *credp)
43890Sstevel@tonic-gate {
43900Sstevel@tonic-gate 	struct zarg *zargp;
43910Sstevel@tonic-gate 	zone_cmd_t zcmd;
43920Sstevel@tonic-gate 	zone_t *zone;
43930Sstevel@tonic-gate 
43940Sstevel@tonic-gate 	zone = curproc->p_zone;
43950Sstevel@tonic-gate 	ASSERT(getzoneid() != GLOBAL_ZONEID);
43960Sstevel@tonic-gate 
43970Sstevel@tonic-gate 	switch (cmd) {
43980Sstevel@tonic-gate 	case A_SHUTDOWN:
43990Sstevel@tonic-gate 		switch (fcn) {
44000Sstevel@tonic-gate 		case AD_HALT:
44010Sstevel@tonic-gate 		case AD_POWEROFF:
44020Sstevel@tonic-gate 			zcmd = Z_HALT;
44030Sstevel@tonic-gate 			break;
44040Sstevel@tonic-gate 		case AD_BOOT:
44050Sstevel@tonic-gate 			zcmd = Z_REBOOT;
44060Sstevel@tonic-gate 			break;
44070Sstevel@tonic-gate 		case AD_IBOOT:
44080Sstevel@tonic-gate 		case AD_SBOOT:
44090Sstevel@tonic-gate 		case AD_SIBOOT:
44100Sstevel@tonic-gate 		case AD_NOSYNC:
44110Sstevel@tonic-gate 			return (ENOTSUP);
44120Sstevel@tonic-gate 		default:
44130Sstevel@tonic-gate 			return (EINVAL);
44140Sstevel@tonic-gate 		}
44150Sstevel@tonic-gate 		break;
44160Sstevel@tonic-gate 	case A_REBOOT:
44170Sstevel@tonic-gate 		zcmd = Z_REBOOT;
44180Sstevel@tonic-gate 		break;
44190Sstevel@tonic-gate 	case A_FTRACE:
44200Sstevel@tonic-gate 	case A_REMOUNT:
44210Sstevel@tonic-gate 	case A_FREEZE:
44220Sstevel@tonic-gate 	case A_DUMP:
44230Sstevel@tonic-gate 		return (ENOTSUP);
44240Sstevel@tonic-gate 	default:
44250Sstevel@tonic-gate 		ASSERT(cmd != A_SWAPCTL);	/* handled by uadmin() */
44260Sstevel@tonic-gate 		return (EINVAL);
44270Sstevel@tonic-gate 	}
44280Sstevel@tonic-gate 
44290Sstevel@tonic-gate 	if (secpolicy_zone_admin(credp, B_FALSE))
44300Sstevel@tonic-gate 		return (EPERM);
44310Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
44320Sstevel@tonic-gate 	/*
44330Sstevel@tonic-gate 	 * zone_status can't be ZONE_IS_EMPTY or higher since curproc
44340Sstevel@tonic-gate 	 * is in the zone.
44350Sstevel@tonic-gate 	 */
44360Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY);
44370Sstevel@tonic-gate 	if (zone_status_get(zone) > ZONE_IS_RUNNING) {
44380Sstevel@tonic-gate 		/*
44390Sstevel@tonic-gate 		 * This zone is already on its way down.
44400Sstevel@tonic-gate 		 */
44410Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
44420Sstevel@tonic-gate 		return (0);
44430Sstevel@tonic-gate 	}
44440Sstevel@tonic-gate 	/*
44450Sstevel@tonic-gate 	 * Prevent future zone_enter()s
44460Sstevel@tonic-gate 	 */
44470Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
44480Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
44490Sstevel@tonic-gate 
44500Sstevel@tonic-gate 	/*
44510Sstevel@tonic-gate 	 * Kill everyone now and call zoneadmd later.
44520Sstevel@tonic-gate 	 * zone_ki_call_zoneadmd() will do a more thorough job of this
44530Sstevel@tonic-gate 	 * later.
44540Sstevel@tonic-gate 	 */
44550Sstevel@tonic-gate 	killall(zone->zone_id);
44560Sstevel@tonic-gate 	/*
44570Sstevel@tonic-gate 	 * Now, create the thread to contact zoneadmd and do the rest of the
44580Sstevel@tonic-gate 	 * work.  This thread can't be created in our zone otherwise
44590Sstevel@tonic-gate 	 * zone_destroy() would deadlock.
44600Sstevel@tonic-gate 	 */
44610Sstevel@tonic-gate 	zargp = kmem_alloc(sizeof (*zargp), KM_SLEEP);
44620Sstevel@tonic-gate 	zargp->arg.cmd = zcmd;
44630Sstevel@tonic-gate 	zargp->arg.uniqid = zone->zone_uniqid;
44640Sstevel@tonic-gate 	(void) strcpy(zargp->arg.locale, "C");
44650Sstevel@tonic-gate 	zone_hold(zargp->zone = zone);
44660Sstevel@tonic-gate 
44670Sstevel@tonic-gate 	(void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0,
44680Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
44690Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
44700Sstevel@tonic-gate 
44710Sstevel@tonic-gate 	return (EINVAL);
44720Sstevel@tonic-gate }
44730Sstevel@tonic-gate 
44740Sstevel@tonic-gate /*
44750Sstevel@tonic-gate  * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's
44760Sstevel@tonic-gate  * status to ZONE_IS_SHUTTING_DOWN.
44770Sstevel@tonic-gate  */
44780Sstevel@tonic-gate void
44790Sstevel@tonic-gate zone_shutdown_global(void)
44800Sstevel@tonic-gate {
44810Sstevel@tonic-gate 	ASSERT(curproc->p_zone == global_zone);
44820Sstevel@tonic-gate 
44830Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
44840Sstevel@tonic-gate 	ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING);
44850Sstevel@tonic-gate 	zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN);
44860Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
44870Sstevel@tonic-gate }
4488789Sahrens 
4489789Sahrens /*
4490789Sahrens  * Returns true if the named dataset is visible in the current zone.
4491789Sahrens  * The 'write' parameter is set to 1 if the dataset is also writable.
4492789Sahrens  */
4493789Sahrens int
4494789Sahrens zone_dataset_visible(const char *dataset, int *write)
4495789Sahrens {
4496789Sahrens 	zone_dataset_t *zd;
4497789Sahrens 	size_t len;
4498789Sahrens 	zone_t *zone = curproc->p_zone;
4499789Sahrens 
4500789Sahrens 	if (dataset[0] == '\0')
4501789Sahrens 		return (0);
4502789Sahrens 
4503789Sahrens 	/*
4504789Sahrens 	 * Walk the list once, looking for datasets which match exactly, or
4505789Sahrens 	 * specify a dataset underneath an exported dataset.  If found, return
4506789Sahrens 	 * true and note that it is writable.
4507789Sahrens 	 */
4508789Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
4509789Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
4510789Sahrens 
4511789Sahrens 		len = strlen(zd->zd_dataset);
4512789Sahrens 		if (strlen(dataset) >= len &&
4513789Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
4514816Smaybee 		    (dataset[len] == '\0' || dataset[len] == '/' ||
4515816Smaybee 		    dataset[len] == '@')) {
4516789Sahrens 			if (write)
4517789Sahrens 				*write = 1;
4518789Sahrens 			return (1);
4519789Sahrens 		}
4520789Sahrens 	}
4521789Sahrens 
4522789Sahrens 	/*
4523789Sahrens 	 * Walk the list a second time, searching for datasets which are parents
4524789Sahrens 	 * of exported datasets.  These should be visible, but read-only.
4525789Sahrens 	 *
4526789Sahrens 	 * Note that we also have to support forms such as 'pool/dataset/', with
4527789Sahrens 	 * a trailing slash.
4528789Sahrens 	 */
4529789Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
4530789Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
4531789Sahrens 
4532789Sahrens 		len = strlen(dataset);
4533789Sahrens 		if (dataset[len - 1] == '/')
4534789Sahrens 			len--;	/* Ignore trailing slash */
4535789Sahrens 		if (len < strlen(zd->zd_dataset) &&
4536789Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
4537789Sahrens 		    zd->zd_dataset[len] == '/') {
4538789Sahrens 			if (write)
4539789Sahrens 				*write = 0;
4540789Sahrens 			return (1);
4541789Sahrens 		}
4542789Sahrens 	}
4543789Sahrens 
4544789Sahrens 	return (0);
4545789Sahrens }
4546*1676Sjpk 
4547*1676Sjpk /*
4548*1676Sjpk  * zone_find_by_any_path() -
4549*1676Sjpk  *
4550*1676Sjpk  * kernel-private routine similar to zone_find_by_path(), but which
4551*1676Sjpk  * effectively compares against zone paths rather than zonerootpath
4552*1676Sjpk  * (i.e., the last component of zonerootpaths, which should be "root/",
4553*1676Sjpk  * are not compared.)  This is done in order to accurately identify all
4554*1676Sjpk  * paths, whether zone-visible or not, including those which are parallel
4555*1676Sjpk  * to /root/, such as /dev/, /home/, etc...
4556*1676Sjpk  *
4557*1676Sjpk  * If the specified path does not fall under any zone path then global
4558*1676Sjpk  * zone is returned.
4559*1676Sjpk  *
4560*1676Sjpk  * The treat_abs parameter indicates whether the path should be treated as
4561*1676Sjpk  * an absolute path although it does not begin with "/".  (This supports
4562*1676Sjpk  * nfs mount syntax such as host:any/path.)
4563*1676Sjpk  *
4564*1676Sjpk  * The caller is responsible for zone_rele of the returned zone.
4565*1676Sjpk  */
4566*1676Sjpk zone_t *
4567*1676Sjpk zone_find_by_any_path(const char *path, boolean_t treat_abs)
4568*1676Sjpk {
4569*1676Sjpk 	zone_t *zone;
4570*1676Sjpk 	int path_offset = 0;
4571*1676Sjpk 
4572*1676Sjpk 	if (path == NULL) {
4573*1676Sjpk 		zone_hold(global_zone);
4574*1676Sjpk 		return (global_zone);
4575*1676Sjpk 	}
4576*1676Sjpk 
4577*1676Sjpk 	if (*path != '/') {
4578*1676Sjpk 		ASSERT(treat_abs);
4579*1676Sjpk 		path_offset = 1;
4580*1676Sjpk 	}
4581*1676Sjpk 
4582*1676Sjpk 	mutex_enter(&zonehash_lock);
4583*1676Sjpk 	for (zone = list_head(&zone_active); zone != NULL;
4584*1676Sjpk 	    zone = list_next(&zone_active, zone)) {
4585*1676Sjpk 		char	*c;
4586*1676Sjpk 		size_t	pathlen;
4587*1676Sjpk 
4588*1676Sjpk 		if (zone == global_zone)	/* skip global zone */
4589*1676Sjpk 			continue;
4590*1676Sjpk 
4591*1676Sjpk 		/* scan backwards to find start of last component */
4592*1676Sjpk 		c = zone->zone_rootpath + zone->zone_rootpathlen - 2;
4593*1676Sjpk 		do {
4594*1676Sjpk 			c--;
4595*1676Sjpk 		} while (*c != '/');
4596*1676Sjpk 
4597*1676Sjpk 		pathlen = c - zone->zone_rootpath + 1;
4598*1676Sjpk 		if (strncmp(path, zone->zone_rootpath + path_offset,
4599*1676Sjpk 		    pathlen - path_offset) == 0)
4600*1676Sjpk 			break;
4601*1676Sjpk 	}
4602*1676Sjpk 	if (zone == NULL)
4603*1676Sjpk 		zone = global_zone;
4604*1676Sjpk 	zone_hold(zone);
4605*1676Sjpk 	mutex_exit(&zonehash_lock);
4606*1676Sjpk 	return (zone);
4607*1676Sjpk }
4608