xref: /onnv-gate/usr/src/uts/common/os/zone.c (revision 3671:48011e38989d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21390Sraf 
220Sstevel@tonic-gate /*
233446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Zones
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  *   A zone is a named collection of processes, namespace constraints,
330Sstevel@tonic-gate  *   and other system resources which comprise a secure and manageable
340Sstevel@tonic-gate  *   application containment facility.
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  *   Zones (represented by the reference counted zone_t) are tracked in
370Sstevel@tonic-gate  *   the kernel in the zonehash.  Elsewhere in the kernel, Zone IDs
380Sstevel@tonic-gate  *   (zoneid_t) are used to track zone association.  Zone IDs are
390Sstevel@tonic-gate  *   dynamically generated when the zone is created; if a persistent
400Sstevel@tonic-gate  *   identifier is needed (core files, accounting logs, audit trail,
410Sstevel@tonic-gate  *   etc.), the zone name should be used.
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  *   Global Zone:
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  *   The global zone (zoneid 0) is automatically associated with all
470Sstevel@tonic-gate  *   system resources that have not been bound to a user-created zone.
480Sstevel@tonic-gate  *   This means that even systems where zones are not in active use
490Sstevel@tonic-gate  *   have a global zone, and all processes, mounts, etc. are
500Sstevel@tonic-gate  *   associated with that zone.  The global zone is generally
510Sstevel@tonic-gate  *   unconstrained in terms of privileges and access, though the usual
520Sstevel@tonic-gate  *   credential and privilege based restrictions apply.
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  *   Zone States:
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  *   The states in which a zone may be in and the transitions are as
580Sstevel@tonic-gate  *   follows:
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  *   ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially
610Sstevel@tonic-gate  *   initialized zone is added to the list of active zones on the system but
620Sstevel@tonic-gate  *   isn't accessible.
630Sstevel@tonic-gate  *
640Sstevel@tonic-gate  *   ZONE_IS_READY: zsched (the kernel dummy process for a zone) is
650Sstevel@tonic-gate  *   ready.  The zone is made visible after the ZSD constructor callbacks are
660Sstevel@tonic-gate  *   executed.  A zone remains in this state until it transitions into
670Sstevel@tonic-gate  *   the ZONE_IS_BOOTING state as a result of a call to zone_boot().
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  *   ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start
700Sstevel@tonic-gate  *   init.  Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN
710Sstevel@tonic-gate  *   state.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  *   ZONE_IS_RUNNING: The zone is open for business: zsched has
740Sstevel@tonic-gate  *   successfully started init.   A zone remains in this state until
750Sstevel@tonic-gate  *   zone_shutdown() is called.
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is
780Sstevel@tonic-gate  *   killing all processes running in the zone. The zone remains
790Sstevel@tonic-gate  *   in this state until there are no more user processes running in the zone.
800Sstevel@tonic-gate  *   zone_create(), zone_enter(), and zone_destroy() on this zone will fail.
810Sstevel@tonic-gate  *   Since zone_shutdown() is restartable, it may be called successfully
820Sstevel@tonic-gate  *   multiple times for the same zone_t.  Setting of the zone's state to
830Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check
840Sstevel@tonic-gate  *   the zone's status without worrying about it being a moving target.
850Sstevel@tonic-gate  *
860Sstevel@tonic-gate  *   ZONE_IS_EMPTY: zone_shutdown() has been called, and there
870Sstevel@tonic-gate  *   are no more user processes in the zone.  The zone remains in this
880Sstevel@tonic-gate  *   state until there are no more kernel threads associated with the
890Sstevel@tonic-gate  *   zone.  zone_create(), zone_enter(), and zone_destroy() on this zone will
900Sstevel@tonic-gate  *   fail.
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  *   ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone
930Sstevel@tonic-gate  *   have exited.  zone_shutdown() returns.  Henceforth it is not possible to
940Sstevel@tonic-gate  *   join the zone or create kernel threads therein.
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  *   ZONE_IS_DYING: zone_destroy() has been called on the zone; zone
970Sstevel@tonic-gate  *   remains in this state until zsched exits.  Calls to zone_find_by_*()
980Sstevel@tonic-gate  *   return NULL from now on.
990Sstevel@tonic-gate  *
1000Sstevel@tonic-gate  *   ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0).  There are no
1010Sstevel@tonic-gate  *   processes or threads doing work on behalf of the zone.  The zone is
1020Sstevel@tonic-gate  *   removed from the list of active zones.  zone_destroy() returns, and
1030Sstevel@tonic-gate  *   the zone can be recreated.
1040Sstevel@tonic-gate  *
1050Sstevel@tonic-gate  *   ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor
1060Sstevel@tonic-gate  *   callbacks are executed, and all memory associated with the zone is
1070Sstevel@tonic-gate  *   freed.
1080Sstevel@tonic-gate  *
1090Sstevel@tonic-gate  *   Threads can wait for the zone to enter a requested state by using
1100Sstevel@tonic-gate  *   zone_status_wait() or zone_status_timedwait() with the desired
1110Sstevel@tonic-gate  *   state passed in as an argument.  Zone state transitions are
1120Sstevel@tonic-gate  *   uni-directional; it is not possible to move back to an earlier state.
1130Sstevel@tonic-gate  *
1140Sstevel@tonic-gate  *
1150Sstevel@tonic-gate  *   Zone-Specific Data:
1160Sstevel@tonic-gate  *
1170Sstevel@tonic-gate  *   Subsystems needing to maintain zone-specific data can store that
1180Sstevel@tonic-gate  *   data using the ZSD mechanism.  This provides a zone-specific data
1190Sstevel@tonic-gate  *   store, similar to thread-specific data (see pthread_getspecific(3C)
1200Sstevel@tonic-gate  *   or the TSD code in uts/common/disp/thread.c.  Also, ZSD can be used
1210Sstevel@tonic-gate  *   to register callbacks to be invoked when a zone is created, shut
1220Sstevel@tonic-gate  *   down, or destroyed.  This can be used to initialize zone-specific
1230Sstevel@tonic-gate  *   data for new zones and to clean up when zones go away.
1240Sstevel@tonic-gate  *
1250Sstevel@tonic-gate  *
1260Sstevel@tonic-gate  *   Data Structures:
1270Sstevel@tonic-gate  *
1280Sstevel@tonic-gate  *   The per-zone structure (zone_t) is reference counted, and freed
1290Sstevel@tonic-gate  *   when all references are released.  zone_hold and zone_rele can be
1300Sstevel@tonic-gate  *   used to adjust the reference count.  In addition, reference counts
1310Sstevel@tonic-gate  *   associated with the cred_t structure are tracked separately using
1320Sstevel@tonic-gate  *   zone_cred_hold and zone_cred_rele.
1330Sstevel@tonic-gate  *
1340Sstevel@tonic-gate  *   Pointers to active zone_t's are stored in two hash tables; one
1350Sstevel@tonic-gate  *   for searching by id, the other for searching by name.  Lookups
1360Sstevel@tonic-gate  *   can be performed on either basis, using zone_find_by_id and
1370Sstevel@tonic-gate  *   zone_find_by_name.  Both return zone_t pointers with the zone
1380Sstevel@tonic-gate  *   held, so zone_rele should be called when the pointer is no longer
1390Sstevel@tonic-gate  *   needed.  Zones can also be searched by path; zone_find_by_path
1400Sstevel@tonic-gate  *   returns the zone with which a path name is associated (global
1410Sstevel@tonic-gate  *   zone if the path is not within some other zone's file system
1420Sstevel@tonic-gate  *   hierarchy).  This currently requires iterating through each zone,
1430Sstevel@tonic-gate  *   so it is slower than an id or name search via a hash table.
1440Sstevel@tonic-gate  *
1450Sstevel@tonic-gate  *
1460Sstevel@tonic-gate  *   Locking:
1470Sstevel@tonic-gate  *
1480Sstevel@tonic-gate  *   zonehash_lock: This is a top-level global lock used to protect the
1490Sstevel@tonic-gate  *       zone hash tables and lists.  Zones cannot be created or destroyed
1500Sstevel@tonic-gate  *       while this lock is held.
1510Sstevel@tonic-gate  *   zone_status_lock: This is a global lock protecting zone state.
1520Sstevel@tonic-gate  *       Zones cannot change state while this lock is held.  It also
1530Sstevel@tonic-gate  *       protects the list of kernel threads associated with a zone.
1540Sstevel@tonic-gate  *   zone_lock: This is a per-zone lock used to protect several fields of
1550Sstevel@tonic-gate  *       the zone_t (see <sys/zone.h> for details).  In addition, holding
1560Sstevel@tonic-gate  *       this lock means that the zone cannot go away.
1573247Sgjelinek  *   zone_nlwps_lock: This is a per-zone lock used to protect the fields
1583247Sgjelinek  *	 related to the zone.max-lwps rctl.
1593247Sgjelinek  *   zone_mem_lock: This is a per-zone lock used to protect the fields
1603247Sgjelinek  *	 related to the zone.max-locked-memory and zone.max-swap rctls.
1610Sstevel@tonic-gate  *   zsd_key_lock: This is a global lock protecting the key state for ZSD.
1620Sstevel@tonic-gate  *   zone_deathrow_lock: This is a global lock protecting the "deathrow"
1630Sstevel@tonic-gate  *       list (a list of zones in the ZONE_IS_DEAD state).
1640Sstevel@tonic-gate  *
1650Sstevel@tonic-gate  *   Ordering requirements:
1660Sstevel@tonic-gate  *       pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock -->
1670Sstevel@tonic-gate  *       	zone_lock --> zsd_key_lock --> pidlock --> p_lock
1680Sstevel@tonic-gate  *
1693247Sgjelinek  *   When taking zone_mem_lock or zone_nlwps_lock, the lock ordering is:
1703247Sgjelinek  *	zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock
1713247Sgjelinek  *	zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock
1723247Sgjelinek  *
1730Sstevel@tonic-gate  *   Blocking memory allocations are permitted while holding any of the
1740Sstevel@tonic-gate  *   zone locks.
1750Sstevel@tonic-gate  *
1760Sstevel@tonic-gate  *
1770Sstevel@tonic-gate  *   System Call Interface:
1780Sstevel@tonic-gate  *
1790Sstevel@tonic-gate  *   The zone subsystem can be managed and queried from user level with
1800Sstevel@tonic-gate  *   the following system calls (all subcodes of the primary "zone"
1810Sstevel@tonic-gate  *   system call):
1820Sstevel@tonic-gate  *   - zone_create: creates a zone with selected attributes (name,
183789Sahrens  *     root path, privileges, resource controls, ZFS datasets)
1840Sstevel@tonic-gate  *   - zone_enter: allows the current process to enter a zone
1850Sstevel@tonic-gate  *   - zone_getattr: reports attributes of a zone
1862267Sdp  *   - zone_setattr: set attributes of a zone
1872267Sdp  *   - zone_boot: set 'init' running for the zone
1880Sstevel@tonic-gate  *   - zone_list: lists all zones active in the system
1890Sstevel@tonic-gate  *   - zone_lookup: looks up zone id based on name
1900Sstevel@tonic-gate  *   - zone_shutdown: initiates shutdown process (see states above)
1910Sstevel@tonic-gate  *   - zone_destroy: completes shutdown process (see states above)
1920Sstevel@tonic-gate  *
1930Sstevel@tonic-gate  */
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate #include <sys/priv_impl.h>
1960Sstevel@tonic-gate #include <sys/cred.h>
1970Sstevel@tonic-gate #include <c2/audit.h>
1980Sstevel@tonic-gate #include <sys/debug.h>
1990Sstevel@tonic-gate #include <sys/file.h>
2000Sstevel@tonic-gate #include <sys/kmem.h>
2013247Sgjelinek #include <sys/kstat.h>
2020Sstevel@tonic-gate #include <sys/mutex.h>
2031676Sjpk #include <sys/note.h>
2040Sstevel@tonic-gate #include <sys/pathname.h>
2050Sstevel@tonic-gate #include <sys/proc.h>
2060Sstevel@tonic-gate #include <sys/project.h>
2071166Sdstaff #include <sys/sysevent.h>
2080Sstevel@tonic-gate #include <sys/task.h>
2090Sstevel@tonic-gate #include <sys/systm.h>
2100Sstevel@tonic-gate #include <sys/types.h>
2110Sstevel@tonic-gate #include <sys/utsname.h>
2120Sstevel@tonic-gate #include <sys/vnode.h>
2130Sstevel@tonic-gate #include <sys/vfs.h>
2140Sstevel@tonic-gate #include <sys/systeminfo.h>
2150Sstevel@tonic-gate #include <sys/policy.h>
2160Sstevel@tonic-gate #include <sys/cred_impl.h>
2170Sstevel@tonic-gate #include <sys/contract_impl.h>
2180Sstevel@tonic-gate #include <sys/contract/process_impl.h>
2190Sstevel@tonic-gate #include <sys/class.h>
2200Sstevel@tonic-gate #include <sys/pool.h>
2210Sstevel@tonic-gate #include <sys/pool_pset.h>
2220Sstevel@tonic-gate #include <sys/pset.h>
2230Sstevel@tonic-gate #include <sys/sysmacros.h>
2240Sstevel@tonic-gate #include <sys/callb.h>
2250Sstevel@tonic-gate #include <sys/vmparam.h>
2260Sstevel@tonic-gate #include <sys/corectl.h>
2272677Sml93401 #include <sys/ipc_impl.h>
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate #include <sys/door.h>
2300Sstevel@tonic-gate #include <sys/cpuvar.h>
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate #include <sys/uadmin.h>
2330Sstevel@tonic-gate #include <sys/session.h>
2340Sstevel@tonic-gate #include <sys/cmn_err.h>
2350Sstevel@tonic-gate #include <sys/modhash.h>
2362267Sdp #include <sys/sunddi.h>
2370Sstevel@tonic-gate #include <sys/nvpair.h>
2380Sstevel@tonic-gate #include <sys/rctl.h>
2390Sstevel@tonic-gate #include <sys/fss.h>
2402712Snn35248 #include <sys/brand.h>
2410Sstevel@tonic-gate #include <sys/zone.h>
2423448Sdh155122 #include <net/if.h>
2433247Sgjelinek #include <vm/seg.h>
2443247Sgjelinek 
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate  * cv used to signal that all references to the zone have been released.  This
2470Sstevel@tonic-gate  * needs to be global since there may be multiple waiters, and the first to
2480Sstevel@tonic-gate  * wake up will free the zone_t, hence we cannot use zone->zone_cv.
2490Sstevel@tonic-gate  */
2500Sstevel@tonic-gate static kcondvar_t zone_destroy_cv;
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate  * Lock used to serialize access to zone_cv.  This could have been per-zone,
2530Sstevel@tonic-gate  * but then we'd need another lock for zone_destroy_cv, and why bother?
2540Sstevel@tonic-gate  */
2550Sstevel@tonic-gate static kmutex_t zone_status_lock;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate /*
2580Sstevel@tonic-gate  * ZSD-related global variables.
2590Sstevel@tonic-gate  */
2600Sstevel@tonic-gate static kmutex_t zsd_key_lock;	/* protects the following two */
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate  * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval.
2630Sstevel@tonic-gate  */
2640Sstevel@tonic-gate static zone_key_t zsd_keyval = 0;
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate  * Global list of registered keys.  We use this when a new zone is created.
2670Sstevel@tonic-gate  */
2680Sstevel@tonic-gate static list_t zsd_registered_keys;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate int zone_hash_size = 256;
2711676Sjpk static mod_hash_t *zonehashbyname, *zonehashbyid, *zonehashbylabel;
2720Sstevel@tonic-gate static kmutex_t zonehash_lock;
2730Sstevel@tonic-gate static uint_t zonecount;
2740Sstevel@tonic-gate static id_space_t *zoneid_space;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate /*
2770Sstevel@tonic-gate  * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the
2780Sstevel@tonic-gate  * kernel proper runs, and which manages all other zones.
2790Sstevel@tonic-gate  *
2800Sstevel@tonic-gate  * Although not declared as static, the variable "zone0" should not be used
2810Sstevel@tonic-gate  * except for by code that needs to reference the global zone early on in boot,
2820Sstevel@tonic-gate  * before it is fully initialized.  All other consumers should use
2830Sstevel@tonic-gate  * 'global_zone'.
2840Sstevel@tonic-gate  */
2850Sstevel@tonic-gate zone_t zone0;
2860Sstevel@tonic-gate zone_t *global_zone = NULL;	/* Set when the global zone is initialized */
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate  * List of active zones, protected by zonehash_lock.
2900Sstevel@tonic-gate  */
2910Sstevel@tonic-gate static list_t zone_active;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate /*
2940Sstevel@tonic-gate  * List of destroyed zones that still have outstanding cred references.
2950Sstevel@tonic-gate  * Used for debugging.  Uses a separate lock to avoid lock ordering
2960Sstevel@tonic-gate  * problems in zone_free.
2970Sstevel@tonic-gate  */
2980Sstevel@tonic-gate static list_t zone_deathrow;
2990Sstevel@tonic-gate static kmutex_t zone_deathrow_lock;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */
3020Sstevel@tonic-gate uint_t maxzones = 8192;
3030Sstevel@tonic-gate 
3041166Sdstaff /* Event channel to sent zone state change notifications */
3051166Sdstaff evchan_t *zone_event_chan;
3061166Sdstaff 
3071166Sdstaff /*
3081166Sdstaff  * This table holds the mapping from kernel zone states to
3091166Sdstaff  * states visible in the state notification API.
3101166Sdstaff  * The idea is that we only expose "obvious" states and
3111166Sdstaff  * do not expose states which are just implementation details.
3121166Sdstaff  */
3131166Sdstaff const char  *zone_status_table[] = {
3141166Sdstaff 	ZONE_EVENT_UNINITIALIZED,	/* uninitialized */
3151166Sdstaff 	ZONE_EVENT_READY,		/* ready */
3161166Sdstaff 	ZONE_EVENT_READY,		/* booting */
3171166Sdstaff 	ZONE_EVENT_RUNNING,		/* running */
3181166Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* shutting_down */
3191166Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* empty */
3201166Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* down */
3211166Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* dying */
3221166Sdstaff 	ZONE_EVENT_UNINITIALIZED,	/* dead */
3231166Sdstaff };
3241166Sdstaff 
3250Sstevel@tonic-gate /*
3260Sstevel@tonic-gate  * This isn't static so lint doesn't complain.
3270Sstevel@tonic-gate  */
3280Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares;
3292768Ssl108498 rctl_hndl_t rc_zone_locked_mem;
3303247Sgjelinek rctl_hndl_t rc_zone_max_swap;
3310Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps;
3322677Sml93401 rctl_hndl_t rc_zone_shmmax;
3332677Sml93401 rctl_hndl_t rc_zone_shmmni;
3342677Sml93401 rctl_hndl_t rc_zone_semmni;
3352677Sml93401 rctl_hndl_t rc_zone_msgmni;
3360Sstevel@tonic-gate /*
3370Sstevel@tonic-gate  * Synchronization primitives used to synchronize between mounts and zone
3380Sstevel@tonic-gate  * creation/destruction.
3390Sstevel@tonic-gate  */
3400Sstevel@tonic-gate static int mounts_in_progress;
3410Sstevel@tonic-gate static kcondvar_t mount_cv;
3420Sstevel@tonic-gate static kmutex_t mount_lock;
3430Sstevel@tonic-gate 
3442267Sdp const char * const zone_default_initname = "/sbin/init";
3451676Sjpk static char * const zone_prefix = "/zone/";
3460Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid);
3473448Sdh155122 static int zone_add_datalink(zoneid_t, char *);
3483448Sdh155122 static int zone_remove_datalink(zoneid_t, char *);
3493448Sdh155122 static int zone_check_datalink(zoneid_t *, char *);
3503448Sdh155122 static int zone_list_datalink(zoneid_t, int *, char *);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate /*
353813Sdp  * Bump this number when you alter the zone syscall interfaces; this is
354813Sdp  * because we need to have support for previous API versions in libc
355813Sdp  * to support patching; libc calls into the kernel to determine this number.
356813Sdp  *
357813Sdp  * Version 1 of the API is the version originally shipped with Solaris 10
358813Sdp  * Version 2 alters the zone_create system call in order to support more
359813Sdp  *     arguments by moving the args into a structure; and to do better
360813Sdp  *     error reporting when zone_create() fails.
361813Sdp  * Version 3 alters the zone_create system call in order to support the
362813Sdp  *     import of ZFS datasets to zones.
3631676Sjpk  * Version 4 alters the zone_create system call in order to support
3641676Sjpk  *     Trusted Extensions.
3652267Sdp  * Version 5 alters the zone_boot system call, and converts its old
3662267Sdp  *     bootargs parameter to be set by the zone_setattr API instead.
3673448Sdh155122  * Version 6 adds the flag argument to zone_create.
368813Sdp  */
3693448Sdh155122 static const int ZONE_SYSCALL_API_VERSION = 6;
370813Sdp 
371813Sdp /*
3720Sstevel@tonic-gate  * Certain filesystems (such as NFS and autofs) need to know which zone
3730Sstevel@tonic-gate  * the mount is being placed in.  Because of this, we need to be able to
3740Sstevel@tonic-gate  * ensure that a zone isn't in the process of being created such that
3750Sstevel@tonic-gate  * nfs_mount() thinks it is in the global zone, while by the time it
3760Sstevel@tonic-gate  * gets added the list of mounted zones, it ends up on zoneA's mount
3770Sstevel@tonic-gate  * list.
3780Sstevel@tonic-gate  *
3790Sstevel@tonic-gate  * The following functions: block_mounts()/resume_mounts() and
3800Sstevel@tonic-gate  * mount_in_progress()/mount_completed() are used by zones and the VFS
3810Sstevel@tonic-gate  * layer (respectively) to synchronize zone creation and new mounts.
3820Sstevel@tonic-gate  *
3830Sstevel@tonic-gate  * The semantics are like a reader-reader lock such that there may
3840Sstevel@tonic-gate  * either be multiple mounts (or zone creations, if that weren't
3850Sstevel@tonic-gate  * serialized by zonehash_lock) in progress at the same time, but not
3860Sstevel@tonic-gate  * both.
3870Sstevel@tonic-gate  *
3880Sstevel@tonic-gate  * We use cv's so the user can ctrl-C out of the operation if it's
3890Sstevel@tonic-gate  * taking too long.
3900Sstevel@tonic-gate  *
3910Sstevel@tonic-gate  * The semantics are such that there is unfair bias towards the
3920Sstevel@tonic-gate  * "current" operation.  This means that zone creations may starve if
3930Sstevel@tonic-gate  * there is a rapid succession of new mounts coming in to the system, or
3940Sstevel@tonic-gate  * there is a remote possibility that zones will be created at such a
3950Sstevel@tonic-gate  * rate that new mounts will not be able to proceed.
3960Sstevel@tonic-gate  */
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate  * Prevent new mounts from progressing to the point of calling
3990Sstevel@tonic-gate  * VFS_MOUNT().  If there are already mounts in this "region", wait for
4000Sstevel@tonic-gate  * them to complete.
4010Sstevel@tonic-gate  */
4020Sstevel@tonic-gate static int
4030Sstevel@tonic-gate block_mounts(void)
4040Sstevel@tonic-gate {
4050Sstevel@tonic-gate 	int retval = 0;
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	/*
4080Sstevel@tonic-gate 	 * Since it may block for a long time, block_mounts() shouldn't be
4090Sstevel@tonic-gate 	 * called with zonehash_lock held.
4100Sstevel@tonic-gate 	 */
4110Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
4120Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4130Sstevel@tonic-gate 	while (mounts_in_progress > 0) {
4140Sstevel@tonic-gate 		if (cv_wait_sig(&mount_cv, &mount_lock) == 0)
4150Sstevel@tonic-gate 			goto signaled;
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 	/*
4180Sstevel@tonic-gate 	 * A negative value of mounts_in_progress indicates that mounts
4190Sstevel@tonic-gate 	 * have been blocked by (-mounts_in_progress) different callers.
4200Sstevel@tonic-gate 	 */
4210Sstevel@tonic-gate 	mounts_in_progress--;
4220Sstevel@tonic-gate 	retval = 1;
4230Sstevel@tonic-gate signaled:
4240Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4250Sstevel@tonic-gate 	return (retval);
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate /*
4290Sstevel@tonic-gate  * The VFS layer may progress with new mounts as far as we're concerned.
4300Sstevel@tonic-gate  * Allow them to progress if we were the last obstacle.
4310Sstevel@tonic-gate  */
4320Sstevel@tonic-gate static void
4330Sstevel@tonic-gate resume_mounts(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  * The VFS layer is busy with a mount; zones should wait until all
4430Sstevel@tonic-gate  * mounts are completed to progress.
4440Sstevel@tonic-gate  */
4450Sstevel@tonic-gate void
4460Sstevel@tonic-gate mount_in_progress(void)
4470Sstevel@tonic-gate {
4480Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4490Sstevel@tonic-gate 	while (mounts_in_progress < 0)
4500Sstevel@tonic-gate 		cv_wait(&mount_cv, &mount_lock);
4510Sstevel@tonic-gate 	mounts_in_progress++;
4520Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate /*
4560Sstevel@tonic-gate  * VFS is done with one mount; wake up any waiting block_mounts()
4570Sstevel@tonic-gate  * callers if this is the last mount.
4580Sstevel@tonic-gate  */
4590Sstevel@tonic-gate void
4600Sstevel@tonic-gate mount_completed(void)
4610Sstevel@tonic-gate {
4620Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4630Sstevel@tonic-gate 	if (--mounts_in_progress == 0)
4640Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
4650Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate /*
4690Sstevel@tonic-gate  * ZSD routines.
4700Sstevel@tonic-gate  *
4710Sstevel@tonic-gate  * Zone Specific Data (ZSD) is modeled after Thread Specific Data as
4720Sstevel@tonic-gate  * defined by the pthread_key_create() and related interfaces.
4730Sstevel@tonic-gate  *
4740Sstevel@tonic-gate  * Kernel subsystems may register one or more data items and/or
4750Sstevel@tonic-gate  * callbacks to be executed when a zone is created, shutdown, or
4760Sstevel@tonic-gate  * destroyed.
4770Sstevel@tonic-gate  *
4780Sstevel@tonic-gate  * Unlike the thread counterpart, destructor callbacks will be executed
4790Sstevel@tonic-gate  * even if the data pointer is NULL and/or there are no constructor
4800Sstevel@tonic-gate  * callbacks, so it is the responsibility of such callbacks to check for
4810Sstevel@tonic-gate  * NULL data values if necessary.
4820Sstevel@tonic-gate  *
4830Sstevel@tonic-gate  * The locking strategy and overall picture is as follows:
4840Sstevel@tonic-gate  *
4850Sstevel@tonic-gate  * When someone calls zone_key_create(), a template ZSD entry is added to the
4860Sstevel@tonic-gate  * global list "zsd_registered_keys", protected by zsd_key_lock.  The
4870Sstevel@tonic-gate  * constructor callback is called immediately on all existing zones, and a
4880Sstevel@tonic-gate  * copy of the ZSD entry added to the per-zone zone_zsd list (protected by
4890Sstevel@tonic-gate  * zone_lock).  As this operation requires the list of zones, the list of
4900Sstevel@tonic-gate  * registered keys, and the per-zone list of ZSD entries to remain constant
4910Sstevel@tonic-gate  * throughout the entire operation, it must grab zonehash_lock, zone_lock for
4920Sstevel@tonic-gate  * all existing zones, and zsd_key_lock, in that order.  Similar locking is
4930Sstevel@tonic-gate  * needed when zone_key_delete() is called.  It is thus sufficient to hold
4940Sstevel@tonic-gate  * zsd_key_lock *or* zone_lock to prevent additions to or removals from the
4950Sstevel@tonic-gate  * per-zone zone_zsd list.
4960Sstevel@tonic-gate  *
4970Sstevel@tonic-gate  * Note that this implementation does not make a copy of the ZSD entry if a
4980Sstevel@tonic-gate  * constructor callback is not provided.  A zone_getspecific() on such an
4990Sstevel@tonic-gate  * uninitialized ZSD entry will return NULL.
5000Sstevel@tonic-gate  *
5010Sstevel@tonic-gate  * When new zones are created constructor callbacks for all registered ZSD
5020Sstevel@tonic-gate  * entries will be called.
5030Sstevel@tonic-gate  *
5040Sstevel@tonic-gate  * The framework does not provide any locking around zone_getspecific() and
5050Sstevel@tonic-gate  * zone_setspecific() apart from that needed for internal consistency, so
5060Sstevel@tonic-gate  * callers interested in atomic "test-and-set" semantics will need to provide
5070Sstevel@tonic-gate  * their own locking.
5080Sstevel@tonic-gate  */
5090Sstevel@tonic-gate void
5100Sstevel@tonic-gate zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t),
5110Sstevel@tonic-gate     void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *))
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate 	struct zsd_entry *zsdp;
5140Sstevel@tonic-gate 	struct zsd_entry *t;
5150Sstevel@tonic-gate 	struct zone *zone;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	zsdp = kmem_alloc(sizeof (*zsdp), KM_SLEEP);
5180Sstevel@tonic-gate 	zsdp->zsd_data = NULL;
5190Sstevel@tonic-gate 	zsdp->zsd_create = create;
5200Sstevel@tonic-gate 	zsdp->zsd_shutdown = shutdown;
5210Sstevel@tonic-gate 	zsdp->zsd_destroy = destroy;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);	/* stop the world */
5240Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
5250Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
5260Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);	/* lock all zones */
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
5290Sstevel@tonic-gate 	*keyp = zsdp->zsd_key = ++zsd_keyval;
5300Sstevel@tonic-gate 	ASSERT(zsd_keyval != 0);
5310Sstevel@tonic-gate 	list_insert_tail(&zsd_registered_keys, zsdp);
5320Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (create != NULL) {
5350Sstevel@tonic-gate 		for (zone = list_head(&zone_active); zone != NULL;
5360Sstevel@tonic-gate 		    zone = list_next(&zone_active, zone)) {
5370Sstevel@tonic-gate 			t = kmem_alloc(sizeof (*t), KM_SLEEP);
5380Sstevel@tonic-gate 			t->zsd_key = *keyp;
5390Sstevel@tonic-gate 			t->zsd_data = (*create)(zone->zone_id);
5400Sstevel@tonic-gate 			t->zsd_create = create;
5410Sstevel@tonic-gate 			t->zsd_shutdown = shutdown;
5420Sstevel@tonic-gate 			t->zsd_destroy = destroy;
5430Sstevel@tonic-gate 			list_insert_tail(&zone->zone_zsd, t);
5440Sstevel@tonic-gate 		}
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
5470Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
5480Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
5490Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate /*
5530Sstevel@tonic-gate  * Helper function to find the zsd_entry associated with the key in the
5540Sstevel@tonic-gate  * given list.
5550Sstevel@tonic-gate  */
5560Sstevel@tonic-gate static struct zsd_entry *
5570Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key)
5580Sstevel@tonic-gate {
5590Sstevel@tonic-gate 	struct zsd_entry *zsd;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) {
5620Sstevel@tonic-gate 		if (zsd->zsd_key == key) {
5630Sstevel@tonic-gate 			/*
5640Sstevel@tonic-gate 			 * Move to head of list to keep list in MRU order.
5650Sstevel@tonic-gate 			 */
5660Sstevel@tonic-gate 			if (zsd != list_head(l)) {
5670Sstevel@tonic-gate 				list_remove(l, zsd);
5680Sstevel@tonic-gate 				list_insert_head(l, zsd);
5690Sstevel@tonic-gate 			}
5700Sstevel@tonic-gate 			return (zsd);
5710Sstevel@tonic-gate 		}
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate 	return (NULL);
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate /*
5770Sstevel@tonic-gate  * Function called when a module is being unloaded, or otherwise wishes
5780Sstevel@tonic-gate  * to unregister its ZSD key and callbacks.
5790Sstevel@tonic-gate  */
5800Sstevel@tonic-gate int
5810Sstevel@tonic-gate zone_key_delete(zone_key_t key)
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate 	struct zsd_entry *zsdp = NULL;
5840Sstevel@tonic-gate 	zone_t *zone;
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);	/* Zone create/delete waits for us */
5870Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
5880Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
5890Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);	/* lock all zones */
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
5920Sstevel@tonic-gate 	zsdp = zsd_find(&zsd_registered_keys, key);
5930Sstevel@tonic-gate 	if (zsdp == NULL)
5940Sstevel@tonic-gate 		goto notfound;
5950Sstevel@tonic-gate 	list_remove(&zsd_registered_keys, zsdp);
5960Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
5990Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
6000Sstevel@tonic-gate 		struct zsd_entry *del;
6010Sstevel@tonic-gate 		void *data;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 		if (!(zone->zone_flags & ZF_DESTROYED)) {
6040Sstevel@tonic-gate 			del = zsd_find(&zone->zone_zsd, key);
6050Sstevel@tonic-gate 			if (del != NULL) {
6060Sstevel@tonic-gate 				data = del->zsd_data;
6070Sstevel@tonic-gate 				ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown);
6080Sstevel@tonic-gate 				ASSERT(del->zsd_destroy == zsdp->zsd_destroy);
6090Sstevel@tonic-gate 				list_remove(&zone->zone_zsd, del);
6100Sstevel@tonic-gate 				kmem_free(del, sizeof (*del));
6110Sstevel@tonic-gate 			} else {
6120Sstevel@tonic-gate 				data = NULL;
6130Sstevel@tonic-gate 			}
6140Sstevel@tonic-gate 			if (zsdp->zsd_shutdown)
6150Sstevel@tonic-gate 				zsdp->zsd_shutdown(zone->zone_id, data);
6160Sstevel@tonic-gate 			if (zsdp->zsd_destroy)
6170Sstevel@tonic-gate 				zsdp->zsd_destroy(zone->zone_id, data);
6180Sstevel@tonic-gate 		}
6190Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
6220Sstevel@tonic-gate 	kmem_free(zsdp, sizeof (*zsdp));
6230Sstevel@tonic-gate 	return (0);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate notfound:
6260Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
6270Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
6280Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
6290Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
6300Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
6310Sstevel@tonic-gate 	return (-1);
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate /*
6350Sstevel@tonic-gate  * ZSD counterpart of pthread_setspecific().
6360Sstevel@tonic-gate  */
6370Sstevel@tonic-gate int
6380Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data)
6390Sstevel@tonic-gate {
6400Sstevel@tonic-gate 	struct zsd_entry *t;
6410Sstevel@tonic-gate 	struct zsd_entry *zsdp = NULL;
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
6440Sstevel@tonic-gate 	t = zsd_find(&zone->zone_zsd, key);
6450Sstevel@tonic-gate 	if (t != NULL) {
6460Sstevel@tonic-gate 		/*
6470Sstevel@tonic-gate 		 * Replace old value with new
6480Sstevel@tonic-gate 		 */
6490Sstevel@tonic-gate 		t->zsd_data = (void *)data;
6500Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
6510Sstevel@tonic-gate 		return (0);
6520Sstevel@tonic-gate 	}
6530Sstevel@tonic-gate 	/*
6540Sstevel@tonic-gate 	 * If there was no previous value, go through the list of registered
6550Sstevel@tonic-gate 	 * keys.
6560Sstevel@tonic-gate 	 *
6570Sstevel@tonic-gate 	 * We avoid grabbing zsd_key_lock until we are sure we need it; this is
6580Sstevel@tonic-gate 	 * necessary for shutdown callbacks to be able to execute without fear
6590Sstevel@tonic-gate 	 * of deadlock.
6600Sstevel@tonic-gate 	 */
6610Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
6620Sstevel@tonic-gate 	zsdp = zsd_find(&zsd_registered_keys, key);
6630Sstevel@tonic-gate 	if (zsdp == NULL) { 	/* Key was not registered */
6640Sstevel@tonic-gate 		mutex_exit(&zsd_key_lock);
6650Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
6660Sstevel@tonic-gate 		return (-1);
6670Sstevel@tonic-gate 	}
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	/*
6700Sstevel@tonic-gate 	 * Add a zsd_entry to this zone, using the template we just retrieved
6710Sstevel@tonic-gate 	 * to initialize the constructor and destructor(s).
6720Sstevel@tonic-gate 	 */
6730Sstevel@tonic-gate 	t = kmem_alloc(sizeof (*t), KM_SLEEP);
6740Sstevel@tonic-gate 	t->zsd_key = key;
6750Sstevel@tonic-gate 	t->zsd_data = (void *)data;
6760Sstevel@tonic-gate 	t->zsd_create = zsdp->zsd_create;
6770Sstevel@tonic-gate 	t->zsd_shutdown = zsdp->zsd_shutdown;
6780Sstevel@tonic-gate 	t->zsd_destroy = zsdp->zsd_destroy;
6790Sstevel@tonic-gate 	list_insert_tail(&zone->zone_zsd, t);
6800Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
6810Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
6820Sstevel@tonic-gate 	return (0);
6830Sstevel@tonic-gate }
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate /*
6860Sstevel@tonic-gate  * ZSD counterpart of pthread_getspecific().
6870Sstevel@tonic-gate  */
6880Sstevel@tonic-gate void *
6890Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone)
6900Sstevel@tonic-gate {
6910Sstevel@tonic-gate 	struct zsd_entry *t;
6920Sstevel@tonic-gate 	void *data;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
6950Sstevel@tonic-gate 	t = zsd_find(&zone->zone_zsd, key);
6960Sstevel@tonic-gate 	data = (t == NULL ? NULL : t->zsd_data);
6970Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
6980Sstevel@tonic-gate 	return (data);
6990Sstevel@tonic-gate }
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate /*
7020Sstevel@tonic-gate  * Function used to initialize a zone's list of ZSD callbacks and data
7030Sstevel@tonic-gate  * when the zone is being created.  The callbacks are initialized from
7040Sstevel@tonic-gate  * the template list (zsd_registered_keys), and the constructor
7050Sstevel@tonic-gate  * callback executed (if one exists).
7060Sstevel@tonic-gate  *
7070Sstevel@tonic-gate  * This is called before the zone is made publicly available, hence no
7080Sstevel@tonic-gate  * need to grab zone_lock.
7090Sstevel@tonic-gate  *
7100Sstevel@tonic-gate  * Although we grab and release zsd_key_lock, new entries cannot be
7110Sstevel@tonic-gate  * added to or removed from the zsd_registered_keys list until we
7120Sstevel@tonic-gate  * release zonehash_lock, so there isn't a window for a
7130Sstevel@tonic-gate  * zone_key_create() to come in after we've dropped zsd_key_lock but
7140Sstevel@tonic-gate  * before the zone is added to the zone list, such that the constructor
7150Sstevel@tonic-gate  * callbacks aren't executed for the new zone.
7160Sstevel@tonic-gate  */
7170Sstevel@tonic-gate static void
7180Sstevel@tonic-gate zone_zsd_configure(zone_t *zone)
7190Sstevel@tonic-gate {
7200Sstevel@tonic-gate 	struct zsd_entry *zsdp;
7210Sstevel@tonic-gate 	struct zsd_entry *t;
7220Sstevel@tonic-gate 	zoneid_t zoneid = zone->zone_id;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
7250Sstevel@tonic-gate 	ASSERT(list_head(&zone->zone_zsd) == NULL);
7260Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
7270Sstevel@tonic-gate 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
7280Sstevel@tonic-gate 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
7290Sstevel@tonic-gate 		if (zsdp->zsd_create != NULL) {
7300Sstevel@tonic-gate 			t = kmem_alloc(sizeof (*t), KM_SLEEP);
7310Sstevel@tonic-gate 			t->zsd_key = zsdp->zsd_key;
7320Sstevel@tonic-gate 			t->zsd_create = zsdp->zsd_create;
7330Sstevel@tonic-gate 			t->zsd_data = (*t->zsd_create)(zoneid);
7340Sstevel@tonic-gate 			t->zsd_shutdown = zsdp->zsd_shutdown;
7350Sstevel@tonic-gate 			t->zsd_destroy = zsdp->zsd_destroy;
7360Sstevel@tonic-gate 			list_insert_tail(&zone->zone_zsd, t);
7370Sstevel@tonic-gate 		}
7380Sstevel@tonic-gate 	}
7390Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY };
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate /*
7450Sstevel@tonic-gate  * Helper function to execute shutdown or destructor callbacks.
7460Sstevel@tonic-gate  */
7470Sstevel@tonic-gate static void
7480Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct)
7490Sstevel@tonic-gate {
7500Sstevel@tonic-gate 	struct zsd_entry *zsdp;
7510Sstevel@tonic-gate 	struct zsd_entry *t;
7520Sstevel@tonic-gate 	zoneid_t zoneid = zone->zone_id;
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY);
7550Sstevel@tonic-gate 	ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY);
7560Sstevel@tonic-gate 	ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN);
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
7590Sstevel@tonic-gate 	if (ct == ZSD_DESTROY) {
7600Sstevel@tonic-gate 		if (zone->zone_flags & ZF_DESTROYED) {
7610Sstevel@tonic-gate 			/*
7620Sstevel@tonic-gate 			 * Make sure destructors are only called once.
7630Sstevel@tonic-gate 			 */
7640Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
7650Sstevel@tonic-gate 			return;
7660Sstevel@tonic-gate 		}
7670Sstevel@tonic-gate 		zone->zone_flags |= ZF_DESTROYED;
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	/*
7720Sstevel@tonic-gate 	 * Both zsd_key_lock and zone_lock need to be held in order to add or
7730Sstevel@tonic-gate 	 * remove a ZSD key, (either globally as part of
7740Sstevel@tonic-gate 	 * zone_key_create()/zone_key_delete(), or on a per-zone basis, as is
7750Sstevel@tonic-gate 	 * possible through zone_setspecific()), so it's sufficient to hold
7760Sstevel@tonic-gate 	 * zsd_key_lock here.
7770Sstevel@tonic-gate 	 *
7780Sstevel@tonic-gate 	 * This is a good thing, since we don't want to recursively try to grab
7790Sstevel@tonic-gate 	 * zone_lock if a callback attempts to do something like a crfree() or
7800Sstevel@tonic-gate 	 * zone_rele().
7810Sstevel@tonic-gate 	 */
7820Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
7830Sstevel@tonic-gate 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
7840Sstevel@tonic-gate 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
7850Sstevel@tonic-gate 		zone_key_t key = zsdp->zsd_key;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 		/* Skip if no callbacks registered */
7880Sstevel@tonic-gate 		if (ct == ZSD_SHUTDOWN && zsdp->zsd_shutdown == NULL)
7890Sstevel@tonic-gate 			continue;
7900Sstevel@tonic-gate 		if (ct == ZSD_DESTROY && zsdp->zsd_destroy == NULL)
7910Sstevel@tonic-gate 			continue;
7920Sstevel@tonic-gate 		/*
7930Sstevel@tonic-gate 		 * Call the callback with the zone-specific data if we can find
7940Sstevel@tonic-gate 		 * any, otherwise with NULL.
7950Sstevel@tonic-gate 		 */
7960Sstevel@tonic-gate 		t = zsd_find(&zone->zone_zsd, key);
7970Sstevel@tonic-gate 		if (t != NULL) {
7980Sstevel@tonic-gate 			if (ct == ZSD_SHUTDOWN) {
7990Sstevel@tonic-gate 				t->zsd_shutdown(zoneid, t->zsd_data);
8000Sstevel@tonic-gate 			} else {
8010Sstevel@tonic-gate 				ASSERT(ct == ZSD_DESTROY);
8020Sstevel@tonic-gate 				t->zsd_destroy(zoneid, t->zsd_data);
8030Sstevel@tonic-gate 			}
8040Sstevel@tonic-gate 		} else {
8050Sstevel@tonic-gate 			if (ct == ZSD_SHUTDOWN) {
8060Sstevel@tonic-gate 				zsdp->zsd_shutdown(zoneid, NULL);
8070Sstevel@tonic-gate 			} else {
8080Sstevel@tonic-gate 				ASSERT(ct == ZSD_DESTROY);
8090Sstevel@tonic-gate 				zsdp->zsd_destroy(zoneid, NULL);
8100Sstevel@tonic-gate 			}
8110Sstevel@tonic-gate 		}
8120Sstevel@tonic-gate 	}
8130Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate /*
8170Sstevel@tonic-gate  * Called when the zone is going away; free ZSD-related memory, and
8180Sstevel@tonic-gate  * destroy the zone_zsd list.
8190Sstevel@tonic-gate  */
8200Sstevel@tonic-gate static void
8210Sstevel@tonic-gate zone_free_zsd(zone_t *zone)
8220Sstevel@tonic-gate {
8230Sstevel@tonic-gate 	struct zsd_entry *t, *next;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	/*
8260Sstevel@tonic-gate 	 * Free all the zsd_entry's we had on this zone.
8270Sstevel@tonic-gate 	 */
8280Sstevel@tonic-gate 	for (t = list_head(&zone->zone_zsd); t != NULL; t = next) {
8290Sstevel@tonic-gate 		next = list_next(&zone->zone_zsd, t);
8300Sstevel@tonic-gate 		list_remove(&zone->zone_zsd, t);
8310Sstevel@tonic-gate 		kmem_free(t, sizeof (*t));
8320Sstevel@tonic-gate 	}
8330Sstevel@tonic-gate 	list_destroy(&zone->zone_zsd);
8340Sstevel@tonic-gate }
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate /*
837789Sahrens  * Frees memory associated with the zone dataset list.
838789Sahrens  */
839789Sahrens static void
840789Sahrens zone_free_datasets(zone_t *zone)
841789Sahrens {
842789Sahrens 	zone_dataset_t *t, *next;
843789Sahrens 
844789Sahrens 	for (t = list_head(&zone->zone_datasets); t != NULL; t = next) {
845789Sahrens 		next = list_next(&zone->zone_datasets, t);
846789Sahrens 		list_remove(&zone->zone_datasets, t);
847789Sahrens 		kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1);
848789Sahrens 		kmem_free(t, sizeof (*t));
849789Sahrens 	}
850789Sahrens 	list_destroy(&zone->zone_datasets);
851789Sahrens }
852789Sahrens 
853789Sahrens /*
8540Sstevel@tonic-gate  * zone.cpu-shares resource control support.
8550Sstevel@tonic-gate  */
8560Sstevel@tonic-gate /*ARGSUSED*/
8570Sstevel@tonic-gate static rctl_qty_t
8580Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p)
8590Sstevel@tonic-gate {
8600Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8610Sstevel@tonic-gate 	return (p->p_zone->zone_shares);
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate /*ARGSUSED*/
8650Sstevel@tonic-gate static int
8660Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
8670Sstevel@tonic-gate     rctl_qty_t nv)
8680Sstevel@tonic-gate {
8690Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8700Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
8710Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
8720Sstevel@tonic-gate 		return (0);
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	e->rcep_p.zone->zone_shares = nv;
8750Sstevel@tonic-gate 	return (0);
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = {
8790Sstevel@tonic-gate 	rcop_no_action,
8800Sstevel@tonic-gate 	zone_cpu_shares_usage,
8810Sstevel@tonic-gate 	zone_cpu_shares_set,
8820Sstevel@tonic-gate 	rcop_no_test
8830Sstevel@tonic-gate };
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate /*ARGSUSED*/
8860Sstevel@tonic-gate static rctl_qty_t
8870Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p)
8880Sstevel@tonic-gate {
8890Sstevel@tonic-gate 	rctl_qty_t nlwps;
8900Sstevel@tonic-gate 	zone_t *zone = p->p_zone;
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
8950Sstevel@tonic-gate 	nlwps = zone->zone_nlwps;
8960Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	return (nlwps);
8990Sstevel@tonic-gate }
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate /*ARGSUSED*/
9020Sstevel@tonic-gate static int
9030Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
9040Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
9050Sstevel@tonic-gate {
9060Sstevel@tonic-gate 	rctl_qty_t nlwps;
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
9090Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
9100Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
9110Sstevel@tonic-gate 		return (0);
9120Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock)));
9130Sstevel@tonic-gate 	nlwps = e->rcep_p.zone->zone_nlwps;
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	if (nlwps + incr > rcntl->rcv_value)
9160Sstevel@tonic-gate 		return (1);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	return (0);
9190Sstevel@tonic-gate }
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate /*ARGSUSED*/
9220Sstevel@tonic-gate static int
9232768Ssl108498 zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv)
9242768Ssl108498 {
9250Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
9260Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
9270Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
9280Sstevel@tonic-gate 		return (0);
9290Sstevel@tonic-gate 	e->rcep_p.zone->zone_nlwps_ctl = nv;
9300Sstevel@tonic-gate 	return (0);
9310Sstevel@tonic-gate }
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = {
9340Sstevel@tonic-gate 	rcop_no_action,
9350Sstevel@tonic-gate 	zone_lwps_usage,
9360Sstevel@tonic-gate 	zone_lwps_set,
9370Sstevel@tonic-gate 	zone_lwps_test,
9380Sstevel@tonic-gate };
9390Sstevel@tonic-gate 
9402677Sml93401 /*ARGSUSED*/
9412677Sml93401 static int
9422677Sml93401 zone_shmmax_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
9432677Sml93401     rctl_qty_t incr, uint_t flags)
9442677Sml93401 {
9452677Sml93401 	rctl_qty_t v;
9462677Sml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
9472677Sml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
9482677Sml93401 	v = e->rcep_p.zone->zone_shmmax + incr;
9492677Sml93401 	if (v > rval->rcv_value)
9502677Sml93401 		return (1);
9512677Sml93401 	return (0);
9522677Sml93401 }
9532677Sml93401 
9542677Sml93401 static rctl_ops_t zone_shmmax_ops = {
9552677Sml93401 	rcop_no_action,
9562677Sml93401 	rcop_no_usage,
9572677Sml93401 	rcop_no_set,
9582677Sml93401 	zone_shmmax_test
9592677Sml93401 };
9602677Sml93401 
9612677Sml93401 /*ARGSUSED*/
9622677Sml93401 static int
9632677Sml93401 zone_shmmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
9642677Sml93401     rctl_qty_t incr, uint_t flags)
9652677Sml93401 {
9662677Sml93401 	rctl_qty_t v;
9672677Sml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
9682677Sml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
9692677Sml93401 	v = e->rcep_p.zone->zone_ipc.ipcq_shmmni + incr;
9702677Sml93401 	if (v > rval->rcv_value)
9712677Sml93401 		return (1);
9722677Sml93401 	return (0);
9732677Sml93401 }
9742677Sml93401 
9752677Sml93401 static rctl_ops_t zone_shmmni_ops = {
9762677Sml93401 	rcop_no_action,
9772677Sml93401 	rcop_no_usage,
9782677Sml93401 	rcop_no_set,
9792677Sml93401 	zone_shmmni_test
9802677Sml93401 };
9812677Sml93401 
9822677Sml93401 /*ARGSUSED*/
9832677Sml93401 static int
9842677Sml93401 zone_semmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
9852677Sml93401     rctl_qty_t incr, uint_t flags)
9862677Sml93401 {
9872677Sml93401 	rctl_qty_t v;
9882677Sml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
9892677Sml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
9902677Sml93401 	v = e->rcep_p.zone->zone_ipc.ipcq_semmni + incr;
9912677Sml93401 	if (v > rval->rcv_value)
9922677Sml93401 		return (1);
9932677Sml93401 	return (0);
9942677Sml93401 }
9952677Sml93401 
9962677Sml93401 static rctl_ops_t zone_semmni_ops = {
9972677Sml93401 	rcop_no_action,
9982677Sml93401 	rcop_no_usage,
9992677Sml93401 	rcop_no_set,
10002677Sml93401 	zone_semmni_test
10012677Sml93401 };
10022677Sml93401 
10032677Sml93401 /*ARGSUSED*/
10042677Sml93401 static int
10052677Sml93401 zone_msgmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
10062677Sml93401     rctl_qty_t incr, uint_t flags)
10072677Sml93401 {
10082677Sml93401 	rctl_qty_t v;
10092677Sml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
10102677Sml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
10112677Sml93401 	v = e->rcep_p.zone->zone_ipc.ipcq_msgmni + incr;
10122677Sml93401 	if (v > rval->rcv_value)
10132677Sml93401 		return (1);
10142677Sml93401 	return (0);
10152677Sml93401 }
10162677Sml93401 
10172677Sml93401 static rctl_ops_t zone_msgmni_ops = {
10182677Sml93401 	rcop_no_action,
10192677Sml93401 	rcop_no_usage,
10202677Sml93401 	rcop_no_set,
10212677Sml93401 	zone_msgmni_test
10222677Sml93401 };
10232677Sml93401 
10242768Ssl108498 /*ARGSUSED*/
10252768Ssl108498 static rctl_qty_t
10262768Ssl108498 zone_locked_mem_usage(rctl_t *rctl, struct proc *p)
10272768Ssl108498 {
10282768Ssl108498 	rctl_qty_t q;
10292768Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
10303247Sgjelinek 	mutex_enter(&p->p_zone->zone_mem_lock);
10312768Ssl108498 	q = p->p_zone->zone_locked_mem;
10323247Sgjelinek 	mutex_exit(&p->p_zone->zone_mem_lock);
10332768Ssl108498 	return (q);
10342768Ssl108498 }
10352768Ssl108498 
10362768Ssl108498 /*ARGSUSED*/
10372768Ssl108498 static int
10382768Ssl108498 zone_locked_mem_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
10392768Ssl108498     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
10402768Ssl108498 {
10412768Ssl108498 	rctl_qty_t q;
10423247Sgjelinek 	zone_t *z;
10433247Sgjelinek 
10443247Sgjelinek 	z = e->rcep_p.zone;
10452768Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
10463247Sgjelinek 	ASSERT(MUTEX_HELD(&z->zone_mem_lock));
10473247Sgjelinek 	q = z->zone_locked_mem;
10482768Ssl108498 	if (q + incr > rcntl->rcv_value)
10492768Ssl108498 		return (1);
10502768Ssl108498 	return (0);
10512768Ssl108498 }
10522768Ssl108498 
10532768Ssl108498 /*ARGSUSED*/
10542768Ssl108498 static int
10552768Ssl108498 zone_locked_mem_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
10562768Ssl108498     rctl_qty_t nv)
10572768Ssl108498 {
10582768Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
10592768Ssl108498 	ASSERT(e->rcep_t == RCENTITY_ZONE);
10602768Ssl108498 	if (e->rcep_p.zone == NULL)
10612768Ssl108498 		return (0);
10622768Ssl108498 	e->rcep_p.zone->zone_locked_mem_ctl = nv;
10632768Ssl108498 	return (0);
10642768Ssl108498 }
10652768Ssl108498 
10662768Ssl108498 static rctl_ops_t zone_locked_mem_ops = {
10672768Ssl108498 	rcop_no_action,
10682768Ssl108498 	zone_locked_mem_usage,
10692768Ssl108498 	zone_locked_mem_set,
10702768Ssl108498 	zone_locked_mem_test
10712768Ssl108498 };
10722677Sml93401 
10733247Sgjelinek /*ARGSUSED*/
10743247Sgjelinek static rctl_qty_t
10753247Sgjelinek zone_max_swap_usage(rctl_t *rctl, struct proc *p)
10763247Sgjelinek {
10773247Sgjelinek 	rctl_qty_t q;
10783247Sgjelinek 	zone_t *z = p->p_zone;
10793247Sgjelinek 
10803247Sgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
10813247Sgjelinek 	mutex_enter(&z->zone_mem_lock);
10823247Sgjelinek 	q = z->zone_max_swap;
10833247Sgjelinek 	mutex_exit(&z->zone_mem_lock);
10843247Sgjelinek 	return (q);
10853247Sgjelinek }
10863247Sgjelinek 
10873247Sgjelinek /*ARGSUSED*/
10883247Sgjelinek static int
10893247Sgjelinek zone_max_swap_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
10903247Sgjelinek     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
10913247Sgjelinek {
10923247Sgjelinek 	rctl_qty_t q;
10933247Sgjelinek 	zone_t *z;
10943247Sgjelinek 
10953247Sgjelinek 	z = e->rcep_p.zone;
10963247Sgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
10973247Sgjelinek 	ASSERT(MUTEX_HELD(&z->zone_mem_lock));
10983247Sgjelinek 	q = z->zone_max_swap;
10993247Sgjelinek 	if (q + incr > rcntl->rcv_value)
11003247Sgjelinek 		return (1);
11013247Sgjelinek 	return (0);
11023247Sgjelinek }
11033247Sgjelinek 
11043247Sgjelinek /*ARGSUSED*/
11053247Sgjelinek static int
11063247Sgjelinek zone_max_swap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
11073247Sgjelinek     rctl_qty_t nv)
11083247Sgjelinek {
11093247Sgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
11103247Sgjelinek 	ASSERT(e->rcep_t == RCENTITY_ZONE);
11113247Sgjelinek 	if (e->rcep_p.zone == NULL)
11123247Sgjelinek 		return (0);
11133247Sgjelinek 	e->rcep_p.zone->zone_max_swap_ctl = nv;
11143247Sgjelinek 	return (0);
11153247Sgjelinek }
11163247Sgjelinek 
11173247Sgjelinek static rctl_ops_t zone_max_swap_ops = {
11183247Sgjelinek 	rcop_no_action,
11193247Sgjelinek 	zone_max_swap_usage,
11203247Sgjelinek 	zone_max_swap_set,
11213247Sgjelinek 	zone_max_swap_test
11223247Sgjelinek };
11233247Sgjelinek 
11240Sstevel@tonic-gate /*
11250Sstevel@tonic-gate  * Helper function to brand the zone with a unique ID.
11260Sstevel@tonic-gate  */
11270Sstevel@tonic-gate static void
11280Sstevel@tonic-gate zone_uniqid(zone_t *zone)
11290Sstevel@tonic-gate {
11300Sstevel@tonic-gate 	static uint64_t uniqid = 0;
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
11330Sstevel@tonic-gate 	zone->zone_uniqid = uniqid++;
11340Sstevel@tonic-gate }
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate /*
11370Sstevel@tonic-gate  * Returns a held pointer to the "kcred" for the specified zone.
11380Sstevel@tonic-gate  */
11390Sstevel@tonic-gate struct cred *
11400Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid)
11410Sstevel@tonic-gate {
11420Sstevel@tonic-gate 	zone_t *zone;
11430Sstevel@tonic-gate 	cred_t *cr;
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	if ((zone = zone_find_by_id(zoneid)) == NULL)
11460Sstevel@tonic-gate 		return (NULL);
11470Sstevel@tonic-gate 	cr = zone->zone_kcred;
11480Sstevel@tonic-gate 	crhold(cr);
11490Sstevel@tonic-gate 	zone_rele(zone);
11500Sstevel@tonic-gate 	return (cr);
11510Sstevel@tonic-gate }
11520Sstevel@tonic-gate 
11533247Sgjelinek static int
11543247Sgjelinek zone_lockedmem_kstat_update(kstat_t *ksp, int rw)
11553247Sgjelinek {
11563247Sgjelinek 	zone_t *zone = ksp->ks_private;
11573247Sgjelinek 	zone_kstat_t *zk = ksp->ks_data;
11583247Sgjelinek 
11593247Sgjelinek 	if (rw == KSTAT_WRITE)
11603247Sgjelinek 		return (EACCES);
11613247Sgjelinek 
11623247Sgjelinek 	zk->zk_usage.value.ui64 = zone->zone_locked_mem;
11633247Sgjelinek 	zk->zk_value.value.ui64 = zone->zone_locked_mem_ctl;
11643247Sgjelinek 	return (0);
11653247Sgjelinek }
11663247Sgjelinek 
11673247Sgjelinek static int
11683247Sgjelinek zone_swapresv_kstat_update(kstat_t *ksp, int rw)
11693247Sgjelinek {
11703247Sgjelinek 	zone_t *zone = ksp->ks_private;
11713247Sgjelinek 	zone_kstat_t *zk = ksp->ks_data;
11723247Sgjelinek 
11733247Sgjelinek 	if (rw == KSTAT_WRITE)
11743247Sgjelinek 		return (EACCES);
11753247Sgjelinek 
11763247Sgjelinek 	zk->zk_usage.value.ui64 = zone->zone_max_swap;
11773247Sgjelinek 	zk->zk_value.value.ui64 = zone->zone_max_swap_ctl;
11783247Sgjelinek 	return (0);
11793247Sgjelinek }
11803247Sgjelinek 
11813247Sgjelinek static void
11823247Sgjelinek zone_kstat_create(zone_t *zone)
11833247Sgjelinek {
11843247Sgjelinek 	kstat_t *ksp;
11853247Sgjelinek 	zone_kstat_t *zk;
11863247Sgjelinek 
11873247Sgjelinek 	ksp = rctl_kstat_create_zone(zone, "lockedmem", KSTAT_TYPE_NAMED,
11883247Sgjelinek 	    sizeof (zone_kstat_t) / sizeof (kstat_named_t),
11893247Sgjelinek 	    KSTAT_FLAG_VIRTUAL);
11903247Sgjelinek 
11913247Sgjelinek 	if (ksp == NULL)
11923247Sgjelinek 		return;
11933247Sgjelinek 
11943247Sgjelinek 	zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP);
11953247Sgjelinek 	ksp->ks_data_size += strlen(zone->zone_name) + 1;
11963247Sgjelinek 	kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING);
11973247Sgjelinek 	kstat_named_setstr(&zk->zk_zonename, zone->zone_name);
11983247Sgjelinek 	kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64);
11993247Sgjelinek 	kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64);
12003247Sgjelinek 	ksp->ks_update = zone_lockedmem_kstat_update;
12013247Sgjelinek 	ksp->ks_private = zone;
12023247Sgjelinek 	kstat_install(ksp);
12033247Sgjelinek 
12043247Sgjelinek 	zone->zone_lockedmem_kstat = ksp;
12053247Sgjelinek 
12063247Sgjelinek 	ksp = rctl_kstat_create_zone(zone, "swapresv", KSTAT_TYPE_NAMED,
12073247Sgjelinek 	    sizeof (zone_kstat_t) / sizeof (kstat_named_t),
12083247Sgjelinek 	    KSTAT_FLAG_VIRTUAL);
12093247Sgjelinek 
12103247Sgjelinek 	if (ksp == NULL)
12113247Sgjelinek 		return;
12123247Sgjelinek 
12133247Sgjelinek 	zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP);
12143247Sgjelinek 	ksp->ks_data_size += strlen(zone->zone_name) + 1;
12153247Sgjelinek 	kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING);
12163247Sgjelinek 	kstat_named_setstr(&zk->zk_zonename, zone->zone_name);
12173247Sgjelinek 	kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64);
12183247Sgjelinek 	kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64);
12193247Sgjelinek 	ksp->ks_update = zone_swapresv_kstat_update;
12203247Sgjelinek 	ksp->ks_private = zone;
12213247Sgjelinek 	kstat_install(ksp);
12223247Sgjelinek 
12233247Sgjelinek 	zone->zone_swapresv_kstat = ksp;
12243247Sgjelinek }
12253247Sgjelinek 
12263247Sgjelinek static void
12273247Sgjelinek zone_kstat_delete(zone_t *zone)
12283247Sgjelinek {
12293247Sgjelinek 	void *data;
12303247Sgjelinek 
12313247Sgjelinek 	if (zone->zone_lockedmem_kstat != NULL) {
12323247Sgjelinek 		data = zone->zone_lockedmem_kstat->ks_data;
12333247Sgjelinek 		kstat_delete(zone->zone_lockedmem_kstat);
12343247Sgjelinek 		kmem_free(data, sizeof (zone_kstat_t));
12353247Sgjelinek 	}
12363247Sgjelinek 	if (zone->zone_swapresv_kstat != NULL) {
12373247Sgjelinek 		data = zone->zone_swapresv_kstat->ks_data;
12383247Sgjelinek 		kstat_delete(zone->zone_swapresv_kstat);
12393247Sgjelinek 		kmem_free(data, sizeof (zone_kstat_t));
12403247Sgjelinek 	}
12413247Sgjelinek }
12423247Sgjelinek 
12430Sstevel@tonic-gate /*
12440Sstevel@tonic-gate  * Called very early on in boot to initialize the ZSD list so that
12450Sstevel@tonic-gate  * zone_key_create() can be called before zone_init().  It also initializes
12460Sstevel@tonic-gate  * portions of zone0 which may be used before zone_init() is called.  The
12470Sstevel@tonic-gate  * variable "global_zone" will be set when zone0 is fully initialized by
12480Sstevel@tonic-gate  * zone_init().
12490Sstevel@tonic-gate  */
12500Sstevel@tonic-gate void
12510Sstevel@tonic-gate zone_zsd_init(void)
12520Sstevel@tonic-gate {
12530Sstevel@tonic-gate 	mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL);
12540Sstevel@tonic-gate 	mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL);
12550Sstevel@tonic-gate 	list_create(&zsd_registered_keys, sizeof (struct zsd_entry),
12560Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
12570Sstevel@tonic-gate 	list_create(&zone_active, sizeof (zone_t),
12580Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
12590Sstevel@tonic-gate 	list_create(&zone_deathrow, sizeof (zone_t),
12600Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL);
12630Sstevel@tonic-gate 	mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
12643247Sgjelinek 	mutex_init(&zone0.zone_mem_lock, NULL, MUTEX_DEFAULT, NULL);
12650Sstevel@tonic-gate 	zone0.zone_shares = 1;
12663247Sgjelinek 	zone0.zone_nlwps = 0;
12670Sstevel@tonic-gate 	zone0.zone_nlwps_ctl = INT_MAX;
12683247Sgjelinek 	zone0.zone_locked_mem = 0;
12693247Sgjelinek 	zone0.zone_locked_mem_ctl = UINT64_MAX;
12703247Sgjelinek 	ASSERT(zone0.zone_max_swap == 0);
12713247Sgjelinek 	zone0.zone_max_swap_ctl = UINT64_MAX;
12722677Sml93401 	zone0.zone_shmmax = 0;
12732677Sml93401 	zone0.zone_ipc.ipcq_shmmni = 0;
12742677Sml93401 	zone0.zone_ipc.ipcq_semmni = 0;
12752677Sml93401 	zone0.zone_ipc.ipcq_msgmni = 0;
12760Sstevel@tonic-gate 	zone0.zone_name = GLOBAL_ZONENAME;
12770Sstevel@tonic-gate 	zone0.zone_nodename = utsname.nodename;
12780Sstevel@tonic-gate 	zone0.zone_domain = srpc_domain;
12790Sstevel@tonic-gate 	zone0.zone_ref = 1;
12800Sstevel@tonic-gate 	zone0.zone_id = GLOBAL_ZONEID;
12810Sstevel@tonic-gate 	zone0.zone_status = ZONE_IS_RUNNING;
12820Sstevel@tonic-gate 	zone0.zone_rootpath = "/";
12830Sstevel@tonic-gate 	zone0.zone_rootpathlen = 2;
12840Sstevel@tonic-gate 	zone0.zone_psetid = ZONE_PS_INVAL;
12850Sstevel@tonic-gate 	zone0.zone_ncpus = 0;
12860Sstevel@tonic-gate 	zone0.zone_ncpus_online = 0;
12870Sstevel@tonic-gate 	zone0.zone_proc_initpid = 1;
12882267Sdp 	zone0.zone_initname = initname;
12893247Sgjelinek 	zone0.zone_lockedmem_kstat = NULL;
12903247Sgjelinek 	zone0.zone_swapresv_kstat = NULL;
12910Sstevel@tonic-gate 	list_create(&zone0.zone_zsd, sizeof (struct zsd_entry),
12920Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
12930Sstevel@tonic-gate 	list_insert_head(&zone_active, &zone0);
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	/*
12960Sstevel@tonic-gate 	 * The root filesystem is not mounted yet, so zone_rootvp cannot be set
12970Sstevel@tonic-gate 	 * to anything meaningful.  It is assigned to be 'rootdir' in
12980Sstevel@tonic-gate 	 * vfs_mountroot().
12990Sstevel@tonic-gate 	 */
13000Sstevel@tonic-gate 	zone0.zone_rootvp = NULL;
13010Sstevel@tonic-gate 	zone0.zone_vfslist = NULL;
13022267Sdp 	zone0.zone_bootargs = initargs;
13030Sstevel@tonic-gate 	zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
13040Sstevel@tonic-gate 	/*
13050Sstevel@tonic-gate 	 * The global zone has all privileges
13060Sstevel@tonic-gate 	 */
13070Sstevel@tonic-gate 	priv_fillset(zone0.zone_privset);
13080Sstevel@tonic-gate 	/*
13090Sstevel@tonic-gate 	 * Add p0 to the global zone
13100Sstevel@tonic-gate 	 */
13110Sstevel@tonic-gate 	zone0.zone_zsched = &p0;
13120Sstevel@tonic-gate 	p0.p_zone = &zone0;
13130Sstevel@tonic-gate }
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate /*
13161676Sjpk  * Compute a hash value based on the contents of the label and the DOI.  The
13171676Sjpk  * hash algorithm is somewhat arbitrary, but is based on the observation that
13181676Sjpk  * humans will likely pick labels that differ by amounts that work out to be
13191676Sjpk  * multiples of the number of hash chains, and thus stirring in some primes
13201676Sjpk  * should help.
13211676Sjpk  */
13221676Sjpk static uint_t
13231676Sjpk hash_bylabel(void *hdata, mod_hash_key_t key)
13241676Sjpk {
13251676Sjpk 	const ts_label_t *lab = (ts_label_t *)key;
13261676Sjpk 	const uint32_t *up, *ue;
13271676Sjpk 	uint_t hash;
13281676Sjpk 	int i;
13291676Sjpk 
13301676Sjpk 	_NOTE(ARGUNUSED(hdata));
13311676Sjpk 
13321676Sjpk 	hash = lab->tsl_doi + (lab->tsl_doi << 1);
13331676Sjpk 	/* we depend on alignment of label, but not representation */
13341676Sjpk 	up = (const uint32_t *)&lab->tsl_label;
13351676Sjpk 	ue = up + sizeof (lab->tsl_label) / sizeof (*up);
13361676Sjpk 	i = 1;
13371676Sjpk 	while (up < ue) {
13381676Sjpk 		/* using 2^n + 1, 1 <= n <= 16 as source of many primes */
13391676Sjpk 		hash += *up + (*up << ((i % 16) + 1));
13401676Sjpk 		up++;
13411676Sjpk 		i++;
13421676Sjpk 	}
13431676Sjpk 	return (hash);
13441676Sjpk }
13451676Sjpk 
13461676Sjpk /*
13471676Sjpk  * All that mod_hash cares about here is zero (equal) versus non-zero (not
13481676Sjpk  * equal).  This may need to be changed if less than / greater than is ever
13491676Sjpk  * needed.
13501676Sjpk  */
13511676Sjpk static int
13521676Sjpk hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
13531676Sjpk {
13541676Sjpk 	ts_label_t *lab1 = (ts_label_t *)key1;
13551676Sjpk 	ts_label_t *lab2 = (ts_label_t *)key2;
13561676Sjpk 
13571676Sjpk 	return (label_equal(lab1, lab2) ? 0 : 1);
13581676Sjpk }
13591676Sjpk 
13601676Sjpk /*
13610Sstevel@tonic-gate  * Called by main() to initialize the zones framework.
13620Sstevel@tonic-gate  */
13630Sstevel@tonic-gate void
13640Sstevel@tonic-gate zone_init(void)
13650Sstevel@tonic-gate {
13660Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
13670Sstevel@tonic-gate 	rctl_val_t *dval;
13680Sstevel@tonic-gate 	rctl_set_t *set;
13690Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
13700Sstevel@tonic-gate 	rctl_entity_p_t e;
13711166Sdstaff 	int res;
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 	ASSERT(curproc == &p0);
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	/*
13760Sstevel@tonic-gate 	 * Create ID space for zone IDs.  ID 0 is reserved for the
13770Sstevel@tonic-gate 	 * global zone.
13780Sstevel@tonic-gate 	 */
13790Sstevel@tonic-gate 	zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID);
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 	/*
13820Sstevel@tonic-gate 	 * Initialize generic zone resource controls, if any.
13830Sstevel@tonic-gate 	 */
13840Sstevel@tonic-gate 	rc_zone_cpu_shares = rctl_register("zone.cpu-shares",
13850Sstevel@tonic-gate 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER |
13861996Sml93401 	    RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER,
13871996Sml93401 	    FSS_MAXSHARES, FSS_MAXSHARES,
13880Sstevel@tonic-gate 	    &zone_cpu_shares_ops);
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE,
13910Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
13920Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &zone_lwps_ops);
13930Sstevel@tonic-gate 	/*
13942677Sml93401 	 * System V IPC resource controls
13952677Sml93401 	 */
13962677Sml93401 	rc_zone_msgmni = rctl_register("zone.max-msg-ids",
13972677Sml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
13982677Sml93401 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_msgmni_ops);
13992677Sml93401 
14002677Sml93401 	rc_zone_semmni = rctl_register("zone.max-sem-ids",
14012677Sml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
14022677Sml93401 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_semmni_ops);
14032677Sml93401 
14042677Sml93401 	rc_zone_shmmni = rctl_register("zone.max-shm-ids",
14052677Sml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
14062677Sml93401 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_shmmni_ops);
14072677Sml93401 
14082677Sml93401 	rc_zone_shmmax = rctl_register("zone.max-shm-memory",
14092677Sml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
14102677Sml93401 	    RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &zone_shmmax_ops);
14112677Sml93401 
14122677Sml93401 	/*
14130Sstevel@tonic-gate 	 * Create a rctl_val with PRIVILEGED, NOACTION, value = 1.  Then attach
14140Sstevel@tonic-gate 	 * this at the head of the rctl_dict_entry for ``zone.cpu-shares''.
14150Sstevel@tonic-gate 	 */
14160Sstevel@tonic-gate 	dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
14170Sstevel@tonic-gate 	bzero(dval, sizeof (rctl_val_t));
14180Sstevel@tonic-gate 	dval->rcv_value = 1;
14190Sstevel@tonic-gate 	dval->rcv_privilege = RCPRIV_PRIVILEGED;
14200Sstevel@tonic-gate 	dval->rcv_flagaction = RCTL_LOCAL_NOACTION;
14210Sstevel@tonic-gate 	dval->rcv_action_recip_pid = -1;
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	rde = rctl_dict_lookup("zone.cpu-shares");
14240Sstevel@tonic-gate 	(void) rctl_val_list_insert(&rde->rcd_default_value, dval);
14250Sstevel@tonic-gate 
14262768Ssl108498 	rc_zone_locked_mem = rctl_register("zone.max-locked-memory",
14272768Ssl108498 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES |
14282768Ssl108498 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
14292768Ssl108498 	    &zone_locked_mem_ops);
14303247Sgjelinek 
14313247Sgjelinek 	rc_zone_max_swap = rctl_register("zone.max-swap",
14323247Sgjelinek 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES |
14333247Sgjelinek 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
14343247Sgjelinek 	    &zone_max_swap_ops);
14353247Sgjelinek 
14360Sstevel@tonic-gate 	/*
14370Sstevel@tonic-gate 	 * Initialize the ``global zone''.
14380Sstevel@tonic-gate 	 */
14390Sstevel@tonic-gate 	set = rctl_set_create();
14400Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
14410Sstevel@tonic-gate 	mutex_enter(&p0.p_lock);
14420Sstevel@tonic-gate 	e.rcep_p.zone = &zone0;
14430Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
14440Sstevel@tonic-gate 	zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set,
14450Sstevel@tonic-gate 	    gp);
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	zone0.zone_nlwps = p0.p_lwpcnt;
14480Sstevel@tonic-gate 	zone0.zone_ntasks = 1;
14490Sstevel@tonic-gate 	mutex_exit(&p0.p_lock);
14502712Snn35248 	zone0.zone_restart_init = B_TRUE;
14512712Snn35248 	zone0.zone_brand = &native_brand;
14520Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
14530Sstevel@tonic-gate 	/*
14543247Sgjelinek 	 * pool_default hasn't been initialized yet, so we let pool_init()
14553247Sgjelinek 	 * take care of making sure the global zone is in the default pool.
14560Sstevel@tonic-gate 	 */
14571676Sjpk 
14581676Sjpk 	/*
14593247Sgjelinek 	 * Initialize global zone kstats
14603247Sgjelinek 	 */
14613247Sgjelinek 	zone_kstat_create(&zone0);
14623247Sgjelinek 
14633247Sgjelinek 	/*
14641676Sjpk 	 * Initialize zone label.
14651676Sjpk 	 * mlp are initialized when tnzonecfg is loaded.
14661676Sjpk 	 */
14671676Sjpk 	zone0.zone_slabel = l_admin_low;
14681676Sjpk 	rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
14691676Sjpk 	label_hold(l_admin_low);
14701676Sjpk 
14710Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
14720Sstevel@tonic-gate 	zone_uniqid(&zone0);
14730Sstevel@tonic-gate 	ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID);
14741676Sjpk 
14750Sstevel@tonic-gate 	zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size,
14760Sstevel@tonic-gate 	    mod_hash_null_valdtor);
14770Sstevel@tonic-gate 	zonehashbyname = mod_hash_create_strhash("zone_by_name",
14780Sstevel@tonic-gate 	    zone_hash_size, mod_hash_null_valdtor);
14791676Sjpk 	/*
14801676Sjpk 	 * maintain zonehashbylabel only for labeled systems
14811676Sjpk 	 */
14821676Sjpk 	if (is_system_labeled())
14831676Sjpk 		zonehashbylabel = mod_hash_create_extended("zone_by_label",
14841676Sjpk 		    zone_hash_size, mod_hash_null_keydtor,
14851676Sjpk 		    mod_hash_null_valdtor, hash_bylabel, NULL,
14861676Sjpk 		    hash_labelkey_cmp, KM_SLEEP);
14870Sstevel@tonic-gate 	zonecount = 1;
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID,
14900Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
14910Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name,
14920Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
14931769Scarlsonj 	if (is_system_labeled()) {
14941769Scarlsonj 		zone0.zone_flags |= ZF_HASHED_LABEL;
14951676Sjpk 		(void) mod_hash_insert(zonehashbylabel,
14961676Sjpk 		    (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0);
14971769Scarlsonj 	}
14981676Sjpk 	mutex_exit(&zonehash_lock);
14991676Sjpk 
15000Sstevel@tonic-gate 	/*
15010Sstevel@tonic-gate 	 * We avoid setting zone_kcred until now, since kcred is initialized
15020Sstevel@tonic-gate 	 * sometime after zone_zsd_init() and before zone_init().
15030Sstevel@tonic-gate 	 */
15040Sstevel@tonic-gate 	zone0.zone_kcred = kcred;
15050Sstevel@tonic-gate 	/*
15060Sstevel@tonic-gate 	 * The global zone is fully initialized (except for zone_rootvp which
15070Sstevel@tonic-gate 	 * will be set when the root filesystem is mounted).
15080Sstevel@tonic-gate 	 */
15090Sstevel@tonic-gate 	global_zone = &zone0;
15101166Sdstaff 
15111166Sdstaff 	/*
15121166Sdstaff 	 * Setup an event channel to send zone status change notifications on
15131166Sdstaff 	 */
15141166Sdstaff 	res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan,
15151166Sdstaff 	    EVCH_CREAT);
15161166Sdstaff 
15171166Sdstaff 	if (res)
15181166Sdstaff 		panic("Sysevent_evc_bind failed during zone setup.\n");
15193247Sgjelinek 
15200Sstevel@tonic-gate }
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate static void
15230Sstevel@tonic-gate zone_free(zone_t *zone)
15240Sstevel@tonic-gate {
15250Sstevel@tonic-gate 	ASSERT(zone != global_zone);
15260Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks == 0);
15270Sstevel@tonic-gate 	ASSERT(zone->zone_nlwps == 0);
15280Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 0);
15290Sstevel@tonic-gate 	ASSERT(zone->zone_kcred == NULL);
15300Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_DEAD ||
15310Sstevel@tonic-gate 	    zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	/* remove from deathrow list */
15340Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_DEAD) {
15350Sstevel@tonic-gate 		ASSERT(zone->zone_ref == 0);
15360Sstevel@tonic-gate 		mutex_enter(&zone_deathrow_lock);
15370Sstevel@tonic-gate 		list_remove(&zone_deathrow, zone);
15380Sstevel@tonic-gate 		mutex_exit(&zone_deathrow_lock);
15390Sstevel@tonic-gate 	}
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	zone_free_zsd(zone);
1542789Sahrens 	zone_free_datasets(zone);
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	if (zone->zone_rootvp != NULL)
15450Sstevel@tonic-gate 		VN_RELE(zone->zone_rootvp);
15460Sstevel@tonic-gate 	if (zone->zone_rootpath)
15470Sstevel@tonic-gate 		kmem_free(zone->zone_rootpath, zone->zone_rootpathlen);
15480Sstevel@tonic-gate 	if (zone->zone_name != NULL)
15490Sstevel@tonic-gate 		kmem_free(zone->zone_name, ZONENAME_MAX);
15501676Sjpk 	if (zone->zone_slabel != NULL)
15511676Sjpk 		label_rele(zone->zone_slabel);
15520Sstevel@tonic-gate 	if (zone->zone_nodename != NULL)
15530Sstevel@tonic-gate 		kmem_free(zone->zone_nodename, _SYS_NMLN);
15540Sstevel@tonic-gate 	if (zone->zone_domain != NULL)
15550Sstevel@tonic-gate 		kmem_free(zone->zone_domain, _SYS_NMLN);
15560Sstevel@tonic-gate 	if (zone->zone_privset != NULL)
15570Sstevel@tonic-gate 		kmem_free(zone->zone_privset, sizeof (priv_set_t));
15580Sstevel@tonic-gate 	if (zone->zone_rctls != NULL)
15590Sstevel@tonic-gate 		rctl_set_free(zone->zone_rctls);
15600Sstevel@tonic-gate 	if (zone->zone_bootargs != NULL)
15612267Sdp 		kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1);
15622267Sdp 	if (zone->zone_initname != NULL)
15632267Sdp 		kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1);
15640Sstevel@tonic-gate 	id_free(zoneid_space, zone->zone_id);
15650Sstevel@tonic-gate 	mutex_destroy(&zone->zone_lock);
15660Sstevel@tonic-gate 	cv_destroy(&zone->zone_cv);
15671676Sjpk 	rw_destroy(&zone->zone_mlps.mlpl_rwlock);
15680Sstevel@tonic-gate 	kmem_free(zone, sizeof (zone_t));
15690Sstevel@tonic-gate }
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate /*
15720Sstevel@tonic-gate  * See block comment at the top of this file for information about zone
15730Sstevel@tonic-gate  * status values.
15740Sstevel@tonic-gate  */
15750Sstevel@tonic-gate /*
15760Sstevel@tonic-gate  * Convenience function for setting zone status.
15770Sstevel@tonic-gate  */
15780Sstevel@tonic-gate static void
15790Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status)
15800Sstevel@tonic-gate {
15811166Sdstaff 
15821166Sdstaff 	nvlist_t *nvl = NULL;
15830Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zone_status_lock));
15840Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE &&
15850Sstevel@tonic-gate 	    status >= zone_status_get(zone));
15861166Sdstaff 
15871166Sdstaff 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) ||
15881166Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) ||
15891166Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_NEWSTATE,
15902267Sdp 	    zone_status_table[status]) ||
15911166Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_OLDSTATE,
15922267Sdp 	    zone_status_table[zone->zone_status]) ||
15931166Sdstaff 	    nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) ||
15941166Sdstaff 	    nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) ||
15951166Sdstaff 	    sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS,
15962267Sdp 	    ZONE_EVENT_STATUS_SUBCLASS, "sun.com", "kernel", nvl, EVCH_SLEEP)) {
15971166Sdstaff #ifdef DEBUG
15981166Sdstaff 		(void) printf(
15991166Sdstaff 		    "Failed to allocate and send zone state change event.\n");
16001166Sdstaff #endif
16011166Sdstaff 	}
16021166Sdstaff 	nvlist_free(nvl);
16031166Sdstaff 
16040Sstevel@tonic-gate 	zone->zone_status = status;
16051166Sdstaff 
16060Sstevel@tonic-gate 	cv_broadcast(&zone->zone_cv);
16070Sstevel@tonic-gate }
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate /*
16100Sstevel@tonic-gate  * Public function to retrieve the zone status.  The zone status may
16110Sstevel@tonic-gate  * change after it is retrieved.
16120Sstevel@tonic-gate  */
16130Sstevel@tonic-gate zone_status_t
16140Sstevel@tonic-gate zone_status_get(zone_t *zone)
16150Sstevel@tonic-gate {
16160Sstevel@tonic-gate 	return (zone->zone_status);
16170Sstevel@tonic-gate }
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate static int
16200Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs)
16210Sstevel@tonic-gate {
16222267Sdp 	char *bootargs = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP);
16232267Sdp 	int err = 0;
16242267Sdp 
16252267Sdp 	ASSERT(zone != global_zone);
16262267Sdp 	if ((err = copyinstr(zone_bootargs, bootargs, BOOTARGS_MAX, NULL)) != 0)
16272267Sdp 		goto done;	/* EFAULT or ENAMETOOLONG */
16282267Sdp 
16292267Sdp 	if (zone->zone_bootargs != NULL)
16302267Sdp 		kmem_free(zone->zone_bootargs, strlen(zone->zone_bootargs) + 1);
16312267Sdp 
16322267Sdp 	zone->zone_bootargs = kmem_alloc(strlen(bootargs) + 1, KM_SLEEP);
16332267Sdp 	(void) strcpy(zone->zone_bootargs, bootargs);
16342267Sdp 
16352267Sdp done:
16362267Sdp 	kmem_free(bootargs, BOOTARGS_MAX);
16372267Sdp 	return (err);
16382267Sdp }
16392267Sdp 
16402267Sdp static int
16412267Sdp zone_set_initname(zone_t *zone, const char *zone_initname)
16422267Sdp {
16432267Sdp 	char initname[INITNAME_SZ];
16440Sstevel@tonic-gate 	size_t len;
16452267Sdp 	int err = 0;
16462267Sdp 
16472267Sdp 	ASSERT(zone != global_zone);
16482267Sdp 	if ((err = copyinstr(zone_initname, initname, INITNAME_SZ, &len)) != 0)
16490Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
16502267Sdp 
16512267Sdp 	if (zone->zone_initname != NULL)
16522267Sdp 		kmem_free(zone->zone_initname, strlen(zone->zone_initname) + 1);
16532267Sdp 
16542267Sdp 	zone->zone_initname = kmem_alloc(strlen(initname) + 1, KM_SLEEP);
16552267Sdp 	(void) strcpy(zone->zone_initname, initname);
16560Sstevel@tonic-gate 	return (0);
16570Sstevel@tonic-gate }
16580Sstevel@tonic-gate 
16593247Sgjelinek static int
16603247Sgjelinek zone_set_phys_mcap(zone_t *zone, const uint64_t *zone_mcap)
16613247Sgjelinek {
16623247Sgjelinek 	uint64_t mcap;
16633247Sgjelinek 	int err = 0;
16643247Sgjelinek 
16653247Sgjelinek 	if ((err = copyin(zone_mcap, &mcap, sizeof (uint64_t))) == 0)
16663247Sgjelinek 		zone->zone_phys_mcap = mcap;
16673247Sgjelinek 
16683247Sgjelinek 	return (err);
16693247Sgjelinek }
16703247Sgjelinek 
16713247Sgjelinek static int
16723247Sgjelinek zone_set_sched_class(zone_t *zone, const char *new_class)
16733247Sgjelinek {
16743247Sgjelinek 	char sched_class[PC_CLNMSZ];
16753247Sgjelinek 	id_t classid;
16763247Sgjelinek 	int err;
16773247Sgjelinek 
16783247Sgjelinek 	ASSERT(zone != global_zone);
16793247Sgjelinek 	if ((err = copyinstr(new_class, sched_class, PC_CLNMSZ, NULL)) != 0)
16803247Sgjelinek 		return (err);	/* EFAULT or ENAMETOOLONG */
16813247Sgjelinek 
16823247Sgjelinek 	if (getcid(sched_class, &classid) != 0 || classid == syscid)
16833247Sgjelinek 		return (set_errno(EINVAL));
16843247Sgjelinek 	zone->zone_defaultcid = classid;
16853247Sgjelinek 	ASSERT(zone->zone_defaultcid > 0 &&
16863247Sgjelinek 	    zone->zone_defaultcid < loaded_classes);
16873247Sgjelinek 
16883247Sgjelinek 	return (0);
16893247Sgjelinek }
16903247Sgjelinek 
16910Sstevel@tonic-gate /*
16920Sstevel@tonic-gate  * Block indefinitely waiting for (zone_status >= status)
16930Sstevel@tonic-gate  */
16940Sstevel@tonic-gate void
16950Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status)
16960Sstevel@tonic-gate {
16970Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
17000Sstevel@tonic-gate 	while (zone->zone_status < status) {
17010Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
17020Sstevel@tonic-gate 	}
17030Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
17040Sstevel@tonic-gate }
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate /*
17070Sstevel@tonic-gate  * Private CPR-safe version of zone_status_wait().
17080Sstevel@tonic-gate  */
17090Sstevel@tonic-gate static void
17100Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str)
17110Sstevel@tonic-gate {
17120Sstevel@tonic-gate 	callb_cpr_t cprinfo;
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr,
17170Sstevel@tonic-gate 	    str);
17180Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
17190Sstevel@tonic-gate 	while (zone->zone_status < status) {
17200Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
17210Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
17220Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock);
17230Sstevel@tonic-gate 	}
17240Sstevel@tonic-gate 	/*
17250Sstevel@tonic-gate 	 * zone_status_lock is implicitly released by the following.
17260Sstevel@tonic-gate 	 */
17270Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
17280Sstevel@tonic-gate }
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate /*
17310Sstevel@tonic-gate  * Block until zone enters requested state or signal is received.  Return (0)
17320Sstevel@tonic-gate  * if signaled, non-zero otherwise.
17330Sstevel@tonic-gate  */
17340Sstevel@tonic-gate int
17350Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status)
17360Sstevel@tonic-gate {
17370Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
17400Sstevel@tonic-gate 	while (zone->zone_status < status) {
17410Sstevel@tonic-gate 		if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) {
17420Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
17430Sstevel@tonic-gate 			return (0);
17440Sstevel@tonic-gate 		}
17450Sstevel@tonic-gate 	}
17460Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
17470Sstevel@tonic-gate 	return (1);
17480Sstevel@tonic-gate }
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate /*
17510Sstevel@tonic-gate  * Block until the zone enters the requested state or the timeout expires,
17520Sstevel@tonic-gate  * whichever happens first.  Return (-1) if operation timed out, time remaining
17530Sstevel@tonic-gate  * otherwise.
17540Sstevel@tonic-gate  */
17550Sstevel@tonic-gate clock_t
17560Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status)
17570Sstevel@tonic-gate {
17580Sstevel@tonic-gate 	clock_t timeleft = 0;
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
17630Sstevel@tonic-gate 	while (zone->zone_status < status && timeleft != -1) {
17640Sstevel@tonic-gate 		timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim);
17650Sstevel@tonic-gate 	}
17660Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
17670Sstevel@tonic-gate 	return (timeleft);
17680Sstevel@tonic-gate }
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate /*
17710Sstevel@tonic-gate  * Block until the zone enters the requested state, the current process is
17720Sstevel@tonic-gate  * signaled,  or the timeout expires, whichever happens first.  Return (-1) if
17730Sstevel@tonic-gate  * operation timed out, 0 if signaled, time remaining otherwise.
17740Sstevel@tonic-gate  */
17750Sstevel@tonic-gate clock_t
17760Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status)
17770Sstevel@tonic-gate {
17780Sstevel@tonic-gate 	clock_t timeleft = tim - lbolt;
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
17830Sstevel@tonic-gate 	while (zone->zone_status < status) {
17840Sstevel@tonic-gate 		timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock,
17850Sstevel@tonic-gate 		    tim);
17860Sstevel@tonic-gate 		if (timeleft <= 0)
17870Sstevel@tonic-gate 			break;
17880Sstevel@tonic-gate 	}
17890Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
17900Sstevel@tonic-gate 	return (timeleft);
17910Sstevel@tonic-gate }
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate /*
17940Sstevel@tonic-gate  * Zones have two reference counts: one for references from credential
17950Sstevel@tonic-gate  * structures (zone_cred_ref), and one (zone_ref) for everything else.
17960Sstevel@tonic-gate  * This is so we can allow a zone to be rebooted while there are still
17970Sstevel@tonic-gate  * outstanding cred references, since certain drivers cache dblks (which
17980Sstevel@tonic-gate  * implicitly results in cached creds).  We wait for zone_ref to drop to
17990Sstevel@tonic-gate  * 0 (actually 1), but not zone_cred_ref.  The zone structure itself is
18000Sstevel@tonic-gate  * later freed when the zone_cred_ref drops to 0, though nothing other
18010Sstevel@tonic-gate  * than the zone id and privilege set should be accessed once the zone
18020Sstevel@tonic-gate  * is "dead".
18030Sstevel@tonic-gate  *
18040Sstevel@tonic-gate  * A debugging flag, zone_wait_for_cred, can be set to a non-zero value
18050Sstevel@tonic-gate  * to force halt/reboot to block waiting for the zone_cred_ref to drop
18060Sstevel@tonic-gate  * to 0.  This can be useful to flush out other sources of cached creds
18070Sstevel@tonic-gate  * that may be less innocuous than the driver case.
18080Sstevel@tonic-gate  */
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate int zone_wait_for_cred = 0;
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate static void
18130Sstevel@tonic-gate zone_hold_locked(zone_t *z)
18140Sstevel@tonic-gate {
18150Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&z->zone_lock));
18160Sstevel@tonic-gate 	z->zone_ref++;
18170Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
18180Sstevel@tonic-gate }
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate void
18210Sstevel@tonic-gate zone_hold(zone_t *z)
18220Sstevel@tonic-gate {
18230Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
18240Sstevel@tonic-gate 	zone_hold_locked(z);
18250Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
18260Sstevel@tonic-gate }
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate /*
18290Sstevel@tonic-gate  * If the non-cred ref count drops to 1 and either the cred ref count
18300Sstevel@tonic-gate  * is 0 or we aren't waiting for cred references, the zone is ready to
18310Sstevel@tonic-gate  * be destroyed.
18320Sstevel@tonic-gate  */
18330Sstevel@tonic-gate #define	ZONE_IS_UNREF(zone)	((zone)->zone_ref == 1 && \
18340Sstevel@tonic-gate 	    (!zone_wait_for_cred || (zone)->zone_cred_ref == 0))
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate void
18370Sstevel@tonic-gate zone_rele(zone_t *z)
18380Sstevel@tonic-gate {
18390Sstevel@tonic-gate 	boolean_t wakeup;
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
18420Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
18430Sstevel@tonic-gate 	z->zone_ref--;
18440Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
18450Sstevel@tonic-gate 		/* no more refs, free the structure */
18460Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
18470Sstevel@tonic-gate 		zone_free(z);
18480Sstevel@tonic-gate 		return;
18490Sstevel@tonic-gate 	}
18500Sstevel@tonic-gate 	/* signal zone_destroy so the zone can finish halting */
18510Sstevel@tonic-gate 	wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD);
18520Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 	if (wakeup) {
18550Sstevel@tonic-gate 		/*
18560Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
18570Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
18580Sstevel@tonic-gate 		 */
18590Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
18600Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
18610Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
18620Sstevel@tonic-gate 	}
18630Sstevel@tonic-gate }
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate void
18660Sstevel@tonic-gate zone_cred_hold(zone_t *z)
18670Sstevel@tonic-gate {
18680Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
18690Sstevel@tonic-gate 	z->zone_cred_ref++;
18700Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
18710Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
18720Sstevel@tonic-gate }
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate void
18750Sstevel@tonic-gate zone_cred_rele(zone_t *z)
18760Sstevel@tonic-gate {
18770Sstevel@tonic-gate 	boolean_t wakeup;
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
18800Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
18810Sstevel@tonic-gate 	z->zone_cred_ref--;
18820Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
18830Sstevel@tonic-gate 		/* no more refs, free the structure */
18840Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
18850Sstevel@tonic-gate 		zone_free(z);
18860Sstevel@tonic-gate 		return;
18870Sstevel@tonic-gate 	}
18880Sstevel@tonic-gate 	/*
18890Sstevel@tonic-gate 	 * If zone_destroy is waiting for the cred references to drain
18900Sstevel@tonic-gate 	 * out, and they have, signal it.
18910Sstevel@tonic-gate 	 */
18920Sstevel@tonic-gate 	wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) &&
18930Sstevel@tonic-gate 	    zone_status_get(z) >= ZONE_IS_DEAD);
18940Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 	if (wakeup) {
18970Sstevel@tonic-gate 		/*
18980Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
18990Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
19000Sstevel@tonic-gate 		 */
19010Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
19020Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
19030Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
19040Sstevel@tonic-gate 	}
19050Sstevel@tonic-gate }
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate void
19080Sstevel@tonic-gate zone_task_hold(zone_t *z)
19090Sstevel@tonic-gate {
19100Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
19110Sstevel@tonic-gate 	z->zone_ntasks++;
19120Sstevel@tonic-gate 	ASSERT(z->zone_ntasks != 0);
19130Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
19140Sstevel@tonic-gate }
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate void
19170Sstevel@tonic-gate zone_task_rele(zone_t *zone)
19180Sstevel@tonic-gate {
19190Sstevel@tonic-gate 	uint_t refcnt;
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
19220Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks != 0);
19230Sstevel@tonic-gate 	refcnt = --zone->zone_ntasks;
19240Sstevel@tonic-gate 	if (refcnt > 1)	{	/* Common case */
19250Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
19260Sstevel@tonic-gate 		return;
19270Sstevel@tonic-gate 	}
19280Sstevel@tonic-gate 	zone_hold_locked(zone);	/* so we can use the zone_t later */
19290Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
19300Sstevel@tonic-gate 	if (refcnt == 1) {
19310Sstevel@tonic-gate 		/*
19320Sstevel@tonic-gate 		 * See if the zone is shutting down.
19330Sstevel@tonic-gate 		 */
19340Sstevel@tonic-gate 		mutex_enter(&zone_status_lock);
19350Sstevel@tonic-gate 		if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) {
19360Sstevel@tonic-gate 			goto out;
19370Sstevel@tonic-gate 		}
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 		/*
19400Sstevel@tonic-gate 		 * Make sure the ntasks didn't change since we
19410Sstevel@tonic-gate 		 * dropped zone_lock.
19420Sstevel@tonic-gate 		 */
19430Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
19440Sstevel@tonic-gate 		if (refcnt != zone->zone_ntasks) {
19450Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
19460Sstevel@tonic-gate 			goto out;
19470Sstevel@tonic-gate 		}
19480Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 		/*
19510Sstevel@tonic-gate 		 * No more user processes in the zone.  The zone is empty.
19520Sstevel@tonic-gate 		 */
19530Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_EMPTY);
19540Sstevel@tonic-gate 		goto out;
19550Sstevel@tonic-gate 	}
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 	ASSERT(refcnt == 0);
19580Sstevel@tonic-gate 	/*
19590Sstevel@tonic-gate 	 * zsched has exited; the zone is dead.
19600Sstevel@tonic-gate 	 */
19610Sstevel@tonic-gate 	zone->zone_zsched = NULL;		/* paranoia */
19620Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
19630Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_DEAD);
19640Sstevel@tonic-gate out:
19650Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
19660Sstevel@tonic-gate 	zone_rele(zone);
19670Sstevel@tonic-gate }
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate zoneid_t
19700Sstevel@tonic-gate getzoneid(void)
19710Sstevel@tonic-gate {
19720Sstevel@tonic-gate 	return (curproc->p_zone->zone_id);
19730Sstevel@tonic-gate }
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate /*
19760Sstevel@tonic-gate  * Internal versions of zone_find_by_*().  These don't zone_hold() or
19770Sstevel@tonic-gate  * check the validity of a zone's state.
19780Sstevel@tonic-gate  */
19790Sstevel@tonic-gate static zone_t *
19800Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid)
19810Sstevel@tonic-gate {
19820Sstevel@tonic-gate 	mod_hash_val_t hv;
19830Sstevel@tonic-gate 	zone_t *zone = NULL;
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyid,
19880Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0)
19890Sstevel@tonic-gate 		zone = (zone_t *)hv;
19900Sstevel@tonic-gate 	return (zone);
19910Sstevel@tonic-gate }
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate static zone_t *
19941676Sjpk zone_find_all_by_label(const ts_label_t *label)
19951676Sjpk {
19961676Sjpk 	mod_hash_val_t hv;
19971676Sjpk 	zone_t *zone = NULL;
19981676Sjpk 
19991676Sjpk 	ASSERT(MUTEX_HELD(&zonehash_lock));
20001676Sjpk 
20011676Sjpk 	/*
20021676Sjpk 	 * zonehashbylabel is not maintained for unlabeled systems
20031676Sjpk 	 */
20041676Sjpk 	if (!is_system_labeled())
20051676Sjpk 		return (NULL);
20061676Sjpk 	if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0)
20071676Sjpk 		zone = (zone_t *)hv;
20081676Sjpk 	return (zone);
20091676Sjpk }
20101676Sjpk 
20111676Sjpk static zone_t *
20120Sstevel@tonic-gate zone_find_all_by_name(char *name)
20130Sstevel@tonic-gate {
20140Sstevel@tonic-gate 	mod_hash_val_t hv;
20150Sstevel@tonic-gate 	zone_t *zone = NULL;
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0)
20200Sstevel@tonic-gate 		zone = (zone_t *)hv;
20210Sstevel@tonic-gate 	return (zone);
20220Sstevel@tonic-gate }
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate /*
20250Sstevel@tonic-gate  * Public interface for looking up a zone by zoneid.  Only returns the zone if
20260Sstevel@tonic-gate  * it is fully initialized, and has not yet begun the zone_destroy() sequence.
20270Sstevel@tonic-gate  * Caller must call zone_rele() once it is done with the zone.
20280Sstevel@tonic-gate  *
20290Sstevel@tonic-gate  * The zone may begin the zone_destroy() sequence immediately after this
20300Sstevel@tonic-gate  * function returns, but may be safely used until zone_rele() is called.
20310Sstevel@tonic-gate  */
20320Sstevel@tonic-gate zone_t *
20330Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid)
20340Sstevel@tonic-gate {
20350Sstevel@tonic-gate 	zone_t *zone;
20360Sstevel@tonic-gate 	zone_status_t status;
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
20390Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
20400Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
20410Sstevel@tonic-gate 		return (NULL);
20420Sstevel@tonic-gate 	}
20430Sstevel@tonic-gate 	status = zone_status_get(zone);
20440Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
20450Sstevel@tonic-gate 		/*
20460Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
20470Sstevel@tonic-gate 		 */
20480Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
20490Sstevel@tonic-gate 		return (NULL);
20500Sstevel@tonic-gate 	}
20510Sstevel@tonic-gate 	zone_hold(zone);
20520Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
20530Sstevel@tonic-gate 	return (zone);
20540Sstevel@tonic-gate }
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate /*
20571676Sjpk  * Similar to zone_find_by_id, but using zone label as the key.
20581676Sjpk  */
20591676Sjpk zone_t *
20601676Sjpk zone_find_by_label(const ts_label_t *label)
20611676Sjpk {
20621676Sjpk 	zone_t *zone;
20632110Srica 	zone_status_t status;
20641676Sjpk 
20651676Sjpk 	mutex_enter(&zonehash_lock);
20661676Sjpk 	if ((zone = zone_find_all_by_label(label)) == NULL) {
20671676Sjpk 		mutex_exit(&zonehash_lock);
20681676Sjpk 		return (NULL);
20691676Sjpk 	}
20702110Srica 
20712110Srica 	status = zone_status_get(zone);
20722110Srica 	if (status > ZONE_IS_DOWN) {
20731676Sjpk 		/*
20741676Sjpk 		 * For all practical purposes the zone doesn't exist.
20751676Sjpk 		 */
20762110Srica 		mutex_exit(&zonehash_lock);
20772110Srica 		return (NULL);
20781676Sjpk 	}
20792110Srica 	zone_hold(zone);
20801676Sjpk 	mutex_exit(&zonehash_lock);
20811676Sjpk 	return (zone);
20821676Sjpk }
20831676Sjpk 
20841676Sjpk /*
20850Sstevel@tonic-gate  * Similar to zone_find_by_id, but using zone name as the key.
20860Sstevel@tonic-gate  */
20870Sstevel@tonic-gate zone_t *
20880Sstevel@tonic-gate zone_find_by_name(char *name)
20890Sstevel@tonic-gate {
20900Sstevel@tonic-gate 	zone_t *zone;
20910Sstevel@tonic-gate 	zone_status_t status;
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
20940Sstevel@tonic-gate 	if ((zone = zone_find_all_by_name(name)) == NULL) {
20950Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
20960Sstevel@tonic-gate 		return (NULL);
20970Sstevel@tonic-gate 	}
20980Sstevel@tonic-gate 	status = zone_status_get(zone);
20990Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
21000Sstevel@tonic-gate 		/*
21010Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
21020Sstevel@tonic-gate 		 */
21030Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
21040Sstevel@tonic-gate 		return (NULL);
21050Sstevel@tonic-gate 	}
21060Sstevel@tonic-gate 	zone_hold(zone);
21070Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
21080Sstevel@tonic-gate 	return (zone);
21090Sstevel@tonic-gate }
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate /*
21120Sstevel@tonic-gate  * Similar to zone_find_by_id(), using the path as a key.  For instance,
21130Sstevel@tonic-gate  * if there is a zone "foo" rooted at /foo/root, and the path argument
21140Sstevel@tonic-gate  * is "/foo/root/proc", it will return the held zone_t corresponding to
21150Sstevel@tonic-gate  * zone "foo".
21160Sstevel@tonic-gate  *
21170Sstevel@tonic-gate  * zone_find_by_path() always returns a non-NULL value, since at the
21180Sstevel@tonic-gate  * very least every path will be contained in the global zone.
21190Sstevel@tonic-gate  *
21200Sstevel@tonic-gate  * As with the other zone_find_by_*() functions, the caller is
21210Sstevel@tonic-gate  * responsible for zone_rele()ing the return value of this function.
21220Sstevel@tonic-gate  */
21230Sstevel@tonic-gate zone_t *
21240Sstevel@tonic-gate zone_find_by_path(const char *path)
21250Sstevel@tonic-gate {
21260Sstevel@tonic-gate 	zone_t *zone;
21270Sstevel@tonic-gate 	zone_t *zret = NULL;
21280Sstevel@tonic-gate 	zone_status_t status;
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 	if (path == NULL) {
21310Sstevel@tonic-gate 		/*
21320Sstevel@tonic-gate 		 * Call from rootconf().
21330Sstevel@tonic-gate 		 */
21340Sstevel@tonic-gate 		zone_hold(global_zone);
21350Sstevel@tonic-gate 		return (global_zone);
21360Sstevel@tonic-gate 	}
21370Sstevel@tonic-gate 	ASSERT(*path == '/');
21380Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
21390Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
21400Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
21410Sstevel@tonic-gate 		if (ZONE_PATH_VISIBLE(path, zone))
21420Sstevel@tonic-gate 			zret = zone;
21430Sstevel@tonic-gate 	}
21440Sstevel@tonic-gate 	ASSERT(zret != NULL);
21450Sstevel@tonic-gate 	status = zone_status_get(zret);
21460Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
21470Sstevel@tonic-gate 		/*
21480Sstevel@tonic-gate 		 * Zone practically doesn't exist.
21490Sstevel@tonic-gate 		 */
21500Sstevel@tonic-gate 		zret = global_zone;
21510Sstevel@tonic-gate 	}
21520Sstevel@tonic-gate 	zone_hold(zret);
21530Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
21540Sstevel@tonic-gate 	return (zret);
21550Sstevel@tonic-gate }
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate /*
21580Sstevel@tonic-gate  * Get the number of cpus visible to this zone.  The system-wide global
21590Sstevel@tonic-gate  * 'ncpus' is returned if pools are disabled, the caller is in the
21600Sstevel@tonic-gate  * global zone, or a NULL zone argument is passed in.
21610Sstevel@tonic-gate  */
21620Sstevel@tonic-gate int
21630Sstevel@tonic-gate zone_ncpus_get(zone_t *zone)
21640Sstevel@tonic-gate {
21650Sstevel@tonic-gate 	int myncpus = zone == NULL ? 0 : zone->zone_ncpus;
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate 	return (myncpus != 0 ? myncpus : ncpus);
21680Sstevel@tonic-gate }
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate /*
21710Sstevel@tonic-gate  * Get the number of online cpus visible to this zone.  The system-wide
21720Sstevel@tonic-gate  * global 'ncpus_online' is returned if pools are disabled, the caller
21730Sstevel@tonic-gate  * is in the global zone, or a NULL zone argument is passed in.
21740Sstevel@tonic-gate  */
21750Sstevel@tonic-gate int
21760Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone)
21770Sstevel@tonic-gate {
21780Sstevel@tonic-gate 	int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online;
21790Sstevel@tonic-gate 
21800Sstevel@tonic-gate 	return (myncpus_online != 0 ? myncpus_online : ncpus_online);
21810Sstevel@tonic-gate }
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate /*
21840Sstevel@tonic-gate  * Return the pool to which the zone is currently bound.
21850Sstevel@tonic-gate  */
21860Sstevel@tonic-gate pool_t *
21870Sstevel@tonic-gate zone_pool_get(zone_t *zone)
21880Sstevel@tonic-gate {
21890Sstevel@tonic-gate 	ASSERT(pool_lock_held());
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	return (zone->zone_pool);
21920Sstevel@tonic-gate }
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate /*
21950Sstevel@tonic-gate  * Set the zone's pool pointer and update the zone's visibility to match
21960Sstevel@tonic-gate  * the resources in the new pool.
21970Sstevel@tonic-gate  */
21980Sstevel@tonic-gate void
21990Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool)
22000Sstevel@tonic-gate {
22010Sstevel@tonic-gate 	ASSERT(pool_lock_held());
22020Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate 	zone->zone_pool = pool;
22050Sstevel@tonic-gate 	zone_pset_set(zone, pool->pool_pset->pset_id);
22060Sstevel@tonic-gate }
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate /*
22090Sstevel@tonic-gate  * Return the cached value of the id of the processor set to which the
22100Sstevel@tonic-gate  * zone is currently bound.  The value will be ZONE_PS_INVAL if the pools
22110Sstevel@tonic-gate  * facility is disabled.
22120Sstevel@tonic-gate  */
22130Sstevel@tonic-gate psetid_t
22140Sstevel@tonic-gate zone_pset_get(zone_t *zone)
22150Sstevel@tonic-gate {
22160Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate 	return (zone->zone_psetid);
22190Sstevel@tonic-gate }
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate /*
22220Sstevel@tonic-gate  * Set the cached value of the id of the processor set to which the zone
22230Sstevel@tonic-gate  * is currently bound.  Also update the zone's visibility to match the
22240Sstevel@tonic-gate  * resources in the new processor set.
22250Sstevel@tonic-gate  */
22260Sstevel@tonic-gate void
22270Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid)
22280Sstevel@tonic-gate {
22290Sstevel@tonic-gate 	psetid_t oldpsetid;
22300Sstevel@tonic-gate 
22310Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
22320Sstevel@tonic-gate 	oldpsetid = zone_pset_get(zone);
22330Sstevel@tonic-gate 
22340Sstevel@tonic-gate 	if (oldpsetid == newpsetid)
22350Sstevel@tonic-gate 		return;
22360Sstevel@tonic-gate 	/*
22370Sstevel@tonic-gate 	 * Global zone sees all.
22380Sstevel@tonic-gate 	 */
22390Sstevel@tonic-gate 	if (zone != global_zone) {
22400Sstevel@tonic-gate 		zone->zone_psetid = newpsetid;
22410Sstevel@tonic-gate 		if (newpsetid != ZONE_PS_INVAL)
22420Sstevel@tonic-gate 			pool_pset_visibility_add(newpsetid, zone);
22430Sstevel@tonic-gate 		if (oldpsetid != ZONE_PS_INVAL)
22440Sstevel@tonic-gate 			pool_pset_visibility_remove(oldpsetid, zone);
22450Sstevel@tonic-gate 	}
22460Sstevel@tonic-gate 	/*
22470Sstevel@tonic-gate 	 * Disabling pools, so we should start using the global values
22480Sstevel@tonic-gate 	 * for ncpus and ncpus_online.
22490Sstevel@tonic-gate 	 */
22500Sstevel@tonic-gate 	if (newpsetid == ZONE_PS_INVAL) {
22510Sstevel@tonic-gate 		zone->zone_ncpus = 0;
22520Sstevel@tonic-gate 		zone->zone_ncpus_online = 0;
22530Sstevel@tonic-gate 	}
22540Sstevel@tonic-gate }
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate /*
22570Sstevel@tonic-gate  * Walk the list of active zones and issue the provided callback for
22580Sstevel@tonic-gate  * each of them.
22590Sstevel@tonic-gate  *
22600Sstevel@tonic-gate  * Caller must not be holding any locks that may be acquired under
22610Sstevel@tonic-gate  * zonehash_lock.  See comment at the beginning of the file for a list of
22620Sstevel@tonic-gate  * common locks and their interactions with zones.
22630Sstevel@tonic-gate  */
22640Sstevel@tonic-gate int
22650Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data)
22660Sstevel@tonic-gate {
22670Sstevel@tonic-gate 	zone_t *zone;
22680Sstevel@tonic-gate 	int ret = 0;
22690Sstevel@tonic-gate 	zone_status_t status;
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
22720Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
22730Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
22740Sstevel@tonic-gate 		/*
22750Sstevel@tonic-gate 		 * Skip zones that shouldn't be externally visible.
22760Sstevel@tonic-gate 		 */
22770Sstevel@tonic-gate 		status = zone_status_get(zone);
22780Sstevel@tonic-gate 		if (status < ZONE_IS_READY || status > ZONE_IS_DOWN)
22790Sstevel@tonic-gate 			continue;
22800Sstevel@tonic-gate 		/*
22810Sstevel@tonic-gate 		 * Bail immediately if any callback invocation returns a
22820Sstevel@tonic-gate 		 * non-zero value.
22830Sstevel@tonic-gate 		 */
22840Sstevel@tonic-gate 		ret = (*cb)(zone, data);
22850Sstevel@tonic-gate 		if (ret != 0)
22860Sstevel@tonic-gate 			break;
22870Sstevel@tonic-gate 	}
22880Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
22890Sstevel@tonic-gate 	return (ret);
22900Sstevel@tonic-gate }
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate static int
22930Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath)
22940Sstevel@tonic-gate {
22950Sstevel@tonic-gate 	vnode_t *vp;
22960Sstevel@tonic-gate 	int trycount;
22970Sstevel@tonic-gate 	int error = 0;
22980Sstevel@tonic-gate 	char *path;
22990Sstevel@tonic-gate 	struct pathname upn, pn;
23000Sstevel@tonic-gate 	size_t pathlen;
23010Sstevel@tonic-gate 
23020Sstevel@tonic-gate 	if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0)
23030Sstevel@tonic-gate 		return (error);
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate 	pn_alloc(&pn);
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 	/* prevent infinite loop */
23080Sstevel@tonic-gate 	trycount = 10;
23090Sstevel@tonic-gate 	for (;;) {
23100Sstevel@tonic-gate 		if (--trycount <= 0) {
23110Sstevel@tonic-gate 			error = ESTALE;
23120Sstevel@tonic-gate 			goto out;
23130Sstevel@tonic-gate 		}
23140Sstevel@tonic-gate 
23150Sstevel@tonic-gate 		if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) {
23160Sstevel@tonic-gate 			/*
23170Sstevel@tonic-gate 			 * VOP_ACCESS() may cover 'vp' with a new
23180Sstevel@tonic-gate 			 * filesystem, if 'vp' is an autoFS vnode.
23190Sstevel@tonic-gate 			 * Get the new 'vp' if so.
23200Sstevel@tonic-gate 			 */
23210Sstevel@tonic-gate 			if ((error = VOP_ACCESS(vp, VEXEC, 0, CRED())) == 0 &&
23220Sstevel@tonic-gate 			    (vp->v_vfsmountedhere == NULL ||
23230Sstevel@tonic-gate 			    (error = traverse(&vp)) == 0)) {
23240Sstevel@tonic-gate 				pathlen = pn.pn_pathlen + 2;
23250Sstevel@tonic-gate 				path = kmem_alloc(pathlen, KM_SLEEP);
23260Sstevel@tonic-gate 				(void) strncpy(path, pn.pn_path,
23270Sstevel@tonic-gate 				    pn.pn_pathlen + 1);
23280Sstevel@tonic-gate 				path[pathlen - 2] = '/';
23290Sstevel@tonic-gate 				path[pathlen - 1] = '\0';
23300Sstevel@tonic-gate 				pn_free(&pn);
23310Sstevel@tonic-gate 				pn_free(&upn);
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 				/* Success! */
23340Sstevel@tonic-gate 				break;
23350Sstevel@tonic-gate 			}
23360Sstevel@tonic-gate 			VN_RELE(vp);
23370Sstevel@tonic-gate 		}
23380Sstevel@tonic-gate 		if (error != ESTALE)
23390Sstevel@tonic-gate 			goto out;
23400Sstevel@tonic-gate 	}
23410Sstevel@tonic-gate 
23420Sstevel@tonic-gate 	ASSERT(error == 0);
23430Sstevel@tonic-gate 	zone->zone_rootvp = vp;		/* we hold a reference to vp */
23440Sstevel@tonic-gate 	zone->zone_rootpath = path;
23450Sstevel@tonic-gate 	zone->zone_rootpathlen = pathlen;
23461769Scarlsonj 	if (pathlen > 5 && strcmp(path + pathlen - 5, "/lu/") == 0)
23471769Scarlsonj 		zone->zone_flags |= ZF_IS_SCRATCH;
23480Sstevel@tonic-gate 	return (0);
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate out:
23510Sstevel@tonic-gate 	pn_free(&pn);
23520Sstevel@tonic-gate 	pn_free(&upn);
23530Sstevel@tonic-gate 	return (error);
23540Sstevel@tonic-gate }
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate #define	isalnum(c)	(((c) >= '0' && (c) <= '9') || \
23570Sstevel@tonic-gate 			((c) >= 'a' && (c) <= 'z') || \
23580Sstevel@tonic-gate 			((c) >= 'A' && (c) <= 'Z'))
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate static int
23610Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname)
23620Sstevel@tonic-gate {
23630Sstevel@tonic-gate 	char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
23640Sstevel@tonic-gate 	size_t len;
23650Sstevel@tonic-gate 	int i, err;
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 	if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) {
23680Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
23690Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
23700Sstevel@tonic-gate 	}
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate 	/* must be less than ZONENAME_MAX */
23730Sstevel@tonic-gate 	if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') {
23740Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
23750Sstevel@tonic-gate 		return (EINVAL);
23760Sstevel@tonic-gate 	}
23770Sstevel@tonic-gate 
23780Sstevel@tonic-gate 	/*
23790Sstevel@tonic-gate 	 * Name must start with an alphanumeric and must contain only
23800Sstevel@tonic-gate 	 * alphanumerics, '-', '_' and '.'.
23810Sstevel@tonic-gate 	 */
23820Sstevel@tonic-gate 	if (!isalnum(kname[0])) {
23830Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
23840Sstevel@tonic-gate 		return (EINVAL);
23850Sstevel@tonic-gate 	}
23860Sstevel@tonic-gate 	for (i = 1; i < len - 1; i++) {
23870Sstevel@tonic-gate 		if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' &&
23880Sstevel@tonic-gate 		    kname[i] != '.') {
23890Sstevel@tonic-gate 			kmem_free(kname, ZONENAME_MAX);
23900Sstevel@tonic-gate 			return (EINVAL);
23910Sstevel@tonic-gate 		}
23920Sstevel@tonic-gate 	}
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 	zone->zone_name = kname;
23950Sstevel@tonic-gate 	return (0);
23960Sstevel@tonic-gate }
23970Sstevel@tonic-gate 
23980Sstevel@tonic-gate /*
23990Sstevel@tonic-gate  * Similar to thread_create(), but makes sure the thread is in the appropriate
24000Sstevel@tonic-gate  * zone's zsched process (curproc->p_zone->zone_zsched) before returning.
24010Sstevel@tonic-gate  */
24020Sstevel@tonic-gate /*ARGSUSED*/
24030Sstevel@tonic-gate kthread_t *
24040Sstevel@tonic-gate zthread_create(
24050Sstevel@tonic-gate     caddr_t stk,
24060Sstevel@tonic-gate     size_t stksize,
24070Sstevel@tonic-gate     void (*proc)(),
24080Sstevel@tonic-gate     void *arg,
24090Sstevel@tonic-gate     size_t len,
24100Sstevel@tonic-gate     pri_t pri)
24110Sstevel@tonic-gate {
24120Sstevel@tonic-gate 	kthread_t *t;
24130Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
24140Sstevel@tonic-gate 	proc_t *pp = zone->zone_zsched;
24150Sstevel@tonic-gate 
24160Sstevel@tonic-gate 	zone_hold(zone);	/* Reference to be dropped when thread exits */
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate 	/*
24190Sstevel@tonic-gate 	 * No-one should be trying to create threads if the zone is shutting
24200Sstevel@tonic-gate 	 * down and there aren't any kernel threads around.  See comment
24210Sstevel@tonic-gate 	 * in zthread_exit().
24220Sstevel@tonic-gate 	 */
24230Sstevel@tonic-gate 	ASSERT(!(zone->zone_kthreads == NULL &&
24240Sstevel@tonic-gate 	    zone_status_get(zone) >= ZONE_IS_EMPTY));
24250Sstevel@tonic-gate 	/*
24260Sstevel@tonic-gate 	 * Create a thread, but don't let it run until we've finished setting
24270Sstevel@tonic-gate 	 * things up.
24280Sstevel@tonic-gate 	 */
24290Sstevel@tonic-gate 	t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri);
24300Sstevel@tonic-gate 	ASSERT(t->t_forw == NULL);
24310Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
24320Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL) {
24330Sstevel@tonic-gate 		t->t_forw = t->t_back = t;
24340Sstevel@tonic-gate 	} else {
24350Sstevel@tonic-gate 		kthread_t *tx = zone->zone_kthreads;
24360Sstevel@tonic-gate 
24370Sstevel@tonic-gate 		t->t_forw = tx;
24380Sstevel@tonic-gate 		t->t_back = tx->t_back;
24390Sstevel@tonic-gate 		tx->t_back->t_forw = t;
24400Sstevel@tonic-gate 		tx->t_back = t;
24410Sstevel@tonic-gate 	}
24420Sstevel@tonic-gate 	zone->zone_kthreads = t;
24430Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
24460Sstevel@tonic-gate 	t->t_proc_flag |= TP_ZTHREAD;
24470Sstevel@tonic-gate 	project_rele(t->t_proj);
24480Sstevel@tonic-gate 	t->t_proj = project_hold(pp->p_task->tk_proj);
24490Sstevel@tonic-gate 
24500Sstevel@tonic-gate 	/*
24510Sstevel@tonic-gate 	 * Setup complete, let it run.
24520Sstevel@tonic-gate 	 */
24530Sstevel@tonic-gate 	thread_lock(t);
24540Sstevel@tonic-gate 	t->t_schedflag |= TS_ALLSTART;
24550Sstevel@tonic-gate 	setrun_locked(t);
24560Sstevel@tonic-gate 	thread_unlock(t);
24570Sstevel@tonic-gate 
24580Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 	return (t);
24610Sstevel@tonic-gate }
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate /*
24640Sstevel@tonic-gate  * Similar to thread_exit().  Must be called by threads created via
24650Sstevel@tonic-gate  * zthread_exit().
24660Sstevel@tonic-gate  */
24670Sstevel@tonic-gate void
24680Sstevel@tonic-gate zthread_exit(void)
24690Sstevel@tonic-gate {
24700Sstevel@tonic-gate 	kthread_t *t = curthread;
24710Sstevel@tonic-gate 	proc_t *pp = curproc;
24720Sstevel@tonic-gate 	zone_t *zone = pp->p_zone;
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
24750Sstevel@tonic-gate 
24760Sstevel@tonic-gate 	/*
24770Sstevel@tonic-gate 	 * Reparent to p0
24780Sstevel@tonic-gate 	 */
24791075Sjosephb 	kpreempt_disable();
24800Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
24810Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_ZTHREAD;
24820Sstevel@tonic-gate 	t->t_procp = &p0;
24830Sstevel@tonic-gate 	hat_thread_exit(t);
24840Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
24851075Sjosephb 	kpreempt_enable();
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate 	if (t->t_back == t) {
24880Sstevel@tonic-gate 		ASSERT(t->t_forw == t);
24890Sstevel@tonic-gate 		/*
24900Sstevel@tonic-gate 		 * If the zone is empty, once the thread count
24910Sstevel@tonic-gate 		 * goes to zero no further kernel threads can be
24920Sstevel@tonic-gate 		 * created.  This is because if the creator is a process
24930Sstevel@tonic-gate 		 * in the zone, then it must have exited before the zone
24940Sstevel@tonic-gate 		 * state could be set to ZONE_IS_EMPTY.
24950Sstevel@tonic-gate 		 * Otherwise, if the creator is a kernel thread in the
24960Sstevel@tonic-gate 		 * zone, the thread count is non-zero.
24970Sstevel@tonic-gate 		 *
24980Sstevel@tonic-gate 		 * This really means that non-zone kernel threads should
24990Sstevel@tonic-gate 		 * not create zone kernel threads.
25000Sstevel@tonic-gate 		 */
25010Sstevel@tonic-gate 		zone->zone_kthreads = NULL;
25020Sstevel@tonic-gate 		if (zone_status_get(zone) == ZONE_IS_EMPTY) {
25030Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_DOWN);
25040Sstevel@tonic-gate 		}
25050Sstevel@tonic-gate 	} else {
25060Sstevel@tonic-gate 		t->t_forw->t_back = t->t_back;
25070Sstevel@tonic-gate 		t->t_back->t_forw = t->t_forw;
25080Sstevel@tonic-gate 		if (zone->zone_kthreads == t)
25090Sstevel@tonic-gate 			zone->zone_kthreads = t->t_forw;
25100Sstevel@tonic-gate 	}
25110Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
25120Sstevel@tonic-gate 	zone_rele(zone);
25130Sstevel@tonic-gate 	thread_exit();
25140Sstevel@tonic-gate 	/* NOTREACHED */
25150Sstevel@tonic-gate }
25160Sstevel@tonic-gate 
25170Sstevel@tonic-gate static void
25180Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp)
25190Sstevel@tonic-gate {
25200Sstevel@tonic-gate 	vnode_t *oldvp;
25210Sstevel@tonic-gate 
25220Sstevel@tonic-gate 	/* we're going to hold a reference here to the directory */
25230Sstevel@tonic-gate 	VN_HOLD(vp);
25240Sstevel@tonic-gate 
25250Sstevel@tonic-gate #ifdef C2_AUDIT
25260Sstevel@tonic-gate 	if (audit_active)	/* update abs cwd/root path see c2audit.c */
25270Sstevel@tonic-gate 		audit_chdirec(vp, vpp);
25280Sstevel@tonic-gate #endif
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
25310Sstevel@tonic-gate 	oldvp = *vpp;
25320Sstevel@tonic-gate 	*vpp = vp;
25330Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
25340Sstevel@tonic-gate 	if (oldvp != NULL)
25350Sstevel@tonic-gate 		VN_RELE(oldvp);
25360Sstevel@tonic-gate }
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate /*
25390Sstevel@tonic-gate  * Convert an rctl value represented by an nvlist_t into an rctl_val_t.
25400Sstevel@tonic-gate  */
25410Sstevel@tonic-gate static int
25420Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv)
25430Sstevel@tonic-gate {
25440Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
25450Sstevel@tonic-gate 	boolean_t priv_set = B_FALSE;
25460Sstevel@tonic-gate 	boolean_t limit_set = B_FALSE;
25470Sstevel@tonic-gate 	boolean_t action_set = B_FALSE;
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
25500Sstevel@tonic-gate 		const char *name;
25510Sstevel@tonic-gate 		uint64_t ui64;
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate 		name = nvpair_name(nvp);
25540Sstevel@tonic-gate 		if (nvpair_type(nvp) != DATA_TYPE_UINT64)
25550Sstevel@tonic-gate 			return (EINVAL);
25560Sstevel@tonic-gate 		(void) nvpair_value_uint64(nvp, &ui64);
25570Sstevel@tonic-gate 		if (strcmp(name, "privilege") == 0) {
25580Sstevel@tonic-gate 			/*
25590Sstevel@tonic-gate 			 * Currently only privileged values are allowed, but
25600Sstevel@tonic-gate 			 * this may change in the future.
25610Sstevel@tonic-gate 			 */
25620Sstevel@tonic-gate 			if (ui64 != RCPRIV_PRIVILEGED)
25630Sstevel@tonic-gate 				return (EINVAL);
25640Sstevel@tonic-gate 			rv->rcv_privilege = ui64;
25650Sstevel@tonic-gate 			priv_set = B_TRUE;
25660Sstevel@tonic-gate 		} else if (strcmp(name, "limit") == 0) {
25670Sstevel@tonic-gate 			rv->rcv_value = ui64;
25680Sstevel@tonic-gate 			limit_set = B_TRUE;
25690Sstevel@tonic-gate 		} else if (strcmp(name, "action") == 0) {
25700Sstevel@tonic-gate 			if (ui64 != RCTL_LOCAL_NOACTION &&
25710Sstevel@tonic-gate 			    ui64 != RCTL_LOCAL_DENY)
25720Sstevel@tonic-gate 				return (EINVAL);
25730Sstevel@tonic-gate 			rv->rcv_flagaction = ui64;
25740Sstevel@tonic-gate 			action_set = B_TRUE;
25750Sstevel@tonic-gate 		} else {
25760Sstevel@tonic-gate 			return (EINVAL);
25770Sstevel@tonic-gate 		}
25780Sstevel@tonic-gate 	}
25790Sstevel@tonic-gate 
25800Sstevel@tonic-gate 	if (!(priv_set && limit_set && action_set))
25810Sstevel@tonic-gate 		return (EINVAL);
25820Sstevel@tonic-gate 	rv->rcv_action_signal = 0;
25830Sstevel@tonic-gate 	rv->rcv_action_recipient = NULL;
25840Sstevel@tonic-gate 	rv->rcv_action_recip_pid = -1;
25850Sstevel@tonic-gate 	rv->rcv_firing_time = 0;
25860Sstevel@tonic-gate 
25870Sstevel@tonic-gate 	return (0);
25880Sstevel@tonic-gate }
25890Sstevel@tonic-gate 
25902267Sdp /*
25912267Sdp  * Non-global zone version of start_init.
25922267Sdp  */
25930Sstevel@tonic-gate void
25942267Sdp zone_start_init(void)
25950Sstevel@tonic-gate {
25960Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
25972712Snn35248 	zone_t *z = p->p_zone;
25982267Sdp 
25992267Sdp 	ASSERT(!INGLOBALZONE(curproc));
26000Sstevel@tonic-gate 
26010Sstevel@tonic-gate 	/*
26022712Snn35248 	 * For all purposes (ZONE_ATTR_INITPID and restart_init),
26032712Snn35248 	 * storing just the pid of init is sufficient.
26042712Snn35248 	 */
26052712Snn35248 	z->zone_proc_initpid = p->p_pid;
26062712Snn35248 
26072712Snn35248 	/*
26082267Sdp 	 * We maintain zone_boot_err so that we can return the cause of the
26092267Sdp 	 * failure back to the caller of the zone_boot syscall.
26100Sstevel@tonic-gate 	 */
26112267Sdp 	p->p_zone->zone_boot_err = start_init_common();
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
26142712Snn35248 	if (z->zone_boot_err != 0) {
26150Sstevel@tonic-gate 		/*
26160Sstevel@tonic-gate 		 * Make sure we are still in the booting state-- we could have
26170Sstevel@tonic-gate 		 * raced and already be shutting down, or even further along.
26180Sstevel@tonic-gate 		 */
26192712Snn35248 		if (zone_status_get(z) == ZONE_IS_BOOTING)
26202712Snn35248 			zone_status_set(z, ZONE_IS_SHUTTING_DOWN);
26210Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
26220Sstevel@tonic-gate 		/* It's gone bad, dispose of the process */
26232712Snn35248 		if (proc_exit(CLD_EXITED, z->zone_boot_err) != 0) {
2624390Sraf 			mutex_enter(&p->p_lock);
2625390Sraf 			ASSERT(p->p_flag & SEXITLWPS);
26260Sstevel@tonic-gate 			lwp_exit();
26270Sstevel@tonic-gate 		}
26280Sstevel@tonic-gate 	} else {
26292712Snn35248 		if (zone_status_get(z) == ZONE_IS_BOOTING)
26302712Snn35248 			zone_status_set(z, ZONE_IS_RUNNING);
26310Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
26320Sstevel@tonic-gate 		/* cause the process to return to userland. */
26330Sstevel@tonic-gate 		lwp_rtt();
26340Sstevel@tonic-gate 	}
26350Sstevel@tonic-gate }
26360Sstevel@tonic-gate 
26370Sstevel@tonic-gate struct zsched_arg {
26380Sstevel@tonic-gate 	zone_t *zone;
26390Sstevel@tonic-gate 	nvlist_t *nvlist;
26400Sstevel@tonic-gate };
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate /*
26430Sstevel@tonic-gate  * Per-zone "sched" workalike.  The similarity to "sched" doesn't have
26440Sstevel@tonic-gate  * anything to do with scheduling, but rather with the fact that
26450Sstevel@tonic-gate  * per-zone kernel threads are parented to zsched, just like regular
26460Sstevel@tonic-gate  * kernel threads are parented to sched (p0).
26470Sstevel@tonic-gate  *
26480Sstevel@tonic-gate  * zsched is also responsible for launching init for the zone.
26490Sstevel@tonic-gate  */
26500Sstevel@tonic-gate static void
26510Sstevel@tonic-gate zsched(void *arg)
26520Sstevel@tonic-gate {
26530Sstevel@tonic-gate 	struct zsched_arg *za = arg;
26540Sstevel@tonic-gate 	proc_t *pp = curproc;
26550Sstevel@tonic-gate 	proc_t *initp = proc_init;
26560Sstevel@tonic-gate 	zone_t *zone = za->zone;
26570Sstevel@tonic-gate 	cred_t *cr, *oldcred;
26580Sstevel@tonic-gate 	rctl_set_t *set;
26590Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
26600Sstevel@tonic-gate 	contract_t *ct = NULL;
26610Sstevel@tonic-gate 	task_t *tk, *oldtk;
26620Sstevel@tonic-gate 	rctl_entity_p_t e;
26630Sstevel@tonic-gate 	kproject_t *pj;
26640Sstevel@tonic-gate 
26650Sstevel@tonic-gate 	nvlist_t *nvl = za->nvlist;
26660Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
26670Sstevel@tonic-gate 
26683446Smrj 	bcopy("zsched", PTOU(pp)->u_psargs, sizeof ("zsched"));
26693446Smrj 	bcopy("zsched", PTOU(pp)->u_comm, sizeof ("zsched"));
26703446Smrj 	PTOU(pp)->u_argc = 0;
26713446Smrj 	PTOU(pp)->u_argv = NULL;
26723446Smrj 	PTOU(pp)->u_envp = NULL;
26730Sstevel@tonic-gate 	closeall(P_FINFO(pp));
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 	/*
26760Sstevel@tonic-gate 	 * We are this zone's "zsched" process.  As the zone isn't generally
26770Sstevel@tonic-gate 	 * visible yet we don't need to grab any locks before initializing its
26780Sstevel@tonic-gate 	 * zone_proc pointer.
26790Sstevel@tonic-gate 	 */
26800Sstevel@tonic-gate 	zone_hold(zone);  /* this hold is released by zone_destroy() */
26810Sstevel@tonic-gate 	zone->zone_zsched = pp;
26820Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
26830Sstevel@tonic-gate 	pp->p_zone = zone;
26840Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
26850Sstevel@tonic-gate 
26860Sstevel@tonic-gate 	/*
26870Sstevel@tonic-gate 	 * Disassociate process from its 'parent'; parent ourselves to init
26880Sstevel@tonic-gate 	 * (pid 1) and change other values as needed.
26890Sstevel@tonic-gate 	 */
26900Sstevel@tonic-gate 	sess_create();
26910Sstevel@tonic-gate 
26920Sstevel@tonic-gate 	mutex_enter(&pidlock);
26930Sstevel@tonic-gate 	proc_detach(pp);
26940Sstevel@tonic-gate 	pp->p_ppid = 1;
26950Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
26960Sstevel@tonic-gate 	pp->p_ancpid = 1;
26970Sstevel@tonic-gate 	pp->p_parent = initp;
26980Sstevel@tonic-gate 	pp->p_psibling = NULL;
26990Sstevel@tonic-gate 	if (initp->p_child)
27000Sstevel@tonic-gate 		initp->p_child->p_psibling = pp;
27010Sstevel@tonic-gate 	pp->p_sibling = initp->p_child;
27020Sstevel@tonic-gate 	initp->p_child = pp;
27030Sstevel@tonic-gate 
27040Sstevel@tonic-gate 	/* Decrement what newproc() incremented. */
27050Sstevel@tonic-gate 	upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID);
27060Sstevel@tonic-gate 	/*
27070Sstevel@tonic-gate 	 * Our credentials are about to become kcred-like, so we don't care
27080Sstevel@tonic-gate 	 * about the caller's ruid.
27090Sstevel@tonic-gate 	 */
27100Sstevel@tonic-gate 	upcount_inc(crgetruid(kcred), zone->zone_id);
27110Sstevel@tonic-gate 	mutex_exit(&pidlock);
27120Sstevel@tonic-gate 
27130Sstevel@tonic-gate 	/*
27140Sstevel@tonic-gate 	 * getting out of global zone, so decrement lwp counts
27150Sstevel@tonic-gate 	 */
27160Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
27170Sstevel@tonic-gate 	mutex_enter(&global_zone->zone_nlwps_lock);
27180Sstevel@tonic-gate 	pj->kpj_nlwps -= pp->p_lwpcnt;
27190Sstevel@tonic-gate 	global_zone->zone_nlwps -= pp->p_lwpcnt;
27200Sstevel@tonic-gate 	mutex_exit(&global_zone->zone_nlwps_lock);
27210Sstevel@tonic-gate 
27220Sstevel@tonic-gate 	/*
27232768Ssl108498 	 * Decrement locked memory counts on old zone and project.
27242768Ssl108498 	 */
27253247Sgjelinek 	mutex_enter(&global_zone->zone_mem_lock);
27262768Ssl108498 	global_zone->zone_locked_mem -= pp->p_locked_mem;
27272768Ssl108498 	pj->kpj_data.kpd_locked_mem -= pp->p_locked_mem;
27283247Sgjelinek 	mutex_exit(&global_zone->zone_mem_lock);
27292768Ssl108498 
27302768Ssl108498 	/*
27310Sstevel@tonic-gate 	 * Create and join a new task in project '0' of this zone.
27320Sstevel@tonic-gate 	 *
27330Sstevel@tonic-gate 	 * We don't need to call holdlwps() since we know we're the only lwp in
27340Sstevel@tonic-gate 	 * this process.
27350Sstevel@tonic-gate 	 *
27360Sstevel@tonic-gate 	 * task_join() returns with p_lock held.
27370Sstevel@tonic-gate 	 */
27380Sstevel@tonic-gate 	tk = task_create(0, zone);
27390Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
27400Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
27412768Ssl108498 
27422768Ssl108498 	pj = pp->p_task->tk_proj;
27432768Ssl108498 
27443247Sgjelinek 	mutex_enter(&zone->zone_mem_lock);
27452768Ssl108498 	zone->zone_locked_mem += pp->p_locked_mem;
27462768Ssl108498 	pj->kpj_data.kpd_locked_mem += pp->p_locked_mem;
27473247Sgjelinek 	mutex_exit(&zone->zone_mem_lock);
27480Sstevel@tonic-gate 
27490Sstevel@tonic-gate 	/*
27500Sstevel@tonic-gate 	 * add lwp counts to zsched's zone, and increment project's task count
27510Sstevel@tonic-gate 	 * due to the task created in the above tasksys_settaskid
27520Sstevel@tonic-gate 	 */
27532768Ssl108498 
27540Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
27550Sstevel@tonic-gate 	pj->kpj_nlwps += pp->p_lwpcnt;
27560Sstevel@tonic-gate 	pj->kpj_ntasks += 1;
27570Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
27580Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
27590Sstevel@tonic-gate 
27602768Ssl108498 	mutex_exit(&curproc->p_lock);
27612768Ssl108498 	mutex_exit(&cpu_lock);
27622768Ssl108498 	task_rele(oldtk);
27632768Ssl108498 
27640Sstevel@tonic-gate 	/*
27650Sstevel@tonic-gate 	 * The process was created by a process in the global zone, hence the
27660Sstevel@tonic-gate 	 * credentials are wrong.  We might as well have kcred-ish credentials.
27670Sstevel@tonic-gate 	 */
27680Sstevel@tonic-gate 	cr = zone->zone_kcred;
27690Sstevel@tonic-gate 	crhold(cr);
27700Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
27710Sstevel@tonic-gate 	oldcred = pp->p_cred;
27720Sstevel@tonic-gate 	pp->p_cred = cr;
27730Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
27740Sstevel@tonic-gate 	crfree(oldcred);
27750Sstevel@tonic-gate 
27760Sstevel@tonic-gate 	/*
27770Sstevel@tonic-gate 	 * Hold credentials again (for thread)
27780Sstevel@tonic-gate 	 */
27790Sstevel@tonic-gate 	crhold(cr);
27800Sstevel@tonic-gate 
27810Sstevel@tonic-gate 	/*
27820Sstevel@tonic-gate 	 * p_lwpcnt can't change since this is a kernel process.
27830Sstevel@tonic-gate 	 */
27840Sstevel@tonic-gate 	crset(pp, cr);
27850Sstevel@tonic-gate 
27860Sstevel@tonic-gate 	/*
27870Sstevel@tonic-gate 	 * Chroot
27880Sstevel@tonic-gate 	 */
27890Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp);
27900Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp);
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate 	/*
27930Sstevel@tonic-gate 	 * Initialize zone's rctl set.
27940Sstevel@tonic-gate 	 */
27950Sstevel@tonic-gate 	set = rctl_set_create();
27960Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
27970Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
27980Sstevel@tonic-gate 	e.rcep_p.zone = zone;
27990Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
28000Sstevel@tonic-gate 	zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp);
28010Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
28020Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate 	/*
28050Sstevel@tonic-gate 	 * Apply the rctls passed in to zone_create().  This is basically a list
28060Sstevel@tonic-gate 	 * assignment: all of the old values are removed and the new ones
28070Sstevel@tonic-gate 	 * inserted.  That is, if an empty list is passed in, all values are
28080Sstevel@tonic-gate 	 * removed.
28090Sstevel@tonic-gate 	 */
28100Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
28110Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
28120Sstevel@tonic-gate 		rctl_hndl_t hndl;
28130Sstevel@tonic-gate 		char *name;
28140Sstevel@tonic-gate 		nvlist_t **nvlarray;
28150Sstevel@tonic-gate 		uint_t i, nelem;
28160Sstevel@tonic-gate 		int error;	/* For ASSERT()s */
28170Sstevel@tonic-gate 
28180Sstevel@tonic-gate 		name = nvpair_name(nvp);
28190Sstevel@tonic-gate 		hndl = rctl_hndl_lookup(name);
28200Sstevel@tonic-gate 		ASSERT(hndl != -1);
28210Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
28220Sstevel@tonic-gate 		ASSERT(rde != NULL);
28230Sstevel@tonic-gate 
28240Sstevel@tonic-gate 		for (; /* ever */; ) {
28250Sstevel@tonic-gate 			rctl_val_t oval;
28260Sstevel@tonic-gate 
28270Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
28280Sstevel@tonic-gate 			error = rctl_local_get(hndl, NULL, &oval, pp);
28290Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
28300Sstevel@tonic-gate 			ASSERT(error == 0);	/* Can't fail for RCTL_FIRST */
28310Sstevel@tonic-gate 			ASSERT(oval.rcv_privilege != RCPRIV_BASIC);
28320Sstevel@tonic-gate 			if (oval.rcv_privilege == RCPRIV_SYSTEM)
28330Sstevel@tonic-gate 				break;
28340Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
28350Sstevel@tonic-gate 			error = rctl_local_delete(hndl, &oval, pp);
28360Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
28370Sstevel@tonic-gate 			ASSERT(error == 0);
28380Sstevel@tonic-gate 		}
28390Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
28400Sstevel@tonic-gate 		ASSERT(error == 0);
28410Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
28420Sstevel@tonic-gate 			rctl_val_t *nvalp;
28430Sstevel@tonic-gate 
28440Sstevel@tonic-gate 			nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
28450Sstevel@tonic-gate 			error = nvlist2rctlval(nvlarray[i], nvalp);
28460Sstevel@tonic-gate 			ASSERT(error == 0);
28470Sstevel@tonic-gate 			/*
28480Sstevel@tonic-gate 			 * rctl_local_insert can fail if the value being
28490Sstevel@tonic-gate 			 * inserted is a duplicate; this is OK.
28500Sstevel@tonic-gate 			 */
28510Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
28520Sstevel@tonic-gate 			if (rctl_local_insert(hndl, nvalp, pp) != 0)
28530Sstevel@tonic-gate 				kmem_cache_free(rctl_val_cache, nvalp);
28540Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
28550Sstevel@tonic-gate 		}
28560Sstevel@tonic-gate 	}
28570Sstevel@tonic-gate 	/*
28580Sstevel@tonic-gate 	 * Tell the world that we're done setting up.
28590Sstevel@tonic-gate 	 *
28600Sstevel@tonic-gate 	 * At this point we want to set the zone status to ZONE_IS_READY
28610Sstevel@tonic-gate 	 * and atomically set the zone's processor set visibility.  Once
28620Sstevel@tonic-gate 	 * we drop pool_lock() this zone will automatically get updated
28630Sstevel@tonic-gate 	 * to reflect any future changes to the pools configuration.
28640Sstevel@tonic-gate 	 */
28650Sstevel@tonic-gate 	pool_lock();
28660Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
28670Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
28680Sstevel@tonic-gate 	zone_uniqid(zone);
28690Sstevel@tonic-gate 	zone_zsd_configure(zone);
28700Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED)
28710Sstevel@tonic-gate 		zone_pset_set(zone, pool_default->pool_pset->pset_id);
28720Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
28730Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
28740Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_READY);
28750Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
28760Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
28770Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
28780Sstevel@tonic-gate 	pool_unlock();
28790Sstevel@tonic-gate 
28800Sstevel@tonic-gate 	/*
28810Sstevel@tonic-gate 	 * Once we see the zone transition to the ZONE_IS_BOOTING state,
28820Sstevel@tonic-gate 	 * we launch init, and set the state to running.
28830Sstevel@tonic-gate 	 */
28840Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched");
28850Sstevel@tonic-gate 
28860Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_BOOTING) {
28870Sstevel@tonic-gate 		id_t cid;
28880Sstevel@tonic-gate 
28890Sstevel@tonic-gate 		/*
28900Sstevel@tonic-gate 		 * Ok, this is a little complicated.  We need to grab the
28910Sstevel@tonic-gate 		 * zone's pool's scheduling class ID; note that by now, we
28920Sstevel@tonic-gate 		 * are already bound to a pool if we need to be (zoneadmd
28930Sstevel@tonic-gate 		 * will have done that to us while we're in the READY
28940Sstevel@tonic-gate 		 * state).  *But* the scheduling class for the zone's 'init'
28950Sstevel@tonic-gate 		 * must be explicitly passed to newproc, which doesn't
28960Sstevel@tonic-gate 		 * respect pool bindings.
28970Sstevel@tonic-gate 		 *
28980Sstevel@tonic-gate 		 * We hold the pool_lock across the call to newproc() to
28990Sstevel@tonic-gate 		 * close the obvious race: the pool's scheduling class
29000Sstevel@tonic-gate 		 * could change before we manage to create the LWP with
29010Sstevel@tonic-gate 		 * classid 'cid'.
29020Sstevel@tonic-gate 		 */
29030Sstevel@tonic-gate 		pool_lock();
29043247Sgjelinek 		if (zone->zone_defaultcid > 0)
29053247Sgjelinek 			cid = zone->zone_defaultcid;
29063247Sgjelinek 		else
29073247Sgjelinek 			cid = pool_get_class(zone->zone_pool);
29080Sstevel@tonic-gate 		if (cid == -1)
29090Sstevel@tonic-gate 			cid = defaultcid;
29100Sstevel@tonic-gate 
29110Sstevel@tonic-gate 		/*
29120Sstevel@tonic-gate 		 * If this fails, zone_boot will ultimately fail.  The
29130Sstevel@tonic-gate 		 * state of the zone will be set to SHUTTING_DOWN-- userland
29140Sstevel@tonic-gate 		 * will have to tear down the zone, and fail, or try again.
29150Sstevel@tonic-gate 		 */
29162267Sdp 		if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid,
29170Sstevel@tonic-gate 		    minclsyspri - 1, &ct)) != 0) {
29180Sstevel@tonic-gate 			mutex_enter(&zone_status_lock);
29190Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
29200Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
29210Sstevel@tonic-gate 		}
29220Sstevel@tonic-gate 		pool_unlock();
29230Sstevel@tonic-gate 	}
29240Sstevel@tonic-gate 
29250Sstevel@tonic-gate 	/*
29260Sstevel@tonic-gate 	 * Wait for zone_destroy() to be called.  This is what we spend
29270Sstevel@tonic-gate 	 * most of our life doing.
29280Sstevel@tonic-gate 	 */
29290Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched");
29300Sstevel@tonic-gate 
29310Sstevel@tonic-gate 	if (ct)
29320Sstevel@tonic-gate 		/*
29330Sstevel@tonic-gate 		 * At this point the process contract should be empty.
29340Sstevel@tonic-gate 		 * (Though if it isn't, it's not the end of the world.)
29350Sstevel@tonic-gate 		 */
29360Sstevel@tonic-gate 		VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0);
29370Sstevel@tonic-gate 
29380Sstevel@tonic-gate 	/*
29390Sstevel@tonic-gate 	 * Allow kcred to be freed when all referring processes
29400Sstevel@tonic-gate 	 * (including this one) go away.  We can't just do this in
29410Sstevel@tonic-gate 	 * zone_free because we need to wait for the zone_cred_ref to
29420Sstevel@tonic-gate 	 * drop to 0 before calling zone_free, and the existence of
29430Sstevel@tonic-gate 	 * zone_kcred will prevent that.  Thus, we call crfree here to
29440Sstevel@tonic-gate 	 * balance the crdup in zone_create.  The crhold calls earlier
29450Sstevel@tonic-gate 	 * in zsched will be dropped when the thread and process exit.
29460Sstevel@tonic-gate 	 */
29470Sstevel@tonic-gate 	crfree(zone->zone_kcred);
29480Sstevel@tonic-gate 	zone->zone_kcred = NULL;
29490Sstevel@tonic-gate 
29500Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
29510Sstevel@tonic-gate }
29520Sstevel@tonic-gate 
29530Sstevel@tonic-gate /*
29540Sstevel@tonic-gate  * Helper function to determine if there are any submounts of the
29550Sstevel@tonic-gate  * provided path.  Used to make sure the zone doesn't "inherit" any
29560Sstevel@tonic-gate  * mounts from before it is created.
29570Sstevel@tonic-gate  */
29580Sstevel@tonic-gate static uint_t
29590Sstevel@tonic-gate zone_mount_count(const char *rootpath)
29600Sstevel@tonic-gate {
29610Sstevel@tonic-gate 	vfs_t *vfsp;
29620Sstevel@tonic-gate 	uint_t count = 0;
29630Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
29640Sstevel@tonic-gate 
29650Sstevel@tonic-gate 	/*
29660Sstevel@tonic-gate 	 * Holding zonehash_lock prevents race conditions with
29670Sstevel@tonic-gate 	 * vfs_list_add()/vfs_list_remove() since we serialize with
29680Sstevel@tonic-gate 	 * zone_find_by_path().
29690Sstevel@tonic-gate 	 */
29700Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
29710Sstevel@tonic-gate 	/*
29720Sstevel@tonic-gate 	 * The rootpath must end with a '/'
29730Sstevel@tonic-gate 	 */
29740Sstevel@tonic-gate 	ASSERT(rootpath[rootpathlen - 1] == '/');
29750Sstevel@tonic-gate 
29760Sstevel@tonic-gate 	/*
29770Sstevel@tonic-gate 	 * This intentionally does not count the rootpath itself if that
29780Sstevel@tonic-gate 	 * happens to be a mount point.
29790Sstevel@tonic-gate 	 */
29800Sstevel@tonic-gate 	vfs_list_read_lock();
29810Sstevel@tonic-gate 	vfsp = rootvfs;
29820Sstevel@tonic-gate 	do {
29830Sstevel@tonic-gate 		if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt),
29840Sstevel@tonic-gate 		    rootpathlen) == 0)
29850Sstevel@tonic-gate 			count++;
29860Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
29870Sstevel@tonic-gate 	} while (vfsp != rootvfs);
29880Sstevel@tonic-gate 	vfs_list_unlock();
29890Sstevel@tonic-gate 	return (count);
29900Sstevel@tonic-gate }
29910Sstevel@tonic-gate 
29920Sstevel@tonic-gate /*
29930Sstevel@tonic-gate  * Helper function to make sure that a zone created on 'rootpath'
29940Sstevel@tonic-gate  * wouldn't end up containing other zones' rootpaths.
29950Sstevel@tonic-gate  */
29960Sstevel@tonic-gate static boolean_t
29970Sstevel@tonic-gate zone_is_nested(const char *rootpath)
29980Sstevel@tonic-gate {
29990Sstevel@tonic-gate 	zone_t *zone;
30000Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
30010Sstevel@tonic-gate 	size_t len;
30020Sstevel@tonic-gate 
30030Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
30040Sstevel@tonic-gate 
30050Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
30060Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
30070Sstevel@tonic-gate 		if (zone == global_zone)
30080Sstevel@tonic-gate 			continue;
30090Sstevel@tonic-gate 		len = strlen(zone->zone_rootpath);
30100Sstevel@tonic-gate 		if (strncmp(rootpath, zone->zone_rootpath,
30110Sstevel@tonic-gate 		    MIN(rootpathlen, len)) == 0)
30120Sstevel@tonic-gate 			return (B_TRUE);
30130Sstevel@tonic-gate 	}
30140Sstevel@tonic-gate 	return (B_FALSE);
30150Sstevel@tonic-gate }
30160Sstevel@tonic-gate 
30170Sstevel@tonic-gate static int
3018813Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs,
3019813Sdp     size_t zone_privssz)
30200Sstevel@tonic-gate {
30210Sstevel@tonic-gate 	priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
30220Sstevel@tonic-gate 
3023813Sdp 	if (zone_privssz < sizeof (priv_set_t))
3024813Sdp 		return (set_errno(ENOMEM));
3025813Sdp 
30260Sstevel@tonic-gate 	if (copyin(zone_privs, privs, sizeof (priv_set_t))) {
30270Sstevel@tonic-gate 		kmem_free(privs, sizeof (priv_set_t));
30280Sstevel@tonic-gate 		return (EFAULT);
30290Sstevel@tonic-gate 	}
30300Sstevel@tonic-gate 
30310Sstevel@tonic-gate 	zone->zone_privset = privs;
30320Sstevel@tonic-gate 	return (0);
30330Sstevel@tonic-gate }
30340Sstevel@tonic-gate 
30350Sstevel@tonic-gate /*
30360Sstevel@tonic-gate  * We make creative use of nvlists to pass in rctls from userland.  The list is
30370Sstevel@tonic-gate  * a list of the following structures:
30380Sstevel@tonic-gate  *
30390Sstevel@tonic-gate  * (name = rctl_name, value = nvpair_list_array)
30400Sstevel@tonic-gate  *
30410Sstevel@tonic-gate  * Where each element of the nvpair_list_array is of the form:
30420Sstevel@tonic-gate  *
30430Sstevel@tonic-gate  * [(name = "privilege", value = RCPRIV_PRIVILEGED),
30440Sstevel@tonic-gate  * 	(name = "limit", value = uint64_t),
30450Sstevel@tonic-gate  * 	(name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))]
30460Sstevel@tonic-gate  */
30470Sstevel@tonic-gate static int
30480Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp)
30490Sstevel@tonic-gate {
30500Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
30510Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
30520Sstevel@tonic-gate 	char *kbuf;
30530Sstevel@tonic-gate 	int error;
30540Sstevel@tonic-gate 	rctl_val_t rv;
30550Sstevel@tonic-gate 
30560Sstevel@tonic-gate 	*nvlp = NULL;
30570Sstevel@tonic-gate 
30580Sstevel@tonic-gate 	if (buflen == 0)
30590Sstevel@tonic-gate 		return (0);
30600Sstevel@tonic-gate 
30610Sstevel@tonic-gate 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
30620Sstevel@tonic-gate 		return (ENOMEM);
30630Sstevel@tonic-gate 	if (copyin(ubuf, kbuf, buflen)) {
30640Sstevel@tonic-gate 		error = EFAULT;
30650Sstevel@tonic-gate 		goto out;
30660Sstevel@tonic-gate 	}
30670Sstevel@tonic-gate 	if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) {
30680Sstevel@tonic-gate 		/*
30690Sstevel@tonic-gate 		 * nvl may have been allocated/free'd, but the value set to
30700Sstevel@tonic-gate 		 * non-NULL, so we reset it here.
30710Sstevel@tonic-gate 		 */
30720Sstevel@tonic-gate 		nvl = NULL;
30730Sstevel@tonic-gate 		error = EINVAL;
30740Sstevel@tonic-gate 		goto out;
30750Sstevel@tonic-gate 	}
30760Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
30770Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
30780Sstevel@tonic-gate 		rctl_hndl_t hndl;
30790Sstevel@tonic-gate 		nvlist_t **nvlarray;
30800Sstevel@tonic-gate 		uint_t i, nelem;
30810Sstevel@tonic-gate 		char *name;
30820Sstevel@tonic-gate 
30830Sstevel@tonic-gate 		error = EINVAL;
30840Sstevel@tonic-gate 		name = nvpair_name(nvp);
30850Sstevel@tonic-gate 		if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1)
30860Sstevel@tonic-gate 		    != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) {
30870Sstevel@tonic-gate 			goto out;
30880Sstevel@tonic-gate 		}
30890Sstevel@tonic-gate 		if ((hndl = rctl_hndl_lookup(name)) == -1) {
30900Sstevel@tonic-gate 			goto out;
30910Sstevel@tonic-gate 		}
30920Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
30930Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
30940Sstevel@tonic-gate 		ASSERT(error == 0);
30950Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
30960Sstevel@tonic-gate 			if (error = nvlist2rctlval(nvlarray[i], &rv))
30970Sstevel@tonic-gate 				goto out;
30980Sstevel@tonic-gate 		}
30990Sstevel@tonic-gate 		if (rctl_invalid_value(rde, &rv)) {
31000Sstevel@tonic-gate 			error = EINVAL;
31010Sstevel@tonic-gate 			goto out;
31020Sstevel@tonic-gate 		}
31030Sstevel@tonic-gate 	}
31040Sstevel@tonic-gate 	error = 0;
31050Sstevel@tonic-gate 	*nvlp = nvl;
31060Sstevel@tonic-gate out:
31070Sstevel@tonic-gate 	kmem_free(kbuf, buflen);
31080Sstevel@tonic-gate 	if (error && nvl != NULL)
31090Sstevel@tonic-gate 		nvlist_free(nvl);
31100Sstevel@tonic-gate 	return (error);
31110Sstevel@tonic-gate }
31120Sstevel@tonic-gate 
31130Sstevel@tonic-gate int
31140Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) {
31150Sstevel@tonic-gate 	if (er_out != NULL) {
31160Sstevel@tonic-gate 		if (copyout(&er_ext, er_out, sizeof (int))) {
31170Sstevel@tonic-gate 			return (set_errno(EFAULT));
31180Sstevel@tonic-gate 		}
31190Sstevel@tonic-gate 	}
31200Sstevel@tonic-gate 	return (set_errno(er_error));
31210Sstevel@tonic-gate }
31220Sstevel@tonic-gate 
31231676Sjpk static int
31241676Sjpk zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi)
31251676Sjpk {
31261676Sjpk 	ts_label_t *tsl;
31271676Sjpk 	bslabel_t blab;
31281676Sjpk 
31291676Sjpk 	/* Get label from user */
31301676Sjpk 	if (copyin(lab, &blab, sizeof (blab)) != 0)
31311676Sjpk 		return (EFAULT);
31321676Sjpk 	tsl = labelalloc(&blab, doi, KM_NOSLEEP);
31331676Sjpk 	if (tsl == NULL)
31341676Sjpk 		return (ENOMEM);
31351676Sjpk 
31361676Sjpk 	zone->zone_slabel = tsl;
31371676Sjpk 	return (0);
31381676Sjpk }
31391676Sjpk 
31400Sstevel@tonic-gate /*
3141789Sahrens  * Parses a comma-separated list of ZFS datasets into a per-zone dictionary.
3142789Sahrens  */
3143789Sahrens static int
3144789Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen)
3145789Sahrens {
3146789Sahrens 	char *kbuf;
3147789Sahrens 	char *dataset, *next;
3148789Sahrens 	zone_dataset_t *zd;
3149789Sahrens 	size_t len;
3150789Sahrens 
3151789Sahrens 	if (ubuf == NULL || buflen == 0)
3152789Sahrens 		return (0);
3153789Sahrens 
3154789Sahrens 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
3155789Sahrens 		return (ENOMEM);
3156789Sahrens 
3157789Sahrens 	if (copyin(ubuf, kbuf, buflen) != 0) {
3158789Sahrens 		kmem_free(kbuf, buflen);
3159789Sahrens 		return (EFAULT);
3160789Sahrens 	}
3161789Sahrens 
3162789Sahrens 	dataset = next = kbuf;
3163789Sahrens 	for (;;) {
3164789Sahrens 		zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP);
3165789Sahrens 
3166789Sahrens 		next = strchr(dataset, ',');
3167789Sahrens 
3168789Sahrens 		if (next == NULL)
3169789Sahrens 			len = strlen(dataset);
3170789Sahrens 		else
3171789Sahrens 			len = next - dataset;
3172789Sahrens 
3173789Sahrens 		zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP);
3174789Sahrens 		bcopy(dataset, zd->zd_dataset, len);
3175789Sahrens 		zd->zd_dataset[len] = '\0';
3176789Sahrens 
3177789Sahrens 		list_insert_head(&zone->zone_datasets, zd);
3178789Sahrens 
3179789Sahrens 		if (next == NULL)
3180789Sahrens 			break;
3181789Sahrens 
3182789Sahrens 		dataset = next + 1;
3183789Sahrens 	}
3184789Sahrens 
3185789Sahrens 	kmem_free(kbuf, buflen);
3186789Sahrens 	return (0);
3187789Sahrens }
3188789Sahrens 
3189789Sahrens /*
31900Sstevel@tonic-gate  * System call to create/initialize a new zone named 'zone_name', rooted
31910Sstevel@tonic-gate  * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs',
31921676Sjpk  * and initialized with the zone-wide rctls described in 'rctlbuf', and
31931676Sjpk  * with labeling set by 'match', 'doi', and 'label'.
31940Sstevel@tonic-gate  *
31950Sstevel@tonic-gate  * If extended error is non-null, we may use it to return more detailed
31960Sstevel@tonic-gate  * error information.
31970Sstevel@tonic-gate  */
31980Sstevel@tonic-gate static zoneid_t
31990Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root,
3200813Sdp     const priv_set_t *zone_privs, size_t zone_privssz,
3201813Sdp     caddr_t rctlbuf, size_t rctlbufsz,
32021676Sjpk     caddr_t zfsbuf, size_t zfsbufsz, int *extended_error,
32033448Sdh155122     int match, uint32_t doi, const bslabel_t *label,
32043448Sdh155122     int flags)
32050Sstevel@tonic-gate {
32060Sstevel@tonic-gate 	struct zsched_arg zarg;
32070Sstevel@tonic-gate 	nvlist_t *rctls = NULL;
32080Sstevel@tonic-gate 	proc_t *pp = curproc;
32090Sstevel@tonic-gate 	zone_t *zone, *ztmp;
32100Sstevel@tonic-gate 	zoneid_t zoneid;
32110Sstevel@tonic-gate 	int error;
32120Sstevel@tonic-gate 	int error2 = 0;
32130Sstevel@tonic-gate 	char *str;
32140Sstevel@tonic-gate 	cred_t *zkcr;
32151769Scarlsonj 	boolean_t insert_label_hash;
32160Sstevel@tonic-gate 
32170Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
32180Sstevel@tonic-gate 		return (set_errno(EPERM));
32190Sstevel@tonic-gate 
32200Sstevel@tonic-gate 	/* can't boot zone from within chroot environment */
32210Sstevel@tonic-gate 	if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir)
32220Sstevel@tonic-gate 		return (zone_create_error(ENOTSUP, ZE_CHROOTED,
3223813Sdp 		    extended_error));
32240Sstevel@tonic-gate 
32250Sstevel@tonic-gate 	zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP);
32260Sstevel@tonic-gate 	zoneid = zone->zone_id = id_alloc(zoneid_space);
32270Sstevel@tonic-gate 	zone->zone_status = ZONE_IS_UNINITIALIZED;
32280Sstevel@tonic-gate 	zone->zone_pool = pool_default;
32290Sstevel@tonic-gate 	zone->zone_pool_mod = gethrtime();
32300Sstevel@tonic-gate 	zone->zone_psetid = ZONE_PS_INVAL;
32310Sstevel@tonic-gate 	zone->zone_ncpus = 0;
32320Sstevel@tonic-gate 	zone->zone_ncpus_online = 0;
32332712Snn35248 	zone->zone_restart_init = B_TRUE;
32342712Snn35248 	zone->zone_brand = &native_brand;
32352712Snn35248 	zone->zone_initname = NULL;
32360Sstevel@tonic-gate 	mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL);
32370Sstevel@tonic-gate 	mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
32383247Sgjelinek 	mutex_init(&zone->zone_mem_lock, NULL, MUTEX_DEFAULT, NULL);
32390Sstevel@tonic-gate 	cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL);
32400Sstevel@tonic-gate 	list_create(&zone->zone_zsd, sizeof (struct zsd_entry),
32410Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
3242789Sahrens 	list_create(&zone->zone_datasets, sizeof (zone_dataset_t),
3243789Sahrens 	    offsetof(zone_dataset_t, zd_linkage));
32441676Sjpk 	rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
32450Sstevel@tonic-gate 
32463448Sdh155122 	if (flags & ZCF_NET_EXCL) {
32473448Sdh155122 		zone->zone_flags |= ZF_NET_EXCL;
32483448Sdh155122 	}
32493448Sdh155122 
32500Sstevel@tonic-gate 	if ((error = zone_set_name(zone, zone_name)) != 0) {
32510Sstevel@tonic-gate 		zone_free(zone);
32520Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
32530Sstevel@tonic-gate 	}
32540Sstevel@tonic-gate 
32550Sstevel@tonic-gate 	if ((error = zone_set_root(zone, zone_root)) != 0) {
32560Sstevel@tonic-gate 		zone_free(zone);
32570Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
32580Sstevel@tonic-gate 	}
3259813Sdp 	if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) {
32600Sstevel@tonic-gate 		zone_free(zone);
32610Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
32620Sstevel@tonic-gate 	}
32630Sstevel@tonic-gate 
32640Sstevel@tonic-gate 	/* initialize node name to be the same as zone name */
32650Sstevel@tonic-gate 	zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP);
32660Sstevel@tonic-gate 	(void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN);
32670Sstevel@tonic-gate 	zone->zone_nodename[_SYS_NMLN - 1] = '\0';
32680Sstevel@tonic-gate 
32690Sstevel@tonic-gate 	zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP);
32700Sstevel@tonic-gate 	zone->zone_domain[0] = '\0';
32710Sstevel@tonic-gate 	zone->zone_shares = 1;
32722677Sml93401 	zone->zone_shmmax = 0;
32732677Sml93401 	zone->zone_ipc.ipcq_shmmni = 0;
32742677Sml93401 	zone->zone_ipc.ipcq_semmni = 0;
32752677Sml93401 	zone->zone_ipc.ipcq_msgmni = 0;
32760Sstevel@tonic-gate 	zone->zone_bootargs = NULL;
32772267Sdp 	zone->zone_initname =
32782267Sdp 	    kmem_alloc(strlen(zone_default_initname) + 1, KM_SLEEP);
32792267Sdp 	(void) strcpy(zone->zone_initname, zone_default_initname);
32803247Sgjelinek 	zone->zone_nlwps = 0;
32813247Sgjelinek 	zone->zone_nlwps_ctl = INT_MAX;
32822768Ssl108498 	zone->zone_locked_mem = 0;
32832768Ssl108498 	zone->zone_locked_mem_ctl = UINT64_MAX;
32843247Sgjelinek 	zone->zone_max_swap = 0;
32853247Sgjelinek 	zone->zone_max_swap_ctl = UINT64_MAX;
32863247Sgjelinek 	zone0.zone_lockedmem_kstat = NULL;
32873247Sgjelinek 	zone0.zone_swapresv_kstat = NULL;
32880Sstevel@tonic-gate 
32890Sstevel@tonic-gate 	/*
32900Sstevel@tonic-gate 	 * Zsched initializes the rctls.
32910Sstevel@tonic-gate 	 */
32920Sstevel@tonic-gate 	zone->zone_rctls = NULL;
32930Sstevel@tonic-gate 
32940Sstevel@tonic-gate 	if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) {
32950Sstevel@tonic-gate 		zone_free(zone);
32960Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
32970Sstevel@tonic-gate 	}
32980Sstevel@tonic-gate 
3299789Sahrens 	if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) {
3300789Sahrens 		zone_free(zone);
3301789Sahrens 		return (set_errno(error));
3302789Sahrens 	}
3303789Sahrens 
33040Sstevel@tonic-gate 	/*
33051676Sjpk 	 * Read in the trusted system parameters:
33061676Sjpk 	 * match flag and sensitivity label.
33071676Sjpk 	 */
33081676Sjpk 	zone->zone_match = match;
33091769Scarlsonj 	if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) {
33101676Sjpk 		error = zone_set_label(zone, label, doi);
33111676Sjpk 		if (error != 0) {
33121676Sjpk 			zone_free(zone);
33131676Sjpk 			return (set_errno(error));
33141676Sjpk 		}
33151769Scarlsonj 		insert_label_hash = B_TRUE;
33161676Sjpk 	} else {
33171676Sjpk 		/* all zones get an admin_low label if system is not labeled */
33181676Sjpk 		zone->zone_slabel = l_admin_low;
33191676Sjpk 		label_hold(l_admin_low);
33201769Scarlsonj 		insert_label_hash = B_FALSE;
33211676Sjpk 	}
33221676Sjpk 
33231676Sjpk 	/*
33240Sstevel@tonic-gate 	 * Stop all lwps since that's what normally happens as part of fork().
33250Sstevel@tonic-gate 	 * This needs to happen before we grab any locks to avoid deadlock
33260Sstevel@tonic-gate 	 * (another lwp in the process could be waiting for the held lock).
33270Sstevel@tonic-gate 	 */
33280Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) {
33290Sstevel@tonic-gate 		zone_free(zone);
33300Sstevel@tonic-gate 		if (rctls)
33310Sstevel@tonic-gate 			nvlist_free(rctls);
33320Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
33330Sstevel@tonic-gate 	}
33340Sstevel@tonic-gate 
33350Sstevel@tonic-gate 	if (block_mounts() == 0) {
33360Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
33370Sstevel@tonic-gate 		if (curthread != pp->p_agenttp)
33380Sstevel@tonic-gate 			continuelwps(pp);
33390Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
33400Sstevel@tonic-gate 		zone_free(zone);
33410Sstevel@tonic-gate 		if (rctls)
33420Sstevel@tonic-gate 			nvlist_free(rctls);
33430Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
33440Sstevel@tonic-gate 	}
33450Sstevel@tonic-gate 
33460Sstevel@tonic-gate 	/*
33470Sstevel@tonic-gate 	 * Set up credential for kernel access.  After this, any errors
33480Sstevel@tonic-gate 	 * should go through the dance in errout rather than calling
33490Sstevel@tonic-gate 	 * zone_free directly.
33500Sstevel@tonic-gate 	 */
33510Sstevel@tonic-gate 	zone->zone_kcred = crdup(kcred);
33520Sstevel@tonic-gate 	crsetzone(zone->zone_kcred, zone);
33530Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred));
33540Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred));
33550Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred));
33560Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred));
33570Sstevel@tonic-gate 
33580Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
33590Sstevel@tonic-gate 	/*
33600Sstevel@tonic-gate 	 * Make sure zone doesn't already exist.
33611676Sjpk 	 *
33621676Sjpk 	 * If the system and zone are labeled,
33631676Sjpk 	 * make sure no other zone exists that has the same label.
33640Sstevel@tonic-gate 	 */
33651676Sjpk 	if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL ||
33661769Scarlsonj 	    (insert_label_hash &&
33671676Sjpk 	    (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) {
33680Sstevel@tonic-gate 		zone_status_t status;
33690Sstevel@tonic-gate 
33700Sstevel@tonic-gate 		status = zone_status_get(ztmp);
33710Sstevel@tonic-gate 		if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING)
33720Sstevel@tonic-gate 			error = EEXIST;
33730Sstevel@tonic-gate 		else
33740Sstevel@tonic-gate 			error = EBUSY;
33750Sstevel@tonic-gate 		goto errout;
33760Sstevel@tonic-gate 	}
33770Sstevel@tonic-gate 
33780Sstevel@tonic-gate 	/*
33790Sstevel@tonic-gate 	 * Don't allow zone creations which would cause one zone's rootpath to
33800Sstevel@tonic-gate 	 * be accessible from that of another (non-global) zone.
33810Sstevel@tonic-gate 	 */
33820Sstevel@tonic-gate 	if (zone_is_nested(zone->zone_rootpath)) {
33830Sstevel@tonic-gate 		error = EBUSY;
33840Sstevel@tonic-gate 		goto errout;
33850Sstevel@tonic-gate 	}
33860Sstevel@tonic-gate 
33870Sstevel@tonic-gate 	ASSERT(zonecount != 0);		/* check for leaks */
33880Sstevel@tonic-gate 	if (zonecount + 1 > maxzones) {
33890Sstevel@tonic-gate 		error = ENOMEM;
33900Sstevel@tonic-gate 		goto errout;
33910Sstevel@tonic-gate 	}
33920Sstevel@tonic-gate 
33930Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
33940Sstevel@tonic-gate 		error = EBUSY;
33950Sstevel@tonic-gate 		error2 = ZE_AREMOUNTS;
33960Sstevel@tonic-gate 		goto errout;
33970Sstevel@tonic-gate 	}
33980Sstevel@tonic-gate 
33990Sstevel@tonic-gate 	/*
34000Sstevel@tonic-gate 	 * Zone is still incomplete, but we need to drop all locks while
34010Sstevel@tonic-gate 	 * zsched() initializes this zone's kernel process.  We
34020Sstevel@tonic-gate 	 * optimistically add the zone to the hashtable and associated
34030Sstevel@tonic-gate 	 * lists so a parallel zone_create() doesn't try to create the
34040Sstevel@tonic-gate 	 * same zone.
34050Sstevel@tonic-gate 	 */
34060Sstevel@tonic-gate 	zonecount++;
34070Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid,
34080Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id,
34090Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
34100Sstevel@tonic-gate 	str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP);
34110Sstevel@tonic-gate 	(void) strcpy(str, zone->zone_name);
34120Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str,
34130Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
34141769Scarlsonj 	if (insert_label_hash) {
34151676Sjpk 		(void) mod_hash_insert(zonehashbylabel,
34161676Sjpk 		    (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone);
34171769Scarlsonj 		zone->zone_flags |= ZF_HASHED_LABEL;
34181676Sjpk 	}
34191676Sjpk 
34200Sstevel@tonic-gate 	/*
34210Sstevel@tonic-gate 	 * Insert into active list.  At this point there are no 'hold's
34220Sstevel@tonic-gate 	 * on the zone, but everyone else knows not to use it, so we can
34230Sstevel@tonic-gate 	 * continue to use it.  zsched() will do a zone_hold() if the
34240Sstevel@tonic-gate 	 * newproc() is successful.
34250Sstevel@tonic-gate 	 */
34260Sstevel@tonic-gate 	list_insert_tail(&zone_active, zone);
34270Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
34280Sstevel@tonic-gate 
34290Sstevel@tonic-gate 	zarg.zone = zone;
34300Sstevel@tonic-gate 	zarg.nvlist = rctls;
34310Sstevel@tonic-gate 	/*
34320Sstevel@tonic-gate 	 * The process, task, and project rctls are probably wrong;
34330Sstevel@tonic-gate 	 * we need an interface to get the default values of all rctls,
34340Sstevel@tonic-gate 	 * and initialize zsched appropriately.  I'm not sure that that
34350Sstevel@tonic-gate 	 * makes much of a difference, though.
34360Sstevel@tonic-gate 	 */
34370Sstevel@tonic-gate 	if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) {
34380Sstevel@tonic-gate 		/*
34390Sstevel@tonic-gate 		 * We need to undo all globally visible state.
34400Sstevel@tonic-gate 		 */
34410Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
34420Sstevel@tonic-gate 		list_remove(&zone_active, zone);
34431769Scarlsonj 		if (zone->zone_flags & ZF_HASHED_LABEL) {
34441676Sjpk 			ASSERT(zone->zone_slabel != NULL);
34451676Sjpk 			(void) mod_hash_destroy(zonehashbylabel,
34461676Sjpk 			    (mod_hash_key_t)zone->zone_slabel);
34471676Sjpk 		}
34480Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyname,
34490Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_name);
34500Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyid,
34510Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_id);
34520Sstevel@tonic-gate 		ASSERT(zonecount > 1);
34530Sstevel@tonic-gate 		zonecount--;
34540Sstevel@tonic-gate 		goto errout;
34550Sstevel@tonic-gate 	}
34560Sstevel@tonic-gate 
34570Sstevel@tonic-gate 	/*
34580Sstevel@tonic-gate 	 * Zone creation can't fail from now on.
34590Sstevel@tonic-gate 	 */
34600Sstevel@tonic-gate 
34610Sstevel@tonic-gate 	/*
34623247Sgjelinek 	 * Create zone kstats
34633247Sgjelinek 	 */
34643247Sgjelinek 	zone_kstat_create(zone);
34653247Sgjelinek 
34663247Sgjelinek 	/*
34670Sstevel@tonic-gate 	 * Let the other lwps continue.
34680Sstevel@tonic-gate 	 */
34690Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
34700Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
34710Sstevel@tonic-gate 		continuelwps(pp);
34720Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
34730Sstevel@tonic-gate 
34740Sstevel@tonic-gate 	/*
34750Sstevel@tonic-gate 	 * Wait for zsched to finish initializing the zone.
34760Sstevel@tonic-gate 	 */
34770Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_READY);
34780Sstevel@tonic-gate 	/*
34790Sstevel@tonic-gate 	 * The zone is fully visible, so we can let mounts progress.
34800Sstevel@tonic-gate 	 */
34810Sstevel@tonic-gate 	resume_mounts();
34820Sstevel@tonic-gate 	if (rctls)
34830Sstevel@tonic-gate 		nvlist_free(rctls);
34840Sstevel@tonic-gate 
34850Sstevel@tonic-gate 	return (zoneid);
34860Sstevel@tonic-gate 
34870Sstevel@tonic-gate errout:
34880Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
34890Sstevel@tonic-gate 	/*
34900Sstevel@tonic-gate 	 * Let the other lwps continue.
34910Sstevel@tonic-gate 	 */
34920Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
34930Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
34940Sstevel@tonic-gate 		continuelwps(pp);
34950Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
34960Sstevel@tonic-gate 
34970Sstevel@tonic-gate 	resume_mounts();
34980Sstevel@tonic-gate 	if (rctls)
34990Sstevel@tonic-gate 		nvlist_free(rctls);
35000Sstevel@tonic-gate 	/*
35010Sstevel@tonic-gate 	 * There is currently one reference to the zone, a cred_ref from
35020Sstevel@tonic-gate 	 * zone_kcred.  To free the zone, we call crfree, which will call
35030Sstevel@tonic-gate 	 * zone_cred_rele, which will call zone_free.
35040Sstevel@tonic-gate 	 */
35050Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 1);	/* for zone_kcred */
35060Sstevel@tonic-gate 	ASSERT(zone->zone_kcred->cr_ref == 1);
35070Sstevel@tonic-gate 	ASSERT(zone->zone_ref == 0);
35080Sstevel@tonic-gate 	zkcr = zone->zone_kcred;
35090Sstevel@tonic-gate 	zone->zone_kcred = NULL;
35100Sstevel@tonic-gate 	crfree(zkcr);				/* triggers call to zone_free */
35110Sstevel@tonic-gate 	return (zone_create_error(error, error2, extended_error));
35120Sstevel@tonic-gate }
35130Sstevel@tonic-gate 
35140Sstevel@tonic-gate /*
35150Sstevel@tonic-gate  * Cause the zone to boot.  This is pretty simple, since we let zoneadmd do
35162267Sdp  * the heavy lifting.  initname is the path to the program to launch
35172267Sdp  * at the "top" of the zone; if this is NULL, we use the system default,
35182267Sdp  * which is stored at zone_default_initname.
35190Sstevel@tonic-gate  */
35200Sstevel@tonic-gate static int
35212267Sdp zone_boot(zoneid_t zoneid)
35220Sstevel@tonic-gate {
35230Sstevel@tonic-gate 	int err;
35240Sstevel@tonic-gate 	zone_t *zone;
35250Sstevel@tonic-gate 
35260Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
35270Sstevel@tonic-gate 		return (set_errno(EPERM));
35280Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
35290Sstevel@tonic-gate 		return (set_errno(EINVAL));
35300Sstevel@tonic-gate 
35310Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
35320Sstevel@tonic-gate 	/*
35330Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with calls to
35340Sstevel@tonic-gate 	 * zone_shutdown, zone_destroy, etc.
35350Sstevel@tonic-gate 	 */
35360Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
35370Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
35380Sstevel@tonic-gate 		return (set_errno(EINVAL));
35390Sstevel@tonic-gate 	}
35400Sstevel@tonic-gate 
35410Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
35420Sstevel@tonic-gate 	if (zone_status_get(zone) != ZONE_IS_READY) {
35430Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
35440Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
35450Sstevel@tonic-gate 		return (set_errno(EINVAL));
35460Sstevel@tonic-gate 	}
35470Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_BOOTING);
35480Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
35490Sstevel@tonic-gate 
35500Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
35510Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
35520Sstevel@tonic-gate 
35530Sstevel@tonic-gate 	if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) {
35540Sstevel@tonic-gate 		zone_rele(zone);
35550Sstevel@tonic-gate 		return (set_errno(EINTR));
35560Sstevel@tonic-gate 	}
35570Sstevel@tonic-gate 
35580Sstevel@tonic-gate 	/*
35590Sstevel@tonic-gate 	 * Boot (starting init) might have failed, in which case the zone
35600Sstevel@tonic-gate 	 * will go to the SHUTTING_DOWN state; an appropriate errno will
35610Sstevel@tonic-gate 	 * be placed in zone->zone_boot_err, and so we return that.
35620Sstevel@tonic-gate 	 */
35630Sstevel@tonic-gate 	err = zone->zone_boot_err;
35640Sstevel@tonic-gate 	zone_rele(zone);
35650Sstevel@tonic-gate 	return (err ? set_errno(err) : 0);
35660Sstevel@tonic-gate }
35670Sstevel@tonic-gate 
35680Sstevel@tonic-gate /*
35690Sstevel@tonic-gate  * Kills all user processes in the zone, waiting for them all to exit
35700Sstevel@tonic-gate  * before returning.
35710Sstevel@tonic-gate  */
35720Sstevel@tonic-gate static int
35730Sstevel@tonic-gate zone_empty(zone_t *zone)
35740Sstevel@tonic-gate {
35750Sstevel@tonic-gate 	int waitstatus;
35760Sstevel@tonic-gate 
35770Sstevel@tonic-gate 	/*
35780Sstevel@tonic-gate 	 * We need to drop zonehash_lock before killing all
35790Sstevel@tonic-gate 	 * processes, otherwise we'll deadlock with zone_find_*
35800Sstevel@tonic-gate 	 * which can be called from the exit path.
35810Sstevel@tonic-gate 	 */
35820Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
35830Sstevel@tonic-gate 	while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz,
35840Sstevel@tonic-gate 	    ZONE_IS_EMPTY)) == -1) {
35850Sstevel@tonic-gate 		killall(zone->zone_id);
35860Sstevel@tonic-gate 	}
35870Sstevel@tonic-gate 	/*
35880Sstevel@tonic-gate 	 * return EINTR if we were signaled
35890Sstevel@tonic-gate 	 */
35900Sstevel@tonic-gate 	if (waitstatus == 0)
35910Sstevel@tonic-gate 		return (EINTR);
35920Sstevel@tonic-gate 	return (0);
35930Sstevel@tonic-gate }
35940Sstevel@tonic-gate 
35950Sstevel@tonic-gate /*
35961676Sjpk  * This function implements the policy for zone visibility.
35971676Sjpk  *
35981676Sjpk  * In standard Solaris, a non-global zone can only see itself.
35991676Sjpk  *
36001676Sjpk  * In Trusted Extensions, a labeled zone can lookup any zone whose label
36011676Sjpk  * it dominates. For this test, the label of the global zone is treated as
36021676Sjpk  * admin_high so it is special-cased instead of being checked for dominance.
36031676Sjpk  *
36041676Sjpk  * Returns true if zone attributes are viewable, false otherwise.
36051676Sjpk  */
36061676Sjpk static boolean_t
36071676Sjpk zone_list_access(zone_t *zone)
36081676Sjpk {
36091676Sjpk 
36101676Sjpk 	if (curproc->p_zone == global_zone ||
36111676Sjpk 	    curproc->p_zone == zone) {
36121676Sjpk 		return (B_TRUE);
36131769Scarlsonj 	} else if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) {
36141676Sjpk 		bslabel_t *curproc_label;
36151676Sjpk 		bslabel_t *zone_label;
36161676Sjpk 
36171676Sjpk 		curproc_label = label2bslabel(curproc->p_zone->zone_slabel);
36181676Sjpk 		zone_label = label2bslabel(zone->zone_slabel);
36191676Sjpk 
36201676Sjpk 		if (zone->zone_id != GLOBAL_ZONEID &&
36211676Sjpk 		    bldominates(curproc_label, zone_label)) {
36221676Sjpk 			return (B_TRUE);
36231676Sjpk 		} else {
36241676Sjpk 			return (B_FALSE);
36251676Sjpk 		}
36261676Sjpk 	} else {
36271676Sjpk 		return (B_FALSE);
36281676Sjpk 	}
36291676Sjpk }
36301676Sjpk 
36311676Sjpk /*
36320Sstevel@tonic-gate  * Systemcall to start the zone's halt sequence.  By the time this
36330Sstevel@tonic-gate  * function successfully returns, all user processes and kernel threads
36340Sstevel@tonic-gate  * executing in it will have exited, ZSD shutdown callbacks executed,
36350Sstevel@tonic-gate  * and the zone status set to ZONE_IS_DOWN.
36360Sstevel@tonic-gate  *
36370Sstevel@tonic-gate  * It is possible that the call will interrupt itself if the caller is the
36380Sstevel@tonic-gate  * parent of any process running in the zone, and doesn't have SIGCHLD blocked.
36390Sstevel@tonic-gate  */
36400Sstevel@tonic-gate static int
36410Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid)
36420Sstevel@tonic-gate {
36430Sstevel@tonic-gate 	int error;
36440Sstevel@tonic-gate 	zone_t *zone;
36450Sstevel@tonic-gate 	zone_status_t status;
36460Sstevel@tonic-gate 
36470Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
36480Sstevel@tonic-gate 		return (set_errno(EPERM));
36490Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
36500Sstevel@tonic-gate 		return (set_errno(EINVAL));
36510Sstevel@tonic-gate 
36520Sstevel@tonic-gate 	/*
36530Sstevel@tonic-gate 	 * Block mounts so that VFS_MOUNT() can get an accurate view of
36540Sstevel@tonic-gate 	 * the zone's status with regards to ZONE_IS_SHUTTING down.
36550Sstevel@tonic-gate 	 *
36560Sstevel@tonic-gate 	 * e.g. NFS can fail the mount if it determines that the zone
36570Sstevel@tonic-gate 	 * has already begun the shutdown sequence.
36580Sstevel@tonic-gate 	 */
36590Sstevel@tonic-gate 	if (block_mounts() == 0)
36600Sstevel@tonic-gate 		return (set_errno(EINTR));
36610Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
36620Sstevel@tonic-gate 	/*
36630Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
36640Sstevel@tonic-gate 	 * calls to zone_shutdown and zone_destroy.
36650Sstevel@tonic-gate 	 */
36660Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
36670Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
36680Sstevel@tonic-gate 		resume_mounts();
36690Sstevel@tonic-gate 		return (set_errno(EINVAL));
36700Sstevel@tonic-gate 	}
36710Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
36720Sstevel@tonic-gate 	status = zone_status_get(zone);
36730Sstevel@tonic-gate 	/*
36740Sstevel@tonic-gate 	 * Fail if the zone isn't fully initialized yet.
36750Sstevel@tonic-gate 	 */
36760Sstevel@tonic-gate 	if (status < ZONE_IS_READY) {
36770Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
36780Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
36790Sstevel@tonic-gate 		resume_mounts();
36800Sstevel@tonic-gate 		return (set_errno(EINVAL));
36810Sstevel@tonic-gate 	}
36820Sstevel@tonic-gate 	/*
36830Sstevel@tonic-gate 	 * If conditions required for zone_shutdown() to return have been met,
36840Sstevel@tonic-gate 	 * return success.
36850Sstevel@tonic-gate 	 */
36860Sstevel@tonic-gate 	if (status >= ZONE_IS_DOWN) {
36870Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
36880Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
36890Sstevel@tonic-gate 		resume_mounts();
36900Sstevel@tonic-gate 		return (0);
36910Sstevel@tonic-gate 	}
36920Sstevel@tonic-gate 	/*
36930Sstevel@tonic-gate 	 * If zone_shutdown() hasn't been called before, go through the motions.
36940Sstevel@tonic-gate 	 * If it has, there's nothing to do but wait for the kernel threads to
36950Sstevel@tonic-gate 	 * drain.
36960Sstevel@tonic-gate 	 */
36970Sstevel@tonic-gate 	if (status < ZONE_IS_EMPTY) {
36980Sstevel@tonic-gate 		uint_t ntasks;
36990Sstevel@tonic-gate 
37000Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
37010Sstevel@tonic-gate 		if ((ntasks = zone->zone_ntasks) != 1) {
37020Sstevel@tonic-gate 			/*
37030Sstevel@tonic-gate 			 * There's still stuff running.
37040Sstevel@tonic-gate 			 */
37050Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
37060Sstevel@tonic-gate 		}
37070Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
37080Sstevel@tonic-gate 		if (ntasks == 1) {
37090Sstevel@tonic-gate 			/*
37100Sstevel@tonic-gate 			 * The only way to create another task is through
37110Sstevel@tonic-gate 			 * zone_enter(), which will block until we drop
37120Sstevel@tonic-gate 			 * zonehash_lock.  The zone is empty.
37130Sstevel@tonic-gate 			 */
37140Sstevel@tonic-gate 			if (zone->zone_kthreads == NULL) {
37150Sstevel@tonic-gate 				/*
37160Sstevel@tonic-gate 				 * Skip ahead to ZONE_IS_DOWN
37170Sstevel@tonic-gate 				 */
37180Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_DOWN);
37190Sstevel@tonic-gate 			} else {
37200Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_EMPTY);
37210Sstevel@tonic-gate 			}
37220Sstevel@tonic-gate 		}
37230Sstevel@tonic-gate 	}
37240Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
37250Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
37260Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
37270Sstevel@tonic-gate 	resume_mounts();
37280Sstevel@tonic-gate 
37290Sstevel@tonic-gate 	if (error = zone_empty(zone)) {
37300Sstevel@tonic-gate 		zone_rele(zone);
37310Sstevel@tonic-gate 		return (set_errno(error));
37320Sstevel@tonic-gate 	}
37330Sstevel@tonic-gate 	/*
37340Sstevel@tonic-gate 	 * After the zone status goes to ZONE_IS_DOWN this zone will no
37350Sstevel@tonic-gate 	 * longer be notified of changes to the pools configuration, so
37360Sstevel@tonic-gate 	 * in order to not end up with a stale pool pointer, we point
37370Sstevel@tonic-gate 	 * ourselves at the default pool and remove all resource
37380Sstevel@tonic-gate 	 * visibility.  This is especially important as the zone_t may
37390Sstevel@tonic-gate 	 * languish on the deathrow for a very long time waiting for
37400Sstevel@tonic-gate 	 * cred's to drain out.
37410Sstevel@tonic-gate 	 *
37420Sstevel@tonic-gate 	 * This rebinding of the zone can happen multiple times
37430Sstevel@tonic-gate 	 * (presumably due to interrupted or parallel systemcalls)
37440Sstevel@tonic-gate 	 * without any adverse effects.
37450Sstevel@tonic-gate 	 */
37460Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
37470Sstevel@tonic-gate 		zone_rele(zone);
37480Sstevel@tonic-gate 		return (set_errno(EINTR));
37490Sstevel@tonic-gate 	}
37500Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
37510Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
37520Sstevel@tonic-gate 		zone_pool_set(zone, pool_default);
37530Sstevel@tonic-gate 		/*
37540Sstevel@tonic-gate 		 * The zone no longer needs to be able to see any cpus.
37550Sstevel@tonic-gate 		 */
37560Sstevel@tonic-gate 		zone_pset_set(zone, ZONE_PS_INVAL);
37570Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
37580Sstevel@tonic-gate 	}
37590Sstevel@tonic-gate 	pool_unlock();
37600Sstevel@tonic-gate 
37610Sstevel@tonic-gate 	/*
37620Sstevel@tonic-gate 	 * ZSD shutdown callbacks can be executed multiple times, hence
37630Sstevel@tonic-gate 	 * it is safe to not be holding any locks across this call.
37640Sstevel@tonic-gate 	 */
37650Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_SHUTDOWN);
37660Sstevel@tonic-gate 
37670Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
37680Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN)
37690Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DOWN);
37700Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
37710Sstevel@tonic-gate 
37720Sstevel@tonic-gate 	/*
37730Sstevel@tonic-gate 	 * Wait for kernel threads to drain.
37740Sstevel@tonic-gate 	 */
37750Sstevel@tonic-gate 	if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) {
37760Sstevel@tonic-gate 		zone_rele(zone);
37770Sstevel@tonic-gate 		return (set_errno(EINTR));
37780Sstevel@tonic-gate 	}
37792712Snn35248 
3780*3671Ssl108498 	/*
3781*3671Ssl108498 	 * Zone can be become down/destroyable even if the above wait
3782*3671Ssl108498 	 * returns EINTR, so any code added here may never execute.
3783*3671Ssl108498 	 * (i.e. don't add code here)
3784*3671Ssl108498 	 */
37852712Snn35248 
37860Sstevel@tonic-gate 	zone_rele(zone);
37870Sstevel@tonic-gate 	return (0);
37880Sstevel@tonic-gate }
37890Sstevel@tonic-gate 
37900Sstevel@tonic-gate /*
37910Sstevel@tonic-gate  * Systemcall entry point to finalize the zone halt process.  The caller
37922677Sml93401  * must have already successfully called zone_shutdown().
37930Sstevel@tonic-gate  *
37940Sstevel@tonic-gate  * Upon successful completion, the zone will have been fully destroyed:
37950Sstevel@tonic-gate  * zsched will have exited, destructor callbacks executed, and the zone
37960Sstevel@tonic-gate  * removed from the list of active zones.
37970Sstevel@tonic-gate  */
37980Sstevel@tonic-gate static int
37990Sstevel@tonic-gate zone_destroy(zoneid_t zoneid)
38000Sstevel@tonic-gate {
38010Sstevel@tonic-gate 	uint64_t uniqid;
38020Sstevel@tonic-gate 	zone_t *zone;
38030Sstevel@tonic-gate 	zone_status_t status;
38040Sstevel@tonic-gate 
38050Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
38060Sstevel@tonic-gate 		return (set_errno(EPERM));
38070Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
38080Sstevel@tonic-gate 		return (set_errno(EINVAL));
38090Sstevel@tonic-gate 
38100Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
38110Sstevel@tonic-gate 	/*
38120Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
38130Sstevel@tonic-gate 	 * calls to zone_destroy.
38140Sstevel@tonic-gate 	 */
38150Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
38160Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
38170Sstevel@tonic-gate 		return (set_errno(EINVAL));
38180Sstevel@tonic-gate 	}
38190Sstevel@tonic-gate 
38200Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
38210Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
38220Sstevel@tonic-gate 		return (set_errno(EBUSY));
38230Sstevel@tonic-gate 	}
38240Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
38250Sstevel@tonic-gate 	status = zone_status_get(zone);
38260Sstevel@tonic-gate 	if (status < ZONE_IS_DOWN) {
38270Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
38280Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
38290Sstevel@tonic-gate 		return (set_errno(EBUSY));
38300Sstevel@tonic-gate 	} else if (status == ZONE_IS_DOWN) {
38310Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */
38320Sstevel@tonic-gate 	}
38330Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
38340Sstevel@tonic-gate 	zone_hold(zone);
38350Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
38360Sstevel@tonic-gate 
38370Sstevel@tonic-gate 	/*
38380Sstevel@tonic-gate 	 * wait for zsched to exit
38390Sstevel@tonic-gate 	 */
38400Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_DEAD);
38410Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_DESTROY);
38423448Sdh155122 	zone->zone_netstack = NULL;
38430Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
38440Sstevel@tonic-gate 	zone_rele(zone);
38450Sstevel@tonic-gate 	zone = NULL;	/* potentially free'd */
38460Sstevel@tonic-gate 
38470Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
38480Sstevel@tonic-gate 	for (; /* ever */; ) {
38490Sstevel@tonic-gate 		boolean_t unref;
38500Sstevel@tonic-gate 
38510Sstevel@tonic-gate 		if ((zone = zone_find_all_by_id(zoneid)) == NULL ||
38520Sstevel@tonic-gate 		    zone->zone_uniqid != uniqid) {
38530Sstevel@tonic-gate 			/*
38540Sstevel@tonic-gate 			 * The zone has gone away.  Necessary conditions
38550Sstevel@tonic-gate 			 * are met, so we return success.
38560Sstevel@tonic-gate 			 */
38570Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
38580Sstevel@tonic-gate 			return (0);
38590Sstevel@tonic-gate 		}
38600Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
38610Sstevel@tonic-gate 		unref = ZONE_IS_UNREF(zone);
38620Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
38630Sstevel@tonic-gate 		if (unref) {
38640Sstevel@tonic-gate 			/*
38650Sstevel@tonic-gate 			 * There is only one reference to the zone -- that
38660Sstevel@tonic-gate 			 * added when the zone was added to the hashtables --
38670Sstevel@tonic-gate 			 * and things will remain this way until we drop
38680Sstevel@tonic-gate 			 * zonehash_lock... we can go ahead and cleanup the
38690Sstevel@tonic-gate 			 * zone.
38700Sstevel@tonic-gate 			 */
38710Sstevel@tonic-gate 			break;
38720Sstevel@tonic-gate 		}
38730Sstevel@tonic-gate 
38740Sstevel@tonic-gate 		if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) {
38750Sstevel@tonic-gate 			/* Signaled */
38760Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
38770Sstevel@tonic-gate 			return (set_errno(EINTR));
38780Sstevel@tonic-gate 		}
38790Sstevel@tonic-gate 
38800Sstevel@tonic-gate 	}
38810Sstevel@tonic-gate 
3882*3671Ssl108498 	/* Get rid of the zone's kstats. */
38833247Sgjelinek 	zone_kstat_delete(zone);
38843247Sgjelinek 
3885*3671Ssl108498 	/* Say goodbye to brand framework. */
3886*3671Ssl108498 	brand_unregister_zone(zone->zone_brand);
3887*3671Ssl108498 
38880Sstevel@tonic-gate 	/*
38890Sstevel@tonic-gate 	 * It is now safe to let the zone be recreated; remove it from the
38900Sstevel@tonic-gate 	 * lists.  The memory will not be freed until the last cred
38910Sstevel@tonic-gate 	 * reference goes away.
38920Sstevel@tonic-gate 	 */
38930Sstevel@tonic-gate 	ASSERT(zonecount > 1);	/* must be > 1; can't destroy global zone */
38940Sstevel@tonic-gate 	zonecount--;
38950Sstevel@tonic-gate 	/* remove from active list and hash tables */
38960Sstevel@tonic-gate 	list_remove(&zone_active, zone);
38970Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyname,
38980Sstevel@tonic-gate 	    (mod_hash_key_t)zone->zone_name);
38990Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyid,
39000Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id);
39011769Scarlsonj 	if (zone->zone_flags & ZF_HASHED_LABEL)
39021676Sjpk 		(void) mod_hash_destroy(zonehashbylabel,
39031676Sjpk 		    (mod_hash_key_t)zone->zone_slabel);
39040Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
39050Sstevel@tonic-gate 
3906766Scarlsonj 	/*
3907766Scarlsonj 	 * Release the root vnode; we're not using it anymore.  Nor should any
3908766Scarlsonj 	 * other thread that might access it exist.
3909766Scarlsonj 	 */
3910766Scarlsonj 	if (zone->zone_rootvp != NULL) {
3911766Scarlsonj 		VN_RELE(zone->zone_rootvp);
3912766Scarlsonj 		zone->zone_rootvp = NULL;
3913766Scarlsonj 	}
3914766Scarlsonj 
39150Sstevel@tonic-gate 	/* add to deathrow list */
39160Sstevel@tonic-gate 	mutex_enter(&zone_deathrow_lock);
39170Sstevel@tonic-gate 	list_insert_tail(&zone_deathrow, zone);
39180Sstevel@tonic-gate 	mutex_exit(&zone_deathrow_lock);
39190Sstevel@tonic-gate 
39200Sstevel@tonic-gate 	/*
39210Sstevel@tonic-gate 	 * Drop last reference (which was added by zsched()), this will
39220Sstevel@tonic-gate 	 * free the zone unless there are outstanding cred references.
39230Sstevel@tonic-gate 	 */
39240Sstevel@tonic-gate 	zone_rele(zone);
39250Sstevel@tonic-gate 	return (0);
39260Sstevel@tonic-gate }
39270Sstevel@tonic-gate 
39280Sstevel@tonic-gate /*
39290Sstevel@tonic-gate  * Systemcall entry point for zone_getattr(2).
39300Sstevel@tonic-gate  */
39310Sstevel@tonic-gate static ssize_t
39320Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
39330Sstevel@tonic-gate {
39340Sstevel@tonic-gate 	size_t size;
39350Sstevel@tonic-gate 	int error = 0, err;
39360Sstevel@tonic-gate 	zone_t *zone;
39370Sstevel@tonic-gate 	char *zonepath;
39382267Sdp 	char *outstr;
39390Sstevel@tonic-gate 	zone_status_t zone_status;
39400Sstevel@tonic-gate 	pid_t initpid;
39410Sstevel@tonic-gate 	boolean_t global = (curproc->p_zone == global_zone);
39421676Sjpk 	boolean_t curzone = (curproc->p_zone->zone_id == zoneid);
39433448Sdh155122 	ushort_t flags;
39440Sstevel@tonic-gate 
39450Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
39460Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
39470Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
39480Sstevel@tonic-gate 		return (set_errno(EINVAL));
39490Sstevel@tonic-gate 	}
39500Sstevel@tonic-gate 	zone_status = zone_status_get(zone);
39510Sstevel@tonic-gate 	if (zone_status < ZONE_IS_READY) {
39520Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
39530Sstevel@tonic-gate 		return (set_errno(EINVAL));
39540Sstevel@tonic-gate 	}
39550Sstevel@tonic-gate 	zone_hold(zone);
39560Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
39570Sstevel@tonic-gate 
39580Sstevel@tonic-gate 	/*
39591676Sjpk 	 * If not in the global zone, don't show information about other zones,
39601676Sjpk 	 * unless the system is labeled and the local zone's label dominates
39611676Sjpk 	 * the other zone.
39620Sstevel@tonic-gate 	 */
39631676Sjpk 	if (!zone_list_access(zone)) {
39640Sstevel@tonic-gate 		zone_rele(zone);
39650Sstevel@tonic-gate 		return (set_errno(EINVAL));
39660Sstevel@tonic-gate 	}
39670Sstevel@tonic-gate 
39680Sstevel@tonic-gate 	switch (attr) {
39690Sstevel@tonic-gate 	case ZONE_ATTR_ROOT:
39700Sstevel@tonic-gate 		if (global) {
39710Sstevel@tonic-gate 			/*
39720Sstevel@tonic-gate 			 * Copy the path to trim the trailing "/" (except for
39730Sstevel@tonic-gate 			 * the global zone).
39740Sstevel@tonic-gate 			 */
39750Sstevel@tonic-gate 			if (zone != global_zone)
39760Sstevel@tonic-gate 				size = zone->zone_rootpathlen - 1;
39770Sstevel@tonic-gate 			else
39780Sstevel@tonic-gate 				size = zone->zone_rootpathlen;
39790Sstevel@tonic-gate 			zonepath = kmem_alloc(size, KM_SLEEP);
39800Sstevel@tonic-gate 			bcopy(zone->zone_rootpath, zonepath, size);
39810Sstevel@tonic-gate 			zonepath[size - 1] = '\0';
39820Sstevel@tonic-gate 		} else {
39831676Sjpk 			if (curzone || !is_system_labeled()) {
39841676Sjpk 				/*
39851676Sjpk 				 * Caller is not in the global zone.
39861676Sjpk 				 * if the query is on the current zone
39871676Sjpk 				 * or the system is not labeled,
39881676Sjpk 				 * just return faked-up path for current zone.
39891676Sjpk 				 */
39901676Sjpk 				zonepath = "/";
39911676Sjpk 				size = 2;
39921676Sjpk 			} else {
39931676Sjpk 				/*
39941676Sjpk 				 * Return related path for current zone.
39951676Sjpk 				 */
39961676Sjpk 				int prefix_len = strlen(zone_prefix);
39971676Sjpk 				int zname_len = strlen(zone->zone_name);
39981676Sjpk 
39991676Sjpk 				size = prefix_len + zname_len + 1;
40001676Sjpk 				zonepath = kmem_alloc(size, KM_SLEEP);
40011676Sjpk 				bcopy(zone_prefix, zonepath, prefix_len);
40021676Sjpk 				bcopy(zone->zone_name, zonepath +
40032267Sdp 				    prefix_len, zname_len);
40041676Sjpk 				zonepath[size - 1] = '\0';
40051676Sjpk 			}
40060Sstevel@tonic-gate 		}
40070Sstevel@tonic-gate 		if (bufsize > size)
40080Sstevel@tonic-gate 			bufsize = size;
40090Sstevel@tonic-gate 		if (buf != NULL) {
40100Sstevel@tonic-gate 			err = copyoutstr(zonepath, buf, bufsize, NULL);
40110Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
40120Sstevel@tonic-gate 				error = EFAULT;
40130Sstevel@tonic-gate 		}
40141676Sjpk 		if (global || (is_system_labeled() && !curzone))
40150Sstevel@tonic-gate 			kmem_free(zonepath, size);
40160Sstevel@tonic-gate 		break;
40170Sstevel@tonic-gate 
40180Sstevel@tonic-gate 	case ZONE_ATTR_NAME:
40190Sstevel@tonic-gate 		size = strlen(zone->zone_name) + 1;
40200Sstevel@tonic-gate 		if (bufsize > size)
40210Sstevel@tonic-gate 			bufsize = size;
40220Sstevel@tonic-gate 		if (buf != NULL) {
40230Sstevel@tonic-gate 			err = copyoutstr(zone->zone_name, buf, bufsize, NULL);
40240Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
40250Sstevel@tonic-gate 				error = EFAULT;
40260Sstevel@tonic-gate 		}
40270Sstevel@tonic-gate 		break;
40280Sstevel@tonic-gate 
40290Sstevel@tonic-gate 	case ZONE_ATTR_STATUS:
40300Sstevel@tonic-gate 		/*
40310Sstevel@tonic-gate 		 * Since we're not holding zonehash_lock, the zone status
40320Sstevel@tonic-gate 		 * may be anything; leave it up to userland to sort it out.
40330Sstevel@tonic-gate 		 */
40340Sstevel@tonic-gate 		size = sizeof (zone_status);
40350Sstevel@tonic-gate 		if (bufsize > size)
40360Sstevel@tonic-gate 			bufsize = size;
40370Sstevel@tonic-gate 		zone_status = zone_status_get(zone);
40380Sstevel@tonic-gate 		if (buf != NULL &&
40390Sstevel@tonic-gate 		    copyout(&zone_status, buf, bufsize) != 0)
40400Sstevel@tonic-gate 			error = EFAULT;
40410Sstevel@tonic-gate 		break;
40423448Sdh155122 	case ZONE_ATTR_FLAGS:
40433448Sdh155122 		size = sizeof (zone->zone_flags);
40443448Sdh155122 		if (bufsize > size)
40453448Sdh155122 			bufsize = size;
40463448Sdh155122 		flags = zone->zone_flags;
40473448Sdh155122 		if (buf != NULL &&
40483448Sdh155122 		    copyout(&flags, buf, bufsize) != 0)
40493448Sdh155122 			error = EFAULT;
40503448Sdh155122 		break;
40510Sstevel@tonic-gate 	case ZONE_ATTR_PRIVSET:
40520Sstevel@tonic-gate 		size = sizeof (priv_set_t);
40530Sstevel@tonic-gate 		if (bufsize > size)
40540Sstevel@tonic-gate 			bufsize = size;
40550Sstevel@tonic-gate 		if (buf != NULL &&
40560Sstevel@tonic-gate 		    copyout(zone->zone_privset, buf, bufsize) != 0)
40570Sstevel@tonic-gate 			error = EFAULT;
40580Sstevel@tonic-gate 		break;
40590Sstevel@tonic-gate 	case ZONE_ATTR_UNIQID:
40600Sstevel@tonic-gate 		size = sizeof (zone->zone_uniqid);
40610Sstevel@tonic-gate 		if (bufsize > size)
40620Sstevel@tonic-gate 			bufsize = size;
40630Sstevel@tonic-gate 		if (buf != NULL &&
40640Sstevel@tonic-gate 		    copyout(&zone->zone_uniqid, buf, bufsize) != 0)
40650Sstevel@tonic-gate 			error = EFAULT;
40660Sstevel@tonic-gate 		break;
40670Sstevel@tonic-gate 	case ZONE_ATTR_POOLID:
40680Sstevel@tonic-gate 		{
40690Sstevel@tonic-gate 			pool_t *pool;
40700Sstevel@tonic-gate 			poolid_t poolid;
40710Sstevel@tonic-gate 
40720Sstevel@tonic-gate 			if (pool_lock_intr() != 0) {
40730Sstevel@tonic-gate 				error = EINTR;
40740Sstevel@tonic-gate 				break;
40750Sstevel@tonic-gate 			}
40760Sstevel@tonic-gate 			pool = zone_pool_get(zone);
40770Sstevel@tonic-gate 			poolid = pool->pool_id;
40780Sstevel@tonic-gate 			pool_unlock();
40790Sstevel@tonic-gate 			size = sizeof (poolid);
40800Sstevel@tonic-gate 			if (bufsize > size)
40810Sstevel@tonic-gate 				bufsize = size;
40820Sstevel@tonic-gate 			if (buf != NULL && copyout(&poolid, buf, size) != 0)
40830Sstevel@tonic-gate 				error = EFAULT;
40840Sstevel@tonic-gate 		}
40850Sstevel@tonic-gate 		break;
40861676Sjpk 	case ZONE_ATTR_SLBL:
40871676Sjpk 		size = sizeof (bslabel_t);
40881676Sjpk 		if (bufsize > size)
40891676Sjpk 			bufsize = size;
40901676Sjpk 		if (zone->zone_slabel == NULL)
40911676Sjpk 			error = EINVAL;
40921676Sjpk 		else if (buf != NULL &&
40931676Sjpk 		    copyout(label2bslabel(zone->zone_slabel), buf,
40941676Sjpk 		    bufsize) != 0)
40951676Sjpk 			error = EFAULT;
40961676Sjpk 		break;
40970Sstevel@tonic-gate 	case ZONE_ATTR_INITPID:
40980Sstevel@tonic-gate 		size = sizeof (initpid);
40990Sstevel@tonic-gate 		if (bufsize > size)
41000Sstevel@tonic-gate 			bufsize = size;
41010Sstevel@tonic-gate 		initpid = zone->zone_proc_initpid;
41020Sstevel@tonic-gate 		if (initpid == -1) {
41030Sstevel@tonic-gate 			error = ESRCH;
41040Sstevel@tonic-gate 			break;
41050Sstevel@tonic-gate 		}
41060Sstevel@tonic-gate 		if (buf != NULL &&
41070Sstevel@tonic-gate 		    copyout(&initpid, buf, bufsize) != 0)
41080Sstevel@tonic-gate 			error = EFAULT;
41090Sstevel@tonic-gate 		break;
41102712Snn35248 	case ZONE_ATTR_BRAND:
41112712Snn35248 		size = strlen(zone->zone_brand->b_name) + 1;
41122712Snn35248 
41132712Snn35248 		if (bufsize > size)
41142712Snn35248 			bufsize = size;
41152712Snn35248 		if (buf != NULL) {
41162712Snn35248 			err = copyoutstr(zone->zone_brand->b_name, buf,
41172712Snn35248 			    bufsize, NULL);
41182712Snn35248 			if (err != 0 && err != ENAMETOOLONG)
41192712Snn35248 				error = EFAULT;
41202712Snn35248 		}
41212712Snn35248 		break;
41222267Sdp 	case ZONE_ATTR_INITNAME:
41232267Sdp 		size = strlen(zone->zone_initname) + 1;
41242267Sdp 		if (bufsize > size)
41252267Sdp 			bufsize = size;
41262267Sdp 		if (buf != NULL) {
41272267Sdp 			err = copyoutstr(zone->zone_initname, buf, bufsize,
41282267Sdp 			    NULL);
41292267Sdp 			if (err != 0 && err != ENAMETOOLONG)
41302267Sdp 				error = EFAULT;
41312267Sdp 		}
41322267Sdp 		break;
41332267Sdp 	case ZONE_ATTR_BOOTARGS:
41342267Sdp 		if (zone->zone_bootargs == NULL)
41352267Sdp 			outstr = "";
41362267Sdp 		else
41372267Sdp 			outstr = zone->zone_bootargs;
41382267Sdp 		size = strlen(outstr) + 1;
41392267Sdp 		if (bufsize > size)
41402267Sdp 			bufsize = size;
41412267Sdp 		if (buf != NULL) {
41422267Sdp 			err = copyoutstr(outstr, buf, bufsize, NULL);
41432267Sdp 			if (err != 0 && err != ENAMETOOLONG)
41442267Sdp 				error = EFAULT;
41452267Sdp 		}
41462267Sdp 		break;
41473247Sgjelinek 	case ZONE_ATTR_PHYS_MCAP:
41483247Sgjelinek 		size = sizeof (zone->zone_phys_mcap);
41493247Sgjelinek 		if (bufsize > size)
41503247Sgjelinek 			bufsize = size;
41513247Sgjelinek 		if (buf != NULL &&
41523247Sgjelinek 		    copyout(&zone->zone_phys_mcap, buf, bufsize) != 0)
41533247Sgjelinek 			error = EFAULT;
41543247Sgjelinek 		break;
41553247Sgjelinek 	case ZONE_ATTR_SCHED_CLASS:
41563247Sgjelinek 		mutex_enter(&class_lock);
41573247Sgjelinek 
41583247Sgjelinek 		if (zone->zone_defaultcid >= loaded_classes)
41593247Sgjelinek 			outstr = "";
41603247Sgjelinek 		else
41613247Sgjelinek 			outstr = sclass[zone->zone_defaultcid].cl_name;
41623247Sgjelinek 		size = strlen(outstr) + 1;
41633247Sgjelinek 		if (bufsize > size)
41643247Sgjelinek 			bufsize = size;
41653247Sgjelinek 		if (buf != NULL) {
41663247Sgjelinek 			err = copyoutstr(outstr, buf, bufsize, NULL);
41673247Sgjelinek 			if (err != 0 && err != ENAMETOOLONG)
41683247Sgjelinek 				error = EFAULT;
41693247Sgjelinek 		}
41703247Sgjelinek 
41713247Sgjelinek 		mutex_exit(&class_lock);
41723247Sgjelinek 		break;
41730Sstevel@tonic-gate 	default:
41742712Snn35248 		if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) {
41752712Snn35248 			size = bufsize;
41762712Snn35248 			error = ZBROP(zone)->b_getattr(zone, attr, buf, &size);
41772712Snn35248 		} else {
41782712Snn35248 			error = EINVAL;
41792712Snn35248 		}
41800Sstevel@tonic-gate 	}
41810Sstevel@tonic-gate 	zone_rele(zone);
41820Sstevel@tonic-gate 
41830Sstevel@tonic-gate 	if (error)
41840Sstevel@tonic-gate 		return (set_errno(error));
41850Sstevel@tonic-gate 	return ((ssize_t)size);
41860Sstevel@tonic-gate }
41870Sstevel@tonic-gate 
41880Sstevel@tonic-gate /*
41892267Sdp  * Systemcall entry point for zone_setattr(2).
41902267Sdp  */
41912267Sdp /*ARGSUSED*/
41922267Sdp static int
41932267Sdp zone_setattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
41942267Sdp {
41952267Sdp 	zone_t *zone;
41962267Sdp 	zone_status_t zone_status;
41972712Snn35248 	struct brand_attr *attrp;
41982267Sdp 	int err;
41992267Sdp 
42002267Sdp 	if (secpolicy_zone_config(CRED()) != 0)
42012267Sdp 		return (set_errno(EPERM));
42022267Sdp 
42032267Sdp 	/*
42043247Sgjelinek 	 * Only the ZONE_ATTR_PHYS_MCAP attribute can be set on the
42053247Sgjelinek 	 * global zone.
42062267Sdp 	 */
42073247Sgjelinek 	if (zoneid == GLOBAL_ZONEID && attr != ZONE_ATTR_PHYS_MCAP) {
42082267Sdp 		return (set_errno(EINVAL));
42092267Sdp 	}
42102267Sdp 
42112267Sdp 	mutex_enter(&zonehash_lock);
42122267Sdp 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
42132267Sdp 		mutex_exit(&zonehash_lock);
42142267Sdp 		return (set_errno(EINVAL));
42152267Sdp 	}
42162267Sdp 	zone_hold(zone);
42172267Sdp 	mutex_exit(&zonehash_lock);
42182267Sdp 
42193247Sgjelinek 	/*
42203247Sgjelinek 	 * At present most attributes can only be set on non-running,
42213247Sgjelinek 	 * non-global zones.
42223247Sgjelinek 	 */
42232267Sdp 	zone_status = zone_status_get(zone);
42243247Sgjelinek 	if (attr != ZONE_ATTR_PHYS_MCAP && zone_status > ZONE_IS_READY)
42252267Sdp 		goto done;
42262267Sdp 
42272267Sdp 	switch (attr) {
42282267Sdp 	case ZONE_ATTR_INITNAME:
42292267Sdp 		err = zone_set_initname(zone, (const char *)buf);
42302267Sdp 		break;
42312267Sdp 	case ZONE_ATTR_BOOTARGS:
42322267Sdp 		err = zone_set_bootargs(zone, (const char *)buf);
42332267Sdp 		break;
42342712Snn35248 	case ZONE_ATTR_BRAND:
42352712Snn35248 		ASSERT(!ZONE_IS_BRANDED(zone));
42362712Snn35248 		err = 0;
42372712Snn35248 		attrp = kmem_alloc(sizeof (struct brand_attr), KM_SLEEP);
42382712Snn35248 		if ((buf == NULL) ||
42392712Snn35248 		    (copyin(buf, attrp, sizeof (struct brand_attr)) != 0)) {
42402712Snn35248 			kmem_free(attrp, sizeof (struct brand_attr));
42412712Snn35248 			err = EFAULT;
42422712Snn35248 			break;
42432712Snn35248 		}
42442712Snn35248 
42452712Snn35248 		if (is_system_labeled() && strncmp(attrp->ba_brandname,
42462712Snn35248 		    NATIVE_BRAND_NAME, MAXNAMELEN) != 0) {
42472712Snn35248 			err = EPERM;
42482712Snn35248 			break;
42492712Snn35248 		}
42502712Snn35248 
42512712Snn35248 		zone->zone_brand = brand_register_zone(attrp);
42522712Snn35248 		kmem_free(attrp, sizeof (struct brand_attr));
42532712Snn35248 		if (zone->zone_brand == NULL)
42542712Snn35248 			err = EINVAL;
42552712Snn35248 		break;
42563247Sgjelinek 	case ZONE_ATTR_PHYS_MCAP:
42573247Sgjelinek 		err = zone_set_phys_mcap(zone, (const uint64_t *)buf);
42583247Sgjelinek 		break;
42593247Sgjelinek 	case ZONE_ATTR_SCHED_CLASS:
42603247Sgjelinek 		err = zone_set_sched_class(zone, (const char *)buf);
42613247Sgjelinek 		break;
42622267Sdp 	default:
42632712Snn35248 		if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone))
42642712Snn35248 			err = ZBROP(zone)->b_setattr(zone, attr, buf, bufsize);
42652712Snn35248 		else
42662712Snn35248 			err = EINVAL;
42672267Sdp 	}
42682267Sdp 
42692267Sdp done:
42702267Sdp 	zone_rele(zone);
42712267Sdp 	return (err != 0 ? set_errno(err) : 0);
42722267Sdp }
42732267Sdp 
42742267Sdp /*
42750Sstevel@tonic-gate  * Return zero if the process has at least one vnode mapped in to its
42760Sstevel@tonic-gate  * address space which shouldn't be allowed to change zones.
42773247Sgjelinek  *
42783247Sgjelinek  * Also return zero if the process has any shared mappings which reserve
42793247Sgjelinek  * swap.  This is because the counting for zone.max-swap does not allow swap
42803247Sgjelinek  * revervation to be shared between zones.  zone swap reservation is counted
42813247Sgjelinek  * on zone->zone_max_swap.
42820Sstevel@tonic-gate  */
42830Sstevel@tonic-gate static int
42840Sstevel@tonic-gate as_can_change_zones(void)
42850Sstevel@tonic-gate {
42860Sstevel@tonic-gate 	proc_t *pp = curproc;
42870Sstevel@tonic-gate 	struct seg *seg;
42880Sstevel@tonic-gate 	struct as *as = pp->p_as;
42890Sstevel@tonic-gate 	vnode_t *vp;
42900Sstevel@tonic-gate 	int allow = 1;
42910Sstevel@tonic-gate 
42920Sstevel@tonic-gate 	ASSERT(pp->p_as != &kas);
42933247Sgjelinek 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
42940Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
42953247Sgjelinek 
42963247Sgjelinek 		/*
42973247Sgjelinek 		 * Cannot enter zone with shared anon memory which
42983247Sgjelinek 		 * reserves swap.  See comment above.
42993247Sgjelinek 		 */
43003247Sgjelinek 		if (seg_can_change_zones(seg) == B_FALSE) {
43013247Sgjelinek 			allow = 0;
43023247Sgjelinek 			break;
43033247Sgjelinek 		}
43040Sstevel@tonic-gate 		/*
43050Sstevel@tonic-gate 		 * if we can't get a backing vnode for this segment then skip
43060Sstevel@tonic-gate 		 * it.
43070Sstevel@tonic-gate 		 */
43080Sstevel@tonic-gate 		vp = NULL;
43090Sstevel@tonic-gate 		if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL)
43100Sstevel@tonic-gate 			continue;
43110Sstevel@tonic-gate 		if (!vn_can_change_zones(vp)) { /* bail on first match */
43120Sstevel@tonic-gate 			allow = 0;
43130Sstevel@tonic-gate 			break;
43140Sstevel@tonic-gate 		}
43150Sstevel@tonic-gate 	}
43163247Sgjelinek 	AS_LOCK_EXIT(as, &as->a_lock);
43170Sstevel@tonic-gate 	return (allow);
43180Sstevel@tonic-gate }
43190Sstevel@tonic-gate 
43200Sstevel@tonic-gate /*
43213247Sgjelinek  * Count swap reserved by curproc's address space
43223247Sgjelinek  */
43233247Sgjelinek static size_t
43243247Sgjelinek as_swresv(void)
43253247Sgjelinek {
43263247Sgjelinek 	proc_t *pp = curproc;
43273247Sgjelinek 	struct seg *seg;
43283247Sgjelinek 	struct as *as = pp->p_as;
43293247Sgjelinek 	size_t swap = 0;
43303247Sgjelinek 
43313247Sgjelinek 	ASSERT(pp->p_as != &kas);
43323247Sgjelinek 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
43333247Sgjelinek 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg))
43343247Sgjelinek 		swap += seg_swresv(seg);
43353247Sgjelinek 
43363247Sgjelinek 	return (swap);
43373247Sgjelinek }
43383247Sgjelinek 
43393247Sgjelinek /*
43400Sstevel@tonic-gate  * Systemcall entry point for zone_enter().
43410Sstevel@tonic-gate  *
43420Sstevel@tonic-gate  * The current process is injected into said zone.  In the process
43430Sstevel@tonic-gate  * it will change its project membership, privileges, rootdir/cwd,
43440Sstevel@tonic-gate  * zone-wide rctls, and pool association to match those of the zone.
43450Sstevel@tonic-gate  *
43460Sstevel@tonic-gate  * The first zone_enter() called while the zone is in the ZONE_IS_READY
43470Sstevel@tonic-gate  * state will transition it to ZONE_IS_RUNNING.  Processes may only
43480Sstevel@tonic-gate  * enter a zone that is "ready" or "running".
43490Sstevel@tonic-gate  */
43500Sstevel@tonic-gate static int
43510Sstevel@tonic-gate zone_enter(zoneid_t zoneid)
43520Sstevel@tonic-gate {
43530Sstevel@tonic-gate 	zone_t *zone;
43540Sstevel@tonic-gate 	vnode_t *vp;
43550Sstevel@tonic-gate 	proc_t *pp = curproc;
43560Sstevel@tonic-gate 	contract_t *ct;
43570Sstevel@tonic-gate 	cont_process_t *ctp;
43580Sstevel@tonic-gate 	task_t *tk, *oldtk;
43590Sstevel@tonic-gate 	kproject_t *zone_proj0;
43600Sstevel@tonic-gate 	cred_t *cr, *newcr;
43610Sstevel@tonic-gate 	pool_t *oldpool, *newpool;
43620Sstevel@tonic-gate 	sess_t *sp;
43630Sstevel@tonic-gate 	uid_t uid;
43640Sstevel@tonic-gate 	zone_status_t status;
43650Sstevel@tonic-gate 	int err = 0;
43660Sstevel@tonic-gate 	rctl_entity_p_t e;
43673247Sgjelinek 	size_t swap;
43680Sstevel@tonic-gate 
43690Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
43700Sstevel@tonic-gate 		return (set_errno(EPERM));
43710Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
43720Sstevel@tonic-gate 		return (set_errno(EINVAL));
43730Sstevel@tonic-gate 
43740Sstevel@tonic-gate 	/*
43750Sstevel@tonic-gate 	 * Stop all lwps so we don't need to hold a lock to look at
43760Sstevel@tonic-gate 	 * curproc->p_zone.  This needs to happen before we grab any
43770Sstevel@tonic-gate 	 * locks to avoid deadlock (another lwp in the process could
43780Sstevel@tonic-gate 	 * be waiting for the held lock).
43790Sstevel@tonic-gate 	 */
43800Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK))
43810Sstevel@tonic-gate 		return (set_errno(EINTR));
43820Sstevel@tonic-gate 
43830Sstevel@tonic-gate 	/*
43840Sstevel@tonic-gate 	 * Make sure we're not changing zones with files open or mapped in
43850Sstevel@tonic-gate 	 * to our address space which shouldn't be changing zones.
43860Sstevel@tonic-gate 	 */
43870Sstevel@tonic-gate 	if (!files_can_change_zones()) {
43880Sstevel@tonic-gate 		err = EBADF;
43890Sstevel@tonic-gate 		goto out;
43900Sstevel@tonic-gate 	}
43910Sstevel@tonic-gate 	if (!as_can_change_zones()) {
43920Sstevel@tonic-gate 		err = EFAULT;
43930Sstevel@tonic-gate 		goto out;
43940Sstevel@tonic-gate 	}
43950Sstevel@tonic-gate 
43960Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
43970Sstevel@tonic-gate 	if (pp->p_zone != global_zone) {
43980Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
43990Sstevel@tonic-gate 		err = EINVAL;
44000Sstevel@tonic-gate 		goto out;
44010Sstevel@tonic-gate 	}
44020Sstevel@tonic-gate 
44030Sstevel@tonic-gate 	zone = zone_find_all_by_id(zoneid);
44040Sstevel@tonic-gate 	if (zone == NULL) {
44050Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
44060Sstevel@tonic-gate 		err = EINVAL;
44070Sstevel@tonic-gate 		goto out;
44080Sstevel@tonic-gate 	}
44090Sstevel@tonic-gate 
44100Sstevel@tonic-gate 	/*
44110Sstevel@tonic-gate 	 * To prevent processes in a zone from holding contracts on
44120Sstevel@tonic-gate 	 * extrazonal resources, and to avoid process contract
44130Sstevel@tonic-gate 	 * memberships which span zones, contract holders and processes
44140Sstevel@tonic-gate 	 * which aren't the sole members of their encapsulating process
44150Sstevel@tonic-gate 	 * contracts are not allowed to zone_enter.
44160Sstevel@tonic-gate 	 */
44170Sstevel@tonic-gate 	ctp = pp->p_ct_process;
44180Sstevel@tonic-gate 	ct = &ctp->conp_contract;
44190Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
44200Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
44210Sstevel@tonic-gate 	if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) {
44220Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
44230Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
44240Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
44250Sstevel@tonic-gate 		pool_unlock();
44260Sstevel@tonic-gate 		err = EINVAL;
44270Sstevel@tonic-gate 		goto out;
44280Sstevel@tonic-gate 	}
44290Sstevel@tonic-gate 
44300Sstevel@tonic-gate 	/*
44310Sstevel@tonic-gate 	 * Moreover, we don't allow processes whose encapsulating
44320Sstevel@tonic-gate 	 * process contracts have inherited extrazonal contracts.
44330Sstevel@tonic-gate 	 * While it would be easier to eliminate all process contracts
44340Sstevel@tonic-gate 	 * with inherited contracts, we need to be able to give a
44350Sstevel@tonic-gate 	 * restarted init (or other zone-penetrating process) its
44360Sstevel@tonic-gate 	 * predecessor's contracts.
44370Sstevel@tonic-gate 	 */
44380Sstevel@tonic-gate 	if (ctp->conp_ninherited != 0) {
44390Sstevel@tonic-gate 		contract_t *next;
44400Sstevel@tonic-gate 		for (next = list_head(&ctp->conp_inherited); next;
44410Sstevel@tonic-gate 		    next = list_next(&ctp->conp_inherited, next)) {
44420Sstevel@tonic-gate 			if (contract_getzuniqid(next) != zone->zone_uniqid) {
44430Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
44440Sstevel@tonic-gate 				mutex_exit(&ct->ct_lock);
44450Sstevel@tonic-gate 				mutex_exit(&zonehash_lock);
44460Sstevel@tonic-gate 				pool_unlock();
44470Sstevel@tonic-gate 				err = EINVAL;
44480Sstevel@tonic-gate 				goto out;
44490Sstevel@tonic-gate 			}
44500Sstevel@tonic-gate 		}
44510Sstevel@tonic-gate 	}
44520Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
44530Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
44540Sstevel@tonic-gate 
44550Sstevel@tonic-gate 	status = zone_status_get(zone);
44560Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) {
44570Sstevel@tonic-gate 		/*
44580Sstevel@tonic-gate 		 * Can't join
44590Sstevel@tonic-gate 		 */
44600Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
44610Sstevel@tonic-gate 		err = EINVAL;
44620Sstevel@tonic-gate 		goto out;
44630Sstevel@tonic-gate 	}
44640Sstevel@tonic-gate 
44650Sstevel@tonic-gate 	/*
44660Sstevel@tonic-gate 	 * Make sure new priv set is within the permitted set for caller
44670Sstevel@tonic-gate 	 */
44680Sstevel@tonic-gate 	if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) {
44690Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
44700Sstevel@tonic-gate 		err = EPERM;
44710Sstevel@tonic-gate 		goto out;
44720Sstevel@tonic-gate 	}
44730Sstevel@tonic-gate 	/*
44740Sstevel@tonic-gate 	 * We want to momentarily drop zonehash_lock while we optimistically
44750Sstevel@tonic-gate 	 * bind curproc to the pool it should be running in.  This is safe
44760Sstevel@tonic-gate 	 * since the zone can't disappear (we have a hold on it).
44770Sstevel@tonic-gate 	 */
44780Sstevel@tonic-gate 	zone_hold(zone);
44790Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
44800Sstevel@tonic-gate 
44810Sstevel@tonic-gate 	/*
44820Sstevel@tonic-gate 	 * Grab pool_lock to keep the pools configuration from changing
44830Sstevel@tonic-gate 	 * and to stop ourselves from getting rebound to another pool
44840Sstevel@tonic-gate 	 * until we join the zone.
44850Sstevel@tonic-gate 	 */
44860Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
44870Sstevel@tonic-gate 		zone_rele(zone);
44880Sstevel@tonic-gate 		err = EINTR;
44890Sstevel@tonic-gate 		goto out;
44900Sstevel@tonic-gate 	}
44910Sstevel@tonic-gate 	ASSERT(secpolicy_pool(CRED()) == 0);
44920Sstevel@tonic-gate 	/*
44930Sstevel@tonic-gate 	 * Bind ourselves to the pool currently associated with the zone.
44940Sstevel@tonic-gate 	 */
44950Sstevel@tonic-gate 	oldpool = curproc->p_pool;
44960Sstevel@tonic-gate 	newpool = zone_pool_get(zone);
44970Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED && newpool != oldpool &&
44980Sstevel@tonic-gate 	    (err = pool_do_bind(newpool, P_PID, P_MYID,
44990Sstevel@tonic-gate 	    POOL_BIND_ALL)) != 0) {
45000Sstevel@tonic-gate 		pool_unlock();
45010Sstevel@tonic-gate 		zone_rele(zone);
45020Sstevel@tonic-gate 		goto out;
45030Sstevel@tonic-gate 	}
45040Sstevel@tonic-gate 
45050Sstevel@tonic-gate 	/*
45060Sstevel@tonic-gate 	 * Grab cpu_lock now; we'll need it later when we call
45070Sstevel@tonic-gate 	 * task_join().
45080Sstevel@tonic-gate 	 */
45090Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
45100Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
45110Sstevel@tonic-gate 	/*
45120Sstevel@tonic-gate 	 * Make sure the zone hasn't moved on since we dropped zonehash_lock.
45130Sstevel@tonic-gate 	 */
45140Sstevel@tonic-gate 	if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
45150Sstevel@tonic-gate 		/*
45160Sstevel@tonic-gate 		 * Can't join anymore.
45170Sstevel@tonic-gate 		 */
45180Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
45190Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
45200Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED &&
45210Sstevel@tonic-gate 		    newpool != oldpool)
45220Sstevel@tonic-gate 			(void) pool_do_bind(oldpool, P_PID, P_MYID,
45230Sstevel@tonic-gate 			    POOL_BIND_ALL);
45240Sstevel@tonic-gate 		pool_unlock();
45250Sstevel@tonic-gate 		zone_rele(zone);
45260Sstevel@tonic-gate 		err = EINVAL;
45270Sstevel@tonic-gate 		goto out;
45280Sstevel@tonic-gate 	}
45290Sstevel@tonic-gate 
45303247Sgjelinek 	/*
45313247Sgjelinek 	 * a_lock must be held while transfering locked memory and swap
45323247Sgjelinek 	 * reservation from the global zone to the non global zone because
45333247Sgjelinek 	 * asynchronous faults on the processes' address space can lock
45343247Sgjelinek 	 * memory and reserve swap via MCL_FUTURE and MAP_NORESERVE
45353247Sgjelinek 	 * segments respectively.
45363247Sgjelinek 	 */
45373247Sgjelinek 	AS_LOCK_ENTER(pp->as, &pp->p_as->a_lock, RW_WRITER);
45383247Sgjelinek 	swap = as_swresv();
45390Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
45400Sstevel@tonic-gate 	zone_proj0 = zone->zone_zsched->p_task->tk_proj;
45410Sstevel@tonic-gate 	/* verify that we do not exceed and task or lwp limits */
45420Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
45430Sstevel@tonic-gate 	/* add new lwps to zone and zone's proj0 */
45440Sstevel@tonic-gate 	zone_proj0->kpj_nlwps += pp->p_lwpcnt;
45450Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
45460Sstevel@tonic-gate 	/* add 1 task to zone's proj0 */
45470Sstevel@tonic-gate 	zone_proj0->kpj_ntasks += 1;
45480Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
45490Sstevel@tonic-gate 
45503247Sgjelinek 	mutex_enter(&zone->zone_mem_lock);
45512768Ssl108498 	zone->zone_locked_mem += pp->p_locked_mem;
45522768Ssl108498 	zone_proj0->kpj_data.kpd_locked_mem += pp->p_locked_mem;
45533247Sgjelinek 	zone->zone_max_swap += swap;
45543247Sgjelinek 	mutex_exit(&zone->zone_mem_lock);
45552768Ssl108498 
45560Sstevel@tonic-gate 	/* remove lwps from proc's old zone and old project */
45570Sstevel@tonic-gate 	mutex_enter(&pp->p_zone->zone_nlwps_lock);
45580Sstevel@tonic-gate 	pp->p_zone->zone_nlwps -= pp->p_lwpcnt;
45590Sstevel@tonic-gate 	pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt;
45600Sstevel@tonic-gate 	mutex_exit(&pp->p_zone->zone_nlwps_lock);
45610Sstevel@tonic-gate 
45623247Sgjelinek 	mutex_enter(&pp->p_zone->zone_mem_lock);
45632768Ssl108498 	pp->p_zone->zone_locked_mem -= pp->p_locked_mem;
45642768Ssl108498 	pp->p_task->tk_proj->kpj_data.kpd_locked_mem -= pp->p_locked_mem;
45653247Sgjelinek 	pp->p_zone->zone_max_swap -= swap;
45663247Sgjelinek 	mutex_exit(&pp->p_zone->zone_mem_lock);
45672768Ssl108498 
45682768Ssl108498 	mutex_exit(&pp->p_lock);
45693247Sgjelinek 	AS_LOCK_EXIT(pp->p_as, &pp->p_as->a_lock);
45702768Ssl108498 
45710Sstevel@tonic-gate 	/*
45720Sstevel@tonic-gate 	 * Joining the zone cannot fail from now on.
45730Sstevel@tonic-gate 	 *
45740Sstevel@tonic-gate 	 * This means that a lot of the following code can be commonized and
45750Sstevel@tonic-gate 	 * shared with zsched().
45760Sstevel@tonic-gate 	 */
45770Sstevel@tonic-gate 
45780Sstevel@tonic-gate 	/*
45790Sstevel@tonic-gate 	 * Reset the encapsulating process contract's zone.
45800Sstevel@tonic-gate 	 */
45810Sstevel@tonic-gate 	ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID);
45820Sstevel@tonic-gate 	contract_setzuniqid(ct, zone->zone_uniqid);
45830Sstevel@tonic-gate 
45840Sstevel@tonic-gate 	/*
45850Sstevel@tonic-gate 	 * Create a new task and associate the process with the project keyed
45860Sstevel@tonic-gate 	 * by (projid,zoneid).
45870Sstevel@tonic-gate 	 *
45880Sstevel@tonic-gate 	 * We might as well be in project 0; the global zone's projid doesn't
45890Sstevel@tonic-gate 	 * make much sense in a zone anyhow.
45900Sstevel@tonic-gate 	 *
45910Sstevel@tonic-gate 	 * This also increments zone_ntasks, and returns with p_lock held.
45920Sstevel@tonic-gate 	 */
45930Sstevel@tonic-gate 	tk = task_create(0, zone);
45940Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
45950Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
45960Sstevel@tonic-gate 
45970Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
45980Sstevel@tonic-gate 	pp->p_zone = zone;
45990Sstevel@tonic-gate 
46000Sstevel@tonic-gate 	/*
46010Sstevel@tonic-gate 	 * call RCTLOP_SET functions on this proc
46020Sstevel@tonic-gate 	 */
46030Sstevel@tonic-gate 	e.rcep_p.zone = zone;
46040Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
46050Sstevel@tonic-gate 	(void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL,
46060Sstevel@tonic-gate 	    RCD_CALLBACK);
46070Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
46080Sstevel@tonic-gate 
46090Sstevel@tonic-gate 	/*
46100Sstevel@tonic-gate 	 * We don't need to hold any of zsched's locks here; not only do we know
46110Sstevel@tonic-gate 	 * the process and zone aren't going away, we know its session isn't
46120Sstevel@tonic-gate 	 * changing either.
46130Sstevel@tonic-gate 	 *
46140Sstevel@tonic-gate 	 * By joining zsched's session here, we mimic the behavior in the
46150Sstevel@tonic-gate 	 * global zone of init's sid being the pid of sched.  We extend this
46160Sstevel@tonic-gate 	 * to all zlogin-like zone_enter()'ing processes as well.
46170Sstevel@tonic-gate 	 */
46180Sstevel@tonic-gate 	mutex_enter(&pidlock);
46190Sstevel@tonic-gate 	sp = zone->zone_zsched->p_sessp;
46202712Snn35248 	sess_hold(zone->zone_zsched);
46210Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
46220Sstevel@tonic-gate 	pgexit(pp);
46232712Snn35248 	sess_rele(pp->p_sessp, B_TRUE);
46240Sstevel@tonic-gate 	pp->p_sessp = sp;
46250Sstevel@tonic-gate 	pgjoin(pp, zone->zone_zsched->p_pidp);
46263247Sgjelinek 
46273247Sgjelinek 	/*
46283247Sgjelinek 	 * If there is a default scheduling class for the zone and it is not
46293247Sgjelinek 	 * the class we are currently in, change all of the threads in the
46303247Sgjelinek 	 * process to the new class.  We need to be holding pidlock & p_lock
46313247Sgjelinek 	 * when we call parmsset so this is a good place to do it.
46323247Sgjelinek 	 */
46333247Sgjelinek 	if (zone->zone_defaultcid > 0 &&
46343247Sgjelinek 	    zone->zone_defaultcid != curthread->t_cid) {
46353247Sgjelinek 		pcparms_t pcparms;
46363247Sgjelinek 		kthread_id_t t;
46373247Sgjelinek 
46383247Sgjelinek 		pcparms.pc_cid = zone->zone_defaultcid;
46393247Sgjelinek 		pcparms.pc_clparms[0] = 0;
46403247Sgjelinek 
46413247Sgjelinek 		/*
46423247Sgjelinek 		 * If setting the class fails, we still want to enter the zone.
46433247Sgjelinek 		 */
46443247Sgjelinek 		if ((t = pp->p_tlist) != NULL) {
46453247Sgjelinek 			do {
46463247Sgjelinek 				(void) parmsset(&pcparms, t);
46473247Sgjelinek 			} while ((t = t->t_forw) != pp->p_tlist);
46483247Sgjelinek 		}
46493247Sgjelinek 	}
46503247Sgjelinek 
46510Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
46520Sstevel@tonic-gate 	mutex_exit(&pidlock);
46530Sstevel@tonic-gate 
46540Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
46550Sstevel@tonic-gate 	/*
46560Sstevel@tonic-gate 	 * We're firmly in the zone; let pools progress.
46570Sstevel@tonic-gate 	 */
46580Sstevel@tonic-gate 	pool_unlock();
46590Sstevel@tonic-gate 	task_rele(oldtk);
46600Sstevel@tonic-gate 	/*
46610Sstevel@tonic-gate 	 * We don't need to retain a hold on the zone since we already
46620Sstevel@tonic-gate 	 * incremented zone_ntasks, so the zone isn't going anywhere.
46630Sstevel@tonic-gate 	 */
46640Sstevel@tonic-gate 	zone_rele(zone);
46650Sstevel@tonic-gate 
46660Sstevel@tonic-gate 	/*
46670Sstevel@tonic-gate 	 * Chroot
46680Sstevel@tonic-gate 	 */
46690Sstevel@tonic-gate 	vp = zone->zone_rootvp;
46700Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_cdir, pp);
46710Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_rdir, pp);
46720Sstevel@tonic-gate 
46730Sstevel@tonic-gate 	/*
46740Sstevel@tonic-gate 	 * Change process credentials
46750Sstevel@tonic-gate 	 */
46760Sstevel@tonic-gate 	newcr = cralloc();
46770Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
46780Sstevel@tonic-gate 	cr = pp->p_cred;
46790Sstevel@tonic-gate 	crcopy_to(cr, newcr);
46800Sstevel@tonic-gate 	crsetzone(newcr, zone);
46810Sstevel@tonic-gate 	pp->p_cred = newcr;
46820Sstevel@tonic-gate 
46830Sstevel@tonic-gate 	/*
46840Sstevel@tonic-gate 	 * Restrict all process privilege sets to zone limit
46850Sstevel@tonic-gate 	 */
46860Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(newcr));
46870Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(newcr));
46880Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(newcr));
46890Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(newcr));
46900Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
46910Sstevel@tonic-gate 	crset(pp, newcr);
46920Sstevel@tonic-gate 
46930Sstevel@tonic-gate 	/*
46940Sstevel@tonic-gate 	 * Adjust upcount to reflect zone entry.
46950Sstevel@tonic-gate 	 */
46960Sstevel@tonic-gate 	uid = crgetruid(newcr);
46970Sstevel@tonic-gate 	mutex_enter(&pidlock);
46980Sstevel@tonic-gate 	upcount_dec(uid, GLOBAL_ZONEID);
46990Sstevel@tonic-gate 	upcount_inc(uid, zoneid);
47000Sstevel@tonic-gate 	mutex_exit(&pidlock);
47010Sstevel@tonic-gate 
47020Sstevel@tonic-gate 	/*
47030Sstevel@tonic-gate 	 * Set up core file path and content.
47040Sstevel@tonic-gate 	 */
47050Sstevel@tonic-gate 	set_core_defaults();
47060Sstevel@tonic-gate 
47070Sstevel@tonic-gate out:
47080Sstevel@tonic-gate 	/*
47090Sstevel@tonic-gate 	 * Let the other lwps continue.
47100Sstevel@tonic-gate 	 */
47110Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
47120Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
47130Sstevel@tonic-gate 		continuelwps(pp);
47140Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
47150Sstevel@tonic-gate 
47160Sstevel@tonic-gate 	return (err != 0 ? set_errno(err) : 0);
47170Sstevel@tonic-gate }
47180Sstevel@tonic-gate 
47190Sstevel@tonic-gate /*
47200Sstevel@tonic-gate  * Systemcall entry point for zone_list(2).
47210Sstevel@tonic-gate  *
47220Sstevel@tonic-gate  * Processes running in a (non-global) zone only see themselves.
47231676Sjpk  * On labeled systems, they see all zones whose label they dominate.
47240Sstevel@tonic-gate  */
47250Sstevel@tonic-gate static int
47260Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones)
47270Sstevel@tonic-gate {
47280Sstevel@tonic-gate 	zoneid_t *zoneids;
47291769Scarlsonj 	zone_t *zone, *myzone;
47300Sstevel@tonic-gate 	uint_t user_nzones, real_nzones;
47311676Sjpk 	uint_t domi_nzones;
47321676Sjpk 	int error;
47330Sstevel@tonic-gate 
47340Sstevel@tonic-gate 	if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0)
47350Sstevel@tonic-gate 		return (set_errno(EFAULT));
47360Sstevel@tonic-gate 
47371769Scarlsonj 	myzone = curproc->p_zone;
47381769Scarlsonj 	if (myzone != global_zone) {
47391676Sjpk 		bslabel_t *mybslab;
47401676Sjpk 
47411676Sjpk 		if (!is_system_labeled()) {
47421676Sjpk 			/* just return current zone */
47431676Sjpk 			real_nzones = domi_nzones = 1;
47441676Sjpk 			zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP);
47451769Scarlsonj 			zoneids[0] = myzone->zone_id;
47461676Sjpk 		} else {
47471676Sjpk 			/* return all zones that are dominated */
47481676Sjpk 			mutex_enter(&zonehash_lock);
47491676Sjpk 			real_nzones = zonecount;
47501676Sjpk 			domi_nzones = 0;
47511676Sjpk 			if (real_nzones > 0) {
47521676Sjpk 				zoneids = kmem_alloc(real_nzones *
47531676Sjpk 				    sizeof (zoneid_t), KM_SLEEP);
47541769Scarlsonj 				mybslab = label2bslabel(myzone->zone_slabel);
47551676Sjpk 				for (zone = list_head(&zone_active);
47561676Sjpk 				    zone != NULL;
47571676Sjpk 				    zone = list_next(&zone_active, zone)) {
47581676Sjpk 					if (zone->zone_id == GLOBAL_ZONEID)
47591676Sjpk 						continue;
47601769Scarlsonj 					if (zone != myzone &&
47611769Scarlsonj 					    (zone->zone_flags & ZF_IS_SCRATCH))
47621769Scarlsonj 						continue;
47631769Scarlsonj 					/*
47641769Scarlsonj 					 * Note that a label always dominates
47651769Scarlsonj 					 * itself, so myzone is always included
47661769Scarlsonj 					 * in the list.
47671769Scarlsonj 					 */
47681676Sjpk 					if (bldominates(mybslab,
47691676Sjpk 					    label2bslabel(zone->zone_slabel))) {
47701676Sjpk 						zoneids[domi_nzones++] =
47711676Sjpk 						    zone->zone_id;
47721676Sjpk 					}
47731676Sjpk 				}
47741676Sjpk 			}
47751676Sjpk 			mutex_exit(&zonehash_lock);
47761676Sjpk 		}
47770Sstevel@tonic-gate 	} else {
47780Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
47790Sstevel@tonic-gate 		real_nzones = zonecount;
47801676Sjpk 		domi_nzones = 0;
47811676Sjpk 		if (real_nzones > 0) {
47820Sstevel@tonic-gate 			zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t),
47830Sstevel@tonic-gate 			    KM_SLEEP);
47840Sstevel@tonic-gate 			for (zone = list_head(&zone_active); zone != NULL;
47850Sstevel@tonic-gate 			    zone = list_next(&zone_active, zone))
47861676Sjpk 				zoneids[domi_nzones++] = zone->zone_id;
47871676Sjpk 			ASSERT(domi_nzones == real_nzones);
47880Sstevel@tonic-gate 		}
47890Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
47900Sstevel@tonic-gate 	}
47910Sstevel@tonic-gate 
47921676Sjpk 	/*
47931676Sjpk 	 * If user has allocated space for fewer entries than we found, then
47941676Sjpk 	 * return only up to his limit.  Either way, tell him exactly how many
47951676Sjpk 	 * we found.
47961676Sjpk 	 */
47971676Sjpk 	if (domi_nzones < user_nzones)
47981676Sjpk 		user_nzones = domi_nzones;
47991676Sjpk 	error = 0;
48001676Sjpk 	if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) {
48010Sstevel@tonic-gate 		error = EFAULT;
48021676Sjpk 	} else if (zoneidlist != NULL && user_nzones != 0) {
48030Sstevel@tonic-gate 		if (copyout(zoneids, zoneidlist,
48040Sstevel@tonic-gate 		    user_nzones * sizeof (zoneid_t)) != 0)
48050Sstevel@tonic-gate 			error = EFAULT;
48060Sstevel@tonic-gate 	}
48070Sstevel@tonic-gate 
48081676Sjpk 	if (real_nzones > 0)
48090Sstevel@tonic-gate 		kmem_free(zoneids, real_nzones * sizeof (zoneid_t));
48100Sstevel@tonic-gate 
48111676Sjpk 	if (error != 0)
48120Sstevel@tonic-gate 		return (set_errno(error));
48130Sstevel@tonic-gate 	else
48140Sstevel@tonic-gate 		return (0);
48150Sstevel@tonic-gate }
48160Sstevel@tonic-gate 
48170Sstevel@tonic-gate /*
48180Sstevel@tonic-gate  * Systemcall entry point for zone_lookup(2).
48190Sstevel@tonic-gate  *
48201676Sjpk  * Non-global zones are only able to see themselves and (on labeled systems)
48211676Sjpk  * the zones they dominate.
48220Sstevel@tonic-gate  */
48230Sstevel@tonic-gate static zoneid_t
48240Sstevel@tonic-gate zone_lookup(const char *zone_name)
48250Sstevel@tonic-gate {
48260Sstevel@tonic-gate 	char *kname;
48270Sstevel@tonic-gate 	zone_t *zone;
48280Sstevel@tonic-gate 	zoneid_t zoneid;
48290Sstevel@tonic-gate 	int err;
48300Sstevel@tonic-gate 
48310Sstevel@tonic-gate 	if (zone_name == NULL) {
48320Sstevel@tonic-gate 		/* return caller's zone id */
48330Sstevel@tonic-gate 		return (getzoneid());
48340Sstevel@tonic-gate 	}
48350Sstevel@tonic-gate 
48360Sstevel@tonic-gate 	kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
48370Sstevel@tonic-gate 	if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) {
48380Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
48390Sstevel@tonic-gate 		return (set_errno(err));
48400Sstevel@tonic-gate 	}
48410Sstevel@tonic-gate 
48420Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
48430Sstevel@tonic-gate 	zone = zone_find_all_by_name(kname);
48440Sstevel@tonic-gate 	kmem_free(kname, ZONENAME_MAX);
48451676Sjpk 	/*
48461676Sjpk 	 * In a non-global zone, can only lookup global and own name.
48471676Sjpk 	 * In Trusted Extensions zone label dominance rules apply.
48481676Sjpk 	 */
48491676Sjpk 	if (zone == NULL ||
48501676Sjpk 	    zone_status_get(zone) < ZONE_IS_READY ||
48511676Sjpk 	    !zone_list_access(zone)) {
48520Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
48530Sstevel@tonic-gate 		return (set_errno(EINVAL));
48541676Sjpk 	} else {
48551676Sjpk 		zoneid = zone->zone_id;
48561676Sjpk 		mutex_exit(&zonehash_lock);
48571676Sjpk 		return (zoneid);
48580Sstevel@tonic-gate 	}
48590Sstevel@tonic-gate }
48600Sstevel@tonic-gate 
4861813Sdp static int
4862813Sdp zone_version(int *version_arg)
4863813Sdp {
4864813Sdp 	int version = ZONE_SYSCALL_API_VERSION;
4865813Sdp 
4866813Sdp 	if (copyout(&version, version_arg, sizeof (int)) != 0)
4867813Sdp 		return (set_errno(EFAULT));
4868813Sdp 	return (0);
4869813Sdp }
4870813Sdp 
48710Sstevel@tonic-gate /* ARGSUSED */
48720Sstevel@tonic-gate long
4873789Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4)
48740Sstevel@tonic-gate {
48750Sstevel@tonic-gate 	zone_def zs;
48760Sstevel@tonic-gate 
48770Sstevel@tonic-gate 	switch (cmd) {
48780Sstevel@tonic-gate 	case ZONE_CREATE:
48790Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
48800Sstevel@tonic-gate 			if (copyin(arg1, &zs, sizeof (zone_def))) {
48810Sstevel@tonic-gate 				return (set_errno(EFAULT));
48820Sstevel@tonic-gate 			}
48830Sstevel@tonic-gate 		} else {
48840Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
48850Sstevel@tonic-gate 			zone_def32 zs32;
48860Sstevel@tonic-gate 
48870Sstevel@tonic-gate 			if (copyin(arg1, &zs32, sizeof (zone_def32))) {
48880Sstevel@tonic-gate 				return (set_errno(EFAULT));
48890Sstevel@tonic-gate 			}
48900Sstevel@tonic-gate 			zs.zone_name =
48910Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_name;
48920Sstevel@tonic-gate 			zs.zone_root =
48930Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_root;
48940Sstevel@tonic-gate 			zs.zone_privs =
48950Sstevel@tonic-gate 			    (const struct priv_set *)
48960Sstevel@tonic-gate 			    (unsigned long)zs32.zone_privs;
48971409Sdp 			zs.zone_privssz = zs32.zone_privssz;
48980Sstevel@tonic-gate 			zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf;
48990Sstevel@tonic-gate 			zs.rctlbufsz = zs32.rctlbufsz;
4900789Sahrens 			zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf;
4901789Sahrens 			zs.zfsbufsz = zs32.zfsbufsz;
49020Sstevel@tonic-gate 			zs.extended_error =
49030Sstevel@tonic-gate 			    (int *)(unsigned long)zs32.extended_error;
49041676Sjpk 			zs.match = zs32.match;
49051676Sjpk 			zs.doi = zs32.doi;
49061676Sjpk 			zs.label = (const bslabel_t *)(uintptr_t)zs32.label;
49073448Sdh155122 			zs.flags = zs32.flags;
49080Sstevel@tonic-gate #else
49090Sstevel@tonic-gate 			panic("get_udatamodel() returned bogus result\n");
49100Sstevel@tonic-gate #endif
49110Sstevel@tonic-gate 		}
49120Sstevel@tonic-gate 
49130Sstevel@tonic-gate 		return (zone_create(zs.zone_name, zs.zone_root,
4914813Sdp 		    zs.zone_privs, zs.zone_privssz,
4915813Sdp 		    (caddr_t)zs.rctlbuf, zs.rctlbufsz,
4916813Sdp 		    (caddr_t)zs.zfsbuf, zs.zfsbufsz,
49171676Sjpk 		    zs.extended_error, zs.match, zs.doi,
49183448Sdh155122 		    zs.label, zs.flags));
49190Sstevel@tonic-gate 	case ZONE_BOOT:
49202267Sdp 		return (zone_boot((zoneid_t)(uintptr_t)arg1));
49210Sstevel@tonic-gate 	case ZONE_DESTROY:
49220Sstevel@tonic-gate 		return (zone_destroy((zoneid_t)(uintptr_t)arg1));
49230Sstevel@tonic-gate 	case ZONE_GETATTR:
49240Sstevel@tonic-gate 		return (zone_getattr((zoneid_t)(uintptr_t)arg1,
49250Sstevel@tonic-gate 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
49262267Sdp 	case ZONE_SETATTR:
49272267Sdp 		return (zone_setattr((zoneid_t)(uintptr_t)arg1,
49282267Sdp 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
49290Sstevel@tonic-gate 	case ZONE_ENTER:
49300Sstevel@tonic-gate 		return (zone_enter((zoneid_t)(uintptr_t)arg1));
49310Sstevel@tonic-gate 	case ZONE_LIST:
49320Sstevel@tonic-gate 		return (zone_list((zoneid_t *)arg1, (uint_t *)arg2));
49330Sstevel@tonic-gate 	case ZONE_SHUTDOWN:
49340Sstevel@tonic-gate 		return (zone_shutdown((zoneid_t)(uintptr_t)arg1));
49350Sstevel@tonic-gate 	case ZONE_LOOKUP:
49360Sstevel@tonic-gate 		return (zone_lookup((const char *)arg1));
4937813Sdp 	case ZONE_VERSION:
4938813Sdp 		return (zone_version((int *)arg1));
49393448Sdh155122 	case ZONE_ADD_DATALINK:
49403448Sdh155122 		return (zone_add_datalink((zoneid_t)(uintptr_t)arg1,
49413448Sdh155122 		    (char *)arg2));
49423448Sdh155122 	case ZONE_DEL_DATALINK:
49433448Sdh155122 		return (zone_remove_datalink((zoneid_t)(uintptr_t)arg1,
49443448Sdh155122 		    (char *)arg2));
49453448Sdh155122 	case ZONE_CHECK_DATALINK:
49463448Sdh155122 		return (zone_check_datalink((zoneid_t *)arg1, (char *)arg2));
49473448Sdh155122 	case ZONE_LIST_DATALINK:
49483448Sdh155122 		return (zone_list_datalink((zoneid_t)(uintptr_t)arg1,
49493448Sdh155122 		    (int *)arg2, (char *)arg3));
49500Sstevel@tonic-gate 	default:
49510Sstevel@tonic-gate 		return (set_errno(EINVAL));
49520Sstevel@tonic-gate 	}
49530Sstevel@tonic-gate }
49540Sstevel@tonic-gate 
49550Sstevel@tonic-gate struct zarg {
49560Sstevel@tonic-gate 	zone_t *zone;
49570Sstevel@tonic-gate 	zone_cmd_arg_t arg;
49580Sstevel@tonic-gate };
49590Sstevel@tonic-gate 
49600Sstevel@tonic-gate static int
49610Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp)
49620Sstevel@tonic-gate {
49630Sstevel@tonic-gate 	char *buf;
49640Sstevel@tonic-gate 	size_t buflen;
49650Sstevel@tonic-gate 	int error;
49660Sstevel@tonic-gate 
49670Sstevel@tonic-gate 	buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name);
49680Sstevel@tonic-gate 	buf = kmem_alloc(buflen, KM_SLEEP);
49690Sstevel@tonic-gate 	(void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name);
49700Sstevel@tonic-gate 	error = door_ki_open(buf, doorp);
49710Sstevel@tonic-gate 	kmem_free(buf, buflen);
49720Sstevel@tonic-gate 	return (error);
49730Sstevel@tonic-gate }
49740Sstevel@tonic-gate 
49750Sstevel@tonic-gate static void
49760Sstevel@tonic-gate zone_release_door(door_handle_t *doorp)
49770Sstevel@tonic-gate {
49780Sstevel@tonic-gate 	door_ki_rele(*doorp);
49790Sstevel@tonic-gate 	*doorp = NULL;
49800Sstevel@tonic-gate }
49810Sstevel@tonic-gate 
49820Sstevel@tonic-gate static void
49830Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp)
49840Sstevel@tonic-gate {
49850Sstevel@tonic-gate 	door_handle_t door = NULL;
49860Sstevel@tonic-gate 	door_arg_t darg, save_arg;
49870Sstevel@tonic-gate 	char *zone_name;
49880Sstevel@tonic-gate 	size_t zone_namelen;
49890Sstevel@tonic-gate 	zoneid_t zoneid;
49900Sstevel@tonic-gate 	zone_t *zone;
49910Sstevel@tonic-gate 	zone_cmd_arg_t arg;
49920Sstevel@tonic-gate 	uint64_t uniqid;
49930Sstevel@tonic-gate 	size_t size;
49940Sstevel@tonic-gate 	int error;
49950Sstevel@tonic-gate 	int retry;
49960Sstevel@tonic-gate 
49970Sstevel@tonic-gate 	zone = zargp->zone;
49980Sstevel@tonic-gate 	arg = zargp->arg;
49990Sstevel@tonic-gate 	kmem_free(zargp, sizeof (*zargp));
50000Sstevel@tonic-gate 
50010Sstevel@tonic-gate 	zone_namelen = strlen(zone->zone_name) + 1;
50020Sstevel@tonic-gate 	zone_name = kmem_alloc(zone_namelen, KM_SLEEP);
50030Sstevel@tonic-gate 	bcopy(zone->zone_name, zone_name, zone_namelen);
50040Sstevel@tonic-gate 	zoneid = zone->zone_id;
50050Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
50060Sstevel@tonic-gate 	/*
50070Sstevel@tonic-gate 	 * zoneadmd may be down, but at least we can empty out the zone.
50080Sstevel@tonic-gate 	 * We can ignore the return value of zone_empty() since we're called
50090Sstevel@tonic-gate 	 * from a kernel thread and know we won't be delivered any signals.
50100Sstevel@tonic-gate 	 */
50110Sstevel@tonic-gate 	ASSERT(curproc == &p0);
50120Sstevel@tonic-gate 	(void) zone_empty(zone);
50130Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY);
50140Sstevel@tonic-gate 	zone_rele(zone);
50150Sstevel@tonic-gate 
50160Sstevel@tonic-gate 	size = sizeof (arg);
50170Sstevel@tonic-gate 	darg.rbuf = (char *)&arg;
50180Sstevel@tonic-gate 	darg.data_ptr = (char *)&arg;
50190Sstevel@tonic-gate 	darg.rsize = size;
50200Sstevel@tonic-gate 	darg.data_size = size;
50210Sstevel@tonic-gate 	darg.desc_ptr = NULL;
50220Sstevel@tonic-gate 	darg.desc_num = 0;
50230Sstevel@tonic-gate 
50240Sstevel@tonic-gate 	save_arg = darg;
50250Sstevel@tonic-gate 	/*
50260Sstevel@tonic-gate 	 * Since we're not holding a reference to the zone, any number of
50270Sstevel@tonic-gate 	 * things can go wrong, including the zone disappearing before we get a
50280Sstevel@tonic-gate 	 * chance to talk to zoneadmd.
50290Sstevel@tonic-gate 	 */
50300Sstevel@tonic-gate 	for (retry = 0; /* forever */; retry++) {
50310Sstevel@tonic-gate 		if (door == NULL &&
50320Sstevel@tonic-gate 		    (error = zone_lookup_door(zone_name, &door)) != 0) {
50330Sstevel@tonic-gate 			goto next;
50340Sstevel@tonic-gate 		}
50350Sstevel@tonic-gate 		ASSERT(door != NULL);
50360Sstevel@tonic-gate 
50370Sstevel@tonic-gate 		if ((error = door_ki_upcall(door, &darg)) == 0) {
50380Sstevel@tonic-gate 			break;
50390Sstevel@tonic-gate 		}
50400Sstevel@tonic-gate 		switch (error) {
50410Sstevel@tonic-gate 		case EINTR:
50420Sstevel@tonic-gate 			/* FALLTHROUGH */
50430Sstevel@tonic-gate 		case EAGAIN:	/* process may be forking */
50440Sstevel@tonic-gate 			/*
50450Sstevel@tonic-gate 			 * Back off for a bit
50460Sstevel@tonic-gate 			 */
50470Sstevel@tonic-gate 			break;
50480Sstevel@tonic-gate 		case EBADF:
50490Sstevel@tonic-gate 			zone_release_door(&door);
50500Sstevel@tonic-gate 			if (zone_lookup_door(zone_name, &door) != 0) {
50510Sstevel@tonic-gate 				/*
50520Sstevel@tonic-gate 				 * zoneadmd may be dead, but it may come back to
50530Sstevel@tonic-gate 				 * life later.
50540Sstevel@tonic-gate 				 */
50550Sstevel@tonic-gate 				break;
50560Sstevel@tonic-gate 			}
50570Sstevel@tonic-gate 			break;
50580Sstevel@tonic-gate 		default:
50590Sstevel@tonic-gate 			cmn_err(CE_WARN,
50600Sstevel@tonic-gate 			    "zone_ki_call_zoneadmd: door_ki_upcall error %d\n",
50610Sstevel@tonic-gate 			    error);
50620Sstevel@tonic-gate 			goto out;
50630Sstevel@tonic-gate 		}
50640Sstevel@tonic-gate next:
50650Sstevel@tonic-gate 		/*
50660Sstevel@tonic-gate 		 * If this isn't the same zone_t that we originally had in mind,
50670Sstevel@tonic-gate 		 * then this is the same as if two kadmin requests come in at
50680Sstevel@tonic-gate 		 * the same time: the first one wins.  This means we lose, so we
50690Sstevel@tonic-gate 		 * bail.
50700Sstevel@tonic-gate 		 */
50710Sstevel@tonic-gate 		if ((zone = zone_find_by_id(zoneid)) == NULL) {
50720Sstevel@tonic-gate 			/*
50730Sstevel@tonic-gate 			 * Problem is solved.
50740Sstevel@tonic-gate 			 */
50750Sstevel@tonic-gate 			break;
50760Sstevel@tonic-gate 		}
50770Sstevel@tonic-gate 		if (zone->zone_uniqid != uniqid) {
50780Sstevel@tonic-gate 			/*
50790Sstevel@tonic-gate 			 * zoneid recycled
50800Sstevel@tonic-gate 			 */
50810Sstevel@tonic-gate 			zone_rele(zone);
50820Sstevel@tonic-gate 			break;
50830Sstevel@tonic-gate 		}
50840Sstevel@tonic-gate 		/*
50850Sstevel@tonic-gate 		 * We could zone_status_timedwait(), but there doesn't seem to
50860Sstevel@tonic-gate 		 * be much point in doing that (plus, it would mean that
50870Sstevel@tonic-gate 		 * zone_free() isn't called until this thread exits).
50880Sstevel@tonic-gate 		 */
50890Sstevel@tonic-gate 		zone_rele(zone);
50900Sstevel@tonic-gate 		delay(hz);
50910Sstevel@tonic-gate 		darg = save_arg;
50920Sstevel@tonic-gate 	}
50930Sstevel@tonic-gate out:
50940Sstevel@tonic-gate 	if (door != NULL) {
50950Sstevel@tonic-gate 		zone_release_door(&door);
50960Sstevel@tonic-gate 	}
50970Sstevel@tonic-gate 	kmem_free(zone_name, zone_namelen);
50980Sstevel@tonic-gate 	thread_exit();
50990Sstevel@tonic-gate }
51000Sstevel@tonic-gate 
51010Sstevel@tonic-gate /*
51022267Sdp  * Entry point for uadmin() to tell the zone to go away or reboot.  Analog to
51032267Sdp  * kadmin().  The caller is a process in the zone.
51040Sstevel@tonic-gate  *
51050Sstevel@tonic-gate  * In order to shutdown the zone, we will hand off control to zoneadmd
51060Sstevel@tonic-gate  * (running in the global zone) via a door.  We do a half-hearted job at
51070Sstevel@tonic-gate  * killing all processes in the zone, create a kernel thread to contact
51080Sstevel@tonic-gate  * zoneadmd, and make note of the "uniqid" of the zone.  The uniqid is
51090Sstevel@tonic-gate  * a form of generation number used to let zoneadmd (as well as
51100Sstevel@tonic-gate  * zone_destroy()) know exactly which zone they're re talking about.
51110Sstevel@tonic-gate  */
51120Sstevel@tonic-gate int
51132267Sdp zone_kadmin(int cmd, int fcn, const char *mdep, cred_t *credp)
51140Sstevel@tonic-gate {
51150Sstevel@tonic-gate 	struct zarg *zargp;
51160Sstevel@tonic-gate 	zone_cmd_t zcmd;
51170Sstevel@tonic-gate 	zone_t *zone;
51180Sstevel@tonic-gate 
51190Sstevel@tonic-gate 	zone = curproc->p_zone;
51200Sstevel@tonic-gate 	ASSERT(getzoneid() != GLOBAL_ZONEID);
51210Sstevel@tonic-gate 
51220Sstevel@tonic-gate 	switch (cmd) {
51230Sstevel@tonic-gate 	case A_SHUTDOWN:
51240Sstevel@tonic-gate 		switch (fcn) {
51250Sstevel@tonic-gate 		case AD_HALT:
51260Sstevel@tonic-gate 		case AD_POWEROFF:
51270Sstevel@tonic-gate 			zcmd = Z_HALT;
51280Sstevel@tonic-gate 			break;
51290Sstevel@tonic-gate 		case AD_BOOT:
51300Sstevel@tonic-gate 			zcmd = Z_REBOOT;
51310Sstevel@tonic-gate 			break;
51320Sstevel@tonic-gate 		case AD_IBOOT:
51330Sstevel@tonic-gate 		case AD_SBOOT:
51340Sstevel@tonic-gate 		case AD_SIBOOT:
51350Sstevel@tonic-gate 		case AD_NOSYNC:
51360Sstevel@tonic-gate 			return (ENOTSUP);
51370Sstevel@tonic-gate 		default:
51380Sstevel@tonic-gate 			return (EINVAL);
51390Sstevel@tonic-gate 		}
51400Sstevel@tonic-gate 		break;
51410Sstevel@tonic-gate 	case A_REBOOT:
51420Sstevel@tonic-gate 		zcmd = Z_REBOOT;
51430Sstevel@tonic-gate 		break;
51440Sstevel@tonic-gate 	case A_FTRACE:
51450Sstevel@tonic-gate 	case A_REMOUNT:
51460Sstevel@tonic-gate 	case A_FREEZE:
51470Sstevel@tonic-gate 	case A_DUMP:
51480Sstevel@tonic-gate 		return (ENOTSUP);
51490Sstevel@tonic-gate 	default:
51500Sstevel@tonic-gate 		ASSERT(cmd != A_SWAPCTL);	/* handled by uadmin() */
51510Sstevel@tonic-gate 		return (EINVAL);
51520Sstevel@tonic-gate 	}
51530Sstevel@tonic-gate 
51540Sstevel@tonic-gate 	if (secpolicy_zone_admin(credp, B_FALSE))
51550Sstevel@tonic-gate 		return (EPERM);
51560Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
51572267Sdp 
51580Sstevel@tonic-gate 	/*
51590Sstevel@tonic-gate 	 * zone_status can't be ZONE_IS_EMPTY or higher since curproc
51600Sstevel@tonic-gate 	 * is in the zone.
51610Sstevel@tonic-gate 	 */
51620Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY);
51630Sstevel@tonic-gate 	if (zone_status_get(zone) > ZONE_IS_RUNNING) {
51640Sstevel@tonic-gate 		/*
51650Sstevel@tonic-gate 		 * This zone is already on its way down.
51660Sstevel@tonic-gate 		 */
51670Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
51680Sstevel@tonic-gate 		return (0);
51690Sstevel@tonic-gate 	}
51700Sstevel@tonic-gate 	/*
51710Sstevel@tonic-gate 	 * Prevent future zone_enter()s
51720Sstevel@tonic-gate 	 */
51730Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
51740Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
51750Sstevel@tonic-gate 
51760Sstevel@tonic-gate 	/*
51770Sstevel@tonic-gate 	 * Kill everyone now and call zoneadmd later.
51780Sstevel@tonic-gate 	 * zone_ki_call_zoneadmd() will do a more thorough job of this
51790Sstevel@tonic-gate 	 * later.
51800Sstevel@tonic-gate 	 */
51810Sstevel@tonic-gate 	killall(zone->zone_id);
51820Sstevel@tonic-gate 	/*
51830Sstevel@tonic-gate 	 * Now, create the thread to contact zoneadmd and do the rest of the
51840Sstevel@tonic-gate 	 * work.  This thread can't be created in our zone otherwise
51850Sstevel@tonic-gate 	 * zone_destroy() would deadlock.
51860Sstevel@tonic-gate 	 */
51872267Sdp 	zargp = kmem_zalloc(sizeof (*zargp), KM_SLEEP);
51880Sstevel@tonic-gate 	zargp->arg.cmd = zcmd;
51890Sstevel@tonic-gate 	zargp->arg.uniqid = zone->zone_uniqid;
51902267Sdp 	zargp->zone = zone;
51910Sstevel@tonic-gate 	(void) strcpy(zargp->arg.locale, "C");
51922267Sdp 	/* mdep was already copied in for us by uadmin */
51932267Sdp 	if (mdep != NULL)
51942267Sdp 		(void) strlcpy(zargp->arg.bootbuf, mdep,
51952267Sdp 		    sizeof (zargp->arg.bootbuf));
51962267Sdp 	zone_hold(zone);
51970Sstevel@tonic-gate 
51980Sstevel@tonic-gate 	(void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0,
51990Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
52000Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
52010Sstevel@tonic-gate 
52020Sstevel@tonic-gate 	return (EINVAL);
52030Sstevel@tonic-gate }
52040Sstevel@tonic-gate 
52050Sstevel@tonic-gate /*
52060Sstevel@tonic-gate  * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's
52070Sstevel@tonic-gate  * status to ZONE_IS_SHUTTING_DOWN.
52080Sstevel@tonic-gate  */
52090Sstevel@tonic-gate void
52100Sstevel@tonic-gate zone_shutdown_global(void)
52110Sstevel@tonic-gate {
52120Sstevel@tonic-gate 	ASSERT(curproc->p_zone == global_zone);
52130Sstevel@tonic-gate 
52140Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
52150Sstevel@tonic-gate 	ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING);
52160Sstevel@tonic-gate 	zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN);
52170Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
52180Sstevel@tonic-gate }
5219789Sahrens 
5220789Sahrens /*
5221789Sahrens  * Returns true if the named dataset is visible in the current zone.
5222789Sahrens  * The 'write' parameter is set to 1 if the dataset is also writable.
5223789Sahrens  */
5224789Sahrens int
5225789Sahrens zone_dataset_visible(const char *dataset, int *write)
5226789Sahrens {
5227789Sahrens 	zone_dataset_t *zd;
5228789Sahrens 	size_t len;
5229789Sahrens 	zone_t *zone = curproc->p_zone;
5230789Sahrens 
5231789Sahrens 	if (dataset[0] == '\0')
5232789Sahrens 		return (0);
5233789Sahrens 
5234789Sahrens 	/*
5235789Sahrens 	 * Walk the list once, looking for datasets which match exactly, or
5236789Sahrens 	 * specify a dataset underneath an exported dataset.  If found, return
5237789Sahrens 	 * true and note that it is writable.
5238789Sahrens 	 */
5239789Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
5240789Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
5241789Sahrens 
5242789Sahrens 		len = strlen(zd->zd_dataset);
5243789Sahrens 		if (strlen(dataset) >= len &&
5244789Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
5245816Smaybee 		    (dataset[len] == '\0' || dataset[len] == '/' ||
5246816Smaybee 		    dataset[len] == '@')) {
5247789Sahrens 			if (write)
5248789Sahrens 				*write = 1;
5249789Sahrens 			return (1);
5250789Sahrens 		}
5251789Sahrens 	}
5252789Sahrens 
5253789Sahrens 	/*
5254789Sahrens 	 * Walk the list a second time, searching for datasets which are parents
5255789Sahrens 	 * of exported datasets.  These should be visible, but read-only.
5256789Sahrens 	 *
5257789Sahrens 	 * Note that we also have to support forms such as 'pool/dataset/', with
5258789Sahrens 	 * a trailing slash.
5259789Sahrens 	 */
5260789Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
5261789Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
5262789Sahrens 
5263789Sahrens 		len = strlen(dataset);
5264789Sahrens 		if (dataset[len - 1] == '/')
5265789Sahrens 			len--;	/* Ignore trailing slash */
5266789Sahrens 		if (len < strlen(zd->zd_dataset) &&
5267789Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
5268789Sahrens 		    zd->zd_dataset[len] == '/') {
5269789Sahrens 			if (write)
5270789Sahrens 				*write = 0;
5271789Sahrens 			return (1);
5272789Sahrens 		}
5273789Sahrens 	}
5274789Sahrens 
5275789Sahrens 	return (0);
5276789Sahrens }
52771676Sjpk 
52781676Sjpk /*
52791676Sjpk  * zone_find_by_any_path() -
52801676Sjpk  *
52811676Sjpk  * kernel-private routine similar to zone_find_by_path(), but which
52821676Sjpk  * effectively compares against zone paths rather than zonerootpath
52831676Sjpk  * (i.e., the last component of zonerootpaths, which should be "root/",
52841676Sjpk  * are not compared.)  This is done in order to accurately identify all
52851676Sjpk  * paths, whether zone-visible or not, including those which are parallel
52861676Sjpk  * to /root/, such as /dev/, /home/, etc...
52871676Sjpk  *
52881676Sjpk  * If the specified path does not fall under any zone path then global
52891676Sjpk  * zone is returned.
52901676Sjpk  *
52911676Sjpk  * The treat_abs parameter indicates whether the path should be treated as
52921676Sjpk  * an absolute path although it does not begin with "/".  (This supports
52931676Sjpk  * nfs mount syntax such as host:any/path.)
52941676Sjpk  *
52951676Sjpk  * The caller is responsible for zone_rele of the returned zone.
52961676Sjpk  */
52971676Sjpk zone_t *
52981676Sjpk zone_find_by_any_path(const char *path, boolean_t treat_abs)
52991676Sjpk {
53001676Sjpk 	zone_t *zone;
53011676Sjpk 	int path_offset = 0;
53021676Sjpk 
53031676Sjpk 	if (path == NULL) {
53041676Sjpk 		zone_hold(global_zone);
53051676Sjpk 		return (global_zone);
53061676Sjpk 	}
53071676Sjpk 
53081676Sjpk 	if (*path != '/') {
53091676Sjpk 		ASSERT(treat_abs);
53101676Sjpk 		path_offset = 1;
53111676Sjpk 	}
53121676Sjpk 
53131676Sjpk 	mutex_enter(&zonehash_lock);
53141676Sjpk 	for (zone = list_head(&zone_active); zone != NULL;
53151676Sjpk 	    zone = list_next(&zone_active, zone)) {
53161676Sjpk 		char	*c;
53171676Sjpk 		size_t	pathlen;
53181876Smp46848 		char *rootpath_start;
53191676Sjpk 
53201676Sjpk 		if (zone == global_zone)	/* skip global zone */
53211676Sjpk 			continue;
53221676Sjpk 
53231676Sjpk 		/* scan backwards to find start of last component */
53241676Sjpk 		c = zone->zone_rootpath + zone->zone_rootpathlen - 2;
53251676Sjpk 		do {
53261676Sjpk 			c--;
53271676Sjpk 		} while (*c != '/');
53281676Sjpk 
53291876Smp46848 		pathlen = c - zone->zone_rootpath + 1 - path_offset;
53301876Smp46848 		rootpath_start = (zone->zone_rootpath + path_offset);
53311876Smp46848 		if (strncmp(path, rootpath_start, pathlen) == 0)
53321676Sjpk 			break;
53331676Sjpk 	}
53341676Sjpk 	if (zone == NULL)
53351676Sjpk 		zone = global_zone;
53361676Sjpk 	zone_hold(zone);
53371676Sjpk 	mutex_exit(&zonehash_lock);
53381676Sjpk 	return (zone);
53391676Sjpk }
53403448Sdh155122 
53413448Sdh155122 /* List of data link names which are accessible from the zone */
53423448Sdh155122 struct dlnamelist {
53433448Sdh155122 	char			dlnl_name[LIFNAMSIZ];
53443448Sdh155122 	struct dlnamelist	*dlnl_next;
53453448Sdh155122 };
53463448Sdh155122 
53473448Sdh155122 
53483448Sdh155122 /*
53493448Sdh155122  * Check whether the datalink name (dlname) itself is present.
53503448Sdh155122  * Return true if found.
53513448Sdh155122  */
53523448Sdh155122 static boolean_t
53533448Sdh155122 zone_dlname(zone_t *zone, char *dlname)
53543448Sdh155122 {
53553448Sdh155122 	struct dlnamelist *dlnl;
53563448Sdh155122 	boolean_t found = B_FALSE;
53573448Sdh155122 
53583448Sdh155122 	mutex_enter(&zone->zone_lock);
53593448Sdh155122 	for (dlnl = zone->zone_dl_list; dlnl != NULL; dlnl = dlnl->dlnl_next) {
53603448Sdh155122 		if (strncmp(dlnl->dlnl_name, dlname, LIFNAMSIZ) == 0) {
53613448Sdh155122 			found = B_TRUE;
53623448Sdh155122 			break;
53633448Sdh155122 		}
53643448Sdh155122 	}
53653448Sdh155122 	mutex_exit(&zone->zone_lock);
53663448Sdh155122 	return (found);
53673448Sdh155122 }
53683448Sdh155122 
53693448Sdh155122 /*
53703448Sdh155122  * Add an data link name for the zone. Does not check for duplicates.
53713448Sdh155122  */
53723448Sdh155122 static int
53733448Sdh155122 zone_add_datalink(zoneid_t zoneid, char *dlname)
53743448Sdh155122 {
53753448Sdh155122 	struct dlnamelist *dlnl;
53763448Sdh155122 	zone_t *zone;
53773448Sdh155122 	zone_t *thiszone;
53783448Sdh155122 	int err;
53793448Sdh155122 
53803448Sdh155122 	dlnl = kmem_zalloc(sizeof (struct dlnamelist), KM_SLEEP);
53813448Sdh155122 	if ((err = copyinstr(dlname, dlnl->dlnl_name, LIFNAMSIZ, NULL)) != 0) {
53823448Sdh155122 		kmem_free(dlnl, sizeof (struct dlnamelist));
53833448Sdh155122 		return (set_errno(err));
53843448Sdh155122 	}
53853448Sdh155122 
53863448Sdh155122 	thiszone = zone_find_by_id(zoneid);
53873448Sdh155122 	if (thiszone == NULL) {
53883448Sdh155122 		kmem_free(dlnl, sizeof (struct dlnamelist));
53893448Sdh155122 		return (set_errno(ENXIO));
53903448Sdh155122 	}
53913448Sdh155122 
53923448Sdh155122 	/*
53933448Sdh155122 	 * Verify that the datalink name isn't already used by a different
53943448Sdh155122 	 * zone while allowing duplicate entries for the same zone (e.g. due
53953448Sdh155122 	 * to both using IPv4 and IPv6 on an interface)
53963448Sdh155122 	 */
53973448Sdh155122 	mutex_enter(&zonehash_lock);
53983448Sdh155122 	for (zone = list_head(&zone_active); zone != NULL;
53993448Sdh155122 	    zone = list_next(&zone_active, zone)) {
54003448Sdh155122 		if (zone->zone_id == zoneid)
54013448Sdh155122 			continue;
54023448Sdh155122 
54033448Sdh155122 		if (zone_dlname(zone, dlnl->dlnl_name)) {
54043448Sdh155122 			mutex_exit(&zonehash_lock);
54053448Sdh155122 			zone_rele(thiszone);
54063448Sdh155122 			kmem_free(dlnl, sizeof (struct dlnamelist));
54073448Sdh155122 			return (set_errno(EPERM));
54083448Sdh155122 		}
54093448Sdh155122 	}
54103448Sdh155122 	mutex_enter(&thiszone->zone_lock);
54113448Sdh155122 	dlnl->dlnl_next = thiszone->zone_dl_list;
54123448Sdh155122 	thiszone->zone_dl_list = dlnl;
54133448Sdh155122 	mutex_exit(&thiszone->zone_lock);
54143448Sdh155122 	mutex_exit(&zonehash_lock);
54153448Sdh155122 	zone_rele(thiszone);
54163448Sdh155122 	return (0);
54173448Sdh155122 }
54183448Sdh155122 
54193448Sdh155122 static int
54203448Sdh155122 zone_remove_datalink(zoneid_t zoneid, char *dlname)
54213448Sdh155122 {
54223448Sdh155122 	struct dlnamelist *dlnl, *odlnl, **dlnlp;
54233448Sdh155122 	zone_t *zone;
54243448Sdh155122 	int err;
54253448Sdh155122 
54263448Sdh155122 	dlnl = kmem_zalloc(sizeof (struct dlnamelist), KM_SLEEP);
54273448Sdh155122 	if ((err = copyinstr(dlname, dlnl->dlnl_name, LIFNAMSIZ, NULL)) != 0) {
54283448Sdh155122 		kmem_free(dlnl, sizeof (struct dlnamelist));
54293448Sdh155122 		return (set_errno(err));
54303448Sdh155122 	}
54313448Sdh155122 	zone = zone_find_by_id(zoneid);
54323448Sdh155122 	if (zone == NULL) {
54333448Sdh155122 		kmem_free(dlnl, sizeof (struct dlnamelist));
54343448Sdh155122 		return (set_errno(EINVAL));
54353448Sdh155122 	}
54363448Sdh155122 
54373448Sdh155122 	mutex_enter(&zone->zone_lock);
54383448Sdh155122 	/* Look for match */
54393448Sdh155122 	dlnlp = &zone->zone_dl_list;
54403448Sdh155122 	while (*dlnlp != NULL) {
54413448Sdh155122 		if (strncmp(dlnl->dlnl_name, (*dlnlp)->dlnl_name,
54423448Sdh155122 		    LIFNAMSIZ) == 0)
54433448Sdh155122 			goto found;
54443448Sdh155122 		dlnlp = &((*dlnlp)->dlnl_next);
54453448Sdh155122 	}
54463448Sdh155122 	mutex_exit(&zone->zone_lock);
54473448Sdh155122 	zone_rele(zone);
54483448Sdh155122 	kmem_free(dlnl, sizeof (struct dlnamelist));
54493448Sdh155122 	return (set_errno(ENXIO));
54503448Sdh155122 
54513448Sdh155122 found:
54523448Sdh155122 	odlnl = *dlnlp;
54533448Sdh155122 	*dlnlp = (*dlnlp)->dlnl_next;
54543448Sdh155122 	kmem_free(odlnl, sizeof (struct dlnamelist));
54553448Sdh155122 
54563448Sdh155122 	mutex_exit(&zone->zone_lock);
54573448Sdh155122 	zone_rele(zone);
54583448Sdh155122 	kmem_free(dlnl, sizeof (struct dlnamelist));
54593448Sdh155122 	return (0);
54603448Sdh155122 }
54613448Sdh155122 
54623448Sdh155122 /*
54633448Sdh155122  * Using the zoneidp as ALL_ZONES, we can lookup which zone is using datalink
54643448Sdh155122  * name (dlname); otherwise we just check if the specified zoneidp has access
54653448Sdh155122  * to the datalink name.
54663448Sdh155122  */
54673448Sdh155122 static int
54683448Sdh155122 zone_check_datalink(zoneid_t *zoneidp, char *dlname)
54693448Sdh155122 {
54703448Sdh155122 	zoneid_t id;
54713448Sdh155122 	char *dln;
54723448Sdh155122 	zone_t *zone;
54733448Sdh155122 	int err = 0;
54743448Sdh155122 	boolean_t allzones = B_FALSE;
54753448Sdh155122 
54763448Sdh155122 	if (copyin(zoneidp, &id, sizeof (id)) != 0) {
54773448Sdh155122 		return (set_errno(EFAULT));
54783448Sdh155122 	}
54793448Sdh155122 	dln = kmem_zalloc(LIFNAMSIZ, KM_SLEEP);
54803448Sdh155122 	if ((err = copyinstr(dlname, dln, LIFNAMSIZ, NULL)) != 0) {
54813448Sdh155122 		kmem_free(dln, LIFNAMSIZ);
54823448Sdh155122 		return (set_errno(err));
54833448Sdh155122 	}
54843448Sdh155122 
54853448Sdh155122 	if (id == ALL_ZONES)
54863448Sdh155122 		allzones = B_TRUE;
54873448Sdh155122 
54883448Sdh155122 	/*
54893448Sdh155122 	 * Check whether datalink name is already used.
54903448Sdh155122 	 */
54913448Sdh155122 	mutex_enter(&zonehash_lock);
54923448Sdh155122 	for (zone = list_head(&zone_active); zone != NULL;
54933448Sdh155122 	    zone = list_next(&zone_active, zone)) {
54943448Sdh155122 		if (allzones || (id == zone->zone_id)) {
54953448Sdh155122 			if (!zone_dlname(zone, dln))
54963448Sdh155122 				continue;
54973448Sdh155122 			if (allzones)
54983448Sdh155122 				err = copyout(&zone->zone_id, zoneidp,
54993448Sdh155122 				    sizeof (*zoneidp));
55003448Sdh155122 
55013448Sdh155122 			mutex_exit(&zonehash_lock);
55023448Sdh155122 			kmem_free(dln, LIFNAMSIZ);
55033448Sdh155122 			return (err ? set_errno(EFAULT) : 0);
55043448Sdh155122 		}
55053448Sdh155122 	}
55063448Sdh155122 
55073448Sdh155122 	/* datalink name is not found in any active zone. */
55083448Sdh155122 	mutex_exit(&zonehash_lock);
55093448Sdh155122 	kmem_free(dln, LIFNAMSIZ);
55103448Sdh155122 	return (set_errno(ENXIO));
55113448Sdh155122 }
55123448Sdh155122 
55133448Sdh155122 /*
55143448Sdh155122  * Get the names of the datalinks assigned to a zone.
55153448Sdh155122  * Here *nump is the number of datalinks, and the assumption
55163448Sdh155122  * is that the caller will gurantee that the the supplied buffer is
55173448Sdh155122  * big enough to hold at least #*nump datalink names, that is,
55183448Sdh155122  * LIFNAMSIZ X *nump
55193448Sdh155122  * On return, *nump will be the "new" number of datalinks, if it
55203448Sdh155122  * ever changed.
55213448Sdh155122  */
55223448Sdh155122 static int
55233448Sdh155122 zone_list_datalink(zoneid_t zoneid, int *nump, char *buf)
55243448Sdh155122 {
55253448Sdh155122 	int num, dlcount;
55263448Sdh155122 	zone_t *zone;
55273448Sdh155122 	struct dlnamelist *dlnl;
55283448Sdh155122 	char *ptr;
55293448Sdh155122 
55303448Sdh155122 	if (copyin(nump, &dlcount, sizeof (dlcount)) != 0)
55313448Sdh155122 		return (set_errno(EFAULT));
55323448Sdh155122 
55333448Sdh155122 	zone = zone_find_by_id(zoneid);
55343448Sdh155122 	if (zone == NULL) {
55353448Sdh155122 		return (set_errno(ENXIO));
55363448Sdh155122 	}
55373448Sdh155122 
55383448Sdh155122 	num = 0;
55393448Sdh155122 	mutex_enter(&zone->zone_lock);
55403448Sdh155122 	ptr = buf;
55413448Sdh155122 	for (dlnl = zone->zone_dl_list; dlnl != NULL; dlnl = dlnl->dlnl_next) {
55423448Sdh155122 		/*
55433448Sdh155122 		 * If the list changed and the new number is bigger
55443448Sdh155122 		 * than what the caller supplied, just count, don't
55453448Sdh155122 		 * do copyout
55463448Sdh155122 		 */
55473448Sdh155122 		if (++num > dlcount)
55483448Sdh155122 			continue;
55493448Sdh155122 		if (copyout(dlnl->dlnl_name, ptr, LIFNAMSIZ) != 0) {
55503448Sdh155122 			mutex_exit(&zone->zone_lock);
55513448Sdh155122 			zone_rele(zone);
55523448Sdh155122 			return (set_errno(EFAULT));
55533448Sdh155122 		}
55543448Sdh155122 		ptr += LIFNAMSIZ;
55553448Sdh155122 	}
55563448Sdh155122 	mutex_exit(&zone->zone_lock);
55573448Sdh155122 	zone_rele(zone);
55583448Sdh155122 
55593448Sdh155122 	/* Increased or decreased, caller should be notified. */
55603448Sdh155122 	if (num != dlcount) {
55613448Sdh155122 		if (copyout(&num, nump, sizeof (num)) != 0) {
55623448Sdh155122 			return (set_errno(EFAULT));
55633448Sdh155122 		}
55643448Sdh155122 	}
55653448Sdh155122 	return (0);
55663448Sdh155122 }
55673448Sdh155122 
55683448Sdh155122 /*
55693448Sdh155122  * Public interface for looking up a zone by zoneid. It's a customized version
55703448Sdh155122  * for netstack_zone_create(), it:
55713448Sdh155122  * 1. Doesn't acquire the zonehash_lock, since it is called from
55723448Sdh155122  *    zone_key_create() or zone_zsd_configure(), lock already held.
55733448Sdh155122  * 2. Doesn't check the status of the zone.
55743448Sdh155122  * 3. It will be called even before zone_init is called, in that case the
55753448Sdh155122  *    address of zone0 is returned directly, and netstack_zone_create()
55763448Sdh155122  *    will only assign a value to zone0.zone_netstack, won't break anything.
55773448Sdh155122  */
55783448Sdh155122 zone_t *
55793448Sdh155122 zone_find_by_id_nolock(zoneid_t zoneid)
55803448Sdh155122 {
55813448Sdh155122 	ASSERT(MUTEX_HELD(&zonehash_lock));
55823448Sdh155122 
55833448Sdh155122 	if (zonehashbyid == NULL)
55843448Sdh155122 		return (&zone0);
55853448Sdh155122 	else
55863448Sdh155122 		return (zone_find_all_by_id(zoneid));
55873448Sdh155122 }
5588