xref: /onnv-gate/usr/src/uts/common/os/zone.c (revision 2110:31cba59b38be)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21390Sraf 
220Sstevel@tonic-gate /*
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>
1921676Sjpk #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>
2281676Sjpk #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;
2561676Sjpk 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";
3241676Sjpk 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.
3391676Sjpk  * Version 4 alters the zone_create system call in order to support
3401676Sjpk  *     Trusted Extensions.
341813Sdp  */
3421676Sjpk 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 /*
10021676Sjpk  * Compute a hash value based on the contents of the label and the DOI.  The
10031676Sjpk  * hash algorithm is somewhat arbitrary, but is based on the observation that
10041676Sjpk  * humans will likely pick labels that differ by amounts that work out to be
10051676Sjpk  * multiples of the number of hash chains, and thus stirring in some primes
10061676Sjpk  * should help.
10071676Sjpk  */
10081676Sjpk static uint_t
10091676Sjpk hash_bylabel(void *hdata, mod_hash_key_t key)
10101676Sjpk {
10111676Sjpk 	const ts_label_t *lab = (ts_label_t *)key;
10121676Sjpk 	const uint32_t *up, *ue;
10131676Sjpk 	uint_t hash;
10141676Sjpk 	int i;
10151676Sjpk 
10161676Sjpk 	_NOTE(ARGUNUSED(hdata));
10171676Sjpk 
10181676Sjpk 	hash = lab->tsl_doi + (lab->tsl_doi << 1);
10191676Sjpk 	/* we depend on alignment of label, but not representation */
10201676Sjpk 	up = (const uint32_t *)&lab->tsl_label;
10211676Sjpk 	ue = up + sizeof (lab->tsl_label) / sizeof (*up);
10221676Sjpk 	i = 1;
10231676Sjpk 	while (up < ue) {
10241676Sjpk 		/* using 2^n + 1, 1 <= n <= 16 as source of many primes */
10251676Sjpk 		hash += *up + (*up << ((i % 16) + 1));
10261676Sjpk 		up++;
10271676Sjpk 		i++;
10281676Sjpk 	}
10291676Sjpk 	return (hash);
10301676Sjpk }
10311676Sjpk 
10321676Sjpk /*
10331676Sjpk  * All that mod_hash cares about here is zero (equal) versus non-zero (not
10341676Sjpk  * equal).  This may need to be changed if less than / greater than is ever
10351676Sjpk  * needed.
10361676Sjpk  */
10371676Sjpk static int
10381676Sjpk hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
10391676Sjpk {
10401676Sjpk 	ts_label_t *lab1 = (ts_label_t *)key1;
10411676Sjpk 	ts_label_t *lab2 = (ts_label_t *)key2;
10421676Sjpk 
10431676Sjpk 	return (label_equal(lab1, lab2) ? 0 : 1);
10441676Sjpk }
10451676Sjpk 
10461676Sjpk /*
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 |
10721996Sml93401 	    RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER,
10731996Sml93401 	    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 	 */
11121676Sjpk 
11131676Sjpk 	/*
11141676Sjpk 	 * Initialize zone label.
11151676Sjpk 	 * mlp are initialized when tnzonecfg is loaded.
11161676Sjpk 	 */
11171676Sjpk 	zone0.zone_slabel = l_admin_low;
11181676Sjpk 	rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
11191676Sjpk 	label_hold(l_admin_low);
11201676Sjpk 
11210Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
11220Sstevel@tonic-gate 	zone_uniqid(&zone0);
11230Sstevel@tonic-gate 	ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID);
11241676Sjpk 
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);
11291676Sjpk 	/*
11301676Sjpk 	 * maintain zonehashbylabel only for labeled systems
11311676Sjpk 	 */
11321676Sjpk 	if (is_system_labeled())
11331676Sjpk 		zonehashbylabel = mod_hash_create_extended("zone_by_label",
11341676Sjpk 		    zone_hash_size, mod_hash_null_keydtor,
11351676Sjpk 		    mod_hash_null_valdtor, hash_bylabel, NULL,
11361676Sjpk 		    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);
11431769Scarlsonj 	if (is_system_labeled()) {
11441769Scarlsonj 		zone0.zone_flags |= ZF_HASHED_LABEL;
11451676Sjpk 		(void) mod_hash_insert(zonehashbylabel,
11461676Sjpk 		    (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0);
11471769Scarlsonj 	}
11481676Sjpk 	mutex_exit(&zonehash_lock);
11491676Sjpk 
11500Sstevel@tonic-gate 	/*
11510Sstevel@tonic-gate 	 * We avoid setting zone_kcred until now, since kcred is initialized
11520Sstevel@tonic-gate 	 * sometime after zone_zsd_init() and before zone_init().
11530Sstevel@tonic-gate 	 */
11540Sstevel@tonic-gate 	zone0.zone_kcred = kcred;
11550Sstevel@tonic-gate 	/*
11560Sstevel@tonic-gate 	 * The global zone is fully initialized (except for zone_rootvp which
11570Sstevel@tonic-gate 	 * will be set when the root filesystem is mounted).
11580Sstevel@tonic-gate 	 */
11590Sstevel@tonic-gate 	global_zone = &zone0;
11601166Sdstaff 
11611166Sdstaff 	/*
11621166Sdstaff 	 * Setup an event channel to send zone status change notifications on
11631166Sdstaff 	 */
11641166Sdstaff 	res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan,
11651166Sdstaff 	    EVCH_CREAT);
11661166Sdstaff 
11671166Sdstaff 	if (res)
11681166Sdstaff 		panic("Sysevent_evc_bind failed during zone setup.\n");
11690Sstevel@tonic-gate }
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate static void
11720Sstevel@tonic-gate zone_free(zone_t *zone)
11730Sstevel@tonic-gate {
11740Sstevel@tonic-gate 	ASSERT(zone != global_zone);
11750Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks == 0);
11760Sstevel@tonic-gate 	ASSERT(zone->zone_nlwps == 0);
11770Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 0);
11780Sstevel@tonic-gate 	ASSERT(zone->zone_kcred == NULL);
11790Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_DEAD ||
11800Sstevel@tonic-gate 	    zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	/* remove from deathrow list */
11830Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_DEAD) {
11840Sstevel@tonic-gate 		ASSERT(zone->zone_ref == 0);
11850Sstevel@tonic-gate 		mutex_enter(&zone_deathrow_lock);
11860Sstevel@tonic-gate 		list_remove(&zone_deathrow, zone);
11870Sstevel@tonic-gate 		mutex_exit(&zone_deathrow_lock);
11880Sstevel@tonic-gate 	}
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	zone_free_zsd(zone);
1191789Sahrens 	zone_free_datasets(zone);
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	if (zone->zone_rootvp != NULL)
11940Sstevel@tonic-gate 		VN_RELE(zone->zone_rootvp);
11950Sstevel@tonic-gate 	if (zone->zone_rootpath)
11960Sstevel@tonic-gate 		kmem_free(zone->zone_rootpath, zone->zone_rootpathlen);
11970Sstevel@tonic-gate 	if (zone->zone_name != NULL)
11980Sstevel@tonic-gate 		kmem_free(zone->zone_name, ZONENAME_MAX);
11991676Sjpk 	if (zone->zone_slabel != NULL)
12001676Sjpk 		label_rele(zone->zone_slabel);
12010Sstevel@tonic-gate 	if (zone->zone_nodename != NULL)
12020Sstevel@tonic-gate 		kmem_free(zone->zone_nodename, _SYS_NMLN);
12030Sstevel@tonic-gate 	if (zone->zone_domain != NULL)
12040Sstevel@tonic-gate 		kmem_free(zone->zone_domain, _SYS_NMLN);
12050Sstevel@tonic-gate 	if (zone->zone_privset != NULL)
12060Sstevel@tonic-gate 		kmem_free(zone->zone_privset, sizeof (priv_set_t));
12070Sstevel@tonic-gate 	if (zone->zone_rctls != NULL)
12080Sstevel@tonic-gate 		rctl_set_free(zone->zone_rctls);
12090Sstevel@tonic-gate 	if (zone->zone_bootargs != NULL)
12100Sstevel@tonic-gate 		kmem_free(zone->zone_bootargs, ZONEBOOTARGS_MAX);
12110Sstevel@tonic-gate 	id_free(zoneid_space, zone->zone_id);
12120Sstevel@tonic-gate 	mutex_destroy(&zone->zone_lock);
12130Sstevel@tonic-gate 	cv_destroy(&zone->zone_cv);
12141676Sjpk 	rw_destroy(&zone->zone_mlps.mlpl_rwlock);
12150Sstevel@tonic-gate 	kmem_free(zone, sizeof (zone_t));
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate /*
12190Sstevel@tonic-gate  * See block comment at the top of this file for information about zone
12200Sstevel@tonic-gate  * status values.
12210Sstevel@tonic-gate  */
12220Sstevel@tonic-gate /*
12230Sstevel@tonic-gate  * Convenience function for setting zone status.
12240Sstevel@tonic-gate  */
12250Sstevel@tonic-gate static void
12260Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status)
12270Sstevel@tonic-gate {
12281166Sdstaff 
12291166Sdstaff 	nvlist_t *nvl = NULL;
12300Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zone_status_lock));
12310Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE &&
12320Sstevel@tonic-gate 	    status >= zone_status_get(zone));
12331166Sdstaff 
12341166Sdstaff 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) ||
12351166Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) ||
12361166Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_NEWSTATE,
12371166Sdstaff 		zone_status_table[status]) ||
12381166Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_OLDSTATE,
12391166Sdstaff 		zone_status_table[zone->zone_status]) ||
12401166Sdstaff 	    nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) ||
12411166Sdstaff 	    nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) ||
12421166Sdstaff 	    sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS,
12431166Sdstaff 		ZONE_EVENT_STATUS_SUBCLASS,
12441166Sdstaff 		"sun.com", "kernel", nvl, EVCH_SLEEP)) {
12451166Sdstaff #ifdef DEBUG
12461166Sdstaff 		(void) printf(
12471166Sdstaff 		    "Failed to allocate and send zone state change event.\n");
12481166Sdstaff #endif
12491166Sdstaff 	}
12501166Sdstaff 	nvlist_free(nvl);
12511166Sdstaff 
12520Sstevel@tonic-gate 	zone->zone_status = status;
12531166Sdstaff 
12540Sstevel@tonic-gate 	cv_broadcast(&zone->zone_cv);
12550Sstevel@tonic-gate }
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate /*
12580Sstevel@tonic-gate  * Public function to retrieve the zone status.  The zone status may
12590Sstevel@tonic-gate  * change after it is retrieved.
12600Sstevel@tonic-gate  */
12610Sstevel@tonic-gate zone_status_t
12620Sstevel@tonic-gate zone_status_get(zone_t *zone)
12630Sstevel@tonic-gate {
12640Sstevel@tonic-gate 	return (zone->zone_status);
12650Sstevel@tonic-gate }
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate static int
12680Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs)
12690Sstevel@tonic-gate {
12700Sstevel@tonic-gate 	char *bootargs = kmem_zalloc(ZONEBOOTARGS_MAX, KM_SLEEP);
12710Sstevel@tonic-gate 	size_t len;
12720Sstevel@tonic-gate 	int err;
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	err = copyinstr(zone_bootargs, bootargs, ZONEBOOTARGS_MAX - 1, &len);
12750Sstevel@tonic-gate 	if (err != 0) {
12760Sstevel@tonic-gate 		kmem_free(bootargs, ZONEBOOTARGS_MAX);
12770Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
12780Sstevel@tonic-gate 	}
12790Sstevel@tonic-gate 	bootargs[len] = '\0';
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	ASSERT(zone->zone_bootargs == NULL);
12820Sstevel@tonic-gate 	zone->zone_bootargs = bootargs;
12830Sstevel@tonic-gate 	return (0);
12840Sstevel@tonic-gate }
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate /*
12870Sstevel@tonic-gate  * Block indefinitely waiting for (zone_status >= status)
12880Sstevel@tonic-gate  */
12890Sstevel@tonic-gate void
12900Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status)
12910Sstevel@tonic-gate {
12920Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
12950Sstevel@tonic-gate 	while (zone->zone_status < status) {
12960Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
12970Sstevel@tonic-gate 	}
12980Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
12990Sstevel@tonic-gate }
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate /*
13020Sstevel@tonic-gate  * Private CPR-safe version of zone_status_wait().
13030Sstevel@tonic-gate  */
13040Sstevel@tonic-gate static void
13050Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str)
13060Sstevel@tonic-gate {
13070Sstevel@tonic-gate 	callb_cpr_t cprinfo;
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr,
13120Sstevel@tonic-gate 	    str);
13130Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
13140Sstevel@tonic-gate 	while (zone->zone_status < status) {
13150Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
13160Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
13170Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock);
13180Sstevel@tonic-gate 	}
13190Sstevel@tonic-gate 	/*
13200Sstevel@tonic-gate 	 * zone_status_lock is implicitly released by the following.
13210Sstevel@tonic-gate 	 */
13220Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
13230Sstevel@tonic-gate }
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate /*
13260Sstevel@tonic-gate  * Block until zone enters requested state or signal is received.  Return (0)
13270Sstevel@tonic-gate  * if signaled, non-zero otherwise.
13280Sstevel@tonic-gate  */
13290Sstevel@tonic-gate int
13300Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status)
13310Sstevel@tonic-gate {
13320Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
13350Sstevel@tonic-gate 	while (zone->zone_status < status) {
13360Sstevel@tonic-gate 		if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) {
13370Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
13380Sstevel@tonic-gate 			return (0);
13390Sstevel@tonic-gate 		}
13400Sstevel@tonic-gate 	}
13410Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
13420Sstevel@tonic-gate 	return (1);
13430Sstevel@tonic-gate }
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate /*
13460Sstevel@tonic-gate  * Block until the zone enters the requested state or the timeout expires,
13470Sstevel@tonic-gate  * whichever happens first.  Return (-1) if operation timed out, time remaining
13480Sstevel@tonic-gate  * otherwise.
13490Sstevel@tonic-gate  */
13500Sstevel@tonic-gate clock_t
13510Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status)
13520Sstevel@tonic-gate {
13530Sstevel@tonic-gate 	clock_t timeleft = 0;
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
13580Sstevel@tonic-gate 	while (zone->zone_status < status && timeleft != -1) {
13590Sstevel@tonic-gate 		timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim);
13600Sstevel@tonic-gate 	}
13610Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
13620Sstevel@tonic-gate 	return (timeleft);
13630Sstevel@tonic-gate }
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate /*
13660Sstevel@tonic-gate  * Block until the zone enters the requested state, the current process is
13670Sstevel@tonic-gate  * signaled,  or the timeout expires, whichever happens first.  Return (-1) if
13680Sstevel@tonic-gate  * operation timed out, 0 if signaled, time remaining otherwise.
13690Sstevel@tonic-gate  */
13700Sstevel@tonic-gate clock_t
13710Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status)
13720Sstevel@tonic-gate {
13730Sstevel@tonic-gate 	clock_t timeleft = tim - lbolt;
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
13780Sstevel@tonic-gate 	while (zone->zone_status < status) {
13790Sstevel@tonic-gate 		timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock,
13800Sstevel@tonic-gate 		    tim);
13810Sstevel@tonic-gate 		if (timeleft <= 0)
13820Sstevel@tonic-gate 			break;
13830Sstevel@tonic-gate 	}
13840Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
13850Sstevel@tonic-gate 	return (timeleft);
13860Sstevel@tonic-gate }
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate /*
13890Sstevel@tonic-gate  * Zones have two reference counts: one for references from credential
13900Sstevel@tonic-gate  * structures (zone_cred_ref), and one (zone_ref) for everything else.
13910Sstevel@tonic-gate  * This is so we can allow a zone to be rebooted while there are still
13920Sstevel@tonic-gate  * outstanding cred references, since certain drivers cache dblks (which
13930Sstevel@tonic-gate  * implicitly results in cached creds).  We wait for zone_ref to drop to
13940Sstevel@tonic-gate  * 0 (actually 1), but not zone_cred_ref.  The zone structure itself is
13950Sstevel@tonic-gate  * later freed when the zone_cred_ref drops to 0, though nothing other
13960Sstevel@tonic-gate  * than the zone id and privilege set should be accessed once the zone
13970Sstevel@tonic-gate  * is "dead".
13980Sstevel@tonic-gate  *
13990Sstevel@tonic-gate  * A debugging flag, zone_wait_for_cred, can be set to a non-zero value
14000Sstevel@tonic-gate  * to force halt/reboot to block waiting for the zone_cred_ref to drop
14010Sstevel@tonic-gate  * to 0.  This can be useful to flush out other sources of cached creds
14020Sstevel@tonic-gate  * that may be less innocuous than the driver case.
14030Sstevel@tonic-gate  */
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate int zone_wait_for_cred = 0;
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate static void
14080Sstevel@tonic-gate zone_hold_locked(zone_t *z)
14090Sstevel@tonic-gate {
14100Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&z->zone_lock));
14110Sstevel@tonic-gate 	z->zone_ref++;
14120Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
14130Sstevel@tonic-gate }
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate void
14160Sstevel@tonic-gate zone_hold(zone_t *z)
14170Sstevel@tonic-gate {
14180Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
14190Sstevel@tonic-gate 	zone_hold_locked(z);
14200Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
14210Sstevel@tonic-gate }
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate /*
14240Sstevel@tonic-gate  * If the non-cred ref count drops to 1 and either the cred ref count
14250Sstevel@tonic-gate  * is 0 or we aren't waiting for cred references, the zone is ready to
14260Sstevel@tonic-gate  * be destroyed.
14270Sstevel@tonic-gate  */
14280Sstevel@tonic-gate #define	ZONE_IS_UNREF(zone)	((zone)->zone_ref == 1 && \
14290Sstevel@tonic-gate 	    (!zone_wait_for_cred || (zone)->zone_cred_ref == 0))
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate void
14320Sstevel@tonic-gate zone_rele(zone_t *z)
14330Sstevel@tonic-gate {
14340Sstevel@tonic-gate 	boolean_t wakeup;
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
14370Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
14380Sstevel@tonic-gate 	z->zone_ref--;
14390Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
14400Sstevel@tonic-gate 		/* no more refs, free the structure */
14410Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
14420Sstevel@tonic-gate 		zone_free(z);
14430Sstevel@tonic-gate 		return;
14440Sstevel@tonic-gate 	}
14450Sstevel@tonic-gate 	/* signal zone_destroy so the zone can finish halting */
14460Sstevel@tonic-gate 	wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD);
14470Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	if (wakeup) {
14500Sstevel@tonic-gate 		/*
14510Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
14520Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
14530Sstevel@tonic-gate 		 */
14540Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
14550Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
14560Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
14570Sstevel@tonic-gate 	}
14580Sstevel@tonic-gate }
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate void
14610Sstevel@tonic-gate zone_cred_hold(zone_t *z)
14620Sstevel@tonic-gate {
14630Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
14640Sstevel@tonic-gate 	z->zone_cred_ref++;
14650Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
14660Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
14670Sstevel@tonic-gate }
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate void
14700Sstevel@tonic-gate zone_cred_rele(zone_t *z)
14710Sstevel@tonic-gate {
14720Sstevel@tonic-gate 	boolean_t wakeup;
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
14750Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
14760Sstevel@tonic-gate 	z->zone_cred_ref--;
14770Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
14780Sstevel@tonic-gate 		/* no more refs, free the structure */
14790Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
14800Sstevel@tonic-gate 		zone_free(z);
14810Sstevel@tonic-gate 		return;
14820Sstevel@tonic-gate 	}
14830Sstevel@tonic-gate 	/*
14840Sstevel@tonic-gate 	 * If zone_destroy is waiting for the cred references to drain
14850Sstevel@tonic-gate 	 * out, and they have, signal it.
14860Sstevel@tonic-gate 	 */
14870Sstevel@tonic-gate 	wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) &&
14880Sstevel@tonic-gate 	    zone_status_get(z) >= ZONE_IS_DEAD);
14890Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate 	if (wakeup) {
14920Sstevel@tonic-gate 		/*
14930Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
14940Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
14950Sstevel@tonic-gate 		 */
14960Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
14970Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
14980Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
14990Sstevel@tonic-gate 	}
15000Sstevel@tonic-gate }
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate void
15030Sstevel@tonic-gate zone_task_hold(zone_t *z)
15040Sstevel@tonic-gate {
15050Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
15060Sstevel@tonic-gate 	z->zone_ntasks++;
15070Sstevel@tonic-gate 	ASSERT(z->zone_ntasks != 0);
15080Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
15090Sstevel@tonic-gate }
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate void
15120Sstevel@tonic-gate zone_task_rele(zone_t *zone)
15130Sstevel@tonic-gate {
15140Sstevel@tonic-gate 	uint_t refcnt;
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
15170Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks != 0);
15180Sstevel@tonic-gate 	refcnt = --zone->zone_ntasks;
15190Sstevel@tonic-gate 	if (refcnt > 1)	{	/* Common case */
15200Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
15210Sstevel@tonic-gate 		return;
15220Sstevel@tonic-gate 	}
15230Sstevel@tonic-gate 	zone_hold_locked(zone);	/* so we can use the zone_t later */
15240Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
15250Sstevel@tonic-gate 	if (refcnt == 1) {
15260Sstevel@tonic-gate 		/*
15270Sstevel@tonic-gate 		 * See if the zone is shutting down.
15280Sstevel@tonic-gate 		 */
15290Sstevel@tonic-gate 		mutex_enter(&zone_status_lock);
15300Sstevel@tonic-gate 		if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) {
15310Sstevel@tonic-gate 			goto out;
15320Sstevel@tonic-gate 		}
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 		/*
15350Sstevel@tonic-gate 		 * Make sure the ntasks didn't change since we
15360Sstevel@tonic-gate 		 * dropped zone_lock.
15370Sstevel@tonic-gate 		 */
15380Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
15390Sstevel@tonic-gate 		if (refcnt != zone->zone_ntasks) {
15400Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
15410Sstevel@tonic-gate 			goto out;
15420Sstevel@tonic-gate 		}
15430Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 		/*
15460Sstevel@tonic-gate 		 * No more user processes in the zone.  The zone is empty.
15470Sstevel@tonic-gate 		 */
15480Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_EMPTY);
15490Sstevel@tonic-gate 		goto out;
15500Sstevel@tonic-gate 	}
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	ASSERT(refcnt == 0);
15530Sstevel@tonic-gate 	/*
15540Sstevel@tonic-gate 	 * zsched has exited; the zone is dead.
15550Sstevel@tonic-gate 	 */
15560Sstevel@tonic-gate 	zone->zone_zsched = NULL;		/* paranoia */
15570Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
15580Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_DEAD);
15590Sstevel@tonic-gate out:
15600Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
15610Sstevel@tonic-gate 	zone_rele(zone);
15620Sstevel@tonic-gate }
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate zoneid_t
15650Sstevel@tonic-gate getzoneid(void)
15660Sstevel@tonic-gate {
15670Sstevel@tonic-gate 	return (curproc->p_zone->zone_id);
15680Sstevel@tonic-gate }
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate /*
15710Sstevel@tonic-gate  * Internal versions of zone_find_by_*().  These don't zone_hold() or
15720Sstevel@tonic-gate  * check the validity of a zone's state.
15730Sstevel@tonic-gate  */
15740Sstevel@tonic-gate static zone_t *
15750Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid)
15760Sstevel@tonic-gate {
15770Sstevel@tonic-gate 	mod_hash_val_t hv;
15780Sstevel@tonic-gate 	zone_t *zone = NULL;
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyid,
15830Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0)
15840Sstevel@tonic-gate 		zone = (zone_t *)hv;
15850Sstevel@tonic-gate 	return (zone);
15860Sstevel@tonic-gate }
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate static zone_t *
15891676Sjpk zone_find_all_by_label(const ts_label_t *label)
15901676Sjpk {
15911676Sjpk 	mod_hash_val_t hv;
15921676Sjpk 	zone_t *zone = NULL;
15931676Sjpk 
15941676Sjpk 	ASSERT(MUTEX_HELD(&zonehash_lock));
15951676Sjpk 
15961676Sjpk 	/*
15971676Sjpk 	 * zonehashbylabel is not maintained for unlabeled systems
15981676Sjpk 	 */
15991676Sjpk 	if (!is_system_labeled())
16001676Sjpk 		return (NULL);
16011676Sjpk 	if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0)
16021676Sjpk 		zone = (zone_t *)hv;
16031676Sjpk 	return (zone);
16041676Sjpk }
16051676Sjpk 
16061676Sjpk static zone_t *
16070Sstevel@tonic-gate zone_find_all_by_name(char *name)
16080Sstevel@tonic-gate {
16090Sstevel@tonic-gate 	mod_hash_val_t hv;
16100Sstevel@tonic-gate 	zone_t *zone = NULL;
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0)
16150Sstevel@tonic-gate 		zone = (zone_t *)hv;
16160Sstevel@tonic-gate 	return (zone);
16170Sstevel@tonic-gate }
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate /*
16200Sstevel@tonic-gate  * Public interface for looking up a zone by zoneid.  Only returns the zone if
16210Sstevel@tonic-gate  * it is fully initialized, and has not yet begun the zone_destroy() sequence.
16220Sstevel@tonic-gate  * Caller must call zone_rele() once it is done with the zone.
16230Sstevel@tonic-gate  *
16240Sstevel@tonic-gate  * The zone may begin the zone_destroy() sequence immediately after this
16250Sstevel@tonic-gate  * function returns, but may be safely used until zone_rele() is called.
16260Sstevel@tonic-gate  */
16270Sstevel@tonic-gate zone_t *
16280Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid)
16290Sstevel@tonic-gate {
16300Sstevel@tonic-gate 	zone_t *zone;
16310Sstevel@tonic-gate 	zone_status_t status;
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
16340Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
16350Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
16360Sstevel@tonic-gate 		return (NULL);
16370Sstevel@tonic-gate 	}
16380Sstevel@tonic-gate 	status = zone_status_get(zone);
16390Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
16400Sstevel@tonic-gate 		/*
16410Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
16420Sstevel@tonic-gate 		 */
16430Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
16440Sstevel@tonic-gate 		return (NULL);
16450Sstevel@tonic-gate 	}
16460Sstevel@tonic-gate 	zone_hold(zone);
16470Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
16480Sstevel@tonic-gate 	return (zone);
16490Sstevel@tonic-gate }
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate /*
16521676Sjpk  * Similar to zone_find_by_id, but using zone label as the key.
16531676Sjpk  */
16541676Sjpk zone_t *
16551676Sjpk zone_find_by_label(const ts_label_t *label)
16561676Sjpk {
16571676Sjpk 	zone_t *zone;
1658*2110Srica 	zone_status_t status;
16591676Sjpk 
16601676Sjpk 	mutex_enter(&zonehash_lock);
16611676Sjpk 	if ((zone = zone_find_all_by_label(label)) == NULL) {
16621676Sjpk 		mutex_exit(&zonehash_lock);
16631676Sjpk 		return (NULL);
16641676Sjpk 	}
1665*2110Srica 
1666*2110Srica 	status = zone_status_get(zone);
1667*2110Srica 	if (status > ZONE_IS_DOWN) {
16681676Sjpk 		/*
16691676Sjpk 		 * For all practical purposes the zone doesn't exist.
16701676Sjpk 		 */
1671*2110Srica 		mutex_exit(&zonehash_lock);
1672*2110Srica 		return (NULL);
16731676Sjpk 	}
1674*2110Srica 	zone_hold(zone);
16751676Sjpk 	mutex_exit(&zonehash_lock);
16761676Sjpk 	return (zone);
16771676Sjpk }
16781676Sjpk 
16791676Sjpk /*
16800Sstevel@tonic-gate  * Similar to zone_find_by_id, but using zone name as the key.
16810Sstevel@tonic-gate  */
16820Sstevel@tonic-gate zone_t *
16830Sstevel@tonic-gate zone_find_by_name(char *name)
16840Sstevel@tonic-gate {
16850Sstevel@tonic-gate 	zone_t *zone;
16860Sstevel@tonic-gate 	zone_status_t status;
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
16890Sstevel@tonic-gate 	if ((zone = zone_find_all_by_name(name)) == NULL) {
16900Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
16910Sstevel@tonic-gate 		return (NULL);
16920Sstevel@tonic-gate 	}
16930Sstevel@tonic-gate 	status = zone_status_get(zone);
16940Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
16950Sstevel@tonic-gate 		/*
16960Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
16970Sstevel@tonic-gate 		 */
16980Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
16990Sstevel@tonic-gate 		return (NULL);
17000Sstevel@tonic-gate 	}
17010Sstevel@tonic-gate 	zone_hold(zone);
17020Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
17030Sstevel@tonic-gate 	return (zone);
17040Sstevel@tonic-gate }
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate /*
17070Sstevel@tonic-gate  * Similar to zone_find_by_id(), using the path as a key.  For instance,
17080Sstevel@tonic-gate  * if there is a zone "foo" rooted at /foo/root, and the path argument
17090Sstevel@tonic-gate  * is "/foo/root/proc", it will return the held zone_t corresponding to
17100Sstevel@tonic-gate  * zone "foo".
17110Sstevel@tonic-gate  *
17120Sstevel@tonic-gate  * zone_find_by_path() always returns a non-NULL value, since at the
17130Sstevel@tonic-gate  * very least every path will be contained in the global zone.
17140Sstevel@tonic-gate  *
17150Sstevel@tonic-gate  * As with the other zone_find_by_*() functions, the caller is
17160Sstevel@tonic-gate  * responsible for zone_rele()ing the return value of this function.
17170Sstevel@tonic-gate  */
17180Sstevel@tonic-gate zone_t *
17190Sstevel@tonic-gate zone_find_by_path(const char *path)
17200Sstevel@tonic-gate {
17210Sstevel@tonic-gate 	zone_t *zone;
17220Sstevel@tonic-gate 	zone_t *zret = NULL;
17230Sstevel@tonic-gate 	zone_status_t status;
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	if (path == NULL) {
17260Sstevel@tonic-gate 		/*
17270Sstevel@tonic-gate 		 * Call from rootconf().
17280Sstevel@tonic-gate 		 */
17290Sstevel@tonic-gate 		zone_hold(global_zone);
17300Sstevel@tonic-gate 		return (global_zone);
17310Sstevel@tonic-gate 	}
17320Sstevel@tonic-gate 	ASSERT(*path == '/');
17330Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
17340Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
17350Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
17360Sstevel@tonic-gate 		if (ZONE_PATH_VISIBLE(path, zone))
17370Sstevel@tonic-gate 			zret = zone;
17380Sstevel@tonic-gate 	}
17390Sstevel@tonic-gate 	ASSERT(zret != NULL);
17400Sstevel@tonic-gate 	status = zone_status_get(zret);
17410Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
17420Sstevel@tonic-gate 		/*
17430Sstevel@tonic-gate 		 * Zone practically doesn't exist.
17440Sstevel@tonic-gate 		 */
17450Sstevel@tonic-gate 		zret = global_zone;
17460Sstevel@tonic-gate 	}
17470Sstevel@tonic-gate 	zone_hold(zret);
17480Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
17490Sstevel@tonic-gate 	return (zret);
17500Sstevel@tonic-gate }
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate /*
17530Sstevel@tonic-gate  * Get the number of cpus visible to this zone.  The system-wide global
17540Sstevel@tonic-gate  * 'ncpus' is returned if pools are disabled, the caller is in the
17550Sstevel@tonic-gate  * global zone, or a NULL zone argument is passed in.
17560Sstevel@tonic-gate  */
17570Sstevel@tonic-gate int
17580Sstevel@tonic-gate zone_ncpus_get(zone_t *zone)
17590Sstevel@tonic-gate {
17600Sstevel@tonic-gate 	int myncpus = zone == NULL ? 0 : zone->zone_ncpus;
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	return (myncpus != 0 ? myncpus : ncpus);
17630Sstevel@tonic-gate }
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate /*
17660Sstevel@tonic-gate  * Get the number of online cpus visible to this zone.  The system-wide
17670Sstevel@tonic-gate  * global 'ncpus_online' is returned if pools are disabled, the caller
17680Sstevel@tonic-gate  * is in the global zone, or a NULL zone argument is passed in.
17690Sstevel@tonic-gate  */
17700Sstevel@tonic-gate int
17710Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone)
17720Sstevel@tonic-gate {
17730Sstevel@tonic-gate 	int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online;
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	return (myncpus_online != 0 ? myncpus_online : ncpus_online);
17760Sstevel@tonic-gate }
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate /*
17790Sstevel@tonic-gate  * Return the pool to which the zone is currently bound.
17800Sstevel@tonic-gate  */
17810Sstevel@tonic-gate pool_t *
17820Sstevel@tonic-gate zone_pool_get(zone_t *zone)
17830Sstevel@tonic-gate {
17840Sstevel@tonic-gate 	ASSERT(pool_lock_held());
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 	return (zone->zone_pool);
17870Sstevel@tonic-gate }
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate /*
17900Sstevel@tonic-gate  * Set the zone's pool pointer and update the zone's visibility to match
17910Sstevel@tonic-gate  * the resources in the new pool.
17920Sstevel@tonic-gate  */
17930Sstevel@tonic-gate void
17940Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool)
17950Sstevel@tonic-gate {
17960Sstevel@tonic-gate 	ASSERT(pool_lock_held());
17970Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate 	zone->zone_pool = pool;
18000Sstevel@tonic-gate 	zone_pset_set(zone, pool->pool_pset->pset_id);
18010Sstevel@tonic-gate }
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate /*
18040Sstevel@tonic-gate  * Return the cached value of the id of the processor set to which the
18050Sstevel@tonic-gate  * zone is currently bound.  The value will be ZONE_PS_INVAL if the pools
18060Sstevel@tonic-gate  * facility is disabled.
18070Sstevel@tonic-gate  */
18080Sstevel@tonic-gate psetid_t
18090Sstevel@tonic-gate zone_pset_get(zone_t *zone)
18100Sstevel@tonic-gate {
18110Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	return (zone->zone_psetid);
18140Sstevel@tonic-gate }
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate /*
18170Sstevel@tonic-gate  * Set the cached value of the id of the processor set to which the zone
18180Sstevel@tonic-gate  * is currently bound.  Also update the zone's visibility to match the
18190Sstevel@tonic-gate  * resources in the new processor set.
18200Sstevel@tonic-gate  */
18210Sstevel@tonic-gate void
18220Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid)
18230Sstevel@tonic-gate {
18240Sstevel@tonic-gate 	psetid_t oldpsetid;
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
18270Sstevel@tonic-gate 	oldpsetid = zone_pset_get(zone);
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate 	if (oldpsetid == newpsetid)
18300Sstevel@tonic-gate 		return;
18310Sstevel@tonic-gate 	/*
18320Sstevel@tonic-gate 	 * Global zone sees all.
18330Sstevel@tonic-gate 	 */
18340Sstevel@tonic-gate 	if (zone != global_zone) {
18350Sstevel@tonic-gate 		zone->zone_psetid = newpsetid;
18360Sstevel@tonic-gate 		if (newpsetid != ZONE_PS_INVAL)
18370Sstevel@tonic-gate 			pool_pset_visibility_add(newpsetid, zone);
18380Sstevel@tonic-gate 		if (oldpsetid != ZONE_PS_INVAL)
18390Sstevel@tonic-gate 			pool_pset_visibility_remove(oldpsetid, zone);
18400Sstevel@tonic-gate 	}
18410Sstevel@tonic-gate 	/*
18420Sstevel@tonic-gate 	 * Disabling pools, so we should start using the global values
18430Sstevel@tonic-gate 	 * for ncpus and ncpus_online.
18440Sstevel@tonic-gate 	 */
18450Sstevel@tonic-gate 	if (newpsetid == ZONE_PS_INVAL) {
18460Sstevel@tonic-gate 		zone->zone_ncpus = 0;
18470Sstevel@tonic-gate 		zone->zone_ncpus_online = 0;
18480Sstevel@tonic-gate 	}
18490Sstevel@tonic-gate }
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate /*
18520Sstevel@tonic-gate  * Walk the list of active zones and issue the provided callback for
18530Sstevel@tonic-gate  * each of them.
18540Sstevel@tonic-gate  *
18550Sstevel@tonic-gate  * Caller must not be holding any locks that may be acquired under
18560Sstevel@tonic-gate  * zonehash_lock.  See comment at the beginning of the file for a list of
18570Sstevel@tonic-gate  * common locks and their interactions with zones.
18580Sstevel@tonic-gate  */
18590Sstevel@tonic-gate int
18600Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data)
18610Sstevel@tonic-gate {
18620Sstevel@tonic-gate 	zone_t *zone;
18630Sstevel@tonic-gate 	int ret = 0;
18640Sstevel@tonic-gate 	zone_status_t status;
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
18670Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
18680Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
18690Sstevel@tonic-gate 		/*
18700Sstevel@tonic-gate 		 * Skip zones that shouldn't be externally visible.
18710Sstevel@tonic-gate 		 */
18720Sstevel@tonic-gate 		status = zone_status_get(zone);
18730Sstevel@tonic-gate 		if (status < ZONE_IS_READY || status > ZONE_IS_DOWN)
18740Sstevel@tonic-gate 			continue;
18750Sstevel@tonic-gate 		/*
18760Sstevel@tonic-gate 		 * Bail immediately if any callback invocation returns a
18770Sstevel@tonic-gate 		 * non-zero value.
18780Sstevel@tonic-gate 		 */
18790Sstevel@tonic-gate 		ret = (*cb)(zone, data);
18800Sstevel@tonic-gate 		if (ret != 0)
18810Sstevel@tonic-gate 			break;
18820Sstevel@tonic-gate 	}
18830Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
18840Sstevel@tonic-gate 	return (ret);
18850Sstevel@tonic-gate }
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate static int
18880Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath)
18890Sstevel@tonic-gate {
18900Sstevel@tonic-gate 	vnode_t *vp;
18910Sstevel@tonic-gate 	int trycount;
18920Sstevel@tonic-gate 	int error = 0;
18930Sstevel@tonic-gate 	char *path;
18940Sstevel@tonic-gate 	struct pathname upn, pn;
18950Sstevel@tonic-gate 	size_t pathlen;
18960Sstevel@tonic-gate 
18970Sstevel@tonic-gate 	if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0)
18980Sstevel@tonic-gate 		return (error);
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	pn_alloc(&pn);
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 	/* prevent infinite loop */
19030Sstevel@tonic-gate 	trycount = 10;
19040Sstevel@tonic-gate 	for (;;) {
19050Sstevel@tonic-gate 		if (--trycount <= 0) {
19060Sstevel@tonic-gate 			error = ESTALE;
19070Sstevel@tonic-gate 			goto out;
19080Sstevel@tonic-gate 		}
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate 		if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) {
19110Sstevel@tonic-gate 			/*
19120Sstevel@tonic-gate 			 * VOP_ACCESS() may cover 'vp' with a new
19130Sstevel@tonic-gate 			 * filesystem, if 'vp' is an autoFS vnode.
19140Sstevel@tonic-gate 			 * Get the new 'vp' if so.
19150Sstevel@tonic-gate 			 */
19160Sstevel@tonic-gate 			if ((error = VOP_ACCESS(vp, VEXEC, 0, CRED())) == 0 &&
19170Sstevel@tonic-gate 			    (vp->v_vfsmountedhere == NULL ||
19180Sstevel@tonic-gate 			    (error = traverse(&vp)) == 0)) {
19190Sstevel@tonic-gate 				pathlen = pn.pn_pathlen + 2;
19200Sstevel@tonic-gate 				path = kmem_alloc(pathlen, KM_SLEEP);
19210Sstevel@tonic-gate 				(void) strncpy(path, pn.pn_path,
19220Sstevel@tonic-gate 				    pn.pn_pathlen + 1);
19230Sstevel@tonic-gate 				path[pathlen - 2] = '/';
19240Sstevel@tonic-gate 				path[pathlen - 1] = '\0';
19250Sstevel@tonic-gate 				pn_free(&pn);
19260Sstevel@tonic-gate 				pn_free(&upn);
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate 				/* Success! */
19290Sstevel@tonic-gate 				break;
19300Sstevel@tonic-gate 			}
19310Sstevel@tonic-gate 			VN_RELE(vp);
19320Sstevel@tonic-gate 		}
19330Sstevel@tonic-gate 		if (error != ESTALE)
19340Sstevel@tonic-gate 			goto out;
19350Sstevel@tonic-gate 	}
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate 	ASSERT(error == 0);
19380Sstevel@tonic-gate 	zone->zone_rootvp = vp;		/* we hold a reference to vp */
19390Sstevel@tonic-gate 	zone->zone_rootpath = path;
19400Sstevel@tonic-gate 	zone->zone_rootpathlen = pathlen;
19411769Scarlsonj 	if (pathlen > 5 && strcmp(path + pathlen - 5, "/lu/") == 0)
19421769Scarlsonj 		zone->zone_flags |= ZF_IS_SCRATCH;
19430Sstevel@tonic-gate 	return (0);
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate out:
19460Sstevel@tonic-gate 	pn_free(&pn);
19470Sstevel@tonic-gate 	pn_free(&upn);
19480Sstevel@tonic-gate 	return (error);
19490Sstevel@tonic-gate }
19500Sstevel@tonic-gate 
19510Sstevel@tonic-gate #define	isalnum(c)	(((c) >= '0' && (c) <= '9') || \
19520Sstevel@tonic-gate 			((c) >= 'a' && (c) <= 'z') || \
19530Sstevel@tonic-gate 			((c) >= 'A' && (c) <= 'Z'))
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate static int
19560Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname)
19570Sstevel@tonic-gate {
19580Sstevel@tonic-gate 	char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
19590Sstevel@tonic-gate 	size_t len;
19600Sstevel@tonic-gate 	int i, err;
19610Sstevel@tonic-gate 
19620Sstevel@tonic-gate 	if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) {
19630Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
19640Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
19650Sstevel@tonic-gate 	}
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 	/* must be less than ZONENAME_MAX */
19680Sstevel@tonic-gate 	if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') {
19690Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
19700Sstevel@tonic-gate 		return (EINVAL);
19710Sstevel@tonic-gate 	}
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 	/*
19740Sstevel@tonic-gate 	 * Name must start with an alphanumeric and must contain only
19750Sstevel@tonic-gate 	 * alphanumerics, '-', '_' and '.'.
19760Sstevel@tonic-gate 	 */
19770Sstevel@tonic-gate 	if (!isalnum(kname[0])) {
19780Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
19790Sstevel@tonic-gate 		return (EINVAL);
19800Sstevel@tonic-gate 	}
19810Sstevel@tonic-gate 	for (i = 1; i < len - 1; i++) {
19820Sstevel@tonic-gate 		if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' &&
19830Sstevel@tonic-gate 		    kname[i] != '.') {
19840Sstevel@tonic-gate 			kmem_free(kname, ZONENAME_MAX);
19850Sstevel@tonic-gate 			return (EINVAL);
19860Sstevel@tonic-gate 		}
19870Sstevel@tonic-gate 	}
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 	zone->zone_name = kname;
19900Sstevel@tonic-gate 	return (0);
19910Sstevel@tonic-gate }
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate /*
19940Sstevel@tonic-gate  * Similar to thread_create(), but makes sure the thread is in the appropriate
19950Sstevel@tonic-gate  * zone's zsched process (curproc->p_zone->zone_zsched) before returning.
19960Sstevel@tonic-gate  */
19970Sstevel@tonic-gate /*ARGSUSED*/
19980Sstevel@tonic-gate kthread_t *
19990Sstevel@tonic-gate zthread_create(
20000Sstevel@tonic-gate     caddr_t stk,
20010Sstevel@tonic-gate     size_t stksize,
20020Sstevel@tonic-gate     void (*proc)(),
20030Sstevel@tonic-gate     void *arg,
20040Sstevel@tonic-gate     size_t len,
20050Sstevel@tonic-gate     pri_t pri)
20060Sstevel@tonic-gate {
20070Sstevel@tonic-gate 	kthread_t *t;
20080Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
20090Sstevel@tonic-gate 	proc_t *pp = zone->zone_zsched;
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 	zone_hold(zone);	/* Reference to be dropped when thread exits */
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 	/*
20140Sstevel@tonic-gate 	 * No-one should be trying to create threads if the zone is shutting
20150Sstevel@tonic-gate 	 * down and there aren't any kernel threads around.  See comment
20160Sstevel@tonic-gate 	 * in zthread_exit().
20170Sstevel@tonic-gate 	 */
20180Sstevel@tonic-gate 	ASSERT(!(zone->zone_kthreads == NULL &&
20190Sstevel@tonic-gate 	    zone_status_get(zone) >= ZONE_IS_EMPTY));
20200Sstevel@tonic-gate 	/*
20210Sstevel@tonic-gate 	 * Create a thread, but don't let it run until we've finished setting
20220Sstevel@tonic-gate 	 * things up.
20230Sstevel@tonic-gate 	 */
20240Sstevel@tonic-gate 	t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri);
20250Sstevel@tonic-gate 	ASSERT(t->t_forw == NULL);
20260Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
20270Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL) {
20280Sstevel@tonic-gate 		t->t_forw = t->t_back = t;
20290Sstevel@tonic-gate 	} else {
20300Sstevel@tonic-gate 		kthread_t *tx = zone->zone_kthreads;
20310Sstevel@tonic-gate 
20320Sstevel@tonic-gate 		t->t_forw = tx;
20330Sstevel@tonic-gate 		t->t_back = tx->t_back;
20340Sstevel@tonic-gate 		tx->t_back->t_forw = t;
20350Sstevel@tonic-gate 		tx->t_back = t;
20360Sstevel@tonic-gate 	}
20370Sstevel@tonic-gate 	zone->zone_kthreads = t;
20380Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
20410Sstevel@tonic-gate 	t->t_proc_flag |= TP_ZTHREAD;
20420Sstevel@tonic-gate 	project_rele(t->t_proj);
20430Sstevel@tonic-gate 	t->t_proj = project_hold(pp->p_task->tk_proj);
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 	/*
20460Sstevel@tonic-gate 	 * Setup complete, let it run.
20470Sstevel@tonic-gate 	 */
20480Sstevel@tonic-gate 	thread_lock(t);
20490Sstevel@tonic-gate 	t->t_schedflag |= TS_ALLSTART;
20500Sstevel@tonic-gate 	setrun_locked(t);
20510Sstevel@tonic-gate 	thread_unlock(t);
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate 	return (t);
20560Sstevel@tonic-gate }
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate /*
20590Sstevel@tonic-gate  * Similar to thread_exit().  Must be called by threads created via
20600Sstevel@tonic-gate  * zthread_exit().
20610Sstevel@tonic-gate  */
20620Sstevel@tonic-gate void
20630Sstevel@tonic-gate zthread_exit(void)
20640Sstevel@tonic-gate {
20650Sstevel@tonic-gate 	kthread_t *t = curthread;
20660Sstevel@tonic-gate 	proc_t *pp = curproc;
20670Sstevel@tonic-gate 	zone_t *zone = pp->p_zone;
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	/*
20720Sstevel@tonic-gate 	 * Reparent to p0
20730Sstevel@tonic-gate 	 */
20741075Sjosephb 	kpreempt_disable();
20750Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
20760Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_ZTHREAD;
20770Sstevel@tonic-gate 	t->t_procp = &p0;
20780Sstevel@tonic-gate 	hat_thread_exit(t);
20790Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
20801075Sjosephb 	kpreempt_enable();
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 	if (t->t_back == t) {
20830Sstevel@tonic-gate 		ASSERT(t->t_forw == t);
20840Sstevel@tonic-gate 		/*
20850Sstevel@tonic-gate 		 * If the zone is empty, once the thread count
20860Sstevel@tonic-gate 		 * goes to zero no further kernel threads can be
20870Sstevel@tonic-gate 		 * created.  This is because if the creator is a process
20880Sstevel@tonic-gate 		 * in the zone, then it must have exited before the zone
20890Sstevel@tonic-gate 		 * state could be set to ZONE_IS_EMPTY.
20900Sstevel@tonic-gate 		 * Otherwise, if the creator is a kernel thread in the
20910Sstevel@tonic-gate 		 * zone, the thread count is non-zero.
20920Sstevel@tonic-gate 		 *
20930Sstevel@tonic-gate 		 * This really means that non-zone kernel threads should
20940Sstevel@tonic-gate 		 * not create zone kernel threads.
20950Sstevel@tonic-gate 		 */
20960Sstevel@tonic-gate 		zone->zone_kthreads = NULL;
20970Sstevel@tonic-gate 		if (zone_status_get(zone) == ZONE_IS_EMPTY) {
20980Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_DOWN);
20990Sstevel@tonic-gate 		}
21000Sstevel@tonic-gate 	} else {
21010Sstevel@tonic-gate 		t->t_forw->t_back = t->t_back;
21020Sstevel@tonic-gate 		t->t_back->t_forw = t->t_forw;
21030Sstevel@tonic-gate 		if (zone->zone_kthreads == t)
21040Sstevel@tonic-gate 			zone->zone_kthreads = t->t_forw;
21050Sstevel@tonic-gate 	}
21060Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
21070Sstevel@tonic-gate 	zone_rele(zone);
21080Sstevel@tonic-gate 	thread_exit();
21090Sstevel@tonic-gate 	/* NOTREACHED */
21100Sstevel@tonic-gate }
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate static void
21130Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp)
21140Sstevel@tonic-gate {
21150Sstevel@tonic-gate 	vnode_t *oldvp;
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 	/* we're going to hold a reference here to the directory */
21180Sstevel@tonic-gate 	VN_HOLD(vp);
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate #ifdef C2_AUDIT
21210Sstevel@tonic-gate 	if (audit_active)	/* update abs cwd/root path see c2audit.c */
21220Sstevel@tonic-gate 		audit_chdirec(vp, vpp);
21230Sstevel@tonic-gate #endif
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
21260Sstevel@tonic-gate 	oldvp = *vpp;
21270Sstevel@tonic-gate 	*vpp = vp;
21280Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
21290Sstevel@tonic-gate 	if (oldvp != NULL)
21300Sstevel@tonic-gate 		VN_RELE(oldvp);
21310Sstevel@tonic-gate }
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate /*
21340Sstevel@tonic-gate  * Convert an rctl value represented by an nvlist_t into an rctl_val_t.
21350Sstevel@tonic-gate  */
21360Sstevel@tonic-gate static int
21370Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv)
21380Sstevel@tonic-gate {
21390Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
21400Sstevel@tonic-gate 	boolean_t priv_set = B_FALSE;
21410Sstevel@tonic-gate 	boolean_t limit_set = B_FALSE;
21420Sstevel@tonic-gate 	boolean_t action_set = B_FALSE;
21430Sstevel@tonic-gate 
21440Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
21450Sstevel@tonic-gate 		const char *name;
21460Sstevel@tonic-gate 		uint64_t ui64;
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate 		name = nvpair_name(nvp);
21490Sstevel@tonic-gate 		if (nvpair_type(nvp) != DATA_TYPE_UINT64)
21500Sstevel@tonic-gate 			return (EINVAL);
21510Sstevel@tonic-gate 		(void) nvpair_value_uint64(nvp, &ui64);
21520Sstevel@tonic-gate 		if (strcmp(name, "privilege") == 0) {
21530Sstevel@tonic-gate 			/*
21540Sstevel@tonic-gate 			 * Currently only privileged values are allowed, but
21550Sstevel@tonic-gate 			 * this may change in the future.
21560Sstevel@tonic-gate 			 */
21570Sstevel@tonic-gate 			if (ui64 != RCPRIV_PRIVILEGED)
21580Sstevel@tonic-gate 				return (EINVAL);
21590Sstevel@tonic-gate 			rv->rcv_privilege = ui64;
21600Sstevel@tonic-gate 			priv_set = B_TRUE;
21610Sstevel@tonic-gate 		} else if (strcmp(name, "limit") == 0) {
21620Sstevel@tonic-gate 			rv->rcv_value = ui64;
21630Sstevel@tonic-gate 			limit_set = B_TRUE;
21640Sstevel@tonic-gate 		} else if (strcmp(name, "action") == 0) {
21650Sstevel@tonic-gate 			if (ui64 != RCTL_LOCAL_NOACTION &&
21660Sstevel@tonic-gate 			    ui64 != RCTL_LOCAL_DENY)
21670Sstevel@tonic-gate 				return (EINVAL);
21680Sstevel@tonic-gate 			rv->rcv_flagaction = ui64;
21690Sstevel@tonic-gate 			action_set = B_TRUE;
21700Sstevel@tonic-gate 		} else {
21710Sstevel@tonic-gate 			return (EINVAL);
21720Sstevel@tonic-gate 		}
21730Sstevel@tonic-gate 	}
21740Sstevel@tonic-gate 
21750Sstevel@tonic-gate 	if (!(priv_set && limit_set && action_set))
21760Sstevel@tonic-gate 		return (EINVAL);
21770Sstevel@tonic-gate 	rv->rcv_action_signal = 0;
21780Sstevel@tonic-gate 	rv->rcv_action_recipient = NULL;
21790Sstevel@tonic-gate 	rv->rcv_action_recip_pid = -1;
21800Sstevel@tonic-gate 	rv->rcv_firing_time = 0;
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 	return (0);
21830Sstevel@tonic-gate }
21840Sstevel@tonic-gate 
21850Sstevel@tonic-gate void
21860Sstevel@tonic-gate zone_icode(void)
21870Sstevel@tonic-gate {
21880Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
21890Sstevel@tonic-gate 	struct core_globals	*cg;
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	/*
21920Sstevel@tonic-gate 	 * For all purposes (ZONE_ATTR_INITPID and restart_init),
21930Sstevel@tonic-gate 	 * storing just the pid of init is sufficient.
21940Sstevel@tonic-gate 	 */
21950Sstevel@tonic-gate 	p->p_zone->zone_proc_initpid = p->p_pid;
21960Sstevel@tonic-gate 
21970Sstevel@tonic-gate 	/*
21980Sstevel@tonic-gate 	 * Allocate user address space and stack segment
21990Sstevel@tonic-gate 	 */
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate 	p->p_cstime = p->p_stime = p->p_cutime = p->p_utime = 0;
22020Sstevel@tonic-gate 	p->p_usrstack = (caddr_t)USRSTACK32;
22030Sstevel@tonic-gate 	p->p_model = DATAMODEL_ILP32;
22040Sstevel@tonic-gate 	p->p_stkprot = PROT_ZFOD & ~PROT_EXEC;
22050Sstevel@tonic-gate 	p->p_datprot = PROT_ZFOD & ~PROT_EXEC;
22060Sstevel@tonic-gate 	p->p_stk_ctl = INT32_MAX;
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 	p->p_as = as_alloc();
22090Sstevel@tonic-gate 	p->p_as->a_userlimit = (caddr_t)USERLIMIT32;
22100Sstevel@tonic-gate 	(void) hat_setup(p->p_as->a_hat, HAT_INIT);
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 	cg = zone_getspecific(core_zone_key, p->p_zone);
22130Sstevel@tonic-gate 	ASSERT(cg != NULL);
22140Sstevel@tonic-gate 	corectl_path_hold(cg->core_default_path);
22150Sstevel@tonic-gate 	corectl_content_hold(cg->core_default_content);
22160Sstevel@tonic-gate 	p->p_corefile = cg->core_default_path;
22170Sstevel@tonic-gate 	p->p_content = cg->core_default_content;
22180Sstevel@tonic-gate 
22190Sstevel@tonic-gate 	init_mstate(curthread, LMS_SYSTEM);
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate 	p->p_zone->zone_boot_err = exec_init(zone_initname, 0,
22220Sstevel@tonic-gate 	    p->p_zone->zone_bootargs);
22230Sstevel@tonic-gate 
22240Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
22250Sstevel@tonic-gate 	if (p->p_zone->zone_boot_err != 0) {
22260Sstevel@tonic-gate 		/*
22270Sstevel@tonic-gate 		 * Make sure we are still in the booting state-- we could have
22280Sstevel@tonic-gate 		 * raced and already be shutting down, or even further along.
22290Sstevel@tonic-gate 		 */
22300Sstevel@tonic-gate 		if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING)
22310Sstevel@tonic-gate 			zone_status_set(p->p_zone, ZONE_IS_SHUTTING_DOWN);
22320Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
22330Sstevel@tonic-gate 		/* It's gone bad, dispose of the process */
22340Sstevel@tonic-gate 		if (proc_exit(CLD_EXITED, p->p_zone->zone_boot_err) != 0) {
2235390Sraf 			mutex_enter(&p->p_lock);
2236390Sraf 			ASSERT(p->p_flag & SEXITLWPS);
22370Sstevel@tonic-gate 			lwp_exit();
22380Sstevel@tonic-gate 		}
22390Sstevel@tonic-gate 	} else {
22400Sstevel@tonic-gate 		if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING)
22410Sstevel@tonic-gate 			zone_status_set(p->p_zone, ZONE_IS_RUNNING);
22420Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
22430Sstevel@tonic-gate 		/* cause the process to return to userland. */
22440Sstevel@tonic-gate 		lwp_rtt();
22450Sstevel@tonic-gate 	}
22460Sstevel@tonic-gate }
22470Sstevel@tonic-gate 
22480Sstevel@tonic-gate struct zsched_arg {
22490Sstevel@tonic-gate 	zone_t *zone;
22500Sstevel@tonic-gate 	nvlist_t *nvlist;
22510Sstevel@tonic-gate };
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate /*
22540Sstevel@tonic-gate  * Per-zone "sched" workalike.  The similarity to "sched" doesn't have
22550Sstevel@tonic-gate  * anything to do with scheduling, but rather with the fact that
22560Sstevel@tonic-gate  * per-zone kernel threads are parented to zsched, just like regular
22570Sstevel@tonic-gate  * kernel threads are parented to sched (p0).
22580Sstevel@tonic-gate  *
22590Sstevel@tonic-gate  * zsched is also responsible for launching init for the zone.
22600Sstevel@tonic-gate  */
22610Sstevel@tonic-gate static void
22620Sstevel@tonic-gate zsched(void *arg)
22630Sstevel@tonic-gate {
22640Sstevel@tonic-gate 	struct zsched_arg *za = arg;
22650Sstevel@tonic-gate 	proc_t *pp = curproc;
22660Sstevel@tonic-gate 	proc_t *initp = proc_init;
22670Sstevel@tonic-gate 	zone_t *zone = za->zone;
22680Sstevel@tonic-gate 	cred_t *cr, *oldcred;
22690Sstevel@tonic-gate 	rctl_set_t *set;
22700Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
22710Sstevel@tonic-gate 	contract_t *ct = NULL;
22720Sstevel@tonic-gate 	task_t *tk, *oldtk;
22730Sstevel@tonic-gate 	rctl_entity_p_t e;
22740Sstevel@tonic-gate 	kproject_t *pj;
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 	nvlist_t *nvl = za->nvlist;
22770Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate 	bcopy("zsched", u.u_psargs, sizeof ("zsched"));
22800Sstevel@tonic-gate 	bcopy("zsched", u.u_comm, sizeof ("zsched"));
22810Sstevel@tonic-gate 	u.u_argc = 0;
22820Sstevel@tonic-gate 	u.u_argv = NULL;
22830Sstevel@tonic-gate 	u.u_envp = NULL;
22840Sstevel@tonic-gate 	closeall(P_FINFO(pp));
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate 	/*
22870Sstevel@tonic-gate 	 * We are this zone's "zsched" process.  As the zone isn't generally
22880Sstevel@tonic-gate 	 * visible yet we don't need to grab any locks before initializing its
22890Sstevel@tonic-gate 	 * zone_proc pointer.
22900Sstevel@tonic-gate 	 */
22910Sstevel@tonic-gate 	zone_hold(zone);  /* this hold is released by zone_destroy() */
22920Sstevel@tonic-gate 	zone->zone_zsched = pp;
22930Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
22940Sstevel@tonic-gate 	pp->p_zone = zone;
22950Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 	/*
22980Sstevel@tonic-gate 	 * Disassociate process from its 'parent'; parent ourselves to init
22990Sstevel@tonic-gate 	 * (pid 1) and change other values as needed.
23000Sstevel@tonic-gate 	 */
23010Sstevel@tonic-gate 	sess_create();
23020Sstevel@tonic-gate 
23030Sstevel@tonic-gate 	mutex_enter(&pidlock);
23040Sstevel@tonic-gate 	proc_detach(pp);
23050Sstevel@tonic-gate 	pp->p_ppid = 1;
23060Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
23070Sstevel@tonic-gate 	pp->p_ancpid = 1;
23080Sstevel@tonic-gate 	pp->p_parent = initp;
23090Sstevel@tonic-gate 	pp->p_psibling = NULL;
23100Sstevel@tonic-gate 	if (initp->p_child)
23110Sstevel@tonic-gate 		initp->p_child->p_psibling = pp;
23120Sstevel@tonic-gate 	pp->p_sibling = initp->p_child;
23130Sstevel@tonic-gate 	initp->p_child = pp;
23140Sstevel@tonic-gate 
23150Sstevel@tonic-gate 	/* Decrement what newproc() incremented. */
23160Sstevel@tonic-gate 	upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID);
23170Sstevel@tonic-gate 	/*
23180Sstevel@tonic-gate 	 * Our credentials are about to become kcred-like, so we don't care
23190Sstevel@tonic-gate 	 * about the caller's ruid.
23200Sstevel@tonic-gate 	 */
23210Sstevel@tonic-gate 	upcount_inc(crgetruid(kcred), zone->zone_id);
23220Sstevel@tonic-gate 	mutex_exit(&pidlock);
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate 	/*
23250Sstevel@tonic-gate 	 * getting out of global zone, so decrement lwp counts
23260Sstevel@tonic-gate 	 */
23270Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
23280Sstevel@tonic-gate 	mutex_enter(&global_zone->zone_nlwps_lock);
23290Sstevel@tonic-gate 	pj->kpj_nlwps -= pp->p_lwpcnt;
23300Sstevel@tonic-gate 	global_zone->zone_nlwps -= pp->p_lwpcnt;
23310Sstevel@tonic-gate 	mutex_exit(&global_zone->zone_nlwps_lock);
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 	/*
23340Sstevel@tonic-gate 	 * Create and join a new task in project '0' of this zone.
23350Sstevel@tonic-gate 	 *
23360Sstevel@tonic-gate 	 * We don't need to call holdlwps() since we know we're the only lwp in
23370Sstevel@tonic-gate 	 * this process.
23380Sstevel@tonic-gate 	 *
23390Sstevel@tonic-gate 	 * task_join() returns with p_lock held.
23400Sstevel@tonic-gate 	 */
23410Sstevel@tonic-gate 	tk = task_create(0, zone);
23420Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
23430Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
23440Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
23450Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
23460Sstevel@tonic-gate 	task_rele(oldtk);
23470Sstevel@tonic-gate 
23480Sstevel@tonic-gate 	/*
23490Sstevel@tonic-gate 	 * add lwp counts to zsched's zone, and increment project's task count
23500Sstevel@tonic-gate 	 * due to the task created in the above tasksys_settaskid
23510Sstevel@tonic-gate 	 */
23520Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
23530Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
23540Sstevel@tonic-gate 	pj->kpj_nlwps += pp->p_lwpcnt;
23550Sstevel@tonic-gate 	pj->kpj_ntasks += 1;
23560Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
23570Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 	/*
23600Sstevel@tonic-gate 	 * The process was created by a process in the global zone, hence the
23610Sstevel@tonic-gate 	 * credentials are wrong.  We might as well have kcred-ish credentials.
23620Sstevel@tonic-gate 	 */
23630Sstevel@tonic-gate 	cr = zone->zone_kcred;
23640Sstevel@tonic-gate 	crhold(cr);
23650Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
23660Sstevel@tonic-gate 	oldcred = pp->p_cred;
23670Sstevel@tonic-gate 	pp->p_cred = cr;
23680Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
23690Sstevel@tonic-gate 	crfree(oldcred);
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate 	/*
23720Sstevel@tonic-gate 	 * Hold credentials again (for thread)
23730Sstevel@tonic-gate 	 */
23740Sstevel@tonic-gate 	crhold(cr);
23750Sstevel@tonic-gate 
23760Sstevel@tonic-gate 	/*
23770Sstevel@tonic-gate 	 * p_lwpcnt can't change since this is a kernel process.
23780Sstevel@tonic-gate 	 */
23790Sstevel@tonic-gate 	crset(pp, cr);
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate 	/*
23820Sstevel@tonic-gate 	 * Chroot
23830Sstevel@tonic-gate 	 */
23840Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp);
23850Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp);
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate 	/*
23880Sstevel@tonic-gate 	 * Initialize zone's rctl set.
23890Sstevel@tonic-gate 	 */
23900Sstevel@tonic-gate 	set = rctl_set_create();
23910Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
23920Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
23930Sstevel@tonic-gate 	e.rcep_p.zone = zone;
23940Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
23950Sstevel@tonic-gate 	zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp);
23960Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
23970Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	/*
24000Sstevel@tonic-gate 	 * Apply the rctls passed in to zone_create().  This is basically a list
24010Sstevel@tonic-gate 	 * assignment: all of the old values are removed and the new ones
24020Sstevel@tonic-gate 	 * inserted.  That is, if an empty list is passed in, all values are
24030Sstevel@tonic-gate 	 * removed.
24040Sstevel@tonic-gate 	 */
24050Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
24060Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
24070Sstevel@tonic-gate 		rctl_hndl_t hndl;
24080Sstevel@tonic-gate 		char *name;
24090Sstevel@tonic-gate 		nvlist_t **nvlarray;
24100Sstevel@tonic-gate 		uint_t i, nelem;
24110Sstevel@tonic-gate 		int error;	/* For ASSERT()s */
24120Sstevel@tonic-gate 
24130Sstevel@tonic-gate 		name = nvpair_name(nvp);
24140Sstevel@tonic-gate 		hndl = rctl_hndl_lookup(name);
24150Sstevel@tonic-gate 		ASSERT(hndl != -1);
24160Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
24170Sstevel@tonic-gate 		ASSERT(rde != NULL);
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate 		for (; /* ever */; ) {
24200Sstevel@tonic-gate 			rctl_val_t oval;
24210Sstevel@tonic-gate 
24220Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
24230Sstevel@tonic-gate 			error = rctl_local_get(hndl, NULL, &oval, pp);
24240Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
24250Sstevel@tonic-gate 			ASSERT(error == 0);	/* Can't fail for RCTL_FIRST */
24260Sstevel@tonic-gate 			ASSERT(oval.rcv_privilege != RCPRIV_BASIC);
24270Sstevel@tonic-gate 			if (oval.rcv_privilege == RCPRIV_SYSTEM)
24280Sstevel@tonic-gate 				break;
24290Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
24300Sstevel@tonic-gate 			error = rctl_local_delete(hndl, &oval, pp);
24310Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
24320Sstevel@tonic-gate 			ASSERT(error == 0);
24330Sstevel@tonic-gate 		}
24340Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
24350Sstevel@tonic-gate 		ASSERT(error == 0);
24360Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
24370Sstevel@tonic-gate 			rctl_val_t *nvalp;
24380Sstevel@tonic-gate 
24390Sstevel@tonic-gate 			nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
24400Sstevel@tonic-gate 			error = nvlist2rctlval(nvlarray[i], nvalp);
24410Sstevel@tonic-gate 			ASSERT(error == 0);
24420Sstevel@tonic-gate 			/*
24430Sstevel@tonic-gate 			 * rctl_local_insert can fail if the value being
24440Sstevel@tonic-gate 			 * inserted is a duplicate; this is OK.
24450Sstevel@tonic-gate 			 */
24460Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
24470Sstevel@tonic-gate 			if (rctl_local_insert(hndl, nvalp, pp) != 0)
24480Sstevel@tonic-gate 				kmem_cache_free(rctl_val_cache, nvalp);
24490Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
24500Sstevel@tonic-gate 		}
24510Sstevel@tonic-gate 	}
24520Sstevel@tonic-gate 	/*
24530Sstevel@tonic-gate 	 * Tell the world that we're done setting up.
24540Sstevel@tonic-gate 	 *
24550Sstevel@tonic-gate 	 * At this point we want to set the zone status to ZONE_IS_READY
24560Sstevel@tonic-gate 	 * and atomically set the zone's processor set visibility.  Once
24570Sstevel@tonic-gate 	 * we drop pool_lock() this zone will automatically get updated
24580Sstevel@tonic-gate 	 * to reflect any future changes to the pools configuration.
24590Sstevel@tonic-gate 	 */
24600Sstevel@tonic-gate 	pool_lock();
24610Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
24620Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
24630Sstevel@tonic-gate 	zone_uniqid(zone);
24640Sstevel@tonic-gate 	zone_zsd_configure(zone);
24650Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED)
24660Sstevel@tonic-gate 		zone_pset_set(zone, pool_default->pool_pset->pset_id);
24670Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
24680Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
24690Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_READY);
24700Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
24710Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
24720Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
24730Sstevel@tonic-gate 	pool_unlock();
24740Sstevel@tonic-gate 
24750Sstevel@tonic-gate 	/*
24760Sstevel@tonic-gate 	 * Once we see the zone transition to the ZONE_IS_BOOTING state,
24770Sstevel@tonic-gate 	 * we launch init, and set the state to running.
24780Sstevel@tonic-gate 	 */
24790Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched");
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_BOOTING) {
24820Sstevel@tonic-gate 		id_t cid;
24830Sstevel@tonic-gate 
24840Sstevel@tonic-gate 		/*
24850Sstevel@tonic-gate 		 * Ok, this is a little complicated.  We need to grab the
24860Sstevel@tonic-gate 		 * zone's pool's scheduling class ID; note that by now, we
24870Sstevel@tonic-gate 		 * are already bound to a pool if we need to be (zoneadmd
24880Sstevel@tonic-gate 		 * will have done that to us while we're in the READY
24890Sstevel@tonic-gate 		 * state).  *But* the scheduling class for the zone's 'init'
24900Sstevel@tonic-gate 		 * must be explicitly passed to newproc, which doesn't
24910Sstevel@tonic-gate 		 * respect pool bindings.
24920Sstevel@tonic-gate 		 *
24930Sstevel@tonic-gate 		 * We hold the pool_lock across the call to newproc() to
24940Sstevel@tonic-gate 		 * close the obvious race: the pool's scheduling class
24950Sstevel@tonic-gate 		 * could change before we manage to create the LWP with
24960Sstevel@tonic-gate 		 * classid 'cid'.
24970Sstevel@tonic-gate 		 */
24980Sstevel@tonic-gate 		pool_lock();
24990Sstevel@tonic-gate 		cid = pool_get_class(zone->zone_pool);
25000Sstevel@tonic-gate 		if (cid == -1)
25010Sstevel@tonic-gate 			cid = defaultcid;
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 		/*
25040Sstevel@tonic-gate 		 * If this fails, zone_boot will ultimately fail.  The
25050Sstevel@tonic-gate 		 * state of the zone will be set to SHUTTING_DOWN-- userland
25060Sstevel@tonic-gate 		 * will have to tear down the zone, and fail, or try again.
25070Sstevel@tonic-gate 		 */
25080Sstevel@tonic-gate 		if ((zone->zone_boot_err = newproc(zone_icode, NULL, cid,
25090Sstevel@tonic-gate 		    minclsyspri - 1, &ct)) != 0) {
25100Sstevel@tonic-gate 			mutex_enter(&zone_status_lock);
25110Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
25120Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
25130Sstevel@tonic-gate 		}
25140Sstevel@tonic-gate 		pool_unlock();
25150Sstevel@tonic-gate 	}
25160Sstevel@tonic-gate 
25170Sstevel@tonic-gate 	/*
25180Sstevel@tonic-gate 	 * Wait for zone_destroy() to be called.  This is what we spend
25190Sstevel@tonic-gate 	 * most of our life doing.
25200Sstevel@tonic-gate 	 */
25210Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched");
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 	if (ct)
25240Sstevel@tonic-gate 		/*
25250Sstevel@tonic-gate 		 * At this point the process contract should be empty.
25260Sstevel@tonic-gate 		 * (Though if it isn't, it's not the end of the world.)
25270Sstevel@tonic-gate 		 */
25280Sstevel@tonic-gate 		VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0);
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 	/*
25310Sstevel@tonic-gate 	 * Allow kcred to be freed when all referring processes
25320Sstevel@tonic-gate 	 * (including this one) go away.  We can't just do this in
25330Sstevel@tonic-gate 	 * zone_free because we need to wait for the zone_cred_ref to
25340Sstevel@tonic-gate 	 * drop to 0 before calling zone_free, and the existence of
25350Sstevel@tonic-gate 	 * zone_kcred will prevent that.  Thus, we call crfree here to
25360Sstevel@tonic-gate 	 * balance the crdup in zone_create.  The crhold calls earlier
25370Sstevel@tonic-gate 	 * in zsched will be dropped when the thread and process exit.
25380Sstevel@tonic-gate 	 */
25390Sstevel@tonic-gate 	crfree(zone->zone_kcred);
25400Sstevel@tonic-gate 	zone->zone_kcred = NULL;
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
25430Sstevel@tonic-gate }
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate /*
25460Sstevel@tonic-gate  * Helper function to determine if there are any submounts of the
25470Sstevel@tonic-gate  * provided path.  Used to make sure the zone doesn't "inherit" any
25480Sstevel@tonic-gate  * mounts from before it is created.
25490Sstevel@tonic-gate  */
25500Sstevel@tonic-gate static uint_t
25510Sstevel@tonic-gate zone_mount_count(const char *rootpath)
25520Sstevel@tonic-gate {
25530Sstevel@tonic-gate 	vfs_t *vfsp;
25540Sstevel@tonic-gate 	uint_t count = 0;
25550Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate 	/*
25580Sstevel@tonic-gate 	 * Holding zonehash_lock prevents race conditions with
25590Sstevel@tonic-gate 	 * vfs_list_add()/vfs_list_remove() since we serialize with
25600Sstevel@tonic-gate 	 * zone_find_by_path().
25610Sstevel@tonic-gate 	 */
25620Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
25630Sstevel@tonic-gate 	/*
25640Sstevel@tonic-gate 	 * The rootpath must end with a '/'
25650Sstevel@tonic-gate 	 */
25660Sstevel@tonic-gate 	ASSERT(rootpath[rootpathlen - 1] == '/');
25670Sstevel@tonic-gate 
25680Sstevel@tonic-gate 	/*
25690Sstevel@tonic-gate 	 * This intentionally does not count the rootpath itself if that
25700Sstevel@tonic-gate 	 * happens to be a mount point.
25710Sstevel@tonic-gate 	 */
25720Sstevel@tonic-gate 	vfs_list_read_lock();
25730Sstevel@tonic-gate 	vfsp = rootvfs;
25740Sstevel@tonic-gate 	do {
25750Sstevel@tonic-gate 		if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt),
25760Sstevel@tonic-gate 		    rootpathlen) == 0)
25770Sstevel@tonic-gate 			count++;
25780Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
25790Sstevel@tonic-gate 	} while (vfsp != rootvfs);
25800Sstevel@tonic-gate 	vfs_list_unlock();
25810Sstevel@tonic-gate 	return (count);
25820Sstevel@tonic-gate }
25830Sstevel@tonic-gate 
25840Sstevel@tonic-gate /*
25850Sstevel@tonic-gate  * Helper function to make sure that a zone created on 'rootpath'
25860Sstevel@tonic-gate  * wouldn't end up containing other zones' rootpaths.
25870Sstevel@tonic-gate  */
25880Sstevel@tonic-gate static boolean_t
25890Sstevel@tonic-gate zone_is_nested(const char *rootpath)
25900Sstevel@tonic-gate {
25910Sstevel@tonic-gate 	zone_t *zone;
25920Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
25930Sstevel@tonic-gate 	size_t len;
25940Sstevel@tonic-gate 
25950Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
25960Sstevel@tonic-gate 
25970Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
25980Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
25990Sstevel@tonic-gate 		if (zone == global_zone)
26000Sstevel@tonic-gate 			continue;
26010Sstevel@tonic-gate 		len = strlen(zone->zone_rootpath);
26020Sstevel@tonic-gate 		if (strncmp(rootpath, zone->zone_rootpath,
26030Sstevel@tonic-gate 		    MIN(rootpathlen, len)) == 0)
26040Sstevel@tonic-gate 			return (B_TRUE);
26050Sstevel@tonic-gate 	}
26060Sstevel@tonic-gate 	return (B_FALSE);
26070Sstevel@tonic-gate }
26080Sstevel@tonic-gate 
26090Sstevel@tonic-gate static int
2610813Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs,
2611813Sdp     size_t zone_privssz)
26120Sstevel@tonic-gate {
26130Sstevel@tonic-gate 	priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
26140Sstevel@tonic-gate 
2615813Sdp 	if (zone_privssz < sizeof (priv_set_t))
2616813Sdp 		return (set_errno(ENOMEM));
2617813Sdp 
26180Sstevel@tonic-gate 	if (copyin(zone_privs, privs, sizeof (priv_set_t))) {
26190Sstevel@tonic-gate 		kmem_free(privs, sizeof (priv_set_t));
26200Sstevel@tonic-gate 		return (EFAULT);
26210Sstevel@tonic-gate 	}
26220Sstevel@tonic-gate 
26230Sstevel@tonic-gate 	zone->zone_privset = privs;
26240Sstevel@tonic-gate 	return (0);
26250Sstevel@tonic-gate }
26260Sstevel@tonic-gate 
26270Sstevel@tonic-gate /*
26280Sstevel@tonic-gate  * We make creative use of nvlists to pass in rctls from userland.  The list is
26290Sstevel@tonic-gate  * a list of the following structures:
26300Sstevel@tonic-gate  *
26310Sstevel@tonic-gate  * (name = rctl_name, value = nvpair_list_array)
26320Sstevel@tonic-gate  *
26330Sstevel@tonic-gate  * Where each element of the nvpair_list_array is of the form:
26340Sstevel@tonic-gate  *
26350Sstevel@tonic-gate  * [(name = "privilege", value = RCPRIV_PRIVILEGED),
26360Sstevel@tonic-gate  * 	(name = "limit", value = uint64_t),
26370Sstevel@tonic-gate  * 	(name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))]
26380Sstevel@tonic-gate  */
26390Sstevel@tonic-gate static int
26400Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp)
26410Sstevel@tonic-gate {
26420Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
26430Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
26440Sstevel@tonic-gate 	char *kbuf;
26450Sstevel@tonic-gate 	int error;
26460Sstevel@tonic-gate 	rctl_val_t rv;
26470Sstevel@tonic-gate 
26480Sstevel@tonic-gate 	*nvlp = NULL;
26490Sstevel@tonic-gate 
26500Sstevel@tonic-gate 	if (buflen == 0)
26510Sstevel@tonic-gate 		return (0);
26520Sstevel@tonic-gate 
26530Sstevel@tonic-gate 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
26540Sstevel@tonic-gate 		return (ENOMEM);
26550Sstevel@tonic-gate 	if (copyin(ubuf, kbuf, buflen)) {
26560Sstevel@tonic-gate 		error = EFAULT;
26570Sstevel@tonic-gate 		goto out;
26580Sstevel@tonic-gate 	}
26590Sstevel@tonic-gate 	if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) {
26600Sstevel@tonic-gate 		/*
26610Sstevel@tonic-gate 		 * nvl may have been allocated/free'd, but the value set to
26620Sstevel@tonic-gate 		 * non-NULL, so we reset it here.
26630Sstevel@tonic-gate 		 */
26640Sstevel@tonic-gate 		nvl = NULL;
26650Sstevel@tonic-gate 		error = EINVAL;
26660Sstevel@tonic-gate 		goto out;
26670Sstevel@tonic-gate 	}
26680Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
26690Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
26700Sstevel@tonic-gate 		rctl_hndl_t hndl;
26710Sstevel@tonic-gate 		nvlist_t **nvlarray;
26720Sstevel@tonic-gate 		uint_t i, nelem;
26730Sstevel@tonic-gate 		char *name;
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 		error = EINVAL;
26760Sstevel@tonic-gate 		name = nvpair_name(nvp);
26770Sstevel@tonic-gate 		if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1)
26780Sstevel@tonic-gate 		    != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) {
26790Sstevel@tonic-gate 			goto out;
26800Sstevel@tonic-gate 		}
26810Sstevel@tonic-gate 		if ((hndl = rctl_hndl_lookup(name)) == -1) {
26820Sstevel@tonic-gate 			goto out;
26830Sstevel@tonic-gate 		}
26840Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
26850Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
26860Sstevel@tonic-gate 		ASSERT(error == 0);
26870Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
26880Sstevel@tonic-gate 			if (error = nvlist2rctlval(nvlarray[i], &rv))
26890Sstevel@tonic-gate 				goto out;
26900Sstevel@tonic-gate 		}
26910Sstevel@tonic-gate 		if (rctl_invalid_value(rde, &rv)) {
26920Sstevel@tonic-gate 			error = EINVAL;
26930Sstevel@tonic-gate 			goto out;
26940Sstevel@tonic-gate 		}
26950Sstevel@tonic-gate 	}
26960Sstevel@tonic-gate 	error = 0;
26970Sstevel@tonic-gate 	*nvlp = nvl;
26980Sstevel@tonic-gate out:
26990Sstevel@tonic-gate 	kmem_free(kbuf, buflen);
27000Sstevel@tonic-gate 	if (error && nvl != NULL)
27010Sstevel@tonic-gate 		nvlist_free(nvl);
27020Sstevel@tonic-gate 	return (error);
27030Sstevel@tonic-gate }
27040Sstevel@tonic-gate 
27050Sstevel@tonic-gate int
27060Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) {
27070Sstevel@tonic-gate 	if (er_out != NULL) {
27080Sstevel@tonic-gate 		if (copyout(&er_ext, er_out, sizeof (int))) {
27090Sstevel@tonic-gate 			return (set_errno(EFAULT));
27100Sstevel@tonic-gate 		}
27110Sstevel@tonic-gate 	}
27120Sstevel@tonic-gate 	return (set_errno(er_error));
27130Sstevel@tonic-gate }
27140Sstevel@tonic-gate 
27151676Sjpk static int
27161676Sjpk zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi)
27171676Sjpk {
27181676Sjpk 	ts_label_t *tsl;
27191676Sjpk 	bslabel_t blab;
27201676Sjpk 
27211676Sjpk 	/* Get label from user */
27221676Sjpk 	if (copyin(lab, &blab, sizeof (blab)) != 0)
27231676Sjpk 		return (EFAULT);
27241676Sjpk 	tsl = labelalloc(&blab, doi, KM_NOSLEEP);
27251676Sjpk 	if (tsl == NULL)
27261676Sjpk 		return (ENOMEM);
27271676Sjpk 
27281676Sjpk 	zone->zone_slabel = tsl;
27291676Sjpk 	return (0);
27301676Sjpk }
27311676Sjpk 
27320Sstevel@tonic-gate /*
2733789Sahrens  * Parses a comma-separated list of ZFS datasets into a per-zone dictionary.
2734789Sahrens  */
2735789Sahrens static int
2736789Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen)
2737789Sahrens {
2738789Sahrens 	char *kbuf;
2739789Sahrens 	char *dataset, *next;
2740789Sahrens 	zone_dataset_t *zd;
2741789Sahrens 	size_t len;
2742789Sahrens 
2743789Sahrens 	if (ubuf == NULL || buflen == 0)
2744789Sahrens 		return (0);
2745789Sahrens 
2746789Sahrens 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
2747789Sahrens 		return (ENOMEM);
2748789Sahrens 
2749789Sahrens 	if (copyin(ubuf, kbuf, buflen) != 0) {
2750789Sahrens 		kmem_free(kbuf, buflen);
2751789Sahrens 		return (EFAULT);
2752789Sahrens 	}
2753789Sahrens 
2754789Sahrens 	dataset = next = kbuf;
2755789Sahrens 	for (;;) {
2756789Sahrens 		zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP);
2757789Sahrens 
2758789Sahrens 		next = strchr(dataset, ',');
2759789Sahrens 
2760789Sahrens 		if (next == NULL)
2761789Sahrens 			len = strlen(dataset);
2762789Sahrens 		else
2763789Sahrens 			len = next - dataset;
2764789Sahrens 
2765789Sahrens 		zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP);
2766789Sahrens 		bcopy(dataset, zd->zd_dataset, len);
2767789Sahrens 		zd->zd_dataset[len] = '\0';
2768789Sahrens 
2769789Sahrens 		list_insert_head(&zone->zone_datasets, zd);
2770789Sahrens 
2771789Sahrens 		if (next == NULL)
2772789Sahrens 			break;
2773789Sahrens 
2774789Sahrens 		dataset = next + 1;
2775789Sahrens 	}
2776789Sahrens 
2777789Sahrens 	kmem_free(kbuf, buflen);
2778789Sahrens 	return (0);
2779789Sahrens }
2780789Sahrens 
2781789Sahrens /*
27820Sstevel@tonic-gate  * System call to create/initialize a new zone named 'zone_name', rooted
27830Sstevel@tonic-gate  * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs',
27841676Sjpk  * and initialized with the zone-wide rctls described in 'rctlbuf', and
27851676Sjpk  * with labeling set by 'match', 'doi', and 'label'.
27860Sstevel@tonic-gate  *
27870Sstevel@tonic-gate  * If extended error is non-null, we may use it to return more detailed
27880Sstevel@tonic-gate  * error information.
27890Sstevel@tonic-gate  */
27900Sstevel@tonic-gate static zoneid_t
27910Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root,
2792813Sdp     const priv_set_t *zone_privs, size_t zone_privssz,
2793813Sdp     caddr_t rctlbuf, size_t rctlbufsz,
27941676Sjpk     caddr_t zfsbuf, size_t zfsbufsz, int *extended_error,
27951676Sjpk     int match, uint32_t doi, const bslabel_t *label)
27960Sstevel@tonic-gate {
27970Sstevel@tonic-gate 	struct zsched_arg zarg;
27980Sstevel@tonic-gate 	nvlist_t *rctls = NULL;
27990Sstevel@tonic-gate 	proc_t *pp = curproc;
28000Sstevel@tonic-gate 	zone_t *zone, *ztmp;
28010Sstevel@tonic-gate 	zoneid_t zoneid;
28020Sstevel@tonic-gate 	int error;
28030Sstevel@tonic-gate 	int error2 = 0;
28040Sstevel@tonic-gate 	char *str;
28050Sstevel@tonic-gate 	cred_t *zkcr;
28061769Scarlsonj 	boolean_t insert_label_hash;
28070Sstevel@tonic-gate 
28080Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
28090Sstevel@tonic-gate 		return (set_errno(EPERM));
28100Sstevel@tonic-gate 
28110Sstevel@tonic-gate 	/* can't boot zone from within chroot environment */
28120Sstevel@tonic-gate 	if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir)
28130Sstevel@tonic-gate 		return (zone_create_error(ENOTSUP, ZE_CHROOTED,
2814813Sdp 		    extended_error));
28150Sstevel@tonic-gate 
28160Sstevel@tonic-gate 	zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP);
28170Sstevel@tonic-gate 	zoneid = zone->zone_id = id_alloc(zoneid_space);
28180Sstevel@tonic-gate 	zone->zone_status = ZONE_IS_UNINITIALIZED;
28190Sstevel@tonic-gate 	zone->zone_pool = pool_default;
28200Sstevel@tonic-gate 	zone->zone_pool_mod = gethrtime();
28210Sstevel@tonic-gate 	zone->zone_psetid = ZONE_PS_INVAL;
28220Sstevel@tonic-gate 	zone->zone_ncpus = 0;
28230Sstevel@tonic-gate 	zone->zone_ncpus_online = 0;
28240Sstevel@tonic-gate 	mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL);
28250Sstevel@tonic-gate 	mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
28260Sstevel@tonic-gate 	cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL);
28270Sstevel@tonic-gate 	list_create(&zone->zone_zsd, sizeof (struct zsd_entry),
28280Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
2829789Sahrens 	list_create(&zone->zone_datasets, sizeof (zone_dataset_t),
2830789Sahrens 	    offsetof(zone_dataset_t, zd_linkage));
28311676Sjpk 	rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
28320Sstevel@tonic-gate 
28330Sstevel@tonic-gate 	if ((error = zone_set_name(zone, zone_name)) != 0) {
28340Sstevel@tonic-gate 		zone_free(zone);
28350Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
28360Sstevel@tonic-gate 	}
28370Sstevel@tonic-gate 
28380Sstevel@tonic-gate 	if ((error = zone_set_root(zone, zone_root)) != 0) {
28390Sstevel@tonic-gate 		zone_free(zone);
28400Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
28410Sstevel@tonic-gate 	}
2842813Sdp 	if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) {
28430Sstevel@tonic-gate 		zone_free(zone);
28440Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
28450Sstevel@tonic-gate 	}
28460Sstevel@tonic-gate 
28470Sstevel@tonic-gate 	/* initialize node name to be the same as zone name */
28480Sstevel@tonic-gate 	zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP);
28490Sstevel@tonic-gate 	(void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN);
28500Sstevel@tonic-gate 	zone->zone_nodename[_SYS_NMLN - 1] = '\0';
28510Sstevel@tonic-gate 
28520Sstevel@tonic-gate 	zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP);
28530Sstevel@tonic-gate 	zone->zone_domain[0] = '\0';
28540Sstevel@tonic-gate 	zone->zone_shares = 1;
28550Sstevel@tonic-gate 	zone->zone_bootargs = NULL;
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate 	/*
28580Sstevel@tonic-gate 	 * Zsched initializes the rctls.
28590Sstevel@tonic-gate 	 */
28600Sstevel@tonic-gate 	zone->zone_rctls = NULL;
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate 	if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) {
28630Sstevel@tonic-gate 		zone_free(zone);
28640Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
28650Sstevel@tonic-gate 	}
28660Sstevel@tonic-gate 
2867789Sahrens 	if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) {
2868789Sahrens 		zone_free(zone);
2869789Sahrens 		return (set_errno(error));
2870789Sahrens 	}
2871789Sahrens 
28720Sstevel@tonic-gate 	/*
28731676Sjpk 	 * Read in the trusted system parameters:
28741676Sjpk 	 * match flag and sensitivity label.
28751676Sjpk 	 */
28761676Sjpk 	zone->zone_match = match;
28771769Scarlsonj 	if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) {
28781676Sjpk 		error = zone_set_label(zone, label, doi);
28791676Sjpk 		if (error != 0) {
28801676Sjpk 			zone_free(zone);
28811676Sjpk 			return (set_errno(error));
28821676Sjpk 		}
28831769Scarlsonj 		insert_label_hash = B_TRUE;
28841676Sjpk 	} else {
28851676Sjpk 		/* all zones get an admin_low label if system is not labeled */
28861676Sjpk 		zone->zone_slabel = l_admin_low;
28871676Sjpk 		label_hold(l_admin_low);
28881769Scarlsonj 		insert_label_hash = B_FALSE;
28891676Sjpk 	}
28901676Sjpk 
28911676Sjpk 	/*
28920Sstevel@tonic-gate 	 * Stop all lwps since that's what normally happens as part of fork().
28930Sstevel@tonic-gate 	 * This needs to happen before we grab any locks to avoid deadlock
28940Sstevel@tonic-gate 	 * (another lwp in the process could be waiting for the held lock).
28950Sstevel@tonic-gate 	 */
28960Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) {
28970Sstevel@tonic-gate 		zone_free(zone);
28980Sstevel@tonic-gate 		if (rctls)
28990Sstevel@tonic-gate 			nvlist_free(rctls);
29000Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
29010Sstevel@tonic-gate 	}
29020Sstevel@tonic-gate 
29030Sstevel@tonic-gate 	if (block_mounts() == 0) {
29040Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
29050Sstevel@tonic-gate 		if (curthread != pp->p_agenttp)
29060Sstevel@tonic-gate 			continuelwps(pp);
29070Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
29080Sstevel@tonic-gate 		zone_free(zone);
29090Sstevel@tonic-gate 		if (rctls)
29100Sstevel@tonic-gate 			nvlist_free(rctls);
29110Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
29120Sstevel@tonic-gate 	}
29130Sstevel@tonic-gate 
29140Sstevel@tonic-gate 	/*
29150Sstevel@tonic-gate 	 * Set up credential for kernel access.  After this, any errors
29160Sstevel@tonic-gate 	 * should go through the dance in errout rather than calling
29170Sstevel@tonic-gate 	 * zone_free directly.
29180Sstevel@tonic-gate 	 */
29190Sstevel@tonic-gate 	zone->zone_kcred = crdup(kcred);
29200Sstevel@tonic-gate 	crsetzone(zone->zone_kcred, zone);
29210Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred));
29220Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred));
29230Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred));
29240Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred));
29250Sstevel@tonic-gate 
29260Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
29270Sstevel@tonic-gate 	/*
29280Sstevel@tonic-gate 	 * Make sure zone doesn't already exist.
29291676Sjpk 	 *
29301676Sjpk 	 * If the system and zone are labeled,
29311676Sjpk 	 * make sure no other zone exists that has the same label.
29320Sstevel@tonic-gate 	 */
29331676Sjpk 	if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL ||
29341769Scarlsonj 	    (insert_label_hash &&
29351676Sjpk 	    (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) {
29360Sstevel@tonic-gate 		zone_status_t status;
29370Sstevel@tonic-gate 
29380Sstevel@tonic-gate 		status = zone_status_get(ztmp);
29390Sstevel@tonic-gate 		if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING)
29400Sstevel@tonic-gate 			error = EEXIST;
29410Sstevel@tonic-gate 		else
29420Sstevel@tonic-gate 			error = EBUSY;
29430Sstevel@tonic-gate 		goto errout;
29440Sstevel@tonic-gate 	}
29450Sstevel@tonic-gate 
29460Sstevel@tonic-gate 	/*
29470Sstevel@tonic-gate 	 * Don't allow zone creations which would cause one zone's rootpath to
29480Sstevel@tonic-gate 	 * be accessible from that of another (non-global) zone.
29490Sstevel@tonic-gate 	 */
29500Sstevel@tonic-gate 	if (zone_is_nested(zone->zone_rootpath)) {
29510Sstevel@tonic-gate 		error = EBUSY;
29520Sstevel@tonic-gate 		goto errout;
29530Sstevel@tonic-gate 	}
29540Sstevel@tonic-gate 
29550Sstevel@tonic-gate 	ASSERT(zonecount != 0);		/* check for leaks */
29560Sstevel@tonic-gate 	if (zonecount + 1 > maxzones) {
29570Sstevel@tonic-gate 		error = ENOMEM;
29580Sstevel@tonic-gate 		goto errout;
29590Sstevel@tonic-gate 	}
29600Sstevel@tonic-gate 
29610Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
29620Sstevel@tonic-gate 		error = EBUSY;
29630Sstevel@tonic-gate 		error2 = ZE_AREMOUNTS;
29640Sstevel@tonic-gate 		goto errout;
29650Sstevel@tonic-gate 	}
29660Sstevel@tonic-gate 
29670Sstevel@tonic-gate 	/*
29680Sstevel@tonic-gate 	 * Zone is still incomplete, but we need to drop all locks while
29690Sstevel@tonic-gate 	 * zsched() initializes this zone's kernel process.  We
29700Sstevel@tonic-gate 	 * optimistically add the zone to the hashtable and associated
29710Sstevel@tonic-gate 	 * lists so a parallel zone_create() doesn't try to create the
29720Sstevel@tonic-gate 	 * same zone.
29730Sstevel@tonic-gate 	 */
29740Sstevel@tonic-gate 	zonecount++;
29750Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid,
29760Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id,
29770Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
29780Sstevel@tonic-gate 	str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP);
29790Sstevel@tonic-gate 	(void) strcpy(str, zone->zone_name);
29800Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str,
29810Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
29821769Scarlsonj 	if (insert_label_hash) {
29831676Sjpk 		(void) mod_hash_insert(zonehashbylabel,
29841676Sjpk 		    (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone);
29851769Scarlsonj 		zone->zone_flags |= ZF_HASHED_LABEL;
29861676Sjpk 	}
29871676Sjpk 
29880Sstevel@tonic-gate 	/*
29890Sstevel@tonic-gate 	 * Insert into active list.  At this point there are no 'hold's
29900Sstevel@tonic-gate 	 * on the zone, but everyone else knows not to use it, so we can
29910Sstevel@tonic-gate 	 * continue to use it.  zsched() will do a zone_hold() if the
29920Sstevel@tonic-gate 	 * newproc() is successful.
29930Sstevel@tonic-gate 	 */
29940Sstevel@tonic-gate 	list_insert_tail(&zone_active, zone);
29950Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
29960Sstevel@tonic-gate 
29970Sstevel@tonic-gate 	zarg.zone = zone;
29980Sstevel@tonic-gate 	zarg.nvlist = rctls;
29990Sstevel@tonic-gate 	/*
30000Sstevel@tonic-gate 	 * The process, task, and project rctls are probably wrong;
30010Sstevel@tonic-gate 	 * we need an interface to get the default values of all rctls,
30020Sstevel@tonic-gate 	 * and initialize zsched appropriately.  I'm not sure that that
30030Sstevel@tonic-gate 	 * makes much of a difference, though.
30040Sstevel@tonic-gate 	 */
30050Sstevel@tonic-gate 	if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) {
30060Sstevel@tonic-gate 		/*
30070Sstevel@tonic-gate 		 * We need to undo all globally visible state.
30080Sstevel@tonic-gate 		 */
30090Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
30100Sstevel@tonic-gate 		list_remove(&zone_active, zone);
30111769Scarlsonj 		if (zone->zone_flags & ZF_HASHED_LABEL) {
30121676Sjpk 			ASSERT(zone->zone_slabel != NULL);
30131676Sjpk 			(void) mod_hash_destroy(zonehashbylabel,
30141676Sjpk 			    (mod_hash_key_t)zone->zone_slabel);
30151676Sjpk 		}
30160Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyname,
30170Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_name);
30180Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyid,
30190Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_id);
30200Sstevel@tonic-gate 		ASSERT(zonecount > 1);
30210Sstevel@tonic-gate 		zonecount--;
30220Sstevel@tonic-gate 		goto errout;
30230Sstevel@tonic-gate 	}
30240Sstevel@tonic-gate 
30250Sstevel@tonic-gate 	/*
30260Sstevel@tonic-gate 	 * Zone creation can't fail from now on.
30270Sstevel@tonic-gate 	 */
30280Sstevel@tonic-gate 
30290Sstevel@tonic-gate 	/*
30300Sstevel@tonic-gate 	 * Let the other lwps continue.
30310Sstevel@tonic-gate 	 */
30320Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
30330Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
30340Sstevel@tonic-gate 		continuelwps(pp);
30350Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
30360Sstevel@tonic-gate 
30370Sstevel@tonic-gate 	/*
30380Sstevel@tonic-gate 	 * Wait for zsched to finish initializing the zone.
30390Sstevel@tonic-gate 	 */
30400Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_READY);
30410Sstevel@tonic-gate 	/*
30420Sstevel@tonic-gate 	 * The zone is fully visible, so we can let mounts progress.
30430Sstevel@tonic-gate 	 */
30440Sstevel@tonic-gate 	resume_mounts();
30450Sstevel@tonic-gate 	if (rctls)
30460Sstevel@tonic-gate 		nvlist_free(rctls);
30470Sstevel@tonic-gate 
30480Sstevel@tonic-gate 	return (zoneid);
30490Sstevel@tonic-gate 
30500Sstevel@tonic-gate errout:
30510Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
30520Sstevel@tonic-gate 	/*
30530Sstevel@tonic-gate 	 * Let the other lwps continue.
30540Sstevel@tonic-gate 	 */
30550Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
30560Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
30570Sstevel@tonic-gate 		continuelwps(pp);
30580Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
30590Sstevel@tonic-gate 
30600Sstevel@tonic-gate 	resume_mounts();
30610Sstevel@tonic-gate 	if (rctls)
30620Sstevel@tonic-gate 		nvlist_free(rctls);
30630Sstevel@tonic-gate 	/*
30640Sstevel@tonic-gate 	 * There is currently one reference to the zone, a cred_ref from
30650Sstevel@tonic-gate 	 * zone_kcred.  To free the zone, we call crfree, which will call
30660Sstevel@tonic-gate 	 * zone_cred_rele, which will call zone_free.
30670Sstevel@tonic-gate 	 */
30680Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 1);	/* for zone_kcred */
30690Sstevel@tonic-gate 	ASSERT(zone->zone_kcred->cr_ref == 1);
30700Sstevel@tonic-gate 	ASSERT(zone->zone_ref == 0);
30710Sstevel@tonic-gate 	zkcr = zone->zone_kcred;
30720Sstevel@tonic-gate 	zone->zone_kcred = NULL;
30730Sstevel@tonic-gate 	crfree(zkcr);				/* triggers call to zone_free */
30740Sstevel@tonic-gate 	return (zone_create_error(error, error2, extended_error));
30750Sstevel@tonic-gate }
30760Sstevel@tonic-gate 
30770Sstevel@tonic-gate /*
30780Sstevel@tonic-gate  * Cause the zone to boot.  This is pretty simple, since we let zoneadmd do
30790Sstevel@tonic-gate  * the heavy lifting.
30800Sstevel@tonic-gate  */
30810Sstevel@tonic-gate static int
30820Sstevel@tonic-gate zone_boot(zoneid_t zoneid, const char *bootargs)
30830Sstevel@tonic-gate {
30840Sstevel@tonic-gate 	int err;
30850Sstevel@tonic-gate 	zone_t *zone;
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
30880Sstevel@tonic-gate 		return (set_errno(EPERM));
30890Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
30900Sstevel@tonic-gate 		return (set_errno(EINVAL));
30910Sstevel@tonic-gate 
30920Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
30930Sstevel@tonic-gate 	/*
30940Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with calls to
30950Sstevel@tonic-gate 	 * zone_shutdown, zone_destroy, etc.
30960Sstevel@tonic-gate 	 */
30970Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
30980Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
30990Sstevel@tonic-gate 		return (set_errno(EINVAL));
31000Sstevel@tonic-gate 	}
31010Sstevel@tonic-gate 
31020Sstevel@tonic-gate 	if ((err = zone_set_bootargs(zone, bootargs)) != 0) {
31030Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
31040Sstevel@tonic-gate 		return (set_errno(err));
31050Sstevel@tonic-gate 	}
31060Sstevel@tonic-gate 
31070Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
31080Sstevel@tonic-gate 	if (zone_status_get(zone) != ZONE_IS_READY) {
31090Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
31100Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
31110Sstevel@tonic-gate 		return (set_errno(EINVAL));
31120Sstevel@tonic-gate 	}
31130Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_BOOTING);
31140Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
31150Sstevel@tonic-gate 
31160Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
31170Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
31180Sstevel@tonic-gate 
31190Sstevel@tonic-gate 	if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) {
31200Sstevel@tonic-gate 		zone_rele(zone);
31210Sstevel@tonic-gate 		return (set_errno(EINTR));
31220Sstevel@tonic-gate 	}
31230Sstevel@tonic-gate 
31240Sstevel@tonic-gate 	/*
31250Sstevel@tonic-gate 	 * Boot (starting init) might have failed, in which case the zone
31260Sstevel@tonic-gate 	 * will go to the SHUTTING_DOWN state; an appropriate errno will
31270Sstevel@tonic-gate 	 * be placed in zone->zone_boot_err, and so we return that.
31280Sstevel@tonic-gate 	 */
31290Sstevel@tonic-gate 	err = zone->zone_boot_err;
31300Sstevel@tonic-gate 	zone_rele(zone);
31310Sstevel@tonic-gate 	return (err ? set_errno(err) : 0);
31320Sstevel@tonic-gate }
31330Sstevel@tonic-gate 
31340Sstevel@tonic-gate /*
31350Sstevel@tonic-gate  * Kills all user processes in the zone, waiting for them all to exit
31360Sstevel@tonic-gate  * before returning.
31370Sstevel@tonic-gate  */
31380Sstevel@tonic-gate static int
31390Sstevel@tonic-gate zone_empty(zone_t *zone)
31400Sstevel@tonic-gate {
31410Sstevel@tonic-gate 	int waitstatus;
31420Sstevel@tonic-gate 
31430Sstevel@tonic-gate 	/*
31440Sstevel@tonic-gate 	 * We need to drop zonehash_lock before killing all
31450Sstevel@tonic-gate 	 * processes, otherwise we'll deadlock with zone_find_*
31460Sstevel@tonic-gate 	 * which can be called from the exit path.
31470Sstevel@tonic-gate 	 */
31480Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
31490Sstevel@tonic-gate 	while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz,
31500Sstevel@tonic-gate 	    ZONE_IS_EMPTY)) == -1) {
31510Sstevel@tonic-gate 		killall(zone->zone_id);
31520Sstevel@tonic-gate 	}
31530Sstevel@tonic-gate 	/*
31540Sstevel@tonic-gate 	 * return EINTR if we were signaled
31550Sstevel@tonic-gate 	 */
31560Sstevel@tonic-gate 	if (waitstatus == 0)
31570Sstevel@tonic-gate 		return (EINTR);
31580Sstevel@tonic-gate 	return (0);
31590Sstevel@tonic-gate }
31600Sstevel@tonic-gate 
31610Sstevel@tonic-gate /*
31621676Sjpk  * This function implements the policy for zone visibility.
31631676Sjpk  *
31641676Sjpk  * In standard Solaris, a non-global zone can only see itself.
31651676Sjpk  *
31661676Sjpk  * In Trusted Extensions, a labeled zone can lookup any zone whose label
31671676Sjpk  * it dominates. For this test, the label of the global zone is treated as
31681676Sjpk  * admin_high so it is special-cased instead of being checked for dominance.
31691676Sjpk  *
31701676Sjpk  * Returns true if zone attributes are viewable, false otherwise.
31711676Sjpk  */
31721676Sjpk static boolean_t
31731676Sjpk zone_list_access(zone_t *zone)
31741676Sjpk {
31751676Sjpk 
31761676Sjpk 	if (curproc->p_zone == global_zone ||
31771676Sjpk 	    curproc->p_zone == zone) {
31781676Sjpk 		return (B_TRUE);
31791769Scarlsonj 	} else if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) {
31801676Sjpk 		bslabel_t *curproc_label;
31811676Sjpk 		bslabel_t *zone_label;
31821676Sjpk 
31831676Sjpk 		curproc_label = label2bslabel(curproc->p_zone->zone_slabel);
31841676Sjpk 		zone_label = label2bslabel(zone->zone_slabel);
31851676Sjpk 
31861676Sjpk 		if (zone->zone_id != GLOBAL_ZONEID &&
31871676Sjpk 		    bldominates(curproc_label, zone_label)) {
31881676Sjpk 			return (B_TRUE);
31891676Sjpk 		} else {
31901676Sjpk 			return (B_FALSE);
31911676Sjpk 		}
31921676Sjpk 	} else {
31931676Sjpk 		return (B_FALSE);
31941676Sjpk 	}
31951676Sjpk }
31961676Sjpk 
31971676Sjpk /*
31980Sstevel@tonic-gate  * Systemcall to start the zone's halt sequence.  By the time this
31990Sstevel@tonic-gate  * function successfully returns, all user processes and kernel threads
32000Sstevel@tonic-gate  * executing in it will have exited, ZSD shutdown callbacks executed,
32010Sstevel@tonic-gate  * and the zone status set to ZONE_IS_DOWN.
32020Sstevel@tonic-gate  *
32030Sstevel@tonic-gate  * It is possible that the call will interrupt itself if the caller is the
32040Sstevel@tonic-gate  * parent of any process running in the zone, and doesn't have SIGCHLD blocked.
32050Sstevel@tonic-gate  */
32060Sstevel@tonic-gate static int
32070Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid)
32080Sstevel@tonic-gate {
32090Sstevel@tonic-gate 	int error;
32100Sstevel@tonic-gate 	zone_t *zone;
32110Sstevel@tonic-gate 	zone_status_t status;
32120Sstevel@tonic-gate 
32130Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
32140Sstevel@tonic-gate 		return (set_errno(EPERM));
32150Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
32160Sstevel@tonic-gate 		return (set_errno(EINVAL));
32170Sstevel@tonic-gate 
32180Sstevel@tonic-gate 	/*
32190Sstevel@tonic-gate 	 * Block mounts so that VFS_MOUNT() can get an accurate view of
32200Sstevel@tonic-gate 	 * the zone's status with regards to ZONE_IS_SHUTTING down.
32210Sstevel@tonic-gate 	 *
32220Sstevel@tonic-gate 	 * e.g. NFS can fail the mount if it determines that the zone
32230Sstevel@tonic-gate 	 * has already begun the shutdown sequence.
32240Sstevel@tonic-gate 	 */
32250Sstevel@tonic-gate 	if (block_mounts() == 0)
32260Sstevel@tonic-gate 		return (set_errno(EINTR));
32270Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
32280Sstevel@tonic-gate 	/*
32290Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
32300Sstevel@tonic-gate 	 * calls to zone_shutdown and zone_destroy.
32310Sstevel@tonic-gate 	 */
32320Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
32330Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
32340Sstevel@tonic-gate 		resume_mounts();
32350Sstevel@tonic-gate 		return (set_errno(EINVAL));
32360Sstevel@tonic-gate 	}
32370Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
32380Sstevel@tonic-gate 	status = zone_status_get(zone);
32390Sstevel@tonic-gate 	/*
32400Sstevel@tonic-gate 	 * Fail if the zone isn't fully initialized yet.
32410Sstevel@tonic-gate 	 */
32420Sstevel@tonic-gate 	if (status < ZONE_IS_READY) {
32430Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
32440Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
32450Sstevel@tonic-gate 		resume_mounts();
32460Sstevel@tonic-gate 		return (set_errno(EINVAL));
32470Sstevel@tonic-gate 	}
32480Sstevel@tonic-gate 	/*
32490Sstevel@tonic-gate 	 * If conditions required for zone_shutdown() to return have been met,
32500Sstevel@tonic-gate 	 * return success.
32510Sstevel@tonic-gate 	 */
32520Sstevel@tonic-gate 	if (status >= ZONE_IS_DOWN) {
32530Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
32540Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
32550Sstevel@tonic-gate 		resume_mounts();
32560Sstevel@tonic-gate 		return (0);
32570Sstevel@tonic-gate 	}
32580Sstevel@tonic-gate 	/*
32590Sstevel@tonic-gate 	 * If zone_shutdown() hasn't been called before, go through the motions.
32600Sstevel@tonic-gate 	 * If it has, there's nothing to do but wait for the kernel threads to
32610Sstevel@tonic-gate 	 * drain.
32620Sstevel@tonic-gate 	 */
32630Sstevel@tonic-gate 	if (status < ZONE_IS_EMPTY) {
32640Sstevel@tonic-gate 		uint_t ntasks;
32650Sstevel@tonic-gate 
32660Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
32670Sstevel@tonic-gate 		if ((ntasks = zone->zone_ntasks) != 1) {
32680Sstevel@tonic-gate 			/*
32690Sstevel@tonic-gate 			 * There's still stuff running.
32700Sstevel@tonic-gate 			 */
32710Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
32720Sstevel@tonic-gate 		}
32730Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
32740Sstevel@tonic-gate 		if (ntasks == 1) {
32750Sstevel@tonic-gate 			/*
32760Sstevel@tonic-gate 			 * The only way to create another task is through
32770Sstevel@tonic-gate 			 * zone_enter(), which will block until we drop
32780Sstevel@tonic-gate 			 * zonehash_lock.  The zone is empty.
32790Sstevel@tonic-gate 			 */
32800Sstevel@tonic-gate 			if (zone->zone_kthreads == NULL) {
32810Sstevel@tonic-gate 				/*
32820Sstevel@tonic-gate 				 * Skip ahead to ZONE_IS_DOWN
32830Sstevel@tonic-gate 				 */
32840Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_DOWN);
32850Sstevel@tonic-gate 			} else {
32860Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_EMPTY);
32870Sstevel@tonic-gate 			}
32880Sstevel@tonic-gate 		}
32890Sstevel@tonic-gate 	}
32900Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
32910Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
32920Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
32930Sstevel@tonic-gate 	resume_mounts();
32940Sstevel@tonic-gate 
32950Sstevel@tonic-gate 	if (error = zone_empty(zone)) {
32960Sstevel@tonic-gate 		zone_rele(zone);
32970Sstevel@tonic-gate 		return (set_errno(error));
32980Sstevel@tonic-gate 	}
32990Sstevel@tonic-gate 	/*
33000Sstevel@tonic-gate 	 * After the zone status goes to ZONE_IS_DOWN this zone will no
33010Sstevel@tonic-gate 	 * longer be notified of changes to the pools configuration, so
33020Sstevel@tonic-gate 	 * in order to not end up with a stale pool pointer, we point
33030Sstevel@tonic-gate 	 * ourselves at the default pool and remove all resource
33040Sstevel@tonic-gate 	 * visibility.  This is especially important as the zone_t may
33050Sstevel@tonic-gate 	 * languish on the deathrow for a very long time waiting for
33060Sstevel@tonic-gate 	 * cred's to drain out.
33070Sstevel@tonic-gate 	 *
33080Sstevel@tonic-gate 	 * This rebinding of the zone can happen multiple times
33090Sstevel@tonic-gate 	 * (presumably due to interrupted or parallel systemcalls)
33100Sstevel@tonic-gate 	 * without any adverse effects.
33110Sstevel@tonic-gate 	 */
33120Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
33130Sstevel@tonic-gate 		zone_rele(zone);
33140Sstevel@tonic-gate 		return (set_errno(EINTR));
33150Sstevel@tonic-gate 	}
33160Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
33170Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
33180Sstevel@tonic-gate 		zone_pool_set(zone, pool_default);
33190Sstevel@tonic-gate 		/*
33200Sstevel@tonic-gate 		 * The zone no longer needs to be able to see any cpus.
33210Sstevel@tonic-gate 		 */
33220Sstevel@tonic-gate 		zone_pset_set(zone, ZONE_PS_INVAL);
33230Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
33240Sstevel@tonic-gate 	}
33250Sstevel@tonic-gate 	pool_unlock();
33260Sstevel@tonic-gate 
33270Sstevel@tonic-gate 	/*
33280Sstevel@tonic-gate 	 * ZSD shutdown callbacks can be executed multiple times, hence
33290Sstevel@tonic-gate 	 * it is safe to not be holding any locks across this call.
33300Sstevel@tonic-gate 	 */
33310Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_SHUTDOWN);
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
33340Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN)
33350Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DOWN);
33360Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
33370Sstevel@tonic-gate 
33380Sstevel@tonic-gate 	/*
33390Sstevel@tonic-gate 	 * Wait for kernel threads to drain.
33400Sstevel@tonic-gate 	 */
33410Sstevel@tonic-gate 	if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) {
33420Sstevel@tonic-gate 		zone_rele(zone);
33430Sstevel@tonic-gate 		return (set_errno(EINTR));
33440Sstevel@tonic-gate 	}
33450Sstevel@tonic-gate 	zone_rele(zone);
33460Sstevel@tonic-gate 	return (0);
33470Sstevel@tonic-gate }
33480Sstevel@tonic-gate 
33490Sstevel@tonic-gate /*
33500Sstevel@tonic-gate  * Systemcall entry point to finalize the zone halt process.  The caller
33510Sstevel@tonic-gate  * must have already successfully callefd zone_shutdown().
33520Sstevel@tonic-gate  *
33530Sstevel@tonic-gate  * Upon successful completion, the zone will have been fully destroyed:
33540Sstevel@tonic-gate  * zsched will have exited, destructor callbacks executed, and the zone
33550Sstevel@tonic-gate  * removed from the list of active zones.
33560Sstevel@tonic-gate  */
33570Sstevel@tonic-gate static int
33580Sstevel@tonic-gate zone_destroy(zoneid_t zoneid)
33590Sstevel@tonic-gate {
33600Sstevel@tonic-gate 	uint64_t uniqid;
33610Sstevel@tonic-gate 	zone_t *zone;
33620Sstevel@tonic-gate 	zone_status_t status;
33630Sstevel@tonic-gate 
33640Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
33650Sstevel@tonic-gate 		return (set_errno(EPERM));
33660Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
33670Sstevel@tonic-gate 		return (set_errno(EINVAL));
33680Sstevel@tonic-gate 
33690Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
33700Sstevel@tonic-gate 	/*
33710Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
33720Sstevel@tonic-gate 	 * calls to zone_destroy.
33730Sstevel@tonic-gate 	 */
33740Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
33750Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
33760Sstevel@tonic-gate 		return (set_errno(EINVAL));
33770Sstevel@tonic-gate 	}
33780Sstevel@tonic-gate 
33790Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
33800Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
33810Sstevel@tonic-gate 		return (set_errno(EBUSY));
33820Sstevel@tonic-gate 	}
33830Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
33840Sstevel@tonic-gate 	status = zone_status_get(zone);
33850Sstevel@tonic-gate 	if (status < ZONE_IS_DOWN) {
33860Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
33870Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
33880Sstevel@tonic-gate 		return (set_errno(EBUSY));
33890Sstevel@tonic-gate 	} else if (status == ZONE_IS_DOWN) {
33900Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */
33910Sstevel@tonic-gate 	}
33920Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
33930Sstevel@tonic-gate 	zone_hold(zone);
33940Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
33950Sstevel@tonic-gate 
33960Sstevel@tonic-gate 	/*
33970Sstevel@tonic-gate 	 * wait for zsched to exit
33980Sstevel@tonic-gate 	 */
33990Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_DEAD);
34000Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_DESTROY);
34010Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
34020Sstevel@tonic-gate 	zone_rele(zone);
34030Sstevel@tonic-gate 	zone = NULL;	/* potentially free'd */
34040Sstevel@tonic-gate 
34050Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
34060Sstevel@tonic-gate 	for (; /* ever */; ) {
34070Sstevel@tonic-gate 		boolean_t unref;
34080Sstevel@tonic-gate 
34090Sstevel@tonic-gate 		if ((zone = zone_find_all_by_id(zoneid)) == NULL ||
34100Sstevel@tonic-gate 		    zone->zone_uniqid != uniqid) {
34110Sstevel@tonic-gate 			/*
34120Sstevel@tonic-gate 			 * The zone has gone away.  Necessary conditions
34130Sstevel@tonic-gate 			 * are met, so we return success.
34140Sstevel@tonic-gate 			 */
34150Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
34160Sstevel@tonic-gate 			return (0);
34170Sstevel@tonic-gate 		}
34180Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
34190Sstevel@tonic-gate 		unref = ZONE_IS_UNREF(zone);
34200Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
34210Sstevel@tonic-gate 		if (unref) {
34220Sstevel@tonic-gate 			/*
34230Sstevel@tonic-gate 			 * There is only one reference to the zone -- that
34240Sstevel@tonic-gate 			 * added when the zone was added to the hashtables --
34250Sstevel@tonic-gate 			 * and things will remain this way until we drop
34260Sstevel@tonic-gate 			 * zonehash_lock... we can go ahead and cleanup the
34270Sstevel@tonic-gate 			 * zone.
34280Sstevel@tonic-gate 			 */
34290Sstevel@tonic-gate 			break;
34300Sstevel@tonic-gate 		}
34310Sstevel@tonic-gate 
34320Sstevel@tonic-gate 		if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) {
34330Sstevel@tonic-gate 			/* Signaled */
34340Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
34350Sstevel@tonic-gate 			return (set_errno(EINTR));
34360Sstevel@tonic-gate 		}
34370Sstevel@tonic-gate 
34380Sstevel@tonic-gate 	}
34390Sstevel@tonic-gate 
34400Sstevel@tonic-gate 	/*
34410Sstevel@tonic-gate 	 * It is now safe to let the zone be recreated; remove it from the
34420Sstevel@tonic-gate 	 * lists.  The memory will not be freed until the last cred
34430Sstevel@tonic-gate 	 * reference goes away.
34440Sstevel@tonic-gate 	 */
34450Sstevel@tonic-gate 	ASSERT(zonecount > 1);	/* must be > 1; can't destroy global zone */
34460Sstevel@tonic-gate 	zonecount--;
34470Sstevel@tonic-gate 	/* remove from active list and hash tables */
34480Sstevel@tonic-gate 	list_remove(&zone_active, zone);
34490Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyname,
34500Sstevel@tonic-gate 	    (mod_hash_key_t)zone->zone_name);
34510Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyid,
34520Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id);
34531769Scarlsonj 	if (zone->zone_flags & ZF_HASHED_LABEL)
34541676Sjpk 		(void) mod_hash_destroy(zonehashbylabel,
34551676Sjpk 		    (mod_hash_key_t)zone->zone_slabel);
34560Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
34570Sstevel@tonic-gate 
3458766Scarlsonj 	/*
3459766Scarlsonj 	 * Release the root vnode; we're not using it anymore.  Nor should any
3460766Scarlsonj 	 * other thread that might access it exist.
3461766Scarlsonj 	 */
3462766Scarlsonj 	if (zone->zone_rootvp != NULL) {
3463766Scarlsonj 		VN_RELE(zone->zone_rootvp);
3464766Scarlsonj 		zone->zone_rootvp = NULL;
3465766Scarlsonj 	}
3466766Scarlsonj 
34670Sstevel@tonic-gate 	/* add to deathrow list */
34680Sstevel@tonic-gate 	mutex_enter(&zone_deathrow_lock);
34690Sstevel@tonic-gate 	list_insert_tail(&zone_deathrow, zone);
34700Sstevel@tonic-gate 	mutex_exit(&zone_deathrow_lock);
34710Sstevel@tonic-gate 
34720Sstevel@tonic-gate 	/*
34730Sstevel@tonic-gate 	 * Drop last reference (which was added by zsched()), this will
34740Sstevel@tonic-gate 	 * free the zone unless there are outstanding cred references.
34750Sstevel@tonic-gate 	 */
34760Sstevel@tonic-gate 	zone_rele(zone);
34770Sstevel@tonic-gate 	return (0);
34780Sstevel@tonic-gate }
34790Sstevel@tonic-gate 
34800Sstevel@tonic-gate /*
34810Sstevel@tonic-gate  * Systemcall entry point for zone_getattr(2).
34820Sstevel@tonic-gate  */
34830Sstevel@tonic-gate static ssize_t
34840Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
34850Sstevel@tonic-gate {
34860Sstevel@tonic-gate 	size_t size;
34870Sstevel@tonic-gate 	int error = 0, err;
34880Sstevel@tonic-gate 	zone_t *zone;
34890Sstevel@tonic-gate 	char *zonepath;
34900Sstevel@tonic-gate 	zone_status_t zone_status;
34910Sstevel@tonic-gate 	pid_t initpid;
34920Sstevel@tonic-gate 	boolean_t global = (curproc->p_zone == global_zone);
34931676Sjpk 	boolean_t curzone = (curproc->p_zone->zone_id == zoneid);
34940Sstevel@tonic-gate 
34950Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
34960Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
34970Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
34980Sstevel@tonic-gate 		return (set_errno(EINVAL));
34990Sstevel@tonic-gate 	}
35000Sstevel@tonic-gate 	zone_status = zone_status_get(zone);
35010Sstevel@tonic-gate 	if (zone_status < ZONE_IS_READY) {
35020Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
35030Sstevel@tonic-gate 		return (set_errno(EINVAL));
35040Sstevel@tonic-gate 	}
35050Sstevel@tonic-gate 	zone_hold(zone);
35060Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
35070Sstevel@tonic-gate 
35080Sstevel@tonic-gate 	/*
35091676Sjpk 	 * If not in the global zone, don't show information about other zones,
35101676Sjpk 	 * unless the system is labeled and the local zone's label dominates
35111676Sjpk 	 * the other zone.
35120Sstevel@tonic-gate 	 */
35131676Sjpk 	if (!zone_list_access(zone)) {
35140Sstevel@tonic-gate 		zone_rele(zone);
35150Sstevel@tonic-gate 		return (set_errno(EINVAL));
35160Sstevel@tonic-gate 	}
35170Sstevel@tonic-gate 
35180Sstevel@tonic-gate 	switch (attr) {
35190Sstevel@tonic-gate 	case ZONE_ATTR_ROOT:
35200Sstevel@tonic-gate 		if (global) {
35210Sstevel@tonic-gate 			/*
35220Sstevel@tonic-gate 			 * Copy the path to trim the trailing "/" (except for
35230Sstevel@tonic-gate 			 * the global zone).
35240Sstevel@tonic-gate 			 */
35250Sstevel@tonic-gate 			if (zone != global_zone)
35260Sstevel@tonic-gate 				size = zone->zone_rootpathlen - 1;
35270Sstevel@tonic-gate 			else
35280Sstevel@tonic-gate 				size = zone->zone_rootpathlen;
35290Sstevel@tonic-gate 			zonepath = kmem_alloc(size, KM_SLEEP);
35300Sstevel@tonic-gate 			bcopy(zone->zone_rootpath, zonepath, size);
35310Sstevel@tonic-gate 			zonepath[size - 1] = '\0';
35320Sstevel@tonic-gate 		} else {
35331676Sjpk 			if (curzone || !is_system_labeled()) {
35341676Sjpk 				/*
35351676Sjpk 				 * Caller is not in the global zone.
35361676Sjpk 				 * if the query is on the current zone
35371676Sjpk 				 * or the system is not labeled,
35381676Sjpk 				 * just return faked-up path for current zone.
35391676Sjpk 				 */
35401676Sjpk 				zonepath = "/";
35411676Sjpk 				size = 2;
35421676Sjpk 			} else {
35431676Sjpk 				/*
35441676Sjpk 				 * Return related path for current zone.
35451676Sjpk 				 */
35461676Sjpk 				int prefix_len = strlen(zone_prefix);
35471676Sjpk 				int zname_len = strlen(zone->zone_name);
35481676Sjpk 
35491676Sjpk 				size = prefix_len + zname_len + 1;
35501676Sjpk 				zonepath = kmem_alloc(size, KM_SLEEP);
35511676Sjpk 				bcopy(zone_prefix, zonepath, prefix_len);
35521676Sjpk 				bcopy(zone->zone_name, zonepath +
35531676Sjpk 					prefix_len, zname_len);
35541676Sjpk 				zonepath[size - 1] = '\0';
35551676Sjpk 			}
35560Sstevel@tonic-gate 		}
35570Sstevel@tonic-gate 		if (bufsize > size)
35580Sstevel@tonic-gate 			bufsize = size;
35590Sstevel@tonic-gate 		if (buf != NULL) {
35600Sstevel@tonic-gate 			err = copyoutstr(zonepath, buf, bufsize, NULL);
35610Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
35620Sstevel@tonic-gate 				error = EFAULT;
35630Sstevel@tonic-gate 		}
35641676Sjpk 		if (global || (is_system_labeled() && !curzone))
35650Sstevel@tonic-gate 			kmem_free(zonepath, size);
35660Sstevel@tonic-gate 		break;
35670Sstevel@tonic-gate 
35680Sstevel@tonic-gate 	case ZONE_ATTR_NAME:
35690Sstevel@tonic-gate 		size = strlen(zone->zone_name) + 1;
35700Sstevel@tonic-gate 		if (bufsize > size)
35710Sstevel@tonic-gate 			bufsize = size;
35720Sstevel@tonic-gate 		if (buf != NULL) {
35730Sstevel@tonic-gate 			err = copyoutstr(zone->zone_name, buf, bufsize, NULL);
35740Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
35750Sstevel@tonic-gate 				error = EFAULT;
35760Sstevel@tonic-gate 		}
35770Sstevel@tonic-gate 		break;
35780Sstevel@tonic-gate 
35790Sstevel@tonic-gate 	case ZONE_ATTR_STATUS:
35800Sstevel@tonic-gate 		/*
35810Sstevel@tonic-gate 		 * Since we're not holding zonehash_lock, the zone status
35820Sstevel@tonic-gate 		 * may be anything; leave it up to userland to sort it out.
35830Sstevel@tonic-gate 		 */
35840Sstevel@tonic-gate 		size = sizeof (zone_status);
35850Sstevel@tonic-gate 		if (bufsize > size)
35860Sstevel@tonic-gate 			bufsize = size;
35870Sstevel@tonic-gate 		zone_status = zone_status_get(zone);
35880Sstevel@tonic-gate 		if (buf != NULL &&
35890Sstevel@tonic-gate 		    copyout(&zone_status, buf, bufsize) != 0)
35900Sstevel@tonic-gate 			error = EFAULT;
35910Sstevel@tonic-gate 		break;
35920Sstevel@tonic-gate 	case ZONE_ATTR_PRIVSET:
35930Sstevel@tonic-gate 		size = sizeof (priv_set_t);
35940Sstevel@tonic-gate 		if (bufsize > size)
35950Sstevel@tonic-gate 			bufsize = size;
35960Sstevel@tonic-gate 		if (buf != NULL &&
35970Sstevel@tonic-gate 		    copyout(zone->zone_privset, buf, bufsize) != 0)
35980Sstevel@tonic-gate 			error = EFAULT;
35990Sstevel@tonic-gate 		break;
36000Sstevel@tonic-gate 	case ZONE_ATTR_UNIQID:
36010Sstevel@tonic-gate 		size = sizeof (zone->zone_uniqid);
36020Sstevel@tonic-gate 		if (bufsize > size)
36030Sstevel@tonic-gate 			bufsize = size;
36040Sstevel@tonic-gate 		if (buf != NULL &&
36050Sstevel@tonic-gate 		    copyout(&zone->zone_uniqid, buf, bufsize) != 0)
36060Sstevel@tonic-gate 			error = EFAULT;
36070Sstevel@tonic-gate 		break;
36080Sstevel@tonic-gate 	case ZONE_ATTR_POOLID:
36090Sstevel@tonic-gate 		{
36100Sstevel@tonic-gate 			pool_t *pool;
36110Sstevel@tonic-gate 			poolid_t poolid;
36120Sstevel@tonic-gate 
36130Sstevel@tonic-gate 			if (pool_lock_intr() != 0) {
36140Sstevel@tonic-gate 				error = EINTR;
36150Sstevel@tonic-gate 				break;
36160Sstevel@tonic-gate 			}
36170Sstevel@tonic-gate 			pool = zone_pool_get(zone);
36180Sstevel@tonic-gate 			poolid = pool->pool_id;
36190Sstevel@tonic-gate 			pool_unlock();
36200Sstevel@tonic-gate 			size = sizeof (poolid);
36210Sstevel@tonic-gate 			if (bufsize > size)
36220Sstevel@tonic-gate 				bufsize = size;
36230Sstevel@tonic-gate 			if (buf != NULL && copyout(&poolid, buf, size) != 0)
36240Sstevel@tonic-gate 				error = EFAULT;
36250Sstevel@tonic-gate 		}
36260Sstevel@tonic-gate 		break;
36271676Sjpk 	case ZONE_ATTR_SLBL:
36281676Sjpk 		size = sizeof (bslabel_t);
36291676Sjpk 		if (bufsize > size)
36301676Sjpk 			bufsize = size;
36311676Sjpk 		if (zone->zone_slabel == NULL)
36321676Sjpk 			error = EINVAL;
36331676Sjpk 		else if (buf != NULL &&
36341676Sjpk 		    copyout(label2bslabel(zone->zone_slabel), buf,
36351676Sjpk 		    bufsize) != 0)
36361676Sjpk 			error = EFAULT;
36371676Sjpk 		break;
36380Sstevel@tonic-gate 	case ZONE_ATTR_INITPID:
36390Sstevel@tonic-gate 		size = sizeof (initpid);
36400Sstevel@tonic-gate 		if (bufsize > size)
36410Sstevel@tonic-gate 			bufsize = size;
36420Sstevel@tonic-gate 		initpid = zone->zone_proc_initpid;
36430Sstevel@tonic-gate 		if (initpid == -1) {
36440Sstevel@tonic-gate 			error = ESRCH;
36450Sstevel@tonic-gate 			break;
36460Sstevel@tonic-gate 		}
36470Sstevel@tonic-gate 		if (buf != NULL &&
36480Sstevel@tonic-gate 		    copyout(&initpid, buf, bufsize) != 0)
36490Sstevel@tonic-gate 			error = EFAULT;
36500Sstevel@tonic-gate 		break;
36510Sstevel@tonic-gate 	default:
36520Sstevel@tonic-gate 		error = EINVAL;
36530Sstevel@tonic-gate 	}
36540Sstevel@tonic-gate 	zone_rele(zone);
36550Sstevel@tonic-gate 
36560Sstevel@tonic-gate 	if (error)
36570Sstevel@tonic-gate 		return (set_errno(error));
36580Sstevel@tonic-gate 	return ((ssize_t)size);
36590Sstevel@tonic-gate }
36600Sstevel@tonic-gate 
36610Sstevel@tonic-gate /*
36620Sstevel@tonic-gate  * Return zero if the process has at least one vnode mapped in to its
36630Sstevel@tonic-gate  * address space which shouldn't be allowed to change zones.
36640Sstevel@tonic-gate  */
36650Sstevel@tonic-gate static int
36660Sstevel@tonic-gate as_can_change_zones(void)
36670Sstevel@tonic-gate {
36680Sstevel@tonic-gate 	proc_t *pp = curproc;
36690Sstevel@tonic-gate 	struct seg *seg;
36700Sstevel@tonic-gate 	struct as *as = pp->p_as;
36710Sstevel@tonic-gate 	vnode_t *vp;
36720Sstevel@tonic-gate 	int allow = 1;
36730Sstevel@tonic-gate 
36740Sstevel@tonic-gate 	ASSERT(pp->p_as != &kas);
36750Sstevel@tonic-gate 	AS_LOCK_ENTER(&as, &as->a_lock, RW_READER);
36760Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
36770Sstevel@tonic-gate 		/*
36780Sstevel@tonic-gate 		 * if we can't get a backing vnode for this segment then skip
36790Sstevel@tonic-gate 		 * it.
36800Sstevel@tonic-gate 		 */
36810Sstevel@tonic-gate 		vp = NULL;
36820Sstevel@tonic-gate 		if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL)
36830Sstevel@tonic-gate 			continue;
36840Sstevel@tonic-gate 		if (!vn_can_change_zones(vp)) { /* bail on first match */
36850Sstevel@tonic-gate 			allow = 0;
36860Sstevel@tonic-gate 			break;
36870Sstevel@tonic-gate 		}
36880Sstevel@tonic-gate 	}
36890Sstevel@tonic-gate 	AS_LOCK_EXIT(&as, &as->a_lock);
36900Sstevel@tonic-gate 	return (allow);
36910Sstevel@tonic-gate }
36920Sstevel@tonic-gate 
36930Sstevel@tonic-gate /*
36940Sstevel@tonic-gate  * Systemcall entry point for zone_enter().
36950Sstevel@tonic-gate  *
36960Sstevel@tonic-gate  * The current process is injected into said zone.  In the process
36970Sstevel@tonic-gate  * it will change its project membership, privileges, rootdir/cwd,
36980Sstevel@tonic-gate  * zone-wide rctls, and pool association to match those of the zone.
36990Sstevel@tonic-gate  *
37000Sstevel@tonic-gate  * The first zone_enter() called while the zone is in the ZONE_IS_READY
37010Sstevel@tonic-gate  * state will transition it to ZONE_IS_RUNNING.  Processes may only
37020Sstevel@tonic-gate  * enter a zone that is "ready" or "running".
37030Sstevel@tonic-gate  */
37040Sstevel@tonic-gate static int
37050Sstevel@tonic-gate zone_enter(zoneid_t zoneid)
37060Sstevel@tonic-gate {
37070Sstevel@tonic-gate 	zone_t *zone;
37080Sstevel@tonic-gate 	vnode_t *vp;
37090Sstevel@tonic-gate 	proc_t *pp = curproc;
37100Sstevel@tonic-gate 	contract_t *ct;
37110Sstevel@tonic-gate 	cont_process_t *ctp;
37120Sstevel@tonic-gate 	task_t *tk, *oldtk;
37130Sstevel@tonic-gate 	kproject_t *zone_proj0;
37140Sstevel@tonic-gate 	cred_t *cr, *newcr;
37150Sstevel@tonic-gate 	pool_t *oldpool, *newpool;
37160Sstevel@tonic-gate 	sess_t *sp;
37170Sstevel@tonic-gate 	uid_t uid;
37180Sstevel@tonic-gate 	zone_status_t status;
37190Sstevel@tonic-gate 	int err = 0;
37200Sstevel@tonic-gate 	rctl_entity_p_t e;
37210Sstevel@tonic-gate 
37220Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
37230Sstevel@tonic-gate 		return (set_errno(EPERM));
37240Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
37250Sstevel@tonic-gate 		return (set_errno(EINVAL));
37260Sstevel@tonic-gate 
37270Sstevel@tonic-gate 	/*
37280Sstevel@tonic-gate 	 * Stop all lwps so we don't need to hold a lock to look at
37290Sstevel@tonic-gate 	 * curproc->p_zone.  This needs to happen before we grab any
37300Sstevel@tonic-gate 	 * locks to avoid deadlock (another lwp in the process could
37310Sstevel@tonic-gate 	 * be waiting for the held lock).
37320Sstevel@tonic-gate 	 */
37330Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK))
37340Sstevel@tonic-gate 		return (set_errno(EINTR));
37350Sstevel@tonic-gate 
37360Sstevel@tonic-gate 	/*
37370Sstevel@tonic-gate 	 * Make sure we're not changing zones with files open or mapped in
37380Sstevel@tonic-gate 	 * to our address space which shouldn't be changing zones.
37390Sstevel@tonic-gate 	 */
37400Sstevel@tonic-gate 	if (!files_can_change_zones()) {
37410Sstevel@tonic-gate 		err = EBADF;
37420Sstevel@tonic-gate 		goto out;
37430Sstevel@tonic-gate 	}
37440Sstevel@tonic-gate 	if (!as_can_change_zones()) {
37450Sstevel@tonic-gate 		err = EFAULT;
37460Sstevel@tonic-gate 		goto out;
37470Sstevel@tonic-gate 	}
37480Sstevel@tonic-gate 
37490Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
37500Sstevel@tonic-gate 	if (pp->p_zone != global_zone) {
37510Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
37520Sstevel@tonic-gate 		err = EINVAL;
37530Sstevel@tonic-gate 		goto out;
37540Sstevel@tonic-gate 	}
37550Sstevel@tonic-gate 
37560Sstevel@tonic-gate 	zone = zone_find_all_by_id(zoneid);
37570Sstevel@tonic-gate 	if (zone == NULL) {
37580Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
37590Sstevel@tonic-gate 		err = EINVAL;
37600Sstevel@tonic-gate 		goto out;
37610Sstevel@tonic-gate 	}
37620Sstevel@tonic-gate 
37630Sstevel@tonic-gate 	/*
37640Sstevel@tonic-gate 	 * To prevent processes in a zone from holding contracts on
37650Sstevel@tonic-gate 	 * extrazonal resources, and to avoid process contract
37660Sstevel@tonic-gate 	 * memberships which span zones, contract holders and processes
37670Sstevel@tonic-gate 	 * which aren't the sole members of their encapsulating process
37680Sstevel@tonic-gate 	 * contracts are not allowed to zone_enter.
37690Sstevel@tonic-gate 	 */
37700Sstevel@tonic-gate 	ctp = pp->p_ct_process;
37710Sstevel@tonic-gate 	ct = &ctp->conp_contract;
37720Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
37730Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
37740Sstevel@tonic-gate 	if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) {
37750Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
37760Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
37770Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
37780Sstevel@tonic-gate 		pool_unlock();
37790Sstevel@tonic-gate 		err = EINVAL;
37800Sstevel@tonic-gate 		goto out;
37810Sstevel@tonic-gate 	}
37820Sstevel@tonic-gate 
37830Sstevel@tonic-gate 	/*
37840Sstevel@tonic-gate 	 * Moreover, we don't allow processes whose encapsulating
37850Sstevel@tonic-gate 	 * process contracts have inherited extrazonal contracts.
37860Sstevel@tonic-gate 	 * While it would be easier to eliminate all process contracts
37870Sstevel@tonic-gate 	 * with inherited contracts, we need to be able to give a
37880Sstevel@tonic-gate 	 * restarted init (or other zone-penetrating process) its
37890Sstevel@tonic-gate 	 * predecessor's contracts.
37900Sstevel@tonic-gate 	 */
37910Sstevel@tonic-gate 	if (ctp->conp_ninherited != 0) {
37920Sstevel@tonic-gate 		contract_t *next;
37930Sstevel@tonic-gate 		for (next = list_head(&ctp->conp_inherited); next;
37940Sstevel@tonic-gate 		    next = list_next(&ctp->conp_inherited, next)) {
37950Sstevel@tonic-gate 			if (contract_getzuniqid(next) != zone->zone_uniqid) {
37960Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
37970Sstevel@tonic-gate 				mutex_exit(&ct->ct_lock);
37980Sstevel@tonic-gate 				mutex_exit(&zonehash_lock);
37990Sstevel@tonic-gate 				pool_unlock();
38000Sstevel@tonic-gate 				err = EINVAL;
38010Sstevel@tonic-gate 				goto out;
38020Sstevel@tonic-gate 			}
38030Sstevel@tonic-gate 		}
38040Sstevel@tonic-gate 	}
38050Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
38060Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
38070Sstevel@tonic-gate 
38080Sstevel@tonic-gate 	status = zone_status_get(zone);
38090Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) {
38100Sstevel@tonic-gate 		/*
38110Sstevel@tonic-gate 		 * Can't join
38120Sstevel@tonic-gate 		 */
38130Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
38140Sstevel@tonic-gate 		err = EINVAL;
38150Sstevel@tonic-gate 		goto out;
38160Sstevel@tonic-gate 	}
38170Sstevel@tonic-gate 
38180Sstevel@tonic-gate 	/*
38190Sstevel@tonic-gate 	 * Make sure new priv set is within the permitted set for caller
38200Sstevel@tonic-gate 	 */
38210Sstevel@tonic-gate 	if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) {
38220Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
38230Sstevel@tonic-gate 		err = EPERM;
38240Sstevel@tonic-gate 		goto out;
38250Sstevel@tonic-gate 	}
38260Sstevel@tonic-gate 	/*
38270Sstevel@tonic-gate 	 * We want to momentarily drop zonehash_lock while we optimistically
38280Sstevel@tonic-gate 	 * bind curproc to the pool it should be running in.  This is safe
38290Sstevel@tonic-gate 	 * since the zone can't disappear (we have a hold on it).
38300Sstevel@tonic-gate 	 */
38310Sstevel@tonic-gate 	zone_hold(zone);
38320Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
38330Sstevel@tonic-gate 
38340Sstevel@tonic-gate 	/*
38350Sstevel@tonic-gate 	 * Grab pool_lock to keep the pools configuration from changing
38360Sstevel@tonic-gate 	 * and to stop ourselves from getting rebound to another pool
38370Sstevel@tonic-gate 	 * until we join the zone.
38380Sstevel@tonic-gate 	 */
38390Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
38400Sstevel@tonic-gate 		zone_rele(zone);
38410Sstevel@tonic-gate 		err = EINTR;
38420Sstevel@tonic-gate 		goto out;
38430Sstevel@tonic-gate 	}
38440Sstevel@tonic-gate 	ASSERT(secpolicy_pool(CRED()) == 0);
38450Sstevel@tonic-gate 	/*
38460Sstevel@tonic-gate 	 * Bind ourselves to the pool currently associated with the zone.
38470Sstevel@tonic-gate 	 */
38480Sstevel@tonic-gate 	oldpool = curproc->p_pool;
38490Sstevel@tonic-gate 	newpool = zone_pool_get(zone);
38500Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED && newpool != oldpool &&
38510Sstevel@tonic-gate 	    (err = pool_do_bind(newpool, P_PID, P_MYID,
38520Sstevel@tonic-gate 	    POOL_BIND_ALL)) != 0) {
38530Sstevel@tonic-gate 		pool_unlock();
38540Sstevel@tonic-gate 		zone_rele(zone);
38550Sstevel@tonic-gate 		goto out;
38560Sstevel@tonic-gate 	}
38570Sstevel@tonic-gate 
38580Sstevel@tonic-gate 	/*
38590Sstevel@tonic-gate 	 * Grab cpu_lock now; we'll need it later when we call
38600Sstevel@tonic-gate 	 * task_join().
38610Sstevel@tonic-gate 	 */
38620Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
38630Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
38640Sstevel@tonic-gate 	/*
38650Sstevel@tonic-gate 	 * Make sure the zone hasn't moved on since we dropped zonehash_lock.
38660Sstevel@tonic-gate 	 */
38670Sstevel@tonic-gate 	if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
38680Sstevel@tonic-gate 		/*
38690Sstevel@tonic-gate 		 * Can't join anymore.
38700Sstevel@tonic-gate 		 */
38710Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
38720Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
38730Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED &&
38740Sstevel@tonic-gate 		    newpool != oldpool)
38750Sstevel@tonic-gate 			(void) pool_do_bind(oldpool, P_PID, P_MYID,
38760Sstevel@tonic-gate 			    POOL_BIND_ALL);
38770Sstevel@tonic-gate 		pool_unlock();
38780Sstevel@tonic-gate 		zone_rele(zone);
38790Sstevel@tonic-gate 		err = EINVAL;
38800Sstevel@tonic-gate 		goto out;
38810Sstevel@tonic-gate 	}
38820Sstevel@tonic-gate 
38830Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
38840Sstevel@tonic-gate 	zone_proj0 = zone->zone_zsched->p_task->tk_proj;
38850Sstevel@tonic-gate 	/* verify that we do not exceed and task or lwp limits */
38860Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
38870Sstevel@tonic-gate 	/* add new lwps to zone and zone's proj0 */
38880Sstevel@tonic-gate 	zone_proj0->kpj_nlwps += pp->p_lwpcnt;
38890Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
38900Sstevel@tonic-gate 	/* add 1 task to zone's proj0 */
38910Sstevel@tonic-gate 	zone_proj0->kpj_ntasks += 1;
38920Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
38930Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
38940Sstevel@tonic-gate 
38950Sstevel@tonic-gate 	/* remove lwps from proc's old zone and old project */
38960Sstevel@tonic-gate 	mutex_enter(&pp->p_zone->zone_nlwps_lock);
38970Sstevel@tonic-gate 	pp->p_zone->zone_nlwps -= pp->p_lwpcnt;
38980Sstevel@tonic-gate 	pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt;
38990Sstevel@tonic-gate 	mutex_exit(&pp->p_zone->zone_nlwps_lock);
39000Sstevel@tonic-gate 
39010Sstevel@tonic-gate 	/*
39020Sstevel@tonic-gate 	 * Joining the zone cannot fail from now on.
39030Sstevel@tonic-gate 	 *
39040Sstevel@tonic-gate 	 * This means that a lot of the following code can be commonized and
39050Sstevel@tonic-gate 	 * shared with zsched().
39060Sstevel@tonic-gate 	 */
39070Sstevel@tonic-gate 
39080Sstevel@tonic-gate 	/*
39090Sstevel@tonic-gate 	 * Reset the encapsulating process contract's zone.
39100Sstevel@tonic-gate 	 */
39110Sstevel@tonic-gate 	ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID);
39120Sstevel@tonic-gate 	contract_setzuniqid(ct, zone->zone_uniqid);
39130Sstevel@tonic-gate 
39140Sstevel@tonic-gate 	/*
39150Sstevel@tonic-gate 	 * Create a new task and associate the process with the project keyed
39160Sstevel@tonic-gate 	 * by (projid,zoneid).
39170Sstevel@tonic-gate 	 *
39180Sstevel@tonic-gate 	 * We might as well be in project 0; the global zone's projid doesn't
39190Sstevel@tonic-gate 	 * make much sense in a zone anyhow.
39200Sstevel@tonic-gate 	 *
39210Sstevel@tonic-gate 	 * This also increments zone_ntasks, and returns with p_lock held.
39220Sstevel@tonic-gate 	 */
39230Sstevel@tonic-gate 	tk = task_create(0, zone);
39240Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
39250Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
39260Sstevel@tonic-gate 
39270Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
39280Sstevel@tonic-gate 	pp->p_zone = zone;
39290Sstevel@tonic-gate 
39300Sstevel@tonic-gate 	/*
39310Sstevel@tonic-gate 	 * call RCTLOP_SET functions on this proc
39320Sstevel@tonic-gate 	 */
39330Sstevel@tonic-gate 	e.rcep_p.zone = zone;
39340Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
39350Sstevel@tonic-gate 	(void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL,
39360Sstevel@tonic-gate 	    RCD_CALLBACK);
39370Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
39380Sstevel@tonic-gate 
39390Sstevel@tonic-gate 	/*
39400Sstevel@tonic-gate 	 * We don't need to hold any of zsched's locks here; not only do we know
39410Sstevel@tonic-gate 	 * the process and zone aren't going away, we know its session isn't
39420Sstevel@tonic-gate 	 * changing either.
39430Sstevel@tonic-gate 	 *
39440Sstevel@tonic-gate 	 * By joining zsched's session here, we mimic the behavior in the
39450Sstevel@tonic-gate 	 * global zone of init's sid being the pid of sched.  We extend this
39460Sstevel@tonic-gate 	 * to all zlogin-like zone_enter()'ing processes as well.
39470Sstevel@tonic-gate 	 */
39480Sstevel@tonic-gate 	mutex_enter(&pidlock);
39490Sstevel@tonic-gate 	sp = zone->zone_zsched->p_sessp;
39500Sstevel@tonic-gate 	SESS_HOLD(sp);
39510Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
39520Sstevel@tonic-gate 	pgexit(pp);
39530Sstevel@tonic-gate 	SESS_RELE(pp->p_sessp);
39540Sstevel@tonic-gate 	pp->p_sessp = sp;
39550Sstevel@tonic-gate 	pgjoin(pp, zone->zone_zsched->p_pidp);
39560Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
39570Sstevel@tonic-gate 	mutex_exit(&pidlock);
39580Sstevel@tonic-gate 
39590Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
39600Sstevel@tonic-gate 	/*
39610Sstevel@tonic-gate 	 * We're firmly in the zone; let pools progress.
39620Sstevel@tonic-gate 	 */
39630Sstevel@tonic-gate 	pool_unlock();
39640Sstevel@tonic-gate 	task_rele(oldtk);
39650Sstevel@tonic-gate 	/*
39660Sstevel@tonic-gate 	 * We don't need to retain a hold on the zone since we already
39670Sstevel@tonic-gate 	 * incremented zone_ntasks, so the zone isn't going anywhere.
39680Sstevel@tonic-gate 	 */
39690Sstevel@tonic-gate 	zone_rele(zone);
39700Sstevel@tonic-gate 
39710Sstevel@tonic-gate 	/*
39720Sstevel@tonic-gate 	 * Chroot
39730Sstevel@tonic-gate 	 */
39740Sstevel@tonic-gate 	vp = zone->zone_rootvp;
39750Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_cdir, pp);
39760Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_rdir, pp);
39770Sstevel@tonic-gate 
39780Sstevel@tonic-gate 	/*
39790Sstevel@tonic-gate 	 * Change process credentials
39800Sstevel@tonic-gate 	 */
39810Sstevel@tonic-gate 	newcr = cralloc();
39820Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
39830Sstevel@tonic-gate 	cr = pp->p_cred;
39840Sstevel@tonic-gate 	crcopy_to(cr, newcr);
39850Sstevel@tonic-gate 	crsetzone(newcr, zone);
39860Sstevel@tonic-gate 	pp->p_cred = newcr;
39870Sstevel@tonic-gate 
39880Sstevel@tonic-gate 	/*
39890Sstevel@tonic-gate 	 * Restrict all process privilege sets to zone limit
39900Sstevel@tonic-gate 	 */
39910Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(newcr));
39920Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(newcr));
39930Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(newcr));
39940Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(newcr));
39950Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
39960Sstevel@tonic-gate 	crset(pp, newcr);
39970Sstevel@tonic-gate 
39980Sstevel@tonic-gate 	/*
39990Sstevel@tonic-gate 	 * Adjust upcount to reflect zone entry.
40000Sstevel@tonic-gate 	 */
40010Sstevel@tonic-gate 	uid = crgetruid(newcr);
40020Sstevel@tonic-gate 	mutex_enter(&pidlock);
40030Sstevel@tonic-gate 	upcount_dec(uid, GLOBAL_ZONEID);
40040Sstevel@tonic-gate 	upcount_inc(uid, zoneid);
40050Sstevel@tonic-gate 	mutex_exit(&pidlock);
40060Sstevel@tonic-gate 
40070Sstevel@tonic-gate 	/*
40080Sstevel@tonic-gate 	 * Set up core file path and content.
40090Sstevel@tonic-gate 	 */
40100Sstevel@tonic-gate 	set_core_defaults();
40110Sstevel@tonic-gate 
40120Sstevel@tonic-gate out:
40130Sstevel@tonic-gate 	/*
40140Sstevel@tonic-gate 	 * Let the other lwps continue.
40150Sstevel@tonic-gate 	 */
40160Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
40170Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
40180Sstevel@tonic-gate 		continuelwps(pp);
40190Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
40200Sstevel@tonic-gate 
40210Sstevel@tonic-gate 	return (err != 0 ? set_errno(err) : 0);
40220Sstevel@tonic-gate }
40230Sstevel@tonic-gate 
40240Sstevel@tonic-gate /*
40250Sstevel@tonic-gate  * Systemcall entry point for zone_list(2).
40260Sstevel@tonic-gate  *
40270Sstevel@tonic-gate  * Processes running in a (non-global) zone only see themselves.
40281676Sjpk  * On labeled systems, they see all zones whose label they dominate.
40290Sstevel@tonic-gate  */
40300Sstevel@tonic-gate static int
40310Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones)
40320Sstevel@tonic-gate {
40330Sstevel@tonic-gate 	zoneid_t *zoneids;
40341769Scarlsonj 	zone_t *zone, *myzone;
40350Sstevel@tonic-gate 	uint_t user_nzones, real_nzones;
40361676Sjpk 	uint_t domi_nzones;
40371676Sjpk 	int error;
40380Sstevel@tonic-gate 
40390Sstevel@tonic-gate 	if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0)
40400Sstevel@tonic-gate 		return (set_errno(EFAULT));
40410Sstevel@tonic-gate 
40421769Scarlsonj 	myzone = curproc->p_zone;
40431769Scarlsonj 	if (myzone != global_zone) {
40441676Sjpk 		bslabel_t *mybslab;
40451676Sjpk 
40461676Sjpk 		if (!is_system_labeled()) {
40471676Sjpk 			/* just return current zone */
40481676Sjpk 			real_nzones = domi_nzones = 1;
40491676Sjpk 			zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP);
40501769Scarlsonj 			zoneids[0] = myzone->zone_id;
40511676Sjpk 		} else {
40521676Sjpk 			/* return all zones that are dominated */
40531676Sjpk 			mutex_enter(&zonehash_lock);
40541676Sjpk 			real_nzones = zonecount;
40551676Sjpk 			domi_nzones = 0;
40561676Sjpk 			if (real_nzones > 0) {
40571676Sjpk 				zoneids = kmem_alloc(real_nzones *
40581676Sjpk 				    sizeof (zoneid_t), KM_SLEEP);
40591769Scarlsonj 				mybslab = label2bslabel(myzone->zone_slabel);
40601676Sjpk 				for (zone = list_head(&zone_active);
40611676Sjpk 				    zone != NULL;
40621676Sjpk 				    zone = list_next(&zone_active, zone)) {
40631676Sjpk 					if (zone->zone_id == GLOBAL_ZONEID)
40641676Sjpk 						continue;
40651769Scarlsonj 					if (zone != myzone &&
40661769Scarlsonj 					    (zone->zone_flags & ZF_IS_SCRATCH))
40671769Scarlsonj 						continue;
40681769Scarlsonj 					/*
40691769Scarlsonj 					 * Note that a label always dominates
40701769Scarlsonj 					 * itself, so myzone is always included
40711769Scarlsonj 					 * in the list.
40721769Scarlsonj 					 */
40731676Sjpk 					if (bldominates(mybslab,
40741676Sjpk 					    label2bslabel(zone->zone_slabel))) {
40751676Sjpk 						zoneids[domi_nzones++] =
40761676Sjpk 						    zone->zone_id;
40771676Sjpk 					}
40781676Sjpk 				}
40791676Sjpk 			}
40801676Sjpk 			mutex_exit(&zonehash_lock);
40811676Sjpk 		}
40820Sstevel@tonic-gate 	} else {
40830Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
40840Sstevel@tonic-gate 		real_nzones = zonecount;
40851676Sjpk 		domi_nzones = 0;
40861676Sjpk 		if (real_nzones > 0) {
40870Sstevel@tonic-gate 			zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t),
40880Sstevel@tonic-gate 			    KM_SLEEP);
40890Sstevel@tonic-gate 			for (zone = list_head(&zone_active); zone != NULL;
40900Sstevel@tonic-gate 			    zone = list_next(&zone_active, zone))
40911676Sjpk 				zoneids[domi_nzones++] = zone->zone_id;
40921676Sjpk 			ASSERT(domi_nzones == real_nzones);
40930Sstevel@tonic-gate 		}
40940Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
40950Sstevel@tonic-gate 	}
40960Sstevel@tonic-gate 
40971676Sjpk 	/*
40981676Sjpk 	 * If user has allocated space for fewer entries than we found, then
40991676Sjpk 	 * return only up to his limit.  Either way, tell him exactly how many
41001676Sjpk 	 * we found.
41011676Sjpk 	 */
41021676Sjpk 	if (domi_nzones < user_nzones)
41031676Sjpk 		user_nzones = domi_nzones;
41041676Sjpk 	error = 0;
41051676Sjpk 	if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) {
41060Sstevel@tonic-gate 		error = EFAULT;
41071676Sjpk 	} else if (zoneidlist != NULL && user_nzones != 0) {
41080Sstevel@tonic-gate 		if (copyout(zoneids, zoneidlist,
41090Sstevel@tonic-gate 		    user_nzones * sizeof (zoneid_t)) != 0)
41100Sstevel@tonic-gate 			error = EFAULT;
41110Sstevel@tonic-gate 	}
41120Sstevel@tonic-gate 
41131676Sjpk 	if (real_nzones > 0)
41140Sstevel@tonic-gate 		kmem_free(zoneids, real_nzones * sizeof (zoneid_t));
41150Sstevel@tonic-gate 
41161676Sjpk 	if (error != 0)
41170Sstevel@tonic-gate 		return (set_errno(error));
41180Sstevel@tonic-gate 	else
41190Sstevel@tonic-gate 		return (0);
41200Sstevel@tonic-gate }
41210Sstevel@tonic-gate 
41220Sstevel@tonic-gate /*
41230Sstevel@tonic-gate  * Systemcall entry point for zone_lookup(2).
41240Sstevel@tonic-gate  *
41251676Sjpk  * Non-global zones are only able to see themselves and (on labeled systems)
41261676Sjpk  * the zones they dominate.
41270Sstevel@tonic-gate  */
41280Sstevel@tonic-gate static zoneid_t
41290Sstevel@tonic-gate zone_lookup(const char *zone_name)
41300Sstevel@tonic-gate {
41310Sstevel@tonic-gate 	char *kname;
41320Sstevel@tonic-gate 	zone_t *zone;
41330Sstevel@tonic-gate 	zoneid_t zoneid;
41340Sstevel@tonic-gate 	int err;
41350Sstevel@tonic-gate 
41360Sstevel@tonic-gate 	if (zone_name == NULL) {
41370Sstevel@tonic-gate 		/* return caller's zone id */
41380Sstevel@tonic-gate 		return (getzoneid());
41390Sstevel@tonic-gate 	}
41400Sstevel@tonic-gate 
41410Sstevel@tonic-gate 	kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
41420Sstevel@tonic-gate 	if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) {
41430Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
41440Sstevel@tonic-gate 		return (set_errno(err));
41450Sstevel@tonic-gate 	}
41460Sstevel@tonic-gate 
41470Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
41480Sstevel@tonic-gate 	zone = zone_find_all_by_name(kname);
41490Sstevel@tonic-gate 	kmem_free(kname, ZONENAME_MAX);
41501676Sjpk 	/*
41511676Sjpk 	 * In a non-global zone, can only lookup global and own name.
41521676Sjpk 	 * In Trusted Extensions zone label dominance rules apply.
41531676Sjpk 	 */
41541676Sjpk 	if (zone == NULL ||
41551676Sjpk 	    zone_status_get(zone) < ZONE_IS_READY ||
41561676Sjpk 	    !zone_list_access(zone)) {
41570Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
41580Sstevel@tonic-gate 		return (set_errno(EINVAL));
41591676Sjpk 	} else {
41601676Sjpk 		zoneid = zone->zone_id;
41611676Sjpk 		mutex_exit(&zonehash_lock);
41621676Sjpk 		return (zoneid);
41630Sstevel@tonic-gate 	}
41640Sstevel@tonic-gate }
41650Sstevel@tonic-gate 
4166813Sdp static int
4167813Sdp zone_version(int *version_arg)
4168813Sdp {
4169813Sdp 	int version = ZONE_SYSCALL_API_VERSION;
4170813Sdp 
4171813Sdp 	if (copyout(&version, version_arg, sizeof (int)) != 0)
4172813Sdp 		return (set_errno(EFAULT));
4173813Sdp 	return (0);
4174813Sdp }
4175813Sdp 
41760Sstevel@tonic-gate /* ARGSUSED */
41770Sstevel@tonic-gate long
4178789Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4)
41790Sstevel@tonic-gate {
41800Sstevel@tonic-gate 	zone_def zs;
41810Sstevel@tonic-gate 
41820Sstevel@tonic-gate 	switch (cmd) {
41830Sstevel@tonic-gate 	case ZONE_CREATE:
41840Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
41850Sstevel@tonic-gate 			if (copyin(arg1, &zs, sizeof (zone_def))) {
41860Sstevel@tonic-gate 				return (set_errno(EFAULT));
41870Sstevel@tonic-gate 			}
41880Sstevel@tonic-gate 		} else {
41890Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
41900Sstevel@tonic-gate 			zone_def32 zs32;
41910Sstevel@tonic-gate 
41920Sstevel@tonic-gate 			if (copyin(arg1, &zs32, sizeof (zone_def32))) {
41930Sstevel@tonic-gate 				return (set_errno(EFAULT));
41940Sstevel@tonic-gate 			}
41950Sstevel@tonic-gate 			zs.zone_name =
41960Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_name;
41970Sstevel@tonic-gate 			zs.zone_root =
41980Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_root;
41990Sstevel@tonic-gate 			zs.zone_privs =
42000Sstevel@tonic-gate 			    (const struct priv_set *)
42010Sstevel@tonic-gate 			    (unsigned long)zs32.zone_privs;
42021409Sdp 			zs.zone_privssz = zs32.zone_privssz;
42030Sstevel@tonic-gate 			zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf;
42040Sstevel@tonic-gate 			zs.rctlbufsz = zs32.rctlbufsz;
4205789Sahrens 			zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf;
4206789Sahrens 			zs.zfsbufsz = zs32.zfsbufsz;
42070Sstevel@tonic-gate 			zs.extended_error =
42080Sstevel@tonic-gate 			    (int *)(unsigned long)zs32.extended_error;
42091676Sjpk 			zs.match = zs32.match;
42101676Sjpk 			zs.doi = zs32.doi;
42111676Sjpk 			zs.label = (const bslabel_t *)(uintptr_t)zs32.label;
42120Sstevel@tonic-gate #else
42130Sstevel@tonic-gate 			panic("get_udatamodel() returned bogus result\n");
42140Sstevel@tonic-gate #endif
42150Sstevel@tonic-gate 		}
42160Sstevel@tonic-gate 
42170Sstevel@tonic-gate 		return (zone_create(zs.zone_name, zs.zone_root,
4218813Sdp 		    zs.zone_privs, zs.zone_privssz,
4219813Sdp 		    (caddr_t)zs.rctlbuf, zs.rctlbufsz,
4220813Sdp 		    (caddr_t)zs.zfsbuf, zs.zfsbufsz,
42211676Sjpk 		    zs.extended_error, zs.match, zs.doi,
42221676Sjpk 		    zs.label));
42230Sstevel@tonic-gate 	case ZONE_BOOT:
42240Sstevel@tonic-gate 		return (zone_boot((zoneid_t)(uintptr_t)arg1,
42250Sstevel@tonic-gate 		    (const char *)arg2));
42260Sstevel@tonic-gate 	case ZONE_DESTROY:
42270Sstevel@tonic-gate 		return (zone_destroy((zoneid_t)(uintptr_t)arg1));
42280Sstevel@tonic-gate 	case ZONE_GETATTR:
42290Sstevel@tonic-gate 		return (zone_getattr((zoneid_t)(uintptr_t)arg1,
42300Sstevel@tonic-gate 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
42310Sstevel@tonic-gate 	case ZONE_ENTER:
42320Sstevel@tonic-gate 		return (zone_enter((zoneid_t)(uintptr_t)arg1));
42330Sstevel@tonic-gate 	case ZONE_LIST:
42340Sstevel@tonic-gate 		return (zone_list((zoneid_t *)arg1, (uint_t *)arg2));
42350Sstevel@tonic-gate 	case ZONE_SHUTDOWN:
42360Sstevel@tonic-gate 		return (zone_shutdown((zoneid_t)(uintptr_t)arg1));
42370Sstevel@tonic-gate 	case ZONE_LOOKUP:
42380Sstevel@tonic-gate 		return (zone_lookup((const char *)arg1));
4239813Sdp 	case ZONE_VERSION:
4240813Sdp 		return (zone_version((int *)arg1));
42410Sstevel@tonic-gate 	default:
42420Sstevel@tonic-gate 		return (set_errno(EINVAL));
42430Sstevel@tonic-gate 	}
42440Sstevel@tonic-gate }
42450Sstevel@tonic-gate 
42460Sstevel@tonic-gate struct zarg {
42470Sstevel@tonic-gate 	zone_t *zone;
42480Sstevel@tonic-gate 	zone_cmd_arg_t arg;
42490Sstevel@tonic-gate };
42500Sstevel@tonic-gate 
42510Sstevel@tonic-gate static int
42520Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp)
42530Sstevel@tonic-gate {
42540Sstevel@tonic-gate 	char *buf;
42550Sstevel@tonic-gate 	size_t buflen;
42560Sstevel@tonic-gate 	int error;
42570Sstevel@tonic-gate 
42580Sstevel@tonic-gate 	buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name);
42590Sstevel@tonic-gate 	buf = kmem_alloc(buflen, KM_SLEEP);
42600Sstevel@tonic-gate 	(void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name);
42610Sstevel@tonic-gate 	error = door_ki_open(buf, doorp);
42620Sstevel@tonic-gate 	kmem_free(buf, buflen);
42630Sstevel@tonic-gate 	return (error);
42640Sstevel@tonic-gate }
42650Sstevel@tonic-gate 
42660Sstevel@tonic-gate static void
42670Sstevel@tonic-gate zone_release_door(door_handle_t *doorp)
42680Sstevel@tonic-gate {
42690Sstevel@tonic-gate 	door_ki_rele(*doorp);
42700Sstevel@tonic-gate 	*doorp = NULL;
42710Sstevel@tonic-gate }
42720Sstevel@tonic-gate 
42730Sstevel@tonic-gate static void
42740Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp)
42750Sstevel@tonic-gate {
42760Sstevel@tonic-gate 	door_handle_t door = NULL;
42770Sstevel@tonic-gate 	door_arg_t darg, save_arg;
42780Sstevel@tonic-gate 	char *zone_name;
42790Sstevel@tonic-gate 	size_t zone_namelen;
42800Sstevel@tonic-gate 	zoneid_t zoneid;
42810Sstevel@tonic-gate 	zone_t *zone;
42820Sstevel@tonic-gate 	zone_cmd_arg_t arg;
42830Sstevel@tonic-gate 	uint64_t uniqid;
42840Sstevel@tonic-gate 	size_t size;
42850Sstevel@tonic-gate 	int error;
42860Sstevel@tonic-gate 	int retry;
42870Sstevel@tonic-gate 
42880Sstevel@tonic-gate 	zone = zargp->zone;
42890Sstevel@tonic-gate 	arg = zargp->arg;
42900Sstevel@tonic-gate 	kmem_free(zargp, sizeof (*zargp));
42910Sstevel@tonic-gate 
42920Sstevel@tonic-gate 	zone_namelen = strlen(zone->zone_name) + 1;
42930Sstevel@tonic-gate 	zone_name = kmem_alloc(zone_namelen, KM_SLEEP);
42940Sstevel@tonic-gate 	bcopy(zone->zone_name, zone_name, zone_namelen);
42950Sstevel@tonic-gate 	zoneid = zone->zone_id;
42960Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
42970Sstevel@tonic-gate 	/*
42980Sstevel@tonic-gate 	 * zoneadmd may be down, but at least we can empty out the zone.
42990Sstevel@tonic-gate 	 * We can ignore the return value of zone_empty() since we're called
43000Sstevel@tonic-gate 	 * from a kernel thread and know we won't be delivered any signals.
43010Sstevel@tonic-gate 	 */
43020Sstevel@tonic-gate 	ASSERT(curproc == &p0);
43030Sstevel@tonic-gate 	(void) zone_empty(zone);
43040Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY);
43050Sstevel@tonic-gate 	zone_rele(zone);
43060Sstevel@tonic-gate 
43070Sstevel@tonic-gate 	size = sizeof (arg);
43080Sstevel@tonic-gate 	darg.rbuf = (char *)&arg;
43090Sstevel@tonic-gate 	darg.data_ptr = (char *)&arg;
43100Sstevel@tonic-gate 	darg.rsize = size;
43110Sstevel@tonic-gate 	darg.data_size = size;
43120Sstevel@tonic-gate 	darg.desc_ptr = NULL;
43130Sstevel@tonic-gate 	darg.desc_num = 0;
43140Sstevel@tonic-gate 
43150Sstevel@tonic-gate 	save_arg = darg;
43160Sstevel@tonic-gate 	/*
43170Sstevel@tonic-gate 	 * Since we're not holding a reference to the zone, any number of
43180Sstevel@tonic-gate 	 * things can go wrong, including the zone disappearing before we get a
43190Sstevel@tonic-gate 	 * chance to talk to zoneadmd.
43200Sstevel@tonic-gate 	 */
43210Sstevel@tonic-gate 	for (retry = 0; /* forever */; retry++) {
43220Sstevel@tonic-gate 		if (door == NULL &&
43230Sstevel@tonic-gate 		    (error = zone_lookup_door(zone_name, &door)) != 0) {
43240Sstevel@tonic-gate 			goto next;
43250Sstevel@tonic-gate 		}
43260Sstevel@tonic-gate 		ASSERT(door != NULL);
43270Sstevel@tonic-gate 
43280Sstevel@tonic-gate 		if ((error = door_ki_upcall(door, &darg)) == 0) {
43290Sstevel@tonic-gate 			break;
43300Sstevel@tonic-gate 		}
43310Sstevel@tonic-gate 		switch (error) {
43320Sstevel@tonic-gate 		case EINTR:
43330Sstevel@tonic-gate 			/* FALLTHROUGH */
43340Sstevel@tonic-gate 		case EAGAIN:	/* process may be forking */
43350Sstevel@tonic-gate 			/*
43360Sstevel@tonic-gate 			 * Back off for a bit
43370Sstevel@tonic-gate 			 */
43380Sstevel@tonic-gate 			break;
43390Sstevel@tonic-gate 		case EBADF:
43400Sstevel@tonic-gate 			zone_release_door(&door);
43410Sstevel@tonic-gate 			if (zone_lookup_door(zone_name, &door) != 0) {
43420Sstevel@tonic-gate 				/*
43430Sstevel@tonic-gate 				 * zoneadmd may be dead, but it may come back to
43440Sstevel@tonic-gate 				 * life later.
43450Sstevel@tonic-gate 				 */
43460Sstevel@tonic-gate 				break;
43470Sstevel@tonic-gate 			}
43480Sstevel@tonic-gate 			break;
43490Sstevel@tonic-gate 		default:
43500Sstevel@tonic-gate 			cmn_err(CE_WARN,
43510Sstevel@tonic-gate 			    "zone_ki_call_zoneadmd: door_ki_upcall error %d\n",
43520Sstevel@tonic-gate 			    error);
43530Sstevel@tonic-gate 			goto out;
43540Sstevel@tonic-gate 		}
43550Sstevel@tonic-gate next:
43560Sstevel@tonic-gate 		/*
43570Sstevel@tonic-gate 		 * If this isn't the same zone_t that we originally had in mind,
43580Sstevel@tonic-gate 		 * then this is the same as if two kadmin requests come in at
43590Sstevel@tonic-gate 		 * the same time: the first one wins.  This means we lose, so we
43600Sstevel@tonic-gate 		 * bail.
43610Sstevel@tonic-gate 		 */
43620Sstevel@tonic-gate 		if ((zone = zone_find_by_id(zoneid)) == NULL) {
43630Sstevel@tonic-gate 			/*
43640Sstevel@tonic-gate 			 * Problem is solved.
43650Sstevel@tonic-gate 			 */
43660Sstevel@tonic-gate 			break;
43670Sstevel@tonic-gate 		}
43680Sstevel@tonic-gate 		if (zone->zone_uniqid != uniqid) {
43690Sstevel@tonic-gate 			/*
43700Sstevel@tonic-gate 			 * zoneid recycled
43710Sstevel@tonic-gate 			 */
43720Sstevel@tonic-gate 			zone_rele(zone);
43730Sstevel@tonic-gate 			break;
43740Sstevel@tonic-gate 		}
43750Sstevel@tonic-gate 		/*
43760Sstevel@tonic-gate 		 * We could zone_status_timedwait(), but there doesn't seem to
43770Sstevel@tonic-gate 		 * be much point in doing that (plus, it would mean that
43780Sstevel@tonic-gate 		 * zone_free() isn't called until this thread exits).
43790Sstevel@tonic-gate 		 */
43800Sstevel@tonic-gate 		zone_rele(zone);
43810Sstevel@tonic-gate 		delay(hz);
43820Sstevel@tonic-gate 		darg = save_arg;
43830Sstevel@tonic-gate 	}
43840Sstevel@tonic-gate out:
43850Sstevel@tonic-gate 	if (door != NULL) {
43860Sstevel@tonic-gate 		zone_release_door(&door);
43870Sstevel@tonic-gate 	}
43880Sstevel@tonic-gate 	kmem_free(zone_name, zone_namelen);
43890Sstevel@tonic-gate 	thread_exit();
43900Sstevel@tonic-gate }
43910Sstevel@tonic-gate 
43920Sstevel@tonic-gate /*
43930Sstevel@tonic-gate  * Entry point for uadmin() to tell the zone to go away or reboot.  The caller
43940Sstevel@tonic-gate  * is a process in the zone to be modified.
43950Sstevel@tonic-gate  *
43960Sstevel@tonic-gate  * In order to shutdown the zone, we will hand off control to zoneadmd
43970Sstevel@tonic-gate  * (running in the global zone) via a door.  We do a half-hearted job at
43980Sstevel@tonic-gate  * killing all processes in the zone, create a kernel thread to contact
43990Sstevel@tonic-gate  * zoneadmd, and make note of the "uniqid" of the zone.  The uniqid is
44000Sstevel@tonic-gate  * a form of generation number used to let zoneadmd (as well as
44010Sstevel@tonic-gate  * zone_destroy()) know exactly which zone they're re talking about.
44020Sstevel@tonic-gate  */
44030Sstevel@tonic-gate int
44040Sstevel@tonic-gate zone_uadmin(int cmd, int fcn, cred_t *credp)
44050Sstevel@tonic-gate {
44060Sstevel@tonic-gate 	struct zarg *zargp;
44070Sstevel@tonic-gate 	zone_cmd_t zcmd;
44080Sstevel@tonic-gate 	zone_t *zone;
44090Sstevel@tonic-gate 
44100Sstevel@tonic-gate 	zone = curproc->p_zone;
44110Sstevel@tonic-gate 	ASSERT(getzoneid() != GLOBAL_ZONEID);
44120Sstevel@tonic-gate 
44130Sstevel@tonic-gate 	switch (cmd) {
44140Sstevel@tonic-gate 	case A_SHUTDOWN:
44150Sstevel@tonic-gate 		switch (fcn) {
44160Sstevel@tonic-gate 		case AD_HALT:
44170Sstevel@tonic-gate 		case AD_POWEROFF:
44180Sstevel@tonic-gate 			zcmd = Z_HALT;
44190Sstevel@tonic-gate 			break;
44200Sstevel@tonic-gate 		case AD_BOOT:
44210Sstevel@tonic-gate 			zcmd = Z_REBOOT;
44220Sstevel@tonic-gate 			break;
44230Sstevel@tonic-gate 		case AD_IBOOT:
44240Sstevel@tonic-gate 		case AD_SBOOT:
44250Sstevel@tonic-gate 		case AD_SIBOOT:
44260Sstevel@tonic-gate 		case AD_NOSYNC:
44270Sstevel@tonic-gate 			return (ENOTSUP);
44280Sstevel@tonic-gate 		default:
44290Sstevel@tonic-gate 			return (EINVAL);
44300Sstevel@tonic-gate 		}
44310Sstevel@tonic-gate 		break;
44320Sstevel@tonic-gate 	case A_REBOOT:
44330Sstevel@tonic-gate 		zcmd = Z_REBOOT;
44340Sstevel@tonic-gate 		break;
44350Sstevel@tonic-gate 	case A_FTRACE:
44360Sstevel@tonic-gate 	case A_REMOUNT:
44370Sstevel@tonic-gate 	case A_FREEZE:
44380Sstevel@tonic-gate 	case A_DUMP:
44390Sstevel@tonic-gate 		return (ENOTSUP);
44400Sstevel@tonic-gate 	default:
44410Sstevel@tonic-gate 		ASSERT(cmd != A_SWAPCTL);	/* handled by uadmin() */
44420Sstevel@tonic-gate 		return (EINVAL);
44430Sstevel@tonic-gate 	}
44440Sstevel@tonic-gate 
44450Sstevel@tonic-gate 	if (secpolicy_zone_admin(credp, B_FALSE))
44460Sstevel@tonic-gate 		return (EPERM);
44470Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
44480Sstevel@tonic-gate 	/*
44490Sstevel@tonic-gate 	 * zone_status can't be ZONE_IS_EMPTY or higher since curproc
44500Sstevel@tonic-gate 	 * is in the zone.
44510Sstevel@tonic-gate 	 */
44520Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY);
44530Sstevel@tonic-gate 	if (zone_status_get(zone) > ZONE_IS_RUNNING) {
44540Sstevel@tonic-gate 		/*
44550Sstevel@tonic-gate 		 * This zone is already on its way down.
44560Sstevel@tonic-gate 		 */
44570Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
44580Sstevel@tonic-gate 		return (0);
44590Sstevel@tonic-gate 	}
44600Sstevel@tonic-gate 	/*
44610Sstevel@tonic-gate 	 * Prevent future zone_enter()s
44620Sstevel@tonic-gate 	 */
44630Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
44640Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
44650Sstevel@tonic-gate 
44660Sstevel@tonic-gate 	/*
44670Sstevel@tonic-gate 	 * Kill everyone now and call zoneadmd later.
44680Sstevel@tonic-gate 	 * zone_ki_call_zoneadmd() will do a more thorough job of this
44690Sstevel@tonic-gate 	 * later.
44700Sstevel@tonic-gate 	 */
44710Sstevel@tonic-gate 	killall(zone->zone_id);
44720Sstevel@tonic-gate 	/*
44730Sstevel@tonic-gate 	 * Now, create the thread to contact zoneadmd and do the rest of the
44740Sstevel@tonic-gate 	 * work.  This thread can't be created in our zone otherwise
44750Sstevel@tonic-gate 	 * zone_destroy() would deadlock.
44760Sstevel@tonic-gate 	 */
44770Sstevel@tonic-gate 	zargp = kmem_alloc(sizeof (*zargp), KM_SLEEP);
44780Sstevel@tonic-gate 	zargp->arg.cmd = zcmd;
44790Sstevel@tonic-gate 	zargp->arg.uniqid = zone->zone_uniqid;
44800Sstevel@tonic-gate 	(void) strcpy(zargp->arg.locale, "C");
44810Sstevel@tonic-gate 	zone_hold(zargp->zone = zone);
44820Sstevel@tonic-gate 
44830Sstevel@tonic-gate 	(void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0,
44840Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
44850Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
44860Sstevel@tonic-gate 
44870Sstevel@tonic-gate 	return (EINVAL);
44880Sstevel@tonic-gate }
44890Sstevel@tonic-gate 
44900Sstevel@tonic-gate /*
44910Sstevel@tonic-gate  * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's
44920Sstevel@tonic-gate  * status to ZONE_IS_SHUTTING_DOWN.
44930Sstevel@tonic-gate  */
44940Sstevel@tonic-gate void
44950Sstevel@tonic-gate zone_shutdown_global(void)
44960Sstevel@tonic-gate {
44970Sstevel@tonic-gate 	ASSERT(curproc->p_zone == global_zone);
44980Sstevel@tonic-gate 
44990Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
45000Sstevel@tonic-gate 	ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING);
45010Sstevel@tonic-gate 	zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN);
45020Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
45030Sstevel@tonic-gate }
4504789Sahrens 
4505789Sahrens /*
4506789Sahrens  * Returns true if the named dataset is visible in the current zone.
4507789Sahrens  * The 'write' parameter is set to 1 if the dataset is also writable.
4508789Sahrens  */
4509789Sahrens int
4510789Sahrens zone_dataset_visible(const char *dataset, int *write)
4511789Sahrens {
4512789Sahrens 	zone_dataset_t *zd;
4513789Sahrens 	size_t len;
4514789Sahrens 	zone_t *zone = curproc->p_zone;
4515789Sahrens 
4516789Sahrens 	if (dataset[0] == '\0')
4517789Sahrens 		return (0);
4518789Sahrens 
4519789Sahrens 	/*
4520789Sahrens 	 * Walk the list once, looking for datasets which match exactly, or
4521789Sahrens 	 * specify a dataset underneath an exported dataset.  If found, return
4522789Sahrens 	 * true and note that it is writable.
4523789Sahrens 	 */
4524789Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
4525789Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
4526789Sahrens 
4527789Sahrens 		len = strlen(zd->zd_dataset);
4528789Sahrens 		if (strlen(dataset) >= len &&
4529789Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
4530816Smaybee 		    (dataset[len] == '\0' || dataset[len] == '/' ||
4531816Smaybee 		    dataset[len] == '@')) {
4532789Sahrens 			if (write)
4533789Sahrens 				*write = 1;
4534789Sahrens 			return (1);
4535789Sahrens 		}
4536789Sahrens 	}
4537789Sahrens 
4538789Sahrens 	/*
4539789Sahrens 	 * Walk the list a second time, searching for datasets which are parents
4540789Sahrens 	 * of exported datasets.  These should be visible, but read-only.
4541789Sahrens 	 *
4542789Sahrens 	 * Note that we also have to support forms such as 'pool/dataset/', with
4543789Sahrens 	 * a trailing slash.
4544789Sahrens 	 */
4545789Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
4546789Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
4547789Sahrens 
4548789Sahrens 		len = strlen(dataset);
4549789Sahrens 		if (dataset[len - 1] == '/')
4550789Sahrens 			len--;	/* Ignore trailing slash */
4551789Sahrens 		if (len < strlen(zd->zd_dataset) &&
4552789Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
4553789Sahrens 		    zd->zd_dataset[len] == '/') {
4554789Sahrens 			if (write)
4555789Sahrens 				*write = 0;
4556789Sahrens 			return (1);
4557789Sahrens 		}
4558789Sahrens 	}
4559789Sahrens 
4560789Sahrens 	return (0);
4561789Sahrens }
45621676Sjpk 
45631676Sjpk /*
45641676Sjpk  * zone_find_by_any_path() -
45651676Sjpk  *
45661676Sjpk  * kernel-private routine similar to zone_find_by_path(), but which
45671676Sjpk  * effectively compares against zone paths rather than zonerootpath
45681676Sjpk  * (i.e., the last component of zonerootpaths, which should be "root/",
45691676Sjpk  * are not compared.)  This is done in order to accurately identify all
45701676Sjpk  * paths, whether zone-visible or not, including those which are parallel
45711676Sjpk  * to /root/, such as /dev/, /home/, etc...
45721676Sjpk  *
45731676Sjpk  * If the specified path does not fall under any zone path then global
45741676Sjpk  * zone is returned.
45751676Sjpk  *
45761676Sjpk  * The treat_abs parameter indicates whether the path should be treated as
45771676Sjpk  * an absolute path although it does not begin with "/".  (This supports
45781676Sjpk  * nfs mount syntax such as host:any/path.)
45791676Sjpk  *
45801676Sjpk  * The caller is responsible for zone_rele of the returned zone.
45811676Sjpk  */
45821676Sjpk zone_t *
45831676Sjpk zone_find_by_any_path(const char *path, boolean_t treat_abs)
45841676Sjpk {
45851676Sjpk 	zone_t *zone;
45861676Sjpk 	int path_offset = 0;
45871676Sjpk 
45881676Sjpk 	if (path == NULL) {
45891676Sjpk 		zone_hold(global_zone);
45901676Sjpk 		return (global_zone);
45911676Sjpk 	}
45921676Sjpk 
45931676Sjpk 	if (*path != '/') {
45941676Sjpk 		ASSERT(treat_abs);
45951676Sjpk 		path_offset = 1;
45961676Sjpk 	}
45971676Sjpk 
45981676Sjpk 	mutex_enter(&zonehash_lock);
45991676Sjpk 	for (zone = list_head(&zone_active); zone != NULL;
46001676Sjpk 	    zone = list_next(&zone_active, zone)) {
46011676Sjpk 		char	*c;
46021676Sjpk 		size_t	pathlen;
46031876Smp46848 		char *rootpath_start;
46041676Sjpk 
46051676Sjpk 		if (zone == global_zone)	/* skip global zone */
46061676Sjpk 			continue;
46071676Sjpk 
46081676Sjpk 		/* scan backwards to find start of last component */
46091676Sjpk 		c = zone->zone_rootpath + zone->zone_rootpathlen - 2;
46101676Sjpk 		do {
46111676Sjpk 			c--;
46121676Sjpk 		} while (*c != '/');
46131676Sjpk 
46141876Smp46848 		pathlen = c - zone->zone_rootpath + 1 - path_offset;
46151876Smp46848 		rootpath_start = (zone->zone_rootpath + path_offset);
46161876Smp46848 		if (strncmp(path, rootpath_start, pathlen) == 0)
46171676Sjpk 			break;
46181676Sjpk 	}
46191676Sjpk 	if (zone == NULL)
46201676Sjpk 		zone = global_zone;
46211676Sjpk 	zone_hold(zone);
46221676Sjpk 	mutex_exit(&zonehash_lock);
46231676Sjpk 	return (zone);
46241676Sjpk }
4625