xref: /titanic_44/usr/src/uts/common/os/zone.c (revision 86ad481cc8bfbc0155ef221a0fe07ef23db58f68)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2197eda132Sraf 
227c478bd9Sstevel@tonic-gate /*
23134a1f4eSCasper H.S. Dik  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Zones
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  *   A zone is a named collection of processes, namespace constraints,
307c478bd9Sstevel@tonic-gate  *   and other system resources which comprise a secure and manageable
317c478bd9Sstevel@tonic-gate  *   application containment facility.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  *   Zones (represented by the reference counted zone_t) are tracked in
347c478bd9Sstevel@tonic-gate  *   the kernel in the zonehash.  Elsewhere in the kernel, Zone IDs
357c478bd9Sstevel@tonic-gate  *   (zoneid_t) are used to track zone association.  Zone IDs are
367c478bd9Sstevel@tonic-gate  *   dynamically generated when the zone is created; if a persistent
377c478bd9Sstevel@tonic-gate  *   identifier is needed (core files, accounting logs, audit trail,
387c478bd9Sstevel@tonic-gate  *   etc.), the zone name should be used.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  *   Global Zone:
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  *   The global zone (zoneid 0) is automatically associated with all
447c478bd9Sstevel@tonic-gate  *   system resources that have not been bound to a user-created zone.
457c478bd9Sstevel@tonic-gate  *   This means that even systems where zones are not in active use
467c478bd9Sstevel@tonic-gate  *   have a global zone, and all processes, mounts, etc. are
477c478bd9Sstevel@tonic-gate  *   associated with that zone.  The global zone is generally
487c478bd9Sstevel@tonic-gate  *   unconstrained in terms of privileges and access, though the usual
497c478bd9Sstevel@tonic-gate  *   credential and privilege based restrictions apply.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  *   Zone States:
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  *   The states in which a zone may be in and the transitions are as
557c478bd9Sstevel@tonic-gate  *   follows:
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  *   ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially
587c478bd9Sstevel@tonic-gate  *   initialized zone is added to the list of active zones on the system but
597c478bd9Sstevel@tonic-gate  *   isn't accessible.
607c478bd9Sstevel@tonic-gate  *
61bd41d0a8Snordmark  *   ZONE_IS_INITIALIZED: Initialization complete except the ZSD callbacks are
62bd41d0a8Snordmark  *   not yet completed. Not possible to enter the zone, but attributes can
63bd41d0a8Snordmark  *   be retrieved.
64bd41d0a8Snordmark  *
657c478bd9Sstevel@tonic-gate  *   ZONE_IS_READY: zsched (the kernel dummy process for a zone) is
667c478bd9Sstevel@tonic-gate  *   ready.  The zone is made visible after the ZSD constructor callbacks are
677c478bd9Sstevel@tonic-gate  *   executed.  A zone remains in this state until it transitions into
687c478bd9Sstevel@tonic-gate  *   the ZONE_IS_BOOTING state as a result of a call to zone_boot().
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  *   ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start
717c478bd9Sstevel@tonic-gate  *   init.  Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN
727c478bd9Sstevel@tonic-gate  *   state.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  *   ZONE_IS_RUNNING: The zone is open for business: zsched has
757c478bd9Sstevel@tonic-gate  *   successfully started init.   A zone remains in this state until
767c478bd9Sstevel@tonic-gate  *   zone_shutdown() is called.
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is
797c478bd9Sstevel@tonic-gate  *   killing all processes running in the zone. The zone remains
807c478bd9Sstevel@tonic-gate  *   in this state until there are no more user processes running in the zone.
817c478bd9Sstevel@tonic-gate  *   zone_create(), zone_enter(), and zone_destroy() on this zone will fail.
827c478bd9Sstevel@tonic-gate  *   Since zone_shutdown() is restartable, it may be called successfully
837c478bd9Sstevel@tonic-gate  *   multiple times for the same zone_t.  Setting of the zone's state to
847c478bd9Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check
857c478bd9Sstevel@tonic-gate  *   the zone's status without worrying about it being a moving target.
867c478bd9Sstevel@tonic-gate  *
877c478bd9Sstevel@tonic-gate  *   ZONE_IS_EMPTY: zone_shutdown() has been called, and there
887c478bd9Sstevel@tonic-gate  *   are no more user processes in the zone.  The zone remains in this
897c478bd9Sstevel@tonic-gate  *   state until there are no more kernel threads associated with the
907c478bd9Sstevel@tonic-gate  *   zone.  zone_create(), zone_enter(), and zone_destroy() on this zone will
917c478bd9Sstevel@tonic-gate  *   fail.
927c478bd9Sstevel@tonic-gate  *
937c478bd9Sstevel@tonic-gate  *   ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone
947c478bd9Sstevel@tonic-gate  *   have exited.  zone_shutdown() returns.  Henceforth it is not possible to
957c478bd9Sstevel@tonic-gate  *   join the zone or create kernel threads therein.
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  *   ZONE_IS_DYING: zone_destroy() has been called on the zone; zone
987c478bd9Sstevel@tonic-gate  *   remains in this state until zsched exits.  Calls to zone_find_by_*()
997c478bd9Sstevel@tonic-gate  *   return NULL from now on.
1007c478bd9Sstevel@tonic-gate  *
1017c478bd9Sstevel@tonic-gate  *   ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0).  There are no
1027c478bd9Sstevel@tonic-gate  *   processes or threads doing work on behalf of the zone.  The zone is
1037c478bd9Sstevel@tonic-gate  *   removed from the list of active zones.  zone_destroy() returns, and
1047c478bd9Sstevel@tonic-gate  *   the zone can be recreated.
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  *   ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor
1077c478bd9Sstevel@tonic-gate  *   callbacks are executed, and all memory associated with the zone is
1087c478bd9Sstevel@tonic-gate  *   freed.
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  *   Threads can wait for the zone to enter a requested state by using
1117c478bd9Sstevel@tonic-gate  *   zone_status_wait() or zone_status_timedwait() with the desired
1127c478bd9Sstevel@tonic-gate  *   state passed in as an argument.  Zone state transitions are
1137c478bd9Sstevel@tonic-gate  *   uni-directional; it is not possible to move back to an earlier state.
1147c478bd9Sstevel@tonic-gate  *
1157c478bd9Sstevel@tonic-gate  *
1167c478bd9Sstevel@tonic-gate  *   Zone-Specific Data:
1177c478bd9Sstevel@tonic-gate  *
1187c478bd9Sstevel@tonic-gate  *   Subsystems needing to maintain zone-specific data can store that
1197c478bd9Sstevel@tonic-gate  *   data using the ZSD mechanism.  This provides a zone-specific data
1207c478bd9Sstevel@tonic-gate  *   store, similar to thread-specific data (see pthread_getspecific(3C)
1217c478bd9Sstevel@tonic-gate  *   or the TSD code in uts/common/disp/thread.c.  Also, ZSD can be used
1227c478bd9Sstevel@tonic-gate  *   to register callbacks to be invoked when a zone is created, shut
1237c478bd9Sstevel@tonic-gate  *   down, or destroyed.  This can be used to initialize zone-specific
1247c478bd9Sstevel@tonic-gate  *   data for new zones and to clean up when zones go away.
1257c478bd9Sstevel@tonic-gate  *
1267c478bd9Sstevel@tonic-gate  *
1277c478bd9Sstevel@tonic-gate  *   Data Structures:
1287c478bd9Sstevel@tonic-gate  *
1297c478bd9Sstevel@tonic-gate  *   The per-zone structure (zone_t) is reference counted, and freed
1307c478bd9Sstevel@tonic-gate  *   when all references are released.  zone_hold and zone_rele can be
1317c478bd9Sstevel@tonic-gate  *   used to adjust the reference count.  In addition, reference counts
1327c478bd9Sstevel@tonic-gate  *   associated with the cred_t structure are tracked separately using
1337c478bd9Sstevel@tonic-gate  *   zone_cred_hold and zone_cred_rele.
1347c478bd9Sstevel@tonic-gate  *
1357c478bd9Sstevel@tonic-gate  *   Pointers to active zone_t's are stored in two hash tables; one
1367c478bd9Sstevel@tonic-gate  *   for searching by id, the other for searching by name.  Lookups
1377c478bd9Sstevel@tonic-gate  *   can be performed on either basis, using zone_find_by_id and
1387c478bd9Sstevel@tonic-gate  *   zone_find_by_name.  Both return zone_t pointers with the zone
1397c478bd9Sstevel@tonic-gate  *   held, so zone_rele should be called when the pointer is no longer
1407c478bd9Sstevel@tonic-gate  *   needed.  Zones can also be searched by path; zone_find_by_path
1417c478bd9Sstevel@tonic-gate  *   returns the zone with which a path name is associated (global
1427c478bd9Sstevel@tonic-gate  *   zone if the path is not within some other zone's file system
1437c478bd9Sstevel@tonic-gate  *   hierarchy).  This currently requires iterating through each zone,
1447c478bd9Sstevel@tonic-gate  *   so it is slower than an id or name search via a hash table.
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  *
1477c478bd9Sstevel@tonic-gate  *   Locking:
1487c478bd9Sstevel@tonic-gate  *
1497c478bd9Sstevel@tonic-gate  *   zonehash_lock: This is a top-level global lock used to protect the
1507c478bd9Sstevel@tonic-gate  *       zone hash tables and lists.  Zones cannot be created or destroyed
1517c478bd9Sstevel@tonic-gate  *       while this lock is held.
1527c478bd9Sstevel@tonic-gate  *   zone_status_lock: This is a global lock protecting zone state.
1537c478bd9Sstevel@tonic-gate  *       Zones cannot change state while this lock is held.  It also
1547c478bd9Sstevel@tonic-gate  *       protects the list of kernel threads associated with a zone.
1557c478bd9Sstevel@tonic-gate  *   zone_lock: This is a per-zone lock used to protect several fields of
1567c478bd9Sstevel@tonic-gate  *       the zone_t (see <sys/zone.h> for details).  In addition, holding
1577c478bd9Sstevel@tonic-gate  *       this lock means that the zone cannot go away.
1580209230bSgjelinek  *   zone_nlwps_lock: This is a per-zone lock used to protect the fields
1590209230bSgjelinek  *	 related to the zone.max-lwps rctl.
1600209230bSgjelinek  *   zone_mem_lock: This is a per-zone lock used to protect the fields
1610209230bSgjelinek  *	 related to the zone.max-locked-memory and zone.max-swap rctls.
1620fbb751dSJohn Levon  *   zone_rctl_lock: This is a per-zone lock used to protect other rctls,
1630fbb751dSJohn Levon  *       currently just max_lofi
1647c478bd9Sstevel@tonic-gate  *   zsd_key_lock: This is a global lock protecting the key state for ZSD.
1657c478bd9Sstevel@tonic-gate  *   zone_deathrow_lock: This is a global lock protecting the "deathrow"
1667c478bd9Sstevel@tonic-gate  *       list (a list of zones in the ZONE_IS_DEAD state).
1677c478bd9Sstevel@tonic-gate  *
1687c478bd9Sstevel@tonic-gate  *   Ordering requirements:
1697c478bd9Sstevel@tonic-gate  *       pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock -->
1707c478bd9Sstevel@tonic-gate  *       	zone_lock --> zsd_key_lock --> pidlock --> p_lock
1717c478bd9Sstevel@tonic-gate  *
1720209230bSgjelinek  *   When taking zone_mem_lock or zone_nlwps_lock, the lock ordering is:
1730209230bSgjelinek  *	zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock
174ff19e029SMenno Lageman  *	zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_nlwps_lock
1750209230bSgjelinek  *
1767c478bd9Sstevel@tonic-gate  *   Blocking memory allocations are permitted while holding any of the
1777c478bd9Sstevel@tonic-gate  *   zone locks.
1787c478bd9Sstevel@tonic-gate  *
1797c478bd9Sstevel@tonic-gate  *
1807c478bd9Sstevel@tonic-gate  *   System Call Interface:
1817c478bd9Sstevel@tonic-gate  *
1827c478bd9Sstevel@tonic-gate  *   The zone subsystem can be managed and queried from user level with
1837c478bd9Sstevel@tonic-gate  *   the following system calls (all subcodes of the primary "zone"
1847c478bd9Sstevel@tonic-gate  *   system call):
1857c478bd9Sstevel@tonic-gate  *   - zone_create: creates a zone with selected attributes (name,
186fa9e4066Sahrens  *     root path, privileges, resource controls, ZFS datasets)
1877c478bd9Sstevel@tonic-gate  *   - zone_enter: allows the current process to enter a zone
1887c478bd9Sstevel@tonic-gate  *   - zone_getattr: reports attributes of a zone
1893f2f09c1Sdp  *   - zone_setattr: set attributes of a zone
1903f2f09c1Sdp  *   - zone_boot: set 'init' running for the zone
1917c478bd9Sstevel@tonic-gate  *   - zone_list: lists all zones active in the system
1927c478bd9Sstevel@tonic-gate  *   - zone_lookup: looks up zone id based on name
1937c478bd9Sstevel@tonic-gate  *   - zone_shutdown: initiates shutdown process (see states above)
1947c478bd9Sstevel@tonic-gate  *   - zone_destroy: completes shutdown process (see states above)
1957c478bd9Sstevel@tonic-gate  *
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate #include <sys/priv_impl.h>
1997c478bd9Sstevel@tonic-gate #include <sys/cred.h>
2007c478bd9Sstevel@tonic-gate #include <c2/audit.h>
2017c478bd9Sstevel@tonic-gate #include <sys/debug.h>
2027c478bd9Sstevel@tonic-gate #include <sys/file.h>
2037c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
2040209230bSgjelinek #include <sys/kstat.h>
2057c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
20645916cd2Sjpk #include <sys/note.h>
2077c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
2087c478bd9Sstevel@tonic-gate #include <sys/proc.h>
2097c478bd9Sstevel@tonic-gate #include <sys/project.h>
210cf8f45c7Sdstaff #include <sys/sysevent.h>
2117c478bd9Sstevel@tonic-gate #include <sys/task.h>
2127c478bd9Sstevel@tonic-gate #include <sys/systm.h>
2137c478bd9Sstevel@tonic-gate #include <sys/types.h>
2147c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
2157c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
2167c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
2177c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
2187c478bd9Sstevel@tonic-gate #include <sys/policy.h>
2197c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
2207c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h>
2217c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
2227c478bd9Sstevel@tonic-gate #include <sys/class.h>
2237c478bd9Sstevel@tonic-gate #include <sys/pool.h>
2247c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
2257c478bd9Sstevel@tonic-gate #include <sys/pset.h>
226a19609f8Sjv227347 #include <sys/strlog.h>
2277c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
2287c478bd9Sstevel@tonic-gate #include <sys/callb.h>
2297c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
2307c478bd9Sstevel@tonic-gate #include <sys/corectl.h>
231824c205fSml93401 #include <sys/ipc_impl.h>
232134a1f4eSCasper H.S. Dik #include <sys/klpd.h>
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate #include <sys/door.h>
2357c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
236bd41d0a8Snordmark #include <sys/sdt.h>
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
2397c478bd9Sstevel@tonic-gate #include <sys/session.h>
2407c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
2417c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
2423f2f09c1Sdp #include <sys/sunddi.h>
2437c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
2447c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
2457c478bd9Sstevel@tonic-gate #include <sys/fss.h>
2469acbbeafSnn35248 #include <sys/brand.h>
2477c478bd9Sstevel@tonic-gate #include <sys/zone.h>
248f4b3ec61Sdh155122 #include <net/if.h>
249c97ad5cdSakolb #include <sys/cpucaps.h>
2500209230bSgjelinek #include <vm/seg.h>
2512b24ab6bSSebastien Roy #include <sys/mac.h>
2522b24ab6bSSebastien Roy 
253a19609f8Sjv227347 /*
254a19609f8Sjv227347  * This constant specifies the number of seconds that threads waiting for
255a19609f8Sjv227347  * subsystems to release a zone's general-purpose references will wait before
256a19609f8Sjv227347  * they log the zone's reference counts.  The constant's value shouldn't
257a19609f8Sjv227347  * be so small that reference counts are unnecessarily reported for zones
258a19609f8Sjv227347  * whose references are slowly released.  On the other hand, it shouldn't be so
259a19609f8Sjv227347  * large that users reboot their systems out of frustration over hung zones
260a19609f8Sjv227347  * before the system logs the zones' reference counts.
261a19609f8Sjv227347  */
262a19609f8Sjv227347 #define	ZONE_DESTROY_TIMEOUT_SECS	60
263a19609f8Sjv227347 
2642b24ab6bSSebastien Roy /* List of data link IDs which are accessible from the zone */
2652b24ab6bSSebastien Roy typedef struct zone_dl {
2662b24ab6bSSebastien Roy 	datalink_id_t	zdl_id;
267550b6e40SSowmini Varadhan 	nvlist_t	*zdl_net;
2682b24ab6bSSebastien Roy 	list_node_t	zdl_linkage;
2692b24ab6bSSebastien Roy } zone_dl_t;
2700209230bSgjelinek 
2717c478bd9Sstevel@tonic-gate /*
2727c478bd9Sstevel@tonic-gate  * cv used to signal that all references to the zone have been released.  This
2737c478bd9Sstevel@tonic-gate  * needs to be global since there may be multiple waiters, and the first to
2747c478bd9Sstevel@tonic-gate  * wake up will free the zone_t, hence we cannot use zone->zone_cv.
2757c478bd9Sstevel@tonic-gate  */
2767c478bd9Sstevel@tonic-gate static kcondvar_t zone_destroy_cv;
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * Lock used to serialize access to zone_cv.  This could have been per-zone,
2797c478bd9Sstevel@tonic-gate  * but then we'd need another lock for zone_destroy_cv, and why bother?
2807c478bd9Sstevel@tonic-gate  */
2817c478bd9Sstevel@tonic-gate static kmutex_t zone_status_lock;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * ZSD-related global variables.
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate static kmutex_t zsd_key_lock;	/* protects the following two */
2877c478bd9Sstevel@tonic-gate /*
2887c478bd9Sstevel@tonic-gate  * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval.
2897c478bd9Sstevel@tonic-gate  */
2907c478bd9Sstevel@tonic-gate static zone_key_t zsd_keyval = 0;
2917c478bd9Sstevel@tonic-gate /*
2927c478bd9Sstevel@tonic-gate  * Global list of registered keys.  We use this when a new zone is created.
2937c478bd9Sstevel@tonic-gate  */
2947c478bd9Sstevel@tonic-gate static list_t zsd_registered_keys;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate int zone_hash_size = 256;
29745916cd2Sjpk static mod_hash_t *zonehashbyname, *zonehashbyid, *zonehashbylabel;
2987c478bd9Sstevel@tonic-gate static kmutex_t zonehash_lock;
2997c478bd9Sstevel@tonic-gate static uint_t zonecount;
3007c478bd9Sstevel@tonic-gate static id_space_t *zoneid_space;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate  * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the
3047c478bd9Sstevel@tonic-gate  * kernel proper runs, and which manages all other zones.
3057c478bd9Sstevel@tonic-gate  *
3067c478bd9Sstevel@tonic-gate  * Although not declared as static, the variable "zone0" should not be used
3077c478bd9Sstevel@tonic-gate  * except for by code that needs to reference the global zone early on in boot,
3087c478bd9Sstevel@tonic-gate  * before it is fully initialized.  All other consumers should use
3097c478bd9Sstevel@tonic-gate  * 'global_zone'.
3107c478bd9Sstevel@tonic-gate  */
3117c478bd9Sstevel@tonic-gate zone_t zone0;
3127c478bd9Sstevel@tonic-gate zone_t *global_zone = NULL;	/* Set when the global zone is initialized */
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate  * List of active zones, protected by zonehash_lock.
3167c478bd9Sstevel@tonic-gate  */
3177c478bd9Sstevel@tonic-gate static list_t zone_active;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate  * List of destroyed zones that still have outstanding cred references.
3217c478bd9Sstevel@tonic-gate  * Used for debugging.  Uses a separate lock to avoid lock ordering
3227c478bd9Sstevel@tonic-gate  * problems in zone_free.
3237c478bd9Sstevel@tonic-gate  */
3247c478bd9Sstevel@tonic-gate static list_t zone_deathrow;
3257c478bd9Sstevel@tonic-gate static kmutex_t zone_deathrow_lock;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */
3287c478bd9Sstevel@tonic-gate uint_t maxzones = 8192;
3297c478bd9Sstevel@tonic-gate 
330cf8f45c7Sdstaff /* Event channel to sent zone state change notifications */
331cf8f45c7Sdstaff evchan_t *zone_event_chan;
332cf8f45c7Sdstaff 
333cf8f45c7Sdstaff /*
334cf8f45c7Sdstaff  * This table holds the mapping from kernel zone states to
335cf8f45c7Sdstaff  * states visible in the state notification API.
336cf8f45c7Sdstaff  * The idea is that we only expose "obvious" states and
337cf8f45c7Sdstaff  * do not expose states which are just implementation details.
338cf8f45c7Sdstaff  */
339cf8f45c7Sdstaff const char  *zone_status_table[] = {
340cf8f45c7Sdstaff 	ZONE_EVENT_UNINITIALIZED,	/* uninitialized */
341bd41d0a8Snordmark 	ZONE_EVENT_INITIALIZED,		/* initialized */
342cf8f45c7Sdstaff 	ZONE_EVENT_READY,		/* ready */
343cf8f45c7Sdstaff 	ZONE_EVENT_READY,		/* booting */
344cf8f45c7Sdstaff 	ZONE_EVENT_RUNNING,		/* running */
345cf8f45c7Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* shutting_down */
346cf8f45c7Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* empty */
347cf8f45c7Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* down */
348cf8f45c7Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* dying */
349cf8f45c7Sdstaff 	ZONE_EVENT_UNINITIALIZED,	/* dead */
350cf8f45c7Sdstaff };
351cf8f45c7Sdstaff 
3527c478bd9Sstevel@tonic-gate /*
353a19609f8Sjv227347  * This array contains the names of the subsystems listed in zone_ref_subsys_t
354a19609f8Sjv227347  * (see sys/zone.h).
355a19609f8Sjv227347  */
356a19609f8Sjv227347 static char *zone_ref_subsys_names[] = {
357a19609f8Sjv227347 	"NFS",		/* ZONE_REF_NFS */
358a19609f8Sjv227347 	"NFSv4",	/* ZONE_REF_NFSV4 */
359a19609f8Sjv227347 	"SMBFS",	/* ZONE_REF_SMBFS */
360a19609f8Sjv227347 	"MNTFS",	/* ZONE_REF_MNTFS */
361a19609f8Sjv227347 	"LOFI",		/* ZONE_REF_LOFI */
362a19609f8Sjv227347 	"VFS",		/* ZONE_REF_VFS */
363a19609f8Sjv227347 	"IPC"		/* ZONE_REF_IPC */
364a19609f8Sjv227347 };
365a19609f8Sjv227347 
366a19609f8Sjv227347 /*
3677c478bd9Sstevel@tonic-gate  * This isn't static so lint doesn't complain.
3687c478bd9Sstevel@tonic-gate  */
3697c478bd9Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares;
370c6939658Ssl108498 rctl_hndl_t rc_zone_locked_mem;
3710209230bSgjelinek rctl_hndl_t rc_zone_max_swap;
3720fbb751dSJohn Levon rctl_hndl_t rc_zone_max_lofi;
373c97ad5cdSakolb rctl_hndl_t rc_zone_cpu_cap;
3747c478bd9Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps;
375ff19e029SMenno Lageman rctl_hndl_t rc_zone_nprocs;
376824c205fSml93401 rctl_hndl_t rc_zone_shmmax;
377824c205fSml93401 rctl_hndl_t rc_zone_shmmni;
378824c205fSml93401 rctl_hndl_t rc_zone_semmni;
379824c205fSml93401 rctl_hndl_t rc_zone_msgmni;
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate  * Synchronization primitives used to synchronize between mounts and zone
3827c478bd9Sstevel@tonic-gate  * creation/destruction.
3837c478bd9Sstevel@tonic-gate  */
3847c478bd9Sstevel@tonic-gate static int mounts_in_progress;
3857c478bd9Sstevel@tonic-gate static kcondvar_t mount_cv;
3867c478bd9Sstevel@tonic-gate static kmutex_t mount_lock;
3877c478bd9Sstevel@tonic-gate 
3883f2f09c1Sdp const char * const zone_default_initname = "/sbin/init";
38945916cd2Sjpk static char * const zone_prefix = "/zone/";
3907c478bd9Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid);
3912b24ab6bSSebastien Roy static int zone_add_datalink(zoneid_t, datalink_id_t);
3922b24ab6bSSebastien Roy static int zone_remove_datalink(zoneid_t, datalink_id_t);
3932b24ab6bSSebastien Roy static int zone_list_datalink(zoneid_t, int *, datalink_id_t *);
394550b6e40SSowmini Varadhan static int zone_set_network(zoneid_t, zone_net_data_t *);
395550b6e40SSowmini Varadhan static int zone_get_network(zoneid_t, zone_net_data_t *);
3967c478bd9Sstevel@tonic-gate 
397bd41d0a8Snordmark typedef boolean_t zsd_applyfn_t(kmutex_t *, boolean_t, zone_t *, zone_key_t);
398bd41d0a8Snordmark 
399bd41d0a8Snordmark static void zsd_apply_all_zones(zsd_applyfn_t *, zone_key_t);
400bd41d0a8Snordmark static void zsd_apply_all_keys(zsd_applyfn_t *, zone_t *);
401bd41d0a8Snordmark static boolean_t zsd_apply_create(kmutex_t *, boolean_t, zone_t *, zone_key_t);
402bd41d0a8Snordmark static boolean_t zsd_apply_shutdown(kmutex_t *, boolean_t, zone_t *,
403bd41d0a8Snordmark     zone_key_t);
404bd41d0a8Snordmark static boolean_t zsd_apply_destroy(kmutex_t *, boolean_t, zone_t *, zone_key_t);
405bd41d0a8Snordmark static boolean_t zsd_wait_for_creator(zone_t *, struct zsd_entry *,
406bd41d0a8Snordmark     kmutex_t *);
407bd41d0a8Snordmark static boolean_t zsd_wait_for_inprogress(zone_t *, struct zsd_entry *,
408bd41d0a8Snordmark     kmutex_t *);
409bd41d0a8Snordmark 
4107c478bd9Sstevel@tonic-gate /*
411821c4a97Sdp  * Bump this number when you alter the zone syscall interfaces; this is
412821c4a97Sdp  * because we need to have support for previous API versions in libc
413821c4a97Sdp  * to support patching; libc calls into the kernel to determine this number.
414821c4a97Sdp  *
415821c4a97Sdp  * Version 1 of the API is the version originally shipped with Solaris 10
416821c4a97Sdp  * Version 2 alters the zone_create system call in order to support more
417821c4a97Sdp  *     arguments by moving the args into a structure; and to do better
418821c4a97Sdp  *     error reporting when zone_create() fails.
419821c4a97Sdp  * Version 3 alters the zone_create system call in order to support the
420821c4a97Sdp  *     import of ZFS datasets to zones.
42145916cd2Sjpk  * Version 4 alters the zone_create system call in order to support
42245916cd2Sjpk  *     Trusted Extensions.
4233f2f09c1Sdp  * Version 5 alters the zone_boot system call, and converts its old
4243f2f09c1Sdp  *     bootargs parameter to be set by the zone_setattr API instead.
425f4b3ec61Sdh155122  * Version 6 adds the flag argument to zone_create.
426821c4a97Sdp  */
427f4b3ec61Sdh155122 static const int ZONE_SYSCALL_API_VERSION = 6;
428821c4a97Sdp 
429821c4a97Sdp /*
4307c478bd9Sstevel@tonic-gate  * Certain filesystems (such as NFS and autofs) need to know which zone
4317c478bd9Sstevel@tonic-gate  * the mount is being placed in.  Because of this, we need to be able to
4327c478bd9Sstevel@tonic-gate  * ensure that a zone isn't in the process of being created such that
4337c478bd9Sstevel@tonic-gate  * nfs_mount() thinks it is in the global zone, while by the time it
4347c478bd9Sstevel@tonic-gate  * gets added the list of mounted zones, it ends up on zoneA's mount
4357c478bd9Sstevel@tonic-gate  * list.
4367c478bd9Sstevel@tonic-gate  *
4377c478bd9Sstevel@tonic-gate  * The following functions: block_mounts()/resume_mounts() and
4387c478bd9Sstevel@tonic-gate  * mount_in_progress()/mount_completed() are used by zones and the VFS
4397c478bd9Sstevel@tonic-gate  * layer (respectively) to synchronize zone creation and new mounts.
4407c478bd9Sstevel@tonic-gate  *
4417c478bd9Sstevel@tonic-gate  * The semantics are like a reader-reader lock such that there may
4427c478bd9Sstevel@tonic-gate  * either be multiple mounts (or zone creations, if that weren't
4437c478bd9Sstevel@tonic-gate  * serialized by zonehash_lock) in progress at the same time, but not
4447c478bd9Sstevel@tonic-gate  * both.
4457c478bd9Sstevel@tonic-gate  *
4467c478bd9Sstevel@tonic-gate  * We use cv's so the user can ctrl-C out of the operation if it's
4477c478bd9Sstevel@tonic-gate  * taking too long.
4487c478bd9Sstevel@tonic-gate  *
4497c478bd9Sstevel@tonic-gate  * The semantics are such that there is unfair bias towards the
4507c478bd9Sstevel@tonic-gate  * "current" operation.  This means that zone creations may starve if
4517c478bd9Sstevel@tonic-gate  * there is a rapid succession of new mounts coming in to the system, or
4527c478bd9Sstevel@tonic-gate  * there is a remote possibility that zones will be created at such a
4537c478bd9Sstevel@tonic-gate  * rate that new mounts will not be able to proceed.
4547c478bd9Sstevel@tonic-gate  */
4557c478bd9Sstevel@tonic-gate /*
4567c478bd9Sstevel@tonic-gate  * Prevent new mounts from progressing to the point of calling
4577c478bd9Sstevel@tonic-gate  * VFS_MOUNT().  If there are already mounts in this "region", wait for
4587c478bd9Sstevel@tonic-gate  * them to complete.
4597c478bd9Sstevel@tonic-gate  */
4607c478bd9Sstevel@tonic-gate static int
4617c478bd9Sstevel@tonic-gate block_mounts(void)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate 	int retval = 0;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	/*
4667c478bd9Sstevel@tonic-gate 	 * Since it may block for a long time, block_mounts() shouldn't be
4677c478bd9Sstevel@tonic-gate 	 * called with zonehash_lock held.
4687c478bd9Sstevel@tonic-gate 	 */
4697c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
4707c478bd9Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4717c478bd9Sstevel@tonic-gate 	while (mounts_in_progress > 0) {
4727c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&mount_cv, &mount_lock) == 0)
4737c478bd9Sstevel@tonic-gate 			goto signaled;
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 	/*
4767c478bd9Sstevel@tonic-gate 	 * A negative value of mounts_in_progress indicates that mounts
4777c478bd9Sstevel@tonic-gate 	 * have been blocked by (-mounts_in_progress) different callers.
4787c478bd9Sstevel@tonic-gate 	 */
4797c478bd9Sstevel@tonic-gate 	mounts_in_progress--;
4807c478bd9Sstevel@tonic-gate 	retval = 1;
4817c478bd9Sstevel@tonic-gate signaled:
4827c478bd9Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4837c478bd9Sstevel@tonic-gate 	return (retval);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  * The VFS layer may progress with new mounts as far as we're concerned.
4887c478bd9Sstevel@tonic-gate  * Allow them to progress if we were the last obstacle.
4897c478bd9Sstevel@tonic-gate  */
4907c478bd9Sstevel@tonic-gate static void
4917c478bd9Sstevel@tonic-gate resume_mounts(void)
4927c478bd9Sstevel@tonic-gate {
4937c478bd9Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4947c478bd9Sstevel@tonic-gate 	if (++mounts_in_progress == 0)
4957c478bd9Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
4967c478bd9Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate /*
5007c478bd9Sstevel@tonic-gate  * The VFS layer is busy with a mount; zones should wait until all
5017c478bd9Sstevel@tonic-gate  * mounts are completed to progress.
5027c478bd9Sstevel@tonic-gate  */
5037c478bd9Sstevel@tonic-gate void
5047c478bd9Sstevel@tonic-gate mount_in_progress(void)
5057c478bd9Sstevel@tonic-gate {
5067c478bd9Sstevel@tonic-gate 	mutex_enter(&mount_lock);
5077c478bd9Sstevel@tonic-gate 	while (mounts_in_progress < 0)
5087c478bd9Sstevel@tonic-gate 		cv_wait(&mount_cv, &mount_lock);
5097c478bd9Sstevel@tonic-gate 	mounts_in_progress++;
5107c478bd9Sstevel@tonic-gate 	mutex_exit(&mount_lock);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate /*
5147c478bd9Sstevel@tonic-gate  * VFS is done with one mount; wake up any waiting block_mounts()
5157c478bd9Sstevel@tonic-gate  * callers if this is the last mount.
5167c478bd9Sstevel@tonic-gate  */
5177c478bd9Sstevel@tonic-gate void
5187c478bd9Sstevel@tonic-gate mount_completed(void)
5197c478bd9Sstevel@tonic-gate {
5207c478bd9Sstevel@tonic-gate 	mutex_enter(&mount_lock);
5217c478bd9Sstevel@tonic-gate 	if (--mounts_in_progress == 0)
5227c478bd9Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
5237c478bd9Sstevel@tonic-gate 	mutex_exit(&mount_lock);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate /*
5277c478bd9Sstevel@tonic-gate  * ZSD routines.
5287c478bd9Sstevel@tonic-gate  *
5297c478bd9Sstevel@tonic-gate  * Zone Specific Data (ZSD) is modeled after Thread Specific Data as
5307c478bd9Sstevel@tonic-gate  * defined by the pthread_key_create() and related interfaces.
5317c478bd9Sstevel@tonic-gate  *
5327c478bd9Sstevel@tonic-gate  * Kernel subsystems may register one or more data items and/or
5337c478bd9Sstevel@tonic-gate  * callbacks to be executed when a zone is created, shutdown, or
5347c478bd9Sstevel@tonic-gate  * destroyed.
5357c478bd9Sstevel@tonic-gate  *
5367c478bd9Sstevel@tonic-gate  * Unlike the thread counterpart, destructor callbacks will be executed
5377c478bd9Sstevel@tonic-gate  * even if the data pointer is NULL and/or there are no constructor
5387c478bd9Sstevel@tonic-gate  * callbacks, so it is the responsibility of such callbacks to check for
5397c478bd9Sstevel@tonic-gate  * NULL data values if necessary.
5407c478bd9Sstevel@tonic-gate  *
5417c478bd9Sstevel@tonic-gate  * The locking strategy and overall picture is as follows:
5427c478bd9Sstevel@tonic-gate  *
5437c478bd9Sstevel@tonic-gate  * When someone calls zone_key_create(), a template ZSD entry is added to the
544bd41d0a8Snordmark  * global list "zsd_registered_keys", protected by zsd_key_lock.  While
545bd41d0a8Snordmark  * holding that lock all the existing zones are marked as
546bd41d0a8Snordmark  * ZSD_CREATE_NEEDED and a copy of the ZSD entry added to the per-zone
547bd41d0a8Snordmark  * zone_zsd list (protected by zone_lock). The global list is updated first
548bd41d0a8Snordmark  * (under zone_key_lock) to make sure that newly created zones use the
549bd41d0a8Snordmark  * most recent list of keys. Then under zonehash_lock we walk the zones
550bd41d0a8Snordmark  * and mark them.  Similar locking is used in zone_key_delete().
5517c478bd9Sstevel@tonic-gate  *
552bd41d0a8Snordmark  * The actual create, shutdown, and destroy callbacks are done without
553bd41d0a8Snordmark  * holding any lock. And zsd_flags are used to ensure that the operations
554bd41d0a8Snordmark  * completed so that when zone_key_create (and zone_create) is done, as well as
555bd41d0a8Snordmark  * zone_key_delete (and zone_destroy) is done, all the necessary callbacks
556bd41d0a8Snordmark  * are completed.
5577c478bd9Sstevel@tonic-gate  *
5587c478bd9Sstevel@tonic-gate  * When new zones are created constructor callbacks for all registered ZSD
559bd41d0a8Snordmark  * entries will be called. That also uses the above two phases of marking
560bd41d0a8Snordmark  * what needs to be done, and then running the callbacks without holding
561bd41d0a8Snordmark  * any locks.
5627c478bd9Sstevel@tonic-gate  *
5637c478bd9Sstevel@tonic-gate  * The framework does not provide any locking around zone_getspecific() and
5647c478bd9Sstevel@tonic-gate  * zone_setspecific() apart from that needed for internal consistency, so
5657c478bd9Sstevel@tonic-gate  * callers interested in atomic "test-and-set" semantics will need to provide
5667c478bd9Sstevel@tonic-gate  * their own locking.
5677c478bd9Sstevel@tonic-gate  */
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate /*
5707c478bd9Sstevel@tonic-gate  * Helper function to find the zsd_entry associated with the key in the
5717c478bd9Sstevel@tonic-gate  * given list.
5727c478bd9Sstevel@tonic-gate  */
5737c478bd9Sstevel@tonic-gate static struct zsd_entry *
5747c478bd9Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsd;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) {
5797c478bd9Sstevel@tonic-gate 		if (zsd->zsd_key == key) {
580bd41d0a8Snordmark 			return (zsd);
581bd41d0a8Snordmark 		}
582bd41d0a8Snordmark 	}
583bd41d0a8Snordmark 	return (NULL);
584bd41d0a8Snordmark }
585bd41d0a8Snordmark 
586bd41d0a8Snordmark /*
587bd41d0a8Snordmark  * Helper function to find the zsd_entry associated with the key in the
588bd41d0a8Snordmark  * given list. Move it to the front of the list.
589bd41d0a8Snordmark  */
590bd41d0a8Snordmark static struct zsd_entry *
591bd41d0a8Snordmark zsd_find_mru(list_t *l, zone_key_t key)
592bd41d0a8Snordmark {
593bd41d0a8Snordmark 	struct zsd_entry *zsd;
594bd41d0a8Snordmark 
595bd41d0a8Snordmark 	for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) {
596bd41d0a8Snordmark 		if (zsd->zsd_key == key) {
5977c478bd9Sstevel@tonic-gate 			/*
5987c478bd9Sstevel@tonic-gate 			 * Move to head of list to keep list in MRU order.
5997c478bd9Sstevel@tonic-gate 			 */
6007c478bd9Sstevel@tonic-gate 			if (zsd != list_head(l)) {
6017c478bd9Sstevel@tonic-gate 				list_remove(l, zsd);
6027c478bd9Sstevel@tonic-gate 				list_insert_head(l, zsd);
6037c478bd9Sstevel@tonic-gate 			}
6047c478bd9Sstevel@tonic-gate 			return (zsd);
6057c478bd9Sstevel@tonic-gate 		}
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 	return (NULL);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
610bd41d0a8Snordmark void
611bd41d0a8Snordmark zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t),
612bd41d0a8Snordmark     void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *))
613bd41d0a8Snordmark {
614bd41d0a8Snordmark 	struct zsd_entry *zsdp;
615bd41d0a8Snordmark 	struct zsd_entry *t;
616bd41d0a8Snordmark 	struct zone *zone;
617bd41d0a8Snordmark 	zone_key_t  key;
618bd41d0a8Snordmark 
619bd41d0a8Snordmark 	zsdp = kmem_zalloc(sizeof (*zsdp), KM_SLEEP);
620bd41d0a8Snordmark 	zsdp->zsd_data = NULL;
621bd41d0a8Snordmark 	zsdp->zsd_create = create;
622bd41d0a8Snordmark 	zsdp->zsd_shutdown = shutdown;
623bd41d0a8Snordmark 	zsdp->zsd_destroy = destroy;
624bd41d0a8Snordmark 
625bd41d0a8Snordmark 	/*
626bd41d0a8Snordmark 	 * Insert in global list of callbacks. Makes future zone creations
627bd41d0a8Snordmark 	 * see it.
628bd41d0a8Snordmark 	 */
629bd41d0a8Snordmark 	mutex_enter(&zsd_key_lock);
630fe16170aSPramod Batni 	key = zsdp->zsd_key = ++zsd_keyval;
631bd41d0a8Snordmark 	ASSERT(zsd_keyval != 0);
632bd41d0a8Snordmark 	list_insert_tail(&zsd_registered_keys, zsdp);
633bd41d0a8Snordmark 	mutex_exit(&zsd_key_lock);
634bd41d0a8Snordmark 
635bd41d0a8Snordmark 	/*
636bd41d0a8Snordmark 	 * Insert for all existing zones and mark them as needing
637bd41d0a8Snordmark 	 * a create callback.
638bd41d0a8Snordmark 	 */
639bd41d0a8Snordmark 	mutex_enter(&zonehash_lock);	/* stop the world */
640bd41d0a8Snordmark 	for (zone = list_head(&zone_active); zone != NULL;
641bd41d0a8Snordmark 	    zone = list_next(&zone_active, zone)) {
642bd41d0a8Snordmark 		zone_status_t status;
643bd41d0a8Snordmark 
644bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
645bd41d0a8Snordmark 
646bd41d0a8Snordmark 		/* Skip zones that are on the way down or not yet up */
647bd41d0a8Snordmark 		status = zone_status_get(zone);
648bd41d0a8Snordmark 		if (status >= ZONE_IS_DOWN ||
649bd41d0a8Snordmark 		    status == ZONE_IS_UNINITIALIZED) {
650bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
651bd41d0a8Snordmark 			continue;
652bd41d0a8Snordmark 		}
653bd41d0a8Snordmark 
654bd41d0a8Snordmark 		t = zsd_find_mru(&zone->zone_zsd, key);
655bd41d0a8Snordmark 		if (t != NULL) {
656bd41d0a8Snordmark 			/*
657bd41d0a8Snordmark 			 * A zsd_configure already inserted it after
658bd41d0a8Snordmark 			 * we dropped zsd_key_lock above.
659bd41d0a8Snordmark 			 */
660bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
661bd41d0a8Snordmark 			continue;
662bd41d0a8Snordmark 		}
663bd41d0a8Snordmark 		t = kmem_zalloc(sizeof (*t), KM_SLEEP);
664bd41d0a8Snordmark 		t->zsd_key = key;
665bd41d0a8Snordmark 		t->zsd_create = create;
666bd41d0a8Snordmark 		t->zsd_shutdown = shutdown;
667bd41d0a8Snordmark 		t->zsd_destroy = destroy;
668bd41d0a8Snordmark 		if (create != NULL) {
669bd41d0a8Snordmark 			t->zsd_flags = ZSD_CREATE_NEEDED;
670bd41d0a8Snordmark 			DTRACE_PROBE2(zsd__create__needed,
671bd41d0a8Snordmark 			    zone_t *, zone, zone_key_t, key);
672bd41d0a8Snordmark 		}
673bd41d0a8Snordmark 		list_insert_tail(&zone->zone_zsd, t);
674bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
675bd41d0a8Snordmark 	}
676bd41d0a8Snordmark 	mutex_exit(&zonehash_lock);
677bd41d0a8Snordmark 
678bd41d0a8Snordmark 	if (create != NULL) {
679bd41d0a8Snordmark 		/* Now call the create callback for this key */
680bd41d0a8Snordmark 		zsd_apply_all_zones(zsd_apply_create, key);
681bd41d0a8Snordmark 	}
682fe16170aSPramod Batni 	/*
683fe16170aSPramod Batni 	 * It is safe for consumers to use the key now, make it
684fe16170aSPramod Batni 	 * globally visible. Specifically zone_getspecific() will
685fe16170aSPramod Batni 	 * always successfully return the zone specific data associated
686fe16170aSPramod Batni 	 * with the key.
687fe16170aSPramod Batni 	 */
688fe16170aSPramod Batni 	*keyp = key;
689fe16170aSPramod Batni 
690bd41d0a8Snordmark }
691bd41d0a8Snordmark 
6927c478bd9Sstevel@tonic-gate /*
6937c478bd9Sstevel@tonic-gate  * Function called when a module is being unloaded, or otherwise wishes
6947c478bd9Sstevel@tonic-gate  * to unregister its ZSD key and callbacks.
695bd41d0a8Snordmark  *
696bd41d0a8Snordmark  * Remove from the global list and determine the functions that need to
697bd41d0a8Snordmark  * be called under a global lock. Then call the functions without
698bd41d0a8Snordmark  * holding any locks. Finally free up the zone_zsd entries. (The apply
699bd41d0a8Snordmark  * functions need to access the zone_zsd entries to find zsd_data etc.)
7007c478bd9Sstevel@tonic-gate  */
7017c478bd9Sstevel@tonic-gate int
7027c478bd9Sstevel@tonic-gate zone_key_delete(zone_key_t key)
7037c478bd9Sstevel@tonic-gate {
7047c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsdp = NULL;
7057c478bd9Sstevel@tonic-gate 	zone_t *zone;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
708bd41d0a8Snordmark 	zsdp = zsd_find_mru(&zsd_registered_keys, key);
709bd41d0a8Snordmark 	if (zsdp == NULL) {
710bd41d0a8Snordmark 		mutex_exit(&zsd_key_lock);
711bd41d0a8Snordmark 		return (-1);
712bd41d0a8Snordmark 	}
7137c478bd9Sstevel@tonic-gate 	list_remove(&zsd_registered_keys, zsdp);
7147c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
7157c478bd9Sstevel@tonic-gate 
716bd41d0a8Snordmark 	mutex_enter(&zonehash_lock);
7177c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
7187c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
7197c478bd9Sstevel@tonic-gate 		struct zsd_entry *del;
7207c478bd9Sstevel@tonic-gate 
721bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
722bd41d0a8Snordmark 		del = zsd_find_mru(&zone->zone_zsd, key);
723bd41d0a8Snordmark 		if (del == NULL) {
724bd41d0a8Snordmark 			/*
725bd41d0a8Snordmark 			 * Somebody else got here first e.g the zone going
726bd41d0a8Snordmark 			 * away.
727bd41d0a8Snordmark 			 */
728bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
729bd41d0a8Snordmark 			continue;
730bd41d0a8Snordmark 		}
7317c478bd9Sstevel@tonic-gate 		ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown);
7327c478bd9Sstevel@tonic-gate 		ASSERT(del->zsd_destroy == zsdp->zsd_destroy);
733bd41d0a8Snordmark 		if (del->zsd_shutdown != NULL &&
734bd41d0a8Snordmark 		    (del->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) {
735bd41d0a8Snordmark 			del->zsd_flags |= ZSD_SHUTDOWN_NEEDED;
736bd41d0a8Snordmark 			DTRACE_PROBE2(zsd__shutdown__needed,
737bd41d0a8Snordmark 			    zone_t *, zone, zone_key_t, key);
7387c478bd9Sstevel@tonic-gate 		}
739bd41d0a8Snordmark 		if (del->zsd_destroy != NULL &&
740bd41d0a8Snordmark 		    (del->zsd_flags & ZSD_DESTROY_ALL) == 0) {
741bd41d0a8Snordmark 			del->zsd_flags |= ZSD_DESTROY_NEEDED;
742bd41d0a8Snordmark 			DTRACE_PROBE2(zsd__destroy__needed,
743bd41d0a8Snordmark 			    zone_t *, zone, zone_key_t, key);
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
7487c478bd9Sstevel@tonic-gate 	kmem_free(zsdp, sizeof (*zsdp));
7497c478bd9Sstevel@tonic-gate 
750bd41d0a8Snordmark 	/* Now call the shutdown and destroy callback for this key */
751bd41d0a8Snordmark 	zsd_apply_all_zones(zsd_apply_shutdown, key);
752bd41d0a8Snordmark 	zsd_apply_all_zones(zsd_apply_destroy, key);
753bd41d0a8Snordmark 
754bd41d0a8Snordmark 	/* Now we can free up the zsdp structures in each zone */
755bd41d0a8Snordmark 	mutex_enter(&zonehash_lock);
7567c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
757bd41d0a8Snordmark 	    zone = list_next(&zone_active, zone)) {
758bd41d0a8Snordmark 		struct zsd_entry *del;
759bd41d0a8Snordmark 
760bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
761bd41d0a8Snordmark 		del = zsd_find(&zone->zone_zsd, key);
762bd41d0a8Snordmark 		if (del != NULL) {
763bd41d0a8Snordmark 			list_remove(&zone->zone_zsd, del);
764bd41d0a8Snordmark 			ASSERT(!(del->zsd_flags & ZSD_ALL_INPROGRESS));
765bd41d0a8Snordmark 			kmem_free(del, sizeof (*del));
766bd41d0a8Snordmark 		}
7677c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
768bd41d0a8Snordmark 	}
7697c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
770bd41d0a8Snordmark 
771bd41d0a8Snordmark 	return (0);
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate /*
7757c478bd9Sstevel@tonic-gate  * ZSD counterpart of pthread_setspecific().
776bd41d0a8Snordmark  *
777bd41d0a8Snordmark  * Since all zsd callbacks, including those with no create function,
778bd41d0a8Snordmark  * have an entry in zone_zsd, if the key is registered it is part of
779bd41d0a8Snordmark  * the zone_zsd list.
780bd41d0a8Snordmark  * Return an error if the key wasn't registerd.
7817c478bd9Sstevel@tonic-gate  */
7827c478bd9Sstevel@tonic-gate int
7837c478bd9Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data)
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
788bd41d0a8Snordmark 	t = zsd_find_mru(&zone->zone_zsd, key);
7897c478bd9Sstevel@tonic-gate 	if (t != NULL) {
7907c478bd9Sstevel@tonic-gate 		/*
7917c478bd9Sstevel@tonic-gate 		 * Replace old value with new
7927c478bd9Sstevel@tonic-gate 		 */
7937c478bd9Sstevel@tonic-gate 		t->zsd_data = (void *)data;
7947c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
7957c478bd9Sstevel@tonic-gate 		return (0);
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
7987c478bd9Sstevel@tonic-gate 	return (-1);
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate /*
8027c478bd9Sstevel@tonic-gate  * ZSD counterpart of pthread_getspecific().
8037c478bd9Sstevel@tonic-gate  */
8047c478bd9Sstevel@tonic-gate void *
8057c478bd9Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone)
8067c478bd9Sstevel@tonic-gate {
8077c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
8087c478bd9Sstevel@tonic-gate 	void *data;
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
811bd41d0a8Snordmark 	t = zsd_find_mru(&zone->zone_zsd, key);
8127c478bd9Sstevel@tonic-gate 	data = (t == NULL ? NULL : t->zsd_data);
8137c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
8147c478bd9Sstevel@tonic-gate 	return (data);
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate /*
8187c478bd9Sstevel@tonic-gate  * Function used to initialize a zone's list of ZSD callbacks and data
8197c478bd9Sstevel@tonic-gate  * when the zone is being created.  The callbacks are initialized from
820bd41d0a8Snordmark  * the template list (zsd_registered_keys). The constructor callback is
821bd41d0a8Snordmark  * executed later (once the zone exists and with locks dropped).
8227c478bd9Sstevel@tonic-gate  */
8237c478bd9Sstevel@tonic-gate static void
8247c478bd9Sstevel@tonic-gate zone_zsd_configure(zone_t *zone)
8257c478bd9Sstevel@tonic-gate {
8267c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsdp;
8277c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
8307c478bd9Sstevel@tonic-gate 	ASSERT(list_head(&zone->zone_zsd) == NULL);
831bd41d0a8Snordmark 	mutex_enter(&zone->zone_lock);
8327c478bd9Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
8337c478bd9Sstevel@tonic-gate 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
8347c478bd9Sstevel@tonic-gate 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
835bd41d0a8Snordmark 		/*
836bd41d0a8Snordmark 		 * Since this zone is ZONE_IS_UNCONFIGURED, zone_key_create
837bd41d0a8Snordmark 		 * should not have added anything to it.
838bd41d0a8Snordmark 		 */
839bd41d0a8Snordmark 		ASSERT(zsd_find(&zone->zone_zsd, zsdp->zsd_key) == NULL);
840bd41d0a8Snordmark 
841bd41d0a8Snordmark 		t = kmem_zalloc(sizeof (*t), KM_SLEEP);
8427c478bd9Sstevel@tonic-gate 		t->zsd_key = zsdp->zsd_key;
8437c478bd9Sstevel@tonic-gate 		t->zsd_create = zsdp->zsd_create;
8447c478bd9Sstevel@tonic-gate 		t->zsd_shutdown = zsdp->zsd_shutdown;
8457c478bd9Sstevel@tonic-gate 		t->zsd_destroy = zsdp->zsd_destroy;
846bd41d0a8Snordmark 		if (zsdp->zsd_create != NULL) {
847bd41d0a8Snordmark 			t->zsd_flags = ZSD_CREATE_NEEDED;
848bd41d0a8Snordmark 			DTRACE_PROBE2(zsd__create__needed,
849bd41d0a8Snordmark 			    zone_t *, zone, zone_key_t, zsdp->zsd_key);
850bd41d0a8Snordmark 		}
8517c478bd9Sstevel@tonic-gate 		list_insert_tail(&zone->zone_zsd, t);
8527c478bd9Sstevel@tonic-gate 	}
8537c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
854bd41d0a8Snordmark 	mutex_exit(&zone->zone_lock);
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY };
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate /*
8607c478bd9Sstevel@tonic-gate  * Helper function to execute shutdown or destructor callbacks.
8617c478bd9Sstevel@tonic-gate  */
8627c478bd9Sstevel@tonic-gate static void
8637c478bd9Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct)
8647c478bd9Sstevel@tonic-gate {
8657c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY);
8687c478bd9Sstevel@tonic-gate 	ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY);
8697c478bd9Sstevel@tonic-gate 	ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN);
8707c478bd9Sstevel@tonic-gate 
871bd41d0a8Snordmark 	/*
872bd41d0a8Snordmark 	 * Run the callback solely based on what is registered for the zone
873bd41d0a8Snordmark 	 * in zone_zsd. The global list can change independently of this
874bd41d0a8Snordmark 	 * as keys are registered and unregistered and we don't register new
875bd41d0a8Snordmark 	 * callbacks for a zone that is in the process of going away.
876bd41d0a8Snordmark 	 */
8777c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
878bd41d0a8Snordmark 	for (t = list_head(&zone->zone_zsd); t != NULL;
879bd41d0a8Snordmark 	    t = list_next(&zone->zone_zsd, t)) {
880bd41d0a8Snordmark 		zone_key_t key = t->zsd_key;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		/* Skip if no callbacks registered */
883bd41d0a8Snordmark 
8847c478bd9Sstevel@tonic-gate 		if (ct == ZSD_SHUTDOWN) {
885bd41d0a8Snordmark 			if (t->zsd_shutdown != NULL &&
886bd41d0a8Snordmark 			    (t->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) {
887bd41d0a8Snordmark 				t->zsd_flags |= ZSD_SHUTDOWN_NEEDED;
888bd41d0a8Snordmark 				DTRACE_PROBE2(zsd__shutdown__needed,
889bd41d0a8Snordmark 				    zone_t *, zone, zone_key_t, key);
8907c478bd9Sstevel@tonic-gate 			}
8917c478bd9Sstevel@tonic-gate 		} else {
892bd41d0a8Snordmark 			if (t->zsd_destroy != NULL &&
893bd41d0a8Snordmark 			    (t->zsd_flags & ZSD_DESTROY_ALL) == 0) {
894bd41d0a8Snordmark 				t->zsd_flags |= ZSD_DESTROY_NEEDED;
895bd41d0a8Snordmark 				DTRACE_PROBE2(zsd__destroy__needed,
896bd41d0a8Snordmark 				    zone_t *, zone, zone_key_t, key);
8977c478bd9Sstevel@tonic-gate 			}
8987c478bd9Sstevel@tonic-gate 		}
8997c478bd9Sstevel@tonic-gate 	}
900bd41d0a8Snordmark 	mutex_exit(&zone->zone_lock);
901bd41d0a8Snordmark 
902bd41d0a8Snordmark 	/* Now call the shutdown and destroy callback for this key */
903bd41d0a8Snordmark 	zsd_apply_all_keys(zsd_apply_shutdown, zone);
904bd41d0a8Snordmark 	zsd_apply_all_keys(zsd_apply_destroy, zone);
905bd41d0a8Snordmark 
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate /*
9097c478bd9Sstevel@tonic-gate  * Called when the zone is going away; free ZSD-related memory, and
9107c478bd9Sstevel@tonic-gate  * destroy the zone_zsd list.
9117c478bd9Sstevel@tonic-gate  */
9127c478bd9Sstevel@tonic-gate static void
9137c478bd9Sstevel@tonic-gate zone_free_zsd(zone_t *zone)
9147c478bd9Sstevel@tonic-gate {
9157c478bd9Sstevel@tonic-gate 	struct zsd_entry *t, *next;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	/*
9187c478bd9Sstevel@tonic-gate 	 * Free all the zsd_entry's we had on this zone.
9197c478bd9Sstevel@tonic-gate 	 */
920bd41d0a8Snordmark 	mutex_enter(&zone->zone_lock);
9217c478bd9Sstevel@tonic-gate 	for (t = list_head(&zone->zone_zsd); t != NULL; t = next) {
9227c478bd9Sstevel@tonic-gate 		next = list_next(&zone->zone_zsd, t);
9237c478bd9Sstevel@tonic-gate 		list_remove(&zone->zone_zsd, t);
924bd41d0a8Snordmark 		ASSERT(!(t->zsd_flags & ZSD_ALL_INPROGRESS));
9257c478bd9Sstevel@tonic-gate 		kmem_free(t, sizeof (*t));
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 	list_destroy(&zone->zone_zsd);
928bd41d0a8Snordmark 	mutex_exit(&zone->zone_lock);
929bd41d0a8Snordmark 
930bd41d0a8Snordmark }
931bd41d0a8Snordmark 
932bd41d0a8Snordmark /*
933bd41d0a8Snordmark  * Apply a function to all zones for particular key value.
934bd41d0a8Snordmark  *
935bd41d0a8Snordmark  * The applyfn has to drop zonehash_lock if it does some work, and
936bd41d0a8Snordmark  * then reacquire it before it returns.
937bd41d0a8Snordmark  * When the lock is dropped we don't follow list_next even
938bd41d0a8Snordmark  * if it is possible to do so without any hazards. This is
939bd41d0a8Snordmark  * because we want the design to allow for the list of zones
940bd41d0a8Snordmark  * to change in any arbitrary way during the time the
941bd41d0a8Snordmark  * lock was dropped.
942bd41d0a8Snordmark  *
943bd41d0a8Snordmark  * It is safe to restart the loop at list_head since the applyfn
944bd41d0a8Snordmark  * changes the zsd_flags as it does work, so a subsequent
945bd41d0a8Snordmark  * pass through will have no effect in applyfn, hence the loop will terminate
946bd41d0a8Snordmark  * in at worst O(N^2).
947bd41d0a8Snordmark  */
948bd41d0a8Snordmark static void
949bd41d0a8Snordmark zsd_apply_all_zones(zsd_applyfn_t *applyfn, zone_key_t key)
950bd41d0a8Snordmark {
951bd41d0a8Snordmark 	zone_t *zone;
952bd41d0a8Snordmark 
953bd41d0a8Snordmark 	mutex_enter(&zonehash_lock);
954bd41d0a8Snordmark 	zone = list_head(&zone_active);
955bd41d0a8Snordmark 	while (zone != NULL) {
956bd41d0a8Snordmark 		if ((applyfn)(&zonehash_lock, B_FALSE, zone, key)) {
957bd41d0a8Snordmark 			/* Lock dropped - restart at head */
958bd41d0a8Snordmark 			zone = list_head(&zone_active);
959bd41d0a8Snordmark 		} else {
960bd41d0a8Snordmark 			zone = list_next(&zone_active, zone);
961bd41d0a8Snordmark 		}
962bd41d0a8Snordmark 	}
963bd41d0a8Snordmark 	mutex_exit(&zonehash_lock);
964bd41d0a8Snordmark }
965bd41d0a8Snordmark 
966bd41d0a8Snordmark /*
967bd41d0a8Snordmark  * Apply a function to all keys for a particular zone.
968bd41d0a8Snordmark  *
969bd41d0a8Snordmark  * The applyfn has to drop zonehash_lock if it does some work, and
970bd41d0a8Snordmark  * then reacquire it before it returns.
971bd41d0a8Snordmark  * When the lock is dropped we don't follow list_next even
972bd41d0a8Snordmark  * if it is possible to do so without any hazards. This is
973bd41d0a8Snordmark  * because we want the design to allow for the list of zsd callbacks
974bd41d0a8Snordmark  * to change in any arbitrary way during the time the
975bd41d0a8Snordmark  * lock was dropped.
976bd41d0a8Snordmark  *
977bd41d0a8Snordmark  * It is safe to restart the loop at list_head since the applyfn
978bd41d0a8Snordmark  * changes the zsd_flags as it does work, so a subsequent
979bd41d0a8Snordmark  * pass through will have no effect in applyfn, hence the loop will terminate
980bd41d0a8Snordmark  * in at worst O(N^2).
981bd41d0a8Snordmark  */
982bd41d0a8Snordmark static void
983bd41d0a8Snordmark zsd_apply_all_keys(zsd_applyfn_t *applyfn, zone_t *zone)
984bd41d0a8Snordmark {
985bd41d0a8Snordmark 	struct zsd_entry *t;
986bd41d0a8Snordmark 
987bd41d0a8Snordmark 	mutex_enter(&zone->zone_lock);
988bd41d0a8Snordmark 	t = list_head(&zone->zone_zsd);
989bd41d0a8Snordmark 	while (t != NULL) {
990bd41d0a8Snordmark 		if ((applyfn)(NULL, B_TRUE, zone, t->zsd_key)) {
991bd41d0a8Snordmark 			/* Lock dropped - restart at head */
992bd41d0a8Snordmark 			t = list_head(&zone->zone_zsd);
993bd41d0a8Snordmark 		} else {
994bd41d0a8Snordmark 			t = list_next(&zone->zone_zsd, t);
995bd41d0a8Snordmark 		}
996bd41d0a8Snordmark 	}
997bd41d0a8Snordmark 	mutex_exit(&zone->zone_lock);
998bd41d0a8Snordmark }
999bd41d0a8Snordmark 
1000bd41d0a8Snordmark /*
1001bd41d0a8Snordmark  * Call the create function for the zone and key if CREATE_NEEDED
1002bd41d0a8Snordmark  * is set.
1003bd41d0a8Snordmark  * If some other thread gets here first and sets CREATE_INPROGRESS, then
1004bd41d0a8Snordmark  * we wait for that thread to complete so that we can ensure that
1005bd41d0a8Snordmark  * all the callbacks are done when we've looped over all zones/keys.
1006bd41d0a8Snordmark  *
1007bd41d0a8Snordmark  * When we call the create function, we drop the global held by the
1008bd41d0a8Snordmark  * caller, and return true to tell the caller it needs to re-evalute the
1009bd41d0a8Snordmark  * state.
1010bd41d0a8Snordmark  * If the caller holds zone_lock then zone_lock_held is set, and zone_lock
1011bd41d0a8Snordmark  * remains held on exit.
1012bd41d0a8Snordmark  */
1013bd41d0a8Snordmark static boolean_t
1014bd41d0a8Snordmark zsd_apply_create(kmutex_t *lockp, boolean_t zone_lock_held,
1015bd41d0a8Snordmark     zone_t *zone, zone_key_t key)
1016bd41d0a8Snordmark {
1017bd41d0a8Snordmark 	void *result;
1018bd41d0a8Snordmark 	struct zsd_entry *t;
1019bd41d0a8Snordmark 	boolean_t dropped;
1020bd41d0a8Snordmark 
1021bd41d0a8Snordmark 	if (lockp != NULL) {
1022bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(lockp));
1023bd41d0a8Snordmark 	}
1024bd41d0a8Snordmark 	if (zone_lock_held) {
1025bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(&zone->zone_lock));
1026bd41d0a8Snordmark 	} else {
1027bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1028bd41d0a8Snordmark 	}
1029bd41d0a8Snordmark 
1030bd41d0a8Snordmark 	t = zsd_find(&zone->zone_zsd, key);
1031bd41d0a8Snordmark 	if (t == NULL) {
1032bd41d0a8Snordmark 		/*
1033bd41d0a8Snordmark 		 * Somebody else got here first e.g the zone going
1034bd41d0a8Snordmark 		 * away.
1035bd41d0a8Snordmark 		 */
1036bd41d0a8Snordmark 		if (!zone_lock_held)
1037bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
1038bd41d0a8Snordmark 		return (B_FALSE);
1039bd41d0a8Snordmark 	}
1040bd41d0a8Snordmark 	dropped = B_FALSE;
1041bd41d0a8Snordmark 	if (zsd_wait_for_inprogress(zone, t, lockp))
1042bd41d0a8Snordmark 		dropped = B_TRUE;
1043bd41d0a8Snordmark 
1044bd41d0a8Snordmark 	if (t->zsd_flags & ZSD_CREATE_NEEDED) {
1045bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_CREATE_NEEDED;
1046bd41d0a8Snordmark 		t->zsd_flags |= ZSD_CREATE_INPROGRESS;
1047bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__create__inprogress,
1048bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1049bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1050bd41d0a8Snordmark 		if (lockp != NULL)
1051bd41d0a8Snordmark 			mutex_exit(lockp);
1052bd41d0a8Snordmark 
1053bd41d0a8Snordmark 		dropped = B_TRUE;
1054bd41d0a8Snordmark 		ASSERT(t->zsd_create != NULL);
1055bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__create__start,
1056bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1057bd41d0a8Snordmark 
1058bd41d0a8Snordmark 		result = (*t->zsd_create)(zone->zone_id);
1059bd41d0a8Snordmark 
1060bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__create__end,
1061bd41d0a8Snordmark 		    zone_t *, zone, voidn *, result);
1062bd41d0a8Snordmark 
1063bd41d0a8Snordmark 		ASSERT(result != NULL);
1064bd41d0a8Snordmark 		if (lockp != NULL)
1065bd41d0a8Snordmark 			mutex_enter(lockp);
1066bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1067bd41d0a8Snordmark 		t->zsd_data = result;
1068bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_CREATE_INPROGRESS;
1069bd41d0a8Snordmark 		t->zsd_flags |= ZSD_CREATE_COMPLETED;
1070bd41d0a8Snordmark 		cv_broadcast(&t->zsd_cv);
1071bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__create__completed,
1072bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1073bd41d0a8Snordmark 	}
1074bd41d0a8Snordmark 	if (!zone_lock_held)
1075bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1076bd41d0a8Snordmark 	return (dropped);
1077bd41d0a8Snordmark }
1078bd41d0a8Snordmark 
1079bd41d0a8Snordmark /*
1080bd41d0a8Snordmark  * Call the shutdown function for the zone and key if SHUTDOWN_NEEDED
1081bd41d0a8Snordmark  * is set.
1082bd41d0a8Snordmark  * If some other thread gets here first and sets *_INPROGRESS, then
1083bd41d0a8Snordmark  * we wait for that thread to complete so that we can ensure that
1084bd41d0a8Snordmark  * all the callbacks are done when we've looped over all zones/keys.
1085bd41d0a8Snordmark  *
1086bd41d0a8Snordmark  * When we call the shutdown function, we drop the global held by the
1087bd41d0a8Snordmark  * caller, and return true to tell the caller it needs to re-evalute the
1088bd41d0a8Snordmark  * state.
1089bd41d0a8Snordmark  * If the caller holds zone_lock then zone_lock_held is set, and zone_lock
1090bd41d0a8Snordmark  * remains held on exit.
1091bd41d0a8Snordmark  */
1092bd41d0a8Snordmark static boolean_t
1093bd41d0a8Snordmark zsd_apply_shutdown(kmutex_t *lockp, boolean_t zone_lock_held,
1094bd41d0a8Snordmark     zone_t *zone, zone_key_t key)
1095bd41d0a8Snordmark {
1096bd41d0a8Snordmark 	struct zsd_entry *t;
1097bd41d0a8Snordmark 	void *data;
1098bd41d0a8Snordmark 	boolean_t dropped;
1099bd41d0a8Snordmark 
1100bd41d0a8Snordmark 	if (lockp != NULL) {
1101bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(lockp));
1102bd41d0a8Snordmark 	}
1103bd41d0a8Snordmark 	if (zone_lock_held) {
1104bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(&zone->zone_lock));
1105bd41d0a8Snordmark 	} else {
1106bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1107bd41d0a8Snordmark 	}
1108bd41d0a8Snordmark 
1109bd41d0a8Snordmark 	t = zsd_find(&zone->zone_zsd, key);
1110bd41d0a8Snordmark 	if (t == NULL) {
1111bd41d0a8Snordmark 		/*
1112bd41d0a8Snordmark 		 * Somebody else got here first e.g the zone going
1113bd41d0a8Snordmark 		 * away.
1114bd41d0a8Snordmark 		 */
1115bd41d0a8Snordmark 		if (!zone_lock_held)
1116bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
1117bd41d0a8Snordmark 		return (B_FALSE);
1118bd41d0a8Snordmark 	}
1119bd41d0a8Snordmark 	dropped = B_FALSE;
1120bd41d0a8Snordmark 	if (zsd_wait_for_creator(zone, t, lockp))
1121bd41d0a8Snordmark 		dropped = B_TRUE;
1122bd41d0a8Snordmark 
1123bd41d0a8Snordmark 	if (zsd_wait_for_inprogress(zone, t, lockp))
1124bd41d0a8Snordmark 		dropped = B_TRUE;
1125bd41d0a8Snordmark 
1126bd41d0a8Snordmark 	if (t->zsd_flags & ZSD_SHUTDOWN_NEEDED) {
1127bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_SHUTDOWN_NEEDED;
1128bd41d0a8Snordmark 		t->zsd_flags |= ZSD_SHUTDOWN_INPROGRESS;
1129bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__shutdown__inprogress,
1130bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1131bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1132bd41d0a8Snordmark 		if (lockp != NULL)
1133bd41d0a8Snordmark 			mutex_exit(lockp);
1134bd41d0a8Snordmark 		dropped = B_TRUE;
1135bd41d0a8Snordmark 
1136bd41d0a8Snordmark 		ASSERT(t->zsd_shutdown != NULL);
1137bd41d0a8Snordmark 		data = t->zsd_data;
1138bd41d0a8Snordmark 
1139bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__shutdown__start,
1140bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1141bd41d0a8Snordmark 
1142bd41d0a8Snordmark 		(t->zsd_shutdown)(zone->zone_id, data);
1143bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__shutdown__end,
1144bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1145bd41d0a8Snordmark 
1146bd41d0a8Snordmark 		if (lockp != NULL)
1147bd41d0a8Snordmark 			mutex_enter(lockp);
1148bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1149bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_SHUTDOWN_INPROGRESS;
1150bd41d0a8Snordmark 		t->zsd_flags |= ZSD_SHUTDOWN_COMPLETED;
1151bd41d0a8Snordmark 		cv_broadcast(&t->zsd_cv);
1152bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__shutdown__completed,
1153bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1154bd41d0a8Snordmark 	}
1155bd41d0a8Snordmark 	if (!zone_lock_held)
1156bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1157bd41d0a8Snordmark 	return (dropped);
1158bd41d0a8Snordmark }
1159bd41d0a8Snordmark 
1160bd41d0a8Snordmark /*
1161bd41d0a8Snordmark  * Call the destroy function for the zone and key if DESTROY_NEEDED
1162bd41d0a8Snordmark  * is set.
1163bd41d0a8Snordmark  * If some other thread gets here first and sets *_INPROGRESS, then
1164bd41d0a8Snordmark  * we wait for that thread to complete so that we can ensure that
1165bd41d0a8Snordmark  * all the callbacks are done when we've looped over all zones/keys.
1166bd41d0a8Snordmark  *
1167bd41d0a8Snordmark  * When we call the destroy function, we drop the global held by the
1168bd41d0a8Snordmark  * caller, and return true to tell the caller it needs to re-evalute the
1169bd41d0a8Snordmark  * state.
1170bd41d0a8Snordmark  * If the caller holds zone_lock then zone_lock_held is set, and zone_lock
1171bd41d0a8Snordmark  * remains held on exit.
1172bd41d0a8Snordmark  */
1173bd41d0a8Snordmark static boolean_t
1174bd41d0a8Snordmark zsd_apply_destroy(kmutex_t *lockp, boolean_t zone_lock_held,
1175bd41d0a8Snordmark     zone_t *zone, zone_key_t key)
1176bd41d0a8Snordmark {
1177bd41d0a8Snordmark 	struct zsd_entry *t;
1178bd41d0a8Snordmark 	void *data;
1179bd41d0a8Snordmark 	boolean_t dropped;
1180bd41d0a8Snordmark 
1181bd41d0a8Snordmark 	if (lockp != NULL) {
1182bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(lockp));
1183bd41d0a8Snordmark 	}
1184bd41d0a8Snordmark 	if (zone_lock_held) {
1185bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(&zone->zone_lock));
1186bd41d0a8Snordmark 	} else {
1187bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1188bd41d0a8Snordmark 	}
1189bd41d0a8Snordmark 
1190bd41d0a8Snordmark 	t = zsd_find(&zone->zone_zsd, key);
1191bd41d0a8Snordmark 	if (t == NULL) {
1192bd41d0a8Snordmark 		/*
1193bd41d0a8Snordmark 		 * Somebody else got here first e.g the zone going
1194bd41d0a8Snordmark 		 * away.
1195bd41d0a8Snordmark 		 */
1196bd41d0a8Snordmark 		if (!zone_lock_held)
1197bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
1198bd41d0a8Snordmark 		return (B_FALSE);
1199bd41d0a8Snordmark 	}
1200bd41d0a8Snordmark 	dropped = B_FALSE;
1201bd41d0a8Snordmark 	if (zsd_wait_for_creator(zone, t, lockp))
1202bd41d0a8Snordmark 		dropped = B_TRUE;
1203bd41d0a8Snordmark 
1204bd41d0a8Snordmark 	if (zsd_wait_for_inprogress(zone, t, lockp))
1205bd41d0a8Snordmark 		dropped = B_TRUE;
1206bd41d0a8Snordmark 
1207bd41d0a8Snordmark 	if (t->zsd_flags & ZSD_DESTROY_NEEDED) {
1208bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_DESTROY_NEEDED;
1209bd41d0a8Snordmark 		t->zsd_flags |= ZSD_DESTROY_INPROGRESS;
1210bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__destroy__inprogress,
1211bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1212bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1213bd41d0a8Snordmark 		if (lockp != NULL)
1214bd41d0a8Snordmark 			mutex_exit(lockp);
1215bd41d0a8Snordmark 		dropped = B_TRUE;
1216bd41d0a8Snordmark 
1217bd41d0a8Snordmark 		ASSERT(t->zsd_destroy != NULL);
1218bd41d0a8Snordmark 		data = t->zsd_data;
1219bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__destroy__start,
1220bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1221bd41d0a8Snordmark 
1222bd41d0a8Snordmark 		(t->zsd_destroy)(zone->zone_id, data);
1223bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__destroy__end,
1224bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1225bd41d0a8Snordmark 
1226bd41d0a8Snordmark 		if (lockp != NULL)
1227bd41d0a8Snordmark 			mutex_enter(lockp);
1228bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1229bd41d0a8Snordmark 		t->zsd_data = NULL;
1230bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_DESTROY_INPROGRESS;
1231bd41d0a8Snordmark 		t->zsd_flags |= ZSD_DESTROY_COMPLETED;
1232bd41d0a8Snordmark 		cv_broadcast(&t->zsd_cv);
1233bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__destroy__completed,
1234bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1235bd41d0a8Snordmark 	}
1236bd41d0a8Snordmark 	if (!zone_lock_held)
1237bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1238bd41d0a8Snordmark 	return (dropped);
1239bd41d0a8Snordmark }
1240bd41d0a8Snordmark 
1241bd41d0a8Snordmark /*
1242bd41d0a8Snordmark  * Wait for any CREATE_NEEDED flag to be cleared.
1243bd41d0a8Snordmark  * Returns true if lockp was temporarily dropped while waiting.
1244bd41d0a8Snordmark  */
1245bd41d0a8Snordmark static boolean_t
1246bd41d0a8Snordmark zsd_wait_for_creator(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp)
1247bd41d0a8Snordmark {
1248bd41d0a8Snordmark 	boolean_t dropped = B_FALSE;
1249bd41d0a8Snordmark 
1250bd41d0a8Snordmark 	while (t->zsd_flags & ZSD_CREATE_NEEDED) {
1251bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__wait__for__creator,
1252bd41d0a8Snordmark 		    zone_t *, zone, struct zsd_entry *, t);
1253bd41d0a8Snordmark 		if (lockp != NULL) {
1254bd41d0a8Snordmark 			dropped = B_TRUE;
1255bd41d0a8Snordmark 			mutex_exit(lockp);
1256bd41d0a8Snordmark 		}
1257bd41d0a8Snordmark 		cv_wait(&t->zsd_cv, &zone->zone_lock);
1258bd41d0a8Snordmark 		if (lockp != NULL) {
1259bd41d0a8Snordmark 			/* First drop zone_lock to preserve order */
1260bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
1261bd41d0a8Snordmark 			mutex_enter(lockp);
1262bd41d0a8Snordmark 			mutex_enter(&zone->zone_lock);
1263bd41d0a8Snordmark 		}
1264bd41d0a8Snordmark 	}
1265bd41d0a8Snordmark 	return (dropped);
1266bd41d0a8Snordmark }
1267bd41d0a8Snordmark 
1268bd41d0a8Snordmark /*
1269bd41d0a8Snordmark  * Wait for any INPROGRESS flag to be cleared.
1270bd41d0a8Snordmark  * Returns true if lockp was temporarily dropped while waiting.
1271bd41d0a8Snordmark  */
1272bd41d0a8Snordmark static boolean_t
1273bd41d0a8Snordmark zsd_wait_for_inprogress(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp)
1274bd41d0a8Snordmark {
1275bd41d0a8Snordmark 	boolean_t dropped = B_FALSE;
1276bd41d0a8Snordmark 
1277bd41d0a8Snordmark 	while (t->zsd_flags & ZSD_ALL_INPROGRESS) {
1278bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__wait__for__inprogress,
1279bd41d0a8Snordmark 		    zone_t *, zone, struct zsd_entry *, t);
1280bd41d0a8Snordmark 		if (lockp != NULL) {
1281bd41d0a8Snordmark 			dropped = B_TRUE;
1282bd41d0a8Snordmark 			mutex_exit(lockp);
1283bd41d0a8Snordmark 		}
1284bd41d0a8Snordmark 		cv_wait(&t->zsd_cv, &zone->zone_lock);
1285bd41d0a8Snordmark 		if (lockp != NULL) {
1286bd41d0a8Snordmark 			/* First drop zone_lock to preserve order */
1287bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
1288bd41d0a8Snordmark 			mutex_enter(lockp);
1289bd41d0a8Snordmark 			mutex_enter(&zone->zone_lock);
1290bd41d0a8Snordmark 		}
1291bd41d0a8Snordmark 	}
1292bd41d0a8Snordmark 	return (dropped);
12937c478bd9Sstevel@tonic-gate }
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate /*
1296fa9e4066Sahrens  * Frees memory associated with the zone dataset list.
1297fa9e4066Sahrens  */
1298fa9e4066Sahrens static void
1299fa9e4066Sahrens zone_free_datasets(zone_t *zone)
1300fa9e4066Sahrens {
1301fa9e4066Sahrens 	zone_dataset_t *t, *next;
1302fa9e4066Sahrens 
1303fa9e4066Sahrens 	for (t = list_head(&zone->zone_datasets); t != NULL; t = next) {
1304fa9e4066Sahrens 		next = list_next(&zone->zone_datasets, t);
1305fa9e4066Sahrens 		list_remove(&zone->zone_datasets, t);
1306fa9e4066Sahrens 		kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1);
1307fa9e4066Sahrens 		kmem_free(t, sizeof (*t));
1308fa9e4066Sahrens 	}
1309fa9e4066Sahrens 	list_destroy(&zone->zone_datasets);
1310fa9e4066Sahrens }
1311fa9e4066Sahrens 
1312fa9e4066Sahrens /*
13137c478bd9Sstevel@tonic-gate  * zone.cpu-shares resource control support.
13147c478bd9Sstevel@tonic-gate  */
13157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13167c478bd9Sstevel@tonic-gate static rctl_qty_t
13177c478bd9Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p)
13187c478bd9Sstevel@tonic-gate {
13197c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
13207c478bd9Sstevel@tonic-gate 	return (p->p_zone->zone_shares);
13217c478bd9Sstevel@tonic-gate }
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13247c478bd9Sstevel@tonic-gate static int
13257c478bd9Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
13267c478bd9Sstevel@tonic-gate     rctl_qty_t nv)
13277c478bd9Sstevel@tonic-gate {
13287c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
13297c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
13307c478bd9Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
13317c478bd9Sstevel@tonic-gate 		return (0);
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	e->rcep_p.zone->zone_shares = nv;
13347c478bd9Sstevel@tonic-gate 	return (0);
13357c478bd9Sstevel@tonic-gate }
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = {
13387c478bd9Sstevel@tonic-gate 	rcop_no_action,
13397c478bd9Sstevel@tonic-gate 	zone_cpu_shares_usage,
13407c478bd9Sstevel@tonic-gate 	zone_cpu_shares_set,
13417c478bd9Sstevel@tonic-gate 	rcop_no_test
13427c478bd9Sstevel@tonic-gate };
13437c478bd9Sstevel@tonic-gate 
1344c97ad5cdSakolb /*
1345c97ad5cdSakolb  * zone.cpu-cap resource control support.
1346c97ad5cdSakolb  */
1347c97ad5cdSakolb /*ARGSUSED*/
1348c97ad5cdSakolb static rctl_qty_t
1349c97ad5cdSakolb zone_cpu_cap_get(rctl_t *rctl, struct proc *p)
1350c97ad5cdSakolb {
1351c97ad5cdSakolb 	ASSERT(MUTEX_HELD(&p->p_lock));
1352c97ad5cdSakolb 	return (cpucaps_zone_get(p->p_zone));
1353c97ad5cdSakolb }
1354c97ad5cdSakolb 
1355c97ad5cdSakolb /*ARGSUSED*/
1356c97ad5cdSakolb static int
1357c97ad5cdSakolb zone_cpu_cap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
1358c97ad5cdSakolb     rctl_qty_t nv)
1359c97ad5cdSakolb {
1360c97ad5cdSakolb 	zone_t *zone = e->rcep_p.zone;
1361c97ad5cdSakolb 
1362c97ad5cdSakolb 	ASSERT(MUTEX_HELD(&p->p_lock));
1363c97ad5cdSakolb 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1364c97ad5cdSakolb 
1365c97ad5cdSakolb 	if (zone == NULL)
1366c97ad5cdSakolb 		return (0);
1367c97ad5cdSakolb 
1368c97ad5cdSakolb 	/*
1369c97ad5cdSakolb 	 * set cap to the new value.
1370c97ad5cdSakolb 	 */
1371c97ad5cdSakolb 	return (cpucaps_zone_set(zone, nv));
1372c97ad5cdSakolb }
1373c97ad5cdSakolb 
1374c97ad5cdSakolb static rctl_ops_t zone_cpu_cap_ops = {
1375c97ad5cdSakolb 	rcop_no_action,
1376c97ad5cdSakolb 	zone_cpu_cap_get,
1377c97ad5cdSakolb 	zone_cpu_cap_set,
1378c97ad5cdSakolb 	rcop_no_test
1379c97ad5cdSakolb };
1380c97ad5cdSakolb 
13817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13827c478bd9Sstevel@tonic-gate static rctl_qty_t
13837c478bd9Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p)
13847c478bd9Sstevel@tonic-gate {
13857c478bd9Sstevel@tonic-gate 	rctl_qty_t nlwps;
13867c478bd9Sstevel@tonic-gate 	zone_t *zone = p->p_zone;
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
13917c478bd9Sstevel@tonic-gate 	nlwps = zone->zone_nlwps;
13927c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	return (nlwps);
13957c478bd9Sstevel@tonic-gate }
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13987c478bd9Sstevel@tonic-gate static int
13997c478bd9Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
14007c478bd9Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
14017c478bd9Sstevel@tonic-gate {
14027c478bd9Sstevel@tonic-gate 	rctl_qty_t nlwps;
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
14057c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
14067c478bd9Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
14077c478bd9Sstevel@tonic-gate 		return (0);
14087c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock)));
14097c478bd9Sstevel@tonic-gate 	nlwps = e->rcep_p.zone->zone_nlwps;
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	if (nlwps + incr > rcntl->rcv_value)
14127c478bd9Sstevel@tonic-gate 		return (1);
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	return (0);
14157c478bd9Sstevel@tonic-gate }
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14187c478bd9Sstevel@tonic-gate static int
1419c6939658Ssl108498 zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv)
1420c6939658Ssl108498 {
14217c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
14227c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
14237c478bd9Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
14247c478bd9Sstevel@tonic-gate 		return (0);
14257c478bd9Sstevel@tonic-gate 	e->rcep_p.zone->zone_nlwps_ctl = nv;
14267c478bd9Sstevel@tonic-gate 	return (0);
14277c478bd9Sstevel@tonic-gate }
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = {
14307c478bd9Sstevel@tonic-gate 	rcop_no_action,
14317c478bd9Sstevel@tonic-gate 	zone_lwps_usage,
14327c478bd9Sstevel@tonic-gate 	zone_lwps_set,
14337c478bd9Sstevel@tonic-gate 	zone_lwps_test,
14347c478bd9Sstevel@tonic-gate };
14357c478bd9Sstevel@tonic-gate 
1436824c205fSml93401 /*ARGSUSED*/
1437ff19e029SMenno Lageman static rctl_qty_t
1438ff19e029SMenno Lageman zone_procs_usage(rctl_t *r, proc_t *p)
1439ff19e029SMenno Lageman {
1440ff19e029SMenno Lageman 	rctl_qty_t nprocs;
1441ff19e029SMenno Lageman 	zone_t *zone = p->p_zone;
1442ff19e029SMenno Lageman 
1443ff19e029SMenno Lageman 	ASSERT(MUTEX_HELD(&p->p_lock));
1444ff19e029SMenno Lageman 
1445ff19e029SMenno Lageman 	mutex_enter(&zone->zone_nlwps_lock);
1446ff19e029SMenno Lageman 	nprocs = zone->zone_nprocs;
1447ff19e029SMenno Lageman 	mutex_exit(&zone->zone_nlwps_lock);
1448ff19e029SMenno Lageman 
1449ff19e029SMenno Lageman 	return (nprocs);
1450ff19e029SMenno Lageman }
1451ff19e029SMenno Lageman 
1452ff19e029SMenno Lageman /*ARGSUSED*/
1453ff19e029SMenno Lageman static int
1454ff19e029SMenno Lageman zone_procs_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
1455ff19e029SMenno Lageman     rctl_qty_t incr, uint_t flags)
1456ff19e029SMenno Lageman {
1457ff19e029SMenno Lageman 	rctl_qty_t nprocs;
1458ff19e029SMenno Lageman 
1459ff19e029SMenno Lageman 	ASSERT(MUTEX_HELD(&p->p_lock));
1460ff19e029SMenno Lageman 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1461ff19e029SMenno Lageman 	if (e->rcep_p.zone == NULL)
1462ff19e029SMenno Lageman 		return (0);
1463ff19e029SMenno Lageman 	ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock)));
1464ff19e029SMenno Lageman 	nprocs = e->rcep_p.zone->zone_nprocs;
1465ff19e029SMenno Lageman 
1466ff19e029SMenno Lageman 	if (nprocs + incr > rcntl->rcv_value)
1467ff19e029SMenno Lageman 		return (1);
1468ff19e029SMenno Lageman 
1469ff19e029SMenno Lageman 	return (0);
1470ff19e029SMenno Lageman }
1471ff19e029SMenno Lageman 
1472ff19e029SMenno Lageman /*ARGSUSED*/
1473ff19e029SMenno Lageman static int
1474ff19e029SMenno Lageman zone_procs_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv)
1475ff19e029SMenno Lageman {
1476ff19e029SMenno Lageman 	ASSERT(MUTEX_HELD(&p->p_lock));
1477ff19e029SMenno Lageman 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1478ff19e029SMenno Lageman 	if (e->rcep_p.zone == NULL)
1479ff19e029SMenno Lageman 		return (0);
1480ff19e029SMenno Lageman 	e->rcep_p.zone->zone_nprocs_ctl = nv;
1481ff19e029SMenno Lageman 	return (0);
1482ff19e029SMenno Lageman }
1483ff19e029SMenno Lageman 
1484ff19e029SMenno Lageman static rctl_ops_t zone_procs_ops = {
1485ff19e029SMenno Lageman 	rcop_no_action,
1486ff19e029SMenno Lageman 	zone_procs_usage,
1487ff19e029SMenno Lageman 	zone_procs_set,
1488ff19e029SMenno Lageman 	zone_procs_test,
1489ff19e029SMenno Lageman };
1490ff19e029SMenno Lageman 
1491ff19e029SMenno Lageman /*ARGSUSED*/
1492824c205fSml93401 static int
1493824c205fSml93401 zone_shmmax_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
1494824c205fSml93401     rctl_qty_t incr, uint_t flags)
1495824c205fSml93401 {
1496824c205fSml93401 	rctl_qty_t v;
1497824c205fSml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
1498824c205fSml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1499824c205fSml93401 	v = e->rcep_p.zone->zone_shmmax + incr;
1500824c205fSml93401 	if (v > rval->rcv_value)
1501824c205fSml93401 		return (1);
1502824c205fSml93401 	return (0);
1503824c205fSml93401 }
1504824c205fSml93401 
1505824c205fSml93401 static rctl_ops_t zone_shmmax_ops = {
1506824c205fSml93401 	rcop_no_action,
1507824c205fSml93401 	rcop_no_usage,
1508824c205fSml93401 	rcop_no_set,
1509824c205fSml93401 	zone_shmmax_test
1510824c205fSml93401 };
1511824c205fSml93401 
1512824c205fSml93401 /*ARGSUSED*/
1513824c205fSml93401 static int
1514824c205fSml93401 zone_shmmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
1515824c205fSml93401     rctl_qty_t incr, uint_t flags)
1516824c205fSml93401 {
1517824c205fSml93401 	rctl_qty_t v;
1518824c205fSml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
1519824c205fSml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1520824c205fSml93401 	v = e->rcep_p.zone->zone_ipc.ipcq_shmmni + incr;
1521824c205fSml93401 	if (v > rval->rcv_value)
1522824c205fSml93401 		return (1);
1523824c205fSml93401 	return (0);
1524824c205fSml93401 }
1525824c205fSml93401 
1526824c205fSml93401 static rctl_ops_t zone_shmmni_ops = {
1527824c205fSml93401 	rcop_no_action,
1528824c205fSml93401 	rcop_no_usage,
1529824c205fSml93401 	rcop_no_set,
1530824c205fSml93401 	zone_shmmni_test
1531824c205fSml93401 };
1532824c205fSml93401 
1533824c205fSml93401 /*ARGSUSED*/
1534824c205fSml93401 static int
1535824c205fSml93401 zone_semmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
1536824c205fSml93401     rctl_qty_t incr, uint_t flags)
1537824c205fSml93401 {
1538824c205fSml93401 	rctl_qty_t v;
1539824c205fSml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
1540824c205fSml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1541824c205fSml93401 	v = e->rcep_p.zone->zone_ipc.ipcq_semmni + incr;
1542824c205fSml93401 	if (v > rval->rcv_value)
1543824c205fSml93401 		return (1);
1544824c205fSml93401 	return (0);
1545824c205fSml93401 }
1546824c205fSml93401 
1547824c205fSml93401 static rctl_ops_t zone_semmni_ops = {
1548824c205fSml93401 	rcop_no_action,
1549824c205fSml93401 	rcop_no_usage,
1550824c205fSml93401 	rcop_no_set,
1551824c205fSml93401 	zone_semmni_test
1552824c205fSml93401 };
1553824c205fSml93401 
1554824c205fSml93401 /*ARGSUSED*/
1555824c205fSml93401 static int
1556824c205fSml93401 zone_msgmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
1557824c205fSml93401     rctl_qty_t incr, uint_t flags)
1558824c205fSml93401 {
1559824c205fSml93401 	rctl_qty_t v;
1560824c205fSml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
1561824c205fSml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1562824c205fSml93401 	v = e->rcep_p.zone->zone_ipc.ipcq_msgmni + incr;
1563824c205fSml93401 	if (v > rval->rcv_value)
1564824c205fSml93401 		return (1);
1565824c205fSml93401 	return (0);
1566824c205fSml93401 }
1567824c205fSml93401 
1568824c205fSml93401 static rctl_ops_t zone_msgmni_ops = {
1569824c205fSml93401 	rcop_no_action,
1570824c205fSml93401 	rcop_no_usage,
1571824c205fSml93401 	rcop_no_set,
1572824c205fSml93401 	zone_msgmni_test
1573824c205fSml93401 };
1574824c205fSml93401 
1575c6939658Ssl108498 /*ARGSUSED*/
1576c6939658Ssl108498 static rctl_qty_t
1577c6939658Ssl108498 zone_locked_mem_usage(rctl_t *rctl, struct proc *p)
1578c6939658Ssl108498 {
1579c6939658Ssl108498 	rctl_qty_t q;
1580c6939658Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
15810209230bSgjelinek 	mutex_enter(&p->p_zone->zone_mem_lock);
1582c6939658Ssl108498 	q = p->p_zone->zone_locked_mem;
15830209230bSgjelinek 	mutex_exit(&p->p_zone->zone_mem_lock);
1584c6939658Ssl108498 	return (q);
1585c6939658Ssl108498 }
1586c6939658Ssl108498 
1587c6939658Ssl108498 /*ARGSUSED*/
1588c6939658Ssl108498 static int
1589c6939658Ssl108498 zone_locked_mem_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
1590c6939658Ssl108498     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
1591c6939658Ssl108498 {
1592c6939658Ssl108498 	rctl_qty_t q;
15930209230bSgjelinek 	zone_t *z;
15940209230bSgjelinek 
15950209230bSgjelinek 	z = e->rcep_p.zone;
1596c6939658Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
15970209230bSgjelinek 	ASSERT(MUTEX_HELD(&z->zone_mem_lock));
15980209230bSgjelinek 	q = z->zone_locked_mem;
1599c6939658Ssl108498 	if (q + incr > rcntl->rcv_value)
1600c6939658Ssl108498 		return (1);
1601c6939658Ssl108498 	return (0);
1602c6939658Ssl108498 }
1603c6939658Ssl108498 
1604c6939658Ssl108498 /*ARGSUSED*/
1605c6939658Ssl108498 static int
1606c6939658Ssl108498 zone_locked_mem_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
1607c6939658Ssl108498     rctl_qty_t nv)
1608c6939658Ssl108498 {
1609c6939658Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
1610c6939658Ssl108498 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1611c6939658Ssl108498 	if (e->rcep_p.zone == NULL)
1612c6939658Ssl108498 		return (0);
1613c6939658Ssl108498 	e->rcep_p.zone->zone_locked_mem_ctl = nv;
1614c6939658Ssl108498 	return (0);
1615c6939658Ssl108498 }
1616c6939658Ssl108498 
1617c6939658Ssl108498 static rctl_ops_t zone_locked_mem_ops = {
1618c6939658Ssl108498 	rcop_no_action,
1619c6939658Ssl108498 	zone_locked_mem_usage,
1620c6939658Ssl108498 	zone_locked_mem_set,
1621c6939658Ssl108498 	zone_locked_mem_test
1622c6939658Ssl108498 };
1623824c205fSml93401 
16240209230bSgjelinek /*ARGSUSED*/
16250209230bSgjelinek static rctl_qty_t
16260209230bSgjelinek zone_max_swap_usage(rctl_t *rctl, struct proc *p)
16270209230bSgjelinek {
16280209230bSgjelinek 	rctl_qty_t q;
16290209230bSgjelinek 	zone_t *z = p->p_zone;
16300209230bSgjelinek 
16310209230bSgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
16320209230bSgjelinek 	mutex_enter(&z->zone_mem_lock);
16330209230bSgjelinek 	q = z->zone_max_swap;
16340209230bSgjelinek 	mutex_exit(&z->zone_mem_lock);
16350209230bSgjelinek 	return (q);
16360209230bSgjelinek }
16370209230bSgjelinek 
16380209230bSgjelinek /*ARGSUSED*/
16390209230bSgjelinek static int
16400209230bSgjelinek zone_max_swap_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
16410209230bSgjelinek     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
16420209230bSgjelinek {
16430209230bSgjelinek 	rctl_qty_t q;
16440209230bSgjelinek 	zone_t *z;
16450209230bSgjelinek 
16460209230bSgjelinek 	z = e->rcep_p.zone;
16470209230bSgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
16480209230bSgjelinek 	ASSERT(MUTEX_HELD(&z->zone_mem_lock));
16490209230bSgjelinek 	q = z->zone_max_swap;
16500209230bSgjelinek 	if (q + incr > rcntl->rcv_value)
16510209230bSgjelinek 		return (1);
16520209230bSgjelinek 	return (0);
16530209230bSgjelinek }
16540209230bSgjelinek 
16550209230bSgjelinek /*ARGSUSED*/
16560209230bSgjelinek static int
16570209230bSgjelinek zone_max_swap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
16580209230bSgjelinek     rctl_qty_t nv)
16590209230bSgjelinek {
16600209230bSgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
16610209230bSgjelinek 	ASSERT(e->rcep_t == RCENTITY_ZONE);
16620209230bSgjelinek 	if (e->rcep_p.zone == NULL)
16630209230bSgjelinek 		return (0);
16640209230bSgjelinek 	e->rcep_p.zone->zone_max_swap_ctl = nv;
16650209230bSgjelinek 	return (0);
16660209230bSgjelinek }
16670209230bSgjelinek 
16680209230bSgjelinek static rctl_ops_t zone_max_swap_ops = {
16690209230bSgjelinek 	rcop_no_action,
16700209230bSgjelinek 	zone_max_swap_usage,
16710209230bSgjelinek 	zone_max_swap_set,
16720209230bSgjelinek 	zone_max_swap_test
16730209230bSgjelinek };
16740209230bSgjelinek 
16750fbb751dSJohn Levon /*ARGSUSED*/
16760fbb751dSJohn Levon static rctl_qty_t
16770fbb751dSJohn Levon zone_max_lofi_usage(rctl_t *rctl, struct proc *p)
16780fbb751dSJohn Levon {
16790fbb751dSJohn Levon 	rctl_qty_t q;
16800fbb751dSJohn Levon 	zone_t *z = p->p_zone;
16810fbb751dSJohn Levon 
16820fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&p->p_lock));
16830fbb751dSJohn Levon 	mutex_enter(&z->zone_rctl_lock);
16840fbb751dSJohn Levon 	q = z->zone_max_lofi;
16850fbb751dSJohn Levon 	mutex_exit(&z->zone_rctl_lock);
16860fbb751dSJohn Levon 	return (q);
16870fbb751dSJohn Levon }
16880fbb751dSJohn Levon 
16890fbb751dSJohn Levon /*ARGSUSED*/
16900fbb751dSJohn Levon static int
16910fbb751dSJohn Levon zone_max_lofi_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
16920fbb751dSJohn Levon     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
16930fbb751dSJohn Levon {
16940fbb751dSJohn Levon 	rctl_qty_t q;
16950fbb751dSJohn Levon 	zone_t *z;
16960fbb751dSJohn Levon 
16970fbb751dSJohn Levon 	z = e->rcep_p.zone;
16980fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&p->p_lock));
16990fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&z->zone_rctl_lock));
17000fbb751dSJohn Levon 	q = z->zone_max_lofi;
17010fbb751dSJohn Levon 	if (q + incr > rcntl->rcv_value)
17020fbb751dSJohn Levon 		return (1);
17030fbb751dSJohn Levon 	return (0);
17040fbb751dSJohn Levon }
17050fbb751dSJohn Levon 
17060fbb751dSJohn Levon /*ARGSUSED*/
17070fbb751dSJohn Levon static int
17080fbb751dSJohn Levon zone_max_lofi_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
17090fbb751dSJohn Levon     rctl_qty_t nv)
17100fbb751dSJohn Levon {
17110fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&p->p_lock));
17120fbb751dSJohn Levon 	ASSERT(e->rcep_t == RCENTITY_ZONE);
17130fbb751dSJohn Levon 	if (e->rcep_p.zone == NULL)
17140fbb751dSJohn Levon 		return (0);
17150fbb751dSJohn Levon 	e->rcep_p.zone->zone_max_lofi_ctl = nv;
17160fbb751dSJohn Levon 	return (0);
17170fbb751dSJohn Levon }
17180fbb751dSJohn Levon 
17190fbb751dSJohn Levon static rctl_ops_t zone_max_lofi_ops = {
17200fbb751dSJohn Levon 	rcop_no_action,
17210fbb751dSJohn Levon 	zone_max_lofi_usage,
17220fbb751dSJohn Levon 	zone_max_lofi_set,
17230fbb751dSJohn Levon 	zone_max_lofi_test
17240fbb751dSJohn Levon };
17250fbb751dSJohn Levon 
17267c478bd9Sstevel@tonic-gate /*
17277c478bd9Sstevel@tonic-gate  * Helper function to brand the zone with a unique ID.
17287c478bd9Sstevel@tonic-gate  */
17297c478bd9Sstevel@tonic-gate static void
17307c478bd9Sstevel@tonic-gate zone_uniqid(zone_t *zone)
17317c478bd9Sstevel@tonic-gate {
17327c478bd9Sstevel@tonic-gate 	static uint64_t uniqid = 0;
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
17357c478bd9Sstevel@tonic-gate 	zone->zone_uniqid = uniqid++;
17367c478bd9Sstevel@tonic-gate }
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate /*
17397c478bd9Sstevel@tonic-gate  * Returns a held pointer to the "kcred" for the specified zone.
17407c478bd9Sstevel@tonic-gate  */
17417c478bd9Sstevel@tonic-gate struct cred *
17427c478bd9Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid)
17437c478bd9Sstevel@tonic-gate {
17447c478bd9Sstevel@tonic-gate 	zone_t *zone;
17457c478bd9Sstevel@tonic-gate 	cred_t *cr;
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_by_id(zoneid)) == NULL)
17487c478bd9Sstevel@tonic-gate 		return (NULL);
17497c478bd9Sstevel@tonic-gate 	cr = zone->zone_kcred;
17507c478bd9Sstevel@tonic-gate 	crhold(cr);
17517c478bd9Sstevel@tonic-gate 	zone_rele(zone);
17527c478bd9Sstevel@tonic-gate 	return (cr);
17537c478bd9Sstevel@tonic-gate }
17547c478bd9Sstevel@tonic-gate 
17550209230bSgjelinek static int
17560209230bSgjelinek zone_lockedmem_kstat_update(kstat_t *ksp, int rw)
17570209230bSgjelinek {
17580209230bSgjelinek 	zone_t *zone = ksp->ks_private;
17590209230bSgjelinek 	zone_kstat_t *zk = ksp->ks_data;
17600209230bSgjelinek 
17610209230bSgjelinek 	if (rw == KSTAT_WRITE)
17620209230bSgjelinek 		return (EACCES);
17630209230bSgjelinek 
17640209230bSgjelinek 	zk->zk_usage.value.ui64 = zone->zone_locked_mem;
17650209230bSgjelinek 	zk->zk_value.value.ui64 = zone->zone_locked_mem_ctl;
17660209230bSgjelinek 	return (0);
17670209230bSgjelinek }
17680209230bSgjelinek 
17690209230bSgjelinek static int
1770ff19e029SMenno Lageman zone_nprocs_kstat_update(kstat_t *ksp, int rw)
1771ff19e029SMenno Lageman {
1772ff19e029SMenno Lageman 	zone_t *zone = ksp->ks_private;
1773ff19e029SMenno Lageman 	zone_kstat_t *zk = ksp->ks_data;
1774ff19e029SMenno Lageman 
1775ff19e029SMenno Lageman 	if (rw == KSTAT_WRITE)
1776ff19e029SMenno Lageman 		return (EACCES);
1777ff19e029SMenno Lageman 
1778ff19e029SMenno Lageman 	zk->zk_usage.value.ui64 = zone->zone_nprocs;
1779ff19e029SMenno Lageman 	zk->zk_value.value.ui64 = zone->zone_nprocs_ctl;
1780ff19e029SMenno Lageman 	return (0);
1781ff19e029SMenno Lageman }
1782ff19e029SMenno Lageman 
1783ff19e029SMenno Lageman static int
17840209230bSgjelinek zone_swapresv_kstat_update(kstat_t *ksp, int rw)
17850209230bSgjelinek {
17860209230bSgjelinek 	zone_t *zone = ksp->ks_private;
17870209230bSgjelinek 	zone_kstat_t *zk = ksp->ks_data;
17880209230bSgjelinek 
17890209230bSgjelinek 	if (rw == KSTAT_WRITE)
17900209230bSgjelinek 		return (EACCES);
17910209230bSgjelinek 
17920209230bSgjelinek 	zk->zk_usage.value.ui64 = zone->zone_max_swap;
17930209230bSgjelinek 	zk->zk_value.value.ui64 = zone->zone_max_swap_ctl;
17940209230bSgjelinek 	return (0);
17950209230bSgjelinek }
17960209230bSgjelinek 
1797ff19e029SMenno Lageman static kstat_t *
1798ff19e029SMenno Lageman zone_kstat_create_common(zone_t *zone, char *name,
1799ff19e029SMenno Lageman     int (*updatefunc) (kstat_t *, int))
18000209230bSgjelinek {
18010209230bSgjelinek 	kstat_t *ksp;
18020209230bSgjelinek 	zone_kstat_t *zk;
18030209230bSgjelinek 
1804ff19e029SMenno Lageman 	ksp = rctl_kstat_create_zone(zone, name, KSTAT_TYPE_NAMED,
18050209230bSgjelinek 	    sizeof (zone_kstat_t) / sizeof (kstat_named_t),
18060209230bSgjelinek 	    KSTAT_FLAG_VIRTUAL);
18070209230bSgjelinek 
18080209230bSgjelinek 	if (ksp == NULL)
1809ff19e029SMenno Lageman 		return (NULL);
18100209230bSgjelinek 
18110209230bSgjelinek 	zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP);
18120209230bSgjelinek 	ksp->ks_data_size += strlen(zone->zone_name) + 1;
18130209230bSgjelinek 	kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING);
18140209230bSgjelinek 	kstat_named_setstr(&zk->zk_zonename, zone->zone_name);
18150209230bSgjelinek 	kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64);
18160209230bSgjelinek 	kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64);
1817ff19e029SMenno Lageman 	ksp->ks_update = updatefunc;
18180209230bSgjelinek 	ksp->ks_private = zone;
18190209230bSgjelinek 	kstat_install(ksp);
1820ff19e029SMenno Lageman 	return (ksp);
1821ff19e029SMenno Lageman }
18220209230bSgjelinek 
1823ff19e029SMenno Lageman static void
1824ff19e029SMenno Lageman zone_kstat_create(zone_t *zone)
1825ff19e029SMenno Lageman {
1826ff19e029SMenno Lageman 	zone->zone_lockedmem_kstat = zone_kstat_create_common(zone,
1827ff19e029SMenno Lageman 	    "lockedmem", zone_lockedmem_kstat_update);
1828ff19e029SMenno Lageman 	zone->zone_swapresv_kstat = zone_kstat_create_common(zone,
1829ff19e029SMenno Lageman 	    "swapresv", zone_swapresv_kstat_update);
1830ff19e029SMenno Lageman 	zone->zone_nprocs_kstat = zone_kstat_create_common(zone,
1831ff19e029SMenno Lageman 	    "nprocs", zone_nprocs_kstat_update);
1832ff19e029SMenno Lageman }
18330209230bSgjelinek 
1834ff19e029SMenno Lageman static void
1835ff19e029SMenno Lageman zone_kstat_delete_common(kstat_t **pkstat)
1836ff19e029SMenno Lageman {
1837ff19e029SMenno Lageman 	void *data;
18380209230bSgjelinek 
1839ff19e029SMenno Lageman 	if (*pkstat != NULL) {
1840ff19e029SMenno Lageman 		data = (*pkstat)->ks_data;
1841ff19e029SMenno Lageman 		kstat_delete(*pkstat);
1842ff19e029SMenno Lageman 		kmem_free(data, sizeof (zone_kstat_t));
1843ff19e029SMenno Lageman 		*pkstat = NULL;
1844ff19e029SMenno Lageman 	}
18450209230bSgjelinek }
18460209230bSgjelinek 
18470209230bSgjelinek static void
18480209230bSgjelinek zone_kstat_delete(zone_t *zone)
18490209230bSgjelinek {
1850ff19e029SMenno Lageman 	zone_kstat_delete_common(&zone->zone_lockedmem_kstat);
1851ff19e029SMenno Lageman 	zone_kstat_delete_common(&zone->zone_swapresv_kstat);
1852ff19e029SMenno Lageman 	zone_kstat_delete_common(&zone->zone_nprocs_kstat);
18530209230bSgjelinek }
18540209230bSgjelinek 
18557c478bd9Sstevel@tonic-gate /*
18567c478bd9Sstevel@tonic-gate  * Called very early on in boot to initialize the ZSD list so that
18577c478bd9Sstevel@tonic-gate  * zone_key_create() can be called before zone_init().  It also initializes
18587c478bd9Sstevel@tonic-gate  * portions of zone0 which may be used before zone_init() is called.  The
18597c478bd9Sstevel@tonic-gate  * variable "global_zone" will be set when zone0 is fully initialized by
18607c478bd9Sstevel@tonic-gate  * zone_init().
18617c478bd9Sstevel@tonic-gate  */
18627c478bd9Sstevel@tonic-gate void
18637c478bd9Sstevel@tonic-gate zone_zsd_init(void)
18647c478bd9Sstevel@tonic-gate {
18657c478bd9Sstevel@tonic-gate 	mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL);
18667c478bd9Sstevel@tonic-gate 	mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL);
18677c478bd9Sstevel@tonic-gate 	list_create(&zsd_registered_keys, sizeof (struct zsd_entry),
18687c478bd9Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
18697c478bd9Sstevel@tonic-gate 	list_create(&zone_active, sizeof (zone_t),
18707c478bd9Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
18717c478bd9Sstevel@tonic-gate 	list_create(&zone_deathrow, sizeof (zone_t),
18727c478bd9Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL);
18757c478bd9Sstevel@tonic-gate 	mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
18760209230bSgjelinek 	mutex_init(&zone0.zone_mem_lock, NULL, MUTEX_DEFAULT, NULL);
18777c478bd9Sstevel@tonic-gate 	zone0.zone_shares = 1;
18780209230bSgjelinek 	zone0.zone_nlwps = 0;
18797c478bd9Sstevel@tonic-gate 	zone0.zone_nlwps_ctl = INT_MAX;
1880ff19e029SMenno Lageman 	zone0.zone_nprocs = 0;
1881ff19e029SMenno Lageman 	zone0.zone_nprocs_ctl = INT_MAX;
18820209230bSgjelinek 	zone0.zone_locked_mem = 0;
18830209230bSgjelinek 	zone0.zone_locked_mem_ctl = UINT64_MAX;
18840209230bSgjelinek 	ASSERT(zone0.zone_max_swap == 0);
18850209230bSgjelinek 	zone0.zone_max_swap_ctl = UINT64_MAX;
18860fbb751dSJohn Levon 	zone0.zone_max_lofi = 0;
18870fbb751dSJohn Levon 	zone0.zone_max_lofi_ctl = UINT64_MAX;
1888824c205fSml93401 	zone0.zone_shmmax = 0;
1889824c205fSml93401 	zone0.zone_ipc.ipcq_shmmni = 0;
1890824c205fSml93401 	zone0.zone_ipc.ipcq_semmni = 0;
1891824c205fSml93401 	zone0.zone_ipc.ipcq_msgmni = 0;
18927c478bd9Sstevel@tonic-gate 	zone0.zone_name = GLOBAL_ZONENAME;
18937c478bd9Sstevel@tonic-gate 	zone0.zone_nodename = utsname.nodename;
18947c478bd9Sstevel@tonic-gate 	zone0.zone_domain = srpc_domain;
18955679c89fSjv227347 	zone0.zone_hostid = HW_INVALID_HOSTID;
18960fbb751dSJohn Levon 	zone0.zone_fs_allowed = NULL;
18977c478bd9Sstevel@tonic-gate 	zone0.zone_ref = 1;
18987c478bd9Sstevel@tonic-gate 	zone0.zone_id = GLOBAL_ZONEID;
18997c478bd9Sstevel@tonic-gate 	zone0.zone_status = ZONE_IS_RUNNING;
19007c478bd9Sstevel@tonic-gate 	zone0.zone_rootpath = "/";
19017c478bd9Sstevel@tonic-gate 	zone0.zone_rootpathlen = 2;
19027c478bd9Sstevel@tonic-gate 	zone0.zone_psetid = ZONE_PS_INVAL;
19037c478bd9Sstevel@tonic-gate 	zone0.zone_ncpus = 0;
19047c478bd9Sstevel@tonic-gate 	zone0.zone_ncpus_online = 0;
19057c478bd9Sstevel@tonic-gate 	zone0.zone_proc_initpid = 1;
19063f2f09c1Sdp 	zone0.zone_initname = initname;
19070209230bSgjelinek 	zone0.zone_lockedmem_kstat = NULL;
19080209230bSgjelinek 	zone0.zone_swapresv_kstat = NULL;
1909ff19e029SMenno Lageman 	zone0.zone_nprocs_kstat = NULL;
1910a19609f8Sjv227347 	list_create(&zone0.zone_ref_list, sizeof (zone_ref_t),
1911a19609f8Sjv227347 	    offsetof(zone_ref_t, zref_linkage));
19127c478bd9Sstevel@tonic-gate 	list_create(&zone0.zone_zsd, sizeof (struct zsd_entry),
19137c478bd9Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
19147c478bd9Sstevel@tonic-gate 	list_insert_head(&zone_active, &zone0);
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate 	/*
19177c478bd9Sstevel@tonic-gate 	 * The root filesystem is not mounted yet, so zone_rootvp cannot be set
19187c478bd9Sstevel@tonic-gate 	 * to anything meaningful.  It is assigned to be 'rootdir' in
19197c478bd9Sstevel@tonic-gate 	 * vfs_mountroot().
19207c478bd9Sstevel@tonic-gate 	 */
19217c478bd9Sstevel@tonic-gate 	zone0.zone_rootvp = NULL;
19227c478bd9Sstevel@tonic-gate 	zone0.zone_vfslist = NULL;
19233f2f09c1Sdp 	zone0.zone_bootargs = initargs;
19247c478bd9Sstevel@tonic-gate 	zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
19257c478bd9Sstevel@tonic-gate 	/*
19267c478bd9Sstevel@tonic-gate 	 * The global zone has all privileges
19277c478bd9Sstevel@tonic-gate 	 */
19287c478bd9Sstevel@tonic-gate 	priv_fillset(zone0.zone_privset);
19297c478bd9Sstevel@tonic-gate 	/*
19307c478bd9Sstevel@tonic-gate 	 * Add p0 to the global zone
19317c478bd9Sstevel@tonic-gate 	 */
19327c478bd9Sstevel@tonic-gate 	zone0.zone_zsched = &p0;
19337c478bd9Sstevel@tonic-gate 	p0.p_zone = &zone0;
19347c478bd9Sstevel@tonic-gate }
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate /*
193745916cd2Sjpk  * Compute a hash value based on the contents of the label and the DOI.  The
193845916cd2Sjpk  * hash algorithm is somewhat arbitrary, but is based on the observation that
193945916cd2Sjpk  * humans will likely pick labels that differ by amounts that work out to be
194045916cd2Sjpk  * multiples of the number of hash chains, and thus stirring in some primes
194145916cd2Sjpk  * should help.
194245916cd2Sjpk  */
194345916cd2Sjpk static uint_t
194445916cd2Sjpk hash_bylabel(void *hdata, mod_hash_key_t key)
194545916cd2Sjpk {
194645916cd2Sjpk 	const ts_label_t *lab = (ts_label_t *)key;
194745916cd2Sjpk 	const uint32_t *up, *ue;
194845916cd2Sjpk 	uint_t hash;
194945916cd2Sjpk 	int i;
195045916cd2Sjpk 
195145916cd2Sjpk 	_NOTE(ARGUNUSED(hdata));
195245916cd2Sjpk 
195345916cd2Sjpk 	hash = lab->tsl_doi + (lab->tsl_doi << 1);
195445916cd2Sjpk 	/* we depend on alignment of label, but not representation */
195545916cd2Sjpk 	up = (const uint32_t *)&lab->tsl_label;
195645916cd2Sjpk 	ue = up + sizeof (lab->tsl_label) / sizeof (*up);
195745916cd2Sjpk 	i = 1;
195845916cd2Sjpk 	while (up < ue) {
195945916cd2Sjpk 		/* using 2^n + 1, 1 <= n <= 16 as source of many primes */
196045916cd2Sjpk 		hash += *up + (*up << ((i % 16) + 1));
196145916cd2Sjpk 		up++;
196245916cd2Sjpk 		i++;
196345916cd2Sjpk 	}
196445916cd2Sjpk 	return (hash);
196545916cd2Sjpk }
196645916cd2Sjpk 
196745916cd2Sjpk /*
196845916cd2Sjpk  * All that mod_hash cares about here is zero (equal) versus non-zero (not
196945916cd2Sjpk  * equal).  This may need to be changed if less than / greater than is ever
197045916cd2Sjpk  * needed.
197145916cd2Sjpk  */
197245916cd2Sjpk static int
197345916cd2Sjpk hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
197445916cd2Sjpk {
197545916cd2Sjpk 	ts_label_t *lab1 = (ts_label_t *)key1;
197645916cd2Sjpk 	ts_label_t *lab2 = (ts_label_t *)key2;
197745916cd2Sjpk 
197845916cd2Sjpk 	return (label_equal(lab1, lab2) ? 0 : 1);
197945916cd2Sjpk }
198045916cd2Sjpk 
198145916cd2Sjpk /*
19827c478bd9Sstevel@tonic-gate  * Called by main() to initialize the zones framework.
19837c478bd9Sstevel@tonic-gate  */
19847c478bd9Sstevel@tonic-gate void
19857c478bd9Sstevel@tonic-gate zone_init(void)
19867c478bd9Sstevel@tonic-gate {
19877c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
19887c478bd9Sstevel@tonic-gate 	rctl_val_t *dval;
19897c478bd9Sstevel@tonic-gate 	rctl_set_t *set;
19907c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
19917c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
1992cf8f45c7Sdstaff 	int res;
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate 	ASSERT(curproc == &p0);
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	/*
19977c478bd9Sstevel@tonic-gate 	 * Create ID space for zone IDs.  ID 0 is reserved for the
19987c478bd9Sstevel@tonic-gate 	 * global zone.
19997c478bd9Sstevel@tonic-gate 	 */
20007c478bd9Sstevel@tonic-gate 	zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID);
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 	/*
20037c478bd9Sstevel@tonic-gate 	 * Initialize generic zone resource controls, if any.
20047c478bd9Sstevel@tonic-gate 	 */
20057c478bd9Sstevel@tonic-gate 	rc_zone_cpu_shares = rctl_register("zone.cpu-shares",
20067c478bd9Sstevel@tonic-gate 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER |
200719f92332Sml93401 	    RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER,
2008c97ad5cdSakolb 	    FSS_MAXSHARES, FSS_MAXSHARES, &zone_cpu_shares_ops);
2009c97ad5cdSakolb 
2010c97ad5cdSakolb 	rc_zone_cpu_cap = rctl_register("zone.cpu-cap",
2011c97ad5cdSakolb 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_ALWAYS |
2012c97ad5cdSakolb 	    RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT |RCTL_GLOBAL_SYSLOG_NEVER |
2013c97ad5cdSakolb 	    RCTL_GLOBAL_INFINITE,
2014c97ad5cdSakolb 	    MAXCAP, MAXCAP, &zone_cpu_cap_ops);
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE,
20177c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
20187c478bd9Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &zone_lwps_ops);
2019ff19e029SMenno Lageman 
2020ff19e029SMenno Lageman 	rc_zone_nprocs = rctl_register("zone.max-processes", RCENTITY_ZONE,
2021ff19e029SMenno Lageman 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
2022ff19e029SMenno Lageman 	    INT_MAX, INT_MAX, &zone_procs_ops);
2023ff19e029SMenno Lageman 
20247c478bd9Sstevel@tonic-gate 	/*
2025824c205fSml93401 	 * System V IPC resource controls
2026824c205fSml93401 	 */
2027824c205fSml93401 	rc_zone_msgmni = rctl_register("zone.max-msg-ids",
2028824c205fSml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
2029824c205fSml93401 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_msgmni_ops);
2030824c205fSml93401 
2031824c205fSml93401 	rc_zone_semmni = rctl_register("zone.max-sem-ids",
2032824c205fSml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
2033824c205fSml93401 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_semmni_ops);
2034824c205fSml93401 
2035824c205fSml93401 	rc_zone_shmmni = rctl_register("zone.max-shm-ids",
2036824c205fSml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
2037824c205fSml93401 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_shmmni_ops);
2038824c205fSml93401 
2039824c205fSml93401 	rc_zone_shmmax = rctl_register("zone.max-shm-memory",
2040824c205fSml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
2041824c205fSml93401 	    RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &zone_shmmax_ops);
2042824c205fSml93401 
2043824c205fSml93401 	/*
20447c478bd9Sstevel@tonic-gate 	 * Create a rctl_val with PRIVILEGED, NOACTION, value = 1.  Then attach
20457c478bd9Sstevel@tonic-gate 	 * this at the head of the rctl_dict_entry for ``zone.cpu-shares''.
20467c478bd9Sstevel@tonic-gate 	 */
20477c478bd9Sstevel@tonic-gate 	dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
20487c478bd9Sstevel@tonic-gate 	bzero(dval, sizeof (rctl_val_t));
20497c478bd9Sstevel@tonic-gate 	dval->rcv_value = 1;
20507c478bd9Sstevel@tonic-gate 	dval->rcv_privilege = RCPRIV_PRIVILEGED;
20517c478bd9Sstevel@tonic-gate 	dval->rcv_flagaction = RCTL_LOCAL_NOACTION;
20527c478bd9Sstevel@tonic-gate 	dval->rcv_action_recip_pid = -1;
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 	rde = rctl_dict_lookup("zone.cpu-shares");
20557c478bd9Sstevel@tonic-gate 	(void) rctl_val_list_insert(&rde->rcd_default_value, dval);
20567c478bd9Sstevel@tonic-gate 
2057c6939658Ssl108498 	rc_zone_locked_mem = rctl_register("zone.max-locked-memory",
2058c6939658Ssl108498 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES |
2059c6939658Ssl108498 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
2060c6939658Ssl108498 	    &zone_locked_mem_ops);
20610209230bSgjelinek 
20620209230bSgjelinek 	rc_zone_max_swap = rctl_register("zone.max-swap",
20630209230bSgjelinek 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES |
20640209230bSgjelinek 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
20650209230bSgjelinek 	    &zone_max_swap_ops);
20660209230bSgjelinek 
20670fbb751dSJohn Levon 	rc_zone_max_lofi = rctl_register("zone.max-lofi",
20680fbb751dSJohn Levon 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT |
20690fbb751dSJohn Levon 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
20700fbb751dSJohn Levon 	    &zone_max_lofi_ops);
20710fbb751dSJohn Levon 
20727c478bd9Sstevel@tonic-gate 	/*
20737c478bd9Sstevel@tonic-gate 	 * Initialize the ``global zone''.
20747c478bd9Sstevel@tonic-gate 	 */
20757c478bd9Sstevel@tonic-gate 	set = rctl_set_create();
20767c478bd9Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
20777c478bd9Sstevel@tonic-gate 	mutex_enter(&p0.p_lock);
20787c478bd9Sstevel@tonic-gate 	e.rcep_p.zone = &zone0;
20797c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
20807c478bd9Sstevel@tonic-gate 	zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set,
20817c478bd9Sstevel@tonic-gate 	    gp);
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	zone0.zone_nlwps = p0.p_lwpcnt;
2084ff19e029SMenno Lageman 	zone0.zone_nprocs = 1;
20857c478bd9Sstevel@tonic-gate 	zone0.zone_ntasks = 1;
20867c478bd9Sstevel@tonic-gate 	mutex_exit(&p0.p_lock);
20879acbbeafSnn35248 	zone0.zone_restart_init = B_TRUE;
20889acbbeafSnn35248 	zone0.zone_brand = &native_brand;
20897c478bd9Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
20907c478bd9Sstevel@tonic-gate 	/*
20910209230bSgjelinek 	 * pool_default hasn't been initialized yet, so we let pool_init()
20920209230bSgjelinek 	 * take care of making sure the global zone is in the default pool.
20937c478bd9Sstevel@tonic-gate 	 */
209445916cd2Sjpk 
209545916cd2Sjpk 	/*
20960209230bSgjelinek 	 * Initialize global zone kstats
20970209230bSgjelinek 	 */
20980209230bSgjelinek 	zone_kstat_create(&zone0);
20990209230bSgjelinek 
21000209230bSgjelinek 	/*
210145916cd2Sjpk 	 * Initialize zone label.
210245916cd2Sjpk 	 * mlp are initialized when tnzonecfg is loaded.
210345916cd2Sjpk 	 */
210445916cd2Sjpk 	zone0.zone_slabel = l_admin_low;
210545916cd2Sjpk 	rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
210645916cd2Sjpk 	label_hold(l_admin_low);
210745916cd2Sjpk 
2108835ee219SRobert Harris 	/*
2109835ee219SRobert Harris 	 * Initialise the lock for the database structure used by mntfs.
2110835ee219SRobert Harris 	 */
2111835ee219SRobert Harris 	rw_init(&zone0.zone_mntfs_db_lock, NULL, RW_DEFAULT, NULL);
2112835ee219SRobert Harris 
21137c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
21147c478bd9Sstevel@tonic-gate 	zone_uniqid(&zone0);
21157c478bd9Sstevel@tonic-gate 	ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID);
211645916cd2Sjpk 
21177c478bd9Sstevel@tonic-gate 	zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size,
21187c478bd9Sstevel@tonic-gate 	    mod_hash_null_valdtor);
21197c478bd9Sstevel@tonic-gate 	zonehashbyname = mod_hash_create_strhash("zone_by_name",
21207c478bd9Sstevel@tonic-gate 	    zone_hash_size, mod_hash_null_valdtor);
212145916cd2Sjpk 	/*
212245916cd2Sjpk 	 * maintain zonehashbylabel only for labeled systems
212345916cd2Sjpk 	 */
212445916cd2Sjpk 	if (is_system_labeled())
212545916cd2Sjpk 		zonehashbylabel = mod_hash_create_extended("zone_by_label",
212645916cd2Sjpk 		    zone_hash_size, mod_hash_null_keydtor,
212745916cd2Sjpk 		    mod_hash_null_valdtor, hash_bylabel, NULL,
212845916cd2Sjpk 		    hash_labelkey_cmp, KM_SLEEP);
21297c478bd9Sstevel@tonic-gate 	zonecount = 1;
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID,
21327c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
21337c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name,
21347c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
213548451833Scarlsonj 	if (is_system_labeled()) {
213648451833Scarlsonj 		zone0.zone_flags |= ZF_HASHED_LABEL;
213745916cd2Sjpk 		(void) mod_hash_insert(zonehashbylabel,
213845916cd2Sjpk 		    (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0);
213948451833Scarlsonj 	}
214045916cd2Sjpk 	mutex_exit(&zonehash_lock);
214145916cd2Sjpk 
21427c478bd9Sstevel@tonic-gate 	/*
21437c478bd9Sstevel@tonic-gate 	 * We avoid setting zone_kcred until now, since kcred is initialized
21447c478bd9Sstevel@tonic-gate 	 * sometime after zone_zsd_init() and before zone_init().
21457c478bd9Sstevel@tonic-gate 	 */
21467c478bd9Sstevel@tonic-gate 	zone0.zone_kcred = kcred;
21477c478bd9Sstevel@tonic-gate 	/*
21487c478bd9Sstevel@tonic-gate 	 * The global zone is fully initialized (except for zone_rootvp which
21497c478bd9Sstevel@tonic-gate 	 * will be set when the root filesystem is mounted).
21507c478bd9Sstevel@tonic-gate 	 */
21517c478bd9Sstevel@tonic-gate 	global_zone = &zone0;
2152cf8f45c7Sdstaff 
2153cf8f45c7Sdstaff 	/*
2154cf8f45c7Sdstaff 	 * Setup an event channel to send zone status change notifications on
2155cf8f45c7Sdstaff 	 */
2156cf8f45c7Sdstaff 	res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan,
2157cf8f45c7Sdstaff 	    EVCH_CREAT);
2158cf8f45c7Sdstaff 
2159cf8f45c7Sdstaff 	if (res)
2160cf8f45c7Sdstaff 		panic("Sysevent_evc_bind failed during zone setup.\n");
21610209230bSgjelinek 
21627c478bd9Sstevel@tonic-gate }
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate static void
21657c478bd9Sstevel@tonic-gate zone_free(zone_t *zone)
21667c478bd9Sstevel@tonic-gate {
21677c478bd9Sstevel@tonic-gate 	ASSERT(zone != global_zone);
21687c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks == 0);
21697c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_nlwps == 0);
2170ff19e029SMenno Lageman 	ASSERT(zone->zone_nprocs == 0);
21717c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 0);
21727c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_kcred == NULL);
21737c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_DEAD ||
21747c478bd9Sstevel@tonic-gate 	    zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
2175a19609f8Sjv227347 	ASSERT(list_is_empty(&zone->zone_ref_list));
21767c478bd9Sstevel@tonic-gate 
2177c97ad5cdSakolb 	/*
2178c97ad5cdSakolb 	 * Remove any zone caps.
2179c97ad5cdSakolb 	 */
2180c97ad5cdSakolb 	cpucaps_zone_remove(zone);
2181c97ad5cdSakolb 
2182c97ad5cdSakolb 	ASSERT(zone->zone_cpucap == NULL);
2183c97ad5cdSakolb 
21847c478bd9Sstevel@tonic-gate 	/* remove from deathrow list */
21857c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_DEAD) {
21867c478bd9Sstevel@tonic-gate 		ASSERT(zone->zone_ref == 0);
21877c478bd9Sstevel@tonic-gate 		mutex_enter(&zone_deathrow_lock);
21887c478bd9Sstevel@tonic-gate 		list_remove(&zone_deathrow, zone);
21897c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_deathrow_lock);
21907c478bd9Sstevel@tonic-gate 	}
21917c478bd9Sstevel@tonic-gate 
2192a19609f8Sjv227347 	list_destroy(&zone->zone_ref_list);
21937c478bd9Sstevel@tonic-gate 	zone_free_zsd(zone);
2194fa9e4066Sahrens 	zone_free_datasets(zone);
21952b24ab6bSSebastien Roy 	list_destroy(&zone->zone_dl_list);
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate 	if (zone->zone_rootvp != NULL)
21987c478bd9Sstevel@tonic-gate 		VN_RELE(zone->zone_rootvp);
21997c478bd9Sstevel@tonic-gate 	if (zone->zone_rootpath)
22007c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_rootpath, zone->zone_rootpathlen);
22017c478bd9Sstevel@tonic-gate 	if (zone->zone_name != NULL)
22027c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_name, ZONENAME_MAX);
220345916cd2Sjpk 	if (zone->zone_slabel != NULL)
220445916cd2Sjpk 		label_rele(zone->zone_slabel);
22057c478bd9Sstevel@tonic-gate 	if (zone->zone_nodename != NULL)
22067c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_nodename, _SYS_NMLN);
22077c478bd9Sstevel@tonic-gate 	if (zone->zone_domain != NULL)
22087c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_domain, _SYS_NMLN);
22097c478bd9Sstevel@tonic-gate 	if (zone->zone_privset != NULL)
22107c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_privset, sizeof (priv_set_t));
22117c478bd9Sstevel@tonic-gate 	if (zone->zone_rctls != NULL)
22127c478bd9Sstevel@tonic-gate 		rctl_set_free(zone->zone_rctls);
22137c478bd9Sstevel@tonic-gate 	if (zone->zone_bootargs != NULL)
22140fbb751dSJohn Levon 		strfree(zone->zone_bootargs);
22153f2f09c1Sdp 	if (zone->zone_initname != NULL)
22160fbb751dSJohn Levon 		strfree(zone->zone_initname);
22170fbb751dSJohn Levon 	if (zone->zone_fs_allowed != NULL)
22180fbb751dSJohn Levon 		strfree(zone->zone_fs_allowed);
2219134a1f4eSCasper H.S. Dik 	if (zone->zone_pfexecd != NULL)
2220134a1f4eSCasper H.S. Dik 		klpd_freelist(&zone->zone_pfexecd);
22217c478bd9Sstevel@tonic-gate 	id_free(zoneid_space, zone->zone_id);
22227c478bd9Sstevel@tonic-gate 	mutex_destroy(&zone->zone_lock);
22237c478bd9Sstevel@tonic-gate 	cv_destroy(&zone->zone_cv);
222445916cd2Sjpk 	rw_destroy(&zone->zone_mlps.mlpl_rwlock);
2225835ee219SRobert Harris 	rw_destroy(&zone->zone_mntfs_db_lock);
22267c478bd9Sstevel@tonic-gate 	kmem_free(zone, sizeof (zone_t));
22277c478bd9Sstevel@tonic-gate }
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate /*
22307c478bd9Sstevel@tonic-gate  * See block comment at the top of this file for information about zone
22317c478bd9Sstevel@tonic-gate  * status values.
22327c478bd9Sstevel@tonic-gate  */
22337c478bd9Sstevel@tonic-gate /*
22347c478bd9Sstevel@tonic-gate  * Convenience function for setting zone status.
22357c478bd9Sstevel@tonic-gate  */
22367c478bd9Sstevel@tonic-gate static void
22377c478bd9Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status)
22387c478bd9Sstevel@tonic-gate {
2239cf8f45c7Sdstaff 
2240cf8f45c7Sdstaff 	nvlist_t *nvl = NULL;
22417c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zone_status_lock));
22427c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE &&
22437c478bd9Sstevel@tonic-gate 	    status >= zone_status_get(zone));
2244cf8f45c7Sdstaff 
2245cf8f45c7Sdstaff 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) ||
2246cf8f45c7Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) ||
2247cf8f45c7Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_NEWSTATE,
2248cf8f45c7Sdstaff 	    zone_status_table[status]) ||
2249cf8f45c7Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_OLDSTATE,
2250cf8f45c7Sdstaff 	    zone_status_table[zone->zone_status]) ||
2251cf8f45c7Sdstaff 	    nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) ||
2252cf8f45c7Sdstaff 	    nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) ||
2253cf8f45c7Sdstaff 	    sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS,
22543f2f09c1Sdp 	    ZONE_EVENT_STATUS_SUBCLASS, "sun.com", "kernel", nvl, EVCH_SLEEP)) {
2255cf8f45c7Sdstaff #ifdef DEBUG
2256cf8f45c7Sdstaff 		(void) printf(
2257cf8f45c7Sdstaff 		    "Failed to allocate and send zone state change event.\n");
2258cf8f45c7Sdstaff #endif
2259cf8f45c7Sdstaff 	}
2260cf8f45c7Sdstaff 	nvlist_free(nvl);
2261cf8f45c7Sdstaff 
22627c478bd9Sstevel@tonic-gate 	zone->zone_status = status;
2263cf8f45c7Sdstaff 
22647c478bd9Sstevel@tonic-gate 	cv_broadcast(&zone->zone_cv);
22657c478bd9Sstevel@tonic-gate }
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate /*
22687c478bd9Sstevel@tonic-gate  * Public function to retrieve the zone status.  The zone status may
22697c478bd9Sstevel@tonic-gate  * change after it is retrieved.
22707c478bd9Sstevel@tonic-gate  */
22717c478bd9Sstevel@tonic-gate zone_status_t
22727c478bd9Sstevel@tonic-gate zone_status_get(zone_t *zone)
22737c478bd9Sstevel@tonic-gate {
22747c478bd9Sstevel@tonic-gate 	return (zone->zone_status);
22757c478bd9Sstevel@tonic-gate }
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate static int
22787c478bd9Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs)
22797c478bd9Sstevel@tonic-gate {
22800fbb751dSJohn Levon 	char *buf = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP);
22813f2f09c1Sdp 	int err = 0;
22827c478bd9Sstevel@tonic-gate 
22833f2f09c1Sdp 	ASSERT(zone != global_zone);
22840fbb751dSJohn Levon 	if ((err = copyinstr(zone_bootargs, buf, BOOTARGS_MAX, NULL)) != 0)
22853f2f09c1Sdp 		goto done;	/* EFAULT or ENAMETOOLONG */
22863f2f09c1Sdp 
22873f2f09c1Sdp 	if (zone->zone_bootargs != NULL)
22880fbb751dSJohn Levon 		strfree(zone->zone_bootargs);
22893f2f09c1Sdp 
22900fbb751dSJohn Levon 	zone->zone_bootargs = strdup(buf);
22913f2f09c1Sdp 
22923f2f09c1Sdp done:
22930fbb751dSJohn Levon 	kmem_free(buf, BOOTARGS_MAX);
22943f2f09c1Sdp 	return (err);
22957c478bd9Sstevel@tonic-gate }
22967c478bd9Sstevel@tonic-gate 
22973f2f09c1Sdp static int
229859f2ff5cSedp zone_set_brand(zone_t *zone, const char *brand)
229959f2ff5cSedp {
230059f2ff5cSedp 	struct brand_attr *attrp;
230159f2ff5cSedp 	brand_t *bp;
230259f2ff5cSedp 
230359f2ff5cSedp 	attrp = kmem_alloc(sizeof (struct brand_attr), KM_SLEEP);
230459f2ff5cSedp 	if (copyin(brand, attrp, sizeof (struct brand_attr)) != 0) {
230559f2ff5cSedp 		kmem_free(attrp, sizeof (struct brand_attr));
230659f2ff5cSedp 		return (EFAULT);
230759f2ff5cSedp 	}
230859f2ff5cSedp 
230959f2ff5cSedp 	bp = brand_register_zone(attrp);
231059f2ff5cSedp 	kmem_free(attrp, sizeof (struct brand_attr));
231159f2ff5cSedp 	if (bp == NULL)
231259f2ff5cSedp 		return (EINVAL);
231359f2ff5cSedp 
231459f2ff5cSedp 	/*
231559f2ff5cSedp 	 * This is the only place where a zone can change it's brand.
231659f2ff5cSedp 	 * We already need to hold zone_status_lock to check the zone
231759f2ff5cSedp 	 * status, so we'll just use that lock to serialize zone
231859f2ff5cSedp 	 * branding requests as well.
231959f2ff5cSedp 	 */
232059f2ff5cSedp 	mutex_enter(&zone_status_lock);
232159f2ff5cSedp 
232259f2ff5cSedp 	/* Re-Branding is not allowed and the zone can't be booted yet */
232359f2ff5cSedp 	if ((ZONE_IS_BRANDED(zone)) ||
232459f2ff5cSedp 	    (zone_status_get(zone) >= ZONE_IS_BOOTING)) {
232559f2ff5cSedp 		mutex_exit(&zone_status_lock);
232659f2ff5cSedp 		brand_unregister_zone(bp);
232759f2ff5cSedp 		return (EINVAL);
232859f2ff5cSedp 	}
232959f2ff5cSedp 
2330319378d9Seh208807 	/* set up the brand specific data */
233159f2ff5cSedp 	zone->zone_brand = bp;
2332319378d9Seh208807 	ZBROP(zone)->b_init_brand_data(zone);
2333319378d9Seh208807 
233459f2ff5cSedp 	mutex_exit(&zone_status_lock);
233559f2ff5cSedp 	return (0);
233659f2ff5cSedp }
233759f2ff5cSedp 
233859f2ff5cSedp static int
23390fbb751dSJohn Levon zone_set_fs_allowed(zone_t *zone, const char *zone_fs_allowed)
23400fbb751dSJohn Levon {
23410fbb751dSJohn Levon 	char *buf = kmem_zalloc(ZONE_FS_ALLOWED_MAX, KM_SLEEP);
23420fbb751dSJohn Levon 	int err = 0;
23430fbb751dSJohn Levon 
23440fbb751dSJohn Levon 	ASSERT(zone != global_zone);
23450fbb751dSJohn Levon 	if ((err = copyinstr(zone_fs_allowed, buf,
23460fbb751dSJohn Levon 	    ZONE_FS_ALLOWED_MAX, NULL)) != 0)
23470fbb751dSJohn Levon 		goto done;
23480fbb751dSJohn Levon 
23490fbb751dSJohn Levon 	if (zone->zone_fs_allowed != NULL)
23500fbb751dSJohn Levon 		strfree(zone->zone_fs_allowed);
23510fbb751dSJohn Levon 
23520fbb751dSJohn Levon 	zone->zone_fs_allowed = strdup(buf);
23530fbb751dSJohn Levon 
23540fbb751dSJohn Levon done:
23550fbb751dSJohn Levon 	kmem_free(buf, ZONE_FS_ALLOWED_MAX);
23560fbb751dSJohn Levon 	return (err);
23570fbb751dSJohn Levon }
23580fbb751dSJohn Levon 
23590fbb751dSJohn Levon static int
23603f2f09c1Sdp zone_set_initname(zone_t *zone, const char *zone_initname)
23613f2f09c1Sdp {
23623f2f09c1Sdp 	char initname[INITNAME_SZ];
23633f2f09c1Sdp 	size_t len;
23643f2f09c1Sdp 	int err = 0;
23653f2f09c1Sdp 
23663f2f09c1Sdp 	ASSERT(zone != global_zone);
23673f2f09c1Sdp 	if ((err = copyinstr(zone_initname, initname, INITNAME_SZ, &len)) != 0)
23683f2f09c1Sdp 		return (err);	/* EFAULT or ENAMETOOLONG */
23693f2f09c1Sdp 
23703f2f09c1Sdp 	if (zone->zone_initname != NULL)
23710fbb751dSJohn Levon 		strfree(zone->zone_initname);
23723f2f09c1Sdp 
23733f2f09c1Sdp 	zone->zone_initname = kmem_alloc(strlen(initname) + 1, KM_SLEEP);
23743f2f09c1Sdp 	(void) strcpy(zone->zone_initname, initname);
23757c478bd9Sstevel@tonic-gate 	return (0);
23767c478bd9Sstevel@tonic-gate }
23777c478bd9Sstevel@tonic-gate 
23780209230bSgjelinek static int
23790209230bSgjelinek zone_set_phys_mcap(zone_t *zone, const uint64_t *zone_mcap)
23800209230bSgjelinek {
23810209230bSgjelinek 	uint64_t mcap;
23820209230bSgjelinek 	int err = 0;
23830209230bSgjelinek 
23840209230bSgjelinek 	if ((err = copyin(zone_mcap, &mcap, sizeof (uint64_t))) == 0)
23850209230bSgjelinek 		zone->zone_phys_mcap = mcap;
23860209230bSgjelinek 
23870209230bSgjelinek 	return (err);
23880209230bSgjelinek }
23890209230bSgjelinek 
23900209230bSgjelinek static int
23910209230bSgjelinek zone_set_sched_class(zone_t *zone, const char *new_class)
23920209230bSgjelinek {
23930209230bSgjelinek 	char sched_class[PC_CLNMSZ];
23940209230bSgjelinek 	id_t classid;
23950209230bSgjelinek 	int err;
23960209230bSgjelinek 
23970209230bSgjelinek 	ASSERT(zone != global_zone);
23980209230bSgjelinek 	if ((err = copyinstr(new_class, sched_class, PC_CLNMSZ, NULL)) != 0)
23990209230bSgjelinek 		return (err);	/* EFAULT or ENAMETOOLONG */
24000209230bSgjelinek 
240135a5a358SJonathan Adams 	if (getcid(sched_class, &classid) != 0 || CLASS_KERNEL(classid))
24020209230bSgjelinek 		return (set_errno(EINVAL));
24030209230bSgjelinek 	zone->zone_defaultcid = classid;
24040209230bSgjelinek 	ASSERT(zone->zone_defaultcid > 0 &&
24050209230bSgjelinek 	    zone->zone_defaultcid < loaded_classes);
24060209230bSgjelinek 
24070209230bSgjelinek 	return (0);
24080209230bSgjelinek }
24090209230bSgjelinek 
24107c478bd9Sstevel@tonic-gate /*
24117c478bd9Sstevel@tonic-gate  * Block indefinitely waiting for (zone_status >= status)
24127c478bd9Sstevel@tonic-gate  */
24137c478bd9Sstevel@tonic-gate void
24147c478bd9Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status)
24157c478bd9Sstevel@tonic-gate {
24167c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
24197c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
24207c478bd9Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
24217c478bd9Sstevel@tonic-gate 	}
24227c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
24237c478bd9Sstevel@tonic-gate }
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate /*
24267c478bd9Sstevel@tonic-gate  * Private CPR-safe version of zone_status_wait().
24277c478bd9Sstevel@tonic-gate  */
24287c478bd9Sstevel@tonic-gate static void
24297c478bd9Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str)
24307c478bd9Sstevel@tonic-gate {
24317c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
24347c478bd9Sstevel@tonic-gate 
24357c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr,
24367c478bd9Sstevel@tonic-gate 	    str);
24377c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
24387c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
24397c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
24407c478bd9Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
24417c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock);
24427c478bd9Sstevel@tonic-gate 	}
24437c478bd9Sstevel@tonic-gate 	/*
24447c478bd9Sstevel@tonic-gate 	 * zone_status_lock is implicitly released by the following.
24457c478bd9Sstevel@tonic-gate 	 */
24467c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
24477c478bd9Sstevel@tonic-gate }
24487c478bd9Sstevel@tonic-gate 
24497c478bd9Sstevel@tonic-gate /*
24507c478bd9Sstevel@tonic-gate  * Block until zone enters requested state or signal is received.  Return (0)
24517c478bd9Sstevel@tonic-gate  * if signaled, non-zero otherwise.
24527c478bd9Sstevel@tonic-gate  */
24537c478bd9Sstevel@tonic-gate int
24547c478bd9Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status)
24557c478bd9Sstevel@tonic-gate {
24567c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
24597c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
24607c478bd9Sstevel@tonic-gate 		if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) {
24617c478bd9Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
24627c478bd9Sstevel@tonic-gate 			return (0);
24637c478bd9Sstevel@tonic-gate 		}
24647c478bd9Sstevel@tonic-gate 	}
24657c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
24667c478bd9Sstevel@tonic-gate 	return (1);
24677c478bd9Sstevel@tonic-gate }
24687c478bd9Sstevel@tonic-gate 
24697c478bd9Sstevel@tonic-gate /*
24707c478bd9Sstevel@tonic-gate  * Block until the zone enters the requested state or the timeout expires,
24717c478bd9Sstevel@tonic-gate  * whichever happens first.  Return (-1) if operation timed out, time remaining
24727c478bd9Sstevel@tonic-gate  * otherwise.
24737c478bd9Sstevel@tonic-gate  */
24747c478bd9Sstevel@tonic-gate clock_t
24757c478bd9Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status)
24767c478bd9Sstevel@tonic-gate {
24777c478bd9Sstevel@tonic-gate 	clock_t timeleft = 0;
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
24827c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status && timeleft != -1) {
24837c478bd9Sstevel@tonic-gate 		timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim);
24847c478bd9Sstevel@tonic-gate 	}
24857c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
24867c478bd9Sstevel@tonic-gate 	return (timeleft);
24877c478bd9Sstevel@tonic-gate }
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate /*
24907c478bd9Sstevel@tonic-gate  * Block until the zone enters the requested state, the current process is
24917c478bd9Sstevel@tonic-gate  * signaled,  or the timeout expires, whichever happens first.  Return (-1) if
24927c478bd9Sstevel@tonic-gate  * operation timed out, 0 if signaled, time remaining otherwise.
24937c478bd9Sstevel@tonic-gate  */
24947c478bd9Sstevel@tonic-gate clock_t
24957c478bd9Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status)
24967c478bd9Sstevel@tonic-gate {
2497d3d50737SRafael Vanoni 	clock_t timeleft = tim - ddi_get_lbolt();
24987c478bd9Sstevel@tonic-gate 
24997c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
25027c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
25037c478bd9Sstevel@tonic-gate 		timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock,
25047c478bd9Sstevel@tonic-gate 		    tim);
25057c478bd9Sstevel@tonic-gate 		if (timeleft <= 0)
25067c478bd9Sstevel@tonic-gate 			break;
25077c478bd9Sstevel@tonic-gate 	}
25087c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
25097c478bd9Sstevel@tonic-gate 	return (timeleft);
25107c478bd9Sstevel@tonic-gate }
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate /*
25137c478bd9Sstevel@tonic-gate  * Zones have two reference counts: one for references from credential
25147c478bd9Sstevel@tonic-gate  * structures (zone_cred_ref), and one (zone_ref) for everything else.
25157c478bd9Sstevel@tonic-gate  * This is so we can allow a zone to be rebooted while there are still
25167c478bd9Sstevel@tonic-gate  * outstanding cred references, since certain drivers cache dblks (which
25177c478bd9Sstevel@tonic-gate  * implicitly results in cached creds).  We wait for zone_ref to drop to
25187c478bd9Sstevel@tonic-gate  * 0 (actually 1), but not zone_cred_ref.  The zone structure itself is
25197c478bd9Sstevel@tonic-gate  * later freed when the zone_cred_ref drops to 0, though nothing other
25207c478bd9Sstevel@tonic-gate  * than the zone id and privilege set should be accessed once the zone
25217c478bd9Sstevel@tonic-gate  * is "dead".
25227c478bd9Sstevel@tonic-gate  *
25237c478bd9Sstevel@tonic-gate  * A debugging flag, zone_wait_for_cred, can be set to a non-zero value
25247c478bd9Sstevel@tonic-gate  * to force halt/reboot to block waiting for the zone_cred_ref to drop
25257c478bd9Sstevel@tonic-gate  * to 0.  This can be useful to flush out other sources of cached creds
25267c478bd9Sstevel@tonic-gate  * that may be less innocuous than the driver case.
2527a19609f8Sjv227347  *
2528a19609f8Sjv227347  * Zones also provide a tracked reference counting mechanism in which zone
2529a19609f8Sjv227347  * references are represented by "crumbs" (zone_ref structures).  Crumbs help
2530a19609f8Sjv227347  * debuggers determine the sources of leaked zone references.  See
2531a19609f8Sjv227347  * zone_hold_ref() and zone_rele_ref() below for more information.
25327c478bd9Sstevel@tonic-gate  */
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate int zone_wait_for_cred = 0;
25357c478bd9Sstevel@tonic-gate 
25367c478bd9Sstevel@tonic-gate static void
25377c478bd9Sstevel@tonic-gate zone_hold_locked(zone_t *z)
25387c478bd9Sstevel@tonic-gate {
25397c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&z->zone_lock));
25407c478bd9Sstevel@tonic-gate 	z->zone_ref++;
25417c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
25427c478bd9Sstevel@tonic-gate }
25437c478bd9Sstevel@tonic-gate 
2544a19609f8Sjv227347 /*
2545a19609f8Sjv227347  * Increment the specified zone's reference count.  The zone's zone_t structure
2546a19609f8Sjv227347  * will not be freed as long as the zone's reference count is nonzero.
2547a19609f8Sjv227347  * Decrement the zone's reference count via zone_rele().
2548a19609f8Sjv227347  *
2549a19609f8Sjv227347  * NOTE: This function should only be used to hold zones for short periods of
2550a19609f8Sjv227347  * time.  Use zone_hold_ref() if the zone must be held for a long time.
2551a19609f8Sjv227347  */
25527c478bd9Sstevel@tonic-gate void
25537c478bd9Sstevel@tonic-gate zone_hold(zone_t *z)
25547c478bd9Sstevel@tonic-gate {
25557c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
25567c478bd9Sstevel@tonic-gate 	zone_hold_locked(z);
25577c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
25587c478bd9Sstevel@tonic-gate }
25597c478bd9Sstevel@tonic-gate 
25607c478bd9Sstevel@tonic-gate /*
25617c478bd9Sstevel@tonic-gate  * If the non-cred ref count drops to 1 and either the cred ref count
25627c478bd9Sstevel@tonic-gate  * is 0 or we aren't waiting for cred references, the zone is ready to
25637c478bd9Sstevel@tonic-gate  * be destroyed.
25647c478bd9Sstevel@tonic-gate  */
25657c478bd9Sstevel@tonic-gate #define	ZONE_IS_UNREF(zone)	((zone)->zone_ref == 1 && \
25667c478bd9Sstevel@tonic-gate 	    (!zone_wait_for_cred || (zone)->zone_cred_ref == 0))
25677c478bd9Sstevel@tonic-gate 
2568a19609f8Sjv227347 /*
2569a19609f8Sjv227347  * Common zone reference release function invoked by zone_rele() and
2570a19609f8Sjv227347  * zone_rele_ref().  If subsys is ZONE_REF_NUM_SUBSYS, then the specified
2571a19609f8Sjv227347  * zone's subsystem-specific reference counters are not affected by the
2572a19609f8Sjv227347  * release.  If ref is not NULL, then the zone_ref_t to which it refers is
2573a19609f8Sjv227347  * removed from the specified zone's reference list.  ref must be non-NULL iff
2574a19609f8Sjv227347  * subsys is not ZONE_REF_NUM_SUBSYS.
2575a19609f8Sjv227347  */
2576a19609f8Sjv227347 static void
2577a19609f8Sjv227347 zone_rele_common(zone_t *z, zone_ref_t *ref, zone_ref_subsys_t subsys)
25787c478bd9Sstevel@tonic-gate {
25797c478bd9Sstevel@tonic-gate 	boolean_t wakeup;
25807c478bd9Sstevel@tonic-gate 
25817c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
25827c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
25837c478bd9Sstevel@tonic-gate 	z->zone_ref--;
2584a19609f8Sjv227347 	if (subsys != ZONE_REF_NUM_SUBSYS) {
2585a19609f8Sjv227347 		ASSERT(z->zone_subsys_ref[subsys] != 0);
2586a19609f8Sjv227347 		z->zone_subsys_ref[subsys]--;
2587a19609f8Sjv227347 		list_remove(&z->zone_ref_list, ref);
2588a19609f8Sjv227347 	}
25897c478bd9Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
25907c478bd9Sstevel@tonic-gate 		/* no more refs, free the structure */
25917c478bd9Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
25927c478bd9Sstevel@tonic-gate 		zone_free(z);
25937c478bd9Sstevel@tonic-gate 		return;
25947c478bd9Sstevel@tonic-gate 	}
25957c478bd9Sstevel@tonic-gate 	/* signal zone_destroy so the zone can finish halting */
25967c478bd9Sstevel@tonic-gate 	wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD);
25977c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate 	if (wakeup) {
26007c478bd9Sstevel@tonic-gate 		/*
26017c478bd9Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
26027c478bd9Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
26037c478bd9Sstevel@tonic-gate 		 */
26047c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
26057c478bd9Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
26067c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
26077c478bd9Sstevel@tonic-gate 	}
26087c478bd9Sstevel@tonic-gate }
26097c478bd9Sstevel@tonic-gate 
2610a19609f8Sjv227347 /*
2611a19609f8Sjv227347  * Decrement the specified zone's reference count.  The specified zone will
2612a19609f8Sjv227347  * cease to exist after this function returns if the reference count drops to
2613a19609f8Sjv227347  * zero.  This function should be paired with zone_hold().
2614a19609f8Sjv227347  */
2615a19609f8Sjv227347 void
2616a19609f8Sjv227347 zone_rele(zone_t *z)
2617a19609f8Sjv227347 {
2618a19609f8Sjv227347 	zone_rele_common(z, NULL, ZONE_REF_NUM_SUBSYS);
2619a19609f8Sjv227347 }
2620a19609f8Sjv227347 
2621a19609f8Sjv227347 /*
2622a19609f8Sjv227347  * Initialize a zone reference structure.  This function must be invoked for
2623a19609f8Sjv227347  * a reference structure before the structure is passed to zone_hold_ref().
2624a19609f8Sjv227347  */
2625a19609f8Sjv227347 void
2626a19609f8Sjv227347 zone_init_ref(zone_ref_t *ref)
2627a19609f8Sjv227347 {
2628a19609f8Sjv227347 	ref->zref_zone = NULL;
2629a19609f8Sjv227347 	list_link_init(&ref->zref_linkage);
2630a19609f8Sjv227347 }
2631a19609f8Sjv227347 
2632a19609f8Sjv227347 /*
2633a19609f8Sjv227347  * Acquire a reference to zone z.  The caller must specify the
2634a19609f8Sjv227347  * zone_ref_subsys_t constant associated with its subsystem.  The specified
2635a19609f8Sjv227347  * zone_ref_t structure will represent a reference to the specified zone.  Use
2636a19609f8Sjv227347  * zone_rele_ref() to release the reference.
2637a19609f8Sjv227347  *
2638a19609f8Sjv227347  * The referenced zone_t structure will not be freed as long as the zone_t's
2639a19609f8Sjv227347  * zone_status field is not ZONE_IS_DEAD and the zone has outstanding
2640a19609f8Sjv227347  * references.
2641a19609f8Sjv227347  *
2642a19609f8Sjv227347  * NOTE: The zone_ref_t structure must be initialized before it is used.
2643a19609f8Sjv227347  * See zone_init_ref() above.
2644a19609f8Sjv227347  */
2645a19609f8Sjv227347 void
2646a19609f8Sjv227347 zone_hold_ref(zone_t *z, zone_ref_t *ref, zone_ref_subsys_t subsys)
2647a19609f8Sjv227347 {
2648a19609f8Sjv227347 	ASSERT(subsys >= 0 && subsys < ZONE_REF_NUM_SUBSYS);
2649a19609f8Sjv227347 
2650a19609f8Sjv227347 	/*
2651a19609f8Sjv227347 	 * Prevent consumers from reusing a reference structure before
2652a19609f8Sjv227347 	 * releasing it.
2653a19609f8Sjv227347 	 */
2654a19609f8Sjv227347 	VERIFY(ref->zref_zone == NULL);
2655a19609f8Sjv227347 
2656a19609f8Sjv227347 	ref->zref_zone = z;
2657a19609f8Sjv227347 	mutex_enter(&z->zone_lock);
2658a19609f8Sjv227347 	zone_hold_locked(z);
2659a19609f8Sjv227347 	z->zone_subsys_ref[subsys]++;
2660a19609f8Sjv227347 	ASSERT(z->zone_subsys_ref[subsys] != 0);
2661a19609f8Sjv227347 	list_insert_head(&z->zone_ref_list, ref);
2662a19609f8Sjv227347 	mutex_exit(&z->zone_lock);
2663a19609f8Sjv227347 }
2664a19609f8Sjv227347 
2665a19609f8Sjv227347 /*
2666a19609f8Sjv227347  * Release the zone reference represented by the specified zone_ref_t.
2667a19609f8Sjv227347  * The reference is invalid after it's released; however, the zone_ref_t
2668a19609f8Sjv227347  * structure can be reused without having to invoke zone_init_ref().
2669a19609f8Sjv227347  * subsys should be the same value that was passed to zone_hold_ref()
2670a19609f8Sjv227347  * when the reference was acquired.
2671a19609f8Sjv227347  */
2672a19609f8Sjv227347 void
2673a19609f8Sjv227347 zone_rele_ref(zone_ref_t *ref, zone_ref_subsys_t subsys)
2674a19609f8Sjv227347 {
2675a19609f8Sjv227347 	zone_rele_common(ref->zref_zone, ref, subsys);
2676a19609f8Sjv227347 
2677a19609f8Sjv227347 	/*
2678a19609f8Sjv227347 	 * Set the zone_ref_t's zref_zone field to NULL to generate panics
2679a19609f8Sjv227347 	 * when consumers dereference the reference.  This helps us catch
2680a19609f8Sjv227347 	 * consumers who use released references.  Furthermore, this lets
2681a19609f8Sjv227347 	 * consumers reuse the zone_ref_t structure without having to
2682a19609f8Sjv227347 	 * invoke zone_init_ref().
2683a19609f8Sjv227347 	 */
2684a19609f8Sjv227347 	ref->zref_zone = NULL;
2685a19609f8Sjv227347 }
2686a19609f8Sjv227347 
26877c478bd9Sstevel@tonic-gate void
26887c478bd9Sstevel@tonic-gate zone_cred_hold(zone_t *z)
26897c478bd9Sstevel@tonic-gate {
26907c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
26917c478bd9Sstevel@tonic-gate 	z->zone_cred_ref++;
26927c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
26937c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
26947c478bd9Sstevel@tonic-gate }
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate void
26977c478bd9Sstevel@tonic-gate zone_cred_rele(zone_t *z)
26987c478bd9Sstevel@tonic-gate {
26997c478bd9Sstevel@tonic-gate 	boolean_t wakeup;
27007c478bd9Sstevel@tonic-gate 
27017c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
27027c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
27037c478bd9Sstevel@tonic-gate 	z->zone_cred_ref--;
27047c478bd9Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
27057c478bd9Sstevel@tonic-gate 		/* no more refs, free the structure */
27067c478bd9Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
27077c478bd9Sstevel@tonic-gate 		zone_free(z);
27087c478bd9Sstevel@tonic-gate 		return;
27097c478bd9Sstevel@tonic-gate 	}
27107c478bd9Sstevel@tonic-gate 	/*
27117c478bd9Sstevel@tonic-gate 	 * If zone_destroy is waiting for the cred references to drain
27127c478bd9Sstevel@tonic-gate 	 * out, and they have, signal it.
27137c478bd9Sstevel@tonic-gate 	 */
27147c478bd9Sstevel@tonic-gate 	wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) &&
27157c478bd9Sstevel@tonic-gate 	    zone_status_get(z) >= ZONE_IS_DEAD);
27167c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate 	if (wakeup) {
27197c478bd9Sstevel@tonic-gate 		/*
27207c478bd9Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
27217c478bd9Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
27227c478bd9Sstevel@tonic-gate 		 */
27237c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
27247c478bd9Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
27257c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
27267c478bd9Sstevel@tonic-gate 	}
27277c478bd9Sstevel@tonic-gate }
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate void
27307c478bd9Sstevel@tonic-gate zone_task_hold(zone_t *z)
27317c478bd9Sstevel@tonic-gate {
27327c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
27337c478bd9Sstevel@tonic-gate 	z->zone_ntasks++;
27347c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_ntasks != 0);
27357c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
27367c478bd9Sstevel@tonic-gate }
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate void
27397c478bd9Sstevel@tonic-gate zone_task_rele(zone_t *zone)
27407c478bd9Sstevel@tonic-gate {
27417c478bd9Sstevel@tonic-gate 	uint_t refcnt;
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
27447c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks != 0);
27457c478bd9Sstevel@tonic-gate 	refcnt = --zone->zone_ntasks;
27467c478bd9Sstevel@tonic-gate 	if (refcnt > 1)	{	/* Common case */
27477c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
27487c478bd9Sstevel@tonic-gate 		return;
27497c478bd9Sstevel@tonic-gate 	}
27507c478bd9Sstevel@tonic-gate 	zone_hold_locked(zone);	/* so we can use the zone_t later */
27517c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
27527c478bd9Sstevel@tonic-gate 	if (refcnt == 1) {
27537c478bd9Sstevel@tonic-gate 		/*
27547c478bd9Sstevel@tonic-gate 		 * See if the zone is shutting down.
27557c478bd9Sstevel@tonic-gate 		 */
27567c478bd9Sstevel@tonic-gate 		mutex_enter(&zone_status_lock);
27577c478bd9Sstevel@tonic-gate 		if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) {
27587c478bd9Sstevel@tonic-gate 			goto out;
27597c478bd9Sstevel@tonic-gate 		}
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate 		/*
27627c478bd9Sstevel@tonic-gate 		 * Make sure the ntasks didn't change since we
27637c478bd9Sstevel@tonic-gate 		 * dropped zone_lock.
27647c478bd9Sstevel@tonic-gate 		 */
27657c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
27667c478bd9Sstevel@tonic-gate 		if (refcnt != zone->zone_ntasks) {
27677c478bd9Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
27687c478bd9Sstevel@tonic-gate 			goto out;
27697c478bd9Sstevel@tonic-gate 		}
27707c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
27717c478bd9Sstevel@tonic-gate 
27727c478bd9Sstevel@tonic-gate 		/*
27737c478bd9Sstevel@tonic-gate 		 * No more user processes in the zone.  The zone is empty.
27747c478bd9Sstevel@tonic-gate 		 */
27757c478bd9Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_EMPTY);
27767c478bd9Sstevel@tonic-gate 		goto out;
27777c478bd9Sstevel@tonic-gate 	}
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate 	ASSERT(refcnt == 0);
27807c478bd9Sstevel@tonic-gate 	/*
27817c478bd9Sstevel@tonic-gate 	 * zsched has exited; the zone is dead.
27827c478bd9Sstevel@tonic-gate 	 */
27837c478bd9Sstevel@tonic-gate 	zone->zone_zsched = NULL;		/* paranoia */
27847c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
27857c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_DEAD);
27867c478bd9Sstevel@tonic-gate out:
27877c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
27887c478bd9Sstevel@tonic-gate 	zone_rele(zone);
27897c478bd9Sstevel@tonic-gate }
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate zoneid_t
27927c478bd9Sstevel@tonic-gate getzoneid(void)
27937c478bd9Sstevel@tonic-gate {
27947c478bd9Sstevel@tonic-gate 	return (curproc->p_zone->zone_id);
27957c478bd9Sstevel@tonic-gate }
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate /*
27987c478bd9Sstevel@tonic-gate  * Internal versions of zone_find_by_*().  These don't zone_hold() or
27997c478bd9Sstevel@tonic-gate  * check the validity of a zone's state.
28007c478bd9Sstevel@tonic-gate  */
28017c478bd9Sstevel@tonic-gate static zone_t *
28027c478bd9Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid)
28037c478bd9Sstevel@tonic-gate {
28047c478bd9Sstevel@tonic-gate 	mod_hash_val_t hv;
28057c478bd9Sstevel@tonic-gate 	zone_t *zone = NULL;
28067c478bd9Sstevel@tonic-gate 
28077c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyid,
28107c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0)
28117c478bd9Sstevel@tonic-gate 		zone = (zone_t *)hv;
28127c478bd9Sstevel@tonic-gate 	return (zone);
28137c478bd9Sstevel@tonic-gate }
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate static zone_t *
281645916cd2Sjpk zone_find_all_by_label(const ts_label_t *label)
281745916cd2Sjpk {
281845916cd2Sjpk 	mod_hash_val_t hv;
281945916cd2Sjpk 	zone_t *zone = NULL;
282045916cd2Sjpk 
282145916cd2Sjpk 	ASSERT(MUTEX_HELD(&zonehash_lock));
282245916cd2Sjpk 
282345916cd2Sjpk 	/*
282445916cd2Sjpk 	 * zonehashbylabel is not maintained for unlabeled systems
282545916cd2Sjpk 	 */
282645916cd2Sjpk 	if (!is_system_labeled())
282745916cd2Sjpk 		return (NULL);
282845916cd2Sjpk 	if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0)
282945916cd2Sjpk 		zone = (zone_t *)hv;
283045916cd2Sjpk 	return (zone);
283145916cd2Sjpk }
283245916cd2Sjpk 
283345916cd2Sjpk static zone_t *
28347c478bd9Sstevel@tonic-gate zone_find_all_by_name(char *name)
28357c478bd9Sstevel@tonic-gate {
28367c478bd9Sstevel@tonic-gate 	mod_hash_val_t hv;
28377c478bd9Sstevel@tonic-gate 	zone_t *zone = NULL;
28387c478bd9Sstevel@tonic-gate 
28397c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
28407c478bd9Sstevel@tonic-gate 
28417c478bd9Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0)
28427c478bd9Sstevel@tonic-gate 		zone = (zone_t *)hv;
28437c478bd9Sstevel@tonic-gate 	return (zone);
28447c478bd9Sstevel@tonic-gate }
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate /*
28477c478bd9Sstevel@tonic-gate  * Public interface for looking up a zone by zoneid.  Only returns the zone if
28487c478bd9Sstevel@tonic-gate  * it is fully initialized, and has not yet begun the zone_destroy() sequence.
28497c478bd9Sstevel@tonic-gate  * Caller must call zone_rele() once it is done with the zone.
28507c478bd9Sstevel@tonic-gate  *
28517c478bd9Sstevel@tonic-gate  * The zone may begin the zone_destroy() sequence immediately after this
28527c478bd9Sstevel@tonic-gate  * function returns, but may be safely used until zone_rele() is called.
28537c478bd9Sstevel@tonic-gate  */
28547c478bd9Sstevel@tonic-gate zone_t *
28557c478bd9Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid)
28567c478bd9Sstevel@tonic-gate {
28577c478bd9Sstevel@tonic-gate 	zone_t *zone;
28587c478bd9Sstevel@tonic-gate 	zone_status_t status;
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
28617c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
28627c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
28637c478bd9Sstevel@tonic-gate 		return (NULL);
28647c478bd9Sstevel@tonic-gate 	}
28657c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
28667c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
28677c478bd9Sstevel@tonic-gate 		/*
28687c478bd9Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
28697c478bd9Sstevel@tonic-gate 		 */
28707c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
28717c478bd9Sstevel@tonic-gate 		return (NULL);
28727c478bd9Sstevel@tonic-gate 	}
28737c478bd9Sstevel@tonic-gate 	zone_hold(zone);
28747c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
28757c478bd9Sstevel@tonic-gate 	return (zone);
28767c478bd9Sstevel@tonic-gate }
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate /*
287945916cd2Sjpk  * Similar to zone_find_by_id, but using zone label as the key.
288045916cd2Sjpk  */
288145916cd2Sjpk zone_t *
288245916cd2Sjpk zone_find_by_label(const ts_label_t *label)
288345916cd2Sjpk {
288445916cd2Sjpk 	zone_t *zone;
288542bc57c4Srica 	zone_status_t status;
288645916cd2Sjpk 
288745916cd2Sjpk 	mutex_enter(&zonehash_lock);
288845916cd2Sjpk 	if ((zone = zone_find_all_by_label(label)) == NULL) {
288945916cd2Sjpk 		mutex_exit(&zonehash_lock);
289045916cd2Sjpk 		return (NULL);
289145916cd2Sjpk 	}
289242bc57c4Srica 
289342bc57c4Srica 	status = zone_status_get(zone);
289442bc57c4Srica 	if (status > ZONE_IS_DOWN) {
289545916cd2Sjpk 		/*
289645916cd2Sjpk 		 * For all practical purposes the zone doesn't exist.
289745916cd2Sjpk 		 */
289842bc57c4Srica 		mutex_exit(&zonehash_lock);
289942bc57c4Srica 		return (NULL);
290045916cd2Sjpk 	}
290142bc57c4Srica 	zone_hold(zone);
290245916cd2Sjpk 	mutex_exit(&zonehash_lock);
290345916cd2Sjpk 	return (zone);
290445916cd2Sjpk }
290545916cd2Sjpk 
290645916cd2Sjpk /*
29077c478bd9Sstevel@tonic-gate  * Similar to zone_find_by_id, but using zone name as the key.
29087c478bd9Sstevel@tonic-gate  */
29097c478bd9Sstevel@tonic-gate zone_t *
29107c478bd9Sstevel@tonic-gate zone_find_by_name(char *name)
29117c478bd9Sstevel@tonic-gate {
29127c478bd9Sstevel@tonic-gate 	zone_t *zone;
29137c478bd9Sstevel@tonic-gate 	zone_status_t status;
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
29167c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_name(name)) == NULL) {
29177c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
29187c478bd9Sstevel@tonic-gate 		return (NULL);
29197c478bd9Sstevel@tonic-gate 	}
29207c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
29217c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
29227c478bd9Sstevel@tonic-gate 		/*
29237c478bd9Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
29247c478bd9Sstevel@tonic-gate 		 */
29257c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
29267c478bd9Sstevel@tonic-gate 		return (NULL);
29277c478bd9Sstevel@tonic-gate 	}
29287c478bd9Sstevel@tonic-gate 	zone_hold(zone);
29297c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
29307c478bd9Sstevel@tonic-gate 	return (zone);
29317c478bd9Sstevel@tonic-gate }
29327c478bd9Sstevel@tonic-gate 
29337c478bd9Sstevel@tonic-gate /*
29347c478bd9Sstevel@tonic-gate  * Similar to zone_find_by_id(), using the path as a key.  For instance,
29357c478bd9Sstevel@tonic-gate  * if there is a zone "foo" rooted at /foo/root, and the path argument
29367c478bd9Sstevel@tonic-gate  * is "/foo/root/proc", it will return the held zone_t corresponding to
29377c478bd9Sstevel@tonic-gate  * zone "foo".
29387c478bd9Sstevel@tonic-gate  *
29397c478bd9Sstevel@tonic-gate  * zone_find_by_path() always returns a non-NULL value, since at the
29407c478bd9Sstevel@tonic-gate  * very least every path will be contained in the global zone.
29417c478bd9Sstevel@tonic-gate  *
29427c478bd9Sstevel@tonic-gate  * As with the other zone_find_by_*() functions, the caller is
29437c478bd9Sstevel@tonic-gate  * responsible for zone_rele()ing the return value of this function.
29447c478bd9Sstevel@tonic-gate  */
29457c478bd9Sstevel@tonic-gate zone_t *
29467c478bd9Sstevel@tonic-gate zone_find_by_path(const char *path)
29477c478bd9Sstevel@tonic-gate {
29487c478bd9Sstevel@tonic-gate 	zone_t *zone;
29497c478bd9Sstevel@tonic-gate 	zone_t *zret = NULL;
29507c478bd9Sstevel@tonic-gate 	zone_status_t status;
29517c478bd9Sstevel@tonic-gate 
29527c478bd9Sstevel@tonic-gate 	if (path == NULL) {
29537c478bd9Sstevel@tonic-gate 		/*
29547c478bd9Sstevel@tonic-gate 		 * Call from rootconf().
29557c478bd9Sstevel@tonic-gate 		 */
29567c478bd9Sstevel@tonic-gate 		zone_hold(global_zone);
29577c478bd9Sstevel@tonic-gate 		return (global_zone);
29587c478bd9Sstevel@tonic-gate 	}
29597c478bd9Sstevel@tonic-gate 	ASSERT(*path == '/');
29607c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
29617c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
29627c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
29637c478bd9Sstevel@tonic-gate 		if (ZONE_PATH_VISIBLE(path, zone))
29647c478bd9Sstevel@tonic-gate 			zret = zone;
29657c478bd9Sstevel@tonic-gate 	}
29667c478bd9Sstevel@tonic-gate 	ASSERT(zret != NULL);
29677c478bd9Sstevel@tonic-gate 	status = zone_status_get(zret);
29687c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
29697c478bd9Sstevel@tonic-gate 		/*
29707c478bd9Sstevel@tonic-gate 		 * Zone practically doesn't exist.
29717c478bd9Sstevel@tonic-gate 		 */
29727c478bd9Sstevel@tonic-gate 		zret = global_zone;
29737c478bd9Sstevel@tonic-gate 	}
29747c478bd9Sstevel@tonic-gate 	zone_hold(zret);
29757c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
29767c478bd9Sstevel@tonic-gate 	return (zret);
29777c478bd9Sstevel@tonic-gate }
29787c478bd9Sstevel@tonic-gate 
29797c478bd9Sstevel@tonic-gate /*
29807c478bd9Sstevel@tonic-gate  * Get the number of cpus visible to this zone.  The system-wide global
29817c478bd9Sstevel@tonic-gate  * 'ncpus' is returned if pools are disabled, the caller is in the
29827c478bd9Sstevel@tonic-gate  * global zone, or a NULL zone argument is passed in.
29837c478bd9Sstevel@tonic-gate  */
29847c478bd9Sstevel@tonic-gate int
29857c478bd9Sstevel@tonic-gate zone_ncpus_get(zone_t *zone)
29867c478bd9Sstevel@tonic-gate {
29877c478bd9Sstevel@tonic-gate 	int myncpus = zone == NULL ? 0 : zone->zone_ncpus;
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate 	return (myncpus != 0 ? myncpus : ncpus);
29907c478bd9Sstevel@tonic-gate }
29917c478bd9Sstevel@tonic-gate 
29927c478bd9Sstevel@tonic-gate /*
29937c478bd9Sstevel@tonic-gate  * Get the number of online cpus visible to this zone.  The system-wide
29947c478bd9Sstevel@tonic-gate  * global 'ncpus_online' is returned if pools are disabled, the caller
29957c478bd9Sstevel@tonic-gate  * is in the global zone, or a NULL zone argument is passed in.
29967c478bd9Sstevel@tonic-gate  */
29977c478bd9Sstevel@tonic-gate int
29987c478bd9Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone)
29997c478bd9Sstevel@tonic-gate {
30007c478bd9Sstevel@tonic-gate 	int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online;
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate 	return (myncpus_online != 0 ? myncpus_online : ncpus_online);
30037c478bd9Sstevel@tonic-gate }
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate /*
30067c478bd9Sstevel@tonic-gate  * Return the pool to which the zone is currently bound.
30077c478bd9Sstevel@tonic-gate  */
30087c478bd9Sstevel@tonic-gate pool_t *
30097c478bd9Sstevel@tonic-gate zone_pool_get(zone_t *zone)
30107c478bd9Sstevel@tonic-gate {
30117c478bd9Sstevel@tonic-gate 	ASSERT(pool_lock_held());
30127c478bd9Sstevel@tonic-gate 
30137c478bd9Sstevel@tonic-gate 	return (zone->zone_pool);
30147c478bd9Sstevel@tonic-gate }
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate /*
30177c478bd9Sstevel@tonic-gate  * Set the zone's pool pointer and update the zone's visibility to match
30187c478bd9Sstevel@tonic-gate  * the resources in the new pool.
30197c478bd9Sstevel@tonic-gate  */
30207c478bd9Sstevel@tonic-gate void
30217c478bd9Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool)
30227c478bd9Sstevel@tonic-gate {
30237c478bd9Sstevel@tonic-gate 	ASSERT(pool_lock_held());
30247c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
30257c478bd9Sstevel@tonic-gate 
30267c478bd9Sstevel@tonic-gate 	zone->zone_pool = pool;
30277c478bd9Sstevel@tonic-gate 	zone_pset_set(zone, pool->pool_pset->pset_id);
30287c478bd9Sstevel@tonic-gate }
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate /*
30317c478bd9Sstevel@tonic-gate  * Return the cached value of the id of the processor set to which the
30327c478bd9Sstevel@tonic-gate  * zone is currently bound.  The value will be ZONE_PS_INVAL if the pools
30337c478bd9Sstevel@tonic-gate  * facility is disabled.
30347c478bd9Sstevel@tonic-gate  */
30357c478bd9Sstevel@tonic-gate psetid_t
30367c478bd9Sstevel@tonic-gate zone_pset_get(zone_t *zone)
30377c478bd9Sstevel@tonic-gate {
30387c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
30397c478bd9Sstevel@tonic-gate 
30407c478bd9Sstevel@tonic-gate 	return (zone->zone_psetid);
30417c478bd9Sstevel@tonic-gate }
30427c478bd9Sstevel@tonic-gate 
30437c478bd9Sstevel@tonic-gate /*
30447c478bd9Sstevel@tonic-gate  * Set the cached value of the id of the processor set to which the zone
30457c478bd9Sstevel@tonic-gate  * is currently bound.  Also update the zone's visibility to match the
30467c478bd9Sstevel@tonic-gate  * resources in the new processor set.
30477c478bd9Sstevel@tonic-gate  */
30487c478bd9Sstevel@tonic-gate void
30497c478bd9Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid)
30507c478bd9Sstevel@tonic-gate {
30517c478bd9Sstevel@tonic-gate 	psetid_t oldpsetid;
30527c478bd9Sstevel@tonic-gate 
30537c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
30547c478bd9Sstevel@tonic-gate 	oldpsetid = zone_pset_get(zone);
30557c478bd9Sstevel@tonic-gate 
30567c478bd9Sstevel@tonic-gate 	if (oldpsetid == newpsetid)
30577c478bd9Sstevel@tonic-gate 		return;
30587c478bd9Sstevel@tonic-gate 	/*
30597c478bd9Sstevel@tonic-gate 	 * Global zone sees all.
30607c478bd9Sstevel@tonic-gate 	 */
30617c478bd9Sstevel@tonic-gate 	if (zone != global_zone) {
30627c478bd9Sstevel@tonic-gate 		zone->zone_psetid = newpsetid;
30637c478bd9Sstevel@tonic-gate 		if (newpsetid != ZONE_PS_INVAL)
30647c478bd9Sstevel@tonic-gate 			pool_pset_visibility_add(newpsetid, zone);
30657c478bd9Sstevel@tonic-gate 		if (oldpsetid != ZONE_PS_INVAL)
30667c478bd9Sstevel@tonic-gate 			pool_pset_visibility_remove(oldpsetid, zone);
30677c478bd9Sstevel@tonic-gate 	}
30687c478bd9Sstevel@tonic-gate 	/*
30697c478bd9Sstevel@tonic-gate 	 * Disabling pools, so we should start using the global values
30707c478bd9Sstevel@tonic-gate 	 * for ncpus and ncpus_online.
30717c478bd9Sstevel@tonic-gate 	 */
30727c478bd9Sstevel@tonic-gate 	if (newpsetid == ZONE_PS_INVAL) {
30737c478bd9Sstevel@tonic-gate 		zone->zone_ncpus = 0;
30747c478bd9Sstevel@tonic-gate 		zone->zone_ncpus_online = 0;
30757c478bd9Sstevel@tonic-gate 	}
30767c478bd9Sstevel@tonic-gate }
30777c478bd9Sstevel@tonic-gate 
30787c478bd9Sstevel@tonic-gate /*
30797c478bd9Sstevel@tonic-gate  * Walk the list of active zones and issue the provided callback for
30807c478bd9Sstevel@tonic-gate  * each of them.
30817c478bd9Sstevel@tonic-gate  *
30827c478bd9Sstevel@tonic-gate  * Caller must not be holding any locks that may be acquired under
30837c478bd9Sstevel@tonic-gate  * zonehash_lock.  See comment at the beginning of the file for a list of
30847c478bd9Sstevel@tonic-gate  * common locks and their interactions with zones.
30857c478bd9Sstevel@tonic-gate  */
30867c478bd9Sstevel@tonic-gate int
30877c478bd9Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data)
30887c478bd9Sstevel@tonic-gate {
30897c478bd9Sstevel@tonic-gate 	zone_t *zone;
30907c478bd9Sstevel@tonic-gate 	int ret = 0;
30917c478bd9Sstevel@tonic-gate 	zone_status_t status;
30927c478bd9Sstevel@tonic-gate 
30937c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
30947c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
30957c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
30967c478bd9Sstevel@tonic-gate 		/*
30977c478bd9Sstevel@tonic-gate 		 * Skip zones that shouldn't be externally visible.
30987c478bd9Sstevel@tonic-gate 		 */
30997c478bd9Sstevel@tonic-gate 		status = zone_status_get(zone);
31007c478bd9Sstevel@tonic-gate 		if (status < ZONE_IS_READY || status > ZONE_IS_DOWN)
31017c478bd9Sstevel@tonic-gate 			continue;
31027c478bd9Sstevel@tonic-gate 		/*
31037c478bd9Sstevel@tonic-gate 		 * Bail immediately if any callback invocation returns a
31047c478bd9Sstevel@tonic-gate 		 * non-zero value.
31057c478bd9Sstevel@tonic-gate 		 */
31067c478bd9Sstevel@tonic-gate 		ret = (*cb)(zone, data);
31077c478bd9Sstevel@tonic-gate 		if (ret != 0)
31087c478bd9Sstevel@tonic-gate 			break;
31097c478bd9Sstevel@tonic-gate 	}
31107c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
31117c478bd9Sstevel@tonic-gate 	return (ret);
31127c478bd9Sstevel@tonic-gate }
31137c478bd9Sstevel@tonic-gate 
31147c478bd9Sstevel@tonic-gate static int
31157c478bd9Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath)
31167c478bd9Sstevel@tonic-gate {
31177c478bd9Sstevel@tonic-gate 	vnode_t *vp;
31187c478bd9Sstevel@tonic-gate 	int trycount;
31197c478bd9Sstevel@tonic-gate 	int error = 0;
31207c478bd9Sstevel@tonic-gate 	char *path;
31217c478bd9Sstevel@tonic-gate 	struct pathname upn, pn;
31227c478bd9Sstevel@tonic-gate 	size_t pathlen;
31237c478bd9Sstevel@tonic-gate 
31247c478bd9Sstevel@tonic-gate 	if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0)
31257c478bd9Sstevel@tonic-gate 		return (error);
31267c478bd9Sstevel@tonic-gate 
31277c478bd9Sstevel@tonic-gate 	pn_alloc(&pn);
31287c478bd9Sstevel@tonic-gate 
31297c478bd9Sstevel@tonic-gate 	/* prevent infinite loop */
31307c478bd9Sstevel@tonic-gate 	trycount = 10;
31317c478bd9Sstevel@tonic-gate 	for (;;) {
31327c478bd9Sstevel@tonic-gate 		if (--trycount <= 0) {
31337c478bd9Sstevel@tonic-gate 			error = ESTALE;
31347c478bd9Sstevel@tonic-gate 			goto out;
31357c478bd9Sstevel@tonic-gate 		}
31367c478bd9Sstevel@tonic-gate 
31377c478bd9Sstevel@tonic-gate 		if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) {
31387c478bd9Sstevel@tonic-gate 			/*
31397c478bd9Sstevel@tonic-gate 			 * VOP_ACCESS() may cover 'vp' with a new
31407c478bd9Sstevel@tonic-gate 			 * filesystem, if 'vp' is an autoFS vnode.
31417c478bd9Sstevel@tonic-gate 			 * Get the new 'vp' if so.
31427c478bd9Sstevel@tonic-gate 			 */
3143da6c28aaSamw 			if ((error =
3144da6c28aaSamw 			    VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL)) == 0 &&
314525d2dc23Seh208807 			    (!vn_ismntpt(vp) ||
31467c478bd9Sstevel@tonic-gate 			    (error = traverse(&vp)) == 0)) {
31477c478bd9Sstevel@tonic-gate 				pathlen = pn.pn_pathlen + 2;
31487c478bd9Sstevel@tonic-gate 				path = kmem_alloc(pathlen, KM_SLEEP);
31497c478bd9Sstevel@tonic-gate 				(void) strncpy(path, pn.pn_path,
31507c478bd9Sstevel@tonic-gate 				    pn.pn_pathlen + 1);
31517c478bd9Sstevel@tonic-gate 				path[pathlen - 2] = '/';
31527c478bd9Sstevel@tonic-gate 				path[pathlen - 1] = '\0';
31537c478bd9Sstevel@tonic-gate 				pn_free(&pn);
31547c478bd9Sstevel@tonic-gate 				pn_free(&upn);
31557c478bd9Sstevel@tonic-gate 
31567c478bd9Sstevel@tonic-gate 				/* Success! */
31577c478bd9Sstevel@tonic-gate 				break;
31587c478bd9Sstevel@tonic-gate 			}
31597c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
31607c478bd9Sstevel@tonic-gate 		}
31617c478bd9Sstevel@tonic-gate 		if (error != ESTALE)
31627c478bd9Sstevel@tonic-gate 			goto out;
31637c478bd9Sstevel@tonic-gate 	}
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate 	ASSERT(error == 0);
31667c478bd9Sstevel@tonic-gate 	zone->zone_rootvp = vp;		/* we hold a reference to vp */
31677c478bd9Sstevel@tonic-gate 	zone->zone_rootpath = path;
31687c478bd9Sstevel@tonic-gate 	zone->zone_rootpathlen = pathlen;
316948451833Scarlsonj 	if (pathlen > 5 && strcmp(path + pathlen - 5, "/lu/") == 0)
317048451833Scarlsonj 		zone->zone_flags |= ZF_IS_SCRATCH;
31717c478bd9Sstevel@tonic-gate 	return (0);
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate out:
31747c478bd9Sstevel@tonic-gate 	pn_free(&pn);
31757c478bd9Sstevel@tonic-gate 	pn_free(&upn);
31767c478bd9Sstevel@tonic-gate 	return (error);
31777c478bd9Sstevel@tonic-gate }
31787c478bd9Sstevel@tonic-gate 
31797c478bd9Sstevel@tonic-gate #define	isalnum(c)	(((c) >= '0' && (c) <= '9') || \
31807c478bd9Sstevel@tonic-gate 			((c) >= 'a' && (c) <= 'z') || \
31817c478bd9Sstevel@tonic-gate 			((c) >= 'A' && (c) <= 'Z'))
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate static int
31847c478bd9Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname)
31857c478bd9Sstevel@tonic-gate {
31867c478bd9Sstevel@tonic-gate 	char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
31877c478bd9Sstevel@tonic-gate 	size_t len;
31887c478bd9Sstevel@tonic-gate 	int i, err;
31897c478bd9Sstevel@tonic-gate 
31907c478bd9Sstevel@tonic-gate 	if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) {
31917c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
31927c478bd9Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
31937c478bd9Sstevel@tonic-gate 	}
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate 	/* must be less than ZONENAME_MAX */
31967c478bd9Sstevel@tonic-gate 	if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') {
31977c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
31987c478bd9Sstevel@tonic-gate 		return (EINVAL);
31997c478bd9Sstevel@tonic-gate 	}
32007c478bd9Sstevel@tonic-gate 
32017c478bd9Sstevel@tonic-gate 	/*
32027c478bd9Sstevel@tonic-gate 	 * Name must start with an alphanumeric and must contain only
32037c478bd9Sstevel@tonic-gate 	 * alphanumerics, '-', '_' and '.'.
32047c478bd9Sstevel@tonic-gate 	 */
32057c478bd9Sstevel@tonic-gate 	if (!isalnum(kname[0])) {
32067c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
32077c478bd9Sstevel@tonic-gate 		return (EINVAL);
32087c478bd9Sstevel@tonic-gate 	}
32097c478bd9Sstevel@tonic-gate 	for (i = 1; i < len - 1; i++) {
32107c478bd9Sstevel@tonic-gate 		if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' &&
32117c478bd9Sstevel@tonic-gate 		    kname[i] != '.') {
32127c478bd9Sstevel@tonic-gate 			kmem_free(kname, ZONENAME_MAX);
32137c478bd9Sstevel@tonic-gate 			return (EINVAL);
32147c478bd9Sstevel@tonic-gate 		}
32157c478bd9Sstevel@tonic-gate 	}
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate 	zone->zone_name = kname;
32187c478bd9Sstevel@tonic-gate 	return (0);
32197c478bd9Sstevel@tonic-gate }
32207c478bd9Sstevel@tonic-gate 
32217c478bd9Sstevel@tonic-gate /*
32225679c89fSjv227347  * Gets the 32-bit hostid of the specified zone as an unsigned int.  If 'zonep'
32235679c89fSjv227347  * is NULL or it points to a zone with no hostid emulation, then the machine's
32245679c89fSjv227347  * hostid (i.e., the global zone's hostid) is returned.  This function returns
32255679c89fSjv227347  * zero if neither the zone nor the host machine (global zone) have hostids.  It
32265679c89fSjv227347  * returns HW_INVALID_HOSTID if the function attempts to return the machine's
32275679c89fSjv227347  * hostid and the machine's hostid is invalid.
32285679c89fSjv227347  */
32295679c89fSjv227347 uint32_t
32305679c89fSjv227347 zone_get_hostid(zone_t *zonep)
32315679c89fSjv227347 {
32325679c89fSjv227347 	unsigned long machine_hostid;
32335679c89fSjv227347 
32345679c89fSjv227347 	if (zonep == NULL || zonep->zone_hostid == HW_INVALID_HOSTID) {
32355679c89fSjv227347 		if (ddi_strtoul(hw_serial, NULL, 10, &machine_hostid) != 0)
32365679c89fSjv227347 			return (HW_INVALID_HOSTID);
32375679c89fSjv227347 		return ((uint32_t)machine_hostid);
32385679c89fSjv227347 	}
32395679c89fSjv227347 	return (zonep->zone_hostid);
32405679c89fSjv227347 }
32415679c89fSjv227347 
32425679c89fSjv227347 /*
32437c478bd9Sstevel@tonic-gate  * Similar to thread_create(), but makes sure the thread is in the appropriate
32447c478bd9Sstevel@tonic-gate  * zone's zsched process (curproc->p_zone->zone_zsched) before returning.
32457c478bd9Sstevel@tonic-gate  */
32467c478bd9Sstevel@tonic-gate /*ARGSUSED*/
32477c478bd9Sstevel@tonic-gate kthread_t *
32487c478bd9Sstevel@tonic-gate zthread_create(
32497c478bd9Sstevel@tonic-gate     caddr_t stk,
32507c478bd9Sstevel@tonic-gate     size_t stksize,
32517c478bd9Sstevel@tonic-gate     void (*proc)(),
32527c478bd9Sstevel@tonic-gate     void *arg,
32537c478bd9Sstevel@tonic-gate     size_t len,
32547c478bd9Sstevel@tonic-gate     pri_t pri)
32557c478bd9Sstevel@tonic-gate {
32567c478bd9Sstevel@tonic-gate 	kthread_t *t;
32577c478bd9Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
32587c478bd9Sstevel@tonic-gate 	proc_t *pp = zone->zone_zsched;
32597c478bd9Sstevel@tonic-gate 
32607c478bd9Sstevel@tonic-gate 	zone_hold(zone);	/* Reference to be dropped when thread exits */
32617c478bd9Sstevel@tonic-gate 
32627c478bd9Sstevel@tonic-gate 	/*
32637c478bd9Sstevel@tonic-gate 	 * No-one should be trying to create threads if the zone is shutting
32647c478bd9Sstevel@tonic-gate 	 * down and there aren't any kernel threads around.  See comment
32657c478bd9Sstevel@tonic-gate 	 * in zthread_exit().
32667c478bd9Sstevel@tonic-gate 	 */
32677c478bd9Sstevel@tonic-gate 	ASSERT(!(zone->zone_kthreads == NULL &&
32687c478bd9Sstevel@tonic-gate 	    zone_status_get(zone) >= ZONE_IS_EMPTY));
32697c478bd9Sstevel@tonic-gate 	/*
32707c478bd9Sstevel@tonic-gate 	 * Create a thread, but don't let it run until we've finished setting
32717c478bd9Sstevel@tonic-gate 	 * things up.
32727c478bd9Sstevel@tonic-gate 	 */
32737c478bd9Sstevel@tonic-gate 	t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri);
32747c478bd9Sstevel@tonic-gate 	ASSERT(t->t_forw == NULL);
32757c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
32767c478bd9Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL) {
32777c478bd9Sstevel@tonic-gate 		t->t_forw = t->t_back = t;
32787c478bd9Sstevel@tonic-gate 	} else {
32797c478bd9Sstevel@tonic-gate 		kthread_t *tx = zone->zone_kthreads;
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate 		t->t_forw = tx;
32827c478bd9Sstevel@tonic-gate 		t->t_back = tx->t_back;
32837c478bd9Sstevel@tonic-gate 		tx->t_back->t_forw = t;
32847c478bd9Sstevel@tonic-gate 		tx->t_back = t;
32857c478bd9Sstevel@tonic-gate 	}
32867c478bd9Sstevel@tonic-gate 	zone->zone_kthreads = t;
32877c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
32887c478bd9Sstevel@tonic-gate 
32897c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
32907c478bd9Sstevel@tonic-gate 	t->t_proc_flag |= TP_ZTHREAD;
32917c478bd9Sstevel@tonic-gate 	project_rele(t->t_proj);
32927c478bd9Sstevel@tonic-gate 	t->t_proj = project_hold(pp->p_task->tk_proj);
32937c478bd9Sstevel@tonic-gate 
32947c478bd9Sstevel@tonic-gate 	/*
32957c478bd9Sstevel@tonic-gate 	 * Setup complete, let it run.
32967c478bd9Sstevel@tonic-gate 	 */
32977c478bd9Sstevel@tonic-gate 	thread_lock(t);
32987c478bd9Sstevel@tonic-gate 	t->t_schedflag |= TS_ALLSTART;
32997c478bd9Sstevel@tonic-gate 	setrun_locked(t);
33007c478bd9Sstevel@tonic-gate 	thread_unlock(t);
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
33037c478bd9Sstevel@tonic-gate 
33047c478bd9Sstevel@tonic-gate 	return (t);
33057c478bd9Sstevel@tonic-gate }
33067c478bd9Sstevel@tonic-gate 
33077c478bd9Sstevel@tonic-gate /*
33087c478bd9Sstevel@tonic-gate  * Similar to thread_exit().  Must be called by threads created via
33097c478bd9Sstevel@tonic-gate  * zthread_exit().
33107c478bd9Sstevel@tonic-gate  */
33117c478bd9Sstevel@tonic-gate void
33127c478bd9Sstevel@tonic-gate zthread_exit(void)
33137c478bd9Sstevel@tonic-gate {
33147c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
33157c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
33167c478bd9Sstevel@tonic-gate 	zone_t *zone = pp->p_zone;
33177c478bd9Sstevel@tonic-gate 
33187c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
33197c478bd9Sstevel@tonic-gate 
33207c478bd9Sstevel@tonic-gate 	/*
33217c478bd9Sstevel@tonic-gate 	 * Reparent to p0
33227c478bd9Sstevel@tonic-gate 	 */
3323b4b07f87Sjosephb 	kpreempt_disable();
33247c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
33257c478bd9Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_ZTHREAD;
33267c478bd9Sstevel@tonic-gate 	t->t_procp = &p0;
33277c478bd9Sstevel@tonic-gate 	hat_thread_exit(t);
33287c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
3329b4b07f87Sjosephb 	kpreempt_enable();
33307c478bd9Sstevel@tonic-gate 
33317c478bd9Sstevel@tonic-gate 	if (t->t_back == t) {
33327c478bd9Sstevel@tonic-gate 		ASSERT(t->t_forw == t);
33337c478bd9Sstevel@tonic-gate 		/*
33347c478bd9Sstevel@tonic-gate 		 * If the zone is empty, once the thread count
33357c478bd9Sstevel@tonic-gate 		 * goes to zero no further kernel threads can be
33367c478bd9Sstevel@tonic-gate 		 * created.  This is because if the creator is a process
33377c478bd9Sstevel@tonic-gate 		 * in the zone, then it must have exited before the zone
33387c478bd9Sstevel@tonic-gate 		 * state could be set to ZONE_IS_EMPTY.
33397c478bd9Sstevel@tonic-gate 		 * Otherwise, if the creator is a kernel thread in the
33407c478bd9Sstevel@tonic-gate 		 * zone, the thread count is non-zero.
33417c478bd9Sstevel@tonic-gate 		 *
33427c478bd9Sstevel@tonic-gate 		 * This really means that non-zone kernel threads should
33437c478bd9Sstevel@tonic-gate 		 * not create zone kernel threads.
33447c478bd9Sstevel@tonic-gate 		 */
33457c478bd9Sstevel@tonic-gate 		zone->zone_kthreads = NULL;
33467c478bd9Sstevel@tonic-gate 		if (zone_status_get(zone) == ZONE_IS_EMPTY) {
33477c478bd9Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_DOWN);
3348c97ad5cdSakolb 			/*
3349c97ad5cdSakolb 			 * Remove any CPU caps on this zone.
3350c97ad5cdSakolb 			 */
3351c97ad5cdSakolb 			cpucaps_zone_remove(zone);
33527c478bd9Sstevel@tonic-gate 		}
33537c478bd9Sstevel@tonic-gate 	} else {
33547c478bd9Sstevel@tonic-gate 		t->t_forw->t_back = t->t_back;
33557c478bd9Sstevel@tonic-gate 		t->t_back->t_forw = t->t_forw;
33567c478bd9Sstevel@tonic-gate 		if (zone->zone_kthreads == t)
33577c478bd9Sstevel@tonic-gate 			zone->zone_kthreads = t->t_forw;
33587c478bd9Sstevel@tonic-gate 	}
33597c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
33607c478bd9Sstevel@tonic-gate 	zone_rele(zone);
33617c478bd9Sstevel@tonic-gate 	thread_exit();
33627c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
33637c478bd9Sstevel@tonic-gate }
33647c478bd9Sstevel@tonic-gate 
33657c478bd9Sstevel@tonic-gate static void
33667c478bd9Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp)
33677c478bd9Sstevel@tonic-gate {
33687c478bd9Sstevel@tonic-gate 	vnode_t *oldvp;
33697c478bd9Sstevel@tonic-gate 
33707c478bd9Sstevel@tonic-gate 	/* we're going to hold a reference here to the directory */
33717c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
33727c478bd9Sstevel@tonic-gate 
3373005d3febSMarek Pospisil 	/* update abs cwd/root path see c2/audit.c */
3374005d3febSMarek Pospisil 	if (AU_AUDITING())
33757c478bd9Sstevel@tonic-gate 		audit_chdirec(vp, vpp);
33767c478bd9Sstevel@tonic-gate 
33777c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
33787c478bd9Sstevel@tonic-gate 	oldvp = *vpp;
33797c478bd9Sstevel@tonic-gate 	*vpp = vp;
33807c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
33817c478bd9Sstevel@tonic-gate 	if (oldvp != NULL)
33827c478bd9Sstevel@tonic-gate 		VN_RELE(oldvp);
33837c478bd9Sstevel@tonic-gate }
33847c478bd9Sstevel@tonic-gate 
33857c478bd9Sstevel@tonic-gate /*
33867c478bd9Sstevel@tonic-gate  * Convert an rctl value represented by an nvlist_t into an rctl_val_t.
33877c478bd9Sstevel@tonic-gate  */
33887c478bd9Sstevel@tonic-gate static int
33897c478bd9Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv)
33907c478bd9Sstevel@tonic-gate {
33917c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
33927c478bd9Sstevel@tonic-gate 	boolean_t priv_set = B_FALSE;
33937c478bd9Sstevel@tonic-gate 	boolean_t limit_set = B_FALSE;
33947c478bd9Sstevel@tonic-gate 	boolean_t action_set = B_FALSE;
33957c478bd9Sstevel@tonic-gate 
33967c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
33977c478bd9Sstevel@tonic-gate 		const char *name;
33987c478bd9Sstevel@tonic-gate 		uint64_t ui64;
33997c478bd9Sstevel@tonic-gate 
34007c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
34017c478bd9Sstevel@tonic-gate 		if (nvpair_type(nvp) != DATA_TYPE_UINT64)
34027c478bd9Sstevel@tonic-gate 			return (EINVAL);
34037c478bd9Sstevel@tonic-gate 		(void) nvpair_value_uint64(nvp, &ui64);
34047c478bd9Sstevel@tonic-gate 		if (strcmp(name, "privilege") == 0) {
34057c478bd9Sstevel@tonic-gate 			/*
34067c478bd9Sstevel@tonic-gate 			 * Currently only privileged values are allowed, but
34077c478bd9Sstevel@tonic-gate 			 * this may change in the future.
34087c478bd9Sstevel@tonic-gate 			 */
34097c478bd9Sstevel@tonic-gate 			if (ui64 != RCPRIV_PRIVILEGED)
34107c478bd9Sstevel@tonic-gate 				return (EINVAL);
34117c478bd9Sstevel@tonic-gate 			rv->rcv_privilege = ui64;
34127c478bd9Sstevel@tonic-gate 			priv_set = B_TRUE;
34137c478bd9Sstevel@tonic-gate 		} else if (strcmp(name, "limit") == 0) {
34147c478bd9Sstevel@tonic-gate 			rv->rcv_value = ui64;
34157c478bd9Sstevel@tonic-gate 			limit_set = B_TRUE;
34167c478bd9Sstevel@tonic-gate 		} else if (strcmp(name, "action") == 0) {
34177c478bd9Sstevel@tonic-gate 			if (ui64 != RCTL_LOCAL_NOACTION &&
34187c478bd9Sstevel@tonic-gate 			    ui64 != RCTL_LOCAL_DENY)
34197c478bd9Sstevel@tonic-gate 				return (EINVAL);
34207c478bd9Sstevel@tonic-gate 			rv->rcv_flagaction = ui64;
34217c478bd9Sstevel@tonic-gate 			action_set = B_TRUE;
34227c478bd9Sstevel@tonic-gate 		} else {
34237c478bd9Sstevel@tonic-gate 			return (EINVAL);
34247c478bd9Sstevel@tonic-gate 		}
34257c478bd9Sstevel@tonic-gate 	}
34267c478bd9Sstevel@tonic-gate 
34277c478bd9Sstevel@tonic-gate 	if (!(priv_set && limit_set && action_set))
34287c478bd9Sstevel@tonic-gate 		return (EINVAL);
34297c478bd9Sstevel@tonic-gate 	rv->rcv_action_signal = 0;
34307c478bd9Sstevel@tonic-gate 	rv->rcv_action_recipient = NULL;
34317c478bd9Sstevel@tonic-gate 	rv->rcv_action_recip_pid = -1;
34327c478bd9Sstevel@tonic-gate 	rv->rcv_firing_time = 0;
34337c478bd9Sstevel@tonic-gate 
34347c478bd9Sstevel@tonic-gate 	return (0);
34357c478bd9Sstevel@tonic-gate }
34367c478bd9Sstevel@tonic-gate 
34373f2f09c1Sdp /*
34383f2f09c1Sdp  * Non-global zone version of start_init.
34393f2f09c1Sdp  */
34407c478bd9Sstevel@tonic-gate void
34413f2f09c1Sdp zone_start_init(void)
34427c478bd9Sstevel@tonic-gate {
34437c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
34449acbbeafSnn35248 	zone_t *z = p->p_zone;
34453f2f09c1Sdp 
34463f2f09c1Sdp 	ASSERT(!INGLOBALZONE(curproc));
34477c478bd9Sstevel@tonic-gate 
34487c478bd9Sstevel@tonic-gate 	/*
34499acbbeafSnn35248 	 * For all purposes (ZONE_ATTR_INITPID and restart_init),
34509acbbeafSnn35248 	 * storing just the pid of init is sufficient.
34519acbbeafSnn35248 	 */
34529acbbeafSnn35248 	z->zone_proc_initpid = p->p_pid;
34539acbbeafSnn35248 
34549acbbeafSnn35248 	/*
34553f2f09c1Sdp 	 * We maintain zone_boot_err so that we can return the cause of the
34563f2f09c1Sdp 	 * failure back to the caller of the zone_boot syscall.
34577c478bd9Sstevel@tonic-gate 	 */
34583f2f09c1Sdp 	p->p_zone->zone_boot_err = start_init_common();
34597c478bd9Sstevel@tonic-gate 
34608f983ab3Sjv227347 	/*
34618f983ab3Sjv227347 	 * We will prevent booting zones from becoming running zones if the
34628f983ab3Sjv227347 	 * global zone is shutting down.
34638f983ab3Sjv227347 	 */
34647c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
34658f983ab3Sjv227347 	if (z->zone_boot_err != 0 || zone_status_get(global_zone) >=
34668f983ab3Sjv227347 	    ZONE_IS_SHUTTING_DOWN) {
34677c478bd9Sstevel@tonic-gate 		/*
34687c478bd9Sstevel@tonic-gate 		 * Make sure we are still in the booting state-- we could have
34697c478bd9Sstevel@tonic-gate 		 * raced and already be shutting down, or even further along.
34707c478bd9Sstevel@tonic-gate 		 */
3471c97ad5cdSakolb 		if (zone_status_get(z) == ZONE_IS_BOOTING) {
34729acbbeafSnn35248 			zone_status_set(z, ZONE_IS_SHUTTING_DOWN);
3473c97ad5cdSakolb 		}
34747c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
34757c478bd9Sstevel@tonic-gate 		/* It's gone bad, dispose of the process */
34769acbbeafSnn35248 		if (proc_exit(CLD_EXITED, z->zone_boot_err) != 0) {
347797eda132Sraf 			mutex_enter(&p->p_lock);
347897eda132Sraf 			ASSERT(p->p_flag & SEXITLWPS);
34797c478bd9Sstevel@tonic-gate 			lwp_exit();
34807c478bd9Sstevel@tonic-gate 		}
34817c478bd9Sstevel@tonic-gate 	} else {
34829acbbeafSnn35248 		if (zone_status_get(z) == ZONE_IS_BOOTING)
34839acbbeafSnn35248 			zone_status_set(z, ZONE_IS_RUNNING);
34847c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
34857c478bd9Sstevel@tonic-gate 		/* cause the process to return to userland. */
34867c478bd9Sstevel@tonic-gate 		lwp_rtt();
34877c478bd9Sstevel@tonic-gate 	}
34887c478bd9Sstevel@tonic-gate }
34897c478bd9Sstevel@tonic-gate 
34907c478bd9Sstevel@tonic-gate struct zsched_arg {
34917c478bd9Sstevel@tonic-gate 	zone_t *zone;
34927c478bd9Sstevel@tonic-gate 	nvlist_t *nvlist;
34937c478bd9Sstevel@tonic-gate };
34947c478bd9Sstevel@tonic-gate 
34957c478bd9Sstevel@tonic-gate /*
34967c478bd9Sstevel@tonic-gate  * Per-zone "sched" workalike.  The similarity to "sched" doesn't have
34977c478bd9Sstevel@tonic-gate  * anything to do with scheduling, but rather with the fact that
34987c478bd9Sstevel@tonic-gate  * per-zone kernel threads are parented to zsched, just like regular
34997c478bd9Sstevel@tonic-gate  * kernel threads are parented to sched (p0).
35007c478bd9Sstevel@tonic-gate  *
35017c478bd9Sstevel@tonic-gate  * zsched is also responsible for launching init for the zone.
35027c478bd9Sstevel@tonic-gate  */
35037c478bd9Sstevel@tonic-gate static void
35047c478bd9Sstevel@tonic-gate zsched(void *arg)
35057c478bd9Sstevel@tonic-gate {
35067c478bd9Sstevel@tonic-gate 	struct zsched_arg *za = arg;
35077c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
35087c478bd9Sstevel@tonic-gate 	proc_t *initp = proc_init;
35097c478bd9Sstevel@tonic-gate 	zone_t *zone = za->zone;
35107c478bd9Sstevel@tonic-gate 	cred_t *cr, *oldcred;
35117c478bd9Sstevel@tonic-gate 	rctl_set_t *set;
35127c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
35137c478bd9Sstevel@tonic-gate 	contract_t *ct = NULL;
35147c478bd9Sstevel@tonic-gate 	task_t *tk, *oldtk;
35157c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
35167c478bd9Sstevel@tonic-gate 	kproject_t *pj;
35177c478bd9Sstevel@tonic-gate 
35187c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = za->nvlist;
35197c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
35207c478bd9Sstevel@tonic-gate 
3521ae115bc7Smrj 	bcopy("zsched", PTOU(pp)->u_psargs, sizeof ("zsched"));
3522ae115bc7Smrj 	bcopy("zsched", PTOU(pp)->u_comm, sizeof ("zsched"));
3523ae115bc7Smrj 	PTOU(pp)->u_argc = 0;
3524ae115bc7Smrj 	PTOU(pp)->u_argv = NULL;
3525ae115bc7Smrj 	PTOU(pp)->u_envp = NULL;
35267c478bd9Sstevel@tonic-gate 	closeall(P_FINFO(pp));
35277c478bd9Sstevel@tonic-gate 
35287c478bd9Sstevel@tonic-gate 	/*
35297c478bd9Sstevel@tonic-gate 	 * We are this zone's "zsched" process.  As the zone isn't generally
35307c478bd9Sstevel@tonic-gate 	 * visible yet we don't need to grab any locks before initializing its
35317c478bd9Sstevel@tonic-gate 	 * zone_proc pointer.
35327c478bd9Sstevel@tonic-gate 	 */
35337c478bd9Sstevel@tonic-gate 	zone_hold(zone);  /* this hold is released by zone_destroy() */
35347c478bd9Sstevel@tonic-gate 	zone->zone_zsched = pp;
35357c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
35367c478bd9Sstevel@tonic-gate 	pp->p_zone = zone;
35377c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
35387c478bd9Sstevel@tonic-gate 
35397c478bd9Sstevel@tonic-gate 	/*
35407c478bd9Sstevel@tonic-gate 	 * Disassociate process from its 'parent'; parent ourselves to init
35417c478bd9Sstevel@tonic-gate 	 * (pid 1) and change other values as needed.
35427c478bd9Sstevel@tonic-gate 	 */
35437c478bd9Sstevel@tonic-gate 	sess_create();
35447c478bd9Sstevel@tonic-gate 
35457c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
35467c478bd9Sstevel@tonic-gate 	proc_detach(pp);
35477c478bd9Sstevel@tonic-gate 	pp->p_ppid = 1;
35487c478bd9Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
35497c478bd9Sstevel@tonic-gate 	pp->p_ancpid = 1;
35507c478bd9Sstevel@tonic-gate 	pp->p_parent = initp;
35517c478bd9Sstevel@tonic-gate 	pp->p_psibling = NULL;
35527c478bd9Sstevel@tonic-gate 	if (initp->p_child)
35537c478bd9Sstevel@tonic-gate 		initp->p_child->p_psibling = pp;
35547c478bd9Sstevel@tonic-gate 	pp->p_sibling = initp->p_child;
35557c478bd9Sstevel@tonic-gate 	initp->p_child = pp;
35567c478bd9Sstevel@tonic-gate 
35577c478bd9Sstevel@tonic-gate 	/* Decrement what newproc() incremented. */
35587c478bd9Sstevel@tonic-gate 	upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID);
35597c478bd9Sstevel@tonic-gate 	/*
35607c478bd9Sstevel@tonic-gate 	 * Our credentials are about to become kcred-like, so we don't care
35617c478bd9Sstevel@tonic-gate 	 * about the caller's ruid.
35627c478bd9Sstevel@tonic-gate 	 */
35637c478bd9Sstevel@tonic-gate 	upcount_inc(crgetruid(kcred), zone->zone_id);
35647c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
35657c478bd9Sstevel@tonic-gate 
35667c478bd9Sstevel@tonic-gate 	/*
3567ff19e029SMenno Lageman 	 * getting out of global zone, so decrement lwp and process counts
35687c478bd9Sstevel@tonic-gate 	 */
35697c478bd9Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
35707c478bd9Sstevel@tonic-gate 	mutex_enter(&global_zone->zone_nlwps_lock);
35717c478bd9Sstevel@tonic-gate 	pj->kpj_nlwps -= pp->p_lwpcnt;
35727c478bd9Sstevel@tonic-gate 	global_zone->zone_nlwps -= pp->p_lwpcnt;
3573ff19e029SMenno Lageman 	pj->kpj_nprocs--;
3574ff19e029SMenno Lageman 	global_zone->zone_nprocs--;
35757c478bd9Sstevel@tonic-gate 	mutex_exit(&global_zone->zone_nlwps_lock);
35767c478bd9Sstevel@tonic-gate 
35777c478bd9Sstevel@tonic-gate 	/*
3578c6939658Ssl108498 	 * Decrement locked memory counts on old zone and project.
3579c6939658Ssl108498 	 */
35800209230bSgjelinek 	mutex_enter(&global_zone->zone_mem_lock);
3581c6939658Ssl108498 	global_zone->zone_locked_mem -= pp->p_locked_mem;
3582c6939658Ssl108498 	pj->kpj_data.kpd_locked_mem -= pp->p_locked_mem;
35830209230bSgjelinek 	mutex_exit(&global_zone->zone_mem_lock);
3584c6939658Ssl108498 
3585c6939658Ssl108498 	/*
35867c478bd9Sstevel@tonic-gate 	 * Create and join a new task in project '0' of this zone.
35877c478bd9Sstevel@tonic-gate 	 *
35887c478bd9Sstevel@tonic-gate 	 * We don't need to call holdlwps() since we know we're the only lwp in
35897c478bd9Sstevel@tonic-gate 	 * this process.
35907c478bd9Sstevel@tonic-gate 	 *
35917c478bd9Sstevel@tonic-gate 	 * task_join() returns with p_lock held.
35927c478bd9Sstevel@tonic-gate 	 */
35937c478bd9Sstevel@tonic-gate 	tk = task_create(0, zone);
35947c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
35957c478bd9Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
3596c6939658Ssl108498 
3597c6939658Ssl108498 	pj = pp->p_task->tk_proj;
3598c6939658Ssl108498 
35990209230bSgjelinek 	mutex_enter(&zone->zone_mem_lock);
3600c6939658Ssl108498 	zone->zone_locked_mem += pp->p_locked_mem;
3601c6939658Ssl108498 	pj->kpj_data.kpd_locked_mem += pp->p_locked_mem;
36020209230bSgjelinek 	mutex_exit(&zone->zone_mem_lock);
36037c478bd9Sstevel@tonic-gate 
36047c478bd9Sstevel@tonic-gate 	/*
3605ff19e029SMenno Lageman 	 * add lwp and process counts to zsched's zone, and increment
3606ff19e029SMenno Lageman 	 * project's task and process count due to the task created in
3607ff19e029SMenno Lageman 	 * the above task_create.
36087c478bd9Sstevel@tonic-gate 	 */
36097c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
36107c478bd9Sstevel@tonic-gate 	pj->kpj_nlwps += pp->p_lwpcnt;
36117c478bd9Sstevel@tonic-gate 	pj->kpj_ntasks += 1;
36127c478bd9Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
3613ff19e029SMenno Lageman 	pj->kpj_nprocs++;
3614ff19e029SMenno Lageman 	zone->zone_nprocs++;
36157c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
36167c478bd9Sstevel@tonic-gate 
3617c6939658Ssl108498 	mutex_exit(&curproc->p_lock);
3618c6939658Ssl108498 	mutex_exit(&cpu_lock);
3619c6939658Ssl108498 	task_rele(oldtk);
3620c6939658Ssl108498 
36217c478bd9Sstevel@tonic-gate 	/*
36227c478bd9Sstevel@tonic-gate 	 * The process was created by a process in the global zone, hence the
36237c478bd9Sstevel@tonic-gate 	 * credentials are wrong.  We might as well have kcred-ish credentials.
36247c478bd9Sstevel@tonic-gate 	 */
36257c478bd9Sstevel@tonic-gate 	cr = zone->zone_kcred;
36267c478bd9Sstevel@tonic-gate 	crhold(cr);
36277c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
36287c478bd9Sstevel@tonic-gate 	oldcred = pp->p_cred;
36297c478bd9Sstevel@tonic-gate 	pp->p_cred = cr;
36307c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
36317c478bd9Sstevel@tonic-gate 	crfree(oldcred);
36327c478bd9Sstevel@tonic-gate 
36337c478bd9Sstevel@tonic-gate 	/*
36347c478bd9Sstevel@tonic-gate 	 * Hold credentials again (for thread)
36357c478bd9Sstevel@tonic-gate 	 */
36367c478bd9Sstevel@tonic-gate 	crhold(cr);
36377c478bd9Sstevel@tonic-gate 
36387c478bd9Sstevel@tonic-gate 	/*
36397c478bd9Sstevel@tonic-gate 	 * p_lwpcnt can't change since this is a kernel process.
36407c478bd9Sstevel@tonic-gate 	 */
36417c478bd9Sstevel@tonic-gate 	crset(pp, cr);
36427c478bd9Sstevel@tonic-gate 
36437c478bd9Sstevel@tonic-gate 	/*
36447c478bd9Sstevel@tonic-gate 	 * Chroot
36457c478bd9Sstevel@tonic-gate 	 */
36467c478bd9Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp);
36477c478bd9Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp);
36487c478bd9Sstevel@tonic-gate 
36497c478bd9Sstevel@tonic-gate 	/*
36507c478bd9Sstevel@tonic-gate 	 * Initialize zone's rctl set.
36517c478bd9Sstevel@tonic-gate 	 */
36527c478bd9Sstevel@tonic-gate 	set = rctl_set_create();
36537c478bd9Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
36547c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
36557c478bd9Sstevel@tonic-gate 	e.rcep_p.zone = zone;
36567c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
36577c478bd9Sstevel@tonic-gate 	zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp);
36587c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
36597c478bd9Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
36607c478bd9Sstevel@tonic-gate 
36617c478bd9Sstevel@tonic-gate 	/*
36627c478bd9Sstevel@tonic-gate 	 * Apply the rctls passed in to zone_create().  This is basically a list
36637c478bd9Sstevel@tonic-gate 	 * assignment: all of the old values are removed and the new ones
36647c478bd9Sstevel@tonic-gate 	 * inserted.  That is, if an empty list is passed in, all values are
36657c478bd9Sstevel@tonic-gate 	 * removed.
36667c478bd9Sstevel@tonic-gate 	 */
36677c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
36687c478bd9Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
36697c478bd9Sstevel@tonic-gate 		rctl_hndl_t hndl;
36707c478bd9Sstevel@tonic-gate 		char *name;
36717c478bd9Sstevel@tonic-gate 		nvlist_t **nvlarray;
36727c478bd9Sstevel@tonic-gate 		uint_t i, nelem;
36737c478bd9Sstevel@tonic-gate 		int error;	/* For ASSERT()s */
36747c478bd9Sstevel@tonic-gate 
36757c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
36767c478bd9Sstevel@tonic-gate 		hndl = rctl_hndl_lookup(name);
36777c478bd9Sstevel@tonic-gate 		ASSERT(hndl != -1);
36787c478bd9Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
36797c478bd9Sstevel@tonic-gate 		ASSERT(rde != NULL);
36807c478bd9Sstevel@tonic-gate 
36817c478bd9Sstevel@tonic-gate 		for (; /* ever */; ) {
36827c478bd9Sstevel@tonic-gate 			rctl_val_t oval;
36837c478bd9Sstevel@tonic-gate 
36847c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
36857c478bd9Sstevel@tonic-gate 			error = rctl_local_get(hndl, NULL, &oval, pp);
36867c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
36877c478bd9Sstevel@tonic-gate 			ASSERT(error == 0);	/* Can't fail for RCTL_FIRST */
36887c478bd9Sstevel@tonic-gate 			ASSERT(oval.rcv_privilege != RCPRIV_BASIC);
36897c478bd9Sstevel@tonic-gate 			if (oval.rcv_privilege == RCPRIV_SYSTEM)
36907c478bd9Sstevel@tonic-gate 				break;
36917c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
36927c478bd9Sstevel@tonic-gate 			error = rctl_local_delete(hndl, &oval, pp);
36937c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
36947c478bd9Sstevel@tonic-gate 			ASSERT(error == 0);
36957c478bd9Sstevel@tonic-gate 		}
36967c478bd9Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
36977c478bd9Sstevel@tonic-gate 		ASSERT(error == 0);
36987c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
36997c478bd9Sstevel@tonic-gate 			rctl_val_t *nvalp;
37007c478bd9Sstevel@tonic-gate 
37017c478bd9Sstevel@tonic-gate 			nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
37027c478bd9Sstevel@tonic-gate 			error = nvlist2rctlval(nvlarray[i], nvalp);
37037c478bd9Sstevel@tonic-gate 			ASSERT(error == 0);
37047c478bd9Sstevel@tonic-gate 			/*
37057c478bd9Sstevel@tonic-gate 			 * rctl_local_insert can fail if the value being
37067c478bd9Sstevel@tonic-gate 			 * inserted is a duplicate; this is OK.
37077c478bd9Sstevel@tonic-gate 			 */
37087c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
37097c478bd9Sstevel@tonic-gate 			if (rctl_local_insert(hndl, nvalp, pp) != 0)
37107c478bd9Sstevel@tonic-gate 				kmem_cache_free(rctl_val_cache, nvalp);
37117c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
37127c478bd9Sstevel@tonic-gate 		}
37137c478bd9Sstevel@tonic-gate 	}
37147c478bd9Sstevel@tonic-gate 	/*
37157c478bd9Sstevel@tonic-gate 	 * Tell the world that we're done setting up.
37167c478bd9Sstevel@tonic-gate 	 *
3717bd41d0a8Snordmark 	 * At this point we want to set the zone status to ZONE_IS_INITIALIZED
37187c478bd9Sstevel@tonic-gate 	 * and atomically set the zone's processor set visibility.  Once
37197c478bd9Sstevel@tonic-gate 	 * we drop pool_lock() this zone will automatically get updated
37207c478bd9Sstevel@tonic-gate 	 * to reflect any future changes to the pools configuration.
3721bd41d0a8Snordmark 	 *
3722bd41d0a8Snordmark 	 * Note that after we drop the locks below (zonehash_lock in
3723bd41d0a8Snordmark 	 * particular) other operations such as a zone_getattr call can
3724bd41d0a8Snordmark 	 * now proceed and observe the zone. That is the reason for doing a
3725bd41d0a8Snordmark 	 * state transition to the INITIALIZED state.
37267c478bd9Sstevel@tonic-gate 	 */
37277c478bd9Sstevel@tonic-gate 	pool_lock();
37287c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
37297c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
37307c478bd9Sstevel@tonic-gate 	zone_uniqid(zone);
37317c478bd9Sstevel@tonic-gate 	zone_zsd_configure(zone);
37327c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED)
37337c478bd9Sstevel@tonic-gate 		zone_pset_set(zone, pool_default->pool_pset->pset_id);
37347c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
37357c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
3736bd41d0a8Snordmark 	zone_status_set(zone, ZONE_IS_INITIALIZED);
37377c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
37387c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
37397c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
37407c478bd9Sstevel@tonic-gate 	pool_unlock();
37417c478bd9Sstevel@tonic-gate 
3742bd41d0a8Snordmark 	/* Now call the create callback for this key */
3743bd41d0a8Snordmark 	zsd_apply_all_keys(zsd_apply_create, zone);
3744bd41d0a8Snordmark 
3745bd41d0a8Snordmark 	/* The callbacks are complete. Mark ZONE_IS_READY */
3746bd41d0a8Snordmark 	mutex_enter(&zone_status_lock);
3747bd41d0a8Snordmark 	ASSERT(zone_status_get(zone) == ZONE_IS_INITIALIZED);
3748bd41d0a8Snordmark 	zone_status_set(zone, ZONE_IS_READY);
3749bd41d0a8Snordmark 	mutex_exit(&zone_status_lock);
3750bd41d0a8Snordmark 
37517c478bd9Sstevel@tonic-gate 	/*
37527c478bd9Sstevel@tonic-gate 	 * Once we see the zone transition to the ZONE_IS_BOOTING state,
37537c478bd9Sstevel@tonic-gate 	 * we launch init, and set the state to running.
37547c478bd9Sstevel@tonic-gate 	 */
37557c478bd9Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched");
37567c478bd9Sstevel@tonic-gate 
37577c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_BOOTING) {
37587c478bd9Sstevel@tonic-gate 		id_t cid;
37597c478bd9Sstevel@tonic-gate 
37607c478bd9Sstevel@tonic-gate 		/*
37617c478bd9Sstevel@tonic-gate 		 * Ok, this is a little complicated.  We need to grab the
37627c478bd9Sstevel@tonic-gate 		 * zone's pool's scheduling class ID; note that by now, we
37637c478bd9Sstevel@tonic-gate 		 * are already bound to a pool if we need to be (zoneadmd
37647c478bd9Sstevel@tonic-gate 		 * will have done that to us while we're in the READY
37657c478bd9Sstevel@tonic-gate 		 * state).  *But* the scheduling class for the zone's 'init'
37667c478bd9Sstevel@tonic-gate 		 * must be explicitly passed to newproc, which doesn't
37677c478bd9Sstevel@tonic-gate 		 * respect pool bindings.
37687c478bd9Sstevel@tonic-gate 		 *
37697c478bd9Sstevel@tonic-gate 		 * We hold the pool_lock across the call to newproc() to
37707c478bd9Sstevel@tonic-gate 		 * close the obvious race: the pool's scheduling class
37717c478bd9Sstevel@tonic-gate 		 * could change before we manage to create the LWP with
37727c478bd9Sstevel@tonic-gate 		 * classid 'cid'.
37737c478bd9Sstevel@tonic-gate 		 */
37747c478bd9Sstevel@tonic-gate 		pool_lock();
37750209230bSgjelinek 		if (zone->zone_defaultcid > 0)
37760209230bSgjelinek 			cid = zone->zone_defaultcid;
37770209230bSgjelinek 		else
37787c478bd9Sstevel@tonic-gate 			cid = pool_get_class(zone->zone_pool);
37797c478bd9Sstevel@tonic-gate 		if (cid == -1)
37807c478bd9Sstevel@tonic-gate 			cid = defaultcid;
37817c478bd9Sstevel@tonic-gate 
37827c478bd9Sstevel@tonic-gate 		/*
37837c478bd9Sstevel@tonic-gate 		 * If this fails, zone_boot will ultimately fail.  The
37847c478bd9Sstevel@tonic-gate 		 * state of the zone will be set to SHUTTING_DOWN-- userland
37857c478bd9Sstevel@tonic-gate 		 * will have to tear down the zone, and fail, or try again.
37867c478bd9Sstevel@tonic-gate 		 */
37873f2f09c1Sdp 		if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid,
378835a5a358SJonathan Adams 		    minclsyspri - 1, &ct, 0)) != 0) {
37897c478bd9Sstevel@tonic-gate 			mutex_enter(&zone_status_lock);
37907c478bd9Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
37917c478bd9Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
3792*86ad481cSJerry Jelinek 		} else {
3793*86ad481cSJerry Jelinek 			zone->zone_boot_time = gethrestime_sec();
37947c478bd9Sstevel@tonic-gate 		}
3795*86ad481cSJerry Jelinek 
37967c478bd9Sstevel@tonic-gate 		pool_unlock();
37977c478bd9Sstevel@tonic-gate 	}
37987c478bd9Sstevel@tonic-gate 
37997c478bd9Sstevel@tonic-gate 	/*
38007c478bd9Sstevel@tonic-gate 	 * Wait for zone_destroy() to be called.  This is what we spend
38017c478bd9Sstevel@tonic-gate 	 * most of our life doing.
38027c478bd9Sstevel@tonic-gate 	 */
38037c478bd9Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched");
38047c478bd9Sstevel@tonic-gate 
38057c478bd9Sstevel@tonic-gate 	if (ct)
38067c478bd9Sstevel@tonic-gate 		/*
38077c478bd9Sstevel@tonic-gate 		 * At this point the process contract should be empty.
38087c478bd9Sstevel@tonic-gate 		 * (Though if it isn't, it's not the end of the world.)
38097c478bd9Sstevel@tonic-gate 		 */
38107c478bd9Sstevel@tonic-gate 		VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0);
38117c478bd9Sstevel@tonic-gate 
38127c478bd9Sstevel@tonic-gate 	/*
38137c478bd9Sstevel@tonic-gate 	 * Allow kcred to be freed when all referring processes
38147c478bd9Sstevel@tonic-gate 	 * (including this one) go away.  We can't just do this in
38157c478bd9Sstevel@tonic-gate 	 * zone_free because we need to wait for the zone_cred_ref to
38167c478bd9Sstevel@tonic-gate 	 * drop to 0 before calling zone_free, and the existence of
38177c478bd9Sstevel@tonic-gate 	 * zone_kcred will prevent that.  Thus, we call crfree here to
38187c478bd9Sstevel@tonic-gate 	 * balance the crdup in zone_create.  The crhold calls earlier
38197c478bd9Sstevel@tonic-gate 	 * in zsched will be dropped when the thread and process exit.
38207c478bd9Sstevel@tonic-gate 	 */
38217c478bd9Sstevel@tonic-gate 	crfree(zone->zone_kcred);
38227c478bd9Sstevel@tonic-gate 	zone->zone_kcred = NULL;
38237c478bd9Sstevel@tonic-gate 
38247c478bd9Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
38257c478bd9Sstevel@tonic-gate }
38267c478bd9Sstevel@tonic-gate 
38277c478bd9Sstevel@tonic-gate /*
38287c478bd9Sstevel@tonic-gate  * Helper function to determine if there are any submounts of the
38297c478bd9Sstevel@tonic-gate  * provided path.  Used to make sure the zone doesn't "inherit" any
38307c478bd9Sstevel@tonic-gate  * mounts from before it is created.
38317c478bd9Sstevel@tonic-gate  */
38327c478bd9Sstevel@tonic-gate static uint_t
38337c478bd9Sstevel@tonic-gate zone_mount_count(const char *rootpath)
38347c478bd9Sstevel@tonic-gate {
38357c478bd9Sstevel@tonic-gate 	vfs_t *vfsp;
38367c478bd9Sstevel@tonic-gate 	uint_t count = 0;
38377c478bd9Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
38387c478bd9Sstevel@tonic-gate 
38397c478bd9Sstevel@tonic-gate 	/*
38407c478bd9Sstevel@tonic-gate 	 * Holding zonehash_lock prevents race conditions with
38417c478bd9Sstevel@tonic-gate 	 * vfs_list_add()/vfs_list_remove() since we serialize with
38427c478bd9Sstevel@tonic-gate 	 * zone_find_by_path().
38437c478bd9Sstevel@tonic-gate 	 */
38447c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
38457c478bd9Sstevel@tonic-gate 	/*
38467c478bd9Sstevel@tonic-gate 	 * The rootpath must end with a '/'
38477c478bd9Sstevel@tonic-gate 	 */
38487c478bd9Sstevel@tonic-gate 	ASSERT(rootpath[rootpathlen - 1] == '/');
38497c478bd9Sstevel@tonic-gate 
38507c478bd9Sstevel@tonic-gate 	/*
38517c478bd9Sstevel@tonic-gate 	 * This intentionally does not count the rootpath itself if that
38527c478bd9Sstevel@tonic-gate 	 * happens to be a mount point.
38537c478bd9Sstevel@tonic-gate 	 */
38547c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
38557c478bd9Sstevel@tonic-gate 	vfsp = rootvfs;
38567c478bd9Sstevel@tonic-gate 	do {
38577c478bd9Sstevel@tonic-gate 		if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt),
38587c478bd9Sstevel@tonic-gate 		    rootpathlen) == 0)
38597c478bd9Sstevel@tonic-gate 			count++;
38607c478bd9Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
38617c478bd9Sstevel@tonic-gate 	} while (vfsp != rootvfs);
38627c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
38637c478bd9Sstevel@tonic-gate 	return (count);
38647c478bd9Sstevel@tonic-gate }
38657c478bd9Sstevel@tonic-gate 
38667c478bd9Sstevel@tonic-gate /*
38677c478bd9Sstevel@tonic-gate  * Helper function to make sure that a zone created on 'rootpath'
38687c478bd9Sstevel@tonic-gate  * wouldn't end up containing other zones' rootpaths.
38697c478bd9Sstevel@tonic-gate  */
38707c478bd9Sstevel@tonic-gate static boolean_t
38717c478bd9Sstevel@tonic-gate zone_is_nested(const char *rootpath)
38727c478bd9Sstevel@tonic-gate {
38737c478bd9Sstevel@tonic-gate 	zone_t *zone;
38747c478bd9Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
38757c478bd9Sstevel@tonic-gate 	size_t len;
38767c478bd9Sstevel@tonic-gate 
38777c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
38787c478bd9Sstevel@tonic-gate 
38792bb88978SDhanaraj M 	/*
38802bb88978SDhanaraj M 	 * zone_set_root() appended '/' and '\0' at the end of rootpath
38812bb88978SDhanaraj M 	 */
38822bb88978SDhanaraj M 	if ((rootpathlen <= 3) && (rootpath[0] == '/') &&
38832bb88978SDhanaraj M 	    (rootpath[1] == '/') && (rootpath[2] == '\0'))
38842bb88978SDhanaraj M 		return (B_TRUE);
38852bb88978SDhanaraj M 
38867c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
38877c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
38887c478bd9Sstevel@tonic-gate 		if (zone == global_zone)
38897c478bd9Sstevel@tonic-gate 			continue;
38907c478bd9Sstevel@tonic-gate 		len = strlen(zone->zone_rootpath);
38917c478bd9Sstevel@tonic-gate 		if (strncmp(rootpath, zone->zone_rootpath,
38927c478bd9Sstevel@tonic-gate 		    MIN(rootpathlen, len)) == 0)
38937c478bd9Sstevel@tonic-gate 			return (B_TRUE);
38947c478bd9Sstevel@tonic-gate 	}
38957c478bd9Sstevel@tonic-gate 	return (B_FALSE);
38967c478bd9Sstevel@tonic-gate }
38977c478bd9Sstevel@tonic-gate 
38987c478bd9Sstevel@tonic-gate static int
3899821c4a97Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs,
3900821c4a97Sdp     size_t zone_privssz)
39017c478bd9Sstevel@tonic-gate {
39021a88ce5cSDan Price 	priv_set_t *privs;
39037c478bd9Sstevel@tonic-gate 
3904821c4a97Sdp 	if (zone_privssz < sizeof (priv_set_t))
39051a88ce5cSDan Price 		return (ENOMEM);
39061a88ce5cSDan Price 
39071a88ce5cSDan Price 	privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
3908821c4a97Sdp 
39097c478bd9Sstevel@tonic-gate 	if (copyin(zone_privs, privs, sizeof (priv_set_t))) {
39107c478bd9Sstevel@tonic-gate 		kmem_free(privs, sizeof (priv_set_t));
39117c478bd9Sstevel@tonic-gate 		return (EFAULT);
39127c478bd9Sstevel@tonic-gate 	}
39137c478bd9Sstevel@tonic-gate 
39147c478bd9Sstevel@tonic-gate 	zone->zone_privset = privs;
39157c478bd9Sstevel@tonic-gate 	return (0);
39167c478bd9Sstevel@tonic-gate }
39177c478bd9Sstevel@tonic-gate 
39187c478bd9Sstevel@tonic-gate /*
39197c478bd9Sstevel@tonic-gate  * We make creative use of nvlists to pass in rctls from userland.  The list is
39207c478bd9Sstevel@tonic-gate  * a list of the following structures:
39217c478bd9Sstevel@tonic-gate  *
39227c478bd9Sstevel@tonic-gate  * (name = rctl_name, value = nvpair_list_array)
39237c478bd9Sstevel@tonic-gate  *
39247c478bd9Sstevel@tonic-gate  * Where each element of the nvpair_list_array is of the form:
39257c478bd9Sstevel@tonic-gate  *
39267c478bd9Sstevel@tonic-gate  * [(name = "privilege", value = RCPRIV_PRIVILEGED),
39277c478bd9Sstevel@tonic-gate  * 	(name = "limit", value = uint64_t),
39287c478bd9Sstevel@tonic-gate  * 	(name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))]
39297c478bd9Sstevel@tonic-gate  */
39307c478bd9Sstevel@tonic-gate static int
39317c478bd9Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp)
39327c478bd9Sstevel@tonic-gate {
39337c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
39347c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
39357c478bd9Sstevel@tonic-gate 	char *kbuf;
39367c478bd9Sstevel@tonic-gate 	int error;
39377c478bd9Sstevel@tonic-gate 	rctl_val_t rv;
39387c478bd9Sstevel@tonic-gate 
39397c478bd9Sstevel@tonic-gate 	*nvlp = NULL;
39407c478bd9Sstevel@tonic-gate 
39417c478bd9Sstevel@tonic-gate 	if (buflen == 0)
39427c478bd9Sstevel@tonic-gate 		return (0);
39437c478bd9Sstevel@tonic-gate 
39447c478bd9Sstevel@tonic-gate 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
39457c478bd9Sstevel@tonic-gate 		return (ENOMEM);
39467c478bd9Sstevel@tonic-gate 	if (copyin(ubuf, kbuf, buflen)) {
39477c478bd9Sstevel@tonic-gate 		error = EFAULT;
39487c478bd9Sstevel@tonic-gate 		goto out;
39497c478bd9Sstevel@tonic-gate 	}
39507c478bd9Sstevel@tonic-gate 	if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) {
39517c478bd9Sstevel@tonic-gate 		/*
39527c478bd9Sstevel@tonic-gate 		 * nvl may have been allocated/free'd, but the value set to
39537c478bd9Sstevel@tonic-gate 		 * non-NULL, so we reset it here.
39547c478bd9Sstevel@tonic-gate 		 */
39557c478bd9Sstevel@tonic-gate 		nvl = NULL;
39567c478bd9Sstevel@tonic-gate 		error = EINVAL;
39577c478bd9Sstevel@tonic-gate 		goto out;
39587c478bd9Sstevel@tonic-gate 	}
39597c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
39607c478bd9Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
39617c478bd9Sstevel@tonic-gate 		rctl_hndl_t hndl;
39627c478bd9Sstevel@tonic-gate 		nvlist_t **nvlarray;
39637c478bd9Sstevel@tonic-gate 		uint_t i, nelem;
39647c478bd9Sstevel@tonic-gate 		char *name;
39657c478bd9Sstevel@tonic-gate 
39667c478bd9Sstevel@tonic-gate 		error = EINVAL;
39677c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
39687c478bd9Sstevel@tonic-gate 		if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1)
39697c478bd9Sstevel@tonic-gate 		    != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) {
39707c478bd9Sstevel@tonic-gate 			goto out;
39717c478bd9Sstevel@tonic-gate 		}
39727c478bd9Sstevel@tonic-gate 		if ((hndl = rctl_hndl_lookup(name)) == -1) {
39737c478bd9Sstevel@tonic-gate 			goto out;
39747c478bd9Sstevel@tonic-gate 		}
39757c478bd9Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
39767c478bd9Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
39777c478bd9Sstevel@tonic-gate 		ASSERT(error == 0);
39787c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
39797c478bd9Sstevel@tonic-gate 			if (error = nvlist2rctlval(nvlarray[i], &rv))
39807c478bd9Sstevel@tonic-gate 				goto out;
39817c478bd9Sstevel@tonic-gate 		}
39827c478bd9Sstevel@tonic-gate 		if (rctl_invalid_value(rde, &rv)) {
39837c478bd9Sstevel@tonic-gate 			error = EINVAL;
39847c478bd9Sstevel@tonic-gate 			goto out;
39857c478bd9Sstevel@tonic-gate 		}
39867c478bd9Sstevel@tonic-gate 	}
39877c478bd9Sstevel@tonic-gate 	error = 0;
39887c478bd9Sstevel@tonic-gate 	*nvlp = nvl;
39897c478bd9Sstevel@tonic-gate out:
39907c478bd9Sstevel@tonic-gate 	kmem_free(kbuf, buflen);
39917c478bd9Sstevel@tonic-gate 	if (error && nvl != NULL)
39927c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
39937c478bd9Sstevel@tonic-gate 	return (error);
39947c478bd9Sstevel@tonic-gate }
39957c478bd9Sstevel@tonic-gate 
39967c478bd9Sstevel@tonic-gate int
39977c478bd9Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) {
39987c478bd9Sstevel@tonic-gate 	if (er_out != NULL) {
39997c478bd9Sstevel@tonic-gate 		if (copyout(&er_ext, er_out, sizeof (int))) {
40007c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
40017c478bd9Sstevel@tonic-gate 		}
40027c478bd9Sstevel@tonic-gate 	}
40037c478bd9Sstevel@tonic-gate 	return (set_errno(er_error));
40047c478bd9Sstevel@tonic-gate }
40057c478bd9Sstevel@tonic-gate 
400645916cd2Sjpk static int
400745916cd2Sjpk zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi)
400845916cd2Sjpk {
400945916cd2Sjpk 	ts_label_t *tsl;
401045916cd2Sjpk 	bslabel_t blab;
401145916cd2Sjpk 
401245916cd2Sjpk 	/* Get label from user */
401345916cd2Sjpk 	if (copyin(lab, &blab, sizeof (blab)) != 0)
401445916cd2Sjpk 		return (EFAULT);
401545916cd2Sjpk 	tsl = labelalloc(&blab, doi, KM_NOSLEEP);
401645916cd2Sjpk 	if (tsl == NULL)
401745916cd2Sjpk 		return (ENOMEM);
401845916cd2Sjpk 
401945916cd2Sjpk 	zone->zone_slabel = tsl;
402045916cd2Sjpk 	return (0);
402145916cd2Sjpk }
402245916cd2Sjpk 
40237c478bd9Sstevel@tonic-gate /*
4024fa9e4066Sahrens  * Parses a comma-separated list of ZFS datasets into a per-zone dictionary.
4025fa9e4066Sahrens  */
4026fa9e4066Sahrens static int
4027fa9e4066Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen)
4028fa9e4066Sahrens {
4029fa9e4066Sahrens 	char *kbuf;
4030fa9e4066Sahrens 	char *dataset, *next;
4031fa9e4066Sahrens 	zone_dataset_t *zd;
4032fa9e4066Sahrens 	size_t len;
4033fa9e4066Sahrens 
4034fa9e4066Sahrens 	if (ubuf == NULL || buflen == 0)
4035fa9e4066Sahrens 		return (0);
4036fa9e4066Sahrens 
4037fa9e4066Sahrens 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
4038fa9e4066Sahrens 		return (ENOMEM);
4039fa9e4066Sahrens 
4040fa9e4066Sahrens 	if (copyin(ubuf, kbuf, buflen) != 0) {
4041fa9e4066Sahrens 		kmem_free(kbuf, buflen);
4042fa9e4066Sahrens 		return (EFAULT);
4043fa9e4066Sahrens 	}
4044fa9e4066Sahrens 
4045fa9e4066Sahrens 	dataset = next = kbuf;
4046fa9e4066Sahrens 	for (;;) {
4047fa9e4066Sahrens 		zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP);
4048fa9e4066Sahrens 
4049fa9e4066Sahrens 		next = strchr(dataset, ',');
4050fa9e4066Sahrens 
4051fa9e4066Sahrens 		if (next == NULL)
4052fa9e4066Sahrens 			len = strlen(dataset);
4053fa9e4066Sahrens 		else
4054fa9e4066Sahrens 			len = next - dataset;
4055fa9e4066Sahrens 
4056fa9e4066Sahrens 		zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP);
4057fa9e4066Sahrens 		bcopy(dataset, zd->zd_dataset, len);
4058fa9e4066Sahrens 		zd->zd_dataset[len] = '\0';
4059fa9e4066Sahrens 
4060fa9e4066Sahrens 		list_insert_head(&zone->zone_datasets, zd);
4061fa9e4066Sahrens 
4062fa9e4066Sahrens 		if (next == NULL)
4063fa9e4066Sahrens 			break;
4064fa9e4066Sahrens 
4065fa9e4066Sahrens 		dataset = next + 1;
4066fa9e4066Sahrens 	}
4067fa9e4066Sahrens 
4068fa9e4066Sahrens 	kmem_free(kbuf, buflen);
4069fa9e4066Sahrens 	return (0);
4070fa9e4066Sahrens }
4071fa9e4066Sahrens 
4072fa9e4066Sahrens /*
40737c478bd9Sstevel@tonic-gate  * System call to create/initialize a new zone named 'zone_name', rooted
40747c478bd9Sstevel@tonic-gate  * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs',
407545916cd2Sjpk  * and initialized with the zone-wide rctls described in 'rctlbuf', and
407645916cd2Sjpk  * with labeling set by 'match', 'doi', and 'label'.
40777c478bd9Sstevel@tonic-gate  *
40787c478bd9Sstevel@tonic-gate  * If extended error is non-null, we may use it to return more detailed
40797c478bd9Sstevel@tonic-gate  * error information.
40807c478bd9Sstevel@tonic-gate  */
40817c478bd9Sstevel@tonic-gate static zoneid_t
40827c478bd9Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root,
4083821c4a97Sdp     const priv_set_t *zone_privs, size_t zone_privssz,
4084821c4a97Sdp     caddr_t rctlbuf, size_t rctlbufsz,
408545916cd2Sjpk     caddr_t zfsbuf, size_t zfsbufsz, int *extended_error,
4086f4b3ec61Sdh155122     int match, uint32_t doi, const bslabel_t *label,
4087f4b3ec61Sdh155122     int flags)
40887c478bd9Sstevel@tonic-gate {
40897c478bd9Sstevel@tonic-gate 	struct zsched_arg zarg;
40907c478bd9Sstevel@tonic-gate 	nvlist_t *rctls = NULL;
40917c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
40927c478bd9Sstevel@tonic-gate 	zone_t *zone, *ztmp;
40937c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
40947c478bd9Sstevel@tonic-gate 	int error;
40957c478bd9Sstevel@tonic-gate 	int error2 = 0;
40967c478bd9Sstevel@tonic-gate 	char *str;
40977c478bd9Sstevel@tonic-gate 	cred_t *zkcr;
409848451833Scarlsonj 	boolean_t insert_label_hash;
40997c478bd9Sstevel@tonic-gate 
41007c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
41017c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
41027c478bd9Sstevel@tonic-gate 
41037c478bd9Sstevel@tonic-gate 	/* can't boot zone from within chroot environment */
41047c478bd9Sstevel@tonic-gate 	if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir)
41057c478bd9Sstevel@tonic-gate 		return (zone_create_error(ENOTSUP, ZE_CHROOTED,
41067c478bd9Sstevel@tonic-gate 		    extended_error));
41077c478bd9Sstevel@tonic-gate 
41087c478bd9Sstevel@tonic-gate 	zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP);
41097c478bd9Sstevel@tonic-gate 	zoneid = zone->zone_id = id_alloc(zoneid_space);
41107c478bd9Sstevel@tonic-gate 	zone->zone_status = ZONE_IS_UNINITIALIZED;
41117c478bd9Sstevel@tonic-gate 	zone->zone_pool = pool_default;
41127c478bd9Sstevel@tonic-gate 	zone->zone_pool_mod = gethrtime();
41137c478bd9Sstevel@tonic-gate 	zone->zone_psetid = ZONE_PS_INVAL;
41147c478bd9Sstevel@tonic-gate 	zone->zone_ncpus = 0;
41157c478bd9Sstevel@tonic-gate 	zone->zone_ncpus_online = 0;
41169acbbeafSnn35248 	zone->zone_restart_init = B_TRUE;
41179acbbeafSnn35248 	zone->zone_brand = &native_brand;
41189acbbeafSnn35248 	zone->zone_initname = NULL;
41197c478bd9Sstevel@tonic-gate 	mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL);
41207c478bd9Sstevel@tonic-gate 	mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
41210209230bSgjelinek 	mutex_init(&zone->zone_mem_lock, NULL, MUTEX_DEFAULT, NULL);
41227c478bd9Sstevel@tonic-gate 	cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL);
4123a19609f8Sjv227347 	list_create(&zone->zone_ref_list, sizeof (zone_ref_t),
4124a19609f8Sjv227347 	    offsetof(zone_ref_t, zref_linkage));
41257c478bd9Sstevel@tonic-gate 	list_create(&zone->zone_zsd, sizeof (struct zsd_entry),
41267c478bd9Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
4127fa9e4066Sahrens 	list_create(&zone->zone_datasets, sizeof (zone_dataset_t),
4128fa9e4066Sahrens 	    offsetof(zone_dataset_t, zd_linkage));
41292b24ab6bSSebastien Roy 	list_create(&zone->zone_dl_list, sizeof (zone_dl_t),
41302b24ab6bSSebastien Roy 	    offsetof(zone_dl_t, zdl_linkage));
413145916cd2Sjpk 	rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
4132835ee219SRobert Harris 	rw_init(&zone->zone_mntfs_db_lock, NULL, RW_DEFAULT, NULL);
41337c478bd9Sstevel@tonic-gate 
4134f4b3ec61Sdh155122 	if (flags & ZCF_NET_EXCL) {
4135f4b3ec61Sdh155122 		zone->zone_flags |= ZF_NET_EXCL;
4136f4b3ec61Sdh155122 	}
4137f4b3ec61Sdh155122 
41387c478bd9Sstevel@tonic-gate 	if ((error = zone_set_name(zone, zone_name)) != 0) {
41397c478bd9Sstevel@tonic-gate 		zone_free(zone);
41407c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
41417c478bd9Sstevel@tonic-gate 	}
41427c478bd9Sstevel@tonic-gate 
41437c478bd9Sstevel@tonic-gate 	if ((error = zone_set_root(zone, zone_root)) != 0) {
41447c478bd9Sstevel@tonic-gate 		zone_free(zone);
41457c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
41467c478bd9Sstevel@tonic-gate 	}
4147821c4a97Sdp 	if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) {
41487c478bd9Sstevel@tonic-gate 		zone_free(zone);
41497c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
41507c478bd9Sstevel@tonic-gate 	}
41517c478bd9Sstevel@tonic-gate 
41527c478bd9Sstevel@tonic-gate 	/* initialize node name to be the same as zone name */
41537c478bd9Sstevel@tonic-gate 	zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP);
41547c478bd9Sstevel@tonic-gate 	(void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN);
41557c478bd9Sstevel@tonic-gate 	zone->zone_nodename[_SYS_NMLN - 1] = '\0';
41567c478bd9Sstevel@tonic-gate 
41577c478bd9Sstevel@tonic-gate 	zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP);
41587c478bd9Sstevel@tonic-gate 	zone->zone_domain[0] = '\0';
41595679c89fSjv227347 	zone->zone_hostid = HW_INVALID_HOSTID;
41607c478bd9Sstevel@tonic-gate 	zone->zone_shares = 1;
4161824c205fSml93401 	zone->zone_shmmax = 0;
4162824c205fSml93401 	zone->zone_ipc.ipcq_shmmni = 0;
4163824c205fSml93401 	zone->zone_ipc.ipcq_semmni = 0;
4164824c205fSml93401 	zone->zone_ipc.ipcq_msgmni = 0;
41657c478bd9Sstevel@tonic-gate 	zone->zone_bootargs = NULL;
41660fbb751dSJohn Levon 	zone->zone_fs_allowed = NULL;
41673f2f09c1Sdp 	zone->zone_initname =
41683f2f09c1Sdp 	    kmem_alloc(strlen(zone_default_initname) + 1, KM_SLEEP);
41693f2f09c1Sdp 	(void) strcpy(zone->zone_initname, zone_default_initname);
41700209230bSgjelinek 	zone->zone_nlwps = 0;
41710209230bSgjelinek 	zone->zone_nlwps_ctl = INT_MAX;
4172ff19e029SMenno Lageman 	zone->zone_nprocs = 0;
4173ff19e029SMenno Lageman 	zone->zone_nprocs_ctl = INT_MAX;
4174c6939658Ssl108498 	zone->zone_locked_mem = 0;
4175c6939658Ssl108498 	zone->zone_locked_mem_ctl = UINT64_MAX;
41760209230bSgjelinek 	zone->zone_max_swap = 0;
41770209230bSgjelinek 	zone->zone_max_swap_ctl = UINT64_MAX;
41780fbb751dSJohn Levon 	zone->zone_max_lofi = 0;
41790fbb751dSJohn Levon 	zone->zone_max_lofi_ctl = UINT64_MAX;
41800209230bSgjelinek 	zone0.zone_lockedmem_kstat = NULL;
41810209230bSgjelinek 	zone0.zone_swapresv_kstat = NULL;
41827c478bd9Sstevel@tonic-gate 
41837c478bd9Sstevel@tonic-gate 	/*
41847c478bd9Sstevel@tonic-gate 	 * Zsched initializes the rctls.
41857c478bd9Sstevel@tonic-gate 	 */
41867c478bd9Sstevel@tonic-gate 	zone->zone_rctls = NULL;
41877c478bd9Sstevel@tonic-gate 
41887c478bd9Sstevel@tonic-gate 	if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) {
41897c478bd9Sstevel@tonic-gate 		zone_free(zone);
41907c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
41917c478bd9Sstevel@tonic-gate 	}
41927c478bd9Sstevel@tonic-gate 
4193fa9e4066Sahrens 	if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) {
4194fa9e4066Sahrens 		zone_free(zone);
4195fa9e4066Sahrens 		return (set_errno(error));
4196fa9e4066Sahrens 	}
4197fa9e4066Sahrens 
41987c478bd9Sstevel@tonic-gate 	/*
419945916cd2Sjpk 	 * Read in the trusted system parameters:
420045916cd2Sjpk 	 * match flag and sensitivity label.
420145916cd2Sjpk 	 */
420245916cd2Sjpk 	zone->zone_match = match;
420348451833Scarlsonj 	if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) {
42047e6639c2Skp158701 		/* Fail if requested to set doi to anything but system's doi */
42057e6639c2Skp158701 		if (doi != 0 && doi != default_doi) {
42067e6639c2Skp158701 			zone_free(zone);
42077e6639c2Skp158701 			return (set_errno(EINVAL));
42087e6639c2Skp158701 		}
42097e6639c2Skp158701 		/* Always apply system's doi to the zone */
42107e6639c2Skp158701 		error = zone_set_label(zone, label, default_doi);
421145916cd2Sjpk 		if (error != 0) {
421245916cd2Sjpk 			zone_free(zone);
421345916cd2Sjpk 			return (set_errno(error));
421445916cd2Sjpk 		}
421548451833Scarlsonj 		insert_label_hash = B_TRUE;
421645916cd2Sjpk 	} else {
421745916cd2Sjpk 		/* all zones get an admin_low label if system is not labeled */
421845916cd2Sjpk 		zone->zone_slabel = l_admin_low;
421945916cd2Sjpk 		label_hold(l_admin_low);
422048451833Scarlsonj 		insert_label_hash = B_FALSE;
422145916cd2Sjpk 	}
422245916cd2Sjpk 
422345916cd2Sjpk 	/*
42247c478bd9Sstevel@tonic-gate 	 * Stop all lwps since that's what normally happens as part of fork().
42257c478bd9Sstevel@tonic-gate 	 * This needs to happen before we grab any locks to avoid deadlock
42267c478bd9Sstevel@tonic-gate 	 * (another lwp in the process could be waiting for the held lock).
42277c478bd9Sstevel@tonic-gate 	 */
42287c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) {
42297c478bd9Sstevel@tonic-gate 		zone_free(zone);
42307c478bd9Sstevel@tonic-gate 		if (rctls)
42317c478bd9Sstevel@tonic-gate 			nvlist_free(rctls);
42327c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
42337c478bd9Sstevel@tonic-gate 	}
42347c478bd9Sstevel@tonic-gate 
42357c478bd9Sstevel@tonic-gate 	if (block_mounts() == 0) {
42367c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
42377c478bd9Sstevel@tonic-gate 		if (curthread != pp->p_agenttp)
42387c478bd9Sstevel@tonic-gate 			continuelwps(pp);
42397c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
42407c478bd9Sstevel@tonic-gate 		zone_free(zone);
42417c478bd9Sstevel@tonic-gate 		if (rctls)
42427c478bd9Sstevel@tonic-gate 			nvlist_free(rctls);
42437c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
42447c478bd9Sstevel@tonic-gate 	}
42457c478bd9Sstevel@tonic-gate 
42467c478bd9Sstevel@tonic-gate 	/*
42477c478bd9Sstevel@tonic-gate 	 * Set up credential for kernel access.  After this, any errors
42487c478bd9Sstevel@tonic-gate 	 * should go through the dance in errout rather than calling
42497c478bd9Sstevel@tonic-gate 	 * zone_free directly.
42507c478bd9Sstevel@tonic-gate 	 */
42517c478bd9Sstevel@tonic-gate 	zone->zone_kcred = crdup(kcred);
42527c478bd9Sstevel@tonic-gate 	crsetzone(zone->zone_kcred, zone);
42537c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred));
42547c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred));
42557c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred));
42567c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred));
42577c478bd9Sstevel@tonic-gate 
42587c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
42597c478bd9Sstevel@tonic-gate 	/*
42607c478bd9Sstevel@tonic-gate 	 * Make sure zone doesn't already exist.
426145916cd2Sjpk 	 *
426245916cd2Sjpk 	 * If the system and zone are labeled,
426345916cd2Sjpk 	 * make sure no other zone exists that has the same label.
42647c478bd9Sstevel@tonic-gate 	 */
426545916cd2Sjpk 	if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL ||
426648451833Scarlsonj 	    (insert_label_hash &&
426745916cd2Sjpk 	    (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) {
42687c478bd9Sstevel@tonic-gate 		zone_status_t status;
42697c478bd9Sstevel@tonic-gate 
42707c478bd9Sstevel@tonic-gate 		status = zone_status_get(ztmp);
42717c478bd9Sstevel@tonic-gate 		if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING)
42727c478bd9Sstevel@tonic-gate 			error = EEXIST;
42737c478bd9Sstevel@tonic-gate 		else
42747c478bd9Sstevel@tonic-gate 			error = EBUSY;
42758c55461bSton 
42768c55461bSton 		if (insert_label_hash)
42778c55461bSton 			error2 = ZE_LABELINUSE;
42788c55461bSton 
42797c478bd9Sstevel@tonic-gate 		goto errout;
42807c478bd9Sstevel@tonic-gate 	}
42817c478bd9Sstevel@tonic-gate 
42827c478bd9Sstevel@tonic-gate 	/*
42837c478bd9Sstevel@tonic-gate 	 * Don't allow zone creations which would cause one zone's rootpath to
42847c478bd9Sstevel@tonic-gate 	 * be accessible from that of another (non-global) zone.
42857c478bd9Sstevel@tonic-gate 	 */
42867c478bd9Sstevel@tonic-gate 	if (zone_is_nested(zone->zone_rootpath)) {
42877c478bd9Sstevel@tonic-gate 		error = EBUSY;
42887c478bd9Sstevel@tonic-gate 		goto errout;
42897c478bd9Sstevel@tonic-gate 	}
42907c478bd9Sstevel@tonic-gate 
42917c478bd9Sstevel@tonic-gate 	ASSERT(zonecount != 0);		/* check for leaks */
42927c478bd9Sstevel@tonic-gate 	if (zonecount + 1 > maxzones) {
42937c478bd9Sstevel@tonic-gate 		error = ENOMEM;
42947c478bd9Sstevel@tonic-gate 		goto errout;
42957c478bd9Sstevel@tonic-gate 	}
42967c478bd9Sstevel@tonic-gate 
42977c478bd9Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
42987c478bd9Sstevel@tonic-gate 		error = EBUSY;
42997c478bd9Sstevel@tonic-gate 		error2 = ZE_AREMOUNTS;
43007c478bd9Sstevel@tonic-gate 		goto errout;
43017c478bd9Sstevel@tonic-gate 	}
43027c478bd9Sstevel@tonic-gate 
43037c478bd9Sstevel@tonic-gate 	/*
43047c478bd9Sstevel@tonic-gate 	 * Zone is still incomplete, but we need to drop all locks while
43057c478bd9Sstevel@tonic-gate 	 * zsched() initializes this zone's kernel process.  We
43067c478bd9Sstevel@tonic-gate 	 * optimistically add the zone to the hashtable and associated
43077c478bd9Sstevel@tonic-gate 	 * lists so a parallel zone_create() doesn't try to create the
43087c478bd9Sstevel@tonic-gate 	 * same zone.
43097c478bd9Sstevel@tonic-gate 	 */
43107c478bd9Sstevel@tonic-gate 	zonecount++;
43117c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid,
43127c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id,
43137c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
43147c478bd9Sstevel@tonic-gate 	str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP);
43157c478bd9Sstevel@tonic-gate 	(void) strcpy(str, zone->zone_name);
43167c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str,
43177c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
431848451833Scarlsonj 	if (insert_label_hash) {
431945916cd2Sjpk 		(void) mod_hash_insert(zonehashbylabel,
432045916cd2Sjpk 		    (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone);
432148451833Scarlsonj 		zone->zone_flags |= ZF_HASHED_LABEL;
432245916cd2Sjpk 	}
432345916cd2Sjpk 
43247c478bd9Sstevel@tonic-gate 	/*
43257c478bd9Sstevel@tonic-gate 	 * Insert into active list.  At this point there are no 'hold's
43267c478bd9Sstevel@tonic-gate 	 * on the zone, but everyone else knows not to use it, so we can
43277c478bd9Sstevel@tonic-gate 	 * continue to use it.  zsched() will do a zone_hold() if the
43287c478bd9Sstevel@tonic-gate 	 * newproc() is successful.
43297c478bd9Sstevel@tonic-gate 	 */
43307c478bd9Sstevel@tonic-gate 	list_insert_tail(&zone_active, zone);
43317c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
43327c478bd9Sstevel@tonic-gate 
43337c478bd9Sstevel@tonic-gate 	zarg.zone = zone;
43347c478bd9Sstevel@tonic-gate 	zarg.nvlist = rctls;
43357c478bd9Sstevel@tonic-gate 	/*
43367c478bd9Sstevel@tonic-gate 	 * The process, task, and project rctls are probably wrong;
43377c478bd9Sstevel@tonic-gate 	 * we need an interface to get the default values of all rctls,
43387c478bd9Sstevel@tonic-gate 	 * and initialize zsched appropriately.  I'm not sure that that
43397c478bd9Sstevel@tonic-gate 	 * makes much of a difference, though.
43407c478bd9Sstevel@tonic-gate 	 */
434135a5a358SJonathan Adams 	error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL, 0);
434235a5a358SJonathan Adams 	if (error != 0) {
43437c478bd9Sstevel@tonic-gate 		/*
43447c478bd9Sstevel@tonic-gate 		 * We need to undo all globally visible state.
43457c478bd9Sstevel@tonic-gate 		 */
43467c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
43477c478bd9Sstevel@tonic-gate 		list_remove(&zone_active, zone);
434848451833Scarlsonj 		if (zone->zone_flags & ZF_HASHED_LABEL) {
434945916cd2Sjpk 			ASSERT(zone->zone_slabel != NULL);
435045916cd2Sjpk 			(void) mod_hash_destroy(zonehashbylabel,
435145916cd2Sjpk 			    (mod_hash_key_t)zone->zone_slabel);
435245916cd2Sjpk 		}
43537c478bd9Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyname,
43547c478bd9Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_name);
43557c478bd9Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyid,
43567c478bd9Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_id);
43577c478bd9Sstevel@tonic-gate 		ASSERT(zonecount > 1);
43587c478bd9Sstevel@tonic-gate 		zonecount--;
43597c478bd9Sstevel@tonic-gate 		goto errout;
43607c478bd9Sstevel@tonic-gate 	}
43617c478bd9Sstevel@tonic-gate 
43627c478bd9Sstevel@tonic-gate 	/*
43637c478bd9Sstevel@tonic-gate 	 * Zone creation can't fail from now on.
43647c478bd9Sstevel@tonic-gate 	 */
43657c478bd9Sstevel@tonic-gate 
43667c478bd9Sstevel@tonic-gate 	/*
43670209230bSgjelinek 	 * Create zone kstats
43680209230bSgjelinek 	 */
43690209230bSgjelinek 	zone_kstat_create(zone);
43700209230bSgjelinek 
43710209230bSgjelinek 	/*
43727c478bd9Sstevel@tonic-gate 	 * Let the other lwps continue.
43737c478bd9Sstevel@tonic-gate 	 */
43747c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
43757c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
43767c478bd9Sstevel@tonic-gate 		continuelwps(pp);
43777c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
43787c478bd9Sstevel@tonic-gate 
43797c478bd9Sstevel@tonic-gate 	/*
43807c478bd9Sstevel@tonic-gate 	 * Wait for zsched to finish initializing the zone.
43817c478bd9Sstevel@tonic-gate 	 */
43827c478bd9Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_READY);
43837c478bd9Sstevel@tonic-gate 	/*
43847c478bd9Sstevel@tonic-gate 	 * The zone is fully visible, so we can let mounts progress.
43857c478bd9Sstevel@tonic-gate 	 */
43867c478bd9Sstevel@tonic-gate 	resume_mounts();
43877c478bd9Sstevel@tonic-gate 	if (rctls)
43887c478bd9Sstevel@tonic-gate 		nvlist_free(rctls);
43897c478bd9Sstevel@tonic-gate 
43907c478bd9Sstevel@tonic-gate 	return (zoneid);
43917c478bd9Sstevel@tonic-gate 
43927c478bd9Sstevel@tonic-gate errout:
43937c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
43947c478bd9Sstevel@tonic-gate 	/*
43957c478bd9Sstevel@tonic-gate 	 * Let the other lwps continue.
43967c478bd9Sstevel@tonic-gate 	 */
43977c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
43987c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
43997c478bd9Sstevel@tonic-gate 		continuelwps(pp);
44007c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
44017c478bd9Sstevel@tonic-gate 
44027c478bd9Sstevel@tonic-gate 	resume_mounts();
44037c478bd9Sstevel@tonic-gate 	if (rctls)
44047c478bd9Sstevel@tonic-gate 		nvlist_free(rctls);
44057c478bd9Sstevel@tonic-gate 	/*
44067c478bd9Sstevel@tonic-gate 	 * There is currently one reference to the zone, a cred_ref from
44077c478bd9Sstevel@tonic-gate 	 * zone_kcred.  To free the zone, we call crfree, which will call
44087c478bd9Sstevel@tonic-gate 	 * zone_cred_rele, which will call zone_free.
44097c478bd9Sstevel@tonic-gate 	 */
4410a19609f8Sjv227347 	ASSERT(zone->zone_cred_ref == 1);
44117c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_kcred->cr_ref == 1);
44127c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_ref == 0);
44137c478bd9Sstevel@tonic-gate 	zkcr = zone->zone_kcred;
44147c478bd9Sstevel@tonic-gate 	zone->zone_kcred = NULL;
44157c478bd9Sstevel@tonic-gate 	crfree(zkcr);				/* triggers call to zone_free */
44167c478bd9Sstevel@tonic-gate 	return (zone_create_error(error, error2, extended_error));
44177c478bd9Sstevel@tonic-gate }
44187c478bd9Sstevel@tonic-gate 
44197c478bd9Sstevel@tonic-gate /*
44207c478bd9Sstevel@tonic-gate  * Cause the zone to boot.  This is pretty simple, since we let zoneadmd do
44213f2f09c1Sdp  * the heavy lifting.  initname is the path to the program to launch
44223f2f09c1Sdp  * at the "top" of the zone; if this is NULL, we use the system default,
44233f2f09c1Sdp  * which is stored at zone_default_initname.
44247c478bd9Sstevel@tonic-gate  */
44257c478bd9Sstevel@tonic-gate static int
44263f2f09c1Sdp zone_boot(zoneid_t zoneid)
44277c478bd9Sstevel@tonic-gate {
44287c478bd9Sstevel@tonic-gate 	int err;
44297c478bd9Sstevel@tonic-gate 	zone_t *zone;
44307c478bd9Sstevel@tonic-gate 
44317c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
44327c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
44337c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
44347c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
44357c478bd9Sstevel@tonic-gate 
44367c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
44377c478bd9Sstevel@tonic-gate 	/*
44387c478bd9Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with calls to
44397c478bd9Sstevel@tonic-gate 	 * zone_shutdown, zone_destroy, etc.
44407c478bd9Sstevel@tonic-gate 	 */
44417c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
44427c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
44437c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
44447c478bd9Sstevel@tonic-gate 	}
44457c478bd9Sstevel@tonic-gate 
44467c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
44477c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) != ZONE_IS_READY) {
44487c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
44497c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
44507c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
44517c478bd9Sstevel@tonic-gate 	}
44527c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_BOOTING);
44537c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
44547c478bd9Sstevel@tonic-gate 
44557c478bd9Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
44567c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
44577c478bd9Sstevel@tonic-gate 
44587c478bd9Sstevel@tonic-gate 	if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) {
44597c478bd9Sstevel@tonic-gate 		zone_rele(zone);
44607c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
44617c478bd9Sstevel@tonic-gate 	}
44627c478bd9Sstevel@tonic-gate 
44637c478bd9Sstevel@tonic-gate 	/*
44647c478bd9Sstevel@tonic-gate 	 * Boot (starting init) might have failed, in which case the zone
44657c478bd9Sstevel@tonic-gate 	 * will go to the SHUTTING_DOWN state; an appropriate errno will
44667c478bd9Sstevel@tonic-gate 	 * be placed in zone->zone_boot_err, and so we return that.
44677c478bd9Sstevel@tonic-gate 	 */
44687c478bd9Sstevel@tonic-gate 	err = zone->zone_boot_err;
44697c478bd9Sstevel@tonic-gate 	zone_rele(zone);
44707c478bd9Sstevel@tonic-gate 	return (err ? set_errno(err) : 0);
44717c478bd9Sstevel@tonic-gate }
44727c478bd9Sstevel@tonic-gate 
44737c478bd9Sstevel@tonic-gate /*
44747c478bd9Sstevel@tonic-gate  * Kills all user processes in the zone, waiting for them all to exit
44757c478bd9Sstevel@tonic-gate  * before returning.
44767c478bd9Sstevel@tonic-gate  */
44777c478bd9Sstevel@tonic-gate static int
44787c478bd9Sstevel@tonic-gate zone_empty(zone_t *zone)
44797c478bd9Sstevel@tonic-gate {
44807c478bd9Sstevel@tonic-gate 	int waitstatus;
44817c478bd9Sstevel@tonic-gate 
44827c478bd9Sstevel@tonic-gate 	/*
44837c478bd9Sstevel@tonic-gate 	 * We need to drop zonehash_lock before killing all
44847c478bd9Sstevel@tonic-gate 	 * processes, otherwise we'll deadlock with zone_find_*
44857c478bd9Sstevel@tonic-gate 	 * which can be called from the exit path.
44867c478bd9Sstevel@tonic-gate 	 */
44877c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
4488d3d50737SRafael Vanoni 	while ((waitstatus = zone_status_timedwait_sig(zone,
4489d3d50737SRafael Vanoni 	    ddi_get_lbolt() + hz, ZONE_IS_EMPTY)) == -1) {
44907c478bd9Sstevel@tonic-gate 		killall(zone->zone_id);
44917c478bd9Sstevel@tonic-gate 	}
44927c478bd9Sstevel@tonic-gate 	/*
44937c478bd9Sstevel@tonic-gate 	 * return EINTR if we were signaled
44947c478bd9Sstevel@tonic-gate 	 */
44957c478bd9Sstevel@tonic-gate 	if (waitstatus == 0)
44967c478bd9Sstevel@tonic-gate 		return (EINTR);
44977c478bd9Sstevel@tonic-gate 	return (0);
44987c478bd9Sstevel@tonic-gate }
44997c478bd9Sstevel@tonic-gate 
45007c478bd9Sstevel@tonic-gate /*
450145916cd2Sjpk  * This function implements the policy for zone visibility.
450245916cd2Sjpk  *
450345916cd2Sjpk  * In standard Solaris, a non-global zone can only see itself.
450445916cd2Sjpk  *
450545916cd2Sjpk  * In Trusted Extensions, a labeled zone can lookup any zone whose label
450645916cd2Sjpk  * it dominates. For this test, the label of the global zone is treated as
450745916cd2Sjpk  * admin_high so it is special-cased instead of being checked for dominance.
450845916cd2Sjpk  *
450945916cd2Sjpk  * Returns true if zone attributes are viewable, false otherwise.
451045916cd2Sjpk  */
451145916cd2Sjpk static boolean_t
451245916cd2Sjpk zone_list_access(zone_t *zone)
451345916cd2Sjpk {
451445916cd2Sjpk 
451545916cd2Sjpk 	if (curproc->p_zone == global_zone ||
451645916cd2Sjpk 	    curproc->p_zone == zone) {
451745916cd2Sjpk 		return (B_TRUE);
451848451833Scarlsonj 	} else if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) {
451945916cd2Sjpk 		bslabel_t *curproc_label;
452045916cd2Sjpk 		bslabel_t *zone_label;
452145916cd2Sjpk 
452245916cd2Sjpk 		curproc_label = label2bslabel(curproc->p_zone->zone_slabel);
452345916cd2Sjpk 		zone_label = label2bslabel(zone->zone_slabel);
452445916cd2Sjpk 
452545916cd2Sjpk 		if (zone->zone_id != GLOBAL_ZONEID &&
452645916cd2Sjpk 		    bldominates(curproc_label, zone_label)) {
452745916cd2Sjpk 			return (B_TRUE);
452845916cd2Sjpk 		} else {
452945916cd2Sjpk 			return (B_FALSE);
453045916cd2Sjpk 		}
453145916cd2Sjpk 	} else {
453245916cd2Sjpk 		return (B_FALSE);
453345916cd2Sjpk 	}
453445916cd2Sjpk }
453545916cd2Sjpk 
453645916cd2Sjpk /*
45377c478bd9Sstevel@tonic-gate  * Systemcall to start the zone's halt sequence.  By the time this
45387c478bd9Sstevel@tonic-gate  * function successfully returns, all user processes and kernel threads
45397c478bd9Sstevel@tonic-gate  * executing in it will have exited, ZSD shutdown callbacks executed,
45407c478bd9Sstevel@tonic-gate  * and the zone status set to ZONE_IS_DOWN.
45417c478bd9Sstevel@tonic-gate  *
45427c478bd9Sstevel@tonic-gate  * It is possible that the call will interrupt itself if the caller is the
45437c478bd9Sstevel@tonic-gate  * parent of any process running in the zone, and doesn't have SIGCHLD blocked.
45447c478bd9Sstevel@tonic-gate  */
45457c478bd9Sstevel@tonic-gate static int
45467c478bd9Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid)
45477c478bd9Sstevel@tonic-gate {
45487c478bd9Sstevel@tonic-gate 	int error;
45497c478bd9Sstevel@tonic-gate 	zone_t *zone;
45507c478bd9Sstevel@tonic-gate 	zone_status_t status;
45517c478bd9Sstevel@tonic-gate 
45527c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
45537c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
45547c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
45557c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
45567c478bd9Sstevel@tonic-gate 
45577c478bd9Sstevel@tonic-gate 	/*
45587c478bd9Sstevel@tonic-gate 	 * Block mounts so that VFS_MOUNT() can get an accurate view of
45597c478bd9Sstevel@tonic-gate 	 * the zone's status with regards to ZONE_IS_SHUTTING down.
45607c478bd9Sstevel@tonic-gate 	 *
45617c478bd9Sstevel@tonic-gate 	 * e.g. NFS can fail the mount if it determines that the zone
45627c478bd9Sstevel@tonic-gate 	 * has already begun the shutdown sequence.
45637c478bd9Sstevel@tonic-gate 	 */
45647c478bd9Sstevel@tonic-gate 	if (block_mounts() == 0)
45657c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
45667c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
45677c478bd9Sstevel@tonic-gate 	/*
45687c478bd9Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
45697c478bd9Sstevel@tonic-gate 	 * calls to zone_shutdown and zone_destroy.
45707c478bd9Sstevel@tonic-gate 	 */
45717c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
45727c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
45737c478bd9Sstevel@tonic-gate 		resume_mounts();
45747c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
45757c478bd9Sstevel@tonic-gate 	}
45767c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
45777c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
45787c478bd9Sstevel@tonic-gate 	/*
45797c478bd9Sstevel@tonic-gate 	 * Fail if the zone isn't fully initialized yet.
45807c478bd9Sstevel@tonic-gate 	 */
45817c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY) {
45827c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
45837c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
45847c478bd9Sstevel@tonic-gate 		resume_mounts();
45857c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
45867c478bd9Sstevel@tonic-gate 	}
45877c478bd9Sstevel@tonic-gate 	/*
45887c478bd9Sstevel@tonic-gate 	 * If conditions required for zone_shutdown() to return have been met,
45897c478bd9Sstevel@tonic-gate 	 * return success.
45907c478bd9Sstevel@tonic-gate 	 */
45917c478bd9Sstevel@tonic-gate 	if (status >= ZONE_IS_DOWN) {
45927c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
45937c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
45947c478bd9Sstevel@tonic-gate 		resume_mounts();
45957c478bd9Sstevel@tonic-gate 		return (0);
45967c478bd9Sstevel@tonic-gate 	}
45977c478bd9Sstevel@tonic-gate 	/*
45987c478bd9Sstevel@tonic-gate 	 * If zone_shutdown() hasn't been called before, go through the motions.
45997c478bd9Sstevel@tonic-gate 	 * If it has, there's nothing to do but wait for the kernel threads to
46007c478bd9Sstevel@tonic-gate 	 * drain.
46017c478bd9Sstevel@tonic-gate 	 */
46027c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_EMPTY) {
46037c478bd9Sstevel@tonic-gate 		uint_t ntasks;
46047c478bd9Sstevel@tonic-gate 
46057c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
46067c478bd9Sstevel@tonic-gate 		if ((ntasks = zone->zone_ntasks) != 1) {
46077c478bd9Sstevel@tonic-gate 			/*
46087c478bd9Sstevel@tonic-gate 			 * There's still stuff running.
46097c478bd9Sstevel@tonic-gate 			 */
46107c478bd9Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
46117c478bd9Sstevel@tonic-gate 		}
46127c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
46137c478bd9Sstevel@tonic-gate 		if (ntasks == 1) {
46147c478bd9Sstevel@tonic-gate 			/*
46157c478bd9Sstevel@tonic-gate 			 * The only way to create another task is through
46167c478bd9Sstevel@tonic-gate 			 * zone_enter(), which will block until we drop
46177c478bd9Sstevel@tonic-gate 			 * zonehash_lock.  The zone is empty.
46187c478bd9Sstevel@tonic-gate 			 */
46197c478bd9Sstevel@tonic-gate 			if (zone->zone_kthreads == NULL) {
46207c478bd9Sstevel@tonic-gate 				/*
46217c478bd9Sstevel@tonic-gate 				 * Skip ahead to ZONE_IS_DOWN
46227c478bd9Sstevel@tonic-gate 				 */
46237c478bd9Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_DOWN);
46247c478bd9Sstevel@tonic-gate 			} else {
46257c478bd9Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_EMPTY);
46267c478bd9Sstevel@tonic-gate 			}
46277c478bd9Sstevel@tonic-gate 		}
46287c478bd9Sstevel@tonic-gate 	}
46297c478bd9Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
46307c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
46317c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
46327c478bd9Sstevel@tonic-gate 	resume_mounts();
46337c478bd9Sstevel@tonic-gate 
46347c478bd9Sstevel@tonic-gate 	if (error = zone_empty(zone)) {
46357c478bd9Sstevel@tonic-gate 		zone_rele(zone);
46367c478bd9Sstevel@tonic-gate 		return (set_errno(error));
46377c478bd9Sstevel@tonic-gate 	}
46387c478bd9Sstevel@tonic-gate 	/*
46397c478bd9Sstevel@tonic-gate 	 * After the zone status goes to ZONE_IS_DOWN this zone will no
46407c478bd9Sstevel@tonic-gate 	 * longer be notified of changes to the pools configuration, so
46417c478bd9Sstevel@tonic-gate 	 * in order to not end up with a stale pool pointer, we point
46427c478bd9Sstevel@tonic-gate 	 * ourselves at the default pool and remove all resource
46437c478bd9Sstevel@tonic-gate 	 * visibility.  This is especially important as the zone_t may
46447c478bd9Sstevel@tonic-gate 	 * languish on the deathrow for a very long time waiting for
46457c478bd9Sstevel@tonic-gate 	 * cred's to drain out.
46467c478bd9Sstevel@tonic-gate 	 *
46477c478bd9Sstevel@tonic-gate 	 * This rebinding of the zone can happen multiple times
46487c478bd9Sstevel@tonic-gate 	 * (presumably due to interrupted or parallel systemcalls)
46497c478bd9Sstevel@tonic-gate 	 * without any adverse effects.
46507c478bd9Sstevel@tonic-gate 	 */
46517c478bd9Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
46527c478bd9Sstevel@tonic-gate 		zone_rele(zone);
46537c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
46547c478bd9Sstevel@tonic-gate 	}
46557c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
46567c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
46577c478bd9Sstevel@tonic-gate 		zone_pool_set(zone, pool_default);
46587c478bd9Sstevel@tonic-gate 		/*
46597c478bd9Sstevel@tonic-gate 		 * The zone no longer needs to be able to see any cpus.
46607c478bd9Sstevel@tonic-gate 		 */
46617c478bd9Sstevel@tonic-gate 		zone_pset_set(zone, ZONE_PS_INVAL);
46627c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
46637c478bd9Sstevel@tonic-gate 	}
46647c478bd9Sstevel@tonic-gate 	pool_unlock();
46657c478bd9Sstevel@tonic-gate 
46667c478bd9Sstevel@tonic-gate 	/*
46677c478bd9Sstevel@tonic-gate 	 * ZSD shutdown callbacks can be executed multiple times, hence
46687c478bd9Sstevel@tonic-gate 	 * it is safe to not be holding any locks across this call.
46697c478bd9Sstevel@tonic-gate 	 */
46707c478bd9Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_SHUTDOWN);
46717c478bd9Sstevel@tonic-gate 
46727c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
46737c478bd9Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN)
46747c478bd9Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DOWN);
46757c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
46767c478bd9Sstevel@tonic-gate 
46777c478bd9Sstevel@tonic-gate 	/*
46787c478bd9Sstevel@tonic-gate 	 * Wait for kernel threads to drain.
46797c478bd9Sstevel@tonic-gate 	 */
46807c478bd9Sstevel@tonic-gate 	if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) {
46817c478bd9Sstevel@tonic-gate 		zone_rele(zone);
46827c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
46837c478bd9Sstevel@tonic-gate 	}
46849acbbeafSnn35248 
468552978630Ssl108498 	/*
468652978630Ssl108498 	 * Zone can be become down/destroyable even if the above wait
468752978630Ssl108498 	 * returns EINTR, so any code added here may never execute.
468852978630Ssl108498 	 * (i.e. don't add code here)
468952978630Ssl108498 	 */
46909acbbeafSnn35248 
46917c478bd9Sstevel@tonic-gate 	zone_rele(zone);
46927c478bd9Sstevel@tonic-gate 	return (0);
46937c478bd9Sstevel@tonic-gate }
46947c478bd9Sstevel@tonic-gate 
46957c478bd9Sstevel@tonic-gate /*
4696a19609f8Sjv227347  * Log the specified zone's reference counts.  The caller should not be
4697a19609f8Sjv227347  * holding the zone's zone_lock.
4698a19609f8Sjv227347  */
4699a19609f8Sjv227347 static void
4700a19609f8Sjv227347 zone_log_refcounts(zone_t *zone)
4701a19609f8Sjv227347 {
4702a19609f8Sjv227347 	char *buffer;
4703a19609f8Sjv227347 	char *buffer_position;
4704a19609f8Sjv227347 	uint32_t buffer_size;
4705a19609f8Sjv227347 	uint32_t index;
4706a19609f8Sjv227347 	uint_t ref;
4707a19609f8Sjv227347 	uint_t cred_ref;
4708a19609f8Sjv227347 
4709a19609f8Sjv227347 	/*
4710a19609f8Sjv227347 	 * Construct a string representing the subsystem-specific reference
4711a19609f8Sjv227347 	 * counts.  The counts are printed in ascending order by index into the
4712a19609f8Sjv227347 	 * zone_t::zone_subsys_ref array.  The list will be surrounded by
4713a19609f8Sjv227347 	 * square brackets [] and will only contain nonzero reference counts.
4714a19609f8Sjv227347 	 *
4715a19609f8Sjv227347 	 * The buffer will hold two square bracket characters plus ten digits,
4716a19609f8Sjv227347 	 * one colon, one space, one comma, and some characters for a
4717a19609f8Sjv227347 	 * subsystem name per subsystem-specific reference count.  (Unsigned 32-
4718a19609f8Sjv227347 	 * bit integers have at most ten decimal digits.)  The last
4719a19609f8Sjv227347 	 * reference count's comma is replaced by the closing square
4720a19609f8Sjv227347 	 * bracket and a NULL character to terminate the string.
4721a19609f8Sjv227347 	 *
4722a19609f8Sjv227347 	 * NOTE: We have to grab the zone's zone_lock to create a consistent
4723a19609f8Sjv227347 	 * snapshot of the zone's reference counters.
4724a19609f8Sjv227347 	 *
4725a19609f8Sjv227347 	 * First, figure out how much space the string buffer will need.
4726a19609f8Sjv227347 	 * The buffer's size is stored in buffer_size.
4727a19609f8Sjv227347 	 */
4728a19609f8Sjv227347 	buffer_size = 2;			/* for the square brackets */
4729a19609f8Sjv227347 	mutex_enter(&zone->zone_lock);
4730a19609f8Sjv227347 	zone->zone_flags |= ZF_REFCOUNTS_LOGGED;
4731a19609f8Sjv227347 	ref = zone->zone_ref;
4732a19609f8Sjv227347 	cred_ref = zone->zone_cred_ref;
4733a19609f8Sjv227347 	for (index = 0; index < ZONE_REF_NUM_SUBSYS; ++index)
4734a19609f8Sjv227347 		if (zone->zone_subsys_ref[index] != 0)
4735a19609f8Sjv227347 			buffer_size += strlen(zone_ref_subsys_names[index]) +
4736a19609f8Sjv227347 			    13;
4737a19609f8Sjv227347 	if (buffer_size == 2) {
4738a19609f8Sjv227347 		/*
4739a19609f8Sjv227347 		 * No subsystems had nonzero reference counts.  Don't bother
4740a19609f8Sjv227347 		 * with allocating a buffer; just log the general-purpose and
4741a19609f8Sjv227347 		 * credential reference counts.
4742a19609f8Sjv227347 		 */
4743a19609f8Sjv227347 		mutex_exit(&zone->zone_lock);
4744a19609f8Sjv227347 		(void) strlog(0, 0, 1, SL_CONSOLE | SL_NOTE,
4745a19609f8Sjv227347 		    "Zone '%s' (ID: %d) is shutting down, but %u zone "
4746a19609f8Sjv227347 		    "references and %u credential references are still extant",
4747a19609f8Sjv227347 		    zone->zone_name, zone->zone_id, ref, cred_ref);
4748a19609f8Sjv227347 		return;
4749a19609f8Sjv227347 	}
4750a19609f8Sjv227347 
4751a19609f8Sjv227347 	/*
4752a19609f8Sjv227347 	 * buffer_size contains the exact number of characters that the
4753a19609f8Sjv227347 	 * buffer will need.  Allocate the buffer and fill it with nonzero
4754a19609f8Sjv227347 	 * subsystem-specific reference counts.  Surround the results with
4755a19609f8Sjv227347 	 * square brackets afterwards.
4756a19609f8Sjv227347 	 */
4757a19609f8Sjv227347 	buffer = kmem_alloc(buffer_size, KM_SLEEP);
4758a19609f8Sjv227347 	buffer_position = &buffer[1];
4759a19609f8Sjv227347 	for (index = 0; index < ZONE_REF_NUM_SUBSYS; ++index) {
4760a19609f8Sjv227347 		/*
4761a19609f8Sjv227347 		 * NOTE: The DDI's version of sprintf() returns a pointer to
4762a19609f8Sjv227347 		 * the modified buffer rather than the number of bytes written
4763a19609f8Sjv227347 		 * (as in snprintf(3C)).  This is unfortunate and annoying.
4764a19609f8Sjv227347 		 * Therefore, we'll use snprintf() with INT_MAX to get the
4765a19609f8Sjv227347 		 * number of bytes written.  Using INT_MAX is safe because
4766a19609f8Sjv227347 		 * the buffer is perfectly sized for the data: we'll never
4767a19609f8Sjv227347 		 * overrun the buffer.
4768a19609f8Sjv227347 		 */
4769a19609f8Sjv227347 		if (zone->zone_subsys_ref[index] != 0)
4770a19609f8Sjv227347 			buffer_position += snprintf(buffer_position, INT_MAX,
4771a19609f8Sjv227347 			    "%s: %u,", zone_ref_subsys_names[index],
4772a19609f8Sjv227347 			    zone->zone_subsys_ref[index]);
4773a19609f8Sjv227347 	}
4774a19609f8Sjv227347 	mutex_exit(&zone->zone_lock);
4775a19609f8Sjv227347 	buffer[0] = '[';
4776a19609f8Sjv227347 	ASSERT((uintptr_t)(buffer_position - buffer) < buffer_size);
4777a19609f8Sjv227347 	ASSERT(buffer_position[0] == '\0' && buffer_position[-1] == ',');
4778a19609f8Sjv227347 	buffer_position[-1] = ']';
4779a19609f8Sjv227347 
4780a19609f8Sjv227347 	/*
4781a19609f8Sjv227347 	 * Log the reference counts and free the message buffer.
4782a19609f8Sjv227347 	 */
4783a19609f8Sjv227347 	(void) strlog(0, 0, 1, SL_CONSOLE | SL_NOTE,
4784a19609f8Sjv227347 	    "Zone '%s' (ID: %d) is shutting down, but %u zone references and "
4785a19609f8Sjv227347 	    "%u credential references are still extant %s", zone->zone_name,
4786a19609f8Sjv227347 	    zone->zone_id, ref, cred_ref, buffer);
4787a19609f8Sjv227347 	kmem_free(buffer, buffer_size);
4788a19609f8Sjv227347 }
4789a19609f8Sjv227347 
4790a19609f8Sjv227347 /*
47917c478bd9Sstevel@tonic-gate  * Systemcall entry point to finalize the zone halt process.  The caller
4792824c205fSml93401  * must have already successfully called zone_shutdown().
47937c478bd9Sstevel@tonic-gate  *
47947c478bd9Sstevel@tonic-gate  * Upon successful completion, the zone will have been fully destroyed:
47957c478bd9Sstevel@tonic-gate  * zsched will have exited, destructor callbacks executed, and the zone
47967c478bd9Sstevel@tonic-gate  * removed from the list of active zones.
47977c478bd9Sstevel@tonic-gate  */
47987c478bd9Sstevel@tonic-gate static int
47997c478bd9Sstevel@tonic-gate zone_destroy(zoneid_t zoneid)
48007c478bd9Sstevel@tonic-gate {
48017c478bd9Sstevel@tonic-gate 	uint64_t uniqid;
48027c478bd9Sstevel@tonic-gate 	zone_t *zone;
48037c478bd9Sstevel@tonic-gate 	zone_status_t status;
4804a19609f8Sjv227347 	clock_t wait_time;
4805a19609f8Sjv227347 	boolean_t log_refcounts;
48067c478bd9Sstevel@tonic-gate 
48077c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
48087c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
48097c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
48107c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
48117c478bd9Sstevel@tonic-gate 
48127c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
48137c478bd9Sstevel@tonic-gate 	/*
48147c478bd9Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
48157c478bd9Sstevel@tonic-gate 	 * calls to zone_destroy.
48167c478bd9Sstevel@tonic-gate 	 */
48177c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
48187c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
48197c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
48207c478bd9Sstevel@tonic-gate 	}
48217c478bd9Sstevel@tonic-gate 
48227c478bd9Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
48237c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
48247c478bd9Sstevel@tonic-gate 		return (set_errno(EBUSY));
48257c478bd9Sstevel@tonic-gate 	}
48267c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
48277c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
48287c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_DOWN) {
48297c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
48307c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
48317c478bd9Sstevel@tonic-gate 		return (set_errno(EBUSY));
48327c478bd9Sstevel@tonic-gate 	} else if (status == ZONE_IS_DOWN) {
48337c478bd9Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */
48347c478bd9Sstevel@tonic-gate 	}
48357c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
48367c478bd9Sstevel@tonic-gate 	zone_hold(zone);
48377c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
48387c478bd9Sstevel@tonic-gate 
48397c478bd9Sstevel@tonic-gate 	/*
48407c478bd9Sstevel@tonic-gate 	 * wait for zsched to exit
48417c478bd9Sstevel@tonic-gate 	 */
48427c478bd9Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_DEAD);
48437c478bd9Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_DESTROY);
4844f4b3ec61Sdh155122 	zone->zone_netstack = NULL;
48457c478bd9Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
48467c478bd9Sstevel@tonic-gate 	zone_rele(zone);
48477c478bd9Sstevel@tonic-gate 	zone = NULL;	/* potentially free'd */
48487c478bd9Sstevel@tonic-gate 
4849a19609f8Sjv227347 	log_refcounts = B_FALSE;
4850a19609f8Sjv227347 	wait_time = SEC_TO_TICK(ZONE_DESTROY_TIMEOUT_SECS);
48517c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
48527c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
48537c478bd9Sstevel@tonic-gate 		boolean_t unref;
4854a19609f8Sjv227347 		boolean_t refs_have_been_logged;
48557c478bd9Sstevel@tonic-gate 
48567c478bd9Sstevel@tonic-gate 		if ((zone = zone_find_all_by_id(zoneid)) == NULL ||
48577c478bd9Sstevel@tonic-gate 		    zone->zone_uniqid != uniqid) {
48587c478bd9Sstevel@tonic-gate 			/*
48597c478bd9Sstevel@tonic-gate 			 * The zone has gone away.  Necessary conditions
48607c478bd9Sstevel@tonic-gate 			 * are met, so we return success.
48617c478bd9Sstevel@tonic-gate 			 */
48627c478bd9Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
48637c478bd9Sstevel@tonic-gate 			return (0);
48647c478bd9Sstevel@tonic-gate 		}
48657c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
48667c478bd9Sstevel@tonic-gate 		unref = ZONE_IS_UNREF(zone);
4867a19609f8Sjv227347 		refs_have_been_logged = (zone->zone_flags &
4868a19609f8Sjv227347 		    ZF_REFCOUNTS_LOGGED);
48697c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
48707c478bd9Sstevel@tonic-gate 		if (unref) {
48717c478bd9Sstevel@tonic-gate 			/*
48727c478bd9Sstevel@tonic-gate 			 * There is only one reference to the zone -- that
48737c478bd9Sstevel@tonic-gate 			 * added when the zone was added to the hashtables --
48747c478bd9Sstevel@tonic-gate 			 * and things will remain this way until we drop
48757c478bd9Sstevel@tonic-gate 			 * zonehash_lock... we can go ahead and cleanup the
48767c478bd9Sstevel@tonic-gate 			 * zone.
48777c478bd9Sstevel@tonic-gate 			 */
48787c478bd9Sstevel@tonic-gate 			break;
48797c478bd9Sstevel@tonic-gate 		}
48807c478bd9Sstevel@tonic-gate 
4881a19609f8Sjv227347 		/*
4882a19609f8Sjv227347 		 * Wait for zone_rele_common() or zone_cred_rele() to signal
4883a19609f8Sjv227347 		 * zone_destroy_cv.  zone_destroy_cv is signaled only when
4884a19609f8Sjv227347 		 * some zone's general-purpose reference count reaches one.
4885a19609f8Sjv227347 		 * If ZONE_DESTROY_TIMEOUT_SECS seconds elapse while waiting
4886a19609f8Sjv227347 		 * on zone_destroy_cv, then log the zone's reference counts and
4887a19609f8Sjv227347 		 * continue to wait for zone_rele() and zone_cred_rele().
4888a19609f8Sjv227347 		 */
4889a19609f8Sjv227347 		if (!refs_have_been_logged) {
4890a19609f8Sjv227347 			if (!log_refcounts) {
4891a19609f8Sjv227347 				/*
4892a19609f8Sjv227347 				 * This thread hasn't timed out waiting on
4893a19609f8Sjv227347 				 * zone_destroy_cv yet.  Wait wait_time clock
4894a19609f8Sjv227347 				 * ticks (initially ZONE_DESTROY_TIMEOUT_SECS
4895a19609f8Sjv227347 				 * seconds) for the zone's references to clear.
4896a19609f8Sjv227347 				 */
4897a19609f8Sjv227347 				ASSERT(wait_time > 0);
4898a19609f8Sjv227347 				wait_time = cv_reltimedwait_sig(
4899a19609f8Sjv227347 				    &zone_destroy_cv, &zonehash_lock, wait_time,
4900a19609f8Sjv227347 				    TR_SEC);
4901a19609f8Sjv227347 				if (wait_time > 0) {
4902a19609f8Sjv227347 					/*
4903a19609f8Sjv227347 					 * A thread in zone_rele() or
4904a19609f8Sjv227347 					 * zone_cred_rele() signaled
4905a19609f8Sjv227347 					 * zone_destroy_cv before this thread's
4906a19609f8Sjv227347 					 * wait timed out.  The zone might have
4907a19609f8Sjv227347 					 * only one reference left; find out!
4908a19609f8Sjv227347 					 */
4909a19609f8Sjv227347 					continue;
4910a19609f8Sjv227347 				} else if (wait_time == 0) {
4911a19609f8Sjv227347 					/* The thread's process was signaled. */
49127c478bd9Sstevel@tonic-gate 					mutex_exit(&zonehash_lock);
49137c478bd9Sstevel@tonic-gate 					return (set_errno(EINTR));
49147c478bd9Sstevel@tonic-gate 				}
49157c478bd9Sstevel@tonic-gate 
4916a19609f8Sjv227347 				/*
4917a19609f8Sjv227347 				 * The thread timed out while waiting on
4918a19609f8Sjv227347 				 * zone_destroy_cv.  Even though the thread
4919a19609f8Sjv227347 				 * timed out, it has to check whether another
4920a19609f8Sjv227347 				 * thread woke up from zone_destroy_cv and
4921a19609f8Sjv227347 				 * destroyed the zone.
4922a19609f8Sjv227347 				 *
4923a19609f8Sjv227347 				 * If the zone still exists and has more than
4924a19609f8Sjv227347 				 * one unreleased general-purpose reference,
4925a19609f8Sjv227347 				 * then log the zone's reference counts.
4926a19609f8Sjv227347 				 */
4927a19609f8Sjv227347 				log_refcounts = B_TRUE;
4928a19609f8Sjv227347 				continue;
4929a19609f8Sjv227347 			}
4930a19609f8Sjv227347 
4931a19609f8Sjv227347 			/*
4932a19609f8Sjv227347 			 * The thread already timed out on zone_destroy_cv while
4933a19609f8Sjv227347 			 * waiting for subsystems to release the zone's last
4934a19609f8Sjv227347 			 * general-purpose references.  Log the zone's reference
4935a19609f8Sjv227347 			 * counts and wait indefinitely on zone_destroy_cv.
4936a19609f8Sjv227347 			 */
4937a19609f8Sjv227347 			zone_log_refcounts(zone);
4938a19609f8Sjv227347 		}
4939a19609f8Sjv227347 		if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) {
4940a19609f8Sjv227347 			/* The thread's process was signaled. */
4941a19609f8Sjv227347 			mutex_exit(&zonehash_lock);
4942a19609f8Sjv227347 			return (set_errno(EINTR));
4943a19609f8Sjv227347 		}
49447c478bd9Sstevel@tonic-gate 	}
49457c478bd9Sstevel@tonic-gate 
4946c97ad5cdSakolb 	/*
4947c97ad5cdSakolb 	 * Remove CPU cap for this zone now since we're not going to
4948c97ad5cdSakolb 	 * fail below this point.
4949c97ad5cdSakolb 	 */
4950c97ad5cdSakolb 	cpucaps_zone_remove(zone);
4951c97ad5cdSakolb 
4952c97ad5cdSakolb 	/* Get rid of the zone's kstats */
49530209230bSgjelinek 	zone_kstat_delete(zone);
49540209230bSgjelinek 
4955134a1f4eSCasper H.S. Dik 	/* remove the pfexecd doors */
4956134a1f4eSCasper H.S. Dik 	if (zone->zone_pfexecd != NULL) {
4957134a1f4eSCasper H.S. Dik 		klpd_freelist(&zone->zone_pfexecd);
4958134a1f4eSCasper H.S. Dik 		zone->zone_pfexecd = NULL;
4959134a1f4eSCasper H.S. Dik 	}
4960134a1f4eSCasper H.S. Dik 
4961319378d9Seh208807 	/* free brand specific data */
4962319378d9Seh208807 	if (ZONE_IS_BRANDED(zone))
4963319378d9Seh208807 		ZBROP(zone)->b_free_brand_data(zone);
4964319378d9Seh208807 
496552978630Ssl108498 	/* Say goodbye to brand framework. */
496652978630Ssl108498 	brand_unregister_zone(zone->zone_brand);
496752978630Ssl108498 
49687c478bd9Sstevel@tonic-gate 	/*
49697c478bd9Sstevel@tonic-gate 	 * It is now safe to let the zone be recreated; remove it from the
49707c478bd9Sstevel@tonic-gate 	 * lists.  The memory will not be freed until the last cred
49717c478bd9Sstevel@tonic-gate 	 * reference goes away.
49727c478bd9Sstevel@tonic-gate 	 */
49737c478bd9Sstevel@tonic-gate 	ASSERT(zonecount > 1);	/* must be > 1; can't destroy global zone */
49747c478bd9Sstevel@tonic-gate 	zonecount--;
49757c478bd9Sstevel@tonic-gate 	/* remove from active list and hash tables */
49767c478bd9Sstevel@tonic-gate 	list_remove(&zone_active, zone);
49777c478bd9Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyname,
49787c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)zone->zone_name);
49797c478bd9Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyid,
49807c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id);
498148451833Scarlsonj 	if (zone->zone_flags & ZF_HASHED_LABEL)
498245916cd2Sjpk 		(void) mod_hash_destroy(zonehashbylabel,
498345916cd2Sjpk 		    (mod_hash_key_t)zone->zone_slabel);
49847c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
49857c478bd9Sstevel@tonic-gate 
4986108322fbScarlsonj 	/*
4987108322fbScarlsonj 	 * Release the root vnode; we're not using it anymore.  Nor should any
4988108322fbScarlsonj 	 * other thread that might access it exist.
4989108322fbScarlsonj 	 */
4990108322fbScarlsonj 	if (zone->zone_rootvp != NULL) {
4991108322fbScarlsonj 		VN_RELE(zone->zone_rootvp);
4992108322fbScarlsonj 		zone->zone_rootvp = NULL;
4993108322fbScarlsonj 	}
4994108322fbScarlsonj 
49957c478bd9Sstevel@tonic-gate 	/* add to deathrow list */
49967c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_deathrow_lock);
49977c478bd9Sstevel@tonic-gate 	list_insert_tail(&zone_deathrow, zone);
49987c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_deathrow_lock);
49997c478bd9Sstevel@tonic-gate 
50007c478bd9Sstevel@tonic-gate 	/*
50017c478bd9Sstevel@tonic-gate 	 * Drop last reference (which was added by zsched()), this will
50027c478bd9Sstevel@tonic-gate 	 * free the zone unless there are outstanding cred references.
50037c478bd9Sstevel@tonic-gate 	 */
50047c478bd9Sstevel@tonic-gate 	zone_rele(zone);
50057c478bd9Sstevel@tonic-gate 	return (0);
50067c478bd9Sstevel@tonic-gate }
50077c478bd9Sstevel@tonic-gate 
50087c478bd9Sstevel@tonic-gate /*
50097c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_getattr(2).
50107c478bd9Sstevel@tonic-gate  */
50117c478bd9Sstevel@tonic-gate static ssize_t
50127c478bd9Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
50137c478bd9Sstevel@tonic-gate {
50147c478bd9Sstevel@tonic-gate 	size_t size;
50157c478bd9Sstevel@tonic-gate 	int error = 0, err;
50167c478bd9Sstevel@tonic-gate 	zone_t *zone;
50177c478bd9Sstevel@tonic-gate 	char *zonepath;
50183f2f09c1Sdp 	char *outstr;
50197c478bd9Sstevel@tonic-gate 	zone_status_t zone_status;
50207c478bd9Sstevel@tonic-gate 	pid_t initpid;
5021c97ad5cdSakolb 	boolean_t global = (curzone == global_zone);
5022c97ad5cdSakolb 	boolean_t inzone = (curzone->zone_id == zoneid);
5023f4b3ec61Sdh155122 	ushort_t flags;
5024550b6e40SSowmini Varadhan 	zone_net_data_t *zbuf;
50257c478bd9Sstevel@tonic-gate 
50267c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
50277c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
50287c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
50297c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
50307c478bd9Sstevel@tonic-gate 	}
50317c478bd9Sstevel@tonic-gate 	zone_status = zone_status_get(zone);
5032bd41d0a8Snordmark 	if (zone_status < ZONE_IS_INITIALIZED) {
50337c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
50347c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
50357c478bd9Sstevel@tonic-gate 	}
50367c478bd9Sstevel@tonic-gate 	zone_hold(zone);
50377c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
50387c478bd9Sstevel@tonic-gate 
50397c478bd9Sstevel@tonic-gate 	/*
504045916cd2Sjpk 	 * If not in the global zone, don't show information about other zones,
504145916cd2Sjpk 	 * unless the system is labeled and the local zone's label dominates
504245916cd2Sjpk 	 * the other zone.
50437c478bd9Sstevel@tonic-gate 	 */
504445916cd2Sjpk 	if (!zone_list_access(zone)) {
50457c478bd9Sstevel@tonic-gate 		zone_rele(zone);
50467c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
50477c478bd9Sstevel@tonic-gate 	}
50487c478bd9Sstevel@tonic-gate 
50497c478bd9Sstevel@tonic-gate 	switch (attr) {
50507c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_ROOT:
50517c478bd9Sstevel@tonic-gate 		if (global) {
50527c478bd9Sstevel@tonic-gate 			/*
50537c478bd9Sstevel@tonic-gate 			 * Copy the path to trim the trailing "/" (except for
50547c478bd9Sstevel@tonic-gate 			 * the global zone).
50557c478bd9Sstevel@tonic-gate 			 */
50567c478bd9Sstevel@tonic-gate 			if (zone != global_zone)
50577c478bd9Sstevel@tonic-gate 				size = zone->zone_rootpathlen - 1;
50587c478bd9Sstevel@tonic-gate 			else
50597c478bd9Sstevel@tonic-gate 				size = zone->zone_rootpathlen;
50607c478bd9Sstevel@tonic-gate 			zonepath = kmem_alloc(size, KM_SLEEP);
50617c478bd9Sstevel@tonic-gate 			bcopy(zone->zone_rootpath, zonepath, size);
50627c478bd9Sstevel@tonic-gate 			zonepath[size - 1] = '\0';
50637c478bd9Sstevel@tonic-gate 		} else {
5064c97ad5cdSakolb 			if (inzone || !is_system_labeled()) {
50657c478bd9Sstevel@tonic-gate 				/*
506645916cd2Sjpk 				 * Caller is not in the global zone.
506745916cd2Sjpk 				 * if the query is on the current zone
506845916cd2Sjpk 				 * or the system is not labeled,
506945916cd2Sjpk 				 * just return faked-up path for current zone.
50707c478bd9Sstevel@tonic-gate 				 */
50717c478bd9Sstevel@tonic-gate 				zonepath = "/";
50727c478bd9Sstevel@tonic-gate 				size = 2;
507345916cd2Sjpk 			} else {
507445916cd2Sjpk 				/*
507545916cd2Sjpk 				 * Return related path for current zone.
507645916cd2Sjpk 				 */
507745916cd2Sjpk 				int prefix_len = strlen(zone_prefix);
507845916cd2Sjpk 				int zname_len = strlen(zone->zone_name);
507945916cd2Sjpk 
508045916cd2Sjpk 				size = prefix_len + zname_len + 1;
508145916cd2Sjpk 				zonepath = kmem_alloc(size, KM_SLEEP);
508245916cd2Sjpk 				bcopy(zone_prefix, zonepath, prefix_len);
508345916cd2Sjpk 				bcopy(zone->zone_name, zonepath +
508445916cd2Sjpk 				    prefix_len, zname_len);
508545916cd2Sjpk 				zonepath[size - 1] = '\0';
508645916cd2Sjpk 			}
50877c478bd9Sstevel@tonic-gate 		}
50887c478bd9Sstevel@tonic-gate 		if (bufsize > size)
50897c478bd9Sstevel@tonic-gate 			bufsize = size;
50907c478bd9Sstevel@tonic-gate 		if (buf != NULL) {
50917c478bd9Sstevel@tonic-gate 			err = copyoutstr(zonepath, buf, bufsize, NULL);
50927c478bd9Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
50937c478bd9Sstevel@tonic-gate 				error = EFAULT;
50947c478bd9Sstevel@tonic-gate 		}
5095c97ad5cdSakolb 		if (global || (is_system_labeled() && !inzone))
50967c478bd9Sstevel@tonic-gate 			kmem_free(zonepath, size);
50977c478bd9Sstevel@tonic-gate 		break;
50987c478bd9Sstevel@tonic-gate 
50997c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_NAME:
51007c478bd9Sstevel@tonic-gate 		size = strlen(zone->zone_name) + 1;
51017c478bd9Sstevel@tonic-gate 		if (bufsize > size)
51027c478bd9Sstevel@tonic-gate 			bufsize = size;
51037c478bd9Sstevel@tonic-gate 		if (buf != NULL) {
51047c478bd9Sstevel@tonic-gate 			err = copyoutstr(zone->zone_name, buf, bufsize, NULL);
51057c478bd9Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
51067c478bd9Sstevel@tonic-gate 				error = EFAULT;
51077c478bd9Sstevel@tonic-gate 		}
51087c478bd9Sstevel@tonic-gate 		break;
51097c478bd9Sstevel@tonic-gate 
51107c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_STATUS:
51117c478bd9Sstevel@tonic-gate 		/*
51127c478bd9Sstevel@tonic-gate 		 * Since we're not holding zonehash_lock, the zone status
51137c478bd9Sstevel@tonic-gate 		 * may be anything; leave it up to userland to sort it out.
51147c478bd9Sstevel@tonic-gate 		 */
51157c478bd9Sstevel@tonic-gate 		size = sizeof (zone_status);
51167c478bd9Sstevel@tonic-gate 		if (bufsize > size)
51177c478bd9Sstevel@tonic-gate 			bufsize = size;
51187c478bd9Sstevel@tonic-gate 		zone_status = zone_status_get(zone);
51197c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
51207c478bd9Sstevel@tonic-gate 		    copyout(&zone_status, buf, bufsize) != 0)
51217c478bd9Sstevel@tonic-gate 			error = EFAULT;
51227c478bd9Sstevel@tonic-gate 		break;
5123f4b3ec61Sdh155122 	case ZONE_ATTR_FLAGS:
5124f4b3ec61Sdh155122 		size = sizeof (zone->zone_flags);
5125f4b3ec61Sdh155122 		if (bufsize > size)
5126f4b3ec61Sdh155122 			bufsize = size;
5127f4b3ec61Sdh155122 		flags = zone->zone_flags;
5128f4b3ec61Sdh155122 		if (buf != NULL &&
5129f4b3ec61Sdh155122 		    copyout(&flags, buf, bufsize) != 0)
5130f4b3ec61Sdh155122 			error = EFAULT;
5131f4b3ec61Sdh155122 		break;
51327c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_PRIVSET:
51337c478bd9Sstevel@tonic-gate 		size = sizeof (priv_set_t);
51347c478bd9Sstevel@tonic-gate 		if (bufsize > size)
51357c478bd9Sstevel@tonic-gate 			bufsize = size;
51367c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
51377c478bd9Sstevel@tonic-gate 		    copyout(zone->zone_privset, buf, bufsize) != 0)
51387c478bd9Sstevel@tonic-gate 			error = EFAULT;
51397c478bd9Sstevel@tonic-gate 		break;
51407c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_UNIQID:
51417c478bd9Sstevel@tonic-gate 		size = sizeof (zone->zone_uniqid);
51427c478bd9Sstevel@tonic-gate 		if (bufsize > size)
51437c478bd9Sstevel@tonic-gate 			bufsize = size;
51447c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
51457c478bd9Sstevel@tonic-gate 		    copyout(&zone->zone_uniqid, buf, bufsize) != 0)
51467c478bd9Sstevel@tonic-gate 			error = EFAULT;
51477c478bd9Sstevel@tonic-gate 		break;
51487c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_POOLID:
51497c478bd9Sstevel@tonic-gate 		{
51507c478bd9Sstevel@tonic-gate 			pool_t *pool;
51517c478bd9Sstevel@tonic-gate 			poolid_t poolid;
51527c478bd9Sstevel@tonic-gate 
51537c478bd9Sstevel@tonic-gate 			if (pool_lock_intr() != 0) {
51547c478bd9Sstevel@tonic-gate 				error = EINTR;
51557c478bd9Sstevel@tonic-gate 				break;
51567c478bd9Sstevel@tonic-gate 			}
51577c478bd9Sstevel@tonic-gate 			pool = zone_pool_get(zone);
51587c478bd9Sstevel@tonic-gate 			poolid = pool->pool_id;
51597c478bd9Sstevel@tonic-gate 			pool_unlock();
51607c478bd9Sstevel@tonic-gate 			size = sizeof (poolid);
51617c478bd9Sstevel@tonic-gate 			if (bufsize > size)
51627c478bd9Sstevel@tonic-gate 				bufsize = size;
51637c478bd9Sstevel@tonic-gate 			if (buf != NULL && copyout(&poolid, buf, size) != 0)
51647c478bd9Sstevel@tonic-gate 				error = EFAULT;
51657c478bd9Sstevel@tonic-gate 		}
51667c478bd9Sstevel@tonic-gate 		break;
516745916cd2Sjpk 	case ZONE_ATTR_SLBL:
516845916cd2Sjpk 		size = sizeof (bslabel_t);
516945916cd2Sjpk 		if (bufsize > size)
517045916cd2Sjpk 			bufsize = size;
517145916cd2Sjpk 		if (zone->zone_slabel == NULL)
517245916cd2Sjpk 			error = EINVAL;
517345916cd2Sjpk 		else if (buf != NULL &&
517445916cd2Sjpk 		    copyout(label2bslabel(zone->zone_slabel), buf,
517545916cd2Sjpk 		    bufsize) != 0)
517645916cd2Sjpk 			error = EFAULT;
517745916cd2Sjpk 		break;
51787c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_INITPID:
51797c478bd9Sstevel@tonic-gate 		size = sizeof (initpid);
51807c478bd9Sstevel@tonic-gate 		if (bufsize > size)
51817c478bd9Sstevel@tonic-gate 			bufsize = size;
51827c478bd9Sstevel@tonic-gate 		initpid = zone->zone_proc_initpid;
51837c478bd9Sstevel@tonic-gate 		if (initpid == -1) {
51847c478bd9Sstevel@tonic-gate 			error = ESRCH;
51857c478bd9Sstevel@tonic-gate 			break;
51867c478bd9Sstevel@tonic-gate 		}
51877c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
51887c478bd9Sstevel@tonic-gate 		    copyout(&initpid, buf, bufsize) != 0)
51897c478bd9Sstevel@tonic-gate 			error = EFAULT;
51907c478bd9Sstevel@tonic-gate 		break;
51919acbbeafSnn35248 	case ZONE_ATTR_BRAND:
51929acbbeafSnn35248 		size = strlen(zone->zone_brand->b_name) + 1;
51939acbbeafSnn35248 
51949acbbeafSnn35248 		if (bufsize > size)
51959acbbeafSnn35248 			bufsize = size;
51969acbbeafSnn35248 		if (buf != NULL) {
51979acbbeafSnn35248 			err = copyoutstr(zone->zone_brand->b_name, buf,
51989acbbeafSnn35248 			    bufsize, NULL);
51999acbbeafSnn35248 			if (err != 0 && err != ENAMETOOLONG)
52009acbbeafSnn35248 				error = EFAULT;
52019acbbeafSnn35248 		}
52029acbbeafSnn35248 		break;
52033f2f09c1Sdp 	case ZONE_ATTR_INITNAME:
52043f2f09c1Sdp 		size = strlen(zone->zone_initname) + 1;
52053f2f09c1Sdp 		if (bufsize > size)
52063f2f09c1Sdp 			bufsize = size;
52073f2f09c1Sdp 		if (buf != NULL) {
52083f2f09c1Sdp 			err = copyoutstr(zone->zone_initname, buf, bufsize,
52093f2f09c1Sdp 			    NULL);
52103f2f09c1Sdp 			if (err != 0 && err != ENAMETOOLONG)
52113f2f09c1Sdp 				error = EFAULT;
52123f2f09c1Sdp 		}
52133f2f09c1Sdp 		break;
52143f2f09c1Sdp 	case ZONE_ATTR_BOOTARGS:
52153f2f09c1Sdp 		if (zone->zone_bootargs == NULL)
52163f2f09c1Sdp 			outstr = "";
52173f2f09c1Sdp 		else
52183f2f09c1Sdp 			outstr = zone->zone_bootargs;
52193f2f09c1Sdp 		size = strlen(outstr) + 1;
52203f2f09c1Sdp 		if (bufsize > size)
52213f2f09c1Sdp 			bufsize = size;
52223f2f09c1Sdp 		if (buf != NULL) {
52233f2f09c1Sdp 			err = copyoutstr(outstr, buf, bufsize, NULL);
52243f2f09c1Sdp 			if (err != 0 && err != ENAMETOOLONG)
52253f2f09c1Sdp 				error = EFAULT;
52263f2f09c1Sdp 		}
52273f2f09c1Sdp 		break;
52280209230bSgjelinek 	case ZONE_ATTR_PHYS_MCAP:
52290209230bSgjelinek 		size = sizeof (zone->zone_phys_mcap);
52300209230bSgjelinek 		if (bufsize > size)
52310209230bSgjelinek 			bufsize = size;
52320209230bSgjelinek 		if (buf != NULL &&
52330209230bSgjelinek 		    copyout(&zone->zone_phys_mcap, buf, bufsize) != 0)
52340209230bSgjelinek 			error = EFAULT;
52350209230bSgjelinek 		break;
52360209230bSgjelinek 	case ZONE_ATTR_SCHED_CLASS:
52370209230bSgjelinek 		mutex_enter(&class_lock);
52380209230bSgjelinek 
52390209230bSgjelinek 		if (zone->zone_defaultcid >= loaded_classes)
52400209230bSgjelinek 			outstr = "";
52410209230bSgjelinek 		else
52420209230bSgjelinek 			outstr = sclass[zone->zone_defaultcid].cl_name;
52430209230bSgjelinek 		size = strlen(outstr) + 1;
52440209230bSgjelinek 		if (bufsize > size)
52450209230bSgjelinek 			bufsize = size;
52460209230bSgjelinek 		if (buf != NULL) {
52470209230bSgjelinek 			err = copyoutstr(outstr, buf, bufsize, NULL);
52480209230bSgjelinek 			if (err != 0 && err != ENAMETOOLONG)
52490209230bSgjelinek 				error = EFAULT;
52500209230bSgjelinek 		}
52510209230bSgjelinek 
52520209230bSgjelinek 		mutex_exit(&class_lock);
52530209230bSgjelinek 		break;
52545679c89fSjv227347 	case ZONE_ATTR_HOSTID:
52555679c89fSjv227347 		if (zone->zone_hostid != HW_INVALID_HOSTID &&
52565679c89fSjv227347 		    bufsize == sizeof (zone->zone_hostid)) {
52575679c89fSjv227347 			size = sizeof (zone->zone_hostid);
52585679c89fSjv227347 			if (buf != NULL && copyout(&zone->zone_hostid, buf,
52595679c89fSjv227347 			    bufsize) != 0)
52605679c89fSjv227347 				error = EFAULT;
52615679c89fSjv227347 		} else {
52625679c89fSjv227347 			error = EINVAL;
52635679c89fSjv227347 		}
52645679c89fSjv227347 		break;
52650fbb751dSJohn Levon 	case ZONE_ATTR_FS_ALLOWED:
52660fbb751dSJohn Levon 		if (zone->zone_fs_allowed == NULL)
52670fbb751dSJohn Levon 			outstr = "";
52680fbb751dSJohn Levon 		else
52690fbb751dSJohn Levon 			outstr = zone->zone_fs_allowed;
52700fbb751dSJohn Levon 		size = strlen(outstr) + 1;
52710fbb751dSJohn Levon 		if (bufsize > size)
52720fbb751dSJohn Levon 			bufsize = size;
52730fbb751dSJohn Levon 		if (buf != NULL) {
52740fbb751dSJohn Levon 			err = copyoutstr(outstr, buf, bufsize, NULL);
52750fbb751dSJohn Levon 			if (err != 0 && err != ENAMETOOLONG)
52760fbb751dSJohn Levon 				error = EFAULT;
52770fbb751dSJohn Levon 		}
52780fbb751dSJohn Levon 		break;
5279550b6e40SSowmini Varadhan 	case ZONE_ATTR_NETWORK:
5280550b6e40SSowmini Varadhan 		zbuf = kmem_alloc(bufsize, KM_SLEEP);
5281550b6e40SSowmini Varadhan 		if (copyin(buf, zbuf, bufsize) != 0) {
5282550b6e40SSowmini Varadhan 			error = EFAULT;
5283550b6e40SSowmini Varadhan 		} else {
5284550b6e40SSowmini Varadhan 			error = zone_get_network(zoneid, zbuf);
5285550b6e40SSowmini Varadhan 			if (error == 0 && copyout(zbuf, buf, bufsize) != 0)
5286550b6e40SSowmini Varadhan 				error = EFAULT;
5287550b6e40SSowmini Varadhan 		}
5288550b6e40SSowmini Varadhan 		kmem_free(zbuf, bufsize);
5289550b6e40SSowmini Varadhan 		break;
52907c478bd9Sstevel@tonic-gate 	default:
52919acbbeafSnn35248 		if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) {
52929acbbeafSnn35248 			size = bufsize;
52939acbbeafSnn35248 			error = ZBROP(zone)->b_getattr(zone, attr, buf, &size);
52949acbbeafSnn35248 		} else {
52957c478bd9Sstevel@tonic-gate 			error = EINVAL;
52967c478bd9Sstevel@tonic-gate 		}
52979acbbeafSnn35248 	}
52987c478bd9Sstevel@tonic-gate 	zone_rele(zone);
52997c478bd9Sstevel@tonic-gate 
53007c478bd9Sstevel@tonic-gate 	if (error)
53017c478bd9Sstevel@tonic-gate 		return (set_errno(error));
53027c478bd9Sstevel@tonic-gate 	return ((ssize_t)size);
53037c478bd9Sstevel@tonic-gate }
53047c478bd9Sstevel@tonic-gate 
53057c478bd9Sstevel@tonic-gate /*
53063f2f09c1Sdp  * Systemcall entry point for zone_setattr(2).
53073f2f09c1Sdp  */
53083f2f09c1Sdp /*ARGSUSED*/
53093f2f09c1Sdp static int
53103f2f09c1Sdp zone_setattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
53113f2f09c1Sdp {
53123f2f09c1Sdp 	zone_t *zone;
53133f2f09c1Sdp 	zone_status_t zone_status;
53141a88ce5cSDan Price 	int err = -1;
5315550b6e40SSowmini Varadhan 	zone_net_data_t *zbuf;
53163f2f09c1Sdp 
53173f2f09c1Sdp 	if (secpolicy_zone_config(CRED()) != 0)
53183f2f09c1Sdp 		return (set_errno(EPERM));
53193f2f09c1Sdp 
53203f2f09c1Sdp 	/*
53210209230bSgjelinek 	 * Only the ZONE_ATTR_PHYS_MCAP attribute can be set on the
53220209230bSgjelinek 	 * global zone.
53233f2f09c1Sdp 	 */
53240209230bSgjelinek 	if (zoneid == GLOBAL_ZONEID && attr != ZONE_ATTR_PHYS_MCAP) {
53253f2f09c1Sdp 		return (set_errno(EINVAL));
53263f2f09c1Sdp 	}
53273f2f09c1Sdp 
53283f2f09c1Sdp 	mutex_enter(&zonehash_lock);
53293f2f09c1Sdp 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
53303f2f09c1Sdp 		mutex_exit(&zonehash_lock);
53313f2f09c1Sdp 		return (set_errno(EINVAL));
53323f2f09c1Sdp 	}
53333f2f09c1Sdp 	zone_hold(zone);
53343f2f09c1Sdp 	mutex_exit(&zonehash_lock);
53353f2f09c1Sdp 
53360209230bSgjelinek 	/*
53370209230bSgjelinek 	 * At present most attributes can only be set on non-running,
53380209230bSgjelinek 	 * non-global zones.
53390209230bSgjelinek 	 */
53403f2f09c1Sdp 	zone_status = zone_status_get(zone);
53411a88ce5cSDan Price 	if (attr != ZONE_ATTR_PHYS_MCAP && zone_status > ZONE_IS_READY) {
53421a88ce5cSDan Price 		err = EINVAL;
53433f2f09c1Sdp 		goto done;
53441a88ce5cSDan Price 	}
53453f2f09c1Sdp 
53463f2f09c1Sdp 	switch (attr) {
53473f2f09c1Sdp 	case ZONE_ATTR_INITNAME:
53483f2f09c1Sdp 		err = zone_set_initname(zone, (const char *)buf);
53493f2f09c1Sdp 		break;
53503f2f09c1Sdp 	case ZONE_ATTR_BOOTARGS:
53513f2f09c1Sdp 		err = zone_set_bootargs(zone, (const char *)buf);
53523f2f09c1Sdp 		break;
53539acbbeafSnn35248 	case ZONE_ATTR_BRAND:
535459f2ff5cSedp 		err = zone_set_brand(zone, (const char *)buf);
53559acbbeafSnn35248 		break;
53560fbb751dSJohn Levon 	case ZONE_ATTR_FS_ALLOWED:
53570fbb751dSJohn Levon 		err = zone_set_fs_allowed(zone, (const char *)buf);
53580fbb751dSJohn Levon 		break;
53590209230bSgjelinek 	case ZONE_ATTR_PHYS_MCAP:
53600209230bSgjelinek 		err = zone_set_phys_mcap(zone, (const uint64_t *)buf);
53610209230bSgjelinek 		break;
53620209230bSgjelinek 	case ZONE_ATTR_SCHED_CLASS:
53630209230bSgjelinek 		err = zone_set_sched_class(zone, (const char *)buf);
53640209230bSgjelinek 		break;
53655679c89fSjv227347 	case ZONE_ATTR_HOSTID:
53665679c89fSjv227347 		if (bufsize == sizeof (zone->zone_hostid)) {
53675679c89fSjv227347 			if (copyin(buf, &zone->zone_hostid, bufsize) == 0)
53685679c89fSjv227347 				err = 0;
53695679c89fSjv227347 			else
53705679c89fSjv227347 				err = EFAULT;
53715679c89fSjv227347 		} else {
53725679c89fSjv227347 			err = EINVAL;
53735679c89fSjv227347 		}
53745679c89fSjv227347 		break;
5375550b6e40SSowmini Varadhan 	case ZONE_ATTR_NETWORK:
5376550b6e40SSowmini Varadhan 		if (bufsize > (PIPE_BUF + sizeof (zone_net_data_t))) {
5377550b6e40SSowmini Varadhan 			err = EINVAL;
53781a88ce5cSDan Price 			break;
5379550b6e40SSowmini Varadhan 		}
5380550b6e40SSowmini Varadhan 		zbuf = kmem_alloc(bufsize, KM_SLEEP);
5381550b6e40SSowmini Varadhan 		if (copyin(buf, zbuf, bufsize) != 0) {
53821a88ce5cSDan Price 			kmem_free(zbuf, bufsize);
5383550b6e40SSowmini Varadhan 			err = EFAULT;
53841a88ce5cSDan Price 			break;
5385550b6e40SSowmini Varadhan 		}
5386550b6e40SSowmini Varadhan 		err = zone_set_network(zoneid, zbuf);
5387550b6e40SSowmini Varadhan 		kmem_free(zbuf, bufsize);
5388550b6e40SSowmini Varadhan 		break;
53893f2f09c1Sdp 	default:
53909acbbeafSnn35248 		if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone))
53919acbbeafSnn35248 			err = ZBROP(zone)->b_setattr(zone, attr, buf, bufsize);
53929acbbeafSnn35248 		else
53933f2f09c1Sdp 			err = EINVAL;
53943f2f09c1Sdp 	}
53953f2f09c1Sdp 
53963f2f09c1Sdp done:
53973f2f09c1Sdp 	zone_rele(zone);
53981a88ce5cSDan Price 	ASSERT(err != -1);
53993f2f09c1Sdp 	return (err != 0 ? set_errno(err) : 0);
54003f2f09c1Sdp }
54013f2f09c1Sdp 
54023f2f09c1Sdp /*
54037c478bd9Sstevel@tonic-gate  * Return zero if the process has at least one vnode mapped in to its
54047c478bd9Sstevel@tonic-gate  * address space which shouldn't be allowed to change zones.
54050209230bSgjelinek  *
54060209230bSgjelinek  * Also return zero if the process has any shared mappings which reserve
54070209230bSgjelinek  * swap.  This is because the counting for zone.max-swap does not allow swap
5408da6c28aaSamw  * reservation to be shared between zones.  zone swap reservation is counted
54090209230bSgjelinek  * on zone->zone_max_swap.
54107c478bd9Sstevel@tonic-gate  */
54117c478bd9Sstevel@tonic-gate static int
54127c478bd9Sstevel@tonic-gate as_can_change_zones(void)
54137c478bd9Sstevel@tonic-gate {
54147c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
54157c478bd9Sstevel@tonic-gate 	struct seg *seg;
54167c478bd9Sstevel@tonic-gate 	struct as *as = pp->p_as;
54177c478bd9Sstevel@tonic-gate 	vnode_t *vp;
54187c478bd9Sstevel@tonic-gate 	int allow = 1;
54197c478bd9Sstevel@tonic-gate 
54207c478bd9Sstevel@tonic-gate 	ASSERT(pp->p_as != &kas);
54210209230bSgjelinek 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
54227c478bd9Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
54230209230bSgjelinek 
54240209230bSgjelinek 		/*
54250209230bSgjelinek 		 * Cannot enter zone with shared anon memory which
54260209230bSgjelinek 		 * reserves swap.  See comment above.
54270209230bSgjelinek 		 */
54280209230bSgjelinek 		if (seg_can_change_zones(seg) == B_FALSE) {
54290209230bSgjelinek 			allow = 0;
54300209230bSgjelinek 			break;
54310209230bSgjelinek 		}
54327c478bd9Sstevel@tonic-gate 		/*
54337c478bd9Sstevel@tonic-gate 		 * if we can't get a backing vnode for this segment then skip
54347c478bd9Sstevel@tonic-gate 		 * it.
54357c478bd9Sstevel@tonic-gate 		 */
54367c478bd9Sstevel@tonic-gate 		vp = NULL;
54377c478bd9Sstevel@tonic-gate 		if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL)
54387c478bd9Sstevel@tonic-gate 			continue;
54397c478bd9Sstevel@tonic-gate 		if (!vn_can_change_zones(vp)) { /* bail on first match */
54407c478bd9Sstevel@tonic-gate 			allow = 0;
54417c478bd9Sstevel@tonic-gate 			break;
54427c478bd9Sstevel@tonic-gate 		}
54437c478bd9Sstevel@tonic-gate 	}
54440209230bSgjelinek 	AS_LOCK_EXIT(as, &as->a_lock);
54457c478bd9Sstevel@tonic-gate 	return (allow);
54467c478bd9Sstevel@tonic-gate }
54477c478bd9Sstevel@tonic-gate 
54487c478bd9Sstevel@tonic-gate /*
54490209230bSgjelinek  * Count swap reserved by curproc's address space
54500209230bSgjelinek  */
54510209230bSgjelinek static size_t
54520209230bSgjelinek as_swresv(void)
54530209230bSgjelinek {
54540209230bSgjelinek 	proc_t *pp = curproc;
54550209230bSgjelinek 	struct seg *seg;
54560209230bSgjelinek 	struct as *as = pp->p_as;
54570209230bSgjelinek 	size_t swap = 0;
54580209230bSgjelinek 
54590209230bSgjelinek 	ASSERT(pp->p_as != &kas);
54600209230bSgjelinek 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
54610209230bSgjelinek 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg))
54620209230bSgjelinek 		swap += seg_swresv(seg);
54630209230bSgjelinek 
54640209230bSgjelinek 	return (swap);
54650209230bSgjelinek }
54660209230bSgjelinek 
54670209230bSgjelinek /*
54687c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_enter().
54697c478bd9Sstevel@tonic-gate  *
54707c478bd9Sstevel@tonic-gate  * The current process is injected into said zone.  In the process
54717c478bd9Sstevel@tonic-gate  * it will change its project membership, privileges, rootdir/cwd,
54727c478bd9Sstevel@tonic-gate  * zone-wide rctls, and pool association to match those of the zone.
54737c478bd9Sstevel@tonic-gate  *
54747c478bd9Sstevel@tonic-gate  * The first zone_enter() called while the zone is in the ZONE_IS_READY
54757c478bd9Sstevel@tonic-gate  * state will transition it to ZONE_IS_RUNNING.  Processes may only
54767c478bd9Sstevel@tonic-gate  * enter a zone that is "ready" or "running".
54777c478bd9Sstevel@tonic-gate  */
54787c478bd9Sstevel@tonic-gate static int
54797c478bd9Sstevel@tonic-gate zone_enter(zoneid_t zoneid)
54807c478bd9Sstevel@tonic-gate {
54817c478bd9Sstevel@tonic-gate 	zone_t *zone;
54827c478bd9Sstevel@tonic-gate 	vnode_t *vp;
54837c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
54847c478bd9Sstevel@tonic-gate 	contract_t *ct;
54857c478bd9Sstevel@tonic-gate 	cont_process_t *ctp;
54867c478bd9Sstevel@tonic-gate 	task_t *tk, *oldtk;
54877c478bd9Sstevel@tonic-gate 	kproject_t *zone_proj0;
54887c478bd9Sstevel@tonic-gate 	cred_t *cr, *newcr;
54897c478bd9Sstevel@tonic-gate 	pool_t *oldpool, *newpool;
54907c478bd9Sstevel@tonic-gate 	sess_t *sp;
54917c478bd9Sstevel@tonic-gate 	uid_t uid;
54927c478bd9Sstevel@tonic-gate 	zone_status_t status;
54937c478bd9Sstevel@tonic-gate 	int err = 0;
54947c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
54950209230bSgjelinek 	size_t swap;
5496c97ad5cdSakolb 	kthread_id_t t;
54977c478bd9Sstevel@tonic-gate 
54987c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
54997c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
55007c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
55017c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
55027c478bd9Sstevel@tonic-gate 
55037c478bd9Sstevel@tonic-gate 	/*
55047c478bd9Sstevel@tonic-gate 	 * Stop all lwps so we don't need to hold a lock to look at
55057c478bd9Sstevel@tonic-gate 	 * curproc->p_zone.  This needs to happen before we grab any
55067c478bd9Sstevel@tonic-gate 	 * locks to avoid deadlock (another lwp in the process could
55077c478bd9Sstevel@tonic-gate 	 * be waiting for the held lock).
55087c478bd9Sstevel@tonic-gate 	 */
55097c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK))
55107c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
55117c478bd9Sstevel@tonic-gate 
55127c478bd9Sstevel@tonic-gate 	/*
55137c478bd9Sstevel@tonic-gate 	 * Make sure we're not changing zones with files open or mapped in
55147c478bd9Sstevel@tonic-gate 	 * to our address space which shouldn't be changing zones.
55157c478bd9Sstevel@tonic-gate 	 */
55167c478bd9Sstevel@tonic-gate 	if (!files_can_change_zones()) {
55177c478bd9Sstevel@tonic-gate 		err = EBADF;
55187c478bd9Sstevel@tonic-gate 		goto out;
55197c478bd9Sstevel@tonic-gate 	}
55207c478bd9Sstevel@tonic-gate 	if (!as_can_change_zones()) {
55217c478bd9Sstevel@tonic-gate 		err = EFAULT;
55227c478bd9Sstevel@tonic-gate 		goto out;
55237c478bd9Sstevel@tonic-gate 	}
55247c478bd9Sstevel@tonic-gate 
55257c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
55267c478bd9Sstevel@tonic-gate 	if (pp->p_zone != global_zone) {
55277c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
55287c478bd9Sstevel@tonic-gate 		err = EINVAL;
55297c478bd9Sstevel@tonic-gate 		goto out;
55307c478bd9Sstevel@tonic-gate 	}
55317c478bd9Sstevel@tonic-gate 
55327c478bd9Sstevel@tonic-gate 	zone = zone_find_all_by_id(zoneid);
55337c478bd9Sstevel@tonic-gate 	if (zone == NULL) {
55347c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
55357c478bd9Sstevel@tonic-gate 		err = EINVAL;
55367c478bd9Sstevel@tonic-gate 		goto out;
55377c478bd9Sstevel@tonic-gate 	}
55387c478bd9Sstevel@tonic-gate 
55397c478bd9Sstevel@tonic-gate 	/*
55407c478bd9Sstevel@tonic-gate 	 * To prevent processes in a zone from holding contracts on
55417c478bd9Sstevel@tonic-gate 	 * extrazonal resources, and to avoid process contract
55427c478bd9Sstevel@tonic-gate 	 * memberships which span zones, contract holders and processes
55437c478bd9Sstevel@tonic-gate 	 * which aren't the sole members of their encapsulating process
55447c478bd9Sstevel@tonic-gate 	 * contracts are not allowed to zone_enter.
55457c478bd9Sstevel@tonic-gate 	 */
55467c478bd9Sstevel@tonic-gate 	ctp = pp->p_ct_process;
55477c478bd9Sstevel@tonic-gate 	ct = &ctp->conp_contract;
55487c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
55497c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
55507c478bd9Sstevel@tonic-gate 	if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) {
55517c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
55527c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
55537c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
55547c478bd9Sstevel@tonic-gate 		err = EINVAL;
55557c478bd9Sstevel@tonic-gate 		goto out;
55567c478bd9Sstevel@tonic-gate 	}
55577c478bd9Sstevel@tonic-gate 
55587c478bd9Sstevel@tonic-gate 	/*
55597c478bd9Sstevel@tonic-gate 	 * Moreover, we don't allow processes whose encapsulating
55607c478bd9Sstevel@tonic-gate 	 * process contracts have inherited extrazonal contracts.
55617c478bd9Sstevel@tonic-gate 	 * While it would be easier to eliminate all process contracts
55627c478bd9Sstevel@tonic-gate 	 * with inherited contracts, we need to be able to give a
55637c478bd9Sstevel@tonic-gate 	 * restarted init (or other zone-penetrating process) its
55647c478bd9Sstevel@tonic-gate 	 * predecessor's contracts.
55657c478bd9Sstevel@tonic-gate 	 */
55667c478bd9Sstevel@tonic-gate 	if (ctp->conp_ninherited != 0) {
55677c478bd9Sstevel@tonic-gate 		contract_t *next;
55687c478bd9Sstevel@tonic-gate 		for (next = list_head(&ctp->conp_inherited); next;
55697c478bd9Sstevel@tonic-gate 		    next = list_next(&ctp->conp_inherited, next)) {
55707c478bd9Sstevel@tonic-gate 			if (contract_getzuniqid(next) != zone->zone_uniqid) {
55717c478bd9Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
55727c478bd9Sstevel@tonic-gate 				mutex_exit(&ct->ct_lock);
55737c478bd9Sstevel@tonic-gate 				mutex_exit(&zonehash_lock);
55747c478bd9Sstevel@tonic-gate 				err = EINVAL;
55757c478bd9Sstevel@tonic-gate 				goto out;
55767c478bd9Sstevel@tonic-gate 			}
55777c478bd9Sstevel@tonic-gate 		}
55787c478bd9Sstevel@tonic-gate 	}
55797b209c2cSacruz 
55807c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
55817c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
55827c478bd9Sstevel@tonic-gate 
55837c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
55847c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) {
55857c478bd9Sstevel@tonic-gate 		/*
55867c478bd9Sstevel@tonic-gate 		 * Can't join
55877c478bd9Sstevel@tonic-gate 		 */
55887c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
55897c478bd9Sstevel@tonic-gate 		err = EINVAL;
55907c478bd9Sstevel@tonic-gate 		goto out;
55917c478bd9Sstevel@tonic-gate 	}
55927c478bd9Sstevel@tonic-gate 
55937c478bd9Sstevel@tonic-gate 	/*
55947c478bd9Sstevel@tonic-gate 	 * Make sure new priv set is within the permitted set for caller
55957c478bd9Sstevel@tonic-gate 	 */
55967c478bd9Sstevel@tonic-gate 	if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) {
55977c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
55987c478bd9Sstevel@tonic-gate 		err = EPERM;
55997c478bd9Sstevel@tonic-gate 		goto out;
56007c478bd9Sstevel@tonic-gate 	}
56017c478bd9Sstevel@tonic-gate 	/*
56027c478bd9Sstevel@tonic-gate 	 * We want to momentarily drop zonehash_lock while we optimistically
56037c478bd9Sstevel@tonic-gate 	 * bind curproc to the pool it should be running in.  This is safe
56047c478bd9Sstevel@tonic-gate 	 * since the zone can't disappear (we have a hold on it).
56057c478bd9Sstevel@tonic-gate 	 */
56067c478bd9Sstevel@tonic-gate 	zone_hold(zone);
56077c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
56087c478bd9Sstevel@tonic-gate 
56097c478bd9Sstevel@tonic-gate 	/*
56107c478bd9Sstevel@tonic-gate 	 * Grab pool_lock to keep the pools configuration from changing
56117c478bd9Sstevel@tonic-gate 	 * and to stop ourselves from getting rebound to another pool
56127c478bd9Sstevel@tonic-gate 	 * until we join the zone.
56137c478bd9Sstevel@tonic-gate 	 */
56147c478bd9Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
56157c478bd9Sstevel@tonic-gate 		zone_rele(zone);
56167c478bd9Sstevel@tonic-gate 		err = EINTR;
56177c478bd9Sstevel@tonic-gate 		goto out;
56187c478bd9Sstevel@tonic-gate 	}
56197c478bd9Sstevel@tonic-gate 	ASSERT(secpolicy_pool(CRED()) == 0);
56207c478bd9Sstevel@tonic-gate 	/*
56217c478bd9Sstevel@tonic-gate 	 * Bind ourselves to the pool currently associated with the zone.
56227c478bd9Sstevel@tonic-gate 	 */
56237c478bd9Sstevel@tonic-gate 	oldpool = curproc->p_pool;
56247c478bd9Sstevel@tonic-gate 	newpool = zone_pool_get(zone);
56257c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED && newpool != oldpool &&
56267c478bd9Sstevel@tonic-gate 	    (err = pool_do_bind(newpool, P_PID, P_MYID,
56277c478bd9Sstevel@tonic-gate 	    POOL_BIND_ALL)) != 0) {
56287c478bd9Sstevel@tonic-gate 		pool_unlock();
56297c478bd9Sstevel@tonic-gate 		zone_rele(zone);
56307c478bd9Sstevel@tonic-gate 		goto out;
56317c478bd9Sstevel@tonic-gate 	}
56327c478bd9Sstevel@tonic-gate 
56337c478bd9Sstevel@tonic-gate 	/*
56347c478bd9Sstevel@tonic-gate 	 * Grab cpu_lock now; we'll need it later when we call
56357c478bd9Sstevel@tonic-gate 	 * task_join().
56367c478bd9Sstevel@tonic-gate 	 */
56377c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
56387c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
56397c478bd9Sstevel@tonic-gate 	/*
56407c478bd9Sstevel@tonic-gate 	 * Make sure the zone hasn't moved on since we dropped zonehash_lock.
56417c478bd9Sstevel@tonic-gate 	 */
56427c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
56437c478bd9Sstevel@tonic-gate 		/*
56447c478bd9Sstevel@tonic-gate 		 * Can't join anymore.
56457c478bd9Sstevel@tonic-gate 		 */
56467c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
56477c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
56487c478bd9Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED &&
56497c478bd9Sstevel@tonic-gate 		    newpool != oldpool)
56507c478bd9Sstevel@tonic-gate 			(void) pool_do_bind(oldpool, P_PID, P_MYID,
56517c478bd9Sstevel@tonic-gate 			    POOL_BIND_ALL);
56527c478bd9Sstevel@tonic-gate 		pool_unlock();
56537c478bd9Sstevel@tonic-gate 		zone_rele(zone);
56547c478bd9Sstevel@tonic-gate 		err = EINVAL;
56557c478bd9Sstevel@tonic-gate 		goto out;
56567c478bd9Sstevel@tonic-gate 	}
56577c478bd9Sstevel@tonic-gate 
56580209230bSgjelinek 	/*
56590209230bSgjelinek 	 * a_lock must be held while transfering locked memory and swap
56600209230bSgjelinek 	 * reservation from the global zone to the non global zone because
56610209230bSgjelinek 	 * asynchronous faults on the processes' address space can lock
56620209230bSgjelinek 	 * memory and reserve swap via MCL_FUTURE and MAP_NORESERVE
56630209230bSgjelinek 	 * segments respectively.
56640209230bSgjelinek 	 */
56650209230bSgjelinek 	AS_LOCK_ENTER(pp->as, &pp->p_as->a_lock, RW_WRITER);
56660209230bSgjelinek 	swap = as_swresv();
56677c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
56687c478bd9Sstevel@tonic-gate 	zone_proj0 = zone->zone_zsched->p_task->tk_proj;
56697c478bd9Sstevel@tonic-gate 	/* verify that we do not exceed and task or lwp limits */
56707c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
56717c478bd9Sstevel@tonic-gate 	/* add new lwps to zone and zone's proj0 */
56727c478bd9Sstevel@tonic-gate 	zone_proj0->kpj_nlwps += pp->p_lwpcnt;
56737c478bd9Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
56747c478bd9Sstevel@tonic-gate 	/* add 1 task to zone's proj0 */
56757c478bd9Sstevel@tonic-gate 	zone_proj0->kpj_ntasks += 1;
5676ff19e029SMenno Lageman 
5677ff19e029SMenno Lageman 	zone_proj0->kpj_nprocs++;
5678ff19e029SMenno Lageman 	zone->zone_nprocs++;
56797c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
56807c478bd9Sstevel@tonic-gate 
56810209230bSgjelinek 	mutex_enter(&zone->zone_mem_lock);
5682c6939658Ssl108498 	zone->zone_locked_mem += pp->p_locked_mem;
5683c6939658Ssl108498 	zone_proj0->kpj_data.kpd_locked_mem += pp->p_locked_mem;
56840209230bSgjelinek 	zone->zone_max_swap += swap;
56850209230bSgjelinek 	mutex_exit(&zone->zone_mem_lock);
5686c6939658Ssl108498 
5687c1a9a9c3Skrishna 	mutex_enter(&(zone_proj0->kpj_data.kpd_crypto_lock));
5688c1a9a9c3Skrishna 	zone_proj0->kpj_data.kpd_crypto_mem += pp->p_crypto_mem;
5689c1a9a9c3Skrishna 	mutex_exit(&(zone_proj0->kpj_data.kpd_crypto_lock));
5690c1a9a9c3Skrishna 
5691ff19e029SMenno Lageman 	/* remove lwps and process from proc's old zone and old project */
56927c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_zone->zone_nlwps_lock);
56937c478bd9Sstevel@tonic-gate 	pp->p_zone->zone_nlwps -= pp->p_lwpcnt;
56947c478bd9Sstevel@tonic-gate 	pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt;
5695ff19e029SMenno Lageman 	pp->p_task->tk_proj->kpj_nprocs--;
5696ff19e029SMenno Lageman 	pp->p_zone->zone_nprocs--;
56977c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_zone->zone_nlwps_lock);
56987c478bd9Sstevel@tonic-gate 
56990209230bSgjelinek 	mutex_enter(&pp->p_zone->zone_mem_lock);
5700c6939658Ssl108498 	pp->p_zone->zone_locked_mem -= pp->p_locked_mem;
5701c6939658Ssl108498 	pp->p_task->tk_proj->kpj_data.kpd_locked_mem -= pp->p_locked_mem;
57020209230bSgjelinek 	pp->p_zone->zone_max_swap -= swap;
57030209230bSgjelinek 	mutex_exit(&pp->p_zone->zone_mem_lock);
5704c6939658Ssl108498 
5705c1a9a9c3Skrishna 	mutex_enter(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock));
5706c1a9a9c3Skrishna 	pp->p_task->tk_proj->kpj_data.kpd_crypto_mem -= pp->p_crypto_mem;
5707c1a9a9c3Skrishna 	mutex_exit(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock));
5708c1a9a9c3Skrishna 
5709bb5ca623SVamsi Nagineni 	pp->p_flag |= SZONETOP;
5710bb5ca623SVamsi Nagineni 	pp->p_zone = zone;
5711c6939658Ssl108498 	mutex_exit(&pp->p_lock);
57120209230bSgjelinek 	AS_LOCK_EXIT(pp->p_as, &pp->p_as->a_lock);
5713c6939658Ssl108498 
57147c478bd9Sstevel@tonic-gate 	/*
57157c478bd9Sstevel@tonic-gate 	 * Joining the zone cannot fail from now on.
57167c478bd9Sstevel@tonic-gate 	 *
57177c478bd9Sstevel@tonic-gate 	 * This means that a lot of the following code can be commonized and
57187c478bd9Sstevel@tonic-gate 	 * shared with zsched().
57197c478bd9Sstevel@tonic-gate 	 */
57207c478bd9Sstevel@tonic-gate 
57217c478bd9Sstevel@tonic-gate 	/*
57227b209c2cSacruz 	 * If the process contract fmri was inherited, we need to
57237b209c2cSacruz 	 * flag this so that any contract status will not leak
57247b209c2cSacruz 	 * extra zone information, svc_fmri in this case
57257b209c2cSacruz 	 */
57267b209c2cSacruz 	if (ctp->conp_svc_ctid != ct->ct_id) {
57277b209c2cSacruz 		mutex_enter(&ct->ct_lock);
57287b209c2cSacruz 		ctp->conp_svc_zone_enter = ct->ct_id;
57297b209c2cSacruz 		mutex_exit(&ct->ct_lock);
57307b209c2cSacruz 	}
57317b209c2cSacruz 
57327b209c2cSacruz 	/*
57337c478bd9Sstevel@tonic-gate 	 * Reset the encapsulating process contract's zone.
57347c478bd9Sstevel@tonic-gate 	 */
57357c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID);
57367c478bd9Sstevel@tonic-gate 	contract_setzuniqid(ct, zone->zone_uniqid);
57377c478bd9Sstevel@tonic-gate 
57387c478bd9Sstevel@tonic-gate 	/*
57397c478bd9Sstevel@tonic-gate 	 * Create a new task and associate the process with the project keyed
57407c478bd9Sstevel@tonic-gate 	 * by (projid,zoneid).
57417c478bd9Sstevel@tonic-gate 	 *
57427c478bd9Sstevel@tonic-gate 	 * We might as well be in project 0; the global zone's projid doesn't
57437c478bd9Sstevel@tonic-gate 	 * make much sense in a zone anyhow.
57447c478bd9Sstevel@tonic-gate 	 *
57457c478bd9Sstevel@tonic-gate 	 * This also increments zone_ntasks, and returns with p_lock held.
57467c478bd9Sstevel@tonic-gate 	 */
57477c478bd9Sstevel@tonic-gate 	tk = task_create(0, zone);
57487c478bd9Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
57497c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
57507c478bd9Sstevel@tonic-gate 
57517c478bd9Sstevel@tonic-gate 	/*
57527c478bd9Sstevel@tonic-gate 	 * call RCTLOP_SET functions on this proc
57537c478bd9Sstevel@tonic-gate 	 */
57547c478bd9Sstevel@tonic-gate 	e.rcep_p.zone = zone;
57557c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
57567c478bd9Sstevel@tonic-gate 	(void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL,
57577c478bd9Sstevel@tonic-gate 	    RCD_CALLBACK);
57587c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
57597c478bd9Sstevel@tonic-gate 
57607c478bd9Sstevel@tonic-gate 	/*
57617c478bd9Sstevel@tonic-gate 	 * We don't need to hold any of zsched's locks here; not only do we know
57627c478bd9Sstevel@tonic-gate 	 * the process and zone aren't going away, we know its session isn't
57637c478bd9Sstevel@tonic-gate 	 * changing either.
57647c478bd9Sstevel@tonic-gate 	 *
57657c478bd9Sstevel@tonic-gate 	 * By joining zsched's session here, we mimic the behavior in the
57667c478bd9Sstevel@tonic-gate 	 * global zone of init's sid being the pid of sched.  We extend this
57677c478bd9Sstevel@tonic-gate 	 * to all zlogin-like zone_enter()'ing processes as well.
57687c478bd9Sstevel@tonic-gate 	 */
57697c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
57707c478bd9Sstevel@tonic-gate 	sp = zone->zone_zsched->p_sessp;
57719acbbeafSnn35248 	sess_hold(zone->zone_zsched);
57727c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
57737c478bd9Sstevel@tonic-gate 	pgexit(pp);
57749acbbeafSnn35248 	sess_rele(pp->p_sessp, B_TRUE);
57757c478bd9Sstevel@tonic-gate 	pp->p_sessp = sp;
57767c478bd9Sstevel@tonic-gate 	pgjoin(pp, zone->zone_zsched->p_pidp);
57770209230bSgjelinek 
57780209230bSgjelinek 	/*
5779c97ad5cdSakolb 	 * If any threads are scheduled to be placed on zone wait queue they
5780c97ad5cdSakolb 	 * should abandon the idea since the wait queue is changing.
5781c97ad5cdSakolb 	 * We need to be holding pidlock & p_lock to do this.
5782c97ad5cdSakolb 	 */
5783c97ad5cdSakolb 	if ((t = pp->p_tlist) != NULL) {
5784c97ad5cdSakolb 		do {
5785c97ad5cdSakolb 			thread_lock(t);
5786c97ad5cdSakolb 			/*
5787c97ad5cdSakolb 			 * Kick this thread so that he doesn't sit
5788c97ad5cdSakolb 			 * on a wrong wait queue.
5789c97ad5cdSakolb 			 */
5790c97ad5cdSakolb 			if (ISWAITING(t))
5791c97ad5cdSakolb 				setrun_locked(t);
5792c97ad5cdSakolb 
5793c97ad5cdSakolb 			if (t->t_schedflag & TS_ANYWAITQ)
5794c97ad5cdSakolb 				t->t_schedflag &= ~ TS_ANYWAITQ;
5795c97ad5cdSakolb 
5796c97ad5cdSakolb 			thread_unlock(t);
5797c97ad5cdSakolb 		} while ((t = t->t_forw) != pp->p_tlist);
5798c97ad5cdSakolb 	}
5799c97ad5cdSakolb 
5800c97ad5cdSakolb 	/*
58010209230bSgjelinek 	 * If there is a default scheduling class for the zone and it is not
58020209230bSgjelinek 	 * the class we are currently in, change all of the threads in the
58030209230bSgjelinek 	 * process to the new class.  We need to be holding pidlock & p_lock
58040209230bSgjelinek 	 * when we call parmsset so this is a good place to do it.
58050209230bSgjelinek 	 */
58060209230bSgjelinek 	if (zone->zone_defaultcid > 0 &&
58070209230bSgjelinek 	    zone->zone_defaultcid != curthread->t_cid) {
58080209230bSgjelinek 		pcparms_t pcparms;
58090209230bSgjelinek 
58100209230bSgjelinek 		pcparms.pc_cid = zone->zone_defaultcid;
58110209230bSgjelinek 		pcparms.pc_clparms[0] = 0;
58120209230bSgjelinek 
58130209230bSgjelinek 		/*
58140209230bSgjelinek 		 * If setting the class fails, we still want to enter the zone.
58150209230bSgjelinek 		 */
58160209230bSgjelinek 		if ((t = pp->p_tlist) != NULL) {
58170209230bSgjelinek 			do {
58180209230bSgjelinek 				(void) parmsset(&pcparms, t);
58190209230bSgjelinek 			} while ((t = t->t_forw) != pp->p_tlist);
58200209230bSgjelinek 		}
58210209230bSgjelinek 	}
58220209230bSgjelinek 
58237c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
58247c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
58257c478bd9Sstevel@tonic-gate 
58267c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
58277c478bd9Sstevel@tonic-gate 	/*
58287c478bd9Sstevel@tonic-gate 	 * We're firmly in the zone; let pools progress.
58297c478bd9Sstevel@tonic-gate 	 */
58307c478bd9Sstevel@tonic-gate 	pool_unlock();
58317c478bd9Sstevel@tonic-gate 	task_rele(oldtk);
58327c478bd9Sstevel@tonic-gate 	/*
58337c478bd9Sstevel@tonic-gate 	 * We don't need to retain a hold on the zone since we already
58347c478bd9Sstevel@tonic-gate 	 * incremented zone_ntasks, so the zone isn't going anywhere.
58357c478bd9Sstevel@tonic-gate 	 */
58367c478bd9Sstevel@tonic-gate 	zone_rele(zone);
58377c478bd9Sstevel@tonic-gate 
58387c478bd9Sstevel@tonic-gate 	/*
58397c478bd9Sstevel@tonic-gate 	 * Chroot
58407c478bd9Sstevel@tonic-gate 	 */
58417c478bd9Sstevel@tonic-gate 	vp = zone->zone_rootvp;
58427c478bd9Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_cdir, pp);
58437c478bd9Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_rdir, pp);
58447c478bd9Sstevel@tonic-gate 
58457c478bd9Sstevel@tonic-gate 	/*
58467c478bd9Sstevel@tonic-gate 	 * Change process credentials
58477c478bd9Sstevel@tonic-gate 	 */
58487c478bd9Sstevel@tonic-gate 	newcr = cralloc();
58497c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
58507c478bd9Sstevel@tonic-gate 	cr = pp->p_cred;
58517c478bd9Sstevel@tonic-gate 	crcopy_to(cr, newcr);
58527c478bd9Sstevel@tonic-gate 	crsetzone(newcr, zone);
58537c478bd9Sstevel@tonic-gate 	pp->p_cred = newcr;
58547c478bd9Sstevel@tonic-gate 
58557c478bd9Sstevel@tonic-gate 	/*
58567c478bd9Sstevel@tonic-gate 	 * Restrict all process privilege sets to zone limit
58577c478bd9Sstevel@tonic-gate 	 */
58587c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(newcr));
58597c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(newcr));
58607c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(newcr));
58617c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(newcr));
58627c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
58637c478bd9Sstevel@tonic-gate 	crset(pp, newcr);
58647c478bd9Sstevel@tonic-gate 
58657c478bd9Sstevel@tonic-gate 	/*
58667c478bd9Sstevel@tonic-gate 	 * Adjust upcount to reflect zone entry.
58677c478bd9Sstevel@tonic-gate 	 */
58687c478bd9Sstevel@tonic-gate 	uid = crgetruid(newcr);
58697c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
58707c478bd9Sstevel@tonic-gate 	upcount_dec(uid, GLOBAL_ZONEID);
58717c478bd9Sstevel@tonic-gate 	upcount_inc(uid, zoneid);
58727c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
58737c478bd9Sstevel@tonic-gate 
58747c478bd9Sstevel@tonic-gate 	/*
58757c478bd9Sstevel@tonic-gate 	 * Set up core file path and content.
58767c478bd9Sstevel@tonic-gate 	 */
58777c478bd9Sstevel@tonic-gate 	set_core_defaults();
58787c478bd9Sstevel@tonic-gate 
58797c478bd9Sstevel@tonic-gate out:
58807c478bd9Sstevel@tonic-gate 	/*
58817c478bd9Sstevel@tonic-gate 	 * Let the other lwps continue.
58827c478bd9Sstevel@tonic-gate 	 */
58837c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
58847c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
58857c478bd9Sstevel@tonic-gate 		continuelwps(pp);
58867c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
58877c478bd9Sstevel@tonic-gate 
58887c478bd9Sstevel@tonic-gate 	return (err != 0 ? set_errno(err) : 0);
58897c478bd9Sstevel@tonic-gate }
58907c478bd9Sstevel@tonic-gate 
58917c478bd9Sstevel@tonic-gate /*
58927c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_list(2).
58937c478bd9Sstevel@tonic-gate  *
58947c478bd9Sstevel@tonic-gate  * Processes running in a (non-global) zone only see themselves.
589545916cd2Sjpk  * On labeled systems, they see all zones whose label they dominate.
58967c478bd9Sstevel@tonic-gate  */
58977c478bd9Sstevel@tonic-gate static int
58987c478bd9Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones)
58997c478bd9Sstevel@tonic-gate {
59007c478bd9Sstevel@tonic-gate 	zoneid_t *zoneids;
590148451833Scarlsonj 	zone_t *zone, *myzone;
59027c478bd9Sstevel@tonic-gate 	uint_t user_nzones, real_nzones;
590345916cd2Sjpk 	uint_t domi_nzones;
590445916cd2Sjpk 	int error;
59057c478bd9Sstevel@tonic-gate 
59067c478bd9Sstevel@tonic-gate 	if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0)
59077c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
59087c478bd9Sstevel@tonic-gate 
590948451833Scarlsonj 	myzone = curproc->p_zone;
591048451833Scarlsonj 	if (myzone != global_zone) {
591145916cd2Sjpk 		bslabel_t *mybslab;
591245916cd2Sjpk 
591345916cd2Sjpk 		if (!is_system_labeled()) {
59147c478bd9Sstevel@tonic-gate 			/* just return current zone */
591545916cd2Sjpk 			real_nzones = domi_nzones = 1;
59167c478bd9Sstevel@tonic-gate 			zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP);
591748451833Scarlsonj 			zoneids[0] = myzone->zone_id;
59187c478bd9Sstevel@tonic-gate 		} else {
591945916cd2Sjpk 			/* return all zones that are dominated */
59207c478bd9Sstevel@tonic-gate 			mutex_enter(&zonehash_lock);
59217c478bd9Sstevel@tonic-gate 			real_nzones = zonecount;
592245916cd2Sjpk 			domi_nzones = 0;
592345916cd2Sjpk 			if (real_nzones > 0) {
592445916cd2Sjpk 				zoneids = kmem_alloc(real_nzones *
592545916cd2Sjpk 				    sizeof (zoneid_t), KM_SLEEP);
592648451833Scarlsonj 				mybslab = label2bslabel(myzone->zone_slabel);
592745916cd2Sjpk 				for (zone = list_head(&zone_active);
592845916cd2Sjpk 				    zone != NULL;
592945916cd2Sjpk 				    zone = list_next(&zone_active, zone)) {
593045916cd2Sjpk 					if (zone->zone_id == GLOBAL_ZONEID)
593145916cd2Sjpk 						continue;
593248451833Scarlsonj 					if (zone != myzone &&
593348451833Scarlsonj 					    (zone->zone_flags & ZF_IS_SCRATCH))
593448451833Scarlsonj 						continue;
593548451833Scarlsonj 					/*
593648451833Scarlsonj 					 * Note that a label always dominates
593748451833Scarlsonj 					 * itself, so myzone is always included
593848451833Scarlsonj 					 * in the list.
593948451833Scarlsonj 					 */
594045916cd2Sjpk 					if (bldominates(mybslab,
594145916cd2Sjpk 					    label2bslabel(zone->zone_slabel))) {
594245916cd2Sjpk 						zoneids[domi_nzones++] =
594345916cd2Sjpk 						    zone->zone_id;
594445916cd2Sjpk 					}
594545916cd2Sjpk 				}
594645916cd2Sjpk 			}
594745916cd2Sjpk 			mutex_exit(&zonehash_lock);
594845916cd2Sjpk 		}
594945916cd2Sjpk 	} else {
595045916cd2Sjpk 		mutex_enter(&zonehash_lock);
595145916cd2Sjpk 		real_nzones = zonecount;
595245916cd2Sjpk 		domi_nzones = 0;
595345916cd2Sjpk 		if (real_nzones > 0) {
59547c478bd9Sstevel@tonic-gate 			zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t),
59557c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
59567c478bd9Sstevel@tonic-gate 			for (zone = list_head(&zone_active); zone != NULL;
59577c478bd9Sstevel@tonic-gate 			    zone = list_next(&zone_active, zone))
595845916cd2Sjpk 				zoneids[domi_nzones++] = zone->zone_id;
595945916cd2Sjpk 			ASSERT(domi_nzones == real_nzones);
59607c478bd9Sstevel@tonic-gate 		}
59617c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
59627c478bd9Sstevel@tonic-gate 	}
59637c478bd9Sstevel@tonic-gate 
596445916cd2Sjpk 	/*
596545916cd2Sjpk 	 * If user has allocated space for fewer entries than we found, then
596645916cd2Sjpk 	 * return only up to his limit.  Either way, tell him exactly how many
596745916cd2Sjpk 	 * we found.
596845916cd2Sjpk 	 */
596945916cd2Sjpk 	if (domi_nzones < user_nzones)
597045916cd2Sjpk 		user_nzones = domi_nzones;
597145916cd2Sjpk 	error = 0;
597245916cd2Sjpk 	if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) {
59737c478bd9Sstevel@tonic-gate 		error = EFAULT;
597445916cd2Sjpk 	} else if (zoneidlist != NULL && user_nzones != 0) {
59757c478bd9Sstevel@tonic-gate 		if (copyout(zoneids, zoneidlist,
59767c478bd9Sstevel@tonic-gate 		    user_nzones * sizeof (zoneid_t)) != 0)
59777c478bd9Sstevel@tonic-gate 			error = EFAULT;
59787c478bd9Sstevel@tonic-gate 	}
59797c478bd9Sstevel@tonic-gate 
598045916cd2Sjpk 	if (real_nzones > 0)
59817c478bd9Sstevel@tonic-gate 		kmem_free(zoneids, real_nzones * sizeof (zoneid_t));
59827c478bd9Sstevel@tonic-gate 
598345916cd2Sjpk 	if (error != 0)
59847c478bd9Sstevel@tonic-gate 		return (set_errno(error));
59857c478bd9Sstevel@tonic-gate 	else
59867c478bd9Sstevel@tonic-gate 		return (0);
59877c478bd9Sstevel@tonic-gate }
59887c478bd9Sstevel@tonic-gate 
59897c478bd9Sstevel@tonic-gate /*
59907c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_lookup(2).
59917c478bd9Sstevel@tonic-gate  *
599245916cd2Sjpk  * Non-global zones are only able to see themselves and (on labeled systems)
599345916cd2Sjpk  * the zones they dominate.
59947c478bd9Sstevel@tonic-gate  */
59957c478bd9Sstevel@tonic-gate static zoneid_t
59967c478bd9Sstevel@tonic-gate zone_lookup(const char *zone_name)
59977c478bd9Sstevel@tonic-gate {
59987c478bd9Sstevel@tonic-gate 	char *kname;
59997c478bd9Sstevel@tonic-gate 	zone_t *zone;
60007c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
60017c478bd9Sstevel@tonic-gate 	int err;
60027c478bd9Sstevel@tonic-gate 
60037c478bd9Sstevel@tonic-gate 	if (zone_name == NULL) {
60047c478bd9Sstevel@tonic-gate 		/* return caller's zone id */
60057c478bd9Sstevel@tonic-gate 		return (getzoneid());
60067c478bd9Sstevel@tonic-gate 	}
60077c478bd9Sstevel@tonic-gate 
60087c478bd9Sstevel@tonic-gate 	kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
60097c478bd9Sstevel@tonic-gate 	if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) {
60107c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
60117c478bd9Sstevel@tonic-gate 		return (set_errno(err));
60127c478bd9Sstevel@tonic-gate 	}
60137c478bd9Sstevel@tonic-gate 
60147c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
60157c478bd9Sstevel@tonic-gate 	zone = zone_find_all_by_name(kname);
60167c478bd9Sstevel@tonic-gate 	kmem_free(kname, ZONENAME_MAX);
601745916cd2Sjpk 	/*
601845916cd2Sjpk 	 * In a non-global zone, can only lookup global and own name.
601945916cd2Sjpk 	 * In Trusted Extensions zone label dominance rules apply.
602045916cd2Sjpk 	 */
602145916cd2Sjpk 	if (zone == NULL ||
602245916cd2Sjpk 	    zone_status_get(zone) < ZONE_IS_READY ||
602345916cd2Sjpk 	    !zone_list_access(zone)) {
60247c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
60257c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
602645916cd2Sjpk 	} else {
60277c478bd9Sstevel@tonic-gate 		zoneid = zone->zone_id;
60287c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
60297c478bd9Sstevel@tonic-gate 		return (zoneid);
60307c478bd9Sstevel@tonic-gate 	}
603145916cd2Sjpk }
60327c478bd9Sstevel@tonic-gate 
6033821c4a97Sdp static int
6034821c4a97Sdp zone_version(int *version_arg)
6035821c4a97Sdp {
6036821c4a97Sdp 	int version = ZONE_SYSCALL_API_VERSION;
6037821c4a97Sdp 
6038821c4a97Sdp 	if (copyout(&version, version_arg, sizeof (int)) != 0)
6039821c4a97Sdp 		return (set_errno(EFAULT));
6040821c4a97Sdp 	return (0);
6041821c4a97Sdp }
6042821c4a97Sdp 
60437c478bd9Sstevel@tonic-gate /* ARGSUSED */
60447c478bd9Sstevel@tonic-gate long
6045fa9e4066Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4)
60467c478bd9Sstevel@tonic-gate {
60477c478bd9Sstevel@tonic-gate 	zone_def zs;
60482b24ab6bSSebastien Roy 	int err;
60497c478bd9Sstevel@tonic-gate 
60507c478bd9Sstevel@tonic-gate 	switch (cmd) {
60517c478bd9Sstevel@tonic-gate 	case ZONE_CREATE:
60527c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
60537c478bd9Sstevel@tonic-gate 			if (copyin(arg1, &zs, sizeof (zone_def))) {
60547c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
60557c478bd9Sstevel@tonic-gate 			}
60567c478bd9Sstevel@tonic-gate 		} else {
60577c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
60587c478bd9Sstevel@tonic-gate 			zone_def32 zs32;
60597c478bd9Sstevel@tonic-gate 
60607c478bd9Sstevel@tonic-gate 			if (copyin(arg1, &zs32, sizeof (zone_def32))) {
60617c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
60627c478bd9Sstevel@tonic-gate 			}
60637c478bd9Sstevel@tonic-gate 			zs.zone_name =
60647c478bd9Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_name;
60657c478bd9Sstevel@tonic-gate 			zs.zone_root =
60667c478bd9Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_root;
60677c478bd9Sstevel@tonic-gate 			zs.zone_privs =
60687c478bd9Sstevel@tonic-gate 			    (const struct priv_set *)
60697c478bd9Sstevel@tonic-gate 			    (unsigned long)zs32.zone_privs;
60706f70df68Sdp 			zs.zone_privssz = zs32.zone_privssz;
60717c478bd9Sstevel@tonic-gate 			zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf;
60727c478bd9Sstevel@tonic-gate 			zs.rctlbufsz = zs32.rctlbufsz;
6073fa9e4066Sahrens 			zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf;
6074fa9e4066Sahrens 			zs.zfsbufsz = zs32.zfsbufsz;
60757c478bd9Sstevel@tonic-gate 			zs.extended_error =
60767c478bd9Sstevel@tonic-gate 			    (int *)(unsigned long)zs32.extended_error;
607745916cd2Sjpk 			zs.match = zs32.match;
607845916cd2Sjpk 			zs.doi = zs32.doi;
607945916cd2Sjpk 			zs.label = (const bslabel_t *)(uintptr_t)zs32.label;
6080f4b3ec61Sdh155122 			zs.flags = zs32.flags;
60817c478bd9Sstevel@tonic-gate #else
60827c478bd9Sstevel@tonic-gate 			panic("get_udatamodel() returned bogus result\n");
60837c478bd9Sstevel@tonic-gate #endif
60847c478bd9Sstevel@tonic-gate 		}
60857c478bd9Sstevel@tonic-gate 
60867c478bd9Sstevel@tonic-gate 		return (zone_create(zs.zone_name, zs.zone_root,
6087821c4a97Sdp 		    zs.zone_privs, zs.zone_privssz,
6088821c4a97Sdp 		    (caddr_t)zs.rctlbuf, zs.rctlbufsz,
6089fa9e4066Sahrens 		    (caddr_t)zs.zfsbuf, zs.zfsbufsz,
609045916cd2Sjpk 		    zs.extended_error, zs.match, zs.doi,
6091f4b3ec61Sdh155122 		    zs.label, zs.flags));
60927c478bd9Sstevel@tonic-gate 	case ZONE_BOOT:
60933f2f09c1Sdp 		return (zone_boot((zoneid_t)(uintptr_t)arg1));
60947c478bd9Sstevel@tonic-gate 	case ZONE_DESTROY:
60957c478bd9Sstevel@tonic-gate 		return (zone_destroy((zoneid_t)(uintptr_t)arg1));
60967c478bd9Sstevel@tonic-gate 	case ZONE_GETATTR:
60977c478bd9Sstevel@tonic-gate 		return (zone_getattr((zoneid_t)(uintptr_t)arg1,
60987c478bd9Sstevel@tonic-gate 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
60993f2f09c1Sdp 	case ZONE_SETATTR:
61003f2f09c1Sdp 		return (zone_setattr((zoneid_t)(uintptr_t)arg1,
61013f2f09c1Sdp 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
61027c478bd9Sstevel@tonic-gate 	case ZONE_ENTER:
61037c478bd9Sstevel@tonic-gate 		return (zone_enter((zoneid_t)(uintptr_t)arg1));
61047c478bd9Sstevel@tonic-gate 	case ZONE_LIST:
61057c478bd9Sstevel@tonic-gate 		return (zone_list((zoneid_t *)arg1, (uint_t *)arg2));
61067c478bd9Sstevel@tonic-gate 	case ZONE_SHUTDOWN:
61077c478bd9Sstevel@tonic-gate 		return (zone_shutdown((zoneid_t)(uintptr_t)arg1));
61087c478bd9Sstevel@tonic-gate 	case ZONE_LOOKUP:
61097c478bd9Sstevel@tonic-gate 		return (zone_lookup((const char *)arg1));
6110821c4a97Sdp 	case ZONE_VERSION:
6111821c4a97Sdp 		return (zone_version((int *)arg1));
6112f4b3ec61Sdh155122 	case ZONE_ADD_DATALINK:
6113f4b3ec61Sdh155122 		return (zone_add_datalink((zoneid_t)(uintptr_t)arg1,
61142b24ab6bSSebastien Roy 		    (datalink_id_t)(uintptr_t)arg2));
6115f4b3ec61Sdh155122 	case ZONE_DEL_DATALINK:
6116f4b3ec61Sdh155122 		return (zone_remove_datalink((zoneid_t)(uintptr_t)arg1,
61172b24ab6bSSebastien Roy 		    (datalink_id_t)(uintptr_t)arg2));
61182b24ab6bSSebastien Roy 	case ZONE_CHECK_DATALINK: {
61192b24ab6bSSebastien Roy 		zoneid_t	zoneid;
61202b24ab6bSSebastien Roy 		boolean_t	need_copyout;
61212b24ab6bSSebastien Roy 
61222b24ab6bSSebastien Roy 		if (copyin(arg1, &zoneid, sizeof (zoneid)) != 0)
61232b24ab6bSSebastien Roy 			return (EFAULT);
61242b24ab6bSSebastien Roy 		need_copyout = (zoneid == ALL_ZONES);
61252b24ab6bSSebastien Roy 		err = zone_check_datalink(&zoneid,
61262b24ab6bSSebastien Roy 		    (datalink_id_t)(uintptr_t)arg2);
61272b24ab6bSSebastien Roy 		if (err == 0 && need_copyout) {
61282b24ab6bSSebastien Roy 			if (copyout(&zoneid, arg1, sizeof (zoneid)) != 0)
61292b24ab6bSSebastien Roy 				err = EFAULT;
61302b24ab6bSSebastien Roy 		}
61312b24ab6bSSebastien Roy 		return (err == 0 ? 0 : set_errno(err));
61322b24ab6bSSebastien Roy 	}
6133f4b3ec61Sdh155122 	case ZONE_LIST_DATALINK:
6134f4b3ec61Sdh155122 		return (zone_list_datalink((zoneid_t)(uintptr_t)arg1,
61352b24ab6bSSebastien Roy 		    (int *)arg2, (datalink_id_t *)(uintptr_t)arg3));
61367c478bd9Sstevel@tonic-gate 	default:
61377c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
61387c478bd9Sstevel@tonic-gate 	}
61397c478bd9Sstevel@tonic-gate }
61407c478bd9Sstevel@tonic-gate 
61417c478bd9Sstevel@tonic-gate struct zarg {
61427c478bd9Sstevel@tonic-gate 	zone_t *zone;
61437c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t arg;
61447c478bd9Sstevel@tonic-gate };
61457c478bd9Sstevel@tonic-gate 
61467c478bd9Sstevel@tonic-gate static int
61477c478bd9Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp)
61487c478bd9Sstevel@tonic-gate {
61497c478bd9Sstevel@tonic-gate 	char *buf;
61507c478bd9Sstevel@tonic-gate 	size_t buflen;
61517c478bd9Sstevel@tonic-gate 	int error;
61527c478bd9Sstevel@tonic-gate 
61537c478bd9Sstevel@tonic-gate 	buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name);
61547c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(buflen, KM_SLEEP);
61557c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name);
61567c478bd9Sstevel@tonic-gate 	error = door_ki_open(buf, doorp);
61577c478bd9Sstevel@tonic-gate 	kmem_free(buf, buflen);
61587c478bd9Sstevel@tonic-gate 	return (error);
61597c478bd9Sstevel@tonic-gate }
61607c478bd9Sstevel@tonic-gate 
61617c478bd9Sstevel@tonic-gate static void
61627c478bd9Sstevel@tonic-gate zone_release_door(door_handle_t *doorp)
61637c478bd9Sstevel@tonic-gate {
61647c478bd9Sstevel@tonic-gate 	door_ki_rele(*doorp);
61657c478bd9Sstevel@tonic-gate 	*doorp = NULL;
61667c478bd9Sstevel@tonic-gate }
61677c478bd9Sstevel@tonic-gate 
61687c478bd9Sstevel@tonic-gate static void
61697c478bd9Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp)
61707c478bd9Sstevel@tonic-gate {
61717c478bd9Sstevel@tonic-gate 	door_handle_t door = NULL;
61727c478bd9Sstevel@tonic-gate 	door_arg_t darg, save_arg;
61737c478bd9Sstevel@tonic-gate 	char *zone_name;
61747c478bd9Sstevel@tonic-gate 	size_t zone_namelen;
61757c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
61767c478bd9Sstevel@tonic-gate 	zone_t *zone;
61777c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t arg;
61787c478bd9Sstevel@tonic-gate 	uint64_t uniqid;
61797c478bd9Sstevel@tonic-gate 	size_t size;
61807c478bd9Sstevel@tonic-gate 	int error;
61817c478bd9Sstevel@tonic-gate 	int retry;
61827c478bd9Sstevel@tonic-gate 
61837c478bd9Sstevel@tonic-gate 	zone = zargp->zone;
61847c478bd9Sstevel@tonic-gate 	arg = zargp->arg;
61857c478bd9Sstevel@tonic-gate 	kmem_free(zargp, sizeof (*zargp));
61867c478bd9Sstevel@tonic-gate 
61877c478bd9Sstevel@tonic-gate 	zone_namelen = strlen(zone->zone_name) + 1;
61887c478bd9Sstevel@tonic-gate 	zone_name = kmem_alloc(zone_namelen, KM_SLEEP);
61897c478bd9Sstevel@tonic-gate 	bcopy(zone->zone_name, zone_name, zone_namelen);
61907c478bd9Sstevel@tonic-gate 	zoneid = zone->zone_id;
61917c478bd9Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
61927c478bd9Sstevel@tonic-gate 	/*
61937c478bd9Sstevel@tonic-gate 	 * zoneadmd may be down, but at least we can empty out the zone.
61947c478bd9Sstevel@tonic-gate 	 * We can ignore the return value of zone_empty() since we're called
61957c478bd9Sstevel@tonic-gate 	 * from a kernel thread and know we won't be delivered any signals.
61967c478bd9Sstevel@tonic-gate 	 */
61977c478bd9Sstevel@tonic-gate 	ASSERT(curproc == &p0);
61987c478bd9Sstevel@tonic-gate 	(void) zone_empty(zone);
61997c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY);
62007c478bd9Sstevel@tonic-gate 	zone_rele(zone);
62017c478bd9Sstevel@tonic-gate 
62027c478bd9Sstevel@tonic-gate 	size = sizeof (arg);
62037c478bd9Sstevel@tonic-gate 	darg.rbuf = (char *)&arg;
62047c478bd9Sstevel@tonic-gate 	darg.data_ptr = (char *)&arg;
62057c478bd9Sstevel@tonic-gate 	darg.rsize = size;
62067c478bd9Sstevel@tonic-gate 	darg.data_size = size;
62077c478bd9Sstevel@tonic-gate 	darg.desc_ptr = NULL;
62087c478bd9Sstevel@tonic-gate 	darg.desc_num = 0;
62097c478bd9Sstevel@tonic-gate 
62107c478bd9Sstevel@tonic-gate 	save_arg = darg;
62117c478bd9Sstevel@tonic-gate 	/*
62127c478bd9Sstevel@tonic-gate 	 * Since we're not holding a reference to the zone, any number of
62137c478bd9Sstevel@tonic-gate 	 * things can go wrong, including the zone disappearing before we get a
62147c478bd9Sstevel@tonic-gate 	 * chance to talk to zoneadmd.
62157c478bd9Sstevel@tonic-gate 	 */
62167c478bd9Sstevel@tonic-gate 	for (retry = 0; /* forever */; retry++) {
62177c478bd9Sstevel@tonic-gate 		if (door == NULL &&
62187c478bd9Sstevel@tonic-gate 		    (error = zone_lookup_door(zone_name, &door)) != 0) {
62197c478bd9Sstevel@tonic-gate 			goto next;
62207c478bd9Sstevel@tonic-gate 		}
62217c478bd9Sstevel@tonic-gate 		ASSERT(door != NULL);
62227c478bd9Sstevel@tonic-gate 
6223323a81d9Sjwadams 		if ((error = door_ki_upcall_limited(door, &darg, NULL,
6224323a81d9Sjwadams 		    SIZE_MAX, 0)) == 0) {
62257c478bd9Sstevel@tonic-gate 			break;
62267c478bd9Sstevel@tonic-gate 		}
62277c478bd9Sstevel@tonic-gate 		switch (error) {
62287c478bd9Sstevel@tonic-gate 		case EINTR:
62297c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
62307c478bd9Sstevel@tonic-gate 		case EAGAIN:	/* process may be forking */
62317c478bd9Sstevel@tonic-gate 			/*
62327c478bd9Sstevel@tonic-gate 			 * Back off for a bit
62337c478bd9Sstevel@tonic-gate 			 */
62347c478bd9Sstevel@tonic-gate 			break;
62357c478bd9Sstevel@tonic-gate 		case EBADF:
62367c478bd9Sstevel@tonic-gate 			zone_release_door(&door);
62377c478bd9Sstevel@tonic-gate 			if (zone_lookup_door(zone_name, &door) != 0) {
62387c478bd9Sstevel@tonic-gate 				/*
62397c478bd9Sstevel@tonic-gate 				 * zoneadmd may be dead, but it may come back to
62407c478bd9Sstevel@tonic-gate 				 * life later.
62417c478bd9Sstevel@tonic-gate 				 */
62427c478bd9Sstevel@tonic-gate 				break;
62437c478bd9Sstevel@tonic-gate 			}
62447c478bd9Sstevel@tonic-gate 			break;
62457c478bd9Sstevel@tonic-gate 		default:
62467c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
62477c478bd9Sstevel@tonic-gate 			    "zone_ki_call_zoneadmd: door_ki_upcall error %d\n",
62487c478bd9Sstevel@tonic-gate 			    error);
62497c478bd9Sstevel@tonic-gate 			goto out;
62507c478bd9Sstevel@tonic-gate 		}
62517c478bd9Sstevel@tonic-gate next:
62527c478bd9Sstevel@tonic-gate 		/*
62537c478bd9Sstevel@tonic-gate 		 * If this isn't the same zone_t that we originally had in mind,
62547c478bd9Sstevel@tonic-gate 		 * then this is the same as if two kadmin requests come in at
62557c478bd9Sstevel@tonic-gate 		 * the same time: the first one wins.  This means we lose, so we
62567c478bd9Sstevel@tonic-gate 		 * bail.
62577c478bd9Sstevel@tonic-gate 		 */
62587c478bd9Sstevel@tonic-gate 		if ((zone = zone_find_by_id(zoneid)) == NULL) {
62597c478bd9Sstevel@tonic-gate 			/*
62607c478bd9Sstevel@tonic-gate 			 * Problem is solved.
62617c478bd9Sstevel@tonic-gate 			 */
62627c478bd9Sstevel@tonic-gate 			break;
62637c478bd9Sstevel@tonic-gate 		}
62647c478bd9Sstevel@tonic-gate 		if (zone->zone_uniqid != uniqid) {
62657c478bd9Sstevel@tonic-gate 			/*
62667c478bd9Sstevel@tonic-gate 			 * zoneid recycled
62677c478bd9Sstevel@tonic-gate 			 */
62687c478bd9Sstevel@tonic-gate 			zone_rele(zone);
62697c478bd9Sstevel@tonic-gate 			break;
62707c478bd9Sstevel@tonic-gate 		}
62717c478bd9Sstevel@tonic-gate 		/*
62727c478bd9Sstevel@tonic-gate 		 * We could zone_status_timedwait(), but there doesn't seem to
62737c478bd9Sstevel@tonic-gate 		 * be much point in doing that (plus, it would mean that
62747c478bd9Sstevel@tonic-gate 		 * zone_free() isn't called until this thread exits).
62757c478bd9Sstevel@tonic-gate 		 */
62767c478bd9Sstevel@tonic-gate 		zone_rele(zone);
62777c478bd9Sstevel@tonic-gate 		delay(hz);
62787c478bd9Sstevel@tonic-gate 		darg = save_arg;
62797c478bd9Sstevel@tonic-gate 	}
62807c478bd9Sstevel@tonic-gate out:
62817c478bd9Sstevel@tonic-gate 	if (door != NULL) {
62827c478bd9Sstevel@tonic-gate 		zone_release_door(&door);
62837c478bd9Sstevel@tonic-gate 	}
62847c478bd9Sstevel@tonic-gate 	kmem_free(zone_name, zone_namelen);
62857c478bd9Sstevel@tonic-gate 	thread_exit();
62867c478bd9Sstevel@tonic-gate }
62877c478bd9Sstevel@tonic-gate 
62887c478bd9Sstevel@tonic-gate /*
62893f2f09c1Sdp  * Entry point for uadmin() to tell the zone to go away or reboot.  Analog to
62903f2f09c1Sdp  * kadmin().  The caller is a process in the zone.
62917c478bd9Sstevel@tonic-gate  *
62927c478bd9Sstevel@tonic-gate  * In order to shutdown the zone, we will hand off control to zoneadmd
62937c478bd9Sstevel@tonic-gate  * (running in the global zone) via a door.  We do a half-hearted job at
62947c478bd9Sstevel@tonic-gate  * killing all processes in the zone, create a kernel thread to contact
62957c478bd9Sstevel@tonic-gate  * zoneadmd, and make note of the "uniqid" of the zone.  The uniqid is
62967c478bd9Sstevel@tonic-gate  * a form of generation number used to let zoneadmd (as well as
62977c478bd9Sstevel@tonic-gate  * zone_destroy()) know exactly which zone they're re talking about.
62987c478bd9Sstevel@tonic-gate  */
62997c478bd9Sstevel@tonic-gate int
63003f2f09c1Sdp zone_kadmin(int cmd, int fcn, const char *mdep, cred_t *credp)
63017c478bd9Sstevel@tonic-gate {
63027c478bd9Sstevel@tonic-gate 	struct zarg *zargp;
63037c478bd9Sstevel@tonic-gate 	zone_cmd_t zcmd;
63047c478bd9Sstevel@tonic-gate 	zone_t *zone;
63057c478bd9Sstevel@tonic-gate 
63067c478bd9Sstevel@tonic-gate 	zone = curproc->p_zone;
63077c478bd9Sstevel@tonic-gate 	ASSERT(getzoneid() != GLOBAL_ZONEID);
63087c478bd9Sstevel@tonic-gate 
63097c478bd9Sstevel@tonic-gate 	switch (cmd) {
63107c478bd9Sstevel@tonic-gate 	case A_SHUTDOWN:
63117c478bd9Sstevel@tonic-gate 		switch (fcn) {
63127c478bd9Sstevel@tonic-gate 		case AD_HALT:
63137c478bd9Sstevel@tonic-gate 		case AD_POWEROFF:
63147c478bd9Sstevel@tonic-gate 			zcmd = Z_HALT;
63157c478bd9Sstevel@tonic-gate 			break;
63167c478bd9Sstevel@tonic-gate 		case AD_BOOT:
63177c478bd9Sstevel@tonic-gate 			zcmd = Z_REBOOT;
63187c478bd9Sstevel@tonic-gate 			break;
63197c478bd9Sstevel@tonic-gate 		case AD_IBOOT:
63207c478bd9Sstevel@tonic-gate 		case AD_SBOOT:
63217c478bd9Sstevel@tonic-gate 		case AD_SIBOOT:
63227c478bd9Sstevel@tonic-gate 		case AD_NOSYNC:
63237c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
63247c478bd9Sstevel@tonic-gate 		default:
63257c478bd9Sstevel@tonic-gate 			return (EINVAL);
63267c478bd9Sstevel@tonic-gate 		}
63277c478bd9Sstevel@tonic-gate 		break;
63287c478bd9Sstevel@tonic-gate 	case A_REBOOT:
63297c478bd9Sstevel@tonic-gate 		zcmd = Z_REBOOT;
63307c478bd9Sstevel@tonic-gate 		break;
63317c478bd9Sstevel@tonic-gate 	case A_FTRACE:
63327c478bd9Sstevel@tonic-gate 	case A_REMOUNT:
63337c478bd9Sstevel@tonic-gate 	case A_FREEZE:
63347c478bd9Sstevel@tonic-gate 	case A_DUMP:
6335753a6d45SSherry Moore 	case A_CONFIG:
63367c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
63377c478bd9Sstevel@tonic-gate 	default:
63387c478bd9Sstevel@tonic-gate 		ASSERT(cmd != A_SWAPCTL);	/* handled by uadmin() */
63397c478bd9Sstevel@tonic-gate 		return (EINVAL);
63407c478bd9Sstevel@tonic-gate 	}
63417c478bd9Sstevel@tonic-gate 
63427c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_admin(credp, B_FALSE))
63437c478bd9Sstevel@tonic-gate 		return (EPERM);
63447c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
63453f2f09c1Sdp 
63467c478bd9Sstevel@tonic-gate 	/*
63477c478bd9Sstevel@tonic-gate 	 * zone_status can't be ZONE_IS_EMPTY or higher since curproc
63487c478bd9Sstevel@tonic-gate 	 * is in the zone.
63497c478bd9Sstevel@tonic-gate 	 */
63507c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY);
63517c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) > ZONE_IS_RUNNING) {
63527c478bd9Sstevel@tonic-gate 		/*
63537c478bd9Sstevel@tonic-gate 		 * This zone is already on its way down.
63547c478bd9Sstevel@tonic-gate 		 */
63557c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
63567c478bd9Sstevel@tonic-gate 		return (0);
63577c478bd9Sstevel@tonic-gate 	}
63587c478bd9Sstevel@tonic-gate 	/*
63597c478bd9Sstevel@tonic-gate 	 * Prevent future zone_enter()s
63607c478bd9Sstevel@tonic-gate 	 */
63617c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
63627c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
63637c478bd9Sstevel@tonic-gate 
63647c478bd9Sstevel@tonic-gate 	/*
63657c478bd9Sstevel@tonic-gate 	 * Kill everyone now and call zoneadmd later.
63667c478bd9Sstevel@tonic-gate 	 * zone_ki_call_zoneadmd() will do a more thorough job of this
63677c478bd9Sstevel@tonic-gate 	 * later.
63687c478bd9Sstevel@tonic-gate 	 */
63697c478bd9Sstevel@tonic-gate 	killall(zone->zone_id);
63707c478bd9Sstevel@tonic-gate 	/*
63717c478bd9Sstevel@tonic-gate 	 * Now, create the thread to contact zoneadmd and do the rest of the
63727c478bd9Sstevel@tonic-gate 	 * work.  This thread can't be created in our zone otherwise
63737c478bd9Sstevel@tonic-gate 	 * zone_destroy() would deadlock.
63747c478bd9Sstevel@tonic-gate 	 */
63753f2f09c1Sdp 	zargp = kmem_zalloc(sizeof (*zargp), KM_SLEEP);
63767c478bd9Sstevel@tonic-gate 	zargp->arg.cmd = zcmd;
63777c478bd9Sstevel@tonic-gate 	zargp->arg.uniqid = zone->zone_uniqid;
63783f2f09c1Sdp 	zargp->zone = zone;
63797c478bd9Sstevel@tonic-gate 	(void) strcpy(zargp->arg.locale, "C");
63803f2f09c1Sdp 	/* mdep was already copied in for us by uadmin */
63813f2f09c1Sdp 	if (mdep != NULL)
63823f2f09c1Sdp 		(void) strlcpy(zargp->arg.bootbuf, mdep,
63833f2f09c1Sdp 		    sizeof (zargp->arg.bootbuf));
63843f2f09c1Sdp 	zone_hold(zone);
63857c478bd9Sstevel@tonic-gate 
63867c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0,
63877c478bd9Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
63887c478bd9Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
63897c478bd9Sstevel@tonic-gate 
63907c478bd9Sstevel@tonic-gate 	return (EINVAL);
63917c478bd9Sstevel@tonic-gate }
63927c478bd9Sstevel@tonic-gate 
63937c478bd9Sstevel@tonic-gate /*
63947c478bd9Sstevel@tonic-gate  * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's
63957c478bd9Sstevel@tonic-gate  * status to ZONE_IS_SHUTTING_DOWN.
63968f983ab3Sjv227347  *
63978f983ab3Sjv227347  * This function also shuts down all running zones to ensure that they won't
63988f983ab3Sjv227347  * fork new processes.
63997c478bd9Sstevel@tonic-gate  */
64007c478bd9Sstevel@tonic-gate void
64017c478bd9Sstevel@tonic-gate zone_shutdown_global(void)
64027c478bd9Sstevel@tonic-gate {
64038f983ab3Sjv227347 	zone_t *current_zonep;
64047c478bd9Sstevel@tonic-gate 
64058f983ab3Sjv227347 	ASSERT(INGLOBALZONE(curproc));
64068f983ab3Sjv227347 	mutex_enter(&zonehash_lock);
64077c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
64088f983ab3Sjv227347 
64098f983ab3Sjv227347 	/* Modify the global zone's status first. */
64107c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING);
64117c478bd9Sstevel@tonic-gate 	zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN);
64128f983ab3Sjv227347 
64138f983ab3Sjv227347 	/*
64148f983ab3Sjv227347 	 * Now change the states of all running zones to ZONE_IS_SHUTTING_DOWN.
64158f983ab3Sjv227347 	 * We don't mark all zones with ZONE_IS_SHUTTING_DOWN because doing so
64168f983ab3Sjv227347 	 * could cause assertions to fail (e.g., assertions about a zone's
64178f983ab3Sjv227347 	 * state during initialization, readying, or booting) or produce races.
64188f983ab3Sjv227347 	 * We'll let threads continue to initialize and ready new zones: they'll
64198f983ab3Sjv227347 	 * fail to boot the new zones when they see that the global zone is
64208f983ab3Sjv227347 	 * shutting down.
64218f983ab3Sjv227347 	 */
64228f983ab3Sjv227347 	for (current_zonep = list_head(&zone_active); current_zonep != NULL;
64238f983ab3Sjv227347 	    current_zonep = list_next(&zone_active, current_zonep)) {
64248f983ab3Sjv227347 		if (zone_status_get(current_zonep) == ZONE_IS_RUNNING)
64258f983ab3Sjv227347 			zone_status_set(current_zonep, ZONE_IS_SHUTTING_DOWN);
64268f983ab3Sjv227347 	}
64277c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
64288f983ab3Sjv227347 	mutex_exit(&zonehash_lock);
64297c478bd9Sstevel@tonic-gate }
6430fa9e4066Sahrens 
6431fa9e4066Sahrens /*
6432fa9e4066Sahrens  * Returns true if the named dataset is visible in the current zone.
6433fa9e4066Sahrens  * The 'write' parameter is set to 1 if the dataset is also writable.
6434fa9e4066Sahrens  */
6435fa9e4066Sahrens int
6436fa9e4066Sahrens zone_dataset_visible(const char *dataset, int *write)
6437fa9e4066Sahrens {
6438658a1dc7SSanjeev Bagewadi 	static int zfstype = -1;
6439fa9e4066Sahrens 	zone_dataset_t *zd;
6440fa9e4066Sahrens 	size_t len;
6441fa9e4066Sahrens 	zone_t *zone = curproc->p_zone;
6442658a1dc7SSanjeev Bagewadi 	const char *name = NULL;
6443658a1dc7SSanjeev Bagewadi 	vfs_t *vfsp = NULL;
6444fa9e4066Sahrens 
6445fa9e4066Sahrens 	if (dataset[0] == '\0')
6446fa9e4066Sahrens 		return (0);
6447fa9e4066Sahrens 
6448fa9e4066Sahrens 	/*
6449fa9e4066Sahrens 	 * Walk the list once, looking for datasets which match exactly, or
6450fa9e4066Sahrens 	 * specify a dataset underneath an exported dataset.  If found, return
6451fa9e4066Sahrens 	 * true and note that it is writable.
6452fa9e4066Sahrens 	 */
6453fa9e4066Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
6454fa9e4066Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
6455fa9e4066Sahrens 
6456fa9e4066Sahrens 		len = strlen(zd->zd_dataset);
6457fa9e4066Sahrens 		if (strlen(dataset) >= len &&
6458fa9e4066Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
645995c9592aSmaybee 		    (dataset[len] == '\0' || dataset[len] == '/' ||
646095c9592aSmaybee 		    dataset[len] == '@')) {
6461fa9e4066Sahrens 			if (write)
6462fa9e4066Sahrens 				*write = 1;
6463fa9e4066Sahrens 			return (1);
6464fa9e4066Sahrens 		}
6465fa9e4066Sahrens 	}
6466fa9e4066Sahrens 
6467fa9e4066Sahrens 	/*
6468fa9e4066Sahrens 	 * Walk the list a second time, searching for datasets which are parents
6469fa9e4066Sahrens 	 * of exported datasets.  These should be visible, but read-only.
6470fa9e4066Sahrens 	 *
6471fa9e4066Sahrens 	 * Note that we also have to support forms such as 'pool/dataset/', with
6472fa9e4066Sahrens 	 * a trailing slash.
6473fa9e4066Sahrens 	 */
6474fa9e4066Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
6475fa9e4066Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
6476fa9e4066Sahrens 
6477fa9e4066Sahrens 		len = strlen(dataset);
6478fa9e4066Sahrens 		if (dataset[len - 1] == '/')
6479fa9e4066Sahrens 			len--;	/* Ignore trailing slash */
6480fa9e4066Sahrens 		if (len < strlen(zd->zd_dataset) &&
6481fa9e4066Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
6482fa9e4066Sahrens 		    zd->zd_dataset[len] == '/') {
6483fa9e4066Sahrens 			if (write)
6484fa9e4066Sahrens 				*write = 0;
6485fa9e4066Sahrens 			return (1);
6486fa9e4066Sahrens 		}
6487fa9e4066Sahrens 	}
6488fa9e4066Sahrens 
6489658a1dc7SSanjeev Bagewadi 	/*
6490658a1dc7SSanjeev Bagewadi 	 * We reach here if the given dataset is not found in the zone_dataset
6491658a1dc7SSanjeev Bagewadi 	 * list. Check if this dataset was added as a filesystem (ie. "add fs")
6492658a1dc7SSanjeev Bagewadi 	 * instead of delegation. For this we search for the dataset in the
6493658a1dc7SSanjeev Bagewadi 	 * zone_vfslist of this zone. If found, return true and note that it is
6494658a1dc7SSanjeev Bagewadi 	 * not writable.
6495658a1dc7SSanjeev Bagewadi 	 */
6496658a1dc7SSanjeev Bagewadi 
6497658a1dc7SSanjeev Bagewadi 	/*
6498658a1dc7SSanjeev Bagewadi 	 * Initialize zfstype if it is not initialized yet.
6499658a1dc7SSanjeev Bagewadi 	 */
6500658a1dc7SSanjeev Bagewadi 	if (zfstype == -1) {
6501658a1dc7SSanjeev Bagewadi 		struct vfssw *vswp = vfs_getvfssw("zfs");
6502658a1dc7SSanjeev Bagewadi 		zfstype = vswp - vfssw;
6503658a1dc7SSanjeev Bagewadi 		vfs_unrefvfssw(vswp);
6504658a1dc7SSanjeev Bagewadi 	}
6505658a1dc7SSanjeev Bagewadi 
6506658a1dc7SSanjeev Bagewadi 	vfs_list_read_lock();
6507658a1dc7SSanjeev Bagewadi 	vfsp = zone->zone_vfslist;
6508658a1dc7SSanjeev Bagewadi 	do {
6509658a1dc7SSanjeev Bagewadi 		ASSERT(vfsp);
6510658a1dc7SSanjeev Bagewadi 		if (vfsp->vfs_fstype == zfstype) {
6511658a1dc7SSanjeev Bagewadi 			name = refstr_value(vfsp->vfs_resource);
6512658a1dc7SSanjeev Bagewadi 
6513658a1dc7SSanjeev Bagewadi 			/*
6514658a1dc7SSanjeev Bagewadi 			 * Check if we have an exact match.
6515658a1dc7SSanjeev Bagewadi 			 */
6516658a1dc7SSanjeev Bagewadi 			if (strcmp(dataset, name) == 0) {
6517658a1dc7SSanjeev Bagewadi 				vfs_list_unlock();
6518658a1dc7SSanjeev Bagewadi 				if (write)
6519658a1dc7SSanjeev Bagewadi 					*write = 0;
6520658a1dc7SSanjeev Bagewadi 				return (1);
6521658a1dc7SSanjeev Bagewadi 			}
6522658a1dc7SSanjeev Bagewadi 			/*
6523658a1dc7SSanjeev Bagewadi 			 * We need to check if we are looking for parents of
6524658a1dc7SSanjeev Bagewadi 			 * a dataset. These should be visible, but read-only.
6525658a1dc7SSanjeev Bagewadi 			 */
6526658a1dc7SSanjeev Bagewadi 			len = strlen(dataset);
6527658a1dc7SSanjeev Bagewadi 			if (dataset[len - 1] == '/')
6528658a1dc7SSanjeev Bagewadi 				len--;
6529658a1dc7SSanjeev Bagewadi 
6530658a1dc7SSanjeev Bagewadi 			if (len < strlen(name) &&
6531658a1dc7SSanjeev Bagewadi 			    bcmp(dataset, name, len) == 0 && name[len] == '/') {
6532658a1dc7SSanjeev Bagewadi 				vfs_list_unlock();
6533658a1dc7SSanjeev Bagewadi 				if (write)
6534658a1dc7SSanjeev Bagewadi 					*write = 0;
6535658a1dc7SSanjeev Bagewadi 				return (1);
6536658a1dc7SSanjeev Bagewadi 			}
6537658a1dc7SSanjeev Bagewadi 		}
6538658a1dc7SSanjeev Bagewadi 		vfsp = vfsp->vfs_zone_next;
6539658a1dc7SSanjeev Bagewadi 	} while (vfsp != zone->zone_vfslist);
6540658a1dc7SSanjeev Bagewadi 
6541658a1dc7SSanjeev Bagewadi 	vfs_list_unlock();
6542fa9e4066Sahrens 	return (0);
6543fa9e4066Sahrens }
654445916cd2Sjpk 
654545916cd2Sjpk /*
654645916cd2Sjpk  * zone_find_by_any_path() -
654745916cd2Sjpk  *
654845916cd2Sjpk  * kernel-private routine similar to zone_find_by_path(), but which
654945916cd2Sjpk  * effectively compares against zone paths rather than zonerootpath
655045916cd2Sjpk  * (i.e., the last component of zonerootpaths, which should be "root/",
655145916cd2Sjpk  * are not compared.)  This is done in order to accurately identify all
655245916cd2Sjpk  * paths, whether zone-visible or not, including those which are parallel
655345916cd2Sjpk  * to /root/, such as /dev/, /home/, etc...
655445916cd2Sjpk  *
655545916cd2Sjpk  * If the specified path does not fall under any zone path then global
655645916cd2Sjpk  * zone is returned.
655745916cd2Sjpk  *
655845916cd2Sjpk  * The treat_abs parameter indicates whether the path should be treated as
655945916cd2Sjpk  * an absolute path although it does not begin with "/".  (This supports
656045916cd2Sjpk  * nfs mount syntax such as host:any/path.)
656145916cd2Sjpk  *
656245916cd2Sjpk  * The caller is responsible for zone_rele of the returned zone.
656345916cd2Sjpk  */
656445916cd2Sjpk zone_t *
656545916cd2Sjpk zone_find_by_any_path(const char *path, boolean_t treat_abs)
656645916cd2Sjpk {
656745916cd2Sjpk 	zone_t *zone;
656845916cd2Sjpk 	int path_offset = 0;
656945916cd2Sjpk 
657045916cd2Sjpk 	if (path == NULL) {
657145916cd2Sjpk 		zone_hold(global_zone);
657245916cd2Sjpk 		return (global_zone);
657345916cd2Sjpk 	}
657445916cd2Sjpk 
657545916cd2Sjpk 	if (*path != '/') {
657645916cd2Sjpk 		ASSERT(treat_abs);
657745916cd2Sjpk 		path_offset = 1;
657845916cd2Sjpk 	}
657945916cd2Sjpk 
658045916cd2Sjpk 	mutex_enter(&zonehash_lock);
658145916cd2Sjpk 	for (zone = list_head(&zone_active); zone != NULL;
658245916cd2Sjpk 	    zone = list_next(&zone_active, zone)) {
658345916cd2Sjpk 		char	*c;
658445916cd2Sjpk 		size_t	pathlen;
65850f95d722Smp46848 		char *rootpath_start;
658645916cd2Sjpk 
658745916cd2Sjpk 		if (zone == global_zone)	/* skip global zone */
658845916cd2Sjpk 			continue;
658945916cd2Sjpk 
659045916cd2Sjpk 		/* scan backwards to find start of last component */
659145916cd2Sjpk 		c = zone->zone_rootpath + zone->zone_rootpathlen - 2;
659245916cd2Sjpk 		do {
659345916cd2Sjpk 			c--;
659445916cd2Sjpk 		} while (*c != '/');
659545916cd2Sjpk 
65960f95d722Smp46848 		pathlen = c - zone->zone_rootpath + 1 - path_offset;
65970f95d722Smp46848 		rootpath_start = (zone->zone_rootpath + path_offset);
65980f95d722Smp46848 		if (strncmp(path, rootpath_start, pathlen) == 0)
659945916cd2Sjpk 			break;
660045916cd2Sjpk 	}
660145916cd2Sjpk 	if (zone == NULL)
660245916cd2Sjpk 		zone = global_zone;
660345916cd2Sjpk 	zone_hold(zone);
660445916cd2Sjpk 	mutex_exit(&zonehash_lock);
660545916cd2Sjpk 	return (zone);
660645916cd2Sjpk }
6607f4b3ec61Sdh155122 
6608f4b3ec61Sdh155122 /*
66092b24ab6bSSebastien Roy  * Finds a zone_dl_t with the given linkid in the given zone.  Returns the
66102b24ab6bSSebastien Roy  * zone_dl_t pointer if found, and NULL otherwise.
6611f4b3ec61Sdh155122  */
66122b24ab6bSSebastien Roy static zone_dl_t *
66132b24ab6bSSebastien Roy zone_find_dl(zone_t *zone, datalink_id_t linkid)
6614f4b3ec61Sdh155122 {
66152b24ab6bSSebastien Roy 	zone_dl_t *zdl;
6616f4b3ec61Sdh155122 
66172b24ab6bSSebastien Roy 	ASSERT(mutex_owned(&zone->zone_lock));
66182b24ab6bSSebastien Roy 	for (zdl = list_head(&zone->zone_dl_list); zdl != NULL;
66192b24ab6bSSebastien Roy 	    zdl = list_next(&zone->zone_dl_list, zdl)) {
66202b24ab6bSSebastien Roy 		if (zdl->zdl_id == linkid)
6621f4b3ec61Sdh155122 			break;
6622f4b3ec61Sdh155122 	}
66232b24ab6bSSebastien Roy 	return (zdl);
6624f4b3ec61Sdh155122 }
66252b24ab6bSSebastien Roy 
66262b24ab6bSSebastien Roy static boolean_t
66272b24ab6bSSebastien Roy zone_dl_exists(zone_t *zone, datalink_id_t linkid)
66282b24ab6bSSebastien Roy {
66292b24ab6bSSebastien Roy 	boolean_t exists;
66302b24ab6bSSebastien Roy 
66312b24ab6bSSebastien Roy 	mutex_enter(&zone->zone_lock);
66322b24ab6bSSebastien Roy 	exists = (zone_find_dl(zone, linkid) != NULL);
6633f4b3ec61Sdh155122 	mutex_exit(&zone->zone_lock);
66342b24ab6bSSebastien Roy 	return (exists);
6635f4b3ec61Sdh155122 }
6636f4b3ec61Sdh155122 
6637f4b3ec61Sdh155122 /*
66382b24ab6bSSebastien Roy  * Add an data link name for the zone.
6639f4b3ec61Sdh155122  */
6640f4b3ec61Sdh155122 static int
66412b24ab6bSSebastien Roy zone_add_datalink(zoneid_t zoneid, datalink_id_t linkid)
6642f4b3ec61Sdh155122 {
66432b24ab6bSSebastien Roy 	zone_dl_t *zdl;
6644f4b3ec61Sdh155122 	zone_t *zone;
6645f4b3ec61Sdh155122 	zone_t *thiszone;
6646f4b3ec61Sdh155122 
66472b24ab6bSSebastien Roy 	if ((thiszone = zone_find_by_id(zoneid)) == NULL)
6648f4b3ec61Sdh155122 		return (set_errno(ENXIO));
6649f4b3ec61Sdh155122 
66502b24ab6bSSebastien Roy 	/* Verify that the datalink ID doesn't already belong to a zone. */
6651f4b3ec61Sdh155122 	mutex_enter(&zonehash_lock);
6652f4b3ec61Sdh155122 	for (zone = list_head(&zone_active); zone != NULL;
6653f4b3ec61Sdh155122 	    zone = list_next(&zone_active, zone)) {
66542b24ab6bSSebastien Roy 		if (zone_dl_exists(zone, linkid)) {
6655f4b3ec61Sdh155122 			mutex_exit(&zonehash_lock);
6656f4b3ec61Sdh155122 			zone_rele(thiszone);
66572b24ab6bSSebastien Roy 			return (set_errno((zone == thiszone) ? EEXIST : EPERM));
6658f4b3ec61Sdh155122 		}
6659f4b3ec61Sdh155122 	}
66602b24ab6bSSebastien Roy 
66612b24ab6bSSebastien Roy 	zdl = kmem_zalloc(sizeof (*zdl), KM_SLEEP);
66622b24ab6bSSebastien Roy 	zdl->zdl_id = linkid;
6663550b6e40SSowmini Varadhan 	zdl->zdl_net = NULL;
6664f4b3ec61Sdh155122 	mutex_enter(&thiszone->zone_lock);
66652b24ab6bSSebastien Roy 	list_insert_head(&thiszone->zone_dl_list, zdl);
6666f4b3ec61Sdh155122 	mutex_exit(&thiszone->zone_lock);
6667f4b3ec61Sdh155122 	mutex_exit(&zonehash_lock);
6668f4b3ec61Sdh155122 	zone_rele(thiszone);
6669f4b3ec61Sdh155122 	return (0);
6670f4b3ec61Sdh155122 }
6671f4b3ec61Sdh155122 
6672f4b3ec61Sdh155122 static int
66732b24ab6bSSebastien Roy zone_remove_datalink(zoneid_t zoneid, datalink_id_t linkid)
6674f4b3ec61Sdh155122 {
66752b24ab6bSSebastien Roy 	zone_dl_t *zdl;
6676f4b3ec61Sdh155122 	zone_t *zone;
6677f4b3ec61Sdh155122 	int err = 0;
6678f4b3ec61Sdh155122 
66792b24ab6bSSebastien Roy 	if ((zone = zone_find_by_id(zoneid)) == NULL)
66802b24ab6bSSebastien Roy 		return (set_errno(EINVAL));
6681f4b3ec61Sdh155122 
66822b24ab6bSSebastien Roy 	mutex_enter(&zone->zone_lock);
66832b24ab6bSSebastien Roy 	if ((zdl = zone_find_dl(zone, linkid)) == NULL) {
66842b24ab6bSSebastien Roy 		err = ENXIO;
66852b24ab6bSSebastien Roy 	} else {
66862b24ab6bSSebastien Roy 		list_remove(&zone->zone_dl_list, zdl);
6687550b6e40SSowmini Varadhan 		if (zdl->zdl_net != NULL)
6688550b6e40SSowmini Varadhan 			nvlist_free(zdl->zdl_net);
66892b24ab6bSSebastien Roy 		kmem_free(zdl, sizeof (zone_dl_t));
66902b24ab6bSSebastien Roy 	}
66912b24ab6bSSebastien Roy 	mutex_exit(&zone->zone_lock);
66922b24ab6bSSebastien Roy 	zone_rele(zone);
66932b24ab6bSSebastien Roy 	return (err == 0 ? 0 : set_errno(err));
66942b24ab6bSSebastien Roy }
6695f4b3ec61Sdh155122 
6696f4b3ec61Sdh155122 /*
66972b24ab6bSSebastien Roy  * Using the zoneidp as ALL_ZONES, we can lookup which zone has been assigned
66982b24ab6bSSebastien Roy  * the linkid.  Otherwise we just check if the specified zoneidp has been
66992b24ab6bSSebastien Roy  * assigned the supplied linkid.
6700f4b3ec61Sdh155122  */
67012b24ab6bSSebastien Roy int
67022b24ab6bSSebastien Roy zone_check_datalink(zoneid_t *zoneidp, datalink_id_t linkid)
67032b24ab6bSSebastien Roy {
67042b24ab6bSSebastien Roy 	zone_t *zone;
67052b24ab6bSSebastien Roy 	int err = ENXIO;
67062b24ab6bSSebastien Roy 
67072b24ab6bSSebastien Roy 	if (*zoneidp != ALL_ZONES) {
67082b24ab6bSSebastien Roy 		if ((zone = zone_find_by_id(*zoneidp)) != NULL) {
67092b24ab6bSSebastien Roy 			if (zone_dl_exists(zone, linkid))
67102b24ab6bSSebastien Roy 				err = 0;
67112b24ab6bSSebastien Roy 			zone_rele(zone);
67122b24ab6bSSebastien Roy 		}
67132b24ab6bSSebastien Roy 		return (err);
67142b24ab6bSSebastien Roy 	}
67152b24ab6bSSebastien Roy 
6716f4b3ec61Sdh155122 	mutex_enter(&zonehash_lock);
6717f4b3ec61Sdh155122 	for (zone = list_head(&zone_active); zone != NULL;
6718f4b3ec61Sdh155122 	    zone = list_next(&zone_active, zone)) {
67192b24ab6bSSebastien Roy 		if (zone_dl_exists(zone, linkid)) {
67202b24ab6bSSebastien Roy 			*zoneidp = zone->zone_id;
67212b24ab6bSSebastien Roy 			err = 0;
67222b24ab6bSSebastien Roy 			break;
6723f4b3ec61Sdh155122 		}
6724f4b3ec61Sdh155122 	}
6725f4b3ec61Sdh155122 	mutex_exit(&zonehash_lock);
67262b24ab6bSSebastien Roy 	return (err);
6727f4b3ec61Sdh155122 }
6728f4b3ec61Sdh155122 
6729f4b3ec61Sdh155122 /*
67302b24ab6bSSebastien Roy  * Get the list of datalink IDs assigned to a zone.
67312b24ab6bSSebastien Roy  *
67322b24ab6bSSebastien Roy  * On input, *nump is the number of datalink IDs that can fit in the supplied
67332b24ab6bSSebastien Roy  * idarray.  Upon return, *nump is either set to the number of datalink IDs
67342b24ab6bSSebastien Roy  * that were placed in the array if the array was large enough, or to the
67352b24ab6bSSebastien Roy  * number of datalink IDs that the function needs to place in the array if the
67362b24ab6bSSebastien Roy  * array is too small.
6737f4b3ec61Sdh155122  */
6738f4b3ec61Sdh155122 static int
67392b24ab6bSSebastien Roy zone_list_datalink(zoneid_t zoneid, int *nump, datalink_id_t *idarray)
6740f4b3ec61Sdh155122 {
67412b24ab6bSSebastien Roy 	uint_t num, dlcount;
6742f4b3ec61Sdh155122 	zone_t *zone;
67432b24ab6bSSebastien Roy 	zone_dl_t *zdl;
67442b24ab6bSSebastien Roy 	datalink_id_t *idptr = idarray;
6745f4b3ec61Sdh155122 
6746f4b3ec61Sdh155122 	if (copyin(nump, &dlcount, sizeof (dlcount)) != 0)
6747f4b3ec61Sdh155122 		return (set_errno(EFAULT));
67482b24ab6bSSebastien Roy 	if ((zone = zone_find_by_id(zoneid)) == NULL)
6749f4b3ec61Sdh155122 		return (set_errno(ENXIO));
6750f4b3ec61Sdh155122 
6751f4b3ec61Sdh155122 	num = 0;
6752f4b3ec61Sdh155122 	mutex_enter(&zone->zone_lock);
67532b24ab6bSSebastien Roy 	for (zdl = list_head(&zone->zone_dl_list); zdl != NULL;
67542b24ab6bSSebastien Roy 	    zdl = list_next(&zone->zone_dl_list, zdl)) {
6755f4b3ec61Sdh155122 		/*
67562b24ab6bSSebastien Roy 		 * If the list is bigger than what the caller supplied, just
67572b24ab6bSSebastien Roy 		 * count, don't do copyout.
6758f4b3ec61Sdh155122 		 */
6759f4b3ec61Sdh155122 		if (++num > dlcount)
6760f4b3ec61Sdh155122 			continue;
67612b24ab6bSSebastien Roy 		if (copyout(&zdl->zdl_id, idptr, sizeof (*idptr)) != 0) {
6762f4b3ec61Sdh155122 			mutex_exit(&zone->zone_lock);
6763f4b3ec61Sdh155122 			zone_rele(zone);
6764f4b3ec61Sdh155122 			return (set_errno(EFAULT));
6765f4b3ec61Sdh155122 		}
67662b24ab6bSSebastien Roy 		idptr++;
6767f4b3ec61Sdh155122 	}
6768f4b3ec61Sdh155122 	mutex_exit(&zone->zone_lock);
6769f4b3ec61Sdh155122 	zone_rele(zone);
6770f4b3ec61Sdh155122 
6771f4b3ec61Sdh155122 	/* Increased or decreased, caller should be notified. */
6772f4b3ec61Sdh155122 	if (num != dlcount) {
67732b24ab6bSSebastien Roy 		if (copyout(&num, nump, sizeof (num)) != 0)
6774f4b3ec61Sdh155122 			return (set_errno(EFAULT));
6775f4b3ec61Sdh155122 	}
6776f4b3ec61Sdh155122 	return (0);
6777f4b3ec61Sdh155122 }
6778f4b3ec61Sdh155122 
6779f4b3ec61Sdh155122 /*
6780f4b3ec61Sdh155122  * Public interface for looking up a zone by zoneid. It's a customized version
6781bd41d0a8Snordmark  * for netstack_zone_create(). It can only be called from the zsd create
6782bd41d0a8Snordmark  * callbacks, since it doesn't have reference on the zone structure hence if
6783bd41d0a8Snordmark  * it is called elsewhere the zone could disappear after the zonehash_lock
6784bd41d0a8Snordmark  * is dropped.
6785bd41d0a8Snordmark  *
6786bd41d0a8Snordmark  * Furthermore it
6787bd41d0a8Snordmark  * 1. Doesn't check the status of the zone.
6788bd41d0a8Snordmark  * 2. It will be called even before zone_init is called, in that case the
6789f4b3ec61Sdh155122  *    address of zone0 is returned directly, and netstack_zone_create()
6790f4b3ec61Sdh155122  *    will only assign a value to zone0.zone_netstack, won't break anything.
6791bd41d0a8Snordmark  * 3. Returns without the zone being held.
6792f4b3ec61Sdh155122  */
6793f4b3ec61Sdh155122 zone_t *
6794f4b3ec61Sdh155122 zone_find_by_id_nolock(zoneid_t zoneid)
6795f4b3ec61Sdh155122 {
6796bd41d0a8Snordmark 	zone_t *zone;
6797f4b3ec61Sdh155122 
6798bd41d0a8Snordmark 	mutex_enter(&zonehash_lock);
6799f4b3ec61Sdh155122 	if (zonehashbyid == NULL)
6800bd41d0a8Snordmark 		zone = &zone0;
6801f4b3ec61Sdh155122 	else
6802bd41d0a8Snordmark 		zone = zone_find_all_by_id(zoneid);
6803bd41d0a8Snordmark 	mutex_exit(&zonehash_lock);
6804bd41d0a8Snordmark 	return (zone);
6805f4b3ec61Sdh155122 }
6806d62bc4baSyz147064 
6807d62bc4baSyz147064 /*
6808d62bc4baSyz147064  * Walk the datalinks for a given zone
6809d62bc4baSyz147064  */
6810d62bc4baSyz147064 int
68112b24ab6bSSebastien Roy zone_datalink_walk(zoneid_t zoneid, int (*cb)(datalink_id_t, void *),
68122b24ab6bSSebastien Roy     void *data)
6813d62bc4baSyz147064 {
6814d62bc4baSyz147064 	zone_t		*zone;
68152b24ab6bSSebastien Roy 	zone_dl_t	*zdl;
68162b24ab6bSSebastien Roy 	datalink_id_t	*idarray;
68172b24ab6bSSebastien Roy 	uint_t		idcount = 0;
68182b24ab6bSSebastien Roy 	int		i, ret = 0;
6819d62bc4baSyz147064 
6820d62bc4baSyz147064 	if ((zone = zone_find_by_id(zoneid)) == NULL)
6821d62bc4baSyz147064 		return (ENOENT);
6822d62bc4baSyz147064 
68232b24ab6bSSebastien Roy 	/*
68242b24ab6bSSebastien Roy 	 * We first build an array of linkid's so that we can walk these and
68252b24ab6bSSebastien Roy 	 * execute the callback with the zone_lock dropped.
68262b24ab6bSSebastien Roy 	 */
6827d62bc4baSyz147064 	mutex_enter(&zone->zone_lock);
68282b24ab6bSSebastien Roy 	for (zdl = list_head(&zone->zone_dl_list); zdl != NULL;
68292b24ab6bSSebastien Roy 	    zdl = list_next(&zone->zone_dl_list, zdl)) {
68302b24ab6bSSebastien Roy 		idcount++;
6831d62bc4baSyz147064 	}
68322b24ab6bSSebastien Roy 
68332b24ab6bSSebastien Roy 	if (idcount == 0) {
6834d62bc4baSyz147064 		mutex_exit(&zone->zone_lock);
6835d62bc4baSyz147064 		zone_rele(zone);
68362b24ab6bSSebastien Roy 		return (0);
68372b24ab6bSSebastien Roy 	}
68382b24ab6bSSebastien Roy 
68392b24ab6bSSebastien Roy 	idarray = kmem_alloc(sizeof (datalink_id_t) * idcount, KM_NOSLEEP);
68402b24ab6bSSebastien Roy 	if (idarray == NULL) {
68412b24ab6bSSebastien Roy 		mutex_exit(&zone->zone_lock);
68422b24ab6bSSebastien Roy 		zone_rele(zone);
68432b24ab6bSSebastien Roy 		return (ENOMEM);
68442b24ab6bSSebastien Roy 	}
68452b24ab6bSSebastien Roy 
68462b24ab6bSSebastien Roy 	for (i = 0, zdl = list_head(&zone->zone_dl_list); zdl != NULL;
68472b24ab6bSSebastien Roy 	    i++, zdl = list_next(&zone->zone_dl_list, zdl)) {
68482b24ab6bSSebastien Roy 		idarray[i] = zdl->zdl_id;
68492b24ab6bSSebastien Roy 	}
68502b24ab6bSSebastien Roy 
68512b24ab6bSSebastien Roy 	mutex_exit(&zone->zone_lock);
68522b24ab6bSSebastien Roy 
68532b24ab6bSSebastien Roy 	for (i = 0; i < idcount && ret == 0; i++) {
68542b24ab6bSSebastien Roy 		if ((ret = (*cb)(idarray[i], data)) != 0)
68552b24ab6bSSebastien Roy 			break;
68562b24ab6bSSebastien Roy 	}
68572b24ab6bSSebastien Roy 
68582b24ab6bSSebastien Roy 	zone_rele(zone);
68592b24ab6bSSebastien Roy 	kmem_free(idarray, sizeof (datalink_id_t) * idcount);
6860d62bc4baSyz147064 	return (ret);
6861d62bc4baSyz147064 }
6862550b6e40SSowmini Varadhan 
6863550b6e40SSowmini Varadhan static char *
6864550b6e40SSowmini Varadhan zone_net_type2name(int type)
6865550b6e40SSowmini Varadhan {
6866550b6e40SSowmini Varadhan 	switch (type) {
6867550b6e40SSowmini Varadhan 	case ZONE_NETWORK_ADDRESS:
6868550b6e40SSowmini Varadhan 		return (ZONE_NET_ADDRNAME);
6869550b6e40SSowmini Varadhan 	case ZONE_NETWORK_DEFROUTER:
6870550b6e40SSowmini Varadhan 		return (ZONE_NET_RTRNAME);
6871550b6e40SSowmini Varadhan 	default:
6872550b6e40SSowmini Varadhan 		return (NULL);
6873550b6e40SSowmini Varadhan 	}
6874550b6e40SSowmini Varadhan }
6875550b6e40SSowmini Varadhan 
6876550b6e40SSowmini Varadhan static int
6877550b6e40SSowmini Varadhan zone_set_network(zoneid_t zoneid, zone_net_data_t *znbuf)
6878550b6e40SSowmini Varadhan {
6879550b6e40SSowmini Varadhan 	zone_t *zone;
6880550b6e40SSowmini Varadhan 	zone_dl_t *zdl;
6881550b6e40SSowmini Varadhan 	nvlist_t *nvl;
6882550b6e40SSowmini Varadhan 	int err = 0;
6883550b6e40SSowmini Varadhan 	uint8_t *new = NULL;
6884550b6e40SSowmini Varadhan 	char *nvname;
6885550b6e40SSowmini Varadhan 	int bufsize;
6886550b6e40SSowmini Varadhan 	datalink_id_t linkid = znbuf->zn_linkid;
6887550b6e40SSowmini Varadhan 
6888550b6e40SSowmini Varadhan 	if (secpolicy_zone_config(CRED()) != 0)
6889550b6e40SSowmini Varadhan 		return (set_errno(EPERM));
6890550b6e40SSowmini Varadhan 
6891550b6e40SSowmini Varadhan 	if (zoneid == GLOBAL_ZONEID)
6892550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
6893550b6e40SSowmini Varadhan 
6894550b6e40SSowmini Varadhan 	nvname = zone_net_type2name(znbuf->zn_type);
6895550b6e40SSowmini Varadhan 	bufsize = znbuf->zn_len;
6896550b6e40SSowmini Varadhan 	new = znbuf->zn_val;
6897550b6e40SSowmini Varadhan 	if (nvname == NULL)
6898550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
6899550b6e40SSowmini Varadhan 
6900550b6e40SSowmini Varadhan 	if ((zone = zone_find_by_id(zoneid)) == NULL) {
6901550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
6902550b6e40SSowmini Varadhan 	}
6903550b6e40SSowmini Varadhan 
6904550b6e40SSowmini Varadhan 	mutex_enter(&zone->zone_lock);
6905550b6e40SSowmini Varadhan 	if ((zdl = zone_find_dl(zone, linkid)) == NULL) {
6906550b6e40SSowmini Varadhan 		err = ENXIO;
6907550b6e40SSowmini Varadhan 		goto done;
6908550b6e40SSowmini Varadhan 	}
6909550b6e40SSowmini Varadhan 	if ((nvl = zdl->zdl_net) == NULL) {
6910550b6e40SSowmini Varadhan 		if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)) {
6911550b6e40SSowmini Varadhan 			err = ENOMEM;
6912550b6e40SSowmini Varadhan 			goto done;
6913550b6e40SSowmini Varadhan 		} else {
6914550b6e40SSowmini Varadhan 			zdl->zdl_net = nvl;
6915550b6e40SSowmini Varadhan 		}
6916550b6e40SSowmini Varadhan 	}
6917550b6e40SSowmini Varadhan 	if (nvlist_exists(nvl, nvname)) {
6918550b6e40SSowmini Varadhan 		err = EINVAL;
6919550b6e40SSowmini Varadhan 		goto done;
6920550b6e40SSowmini Varadhan 	}
6921550b6e40SSowmini Varadhan 	err = nvlist_add_uint8_array(nvl, nvname, new, bufsize);
6922550b6e40SSowmini Varadhan 	ASSERT(err == 0);
6923550b6e40SSowmini Varadhan done:
6924550b6e40SSowmini Varadhan 	mutex_exit(&zone->zone_lock);
6925550b6e40SSowmini Varadhan 	zone_rele(zone);
6926550b6e40SSowmini Varadhan 	if (err != 0)
6927550b6e40SSowmini Varadhan 		return (set_errno(err));
6928550b6e40SSowmini Varadhan 	else
6929550b6e40SSowmini Varadhan 		return (0);
6930550b6e40SSowmini Varadhan }
6931550b6e40SSowmini Varadhan 
6932550b6e40SSowmini Varadhan static int
6933550b6e40SSowmini Varadhan zone_get_network(zoneid_t zoneid, zone_net_data_t *znbuf)
6934550b6e40SSowmini Varadhan {
6935550b6e40SSowmini Varadhan 	zone_t *zone;
6936550b6e40SSowmini Varadhan 	zone_dl_t *zdl;
6937550b6e40SSowmini Varadhan 	nvlist_t *nvl;
6938550b6e40SSowmini Varadhan 	uint8_t *ptr;
6939550b6e40SSowmini Varadhan 	uint_t psize;
6940550b6e40SSowmini Varadhan 	int err = 0;
6941550b6e40SSowmini Varadhan 	char *nvname;
6942550b6e40SSowmini Varadhan 	int bufsize;
6943550b6e40SSowmini Varadhan 	void *buf;
6944550b6e40SSowmini Varadhan 	datalink_id_t linkid = znbuf->zn_linkid;
6945550b6e40SSowmini Varadhan 
6946550b6e40SSowmini Varadhan 	if (zoneid == GLOBAL_ZONEID)
6947550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
6948550b6e40SSowmini Varadhan 
6949550b6e40SSowmini Varadhan 	nvname = zone_net_type2name(znbuf->zn_type);
6950550b6e40SSowmini Varadhan 	bufsize = znbuf->zn_len;
6951550b6e40SSowmini Varadhan 	buf = znbuf->zn_val;
6952550b6e40SSowmini Varadhan 
6953550b6e40SSowmini Varadhan 	if (nvname == NULL)
6954550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
6955550b6e40SSowmini Varadhan 	if ((zone = zone_find_by_id(zoneid)) == NULL)
6956550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
6957550b6e40SSowmini Varadhan 
6958550b6e40SSowmini Varadhan 	mutex_enter(&zone->zone_lock);
6959550b6e40SSowmini Varadhan 	if ((zdl = zone_find_dl(zone, linkid)) == NULL) {
6960550b6e40SSowmini Varadhan 		err = ENXIO;
6961550b6e40SSowmini Varadhan 		goto done;
6962550b6e40SSowmini Varadhan 	}
6963550b6e40SSowmini Varadhan 	if ((nvl = zdl->zdl_net) == NULL || !nvlist_exists(nvl, nvname)) {
6964550b6e40SSowmini Varadhan 		err = ENOENT;
6965550b6e40SSowmini Varadhan 		goto done;
6966550b6e40SSowmini Varadhan 	}
6967550b6e40SSowmini Varadhan 	err = nvlist_lookup_uint8_array(nvl, nvname, &ptr, &psize);
6968550b6e40SSowmini Varadhan 	ASSERT(err == 0);
6969550b6e40SSowmini Varadhan 
6970550b6e40SSowmini Varadhan 	if (psize > bufsize) {
6971550b6e40SSowmini Varadhan 		err = ENOBUFS;
6972550b6e40SSowmini Varadhan 		goto done;
6973550b6e40SSowmini Varadhan 	}
6974550b6e40SSowmini Varadhan 	znbuf->zn_len = psize;
6975550b6e40SSowmini Varadhan 	bcopy(ptr, buf, psize);
6976550b6e40SSowmini Varadhan done:
6977550b6e40SSowmini Varadhan 	mutex_exit(&zone->zone_lock);
6978550b6e40SSowmini Varadhan 	zone_rele(zone);
6979550b6e40SSowmini Varadhan 	if (err != 0)
6980550b6e40SSowmini Varadhan 		return (set_errno(err));
6981550b6e40SSowmini Varadhan 	else
6982550b6e40SSowmini Varadhan 		return (0);
6983550b6e40SSowmini Varadhan }
6984