xref: /illumos-gate/usr/src/uts/common/os/zone.c (revision 85dff7a0)
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.
24a48d8120SJerry Jelinek  * Copyright 2015, Joyent Inc. All rights reserved.
2548bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
2666d7818bSAndy Fiddaman  * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Zones
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  *   A zone is a named collection of processes, namespace constraints,
337c478bd9Sstevel@tonic-gate  *   and other system resources which comprise a secure and manageable
347c478bd9Sstevel@tonic-gate  *   application containment facility.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  *   Zones (represented by the reference counted zone_t) are tracked in
377c478bd9Sstevel@tonic-gate  *   the kernel in the zonehash.  Elsewhere in the kernel, Zone IDs
387c478bd9Sstevel@tonic-gate  *   (zoneid_t) are used to track zone association.  Zone IDs are
397c478bd9Sstevel@tonic-gate  *   dynamically generated when the zone is created; if a persistent
407c478bd9Sstevel@tonic-gate  *   identifier is needed (core files, accounting logs, audit trail,
417c478bd9Sstevel@tonic-gate  *   etc.), the zone name should be used.
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  *   Global Zone:
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  *   The global zone (zoneid 0) is automatically associated with all
477c478bd9Sstevel@tonic-gate  *   system resources that have not been bound to a user-created zone.
487c478bd9Sstevel@tonic-gate  *   This means that even systems where zones are not in active use
497c478bd9Sstevel@tonic-gate  *   have a global zone, and all processes, mounts, etc. are
507c478bd9Sstevel@tonic-gate  *   associated with that zone.  The global zone is generally
517c478bd9Sstevel@tonic-gate  *   unconstrained in terms of privileges and access, though the usual
527c478bd9Sstevel@tonic-gate  *   credential and privilege based restrictions apply.
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  *   Zone States:
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  *   The states in which a zone may be in and the transitions are as
587c478bd9Sstevel@tonic-gate  *   follows:
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  *   ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially
617c478bd9Sstevel@tonic-gate  *   initialized zone is added to the list of active zones on the system but
627c478bd9Sstevel@tonic-gate  *   isn't accessible.
637c478bd9Sstevel@tonic-gate  *
64bd41d0a8Snordmark  *   ZONE_IS_INITIALIZED: Initialization complete except the ZSD callbacks are
65bd41d0a8Snordmark  *   not yet completed. Not possible to enter the zone, but attributes can
66bd41d0a8Snordmark  *   be retrieved.
67bd41d0a8Snordmark  *
687c478bd9Sstevel@tonic-gate  *   ZONE_IS_READY: zsched (the kernel dummy process for a zone) is
697c478bd9Sstevel@tonic-gate  *   ready.  The zone is made visible after the ZSD constructor callbacks are
707c478bd9Sstevel@tonic-gate  *   executed.  A zone remains in this state until it transitions into
717c478bd9Sstevel@tonic-gate  *   the ZONE_IS_BOOTING state as a result of a call to zone_boot().
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  *   ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start
747c478bd9Sstevel@tonic-gate  *   init.  Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN
757c478bd9Sstevel@tonic-gate  *   state.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  *   ZONE_IS_RUNNING: The zone is open for business: zsched has
787c478bd9Sstevel@tonic-gate  *   successfully started init.   A zone remains in this state until
797c478bd9Sstevel@tonic-gate  *   zone_shutdown() is called.
807c478bd9Sstevel@tonic-gate  *
817c478bd9Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is
827c478bd9Sstevel@tonic-gate  *   killing all processes running in the zone. The zone remains
837c478bd9Sstevel@tonic-gate  *   in this state until there are no more user processes running in the zone.
847c478bd9Sstevel@tonic-gate  *   zone_create(), zone_enter(), and zone_destroy() on this zone will fail.
857c478bd9Sstevel@tonic-gate  *   Since zone_shutdown() is restartable, it may be called successfully
867c478bd9Sstevel@tonic-gate  *   multiple times for the same zone_t.  Setting of the zone's state to
877c478bd9Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check
887c478bd9Sstevel@tonic-gate  *   the zone's status without worrying about it being a moving target.
897c478bd9Sstevel@tonic-gate  *
907c478bd9Sstevel@tonic-gate  *   ZONE_IS_EMPTY: zone_shutdown() has been called, and there
917c478bd9Sstevel@tonic-gate  *   are no more user processes in the zone.  The zone remains in this
927c478bd9Sstevel@tonic-gate  *   state until there are no more kernel threads associated with the
937c478bd9Sstevel@tonic-gate  *   zone.  zone_create(), zone_enter(), and zone_destroy() on this zone will
947c478bd9Sstevel@tonic-gate  *   fail.
957c478bd9Sstevel@tonic-gate  *
967c478bd9Sstevel@tonic-gate  *   ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone
977c478bd9Sstevel@tonic-gate  *   have exited.  zone_shutdown() returns.  Henceforth it is not possible to
987c478bd9Sstevel@tonic-gate  *   join the zone or create kernel threads therein.
997c478bd9Sstevel@tonic-gate  *
1007c478bd9Sstevel@tonic-gate  *   ZONE_IS_DYING: zone_destroy() has been called on the zone; zone
1017c478bd9Sstevel@tonic-gate  *   remains in this state until zsched exits.  Calls to zone_find_by_*()
1027c478bd9Sstevel@tonic-gate  *   return NULL from now on.
1037c478bd9Sstevel@tonic-gate  *
1047c478bd9Sstevel@tonic-gate  *   ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0).  There are no
1057c478bd9Sstevel@tonic-gate  *   processes or threads doing work on behalf of the zone.  The zone is
1067c478bd9Sstevel@tonic-gate  *   removed from the list of active zones.  zone_destroy() returns, and
1077c478bd9Sstevel@tonic-gate  *   the zone can be recreated.
1087c478bd9Sstevel@tonic-gate  *
1097c478bd9Sstevel@tonic-gate  *   ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor
1107c478bd9Sstevel@tonic-gate  *   callbacks are executed, and all memory associated with the zone is
1117c478bd9Sstevel@tonic-gate  *   freed.
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  *   Threads can wait for the zone to enter a requested state by using
1147c478bd9Sstevel@tonic-gate  *   zone_status_wait() or zone_status_timedwait() with the desired
1157c478bd9Sstevel@tonic-gate  *   state passed in as an argument.  Zone state transitions are
1167c478bd9Sstevel@tonic-gate  *   uni-directional; it is not possible to move back to an earlier state.
1177c478bd9Sstevel@tonic-gate  *
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  *   Zone-Specific Data:
1207c478bd9Sstevel@tonic-gate  *
1217c478bd9Sstevel@tonic-gate  *   Subsystems needing to maintain zone-specific data can store that
1227c478bd9Sstevel@tonic-gate  *   data using the ZSD mechanism.  This provides a zone-specific data
1237c478bd9Sstevel@tonic-gate  *   store, similar to thread-specific data (see pthread_getspecific(3C)
1247c478bd9Sstevel@tonic-gate  *   or the TSD code in uts/common/disp/thread.c.  Also, ZSD can be used
1257c478bd9Sstevel@tonic-gate  *   to register callbacks to be invoked when a zone is created, shut
1267c478bd9Sstevel@tonic-gate  *   down, or destroyed.  This can be used to initialize zone-specific
1277c478bd9Sstevel@tonic-gate  *   data for new zones and to clean up when zones go away.
1287c478bd9Sstevel@tonic-gate  *
1297c478bd9Sstevel@tonic-gate  *
1307c478bd9Sstevel@tonic-gate  *   Data Structures:
1317c478bd9Sstevel@tonic-gate  *
1327c478bd9Sstevel@tonic-gate  *   The per-zone structure (zone_t) is reference counted, and freed
1337c478bd9Sstevel@tonic-gate  *   when all references are released.  zone_hold and zone_rele can be
1347c478bd9Sstevel@tonic-gate  *   used to adjust the reference count.  In addition, reference counts
1357c478bd9Sstevel@tonic-gate  *   associated with the cred_t structure are tracked separately using
1367c478bd9Sstevel@tonic-gate  *   zone_cred_hold and zone_cred_rele.
1377c478bd9Sstevel@tonic-gate  *
1387c478bd9Sstevel@tonic-gate  *   Pointers to active zone_t's are stored in two hash tables; one
1397c478bd9Sstevel@tonic-gate  *   for searching by id, the other for searching by name.  Lookups
1407c478bd9Sstevel@tonic-gate  *   can be performed on either basis, using zone_find_by_id and
1417c478bd9Sstevel@tonic-gate  *   zone_find_by_name.  Both return zone_t pointers with the zone
1427c478bd9Sstevel@tonic-gate  *   held, so zone_rele should be called when the pointer is no longer
1437c478bd9Sstevel@tonic-gate  *   needed.  Zones can also be searched by path; zone_find_by_path
1447c478bd9Sstevel@tonic-gate  *   returns the zone with which a path name is associated (global
1457c478bd9Sstevel@tonic-gate  *   zone if the path is not within some other zone's file system
1467c478bd9Sstevel@tonic-gate  *   hierarchy).  This currently requires iterating through each zone,
1477c478bd9Sstevel@tonic-gate  *   so it is slower than an id or name search via a hash table.
1487c478bd9Sstevel@tonic-gate  *
1497c478bd9Sstevel@tonic-gate  *
1507c478bd9Sstevel@tonic-gate  *   Locking:
1517c478bd9Sstevel@tonic-gate  *
1527c478bd9Sstevel@tonic-gate  *   zonehash_lock: This is a top-level global lock used to protect the
1537c478bd9Sstevel@tonic-gate  *       zone hash tables and lists.  Zones cannot be created or destroyed
1547c478bd9Sstevel@tonic-gate  *       while this lock is held.
1557c478bd9Sstevel@tonic-gate  *   zone_status_lock: This is a global lock protecting zone state.
1567c478bd9Sstevel@tonic-gate  *       Zones cannot change state while this lock is held.  It also
1577c478bd9Sstevel@tonic-gate  *       protects the list of kernel threads associated with a zone.
1587c478bd9Sstevel@tonic-gate  *   zone_lock: This is a per-zone lock used to protect several fields of
1597c478bd9Sstevel@tonic-gate  *       the zone_t (see <sys/zone.h> for details).  In addition, holding
1607c478bd9Sstevel@tonic-gate  *       this lock means that the zone cannot go away.
1610209230bSgjelinek  *   zone_nlwps_lock: This is a per-zone lock used to protect the fields
1620209230bSgjelinek  *	 related to the zone.max-lwps rctl.
1630209230bSgjelinek  *   zone_mem_lock: This is a per-zone lock used to protect the fields
1640209230bSgjelinek  *	 related to the zone.max-locked-memory and zone.max-swap rctls.
1650fbb751dSJohn Levon  *   zone_rctl_lock: This is a per-zone lock used to protect other rctls,
1660fbb751dSJohn Levon  *       currently just max_lofi
1677c478bd9Sstevel@tonic-gate  *   zsd_key_lock: This is a global lock protecting the key state for ZSD.
1687c478bd9Sstevel@tonic-gate  *   zone_deathrow_lock: This is a global lock protecting the "deathrow"
1697c478bd9Sstevel@tonic-gate  *       list (a list of zones in the ZONE_IS_DEAD state).
1707c478bd9Sstevel@tonic-gate  *
1717c478bd9Sstevel@tonic-gate  *   Ordering requirements:
1727c478bd9Sstevel@tonic-gate  *       pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock -->
1732918c4a3SJohn Levon  *       zone_lock --> zsd_key_lock --> pidlock --> p_lock
1747c478bd9Sstevel@tonic-gate  *
1750209230bSgjelinek  *   When taking zone_mem_lock or zone_nlwps_lock, the lock ordering is:
1760209230bSgjelinek  *	zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock
177ff19e029SMenno Lageman  *	zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_nlwps_lock
1780209230bSgjelinek  *
1797c478bd9Sstevel@tonic-gate  *   Blocking memory allocations are permitted while holding any of the
1807c478bd9Sstevel@tonic-gate  *   zone locks.
1817c478bd9Sstevel@tonic-gate  *
1827c478bd9Sstevel@tonic-gate  *
1837c478bd9Sstevel@tonic-gate  *   System Call Interface:
1847c478bd9Sstevel@tonic-gate  *
1857c478bd9Sstevel@tonic-gate  *   The zone subsystem can be managed and queried from user level with
1867c478bd9Sstevel@tonic-gate  *   the following system calls (all subcodes of the primary "zone"
1877c478bd9Sstevel@tonic-gate  *   system call):
1887c478bd9Sstevel@tonic-gate  *   - zone_create: creates a zone with selected attributes (name,
189fa9e4066Sahrens  *     root path, privileges, resource controls, ZFS datasets)
1907c478bd9Sstevel@tonic-gate  *   - zone_enter: allows the current process to enter a zone
1917c478bd9Sstevel@tonic-gate  *   - zone_getattr: reports attributes of a zone
1923f2f09c1Sdp  *   - zone_setattr: set attributes of a zone
1933f2f09c1Sdp  *   - zone_boot: set 'init' running for the zone
1947c478bd9Sstevel@tonic-gate  *   - zone_list: lists all zones active in the system
1957c478bd9Sstevel@tonic-gate  *   - zone_lookup: looks up zone id based on name
1967c478bd9Sstevel@tonic-gate  *   - zone_shutdown: initiates shutdown process (see states above)
1977c478bd9Sstevel@tonic-gate  *   - zone_destroy: completes shutdown process (see states above)
1987c478bd9Sstevel@tonic-gate  *
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate #include <sys/priv_impl.h>
2027c478bd9Sstevel@tonic-gate #include <sys/cred.h>
2037c478bd9Sstevel@tonic-gate #include <c2/audit.h>
2047c478bd9Sstevel@tonic-gate #include <sys/debug.h>
2057c478bd9Sstevel@tonic-gate #include <sys/file.h>
2067c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
2070209230bSgjelinek #include <sys/kstat.h>
2087c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
20945916cd2Sjpk #include <sys/note.h>
2107c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
2117c478bd9Sstevel@tonic-gate #include <sys/proc.h>
2127c478bd9Sstevel@tonic-gate #include <sys/project.h>
213cf8f45c7Sdstaff #include <sys/sysevent.h>
2147c478bd9Sstevel@tonic-gate #include <sys/task.h>
2157c478bd9Sstevel@tonic-gate #include <sys/systm.h>
2167c478bd9Sstevel@tonic-gate #include <sys/types.h>
2177c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
2187c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
2197c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
2207c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
2217c478bd9Sstevel@tonic-gate #include <sys/policy.h>
2227c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
2237c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h>
2247c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
2257c478bd9Sstevel@tonic-gate #include <sys/class.h>
2267c478bd9Sstevel@tonic-gate #include <sys/pool.h>
2277c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
2287c478bd9Sstevel@tonic-gate #include <sys/pset.h>
229a19609f8Sjv #include <sys/strlog.h>
2307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
2317c478bd9Sstevel@tonic-gate #include <sys/callb.h>
2327c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
2337c478bd9Sstevel@tonic-gate #include <sys/corectl.h>
234824c205fSml #include <sys/ipc_impl.h>
235134a1f4eSCasper H.S. Dik #include <sys/klpd.h>
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate #include <sys/door.h>
2387c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
239bd41d0a8Snordmark #include <sys/sdt.h>
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
2427c478bd9Sstevel@tonic-gate #include <sys/session.h>
2437c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
2447c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
2453f2f09c1Sdp #include <sys/sunddi.h>
2467c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
2477c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
2487c478bd9Sstevel@tonic-gate #include <sys/fss.h>
2499acbbeafSnn #include <sys/brand.h>
2507c478bd9Sstevel@tonic-gate #include <sys/zone.h>
251f4b3ec61Sdh #include <net/if.h>
252c97ad5cdSakolb #include <sys/cpucaps.h>
2530209230bSgjelinek #include <vm/seg.h>
2542b24ab6bSSebastien Roy #include <sys/mac.h>
2552b24ab6bSSebastien Roy 
256a19609f8Sjv /*
257a19609f8Sjv  * This constant specifies the number of seconds that threads waiting for
258a19609f8Sjv  * subsystems to release a zone's general-purpose references will wait before
259a19609f8Sjv  * they log the zone's reference counts.  The constant's value shouldn't
260a19609f8Sjv  * be so small that reference counts are unnecessarily reported for zones
261a19609f8Sjv  * whose references are slowly released.  On the other hand, it shouldn't be so
262a19609f8Sjv  * large that users reboot their systems out of frustration over hung zones
263a19609f8Sjv  * before the system logs the zones' reference counts.
264a19609f8Sjv  */
265a19609f8Sjv #define	ZONE_DESTROY_TIMEOUT_SECS	60
266a19609f8Sjv 
2672b24ab6bSSebastien Roy /* List of data link IDs which are accessible from the zone */
2682b24ab6bSSebastien Roy typedef struct zone_dl {
2692b24ab6bSSebastien Roy 	datalink_id_t	zdl_id;
270550b6e40SSowmini Varadhan 	nvlist_t	*zdl_net;
2712b24ab6bSSebastien Roy 	list_node_t	zdl_linkage;
2722b24ab6bSSebastien Roy } zone_dl_t;
2730209230bSgjelinek 
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate  * cv used to signal that all references to the zone have been released.  This
2767c478bd9Sstevel@tonic-gate  * needs to be global since there may be multiple waiters, and the first to
2777c478bd9Sstevel@tonic-gate  * wake up will free the zone_t, hence we cannot use zone->zone_cv.
2787c478bd9Sstevel@tonic-gate  */
2797c478bd9Sstevel@tonic-gate static kcondvar_t zone_destroy_cv;
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate  * Lock used to serialize access to zone_cv.  This could have been per-zone,
2827c478bd9Sstevel@tonic-gate  * but then we'd need another lock for zone_destroy_cv, and why bother?
2837c478bd9Sstevel@tonic-gate  */
2847c478bd9Sstevel@tonic-gate static kmutex_t zone_status_lock;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate /*
2877c478bd9Sstevel@tonic-gate  * ZSD-related global variables.
2887c478bd9Sstevel@tonic-gate  */
2897c478bd9Sstevel@tonic-gate static kmutex_t zsd_key_lock;	/* protects the following two */
2907c478bd9Sstevel@tonic-gate /*
2917c478bd9Sstevel@tonic-gate  * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval.
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate static zone_key_t zsd_keyval = 0;
2947c478bd9Sstevel@tonic-gate /*
2957c478bd9Sstevel@tonic-gate  * Global list of registered keys.  We use this when a new zone is created.
2967c478bd9Sstevel@tonic-gate  */
2977c478bd9Sstevel@tonic-gate static list_t zsd_registered_keys;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate int zone_hash_size = 256;
30045916cd2Sjpk static mod_hash_t *zonehashbyname, *zonehashbyid, *zonehashbylabel;
3017c478bd9Sstevel@tonic-gate static kmutex_t zonehash_lock;
3027c478bd9Sstevel@tonic-gate static uint_t zonecount;
3037c478bd9Sstevel@tonic-gate static id_space_t *zoneid_space;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate /*
3067c478bd9Sstevel@tonic-gate  * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the
3077c478bd9Sstevel@tonic-gate  * kernel proper runs, and which manages all other zones.
3087c478bd9Sstevel@tonic-gate  *
3097c478bd9Sstevel@tonic-gate  * Although not declared as static, the variable "zone0" should not be used
3107c478bd9Sstevel@tonic-gate  * except for by code that needs to reference the global zone early on in boot,
3117c478bd9Sstevel@tonic-gate  * before it is fully initialized.  All other consumers should use
3127c478bd9Sstevel@tonic-gate  * 'global_zone'.
3137c478bd9Sstevel@tonic-gate  */
3147c478bd9Sstevel@tonic-gate zone_t zone0;
3157c478bd9Sstevel@tonic-gate zone_t *global_zone = NULL;	/* Set when the global zone is initialized */
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate /*
3187c478bd9Sstevel@tonic-gate  * List of active zones, protected by zonehash_lock.
3197c478bd9Sstevel@tonic-gate  */
3207c478bd9Sstevel@tonic-gate static list_t zone_active;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate /*
3237c478bd9Sstevel@tonic-gate  * List of destroyed zones that still have outstanding cred references.
3247c478bd9Sstevel@tonic-gate  * Used for debugging.  Uses a separate lock to avoid lock ordering
3257c478bd9Sstevel@tonic-gate  * problems in zone_free.
3267c478bd9Sstevel@tonic-gate  */
3277c478bd9Sstevel@tonic-gate static list_t zone_deathrow;
3287c478bd9Sstevel@tonic-gate static kmutex_t zone_deathrow_lock;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */
3317c478bd9Sstevel@tonic-gate uint_t maxzones = 8192;
3327c478bd9Sstevel@tonic-gate 
333cf8f45c7Sdstaff /* Event channel to sent zone state change notifications */
334cf8f45c7Sdstaff evchan_t *zone_event_chan;
335cf8f45c7Sdstaff 
336cf8f45c7Sdstaff /*
337cf8f45c7Sdstaff  * This table holds the mapping from kernel zone states to
338cf8f45c7Sdstaff  * states visible in the state notification API.
339cf8f45c7Sdstaff  * The idea is that we only expose "obvious" states and
340cf8f45c7Sdstaff  * do not expose states which are just implementation details.
341cf8f45c7Sdstaff  */
342cf8f45c7Sdstaff const char  *zone_status_table[] = {
343cf8f45c7Sdstaff 	ZONE_EVENT_UNINITIALIZED,	/* uninitialized */
344bd41d0a8Snordmark 	ZONE_EVENT_INITIALIZED,		/* initialized */
345cf8f45c7Sdstaff 	ZONE_EVENT_READY,		/* ready */
346cf8f45c7Sdstaff 	ZONE_EVENT_READY,		/* booting */
347cf8f45c7Sdstaff 	ZONE_EVENT_RUNNING,		/* running */
348cf8f45c7Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* shutting_down */
349cf8f45c7Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* empty */
350cf8f45c7Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* down */
351cf8f45c7Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* dying */
352cf8f45c7Sdstaff 	ZONE_EVENT_UNINITIALIZED,	/* dead */
353cf8f45c7Sdstaff };
354cf8f45c7Sdstaff 
355a19609f8Sjv /*
356a19609f8Sjv  * This array contains the names of the subsystems listed in zone_ref_subsys_t
357a19609f8Sjv  * (see sys/zone.h).
358a19609f8Sjv  */
359a19609f8Sjv static char *zone_ref_subsys_names[] = {
360a19609f8Sjv 	"NFS",		/* ZONE_REF_NFS */
361a19609f8Sjv 	"NFSv4",	/* ZONE_REF_NFSV4 */
362a19609f8Sjv 	"SMBFS",	/* ZONE_REF_SMBFS */
363a19609f8Sjv 	"MNTFS",	/* ZONE_REF_MNTFS */
364a19609f8Sjv 	"LOFI",		/* ZONE_REF_LOFI */
365a19609f8Sjv 	"VFS",		/* ZONE_REF_VFS */
366a19609f8Sjv 	"IPC"		/* ZONE_REF_IPC */
367a19609f8Sjv };
368a19609f8Sjv 
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate  * This isn't static so lint doesn't complain.
3717c478bd9Sstevel@tonic-gate  */
3727c478bd9Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares;
373c6939658Ssl rctl_hndl_t rc_zone_locked_mem;
3740209230bSgjelinek rctl_hndl_t rc_zone_max_swap;
3750fbb751dSJohn Levon rctl_hndl_t rc_zone_max_lofi;
376c97ad5cdSakolb rctl_hndl_t rc_zone_cpu_cap;
3777c478bd9Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps;
378ff19e029SMenno Lageman rctl_hndl_t rc_zone_nprocs;
379824c205fSml rctl_hndl_t rc_zone_shmmax;
380824c205fSml rctl_hndl_t rc_zone_shmmni;
381824c205fSml rctl_hndl_t rc_zone_semmni;
382824c205fSml rctl_hndl_t rc_zone_msgmni;
3837c478bd9Sstevel@tonic-gate 
3843f2f09c1Sdp const char * const zone_default_initname = "/sbin/init";
38545916cd2Sjpk static char * const zone_prefix = "/zone/";
3867c478bd9Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid);
3872b24ab6bSSebastien Roy static int zone_add_datalink(zoneid_t, datalink_id_t);
3882b24ab6bSSebastien Roy static int zone_remove_datalink(zoneid_t, datalink_id_t);
3892b24ab6bSSebastien Roy static int zone_list_datalink(zoneid_t, int *, datalink_id_t *);
390550b6e40SSowmini Varadhan static int zone_set_network(zoneid_t, zone_net_data_t *);
391550b6e40SSowmini Varadhan static int zone_get_network(zoneid_t, zone_net_data_t *);
3927c478bd9Sstevel@tonic-gate 
393bd41d0a8Snordmark typedef boolean_t zsd_applyfn_t(kmutex_t *, boolean_t, zone_t *, zone_key_t);
394bd41d0a8Snordmark 
395bd41d0a8Snordmark static void zsd_apply_all_zones(zsd_applyfn_t *, zone_key_t);
396bd41d0a8Snordmark static void zsd_apply_all_keys(zsd_applyfn_t *, zone_t *);
397bd41d0a8Snordmark static boolean_t zsd_apply_create(kmutex_t *, boolean_t, zone_t *, zone_key_t);
398bd41d0a8Snordmark static boolean_t zsd_apply_shutdown(kmutex_t *, boolean_t, zone_t *,
399bd41d0a8Snordmark     zone_key_t);
400bd41d0a8Snordmark static boolean_t zsd_apply_destroy(kmutex_t *, boolean_t, zone_t *, zone_key_t);
401bd41d0a8Snordmark static boolean_t zsd_wait_for_creator(zone_t *, struct zsd_entry *,
402bd41d0a8Snordmark     kmutex_t *);
403bd41d0a8Snordmark static boolean_t zsd_wait_for_inprogress(zone_t *, struct zsd_entry *,
404bd41d0a8Snordmark     kmutex_t *);
405bd41d0a8Snordmark 
406821c4a97Sdp /*
407821c4a97Sdp  * Bump this number when you alter the zone syscall interfaces; this is
408821c4a97Sdp  * because we need to have support for previous API versions in libc
409821c4a97Sdp  * to support patching; libc calls into the kernel to determine this number.
410821c4a97Sdp  *
411821c4a97Sdp  * Version 1 of the API is the version originally shipped with Solaris 10
412821c4a97Sdp  * Version 2 alters the zone_create system call in order to support more
413821c4a97Sdp  *     arguments by moving the args into a structure; and to do better
414821c4a97Sdp  *     error reporting when zone_create() fails.
415821c4a97Sdp  * Version 3 alters the zone_create system call in order to support the
416821c4a97Sdp  *     import of ZFS datasets to zones.
41745916cd2Sjpk  * Version 4 alters the zone_create system call in order to support
41845916cd2Sjpk  *     Trusted Extensions.
4193f2f09c1Sdp  * Version 5 alters the zone_boot system call, and converts its old
4203f2f09c1Sdp  *     bootargs parameter to be set by the zone_setattr API instead.
421f4b3ec61Sdh  * Version 6 adds the flag argument to zone_create.
422821c4a97Sdp  */
423f4b3ec61Sdh static const int ZONE_SYSCALL_API_VERSION = 6;
424821c4a97Sdp 
4257c478bd9Sstevel@tonic-gate /*
4267c478bd9Sstevel@tonic-gate  * Certain filesystems (such as NFS and autofs) need to know which zone
4277c478bd9Sstevel@tonic-gate  * the mount is being placed in.  Because of this, we need to be able to
4285fd5c689SJerry Jelinek  * ensure that a zone isn't in the process of being created/destroyed such
4295fd5c689SJerry Jelinek  * that nfs_mount() thinks it is in the global/NGZ zone, while by the time
4305fd5c689SJerry Jelinek  * it gets added the list of mounted zones, it ends up on the wrong zone's
4315fd5c689SJerry Jelinek  * mount list. Since a zone can't reside on an NFS file system, we don't
4325fd5c689SJerry Jelinek  * have to worry about the zonepath itself.
4337c478bd9Sstevel@tonic-gate  *
4347c478bd9Sstevel@tonic-gate  * The following functions: block_mounts()/resume_mounts() and
4357c478bd9Sstevel@tonic-gate  * mount_in_progress()/mount_completed() are used by zones and the VFS
4365fd5c689SJerry Jelinek  * layer (respectively) to synchronize zone state transitions and new
4375fd5c689SJerry Jelinek  * mounts within a zone. This syncronization is on a per-zone basis, so
4385fd5c689SJerry Jelinek  * activity for one zone will not interfere with activity for another zone.
4397c478bd9Sstevel@tonic-gate  *
4407c478bd9Sstevel@tonic-gate  * The semantics are like a reader-reader lock such that there may
4415fd5c689SJerry Jelinek  * either be multiple mounts (or zone state transitions, if that weren't
4427c478bd9Sstevel@tonic-gate  * serialized by zonehash_lock) in progress at the same time, but not
4437c478bd9Sstevel@tonic-gate  * both.
4447c478bd9Sstevel@tonic-gate  *
4457c478bd9Sstevel@tonic-gate  * We use cv's so the user can ctrl-C out of the operation if it's
4467c478bd9Sstevel@tonic-gate  * taking too long.
4477c478bd9Sstevel@tonic-gate  *
4487c478bd9Sstevel@tonic-gate  * The semantics are such that there is unfair bias towards the
4495fd5c689SJerry Jelinek  * "current" operation.  This means that zone halt may starve if
4505fd5c689SJerry Jelinek  * there is a rapid succession of new mounts coming in to the zone.
4517c478bd9Sstevel@tonic-gate  */
4527c478bd9Sstevel@tonic-gate /*
4537c478bd9Sstevel@tonic-gate  * Prevent new mounts from progressing to the point of calling
4547c478bd9Sstevel@tonic-gate  * VFS_MOUNT().  If there are already mounts in this "region", wait for
4557c478bd9Sstevel@tonic-gate  * them to complete.
4567c478bd9Sstevel@tonic-gate  */
4577c478bd9Sstevel@tonic-gate static int
block_mounts(zone_t * zp)4585fd5c689SJerry Jelinek block_mounts(zone_t *zp)
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	int retval = 0;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	/*
4637c478bd9Sstevel@tonic-gate 	 * Since it may block for a long time, block_mounts() shouldn't be
4647c478bd9Sstevel@tonic-gate 	 * called with zonehash_lock held.
4657c478bd9Sstevel@tonic-gate 	 */
4667c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
4675fd5c689SJerry Jelinek 	mutex_enter(&zp->zone_mount_lock);
4685fd5c689SJerry Jelinek 	while (zp->zone_mounts_in_progress > 0) {
4695fd5c689SJerry Jelinek 		if (cv_wait_sig(&zp->zone_mount_cv, &zp->zone_mount_lock) == 0)
4707c478bd9Sstevel@tonic-gate 			goto signaled;
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 	/*
4737c478bd9Sstevel@tonic-gate 	 * A negative value of mounts_in_progress indicates that mounts
4745fd5c689SJerry Jelinek 	 * have been blocked by (-mounts_in_progress) different callers
4755fd5c689SJerry Jelinek 	 * (remotely possible if two threads enter zone_shutdown at the same
4765fd5c689SJerry Jelinek 	 * time).
4777c478bd9Sstevel@tonic-gate 	 */
4785fd5c689SJerry Jelinek 	zp->zone_mounts_in_progress--;
4797c478bd9Sstevel@tonic-gate 	retval = 1;
4807c478bd9Sstevel@tonic-gate signaled:
4815fd5c689SJerry Jelinek 	mutex_exit(&zp->zone_mount_lock);
4827c478bd9Sstevel@tonic-gate 	return (retval);
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate /*
4867c478bd9Sstevel@tonic-gate  * The VFS layer may progress with new mounts as far as we're concerned.
4877c478bd9Sstevel@tonic-gate  * Allow them to progress if we were the last obstacle.
4887c478bd9Sstevel@tonic-gate  */
4897c478bd9Sstevel@tonic-gate static void
resume_mounts(zone_t * zp)4905fd5c689SJerry Jelinek resume_mounts(zone_t *zp)
4917c478bd9Sstevel@tonic-gate {
4925fd5c689SJerry Jelinek 	mutex_enter(&zp->zone_mount_lock);
4935fd5c689SJerry Jelinek 	if (++zp->zone_mounts_in_progress == 0)
4945fd5c689SJerry Jelinek 		cv_broadcast(&zp->zone_mount_cv);
4955fd5c689SJerry Jelinek 	mutex_exit(&zp->zone_mount_lock);
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate /*
4995fd5c689SJerry Jelinek  * The VFS layer is busy with a mount; this zone should wait until all
5005fd5c689SJerry Jelinek  * of its mounts are completed to progress.
5017c478bd9Sstevel@tonic-gate  */
5027c478bd9Sstevel@tonic-gate void
mount_in_progress(zone_t * zp)5035fd5c689SJerry Jelinek mount_in_progress(zone_t *zp)
5047c478bd9Sstevel@tonic-gate {
5055fd5c689SJerry Jelinek 	mutex_enter(&zp->zone_mount_lock);
5065fd5c689SJerry Jelinek 	while (zp->zone_mounts_in_progress < 0)
5075fd5c689SJerry Jelinek 		cv_wait(&zp->zone_mount_cv, &zp->zone_mount_lock);
5085fd5c689SJerry Jelinek 	zp->zone_mounts_in_progress++;
5095fd5c689SJerry Jelinek 	mutex_exit(&zp->zone_mount_lock);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate /*
5137c478bd9Sstevel@tonic-gate  * VFS is done with one mount; wake up any waiting block_mounts()
5147c478bd9Sstevel@tonic-gate  * callers if this is the last mount.
5157c478bd9Sstevel@tonic-gate  */
5167c478bd9Sstevel@tonic-gate void
mount_completed(zone_t * zp)5175fd5c689SJerry Jelinek mount_completed(zone_t *zp)
5187c478bd9Sstevel@tonic-gate {
5195fd5c689SJerry Jelinek 	mutex_enter(&zp->zone_mount_lock);
5205fd5c689SJerry Jelinek 	if (--zp->zone_mounts_in_progress == 0)
5215fd5c689SJerry Jelinek 		cv_broadcast(&zp->zone_mount_cv);
5225fd5c689SJerry Jelinek 	mutex_exit(&zp->zone_mount_lock);
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate /*
5267c478bd9Sstevel@tonic-gate  * ZSD routines.
5277c478bd9Sstevel@tonic-gate  *
5287c478bd9Sstevel@tonic-gate  * Zone Specific Data (ZSD) is modeled after Thread Specific Data as
5297c478bd9Sstevel@tonic-gate  * defined by the pthread_key_create() and related interfaces.
5307c478bd9Sstevel@tonic-gate  *
5317c478bd9Sstevel@tonic-gate  * Kernel subsystems may register one or more data items and/or
5327c478bd9Sstevel@tonic-gate  * callbacks to be executed when a zone is created, shutdown, or
5337c478bd9Sstevel@tonic-gate  * destroyed.
5347c478bd9Sstevel@tonic-gate  *
5357c478bd9Sstevel@tonic-gate  * Unlike the thread counterpart, destructor callbacks will be executed
5367c478bd9Sstevel@tonic-gate  * even if the data pointer is NULL and/or there are no constructor
5377c478bd9Sstevel@tonic-gate  * callbacks, so it is the responsibility of such callbacks to check for
5387c478bd9Sstevel@tonic-gate  * NULL data values if necessary.
5397c478bd9Sstevel@tonic-gate  *
5407c478bd9Sstevel@tonic-gate  * The locking strategy and overall picture is as follows:
5417c478bd9Sstevel@tonic-gate  *
5427c478bd9Sstevel@tonic-gate  * When someone calls zone_key_create(), a template ZSD entry is added to the
543bd41d0a8Snordmark  * global list "zsd_registered_keys", protected by zsd_key_lock.  While
544bd41d0a8Snordmark  * holding that lock all the existing zones are marked as
545bd41d0a8Snordmark  * ZSD_CREATE_NEEDED and a copy of the ZSD entry added to the per-zone
546bd41d0a8Snordmark  * zone_zsd list (protected by zone_lock). The global list is updated first
547bd41d0a8Snordmark  * (under zone_key_lock) to make sure that newly created zones use the
548bd41d0a8Snordmark  * most recent list of keys. Then under zonehash_lock we walk the zones
549bd41d0a8Snordmark  * and mark them.  Similar locking is used in zone_key_delete().
5507c478bd9Sstevel@tonic-gate  *
551bd41d0a8Snordmark  * The actual create, shutdown, and destroy callbacks are done without
552bd41d0a8Snordmark  * holding any lock. And zsd_flags are used to ensure that the operations
553bd41d0a8Snordmark  * completed so that when zone_key_create (and zone_create) is done, as well as
554bd41d0a8Snordmark  * zone_key_delete (and zone_destroy) is done, all the necessary callbacks
555bd41d0a8Snordmark  * are completed.
5567c478bd9Sstevel@tonic-gate  *
5577c478bd9Sstevel@tonic-gate  * When new zones are created constructor callbacks for all registered ZSD
558bd41d0a8Snordmark  * entries will be called. That also uses the above two phases of marking
559bd41d0a8Snordmark  * what needs to be done, and then running the callbacks without holding
560bd41d0a8Snordmark  * any locks.
5617c478bd9Sstevel@tonic-gate  *
5627c478bd9Sstevel@tonic-gate  * The framework does not provide any locking around zone_getspecific() and
5637c478bd9Sstevel@tonic-gate  * zone_setspecific() apart from that needed for internal consistency, so
5647c478bd9Sstevel@tonic-gate  * callers interested in atomic "test-and-set" semantics will need to provide
5657c478bd9Sstevel@tonic-gate  * their own locking.
5667c478bd9Sstevel@tonic-gate  */
5677c478bd9Sstevel@tonic-gate 
568bd41d0a8Snordmark /*
569bd41d0a8Snordmark  * Helper function to find the zsd_entry associated with the key in the
570bd41d0a8Snordmark  * given list.
571bd41d0a8Snordmark  */
572bd41d0a8Snordmark static struct zsd_entry *
zsd_find(list_t * l,zone_key_t key)573bd41d0a8Snordmark zsd_find(list_t *l, zone_key_t key)
574bd41d0a8Snordmark {
575bd41d0a8Snordmark 	struct zsd_entry *zsd;
5767c478bd9Sstevel@tonic-gate 
577bd41d0a8Snordmark 	for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) {
578bd41d0a8Snordmark 		if (zsd->zsd_key == key) {
579bd41d0a8Snordmark 			return (zsd);
5807c478bd9Sstevel@tonic-gate 		}
5817c478bd9Sstevel@tonic-gate 	}
582bd41d0a8Snordmark 	return (NULL);
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate  * Helper function to find the zsd_entry associated with the key in the
587bd41d0a8Snordmark  * given list. Move it to the front of the list.
5887c478bd9Sstevel@tonic-gate  */
5897c478bd9Sstevel@tonic-gate static struct zsd_entry *
zsd_find_mru(list_t * l,zone_key_t key)590bd41d0a8Snordmark zsd_find_mru(list_t *l, zone_key_t key)
5917c478bd9Sstevel@tonic-gate {
5927c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsd;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) {
5957c478bd9Sstevel@tonic-gate 		if (zsd->zsd_key == key) {
5967c478bd9Sstevel@tonic-gate 			/*
5977c478bd9Sstevel@tonic-gate 			 * Move to head of list to keep list in MRU order.
5987c478bd9Sstevel@tonic-gate 			 */
5997c478bd9Sstevel@tonic-gate 			if (zsd != list_head(l)) {
6007c478bd9Sstevel@tonic-gate 				list_remove(l, zsd);
6017c478bd9Sstevel@tonic-gate 				list_insert_head(l, zsd);
6027c478bd9Sstevel@tonic-gate 			}
6037c478bd9Sstevel@tonic-gate 			return (zsd);
6047c478bd9Sstevel@tonic-gate 		}
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 	return (NULL);
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
609bd41d0a8Snordmark void
zone_key_create(zone_key_t * keyp,void * (* create)(zoneid_t),void (* shutdown)(zoneid_t,void *),void (* destroy)(zoneid_t,void *))610bd41d0a8Snordmark zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t),
611bd41d0a8Snordmark     void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *))
612bd41d0a8Snordmark {
613bd41d0a8Snordmark 	struct zsd_entry *zsdp;
614bd41d0a8Snordmark 	struct zsd_entry *t;
615bd41d0a8Snordmark 	struct zone *zone;
616bd41d0a8Snordmark 	zone_key_t  key;
617bd41d0a8Snordmark 
618bd41d0a8Snordmark 	zsdp = kmem_zalloc(sizeof (*zsdp), KM_SLEEP);
619bd41d0a8Snordmark 	zsdp->zsd_data = NULL;
620bd41d0a8Snordmark 	zsdp->zsd_create = create;
621bd41d0a8Snordmark 	zsdp->zsd_shutdown = shutdown;
622bd41d0a8Snordmark 	zsdp->zsd_destroy = destroy;
623bd41d0a8Snordmark 
624bd41d0a8Snordmark 	/*
625bd41d0a8Snordmark 	 * Insert in global list of callbacks. Makes future zone creations
626bd41d0a8Snordmark 	 * see it.
627bd41d0a8Snordmark 	 */
628bd41d0a8Snordmark 	mutex_enter(&zsd_key_lock);
629fe16170aSPramod Batni 	key = zsdp->zsd_key = ++zsd_keyval;
630bd41d0a8Snordmark 	ASSERT(zsd_keyval != 0);
631bd41d0a8Snordmark 	list_insert_tail(&zsd_registered_keys, zsdp);
632bd41d0a8Snordmark 	mutex_exit(&zsd_key_lock);
633bd41d0a8Snordmark 
634bd41d0a8Snordmark 	/*
635bd41d0a8Snordmark 	 * Insert for all existing zones and mark them as needing
636bd41d0a8Snordmark 	 * a create callback.
637bd41d0a8Snordmark 	 */
638bd41d0a8Snordmark 	mutex_enter(&zonehash_lock);	/* stop the world */
639bd41d0a8Snordmark 	for (zone = list_head(&zone_active); zone != NULL;
640bd41d0a8Snordmark 	    zone = list_next(&zone_active, zone)) {
641bd41d0a8Snordmark 		zone_status_t status;
642bd41d0a8Snordmark 
643bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
644bd41d0a8Snordmark 
645bd41d0a8Snordmark 		/* Skip zones that are on the way down or not yet up */
646bd41d0a8Snordmark 		status = zone_status_get(zone);
647bd41d0a8Snordmark 		if (status >= ZONE_IS_DOWN ||
648bd41d0a8Snordmark 		    status == ZONE_IS_UNINITIALIZED) {
649bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
650bd41d0a8Snordmark 			continue;
651bd41d0a8Snordmark 		}
652bd41d0a8Snordmark 
653bd41d0a8Snordmark 		t = zsd_find_mru(&zone->zone_zsd, key);
654bd41d0a8Snordmark 		if (t != NULL) {
655bd41d0a8Snordmark 			/*
656bd41d0a8Snordmark 			 * A zsd_configure already inserted it after
657bd41d0a8Snordmark 			 * we dropped zsd_key_lock above.
658bd41d0a8Snordmark 			 */
659bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
660bd41d0a8Snordmark 			continue;
661bd41d0a8Snordmark 		}
662bd41d0a8Snordmark 		t = kmem_zalloc(sizeof (*t), KM_SLEEP);
663bd41d0a8Snordmark 		t->zsd_key = key;
664bd41d0a8Snordmark 		t->zsd_create = create;
665bd41d0a8Snordmark 		t->zsd_shutdown = shutdown;
666bd41d0a8Snordmark 		t->zsd_destroy = destroy;
667bd41d0a8Snordmark 		if (create != NULL) {
668bd41d0a8Snordmark 			t->zsd_flags = ZSD_CREATE_NEEDED;
669bd41d0a8Snordmark 			DTRACE_PROBE2(zsd__create__needed,
670bd41d0a8Snordmark 			    zone_t *, zone, zone_key_t, key);
671bd41d0a8Snordmark 		}
672bd41d0a8Snordmark 		list_insert_tail(&zone->zone_zsd, t);
673bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
674bd41d0a8Snordmark 	}
675bd41d0a8Snordmark 	mutex_exit(&zonehash_lock);
676bd41d0a8Snordmark 
677bd41d0a8Snordmark 	if (create != NULL) {
678bd41d0a8Snordmark 		/* Now call the create callback for this key */
679bd41d0a8Snordmark 		zsd_apply_all_zones(zsd_apply_create, key);
680bd41d0a8Snordmark 	}
681fe16170aSPramod Batni 	/*
682835ee219SRobert Harris 	 * It is safe for consumers to use the key now, make it
683835ee219SRobert Harris 	 * globally visible. Specifically zone_getspecific() will
684835ee219SRobert Harris 	 * always successfully return the zone specific data associated
685835ee219SRobert Harris 	 * with the key.
686835ee219SRobert Harris 	 */
687fe16170aSPramod Batni 	*keyp = key;
688fe16170aSPramod Batni 
689bd41d0a8Snordmark }
690bd41d0a8Snordmark 
6917c478bd9Sstevel@tonic-gate /*
6927c478bd9Sstevel@tonic-gate  * Function called when a module is being unloaded, or otherwise wishes
6937c478bd9Sstevel@tonic-gate  * to unregister its ZSD key and callbacks.
694bd41d0a8Snordmark  *
695bd41d0a8Snordmark  * Remove from the global list and determine the functions that need to
696bd41d0a8Snordmark  * be called under a global lock. Then call the functions without
697bd41d0a8Snordmark  * holding any locks. Finally free up the zone_zsd entries. (The apply
698bd41d0a8Snordmark  * functions need to access the zone_zsd entries to find zsd_data etc.)
6997c478bd9Sstevel@tonic-gate  */
7007c478bd9Sstevel@tonic-gate int
zone_key_delete(zone_key_t key)7017c478bd9Sstevel@tonic-gate zone_key_delete(zone_key_t key)
7027c478bd9Sstevel@tonic-gate {
7037c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsdp = NULL;
7047c478bd9Sstevel@tonic-gate 	zone_t *zone;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
707bd41d0a8Snordmark 	zsdp = zsd_find_mru(&zsd_registered_keys, key);
708bd41d0a8Snordmark 	if (zsdp == NULL) {
709bd41d0a8Snordmark 		mutex_exit(&zsd_key_lock);
710bd41d0a8Snordmark 		return (-1);
711bd41d0a8Snordmark 	}
7127c478bd9Sstevel@tonic-gate 	list_remove(&zsd_registered_keys, zsdp);
7137c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
7147c478bd9Sstevel@tonic-gate 
715bd41d0a8Snordmark 	mutex_enter(&zonehash_lock);
7167c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
7177c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
7187c478bd9Sstevel@tonic-gate 		struct zsd_entry *del;
719bd41d0a8Snordmark 
720bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
721bd41d0a8Snordmark 		del = zsd_find_mru(&zone->zone_zsd, key);
722bd41d0a8Snordmark 		if (del == NULL) {
723bd41d0a8Snordmark 			/*
724bd41d0a8Snordmark 			 * Somebody else got here first e.g the zone going
725bd41d0a8Snordmark 			 * away.
726bd41d0a8Snordmark 			 */
727bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
728bd41d0a8Snordmark 			continue;
729bd41d0a8Snordmark 		}
730bd41d0a8Snordmark 		ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown);
731bd41d0a8Snordmark 		ASSERT(del->zsd_destroy == zsdp->zsd_destroy);
732bd41d0a8Snordmark 		if (del->zsd_shutdown != NULL &&
733bd41d0a8Snordmark 		    (del->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) {
734bd41d0a8Snordmark 			del->zsd_flags |= ZSD_SHUTDOWN_NEEDED;
735bd41d0a8Snordmark 			DTRACE_PROBE2(zsd__shutdown__needed,
736bd41d0a8Snordmark 			    zone_t *, zone, zone_key_t, key);
737bd41d0a8Snordmark 		}
738bd41d0a8Snordmark 		if (del->zsd_destroy != NULL &&
739bd41d0a8Snordmark 		    (del->zsd_flags & ZSD_DESTROY_ALL) == 0) {
740bd41d0a8Snordmark 			del->zsd_flags |= ZSD_DESTROY_NEEDED;
741bd41d0a8Snordmark 			DTRACE_PROBE2(zsd__destroy__needed,
742bd41d0a8Snordmark 			    zone_t *, zone, zone_key_t, key);
7437c478bd9Sstevel@tonic-gate 		}
7447c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
7477c478bd9Sstevel@tonic-gate 	kmem_free(zsdp, sizeof (*zsdp));
7487c478bd9Sstevel@tonic-gate 
749bd41d0a8Snordmark 	/* Now call the shutdown and destroy callback for this key */
750bd41d0a8Snordmark 	zsd_apply_all_zones(zsd_apply_shutdown, key);
751bd41d0a8Snordmark 	zsd_apply_all_zones(zsd_apply_destroy, key);
752bd41d0a8Snordmark 
753bd41d0a8Snordmark 	/* Now we can free up the zsdp structures in each zone */
754bd41d0a8Snordmark 	mutex_enter(&zonehash_lock);
7557c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
756bd41d0a8Snordmark 	    zone = list_next(&zone_active, zone)) {
757bd41d0a8Snordmark 		struct zsd_entry *del;
758bd41d0a8Snordmark 
759bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
760bd41d0a8Snordmark 		del = zsd_find(&zone->zone_zsd, key);
761bd41d0a8Snordmark 		if (del != NULL) {
762bd41d0a8Snordmark 			list_remove(&zone->zone_zsd, del);
763bd41d0a8Snordmark 			ASSERT(!(del->zsd_flags & ZSD_ALL_INPROGRESS));
764bd41d0a8Snordmark 			kmem_free(del, sizeof (*del));
765bd41d0a8Snordmark 		}
7667c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
767bd41d0a8Snordmark 	}
7687c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
769bd41d0a8Snordmark 
770bd41d0a8Snordmark 	return (0);
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate /*
7747c478bd9Sstevel@tonic-gate  * ZSD counterpart of pthread_setspecific().
775bd41d0a8Snordmark  *
776bd41d0a8Snordmark  * Since all zsd callbacks, including those with no create function,
777bd41d0a8Snordmark  * have an entry in zone_zsd, if the key is registered it is part of
778bd41d0a8Snordmark  * the zone_zsd list.
779bd41d0a8Snordmark  * Return an error if the key wasn't registerd.
7807c478bd9Sstevel@tonic-gate  */
7817c478bd9Sstevel@tonic-gate int
zone_setspecific(zone_key_t key,zone_t * zone,const void * data)7827c478bd9Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data)
7837c478bd9Sstevel@tonic-gate {
7847c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
787bd41d0a8Snordmark 	t = zsd_find_mru(&zone->zone_zsd, key);
7887c478bd9Sstevel@tonic-gate 	if (t != NULL) {
7897c478bd9Sstevel@tonic-gate 		/*
7907c478bd9Sstevel@tonic-gate 		 * Replace old value with new
7917c478bd9Sstevel@tonic-gate 		 */
7927c478bd9Sstevel@tonic-gate 		t->zsd_data = (void *)data;
7937c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
7947c478bd9Sstevel@tonic-gate 		return (0);
7957c478bd9Sstevel@tonic-gate 	}
7967c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
797bd41d0a8Snordmark 	return (-1);
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate /*
8017c478bd9Sstevel@tonic-gate  * ZSD counterpart of pthread_getspecific().
8027c478bd9Sstevel@tonic-gate  */
8037c478bd9Sstevel@tonic-gate void *
zone_getspecific(zone_key_t key,zone_t * zone)8047c478bd9Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone)
8057c478bd9Sstevel@tonic-gate {
8067c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
8077c478bd9Sstevel@tonic-gate 	void *data;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
810bd41d0a8Snordmark 	t = zsd_find_mru(&zone->zone_zsd, key);
8117c478bd9Sstevel@tonic-gate 	data = (t == NULL ? NULL : t->zsd_data);
8127c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
8137c478bd9Sstevel@tonic-gate 	return (data);
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate /*
8177c478bd9Sstevel@tonic-gate  * Function used to initialize a zone's list of ZSD callbacks and data
8187c478bd9Sstevel@tonic-gate  * when the zone is being created.  The callbacks are initialized from
819bd41d0a8Snordmark  * the template list (zsd_registered_keys). The constructor callback is
820bd41d0a8Snordmark  * executed later (once the zone exists and with locks dropped).
8217c478bd9Sstevel@tonic-gate  */
8227c478bd9Sstevel@tonic-gate static void
zone_zsd_configure(zone_t * zone)8237c478bd9Sstevel@tonic-gate zone_zsd_configure(zone_t *zone)
8247c478bd9Sstevel@tonic-gate {
8257c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsdp;
8267c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
8297c478bd9Sstevel@tonic-gate 	ASSERT(list_head(&zone->zone_zsd) == NULL);
830bd41d0a8Snordmark 	mutex_enter(&zone->zone_lock);
8317c478bd9Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
8327c478bd9Sstevel@tonic-gate 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
8337c478bd9Sstevel@tonic-gate 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
834bd41d0a8Snordmark 		/*
835bd41d0a8Snordmark 		 * Since this zone is ZONE_IS_UNCONFIGURED, zone_key_create
836bd41d0a8Snordmark 		 * should not have added anything to it.
837bd41d0a8Snordmark 		 */
838bd41d0a8Snordmark 		ASSERT(zsd_find(&zone->zone_zsd, zsdp->zsd_key) == NULL);
839bd41d0a8Snordmark 
840bd41d0a8Snordmark 		t = kmem_zalloc(sizeof (*t), KM_SLEEP);
841bd41d0a8Snordmark 		t->zsd_key = zsdp->zsd_key;
842bd41d0a8Snordmark 		t->zsd_create = zsdp->zsd_create;
843bd41d0a8Snordmark 		t->zsd_shutdown = zsdp->zsd_shutdown;
844bd41d0a8Snordmark 		t->zsd_destroy = zsdp->zsd_destroy;
8457c478bd9Sstevel@tonic-gate 		if (zsdp->zsd_create != NULL) {
846bd41d0a8Snordmark 			t->zsd_flags = ZSD_CREATE_NEEDED;
847bd41d0a8Snordmark 			DTRACE_PROBE2(zsd__create__needed,
848bd41d0a8Snordmark 			    zone_t *, zone, zone_key_t, zsdp->zsd_key);
8497c478bd9Sstevel@tonic-gate 		}
850bd41d0a8Snordmark 		list_insert_tail(&zone->zone_zsd, t);
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
853bd41d0a8Snordmark 	mutex_exit(&zone->zone_lock);
8547c478bd9Sstevel@tonic-gate }
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY };
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate /*
8597c478bd9Sstevel@tonic-gate  * Helper function to execute shutdown or destructor callbacks.
8607c478bd9Sstevel@tonic-gate  */
8617c478bd9Sstevel@tonic-gate static void
zone_zsd_callbacks(zone_t * zone,enum zsd_callback_type ct)8627c478bd9Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct)
8637c478bd9Sstevel@tonic-gate {
8647c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY);
8677c478bd9Sstevel@tonic-gate 	ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY);
8687c478bd9Sstevel@tonic-gate 	ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN);
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	/*
871bd41d0a8Snordmark 	 * Run the callback solely based on what is registered for the zone
872bd41d0a8Snordmark 	 * in zone_zsd. The global list can change independently of this
873bd41d0a8Snordmark 	 * as keys are registered and unregistered and we don't register new
874bd41d0a8Snordmark 	 * callbacks for a zone that is in the process of going away.
8757c478bd9Sstevel@tonic-gate 	 */
876bd41d0a8Snordmark 	mutex_enter(&zone->zone_lock);
877bd41d0a8Snordmark 	for (t = list_head(&zone->zone_zsd); t != NULL;
878bd41d0a8Snordmark 	    t = list_next(&zone->zone_zsd, t)) {
879bd41d0a8Snordmark 		zone_key_t key = t->zsd_key;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 		/* Skip if no callbacks registered */
882bd41d0a8Snordmark 
883bd41d0a8Snordmark 		if (ct == ZSD_SHUTDOWN) {
884bd41d0a8Snordmark 			if (t->zsd_shutdown != NULL &&
885bd41d0a8Snordmark 			    (t->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) {
886bd41d0a8Snordmark 				t->zsd_flags |= ZSD_SHUTDOWN_NEEDED;
887bd41d0a8Snordmark 				DTRACE_PROBE2(zsd__shutdown__needed,
888bd41d0a8Snordmark 				    zone_t *, zone, zone_key_t, key);
8897c478bd9Sstevel@tonic-gate 			}
8907c478bd9Sstevel@tonic-gate 		} else {
891bd41d0a8Snordmark 			if (t->zsd_destroy != NULL &&
892bd41d0a8Snordmark 			    (t->zsd_flags & ZSD_DESTROY_ALL) == 0) {
893bd41d0a8Snordmark 				t->zsd_flags |= ZSD_DESTROY_NEEDED;
894bd41d0a8Snordmark 				DTRACE_PROBE2(zsd__destroy__needed,
895bd41d0a8Snordmark 				    zone_t *, zone, zone_key_t, key);
8967c478bd9Sstevel@tonic-gate 			}
8977c478bd9Sstevel@tonic-gate 		}
8987c478bd9Sstevel@tonic-gate 	}
899bd41d0a8Snordmark 	mutex_exit(&zone->zone_lock);
900bd41d0a8Snordmark 
901bd41d0a8Snordmark 	/* Now call the shutdown and destroy callback for this key */
902bd41d0a8Snordmark 	zsd_apply_all_keys(zsd_apply_shutdown, zone);
903bd41d0a8Snordmark 	zsd_apply_all_keys(zsd_apply_destroy, zone);
904bd41d0a8Snordmark 
9057c478bd9Sstevel@tonic-gate }
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate /*
9087c478bd9Sstevel@tonic-gate  * Called when the zone is going away; free ZSD-related memory, and
9097c478bd9Sstevel@tonic-gate  * destroy the zone_zsd list.
9107c478bd9Sstevel@tonic-gate  */
9117c478bd9Sstevel@tonic-gate static void
zone_free_zsd(zone_t * zone)9127c478bd9Sstevel@tonic-gate zone_free_zsd(zone_t *zone)
9137c478bd9Sstevel@tonic-gate {
9147c478bd9Sstevel@tonic-gate 	struct zsd_entry *t, *next;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	/*
9177c478bd9Sstevel@tonic-gate 	 * Free all the zsd_entry's we had on this zone.
9187c478bd9Sstevel@tonic-gate 	 */
919bd41d0a8Snordmark 	mutex_enter(&zone->zone_lock);
9207c478bd9Sstevel@tonic-gate 	for (t = list_head(&zone->zone_zsd); t != NULL; t = next) {
9217c478bd9Sstevel@tonic-gate 		next = list_next(&zone->zone_zsd, t);
9227c478bd9Sstevel@tonic-gate 		list_remove(&zone->zone_zsd, t);
923bd41d0a8Snordmark 		ASSERT(!(t->zsd_flags & ZSD_ALL_INPROGRESS));
9247c478bd9Sstevel@tonic-gate 		kmem_free(t, sizeof (*t));
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 	list_destroy(&zone->zone_zsd);
927bd41d0a8Snordmark 	mutex_exit(&zone->zone_lock);
928bd41d0a8Snordmark 
929bd41d0a8Snordmark }
930bd41d0a8Snordmark 
931bd41d0a8Snordmark /*
932bd41d0a8Snordmark  * Apply a function to all zones for particular key value.
933bd41d0a8Snordmark  *
934bd41d0a8Snordmark  * The applyfn has to drop zonehash_lock if it does some work, and
935bd41d0a8Snordmark  * then reacquire it before it returns.
936bd41d0a8Snordmark  * When the lock is dropped we don't follow list_next even
937bd41d0a8Snordmark  * if it is possible to do so without any hazards. This is
938bd41d0a8Snordmark  * because we want the design to allow for the list of zones
939bd41d0a8Snordmark  * to change in any arbitrary way during the time the
940bd41d0a8Snordmark  * lock was dropped.
941bd41d0a8Snordmark  *
942bd41d0a8Snordmark  * It is safe to restart the loop at list_head since the applyfn
943bd41d0a8Snordmark  * changes the zsd_flags as it does work, so a subsequent
944bd41d0a8Snordmark  * pass through will have no effect in applyfn, hence the loop will terminate
945bd41d0a8Snordmark  * in at worst O(N^2).
946bd41d0a8Snordmark  */
947bd41d0a8Snordmark static void
zsd_apply_all_zones(zsd_applyfn_t * applyfn,zone_key_t key)948bd41d0a8Snordmark zsd_apply_all_zones(zsd_applyfn_t *applyfn, zone_key_t key)
949bd41d0a8Snordmark {
950bd41d0a8Snordmark 	zone_t *zone;
951bd41d0a8Snordmark 
952bd41d0a8Snordmark 	mutex_enter(&zonehash_lock);
953bd41d0a8Snordmark 	zone = list_head(&zone_active);
954bd41d0a8Snordmark 	while (zone != NULL) {
955bd41d0a8Snordmark 		if ((applyfn)(&zonehash_lock, B_FALSE, zone, key)) {
956bd41d0a8Snordmark 			/* Lock dropped - restart at head */
957bd41d0a8Snordmark 			zone = list_head(&zone_active);
958bd41d0a8Snordmark 		} else {
959bd41d0a8Snordmark 			zone = list_next(&zone_active, zone);
960bd41d0a8Snordmark 		}
961bd41d0a8Snordmark 	}
962bd41d0a8Snordmark 	mutex_exit(&zonehash_lock);
963bd41d0a8Snordmark }
964bd41d0a8Snordmark 
965bd41d0a8Snordmark /*
966bd41d0a8Snordmark  * Apply a function to all keys for a particular zone.
967bd41d0a8Snordmark  *
968bd41d0a8Snordmark  * The applyfn has to drop zonehash_lock if it does some work, and
969bd41d0a8Snordmark  * then reacquire it before it returns.
970bd41d0a8Snordmark  * When the lock is dropped we don't follow list_next even
971bd41d0a8Snordmark  * if it is possible to do so without any hazards. This is
972bd41d0a8Snordmark  * because we want the design to allow for the list of zsd callbacks
973bd41d0a8Snordmark  * to change in any arbitrary way during the time the
974bd41d0a8Snordmark  * lock was dropped.
975bd41d0a8Snordmark  *
976bd41d0a8Snordmark  * It is safe to restart the loop at list_head since the applyfn
977bd41d0a8Snordmark  * changes the zsd_flags as it does work, so a subsequent
978bd41d0a8Snordmark  * pass through will have no effect in applyfn, hence the loop will terminate
979bd41d0a8Snordmark  * in at worst O(N^2).
980bd41d0a8Snordmark  */
981bd41d0a8Snordmark static void
zsd_apply_all_keys(zsd_applyfn_t * applyfn,zone_t * zone)982bd41d0a8Snordmark zsd_apply_all_keys(zsd_applyfn_t *applyfn, zone_t *zone)
983bd41d0a8Snordmark {
984bd41d0a8Snordmark 	struct zsd_entry *t;
985bd41d0a8Snordmark 
986bd41d0a8Snordmark 	mutex_enter(&zone->zone_lock);
987bd41d0a8Snordmark 	t = list_head(&zone->zone_zsd);
988bd41d0a8Snordmark 	while (t != NULL) {
989bd41d0a8Snordmark 		if ((applyfn)(NULL, B_TRUE, zone, t->zsd_key)) {
990bd41d0a8Snordmark 			/* Lock dropped - restart at head */
991bd41d0a8Snordmark 			t = list_head(&zone->zone_zsd);
992bd41d0a8Snordmark 		} else {
993bd41d0a8Snordmark 			t = list_next(&zone->zone_zsd, t);
994bd41d0a8Snordmark 		}
995bd41d0a8Snordmark 	}
996bd41d0a8Snordmark 	mutex_exit(&zone->zone_lock);
997bd41d0a8Snordmark }
998bd41d0a8Snordmark 
999bd41d0a8Snordmark /*
1000bd41d0a8Snordmark  * Call the create function for the zone and key if CREATE_NEEDED
1001bd41d0a8Snordmark  * is set.
1002bd41d0a8Snordmark  * If some other thread gets here first and sets CREATE_INPROGRESS, then
1003bd41d0a8Snordmark  * we wait for that thread to complete so that we can ensure that
1004bd41d0a8Snordmark  * all the callbacks are done when we've looped over all zones/keys.
1005bd41d0a8Snordmark  *
1006bd41d0a8Snordmark  * When we call the create function, we drop the global held by the
1007bd41d0a8Snordmark  * caller, and return true to tell the caller it needs to re-evalute the
1008bd41d0a8Snordmark  * state.
1009bd41d0a8Snordmark  * If the caller holds zone_lock then zone_lock_held is set, and zone_lock
1010bd41d0a8Snordmark  * remains held on exit.
1011bd41d0a8Snordmark  */
1012bd41d0a8Snordmark static boolean_t
zsd_apply_create(kmutex_t * lockp,boolean_t zone_lock_held,zone_t * zone,zone_key_t key)1013bd41d0a8Snordmark zsd_apply_create(kmutex_t *lockp, boolean_t zone_lock_held,
1014bd41d0a8Snordmark     zone_t *zone, zone_key_t key)
1015bd41d0a8Snordmark {
1016bd41d0a8Snordmark 	void *result;
1017bd41d0a8Snordmark 	struct zsd_entry *t;
1018bd41d0a8Snordmark 	boolean_t dropped;
1019bd41d0a8Snordmark 
1020bd41d0a8Snordmark 	if (lockp != NULL) {
1021bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(lockp));
1022bd41d0a8Snordmark 	}
1023bd41d0a8Snordmark 	if (zone_lock_held) {
1024bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(&zone->zone_lock));
1025bd41d0a8Snordmark 	} else {
1026bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1027bd41d0a8Snordmark 	}
1028bd41d0a8Snordmark 
1029bd41d0a8Snordmark 	t = zsd_find(&zone->zone_zsd, key);
1030bd41d0a8Snordmark 	if (t == NULL) {
1031bd41d0a8Snordmark 		/*
1032bd41d0a8Snordmark 		 * Somebody else got here first e.g the zone going
1033bd41d0a8Snordmark 		 * away.
1034bd41d0a8Snordmark 		 */
1035bd41d0a8Snordmark 		if (!zone_lock_held)
1036bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
1037bd41d0a8Snordmark 		return (B_FALSE);
1038bd41d0a8Snordmark 	}
1039bd41d0a8Snordmark 	dropped = B_FALSE;
1040bd41d0a8Snordmark 	if (zsd_wait_for_inprogress(zone, t, lockp))
1041bd41d0a8Snordmark 		dropped = B_TRUE;
1042bd41d0a8Snordmark 
1043bd41d0a8Snordmark 	if (t->zsd_flags & ZSD_CREATE_NEEDED) {
1044bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_CREATE_NEEDED;
1045bd41d0a8Snordmark 		t->zsd_flags |= ZSD_CREATE_INPROGRESS;
1046bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__create__inprogress,
1047bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1048bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1049bd41d0a8Snordmark 		if (lockp != NULL)
1050bd41d0a8Snordmark 			mutex_exit(lockp);
1051bd41d0a8Snordmark 
1052bd41d0a8Snordmark 		dropped = B_TRUE;
1053bd41d0a8Snordmark 		ASSERT(t->zsd_create != NULL);
1054bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__create__start,
1055bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1056bd41d0a8Snordmark 
1057bd41d0a8Snordmark 		result = (*t->zsd_create)(zone->zone_id);
1058bd41d0a8Snordmark 
1059bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__create__end,
1060bd41d0a8Snordmark 		    zone_t *, zone, voidn *, result);
1061bd41d0a8Snordmark 
1062bd41d0a8Snordmark 		ASSERT(result != NULL);
1063bd41d0a8Snordmark 		if (lockp != NULL)
1064bd41d0a8Snordmark 			mutex_enter(lockp);
1065bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1066bd41d0a8Snordmark 		t->zsd_data = result;
1067bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_CREATE_INPROGRESS;
1068bd41d0a8Snordmark 		t->zsd_flags |= ZSD_CREATE_COMPLETED;
1069bd41d0a8Snordmark 		cv_broadcast(&t->zsd_cv);
1070bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__create__completed,
1071bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1072bd41d0a8Snordmark 	}
1073bd41d0a8Snordmark 	if (!zone_lock_held)
1074bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1075bd41d0a8Snordmark 	return (dropped);
1076bd41d0a8Snordmark }
1077bd41d0a8Snordmark 
1078bd41d0a8Snordmark /*
1079bd41d0a8Snordmark  * Call the shutdown function for the zone and key if SHUTDOWN_NEEDED
1080bd41d0a8Snordmark  * is set.
1081bd41d0a8Snordmark  * If some other thread gets here first and sets *_INPROGRESS, then
1082bd41d0a8Snordmark  * we wait for that thread to complete so that we can ensure that
1083bd41d0a8Snordmark  * all the callbacks are done when we've looped over all zones/keys.
1084bd41d0a8Snordmark  *
1085bd41d0a8Snordmark  * When we call the shutdown function, we drop the global held by the
1086bd41d0a8Snordmark  * caller, and return true to tell the caller it needs to re-evalute the
1087bd41d0a8Snordmark  * state.
1088bd41d0a8Snordmark  * If the caller holds zone_lock then zone_lock_held is set, and zone_lock
1089bd41d0a8Snordmark  * remains held on exit.
1090bd41d0a8Snordmark  */
1091bd41d0a8Snordmark static boolean_t
zsd_apply_shutdown(kmutex_t * lockp,boolean_t zone_lock_held,zone_t * zone,zone_key_t key)1092bd41d0a8Snordmark zsd_apply_shutdown(kmutex_t *lockp, boolean_t zone_lock_held,
1093bd41d0a8Snordmark     zone_t *zone, zone_key_t key)
1094bd41d0a8Snordmark {
1095bd41d0a8Snordmark 	struct zsd_entry *t;
1096bd41d0a8Snordmark 	void *data;
1097bd41d0a8Snordmark 	boolean_t dropped;
1098bd41d0a8Snordmark 
1099bd41d0a8Snordmark 	if (lockp != NULL) {
1100bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(lockp));
1101bd41d0a8Snordmark 	}
1102bd41d0a8Snordmark 	if (zone_lock_held) {
1103bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(&zone->zone_lock));
1104bd41d0a8Snordmark 	} else {
1105bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1106bd41d0a8Snordmark 	}
1107bd41d0a8Snordmark 
1108bd41d0a8Snordmark 	t = zsd_find(&zone->zone_zsd, key);
1109bd41d0a8Snordmark 	if (t == NULL) {
1110bd41d0a8Snordmark 		/*
1111bd41d0a8Snordmark 		 * Somebody else got here first e.g the zone going
1112bd41d0a8Snordmark 		 * away.
1113bd41d0a8Snordmark 		 */
1114bd41d0a8Snordmark 		if (!zone_lock_held)
1115bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
1116bd41d0a8Snordmark 		return (B_FALSE);
1117bd41d0a8Snordmark 	}
1118bd41d0a8Snordmark 	dropped = B_FALSE;
1119bd41d0a8Snordmark 	if (zsd_wait_for_creator(zone, t, lockp))
1120bd41d0a8Snordmark 		dropped = B_TRUE;
1121bd41d0a8Snordmark 
1122bd41d0a8Snordmark 	if (zsd_wait_for_inprogress(zone, t, lockp))
1123bd41d0a8Snordmark 		dropped = B_TRUE;
1124bd41d0a8Snordmark 
1125bd41d0a8Snordmark 	if (t->zsd_flags & ZSD_SHUTDOWN_NEEDED) {
1126bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_SHUTDOWN_NEEDED;
1127bd41d0a8Snordmark 		t->zsd_flags |= ZSD_SHUTDOWN_INPROGRESS;
1128bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__shutdown__inprogress,
1129bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1130bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1131bd41d0a8Snordmark 		if (lockp != NULL)
1132bd41d0a8Snordmark 			mutex_exit(lockp);
1133bd41d0a8Snordmark 		dropped = B_TRUE;
1134bd41d0a8Snordmark 
1135bd41d0a8Snordmark 		ASSERT(t->zsd_shutdown != NULL);
1136bd41d0a8Snordmark 		data = t->zsd_data;
1137bd41d0a8Snordmark 
1138bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__shutdown__start,
1139bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1140bd41d0a8Snordmark 
1141bd41d0a8Snordmark 		(t->zsd_shutdown)(zone->zone_id, data);
1142bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__shutdown__end,
1143bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1144bd41d0a8Snordmark 
1145bd41d0a8Snordmark 		if (lockp != NULL)
1146bd41d0a8Snordmark 			mutex_enter(lockp);
1147bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1148bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_SHUTDOWN_INPROGRESS;
1149bd41d0a8Snordmark 		t->zsd_flags |= ZSD_SHUTDOWN_COMPLETED;
1150bd41d0a8Snordmark 		cv_broadcast(&t->zsd_cv);
1151bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__shutdown__completed,
1152bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1153bd41d0a8Snordmark 	}
1154bd41d0a8Snordmark 	if (!zone_lock_held)
1155bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1156bd41d0a8Snordmark 	return (dropped);
1157bd41d0a8Snordmark }
1158bd41d0a8Snordmark 
1159bd41d0a8Snordmark /*
1160bd41d0a8Snordmark  * Call the destroy function for the zone and key if DESTROY_NEEDED
1161bd41d0a8Snordmark  * is set.
1162bd41d0a8Snordmark  * If some other thread gets here first and sets *_INPROGRESS, then
1163bd41d0a8Snordmark  * we wait for that thread to complete so that we can ensure that
1164bd41d0a8Snordmark  * all the callbacks are done when we've looped over all zones/keys.
1165bd41d0a8Snordmark  *
1166bd41d0a8Snordmark  * When we call the destroy function, we drop the global held by the
1167bd41d0a8Snordmark  * caller, and return true to tell the caller it needs to re-evalute the
1168bd41d0a8Snordmark  * state.
1169bd41d0a8Snordmark  * If the caller holds zone_lock then zone_lock_held is set, and zone_lock
1170bd41d0a8Snordmark  * remains held on exit.
1171bd41d0a8Snordmark  */
1172bd41d0a8Snordmark static boolean_t
zsd_apply_destroy(kmutex_t * lockp,boolean_t zone_lock_held,zone_t * zone,zone_key_t key)1173bd41d0a8Snordmark zsd_apply_destroy(kmutex_t *lockp, boolean_t zone_lock_held,
1174bd41d0a8Snordmark     zone_t *zone, zone_key_t key)
1175bd41d0a8Snordmark {
1176bd41d0a8Snordmark 	struct zsd_entry *t;
1177bd41d0a8Snordmark 	void *data;
1178bd41d0a8Snordmark 	boolean_t dropped;
1179bd41d0a8Snordmark 
1180bd41d0a8Snordmark 	if (lockp != NULL) {
1181bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(lockp));
1182bd41d0a8Snordmark 	}
1183bd41d0a8Snordmark 	if (zone_lock_held) {
1184bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(&zone->zone_lock));
1185bd41d0a8Snordmark 	} else {
1186bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1187bd41d0a8Snordmark 	}
1188bd41d0a8Snordmark 
1189bd41d0a8Snordmark 	t = zsd_find(&zone->zone_zsd, key);
1190bd41d0a8Snordmark 	if (t == NULL) {
1191bd41d0a8Snordmark 		/*
1192bd41d0a8Snordmark 		 * Somebody else got here first e.g the zone going
1193bd41d0a8Snordmark 		 * away.
1194bd41d0a8Snordmark 		 */
1195bd41d0a8Snordmark 		if (!zone_lock_held)
1196bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
1197bd41d0a8Snordmark 		return (B_FALSE);
1198bd41d0a8Snordmark 	}
1199bd41d0a8Snordmark 	dropped = B_FALSE;
1200bd41d0a8Snordmark 	if (zsd_wait_for_creator(zone, t, lockp))
1201bd41d0a8Snordmark 		dropped = B_TRUE;
1202bd41d0a8Snordmark 
1203bd41d0a8Snordmark 	if (zsd_wait_for_inprogress(zone, t, lockp))
1204bd41d0a8Snordmark 		dropped = B_TRUE;
1205bd41d0a8Snordmark 
1206bd41d0a8Snordmark 	if (t->zsd_flags & ZSD_DESTROY_NEEDED) {
1207bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_DESTROY_NEEDED;
1208bd41d0a8Snordmark 		t->zsd_flags |= ZSD_DESTROY_INPROGRESS;
1209bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__destroy__inprogress,
1210bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1211bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1212bd41d0a8Snordmark 		if (lockp != NULL)
1213bd41d0a8Snordmark 			mutex_exit(lockp);
1214bd41d0a8Snordmark 		dropped = B_TRUE;
1215bd41d0a8Snordmark 
1216bd41d0a8Snordmark 		ASSERT(t->zsd_destroy != NULL);
1217bd41d0a8Snordmark 		data = t->zsd_data;
1218bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__destroy__start,
1219bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1220bd41d0a8Snordmark 
1221bd41d0a8Snordmark 		(t->zsd_destroy)(zone->zone_id, data);
1222bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__destroy__end,
1223bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1224bd41d0a8Snordmark 
1225bd41d0a8Snordmark 		if (lockp != NULL)
1226bd41d0a8Snordmark 			mutex_enter(lockp);
1227bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1228bd41d0a8Snordmark 		t->zsd_data = NULL;
1229bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_DESTROY_INPROGRESS;
1230bd41d0a8Snordmark 		t->zsd_flags |= ZSD_DESTROY_COMPLETED;
1231bd41d0a8Snordmark 		cv_broadcast(&t->zsd_cv);
1232bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__destroy__completed,
1233bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1234bd41d0a8Snordmark 	}
1235bd41d0a8Snordmark 	if (!zone_lock_held)
1236bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1237bd41d0a8Snordmark 	return (dropped);
1238bd41d0a8Snordmark }
1239bd41d0a8Snordmark 
1240bd41d0a8Snordmark /*
1241bd41d0a8Snordmark  * Wait for any CREATE_NEEDED flag to be cleared.
1242bd41d0a8Snordmark  * Returns true if lockp was temporarily dropped while waiting.
1243bd41d0a8Snordmark  */
1244bd41d0a8Snordmark static boolean_t
zsd_wait_for_creator(zone_t * zone,struct zsd_entry * t,kmutex_t * lockp)1245bd41d0a8Snordmark zsd_wait_for_creator(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp)
1246bd41d0a8Snordmark {
1247bd41d0a8Snordmark 	boolean_t dropped = B_FALSE;
1248bd41d0a8Snordmark 
1249bd41d0a8Snordmark 	while (t->zsd_flags & ZSD_CREATE_NEEDED) {
1250bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__wait__for__creator,
1251bd41d0a8Snordmark 		    zone_t *, zone, struct zsd_entry *, t);
1252bd41d0a8Snordmark 		if (lockp != NULL) {
1253bd41d0a8Snordmark 			dropped = B_TRUE;
1254bd41d0a8Snordmark 			mutex_exit(lockp);
1255bd41d0a8Snordmark 		}
1256bd41d0a8Snordmark 		cv_wait(&t->zsd_cv, &zone->zone_lock);
1257bd41d0a8Snordmark 		if (lockp != NULL) {
1258bd41d0a8Snordmark 			/* First drop zone_lock to preserve order */
1259bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
1260bd41d0a8Snordmark 			mutex_enter(lockp);
1261bd41d0a8Snordmark 			mutex_enter(&zone->zone_lock);
1262bd41d0a8Snordmark 		}
1263bd41d0a8Snordmark 	}
1264bd41d0a8Snordmark 	return (dropped);
1265bd41d0a8Snordmark }
1266bd41d0a8Snordmark 
1267bd41d0a8Snordmark /*
1268bd41d0a8Snordmark  * Wait for any INPROGRESS flag to be cleared.
1269bd41d0a8Snordmark  * Returns true if lockp was temporarily dropped while waiting.
1270bd41d0a8Snordmark  */
1271bd41d0a8Snordmark static boolean_t
zsd_wait_for_inprogress(zone_t * zone,struct zsd_entry * t,kmutex_t * lockp)1272bd41d0a8Snordmark zsd_wait_for_inprogress(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp)
1273bd41d0a8Snordmark {
1274bd41d0a8Snordmark 	boolean_t dropped = B_FALSE;
1275bd41d0a8Snordmark 
1276bd41d0a8Snordmark 	while (t->zsd_flags & ZSD_ALL_INPROGRESS) {
1277bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__wait__for__inprogress,
1278bd41d0a8Snordmark 		    zone_t *, zone, struct zsd_entry *, t);
1279bd41d0a8Snordmark 		if (lockp != NULL) {
1280bd41d0a8Snordmark 			dropped = B_TRUE;
1281bd41d0a8Snordmark 			mutex_exit(lockp);
1282bd41d0a8Snordmark 		}
1283bd41d0a8Snordmark 		cv_wait(&t->zsd_cv, &zone->zone_lock);
1284bd41d0a8Snordmark 		if (lockp != NULL) {
1285bd41d0a8Snordmark 			/* First drop zone_lock to preserve order */
1286bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
1287bd41d0a8Snordmark 			mutex_enter(lockp);
1288bd41d0a8Snordmark 			mutex_enter(&zone->zone_lock);
1289bd41d0a8Snordmark 		}
1290bd41d0a8Snordmark 	}
1291bd41d0a8Snordmark 	return (dropped);
12927c478bd9Sstevel@tonic-gate }
12937c478bd9Sstevel@tonic-gate 
1294fa9e4066Sahrens /*
1295fa9e4066Sahrens  * Frees memory associated with the zone dataset list.
1296fa9e4066Sahrens  */
1297fa9e4066Sahrens static void
zone_free_datasets(zone_t * zone)1298fa9e4066Sahrens zone_free_datasets(zone_t *zone)
1299fa9e4066Sahrens {
1300fa9e4066Sahrens 	zone_dataset_t *t, *next;
1301fa9e4066Sahrens 
1302fa9e4066Sahrens 	for (t = list_head(&zone->zone_datasets); t != NULL; t = next) {
1303fa9e4066Sahrens 		next = list_next(&zone->zone_datasets, t);
1304fa9e4066Sahrens 		list_remove(&zone->zone_datasets, t);
1305fa9e4066Sahrens 		kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1);
1306fa9e4066Sahrens 		kmem_free(t, sizeof (*t));
1307fa9e4066Sahrens 	}
1308fa9e4066Sahrens 	list_destroy(&zone->zone_datasets);
1309fa9e4066Sahrens }
1310fa9e4066Sahrens 
13117c478bd9Sstevel@tonic-gate /*
13127c478bd9Sstevel@tonic-gate  * zone.cpu-shares resource control support.
13137c478bd9Sstevel@tonic-gate  */
13147c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13157c478bd9Sstevel@tonic-gate static rctl_qty_t
zone_cpu_shares_usage(rctl_t * rctl,struct proc * p)13167c478bd9Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p)
13177c478bd9Sstevel@tonic-gate {
13187c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
13197c478bd9Sstevel@tonic-gate 	return (p->p_zone->zone_shares);
13207c478bd9Sstevel@tonic-gate }
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13237c478bd9Sstevel@tonic-gate static int
zone_cpu_shares_set(rctl_t * rctl,struct proc * p,rctl_entity_p_t * e,rctl_qty_t nv)13247c478bd9Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
13257c478bd9Sstevel@tonic-gate     rctl_qty_t nv)
13267c478bd9Sstevel@tonic-gate {
13277c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
13287c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
13297c478bd9Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
13307c478bd9Sstevel@tonic-gate 		return (0);
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	e->rcep_p.zone->zone_shares = nv;
13337c478bd9Sstevel@tonic-gate 	return (0);
13347c478bd9Sstevel@tonic-gate }
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = {
13377c478bd9Sstevel@tonic-gate 	rcop_no_action,
13387c478bd9Sstevel@tonic-gate 	zone_cpu_shares_usage,
13397c478bd9Sstevel@tonic-gate 	zone_cpu_shares_set,
13407c478bd9Sstevel@tonic-gate 	rcop_no_test
13417c478bd9Sstevel@tonic-gate };
13427c478bd9Sstevel@tonic-gate 
1343c97ad5cdSakolb /*
1344c97ad5cdSakolb  * zone.cpu-cap resource control support.
1345c97ad5cdSakolb  */
1346c97ad5cdSakolb /*ARGSUSED*/
1347c97ad5cdSakolb static rctl_qty_t
zone_cpu_cap_get(rctl_t * rctl,struct proc * p)1348c97ad5cdSakolb zone_cpu_cap_get(rctl_t *rctl, struct proc *p)
1349c97ad5cdSakolb {
1350c97ad5cdSakolb 	ASSERT(MUTEX_HELD(&p->p_lock));
1351c97ad5cdSakolb 	return (cpucaps_zone_get(p->p_zone));
1352c97ad5cdSakolb }
1353c97ad5cdSakolb 
1354c97ad5cdSakolb /*ARGSUSED*/
1355c97ad5cdSakolb static int
zone_cpu_cap_set(rctl_t * rctl,struct proc * p,rctl_entity_p_t * e,rctl_qty_t nv)1356c97ad5cdSakolb zone_cpu_cap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
1357c97ad5cdSakolb     rctl_qty_t nv)
1358c97ad5cdSakolb {
1359c97ad5cdSakolb 	zone_t *zone = e->rcep_p.zone;
1360c97ad5cdSakolb 
1361c97ad5cdSakolb 	ASSERT(MUTEX_HELD(&p->p_lock));
1362c97ad5cdSakolb 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1363c97ad5cdSakolb 
1364c97ad5cdSakolb 	if (zone == NULL)
1365c97ad5cdSakolb 		return (0);
1366c97ad5cdSakolb 
1367c97ad5cdSakolb 	/*
1368c97ad5cdSakolb 	 * set cap to the new value.
1369c97ad5cdSakolb 	 */
1370c97ad5cdSakolb 	return (cpucaps_zone_set(zone, nv));
1371c97ad5cdSakolb }
1372c97ad5cdSakolb 
1373c97ad5cdSakolb static rctl_ops_t zone_cpu_cap_ops = {
1374c97ad5cdSakolb 	rcop_no_action,
1375c97ad5cdSakolb 	zone_cpu_cap_get,
1376c97ad5cdSakolb 	zone_cpu_cap_set,
1377c97ad5cdSakolb 	rcop_no_test
1378c97ad5cdSakolb };
1379c97ad5cdSakolb 
13807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13817c478bd9Sstevel@tonic-gate static rctl_qty_t
zone_lwps_usage(rctl_t * r,proc_t * p)13827c478bd9Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p)
13837c478bd9Sstevel@tonic-gate {
13847c478bd9Sstevel@tonic-gate 	rctl_qty_t nlwps;
13857c478bd9Sstevel@tonic-gate 	zone_t *zone = p->p_zone;
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
13907c478bd9Sstevel@tonic-gate 	nlwps = zone->zone_nlwps;
13917c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 	return (nlwps);
13947c478bd9Sstevel@tonic-gate }
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13977c478bd9Sstevel@tonic-gate static int
zone_lwps_test(rctl_t * r,proc_t * p,rctl_entity_p_t * e,rctl_val_t * rcntl,rctl_qty_t incr,uint_t flags)13987c478bd9Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
13997c478bd9Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
14007c478bd9Sstevel@tonic-gate {
14017c478bd9Sstevel@tonic-gate 	rctl_qty_t nlwps;
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
14047c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
14057c478bd9Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
14067c478bd9Sstevel@tonic-gate 		return (0);
14077c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock)));
14087c478bd9Sstevel@tonic-gate 	nlwps = e->rcep_p.zone->zone_nlwps;
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	if (nlwps + incr > rcntl->rcv_value)
14117c478bd9Sstevel@tonic-gate 		return (1);
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	return (0);
14147c478bd9Sstevel@tonic-gate }
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14177c478bd9Sstevel@tonic-gate static int
zone_lwps_set(rctl_t * rctl,struct proc * p,rctl_entity_p_t * e,rctl_qty_t nv)1418c6939658Ssl zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv)
1419c6939658Ssl {
14207c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
14217c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
14227c478bd9Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
14237c478bd9Sstevel@tonic-gate 		return (0);
14247c478bd9Sstevel@tonic-gate 	e->rcep_p.zone->zone_nlwps_ctl = nv;
14257c478bd9Sstevel@tonic-gate 	return (0);
14267c478bd9Sstevel@tonic-gate }
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = {
14297c478bd9Sstevel@tonic-gate 	rcop_no_action,
14307c478bd9Sstevel@tonic-gate 	zone_lwps_usage,
14317c478bd9Sstevel@tonic-gate 	zone_lwps_set,
14327c478bd9Sstevel@tonic-gate 	zone_lwps_test,
14337c478bd9Sstevel@tonic-gate };
14347c478bd9Sstevel@tonic-gate 
1435ff19e029SMenno Lageman /*ARGSUSED*/
1436ff19e029SMenno Lageman static rctl_qty_t
zone_procs_usage(rctl_t * r,proc_t * p)1437ff19e029SMenno Lageman zone_procs_usage(rctl_t *r, proc_t *p)
1438ff19e029SMenno Lageman {
1439ff19e029SMenno Lageman 	rctl_qty_t nprocs;
1440ff19e029SMenno Lageman 	zone_t *zone = p->p_zone;
1441ff19e029SMenno Lageman 
1442ff19e029SMenno Lageman 	ASSERT(MUTEX_HELD(&p->p_lock));
1443ff19e029SMenno Lageman 
1444ff19e029SMenno Lageman 	mutex_enter(&zone->zone_nlwps_lock);
1445ff19e029SMenno Lageman 	nprocs = zone->zone_nprocs;
1446ff19e029SMenno Lageman 	mutex_exit(&zone->zone_nlwps_lock);
1447ff19e029SMenno Lageman 
1448ff19e029SMenno Lageman 	return (nprocs);
1449ff19e029SMenno Lageman }
1450ff19e029SMenno Lageman 
1451ff19e029SMenno Lageman /*ARGSUSED*/
1452ff19e029SMenno Lageman static int
zone_procs_test(rctl_t * r,proc_t * p,rctl_entity_p_t * e,rctl_val_t * rcntl,rctl_qty_t incr,uint_t flags)1453ff19e029SMenno Lageman zone_procs_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
1454ff19e029SMenno Lageman     rctl_qty_t incr, uint_t flags)
1455ff19e029SMenno Lageman {
1456ff19e029SMenno Lageman 	rctl_qty_t nprocs;
1457ff19e029SMenno Lageman 
1458ff19e029SMenno Lageman 	ASSERT(MUTEX_HELD(&p->p_lock));
1459ff19e029SMenno Lageman 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1460ff19e029SMenno Lageman 	if (e->rcep_p.zone == NULL)
1461ff19e029SMenno Lageman 		return (0);
1462ff19e029SMenno Lageman 	ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock)));
1463ff19e029SMenno Lageman 	nprocs = e->rcep_p.zone->zone_nprocs;
1464ff19e029SMenno Lageman 
1465ff19e029SMenno Lageman 	if (nprocs + incr > rcntl->rcv_value)
1466ff19e029SMenno Lageman 		return (1);
1467ff19e029SMenno Lageman 
1468ff19e029SMenno Lageman 	return (0);
1469ff19e029SMenno Lageman }
1470ff19e029SMenno Lageman 
1471ff19e029SMenno Lageman /*ARGSUSED*/
1472ff19e029SMenno Lageman static int
zone_procs_set(rctl_t * rctl,struct proc * p,rctl_entity_p_t * e,rctl_qty_t nv)1473ff19e029SMenno Lageman zone_procs_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv)
1474ff19e029SMenno Lageman {
1475ff19e029SMenno Lageman 	ASSERT(MUTEX_HELD(&p->p_lock));
1476ff19e029SMenno Lageman 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1477ff19e029SMenno Lageman 	if (e->rcep_p.zone == NULL)
1478ff19e029SMenno Lageman 		return (0);
1479ff19e029SMenno Lageman 	e->rcep_p.zone->zone_nprocs_ctl = nv;
1480ff19e029SMenno Lageman 	return (0);
1481ff19e029SMenno Lageman }
1482ff19e029SMenno Lageman 
1483ff19e029SMenno Lageman static rctl_ops_t zone_procs_ops = {
1484ff19e029SMenno Lageman 	rcop_no_action,
1485ff19e029SMenno Lageman 	zone_procs_usage,
1486ff19e029SMenno Lageman 	zone_procs_set,
1487ff19e029SMenno Lageman 	zone_procs_test,
1488ff19e029SMenno Lageman };
1489ff19e029SMenno Lageman 
149008c359e5SJerry Jelinek /*ARGSUSED*/
149108c359e5SJerry Jelinek static rctl_qty_t
zone_shmmax_usage(rctl_t * rctl,struct proc * p)149208c359e5SJerry Jelinek zone_shmmax_usage(rctl_t *rctl, struct proc *p)
149308c359e5SJerry Jelinek {
149408c359e5SJerry Jelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
149508c359e5SJerry Jelinek 	return (p->p_zone->zone_shmmax);
149608c359e5SJerry Jelinek }
149708c359e5SJerry Jelinek 
1498824c205fSml /*ARGSUSED*/
1499824c205fSml static int
zone_shmmax_test(rctl_t * r,proc_t * p,rctl_entity_p_t * e,rctl_val_t * rval,rctl_qty_t incr,uint_t flags)1500824c205fSml zone_shmmax_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
1501824c205fSml     rctl_qty_t incr, uint_t flags)
1502824c205fSml {
1503824c205fSml 	rctl_qty_t v;
1504824c205fSml 	ASSERT(MUTEX_HELD(&p->p_lock));
1505824c205fSml 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1506824c205fSml 	v = e->rcep_p.zone->zone_shmmax + incr;
1507824c205fSml 	if (v > rval->rcv_value)
1508824c205fSml 		return (1);
1509824c205fSml 	return (0);
1510824c205fSml }
1511824c205fSml 
1512824c205fSml static rctl_ops_t zone_shmmax_ops = {
1513824c205fSml 	rcop_no_action,
151408c359e5SJerry Jelinek 	zone_shmmax_usage,
1515824c205fSml 	rcop_no_set,
1516824c205fSml 	zone_shmmax_test
1517824c205fSml };
1518824c205fSml 
151908c359e5SJerry Jelinek /*ARGSUSED*/
152008c359e5SJerry Jelinek static rctl_qty_t
zone_shmmni_usage(rctl_t * rctl,struct proc * p)152108c359e5SJerry Jelinek zone_shmmni_usage(rctl_t *rctl, struct proc *p)
152208c359e5SJerry Jelinek {
152308c359e5SJerry Jelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
152408c359e5SJerry Jelinek 	return (p->p_zone->zone_ipc.ipcq_shmmni);
152508c359e5SJerry Jelinek }
152608c359e5SJerry Jelinek 
1527824c205fSml /*ARGSUSED*/
1528824c205fSml static int
zone_shmmni_test(rctl_t * r,proc_t * p,rctl_entity_p_t * e,rctl_val_t * rval,rctl_qty_t incr,uint_t flags)1529824c205fSml zone_shmmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
1530824c205fSml     rctl_qty_t incr, uint_t flags)
1531824c205fSml {
1532824c205fSml 	rctl_qty_t v;
1533824c205fSml 	ASSERT(MUTEX_HELD(&p->p_lock));
1534824c205fSml 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1535824c205fSml 	v = e->rcep_p.zone->zone_ipc.ipcq_shmmni + incr;
1536824c205fSml 	if (v > rval->rcv_value)
1537824c205fSml 		return (1);
1538824c205fSml 	return (0);
1539824c205fSml }
1540824c205fSml 
1541824c205fSml static rctl_ops_t zone_shmmni_ops = {
1542824c205fSml 	rcop_no_action,
154308c359e5SJerry Jelinek 	zone_shmmni_usage,
1544824c205fSml 	rcop_no_set,
1545824c205fSml 	zone_shmmni_test
1546824c205fSml };
1547824c205fSml 
154808c359e5SJerry Jelinek /*ARGSUSED*/
154908c359e5SJerry Jelinek static rctl_qty_t
zone_semmni_usage(rctl_t * rctl,struct proc * p)155008c359e5SJerry Jelinek zone_semmni_usage(rctl_t *rctl, struct proc *p)
155108c359e5SJerry Jelinek {
155208c359e5SJerry Jelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
155308c359e5SJerry Jelinek 	return (p->p_zone->zone_ipc.ipcq_semmni);
155408c359e5SJerry Jelinek }
155508c359e5SJerry Jelinek 
1556824c205fSml /*ARGSUSED*/
1557824c205fSml static int
zone_semmni_test(rctl_t * r,proc_t * p,rctl_entity_p_t * e,rctl_val_t * rval,rctl_qty_t incr,uint_t flags)1558824c205fSml zone_semmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
1559824c205fSml     rctl_qty_t incr, uint_t flags)
1560824c205fSml {
1561824c205fSml 	rctl_qty_t v;
1562824c205fSml 	ASSERT(MUTEX_HELD(&p->p_lock));
1563824c205fSml 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1564824c205fSml 	v = e->rcep_p.zone->zone_ipc.ipcq_semmni + incr;
1565824c205fSml 	if (v > rval->rcv_value)
1566824c205fSml 		return (1);
1567824c205fSml 	return (0);
1568824c205fSml }
1569824c205fSml 
1570824c205fSml static rctl_ops_t zone_semmni_ops = {
1571824c205fSml 	rcop_no_action,
157208c359e5SJerry Jelinek 	zone_semmni_usage,
1573824c205fSml 	rcop_no_set,
1574824c205fSml 	zone_semmni_test
1575824c205fSml };
1576824c205fSml 
157708c359e5SJerry Jelinek /*ARGSUSED*/
157808c359e5SJerry Jelinek static rctl_qty_t
zone_msgmni_usage(rctl_t * rctl,struct proc * p)157908c359e5SJerry Jelinek zone_msgmni_usage(rctl_t *rctl, struct proc *p)
158008c359e5SJerry Jelinek {
158108c359e5SJerry Jelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
158208c359e5SJerry Jelinek 	return (p->p_zone->zone_ipc.ipcq_msgmni);
158308c359e5SJerry Jelinek }
158408c359e5SJerry Jelinek 
1585824c205fSml /*ARGSUSED*/
1586824c205fSml static int
zone_msgmni_test(rctl_t * r,proc_t * p,rctl_entity_p_t * e,rctl_val_t * rval,rctl_qty_t incr,uint_t flags)1587824c205fSml zone_msgmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
1588824c205fSml     rctl_qty_t incr, uint_t flags)
1589824c205fSml {
1590824c205fSml 	rctl_qty_t v;
1591824c205fSml 	ASSERT(MUTEX_HELD(&p->p_lock));
1592824c205fSml 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1593824c205fSml 	v = e->rcep_p.zone->zone_ipc.ipcq_msgmni + incr;
1594824c205fSml 	if (v > rval->rcv_value)
1595824c205fSml 		return (1);
1596824c205fSml 	return (0);
1597824c205fSml }
1598824c205fSml 
1599824c205fSml static rctl_ops_t zone_msgmni_ops = {
1600824c205fSml 	rcop_no_action,
160108c359e5SJerry Jelinek 	zone_msgmni_usage,
1602824c205fSml 	rcop_no_set,
1603824c205fSml 	zone_msgmni_test
1604824c205fSml };
1605824c205fSml 
1606c6939658Ssl /*ARGSUSED*/
1607c6939658Ssl static rctl_qty_t
zone_locked_mem_usage(rctl_t * rctl,struct proc * p)1608c6939658Ssl zone_locked_mem_usage(rctl_t *rctl, struct proc *p)
1609c6939658Ssl {
1610c6939658Ssl 	rctl_qty_t q;
1611c6939658Ssl 	ASSERT(MUTEX_HELD(&p->p_lock));
16120209230bSgjelinek 	mutex_enter(&p->p_zone->zone_mem_lock);
1613c6939658Ssl 	q = p->p_zone->zone_locked_mem;
16140209230bSgjelinek 	mutex_exit(&p->p_zone->zone_mem_lock);
1615c6939658Ssl 	return (q);
1616c6939658Ssl }
1617c6939658Ssl 
1618c6939658Ssl /*ARGSUSED*/
1619c6939658Ssl static int
zone_locked_mem_test(rctl_t * r,proc_t * p,rctl_entity_p_t * e,rctl_val_t * rcntl,rctl_qty_t incr,uint_t flags)1620c6939658Ssl zone_locked_mem_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
1621c6939658Ssl     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
1622c6939658Ssl {
1623c6939658Ssl 	rctl_qty_t q;
16240209230bSgjelinek 	zone_t *z;
16250209230bSgjelinek 
16260209230bSgjelinek 	z = e->rcep_p.zone;
1627c6939658Ssl 	ASSERT(MUTEX_HELD(&p->p_lock));
16280209230bSgjelinek 	ASSERT(MUTEX_HELD(&z->zone_mem_lock));
16290209230bSgjelinek 	q = z->zone_locked_mem;
1630c6939658Ssl 	if (q + incr > rcntl->rcv_value)
1631c6939658Ssl 		return (1);
1632c6939658Ssl 	return (0);
1633c6939658Ssl }
1634c6939658Ssl 
1635c6939658Ssl /*ARGSUSED*/
1636c6939658Ssl static int
zone_locked_mem_set(rctl_t * rctl,struct proc * p,rctl_entity_p_t * e,rctl_qty_t nv)1637c6939658Ssl zone_locked_mem_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
1638c6939658Ssl     rctl_qty_t nv)
1639c6939658Ssl {
1640c6939658Ssl 	ASSERT(MUTEX_HELD(&p->p_lock));
1641c6939658Ssl 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1642c6939658Ssl 	if (e->rcep_p.zone == NULL)
1643c6939658Ssl 		return (0);
1644c6939658Ssl 	e->rcep_p.zone->zone_locked_mem_ctl = nv;
1645c6939658Ssl 	return (0);
1646c6939658Ssl }
1647c6939658Ssl 
1648c6939658Ssl static rctl_ops_t zone_locked_mem_ops = {
1649c6939658Ssl 	rcop_no_action,
1650c6939658Ssl 	zone_locked_mem_usage,
1651c6939658Ssl 	zone_locked_mem_set,
1652c6939658Ssl 	zone_locked_mem_test
1653c6939658Ssl };
1654824c205fSml 
16550209230bSgjelinek /*ARGSUSED*/
16560209230bSgjelinek static rctl_qty_t
zone_max_swap_usage(rctl_t * rctl,struct proc * p)16570209230bSgjelinek zone_max_swap_usage(rctl_t *rctl, struct proc *p)
16580209230bSgjelinek {
16590209230bSgjelinek 	rctl_qty_t q;
16600209230bSgjelinek 	zone_t *z = p->p_zone;
16610209230bSgjelinek 
16620209230bSgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
16630209230bSgjelinek 	mutex_enter(&z->zone_mem_lock);
16640209230bSgjelinek 	q = z->zone_max_swap;
16650209230bSgjelinek 	mutex_exit(&z->zone_mem_lock);
16660209230bSgjelinek 	return (q);
16670209230bSgjelinek }
16680209230bSgjelinek 
16690209230bSgjelinek /*ARGSUSED*/
16700209230bSgjelinek static int
zone_max_swap_test(rctl_t * r,proc_t * p,rctl_entity_p_t * e,rctl_val_t * rcntl,rctl_qty_t incr,uint_t flags)16710209230bSgjelinek zone_max_swap_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
16720209230bSgjelinek     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
16730209230bSgjelinek {
16740209230bSgjelinek 	rctl_qty_t q;
16750209230bSgjelinek 	zone_t *z;
16760209230bSgjelinek 
16770209230bSgjelinek 	z = e->rcep_p.zone;
16780209230bSgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
16790209230bSgjelinek 	ASSERT(MUTEX_HELD(&z->zone_mem_lock));
16800209230bSgjelinek 	q = z->zone_max_swap;
16810209230bSgjelinek 	if (q + incr > rcntl->rcv_value)
16820209230bSgjelinek 		return (1);
16830209230bSgjelinek 	return (0);
16840209230bSgjelinek }
16850209230bSgjelinek 
16860209230bSgjelinek /*ARGSUSED*/
16870209230bSgjelinek static int
zone_max_swap_set(rctl_t * rctl,struct proc * p,rctl_entity_p_t * e,rctl_qty_t nv)16880209230bSgjelinek zone_max_swap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
16890209230bSgjelinek     rctl_qty_t nv)
16900209230bSgjelinek {
16910209230bSgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
16920209230bSgjelinek 	ASSERT(e->rcep_t == RCENTITY_ZONE);
16930209230bSgjelinek 	if (e->rcep_p.zone == NULL)
16940209230bSgjelinek 		return (0);
16950209230bSgjelinek 	e->rcep_p.zone->zone_max_swap_ctl = nv;
16960209230bSgjelinek 	return (0);
16970209230bSgjelinek }
16980209230bSgjelinek 
16990209230bSgjelinek static rctl_ops_t zone_max_swap_ops = {
17000209230bSgjelinek 	rcop_no_action,
17010209230bSgjelinek 	zone_max_swap_usage,
17020209230bSgjelinek 	zone_max_swap_set,
17030209230bSgjelinek 	zone_max_swap_test
17040209230bSgjelinek };
17050209230bSgjelinek 
17060fbb751dSJohn Levon /*ARGSUSED*/
17070fbb751dSJohn Levon static rctl_qty_t
zone_max_lofi_usage(rctl_t * rctl,struct proc * p)17080fbb751dSJohn Levon zone_max_lofi_usage(rctl_t *rctl, struct proc *p)
17090fbb751dSJohn Levon {
17100fbb751dSJohn Levon 	rctl_qty_t q;
17110fbb751dSJohn Levon 	zone_t *z = p->p_zone;
17120fbb751dSJohn Levon 
17130fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&p->p_lock));
17140fbb751dSJohn Levon 	mutex_enter(&z->zone_rctl_lock);
17150fbb751dSJohn Levon 	q = z->zone_max_lofi;
17160fbb751dSJohn Levon 	mutex_exit(&z->zone_rctl_lock);
17170fbb751dSJohn Levon 	return (q);
17180fbb751dSJohn Levon }
17190fbb751dSJohn Levon 
17200fbb751dSJohn Levon /*ARGSUSED*/
17210fbb751dSJohn Levon static int
zone_max_lofi_test(rctl_t * r,proc_t * p,rctl_entity_p_t * e,rctl_val_t * rcntl,rctl_qty_t incr,uint_t flags)17220fbb751dSJohn Levon zone_max_lofi_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
17230fbb751dSJohn Levon     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
17240fbb751dSJohn Levon {
17250fbb751dSJohn Levon 	rctl_qty_t q;
17260fbb751dSJohn Levon 	zone_t *z;
17270fbb751dSJohn Levon 
17280fbb751dSJohn Levon 	z = e->rcep_p.zone;
17290fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&p->p_lock));
17300fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&z->zone_rctl_lock));
17310fbb751dSJohn Levon 	q = z->zone_max_lofi;
17320fbb751dSJohn Levon 	if (q + incr > rcntl->rcv_value)
17330fbb751dSJohn Levon 		return (1);
17340fbb751dSJohn Levon 	return (0);
17350fbb751dSJohn Levon }
17360fbb751dSJohn Levon 
17370fbb751dSJohn Levon /*ARGSUSED*/
17380fbb751dSJohn Levon static int
zone_max_lofi_set(rctl_t * rctl,struct proc * p,rctl_entity_p_t * e,rctl_qty_t nv)17390fbb751dSJohn Levon zone_max_lofi_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
17400fbb751dSJohn Levon     rctl_qty_t nv)
17410fbb751dSJohn Levon {
17420fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&p->p_lock));
17430fbb751dSJohn Levon 	ASSERT(e->rcep_t == RCENTITY_ZONE);
17440fbb751dSJohn Levon 	if (e->rcep_p.zone == NULL)
17450fbb751dSJohn Levon 		return (0);
17460fbb751dSJohn Levon 	e->rcep_p.zone->zone_max_lofi_ctl = nv;
17470fbb751dSJohn Levon 	return (0);
17480fbb751dSJohn Levon }
17490fbb751dSJohn Levon 
17500fbb751dSJohn Levon static rctl_ops_t zone_max_lofi_ops = {
17510fbb751dSJohn Levon 	rcop_no_action,
17520fbb751dSJohn Levon 	zone_max_lofi_usage,
17530fbb751dSJohn Levon 	zone_max_lofi_set,
17540fbb751dSJohn Levon 	zone_max_lofi_test
17550fbb751dSJohn Levon };
17560fbb751dSJohn Levon 
17577c478bd9Sstevel@tonic-gate /*
17587c478bd9Sstevel@tonic-gate  * Helper function to brand the zone with a unique ID.
17597c478bd9Sstevel@tonic-gate  */
17607c478bd9Sstevel@tonic-gate static void
zone_uniqid(zone_t * zone)17617c478bd9Sstevel@tonic-gate zone_uniqid(zone_t *zone)
17627c478bd9Sstevel@tonic-gate {
17637c478bd9Sstevel@tonic-gate 	static uint64_t uniqid = 0;
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
17667c478bd9Sstevel@tonic-gate 	zone->zone_uniqid = uniqid++;
17677c478bd9Sstevel@tonic-gate }
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate /*
17707c478bd9Sstevel@tonic-gate  * Returns a held pointer to the "kcred" for the specified zone.
17717c478bd9Sstevel@tonic-gate  */
17727c478bd9Sstevel@tonic-gate struct cred *
zone_get_kcred(zoneid_t zoneid)17737c478bd9Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid)
17747c478bd9Sstevel@tonic-gate {
17757c478bd9Sstevel@tonic-gate 	zone_t *zone;
17767c478bd9Sstevel@tonic-gate 	cred_t *cr;
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_by_id(zoneid)) == NULL)
17797c478bd9Sstevel@tonic-gate 		return (NULL);
17807c478bd9Sstevel@tonic-gate 	cr = zone->zone_kcred;
17817c478bd9Sstevel@tonic-gate 	crhold(cr);
17827c478bd9Sstevel@tonic-gate 	zone_rele(zone);
17837c478bd9Sstevel@tonic-gate 	return (cr);
17847c478bd9Sstevel@tonic-gate }
17857c478bd9Sstevel@tonic-gate 
17860209230bSgjelinek static int
zone_lockedmem_kstat_update(kstat_t * ksp,int rw)17870209230bSgjelinek zone_lockedmem_kstat_update(kstat_t *ksp, int rw)
17880209230bSgjelinek {
17890209230bSgjelinek 	zone_t *zone = ksp->ks_private;
17900209230bSgjelinek 	zone_kstat_t *zk = ksp->ks_data;
17910209230bSgjelinek 
17920209230bSgjelinek 	if (rw == KSTAT_WRITE)
17930209230bSgjelinek 		return (EACCES);
17940209230bSgjelinek 
17950209230bSgjelinek 	zk->zk_usage.value.ui64 = zone->zone_locked_mem;
17960209230bSgjelinek 	zk->zk_value.value.ui64 = zone->zone_locked_mem_ctl;
17970209230bSgjelinek 	return (0);
17980209230bSgjelinek }
17990209230bSgjelinek 
1800ff19e029SMenno Lageman static int
zone_nprocs_kstat_update(kstat_t * ksp,int rw)1801ff19e029SMenno Lageman zone_nprocs_kstat_update(kstat_t *ksp, int rw)
1802ff19e029SMenno Lageman {
1803ff19e029SMenno Lageman 	zone_t *zone = ksp->ks_private;
1804ff19e029SMenno Lageman 	zone_kstat_t *zk = ksp->ks_data;
1805ff19e029SMenno Lageman 
1806ff19e029SMenno Lageman 	if (rw == KSTAT_WRITE)
1807ff19e029SMenno Lageman 		return (EACCES);
1808ff19e029SMenno Lageman 
1809ff19e029SMenno Lageman 	zk->zk_usage.value.ui64 = zone->zone_nprocs;
1810ff19e029SMenno Lageman 	zk->zk_value.value.ui64 = zone->zone_nprocs_ctl;
1811ff19e029SMenno Lageman 	return (0);
1812ff19e029SMenno Lageman }
1813ff19e029SMenno Lageman 
18140209230bSgjelinek static int
zone_swapresv_kstat_update(kstat_t * ksp,int rw)18150209230bSgjelinek zone_swapresv_kstat_update(kstat_t *ksp, int rw)
18160209230bSgjelinek {
18170209230bSgjelinek 	zone_t *zone = ksp->ks_private;
18180209230bSgjelinek 	zone_kstat_t *zk = ksp->ks_data;
18190209230bSgjelinek 
18200209230bSgjelinek 	if (rw == KSTAT_WRITE)
18210209230bSgjelinek 		return (EACCES);
18220209230bSgjelinek 
18230209230bSgjelinek 	zk->zk_usage.value.ui64 = zone->zone_max_swap;
18240209230bSgjelinek 	zk->zk_value.value.ui64 = zone->zone_max_swap_ctl;
18250209230bSgjelinek 	return (0);
18260209230bSgjelinek }
18270209230bSgjelinek 
1828ff19e029SMenno Lageman static kstat_t *
zone_kstat_create_common(zone_t * zone,char * name,int (* updatefunc)(kstat_t *,int))1829ff19e029SMenno Lageman zone_kstat_create_common(zone_t *zone, char *name,
1830ff19e029SMenno Lageman     int (*updatefunc) (kstat_t *, int))
18310209230bSgjelinek {
18320209230bSgjelinek 	kstat_t *ksp;
18330209230bSgjelinek 	zone_kstat_t *zk;
18340209230bSgjelinek 
1835ff19e029SMenno Lageman 	ksp = rctl_kstat_create_zone(zone, name, KSTAT_TYPE_NAMED,
18360209230bSgjelinek 	    sizeof (zone_kstat_t) / sizeof (kstat_named_t),
18370209230bSgjelinek 	    KSTAT_FLAG_VIRTUAL);
18380209230bSgjelinek 
18390209230bSgjelinek 	if (ksp == NULL)
1840ff19e029SMenno Lageman 		return (NULL);
18410209230bSgjelinek 
18420209230bSgjelinek 	zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP);
18430209230bSgjelinek 	ksp->ks_data_size += strlen(zone->zone_name) + 1;
18440209230bSgjelinek 	kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING);
18450209230bSgjelinek 	kstat_named_setstr(&zk->zk_zonename, zone->zone_name);
18460209230bSgjelinek 	kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64);
18470209230bSgjelinek 	kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64);
1848ff19e029SMenno Lageman 	ksp->ks_update = updatefunc;
18490209230bSgjelinek 	ksp->ks_private = zone;
18500209230bSgjelinek 	kstat_install(ksp);
1851ff19e029SMenno Lageman 	return (ksp);
1852ff19e029SMenno Lageman }
18530209230bSgjelinek 
18549468939eSJerry Jelinek 
18559468939eSJerry Jelinek static int
zone_mcap_kstat_update(kstat_t * ksp,int rw)18569468939eSJerry Jelinek zone_mcap_kstat_update(kstat_t *ksp, int rw)
18579468939eSJerry Jelinek {
18589468939eSJerry Jelinek 	zone_t *zone = ksp->ks_private;
18599468939eSJerry Jelinek 	zone_mcap_kstat_t *zmp = ksp->ks_data;
18609468939eSJerry Jelinek 
18619468939eSJerry Jelinek 	if (rw == KSTAT_WRITE)
18629468939eSJerry Jelinek 		return (EACCES);
18639468939eSJerry Jelinek 
18649468939eSJerry Jelinek 	zmp->zm_pgpgin.value.ui64 = zone->zone_pgpgin;
18659468939eSJerry Jelinek 	zmp->zm_anonpgin.value.ui64 = zone->zone_anonpgin;
18669468939eSJerry Jelinek 	zmp->zm_execpgin.value.ui64 = zone->zone_execpgin;
18679468939eSJerry Jelinek 	zmp->zm_fspgin.value.ui64 = zone->zone_fspgin;
1868d14b1d19SJerry Jelinek 	zmp->zm_anon_alloc_fail.value.ui64 = zone->zone_anon_alloc_fail;
18699468939eSJerry Jelinek 
18709468939eSJerry Jelinek 	return (0);
18719468939eSJerry Jelinek }
18729468939eSJerry Jelinek 
18739468939eSJerry Jelinek static kstat_t *
zone_mcap_kstat_create(zone_t * zone)18749468939eSJerry Jelinek zone_mcap_kstat_create(zone_t *zone)
18759468939eSJerry Jelinek {
18769468939eSJerry Jelinek 	kstat_t *ksp;
18779468939eSJerry Jelinek 	zone_mcap_kstat_t *zmp;
18789468939eSJerry Jelinek 
18799468939eSJerry Jelinek 	if ((ksp = kstat_create_zone("memory_cap", zone->zone_id,
18809468939eSJerry Jelinek 	    zone->zone_name, "zone_memory_cap", KSTAT_TYPE_NAMED,
18819468939eSJerry Jelinek 	    sizeof (zone_mcap_kstat_t) / sizeof (kstat_named_t),
18829468939eSJerry Jelinek 	    KSTAT_FLAG_VIRTUAL, zone->zone_id)) == NULL)
18839468939eSJerry Jelinek 		return (NULL);
18849468939eSJerry Jelinek 
18859468939eSJerry Jelinek 	if (zone->zone_id != GLOBAL_ZONEID)
18869468939eSJerry Jelinek 		kstat_zone_add(ksp, GLOBAL_ZONEID);
18879468939eSJerry Jelinek 
18889468939eSJerry Jelinek 	zmp = ksp->ks_data = kmem_zalloc(sizeof (zone_mcap_kstat_t), KM_SLEEP);
18899468939eSJerry Jelinek 	ksp->ks_data_size += strlen(zone->zone_name) + 1;
18909468939eSJerry Jelinek 	ksp->ks_lock = &zone->zone_mcap_lock;
18919468939eSJerry Jelinek 	zone->zone_mcap_stats = zmp;
18929468939eSJerry Jelinek 
18939468939eSJerry Jelinek 	/* The kstat "name" field is not large enough for a full zonename */
18949468939eSJerry Jelinek 	kstat_named_init(&zmp->zm_zonename, "zonename", KSTAT_DATA_STRING);
18959468939eSJerry Jelinek 	kstat_named_setstr(&zmp->zm_zonename, zone->zone_name);
18969468939eSJerry Jelinek 	kstat_named_init(&zmp->zm_pgpgin, "pgpgin", KSTAT_DATA_UINT64);
18979468939eSJerry Jelinek 	kstat_named_init(&zmp->zm_anonpgin, "anonpgin", KSTAT_DATA_UINT64);
18989468939eSJerry Jelinek 	kstat_named_init(&zmp->zm_execpgin, "execpgin", KSTAT_DATA_UINT64);
18999468939eSJerry Jelinek 	kstat_named_init(&zmp->zm_fspgin, "fspgin", KSTAT_DATA_UINT64);
1900d14b1d19SJerry Jelinek 	kstat_named_init(&zmp->zm_anon_alloc_fail, "anon_alloc_fail",
1901d14b1d19SJerry Jelinek 	    KSTAT_DATA_UINT64);
19029468939eSJerry Jelinek 
19039468939eSJerry Jelinek 	ksp->ks_update = zone_mcap_kstat_update;
19049468939eSJerry Jelinek 	ksp->ks_private = zone;
19059468939eSJerry Jelinek 
19069468939eSJerry Jelinek 	kstat_install(ksp);
19079468939eSJerry Jelinek 	return (ksp);
19089468939eSJerry Jelinek }
19099468939eSJerry Jelinek 
1910542a813cSJerry Jelinek static int
zone_misc_kstat_update(kstat_t * ksp,int rw)1911542a813cSJerry Jelinek zone_misc_kstat_update(kstat_t *ksp, int rw)
1912542a813cSJerry Jelinek {
1913542a813cSJerry Jelinek 	zone_t *zone = ksp->ks_private;
1914542a813cSJerry Jelinek 	zone_misc_kstat_t *zmp = ksp->ks_data;
19152918c4a3SJohn Levon 	hrtime_t hrtime;
19162918c4a3SJohn Levon 	uint64_t tmp;
1917542a813cSJerry Jelinek 
1918542a813cSJerry Jelinek 	if (rw == KSTAT_WRITE)
1919542a813cSJerry Jelinek 		return (EACCES);
1920542a813cSJerry Jelinek 
19212918c4a3SJohn Levon 	tmp = cpu_uarray_sum(zone->zone_ustate, ZONE_USTATE_STIME);
19222918c4a3SJohn Levon 	hrtime = UINT64_OVERFLOW_TO_INT64(tmp);
19232918c4a3SJohn Levon 	scalehrtime(&hrtime);
19242918c4a3SJohn Levon 	zmp->zm_stime.value.ui64 = hrtime;
19252918c4a3SJohn Levon 
19262918c4a3SJohn Levon 	tmp = cpu_uarray_sum(zone->zone_ustate, ZONE_USTATE_UTIME);
19272918c4a3SJohn Levon 	hrtime = UINT64_OVERFLOW_TO_INT64(tmp);
19282918c4a3SJohn Levon 	scalehrtime(&hrtime);
19292918c4a3SJohn Levon 	zmp->zm_utime.value.ui64 = hrtime;
19302918c4a3SJohn Levon 
19312918c4a3SJohn Levon 	tmp = cpu_uarray_sum(zone->zone_ustate, ZONE_USTATE_WTIME);
19322918c4a3SJohn Levon 	hrtime = UINT64_OVERFLOW_TO_INT64(tmp);
19332918c4a3SJohn Levon 	scalehrtime(&hrtime);
19342918c4a3SJohn Levon 	zmp->zm_wtime.value.ui64 = hrtime;
1935542a813cSJerry Jelinek 
193681d43577SJerry Jelinek 	zmp->zm_avenrun1.value.ui32 = zone->zone_avenrun[0];
193781d43577SJerry Jelinek 	zmp->zm_avenrun5.value.ui32 = zone->zone_avenrun[1];
193881d43577SJerry Jelinek 	zmp->zm_avenrun15.value.ui32 = zone->zone_avenrun[2];
193981d43577SJerry Jelinek 
19402dc692e0SJerry Jelinek 	zmp->zm_ffcap.value.ui32 = zone->zone_ffcap;
19412dc692e0SJerry Jelinek 	zmp->zm_ffnoproc.value.ui32 = zone->zone_ffnoproc;
19422dc692e0SJerry Jelinek 	zmp->zm_ffnomem.value.ui32 = zone->zone_ffnomem;
19432dc692e0SJerry Jelinek 	zmp->zm_ffmisc.value.ui32 = zone->zone_ffmisc;
19442dc692e0SJerry Jelinek 
194593cf283aSJerry Jelinek 	zmp->zm_nested_intp.value.ui32 = zone->zone_nested_intp;
194693cf283aSJerry Jelinek 
1947a48d8120SJerry Jelinek 	zmp->zm_init_pid.value.ui32 = zone->zone_proc_initpid;
1948a48d8120SJerry Jelinek 	zmp->zm_boot_time.value.ui64 = (uint64_t)zone->zone_boot_time;
1949a48d8120SJerry Jelinek 
1950542a813cSJerry Jelinek 	return (0);
1951542a813cSJerry Jelinek }
1952542a813cSJerry Jelinek 
1953542a813cSJerry Jelinek static kstat_t *
zone_misc_kstat_create(zone_t * zone)1954542a813cSJerry Jelinek zone_misc_kstat_create(zone_t *zone)
1955542a813cSJerry Jelinek {
1956542a813cSJerry Jelinek 	kstat_t *ksp;
1957542a813cSJerry Jelinek 	zone_misc_kstat_t *zmp;
1958542a813cSJerry Jelinek 
1959542a813cSJerry Jelinek 	if ((ksp = kstat_create_zone("zones", zone->zone_id,
1960542a813cSJerry Jelinek 	    zone->zone_name, "zone_misc", KSTAT_TYPE_NAMED,
1961542a813cSJerry Jelinek 	    sizeof (zone_misc_kstat_t) / sizeof (kstat_named_t),
1962542a813cSJerry Jelinek 	    KSTAT_FLAG_VIRTUAL, zone->zone_id)) == NULL)
1963542a813cSJerry Jelinek 		return (NULL);
1964542a813cSJerry Jelinek 
1965542a813cSJerry Jelinek 	if (zone->zone_id != GLOBAL_ZONEID)
1966542a813cSJerry Jelinek 		kstat_zone_add(ksp, GLOBAL_ZONEID);
1967542a813cSJerry Jelinek 
1968542a813cSJerry Jelinek 	zmp = ksp->ks_data = kmem_zalloc(sizeof (zone_misc_kstat_t), KM_SLEEP);
1969542a813cSJerry Jelinek 	ksp->ks_data_size += strlen(zone->zone_name) + 1;
1970542a813cSJerry Jelinek 	ksp->ks_lock = &zone->zone_misc_lock;
1971542a813cSJerry Jelinek 	zone->zone_misc_stats = zmp;
1972542a813cSJerry Jelinek 
1973542a813cSJerry Jelinek 	/* The kstat "name" field is not large enough for a full zonename */
1974542a813cSJerry Jelinek 	kstat_named_init(&zmp->zm_zonename, "zonename", KSTAT_DATA_STRING);
1975542a813cSJerry Jelinek 	kstat_named_setstr(&zmp->zm_zonename, zone->zone_name);
1976542a813cSJerry Jelinek 	kstat_named_init(&zmp->zm_utime, "nsec_user", KSTAT_DATA_UINT64);
1977542a813cSJerry Jelinek 	kstat_named_init(&zmp->zm_stime, "nsec_sys", KSTAT_DATA_UINT64);
1978542a813cSJerry Jelinek 	kstat_named_init(&zmp->zm_wtime, "nsec_waitrq", KSTAT_DATA_UINT64);
197981d43577SJerry Jelinek 	kstat_named_init(&zmp->zm_avenrun1, "avenrun_1min", KSTAT_DATA_UINT32);
198081d43577SJerry Jelinek 	kstat_named_init(&zmp->zm_avenrun5, "avenrun_5min", KSTAT_DATA_UINT32);
198181d43577SJerry Jelinek 	kstat_named_init(&zmp->zm_avenrun15, "avenrun_15min",
198281d43577SJerry Jelinek 	    KSTAT_DATA_UINT32);
19832dc692e0SJerry Jelinek 	kstat_named_init(&zmp->zm_ffcap, "forkfail_cap", KSTAT_DATA_UINT32);
19842dc692e0SJerry Jelinek 	kstat_named_init(&zmp->zm_ffnoproc, "forkfail_noproc",
19852dc692e0SJerry Jelinek 	    KSTAT_DATA_UINT32);
19862dc692e0SJerry Jelinek 	kstat_named_init(&zmp->zm_ffnomem, "forkfail_nomem", KSTAT_DATA_UINT32);
19872dc692e0SJerry Jelinek 	kstat_named_init(&zmp->zm_ffmisc, "forkfail_misc", KSTAT_DATA_UINT32);
198893cf283aSJerry Jelinek 	kstat_named_init(&zmp->zm_nested_intp, "nested_interp",
198993cf283aSJerry Jelinek 	    KSTAT_DATA_UINT32);
1990a48d8120SJerry Jelinek 	kstat_named_init(&zmp->zm_init_pid, "init_pid", KSTAT_DATA_UINT32);
1991a48d8120SJerry Jelinek 	kstat_named_init(&zmp->zm_boot_time, "boot_time", KSTAT_DATA_UINT64);
1992542a813cSJerry Jelinek 
1993542a813cSJerry Jelinek 	ksp->ks_update = zone_misc_kstat_update;
1994542a813cSJerry Jelinek 	ksp->ks_private = zone;
1995542a813cSJerry Jelinek 
1996542a813cSJerry Jelinek 	kstat_install(ksp);
1997542a813cSJerry Jelinek 	return (ksp);
1998542a813cSJerry Jelinek }
1999542a813cSJerry Jelinek 
2000ff19e029SMenno Lageman static void
zone_kstat_create(zone_t * zone)2001ff19e029SMenno Lageman zone_kstat_create(zone_t *zone)
2002ff19e029SMenno Lageman {
2003ff19e029SMenno Lageman 	zone->zone_lockedmem_kstat = zone_kstat_create_common(zone,
2004ff19e029SMenno Lageman 	    "lockedmem", zone_lockedmem_kstat_update);
2005ff19e029SMenno Lageman 	zone->zone_swapresv_kstat = zone_kstat_create_common(zone,
2006ff19e029SMenno Lageman 	    "swapresv", zone_swapresv_kstat_update);
2007ff19e029SMenno Lageman 	zone->zone_nprocs_kstat = zone_kstat_create_common(zone,
2008ff19e029SMenno Lageman 	    "nprocs", zone_nprocs_kstat_update);
2009542a813cSJerry Jelinek 
20109468939eSJerry Jelinek 	if ((zone->zone_mcap_ksp = zone_mcap_kstat_create(zone)) == NULL) {
20119468939eSJerry Jelinek 		zone->zone_mcap_stats = kmem_zalloc(
20129468939eSJerry Jelinek 		    sizeof (zone_mcap_kstat_t), KM_SLEEP);
20139468939eSJerry Jelinek 	}
20149468939eSJerry Jelinek 
2015542a813cSJerry Jelinek 	if ((zone->zone_misc_ksp = zone_misc_kstat_create(zone)) == NULL) {
2016542a813cSJerry Jelinek 		zone->zone_misc_stats = kmem_zalloc(
2017542a813cSJerry Jelinek 		    sizeof (zone_misc_kstat_t), KM_SLEEP);
2018542a813cSJerry Jelinek 	}
20190209230bSgjelinek }
20200209230bSgjelinek 
20210209230bSgjelinek static void
zone_kstat_delete_common(kstat_t ** pkstat,size_t datasz)2022542a813cSJerry Jelinek zone_kstat_delete_common(kstat_t **pkstat, size_t datasz)
20230209230bSgjelinek {
20240209230bSgjelinek 	void *data;
20250209230bSgjelinek 
2026ff19e029SMenno Lageman 	if (*pkstat != NULL) {
2027ff19e029SMenno Lageman 		data = (*pkstat)->ks_data;
2028ff19e029SMenno Lageman 		kstat_delete(*pkstat);
2029542a813cSJerry Jelinek 		kmem_free(data, datasz);
2030ff19e029SMenno Lageman 		*pkstat = NULL;
20310209230bSgjelinek 	}
20320209230bSgjelinek }
20330209230bSgjelinek 
2034ff19e029SMenno Lageman static void
zone_kstat_delete(zone_t * zone)2035ff19e029SMenno Lageman zone_kstat_delete(zone_t *zone)
2036ff19e029SMenno Lageman {
2037542a813cSJerry Jelinek 	zone_kstat_delete_common(&zone->zone_lockedmem_kstat,
2038542a813cSJerry Jelinek 	    sizeof (zone_kstat_t));
2039542a813cSJerry Jelinek 	zone_kstat_delete_common(&zone->zone_swapresv_kstat,
2040542a813cSJerry Jelinek 	    sizeof (zone_kstat_t));
2041542a813cSJerry Jelinek 	zone_kstat_delete_common(&zone->zone_nprocs_kstat,
2042542a813cSJerry Jelinek 	    sizeof (zone_kstat_t));
20439468939eSJerry Jelinek 	zone_kstat_delete_common(&zone->zone_mcap_ksp,
20449468939eSJerry Jelinek 	    sizeof (zone_mcap_kstat_t));
2045542a813cSJerry Jelinek 	zone_kstat_delete_common(&zone->zone_misc_ksp,
2046542a813cSJerry Jelinek 	    sizeof (zone_misc_kstat_t));
2047ff19e029SMenno Lageman }
2048ff19e029SMenno Lageman 
20497c478bd9Sstevel@tonic-gate /*
20507c478bd9Sstevel@tonic-gate  * Called very early on in boot to initialize the ZSD list so that
20517c478bd9Sstevel@tonic-gate  * zone_key_create() can be called before zone_init().  It also initializes
20527c478bd9Sstevel@tonic-gate  * portions of zone0 which may be used before zone_init() is called.  The
20537c478bd9Sstevel@tonic-gate  * variable "global_zone" will be set when zone0 is fully initialized by
20547c478bd9Sstevel@tonic-gate  * zone_init().
20557c478bd9Sstevel@tonic-gate  */
20567c478bd9Sstevel@tonic-gate void
zone_zsd_init(void)20577c478bd9Sstevel@tonic-gate zone_zsd_init(void)
20587c478bd9Sstevel@tonic-gate {
20597c478bd9Sstevel@tonic-gate 	mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL);
20607c478bd9Sstevel@tonic-gate 	mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL);
20617c478bd9Sstevel@tonic-gate 	list_create(&zsd_registered_keys, sizeof (struct zsd_entry),
20627c478bd9Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
20637c478bd9Sstevel@tonic-gate 	list_create(&zone_active, sizeof (zone_t),
20647c478bd9Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
20657c478bd9Sstevel@tonic-gate 	list_create(&zone_deathrow, sizeof (zone_t),
20667c478bd9Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL);
20697c478bd9Sstevel@tonic-gate 	mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
20700209230bSgjelinek 	mutex_init(&zone0.zone_mem_lock, NULL, MUTEX_DEFAULT, NULL);
20717c478bd9Sstevel@tonic-gate 	zone0.zone_shares = 1;
20720209230bSgjelinek 	zone0.zone_nlwps = 0;
20737c478bd9Sstevel@tonic-gate 	zone0.zone_nlwps_ctl = INT_MAX;
2074ff19e029SMenno Lageman 	zone0.zone_nprocs = 0;
2075ff19e029SMenno Lageman 	zone0.zone_nprocs_ctl = INT_MAX;
20760209230bSgjelinek 	zone0.zone_locked_mem = 0;
20770209230bSgjelinek 	zone0.zone_locked_mem_ctl = UINT64_MAX;
20780209230bSgjelinek 	ASSERT(zone0.zone_max_swap == 0);
20790209230bSgjelinek 	zone0.zone_max_swap_ctl = UINT64_MAX;
20800fbb751dSJohn Levon 	zone0.zone_max_lofi = 0;
20810fbb751dSJohn Levon 	zone0.zone_max_lofi_ctl = UINT64_MAX;
2082824c205fSml 	zone0.zone_shmmax = 0;
2083824c205fSml 	zone0.zone_ipc.ipcq_shmmni = 0;
2084824c205fSml 	zone0.zone_ipc.ipcq_semmni = 0;
2085824c205fSml 	zone0.zone_ipc.ipcq_msgmni = 0;
20867c478bd9Sstevel@tonic-gate 	zone0.zone_name = GLOBAL_ZONENAME;
20877c478bd9Sstevel@tonic-gate 	zone0.zone_nodename = utsname.nodename;
20887c478bd9Sstevel@tonic-gate 	zone0.zone_domain = srpc_domain;
20895679c89fSjv 	zone0.zone_hostid = HW_INVALID_HOSTID;
20900fbb751dSJohn Levon 	zone0.zone_fs_allowed = NULL;
2091d2a70789SRichard Lowe 	psecflags_default(&zone0.zone_secflags);
20927c478bd9Sstevel@tonic-gate 	zone0.zone_ref = 1;
20937c478bd9Sstevel@tonic-gate 	zone0.zone_id = GLOBAL_ZONEID;
20947c478bd9Sstevel@tonic-gate 	zone0.zone_status = ZONE_IS_RUNNING;
20957c478bd9Sstevel@tonic-gate 	zone0.zone_rootpath = "/";
20967c478bd9Sstevel@tonic-gate 	zone0.zone_rootpathlen = 2;
20977c478bd9Sstevel@tonic-gate 	zone0.zone_psetid = ZONE_PS_INVAL;
20987c478bd9Sstevel@tonic-gate 	zone0.zone_ncpus = 0;
20997c478bd9Sstevel@tonic-gate 	zone0.zone_ncpus_online = 0;
21007c478bd9Sstevel@tonic-gate 	zone0.zone_proc_initpid = 1;
21013f2f09c1Sdp 	zone0.zone_initname = initname;
21020209230bSgjelinek 	zone0.zone_lockedmem_kstat = NULL;
21030209230bSgjelinek 	zone0.zone_swapresv_kstat = NULL;
2104ff19e029SMenno Lageman 	zone0.zone_nprocs_kstat = NULL;
2105542a813cSJerry Jelinek 
2106a19609f8Sjv 	list_create(&zone0.zone_ref_list, sizeof (zone_ref_t),
2107a19609f8Sjv 	    offsetof(zone_ref_t, zref_linkage));
21087c478bd9Sstevel@tonic-gate 	list_create(&zone0.zone_zsd, sizeof (struct zsd_entry),
21097c478bd9Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
21107c478bd9Sstevel@tonic-gate 	list_insert_head(&zone_active, &zone0);
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	/*
21137c478bd9Sstevel@tonic-gate 	 * The root filesystem is not mounted yet, so zone_rootvp cannot be set
21147c478bd9Sstevel@tonic-gate 	 * to anything meaningful.  It is assigned to be 'rootdir' in
21157c478bd9Sstevel@tonic-gate 	 * vfs_mountroot().
21167c478bd9Sstevel@tonic-gate 	 */
21177c478bd9Sstevel@tonic-gate 	zone0.zone_rootvp = NULL;
21187c478bd9Sstevel@tonic-gate 	zone0.zone_vfslist = NULL;
21193f2f09c1Sdp 	zone0.zone_bootargs = initargs;
21207c478bd9Sstevel@tonic-gate 	zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
21217c478bd9Sstevel@tonic-gate 	/*
21227c478bd9Sstevel@tonic-gate 	 * The global zone has all privileges
21237c478bd9Sstevel@tonic-gate 	 */
21247c478bd9Sstevel@tonic-gate 	priv_fillset(zone0.zone_privset);
21257c478bd9Sstevel@tonic-gate 	/*
21267c478bd9Sstevel@tonic-gate 	 * Add p0 to the global zone
21277c478bd9Sstevel@tonic-gate 	 */
21287c478bd9Sstevel@tonic-gate 	zone0.zone_zsched = &p0;
21297c478bd9Sstevel@tonic-gate 	p0.p_zone = &zone0;
21307c478bd9Sstevel@tonic-gate }
21317c478bd9Sstevel@tonic-gate 
213245916cd2Sjpk /*
213345916cd2Sjpk  * Compute a hash value based on the contents of the label and the DOI.  The
213445916cd2Sjpk  * hash algorithm is somewhat arbitrary, but is based on the observation that
213545916cd2Sjpk  * humans will likely pick labels that differ by amounts that work out to be
213645916cd2Sjpk  * multiples of the number of hash chains, and thus stirring in some primes
213745916cd2Sjpk  * should help.
213845916cd2Sjpk  */
213945916cd2Sjpk static uint_t
hash_bylabel(void * hdata,mod_hash_key_t key)214045916cd2Sjpk hash_bylabel(void *hdata, mod_hash_key_t key)
214145916cd2Sjpk {
214245916cd2Sjpk 	const ts_label_t *lab = (ts_label_t *)key;
214345916cd2Sjpk 	const uint32_t *up, *ue;
214445916cd2Sjpk 	uint_t hash;
214545916cd2Sjpk 	int i;
214645916cd2Sjpk 
214745916cd2Sjpk 	_NOTE(ARGUNUSED(hdata));
214845916cd2Sjpk 
214945916cd2Sjpk 	hash = lab->tsl_doi + (lab->tsl_doi << 1);
215045916cd2Sjpk 	/* we depend on alignment of label, but not representation */
215145916cd2Sjpk 	up = (const uint32_t *)&lab->tsl_label;
215245916cd2Sjpk 	ue = up + sizeof (lab->tsl_label) / sizeof (*up);
215345916cd2Sjpk 	i = 1;
215445916cd2Sjpk 	while (up < ue) {
215545916cd2Sjpk 		/* using 2^n + 1, 1 <= n <= 16 as source of many primes */
215645916cd2Sjpk 		hash += *up + (*up << ((i % 16) + 1));
215745916cd2Sjpk 		up++;
215845916cd2Sjpk 		i++;
215945916cd2Sjpk 	}
216045916cd2Sjpk 	return (hash);
216145916cd2Sjpk }
216245916cd2Sjpk 
216345916cd2Sjpk /*
216445916cd2Sjpk  * All that mod_hash cares about here is zero (equal) versus non-zero (not
216545916cd2Sjpk  * equal).  This may need to be changed if less than / greater than is ever
216645916cd2Sjpk  * needed.
216745916cd2Sjpk  */
216845916cd2Sjpk static int
hash_labelkey_cmp(mod_hash_key_t key1,mod_hash_key_t key2)216945916cd2Sjpk hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
217045916cd2Sjpk {
217145916cd2Sjpk 	ts_label_t *lab1 = (ts_label_t *)key1;
217245916cd2Sjpk 	ts_label_t *lab2 = (ts_label_t *)key2;
217345916cd2Sjpk 
217445916cd2Sjpk 	return (label_equal(lab1, lab2) ? 0 : 1);
217545916cd2Sjpk }
217645916cd2Sjpk 
21777c478bd9Sstevel@tonic-gate /*
21787c478bd9Sstevel@tonic-gate  * Called by main() to initialize the zones framework.
21797c478bd9Sstevel@tonic-gate  */
21807c478bd9Sstevel@tonic-gate void
zone_init(void)21817c478bd9Sstevel@tonic-gate zone_init(void)
21827c478bd9Sstevel@tonic-gate {
21837c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
21847c478bd9Sstevel@tonic-gate 	rctl_val_t *dval;
21857c478bd9Sstevel@tonic-gate 	rctl_set_t *set;
21867c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
21877c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
2188cf8f45c7Sdstaff 	int res;
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 	ASSERT(curproc == &p0);
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 	/*
21937c478bd9Sstevel@tonic-gate 	 * Create ID space for zone IDs.  ID 0 is reserved for the
21947c478bd9Sstevel@tonic-gate 	 * global zone.
21957c478bd9Sstevel@tonic-gate 	 */
21967c478bd9Sstevel@tonic-gate 	zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID);
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 	/*
21997c478bd9Sstevel@tonic-gate 	 * Initialize generic zone resource controls, if any.
22007c478bd9Sstevel@tonic-gate 	 */
22017c478bd9Sstevel@tonic-gate 	rc_zone_cpu_shares = rctl_register("zone.cpu-shares",
22027c478bd9Sstevel@tonic-gate 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER |
220319f92332Sml 	    RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER,
2204c97ad5cdSakolb 	    FSS_MAXSHARES, FSS_MAXSHARES, &zone_cpu_shares_ops);
2205c97ad5cdSakolb 
2206c97ad5cdSakolb 	rc_zone_cpu_cap = rctl_register("zone.cpu-cap",
2207c97ad5cdSakolb 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_ALWAYS |
2208c97ad5cdSakolb 	    RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT |RCTL_GLOBAL_SYSLOG_NEVER |
2209c97ad5cdSakolb 	    RCTL_GLOBAL_INFINITE,
2210c97ad5cdSakolb 	    MAXCAP, MAXCAP, &zone_cpu_cap_ops);
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 	rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE,
22137c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
22147c478bd9Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &zone_lwps_ops);
2215ff19e029SMenno Lageman 
2216ff19e029SMenno Lageman 	rc_zone_nprocs = rctl_register("zone.max-processes", RCENTITY_ZONE,
2217ff19e029SMenno Lageman 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
2218ff19e029SMenno Lageman 	    INT_MAX, INT_MAX, &zone_procs_ops);
2219ff19e029SMenno Lageman 
2220824c205fSml 	/*
2221824c205fSml 	 * System V IPC resource controls
2222824c205fSml 	 */
2223824c205fSml 	rc_zone_msgmni = rctl_register("zone.max-msg-ids",
2224824c205fSml 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
2225824c205fSml 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_msgmni_ops);
2226824c205fSml 
2227824c205fSml 	rc_zone_semmni = rctl_register("zone.max-sem-ids",
2228824c205fSml 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
2229824c205fSml 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_semmni_ops);
2230824c205fSml 
2231824c205fSml 	rc_zone_shmmni = rctl_register("zone.max-shm-ids",
2232824c205fSml 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
2233824c205fSml 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_shmmni_ops);
2234824c205fSml 
2235824c205fSml 	rc_zone_shmmax = rctl_register("zone.max-shm-memory",
2236824c205fSml 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
2237824c205fSml 	    RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &zone_shmmax_ops);
2238824c205fSml 
22397c478bd9Sstevel@tonic-gate 	/*
22407c478bd9Sstevel@tonic-gate 	 * Create a rctl_val with PRIVILEGED, NOACTION, value = 1.  Then attach
22417c478bd9Sstevel@tonic-gate 	 * this at the head of the rctl_dict_entry for ``zone.cpu-shares''.
22427c478bd9Sstevel@tonic-gate 	 */
22437c478bd9Sstevel@tonic-gate 	dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
22447c478bd9Sstevel@tonic-gate 	bzero(dval, sizeof (rctl_val_t));
22457c478bd9Sstevel@tonic-gate 	dval->rcv_value = 1;
22467c478bd9Sstevel@tonic-gate 	dval->rcv_privilege = RCPRIV_PRIVILEGED;
22477c478bd9Sstevel@tonic-gate 	dval->rcv_flagaction = RCTL_LOCAL_NOACTION;
22487c478bd9Sstevel@tonic-gate 	dval->rcv_action_recip_pid = -1;
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate 	rde = rctl_dict_lookup("zone.cpu-shares");
22517c478bd9Sstevel@tonic-gate 	(void) rctl_val_list_insert(&rde->rcd_default_value, dval);
22527c478bd9Sstevel@tonic-gate 
2253c6939658Ssl 	rc_zone_locked_mem = rctl_register("zone.max-locked-memory",
2254c6939658Ssl 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES |
2255c6939658Ssl 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
2256c6939658Ssl 	    &zone_locked_mem_ops);
22570209230bSgjelinek 
22580209230bSgjelinek 	rc_zone_max_swap = rctl_register("zone.max-swap",
22590209230bSgjelinek 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES |
22600209230bSgjelinek 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
22610209230bSgjelinek 	    &zone_max_swap_ops);
22620209230bSgjelinek 
22630fbb751dSJohn Levon 	rc_zone_max_lofi = rctl_register("zone.max-lofi",
22640fbb751dSJohn Levon 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT |
22650fbb751dSJohn Levon 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
22660fbb751dSJohn Levon 	    &zone_max_lofi_ops);
22670fbb751dSJohn Levon 
22687c478bd9Sstevel@tonic-gate 	/*
22697c478bd9Sstevel@tonic-gate 	 * Initialize the ``global zone''.
22707c478bd9Sstevel@tonic-gate 	 */
22717c478bd9Sstevel@tonic-gate 	set = rctl_set_create();
22727c478bd9Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
22737c478bd9Sstevel@tonic-gate 	mutex_enter(&p0.p_lock);
22747c478bd9Sstevel@tonic-gate 	e.rcep_p.zone = &zone0;
22757c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
22767c478bd9Sstevel@tonic-gate 	zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set,
22777c478bd9Sstevel@tonic-gate 	    gp);
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 	zone0.zone_nlwps = p0.p_lwpcnt;
2280ff19e029SMenno Lageman 	zone0.zone_nprocs = 1;
22817c478bd9Sstevel@tonic-gate 	zone0.zone_ntasks = 1;
22827c478bd9Sstevel@tonic-gate 	mutex_exit(&p0.p_lock);
22839acbbeafSnn 	zone0.zone_restart_init = B_TRUE;
228455fcd84fSJerry Jelinek 	zone0.zone_reboot_on_init_exit = B_FALSE;
228555fcd84fSJerry Jelinek 	zone0.zone_restart_init_0 = B_FALSE;
22869acbbeafSnn 	zone0.zone_brand = &native_brand;
22877c478bd9Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
22887c478bd9Sstevel@tonic-gate 	/*
22890209230bSgjelinek 	 * pool_default hasn't been initialized yet, so we let pool_init()
22900209230bSgjelinek 	 * take care of making sure the global zone is in the default pool.
22910209230bSgjelinek 	 */
22920209230bSgjelinek 
22930209230bSgjelinek 	/*
22940209230bSgjelinek 	 * Initialize global zone kstats
22957c478bd9Sstevel@tonic-gate 	 */
22960209230bSgjelinek 	zone_kstat_create(&zone0);
229745916cd2Sjpk 
229845916cd2Sjpk 	/*
229945916cd2Sjpk 	 * Initialize zone label.
230045916cd2Sjpk 	 * mlp are initialized when tnzonecfg is loaded.
230145916cd2Sjpk 	 */
230245916cd2Sjpk 	zone0.zone_slabel = l_admin_low;
230345916cd2Sjpk 	rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
230445916cd2Sjpk 	label_hold(l_admin_low);
230545916cd2Sjpk 
2306835ee219SRobert Harris 	/*
2307835ee219SRobert Harris 	 * Initialise the lock for the database structure used by mntfs.
2308835ee219SRobert Harris 	 */
2309835ee219SRobert Harris 	rw_init(&zone0.zone_mntfs_db_lock, NULL, RW_DEFAULT, NULL);
2310835ee219SRobert Harris 
23112918c4a3SJohn Levon 	zone0.zone_ustate = cpu_uarray_zalloc(ZONE_USTATE_MAX, KM_SLEEP);
23122918c4a3SJohn Levon 
23137c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
23147c478bd9Sstevel@tonic-gate 	zone_uniqid(&zone0);
23157c478bd9Sstevel@tonic-gate 	ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID);
231645916cd2Sjpk 
23177c478bd9Sstevel@tonic-gate 	zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size,
23187c478bd9Sstevel@tonic-gate 	    mod_hash_null_valdtor);
23197c478bd9Sstevel@tonic-gate 	zonehashbyname = mod_hash_create_strhash("zone_by_name",
23207c478bd9Sstevel@tonic-gate 	    zone_hash_size, mod_hash_null_valdtor);
232145916cd2Sjpk 	/*
232245916cd2Sjpk 	 * maintain zonehashbylabel only for labeled systems
232345916cd2Sjpk 	 */
232445916cd2Sjpk 	if (is_system_labeled())
232545916cd2Sjpk 		zonehashbylabel = mod_hash_create_extended("zone_by_label",
232645916cd2Sjpk 		    zone_hash_size, mod_hash_null_keydtor,
232745916cd2Sjpk 		    mod_hash_null_valdtor, hash_bylabel, NULL,
232845916cd2Sjpk 		    hash_labelkey_cmp, KM_SLEEP);
23297c478bd9Sstevel@tonic-gate 	zonecount = 1;
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID,
23327c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
23337c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name,
23347c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
233548451833Scarlsonj 	if (is_system_labeled()) {
233648451833Scarlsonj 		zone0.zone_flags |= ZF_HASHED_LABEL;
233745916cd2Sjpk 		(void) mod_hash_insert(zonehashbylabel,
233845916cd2Sjpk 		    (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0);
233948451833Scarlsonj 	}
234045916cd2Sjpk 	mutex_exit(&zonehash_lock);
234145916cd2Sjpk 
23427c478bd9Sstevel@tonic-gate 	/*
23437c478bd9Sstevel@tonic-gate 	 * We avoid setting zone_kcred until now, since kcred is initialized
23447c478bd9Sstevel@tonic-gate 	 * sometime after zone_zsd_init() and before zone_init().
23457c478bd9Sstevel@tonic-gate 	 */
23467c478bd9Sstevel@tonic-gate 	zone0.zone_kcred = kcred;
23477c478bd9Sstevel@tonic-gate 	/*
23487c478bd9Sstevel@tonic-gate 	 * The global zone is fully initialized (except for zone_rootvp which
23497c478bd9Sstevel@tonic-gate 	 * will be set when the root filesystem is mounted).
23507c478bd9Sstevel@tonic-gate 	 */
23517c478bd9Sstevel@tonic-gate 	global_zone = &zone0;
2352cf8f45c7Sdstaff 
2353cf8f45c7Sdstaff 	/*
2354cf8f45c7Sdstaff 	 * Setup an event channel to send zone status change notifications on
2355cf8f45c7Sdstaff 	 */
2356cf8f45c7Sdstaff 	res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan,
2357cf8f45c7Sdstaff 	    EVCH_CREAT);
2358cf8f45c7Sdstaff 
2359cf8f45c7Sdstaff 	if (res)
2360cf8f45c7Sdstaff 		panic("Sysevent_evc_bind failed during zone setup.\n");
23610209230bSgjelinek 
23627c478bd9Sstevel@tonic-gate }
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate static void
zone_free(zone_t * zone)23657c478bd9Sstevel@tonic-gate zone_free(zone_t *zone)
23667c478bd9Sstevel@tonic-gate {
2367*85dff7a0SAndy Fiddaman 	zone_dl_t *zdl;
2368*85dff7a0SAndy Fiddaman 
23697c478bd9Sstevel@tonic-gate 	ASSERT(zone != global_zone);
23707c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks == 0);
23717c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_nlwps == 0);
2372ff19e029SMenno Lageman 	ASSERT(zone->zone_nprocs == 0);
23737c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 0);
23747c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_kcred == NULL);
23757c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_DEAD ||
23767c478bd9Sstevel@tonic-gate 	    zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
2377a19609f8Sjv 	ASSERT(list_is_empty(&zone->zone_ref_list));
23787c478bd9Sstevel@tonic-gate 
2379c97ad5cdSakolb 	/*
2380c97ad5cdSakolb 	 * Remove any zone caps.
2381c97ad5cdSakolb 	 */
2382c97ad5cdSakolb 	cpucaps_zone_remove(zone);
2383c97ad5cdSakolb 
2384c97ad5cdSakolb 	ASSERT(zone->zone_cpucap == NULL);
2385c97ad5cdSakolb 
23867c478bd9Sstevel@tonic-gate 	/* remove from deathrow list */
23877c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_DEAD) {
23887c478bd9Sstevel@tonic-gate 		ASSERT(zone->zone_ref == 0);
23897c478bd9Sstevel@tonic-gate 		mutex_enter(&zone_deathrow_lock);
23907c478bd9Sstevel@tonic-gate 		list_remove(&zone_deathrow, zone);
23917c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_deathrow_lock);
23927c478bd9Sstevel@tonic-gate 	}
23937c478bd9Sstevel@tonic-gate 
2394a19609f8Sjv 	list_destroy(&zone->zone_ref_list);
23957c478bd9Sstevel@tonic-gate 	zone_free_zsd(zone);
2396fa9e4066Sahrens 	zone_free_datasets(zone);
2397*85dff7a0SAndy Fiddaman 
2398*85dff7a0SAndy Fiddaman 	/*
2399*85dff7a0SAndy Fiddaman 	 * While dlmgmtd should have removed all of these, it could have left
2400*85dff7a0SAndy Fiddaman 	 * something behind or crashed. In which case it's not safe for us to
2401*85dff7a0SAndy Fiddaman 	 * assume that the list is empty which list_destroy() will ASSERT. We
2402*85dff7a0SAndy Fiddaman 	 * clean up for our userland comrades which may have crashed, or worse,
2403*85dff7a0SAndy Fiddaman 	 * been disabled by SMF.
2404*85dff7a0SAndy Fiddaman 	 */
2405*85dff7a0SAndy Fiddaman 	while ((zdl = list_remove_head(&zone->zone_dl_list)) != NULL) {
2406*85dff7a0SAndy Fiddaman 		if (zdl->zdl_net != NULL)
2407*85dff7a0SAndy Fiddaman 			nvlist_free(zdl->zdl_net);
2408*85dff7a0SAndy Fiddaman 		kmem_free(zdl, sizeof (zone_dl_t));
2409*85dff7a0SAndy Fiddaman 	}
24102b24ab6bSSebastien Roy 	list_destroy(&zone->zone_dl_list);
24117c478bd9Sstevel@tonic-gate 
24122918c4a3SJohn Levon 	cpu_uarray_free(zone->zone_ustate);
24132918c4a3SJohn Levon 
24147c478bd9Sstevel@tonic-gate 	if (zone->zone_rootvp != NULL)
24157c478bd9Sstevel@tonic-gate 		VN_RELE(zone->zone_rootvp);
24167c478bd9Sstevel@tonic-gate 	if (zone->zone_rootpath)
24177c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_rootpath, zone->zone_rootpathlen);
24187c478bd9Sstevel@tonic-gate 	if (zone->zone_name != NULL)
24197c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_name, ZONENAME_MAX);
242045916cd2Sjpk 	if (zone->zone_slabel != NULL)
242145916cd2Sjpk 		label_rele(zone->zone_slabel);
24227c478bd9Sstevel@tonic-gate 	if (zone->zone_nodename != NULL)
24237c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_nodename, _SYS_NMLN);
24247c478bd9Sstevel@tonic-gate 	if (zone->zone_domain != NULL)
24257c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_domain, _SYS_NMLN);
24267c478bd9Sstevel@tonic-gate 	if (zone->zone_privset != NULL)
24277c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_privset, sizeof (priv_set_t));
24287c478bd9Sstevel@tonic-gate 	if (zone->zone_rctls != NULL)
24297c478bd9Sstevel@tonic-gate 		rctl_set_free(zone->zone_rctls);
24307c478bd9Sstevel@tonic-gate 	if (zone->zone_bootargs != NULL)
24310fbb751dSJohn Levon 		strfree(zone->zone_bootargs);
24323f2f09c1Sdp 	if (zone->zone_initname != NULL)
24330fbb751dSJohn Levon 		strfree(zone->zone_initname);
24340fbb751dSJohn Levon 	if (zone->zone_fs_allowed != NULL)
24350fbb751dSJohn Levon 		strfree(zone->zone_fs_allowed);
2436134a1f4eSCasper H.S. Dik 	if (zone->zone_pfexecd != NULL)
2437134a1f4eSCasper H.S. Dik 		klpd_freelist(&zone->zone_pfexecd);
24387c478bd9Sstevel@tonic-gate 	id_free(zoneid_space, zone->zone_id);
24397c478bd9Sstevel@tonic-gate 	mutex_destroy(&zone->zone_lock);
24407c478bd9Sstevel@tonic-gate 	cv_destroy(&zone->zone_cv);
244145916cd2Sjpk 	rw_destroy(&zone->zone_mlps.mlpl_rwlock);
2442835ee219SRobert Harris 	rw_destroy(&zone->zone_mntfs_db_lock);
24437c478bd9Sstevel@tonic-gate 	kmem_free(zone, sizeof (zone_t));
24447c478bd9Sstevel@tonic-gate }
24457c478bd9Sstevel@tonic-gate 
24467c478bd9Sstevel@tonic-gate /*
24477c478bd9Sstevel@tonic-gate  * See block comment at the top of this file for information about zone
24487c478bd9Sstevel@tonic-gate  * status values.
24497c478bd9Sstevel@tonic-gate  */
24507c478bd9Sstevel@tonic-gate /*
24517c478bd9Sstevel@tonic-gate  * Convenience function for setting zone status.
24527c478bd9Sstevel@tonic-gate  */
24537c478bd9Sstevel@tonic-gate static void
zone_status_set(zone_t * zone,zone_status_t status)24547c478bd9Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status)
24557c478bd9Sstevel@tonic-gate {
2456cf8f45c7Sdstaff 
2457cf8f45c7Sdstaff 	nvlist_t *nvl = NULL;
24587c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zone_status_lock));
24597c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE &&
24607c478bd9Sstevel@tonic-gate 	    status >= zone_status_get(zone));
2461cf8f45c7Sdstaff 
2462cf8f45c7Sdstaff 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) ||
2463cf8f45c7Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) ||
2464cf8f45c7Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_NEWSTATE,
24653f2f09c1Sdp 	    zone_status_table[status]) ||
2466cf8f45c7Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_OLDSTATE,
24673f2f09c1Sdp 	    zone_status_table[zone->zone_status]) ||
2468cf8f45c7Sdstaff 	    nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) ||
2469cf8f45c7Sdstaff 	    nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) ||
2470cf8f45c7Sdstaff 	    sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS,
24713f2f09c1Sdp 	    ZONE_EVENT_STATUS_SUBCLASS, "sun.com", "kernel", nvl, EVCH_SLEEP)) {
2472cf8f45c7Sdstaff #ifdef DEBUG
2473cf8f45c7Sdstaff 		(void) printf(
2474cf8f45c7Sdstaff 		    "Failed to allocate and send zone state change event.\n");
2475cf8f45c7Sdstaff #endif
2476cf8f45c7Sdstaff 	}
2477cf8f45c7Sdstaff 	nvlist_free(nvl);
2478cf8f45c7Sdstaff 
24797c478bd9Sstevel@tonic-gate 	zone->zone_status = status;
2480cf8f45c7Sdstaff 
24817c478bd9Sstevel@tonic-gate 	cv_broadcast(&zone->zone_cv);
24827c478bd9Sstevel@tonic-gate }
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate /*
24857c478bd9Sstevel@tonic-gate  * Public function to retrieve the zone status.  The zone status may
24867c478bd9Sstevel@tonic-gate  * change after it is retrieved.
24877c478bd9Sstevel@tonic-gate  */
24887c478bd9Sstevel@tonic-gate zone_status_t
zone_status_get(zone_t * zone)24897c478bd9Sstevel@tonic-gate zone_status_get(zone_t *zone)
24907c478bd9Sstevel@tonic-gate {
24917c478bd9Sstevel@tonic-gate 	return (zone->zone_status);
24927c478bd9Sstevel@tonic-gate }
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate static int
zone_set_bootargs(zone_t * zone,const char * zone_bootargs)24957c478bd9Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs)
24967c478bd9Sstevel@tonic-gate {
24970fbb751dSJohn Levon 	char *buf = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP);
24983f2f09c1Sdp 	int err = 0;
24993f2f09c1Sdp 
25003f2f09c1Sdp 	ASSERT(zone != global_zone);
25010fbb751dSJohn Levon 	if ((err = copyinstr(zone_bootargs, buf, BOOTARGS_MAX, NULL)) != 0)
25023f2f09c1Sdp 		goto done;	/* EFAULT or ENAMETOOLONG */
25033f2f09c1Sdp 
25043f2f09c1Sdp 	if (zone->zone_bootargs != NULL)
25050fbb751dSJohn Levon 		strfree(zone->zone_bootargs);
25063f2f09c1Sdp 
25070fbb751dSJohn Levon 	zone->zone_bootargs = strdup(buf);
25083f2f09c1Sdp 
25093f2f09c1Sdp done:
25100fbb751dSJohn Levon 	kmem_free(buf, BOOTARGS_MAX);
25113f2f09c1Sdp 	return (err);
25123f2f09c1Sdp }
25133f2f09c1Sdp 
251459f2ff5cSedp static int
zone_set_brand(zone_t * zone,const char * brand)251559f2ff5cSedp zone_set_brand(zone_t *zone, const char *brand)
251659f2ff5cSedp {
251759f2ff5cSedp 	struct brand_attr *attrp;
251859f2ff5cSedp 	brand_t *bp;
251959f2ff5cSedp 
252059f2ff5cSedp 	attrp = kmem_alloc(sizeof (struct brand_attr), KM_SLEEP);
252159f2ff5cSedp 	if (copyin(brand, attrp, sizeof (struct brand_attr)) != 0) {
252259f2ff5cSedp 		kmem_free(attrp, sizeof (struct brand_attr));
252359f2ff5cSedp 		return (EFAULT);
252459f2ff5cSedp 	}
252559f2ff5cSedp 
252659f2ff5cSedp 	bp = brand_register_zone(attrp);
252759f2ff5cSedp 	kmem_free(attrp, sizeof (struct brand_attr));
252859f2ff5cSedp 	if (bp == NULL)
252959f2ff5cSedp 		return (EINVAL);
253059f2ff5cSedp 
253159f2ff5cSedp 	/*
253259f2ff5cSedp 	 * This is the only place where a zone can change it's brand.
253359f2ff5cSedp 	 * We already need to hold zone_status_lock to check the zone
253459f2ff5cSedp 	 * status, so we'll just use that lock to serialize zone
253559f2ff5cSedp 	 * branding requests as well.
253659f2ff5cSedp 	 */
253759f2ff5cSedp 	mutex_enter(&zone_status_lock);
253859f2ff5cSedp 
253959f2ff5cSedp 	/* Re-Branding is not allowed and the zone can't be booted yet */
254059f2ff5cSedp 	if ((ZONE_IS_BRANDED(zone)) ||
254159f2ff5cSedp 	    (zone_status_get(zone) >= ZONE_IS_BOOTING)) {
254259f2ff5cSedp 		mutex_exit(&zone_status_lock);
254359f2ff5cSedp 		brand_unregister_zone(bp);
254459f2ff5cSedp 		return (EINVAL);
254559f2ff5cSedp 	}
254659f2ff5cSedp 
2547319378d9Seh 	/* set up the brand specific data */
254859f2ff5cSedp 	zone->zone_brand = bp;
2549319378d9Seh 	ZBROP(zone)->b_init_brand_data(zone);
2550319378d9Seh 
255159f2ff5cSedp 	mutex_exit(&zone_status_lock);
255259f2ff5cSedp 	return (0);
255359f2ff5cSedp }
255459f2ff5cSedp 
2555d2a70789SRichard Lowe static int
zone_set_secflags(zone_t * zone,const psecflags_t * zone_secflags)2556d2a70789SRichard Lowe zone_set_secflags(zone_t *zone, const psecflags_t *zone_secflags)
2557d2a70789SRichard Lowe {
2558d2a70789SRichard Lowe 	int err = 0;
2559d2a70789SRichard Lowe 	psecflags_t psf;
2560d2a70789SRichard Lowe 
2561d2a70789SRichard Lowe 	ASSERT(zone != global_zone);
2562d2a70789SRichard Lowe 
2563d2a70789SRichard Lowe 	if ((err = copyin(zone_secflags, &psf, sizeof (psf))) != 0)
2564d2a70789SRichard Lowe 		return (err);
2565d2a70789SRichard Lowe 
2566d2a70789SRichard Lowe 	if (zone_status_get(zone) > ZONE_IS_READY)
2567d2a70789SRichard Lowe 		return (EINVAL);
2568d2a70789SRichard Lowe 
2569d2a70789SRichard Lowe 	if (!psecflags_validate(&psf))
2570d2a70789SRichard Lowe 		return (EINVAL);
2571d2a70789SRichard Lowe 
2572d2a70789SRichard Lowe 	(void) memcpy(&zone->zone_secflags, &psf, sizeof (psf));
2573d2a70789SRichard Lowe 
2574d2a70789SRichard Lowe 	/* Set security flags on the zone's zsched */
2575d2a70789SRichard Lowe 	(void) memcpy(&zone->zone_zsched->p_secflags, &zone->zone_secflags,
2576d2a70789SRichard Lowe 	    sizeof (zone->zone_zsched->p_secflags));
2577d2a70789SRichard Lowe 
2578d2a70789SRichard Lowe 	return (0);
2579d2a70789SRichard Lowe }
2580d2a70789SRichard Lowe 
25810fbb751dSJohn Levon static int
zone_set_fs_allowed(zone_t * zone,const char * zone_fs_allowed)25820fbb751dSJohn Levon zone_set_fs_allowed(zone_t *zone, const char *zone_fs_allowed)
25830fbb751dSJohn Levon {
25840fbb751dSJohn Levon 	char *buf = kmem_zalloc(ZONE_FS_ALLOWED_MAX, KM_SLEEP);
25850fbb751dSJohn Levon 	int err = 0;
25860fbb751dSJohn Levon 
25870fbb751dSJohn Levon 	ASSERT(zone != global_zone);
25880fbb751dSJohn Levon 	if ((err = copyinstr(zone_fs_allowed, buf,
25890fbb751dSJohn Levon 	    ZONE_FS_ALLOWED_MAX, NULL)) != 0)
25900fbb751dSJohn Levon 		goto done;
25910fbb751dSJohn Levon 
25920fbb751dSJohn Levon 	if (zone->zone_fs_allowed != NULL)
25930fbb751dSJohn Levon 		strfree(zone->zone_fs_allowed);
25940fbb751dSJohn Levon 
25950fbb751dSJohn Levon 	zone->zone_fs_allowed = strdup(buf);
25960fbb751dSJohn Levon 
25970fbb751dSJohn Levon done:
25980fbb751dSJohn Levon 	kmem_free(buf, ZONE_FS_ALLOWED_MAX);
25990fbb751dSJohn Levon 	return (err);
26000fbb751dSJohn Levon }
26010fbb751dSJohn Levon 
26023f2f09c1Sdp static int
zone_set_initname(zone_t * zone,const char * zone_initname)26033f2f09c1Sdp zone_set_initname(zone_t *zone, const char *zone_initname)
26043f2f09c1Sdp {
26053f2f09c1Sdp 	char initname[INITNAME_SZ];
26067c478bd9Sstevel@tonic-gate 	size_t len;
26073f2f09c1Sdp 	int err = 0;
26087c478bd9Sstevel@tonic-gate 
26093f2f09c1Sdp 	ASSERT(zone != global_zone);
26103f2f09c1Sdp 	if ((err = copyinstr(zone_initname, initname, INITNAME_SZ, &len)) != 0)
26117c478bd9Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
26127c478bd9Sstevel@tonic-gate 
26133f2f09c1Sdp 	if (zone->zone_initname != NULL)
26140fbb751dSJohn Levon 		strfree(zone->zone_initname);
26153f2f09c1Sdp 
26163f2f09c1Sdp 	zone->zone_initname = kmem_alloc(strlen(initname) + 1, KM_SLEEP);
26173f2f09c1Sdp 	(void) strcpy(zone->zone_initname, initname);
26187c478bd9Sstevel@tonic-gate 	return (0);
26197c478bd9Sstevel@tonic-gate }
26207c478bd9Sstevel@tonic-gate 
26210209230bSgjelinek static int
zone_set_phys_mcap(zone_t * zone,const uint64_t * zone_mcap)26220209230bSgjelinek zone_set_phys_mcap(zone_t *zone, const uint64_t *zone_mcap)
26230209230bSgjelinek {
26240209230bSgjelinek 	uint64_t mcap;
26250209230bSgjelinek 	int err = 0;
26260209230bSgjelinek 
26270209230bSgjelinek 	if ((err = copyin(zone_mcap, &mcap, sizeof (uint64_t))) == 0)
26280209230bSgjelinek 		zone->zone_phys_mcap = mcap;
26290209230bSgjelinek 
26300209230bSgjelinek 	return (err);
26310209230bSgjelinek }
26320209230bSgjelinek 
26330209230bSgjelinek static int
zone_set_sched_class(zone_t * zone,const char * new_class)26340209230bSgjelinek zone_set_sched_class(zone_t *zone, const char *new_class)
26350209230bSgjelinek {
26360209230bSgjelinek 	char sched_class[PC_CLNMSZ];
26370209230bSgjelinek 	id_t classid;
26380209230bSgjelinek 	int err;
26390209230bSgjelinek 
26400209230bSgjelinek 	ASSERT(zone != global_zone);
26410209230bSgjelinek 	if ((err = copyinstr(new_class, sched_class, PC_CLNMSZ, NULL)) != 0)
26420209230bSgjelinek 		return (err);	/* EFAULT or ENAMETOOLONG */
26430209230bSgjelinek 
264435a5a358SJonathan Adams 	if (getcid(sched_class, &classid) != 0 || CLASS_KERNEL(classid))
26450209230bSgjelinek 		return (set_errno(EINVAL));
26460209230bSgjelinek 	zone->zone_defaultcid = classid;
26470209230bSgjelinek 	ASSERT(zone->zone_defaultcid > 0 &&
26480209230bSgjelinek 	    zone->zone_defaultcid < loaded_classes);
26490209230bSgjelinek 
26500209230bSgjelinek 	return (0);
26510209230bSgjelinek }
26520209230bSgjelinek 
26537c478bd9Sstevel@tonic-gate /*
26547c478bd9Sstevel@tonic-gate  * Block indefinitely waiting for (zone_status >= status)
26557c478bd9Sstevel@tonic-gate  */
26567c478bd9Sstevel@tonic-gate void
zone_status_wait(zone_t * zone,zone_status_t status)26577c478bd9Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status)
26587c478bd9Sstevel@tonic-gate {
26597c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
26607c478bd9Sstevel@tonic-gate 
26617c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
26627c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
26637c478bd9Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
26647c478bd9Sstevel@tonic-gate 	}
26657c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
26667c478bd9Sstevel@tonic-gate }
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate /*
26697c478bd9Sstevel@tonic-gate  * Private CPR-safe version of zone_status_wait().
26707c478bd9Sstevel@tonic-gate  */
26717c478bd9Sstevel@tonic-gate static void
zone_status_wait_cpr(zone_t * zone,zone_status_t status,char * str)26727c478bd9Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str)
26737c478bd9Sstevel@tonic-gate {
26747c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr,
26797c478bd9Sstevel@tonic-gate 	    str);
26807c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
26817c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
26827c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
26837c478bd9Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
26847c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock);
26857c478bd9Sstevel@tonic-gate 	}
26867c478bd9Sstevel@tonic-gate 	/*
26877c478bd9Sstevel@tonic-gate 	 * zone_status_lock is implicitly released by the following.
26887c478bd9Sstevel@tonic-gate 	 */
26897c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
26907c478bd9Sstevel@tonic-gate }
26917c478bd9Sstevel@tonic-gate 
26927c478bd9Sstevel@tonic-gate /*
26937c478bd9Sstevel@tonic-gate  * Block until zone enters requested state or signal is received.  Return (0)
26947c478bd9Sstevel@tonic-gate  * if signaled, non-zero otherwise.
26957c478bd9Sstevel@tonic-gate  */
26967c478bd9Sstevel@tonic-gate int
zone_status_wait_sig(zone_t * zone,zone_status_t status)26977c478bd9Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status)
26987c478bd9Sstevel@tonic-gate {
26997c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
27007c478bd9Sstevel@tonic-gate 
27017c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
27027c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
27037c478bd9Sstevel@tonic-gate 		if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) {
27047c478bd9Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
27057c478bd9Sstevel@tonic-gate 			return (0);
27067c478bd9Sstevel@tonic-gate 		}
27077c478bd9Sstevel@tonic-gate 	}
27087c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
27097c478bd9Sstevel@tonic-gate 	return (1);
27107c478bd9Sstevel@tonic-gate }
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate /*
27137c478bd9Sstevel@tonic-gate  * Block until the zone enters the requested state or the timeout expires,
27147c478bd9Sstevel@tonic-gate  * whichever happens first.  Return (-1) if operation timed out, time remaining
27157c478bd9Sstevel@tonic-gate  * otherwise.
27167c478bd9Sstevel@tonic-gate  */
27177c478bd9Sstevel@tonic-gate clock_t
zone_status_timedwait(zone_t * zone,clock_t tim,zone_status_t status)27187c478bd9Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status)
27197c478bd9Sstevel@tonic-gate {
27207c478bd9Sstevel@tonic-gate 	clock_t timeleft = 0;
27217c478bd9Sstevel@tonic-gate 
27227c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
27257c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status && timeleft != -1) {
27267c478bd9Sstevel@tonic-gate 		timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim);
27277c478bd9Sstevel@tonic-gate 	}
27287c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
27297c478bd9Sstevel@tonic-gate 	return (timeleft);
27307c478bd9Sstevel@tonic-gate }
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate /*
27337c478bd9Sstevel@tonic-gate  * Block until the zone enters the requested state, the current process is
27347c478bd9Sstevel@tonic-gate  * signaled,  or the timeout expires, whichever happens first.  Return (-1) if
27357c478bd9Sstevel@tonic-gate  * operation timed out, 0 if signaled, time remaining otherwise.
27367c478bd9Sstevel@tonic-gate  */
27377c478bd9Sstevel@tonic-gate clock_t
zone_status_timedwait_sig(zone_t * zone,clock_t tim,zone_status_t status)27387c478bd9Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status)
27397c478bd9Sstevel@tonic-gate {
2740d3d50737SRafael Vanoni 	clock_t timeleft = tim - ddi_get_lbolt();
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
27457c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
27467c478bd9Sstevel@tonic-gate 		timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock,
27477c478bd9Sstevel@tonic-gate 		    tim);
27487c478bd9Sstevel@tonic-gate 		if (timeleft <= 0)
27497c478bd9Sstevel@tonic-gate 			break;
27507c478bd9Sstevel@tonic-gate 	}
27517c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
27527c478bd9Sstevel@tonic-gate 	return (timeleft);
27537c478bd9Sstevel@tonic-gate }
27547c478bd9Sstevel@tonic-gate 
27557c478bd9Sstevel@tonic-gate /*
27567c478bd9Sstevel@tonic-gate  * Zones have two reference counts: one for references from credential
27577c478bd9Sstevel@tonic-gate  * structures (zone_cred_ref), and one (zone_ref) for everything else.
27587c478bd9Sstevel@tonic-gate  * This is so we can allow a zone to be rebooted while there are still
27597c478bd9Sstevel@tonic-gate  * outstanding cred references, since certain drivers cache dblks (which
27607c478bd9Sstevel@tonic-gate  * implicitly results in cached creds).  We wait for zone_ref to drop to
27617c478bd9Sstevel@tonic-gate  * 0 (actually 1), but not zone_cred_ref.  The zone structure itself is
27627c478bd9Sstevel@tonic-gate  * later freed when the zone_cred_ref drops to 0, though nothing other
27637c478bd9Sstevel@tonic-gate  * than the zone id and privilege set should be accessed once the zone
27647c478bd9Sstevel@tonic-gate  * is "dead".
27657c478bd9Sstevel@tonic-gate  *
27667c478bd9Sstevel@tonic-gate  * A debugging flag, zone_wait_for_cred, can be set to a non-zero value
27677c478bd9Sstevel@tonic-gate  * to force halt/reboot to block waiting for the zone_cred_ref to drop
27687c478bd9Sstevel@tonic-gate  * to 0.  This can be useful to flush out other sources of cached creds
27697c478bd9Sstevel@tonic-gate  * that may be less innocuous than the driver case.
2770a19609f8Sjv  *
2771a19609f8Sjv  * Zones also provide a tracked reference counting mechanism in which zone
2772a19609f8Sjv  * references are represented by "crumbs" (zone_ref structures).  Crumbs help
2773a19609f8Sjv  * debuggers determine the sources of leaked zone references.  See
2774a19609f8Sjv  * zone_hold_ref() and zone_rele_ref() below for more information.
27757c478bd9Sstevel@tonic-gate  */
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate int zone_wait_for_cred = 0;
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate static void
zone_hold_locked(zone_t * z)27807c478bd9Sstevel@tonic-gate zone_hold_locked(zone_t *z)
27817c478bd9Sstevel@tonic-gate {
27827c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&z->zone_lock));
27837c478bd9Sstevel@tonic-gate 	z->zone_ref++;
27847c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
27857c478bd9Sstevel@tonic-gate }
27867c478bd9Sstevel@tonic-gate 
2787a19609f8Sjv /*
2788a19609f8Sjv  * Increment the specified zone's reference count.  The zone's zone_t structure
2789a19609f8Sjv  * will not be freed as long as the zone's reference count is nonzero.
2790a19609f8Sjv  * Decrement the zone's reference count via zone_rele().
2791a19609f8Sjv  *
2792a19609f8Sjv  * NOTE: This function should only be used to hold zones for short periods of
2793a19609f8Sjv  * time.  Use zone_hold_ref() if the zone must be held for a long time.
2794a19609f8Sjv  */
27957c478bd9Sstevel@tonic-gate void
zone_hold(zone_t * z)27967c478bd9Sstevel@tonic-gate zone_hold(zone_t *z)
27977c478bd9Sstevel@tonic-gate {
27987c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
27997c478bd9Sstevel@tonic-gate 	zone_hold_locked(z);
28007c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
28017c478bd9Sstevel@tonic-gate }
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate /*
28047c478bd9Sstevel@tonic-gate  * If the non-cred ref count drops to 1 and either the cred ref count
28057c478bd9Sstevel@tonic-gate  * is 0 or we aren't waiting for cred references, the zone is ready to
28067c478bd9Sstevel@tonic-gate  * be destroyed.
28077c478bd9Sstevel@tonic-gate  */
28087c478bd9Sstevel@tonic-gate #define	ZONE_IS_UNREF(zone)	((zone)->zone_ref == 1 && \
28097c478bd9Sstevel@tonic-gate 	    (!zone_wait_for_cred || (zone)->zone_cred_ref == 0))
28107c478bd9Sstevel@tonic-gate 
2811a19609f8Sjv /*
2812a19609f8Sjv  * Common zone reference release function invoked by zone_rele() and
2813a19609f8Sjv  * zone_rele_ref().  If subsys is ZONE_REF_NUM_SUBSYS, then the specified
2814a19609f8Sjv  * zone's subsystem-specific reference counters are not affected by the
2815a19609f8Sjv  * release.  If ref is not NULL, then the zone_ref_t to which it refers is
2816a19609f8Sjv  * removed from the specified zone's reference list.  ref must be non-NULL iff
2817a19609f8Sjv  * subsys is not ZONE_REF_NUM_SUBSYS.
2818a19609f8Sjv  */
2819a19609f8Sjv static void
zone_rele_common(zone_t * z,zone_ref_t * ref,zone_ref_subsys_t subsys)2820a19609f8Sjv zone_rele_common(zone_t *z, zone_ref_t *ref, zone_ref_subsys_t subsys)
28217c478bd9Sstevel@tonic-gate {
28227c478bd9Sstevel@tonic-gate 	boolean_t wakeup;
28237c478bd9Sstevel@tonic-gate 
28247c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
28257c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
28267c478bd9Sstevel@tonic-gate 	z->zone_ref--;
2827a19609f8Sjv 	if (subsys != ZONE_REF_NUM_SUBSYS) {
2828a19609f8Sjv 		ASSERT(z->zone_subsys_ref[subsys] != 0);
2829a19609f8Sjv 		z->zone_subsys_ref[subsys]--;
2830a19609f8Sjv 		list_remove(&z->zone_ref_list, ref);
2831a19609f8Sjv 	}
28327c478bd9Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
28337c478bd9Sstevel@tonic-gate 		/* no more refs, free the structure */
28347c478bd9Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
28357c478bd9Sstevel@tonic-gate 		zone_free(z);
28367c478bd9Sstevel@tonic-gate 		return;
28377c478bd9Sstevel@tonic-gate 	}
28387c478bd9Sstevel@tonic-gate 	/* signal zone_destroy so the zone can finish halting */
28397c478bd9Sstevel@tonic-gate 	wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD);
28407c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 	if (wakeup) {
28437c478bd9Sstevel@tonic-gate 		/*
28447c478bd9Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
28457c478bd9Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
28467c478bd9Sstevel@tonic-gate 		 */
28477c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
28487c478bd9Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
28497c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
28507c478bd9Sstevel@tonic-gate 	}
28517c478bd9Sstevel@tonic-gate }
28527c478bd9Sstevel@tonic-gate 
2853a19609f8Sjv /*
2854a19609f8Sjv  * Decrement the specified zone's reference count.  The specified zone will
2855a19609f8Sjv  * cease to exist after this function returns if the reference count drops to
2856a19609f8Sjv  * zero.  This function should be paired with zone_hold().
2857a19609f8Sjv  */
2858a19609f8Sjv void
zone_rele(zone_t * z)2859a19609f8Sjv zone_rele(zone_t *z)
2860a19609f8Sjv {
2861a19609f8Sjv 	zone_rele_common(z, NULL, ZONE_REF_NUM_SUBSYS);
2862a19609f8Sjv }
2863a19609f8Sjv 
2864a19609f8Sjv /*
2865a19609f8Sjv  * Initialize a zone reference structure.  This function must be invoked for
2866a19609f8Sjv  * a reference structure before the structure is passed to zone_hold_ref().
2867a19609f8Sjv  */
2868a19609f8Sjv void
zone_init_ref(zone_ref_t * ref)2869a19609f8Sjv zone_init_ref(zone_ref_t *ref)
2870a19609f8Sjv {
2871a19609f8Sjv 	ref->zref_zone = NULL;
2872a19609f8Sjv 	list_link_init(&ref->zref_linkage);
2873a19609f8Sjv }
2874a19609f8Sjv 
2875a19609f8Sjv /*
2876a19609f8Sjv  * Acquire a reference to zone z.  The caller must specify the
2877a19609f8Sjv  * zone_ref_subsys_t constant associated with its subsystem.  The specified
2878a19609f8Sjv  * zone_ref_t structure will represent a reference to the specified zone.  Use
2879a19609f8Sjv  * zone_rele_ref() to release the reference.
2880a19609f8Sjv  *
2881a19609f8Sjv  * The referenced zone_t structure will not be freed as long as the zone_t's
2882a19609f8Sjv  * zone_status field is not ZONE_IS_DEAD and the zone has outstanding
2883a19609f8Sjv  * references.
2884a19609f8Sjv  *
2885a19609f8Sjv  * NOTE: The zone_ref_t structure must be initialized before it is used.
2886a19609f8Sjv  * See zone_init_ref() above.
2887a19609f8Sjv  */
2888a19609f8Sjv void
zone_hold_ref(zone_t * z,zone_ref_t * ref,zone_ref_subsys_t subsys)2889a19609f8Sjv zone_hold_ref(zone_t *z, zone_ref_t *ref, zone_ref_subsys_t subsys)
2890a19609f8Sjv {
2891a19609f8Sjv 	ASSERT(subsys >= 0 && subsys < ZONE_REF_NUM_SUBSYS);
2892a19609f8Sjv 
2893a19609f8Sjv 	/*
2894a19609f8Sjv 	 * Prevent consumers from reusing a reference structure before
2895a19609f8Sjv 	 * releasing it.
2896a19609f8Sjv 	 */
2897a19609f8Sjv 	VERIFY(ref->zref_zone == NULL);
2898a19609f8Sjv 
2899a19609f8Sjv 	ref->zref_zone = z;
2900a19609f8Sjv 	mutex_enter(&z->zone_lock);
2901a19609f8Sjv 	zone_hold_locked(z);
2902a19609f8Sjv 	z->zone_subsys_ref[subsys]++;
2903a19609f8Sjv 	ASSERT(z->zone_subsys_ref[subsys] != 0);
2904a19609f8Sjv 	list_insert_head(&z->zone_ref_list, ref);
2905a19609f8Sjv 	mutex_exit(&z->zone_lock);
2906a19609f8Sjv }
2907a19609f8Sjv 
2908a19609f8Sjv /*
2909a19609f8Sjv  * Release the zone reference represented by the specified zone_ref_t.
2910a19609f8Sjv  * The reference is invalid after it's released; however, the zone_ref_t
2911a19609f8Sjv  * structure can be reused without having to invoke zone_init_ref().
2912a19609f8Sjv  * subsys should be the same value that was passed to zone_hold_ref()
2913a19609f8Sjv  * when the reference was acquired.
2914a19609f8Sjv  */
2915a19609f8Sjv void
zone_rele_ref(zone_ref_t * ref,zone_ref_subsys_t subsys)2916a19609f8Sjv zone_rele_ref(zone_ref_t *ref, zone_ref_subsys_t subsys)
2917a19609f8Sjv {
2918a19609f8Sjv 	zone_rele_common(ref->zref_zone, ref, subsys);
2919a19609f8Sjv 
2920a19609f8Sjv 	/*
2921a19609f8Sjv 	 * Set the zone_ref_t's zref_zone field to NULL to generate panics
2922a19609f8Sjv 	 * when consumers dereference the reference.  This helps us catch
2923a19609f8Sjv 	 * consumers who use released references.  Furthermore, this lets
2924a19609f8Sjv 	 * consumers reuse the zone_ref_t structure without having to
2925a19609f8Sjv 	 * invoke zone_init_ref().
2926a19609f8Sjv 	 */
2927a19609f8Sjv 	ref->zref_zone = NULL;
2928a19609f8Sjv }
2929a19609f8Sjv 
29307c478bd9Sstevel@tonic-gate void
zone_cred_hold(zone_t * z)29317c478bd9Sstevel@tonic-gate zone_cred_hold(zone_t *z)
29327c478bd9Sstevel@tonic-gate {
29337c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
29347c478bd9Sstevel@tonic-gate 	z->zone_cred_ref++;
29357c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
29367c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
29377c478bd9Sstevel@tonic-gate }
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate void
zone_cred_rele(zone_t * z)29407c478bd9Sstevel@tonic-gate zone_cred_rele(zone_t *z)
29417c478bd9Sstevel@tonic-gate {
29427c478bd9Sstevel@tonic-gate 	boolean_t wakeup;
29437c478bd9Sstevel@tonic-gate 
29447c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
29457c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
29467c478bd9Sstevel@tonic-gate 	z->zone_cred_ref--;
29477c478bd9Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
29487c478bd9Sstevel@tonic-gate 		/* no more refs, free the structure */
29497c478bd9Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
29507c478bd9Sstevel@tonic-gate 		zone_free(z);
29517c478bd9Sstevel@tonic-gate 		return;
29527c478bd9Sstevel@tonic-gate 	}
29537c478bd9Sstevel@tonic-gate 	/*
29547c478bd9Sstevel@tonic-gate 	 * If zone_destroy is waiting for the cred references to drain
29557c478bd9Sstevel@tonic-gate 	 * out, and they have, signal it.
29567c478bd9Sstevel@tonic-gate 	 */
29577c478bd9Sstevel@tonic-gate 	wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) &&
29587c478bd9Sstevel@tonic-gate 	    zone_status_get(z) >= ZONE_IS_DEAD);
29597c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
29607c478bd9Sstevel@tonic-gate 
29617c478bd9Sstevel@tonic-gate 	if (wakeup) {
29627c478bd9Sstevel@tonic-gate 		/*
29637c478bd9Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
29647c478bd9Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
29657c478bd9Sstevel@tonic-gate 		 */
29667c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
29677c478bd9Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
29687c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
29697c478bd9Sstevel@tonic-gate 	}
29707c478bd9Sstevel@tonic-gate }
29717c478bd9Sstevel@tonic-gate 
29727c478bd9Sstevel@tonic-gate void
zone_task_hold(zone_t * z)29737c478bd9Sstevel@tonic-gate zone_task_hold(zone_t *z)
29747c478bd9Sstevel@tonic-gate {
29757c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
29767c478bd9Sstevel@tonic-gate 	z->zone_ntasks++;
29777c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_ntasks != 0);
29787c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
29797c478bd9Sstevel@tonic-gate }
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate void
zone_task_rele(zone_t * zone)29827c478bd9Sstevel@tonic-gate zone_task_rele(zone_t *zone)
29837c478bd9Sstevel@tonic-gate {
29847c478bd9Sstevel@tonic-gate 	uint_t refcnt;
29857c478bd9Sstevel@tonic-gate 
29867c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
29877c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks != 0);
29887c478bd9Sstevel@tonic-gate 	refcnt = --zone->zone_ntasks;
29897c478bd9Sstevel@tonic-gate 	if (refcnt > 1)	{	/* Common case */
29907c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
29917c478bd9Sstevel@tonic-gate 		return;
29927c478bd9Sstevel@tonic-gate 	}
29937c478bd9Sstevel@tonic-gate 	zone_hold_locked(zone);	/* so we can use the zone_t later */
29947c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
29957c478bd9Sstevel@tonic-gate 	if (refcnt == 1) {
29967c478bd9Sstevel@tonic-gate 		/*
29977c478bd9Sstevel@tonic-gate 		 * See if the zone is shutting down.
29987c478bd9Sstevel@tonic-gate 		 */
29997c478bd9Sstevel@tonic-gate 		mutex_enter(&zone_status_lock);
30007c478bd9Sstevel@tonic-gate 		if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) {
30017c478bd9Sstevel@tonic-gate 			goto out;
30027c478bd9Sstevel@tonic-gate 		}
30037c478bd9Sstevel@tonic-gate 
30047c478bd9Sstevel@tonic-gate 		/*
30057c478bd9Sstevel@tonic-gate 		 * Make sure the ntasks didn't change since we
30067c478bd9Sstevel@tonic-gate 		 * dropped zone_lock.
30077c478bd9Sstevel@tonic-gate 		 */
30087c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
30097c478bd9Sstevel@tonic-gate 		if (refcnt != zone->zone_ntasks) {
30107c478bd9Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
30117c478bd9Sstevel@tonic-gate 			goto out;
30127c478bd9Sstevel@tonic-gate 		}
30137c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
30147c478bd9Sstevel@tonic-gate 
30157c478bd9Sstevel@tonic-gate 		/*
30167c478bd9Sstevel@tonic-gate 		 * No more user processes in the zone.  The zone is empty.
30177c478bd9Sstevel@tonic-gate 		 */
30187c478bd9Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_EMPTY);
30197c478bd9Sstevel@tonic-gate 		goto out;
30207c478bd9Sstevel@tonic-gate 	}
30217c478bd9Sstevel@tonic-gate 
30227c478bd9Sstevel@tonic-gate 	ASSERT(refcnt == 0);
30237c478bd9Sstevel@tonic-gate 	/*
30247c478bd9Sstevel@tonic-gate 	 * zsched has exited; the zone is dead.
30257c478bd9Sstevel@tonic-gate 	 */
30267c478bd9Sstevel@tonic-gate 	zone->zone_zsched = NULL;		/* paranoia */
30277c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
30287c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_DEAD);
30297c478bd9Sstevel@tonic-gate out:
30307c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
30317c478bd9Sstevel@tonic-gate 	zone_rele(zone);
30327c478bd9Sstevel@tonic-gate }
30337c478bd9Sstevel@tonic-gate 
30347c478bd9Sstevel@tonic-gate zoneid_t
getzoneid(void)30357c478bd9Sstevel@tonic-gate getzoneid(void)
30367c478bd9Sstevel@tonic-gate {
30377c478bd9Sstevel@tonic-gate 	return (curproc->p_zone->zone_id);
30387c478bd9Sstevel@tonic-gate }
30397c478bd9Sstevel@tonic-gate 
30407c478bd9Sstevel@tonic-gate /*
30417c478bd9Sstevel@tonic-gate  * Internal versions of zone_find_by_*().  These don't zone_hold() or
30427c478bd9Sstevel@tonic-gate  * check the validity of a zone's state.
30437c478bd9Sstevel@tonic-gate  */
30447c478bd9Sstevel@tonic-gate static zone_t *
zone_find_all_by_id(zoneid_t zoneid)30457c478bd9Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid)
30467c478bd9Sstevel@tonic-gate {
30477c478bd9Sstevel@tonic-gate 	mod_hash_val_t hv;
30487c478bd9Sstevel@tonic-gate 	zone_t *zone = NULL;
30497c478bd9Sstevel@tonic-gate 
30507c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
30517c478bd9Sstevel@tonic-gate 
30527c478bd9Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyid,
30537c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0)
30547c478bd9Sstevel@tonic-gate 		zone = (zone_t *)hv;
30557c478bd9Sstevel@tonic-gate 	return (zone);
30567c478bd9Sstevel@tonic-gate }
30577c478bd9Sstevel@tonic-gate 
305845916cd2Sjpk static zone_t *
zone_find_all_by_label(const ts_label_t * label)305945916cd2Sjpk zone_find_all_by_label(const ts_label_t *label)
306045916cd2Sjpk {
306145916cd2Sjpk 	mod_hash_val_t hv;
306245916cd2Sjpk 	zone_t *zone = NULL;
306345916cd2Sjpk 
306445916cd2Sjpk 	ASSERT(MUTEX_HELD(&zonehash_lock));
306545916cd2Sjpk 
306645916cd2Sjpk 	/*
306745916cd2Sjpk 	 * zonehashbylabel is not maintained for unlabeled systems
306845916cd2Sjpk 	 */
306945916cd2Sjpk 	if (!is_system_labeled())
307045916cd2Sjpk 		return (NULL);
307145916cd2Sjpk 	if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0)
307245916cd2Sjpk 		zone = (zone_t *)hv;
307345916cd2Sjpk 	return (zone);
307445916cd2Sjpk }
307545916cd2Sjpk 
30767c478bd9Sstevel@tonic-gate static zone_t *
zone_find_all_by_name(char * name)30777c478bd9Sstevel@tonic-gate zone_find_all_by_name(char *name)
30787c478bd9Sstevel@tonic-gate {
30797c478bd9Sstevel@tonic-gate 	mod_hash_val_t hv;
30807c478bd9Sstevel@tonic-gate 	zone_t *zone = NULL;
30817c478bd9Sstevel@tonic-gate 
30827c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
30837c478bd9Sstevel@tonic-gate 
30847c478bd9Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0)
30857c478bd9Sstevel@tonic-gate 		zone = (zone_t *)hv;
30867c478bd9Sstevel@tonic-gate 	return (zone);
30877c478bd9Sstevel@tonic-gate }
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate /*
30907c478bd9Sstevel@tonic-gate  * Public interface for looking up a zone by zoneid.  Only returns the zone if
30917c478bd9Sstevel@tonic-gate  * it is fully initialized, and has not yet begun the zone_destroy() sequence.
30927c478bd9Sstevel@tonic-gate  * Caller must call zone_rele() once it is done with the zone.
30937c478bd9Sstevel@tonic-gate  *
30947c478bd9Sstevel@tonic-gate  * The zone may begin the zone_destroy() sequence immediately after this
30957c478bd9Sstevel@tonic-gate  * function returns, but may be safely used until zone_rele() is called.
30967c478bd9Sstevel@tonic-gate  */
30977c478bd9Sstevel@tonic-gate zone_t *
zone_find_by_id(zoneid_t zoneid)30987c478bd9Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid)
30997c478bd9Sstevel@tonic-gate {
31007c478bd9Sstevel@tonic-gate 	zone_t *zone;
31017c478bd9Sstevel@tonic-gate 	zone_status_t status;
31027c478bd9Sstevel@tonic-gate 
31037c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
31047c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
31057c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
31067c478bd9Sstevel@tonic-gate 		return (NULL);
31077c478bd9Sstevel@tonic-gate 	}
31087c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
31097c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
31107c478bd9Sstevel@tonic-gate 		/*
31117c478bd9Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
31127c478bd9Sstevel@tonic-gate 		 */
31137c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
31147c478bd9Sstevel@tonic-gate 		return (NULL);
31157c478bd9Sstevel@tonic-gate 	}
31167c478bd9Sstevel@tonic-gate 	zone_hold(zone);
31177c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
31187c478bd9Sstevel@tonic-gate 	return (zone);
31197c478bd9Sstevel@tonic-gate }
31207c478bd9Sstevel@tonic-gate 
312145916cd2Sjpk /*
312245916cd2Sjpk  * Similar to zone_find_by_id, but using zone label as the key.
312345916cd2Sjpk  */
312445916cd2Sjpk zone_t *
zone_find_by_label(const ts_label_t * label)312545916cd2Sjpk zone_find_by_label(const ts_label_t *label)
312645916cd2Sjpk {
312745916cd2Sjpk 	zone_t *zone;
312842bc57c4Srica 	zone_status_t status;
312945916cd2Sjpk 
313045916cd2Sjpk 	mutex_enter(&zonehash_lock);
313145916cd2Sjpk 	if ((zone = zone_find_all_by_label(label)) == NULL) {
313245916cd2Sjpk 		mutex_exit(&zonehash_lock);
313345916cd2Sjpk 		return (NULL);
313445916cd2Sjpk 	}
313542bc57c4Srica 
313642bc57c4Srica 	status = zone_status_get(zone);
313742bc57c4Srica 	if (status > ZONE_IS_DOWN) {
313845916cd2Sjpk 		/*
313945916cd2Sjpk 		 * For all practical purposes the zone doesn't exist.
314045916cd2Sjpk 		 */
314142bc57c4Srica 		mutex_exit(&zonehash_lock);
314242bc57c4Srica 		return (NULL);
314345916cd2Sjpk 	}
314442bc57c4Srica 	zone_hold(zone);
314545916cd2Sjpk 	mutex_exit(&zonehash_lock);
314645916cd2Sjpk 	return (zone);
314745916cd2Sjpk }
314845916cd2Sjpk 
31497c478bd9Sstevel@tonic-gate /*
31507c478bd9Sstevel@tonic-gate  * Similar to zone_find_by_id, but using zone name as the key.
31517c478bd9Sstevel@tonic-gate  */
31527c478bd9Sstevel@tonic-gate zone_t *
zone_find_by_name(char * name)31537c478bd9Sstevel@tonic-gate zone_find_by_name(char *name)
31547c478bd9Sstevel@tonic-gate {
31557c478bd9Sstevel@tonic-gate 	zone_t *zone;
31567c478bd9Sstevel@tonic-gate 	zone_status_t status;
31577c478bd9Sstevel@tonic-gate 
31587c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
31597c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_name(name)) == NULL) {
31607c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
31617c478bd9Sstevel@tonic-gate 		return (NULL);
31627c478bd9Sstevel@tonic-gate 	}
31637c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
31647c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
31657c478bd9Sstevel@tonic-gate 		/*
31667c478bd9Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
31677c478bd9Sstevel@tonic-gate 		 */
31687c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
31697c478bd9Sstevel@tonic-gate 		return (NULL);
31707c478bd9Sstevel@tonic-gate 	}
31717c478bd9Sstevel@tonic-gate 	zone_hold(zone);
31727c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
31737c478bd9Sstevel@tonic-gate 	return (zone);
31747c478bd9Sstevel@tonic-gate }
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate /*
31777c478bd9Sstevel@tonic-gate  * Similar to zone_find_by_id(), using the path as a key.  For instance,
31787c478bd9Sstevel@tonic-gate  * if there is a zone "foo" rooted at /foo/root, and the path argument
31797c478bd9Sstevel@tonic-gate  * is "/foo/root/proc", it will return the held zone_t corresponding to
31807c478bd9Sstevel@tonic-gate  * zone "foo".
31817c478bd9Sstevel@tonic-gate  *
31827c478bd9Sstevel@tonic-gate  * zone_find_by_path() always returns a non-NULL value, since at the
31837c478bd9Sstevel@tonic-gate  * very least every path will be contained in the global zone.
31847c478bd9Sstevel@tonic-gate  *
31857c478bd9Sstevel@tonic-gate  * As with the other zone_find_by_*() functions, the caller is
31867c478bd9Sstevel@tonic-gate  * responsible for zone_rele()ing the return value of this function.
31877c478bd9Sstevel@tonic-gate  */
31887c478bd9Sstevel@tonic-gate zone_t *
zone_find_by_path(const char * path)31897c478bd9Sstevel@tonic-gate zone_find_by_path(const char *path)
31907c478bd9Sstevel@tonic-gate {
31917c478bd9Sstevel@tonic-gate 	zone_t *zone;
31927c478bd9Sstevel@tonic-gate 	zone_t *zret = NULL;
31937c478bd9Sstevel@tonic-gate 	zone_status_t status;
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate 	if (path == NULL) {
31967c478bd9Sstevel@tonic-gate 		/*
31977c478bd9Sstevel@tonic-gate 		 * Call from rootconf().
31987c478bd9Sstevel@tonic-gate 		 */
31997c478bd9Sstevel@tonic-gate 		zone_hold(global_zone);
32007c478bd9Sstevel@tonic-gate 		return (global_zone);
32017c478bd9Sstevel@tonic-gate 	}
32027c478bd9Sstevel@tonic-gate 	ASSERT(*path == '/');
32037c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
32047c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
32057c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
32067c478bd9Sstevel@tonic-gate 		if (ZONE_PATH_VISIBLE(path, zone))
32077c478bd9Sstevel@tonic-gate 			zret = zone;
32087c478bd9Sstevel@tonic-gate 	}
32097c478bd9Sstevel@tonic-gate 	ASSERT(zret != NULL);
32107c478bd9Sstevel@tonic-gate 	status = zone_status_get(zret);
32117c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
32127c478bd9Sstevel@tonic-gate 		/*
32137c478bd9Sstevel@tonic-gate 		 * Zone practically doesn't exist.
32147c478bd9Sstevel@tonic-gate 		 */
32157c478bd9Sstevel@tonic-gate 		zret = global_zone;
32167c478bd9Sstevel@tonic-gate 	}
32177c478bd9Sstevel@tonic-gate 	zone_hold(zret);
32187c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
32197c478bd9Sstevel@tonic-gate 	return (zret);
32207c478bd9Sstevel@tonic-gate }
32217c478bd9Sstevel@tonic-gate 
322281d43577SJerry Jelinek /*
322381d43577SJerry Jelinek  * Public interface for updating per-zone load averages.  Called once per
322481d43577SJerry Jelinek  * second.
322581d43577SJerry Jelinek  *
322681d43577SJerry Jelinek  * Based on loadavg_update(), genloadavg() and calcloadavg() from clock.c.
322781d43577SJerry Jelinek  */
322881d43577SJerry Jelinek void
zone_loadavg_update(void)32292918c4a3SJohn Levon zone_loadavg_update(void)
323081d43577SJerry Jelinek {
323181d43577SJerry Jelinek 	zone_t *zp;
323281d43577SJerry Jelinek 	zone_status_t status;
323381d43577SJerry Jelinek 	struct loadavg_s *lavg;
323481d43577SJerry Jelinek 	hrtime_t zone_total;
32352918c4a3SJohn Levon 	uint64_t tmp;
323681d43577SJerry Jelinek 	int i;
323781d43577SJerry Jelinek 	hrtime_t hr_avg;
323881d43577SJerry Jelinek 	int nrun;
323981d43577SJerry Jelinek 	static int64_t f[3] = { 135, 27, 9 };
324081d43577SJerry Jelinek 	int64_t q, r;
324181d43577SJerry Jelinek 
324281d43577SJerry Jelinek 	mutex_enter(&zonehash_lock);
324381d43577SJerry Jelinek 	for (zp = list_head(&zone_active); zp != NULL;
324481d43577SJerry Jelinek 	    zp = list_next(&zone_active, zp)) {
324581d43577SJerry Jelinek 		mutex_enter(&zp->zone_lock);
324681d43577SJerry Jelinek 
324781d43577SJerry Jelinek 		/* Skip zones that are on the way down or not yet up */
324881d43577SJerry Jelinek 		status = zone_status_get(zp);
324981d43577SJerry Jelinek 		if (status < ZONE_IS_READY || status >= ZONE_IS_DOWN) {
325081d43577SJerry Jelinek 			/* For all practical purposes the zone doesn't exist. */
325181d43577SJerry Jelinek 			mutex_exit(&zp->zone_lock);
325281d43577SJerry Jelinek 			continue;
325381d43577SJerry Jelinek 		}
325481d43577SJerry Jelinek 
325581d43577SJerry Jelinek 		/*
325681d43577SJerry Jelinek 		 * Update the 10 second moving average data in zone_loadavg.
325781d43577SJerry Jelinek 		 */
325881d43577SJerry Jelinek 		lavg = &zp->zone_loadavg;
325981d43577SJerry Jelinek 
32602918c4a3SJohn Levon 		tmp = cpu_uarray_sum_all(zp->zone_ustate);
32612918c4a3SJohn Levon 		zone_total = UINT64_OVERFLOW_TO_INT64(tmp);
32622918c4a3SJohn Levon 
326381d43577SJerry Jelinek 		scalehrtime(&zone_total);
326481d43577SJerry Jelinek 
326581d43577SJerry Jelinek 		/* The zone_total should always be increasing. */
326681d43577SJerry Jelinek 		lavg->lg_loads[lavg->lg_cur] = (zone_total > lavg->lg_total) ?
326781d43577SJerry Jelinek 		    zone_total - lavg->lg_total : 0;
326881d43577SJerry Jelinek 		lavg->lg_cur = (lavg->lg_cur + 1) % S_LOADAVG_SZ;
326981d43577SJerry Jelinek 		/* lg_total holds the prev. 1 sec. total */
327081d43577SJerry Jelinek 		lavg->lg_total = zone_total;
327181d43577SJerry Jelinek 
327281d43577SJerry Jelinek 		/*
327381d43577SJerry Jelinek 		 * To simplify the calculation, we don't calculate the load avg.
327481d43577SJerry Jelinek 		 * until the zone has been up for at least 10 seconds and our
327581d43577SJerry Jelinek 		 * moving average is thus full.
327681d43577SJerry Jelinek 		 */
327781d43577SJerry Jelinek 		if ((lavg->lg_len + 1) < S_LOADAVG_SZ) {
327881d43577SJerry Jelinek 			lavg->lg_len++;
327981d43577SJerry Jelinek 			mutex_exit(&zp->zone_lock);
328081d43577SJerry Jelinek 			continue;
328181d43577SJerry Jelinek 		}
328281d43577SJerry Jelinek 
328381d43577SJerry Jelinek 		/* Now calculate the 1min, 5min, 15 min load avg. */
328481d43577SJerry Jelinek 		hr_avg = 0;
328581d43577SJerry Jelinek 		for (i = 0; i < S_LOADAVG_SZ; i++)
328681d43577SJerry Jelinek 			hr_avg += lavg->lg_loads[i];
328781d43577SJerry Jelinek 		hr_avg = hr_avg / S_LOADAVG_SZ;
328881d43577SJerry Jelinek 		nrun = hr_avg / (NANOSEC / LGRP_LOADAVG_IN_THREAD_MAX);
328981d43577SJerry Jelinek 
329081d43577SJerry Jelinek 		/* Compute load avg. See comment in calcloadavg() */
329181d43577SJerry Jelinek 		for (i = 0; i < 3; i++) {
329281d43577SJerry Jelinek 			q = (zp->zone_hp_avenrun[i] >> 16) << 7;
329381d43577SJerry Jelinek 			r = (zp->zone_hp_avenrun[i] & 0xffff) << 7;
329481d43577SJerry Jelinek 			zp->zone_hp_avenrun[i] +=
329581d43577SJerry Jelinek 			    ((nrun - q) * f[i] - ((r * f[i]) >> 16)) >> 4;
329681d43577SJerry Jelinek 
329781d43577SJerry Jelinek 			/* avenrun[] can only hold 31 bits of load avg. */
329881d43577SJerry Jelinek 			if (zp->zone_hp_avenrun[i] <
329981d43577SJerry Jelinek 			    ((uint64_t)1<<(31+16-FSHIFT)))
330081d43577SJerry Jelinek 				zp->zone_avenrun[i] = (int32_t)
330181d43577SJerry Jelinek 				    (zp->zone_hp_avenrun[i] >> (16 - FSHIFT));
330281d43577SJerry Jelinek 			else
330381d43577SJerry Jelinek 				zp->zone_avenrun[i] = 0x7fffffff;
330481d43577SJerry Jelinek 		}
330581d43577SJerry Jelinek 
330681d43577SJerry Jelinek 		mutex_exit(&zp->zone_lock);
330781d43577SJerry Jelinek 	}
330881d43577SJerry Jelinek 	mutex_exit(&zonehash_lock);
330981d43577SJerry Jelinek }
331081d43577SJerry Jelinek 
33117c478bd9Sstevel@tonic-gate /*
33127c478bd9Sstevel@tonic-gate  * Get the number of cpus visible to this zone.  The system-wide global
33137c478bd9Sstevel@tonic-gate  * 'ncpus' is returned if pools are disabled, the caller is in the
33147c478bd9Sstevel@tonic-gate  * global zone, or a NULL zone argument is passed in.
33157c478bd9Sstevel@tonic-gate  */
33167c478bd9Sstevel@tonic-gate int
zone_ncpus_get(zone_t * zone)33177c478bd9Sstevel@tonic-gate zone_ncpus_get(zone_t *zone)
33187c478bd9Sstevel@tonic-gate {
33197c478bd9Sstevel@tonic-gate 	int myncpus = zone == NULL ? 0 : zone->zone_ncpus;
33207c478bd9Sstevel@tonic-gate 
33217c478bd9Sstevel@tonic-gate 	return (myncpus != 0 ? myncpus : ncpus);
33227c478bd9Sstevel@tonic-gate }
33237c478bd9Sstevel@tonic-gate 
33247c478bd9Sstevel@tonic-gate /*
33257c478bd9Sstevel@tonic-gate  * Get the number of online cpus visible to this zone.  The system-wide
33267c478bd9Sstevel@tonic-gate  * global 'ncpus_online' is returned if pools are disabled, the caller
33277c478bd9Sstevel@tonic-gate  * is in the global zone, or a NULL zone argument is passed in.
33287c478bd9Sstevel@tonic-gate  */
33297c478bd9Sstevel@tonic-gate int
zone_ncpus_online_get(zone_t * zone)33307c478bd9Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone)
33317c478bd9Sstevel@tonic-gate {
33327c478bd9Sstevel@tonic-gate 	int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online;
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 	return (myncpus_online != 0 ? myncpus_online : ncpus_online);
33357c478bd9Sstevel@tonic-gate }
33367c478bd9Sstevel@tonic-gate 
33377c478bd9Sstevel@tonic-gate /*
33387c478bd9Sstevel@tonic-gate  * Return the pool to which the zone is currently bound.
33397c478bd9Sstevel@tonic-gate  */
33407c478bd9Sstevel@tonic-gate pool_t *
zone_pool_get(zone_t * zone)33417c478bd9Sstevel@tonic-gate zone_pool_get(zone_t *zone)
33427c478bd9Sstevel@tonic-gate {
33437c478bd9Sstevel@tonic-gate 	ASSERT(pool_lock_held());
33447c478bd9Sstevel@tonic-gate 
33457c478bd9Sstevel@tonic-gate 	return (zone->zone_pool);
33467c478bd9Sstevel@tonic-gate }
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate /*
33497c478bd9Sstevel@tonic-gate  * Set the zone's pool pointer and update the zone's visibility to match
33507c478bd9Sstevel@tonic-gate  * the resources in the new pool.
33517c478bd9Sstevel@tonic-gate  */
33527c478bd9Sstevel@tonic-gate void
zone_pool_set(zone_t * zone,pool_t * pool)33537c478bd9Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool)
33547c478bd9Sstevel@tonic-gate {
33557c478bd9Sstevel@tonic-gate 	ASSERT(pool_lock_held());
33567c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
33577c478bd9Sstevel@tonic-gate 
33587c478bd9Sstevel@tonic-gate 	zone->zone_pool = pool;
33597c478bd9Sstevel@tonic-gate 	zone_pset_set(zone, pool->pool_pset->pset_id);
33607c478bd9Sstevel@tonic-gate }
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate /*
33637c478bd9Sstevel@tonic-gate  * Return the cached value of the id of the processor set to which the
33647c478bd9Sstevel@tonic-gate  * zone is currently bound.  The value will be ZONE_PS_INVAL if the pools
33657c478bd9Sstevel@tonic-gate  * facility is disabled.
33667c478bd9Sstevel@tonic-gate  */
33677c478bd9Sstevel@tonic-gate psetid_t
zone_pset_get(zone_t * zone)33687c478bd9Sstevel@tonic-gate zone_pset_get(zone_t *zone)
33697c478bd9Sstevel@tonic-gate {
33707c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
33717c478bd9Sstevel@tonic-gate 
33727c478bd9Sstevel@tonic-gate 	return (zone->zone_psetid);
33737c478bd9Sstevel@tonic-gate }
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate /*
33767c478bd9Sstevel@tonic-gate  * Set the cached value of the id of the processor set to which the zone
33777c478bd9Sstevel@tonic-gate  * is currently bound.  Also update the zone's visibility to match the
33787c478bd9Sstevel@tonic-gate  * resources in the new processor set.
33797c478bd9Sstevel@tonic-gate  */
33807c478bd9Sstevel@tonic-gate void
zone_pset_set(zone_t * zone,psetid_t newpsetid)33817c478bd9Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid)
33827c478bd9Sstevel@tonic-gate {
33837c478bd9Sstevel@tonic-gate 	psetid_t oldpsetid;
33847c478bd9Sstevel@tonic-gate 
33857c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
33867c478bd9Sstevel@tonic-gate 	oldpsetid = zone_pset_get(zone);
33877c478bd9Sstevel@tonic-gate 
33887c478bd9Sstevel@tonic-gate 	if (oldpsetid == newpsetid)
33897c478bd9Sstevel@tonic-gate 		return;
33907c478bd9Sstevel@tonic-gate 	/*
33917c478bd9Sstevel@tonic-gate 	 * Global zone sees all.
33927c478bd9Sstevel@tonic-gate 	 */
33937c478bd9Sstevel@tonic-gate 	if (zone != global_zone) {
33947c478bd9Sstevel@tonic-gate 		zone->zone_psetid = newpsetid;
33957c478bd9Sstevel@tonic-gate 		if (newpsetid != ZONE_PS_INVAL)
33967c478bd9Sstevel@tonic-gate 			pool_pset_visibility_add(newpsetid, zone);
33977c478bd9Sstevel@tonic-gate 		if (oldpsetid != ZONE_PS_INVAL)
33987c478bd9Sstevel@tonic-gate 			pool_pset_visibility_remove(oldpsetid, zone);
33997c478bd9Sstevel@tonic-gate 	}
34007c478bd9Sstevel@tonic-gate 	/*
34017c478bd9Sstevel@tonic-gate 	 * Disabling pools, so we should start using the global values
34027c478bd9Sstevel@tonic-gate 	 * for ncpus and ncpus_online.
34037c478bd9Sstevel@tonic-gate 	 */
34047c478bd9Sstevel@tonic-gate 	if (newpsetid == ZONE_PS_INVAL) {
34057c478bd9Sstevel@tonic-gate 		zone->zone_ncpus = 0;
34067c478bd9Sstevel@tonic-gate 		zone->zone_ncpus_online = 0;
34077c478bd9Sstevel@tonic-gate 	}
34087c478bd9Sstevel@tonic-gate }
34097c478bd9Sstevel@tonic-gate 
34107c478bd9Sstevel@tonic-gate /*
34117c478bd9Sstevel@tonic-gate  * Walk the list of active zones and issue the provided callback for
34127c478bd9Sstevel@tonic-gate  * each of them.
34137c478bd9Sstevel@tonic-gate  *
34147c478bd9Sstevel@tonic-gate  * Caller must not be holding any locks that may be acquired under
34157c478bd9Sstevel@tonic-gate  * zonehash_lock.  See comment at the beginning of the file for a list of
34167c478bd9Sstevel@tonic-gate  * common locks and their interactions with zones.
34177c478bd9Sstevel@tonic-gate  */
34187c478bd9Sstevel@tonic-gate int
zone_walk(int (* cb)(zone_t *,void *),void * data)34197c478bd9Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data)
34207c478bd9Sstevel@tonic-gate {
34217c478bd9Sstevel@tonic-gate 	zone_t *zone;
34227c478bd9Sstevel@tonic-gate 	int ret = 0;
34237c478bd9Sstevel@tonic-gate 	zone_status_t status;
34247c478bd9Sstevel@tonic-gate 
34257c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
34267c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
34277c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
34287c478bd9Sstevel@tonic-gate 		/*
34297c478bd9Sstevel@tonic-gate 		 * Skip zones that shouldn't be externally visible.
34307c478bd9Sstevel@tonic-gate 		 */
34317c478bd9Sstevel@tonic-gate 		status = zone_status_get(zone);
34327c478bd9Sstevel@tonic-gate 		if (status < ZONE_IS_READY || status > ZONE_IS_DOWN)
34337c478bd9Sstevel@tonic-gate 			continue;
34347c478bd9Sstevel@tonic-gate 		/*
34357c478bd9Sstevel@tonic-gate 		 * Bail immediately if any callback invocation returns a
34367c478bd9Sstevel@tonic-gate 		 * non-zero value.
34377c478bd9Sstevel@tonic-gate 		 */
34387c478bd9Sstevel@tonic-gate 		ret = (*cb)(zone, data);
34397c478bd9Sstevel@tonic-gate 		if (ret != 0)
34407c478bd9Sstevel@tonic-gate 			break;
34417c478bd9Sstevel@tonic-gate 	}
34427c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
34437c478bd9Sstevel@tonic-gate 	return (ret);
34447c478bd9Sstevel@tonic-gate }
34457c478bd9Sstevel@tonic-gate 
34467c478bd9Sstevel@tonic-gate static int
zone_set_root(zone_t * zone,const char * upath)34477c478bd9Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath)
34487c478bd9Sstevel@tonic-gate {
34497c478bd9Sstevel@tonic-gate 	vnode_t *vp;
34507c478bd9Sstevel@tonic-gate 	int trycount;
34517c478bd9Sstevel@tonic-gate 	int error = 0;
34527c478bd9Sstevel@tonic-gate 	char *path;
34537c478bd9Sstevel@tonic-gate 	struct pathname upn, pn;
34547c478bd9Sstevel@tonic-gate 	size_t pathlen;
34557c478bd9Sstevel@tonic-gate 
34567c478bd9Sstevel@tonic-gate 	if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0)
34577c478bd9Sstevel@tonic-gate 		return (error);
34587c478bd9Sstevel@tonic-gate 
34597c478bd9Sstevel@tonic-gate 	pn_alloc(&pn);
34607c478bd9Sstevel@tonic-gate 
34617c478bd9Sstevel@tonic-gate 	/* prevent infinite loop */
34627c478bd9Sstevel@tonic-gate 	trycount = 10;
34637c478bd9Sstevel@tonic-gate 	for (;;) {
34647c478bd9Sstevel@tonic-gate 		if (--trycount <= 0) {
34657c478bd9Sstevel@tonic-gate 			error = ESTALE;
34667c478bd9Sstevel@tonic-gate 			goto out;
34677c478bd9Sstevel@tonic-gate 		}
34687c478bd9Sstevel@tonic-gate 
34697c478bd9Sstevel@tonic-gate 		if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) {
34707c478bd9Sstevel@tonic-gate 			/*
34717c478bd9Sstevel@tonic-gate 			 * VOP_ACCESS() may cover 'vp' with a new
34727c478bd9Sstevel@tonic-gate 			 * filesystem, if 'vp' is an autoFS vnode.
34737c478bd9Sstevel@tonic-gate 			 * Get the new 'vp' if so.
34747c478bd9Sstevel@tonic-gate 			 */
3475da6c28aaSamw 			if ((error =
3476da6c28aaSamw 			    VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL)) == 0 &&
347725d2dc23Seh 			    (!vn_ismntpt(vp) ||
34787c478bd9Sstevel@tonic-gate 			    (error = traverse(&vp)) == 0)) {
34797c478bd9Sstevel@tonic-gate 				pathlen = pn.pn_pathlen + 2;
34807c478bd9Sstevel@tonic-gate 				path = kmem_alloc(pathlen, KM_SLEEP);
34817c478bd9Sstevel@tonic-gate 				(void) strncpy(path, pn.pn_path,
34827c478bd9Sstevel@tonic-gate 				    pn.pn_pathlen + 1);
34837c478bd9Sstevel@tonic-gate 				path[pathlen - 2] = '/';
34847c478bd9Sstevel@tonic-gate 				path[pathlen - 1] = '\0';
34857c478bd9Sstevel@tonic-gate 				pn_free(&pn);
34867c478bd9Sstevel@tonic-gate 				pn_free(&upn);
34877c478bd9Sstevel@tonic-gate 
34887c478bd9Sstevel@tonic-gate 				/* Success! */
34897c478bd9Sstevel@tonic-gate 				break;
34907c478bd9Sstevel@tonic-gate 			}
34917c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
34927c478bd9Sstevel@tonic-gate 		}
34937c478bd9Sstevel@tonic-gate 		if (error != ESTALE)
34947c478bd9Sstevel@tonic-gate 			goto out;
34957c478bd9Sstevel@tonic-gate 	}
34967c478bd9Sstevel@tonic-gate 
34977c478bd9Sstevel@tonic-gate 	ASSERT(error == 0);
34987c478bd9Sstevel@tonic-gate 	zone->zone_rootvp = vp;		/* we hold a reference to vp */
34997c478bd9Sstevel@tonic-gate 	zone->zone_rootpath = path;
35007c478bd9Sstevel@tonic-gate 	zone->zone_rootpathlen = pathlen;
350148451833Scarlsonj 	if (pathlen > 5 && strcmp(path + pathlen - 5, "/lu/") == 0)
350248451833Scarlsonj 		zone->zone_flags |= ZF_IS_SCRATCH;
35037c478bd9Sstevel@tonic-gate 	return (0);
35047c478bd9Sstevel@tonic-gate 
35057c478bd9Sstevel@tonic-gate out:
35067c478bd9Sstevel@tonic-gate 	pn_free(&pn);
35077c478bd9Sstevel@tonic-gate 	pn_free(&upn);
35087c478bd9Sstevel@tonic-gate 	return (error);
35097c478bd9Sstevel@tonic-gate }
35107c478bd9Sstevel@tonic-gate 
35117c478bd9Sstevel@tonic-gate #define	isalnum(c)	(((c) >= '0' && (c) <= '9') || \
35127c478bd9Sstevel@tonic-gate 			((c) >= 'a' && (c) <= 'z') || \
35137c478bd9Sstevel@tonic-gate 			((c) >= 'A' && (c) <= 'Z'))
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate static int
zone_set_name(zone_t * zone,const char * uname)35167c478bd9Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname)
35177c478bd9Sstevel@tonic-gate {
35187c478bd9Sstevel@tonic-gate 	char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
35197c478bd9Sstevel@tonic-gate 	size_t len;
35207c478bd9Sstevel@tonic-gate 	int i, err;
35217c478bd9Sstevel@tonic-gate 
35227c478bd9Sstevel@tonic-gate 	if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) {
35237c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
35247c478bd9Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
35257c478bd9Sstevel@tonic-gate 	}
35267c478bd9Sstevel@tonic-gate 
35277c478bd9Sstevel@tonic-gate 	/* must be less than ZONENAME_MAX */
35287c478bd9Sstevel@tonic-gate 	if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') {
35297c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
35307c478bd9Sstevel@tonic-gate 		return (EINVAL);
35317c478bd9Sstevel@tonic-gate 	}
35327c478bd9Sstevel@tonic-gate 
35337c478bd9Sstevel@tonic-gate 	/*
35347c478bd9Sstevel@tonic-gate 	 * Name must start with an alphanumeric and must contain only
35357c478bd9Sstevel@tonic-gate 	 * alphanumerics, '-', '_' and '.'.
35367c478bd9Sstevel@tonic-gate 	 */
35377c478bd9Sstevel@tonic-gate 	if (!isalnum(kname[0])) {
35387c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
35397c478bd9Sstevel@tonic-gate 		return (EINVAL);
35407c478bd9Sstevel@tonic-gate 	}
35417c478bd9Sstevel@tonic-gate 	for (i = 1; i < len - 1; i++) {
35427c478bd9Sstevel@tonic-gate 		if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' &&
35437c478bd9Sstevel@tonic-gate 		    kname[i] != '.') {
35447c478bd9Sstevel@tonic-gate 			kmem_free(kname, ZONENAME_MAX);
35457c478bd9Sstevel@tonic-gate 			return (EINVAL);
35467c478bd9Sstevel@tonic-gate 		}
35477c478bd9Sstevel@tonic-gate 	}
35487c478bd9Sstevel@tonic-gate 
35497c478bd9Sstevel@tonic-gate 	zone->zone_name = kname;
35507c478bd9Sstevel@tonic-gate 	return (0);
35517c478bd9Sstevel@tonic-gate }
35527c478bd9Sstevel@tonic-gate 
35535679c89fSjv /*
35545679c89fSjv  * Gets the 32-bit hostid of the specified zone as an unsigned int.  If 'zonep'
35555679c89fSjv  * is NULL or it points to a zone with no hostid emulation, then the machine's
35565679c89fSjv  * hostid (i.e., the global zone's hostid) is returned.  This function returns
35575679c89fSjv  * zero if neither the zone nor the host machine (global zone) have hostids.  It
35585679c89fSjv  * returns HW_INVALID_HOSTID if the function attempts to return the machine's
35595679c89fSjv  * hostid and the machine's hostid is invalid.
35605679c89fSjv  */
35615679c89fSjv uint32_t
zone_get_hostid(zone_t * zonep)35625679c89fSjv zone_get_hostid(zone_t *zonep)
35635679c89fSjv {
35645679c89fSjv 	unsigned long machine_hostid;
35655679c89fSjv 
35665679c89fSjv 	if (zonep == NULL || zonep->zone_hostid == HW_INVALID_HOSTID) {
35675679c89fSjv 		if (ddi_strtoul(hw_serial, NULL, 10, &machine_hostid) != 0)
35685679c89fSjv 			return (HW_INVALID_HOSTID);
35695679c89fSjv 		return ((uint32_t)machine_hostid);
35705679c89fSjv 	}
35715679c89fSjv 	return (zonep->zone_hostid);
35725679c89fSjv }
35735679c89fSjv 
35747c478bd9Sstevel@tonic-gate /*
35757c478bd9Sstevel@tonic-gate  * Similar to thread_create(), but makes sure the thread is in the appropriate
35767c478bd9Sstevel@tonic-gate  * zone's zsched process (curproc->p_zone->zone_zsched) before returning.
35777c478bd9Sstevel@tonic-gate  */
35787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
35797c478bd9Sstevel@tonic-gate kthread_t *
zthread_create(caddr_t stk,size_t stksize,void (* proc)(),void * arg,size_t len,pri_t pri)35807c478bd9Sstevel@tonic-gate zthread_create(
35817c478bd9Sstevel@tonic-gate     caddr_t stk,
35827c478bd9Sstevel@tonic-gate     size_t stksize,
35837c478bd9Sstevel@tonic-gate     void (*proc)(),
35847c478bd9Sstevel@tonic-gate     void *arg,
35857c478bd9Sstevel@tonic-gate     size_t len,
35867c478bd9Sstevel@tonic-gate     pri_t pri)
35877c478bd9Sstevel@tonic-gate {
35887c478bd9Sstevel@tonic-gate 	kthread_t *t;
35897c478bd9Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
35907c478bd9Sstevel@tonic-gate 	proc_t *pp = zone->zone_zsched;
35917c478bd9Sstevel@tonic-gate 
35927c478bd9Sstevel@tonic-gate 	zone_hold(zone);	/* Reference to be dropped when thread exits */
35937c478bd9Sstevel@tonic-gate 
35947c478bd9Sstevel@tonic-gate 	/*
35957c478bd9Sstevel@tonic-gate 	 * No-one should be trying to create threads if the zone is shutting
35967c478bd9Sstevel@tonic-gate 	 * down and there aren't any kernel threads around.  See comment
35977c478bd9Sstevel@tonic-gate 	 * in zthread_exit().
35987c478bd9Sstevel@tonic-gate 	 */
35997c478bd9Sstevel@tonic-gate 	ASSERT(!(zone->zone_kthreads == NULL &&
36007c478bd9Sstevel@tonic-gate 	    zone_status_get(zone) >= ZONE_IS_EMPTY));
36017c478bd9Sstevel@tonic-gate 	/*
36027c478bd9Sstevel@tonic-gate 	 * Create a thread, but don't let it run until we've finished setting
36037c478bd9Sstevel@tonic-gate 	 * things up.
36047c478bd9Sstevel@tonic-gate 	 */
36057c478bd9Sstevel@tonic-gate 	t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri);
36067c478bd9Sstevel@tonic-gate 	ASSERT(t->t_forw == NULL);
36077c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
36087c478bd9Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL) {
36097c478bd9Sstevel@tonic-gate 		t->t_forw = t->t_back = t;
36107c478bd9Sstevel@tonic-gate 	} else {
36117c478bd9Sstevel@tonic-gate 		kthread_t *tx = zone->zone_kthreads;
36127c478bd9Sstevel@tonic-gate 
36137c478bd9Sstevel@tonic-gate 		t->t_forw = tx;
36147c478bd9Sstevel@tonic-gate 		t->t_back = tx->t_back;
36157c478bd9Sstevel@tonic-gate 		tx->t_back->t_forw = t;
36167c478bd9Sstevel@tonic-gate 		tx->t_back = t;
36177c478bd9Sstevel@tonic-gate 	}
36187c478bd9Sstevel@tonic-gate 	zone->zone_kthreads = t;
36197c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
36227c478bd9Sstevel@tonic-gate 	t->t_proc_flag |= TP_ZTHREAD;
36237c478bd9Sstevel@tonic-gate 	project_rele(t->t_proj);
36247c478bd9Sstevel@tonic-gate 	t->t_proj = project_hold(pp->p_task->tk_proj);
36257c478bd9Sstevel@tonic-gate 
36267c478bd9Sstevel@tonic-gate 	/*
36277c478bd9Sstevel@tonic-gate 	 * Setup complete, let it run.
36287c478bd9Sstevel@tonic-gate 	 */
36297c478bd9Sstevel@tonic-gate 	thread_lock(t);
36307c478bd9Sstevel@tonic-gate 	t->t_schedflag |= TS_ALLSTART;
36317c478bd9Sstevel@tonic-gate 	setrun_locked(t);
36327c478bd9Sstevel@tonic-gate 	thread_unlock(t);
36337c478bd9Sstevel@tonic-gate 
36347c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
36357c478bd9Sstevel@tonic-gate 
36367c478bd9Sstevel@tonic-gate 	return (t);
36377c478bd9Sstevel@tonic-gate }
36387c478bd9Sstevel@tonic-gate 
36397c478bd9Sstevel@tonic-gate /*
36407c478bd9Sstevel@tonic-gate  * Similar to thread_exit().  Must be called by threads created via
36417c478bd9Sstevel@tonic-gate  * zthread_exit().
36427c478bd9Sstevel@tonic-gate  */
36437c478bd9Sstevel@tonic-gate void
zthread_exit(void)36447c478bd9Sstevel@tonic-gate zthread_exit(void)
36457c478bd9Sstevel@tonic-gate {
36467c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
36477c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
36487c478bd9Sstevel@tonic-gate 	zone_t *zone = pp->p_zone;
36497c478bd9Sstevel@tonic-gate 
36507c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
36517c478bd9Sstevel@tonic-gate 
36527c478bd9Sstevel@tonic-gate 	/*
36537c478bd9Sstevel@tonic-gate 	 * Reparent to p0
36547c478bd9Sstevel@tonic-gate 	 */
3655b4b07f87Sjosephb 	kpreempt_disable();
36567c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
36577c478bd9Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_ZTHREAD;
36587c478bd9Sstevel@tonic-gate 	t->t_procp = &p0;
36597c478bd9Sstevel@tonic-gate 	hat_thread_exit(t);
36607c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
3661b4b07f87Sjosephb 	kpreempt_enable();
36627c478bd9Sstevel@tonic-gate 
36637c478bd9Sstevel@tonic-gate 	if (t->t_back == t) {
36647c478bd9Sstevel@tonic-gate 		ASSERT(t->t_forw == t);
36657c478bd9Sstevel@tonic-gate 		/*
36667c478bd9Sstevel@tonic-gate 		 * If the zone is empty, once the thread count
36677c478bd9Sstevel@tonic-gate 		 * goes to zero no further kernel threads can be
36687c478bd9Sstevel@tonic-gate 		 * created.  This is because if the creator is a process
36697c478bd9Sstevel@tonic-gate 		 * in the zone, then it must have exited before the zone
36707c478bd9Sstevel@tonic-gate 		 * state could be set to ZONE_IS_EMPTY.
36717c478bd9Sstevel@tonic-gate 		 * Otherwise, if the creator is a kernel thread in the
36727c478bd9Sstevel@tonic-gate 		 * zone, the thread count is non-zero.
36737c478bd9Sstevel@tonic-gate 		 *
36747c478bd9Sstevel@tonic-gate 		 * This really means that non-zone kernel threads should
36757c478bd9Sstevel@tonic-gate 		 * not create zone kernel threads.
36767c478bd9Sstevel@tonic-gate 		 */
36777c478bd9Sstevel@tonic-gate 		zone->zone_kthreads = NULL;
36787c478bd9Sstevel@tonic-gate 		if (zone_status_get(zone) == ZONE_IS_EMPTY) {
36797c478bd9Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_DOWN);
3680c97ad5cdSakolb 			/*
3681c97ad5cdSakolb 			 * Remove any CPU caps on this zone.
3682c97ad5cdSakolb 			 */
3683c97ad5cdSakolb 			cpucaps_zone_remove(zone);
36847c478bd9Sstevel@tonic-gate 		}
36857c478bd9Sstevel@tonic-gate 	} else {
36867c478bd9Sstevel@tonic-gate 		t->t_forw->t_back = t->t_back;
36877c478bd9Sstevel@tonic-gate 		t->t_back->t_forw = t->t_forw;
36887c478bd9Sstevel@tonic-gate 		if (zone->zone_kthreads == t)
36897c478bd9Sstevel@tonic-gate 			zone->zone_kthreads = t->t_forw;
36907c478bd9Sstevel@tonic-gate 	}
36917c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
36927c478bd9Sstevel@tonic-gate 	zone_rele(zone);
36937c478bd9Sstevel@tonic-gate 	thread_exit();
36947c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
36957c478bd9Sstevel@tonic-gate }
36967c478bd9Sstevel@tonic-gate 
36977c478bd9Sstevel@tonic-gate static void
zone_chdir(vnode_t * vp,vnode_t ** vpp,proc_t * pp)36987c478bd9Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp)
36997c478bd9Sstevel@tonic-gate {
37007c478bd9Sstevel@tonic-gate 	vnode_t *oldvp;
37017c478bd9Sstevel@tonic-gate 
37027c478bd9Sstevel@tonic-gate 	/* we're going to hold a reference here to the directory */
37037c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
37047c478bd9Sstevel@tonic-gate 
3705005d3febSMarek Pospisil 	/* update abs cwd/root path see c2/audit.c */
3706005d3febSMarek Pospisil 	if (AU_AUDITING())
37077c478bd9Sstevel@tonic-gate 		audit_chdirec(vp, vpp);
37087c478bd9Sstevel@tonic-gate 
37097c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
37107c478bd9Sstevel@tonic-gate 	oldvp = *vpp;
37117c478bd9Sstevel@tonic-gate 	*vpp = vp;
37127c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
37137c478bd9Sstevel@tonic-gate 	if (oldvp != NULL)
37147c478bd9Sstevel@tonic-gate 		VN_RELE(oldvp);
37157c478bd9Sstevel@tonic-gate }
37167c478bd9Sstevel@tonic-gate 
37177c478bd9Sstevel@tonic-gate /*
37187c478bd9Sstevel@tonic-gate  * Convert an rctl value represented by an nvlist_t into an rctl_val_t.
37197c478bd9Sstevel@tonic-gate  */
37207c478bd9Sstevel@tonic-gate static int
nvlist2rctlval(nvlist_t * nvl,rctl_val_t * rv)37217c478bd9Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv)
37227c478bd9Sstevel@tonic-gate {
37237c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
37247c478bd9Sstevel@tonic-gate 	boolean_t priv_set = B_FALSE;
37257c478bd9Sstevel@tonic-gate 	boolean_t limit_set = B_FALSE;
37267c478bd9Sstevel@tonic-gate 	boolean_t action_set = B_FALSE;
37277c478bd9Sstevel@tonic-gate 
37287c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
37297c478bd9Sstevel@tonic-gate 		const char *name;
37307c478bd9Sstevel@tonic-gate 		uint64_t ui64;
37317c478bd9Sstevel@tonic-gate 
37327c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
37337c478bd9Sstevel@tonic-gate 		if (nvpair_type(nvp) != DATA_TYPE_UINT64)
37347c478bd9Sstevel@tonic-gate 			return (EINVAL);
37357c478bd9Sstevel@tonic-gate 		(void) nvpair_value_uint64(nvp, &ui64);
37367c478bd9Sstevel@tonic-gate 		if (strcmp(name, "privilege") == 0) {
37377c478bd9Sstevel@tonic-gate 			/*
37387c478bd9Sstevel@tonic-gate 			 * Currently only privileged values are allowed, but
37397c478bd9Sstevel@tonic-gate 			 * this may change in the future.
37407c478bd9Sstevel@tonic-gate 			 */
37417c478bd9Sstevel@tonic-gate 			if (ui64 != RCPRIV_PRIVILEGED)
37427c478bd9Sstevel@tonic-gate 				return (EINVAL);
37437c478bd9Sstevel@tonic-gate 			rv->rcv_privilege = ui64;
37447c478bd9Sstevel@tonic-gate 			priv_set = B_TRUE;
37457c478bd9Sstevel@tonic-gate 		} else if (strcmp(name, "limit") == 0) {
37467c478bd9Sstevel@tonic-gate 			rv->rcv_value = ui64;
37477c478bd9Sstevel@tonic-gate 			limit_set = B_TRUE;
37487c478bd9Sstevel@tonic-gate 		} else if (strcmp(name, "action") == 0) {
37497c478bd9Sstevel@tonic-gate 			if (ui64 != RCTL_LOCAL_NOACTION &&
37507c478bd9Sstevel@tonic-gate 			    ui64 != RCTL_LOCAL_DENY)
37517c478bd9Sstevel@tonic-gate 				return (EINVAL);
37527c478bd9Sstevel@tonic-gate 			rv->rcv_flagaction = ui64;
37537c478bd9Sstevel@tonic-gate 			action_set = B_TRUE;
37547c478bd9Sstevel@tonic-gate 		} else {
37557c478bd9Sstevel@tonic-gate 			return (EINVAL);
37567c478bd9Sstevel@tonic-gate 		}
37577c478bd9Sstevel@tonic-gate 	}
37587c478bd9Sstevel@tonic-gate 
37597c478bd9Sstevel@tonic-gate 	if (!(priv_set && limit_set && action_set))
37607c478bd9Sstevel@tonic-gate 		return (EINVAL);
37617c478bd9Sstevel@tonic-gate 	rv->rcv_action_signal = 0;
37627c478bd9Sstevel@tonic-gate 	rv->rcv_action_recipient = NULL;
37637c478bd9Sstevel@tonic-gate 	rv->rcv_action_recip_pid = -1;
37647c478bd9Sstevel@tonic-gate 	rv->rcv_firing_time = 0;
37657c478bd9Sstevel@tonic-gate 
37667c478bd9Sstevel@tonic-gate 	return (0);
37677c478bd9Sstevel@tonic-gate }
37687c478bd9Sstevel@tonic-gate 
37693f2f09c1Sdp /*
37703f2f09c1Sdp  * Non-global zone version of start_init.
37713f2f09c1Sdp  */
37727c478bd9Sstevel@tonic-gate void
zone_start_init(void)37733f2f09c1Sdp zone_start_init(void)
37747c478bd9Sstevel@tonic-gate {
37757c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
37769acbbeafSnn 	zone_t *z = p->p_zone;
37777c478bd9Sstevel@tonic-gate 
37783f2f09c1Sdp 	ASSERT(!INGLOBALZONE(curproc));
37797c478bd9Sstevel@tonic-gate 
37809acbbeafSnn 	/*
37819acbbeafSnn 	 * For all purposes (ZONE_ATTR_INITPID and restart_init),
37829acbbeafSnn 	 * storing just the pid of init is sufficient.
37839acbbeafSnn 	 */
37849acbbeafSnn 	z->zone_proc_initpid = p->p_pid;
37859acbbeafSnn 
37867c478bd9Sstevel@tonic-gate 	/*
37873f2f09c1Sdp 	 * We maintain zone_boot_err so that we can return the cause of the
37883f2f09c1Sdp 	 * failure back to the caller of the zone_boot syscall.
37897c478bd9Sstevel@tonic-gate 	 */
37903f2f09c1Sdp 	p->p_zone->zone_boot_err = start_init_common();
37917c478bd9Sstevel@tonic-gate 
37928f983ab3Sjv 	/*
37938f983ab3Sjv 	 * We will prevent booting zones from becoming running zones if the
37948f983ab3Sjv 	 * global zone is shutting down.
37958f983ab3Sjv 	 */
37967c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
37978f983ab3Sjv 	if (z->zone_boot_err != 0 || zone_status_get(global_zone) >=
37988f983ab3Sjv 	    ZONE_IS_SHUTTING_DOWN) {
37997c478bd9Sstevel@tonic-gate 		/*
38007c478bd9Sstevel@tonic-gate 		 * Make sure we are still in the booting state-- we could have
38017c478bd9Sstevel@tonic-gate 		 * raced and already be shutting down, or even further along.
38027c478bd9Sstevel@tonic-gate 		 */
3803c97ad5cdSakolb 		if (zone_status_get(z) == ZONE_IS_BOOTING) {
38049acbbeafSnn 			zone_status_set(z, ZONE_IS_SHUTTING_DOWN);
3805c97ad5cdSakolb 		}
38067c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
38077c478bd9Sstevel@tonic-gate 		/* It's gone bad, dispose of the process */
38089acbbeafSnn 		if (proc_exit(CLD_EXITED, z->zone_boot_err) != 0) {
380997eda132Sraf 			mutex_enter(&p->p_lock);
381097eda132Sraf 			ASSERT(p->p_flag & SEXITLWPS);
38117c478bd9Sstevel@tonic-gate 			lwp_exit();
38127c478bd9Sstevel@tonic-gate 		}
38137c478bd9Sstevel@tonic-gate 	} else {
38149acbbeafSnn 		if (zone_status_get(z) == ZONE_IS_BOOTING)
38159acbbeafSnn 			zone_status_set(z, ZONE_IS_RUNNING);
38167c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
38177c478bd9Sstevel@tonic-gate 		/* cause the process to return to userland. */
38187c478bd9Sstevel@tonic-gate 		lwp_rtt();
38197c478bd9Sstevel@tonic-gate 	}
38207c478bd9Sstevel@tonic-gate }
38217c478bd9Sstevel@tonic-gate 
38227c478bd9Sstevel@tonic-gate struct zsched_arg {
38237c478bd9Sstevel@tonic-gate 	zone_t *zone;
38247c478bd9Sstevel@tonic-gate 	nvlist_t *nvlist;
38257c478bd9Sstevel@tonic-gate };
38267c478bd9Sstevel@tonic-gate 
38277c478bd9Sstevel@tonic-gate /*
38287c478bd9Sstevel@tonic-gate  * Per-zone "sched" workalike.  The similarity to "sched" doesn't have
38297c478bd9Sstevel@tonic-gate  * anything to do with scheduling, but rather with the fact that
38307c478bd9Sstevel@tonic-gate  * per-zone kernel threads are parented to zsched, just like regular
38317c478bd9Sstevel@tonic-gate  * kernel threads are parented to sched (p0).
38327c478bd9Sstevel@tonic-gate  *
38337c478bd9Sstevel@tonic-gate  * zsched is also responsible for launching init for the zone.
38347c478bd9Sstevel@tonic-gate  */
38357c478bd9Sstevel@tonic-gate static void
zsched(void * arg)38367c478bd9Sstevel@tonic-gate zsched(void *arg)
38377c478bd9Sstevel@tonic-gate {
38387c478bd9Sstevel@tonic-gate 	struct zsched_arg *za = arg;
38397c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
38407c478bd9Sstevel@tonic-gate 	proc_t *initp = proc_init;
38417c478bd9Sstevel@tonic-gate 	zone_t *zone = za->zone;
38427c478bd9Sstevel@tonic-gate 	cred_t *cr, *oldcred;
38437c478bd9Sstevel@tonic-gate 	rctl_set_t *set;
38447c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
38457c478bd9Sstevel@tonic-gate 	contract_t *ct = NULL;
38467c478bd9Sstevel@tonic-gate 	task_t *tk, *oldtk;
38477c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
38487c478bd9Sstevel@tonic-gate 	kproject_t *pj;
38497c478bd9Sstevel@tonic-gate 
38507c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = za->nvlist;
38517c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
38527c478bd9Sstevel@tonic-gate 
3853ae115bc7Smrj 	bcopy("zsched", PTOU(pp)->u_psargs, sizeof ("zsched"));
3854ae115bc7Smrj 	bcopy("zsched", PTOU(pp)->u_comm, sizeof ("zsched"));
3855ae115bc7Smrj 	PTOU(pp)->u_argc = 0;
38567e12ceb3SToomas Soome 	PTOU(pp)->u_argv = 0;
38577e12ceb3SToomas Soome 	PTOU(pp)->u_envp = 0;
38587e12ceb3SToomas Soome 	PTOU(pp)->u_commpagep = 0;
38597c478bd9Sstevel@tonic-gate 	closeall(P_FINFO(pp));
38607c478bd9Sstevel@tonic-gate 
38617c478bd9Sstevel@tonic-gate 	/*
38627c478bd9Sstevel@tonic-gate 	 * We are this zone's "zsched" process.  As the zone isn't generally
38637c478bd9Sstevel@tonic-gate 	 * visible yet we don't need to grab any locks before initializing its
38647c478bd9Sstevel@tonic-gate 	 * zone_proc pointer.
38657c478bd9Sstevel@tonic-gate 	 */
38667c478bd9Sstevel@tonic-gate 	zone_hold(zone);  /* this hold is released by zone_destroy() */
38677c478bd9Sstevel@tonic-gate 	zone->zone_zsched = pp;
38687c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
38697c478bd9Sstevel@tonic-gate 	pp->p_zone = zone;
38707c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
38717c478bd9Sstevel@tonic-gate 
38727c478bd9Sstevel@tonic-gate 	/*
38737c478bd9Sstevel@tonic-gate 	 * Disassociate process from its 'parent'; parent ourselves to init
38747c478bd9Sstevel@tonic-gate 	 * (pid 1) and change other values as needed.
38757c478bd9Sstevel@tonic-gate 	 */
38767c478bd9Sstevel@tonic-gate 	sess_create();
38777c478bd9Sstevel@tonic-gate 
38787c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
38797c478bd9Sstevel@tonic-gate 	proc_detach(pp);
38807c478bd9Sstevel@tonic-gate 	pp->p_ppid = 1;
38817c478bd9Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
38827c478bd9Sstevel@tonic-gate 	pp->p_ancpid = 1;
38837c478bd9Sstevel@tonic-gate 	pp->p_parent = initp;
38847c478bd9Sstevel@tonic-gate 	pp->p_psibling = NULL;
38857c478bd9Sstevel@tonic-gate 	if (initp->p_child)
38867c478bd9Sstevel@tonic-gate 		initp->p_child->p_psibling = pp;
38877c478bd9Sstevel@tonic-gate 	pp->p_sibling = initp->p_child;
38887c478bd9Sstevel@tonic-gate 	initp->p_child = pp;
38897c478bd9Sstevel@tonic-gate 
38907c478bd9Sstevel@tonic-gate 	/* Decrement what newproc() incremented. */
38917c478bd9Sstevel@tonic-gate 	upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID);
38927c478bd9Sstevel@tonic-gate 	/*
38937c478bd9Sstevel@tonic-gate 	 * Our credentials are about to become kcred-like, so we don't care
38947c478bd9Sstevel@tonic-gate 	 * about the caller's ruid.
38957c478bd9Sstevel@tonic-gate 	 */
38967c478bd9Sstevel@tonic-gate 	upcount_inc(crgetruid(kcred), zone->zone_id);
38977c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
38987c478bd9Sstevel@tonic-gate 
38997c478bd9Sstevel@tonic-gate 	/*
3900ff19e029SMenno Lageman 	 * getting out of global zone, so decrement lwp and process counts
39017c478bd9Sstevel@tonic-gate 	 */
39027c478bd9Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
39037c478bd9Sstevel@tonic-gate 	mutex_enter(&global_zone->zone_nlwps_lock);
39047c478bd9Sstevel@tonic-gate 	pj->kpj_nlwps -= pp->p_lwpcnt;
39057c478bd9Sstevel@tonic-gate 	global_zone->zone_nlwps -= pp->p_lwpcnt;
3906ff19e029SMenno Lageman 	pj->kpj_nprocs--;
3907ff19e029SMenno Lageman 	global_zone->zone_nprocs--;
39087c478bd9Sstevel@tonic-gate 	mutex_exit(&global_zone->zone_nlwps_lock);
39097c478bd9Sstevel@tonic-gate 
3910c6939658Ssl 	/*
3911c6939658Ssl 	 * Decrement locked memory counts on old zone and project.
3912c6939658Ssl 	 */
39130209230bSgjelinek 	mutex_enter(&global_zone->zone_mem_lock);
3914c6939658Ssl 	global_zone->zone_locked_mem -= pp->p_locked_mem;
3915c6939658Ssl 	pj->kpj_data.kpd_locked_mem -= pp->p_locked_mem;
39160209230bSgjelinek 	mutex_exit(&global_zone->zone_mem_lock);
3917c6939658Ssl 
39187c478bd9Sstevel@tonic-gate 	/*
39197c478bd9Sstevel@tonic-gate 	 * Create and join a new task in project '0' of this zone.
39207c478bd9Sstevel@tonic-gate 	 *
39217c478bd9Sstevel@tonic-gate 	 * We don't need to call holdlwps() since we know we're the only lwp in
39227c478bd9Sstevel@tonic-gate 	 * this process.
39237c478bd9Sstevel@tonic-gate 	 *
39247c478bd9Sstevel@tonic-gate 	 * task_join() returns with p_lock held.
39257c478bd9Sstevel@tonic-gate 	 */
39267c478bd9Sstevel@tonic-gate 	tk = task_create(0, zone);
39277c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
39287c478bd9Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
3929c6939658Ssl 
3930c6939658Ssl 	pj = pp->p_task->tk_proj;
3931c6939658Ssl 
39320209230bSgjelinek 	mutex_enter(&zone->zone_mem_lock);
3933c6939658Ssl 	zone->zone_locked_mem += pp->p_locked_mem;
3934c6939658Ssl 	pj->kpj_data.kpd_locked_mem += pp->p_locked_mem;
39350209230bSgjelinek 	mutex_exit(&zone->zone_mem_lock);
39367c478bd9Sstevel@tonic-gate 
39377c478bd9Sstevel@tonic-gate 	/*
3938ff19e029SMenno Lageman 	 * add lwp and process counts to zsched's zone, and increment
3939ff19e029SMenno Lageman 	 * project's task and process count due to the task created in
3940ff19e029SMenno Lageman 	 * the above task_create.
39417c478bd9Sstevel@tonic-gate 	 */
39427c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
39437c478bd9Sstevel@tonic-gate 	pj->kpj_nlwps += pp->p_lwpcnt;
39447c478bd9Sstevel@tonic-gate 	pj->kpj_ntasks += 1;
39457c478bd9Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
3946ff19e029SMenno Lageman 	pj->kpj_nprocs++;
3947ff19e029SMenno Lageman 	zone->zone_nprocs++;
39487c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
39497c478bd9Sstevel@tonic-gate 
3950c6939658Ssl 	mutex_exit(&curproc->p_lock);
3951c6939658Ssl 	mutex_exit(&cpu_lock);
3952c6939658Ssl 	task_rele(oldtk);
3953c6939658Ssl 
39547c478bd9Sstevel@tonic-gate 	/*
39557c478bd9Sstevel@tonic-gate 	 * The process was created by a process in the global zone, hence the
39567c478bd9Sstevel@tonic-gate 	 * credentials are wrong.  We might as well have kcred-ish credentials.
39577c478bd9Sstevel@tonic-gate 	 */
39587c478bd9Sstevel@tonic-gate 	cr = zone->zone_kcred;
39597c478bd9Sstevel@tonic-gate 	crhold(cr);
39607c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
39617c478bd9Sstevel@tonic-gate 	oldcred = pp->p_cred;
39627c478bd9Sstevel@tonic-gate 	pp->p_cred = cr;
39637c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
39647c478bd9Sstevel@tonic-gate 	crfree(oldcred);
39657c478bd9Sstevel@tonic-gate 
39667c478bd9Sstevel@tonic-gate 	/*
39677c478bd9Sstevel@tonic-gate 	 * Hold credentials again (for thread)
39687c478bd9Sstevel@tonic-gate 	 */
39697c478bd9Sstevel@tonic-gate 	crhold(cr);
39707c478bd9Sstevel@tonic-gate 
39717c478bd9Sstevel@tonic-gate 	/*
39727c478bd9Sstevel@tonic-gate 	 * p_lwpcnt can't change since this is a kernel process.
39737c478bd9Sstevel@tonic-gate 	 */
39747c478bd9Sstevel@tonic-gate 	crset(pp, cr);
39757c478bd9Sstevel@tonic-gate 
39767c478bd9Sstevel@tonic-gate 	/*
39777c478bd9Sstevel@tonic-gate 	 * Chroot
39787c478bd9Sstevel@tonic-gate 	 */
39797c478bd9Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp);
39807c478bd9Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp);
39817c478bd9Sstevel@tonic-gate 
39827c478bd9Sstevel@tonic-gate 	/*
39837c478bd9Sstevel@tonic-gate 	 * Initialize zone's rctl set.
39847c478bd9Sstevel@tonic-gate 	 */
39857c478bd9Sstevel@tonic-gate 	set = rctl_set_create();
39867c478bd9Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
39877c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
39887c478bd9Sstevel@tonic-gate 	e.rcep_p.zone = zone;
39897c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
39907c478bd9Sstevel@tonic-gate 	zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp);
39917c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
39927c478bd9Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
39937c478bd9Sstevel@tonic-gate 
39947c478bd9Sstevel@tonic-gate 	/*
39957c478bd9Sstevel@tonic-gate 	 * Apply the rctls passed in to zone_create().  This is basically a list
39967c478bd9Sstevel@tonic-gate 	 * assignment: all of the old values are removed and the new ones
39977c478bd9Sstevel@tonic-gate 	 * inserted.  That is, if an empty list is passed in, all values are
39987c478bd9Sstevel@tonic-gate 	 * removed.
39997c478bd9Sstevel@tonic-gate 	 */
40007c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
40017c478bd9Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
40027c478bd9Sstevel@tonic-gate 		rctl_hndl_t hndl;
40037c478bd9Sstevel@tonic-gate 		char *name;
40047c478bd9Sstevel@tonic-gate 		nvlist_t **nvlarray;
40057c478bd9Sstevel@tonic-gate 		uint_t i, nelem;
40067c478bd9Sstevel@tonic-gate 		int error;	/* For ASSERT()s */
40077c478bd9Sstevel@tonic-gate 
40087c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
40097c478bd9Sstevel@tonic-gate 		hndl = rctl_hndl_lookup(name);
40107c478bd9Sstevel@tonic-gate 		ASSERT(hndl != -1);
40117c478bd9Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
40127c478bd9Sstevel@tonic-gate 		ASSERT(rde != NULL);
40137c478bd9Sstevel@tonic-gate 
40147c478bd9Sstevel@tonic-gate 		for (; /* ever */; ) {
40157c478bd9Sstevel@tonic-gate 			rctl_val_t oval;
40167c478bd9Sstevel@tonic-gate 
40177c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
40187c478bd9Sstevel@tonic-gate 			error = rctl_local_get(hndl, NULL, &oval, pp);
40197c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
40207c478bd9Sstevel@tonic-gate 			ASSERT(error == 0);	/* Can't fail for RCTL_FIRST */
40217c478bd9Sstevel@tonic-gate 			ASSERT(oval.rcv_privilege != RCPRIV_BASIC);
40227c478bd9Sstevel@tonic-gate 			if (oval.rcv_privilege == RCPRIV_SYSTEM)
40237c478bd9Sstevel@tonic-gate 				break;
40247c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
40257c478bd9Sstevel@tonic-gate 			error = rctl_local_delete(hndl, &oval, pp);
40267c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
40277c478bd9Sstevel@tonic-gate 			ASSERT(error == 0);
40287c478bd9Sstevel@tonic-gate 		}
40297c478bd9Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
40307c478bd9Sstevel@tonic-gate 		ASSERT(error == 0);
40317c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
40327c478bd9Sstevel@tonic-gate 			rctl_val_t *nvalp;
40337c478bd9Sstevel@tonic-gate 
40347c478bd9Sstevel@tonic-gate 			nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
40357c478bd9Sstevel@tonic-gate 			error = nvlist2rctlval(nvlarray[i], nvalp);
40367c478bd9Sstevel@tonic-gate 			ASSERT(error == 0);
40377c478bd9Sstevel@tonic-gate 			/*
40387c478bd9Sstevel@tonic-gate 			 * rctl_local_insert can fail if the value being
40397c478bd9Sstevel@tonic-gate 			 * inserted is a duplicate; this is OK.
40407c478bd9Sstevel@tonic-gate 			 */
40417c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
40427c478bd9Sstevel@tonic-gate 			if (rctl_local_insert(hndl, nvalp, pp) != 0)
40437c478bd9Sstevel@tonic-gate 				kmem_cache_free(rctl_val_cache, nvalp);
40447c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
40457c478bd9Sstevel@tonic-gate 		}
40467c478bd9Sstevel@tonic-gate 	}
4047d2a70789SRichard Lowe 
40487c478bd9Sstevel@tonic-gate 	/*
40497c478bd9Sstevel@tonic-gate 	 * Tell the world that we're done setting up.
40507c478bd9Sstevel@tonic-gate 	 *
4051bd41d0a8Snordmark 	 * At this point we want to set the zone status to ZONE_IS_INITIALIZED
40527c478bd9Sstevel@tonic-gate 	 * and atomically set the zone's processor set visibility.  Once
40537c478bd9Sstevel@tonic-gate 	 * we drop pool_lock() this zone will automatically get updated
40547c478bd9Sstevel@tonic-gate 	 * to reflect any future changes to the pools configuration.
4055bd41d0a8Snordmark 	 *
4056bd41d0a8Snordmark 	 * Note that after we drop the locks below (zonehash_lock in
4057bd41d0a8Snordmark 	 * particular) other operations such as a zone_getattr call can
4058bd41d0a8Snordmark 	 * now proceed and observe the zone. That is the reason for doing a
4059bd41d0a8Snordmark 	 * state transition to the INITIALIZED state.
40607c478bd9Sstevel@tonic-gate 	 */
40617c478bd9Sstevel@tonic-gate 	pool_lock();
40627c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
40637c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
40647c478bd9Sstevel@tonic-gate 	zone_uniqid(zone);
40657c478bd9Sstevel@tonic-gate 	zone_zsd_configure(zone);
40667c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED)
40677c478bd9Sstevel@tonic-gate 		zone_pset_set(zone, pool_default->pool_pset->pset_id);
40687c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
40697c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
4070bd41d0a8Snordmark 	zone_status_set(zone, ZONE_IS_INITIALIZED);
40717c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
40727c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
40737c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
40747c478bd9Sstevel@tonic-gate 	pool_unlock();
40757c478bd9Sstevel@tonic-gate 
4076bd41d0a8Snordmark 	/* Now call the create callback for this key */
4077bd41d0a8Snordmark 	zsd_apply_all_keys(zsd_apply_create, zone);
4078bd41d0a8Snordmark 
4079bd41d0a8Snordmark 	/* The callbacks are complete. Mark ZONE_IS_READY */
4080bd41d0a8Snordmark 	mutex_enter(&zone_status_lock);
4081bd41d0a8Snordmark 	ASSERT(zone_status_get(zone) == ZONE_IS_INITIALIZED);
4082bd41d0a8Snordmark 	zone_status_set(zone, ZONE_IS_READY);
4083bd41d0a8Snordmark 	mutex_exit(&zone_status_lock);
4084bd41d0a8Snordmark 
40857c478bd9Sstevel@tonic-gate 	/*
40867c478bd9Sstevel@tonic-gate 	 * Once we see the zone transition to the ZONE_IS_BOOTING state,
40877c478bd9Sstevel@tonic-gate 	 * we launch init, and set the state to running.
40887c478bd9Sstevel@tonic-gate 	 */
40897c478bd9Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched");
40907c478bd9Sstevel@tonic-gate 
40917c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_BOOTING) {
40927c478bd9Sstevel@tonic-gate 		id_t cid;
40937c478bd9Sstevel@tonic-gate 
40947c478bd9Sstevel@tonic-gate 		/*
40957c478bd9Sstevel@tonic-gate 		 * Ok, this is a little complicated.  We need to grab the
40967c478bd9Sstevel@tonic-gate 		 * zone's pool's scheduling class ID; note that by now, we
40977c478bd9Sstevel@tonic-gate 		 * are already bound to a pool if we need to be (zoneadmd
40987c478bd9Sstevel@tonic-gate 		 * will have done that to us while we're in the READY
40997c478bd9Sstevel@tonic-gate 		 * state).  *But* the scheduling class for the zone's 'init'
41007c478bd9Sstevel@tonic-gate 		 * must be explicitly passed to newproc, which doesn't
41017c478bd9Sstevel@tonic-gate 		 * respect pool bindings.
41027c478bd9Sstevel@tonic-gate 		 *
41037c478bd9Sstevel@tonic-gate 		 * We hold the pool_lock across the call to newproc() to
41047c478bd9Sstevel@tonic-gate 		 * close the obvious race: the pool's scheduling class
41057c478bd9Sstevel@tonic-gate 		 * could change before we manage to create the LWP with
41067c478bd9Sstevel@tonic-gate 		 * classid 'cid'.
41077c478bd9Sstevel@tonic-gate 		 */
41087c478bd9Sstevel@tonic-gate 		pool_lock();
41090209230bSgjelinek 		if (zone->zone_defaultcid > 0)
41100209230bSgjelinek 			cid = zone->zone_defaultcid;
41110209230bSgjelinek 		else
41120209230bSgjelinek 			cid = pool_get_class(zone->zone_pool);
41137c478bd9Sstevel@tonic-gate 		if (cid == -1)
41147c478bd9Sstevel@tonic-gate 			cid = defaultcid;
41157c478bd9Sstevel@tonic-gate 
41167c478bd9Sstevel@tonic-gate 		/*
41177c478bd9Sstevel@tonic-gate 		 * If this fails, zone_boot will ultimately fail.  The
41187c478bd9Sstevel@tonic-gate 		 * state of the zone will be set to SHUTTING_DOWN-- userland
41197c478bd9Sstevel@tonic-gate 		 * will have to tear down the zone, and fail, or try again.
41207c478bd9Sstevel@tonic-gate 		 */
41213f2f09c1Sdp 		if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid,
412235a5a358SJonathan Adams 		    minclsyspri - 1, &ct, 0)) != 0) {
41237c478bd9Sstevel@tonic-gate 			mutex_enter(&zone_status_lock);
41247c478bd9Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
41257c478bd9Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
412686ad481cSJerry Jelinek 		} else {
412786ad481cSJerry Jelinek 			zone->zone_boot_time = gethrestime_sec();
41287c478bd9Sstevel@tonic-gate 		}
412986ad481cSJerry Jelinek 
41307c478bd9Sstevel@tonic-gate 		pool_unlock();
41317c478bd9Sstevel@tonic-gate 	}
41327c478bd9Sstevel@tonic-gate 
41337c478bd9Sstevel@tonic-gate 	/*
41347c478bd9Sstevel@tonic-gate 	 * Wait for zone_destroy() to be called.  This is what we spend
41357c478bd9Sstevel@tonic-gate 	 * most of our life doing.
41367c478bd9Sstevel@tonic-gate 	 */
41377c478bd9Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched");
41387c478bd9Sstevel@tonic-gate 
41397c478bd9Sstevel@tonic-gate 	if (ct)
41407c478bd9Sstevel@tonic-gate 		/*
41417c478bd9Sstevel@tonic-gate 		 * At this point the process contract should be empty.
41427c478bd9Sstevel@tonic-gate 		 * (Though if it isn't, it's not the end of the world.)
41437c478bd9Sstevel@tonic-gate 		 */
41447c478bd9Sstevel@tonic-gate 		VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0);
41457c478bd9Sstevel@tonic-gate 
41467c478bd9Sstevel@tonic-gate 	/*
41477c478bd9Sstevel@tonic-gate 	 * Allow kcred to be freed when all referring processes
41487c478bd9Sstevel@tonic-gate 	 * (including this one) go away.  We can't just do this in
41497c478bd9Sstevel@tonic-gate 	 * zone_free because we need to wait for the zone_cred_ref to
41507c478bd9Sstevel@tonic-gate 	 * drop to 0 before calling zone_free, and the existence of
41517c478bd9Sstevel@tonic-gate 	 * zone_kcred will prevent that.  Thus, we call crfree here to
41527c478bd9Sstevel@tonic-gate 	 * balance the crdup in zone_create.  The crhold calls earlier
41537c478bd9Sstevel@tonic-gate 	 * in zsched will be dropped when the thread and process exit.
41547c478bd9Sstevel@tonic-gate 	 */
41557c478bd9Sstevel@tonic-gate 	crfree(zone->zone_kcred);
41567c478bd9Sstevel@tonic-gate 	zone->zone_kcred = NULL;
41577c478bd9Sstevel@tonic-gate 
41587c478bd9Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
41597c478bd9Sstevel@tonic-gate }
41607c478bd9Sstevel@tonic-gate 
41617c478bd9Sstevel@tonic-gate /*
41627c478bd9Sstevel@tonic-gate  * Helper function to determine if there are any submounts of the
41637c478bd9Sstevel@tonic-gate  * provided path.  Used to make sure the zone doesn't "inherit" any
41647c478bd9Sstevel@tonic-gate  * mounts from before it is created.
41657c478bd9Sstevel@tonic-gate  */
41667c478bd9Sstevel@tonic-gate static uint_t
zone_mount_count(const char * rootpath)41677c478bd9Sstevel@tonic-gate zone_mount_count(const char *rootpath)
41687c478bd9Sstevel@tonic-gate {
41697c478bd9Sstevel@tonic-gate 	vfs_t *vfsp;
41707c478bd9Sstevel@tonic-gate 	uint_t count = 0;
41717c478bd9Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
41727c478bd9Sstevel@tonic-gate 
41737c478bd9Sstevel@tonic-gate 	/*
41747c478bd9Sstevel@tonic-gate 	 * Holding zonehash_lock prevents race conditions with
41757c478bd9Sstevel@tonic-gate 	 * vfs_list_add()/vfs_list_remove() since we serialize with
41767c478bd9Sstevel@tonic-gate 	 * zone_find_by_path().
41777c478bd9Sstevel@tonic-gate 	 */
41787c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
41797c478bd9Sstevel@tonic-gate 	/*
41807c478bd9Sstevel@tonic-gate 	 * The rootpath must end with a '/'
41817c478bd9Sstevel@tonic-gate 	 */
41827c478bd9Sstevel@tonic-gate 	ASSERT(rootpath[rootpathlen - 1] == '/');
41837c478bd9Sstevel@tonic-gate 
41847c478bd9Sstevel@tonic-gate 	/*
41857c478bd9Sstevel@tonic-gate 	 * This intentionally does not count the rootpath itself if that
41867c478bd9Sstevel@tonic-gate 	 * happens to be a mount point.
41877c478bd9Sstevel@tonic-gate 	 */
41887c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
41897c478bd9Sstevel@tonic-gate 	vfsp = rootvfs;
41907c478bd9Sstevel@tonic-gate 	do {
41917c478bd9Sstevel@tonic-gate 		if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt),
41927c478bd9Sstevel@tonic-gate 		    rootpathlen) == 0)
41937c478bd9Sstevel@tonic-gate 			count++;
41947c478bd9Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
41957c478bd9Sstevel@tonic-gate 	} while (vfsp != rootvfs);
41967c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
41977c478bd9Sstevel@tonic-gate 	return (count);
41987c478bd9Sstevel@tonic-gate }
41997c478bd9Sstevel@tonic-gate 
42007c478bd9Sstevel@tonic-gate /*
42017c478bd9Sstevel@tonic-gate  * Helper function to make sure that a zone created on 'rootpath'
42027c478bd9Sstevel@tonic-gate  * wouldn't end up containing other zones' rootpaths.
42037c478bd9Sstevel@tonic-gate  */
42047c478bd9Sstevel@tonic-gate static boolean_t
zone_is_nested(const char * rootpath)42057c478bd9Sstevel@tonic-gate zone_is_nested(const char *rootpath)
42067c478bd9Sstevel@tonic-gate {
42077c478bd9Sstevel@tonic-gate 	zone_t *zone;
42087c478bd9Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
42097c478bd9Sstevel@tonic-gate 	size_t len;
42107c478bd9Sstevel@tonic-gate 
42117c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
42127c478bd9Sstevel@tonic-gate 
42132bb88978SDhanaraj M 	/*
42142bb88978SDhanaraj M 	 * zone_set_root() appended '/' and '\0' at the end of rootpath
42152bb88978SDhanaraj M 	 */
42162bb88978SDhanaraj M 	if ((rootpathlen <= 3) && (rootpath[0] == '/') &&
42172bb88978SDhanaraj M 	    (rootpath[1] == '/') && (rootpath[2] == '\0'))
42182bb88978SDhanaraj M 		return (B_TRUE);
42192bb88978SDhanaraj M 
42207c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
42217c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
42227c478bd9Sstevel@tonic-gate 		if (zone == global_zone)
42237c478bd9Sstevel@tonic-gate 			continue;
42247c478bd9Sstevel@tonic-gate 		len = strlen(zone->zone_rootpath);
42257c478bd9Sstevel@tonic-gate 		if (strncmp(rootpath, zone->zone_rootpath,
42267c478bd9Sstevel@tonic-gate 		    MIN(rootpathlen, len)) == 0)
42277c478bd9Sstevel@tonic-gate 			return (B_TRUE);
42287c478bd9Sstevel@tonic-gate 	}
42297c478bd9Sstevel@tonic-gate 	return (B_FALSE);
42307c478bd9Sstevel@tonic-gate }
42317c478bd9Sstevel@tonic-gate 
42327c478bd9Sstevel@tonic-gate static int
zone_set_privset(zone_t * zone,const priv_set_t * zone_privs,size_t zone_privssz)4233821c4a97Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs,
4234821c4a97Sdp     size_t zone_privssz)
42357c478bd9Sstevel@tonic-gate {
42361a88ce5cSDan Price 	priv_set_t *privs;
42377c478bd9Sstevel@tonic-gate 
4238821c4a97Sdp 	if (zone_privssz < sizeof (priv_set_t))
42391a88ce5cSDan Price 		return (ENOMEM);
42401a88ce5cSDan Price 
42411a88ce5cSDan Price 	privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
4242821c4a97Sdp 
42437c478bd9Sstevel@tonic-gate 	if (copyin(zone_privs, privs, sizeof (priv_set_t))) {
42447c478bd9Sstevel@tonic-gate 		kmem_free(privs, sizeof (priv_set_t));
42457c478bd9Sstevel@tonic-gate 		return (EFAULT);
42467c478bd9Sstevel@tonic-gate 	}
42477c478bd9Sstevel@tonic-gate 
42487c478bd9Sstevel@tonic-gate 	zone->zone_privset = privs;
42497c478bd9Sstevel@tonic-gate 	return (0);
42507c478bd9Sstevel@tonic-gate }
42517c478bd9Sstevel@tonic-gate 
42527c478bd9Sstevel@tonic-gate /*
42537c478bd9Sstevel@tonic-gate  * We make creative use of nvlists to pass in rctls from userland.  The list is
42547c478bd9Sstevel@tonic-gate  * a list of the following structures:
42557c478bd9Sstevel@tonic-gate  *
42567c478bd9Sstevel@tonic-gate  * (name = rctl_name, value = nvpair_list_array)
42577c478bd9Sstevel@tonic-gate  *
42587c478bd9Sstevel@tonic-gate  * Where each element of the nvpair_list_array is of the form:
42597c478bd9Sstevel@tonic-gate  *
42607c478bd9Sstevel@tonic-gate  * [(name = "privilege", value = RCPRIV_PRIVILEGED),
42612918c4a3SJohn Levon  *	(name = "limit", value = uint64_t),
42622918c4a3SJohn Levon  *	(name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))]
42637c478bd9Sstevel@tonic-gate  */
42647c478bd9Sstevel@tonic-gate static int
parse_rctls(caddr_t ubuf,size_t buflen,nvlist_t ** nvlp)42657c478bd9Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp)
42667c478bd9Sstevel@tonic-gate {
42677c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
42687c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
42697c478bd9Sstevel@tonic-gate 	char *kbuf;
42707c478bd9Sstevel@tonic-gate 	int error;
42717c478bd9Sstevel@tonic-gate 	rctl_val_t rv;
42727c478bd9Sstevel@tonic-gate 
42737c478bd9Sstevel@tonic-gate 	*nvlp = NULL;
42747c478bd9Sstevel@tonic-gate 
42757c478bd9Sstevel@tonic-gate 	if (buflen == 0)
42767c478bd9Sstevel@tonic-gate 		return (0);
42777c478bd9Sstevel@tonic-gate 
42787c478bd9Sstevel@tonic-gate 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
42797c478bd9Sstevel@tonic-gate 		return (ENOMEM);
42807c478bd9Sstevel@tonic-gate 	if (copyin(ubuf, kbuf, buflen)) {
42817c478bd9Sstevel@tonic-gate 		error = EFAULT;
42827c478bd9Sstevel@tonic-gate 		goto out;
42837c478bd9Sstevel@tonic-gate 	}
42847c478bd9Sstevel@tonic-gate 	if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) {
42857c478bd9Sstevel@tonic-gate 		/*
42867c478bd9Sstevel@tonic-gate 		 * nvl may have been allocated/free'd, but the value set to
42877c478bd9Sstevel@tonic-gate 		 * non-NULL, so we reset it here.
42887c478bd9Sstevel@tonic-gate 		 */
42897c478bd9Sstevel@tonic-gate 		nvl = NULL;
42907c478bd9Sstevel@tonic-gate 		error = EINVAL;
42917c478bd9Sstevel@tonic-gate 		goto out;
42927c478bd9Sstevel@tonic-gate 	}
42937c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
42947c478bd9Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
42957c478bd9Sstevel@tonic-gate 		rctl_hndl_t hndl;
42967c478bd9Sstevel@tonic-gate 		nvlist_t **nvlarray;
42977c478bd9Sstevel@tonic-gate 		uint_t i, nelem;
42987c478bd9Sstevel@tonic-gate 		char *name;
42997c478bd9Sstevel@tonic-gate 
43007c478bd9Sstevel@tonic-gate 		error = EINVAL;
43017c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
43027c478bd9Sstevel@tonic-gate 		if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1)
43037c478bd9Sstevel@tonic-gate 		    != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) {
43047c478bd9Sstevel@tonic-gate 			goto out;
43057c478bd9Sstevel@tonic-gate 		}
43067c478bd9Sstevel@tonic-gate 		if ((hndl = rctl_hndl_lookup(name)) == -1) {
43077c478bd9Sstevel@tonic-gate 			goto out;
43087c478bd9Sstevel@tonic-gate 		}
43097c478bd9Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
43107c478bd9Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
43117c478bd9Sstevel@tonic-gate 		ASSERT(error == 0);
43127c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
43137c478bd9Sstevel@tonic-gate 			if (error = nvlist2rctlval(nvlarray[i], &rv))
43147c478bd9Sstevel@tonic-gate 				goto out;
43157c478bd9Sstevel@tonic-gate 		}
43167c478bd9Sstevel@tonic-gate 		if (rctl_invalid_value(rde, &rv)) {
43177c478bd9Sstevel@tonic-gate 			error = EINVAL;
43187c478bd9Sstevel@tonic-gate 			goto out;
43197c478bd9Sstevel@tonic-gate 		}
43207c478bd9Sstevel@tonic-gate 	}
43217c478bd9Sstevel@tonic-gate 	error = 0;
43227c478bd9Sstevel@tonic-gate 	*nvlp = nvl;
43237c478bd9Sstevel@tonic-gate out:
43247c478bd9Sstevel@tonic-gate 	kmem_free(kbuf, buflen);
43257c478bd9Sstevel@tonic-gate 	if (error && nvl != NULL)
43267c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
43277c478bd9Sstevel@tonic-gate 	return (error);
43287c478bd9Sstevel@tonic-gate }
43297c478bd9Sstevel@tonic-gate 
43307c478bd9Sstevel@tonic-gate int
zone_create_error(int er_error,int er_ext,int * er_out)4331854956ceSBryan Cantrill zone_create_error(int er_error, int er_ext, int *er_out)
4332854956ceSBryan Cantrill {
43337c478bd9Sstevel@tonic-gate 	if (er_out != NULL) {
43347c478bd9Sstevel@tonic-gate 		if (copyout(&er_ext, er_out, sizeof (int))) {
43357c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
43367c478bd9Sstevel@tonic-gate 		}
43377c478bd9Sstevel@tonic-gate 	}
43387c478bd9Sstevel@tonic-gate 	return (set_errno(er_error));
43397c478bd9Sstevel@tonic-gate }
43407c478bd9Sstevel@tonic-gate 
434145916cd2Sjpk static int
zone_set_label(zone_t * zone,const bslabel_t * lab,uint32_t doi)434245916cd2Sjpk zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi)
434345916cd2Sjpk {
434445916cd2Sjpk 	ts_label_t *tsl;
434545916cd2Sjpk 	bslabel_t blab;
434645916cd2Sjpk 
434745916cd2Sjpk 	/* Get label from user */
434845916cd2Sjpk 	if (copyin(lab, &blab, sizeof (blab)) != 0)
434945916cd2Sjpk 		return (EFAULT);
435045916cd2Sjpk 	tsl = labelalloc(&blab, doi, KM_NOSLEEP);
435145916cd2Sjpk 	if (tsl == NULL)
435245916cd2Sjpk 		return (ENOMEM);
435345916cd2Sjpk 
435445916cd2Sjpk 	zone->zone_slabel = tsl;
435545916cd2Sjpk 	return (0);
435645916cd2Sjpk }
435745916cd2Sjpk 
4358fa9e4066Sahrens /*
4359fa9e4066Sahrens  * Parses a comma-separated list of ZFS datasets into a per-zone dictionary.
4360fa9e4066Sahrens  */
4361fa9e4066Sahrens static int
parse_zfs(zone_t * zone,caddr_t ubuf,size_t buflen)4362fa9e4066Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen)
4363fa9e4066Sahrens {
4364fa9e4066Sahrens 	char *kbuf;
4365fa9e4066Sahrens 	char *dataset, *next;
4366fa9e4066Sahrens 	zone_dataset_t *zd;
4367fa9e4066Sahrens 	size_t len;
4368fa9e4066Sahrens 
4369fa9e4066Sahrens 	if (ubuf == NULL || buflen == 0)
4370fa9e4066Sahrens 		return (0);
4371fa9e4066Sahrens 
4372fa9e4066Sahrens 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
4373fa9e4066Sahrens 		return (ENOMEM);
4374fa9e4066Sahrens 
4375fa9e4066Sahrens 	if (copyin(ubuf, kbuf, buflen) != 0) {
4376fa9e4066Sahrens 		kmem_free(kbuf, buflen);
4377fa9e4066Sahrens 		return (EFAULT);
4378fa9e4066Sahrens 	}
4379fa9e4066Sahrens 
4380fa9e4066Sahrens 	dataset = next = kbuf;
4381fa9e4066Sahrens 	for (;;) {
4382fa9e4066Sahrens 		zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP);
4383fa9e4066Sahrens 
4384fa9e4066Sahrens 		next = strchr(dataset, ',');
4385fa9e4066Sahrens 
4386fa9e4066Sahrens 		if (next == NULL)
4387fa9e4066Sahrens 			len = strlen(dataset);
4388fa9e4066Sahrens 		else
4389fa9e4066Sahrens 			len = next - dataset;
4390fa9e4066Sahrens 
4391fa9e4066Sahrens 		zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP);
4392fa9e4066Sahrens 		bcopy(dataset, zd->zd_dataset, len);
4393fa9e4066Sahrens 		zd->zd_dataset[len] = '\0';
4394fa9e4066Sahrens 
4395fa9e4066Sahrens 		list_insert_head(&zone->zone_datasets, zd);
4396fa9e4066Sahrens 
4397fa9e4066Sahrens 		if (next == NULL)
4398fa9e4066Sahrens 			break;
4399fa9e4066Sahrens 
4400fa9e4066Sahrens 		dataset = next + 1;
4401fa9e4066Sahrens 	}
4402fa9e4066Sahrens 
4403fa9e4066Sahrens 	kmem_free(kbuf, buflen);
4404fa9e4066Sahrens 	return (0);
4405fa9e4066Sahrens }
4406fa9e4066Sahrens 
44077c478bd9Sstevel@tonic-gate /*
44087c478bd9Sstevel@tonic-gate  * System call to create/initialize a new zone named 'zone_name', rooted
44097c478bd9Sstevel@tonic-gate  * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs',
441045916cd2Sjpk  * and initialized with the zone-wide rctls described in 'rctlbuf', and
441145916cd2Sjpk  * with labeling set by 'match', 'doi', and 'label'.
44127c478bd9Sstevel@tonic-gate  *
44137c478bd9Sstevel@tonic-gate  * If extended error is non-null, we may use it to return more detailed
44147c478bd9Sstevel@tonic-gate  * error information.
44157c478bd9Sstevel@tonic-gate  */
44167c478bd9Sstevel@tonic-gate static zoneid_t
zone_create(const char * zone_name,const char * zone_root,const priv_set_t * zone_privs,size_t zone_privssz,caddr_t rctlbuf,size_t rctlbufsz,caddr_t zfsbuf,size_t zfsbufsz,int * extended_error,int match,uint32_t doi,const bslabel_t * label,int flags)44177c478bd9Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root,
4418821c4a97Sdp     const priv_set_t *zone_privs, size_t zone_privssz,
4419821c4a97Sdp     caddr_t rctlbuf, size_t rctlbufsz,
442045916cd2Sjpk     caddr_t zfsbuf, size_t zfsbufsz, int *extended_error,
4421f4b3ec61Sdh     int match, uint32_t doi, const bslabel_t *label,
4422f4b3ec61Sdh     int flags)
44237c478bd9Sstevel@tonic-gate {
44247c478bd9Sstevel@tonic-gate 	struct zsched_arg zarg;
44257c478bd9Sstevel@tonic-gate 	nvlist_t *rctls = NULL;
44267c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
44277c478bd9Sstevel@tonic-gate 	zone_t *zone, *ztmp;
4428854956ceSBryan Cantrill 	zoneid_t zoneid, start = GLOBAL_ZONEID;
44297c478bd9Sstevel@tonic-gate 	int error;
44307c478bd9Sstevel@tonic-gate 	int error2 = 0;
44317c478bd9Sstevel@tonic-gate 	char *str;
44327c478bd9Sstevel@tonic-gate 	cred_t *zkcr;
443348451833Scarlsonj 	boolean_t insert_label_hash;
44347c478bd9Sstevel@tonic-gate 
44357c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
44367c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
44377c478bd9Sstevel@tonic-gate 
44387c478bd9Sstevel@tonic-gate 	/* can't boot zone from within chroot environment */
44397c478bd9Sstevel@tonic-gate 	if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir)
44407c478bd9Sstevel@tonic-gate 		return (zone_create_error(ENOTSUP, ZE_CHROOTED,
4441821c4a97Sdp 		    extended_error));
4442854956ceSBryan Cantrill 	/*
4443854956ceSBryan Cantrill 	 * As the first step of zone creation, we want to allocate a zoneid.
4444854956ceSBryan Cantrill 	 * This allocation is complicated by the fact that netstacks use the
4445854956ceSBryan Cantrill 	 * zoneid to determine their stackid, but netstacks themselves are
4446854956ceSBryan Cantrill 	 * freed asynchronously with respect to zone destruction.  This means
4447854956ceSBryan Cantrill 	 * that a netstack reference leak (or in principle, an extraordinarily
4448854956ceSBryan Cantrill 	 * long netstack reference hold) could result in a zoneid being
4449854956ceSBryan Cantrill 	 * allocated that in fact corresponds to a stackid from an active
4450854956ceSBryan Cantrill 	 * (referenced) netstack -- unleashing all sorts of havoc when that
4451854956ceSBryan Cantrill 	 * netstack is actually (re)used.  (In the abstract, we might wish a
4452854956ceSBryan Cantrill 	 * zoneid to not be deallocated until its last referencing netstack
4453854956ceSBryan Cantrill 	 * has been released, but netstacks lack a backpointer into their
4454854956ceSBryan Cantrill 	 * referencing zone -- and changing them to have such a pointer would
4455854956ceSBryan Cantrill 	 * be substantial, to put it euphemistically.)  To avoid this, we
4456854956ceSBryan Cantrill 	 * detect this condition on allocation: if we have allocated a zoneid
4457854956ceSBryan Cantrill 	 * that corresponds to a netstack that's still in use, we warn about
4458854956ceSBryan Cantrill 	 * it (as it is much more likely to be a reference leak than an actual
4459854956ceSBryan Cantrill 	 * netstack reference), free it, and allocate another.  That these
4460854956ceSBryan Cantrill 	 * identifers are allocated out of an ID space assures that we won't
4461854956ceSBryan Cantrill 	 * see the identifier we just allocated.
4462854956ceSBryan Cantrill 	 */
4463854956ceSBryan Cantrill 	for (;;) {
4464854956ceSBryan Cantrill 		zoneid = id_alloc(zoneid_space);
4465854956ceSBryan Cantrill 
4466854956ceSBryan Cantrill 		if (!netstack_inuse_by_stackid(zoneid_to_netstackid(zoneid)))
4467854956ceSBryan Cantrill 			break;
4468854956ceSBryan Cantrill 
4469854956ceSBryan Cantrill 		id_free(zoneid_space, zoneid);
4470854956ceSBryan Cantrill 
4471854956ceSBryan Cantrill 		if (start == GLOBAL_ZONEID) {
4472854956ceSBryan Cantrill 			start = zoneid;
4473854956ceSBryan Cantrill 		} else if (zoneid == start) {
4474854956ceSBryan Cantrill 			/*
4475854956ceSBryan Cantrill 			 * We have managed to iterate over the entire available
4476854956ceSBryan Cantrill 			 * zoneid space -- there are no identifiers available,
4477854956ceSBryan Cantrill 			 * presumably due to some number of leaked netstack
4478854956ceSBryan Cantrill 			 * references.  While it's in principle possible for us
4479854956ceSBryan Cantrill 			 * to continue to try, it seems wiser to give up at
4480854956ceSBryan Cantrill 			 * this point to warn and fail explicitly with a
4481854956ceSBryan Cantrill 			 * distinctive error.
4482854956ceSBryan Cantrill 			 */
4483854956ceSBryan Cantrill 			cmn_err(CE_WARN, "zone_create() failed: all available "
4484854956ceSBryan Cantrill 			    "zone IDs have netstacks still in use");
4485854956ceSBryan Cantrill 			return (set_errno(ENFILE));
4486854956ceSBryan Cantrill 		}
4487854956ceSBryan Cantrill 
4488854956ceSBryan Cantrill 		cmn_err(CE_WARN, "unable to reuse zone ID %d; "
4489854956ceSBryan Cantrill 		    "netstack still in use", zoneid);
4490854956ceSBryan Cantrill 	}
44917c478bd9Sstevel@tonic-gate 
44927c478bd9Sstevel@tonic-gate 	zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP);
4493854956ceSBryan Cantrill 	zone->zone_id = zoneid;
44947c478bd9Sstevel@tonic-gate 	zone->zone_status = ZONE_IS_UNINITIALIZED;
44957c478bd9Sstevel@tonic-gate 	zone->zone_pool = pool_default;
44967c478bd9Sstevel@tonic-gate 	zone->zone_pool_mod = gethrtime();
44977c478bd9Sstevel@tonic-gate 	zone->zone_psetid = ZONE_PS_INVAL;
44987c478bd9Sstevel@tonic-gate 	zone->zone_ncpus = 0;
44997c478bd9Sstevel@tonic-gate 	zone->zone_ncpus_online = 0;
45009acbbeafSnn 	zone->zone_restart_init = B_TRUE;
450155fcd84fSJerry Jelinek 	zone->zone_reboot_on_init_exit = B_FALSE;
450255fcd84fSJerry Jelinek 	zone->zone_restart_init_0 = B_FALSE;
45039acbbeafSnn 	zone->zone_brand = &native_brand;
45049acbbeafSnn 	zone->zone_initname = NULL;
45057c478bd9Sstevel@tonic-gate 	mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL);
45067c478bd9Sstevel@tonic-gate 	mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
45070209230bSgjelinek 	mutex_init(&zone->zone_mem_lock, NULL, MUTEX_DEFAULT, NULL);
45087c478bd9Sstevel@tonic-gate 	cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL);
4509a19609f8Sjv 	list_create(&zone->zone_ref_list, sizeof (zone_ref_t),
4510a19609f8Sjv 	    offsetof(zone_ref_t, zref_linkage));
45117c478bd9Sstevel@tonic-gate 	list_create(&zone->zone_zsd, sizeof (struct zsd_entry),
45127c478bd9Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
4513fa9e4066Sahrens 	list_create(&zone->zone_datasets, sizeof (zone_dataset_t),
4514fa9e4066Sahrens 	    offsetof(zone_dataset_t, zd_linkage));
45152b24ab6bSSebastien Roy 	list_create(&zone->zone_dl_list, sizeof (zone_dl_t),
45162b24ab6bSSebastien Roy 	    offsetof(zone_dl_t, zdl_linkage));
451745916cd2Sjpk 	rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
4518835ee219SRobert Harris 	rw_init(&zone->zone_mntfs_db_lock, NULL, RW_DEFAULT, NULL);
45197c478bd9Sstevel@tonic-gate 
4520f4b3ec61Sdh 	if (flags & ZCF_NET_EXCL) {
4521f4b3ec61Sdh 		zone->zone_flags |= ZF_NET_EXCL;
4522f4b3ec61Sdh 	}
4523f4b3ec61Sdh 
45247c478bd9Sstevel@tonic-gate 	if ((error = zone_set_name(zone, zone_name)) != 0) {
45257c478bd9Sstevel@tonic-gate 		zone_free(zone);
45267c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
45277c478bd9Sstevel@tonic-gate 	}
45287c478bd9Sstevel@tonic-gate 
45297c478bd9Sstevel@tonic-gate 	if ((error = zone_set_root(zone, zone_root)) != 0) {
45307c478bd9Sstevel@tonic-gate 		zone_free(zone);
45317c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
45327c478bd9Sstevel@tonic-gate 	}
4533821c4a97Sdp 	if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) {
45347c478bd9Sstevel@tonic-gate 		zone_free(zone);
45357c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
45367c478bd9Sstevel@tonic-gate 	}
45377c478bd9Sstevel@tonic-gate 
45387c478bd9Sstevel@tonic-gate 	/* initialize node name to be the same as zone name */
45397c478bd9Sstevel@tonic-gate 	zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP);
45407c478bd9Sstevel@tonic-gate 	(void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN);
45417c478bd9Sstevel@tonic-gate 	zone->zone_nodename[_SYS_NMLN - 1] = '\0';
45427c478bd9Sstevel@tonic-gate 
45437c478bd9Sstevel@tonic-gate 	zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP);
45447c478bd9Sstevel@tonic-gate 	zone->zone_domain[0] = '\0';
45455679c89fSjv 	zone->zone_hostid = HW_INVALID_HOSTID;
45467c478bd9Sstevel@tonic-gate 	zone->zone_shares = 1;
4547824c205fSml 	zone->zone_shmmax = 0;
4548824c205fSml 	zone->zone_ipc.ipcq_shmmni = 0;
4549824c205fSml 	zone->zone_ipc.ipcq_semmni = 0;
4550824c205fSml 	zone->zone_ipc.ipcq_msgmni = 0;
45517c478bd9Sstevel@tonic-gate 	zone->zone_bootargs = NULL;
45520fbb751dSJohn Levon 	zone->zone_fs_allowed = NULL;
4553d2a70789SRichard Lowe 
45542918c4a3SJohn Levon 	psecflags_default(&zone->zone_secflags);
4555d2a70789SRichard Lowe 
45563f2f09c1Sdp 	zone->zone_initname =
45573f2f09c1Sdp 	    kmem_alloc(strlen(zone_default_initname) + 1, KM_SLEEP);
45583f2f09c1Sdp 	(void) strcpy(zone->zone_initname, zone_default_initname);
45590209230bSgjelinek 	zone->zone_nlwps = 0;
45600209230bSgjelinek 	zone->zone_nlwps_ctl = INT_MAX;
4561ff19e029SMenno Lageman 	zone->zone_nprocs = 0;
4562ff19e029SMenno Lageman 	zone->zone_nprocs_ctl = INT_MAX;
4563c6939658Ssl 	zone->zone_locked_mem = 0;
4564c6939658Ssl 	zone->zone_locked_mem_ctl = UINT64_MAX;
45650209230bSgjelinek 	zone->zone_max_swap = 0;
45660209230bSgjelinek 	zone->zone_max_swap_ctl = UINT64_MAX;
45670fbb751dSJohn Levon 	zone->zone_max_lofi = 0;
45680fbb751dSJohn Levon 	zone->zone_max_lofi_ctl = UINT64_MAX;
45690209230bSgjelinek 	zone0.zone_lockedmem_kstat = NULL;
45700209230bSgjelinek 	zone0.zone_swapresv_kstat = NULL;
45717c478bd9Sstevel@tonic-gate 
45722918c4a3SJohn Levon 	zone->zone_ustate = cpu_uarray_zalloc(ZONE_USTATE_MAX, KM_SLEEP);
45732918c4a3SJohn Levon 
45747c478bd9Sstevel@tonic-gate 	/*
45757c478bd9Sstevel@tonic-gate 	 * Zsched initializes the rctls.
45767c478bd9Sstevel@tonic-gate 	 */
45777c478bd9Sstevel@tonic-gate 	zone->zone_rctls = NULL;
45787c478bd9Sstevel@tonic-gate 
45797c478bd9Sstevel@tonic-gate 	if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) {
45807c478bd9Sstevel@tonic-gate 		zone_free(zone);
45817c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
45827c478bd9Sstevel@tonic-gate 	}
45837c478bd9Sstevel@tonic-gate 
4584fa9e4066Sahrens 	if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) {
4585fa9e4066Sahrens 		zone_free(zone);
4586fa9e4066Sahrens 		return (set_errno(error));
4587fa9e4066Sahrens 	}
4588fa9e4066Sahrens 
458945916cd2Sjpk 	/*
459045916cd2Sjpk 	 * Read in the trusted system parameters:
459145916cd2Sjpk 	 * match flag and sensitivity label.
459245916cd2Sjpk 	 */
459345916cd2Sjpk 	zone->zone_match = match;
459448451833Scarlsonj 	if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) {
45957e6639c2Skp 		/* Fail if requested to set doi to anything but system's doi */
45967e6639c2Skp 		if (doi != 0 && doi != default_doi) {
45977e6639c2Skp 			zone_free(zone);
45987e6639c2Skp 			return (set_errno(EINVAL));
45997e6639c2Skp 		}
46007e6639c2Skp 		/* Always apply system's doi to the zone */
46017e6639c2Skp 		error = zone_set_label(zone, label, default_doi);
460245916cd2Sjpk 		if (error != 0) {
460345916cd2Sjpk 			zone_free(zone);
460445916cd2Sjpk 			return (set_errno(error));
460545916cd2Sjpk 		}
460648451833Scarlsonj 		insert_label_hash = B_TRUE;
460745916cd2Sjpk 	} else {
460845916cd2Sjpk 		/* all zones get an admin_low label if system is not labeled */
460945916cd2Sjpk 		zone->zone_slabel = l_admin_low;
461045916cd2Sjpk 		label_hold(l_admin_low);
461148451833Scarlsonj 		insert_label_hash = B_FALSE;
461245916cd2Sjpk 	}
461345916cd2Sjpk 
46147c478bd9Sstevel@tonic-gate 	/*
46157c478bd9Sstevel@tonic-gate 	 * Stop all lwps since that's what normally happens as part of fork().
46167c478bd9Sstevel@tonic-gate 	 * This needs to happen before we grab any locks to avoid deadlock
46177c478bd9Sstevel@tonic-gate 	 * (another lwp in the process could be waiting for the held lock).
46187c478bd9Sstevel@tonic-gate 	 */
46197c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) {
46207c478bd9Sstevel@tonic-gate 		zone_free(zone);
4621aab83bb8SJosef 'Jeff' Sipek 		nvlist_free(rctls);
46227c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
46237c478bd9Sstevel@tonic-gate 	}
46247c478bd9Sstevel@tonic-gate 
46255fd5c689SJerry Jelinek 	if (block_mounts(zone) == 0) {
46267c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
46277c478bd9Sstevel@tonic-gate 		if (curthread != pp->p_agenttp)
46287c478bd9Sstevel@tonic-gate 			continuelwps(pp);
46297c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
46307c478bd9Sstevel@tonic-gate 		zone_free(zone);
4631aab83bb8SJosef 'Jeff' Sipek 		nvlist_free(rctls);
46327c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
46337c478bd9Sstevel@tonic-gate 	}
46347c478bd9Sstevel@tonic-gate 
46357c478bd9Sstevel@tonic-gate 	/*
46367c478bd9Sstevel@tonic-gate 	 * Set up credential for kernel access.  After this, any errors
46377c478bd9Sstevel@tonic-gate 	 * should go through the dance in errout rather than calling
46387c478bd9Sstevel@tonic-gate 	 * zone_free directly.
46397c478bd9Sstevel@tonic-gate 	 */
46407c478bd9Sstevel@tonic-gate 	zone->zone_kcred = crdup(kcred);
46417c478bd9Sstevel@tonic-gate 	crsetzone(zone->zone_kcred, zone);
46427c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred));
46437c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred));
46447c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred));
46457c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred));
46467c478bd9Sstevel@tonic-gate 
46477c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
46487c478bd9Sstevel@tonic-gate 	/*
46497c478bd9Sstevel@tonic-gate 	 * Make sure zone doesn't already exist.
465045916cd2Sjpk 	 *
465145916cd2Sjpk 	 * If the system and zone are labeled,
465245916cd2Sjpk 	 * make sure no other zone exists that has the same label.
46537c478bd9Sstevel@tonic-gate 	 */
465445916cd2Sjpk 	if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL ||
465548451833Scarlsonj 	    (insert_label_hash &&
465645916cd2Sjpk 	    (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) {
46577c478bd9Sstevel@tonic-gate 		zone_status_t status;
46587c478bd9Sstevel@tonic-gate 
46597c478bd9Sstevel@tonic-gate 		status = zone_status_get(ztmp);
46607c478bd9Sstevel@tonic-gate 		if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING)
46617c478bd9Sstevel@tonic-gate 			error = EEXIST;
46627c478bd9Sstevel@tonic-gate 		else
46637c478bd9Sstevel@tonic-gate 			error = EBUSY;
46648c55461bSton 
46658c55461bSton 		if (insert_label_hash)
46668c55461bSton 			error2 = ZE_LABELINUSE;
46678c55461bSton 
46687c478bd9Sstevel@tonic-gate 		goto errout;
46697c478bd9Sstevel@tonic-gate 	}
46707c478bd9Sstevel@tonic-gate 
46717c478bd9Sstevel@tonic-gate 	/*
46727c478bd9Sstevel@tonic-gate 	 * Don't allow zone creations which would cause one zone's rootpath to
46737c478bd9Sstevel@tonic-gate 	 * be accessible from that of another (non-global) zone.
46747c478bd9Sstevel@tonic-gate 	 */
46757c478bd9Sstevel@tonic-gate 	if (zone_is_nested(zone->zone_rootpath)) {
46767c478bd9Sstevel@tonic-gate 		error = EBUSY;
46777c478bd9Sstevel@tonic-gate 		goto errout;
46787c478bd9Sstevel@tonic-gate 	}
46797c478bd9Sstevel@tonic-gate 
46807c478bd9Sstevel@tonic-gate 	ASSERT(zonecount != 0);		/* check for leaks */
46817c478bd9Sstevel@tonic-gate 	if (zonecount + 1 > maxzones) {
46827c478bd9Sstevel@tonic-gate 		error = ENOMEM;
46837c478bd9Sstevel@tonic-gate 		goto errout;
46847c478bd9Sstevel@tonic-gate 	}
46857c478bd9Sstevel@tonic-gate 
46867c478bd9Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
46877c478bd9Sstevel@tonic-gate 		error = EBUSY;
46887c478bd9Sstevel@tonic-gate 		error2 = ZE_AREMOUNTS;
46897c478bd9Sstevel@tonic-gate 		goto errout;
46907c478bd9Sstevel@tonic-gate 	}
46917c478bd9Sstevel@tonic-gate 
46927c478bd9Sstevel@tonic-gate 	/*
46937c478bd9Sstevel@tonic-gate 	 * Zone is still incomplete, but we need to drop all locks while
46947c478bd9Sstevel@tonic-gate 	 * zsched() initializes this zone's kernel process.  We
46957c478bd9Sstevel@tonic-gate 	 * optimistically add the zone to the hashtable and associated
46967c478bd9Sstevel@tonic-gate 	 * lists so a parallel zone_create() doesn't try to create the
46977c478bd9Sstevel@tonic-gate 	 * same zone.
46987c478bd9Sstevel@tonic-gate 	 */
46997c478bd9Sstevel@tonic-gate 	zonecount++;
47007c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid,
47017c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id,
47027c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
47037c478bd9Sstevel@tonic-gate 	str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP);
47047c478bd9Sstevel@tonic-gate 	(void) strcpy(str, zone->zone_name);
47057c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str,
47067c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
470748451833Scarlsonj 	if (insert_label_hash) {
470845916cd2Sjpk 		(void) mod_hash_insert(zonehashbylabel,
470945916cd2Sjpk 		    (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone);
471048451833Scarlsonj 		zone->zone_flags |= ZF_HASHED_LABEL;
471145916cd2Sjpk 	}
471245916cd2Sjpk 
47137c478bd9Sstevel@tonic-gate 	/*
47147c478bd9Sstevel@tonic-gate 	 * Insert into active list.  At this point there are no 'hold's
47157c478bd9Sstevel@tonic-gate 	 * on the zone, but everyone else knows not to use it, so we can
47167c478bd9Sstevel@tonic-gate 	 * continue to use it.  zsched() will do a zone_hold() if the
47177c478bd9Sstevel@tonic-gate 	 * newproc() is successful.
47187c478bd9Sstevel@tonic-gate 	 */
47197c478bd9Sstevel@tonic-gate 	list_insert_tail(&zone_active, zone);
47207c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
47217c478bd9Sstevel@tonic-gate 
47227c478bd9Sstevel@tonic-gate 	zarg.zone = zone;
47237c478bd9Sstevel@tonic-gate 	zarg.nvlist = rctls;
47247c478bd9Sstevel@tonic-gate 	/*
47257c478bd9Sstevel@tonic-gate 	 * The process, task, and project rctls are probably wrong;
47267c478bd9Sstevel@tonic-gate 	 * we need an interface to get the default values of all rctls,
47277c478bd9Sstevel@tonic-gate 	 * and initialize zsched appropriately.  I'm not sure that that
47287c478bd9Sstevel@tonic-gate 	 * makes much of a difference, though.
47297c478bd9Sstevel@tonic-gate 	 */
473035a5a358SJonathan Adams 	error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL, 0);
473135a5a358SJonathan Adams 	if (error != 0) {
47327c478bd9Sstevel@tonic-gate 		/*
47337c478bd9Sstevel@tonic-gate 		 * We need to undo all globally visible state.
47347c478bd9Sstevel@tonic-gate 		 */
47357c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
47367c478bd9Sstevel@tonic-gate 		list_remove(&zone_active, zone);
473748451833Scarlsonj 		if (zone->zone_flags & ZF_HASHED_LABEL) {
473845916cd2Sjpk 			ASSERT(zone->zone_slabel != NULL);
473945916cd2Sjpk 			(void) mod_hash_destroy(zonehashbylabel,
474045916cd2Sjpk 			    (mod_hash_key_t)zone->zone_slabel);
474145916cd2Sjpk 		}
47427c478bd9Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyname,
47437c478bd9Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_name);
47447c478bd9Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyid,
47457c478bd9Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_id);
47467c478bd9Sstevel@tonic-gate 		ASSERT(zonecount > 1);
47477c478bd9Sstevel@tonic-gate 		zonecount--;
47487c478bd9Sstevel@tonic-gate 		goto errout;
47497c478bd9Sstevel@tonic-gate 	}
47507c478bd9Sstevel@tonic-gate 
47517c478bd9Sstevel@tonic-gate 	/*
47527c478bd9Sstevel@tonic-gate 	 * Zone creation can't fail from now on.
47537c478bd9Sstevel@tonic-gate 	 */
47547c478bd9Sstevel@tonic-gate 
47550209230bSgjelinek 	/*
47560209230bSgjelinek 	 * Create zone kstats
47570209230bSgjelinek 	 */
47580209230bSgjelinek 	zone_kstat_create(zone);
47590209230bSgjelinek 
47607c478bd9Sstevel@tonic-gate 	/*
47617c478bd9Sstevel@tonic-gate 	 * Let the other lwps continue.
47627c478bd9Sstevel@tonic-gate 	 */
47637c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
47647c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
47657c478bd9Sstevel@tonic-gate 		continuelwps(pp);
47667c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
47677c478bd9Sstevel@tonic-gate 
47687c478bd9Sstevel@tonic-gate 	/*
47697c478bd9Sstevel@tonic-gate 	 * Wait for zsched to finish initializing the zone.
47707c478bd9Sstevel@tonic-gate 	 */
47717c478bd9Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_READY);
47727c478bd9Sstevel@tonic-gate 	/*
47737c478bd9Sstevel@tonic-gate 	 * The zone is fully visible, so we can let mounts progress.
47747c478bd9Sstevel@tonic-gate 	 */
47755fd5c689SJerry Jelinek 	resume_mounts(zone);
4776aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(rctls);
47777c478bd9Sstevel@tonic-gate 
47787c478bd9Sstevel@tonic-gate 	return (zoneid);
47797c478bd9Sstevel@tonic-gate 
47807c478bd9Sstevel@tonic-gate errout:
47817c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
47827c478bd9Sstevel@tonic-gate 	/*
47837c478bd9Sstevel@tonic-gate 	 * Let the other lwps continue.
47847c478bd9Sstevel@tonic-gate 	 */
47857c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
47867c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
47877c478bd9Sstevel@tonic-gate 		continuelwps(pp);
47887c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
47897c478bd9Sstevel@tonic-gate 
47905fd5c689SJerry Jelinek 	resume_mounts(zone);
4791aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(rctls);
47927c478bd9Sstevel@tonic-gate 	/*
47937c478bd9Sstevel@tonic-gate 	 * There is currently one reference to the zone, a cred_ref from
47947c478bd9Sstevel@tonic-gate 	 * zone_kcred.  To free the zone, we call crfree, which will call
47957c478bd9Sstevel@tonic-gate 	 * zone_cred_rele, which will call zone_free.
47967c478bd9Sstevel@tonic-gate 	 */
4797a19609f8Sjv 	ASSERT(zone->zone_cred_ref == 1);
47987c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_kcred->cr_ref == 1);
47997c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_ref == 0);
48007c478bd9Sstevel@tonic-gate 	zkcr = zone->zone_kcred;
48017c478bd9Sstevel@tonic-gate 	zone->zone_kcred = NULL;
48027c478bd9Sstevel@tonic-gate 	crfree(zkcr);				/* triggers call to zone_free */
48037c478bd9Sstevel@tonic-gate 	return (zone_create_error(error, error2, extended_error));
48047c478bd9Sstevel@tonic-gate }
48057c478bd9Sstevel@tonic-gate 
48067c478bd9Sstevel@tonic-gate /*
48077c478bd9Sstevel@tonic-gate  * Cause the zone to boot.  This is pretty simple, since we let zoneadmd do
48083f2f09c1Sdp  * the heavy lifting.  initname is the path to the program to launch
48093f2f09c1Sdp  * at the "top" of the zone; if this is NULL, we use the system default,
48103f2f09c1Sdp  * which is stored at zone_default_initname.
48117c478bd9Sstevel@tonic-gate  */
48127c478bd9Sstevel@tonic-gate static int
zone_boot(zoneid_t zoneid)48133f2f09c1Sdp zone_boot(zoneid_t zoneid)
48147c478bd9Sstevel@tonic-gate {
48157c478bd9Sstevel@tonic-gate 	int err;
48167c478bd9Sstevel@tonic-gate 	zone_t *zone;
48177c478bd9Sstevel@tonic-gate 
48187c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
48197c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
48207c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
48217c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
48227c478bd9Sstevel@tonic-gate 
48237c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
48247c478bd9Sstevel@tonic-gate 	/*
48257c478bd9Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with calls to
48267c478bd9Sstevel@tonic-gate 	 * zone_shutdown, zone_destroy, etc.
48277c478bd9Sstevel@tonic-gate 	 */
48287c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
48297c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
48307c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
48317c478bd9Sstevel@tonic-gate 	}
48327c478bd9Sstevel@tonic-gate 
48337c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
48347c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) != ZONE_IS_READY) {
48357c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
48367c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
48377c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
48387c478bd9Sstevel@tonic-gate 	}
48397c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_BOOTING);
48407c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
48417c478bd9Sstevel@tonic-gate 
48427c478bd9Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
48437c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
48447c478bd9Sstevel@tonic-gate 
48457c478bd9Sstevel@tonic-gate 	if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) {
48467c478bd9Sstevel@tonic-gate 		zone_rele(zone);
48477c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
48487c478bd9Sstevel@tonic-gate 	}
48497c478bd9Sstevel@tonic-gate 
48507c478bd9Sstevel@tonic-gate 	/*
48517c478bd9Sstevel@tonic-gate 	 * Boot (starting init) might have failed, in which case the zone
48527c478bd9Sstevel@tonic-gate 	 * will go to the SHUTTING_DOWN state; an appropriate errno will
48537c478bd9Sstevel@tonic-gate 	 * be placed in zone->zone_boot_err, and so we return that.
48547c478bd9Sstevel@tonic-gate 	 */
48557c478bd9Sstevel@tonic-gate 	err = zone->zone_boot_err;
48567c478bd9Sstevel@tonic-gate 	zone_rele(zone);
48577c478bd9Sstevel@tonic-gate 	return (err ? set_errno(err) : 0);
48587c478bd9Sstevel@tonic-gate }
48597c478bd9Sstevel@tonic-gate 
48607c478bd9Sstevel@tonic-gate /*
48617c478bd9Sstevel@tonic-gate  * Kills all user processes in the zone, waiting for them all to exit
48627c478bd9Sstevel@tonic-gate  * before returning.
48637c478bd9Sstevel@tonic-gate  */
48647c478bd9Sstevel@tonic-gate static int
zone_empty(zone_t * zone)48657c478bd9Sstevel@tonic-gate zone_empty(zone_t *zone)
48667c478bd9Sstevel@tonic-gate {
48677c478bd9Sstevel@tonic-gate 	int waitstatus;
48687c478bd9Sstevel@tonic-gate 
48697c478bd9Sstevel@tonic-gate 	/*
48707c478bd9Sstevel@tonic-gate 	 * We need to drop zonehash_lock before killing all
48717c478bd9Sstevel@tonic-gate 	 * processes, otherwise we'll deadlock with zone_find_*
48727c478bd9Sstevel@tonic-gate 	 * which can be called from the exit path.
48737c478bd9Sstevel@tonic-gate 	 */
48747c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
4875d3d50737SRafael Vanoni 	while ((waitstatus = zone_status_timedwait_sig(zone,
4876d3d50737SRafael Vanoni 	    ddi_get_lbolt() + hz, ZONE_IS_EMPTY)) == -1) {
48777c478bd9Sstevel@tonic-gate 		killall(zone->zone_id);
48787c478bd9Sstevel@tonic-gate 	}
48797c478bd9Sstevel@tonic-gate 	/*
48807c478bd9Sstevel@tonic-gate 	 * return EINTR if we were signaled
48817c478bd9Sstevel@tonic-gate 	 */
48827c478bd9Sstevel@tonic-gate 	if (waitstatus == 0)
48837c478bd9Sstevel@tonic-gate 		return (EINTR);
48847c478bd9Sstevel@tonic-gate 	return (0);
48857c478bd9Sstevel@tonic-gate }
48867c478bd9Sstevel@tonic-gate 
488745916cd2Sjpk /*
488845916cd2Sjpk  * This function implements the policy for zone visibility.
488945916cd2Sjpk  *
489045916cd2Sjpk  * In standard Solaris, a non-global zone can only see itself.
489145916cd2Sjpk  *
489245916cd2Sjpk  * In Trusted Extensions, a labeled zone can lookup any zone whose label
489345916cd2Sjpk  * it dominates. For this test, the label of the global zone is treated as
489445916cd2Sjpk  * admin_high so it is special-cased instead of being checked for dominance.
489545916cd2Sjpk  *
489645916cd2Sjpk  * Returns true if zone attributes are viewable, false otherwise.
489745916cd2Sjpk  */
489845916cd2Sjpk static boolean_t
zone_list_access(zone_t * zone)489945916cd2Sjpk zone_list_access(zone_t *zone)
490045916cd2Sjpk {
490145916cd2Sjpk 
490245916cd2Sjpk 	if (curproc->p_zone == global_zone ||
490345916cd2Sjpk 	    curproc->p_zone == zone) {
490445916cd2Sjpk 		return (B_TRUE);
490548451833Scarlsonj 	} else if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) {
490645916cd2Sjpk 		bslabel_t *curproc_label;
490745916cd2Sjpk 		bslabel_t *zone_label;
490845916cd2Sjpk 
490945916cd2Sjpk 		curproc_label = label2bslabel(curproc->p_zone->zone_slabel);
491045916cd2Sjpk 		zone_label = label2bslabel(zone->zone_slabel);
491145916cd2Sjpk 
491245916cd2Sjpk 		if (zone->zone_id != GLOBAL_ZONEID &&
491345916cd2Sjpk 		    bldominates(curproc_label, zone_label)) {
491445916cd2Sjpk 			return (B_TRUE);
491545916cd2Sjpk 		} else {
491645916cd2Sjpk 			return (B_FALSE);
491745916cd2Sjpk 		}
491845916cd2Sjpk 	} else {
491945916cd2Sjpk 		return (B_FALSE);
492045916cd2Sjpk 	}
492145916cd2Sjpk }
492245916cd2Sjpk 
49237c478bd9Sstevel@tonic-gate /*
49247c478bd9Sstevel@tonic-gate  * Systemcall to start the zone's halt sequence.  By the time this
49257c478bd9Sstevel@tonic-gate  * function successfully returns, all user processes and kernel threads
49267c478bd9Sstevel@tonic-gate  * executing in it will have exited, ZSD shutdown callbacks executed,
49277c478bd9Sstevel@tonic-gate  * and the zone status set to ZONE_IS_DOWN.
49287c478bd9Sstevel@tonic-gate  *
49297c478bd9Sstevel@tonic-gate  * It is possible that the call will interrupt itself if the caller is the
49307c478bd9Sstevel@tonic-gate  * parent of any process running in the zone, and doesn't have SIGCHLD blocked.
49317c478bd9Sstevel@tonic-gate  */
49327c478bd9Sstevel@tonic-gate static int
zone_shutdown(zoneid_t zoneid)49337c478bd9Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid)
49347c478bd9Sstevel@tonic-gate {
49357c478bd9Sstevel@tonic-gate 	int error;
49367c478bd9Sstevel@tonic-gate 	zone_t *zone;
49377c478bd9Sstevel@tonic-gate 	zone_status_t status;
49387c478bd9Sstevel@tonic-gate 
49397c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
49407c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
49417c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
49427c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
49437c478bd9Sstevel@tonic-gate 
49447c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
49457c478bd9Sstevel@tonic-gate 	/*
49467c478bd9Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
49477c478bd9Sstevel@tonic-gate 	 * calls to zone_shutdown and zone_destroy.
49487c478bd9Sstevel@tonic-gate 	 */
49497c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
49507c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
49517c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
49527c478bd9Sstevel@tonic-gate 	}
49535fd5c689SJerry Jelinek 
49545fd5c689SJerry Jelinek 	/*
49555fd5c689SJerry Jelinek 	 * We have to drop zonehash_lock before calling block_mounts.
49565fd5c689SJerry Jelinek 	 * Hold the zone so we can continue to use the zone_t.
49575fd5c689SJerry Jelinek 	 */
49585fd5c689SJerry Jelinek 	zone_hold(zone);
49595fd5c689SJerry Jelinek 	mutex_exit(&zonehash_lock);
49605fd5c689SJerry Jelinek 
49615fd5c689SJerry Jelinek 	/*
49625fd5c689SJerry Jelinek 	 * Block mounts so that VFS_MOUNT() can get an accurate view of
49635fd5c689SJerry Jelinek 	 * the zone's status with regards to ZONE_IS_SHUTTING down.
49645fd5c689SJerry Jelinek 	 *
49655fd5c689SJerry Jelinek 	 * e.g. NFS can fail the mount if it determines that the zone
49665fd5c689SJerry Jelinek 	 * has already begun the shutdown sequence.
49675fd5c689SJerry Jelinek 	 *
49685fd5c689SJerry Jelinek 	 */
49695fd5c689SJerry Jelinek 	if (block_mounts(zone) == 0) {
49705fd5c689SJerry Jelinek 		zone_rele(zone);
49715fd5c689SJerry Jelinek 		return (set_errno(EINTR));
49725fd5c689SJerry Jelinek 	}
49735fd5c689SJerry Jelinek 
49745fd5c689SJerry Jelinek 	mutex_enter(&zonehash_lock);
49757c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
49767c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
49777c478bd9Sstevel@tonic-gate 	/*
49787c478bd9Sstevel@tonic-gate 	 * Fail if the zone isn't fully initialized yet.
49797c478bd9Sstevel@tonic-gate 	 */
49807c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY) {
49817c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
49827c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
49835fd5c689SJerry Jelinek 		resume_mounts(zone);
49845fd5c689SJerry Jelinek 		zone_rele(zone);
49857c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
49867c478bd9Sstevel@tonic-gate 	}
49877c478bd9Sstevel@tonic-gate 	/*
49887c478bd9Sstevel@tonic-gate 	 * If conditions required for zone_shutdown() to return have been met,
49897c478bd9Sstevel@tonic-gate 	 * return success.
49907c478bd9Sstevel@tonic-gate 	 */
49917c478bd9Sstevel@tonic-gate 	if (status >= ZONE_IS_DOWN) {
49927c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
49937c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
49945fd5c689SJerry Jelinek 		resume_mounts(zone);
49955fd5c689SJerry Jelinek 		zone_rele(zone);
49967c478bd9Sstevel@tonic-gate 		return (0);
49977c478bd9Sstevel@tonic-gate 	}
49987c478bd9Sstevel@tonic-gate 	/*
49997c478bd9Sstevel@tonic-gate 	 * If zone_shutdown() hasn't been called before, go through the motions.
50007c478bd9Sstevel@tonic-gate 	 * If it has, there's nothing to do but wait for the kernel threads to
50017c478bd9Sstevel@tonic-gate 	 * drain.
50027c478bd9Sstevel@tonic-gate 	 */
50037c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_EMPTY) {
50047c478bd9Sstevel@tonic-gate 		uint_t ntasks;
50057c478bd9Sstevel@tonic-gate 
50067c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
50077c478bd9Sstevel@tonic-gate 		if ((ntasks = zone->zone_ntasks) != 1) {
50087c478bd9Sstevel@tonic-gate 			/*
50097c478bd9Sstevel@tonic-gate 			 * There's still stuff running.
50107c478bd9Sstevel@tonic-gate 			 */
50117c478bd9Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
50127c478bd9Sstevel@tonic-gate 		}
50137c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
50147c478bd9Sstevel@tonic-gate 		if (ntasks == 1) {
50157c478bd9Sstevel@tonic-gate 			/*
50167c478bd9Sstevel@tonic-gate 			 * The only way to create another task is through
50177c478bd9Sstevel@tonic-gate 			 * zone_enter(), which will block until we drop
50187c478bd9Sstevel@tonic-gate 			 * zonehash_lock.  The zone is empty.
50197c478bd9Sstevel@tonic-gate 			 */
50207c478bd9Sstevel@tonic-gate 			if (zone->zone_kthreads == NULL) {
50217c478bd9Sstevel@tonic-gate 				/*
50227c478bd9Sstevel@tonic-gate 				 * Skip ahead to ZONE_IS_DOWN
50237c478bd9Sstevel@tonic-gate 				 */
50247c478bd9Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_DOWN);
50257c478bd9Sstevel@tonic-gate 			} else {
50267c478bd9Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_EMPTY);
50277c478bd9Sstevel@tonic-gate 			}
50287c478bd9Sstevel@tonic-gate 		}
50297c478bd9Sstevel@tonic-gate 	}
50307c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
50317c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
50325fd5c689SJerry Jelinek 	resume_mounts(zone);
50337c478bd9Sstevel@tonic-gate 
50347c478bd9Sstevel@tonic-gate 	if (error = zone_empty(zone)) {
50357c478bd9Sstevel@tonic-gate 		zone_rele(zone);
50367c478bd9Sstevel@tonic-gate 		return (set_errno(error));
50377c478bd9Sstevel@tonic-gate 	}
50387c478bd9Sstevel@tonic-gate 	/*
50397c478bd9Sstevel@tonic-gate 	 * After the zone status goes to ZONE_IS_DOWN this zone will no
50407c478bd9Sstevel@tonic-gate 	 * longer be notified of changes to the pools configuration, so
50417c478bd9Sstevel@tonic-gate 	 * in order to not end up with a stale pool pointer, we point
50427c478bd9Sstevel@tonic-gate 	 * ourselves at the default pool and remove all resource
50437c478bd9Sstevel@tonic-gate 	 * visibility.  This is especially important as the zone_t may
50447c478bd9Sstevel@tonic-gate 	 * languish on the deathrow for a very long time waiting for
50457c478bd9Sstevel@tonic-gate 	 * cred's to drain out.
50467c478bd9Sstevel@tonic-gate 	 *
50477c478bd9Sstevel@tonic-gate 	 * This rebinding of the zone can happen multiple times
50487c478bd9Sstevel@tonic-gate 	 * (presumably due to interrupted or parallel systemcalls)
50497c478bd9Sstevel@tonic-gate 	 * without any adverse effects.
50507c478bd9Sstevel@tonic-gate 	 */
50517c478bd9Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
50527c478bd9Sstevel@tonic-gate 		zone_rele(zone);
50537c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
50547c478bd9Sstevel@tonic-gate 	}
50557c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
50567c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
50577c478bd9Sstevel@tonic-gate 		zone_pool_set(zone, pool_default);
50587c478bd9Sstevel@tonic-gate 		/*
50597c478bd9Sstevel@tonic-gate 		 * The zone no longer needs to be able to see any cpus.
50607c478bd9Sstevel@tonic-gate 		 */
50617c478bd9Sstevel@tonic-gate 		zone_pset_set(zone, ZONE_PS_INVAL);
50627c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
50637c478bd9Sstevel@tonic-gate 	}
50647c478bd9Sstevel@tonic-gate 	pool_unlock();
50657c478bd9Sstevel@tonic-gate 
50667c478bd9Sstevel@tonic-gate 	/*
50677c478bd9Sstevel@tonic-gate 	 * ZSD shutdown callbacks can be executed multiple times, hence
50687c478bd9Sstevel@tonic-gate 	 * it is safe to not be holding any locks across this call.
50697c478bd9Sstevel@tonic-gate 	 */
50707c478bd9Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_SHUTDOWN);
50717c478bd9Sstevel@tonic-gate 
50727c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
50737c478bd9Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN)
50747c478bd9Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DOWN);
50757c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
50767c478bd9Sstevel@tonic-gate 
50777c478bd9Sstevel@tonic-gate 	/*
50787c478bd9Sstevel@tonic-gate 	 * Wait for kernel threads to drain.
50797c478bd9Sstevel@tonic-gate 	 */
50807c478bd9Sstevel@tonic-gate 	if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) {
50817c478bd9Sstevel@tonic-gate 		zone_rele(zone);
50827c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
50837c478bd9Sstevel@tonic-gate 	}
50849acbbeafSnn 
508552978630Ssl 	/*
508652978630Ssl 	 * Zone can be become down/destroyable even if the above wait
508752978630Ssl 	 * returns EINTR, so any code added here may never execute.
508852978630Ssl 	 * (i.e. don't add code here)
508952978630Ssl 	 */
50909acbbeafSnn 
50917c478bd9Sstevel@tonic-gate 	zone_rele(zone);
50927c478bd9Sstevel@tonic-gate 	return (0);
50937c478bd9Sstevel@tonic-gate }
50947c478bd9Sstevel@tonic-gate 
5095a19609f8Sjv /*
5096a19609f8Sjv  * Log the specified zone's reference counts.  The caller should not be
5097a19609f8Sjv  * holding the zone's zone_lock.
5098a19609f8Sjv  */
5099a19609f8Sjv static void
zone_log_refcounts(zone_t * zone)5100a19609f8Sjv zone_log_refcounts(zone_t *zone)
5101a19609f8Sjv {
5102a19609f8Sjv 	char *buffer;
5103a19609f8Sjv 	char *buffer_position;
5104a19609f8Sjv 	uint32_t buffer_size;
5105a19609f8Sjv 	uint32_t index;
5106a19609f8Sjv 	uint_t ref;
5107a19609f8Sjv 	uint_t cred_ref;
5108a19609f8Sjv 
5109a19609f8Sjv 	/*
5110a19609f8Sjv 	 * Construct a string representing the subsystem-specific reference
5111a19609f8Sjv 	 * counts.  The counts are printed in ascending order by index into the
5112a19609f8Sjv 	 * zone_t::zone_subsys_ref array.  The list will be surrounded by
5113a19609f8Sjv 	 * square brackets [] and will only contain nonzero reference counts.
5114a19609f8Sjv 	 *
5115a19609f8Sjv 	 * The buffer will hold two square bracket characters plus ten digits,
5116a19609f8Sjv 	 * one colon, one space, one comma, and some characters for a
5117a19609f8Sjv 	 * subsystem name per subsystem-specific reference count.  (Unsigned 32-
5118a19609f8Sjv 	 * bit integers have at most ten decimal digits.)  The last
5119a19609f8Sjv 	 * reference count's comma is replaced by the closing square
5120a19609f8Sjv 	 * bracket and a NULL character to terminate the string.
5121a19609f8Sjv 	 *
5122a19609f8Sjv 	 * NOTE: We have to grab the zone's zone_lock to create a consistent
5123a19609f8Sjv 	 * snapshot of the zone's reference counters.
5124a19609f8Sjv 	 *
5125a19609f8Sjv 	 * First, figure out how much space the string buffer will need.
5126a19609f8Sjv 	 * The buffer's size is stored in buffer_size.
5127a19609f8Sjv 	 */
5128a19609f8Sjv 	buffer_size = 2;			/* for the square brackets */
5129a19609f8Sjv 	mutex_enter(&zone->zone_lock);
5130a19609f8Sjv 	zone->zone_flags |= ZF_REFCOUNTS_LOGGED;
5131a19609f8Sjv 	ref = zone->zone_ref;
5132a19609f8Sjv 	cred_ref = zone->zone_cred_ref;
5133a19609f8Sjv 	for (index = 0; index < ZONE_REF_NUM_SUBSYS; ++index)
5134a19609f8Sjv 		if (zone->zone_subsys_ref[index] != 0)
5135a19609f8Sjv 			buffer_size += strlen(zone_ref_subsys_names[index]) +
5136a19609f8Sjv 			    13;
5137a19609f8Sjv 	if (buffer_size == 2) {
5138a19609f8Sjv 		/*
5139a19609f8Sjv 		 * No subsystems had nonzero reference counts.  Don't bother
5140a19609f8Sjv 		 * with allocating a buffer; just log the general-purpose and
5141a19609f8Sjv 		 * credential reference counts.
5142a19609f8Sjv 		 */
5143a19609f8Sjv 		mutex_exit(&zone->zone_lock);
5144a19609f8Sjv 		(void) strlog(0, 0, 1, SL_CONSOLE | SL_NOTE,
5145a19609f8Sjv 		    "Zone '%s' (ID: %d) is shutting down, but %u zone "
5146a19609f8Sjv 		    "references and %u credential references are still extant",
5147a19609f8Sjv 		    zone->zone_name, zone->zone_id, ref, cred_ref);
5148a19609f8Sjv 		return;
5149a19609f8Sjv 	}
5150a19609f8Sjv 
5151a19609f8Sjv 	/*
5152a19609f8Sjv 	 * buffer_size contains the exact number of characters that the
5153a19609f8Sjv 	 * buffer will need.  Allocate the buffer and fill it with nonzero
5154a19609f8Sjv 	 * subsystem-specific reference counts.  Surround the results with
5155a19609f8Sjv 	 * square brackets afterwards.
5156a19609f8Sjv 	 */
5157a19609f8Sjv 	buffer = kmem_alloc(buffer_size, KM_SLEEP);
5158a19609f8Sjv 	buffer_position = &buffer[1];
5159a19609f8Sjv 	for (index = 0; index < ZONE_REF_NUM_SUBSYS; ++index) {
5160a19609f8Sjv 		/*
5161a19609f8Sjv 		 * NOTE: The DDI's version of sprintf() returns a pointer to
5162a19609f8Sjv 		 * the modified buffer rather than the number of bytes written
5163a19609f8Sjv 		 * (as in snprintf(3C)).  This is unfortunate and annoying.
5164a19609f8Sjv 		 * Therefore, we'll use snprintf() with INT_MAX to get the
5165a19609f8Sjv 		 * number of bytes written.  Using INT_MAX is safe because
5166a19609f8Sjv 		 * the buffer is perfectly sized for the data: we'll never
5167a19609f8Sjv 		 * overrun the buffer.
5168a19609f8Sjv 		 */
5169a19609f8Sjv 		if (zone->zone_subsys_ref[index] != 0)
5170a19609f8Sjv 			buffer_position += snprintf(buffer_position, INT_MAX,
5171a19609f8Sjv 			    "%s: %u,", zone_ref_subsys_names[index],
5172a19609f8Sjv 			    zone->zone_subsys_ref[index]);
5173a19609f8Sjv 	}
5174a19609f8Sjv 	mutex_exit(&zone->zone_lock);
5175a19609f8Sjv 	buffer[0] = '[';
5176a19609f8Sjv 	ASSERT((uintptr_t)(buffer_position - buffer) < buffer_size);
5177a19609f8Sjv 	ASSERT(buffer_position[0] == '\0' && buffer_position[-1] == ',');
5178a19609f8Sjv 	buffer_position[-1] = ']';
5179a19609f8Sjv 
5180a19609f8Sjv 	/*
5181a19609f8Sjv 	 * Log the reference counts and free the message buffer.
5182a19609f8Sjv 	 */
5183a19609f8Sjv 	(void) strlog(0, 0, 1, SL_CONSOLE | SL_NOTE,
5184a19609f8Sjv 	    "Zone '%s' (ID: %d) is shutting down, but %u zone references and "
5185a19609f8Sjv 	    "%u credential references are still extant %s", zone->zone_name,
5186a19609f8Sjv 	    zone->zone_id, ref, cred_ref, buffer);
5187a19609f8Sjv 	kmem_free(buffer, buffer_size);
5188a19609f8Sjv }
5189a19609f8Sjv 
51907c478bd9Sstevel@tonic-gate /*
51917c478bd9Sstevel@tonic-gate  * Systemcall entry point to finalize the zone halt process.  The caller
5192824c205fSml  * must have already successfully called zone_shutdown().
51937c478bd9Sstevel@tonic-gate  *
51947c478bd9Sstevel@tonic-gate  * Upon successful completion, the zone will have been fully destroyed:
51957c478bd9Sstevel@tonic-gate  * zsched will have exited, destructor callbacks executed, and the zone
51967c478bd9Sstevel@tonic-gate  * removed from the list of active zones.
51977c478bd9Sstevel@tonic-gate  */
51987c478bd9Sstevel@tonic-gate static int
zone_destroy(zoneid_t zoneid)51997c478bd9Sstevel@tonic-gate zone_destroy(zoneid_t zoneid)
52007c478bd9Sstevel@tonic-gate {
52017c478bd9Sstevel@tonic-gate 	uint64_t uniqid;
52027c478bd9Sstevel@tonic-gate 	zone_t *zone;
52037c478bd9Sstevel@tonic-gate 	zone_status_t status;
5204a19609f8Sjv 	clock_t wait_time;
5205a19609f8Sjv 	boolean_t log_refcounts;
52067c478bd9Sstevel@tonic-gate 
52077c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
52087c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
52097c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
52107c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
52117c478bd9Sstevel@tonic-gate 
52127c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
52137c478bd9Sstevel@tonic-gate 	/*
52147c478bd9Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
52157c478bd9Sstevel@tonic-gate 	 * calls to zone_destroy.
52167c478bd9Sstevel@tonic-gate 	 */
52177c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
52187c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
52197c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
52207c478bd9Sstevel@tonic-gate 	}
52217c478bd9Sstevel@tonic-gate 
52227c478bd9Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
52237c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
52247c478bd9Sstevel@tonic-gate 		return (set_errno(EBUSY));
52257c478bd9Sstevel@tonic-gate 	}
52267c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
52277c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
52287c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_DOWN) {
52297c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
52307c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
52317c478bd9Sstevel@tonic-gate 		return (set_errno(EBUSY));
52327c478bd9Sstevel@tonic-gate 	} else if (status == ZONE_IS_DOWN) {
52337c478bd9Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */
52347c478bd9Sstevel@tonic-gate 	}
52357c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
52367c478bd9Sstevel@tonic-gate 	zone_hold(zone);
52377c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
52387c478bd9Sstevel@tonic-gate 
52397c478bd9Sstevel@tonic-gate 	/*
52407c478bd9Sstevel@tonic-gate 	 * wait for zsched to exit
52417c478bd9Sstevel@tonic-gate 	 */
52427c478bd9Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_DEAD);
52437c478bd9Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_DESTROY);
5244f4b3ec61Sdh 	zone->zone_netstack = NULL;
52457c478bd9Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
52467c478bd9Sstevel@tonic-gate 	zone_rele(zone);
52477c478bd9Sstevel@tonic-gate 	zone = NULL;	/* potentially free'd */
52487c478bd9Sstevel@tonic-gate 
5249a19609f8Sjv 	log_refcounts = B_FALSE;
5250a19609f8Sjv 	wait_time = SEC_TO_TICK(ZONE_DESTROY_TIMEOUT_SECS);
52517c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
52527c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
52537c478bd9Sstevel@tonic-gate 		boolean_t unref;
5254a19609f8Sjv 		boolean_t refs_have_been_logged;
52557c478bd9Sstevel@tonic-gate 
52567c478bd9Sstevel@tonic-gate 		if ((zone = zone_find_all_by_id(zoneid)) == NULL ||
52577c478bd9Sstevel@tonic-gate 		    zone->zone_uniqid != uniqid) {
52587c478bd9Sstevel@tonic-gate 			/*
52597c478bd9Sstevel@tonic-gate 			 * The zone has gone away.  Necessary conditions
52607c478bd9Sstevel@tonic-gate 			 * are met, so we return success.
52617c478bd9Sstevel@tonic-gate 			 */
52627c478bd9Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
52637c478bd9Sstevel@tonic-gate 			return (0);
52647c478bd9Sstevel@tonic-gate 		}
52657c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
52667c478bd9Sstevel@tonic-gate 		unref = ZONE_IS_UNREF(zone);
5267a19609f8Sjv 		refs_have_been_logged = (zone->zone_flags &
5268a19609f8Sjv 		    ZF_REFCOUNTS_LOGGED);
52697c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
52707c478bd9Sstevel@tonic-gate 		if (unref) {
52717c478bd9Sstevel@tonic-gate 			/*
52727c478bd9Sstevel@tonic-gate 			 * There is only one reference to the zone -- that
52737c478bd9Sstevel@tonic-gate 			 * added when the zone was added to the hashtables --
52747c478bd9Sstevel@tonic-gate 			 * and things will remain this way until we drop
52757c478bd9Sstevel@tonic-gate 			 * zonehash_lock... we can go ahead and cleanup the
52767c478bd9Sstevel@tonic-gate 			 * zone.
52777c478bd9Sstevel@tonic-gate 			 */
52787c478bd9Sstevel@tonic-gate 			break;
52797c478bd9Sstevel@tonic-gate 		}
52807c478bd9Sstevel@tonic-gate 
5281a19609f8Sjv 		/*
5282a19609f8Sjv 		 * Wait for zone_rele_common() or zone_cred_rele() to signal
5283a19609f8Sjv 		 * zone_destroy_cv.  zone_destroy_cv is signaled only when
5284a19609f8Sjv 		 * some zone's general-purpose reference count reaches one.
5285a19609f8Sjv 		 * If ZONE_DESTROY_TIMEOUT_SECS seconds elapse while waiting
5286a19609f8Sjv 		 * on zone_destroy_cv, then log the zone's reference counts and
5287a19609f8Sjv 		 * continue to wait for zone_rele() and zone_cred_rele().
5288a19609f8Sjv 		 */
5289a19609f8Sjv 		if (!refs_have_been_logged) {
5290a19609f8Sjv 			if (!log_refcounts) {
5291a19609f8Sjv 				/*
5292a19609f8Sjv 				 * This thread hasn't timed out waiting on
5293a19609f8Sjv 				 * zone_destroy_cv yet.  Wait wait_time clock
5294a19609f8Sjv 				 * ticks (initially ZONE_DESTROY_TIMEOUT_SECS
5295a19609f8Sjv 				 * seconds) for the zone's references to clear.
5296a19609f8Sjv 				 */
5297a19609f8Sjv 				ASSERT(wait_time > 0);
5298a19609f8Sjv 				wait_time = cv_reltimedwait_sig(
5299a19609f8Sjv 				    &zone_destroy_cv, &zonehash_lock, wait_time,
5300a19609f8Sjv 				    TR_SEC);
5301a19609f8Sjv 				if (wait_time > 0) {
5302a19609f8Sjv 					/*
5303a19609f8Sjv 					 * A thread in zone_rele() or
5304a19609f8Sjv 					 * zone_cred_rele() signaled
5305a19609f8Sjv 					 * zone_destroy_cv before this thread's
5306a19609f8Sjv 					 * wait timed out.  The zone might have
5307a19609f8Sjv 					 * only one reference left; find out!
5308a19609f8Sjv 					 */
5309a19609f8Sjv 					continue;
5310a19609f8Sjv 				} else if (wait_time == 0) {
5311a19609f8Sjv 					/* The thread's process was signaled. */
5312a19609f8Sjv 					mutex_exit(&zonehash_lock);
5313a19609f8Sjv 					return (set_errno(EINTR));
5314a19609f8Sjv 				}
5315a19609f8Sjv 
5316a19609f8Sjv 				/*
5317a19609f8Sjv 				 * The thread timed out while waiting on
5318a19609f8Sjv 				 * zone_destroy_cv.  Even though the thread
5319a19609f8Sjv 				 * timed out, it has to check whether another
5320a19609f8Sjv 				 * thread woke up from zone_destroy_cv and
5321a19609f8Sjv 				 * destroyed the zone.
5322a19609f8Sjv 				 *
5323a19609f8Sjv 				 * If the zone still exists and has more than
5324a19609f8Sjv 				 * one unreleased general-purpose reference,
5325a19609f8Sjv 				 * then log the zone's reference counts.
5326a19609f8Sjv 				 */
5327a19609f8Sjv 				log_refcounts = B_TRUE;
5328a19609f8Sjv 				continue;
5329a19609f8Sjv 			}
5330a19609f8Sjv 
5331a19609f8Sjv 			/*
5332a19609f8Sjv 			 * The thread already timed out on zone_destroy_cv while
5333a19609f8Sjv 			 * waiting for subsystems to release the zone's last
5334a19609f8Sjv 			 * general-purpose references.  Log the zone's reference
5335a19609f8Sjv 			 * counts and wait indefinitely on zone_destroy_cv.
5336a19609f8Sjv 			 */
5337a19609f8Sjv 			zone_log_refcounts(zone);
5338a19609f8Sjv 		}
53397c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) {
5340a19609f8Sjv 			/* The thread's process was signaled. */
53417c478bd9Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
53427c478bd9Sstevel@tonic-gate 			return (set_errno(EINTR));
53437c478bd9Sstevel@tonic-gate 		}
53447c478bd9Sstevel@tonic-gate 	}
53457c478bd9Sstevel@tonic-gate 
5346c97ad5cdSakolb 	/*
5347c97ad5cdSakolb 	 * Remove CPU cap for this zone now since we're not going to
5348c97ad5cdSakolb 	 * fail below this point.
5349c97ad5cdSakolb 	 */
5350c97ad5cdSakolb 	cpucaps_zone_remove(zone);
5351c97ad5cdSakolb 
5352c97ad5cdSakolb 	/* Get rid of the zone's kstats */
53530209230bSgjelinek 	zone_kstat_delete(zone);
53540209230bSgjelinek 
5355134a1f4eSCasper H.S. Dik 	/* remove the pfexecd doors */
5356134a1f4eSCasper H.S. Dik 	if (zone->zone_pfexecd != NULL) {
5357134a1f4eSCasper H.S. Dik 		klpd_freelist(&zone->zone_pfexecd);
5358134a1f4eSCasper H.S. Dik 		zone->zone_pfexecd = NULL;
5359134a1f4eSCasper H.S. Dik 	}
5360134a1f4eSCasper H.S. Dik 
5361319378d9Seh 	/* free brand specific data */
5362319378d9Seh 	if (ZONE_IS_BRANDED(zone))
5363319378d9Seh 		ZBROP(zone)->b_free_brand_data(zone);
5364319378d9Seh 
536552978630Ssl 	/* Say goodbye to brand framework. */
536652978630Ssl 	brand_unregister_zone(zone->zone_brand);
536752978630Ssl 
53687c478bd9Sstevel@tonic-gate 	/*
53697c478bd9Sstevel@tonic-gate 	 * It is now safe to let the zone be recreated; remove it from the
53707c478bd9Sstevel@tonic-gate 	 * lists.  The memory will not be freed until the last cred
53717c478bd9Sstevel@tonic-gate 	 * reference goes away.
53727c478bd9Sstevel@tonic-gate 	 */
53737c478bd9Sstevel@tonic-gate 	ASSERT(zonecount > 1);	/* must be > 1; can't destroy global zone */
53747c478bd9Sstevel@tonic-gate 	zonecount--;
53757c478bd9Sstevel@tonic-gate 	/* remove from active list and hash tables */
53767c478bd9Sstevel@tonic-gate 	list_remove(&zone_active, zone);
53777c478bd9Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyname,
53787c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)zone->zone_name);
53797c478bd9Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyid,
53807c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id);
538148451833Scarlsonj 	if (zone->zone_flags & ZF_HASHED_LABEL)
538245916cd2Sjpk 		(void) mod_hash_destroy(zonehashbylabel,
538345916cd2Sjpk 		    (mod_hash_key_t)zone->zone_slabel);
53847c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
53857c478bd9Sstevel@tonic-gate 
5386108322fbScarlsonj 	/*
5387108322fbScarlsonj 	 * Release the root vnode; we're not using it anymore.  Nor should any
5388108322fbScarlsonj 	 * other thread that might access it exist.
5389108322fbScarlsonj 	 */
5390108322fbScarlsonj 	if (zone->zone_rootvp != NULL) {
5391108322fbScarlsonj 		VN_RELE(zone->zone_rootvp);
5392108322fbScarlsonj 		zone->zone_rootvp = NULL;
5393108322fbScarlsonj 	}
5394108322fbScarlsonj 
53957c478bd9Sstevel@tonic-gate 	/* add to deathrow list */
53967c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_deathrow_lock);
53977c478bd9Sstevel@tonic-gate 	list_insert_tail(&zone_deathrow, zone);
53987c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_deathrow_lock);
53997c478bd9Sstevel@tonic-gate 
54007c478bd9Sstevel@tonic-gate 	/*
54017c478bd9Sstevel@tonic-gate 	 * Drop last reference (which was added by zsched()), this will
54027c478bd9Sstevel@tonic-gate 	 * free the zone unless there are outstanding cred references.
54037c478bd9Sstevel@tonic-gate 	 */
54047c478bd9Sstevel@tonic-gate 	zone_rele(zone);
54057c478bd9Sstevel@tonic-gate 	return (0);
54067c478bd9Sstevel@tonic-gate }
54077c478bd9Sstevel@tonic-gate 
54087c478bd9Sstevel@tonic-gate /*
54097c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_getattr(2).
54107c478bd9Sstevel@tonic-gate  */
54117c478bd9Sstevel@tonic-gate static ssize_t
zone_getattr(zoneid_t zoneid,int attr,void * buf,size_t bufsize)54127c478bd9Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
54137c478bd9Sstevel@tonic-gate {
54147c478bd9Sstevel@tonic-gate 	size_t size;
54157c478bd9Sstevel@tonic-gate 	int error = 0, err;
54167c478bd9Sstevel@tonic-gate 	zone_t *zone;
54177c478bd9Sstevel@tonic-gate 	char *zonepath;
54183f2f09c1Sdp 	char *outstr;
54197c478bd9Sstevel@tonic-gate 	zone_status_t zone_status;
54207c478bd9Sstevel@tonic-gate 	pid_t initpid;
5421c97ad5cdSakolb 	boolean_t global = (curzone == global_zone);
5422c97ad5cdSakolb 	boolean_t inzone = (curzone->zone_id == zoneid);
5423f4b3ec61Sdh 	ushort_t flags;
5424550b6e40SSowmini Varadhan 	zone_net_data_t *zbuf;
54257c478bd9Sstevel@tonic-gate 
54267c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
54277c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
54287c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
54297c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
54307c478bd9Sstevel@tonic-gate 	}
54317c478bd9Sstevel@tonic-gate 	zone_status = zone_status_get(zone);
5432bd41d0a8Snordmark 	if (zone_status < ZONE_IS_INITIALIZED) {
54337c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
54347c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
54357c478bd9Sstevel@tonic-gate 	}
54367c478bd9Sstevel@tonic-gate 	zone_hold(zone);
54377c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
54387c478bd9Sstevel@tonic-gate 
54397c478bd9Sstevel@tonic-gate 	/*
544045916cd2Sjpk 	 * If not in the global zone, don't show information about other zones,
544145916cd2Sjpk 	 * unless the system is labeled and the local zone's label dominates
544245916cd2Sjpk 	 * the other zone.
54437c478bd9Sstevel@tonic-gate 	 */
544445916cd2Sjpk 	if (!zone_list_access(zone)) {
54457c478bd9Sstevel@tonic-gate 		zone_rele(zone);
54467c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
54477c478bd9Sstevel@tonic-gate 	}
54487c478bd9Sstevel@tonic-gate 
54497c478bd9Sstevel@tonic-gate 	switch (attr) {
54507c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_ROOT:
54517c478bd9Sstevel@tonic-gate 		if (global) {
54527c478bd9Sstevel@tonic-gate 			/*
54537c478bd9Sstevel@tonic-gate 			 * Copy the path to trim the trailing "/" (except for
54547c478bd9Sstevel@tonic-gate 			 * the global zone).
54557c478bd9Sstevel@tonic-gate 			 */
54567c478bd9Sstevel@tonic-gate 			if (zone != global_zone)
54577c478bd9Sstevel@tonic-gate 				size = zone->zone_rootpathlen - 1;
54587c478bd9Sstevel@tonic-gate 			else
54597c478bd9Sstevel@tonic-gate 				size = zone->zone_rootpathlen;
54607c478bd9Sstevel@tonic-gate 			zonepath = kmem_alloc(size, KM_SLEEP);
54617c478bd9Sstevel@tonic-gate 			bcopy(zone->zone_rootpath, zonepath, size);
54627c478bd9Sstevel@tonic-gate 			zonepath[size - 1] = '\0';
54637c478bd9Sstevel@tonic-gate 		} else {
5464c97ad5cdSakolb 			if (inzone || !is_system_labeled()) {
546545916cd2Sjpk 				/*
546645916cd2Sjpk 				 * Caller is not in the global zone.
546745916cd2Sjpk 				 * if the query is on the current zone
546845916cd2Sjpk 				 * or the system is not labeled,
546945916cd2Sjpk 				 * just return faked-up path for current zone.
547045916cd2Sjpk 				 */
547145916cd2Sjpk 				zonepath = "/";
547245916cd2Sjpk 				size = 2;
547345916cd2Sjpk 			} else {
547445916cd2Sjpk 				/*
547545916cd2Sjpk 				 * Return related path for current zone.
547645916cd2Sjpk 				 */
547745916cd2Sjpk 				int prefix_len = strlen(zone_prefix);
547845916cd2Sjpk 				int zname_len = strlen(zone->zone_name);
547945916cd2Sjpk 
548045916cd2Sjpk 				size = prefix_len + zname_len + 1;
548145916cd2Sjpk 				zonepath = kmem_alloc(size, KM_SLEEP);
548245916cd2Sjpk 				bcopy(zone_prefix, zonepath, prefix_len);
548345916cd2Sjpk 				bcopy(zone->zone_name, zonepath +
54843f2f09c1Sdp 				    prefix_len, zname_len);
548545916cd2Sjpk 				zonepath[size - 1] = '\0';
548645916cd2Sjpk 			}
54877c478bd9Sstevel@tonic-gate 		}
54887c478bd9Sstevel@tonic-gate 		if (bufsize > size)
54897c478bd9Sstevel@tonic-gate 			bufsize = size;
54907c478bd9Sstevel@tonic-gate 		if (buf != NULL) {
54917c478bd9Sstevel@tonic-gate 			err = copyoutstr(zonepath, buf, bufsize, NULL);
54927c478bd9Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
54937c478bd9Sstevel@tonic-gate 				error = EFAULT;
54947c478bd9Sstevel@tonic-gate 		}
5495c97ad5cdSakolb 		if (global || (is_system_labeled() && !inzone))
54967c478bd9Sstevel@tonic-gate 			kmem_free(zonepath, size);
54977c478bd9Sstevel@tonic-gate 		break;
54987c478bd9Sstevel@tonic-gate 
54997c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_NAME:
55007c478bd9Sstevel@tonic-gate 		size = strlen(zone->zone_name) + 1;
55017c478bd9Sstevel@tonic-gate 		if (bufsize > size)
55027c478bd9Sstevel@tonic-gate 			bufsize = size;
55037c478bd9Sstevel@tonic-gate 		if (buf != NULL) {
55047c478bd9Sstevel@tonic-gate 			err = copyoutstr(zone->zone_name, buf, bufsize, NULL);
55057c478bd9Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
55067c478bd9Sstevel@tonic-gate 				error = EFAULT;
55077c478bd9Sstevel@tonic-gate 		}
55087c478bd9Sstevel@tonic-gate 		break;
55097c478bd9Sstevel@tonic-gate 
55107c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_STATUS:
55117c478bd9Sstevel@tonic-gate 		/*
55127c478bd9Sstevel@tonic-gate 		 * Since we're not holding zonehash_lock, the zone status
55137c478bd9Sstevel@tonic-gate 		 * may be anything; leave it up to userland to sort it out.
55147c478bd9Sstevel@tonic-gate 		 */
55157c478bd9Sstevel@tonic-gate 		size = sizeof (zone_status);
55167c478bd9Sstevel@tonic-gate 		if (bufsize > size)
55177c478bd9Sstevel@tonic-gate 			bufsize = size;
55187c478bd9Sstevel@tonic-gate 		zone_status = zone_status_get(zone);
55197c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
55207c478bd9Sstevel@tonic-gate 		    copyout(&zone_status, buf, bufsize) != 0)
55217c478bd9Sstevel@tonic-gate 			error = EFAULT;
55227c478bd9Sstevel@tonic-gate 		break;
5523f4b3ec61Sdh 	case ZONE_ATTR_FLAGS:
5524f4b3ec61Sdh 		size = sizeof (zone->zone_flags);
5525f4b3ec61Sdh 		if (bufsize > size)
5526f4b3ec61Sdh 			bufsize = size;
5527f4b3ec61Sdh 		flags = zone->zone_flags;
5528f4b3ec61Sdh 		if (buf != NULL &&
5529f4b3ec61Sdh 		    copyout(&flags, buf, bufsize) != 0)
5530f4b3ec61Sdh 			error = EFAULT;
5531f4b3ec61Sdh 		break;
55327c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_PRIVSET:
55337c478bd9Sstevel@tonic-gate 		size = sizeof (priv_set_t);
55347c478bd9Sstevel@tonic-gate 		if (bufsize > size)
55357c478bd9Sstevel@tonic-gate 			bufsize = size;
55367c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
55377c478bd9Sstevel@tonic-gate 		    copyout(zone->zone_privset, buf, bufsize) != 0)
55387c478bd9Sstevel@tonic-gate 			error = EFAULT;
55397c478bd9Sstevel@tonic-gate 		break;
55407c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_UNIQID:
55417c478bd9Sstevel@tonic-gate 		size = sizeof (zone->zone_uniqid);
55427c478bd9Sstevel@tonic-gate 		if (bufsize > size)
55437c478bd9Sstevel@tonic-gate 			bufsize = size;
55447c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
55457c478bd9Sstevel@tonic-gate 		    copyout(&zone->zone_uniqid, buf, bufsize) != 0)
55467c478bd9Sstevel@tonic-gate 			error = EFAULT;
55477c478bd9Sstevel@tonic-gate 		break;
55487c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_POOLID:
55497c478bd9Sstevel@tonic-gate 		{
55507c478bd9Sstevel@tonic-gate 			pool_t *pool;
55517c478bd9Sstevel@tonic-gate 			poolid_t poolid;
55527c478bd9Sstevel@tonic-gate 
55537c478bd9Sstevel@tonic-gate 			if (pool_lock_intr() != 0) {
55547c478bd9Sstevel@tonic-gate 				error = EINTR;
55557c478bd9Sstevel@tonic-gate 				break;
55567c478bd9Sstevel@tonic-gate 			}
55577c478bd9Sstevel@tonic-gate 			pool = zone_pool_get(zone);
55587c478bd9Sstevel@tonic-gate 			poolid = pool->pool_id;
55597c478bd9Sstevel@tonic-gate 			pool_unlock();
55607c478bd9Sstevel@tonic-gate 			size = sizeof (poolid);
55617c478bd9Sstevel@tonic-gate 			if (bufsize > size)
55627c478bd9Sstevel@tonic-gate 				bufsize = size;
55637c478bd9Sstevel@tonic-gate 			if (buf != NULL && copyout(&poolid, buf, size) != 0)
55647c478bd9Sstevel@tonic-gate 				error = EFAULT;
55657c478bd9Sstevel@tonic-gate 		}
55667c478bd9Sstevel@tonic-gate 		break;
556745916cd2Sjpk 	case ZONE_ATTR_SLBL:
556845916cd2Sjpk 		size = sizeof (bslabel_t);
556945916cd2Sjpk 		if (bufsize > size)
557045916cd2Sjpk 			bufsize = size;
557145916cd2Sjpk 		if (zone->zone_slabel == NULL)
557245916cd2Sjpk 			error = EINVAL;
557345916cd2Sjpk 		else if (buf != NULL &&
557445916cd2Sjpk 		    copyout(label2bslabel(zone->zone_slabel), buf,
557545916cd2Sjpk 		    bufsize) != 0)
557645916cd2Sjpk 			error = EFAULT;
557745916cd2Sjpk 		break;
55787c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_INITPID:
55797c478bd9Sstevel@tonic-gate 		size = sizeof (initpid);
55807c478bd9Sstevel@tonic-gate 		if (bufsize > size)
55817c478bd9Sstevel@tonic-gate 			bufsize = size;
55827c478bd9Sstevel@tonic-gate 		initpid = zone->zone_proc_initpid;
55837c478bd9Sstevel@tonic-gate 		if (initpid == -1) {
55847c478bd9Sstevel@tonic-gate 			error = ESRCH;
55857c478bd9Sstevel@tonic-gate 			break;
55867c478bd9Sstevel@tonic-gate 		}
55877c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
55887c478bd9Sstevel@tonic-gate 		    copyout(&initpid, buf, bufsize) != 0)
55897c478bd9Sstevel@tonic-gate 			error = EFAULT;
55907c478bd9Sstevel@tonic-gate 		break;
55919acbbeafSnn 	case ZONE_ATTR_BRAND:
55929acbbeafSnn 		size = strlen(zone->zone_brand->b_name) + 1;
55939acbbeafSnn 
55949acbbeafSnn 		if (bufsize > size)
55959acbbeafSnn 			bufsize = size;
55969acbbeafSnn 		if (buf != NULL) {
55979acbbeafSnn 			err = copyoutstr(zone->zone_brand->b_name, buf,
55989acbbeafSnn 			    bufsize, NULL);
55999acbbeafSnn 			if (err != 0 && err != ENAMETOOLONG)
56009acbbeafSnn 				error = EFAULT;
56019acbbeafSnn 		}
56029acbbeafSnn 		break;
56033f2f09c1Sdp 	case ZONE_ATTR_INITNAME:
56043f2f09c1Sdp 		size = strlen(zone->zone_initname) + 1;
56053f2f09c1Sdp 		if (bufsize > size)
56063f2f09c1Sdp 			bufsize = size;
56073f2f09c1Sdp 		if (buf != NULL) {
56083f2f09c1Sdp 			err = copyoutstr(zone->zone_initname, buf, bufsize,
56093f2f09c1Sdp 			    NULL);
56103f2f09c1Sdp 			if (err != 0 && err != ENAMETOOLONG)
56113f2f09c1Sdp 				error = EFAULT;
56123f2f09c1Sdp 		}
56133f2f09c1Sdp 		break;
56143f2f09c1Sdp 	case ZONE_ATTR_BOOTARGS:
56153f2f09c1Sdp 		if (zone->zone_bootargs == NULL)
56163f2f09c1Sdp 			outstr = "";
56173f2f09c1Sdp 		else
56183f2f09c1Sdp 			outstr = zone->zone_bootargs;
56193f2f09c1Sdp 		size = strlen(outstr) + 1;
56203f2f09c1Sdp 		if (bufsize > size)
56213f2f09c1Sdp 			bufsize = size;
56223f2f09c1Sdp 		if (buf != NULL) {
56233f2f09c1Sdp 			err = copyoutstr(outstr, buf, bufsize, NULL);
56243f2f09c1Sdp 			if (err != 0 && err != ENAMETOOLONG)
56253f2f09c1Sdp 				error = EFAULT;
56263f2f09c1Sdp 		}
56273f2f09c1Sdp 		break;
56280209230bSgjelinek 	case ZONE_ATTR_PHYS_MCAP:
56290209230bSgjelinek 		size = sizeof (zone->zone_phys_mcap);
56300209230bSgjelinek 		if (bufsize > size)
56310209230bSgjelinek 			bufsize = size;
56320209230bSgjelinek 		if (buf != NULL &&
56330209230bSgjelinek 		    copyout(&zone->zone_phys_mcap, buf, bufsize) != 0)
56340209230bSgjelinek 			error = EFAULT;
56350209230bSgjelinek 		break;
56360209230bSgjelinek 	case ZONE_ATTR_SCHED_CLASS:
56370209230bSgjelinek 		mutex_enter(&class_lock);
56380209230bSgjelinek 
56390209230bSgjelinek 		if (zone->zone_defaultcid >= loaded_classes)
56400209230bSgjelinek 			outstr = "";
56410209230bSgjelinek 		else
56420209230bSgjelinek 			outstr = sclass[zone->zone_defaultcid].cl_name;
56430209230bSgjelinek 		size = strlen(outstr) + 1;
56440209230bSgjelinek 		if (bufsize > size)
56450209230bSgjelinek 			bufsize = size;
56460209230bSgjelinek 		if (buf != NULL) {
56470209230bSgjelinek 			err = copyoutstr(outstr, buf, bufsize, NULL);
56480209230bSgjelinek 			if (err != 0 && err != ENAMETOOLONG)
56490209230bSgjelinek 				error = EFAULT;
56500209230bSgjelinek 		}
56510209230bSgjelinek 
56520209230bSgjelinek 		mutex_exit(&class_lock);
56530209230bSgjelinek 		break;
56545679c89fSjv 	case ZONE_ATTR_HOSTID:
56555679c89fSjv 		if (zone->zone_hostid != HW_INVALID_HOSTID &&
56565679c89fSjv 		    bufsize == sizeof (zone->zone_hostid)) {
56575679c89fSjv 			size = sizeof (zone->zone_hostid);
56585679c89fSjv 			if (buf != NULL && copyout(&zone->zone_hostid, buf,
56595679c89fSjv 			    bufsize) != 0)
56605679c89fSjv 				error = EFAULT;
56615679c89fSjv 		} else {
56625679c89fSjv 			error = EINVAL;
56635679c89fSjv 		}
56645679c89fSjv 		break;
56650fbb751dSJohn Levon 	case ZONE_ATTR_FS_ALLOWED:
56660fbb751dSJohn Levon 		if (zone->zone_fs_allowed == NULL)
56670fbb751dSJohn Levon 			outstr = "";
56680fbb751dSJohn Levon 		else
56690fbb751dSJohn Levon 			outstr = zone->zone_fs_allowed;
56700fbb751dSJohn Levon 		size = strlen(outstr) + 1;
56710fbb751dSJohn Levon 		if (bufsize > size)
56720fbb751dSJohn Levon 			bufsize = size;
56730fbb751dSJohn Levon 		if (buf != NULL) {
56740fbb751dSJohn Levon 			err = copyoutstr(outstr, buf, bufsize, NULL);
56750fbb751dSJohn Levon 			if (err != 0 && err != ENAMETOOLONG)
56760fbb751dSJohn Levon 				error = EFAULT;
56770fbb751dSJohn Levon 		}
56780fbb751dSJohn Levon 		break;
5679d2a70789SRichard Lowe 	case ZONE_ATTR_SECFLAGS:
5680d2a70789SRichard Lowe 		size = sizeof (zone->zone_secflags);
5681d2a70789SRichard Lowe 		if (bufsize > size)
5682d2a70789SRichard Lowe 			bufsize = size;
5683d2a70789SRichard Lowe 		if ((err = copyout(&zone->zone_secflags, buf, bufsize)) != 0)
5684d2a70789SRichard Lowe 			error = EFAULT;
5685d2a70789SRichard Lowe 		break;
5686550b6e40SSowmini Varadhan 	case ZONE_ATTR_NETWORK:
568766d7818bSAndy Fiddaman 		bufsize = MIN(bufsize, PIPE_BUF + sizeof (zone_net_data_t));
568866d7818bSAndy Fiddaman 		size = bufsize;
5689550b6e40SSowmini Varadhan 		zbuf = kmem_alloc(bufsize, KM_SLEEP);
5690550b6e40SSowmini Varadhan 		if (copyin(buf, zbuf, bufsize) != 0) {
5691550b6e40SSowmini Varadhan 			error = EFAULT;
5692550b6e40SSowmini Varadhan 		} else {
5693550b6e40SSowmini Varadhan 			error = zone_get_network(zoneid, zbuf);
5694550b6e40SSowmini Varadhan 			if (error == 0 && copyout(zbuf, buf, bufsize) != 0)
5695550b6e40SSowmini Varadhan 				error = EFAULT;
5696550b6e40SSowmini Varadhan 		}
5697550b6e40SSowmini Varadhan 		kmem_free(zbuf, bufsize);
5698550b6e40SSowmini Varadhan 		break;
56997c478bd9Sstevel@tonic-gate 	default:
57009acbbeafSnn 		if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) {
57019acbbeafSnn 			size = bufsize;
57029acbbeafSnn 			error = ZBROP(zone)->b_getattr(zone, attr, buf, &size);
57039acbbeafSnn 		} else {
57049acbbeafSnn 			error = EINVAL;
57059acbbeafSnn 		}
57067c478bd9Sstevel@tonic-gate 	}
57077c478bd9Sstevel@tonic-gate 	zone_rele(zone);
57087c478bd9Sstevel@tonic-gate 
57097c478bd9Sstevel@tonic-gate 	if (error)
57107c478bd9Sstevel@tonic-gate 		return (set_errno(error));
57117c478bd9Sstevel@tonic-gate 	return ((ssize_t)size);
57127c478bd9Sstevel@tonic-gate }
57137c478bd9Sstevel@tonic-gate 
57143f2f09c1Sdp /*
57153f2f09c1Sdp  * Systemcall entry point for zone_setattr(2).
57163f2f09c1Sdp  */
57173f2f09c1Sdp /*ARGSUSED*/
57183f2f09c1Sdp static int
zone_setattr(zoneid_t zoneid,int attr,void * buf,size_t bufsize)57193f2f09c1Sdp zone_setattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
57203f2f09c1Sdp {
57213f2f09c1Sdp 	zone_t *zone;
57223f2f09c1Sdp 	zone_status_t zone_status;
57231a88ce5cSDan Price 	int err = -1;
5724550b6e40SSowmini Varadhan 	zone_net_data_t *zbuf;
57253f2f09c1Sdp 
57263f2f09c1Sdp 	if (secpolicy_zone_config(CRED()) != 0)
57273f2f09c1Sdp 		return (set_errno(EPERM));
57283f2f09c1Sdp 
57293f2f09c1Sdp 	/*
57300209230bSgjelinek 	 * Only the ZONE_ATTR_PHYS_MCAP attribute can be set on the
57310209230bSgjelinek 	 * global zone.
57323f2f09c1Sdp 	 */
57330209230bSgjelinek 	if (zoneid == GLOBAL_ZONEID && attr != ZONE_ATTR_PHYS_MCAP) {
57343f2f09c1Sdp 		return (set_errno(EINVAL));
57353f2f09c1Sdp 	}
57363f2f09c1Sdp 
57373f2f09c1Sdp 	mutex_enter(&zonehash_lock);
57383f2f09c1Sdp 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
57393f2f09c1Sdp 		mutex_exit(&zonehash_lock);
57403f2f09c1Sdp 		return (set_errno(EINVAL));
57413f2f09c1Sdp 	}
57423f2f09c1Sdp 	zone_hold(zone);
57433f2f09c1Sdp 	mutex_exit(&zonehash_lock);
57443f2f09c1Sdp 
57450209230bSgjelinek 	/*
57460209230bSgjelinek 	 * At present most attributes can only be set on non-running,
57470209230bSgjelinek 	 * non-global zones.
57480209230bSgjelinek 	 */
57493f2f09c1Sdp 	zone_status = zone_status_get(zone);
57501a88ce5cSDan Price 	if (attr != ZONE_ATTR_PHYS_MCAP && zone_status > ZONE_IS_READY) {
57511a88ce5cSDan Price 		err = EINVAL;
57523f2f09c1Sdp 		goto done;
57531a88ce5cSDan Price 	}
57543f2f09c1Sdp 
57553f2f09c1Sdp 	switch (attr) {
57563f2f09c1Sdp 	case ZONE_ATTR_INITNAME:
57573f2f09c1Sdp 		err = zone_set_initname(zone, (const char *)buf);
57583f2f09c1Sdp 		break;
5759bafd1f14SJerry Jelinek 	case ZONE_ATTR_INITNORESTART:
5760bafd1f14SJerry Jelinek 		zone->zone_restart_init = B_FALSE;
5761bafd1f14SJerry Jelinek 		err = 0;
5762bafd1f14SJerry Jelinek 		break;
576355fcd84fSJerry Jelinek 	case ZONE_ATTR_INITRESTART0:
576455fcd84fSJerry Jelinek 		zone->zone_restart_init_0 = B_TRUE;
576555fcd84fSJerry Jelinek 		err = 0;
576655fcd84fSJerry Jelinek 		break;
576755fcd84fSJerry Jelinek 	case ZONE_ATTR_INITREBOOT:
576855fcd84fSJerry Jelinek 		zone->zone_reboot_on_init_exit = B_TRUE;
576955fcd84fSJerry Jelinek 		err = 0;
577055fcd84fSJerry Jelinek 		break;
57713f2f09c1Sdp 	case ZONE_ATTR_BOOTARGS:
57723f2f09c1Sdp 		err = zone_set_bootargs(zone, (const char *)buf);
57733f2f09c1Sdp 		break;
57749acbbeafSnn 	case ZONE_ATTR_BRAND:
577559f2ff5cSedp 		err = zone_set_brand(zone, (const char *)buf);
57769acbbeafSnn 		break;
57770fbb751dSJohn Levon 	case ZONE_ATTR_FS_ALLOWED:
57780fbb751dSJohn Levon 		err = zone_set_fs_allowed(zone, (const char *)buf);
57790fbb751dSJohn Levon 		break;
5780d2a70789SRichard Lowe 	case ZONE_ATTR_SECFLAGS:
5781d2a70789SRichard Lowe 		err = zone_set_secflags(zone, (psecflags_t *)buf);
5782d2a70789SRichard Lowe 		break;
57830209230bSgjelinek 	case ZONE_ATTR_PHYS_MCAP:
57840209230bSgjelinek 		err = zone_set_phys_mcap(zone, (const uint64_t *)buf);
57850209230bSgjelinek 		break;
57860209230bSgjelinek 	case ZONE_ATTR_SCHED_CLASS:
57870209230bSgjelinek 		err = zone_set_sched_class(zone, (const char *)buf);
57880209230bSgjelinek 		break;
57895679c89fSjv 	case ZONE_ATTR_HOSTID:
57905679c89fSjv 		if (bufsize == sizeof (zone->zone_hostid)) {
57915679c89fSjv 			if (copyin(buf, &zone->zone_hostid, bufsize) == 0)
57925679c89fSjv 				err = 0;
57935679c89fSjv 			else
57945679c89fSjv 				err = EFAULT;
57955679c89fSjv 		} else {
57965679c89fSjv 			err = EINVAL;
57975679c89fSjv 		}
57985679c89fSjv 		break;
5799550b6e40SSowmini Varadhan 	case ZONE_ATTR_NETWORK:
5800550b6e40SSowmini Varadhan 		if (bufsize > (PIPE_BUF + sizeof (zone_net_data_t))) {
5801550b6e40SSowmini Varadhan 			err = EINVAL;
58021a88ce5cSDan Price 			break;
5803550b6e40SSowmini Varadhan 		}
5804550b6e40SSowmini Varadhan 		zbuf = kmem_alloc(bufsize, KM_SLEEP);
5805550b6e40SSowmini Varadhan 		if (copyin(buf, zbuf, bufsize) != 0) {
58061a88ce5cSDan Price 			kmem_free(zbuf, bufsize);
5807550b6e40SSowmini Varadhan 			err = EFAULT;
58081a88ce5cSDan Price 			break;
5809550b6e40SSowmini Varadhan 		}
5810550b6e40SSowmini Varadhan 		err = zone_set_network(zoneid, zbuf);
5811550b6e40SSowmini Varadhan 		kmem_free(zbuf, bufsize);
5812550b6e40SSowmini Varadhan 		break;
58133f2f09c1Sdp 	default:
58149acbbeafSnn 		if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone))
58159acbbeafSnn 			err = ZBROP(zone)->b_setattr(zone, attr, buf, bufsize);
58169acbbeafSnn 		else
58179acbbeafSnn 			err = EINVAL;
58183f2f09c1Sdp 	}
58193f2f09c1Sdp 
58203f2f09c1Sdp done:
58213f2f09c1Sdp 	zone_rele(zone);
58221a88ce5cSDan Price 	ASSERT(err != -1);
58233f2f09c1Sdp 	return (err != 0 ? set_errno(err) : 0);
58243f2f09c1Sdp }
58253f2f09c1Sdp 
58267c478bd9Sstevel@tonic-gate /*
58277c478bd9Sstevel@tonic-gate  * Return zero if the process has at least one vnode mapped in to its
58287c478bd9Sstevel@tonic-gate  * address space which shouldn't be allowed to change zones.
58290209230bSgjelinek  *
58300209230bSgjelinek  * Also return zero if the process has any shared mappings which reserve
58310209230bSgjelinek  * swap.  This is because the counting for zone.max-swap does not allow swap
5832da6c28aaSamw  * reservation to be shared between zones.  zone swap reservation is counted
58330209230bSgjelinek  * on zone->zone_max_swap.
58347c478bd9Sstevel@tonic-gate  */
58357c478bd9Sstevel@tonic-gate static int
as_can_change_zones(void)58367c478bd9Sstevel@tonic-gate as_can_change_zones(void)
58377c478bd9Sstevel@tonic-gate {
58387c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
58397c478bd9Sstevel@tonic-gate 	struct seg *seg;
58407c478bd9Sstevel@tonic-gate 	struct as *as = pp->p_as;
58417c478bd9Sstevel@tonic-gate 	vnode_t *vp;
58427c478bd9Sstevel@tonic-gate 	int allow = 1;
58437c478bd9Sstevel@tonic-gate 
58447c478bd9Sstevel@tonic-gate 	ASSERT(pp->p_as != &kas);
5845dc32d872SJosef 'Jeff' Sipek 	AS_LOCK_ENTER(as, RW_READER);
58467c478bd9Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
58470209230bSgjelinek 
58480209230bSgjelinek 		/*
58490209230bSgjelinek 		 * Cannot enter zone with shared anon memory which
58500209230bSgjelinek 		 * reserves swap.  See comment above.
58510209230bSgjelinek 		 */
58520209230bSgjelinek 		if (seg_can_change_zones(seg) == B_FALSE) {
58530209230bSgjelinek 			allow = 0;
58540209230bSgjelinek 			break;
58550209230bSgjelinek 		}
58567c478bd9Sstevel@tonic-gate 		/*
58577c478bd9Sstevel@tonic-gate 		 * if we can't get a backing vnode for this segment then skip
58587c478bd9Sstevel@tonic-gate 		 * it.
58597c478bd9Sstevel@tonic-gate 		 */
58607c478bd9Sstevel@tonic-gate 		vp = NULL;
58617c478bd9Sstevel@tonic-gate 		if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL)
58627c478bd9Sstevel@tonic-gate 			continue;
58637c478bd9Sstevel@tonic-gate 		if (!vn_can_change_zones(vp)) { /* bail on first match */
58647c478bd9Sstevel@tonic-gate 			allow = 0;
58657c478bd9Sstevel@tonic-gate 			break;
58667c478bd9Sstevel@tonic-gate 		}
58677c478bd9Sstevel@tonic-gate 	}
5868dc32d872SJosef 'Jeff' Sipek 	AS_LOCK_EXIT(as);
58697c478bd9Sstevel@tonic-gate 	return (allow);
58707c478bd9Sstevel@tonic-gate }
58717c478bd9Sstevel@tonic-gate 
58720209230bSgjelinek /*
58730209230bSgjelinek  * Count swap reserved by curproc's address space
58740209230bSgjelinek  */
58750209230bSgjelinek static size_t
as_swresv(void)58760209230bSgjelinek as_swresv(void)
58770209230bSgjelinek {
58780209230bSgjelinek 	proc_t *pp = curproc;
58790209230bSgjelinek 	struct seg *seg;
58800209230bSgjelinek 	struct as *as = pp->p_as;
58810209230bSgjelinek 	size_t swap = 0;
58820209230bSgjelinek 
58830209230bSgjelinek 	ASSERT(pp->p_as != &kas);
5884dc32d872SJosef 'Jeff' Sipek 	ASSERT(AS_WRITE_HELD(as));
58850209230bSgjelinek 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg))
58860209230bSgjelinek 		swap += seg_swresv(seg);
58870209230bSgjelinek 
58880209230bSgjelinek 	return (swap);
58890209230bSgjelinek }
58900209230bSgjelinek 
58917c478bd9Sstevel@tonic-gate /*
58927c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_enter().
58937c478bd9Sstevel@tonic-gate  *
58947c478bd9Sstevel@tonic-gate  * The current process is injected into said zone.  In the process
58957c478bd9Sstevel@tonic-gate  * it will change its project membership, privileges, rootdir/cwd,
58967c478bd9Sstevel@tonic-gate  * zone-wide rctls, and pool association to match those of the zone.
58977c478bd9Sstevel@tonic-gate  *
58987c478bd9Sstevel@tonic-gate  * The first zone_enter() called while the zone is in the ZONE_IS_READY
58997c478bd9Sstevel@tonic-gate  * state will transition it to ZONE_IS_RUNNING.  Processes may only
59007c478bd9Sstevel@tonic-gate  * enter a zone that is "ready" or "running".
59017c478bd9Sstevel@tonic-gate  */
59027c478bd9Sstevel@tonic-gate static int
zone_enter(zoneid_t zoneid)59037c478bd9Sstevel@tonic-gate zone_enter(zoneid_t zoneid)
59047c478bd9Sstevel@tonic-gate {
59057c478bd9Sstevel@tonic-gate 	zone_t *zone;
59067c478bd9Sstevel@tonic-gate 	vnode_t *vp;
59077c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
59087c478bd9Sstevel@tonic-gate 	contract_t *ct;
59097c478bd9Sstevel@tonic-gate 	cont_process_t *ctp;
59107c478bd9Sstevel@tonic-gate 	task_t *tk, *oldtk;
59117c478bd9Sstevel@tonic-gate 	kproject_t *zone_proj0;
59127c478bd9Sstevel@tonic-gate 	cred_t *cr, *newcr;
59137c478bd9Sstevel@tonic-gate 	pool_t *oldpool, *newpool;
59147c478bd9Sstevel@tonic-gate 	sess_t *sp;
59157c478bd9Sstevel@tonic-gate 	uid_t uid;
59167c478bd9Sstevel@tonic-gate 	zone_status_t status;
59177c478bd9Sstevel@tonic-gate 	int err = 0;
59187c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
59190209230bSgjelinek 	size_t swap;
5920c97ad5cdSakolb 	kthread_id_t t;
59217c478bd9Sstevel@tonic-gate 
59227c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
59237c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
59247c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
59257c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
59267c478bd9Sstevel@tonic-gate 
59277c478bd9Sstevel@tonic-gate 	/*
59287c478bd9Sstevel@tonic-gate 	 * Stop all lwps so we don't need to hold a lock to look at
59297c478bd9Sstevel@tonic-gate 	 * curproc->p_zone.  This needs to happen before we grab any
59307c478bd9Sstevel@tonic-gate 	 * locks to avoid deadlock (another lwp in the process could
59317c478bd9Sstevel@tonic-gate 	 * be waiting for the held lock).
59327c478bd9Sstevel@tonic-gate 	 */
59337c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK))
59347c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
59357c478bd9Sstevel@tonic-gate 
59367c478bd9Sstevel@tonic-gate 	/*
59377c478bd9Sstevel@tonic-gate 	 * Make sure we're not changing zones with files open or mapped in
59387c478bd9Sstevel@tonic-gate 	 * to our address space which shouldn't be changing zones.
59397c478bd9Sstevel@tonic-gate 	 */
59407c478bd9Sstevel@tonic-gate 	if (!files_can_change_zones()) {
59417c478bd9Sstevel@tonic-gate 		err = EBADF;
59427c478bd9Sstevel@tonic-gate 		goto out;
59437c478bd9Sstevel@tonic-gate 	}
59447c478bd9Sstevel@tonic-gate 	if (!as_can_change_zones()) {
59457c478bd9Sstevel@tonic-gate 		err = EFAULT;
59467c478bd9Sstevel@tonic-gate 		goto out;
59477c478bd9Sstevel@tonic-gate 	}
59487c478bd9Sstevel@tonic-gate 
59497c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
59507c478bd9Sstevel@tonic-gate 	if (pp->p_zone != global_zone) {
59517c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
59527c478bd9Sstevel@tonic-gate 		err = EINVAL;
59537c478bd9Sstevel@tonic-gate 		goto out;
59547c478bd9Sstevel@tonic-gate 	}
59557c478bd9Sstevel@tonic-gate 
59567c478bd9Sstevel@tonic-gate 	zone = zone_find_all_by_id(zoneid);
59577c478bd9Sstevel@tonic-gate 	if (zone == NULL) {
59587c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
59597c478bd9Sstevel@tonic-gate 		err = EINVAL;
59607c478bd9Sstevel@tonic-gate 		goto out;
59617c478bd9Sstevel@tonic-gate 	}
59627c478bd9Sstevel@tonic-gate 
59637c478bd9Sstevel@tonic-gate 	/*
59647c478bd9Sstevel@tonic-gate 	 * To prevent processes in a zone from holding contracts on
59657c478bd9Sstevel@tonic-gate 	 * extrazonal resources, and to avoid process contract
59667c478bd9Sstevel@tonic-gate 	 * memberships which span zones, contract holders and processes
59677c478bd9Sstevel@tonic-gate 	 * which aren't the sole members of their encapsulating process
59687c478bd9Sstevel@tonic-gate 	 * contracts are not allowed to zone_enter.
59697c478bd9Sstevel@tonic-gate 	 */
59707c478bd9Sstevel@tonic-gate 	ctp = pp->p_ct_process;
59717c478bd9Sstevel@tonic-gate 	ct = &ctp->conp_contract;
59727c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
59737c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
59747c478bd9Sstevel@tonic-gate 	if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) {
59757c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
59767c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
59777c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
59787c478bd9Sstevel@tonic-gate 		err = EINVAL;
59797c478bd9Sstevel@tonic-gate 		goto out;
59807c478bd9Sstevel@tonic-gate 	}
59817c478bd9Sstevel@tonic-gate 
59827c478bd9Sstevel@tonic-gate 	/*
59837c478bd9Sstevel@tonic-gate 	 * Moreover, we don't allow processes whose encapsulating
59847c478bd9Sstevel@tonic-gate 	 * process contracts have inherited extrazonal contracts.
59857c478bd9Sstevel@tonic-gate 	 * While it would be easier to eliminate all process contracts
59867c478bd9Sstevel@tonic-gate 	 * with inherited contracts, we need to be able to give a
59877c478bd9Sstevel@tonic-gate 	 * restarted init (or other zone-penetrating process) its
59887c478bd9Sstevel@tonic-gate 	 * predecessor's contracts.
59897c478bd9Sstevel@tonic-gate 	 */
59907c478bd9Sstevel@tonic-gate 	if (ctp->conp_ninherited != 0) {
59917c478bd9Sstevel@tonic-gate 		contract_t *next;
59927c478bd9Sstevel@tonic-gate 		for (next = list_head(&ctp->conp_inherited); next;
59937c478bd9Sstevel@tonic-gate 		    next = list_next(&ctp->conp_inherited, next)) {
59947c478bd9Sstevel@tonic-gate 			if (contract_getzuniqid(next) != zone->zone_uniqid) {
59957c478bd9Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
59967c478bd9Sstevel@tonic-gate 				mutex_exit(&ct->ct_lock);
59977c478bd9Sstevel@tonic-gate 				mutex_exit(&zonehash_lock);
59987c478bd9Sstevel@tonic-gate 				err = EINVAL;
59997c478bd9Sstevel@tonic-gate 				goto out;
60007c478bd9Sstevel@tonic-gate 			}
60017c478bd9Sstevel@tonic-gate 		}
60027c478bd9Sstevel@tonic-gate 	}
60037b209c2cSacruz 
60047c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
60057c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
60067c478bd9Sstevel@tonic-gate 
60077c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
60087c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) {
60097c478bd9Sstevel@tonic-gate 		/*
60107c478bd9Sstevel@tonic-gate 		 * Can't join
60117c478bd9Sstevel@tonic-gate 		 */
60127c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
60137c478bd9Sstevel@tonic-gate 		err = EINVAL;
60147c478bd9Sstevel@tonic-gate 		goto out;
60157c478bd9Sstevel@tonic-gate 	}
60167c478bd9Sstevel@tonic-gate 
60177c478bd9Sstevel@tonic-gate 	/*
60187c478bd9Sstevel@tonic-gate 	 * Make sure new priv set is within the permitted set for caller
60197c478bd9Sstevel@tonic-gate 	 */
60207c478bd9Sstevel@tonic-gate 	if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) {
60217c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
60227c478bd9Sstevel@tonic-gate 		err = EPERM;
60237c478bd9Sstevel@tonic-gate 		goto out;
60247c478bd9Sstevel@tonic-gate 	}
60257c478bd9Sstevel@tonic-gate 	/*
60267c478bd9Sstevel@tonic-gate 	 * We want to momentarily drop zonehash_lock while we optimistically
60277c478bd9Sstevel@tonic-gate 	 * bind curproc to the pool it should be running in.  This is safe
60287c478bd9Sstevel@tonic-gate 	 * since the zone can't disappear (we have a hold on it).
60297c478bd9Sstevel@tonic-gate 	 */
60307c478bd9Sstevel@tonic-gate 	zone_hold(zone);
60317c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
60327c478bd9Sstevel@tonic-gate 
60337c478bd9Sstevel@tonic-gate 	/*
60347c478bd9Sstevel@tonic-gate 	 * Grab pool_lock to keep the pools configuration from changing
60357c478bd9Sstevel@tonic-gate 	 * and to stop ourselves from getting rebound to another pool
60367c478bd9Sstevel@tonic-gate 	 * until we join the zone.
60377c478bd9Sstevel@tonic-gate 	 */
60387c478bd9Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
60397c478bd9Sstevel@tonic-gate 		zone_rele(zone);
60407c478bd9Sstevel@tonic-gate 		err = EINTR;
60417c478bd9Sstevel@tonic-gate 		goto out;
60427c478bd9Sstevel@tonic-gate 	}
60437c478bd9Sstevel@tonic-gate 	ASSERT(secpolicy_pool(CRED()) == 0);
60447c478bd9Sstevel@tonic-gate 	/*
60457c478bd9Sstevel@tonic-gate 	 * Bind ourselves to the pool currently associated with the zone.
60467c478bd9Sstevel@tonic-gate 	 */
60477c478bd9Sstevel@tonic-gate 	oldpool = curproc->p_pool;
60487c478bd9Sstevel@tonic-gate 	newpool = zone_pool_get(zone);
60497c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED && newpool != oldpool &&
60507c478bd9Sstevel@tonic-gate 	    (err = pool_do_bind(newpool, P_PID, P_MYID,
60517c478bd9Sstevel@tonic-gate 	    POOL_BIND_ALL)) != 0) {
60527c478bd9Sstevel@tonic-gate 		pool_unlock();
60537c478bd9Sstevel@tonic-gate 		zone_rele(zone);
60547c478bd9Sstevel@tonic-gate 		goto out;
60557c478bd9Sstevel@tonic-gate 	}
60567c478bd9Sstevel@tonic-gate 
60577c478bd9Sstevel@tonic-gate 	/*
60587c478bd9Sstevel@tonic-gate 	 * Grab cpu_lock now; we'll need it later when we call
60597c478bd9Sstevel@tonic-gate 	 * task_join().
60607c478bd9Sstevel@tonic-gate 	 */
60617c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
60627c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
60637c478bd9Sstevel@tonic-gate 	/*
60647c478bd9Sstevel@tonic-gate 	 * Make sure the zone hasn't moved on since we dropped zonehash_lock.
60657c478bd9Sstevel@tonic-gate 	 */
60667c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
60677c478bd9Sstevel@tonic-gate 		/*
60687c478bd9Sstevel@tonic-gate 		 * Can't join anymore.
60697c478bd9Sstevel@tonic-gate 		 */
60707c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
60717c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
60727c478bd9Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED &&
60737c478bd9Sstevel@tonic-gate 		    newpool != oldpool)
60747c478bd9Sstevel@tonic-gate 			(void) pool_do_bind(oldpool, P_PID, P_MYID,
60757c478bd9Sstevel@tonic-gate 			    POOL_BIND_ALL);
60767c478bd9Sstevel@tonic-gate 		pool_unlock();
60777c478bd9Sstevel@tonic-gate 		zone_rele(zone);
60787c478bd9Sstevel@tonic-gate 		err = EINVAL;
60797c478bd9Sstevel@tonic-gate 		goto out;
60807c478bd9Sstevel@tonic-gate 	}
60817c478bd9Sstevel@tonic-gate 
60820209230bSgjelinek 	/*
60830209230bSgjelinek 	 * a_lock must be held while transfering locked memory and swap
60840209230bSgjelinek 	 * reservation from the global zone to the non global zone because
60850209230bSgjelinek 	 * asynchronous faults on the processes' address space can lock
60860209230bSgjelinek 	 * memory and reserve swap via MCL_FUTURE and MAP_NORESERVE
60870209230bSgjelinek 	 * segments respectively.
60880209230bSgjelinek 	 */
6089dc32d872SJosef 'Jeff' Sipek 	AS_LOCK_ENTER(pp->p_as, RW_WRITER);
60900209230bSgjelinek 	swap = as_swresv();
60917c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
60927c478bd9Sstevel@tonic-gate 	zone_proj0 = zone->zone_zsched->p_task->tk_proj;
60937c478bd9Sstevel@tonic-gate 	/* verify that we do not exceed and task or lwp limits */
60947c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
60957c478bd9Sstevel@tonic-gate 	/* add new lwps to zone and zone's proj0 */
60967c478bd9Sstevel@tonic-gate 	zone_proj0->kpj_nlwps += pp->p_lwpcnt;
60977c478bd9Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
60987c478bd9Sstevel@tonic-gate 	/* add 1 task to zone's proj0 */
60997c478bd9Sstevel@tonic-gate 	zone_proj0->kpj_ntasks += 1;
6100ff19e029SMenno Lageman 
6101ff19e029SMenno Lageman 	zone_proj0->kpj_nprocs++;
6102ff19e029SMenno Lageman 	zone->zone_nprocs++;
61037c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
61047c478bd9Sstevel@tonic-gate 
61050209230bSgjelinek 	mutex_enter(&zone->zone_mem_lock);
6106c6939658Ssl 	zone->zone_locked_mem += pp->p_locked_mem;
6107c6939658Ssl 	zone_proj0->kpj_data.kpd_locked_mem += pp->p_locked_mem;
61080209230bSgjelinek 	zone->zone_max_swap += swap;
61090209230bSgjelinek 	mutex_exit(&zone->zone_mem_lock);
6110c6939658Ssl 
6111c1a9a9c3Skrishna 	mutex_enter(&(zone_proj0->kpj_data.kpd_crypto_lock));
6112c1a9a9c3Skrishna 	zone_proj0->kpj_data.kpd_crypto_mem += pp->p_crypto_mem;
6113c1a9a9c3Skrishna 	mutex_exit(&(zone_proj0->kpj_data.kpd_crypto_lock));
6114c1a9a9c3Skrishna 
6115ff19e029SMenno Lageman 	/* remove lwps and process from proc's old zone and old project */
61167c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_zone->zone_nlwps_lock);
61177c478bd9Sstevel@tonic-gate 	pp->p_zone->zone_nlwps -= pp->p_lwpcnt;
61187c478bd9Sstevel@tonic-gate 	pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt;
6119ff19e029SMenno Lageman 	pp->p_task->tk_proj->kpj_nprocs--;
6120ff19e029SMenno Lageman 	pp->p_zone->zone_nprocs--;
61217c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_zone->zone_nlwps_lock);
61227c478bd9Sstevel@tonic-gate 
61230209230bSgjelinek 	mutex_enter(&pp->p_zone->zone_mem_lock);
6124c6939658Ssl 	pp->p_zone->zone_locked_mem -= pp->p_locked_mem;
6125c6939658Ssl 	pp->p_task->tk_proj->kpj_data.kpd_locked_mem -= pp->p_locked_mem;
61260209230bSgjelinek 	pp->p_zone->zone_max_swap -= swap;
61270209230bSgjelinek 	mutex_exit(&pp->p_zone->zone_mem_lock);
6128c6939658Ssl 
6129c1a9a9c3Skrishna 	mutex_enter(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock));
6130c1a9a9c3Skrishna 	pp->p_task->tk_proj->kpj_data.kpd_crypto_mem -= pp->p_crypto_mem;
6131c1a9a9c3Skrishna 	mutex_exit(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock));
6132c1a9a9c3Skrishna 
6133bb5ca623SVamsi Nagineni 	pp->p_flag |= SZONETOP;
6134bb5ca623SVamsi Nagineni 	pp->p_zone = zone;
6135c6939658Ssl 	mutex_exit(&pp->p_lock);
6136dc32d872SJosef 'Jeff' Sipek 	AS_LOCK_EXIT(pp->p_as);
6137c6939658Ssl 
61387c478bd9Sstevel@tonic-gate 	/*
61397c478bd9Sstevel@tonic-gate 	 * Joining the zone cannot fail from now on.
61407c478bd9Sstevel@tonic-gate 	 *
61417c478bd9Sstevel@tonic-gate 	 * This means that a lot of the following code can be commonized and
61427c478bd9Sstevel@tonic-gate 	 * shared with zsched().
61437c478bd9Sstevel@tonic-gate 	 */
61447c478bd9Sstevel@tonic-gate 
61457b209c2cSacruz 	/*
61467b209c2cSacruz 	 * If the process contract fmri was inherited, we need to
61477b209c2cSacruz 	 * flag this so that any contract status will not leak
61487b209c2cSacruz 	 * extra zone information, svc_fmri in this case
61497b209c2cSacruz 	 */
61507b209c2cSacruz 	if (ctp->conp_svc_ctid != ct->ct_id) {
61517b209c2cSacruz 		mutex_enter(&ct->ct_lock);
61527b209c2cSacruz 		ctp->conp_svc_zone_enter = ct->ct_id;
61537b209c2cSacruz 		mutex_exit(&ct->ct_lock);
61547b209c2cSacruz 	}
61557b209c2cSacruz 
61567c478bd9Sstevel@tonic-gate 	/*
61577c478bd9Sstevel@tonic-gate 	 * Reset the encapsulating process contract's zone.
61587c478bd9Sstevel@tonic-gate 	 */
61597c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID);
61607c478bd9Sstevel@tonic-gate 	contract_setzuniqid(ct, zone->zone_uniqid);
61617c478bd9Sstevel@tonic-gate 
61627c478bd9Sstevel@tonic-gate 	/*
61637c478bd9Sstevel@tonic-gate 	 * Create a new task and associate the process with the project keyed
61647c478bd9Sstevel@tonic-gate 	 * by (projid,zoneid).
61657c478bd9Sstevel@tonic-gate 	 *
61667c478bd9Sstevel@tonic-gate 	 * We might as well be in project 0; the global zone's projid doesn't
61677c478bd9Sstevel@tonic-gate 	 * make much sense in a zone anyhow.
61687c478bd9Sstevel@tonic-gate 	 *
61697c478bd9Sstevel@tonic-gate 	 * This also increments zone_ntasks, and returns with p_lock held.
61707c478bd9Sstevel@tonic-gate 	 */
61717c478bd9Sstevel@tonic-gate 	tk = task_create(0, zone);
61727c478bd9Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
61737c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
61747c478bd9Sstevel@tonic-gate 
61757c478bd9Sstevel@tonic-gate 	/*
61767c478bd9Sstevel@tonic-gate 	 * call RCTLOP_SET functions on this proc
61777c478bd9Sstevel@tonic-gate 	 */
61787c478bd9Sstevel@tonic-gate 	e.rcep_p.zone = zone;
61797c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
61807c478bd9Sstevel@tonic-gate 	(void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL,
61817c478bd9Sstevel@tonic-gate 	    RCD_CALLBACK);
61827c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
61837c478bd9Sstevel@tonic-gate 
61847c478bd9Sstevel@tonic-gate 	/*
61857c478bd9Sstevel@tonic-gate 	 * We don't need to hold any of zsched's locks here; not only do we know
61867c478bd9Sstevel@tonic-gate 	 * the process and zone aren't going away, we know its session isn't
61877c478bd9Sstevel@tonic-gate 	 * changing either.
61887c478bd9Sstevel@tonic-gate 	 *
61897c478bd9Sstevel@tonic-gate 	 * By joining zsched's session here, we mimic the behavior in the
61907c478bd9Sstevel@tonic-gate 	 * global zone of init's sid being the pid of sched.  We extend this
61917c478bd9Sstevel@tonic-gate 	 * to all zlogin-like zone_enter()'ing processes as well.
61927c478bd9Sstevel@tonic-gate 	 */
61937c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
61947c478bd9Sstevel@tonic-gate 	sp = zone->zone_zsched->p_sessp;
61959acbbeafSnn 	sess_hold(zone->zone_zsched);
61967c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
61977c478bd9Sstevel@tonic-gate 	pgexit(pp);
61989acbbeafSnn 	sess_rele(pp->p_sessp, B_TRUE);
61997c478bd9Sstevel@tonic-gate 	pp->p_sessp = sp;
62007c478bd9Sstevel@tonic-gate 	pgjoin(pp, zone->zone_zsched->p_pidp);
62010209230bSgjelinek 
6202c97ad5cdSakolb 	/*
6203c97ad5cdSakolb 	 * If any threads are scheduled to be placed on zone wait queue they
6204c97ad5cdSakolb 	 * should abandon the idea since the wait queue is changing.
6205c97ad5cdSakolb 	 * We need to be holding pidlock & p_lock to do this.
6206c97ad5cdSakolb 	 */
6207c97ad5cdSakolb 	if ((t = pp->p_tlist) != NULL) {
6208c97ad5cdSakolb 		do {
6209c97ad5cdSakolb 			thread_lock(t);
6210c97ad5cdSakolb 			/*
621148bbca81SDaniel Hoffman 			 * Kick this thread so that it doesn't sit
6212c97ad5cdSakolb 			 * on a wrong wait queue.
6213c97ad5cdSakolb 			 */
6214c97ad5cdSakolb 			if (ISWAITING(t))
6215c97ad5cdSakolb 				setrun_locked(t);
6216c97ad5cdSakolb 
6217c97ad5cdSakolb 			if (t->t_schedflag & TS_ANYWAITQ)
6218c97ad5cdSakolb 				t->t_schedflag &= ~ TS_ANYWAITQ;
6219c97ad5cdSakolb 
6220c97ad5cdSakolb 			thread_unlock(t);
6221c97ad5cdSakolb 		} while ((t = t->t_forw) != pp->p_tlist);
6222c97ad5cdSakolb 	}
6223c97ad5cdSakolb 
62240209230bSgjelinek 	/*
62250209230bSgjelinek 	 * If there is a default scheduling class for the zone and it is not
62260209230bSgjelinek 	 * the class we are currently in, change all of the threads in the
62270209230bSgjelinek 	 * process to the new class.  We need to be holding pidlock & p_lock
62280209230bSgjelinek 	 * when we call parmsset so this is a good place to do it.
62290209230bSgjelinek 	 */
62300209230bSgjelinek 	if (zone->zone_defaultcid > 0 &&
62310209230bSgjelinek 	    zone->zone_defaultcid != curthread->t_cid) {
62320209230bSgjelinek 		pcparms_t pcparms;
62330209230bSgjelinek 
62340209230bSgjelinek 		pcparms.pc_cid = zone->zone_defaultcid;
62350209230bSgjelinek 		pcparms.pc_clparms[0] = 0;
62360209230bSgjelinek 
62370209230bSgjelinek 		/*
62380209230bSgjelinek 		 * If setting the class fails, we still want to enter the zone.
62390209230bSgjelinek 		 */
62400209230bSgjelinek 		if ((t = pp->p_tlist) != NULL) {
62410209230bSgjelinek 			do {
62420209230bSgjelinek 				(void) parmsset(&pcparms, t);
62430209230bSgjelinek 			} while ((t = t->t_forw) != pp->p_tlist);
62440209230bSgjelinek 		}
62450209230bSgjelinek 	}
62460209230bSgjelinek 
62477c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
62487c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
62497c478bd9Sstevel@tonic-gate 
62507c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
62517c478bd9Sstevel@tonic-gate 	/*
62527c478bd9Sstevel@tonic-gate 	 * We're firmly in the zone; let pools progress.
62537c478bd9Sstevel@tonic-gate 	 */
62547c478bd9Sstevel@tonic-gate 	pool_unlock();
62557c478bd9Sstevel@tonic-gate 	task_rele(oldtk);
62567c478bd9Sstevel@tonic-gate 	/*
62577c478bd9Sstevel@tonic-gate 	 * We don't need to retain a hold on the zone since we already
62587c478bd9Sstevel@tonic-gate 	 * incremented zone_ntasks, so the zone isn't going anywhere.
62597c478bd9Sstevel@tonic-gate 	 */
62607c478bd9Sstevel@tonic-gate 	zone_rele(zone);
62617c478bd9Sstevel@tonic-gate 
62627c478bd9Sstevel@tonic-gate 	/*
62637c478bd9Sstevel@tonic-gate 	 * Chroot
62647c478bd9Sstevel@tonic-gate 	 */
62657c478bd9Sstevel@tonic-gate 	vp = zone->zone_rootvp;
62667c478bd9Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_cdir, pp);
62677c478bd9Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_rdir, pp);
62687c478bd9Sstevel@tonic-gate 
6269d2a70789SRichard Lowe 	/*
6270d2a70789SRichard Lowe 	 * Change process security flags.  Note that the _effective_ flags
6271d2a70789SRichard Lowe 	 * cannot change
6272d2a70789SRichard Lowe 	 */
6273d2a70789SRichard Lowe 	secflags_copy(&pp->p_secflags.psf_lower,
6274d2a70789SRichard Lowe 	    &zone->zone_secflags.psf_lower);
6275d2a70789SRichard Lowe 	secflags_copy(&pp->p_secflags.psf_upper,
6276d2a70789SRichard Lowe 	    &zone->zone_secflags.psf_upper);
6277d2a70789SRichard Lowe 	secflags_copy(&pp->p_secflags.psf_inherit,
6278d2a70789SRichard Lowe 	    &zone->zone_secflags.psf_inherit);
6279d2a70789SRichard Lowe 
62807c478bd9Sstevel@tonic-gate 	/*
62817c478bd9Sstevel@tonic-gate 	 * Change process credentials
62827c478bd9Sstevel@tonic-gate 	 */
62837c478bd9Sstevel@tonic-gate 	newcr = cralloc();
62847c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
62857c478bd9Sstevel@tonic-gate 	cr = pp->p_cred;
62867c478bd9Sstevel@tonic-gate 	crcopy_to(cr, newcr);
62877c478bd9Sstevel@tonic-gate 	crsetzone(newcr, zone);
62887c478bd9Sstevel@tonic-gate 	pp->p_cred = newcr;
62897c478bd9Sstevel@tonic-gate 
62907c478bd9Sstevel@tonic-gate 	/*
62917c478bd9Sstevel@tonic-gate 	 * Restrict all process privilege sets to zone limit
62927c478bd9Sstevel@tonic-gate 	 */
62937c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(newcr));
62947c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(newcr));
62957c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(newcr));
62967c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(newcr));
62977c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
62987c478bd9Sstevel@tonic-gate 	crset(pp, newcr);
62997c478bd9Sstevel@tonic-gate 
63007c478bd9Sstevel@tonic-gate 	/*
63017c478bd9Sstevel@tonic-gate 	 * Adjust upcount to reflect zone entry.
63027c478bd9Sstevel@tonic-gate 	 */
63037c478bd9Sstevel@tonic-gate 	uid = crgetruid(newcr);
63047c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
63057c478bd9Sstevel@tonic-gate 	upcount_dec(uid, GLOBAL_ZONEID);
63067c478bd9Sstevel@tonic-gate 	upcount_inc(uid, zoneid);
63077c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
63087c478bd9Sstevel@tonic-gate 
63097c478bd9Sstevel@tonic-gate 	/*
63107c478bd9Sstevel@tonic-gate 	 * Set up core file path and content.
63117c478bd9Sstevel@tonic-gate 	 */
63127c478bd9Sstevel@tonic-gate 	set_core_defaults();
63137c478bd9Sstevel@tonic-gate 
63147c478bd9Sstevel@tonic-gate out:
63157c478bd9Sstevel@tonic-gate 	/*
63167c478bd9Sstevel@tonic-gate 	 * Let the other lwps continue.
63177c478bd9Sstevel@tonic-gate 	 */
63187c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
63197c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
63207c478bd9Sstevel@tonic-gate 		continuelwps(pp);
63217c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
63227c478bd9Sstevel@tonic-gate 
63237c478bd9Sstevel@tonic-gate 	return (err != 0 ? set_errno(err) : 0);
63247c478bd9Sstevel@tonic-gate }
63257c478bd9Sstevel@tonic-gate 
63267c478bd9Sstevel@tonic-gate /*
63277c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_list(2).
63287c478bd9Sstevel@tonic-gate  *
63297c478bd9Sstevel@tonic-gate  * Processes running in a (non-global) zone only see themselves.
633045916cd2Sjpk  * On labeled systems, they see all zones whose label they dominate.
63317c478bd9Sstevel@tonic-gate  */
63327c478bd9Sstevel@tonic-gate static int
zone_list(zoneid_t * zoneidlist,uint_t * numzones)63337c478bd9Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones)
63347c478bd9Sstevel@tonic-gate {
63357c478bd9Sstevel@tonic-gate 	zoneid_t *zoneids;
633648451833Scarlsonj 	zone_t *zone, *myzone;
63377c478bd9Sstevel@tonic-gate 	uint_t user_nzones, real_nzones;
633845916cd2Sjpk 	uint_t domi_nzones;
633945916cd2Sjpk 	int error;
63407c478bd9Sstevel@tonic-gate 
63417c478bd9Sstevel@tonic-gate 	if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0)
63427c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
63437c478bd9Sstevel@tonic-gate 
634448451833Scarlsonj 	myzone = curproc->p_zone;
6345c6f039c7SToomas Soome 	ASSERT(zonecount > 0);
634648451833Scarlsonj 	if (myzone != global_zone) {
634745916cd2Sjpk 		bslabel_t *mybslab;
634845916cd2Sjpk 
634945916cd2Sjpk 		if (!is_system_labeled()) {
635045916cd2Sjpk 			/* just return current zone */
635145916cd2Sjpk 			real_nzones = domi_nzones = 1;
635245916cd2Sjpk 			zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP);
635348451833Scarlsonj 			zoneids[0] = myzone->zone_id;
635445916cd2Sjpk 		} else {
635545916cd2Sjpk 			/* return all zones that are dominated */
635645916cd2Sjpk 			mutex_enter(&zonehash_lock);
635745916cd2Sjpk 			real_nzones = zonecount;
635845916cd2Sjpk 			domi_nzones = 0;
6359c6f039c7SToomas Soome 			zoneids = kmem_alloc(real_nzones *
6360c6f039c7SToomas Soome 			    sizeof (zoneid_t), KM_SLEEP);
6361c6f039c7SToomas Soome 			mybslab = label2bslabel(myzone->zone_slabel);
6362c6f039c7SToomas Soome 			for (zone = list_head(&zone_active);
6363c6f039c7SToomas Soome 			    zone != NULL;
6364c6f039c7SToomas Soome 			    zone = list_next(&zone_active, zone)) {
6365c6f039c7SToomas Soome 				if (zone->zone_id == GLOBAL_ZONEID)
6366c6f039c7SToomas Soome 					continue;
6367c6f039c7SToomas Soome 				if (zone != myzone &&
6368c6f039c7SToomas Soome 				    (zone->zone_flags & ZF_IS_SCRATCH))
6369c6f039c7SToomas Soome 					continue;
6370c6f039c7SToomas Soome 				/*
6371c6f039c7SToomas Soome 				 * Note that a label always dominates
6372c6f039c7SToomas Soome 				 * itself, so myzone is always included
6373c6f039c7SToomas Soome 				 * in the list.
6374c6f039c7SToomas Soome 				 */
6375c6f039c7SToomas Soome 				if (bldominates(mybslab,
6376c6f039c7SToomas Soome 				    label2bslabel(zone->zone_slabel))) {
6377c6f039c7SToomas Soome 					zoneids[domi_nzones++] = zone->zone_id;
637845916cd2Sjpk 				}
637945916cd2Sjpk 			}
638045916cd2Sjpk 			mutex_exit(&zonehash_lock);
638145916cd2Sjpk 		}
63827c478bd9Sstevel@tonic-gate 	} else {
63837c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
63847c478bd9Sstevel@tonic-gate 		real_nzones = zonecount;
638545916cd2Sjpk 		domi_nzones = 0;
6386c6f039c7SToomas Soome 		zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t), KM_SLEEP);
6387c6f039c7SToomas Soome 		for (zone = list_head(&zone_active); zone != NULL;
6388c6f039c7SToomas Soome 		    zone = list_next(&zone_active, zone))
6389c6f039c7SToomas Soome 			zoneids[domi_nzones++] = zone->zone_id;
6390c6f039c7SToomas Soome 
6391c6f039c7SToomas Soome 		ASSERT(domi_nzones == real_nzones);
63927c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
63937c478bd9Sstevel@tonic-gate 	}
63947c478bd9Sstevel@tonic-gate 
639545916cd2Sjpk 	/*
639645916cd2Sjpk 	 * If user has allocated space for fewer entries than we found, then
639748bbca81SDaniel Hoffman 	 * return only up to their limit.  Either way, tell them exactly how
639848bbca81SDaniel Hoffman 	 * many we found.
639945916cd2Sjpk 	 */
640045916cd2Sjpk 	if (domi_nzones < user_nzones)
640145916cd2Sjpk 		user_nzones = domi_nzones;
640245916cd2Sjpk 	error = 0;
640345916cd2Sjpk 	if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) {
64047c478bd9Sstevel@tonic-gate 		error = EFAULT;
640545916cd2Sjpk 	} else if (zoneidlist != NULL && user_nzones != 0) {
64067c478bd9Sstevel@tonic-gate 		if (copyout(zoneids, zoneidlist,
64077c478bd9Sstevel@tonic-gate 		    user_nzones * sizeof (zoneid_t)) != 0)
64087c478bd9Sstevel@tonic-gate 			error = EFAULT;
64097c478bd9Sstevel@tonic-gate 	}
64107c478bd9Sstevel@tonic-gate 
6411c6f039c7SToomas Soome 	kmem_free(zoneids, real_nzones * sizeof (zoneid_t));
64127c478bd9Sstevel@tonic-gate 
641345916cd2Sjpk 	if (error != 0)
64147c478bd9Sstevel@tonic-gate 		return (set_errno(error));
64157c478bd9Sstevel@tonic-gate 	else
64167c478bd9Sstevel@tonic-gate 		return (0);
64177c478bd9Sstevel@tonic-gate }
64187c478bd9Sstevel@tonic-gate 
64197c478bd9Sstevel@tonic-gate /*
64207c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_lookup(2).
64217c478bd9Sstevel@tonic-gate  *
642245916cd2Sjpk  * Non-global zones are only able to see themselves and (on labeled systems)
642345916cd2Sjpk  * the zones they dominate.
64247c478bd9Sstevel@tonic-gate  */
64257c478bd9Sstevel@tonic-gate static zoneid_t
zone_lookup(const char * zone_name)64267c478bd9Sstevel@tonic-gate zone_lookup(const char *zone_name)
64277c478bd9Sstevel@tonic-gate {
64287c478bd9Sstevel@tonic-gate 	char *kname;
64297c478bd9Sstevel@tonic-gate 	zone_t *zone;
64307c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
64317c478bd9Sstevel@tonic-gate 	int err;
64327c478bd9Sstevel@tonic-gate 
64337c478bd9Sstevel@tonic-gate 	if (zone_name == NULL) {
64347c478bd9Sstevel@tonic-gate 		/* return caller's zone id */
64357c478bd9Sstevel@tonic-gate 		return (getzoneid());
64367c478bd9Sstevel@tonic-gate 	}
64377c478bd9Sstevel@tonic-gate 
64387c478bd9Sstevel@tonic-gate 	kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
64397c478bd9Sstevel@tonic-gate 	if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) {
64407c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
64417c478bd9Sstevel@tonic-gate 		return (set_errno(err));
64427c478bd9Sstevel@tonic-gate 	}
64437c478bd9Sstevel@tonic-gate 
64447c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
64457c478bd9Sstevel@tonic-gate 	zone = zone_find_all_by_name(kname);
64467c478bd9Sstevel@tonic-gate 	kmem_free(kname, ZONENAME_MAX);
644745916cd2Sjpk 	/*
644845916cd2Sjpk 	 * In a non-global zone, can only lookup global and own name.
644945916cd2Sjpk 	 * In Trusted Extensions zone label dominance rules apply.
645045916cd2Sjpk 	 */
645145916cd2Sjpk 	if (zone == NULL ||
645245916cd2Sjpk 	    zone_status_get(zone) < ZONE_IS_READY ||
645345916cd2Sjpk 	    !zone_list_access(zone)) {
64547c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
64557c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
645645916cd2Sjpk 	} else {
645745916cd2Sjpk 		zoneid = zone->zone_id;
645845916cd2Sjpk 		mutex_exit(&zonehash_lock);
645945916cd2Sjpk 		return (zoneid);
64607c478bd9Sstevel@tonic-gate 	}
64617c478bd9Sstevel@tonic-gate }
64627c478bd9Sstevel@tonic-gate 
6463821c4a97Sdp static int
zone_version(int * version_arg)6464821c4a97Sdp zone_version(int *version_arg)
6465821c4a97Sdp {
6466821c4a97Sdp 	int version = ZONE_SYSCALL_API_VERSION;
6467821c4a97Sdp 
6468821c4a97Sdp 	if (copyout(&version, version_arg, sizeof (int)) != 0)
6469821c4a97Sdp 		return (set_errno(EFAULT));
6470821c4a97Sdp 	return (0);
6471821c4a97Sdp }
6472821c4a97Sdp 
64737c478bd9Sstevel@tonic-gate /* ARGSUSED */
64747c478bd9Sstevel@tonic-gate long
zone(int cmd,void * arg1,void * arg2,void * arg3,void * arg4)6475fa9e4066Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4)
64767c478bd9Sstevel@tonic-gate {
64777c478bd9Sstevel@tonic-gate 	zone_def zs;
64782b24ab6bSSebastien Roy 	int err;
64797c478bd9Sstevel@tonic-gate 
64807c478bd9Sstevel@tonic-gate 	switch (cmd) {
64817c478bd9Sstevel@tonic-gate 	case ZONE_CREATE:
64827c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
64837c478bd9Sstevel@tonic-gate 			if (copyin(arg1, &zs, sizeof (zone_def))) {
64847c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
64857c478bd9Sstevel@tonic-gate 			}
64867c478bd9Sstevel@tonic-gate 		} else {
64877c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
64887c478bd9Sstevel@tonic-gate 			zone_def32 zs32;
64897c478bd9Sstevel@tonic-gate 
64907c478bd9Sstevel@tonic-gate 			if (copyin(arg1, &zs32, sizeof (zone_def32))) {
64917c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
64927c478bd9Sstevel@tonic-gate 			}
64937c478bd9Sstevel@tonic-gate 			zs.zone_name =
64947c478bd9Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_name;
64957c478bd9Sstevel@tonic-gate 			zs.zone_root =
64967c478bd9Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_root;
64977c478bd9Sstevel@tonic-gate 			zs.zone_privs =
64987c478bd9Sstevel@tonic-gate 			    (const struct priv_set *)
64997c478bd9Sstevel@tonic-gate 			    (unsigned long)zs32.zone_privs;
65006f70df68Sdp 			zs.zone_privssz = zs32.zone_privssz;
65017c478bd9Sstevel@tonic-gate 			zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf;
65027c478bd9Sstevel@tonic-gate 			zs.rctlbufsz = zs32.rctlbufsz;
6503fa9e4066Sahrens 			zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf;
6504fa9e4066Sahrens 			zs.zfsbufsz = zs32.zfsbufsz;
65057c478bd9Sstevel@tonic-gate 			zs.extended_error =
65067c478bd9Sstevel@tonic-gate 			    (int *)(unsigned long)zs32.extended_error;
650745916cd2Sjpk 			zs.match = zs32.match;
650845916cd2Sjpk 			zs.doi = zs32.doi;
650945916cd2Sjpk 			zs.label = (const bslabel_t *)(uintptr_t)zs32.label;
6510f4b3ec61Sdh 			zs.flags = zs32.flags;
65117c478bd9Sstevel@tonic-gate #else
65127c478bd9Sstevel@tonic-gate 			panic("get_udatamodel() returned bogus result\n");
65137c478bd9Sstevel@tonic-gate #endif
65147c478bd9Sstevel@tonic-gate 		}
65157c478bd9Sstevel@tonic-gate 
65167c478bd9Sstevel@tonic-gate 		return (zone_create(zs.zone_name, zs.zone_root,
6517821c4a97Sdp 		    zs.zone_privs, zs.zone_privssz,
6518821c4a97Sdp 		    (caddr_t)zs.rctlbuf, zs.rctlbufsz,
6519821c4a97Sdp 		    (caddr_t)zs.zfsbuf, zs.zfsbufsz,
652045916cd2Sjpk 		    zs.extended_error, zs.match, zs.doi,
6521f4b3ec61Sdh 		    zs.label, zs.flags));
65227c478bd9Sstevel@tonic-gate 	case ZONE_BOOT:
65233f2f09c1Sdp 		return (zone_boot((zoneid_t)(uintptr_t)arg1));
65247c478bd9Sstevel@tonic-gate 	case ZONE_DESTROY:
65257c478bd9Sstevel@tonic-gate 		return (zone_destroy((zoneid_t)(uintptr_t)arg1));
65267c478bd9Sstevel@tonic-gate 	case ZONE_GETATTR:
65277c478bd9Sstevel@tonic-gate 		return (zone_getattr((zoneid_t)(uintptr_t)arg1,
65287c478bd9Sstevel@tonic-gate 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
65293f2f09c1Sdp 	case ZONE_SETATTR:
65303f2f09c1Sdp 		return (zone_setattr((zoneid_t)(uintptr_t)arg1,
65313f2f09c1Sdp 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
65327c478bd9Sstevel@tonic-gate 	case ZONE_ENTER:
65337c478bd9Sstevel@tonic-gate 		return (zone_enter((zoneid_t)(uintptr_t)arg1));
65347c478bd9Sstevel@tonic-gate 	case ZONE_LIST:
65357c478bd9Sstevel@tonic-gate 		return (zone_list((zoneid_t *)arg1, (uint_t *)arg2));
65367c478bd9Sstevel@tonic-gate 	case ZONE_SHUTDOWN:
65377c478bd9Sstevel@tonic-gate 		return (zone_shutdown((zoneid_t)(uintptr_t)arg1));
65387c478bd9Sstevel@tonic-gate 	case ZONE_LOOKUP:
65397c478bd9Sstevel@tonic-gate 		return (zone_lookup((const char *)arg1));
6540821c4a97Sdp 	case ZONE_VERSION:
6541821c4a97Sdp 		return (zone_version((int *)arg1));
6542f4b3ec61Sdh 	case ZONE_ADD_DATALINK:
6543f4b3ec61Sdh 		return (zone_add_datalink((zoneid_t)(uintptr_t)arg1,
65442b24ab6bSSebastien Roy 		    (datalink_id_t)(uintptr_t)arg2));
6545f4b3ec61Sdh 	case ZONE_DEL_DATALINK:
6546f4b3ec61Sdh 		return (zone_remove_datalink((zoneid_t)(uintptr_t)arg1,
65472b24ab6bSSebastien Roy 		    (datalink_id_t)(uintptr_t)arg2));
65482b24ab6bSSebastien Roy 	case ZONE_CHECK_DATALINK: {
65492b24ab6bSSebastien Roy 		zoneid_t	zoneid;
65502b24ab6bSSebastien Roy 		boolean_t	need_copyout;
65512b24ab6bSSebastien Roy 
65522b24ab6bSSebastien Roy 		if (copyin(arg1, &zoneid, sizeof (zoneid)) != 0)
65532b24ab6bSSebastien Roy 			return (EFAULT);
65542b24ab6bSSebastien Roy 		need_copyout = (zoneid == ALL_ZONES);
65552b24ab6bSSebastien Roy 		err = zone_check_datalink(&zoneid,
65562b24ab6bSSebastien Roy 		    (datalink_id_t)(uintptr_t)arg2);
65572b24ab6bSSebastien Roy 		if (err == 0 && need_copyout) {
65582b24ab6bSSebastien Roy 			if (copyout(&zoneid, arg1, sizeof (zoneid)) != 0)
65592b24ab6bSSebastien Roy 				err = EFAULT;
65602b24ab6bSSebastien Roy 		}
65612b24ab6bSSebastien Roy 		return (err == 0 ? 0 : set_errno(err));
65622b24ab6bSSebastien Roy 	}
6563f4b3ec61Sdh 	case ZONE_LIST_DATALINK:
6564f4b3ec61Sdh 		return (zone_list_datalink((zoneid_t)(uintptr_t)arg1,
65652b24ab6bSSebastien Roy 		    (int *)arg2, (datalink_id_t *)(uintptr_t)arg3));
65667c478bd9Sstevel@tonic-gate 	default:
65677c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
65687c478bd9Sstevel@tonic-gate 	}
65697c478bd9Sstevel@tonic-gate }
65707c478bd9Sstevel@tonic-gate 
65717c478bd9Sstevel@tonic-gate struct zarg {
65727c478bd9Sstevel@tonic-gate 	zone_t *zone;
65737c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t arg;
65747c478bd9Sstevel@tonic-gate };
65757c478bd9Sstevel@tonic-gate 
65767c478bd9Sstevel@tonic-gate static int
zone_lookup_door(const char * zone_name,door_handle_t * doorp)65777c478bd9Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp)
65787c478bd9Sstevel@tonic-gate {
65797c478bd9Sstevel@tonic-gate 	char *buf;
65807c478bd9Sstevel@tonic-gate 	size_t buflen;
65817c478bd9Sstevel@tonic-gate 	int error;
65827c478bd9Sstevel@tonic-gate 
65837c478bd9Sstevel@tonic-gate 	buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name);
65847c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(buflen, KM_SLEEP);
65857c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name);
65867c478bd9Sstevel@tonic-gate 	error = door_ki_open(buf, doorp);
65877c478bd9Sstevel@tonic-gate 	kmem_free(buf, buflen);
65887c478bd9Sstevel@tonic-gate 	return (error);
65897c478bd9Sstevel@tonic-gate }
65907c478bd9Sstevel@tonic-gate 
65917c478bd9Sstevel@tonic-gate static void
zone_release_door(door_handle_t * doorp)65927c478bd9Sstevel@tonic-gate zone_release_door(door_handle_t *doorp)
65937c478bd9Sstevel@tonic-gate {
65947c478bd9Sstevel@tonic-gate 	door_ki_rele(*doorp);
65957c478bd9Sstevel@tonic-gate 	*doorp = NULL;
65967c478bd9Sstevel@tonic-gate }
65977c478bd9Sstevel@tonic-gate 
65987c478bd9Sstevel@tonic-gate static void
zone_ki_call_zoneadmd(struct zarg * zargp)65997c478bd9Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp)
66007c478bd9Sstevel@tonic-gate {
66017c478bd9Sstevel@tonic-gate 	door_handle_t door = NULL;
66027c478bd9Sstevel@tonic-gate 	door_arg_t darg, save_arg;
66037c478bd9Sstevel@tonic-gate 	char *zone_name;
66047c478bd9Sstevel@tonic-gate 	size_t zone_namelen;
66057c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
66067c478bd9Sstevel@tonic-gate 	zone_t *zone;
66077c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t arg;
66087c478bd9Sstevel@tonic-gate 	uint64_t uniqid;
66097c478bd9Sstevel@tonic-gate 	size_t size;
66107c478bd9Sstevel@tonic-gate 	int error;
66117c478bd9Sstevel@tonic-gate 	int retry;
66127c478bd9Sstevel@tonic-gate 
66137c478bd9Sstevel@tonic-gate 	zone = zargp->zone;
66147c478bd9Sstevel@tonic-gate 	arg = zargp->arg;
66157c478bd9Sstevel@tonic-gate 	kmem_free(zargp, sizeof (*zargp));
66167c478bd9Sstevel@tonic-gate 
66177c478bd9Sstevel@tonic-gate 	zone_namelen = strlen(zone->zone_name) + 1;
66187c478bd9Sstevel@tonic-gate 	zone_name = kmem_alloc(zone_namelen, KM_SLEEP);
66197c478bd9Sstevel@tonic-gate 	bcopy(zone->zone_name, zone_name, zone_namelen);
66207c478bd9Sstevel@tonic-gate 	zoneid = zone->zone_id;
66217c478bd9Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
66227c478bd9Sstevel@tonic-gate 	/*
66237c478bd9Sstevel@tonic-gate 	 * zoneadmd may be down, but at least we can empty out the zone.
66247c478bd9Sstevel@tonic-gate 	 * We can ignore the return value of zone_empty() since we're called
66257c478bd9Sstevel@tonic-gate 	 * from a kernel thread and know we won't be delivered any signals.
66267c478bd9Sstevel@tonic-gate 	 */
66277c478bd9Sstevel@tonic-gate 	ASSERT(curproc == &p0);
66287c478bd9Sstevel@tonic-gate 	(void) zone_empty(zone);
66297c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY);
66307c478bd9Sstevel@tonic-gate 	zone_rele(zone);
66317c478bd9Sstevel@tonic-gate 
66327c478bd9Sstevel@tonic-gate 	size = sizeof (arg);
66337c478bd9Sstevel@tonic-gate 	darg.rbuf = (char *)&arg;
66347c478bd9Sstevel@tonic-gate 	darg.data_ptr = (char *)&arg;
66357c478bd9Sstevel@tonic-gate 	darg.rsize = size;
66367c478bd9Sstevel@tonic-gate 	darg.data_size = size;
66377c478bd9Sstevel@tonic-gate 	darg.desc_ptr = NULL;
66387c478bd9Sstevel@tonic-gate 	darg.desc_num = 0;
66397c478bd9Sstevel@tonic-gate 
66407c478bd9Sstevel@tonic-gate 	save_arg = darg;
66417c478bd9Sstevel@tonic-gate 	/*
66427c478bd9Sstevel@tonic-gate 	 * Since we're not holding a reference to the zone, any number of
66437c478bd9Sstevel@tonic-gate 	 * things can go wrong, including the zone disappearing before we get a
66447c478bd9Sstevel@tonic-gate 	 * chance to talk to zoneadmd.
66457c478bd9Sstevel@tonic-gate 	 */
66467c478bd9Sstevel@tonic-gate 	for (retry = 0; /* forever */; retry++) {
66477c478bd9Sstevel@tonic-gate 		if (door == NULL &&
66487c478bd9Sstevel@tonic-gate 		    (error = zone_lookup_door(zone_name, &door)) != 0) {
66497c478bd9Sstevel@tonic-gate 			goto next;
66507c478bd9Sstevel@tonic-gate 		}
66517c478bd9Sstevel@tonic-gate 		ASSERT(door != NULL);
66527c478bd9Sstevel@tonic-gate 
6653323a81d9Sjwadams 		if ((error = door_ki_upcall_limited(door, &darg, NULL,
6654323a81d9Sjwadams 		    SIZE_MAX, 0)) == 0) {
66557c478bd9Sstevel@tonic-gate 			break;
66567c478bd9Sstevel@tonic-gate 		}
66577c478bd9Sstevel@tonic-gate 		switch (error) {
66587c478bd9Sstevel@tonic-gate 		case EINTR:
66597c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
66607c478bd9Sstevel@tonic-gate 		case EAGAIN:	/* process may be forking */
66617c478bd9Sstevel@tonic-gate 			/*
66627c478bd9Sstevel@tonic-gate 			 * Back off for a bit
66637c478bd9Sstevel@tonic-gate 			 */
66647c478bd9Sstevel@tonic-gate 			break;
66657c478bd9Sstevel@tonic-gate 		case EBADF:
66667c478bd9Sstevel@tonic-gate 			zone_release_door(&door);
66677c478bd9Sstevel@tonic-gate 			if (zone_lookup_door(zone_name, &door) != 0) {
66687c478bd9Sstevel@tonic-gate 				/*
66697c478bd9Sstevel@tonic-gate 				 * zoneadmd may be dead, but it may come back to
66707c478bd9Sstevel@tonic-gate 				 * life later.
66717c478bd9Sstevel@tonic-gate 				 */
66727c478bd9Sstevel@tonic-gate 				break;
66737c478bd9Sstevel@tonic-gate 			}
66747c478bd9Sstevel@tonic-gate 			break;
66757c478bd9Sstevel@tonic-gate 		default:
66767c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
66777c478bd9Sstevel@tonic-gate 			    "zone_ki_call_zoneadmd: door_ki_upcall error %d\n",
66787c478bd9Sstevel@tonic-gate 			    error);
66797c478bd9Sstevel@tonic-gate 			goto out;
66807c478bd9Sstevel@tonic-gate 		}
66817c478bd9Sstevel@tonic-gate next:
66827c478bd9Sstevel@tonic-gate 		/*
66837c478bd9Sstevel@tonic-gate 		 * If this isn't the same zone_t that we originally had in mind,
66847c478bd9Sstevel@tonic-gate 		 * then this is the same as if two kadmin requests come in at
66857c478bd9Sstevel@tonic-gate 		 * the same time: the first one wins.  This means we lose, so we
66867c478bd9Sstevel@tonic-gate 		 * bail.
66877c478bd9Sstevel@tonic-gate 		 */
66887c478bd9Sstevel@tonic-gate 		if ((zone = zone_find_by_id(zoneid)) == NULL) {
66897c478bd9Sstevel@tonic-gate 			/*
66907c478bd9Sstevel@tonic-gate 			 * Problem is solved.
66917c478bd9Sstevel@tonic-gate 			 */
66927c478bd9Sstevel@tonic-gate 			break;
66937c478bd9Sstevel@tonic-gate 		}
66947c478bd9Sstevel@tonic-gate 		if (zone->zone_uniqid != uniqid) {
66957c478bd9Sstevel@tonic-gate 			/*
66967c478bd9Sstevel@tonic-gate 			 * zoneid recycled
66977c478bd9Sstevel@tonic-gate 			 */
66987c478bd9Sstevel@tonic-gate 			zone_rele(zone);
66997c478bd9Sstevel@tonic-gate 			break;
67007c478bd9Sstevel@tonic-gate 		}
67017c478bd9Sstevel@tonic-gate 		/*
67027c478bd9Sstevel@tonic-gate 		 * We could zone_status_timedwait(), but there doesn't seem to
67037c478bd9Sstevel@tonic-gate 		 * be much point in doing that (plus, it would mean that
67047c478bd9Sstevel@tonic-gate 		 * zone_free() isn't called until this thread exits).
67057c478bd9Sstevel@tonic-gate 		 */
67067c478bd9Sstevel@tonic-gate 		zone_rele(zone);
67077c478bd9Sstevel@tonic-gate 		delay(hz);
67087c478bd9Sstevel@tonic-gate 		darg = save_arg;
67097c478bd9Sstevel@tonic-gate 	}
67107c478bd9Sstevel@tonic-gate out:
67117c478bd9Sstevel@tonic-gate 	if (door != NULL) {
67127c478bd9Sstevel@tonic-gate 		zone_release_door(&door);
67137c478bd9Sstevel@tonic-gate 	}
67147c478bd9Sstevel@tonic-gate 	kmem_free(zone_name, zone_namelen);
67157c478bd9Sstevel@tonic-gate 	thread_exit();
67167c478bd9Sstevel@tonic-gate }
67177c478bd9Sstevel@tonic-gate 
67187c478bd9Sstevel@tonic-gate /*
67193f2f09c1Sdp  * Entry point for uadmin() to tell the zone to go away or reboot.  Analog to
67203f2f09c1Sdp  * kadmin().  The caller is a process in the zone.
67217c478bd9Sstevel@tonic-gate  *
67227c478bd9Sstevel@tonic-gate  * In order to shutdown the zone, we will hand off control to zoneadmd
67237c478bd9Sstevel@tonic-gate  * (running in the global zone) via a door.  We do a half-hearted job at
67247c478bd9Sstevel@tonic-gate  * killing all processes in the zone, create a kernel thread to contact
67257c478bd9Sstevel@tonic-gate  * zoneadmd, and make note of the "uniqid" of the zone.  The uniqid is
67267c478bd9Sstevel@tonic-gate  * a form of generation number used to let zoneadmd (as well as
67277c478bd9Sstevel@tonic-gate  * zone_destroy()) know exactly which zone they're re talking about.
67287c478bd9Sstevel@tonic-gate  */
67297c478bd9Sstevel@tonic-gate int
zone_kadmin(int cmd,int fcn,const char * mdep,cred_t * credp)67303f2f09c1Sdp zone_kadmin(int cmd, int fcn, const char *mdep, cred_t *credp)
67317c478bd9Sstevel@tonic-gate {
67327c478bd9Sstevel@tonic-gate 	struct zarg *zargp;
67337c478bd9Sstevel@tonic-gate 	zone_cmd_t zcmd;
67347c478bd9Sstevel@tonic-gate 	zone_t *zone;
67357c478bd9Sstevel@tonic-gate 
67367c478bd9Sstevel@tonic-gate 	zone = curproc->p_zone;
67377c478bd9Sstevel@tonic-gate 	ASSERT(getzoneid() != GLOBAL_ZONEID);
67387c478bd9Sstevel@tonic-gate 
67397c478bd9Sstevel@tonic-gate 	switch (cmd) {
67407c478bd9Sstevel@tonic-gate 	case A_SHUTDOWN:
67417c478bd9Sstevel@tonic-gate 		switch (fcn) {
67427c478bd9Sstevel@tonic-gate 		case AD_HALT:
67437c478bd9Sstevel@tonic-gate 		case AD_POWEROFF:
67447c478bd9Sstevel@tonic-gate 			zcmd = Z_HALT;
67457c478bd9Sstevel@tonic-gate 			break;
67467c478bd9Sstevel@tonic-gate 		case AD_BOOT:
67477c478bd9Sstevel@tonic-gate 			zcmd = Z_REBOOT;
67487c478bd9Sstevel@tonic-gate 			break;
67497c478bd9Sstevel@tonic-gate 		case AD_IBOOT:
67507c478bd9Sstevel@tonic-gate 		case AD_SBOOT:
67517c478bd9Sstevel@tonic-gate 		case AD_SIBOOT:
67527c478bd9Sstevel@tonic-gate 		case AD_NOSYNC:
67537c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
67547c478bd9Sstevel@tonic-gate 		default:
67557c478bd9Sstevel@tonic-gate 			return (EINVAL);
67567c478bd9Sstevel@tonic-gate 		}
67577c478bd9Sstevel@tonic-gate 		break;
67587c478bd9Sstevel@tonic-gate 	case A_REBOOT:
67597c478bd9Sstevel@tonic-gate 		zcmd = Z_REBOOT;
67607c478bd9Sstevel@tonic-gate 		break;
67617c478bd9Sstevel@tonic-gate 	case A_FTRACE:
67627c478bd9Sstevel@tonic-gate 	case A_REMOUNT:
67637c478bd9Sstevel@tonic-gate 	case A_FREEZE:
67647c478bd9Sstevel@tonic-gate 	case A_DUMP:
6765753a6d45SSherry Moore 	case A_CONFIG:
67667c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
67677c478bd9Sstevel@tonic-gate 	default:
67687c478bd9Sstevel@tonic-gate 		ASSERT(cmd != A_SWAPCTL);	/* handled by uadmin() */
67697c478bd9Sstevel@tonic-gate 		return (EINVAL);
67707c478bd9Sstevel@tonic-gate 	}
67717c478bd9Sstevel@tonic-gate 
67727c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_admin(credp, B_FALSE))
67737c478bd9Sstevel@tonic-gate 		return (EPERM);
67747c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
67753f2f09c1Sdp 
67767c478bd9Sstevel@tonic-gate 	/*
67777c478bd9Sstevel@tonic-gate 	 * zone_status can't be ZONE_IS_EMPTY or higher since curproc
67787c478bd9Sstevel@tonic-gate 	 * is in the zone.
67797c478bd9Sstevel@tonic-gate 	 */
67807c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY);
67817c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) > ZONE_IS_RUNNING) {
67827c478bd9Sstevel@tonic-gate 		/*
67837c478bd9Sstevel@tonic-gate 		 * This zone is already on its way down.
67847c478bd9Sstevel@tonic-gate 		 */
67857c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
67867c478bd9Sstevel@tonic-gate 		return (0);
67877c478bd9Sstevel@tonic-gate 	}
67887c478bd9Sstevel@tonic-gate 	/*
67897c478bd9Sstevel@tonic-gate 	 * Prevent future zone_enter()s
67907c478bd9Sstevel@tonic-gate 	 */
67917c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
67927c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
67937c478bd9Sstevel@tonic-gate 
67947c478bd9Sstevel@tonic-gate 	/*
67957c478bd9Sstevel@tonic-gate 	 * Kill everyone now and call zoneadmd later.
67967c478bd9Sstevel@tonic-gate 	 * zone_ki_call_zoneadmd() will do a more thorough job of this
67977c478bd9Sstevel@tonic-gate 	 * later.
67987c478bd9Sstevel@tonic-gate 	 */
67997c478bd9Sstevel@tonic-gate 	killall(zone->zone_id);
68007c478bd9Sstevel@tonic-gate 	/*
68017c478bd9Sstevel@tonic-gate 	 * Now, create the thread to contact zoneadmd and do the rest of the
68027c478bd9Sstevel@tonic-gate 	 * work.  This thread can't be created in our zone otherwise
68037c478bd9Sstevel@tonic-gate 	 * zone_destroy() would deadlock.
68047c478bd9Sstevel@tonic-gate 	 */
68053f2f09c1Sdp 	zargp = kmem_zalloc(sizeof (*zargp), KM_SLEEP);
68067c478bd9Sstevel@tonic-gate 	zargp->arg.cmd = zcmd;
68077c478bd9Sstevel@tonic-gate 	zargp->arg.uniqid = zone->zone_uniqid;
68083f2f09c1Sdp 	zargp->zone = zone;
68097c478bd9Sstevel@tonic-gate 	(void) strcpy(zargp->arg.locale, "C");
68103f2f09c1Sdp 	/* mdep was already copied in for us by uadmin */
68113f2f09c1Sdp 	if (mdep != NULL)
68123f2f09c1Sdp 		(void) strlcpy(zargp->arg.bootbuf, mdep,
68133f2f09c1Sdp 		    sizeof (zargp->arg.bootbuf));
68143f2f09c1Sdp 	zone_hold(zone);
68157c478bd9Sstevel@tonic-gate 
68167c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0,
68177c478bd9Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
68187c478bd9Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
68197c478bd9Sstevel@tonic-gate 
68207c478bd9Sstevel@tonic-gate 	return (EINVAL);
68217c478bd9Sstevel@tonic-gate }
68227c478bd9Sstevel@tonic-gate 
68237c478bd9Sstevel@tonic-gate /*
68247c478bd9Sstevel@tonic-gate  * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's
68257c478bd9Sstevel@tonic-gate  * status to ZONE_IS_SHUTTING_DOWN.
68268f983ab3Sjv  *
68278f983ab3Sjv  * This function also shuts down all running zones to ensure that they won't
68288f983ab3Sjv  * fork new processes.
68297c478bd9Sstevel@tonic-gate  */
68307c478bd9Sstevel@tonic-gate void
zone_shutdown_global(void)68317c478bd9Sstevel@tonic-gate zone_shutdown_global(void)
68327c478bd9Sstevel@tonic-gate {
68338f983ab3Sjv 	zone_t *current_zonep;
68347c478bd9Sstevel@tonic-gate 
68358f983ab3Sjv 	ASSERT(INGLOBALZONE(curproc));
68368f983ab3Sjv 	mutex_enter(&zonehash_lock);
68377c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
68388f983ab3Sjv 
68398f983ab3Sjv 	/* Modify the global zone's status first. */
68407c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING);
68417c478bd9Sstevel@tonic-gate 	zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN);
68428f983ab3Sjv 
68438f983ab3Sjv 	/*
68448f983ab3Sjv 	 * Now change the states of all running zones to ZONE_IS_SHUTTING_DOWN.
68458f983ab3Sjv 	 * We don't mark all zones with ZONE_IS_SHUTTING_DOWN because doing so
68468f983ab3Sjv 	 * could cause assertions to fail (e.g., assertions about a zone's
68478f983ab3Sjv 	 * state during initialization, readying, or booting) or produce races.
68488f983ab3Sjv 	 * We'll let threads continue to initialize and ready new zones: they'll
68498f983ab3Sjv 	 * fail to boot the new zones when they see that the global zone is
68508f983ab3Sjv 	 * shutting down.
68518f983ab3Sjv 	 */
68528f983ab3Sjv 	for (current_zonep = list_head(&zone_active); current_zonep != NULL;
68538f983ab3Sjv 	    current_zonep = list_next(&zone_active, current_zonep)) {
68548f983ab3Sjv 		if (zone_status_get(current_zonep) == ZONE_IS_RUNNING)
68558f983ab3Sjv 			zone_status_set(current_zonep, ZONE_IS_SHUTTING_DOWN);
68568f983ab3Sjv 	}
68577c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
68588f983ab3Sjv 	mutex_exit(&zonehash_lock);
68597c478bd9Sstevel@tonic-gate }
6860fa9e4066Sahrens 
6861fa9e4066Sahrens /*
6862fa9e4066Sahrens  * Returns true if the named dataset is visible in the current zone.
6863fa9e4066Sahrens  * The 'write' parameter is set to 1 if the dataset is also writable.
6864fa9e4066Sahrens  */
6865fa9e4066Sahrens int
zone_dataset_visible(const char * dataset,int * write)6866fa9e4066Sahrens zone_dataset_visible(const char *dataset, int *write)
6867fa9e4066Sahrens {
6868658a1dc7SSanjeev Bagewadi 	static int zfstype = -1;
6869fa9e4066Sahrens 	zone_dataset_t *zd;
6870fa9e4066Sahrens 	size_t len;
6871fa9e4066Sahrens 	zone_t *zone = curproc->p_zone;
6872658a1dc7SSanjeev Bagewadi 	const char *name = NULL;
6873658a1dc7SSanjeev Bagewadi 	vfs_t *vfsp = NULL;
6874fa9e4066Sahrens 
6875fa9e4066Sahrens 	if (dataset[0] == '\0')
6876fa9e4066Sahrens 		return (0);
6877fa9e4066Sahrens 
6878fa9e4066Sahrens 	/*
6879fa9e4066Sahrens 	 * Walk the list once, looking for datasets which match exactly, or
6880fa9e4066Sahrens 	 * specify a dataset underneath an exported dataset.  If found, return
6881fa9e4066Sahrens 	 * true and note that it is writable.
6882fa9e4066Sahrens 	 */
6883fa9e4066Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
6884fa9e4066Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
6885fa9e4066Sahrens 
6886fa9e4066Sahrens 		len = strlen(zd->zd_dataset);
6887fa9e4066Sahrens 		if (strlen(dataset) >= len &&
6888fa9e4066Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
688995c9592aSmaybee 		    (dataset[len] == '\0' || dataset[len] == '/' ||
689095c9592aSmaybee 		    dataset[len] == '@')) {
6891fa9e4066Sahrens 			if (write)
6892fa9e4066Sahrens 				*write = 1;
6893fa9e4066Sahrens 			return (1);
6894fa9e4066Sahrens 		}
6895fa9e4066Sahrens 	}
6896fa9e4066Sahrens 
6897fa9e4066Sahrens 	/*
6898fa9e4066Sahrens 	 * Walk the list a second time, searching for datasets which are parents
6899fa9e4066Sahrens 	 * of exported datasets.  These should be visible, but read-only.
6900fa9e4066Sahrens 	 *
6901fa9e4066Sahrens 	 * Note that we also have to support forms such as 'pool/dataset/', with
6902fa9e4066Sahrens 	 * a trailing slash.
6903fa9e4066Sahrens 	 */
6904fa9e4066Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
6905fa9e4066Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
6906fa9e4066Sahrens 
6907fa9e4066Sahrens 		len = strlen(dataset);
6908fa9e4066Sahrens 		if (dataset[len - 1] == '/')
6909fa9e4066Sahrens 			len--;	/* Ignore trailing slash */
6910fa9e4066Sahrens 		if (len < strlen(zd->zd_dataset) &&
6911fa9e4066Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
6912fa9e4066Sahrens 		    zd->zd_dataset[len] == '/') {
6913fa9e4066Sahrens 			if (write)
6914fa9e4066Sahrens 				*write = 0;
6915fa9e4066Sahrens 			return (1);
6916fa9e4066Sahrens 		}
6917fa9e4066Sahrens 	}
6918fa9e4066Sahrens 
6919658a1dc7SSanjeev Bagewadi 	/*
6920658a1dc7SSanjeev Bagewadi 	 * We reach here if the given dataset is not found in the zone_dataset
6921658a1dc7SSanjeev Bagewadi 	 * list. Check if this dataset was added as a filesystem (ie. "add fs")
6922658a1dc7SSanjeev Bagewadi 	 * instead of delegation. For this we search for the dataset in the
6923658a1dc7SSanjeev Bagewadi 	 * zone_vfslist of this zone. If found, return true and note that it is
6924658a1dc7SSanjeev Bagewadi 	 * not writable.
6925658a1dc7SSanjeev Bagewadi 	 */
6926658a1dc7SSanjeev Bagewadi 
6927658a1dc7SSanjeev Bagewadi 	/*
6928658a1dc7SSanjeev Bagewadi 	 * Initialize zfstype if it is not initialized yet.
6929658a1dc7SSanjeev Bagewadi 	 */
6930658a1dc7SSanjeev Bagewadi 	if (zfstype == -1) {
6931658a1dc7SSanjeev Bagewadi 		struct vfssw *vswp = vfs_getvfssw("zfs");
6932658a1dc7SSanjeev Bagewadi 		zfstype = vswp - vfssw;
6933658a1dc7SSanjeev Bagewadi 		vfs_unrefvfssw(vswp);
6934658a1dc7SSanjeev Bagewadi 	}
6935658a1dc7SSanjeev Bagewadi 
6936658a1dc7SSanjeev Bagewadi 	vfs_list_read_lock();
6937658a1dc7SSanjeev Bagewadi 	vfsp = zone->zone_vfslist;
6938658a1dc7SSanjeev Bagewadi 	do {
6939658a1dc7SSanjeev Bagewadi 		ASSERT(vfsp);
6940658a1dc7SSanjeev Bagewadi 		if (vfsp->vfs_fstype == zfstype) {
6941658a1dc7SSanjeev Bagewadi 			name = refstr_value(vfsp->vfs_resource);
6942658a1dc7SSanjeev Bagewadi 
6943658a1dc7SSanjeev Bagewadi 			/*
6944658a1dc7SSanjeev Bagewadi 			 * Check if we have an exact match.
6945658a1dc7SSanjeev Bagewadi 			 */
6946658a1dc7SSanjeev Bagewadi 			if (strcmp(dataset, name) == 0) {
6947658a1dc7SSanjeev Bagewadi 				vfs_list_unlock();
6948658a1dc7SSanjeev Bagewadi 				if (write)
6949658a1dc7SSanjeev Bagewadi 					*write = 0;
6950658a1dc7SSanjeev Bagewadi 				return (1);
6951658a1dc7SSanjeev Bagewadi 			}
6952658a1dc7SSanjeev Bagewadi 			/*
6953658a1dc7SSanjeev Bagewadi 			 * We need to check if we are looking for parents of
6954658a1dc7SSanjeev Bagewadi 			 * a dataset. These should be visible, but read-only.
6955658a1dc7SSanjeev Bagewadi 			 */
6956658a1dc7SSanjeev Bagewadi 			len = strlen(dataset);
6957658a1dc7SSanjeev Bagewadi 			if (dataset[len - 1] == '/')
6958658a1dc7SSanjeev Bagewadi 				len--;
6959658a1dc7SSanjeev Bagewadi 
6960658a1dc7SSanjeev Bagewadi 			if (len < strlen(name) &&
6961658a1dc7SSanjeev Bagewadi 			    bcmp(dataset, name, len) == 0 && name[len] == '/') {
6962658a1dc7SSanjeev Bagewadi 				vfs_list_unlock();
6963658a1dc7SSanjeev Bagewadi 				if (write)
6964658a1dc7SSanjeev Bagewadi 					*write = 0;
6965658a1dc7SSanjeev Bagewadi 				return (1);
6966658a1dc7SSanjeev Bagewadi 			}
6967658a1dc7SSanjeev Bagewadi 		}
6968658a1dc7SSanjeev Bagewadi 		vfsp = vfsp->vfs_zone_next;
6969658a1dc7SSanjeev Bagewadi 	} while (vfsp != zone->zone_vfslist);
6970658a1dc7SSanjeev Bagewadi 
6971658a1dc7SSanjeev Bagewadi 	vfs_list_unlock();
6972fa9e4066Sahrens 	return (0);
6973fa9e4066Sahrens }
697445916cd2Sjpk 
697545916cd2Sjpk /*
697645916cd2Sjpk  * zone_find_by_any_path() -
697745916cd2Sjpk  *
697845916cd2Sjpk  * kernel-private routine similar to zone_find_by_path(), but which
697945916cd2Sjpk  * effectively compares against zone paths rather than zonerootpath
698045916cd2Sjpk  * (i.e., the last component of zonerootpaths, which should be "root/",
698145916cd2Sjpk  * are not compared.)  This is done in order to accurately identify all
698245916cd2Sjpk  * paths, whether zone-visible or not, including those which are parallel
698345916cd2Sjpk  * to /root/, such as /dev/, /home/, etc...
698445916cd2Sjpk  *
698545916cd2Sjpk  * If the specified path does not fall under any zone path then global
698645916cd2Sjpk  * zone is returned.
698745916cd2Sjpk  *
698845916cd2Sjpk  * The treat_abs parameter indicates whether the path should be treated as
698945916cd2Sjpk  * an absolute path although it does not begin with "/".  (This supports
699045916cd2Sjpk  * nfs mount syntax such as host:any/path.)
699145916cd2Sjpk  *
699245916cd2Sjpk  * The caller is responsible for zone_rele of the returned zone.
699345916cd2Sjpk  */
699445916cd2Sjpk zone_t *
zone_find_by_any_path(const char * path,boolean_t treat_abs)699545916cd2Sjpk zone_find_by_any_path(const char *path, boolean_t treat_abs)
699645916cd2Sjpk {
699745916cd2Sjpk 	zone_t *zone;
699845916cd2Sjpk 	int path_offset = 0;
699945916cd2Sjpk 
700045916cd2Sjpk 	if (path == NULL) {
700145916cd2Sjpk 		zone_hold(global_zone);
700245916cd2Sjpk 		return (global_zone);
700345916cd2Sjpk 	}
700445916cd2Sjpk 
700545916cd2Sjpk 	if (*path != '/') {
700645916cd2Sjpk 		ASSERT(treat_abs);
700745916cd2Sjpk 		path_offset = 1;
700845916cd2Sjpk 	}
700945916cd2Sjpk 
701045916cd2Sjpk 	mutex_enter(&zonehash_lock);
701145916cd2Sjpk 	for (zone = list_head(&zone_active); zone != NULL;
701245916cd2Sjpk 	    zone = list_next(&zone_active, zone)) {
701345916cd2Sjpk 		char	*c;
701445916cd2Sjpk 		size_t	pathlen;
70150f95d722Smp 		char *rootpath_start;
701645916cd2Sjpk 
701745916cd2Sjpk 		if (zone == global_zone)	/* skip global zone */
701845916cd2Sjpk 			continue;
701945916cd2Sjpk 
702045916cd2Sjpk 		/* scan backwards to find start of last component */
702145916cd2Sjpk 		c = zone->zone_rootpath + zone->zone_rootpathlen - 2;
702245916cd2Sjpk 		do {
702345916cd2Sjpk 			c--;
702445916cd2Sjpk 		} while (*c != '/');
702545916cd2Sjpk 
70260f95d722Smp 		pathlen = c - zone->zone_rootpath + 1 - path_offset;
70270f95d722Smp 		rootpath_start = (zone->zone_rootpath + path_offset);
70280f95d722Smp 		if (strncmp(path, rootpath_start, pathlen) == 0)
702945916cd2Sjpk 			break;
703045916cd2Sjpk 	}
703145916cd2Sjpk 	if (zone == NULL)
703245916cd2Sjpk 		zone = global_zone;
703345916cd2Sjpk 	zone_hold(zone);
703445916cd2Sjpk 	mutex_exit(&zonehash_lock);
703545916cd2Sjpk 	return (zone);
703645916cd2Sjpk }
7037f4b3ec61Sdh 
7038f4b3ec61Sdh /*
70392b24ab6bSSebastien Roy  * Finds a zone_dl_t with the given linkid in the given zone.  Returns the
70402b24ab6bSSebastien Roy  * zone_dl_t pointer if found, and NULL otherwise.
7041f4b3ec61Sdh  */
70422b24ab6bSSebastien Roy static zone_dl_t *
zone_find_dl(zone_t * zone,datalink_id_t linkid)70432b24ab6bSSebastien Roy zone_find_dl(zone_t *zone, datalink_id_t linkid)
7044f4b3ec61Sdh {
70452b24ab6bSSebastien Roy 	zone_dl_t *zdl;
7046f4b3ec61Sdh 
70472b24ab6bSSebastien Roy 	ASSERT(mutex_owned(&zone->zone_lock));
70482b24ab6bSSebastien Roy 	for (zdl = list_head(&zone->zone_dl_list); zdl != NULL;
70492b24ab6bSSebastien Roy 	    zdl = list_next(&zone->zone_dl_list, zdl)) {
70502b24ab6bSSebastien Roy 		if (zdl->zdl_id == linkid)
7051f4b3ec61Sdh 			break;
7052f4b3ec61Sdh 	}
70532b24ab6bSSebastien Roy 	return (zdl);
70542b24ab6bSSebastien Roy }
70552b24ab6bSSebastien Roy 
70562b24ab6bSSebastien Roy static boolean_t
zone_dl_exists(zone_t * zone,datalink_id_t linkid)70572b24ab6bSSebastien Roy zone_dl_exists(zone_t *zone, datalink_id_t linkid)
70582b24ab6bSSebastien Roy {
70592b24ab6bSSebastien Roy 	boolean_t exists;
70602b24ab6bSSebastien Roy 
70612b24ab6bSSebastien Roy 	mutex_enter(&zone->zone_lock);
70622b24ab6bSSebastien Roy 	exists = (zone_find_dl(zone, linkid) != NULL);
7063f4b3ec61Sdh 	mutex_exit(&zone->zone_lock);
70642b24ab6bSSebastien Roy 	return (exists);
7065f4b3ec61Sdh }
7066f4b3ec61Sdh 
7067f4b3ec61Sdh /*
70682b24ab6bSSebastien Roy  * Add an data link name for the zone.
7069f4b3ec61Sdh  */
7070f4b3ec61Sdh static int
zone_add_datalink(zoneid_t zoneid,datalink_id_t linkid)70712b24ab6bSSebastien Roy zone_add_datalink(zoneid_t zoneid, datalink_id_t linkid)
7072f4b3ec61Sdh {
70732b24ab6bSSebastien Roy 	zone_dl_t *zdl;
7074f4b3ec61Sdh 	zone_t *zone;
7075f4b3ec61Sdh 	zone_t *thiszone;
7076f4b3ec61Sdh 
70772b24ab6bSSebastien Roy 	if ((thiszone = zone_find_by_id(zoneid)) == NULL)
7078f4b3ec61Sdh 		return (set_errno(ENXIO));
7079f4b3ec61Sdh 
70802b24ab6bSSebastien Roy 	/* Verify that the datalink ID doesn't already belong to a zone. */
7081f4b3ec61Sdh 	mutex_enter(&zonehash_lock);
7082f4b3ec61Sdh 	for (zone = list_head(&zone_active); zone != NULL;
7083f4b3ec61Sdh 	    zone = list_next(&zone_active, zone)) {
70842b24ab6bSSebastien Roy 		if (zone_dl_exists(zone, linkid)) {
7085f4b3ec61Sdh 			mutex_exit(&zonehash_lock);
7086f4b3ec61Sdh 			zone_rele(thiszone);
70872b24ab6bSSebastien Roy 			return (set_errno((zone == thiszone) ? EEXIST : EPERM));
7088f4b3ec61Sdh 		}
7089f4b3ec61Sdh 	}
70902b24ab6bSSebastien Roy 
70912b24ab6bSSebastien Roy 	zdl = kmem_zalloc(sizeof (*zdl), KM_SLEEP);
70922b24ab6bSSebastien Roy 	zdl->zdl_id = linkid;
7093550b6e40SSowmini Varadhan 	zdl->zdl_net = NULL;
7094f4b3ec61Sdh 	mutex_enter(&thiszone->zone_lock);
70952b24ab6bSSebastien Roy 	list_insert_head(&thiszone->zone_dl_list, zdl);
7096f4b3ec61Sdh 	mutex_exit(&thiszone->zone_lock);
7097f4b3ec61Sdh 	mutex_exit(&zonehash_lock);
7098f4b3ec61Sdh 	zone_rele(thiszone);
7099f4b3ec61Sdh 	return (0);
7100f4b3ec61Sdh }
7101f4b3ec61Sdh 
7102f4b3ec61Sdh static int
zone_remove_datalink(zoneid_t zoneid,datalink_id_t linkid)71032b24ab6bSSebastien Roy zone_remove_datalink(zoneid_t zoneid, datalink_id_t linkid)
7104f4b3ec61Sdh {
71052b24ab6bSSebastien Roy 	zone_dl_t *zdl;
7106f4b3ec61Sdh 	zone_t *zone;
71072b24ab6bSSebastien Roy 	int err = 0;
7108f4b3ec61Sdh 
71092b24ab6bSSebastien Roy 	if ((zone = zone_find_by_id(zoneid)) == NULL)
7110f4b3ec61Sdh 		return (set_errno(EINVAL));
7111f4b3ec61Sdh 
7112f4b3ec61Sdh 	mutex_enter(&zone->zone_lock);
71132b24ab6bSSebastien Roy 	if ((zdl = zone_find_dl(zone, linkid)) == NULL) {
71142b24ab6bSSebastien Roy 		err = ENXIO;
71152b24ab6bSSebastien Roy 	} else {
71162b24ab6bSSebastien Roy 		list_remove(&zone->zone_dl_list, zdl);
7117aab83bb8SJosef 'Jeff' Sipek 		nvlist_free(zdl->zdl_net);
71182b24ab6bSSebastien Roy 		kmem_free(zdl, sizeof (zone_dl_t));
7119f4b3ec61Sdh 	}
7120f4b3ec61Sdh 	mutex_exit(&zone->zone_lock);
7121f4b3ec61Sdh 	zone_rele(zone);
71222b24ab6bSSebastien Roy 	return (err == 0 ? 0 : set_errno(err));
7123f4b3ec61Sdh }
7124f4b3ec61Sdh 
7125f4b3ec61Sdh /*
71262b24ab6bSSebastien Roy  * Using the zoneidp as ALL_ZONES, we can lookup which zone has been assigned
71272b24ab6bSSebastien Roy  * the linkid.  Otherwise we just check if the specified zoneidp has been
71282b24ab6bSSebastien Roy  * assigned the supplied linkid.
7129f4b3ec61Sdh  */
71302b24ab6bSSebastien Roy int
zone_check_datalink(zoneid_t * zoneidp,datalink_id_t linkid)71312b24ab6bSSebastien Roy zone_check_datalink(zoneid_t *zoneidp, datalink_id_t linkid)
7132f4b3ec61Sdh {
7133f4b3ec61Sdh 	zone_t *zone;
71342b24ab6bSSebastien Roy 	int err = ENXIO;
7135f4b3ec61Sdh 
71362b24ab6bSSebastien Roy 	if (*zoneidp != ALL_ZONES) {
71372b24ab6bSSebastien Roy 		if ((zone = zone_find_by_id(*zoneidp)) != NULL) {
71382b24ab6bSSebastien Roy 			if (zone_dl_exists(zone, linkid))
71392b24ab6bSSebastien Roy 				err = 0;
71402b24ab6bSSebastien Roy 			zone_rele(zone);
71412b24ab6bSSebastien Roy 		}
71422b24ab6bSSebastien Roy 		return (err);
7143f4b3ec61Sdh 	}
7144f4b3ec61Sdh 
7145f4b3ec61Sdh 	mutex_enter(&zonehash_lock);
7146f4b3ec61Sdh 	for (zone = list_head(&zone_active); zone != NULL;
7147f4b3ec61Sdh 	    zone = list_next(&zone_active, zone)) {
71482b24ab6bSSebastien Roy 		if (zone_dl_exists(zone, linkid)) {
71492b24ab6bSSebastien Roy 			*zoneidp = zone->zone_id;
71502b24ab6bSSebastien Roy 			err = 0;
71512b24ab6bSSebastien Roy 			break;
7152f4b3ec61Sdh 		}
7153f4b3ec61Sdh 	}
7154f4b3ec61Sdh 	mutex_exit(&zonehash_lock);
71552b24ab6bSSebastien Roy 	return (err);
7156f4b3ec61Sdh }
7157f4b3ec61Sdh 
7158f4b3ec61Sdh /*
71592b24ab6bSSebastien Roy  * Get the list of datalink IDs assigned to a zone.
71602b24ab6bSSebastien Roy  *
71612b24ab6bSSebastien Roy  * On input, *nump is the number of datalink IDs that can fit in the supplied
71622b24ab6bSSebastien Roy  * idarray.  Upon return, *nump is either set to the number of datalink IDs
71632b24ab6bSSebastien Roy  * that were placed in the array if the array was large enough, or to the
71642b24ab6bSSebastien Roy  * number of datalink IDs that the function needs to place in the array if the
71652b24ab6bSSebastien Roy  * array is too small.
7166f4b3ec61Sdh  */
7167f4b3ec61Sdh static int
zone_list_datalink(zoneid_t zoneid,int * nump,datalink_id_t * idarray)71682b24ab6bSSebastien Roy zone_list_datalink(zoneid_t zoneid, int *nump, datalink_id_t *idarray)
7169f4b3ec61Sdh {
71702b24ab6bSSebastien Roy 	uint_t num, dlcount;
7171f4b3ec61Sdh 	zone_t *zone;
71722b24ab6bSSebastien Roy 	zone_dl_t *zdl;
71732b24ab6bSSebastien Roy 	datalink_id_t *idptr = idarray;
7174f4b3ec61Sdh 
7175f4b3ec61Sdh 	if (copyin(nump, &dlcount, sizeof (dlcount)) != 0)
7176f4b3ec61Sdh 		return (set_errno(EFAULT));
71772b24ab6bSSebastien Roy 	if ((zone = zone_find_by_id(zoneid)) == NULL)
7178f4b3ec61Sdh 		return (set_errno(ENXIO));
7179f4b3ec61Sdh 
7180f4b3ec61Sdh 	num = 0;
7181f4b3ec61Sdh 	mutex_enter(&zone->zone_lock);
71822b24ab6bSSebastien Roy 	for (zdl = list_head(&zone->zone_dl_list); zdl != NULL;
71832b24ab6bSSebastien Roy 	    zdl = list_next(&zone->zone_dl_list, zdl)) {
7184f4b3ec61Sdh 		/*
71852b24ab6bSSebastien Roy 		 * If the list is bigger than what the caller supplied, just
71862b24ab6bSSebastien Roy 		 * count, don't do copyout.
7187f4b3ec61Sdh 		 */
7188f4b3ec61Sdh 		if (++num > dlcount)
7189f4b3ec61Sdh 			continue;
71902b24ab6bSSebastien Roy 		if (copyout(&zdl->zdl_id, idptr, sizeof (*idptr)) != 0) {
7191f4b3ec61Sdh 			mutex_exit(&zone->zone_lock);
7192f4b3ec61Sdh 			zone_rele(zone);
7193f4b3ec61Sdh 			return (set_errno(EFAULT));
7194f4b3ec61Sdh 		}
71952b24ab6bSSebastien Roy 		idptr++;
7196f4b3ec61Sdh 	}
7197f4b3ec61Sdh 	mutex_exit(&zone->zone_lock);
7198f4b3ec61Sdh 	zone_rele(zone);
7199f4b3ec61Sdh 
7200*85dff7a0SAndy Fiddaman 	/*
7201*85dff7a0SAndy Fiddaman 	 * Prevent returning negative nump values -- we should never
7202*85dff7a0SAndy Fiddaman 	 * have this many links anyways.
7203*85dff7a0SAndy Fiddaman 	 */
7204*85dff7a0SAndy Fiddaman 	if (num > INT_MAX)
7205*85dff7a0SAndy Fiddaman 		return (set_errno(EOVERFLOW));
7206*85dff7a0SAndy Fiddaman 
7207f4b3ec61Sdh 	/* Increased or decreased, caller should be notified. */
7208f4b3ec61Sdh 	if (num != dlcount) {
72092b24ab6bSSebastien Roy 		if (copyout(&num, nump, sizeof (num)) != 0)
7210f4b3ec61Sdh 			return (set_errno(EFAULT));
7211f4b3ec61Sdh 	}
7212f4b3ec61Sdh 	return (0);
7213f4b3ec61Sdh }
7214f4b3ec61Sdh 
7215f4b3ec61Sdh /*
7216f4b3ec61Sdh  * Public interface for looking up a zone by zoneid. It's a customized version
7217bd41d0a8Snordmark  * for netstack_zone_create(). It can only be called from the zsd create
7218bd41d0a8Snordmark  * callbacks, since it doesn't have reference on the zone structure hence if
7219bd41d0a8Snordmark  * it is called elsewhere the zone could disappear after the zonehash_lock
7220bd41d0a8Snordmark  * is dropped.
7221bd41d0a8Snordmark  *
7222bd41d0a8Snordmark  * Furthermore it
7223bd41d0a8Snordmark  * 1. Doesn't check the status of the zone.
7224bd41d0a8Snordmark  * 2. It will be called even before zone_init is called, in that case the
7225f4b3ec61Sdh  *    address of zone0 is returned directly, and netstack_zone_create()
7226f4b3ec61Sdh  *    will only assign a value to zone0.zone_netstack, won't break anything.
7227bd41d0a8Snordmark  * 3. Returns without the zone being held.
7228f4b3ec61Sdh  */
7229f4b3ec61Sdh zone_t *
zone_find_by_id_nolock(zoneid_t zoneid)7230f4b3ec61Sdh zone_find_by_id_nolock(zoneid_t zoneid)
7231f4b3ec61Sdh {
7232bd41d0a8Snordmark 	zone_t *zone;
7233f4b3ec61Sdh 
7234bd41d0a8Snordmark 	mutex_enter(&zonehash_lock);
7235f4b3ec61Sdh 	if (zonehashbyid == NULL)
7236bd41d0a8Snordmark 		zone = &zone0;
7237f4b3ec61Sdh 	else
7238bd41d0a8Snordmark 		zone = zone_find_all_by_id(zoneid);
7239bd41d0a8Snordmark 	mutex_exit(&zonehash_lock);
7240bd41d0a8Snordmark 	return (zone);
7241f4b3ec61Sdh }
7242d62bc4baSyz 
7243d62bc4baSyz /*
7244d62bc4baSyz  * Walk the datalinks for a given zone
7245d62bc4baSyz  */
7246d62bc4baSyz int
zone_datalink_walk(zoneid_t zoneid,int (* cb)(datalink_id_t,void *),void * data)72472b24ab6bSSebastien Roy zone_datalink_walk(zoneid_t zoneid, int (*cb)(datalink_id_t, void *),
72482b24ab6bSSebastien Roy     void *data)
7249d62bc4baSyz {
72502b24ab6bSSebastien Roy 	zone_t		*zone;
72512b24ab6bSSebastien Roy 	zone_dl_t	*zdl;
72522b24ab6bSSebastien Roy 	datalink_id_t	*idarray;
72532b24ab6bSSebastien Roy 	uint_t		idcount = 0;
72542b24ab6bSSebastien Roy 	int		i, ret = 0;
7255d62bc4baSyz 
7256d62bc4baSyz 	if ((zone = zone_find_by_id(zoneid)) == NULL)
7257d62bc4baSyz 		return (ENOENT);
7258d62bc4baSyz 
72592b24ab6bSSebastien Roy 	/*
72602b24ab6bSSebastien Roy 	 * We first build an array of linkid's so that we can walk these and
72612b24ab6bSSebastien Roy 	 * execute the callback with the zone_lock dropped.
72622b24ab6bSSebastien Roy 	 */
7263d62bc4baSyz 	mutex_enter(&zone->zone_lock);
72642b24ab6bSSebastien Roy 	for (zdl = list_head(&zone->zone_dl_list); zdl != NULL;
72652b24ab6bSSebastien Roy 	    zdl = list_next(&zone->zone_dl_list, zdl)) {
72662b24ab6bSSebastien Roy 		idcount++;
7267d62bc4baSyz 	}
72682b24ab6bSSebastien Roy 
72692b24ab6bSSebastien Roy 	if (idcount == 0) {
72702b24ab6bSSebastien Roy 		mutex_exit(&zone->zone_lock);
72712b24ab6bSSebastien Roy 		zone_rele(zone);
72722b24ab6bSSebastien Roy 		return (0);
72732b24ab6bSSebastien Roy 	}
72742b24ab6bSSebastien Roy 
72752b24ab6bSSebastien Roy 	idarray = kmem_alloc(sizeof (datalink_id_t) * idcount, KM_NOSLEEP);
72762b24ab6bSSebastien Roy 	if (idarray == NULL) {
72772b24ab6bSSebastien Roy 		mutex_exit(&zone->zone_lock);
72782b24ab6bSSebastien Roy 		zone_rele(zone);
72792b24ab6bSSebastien Roy 		return (ENOMEM);
72802b24ab6bSSebastien Roy 	}
72812b24ab6bSSebastien Roy 
72822b24ab6bSSebastien Roy 	for (i = 0, zdl = list_head(&zone->zone_dl_list); zdl != NULL;
72832b24ab6bSSebastien Roy 	    i++, zdl = list_next(&zone->zone_dl_list, zdl)) {
72842b24ab6bSSebastien Roy 		idarray[i] = zdl->zdl_id;
72852b24ab6bSSebastien Roy 	}
72862b24ab6bSSebastien Roy 
7287d62bc4baSyz 	mutex_exit(&zone->zone_lock);
72882b24ab6bSSebastien Roy 
72892b24ab6bSSebastien Roy 	for (i = 0; i < idcount && ret == 0; i++) {
72902b24ab6bSSebastien Roy 		if ((ret = (*cb)(idarray[i], data)) != 0)
72912b24ab6bSSebastien Roy 			break;
72922b24ab6bSSebastien Roy 	}
72932b24ab6bSSebastien Roy 
7294d62bc4baSyz 	zone_rele(zone);
72952b24ab6bSSebastien Roy 	kmem_free(idarray, sizeof (datalink_id_t) * idcount);
7296d62bc4baSyz 	return (ret);
7297d62bc4baSyz }
7298550b6e40SSowmini Varadhan 
7299550b6e40SSowmini Varadhan static char *
zone_net_type2name(int type)7300550b6e40SSowmini Varadhan zone_net_type2name(int type)
7301550b6e40SSowmini Varadhan {
7302550b6e40SSowmini Varadhan 	switch (type) {
7303550b6e40SSowmini Varadhan 	case ZONE_NETWORK_ADDRESS:
7304550b6e40SSowmini Varadhan 		return (ZONE_NET_ADDRNAME);
7305550b6e40SSowmini Varadhan 	case ZONE_NETWORK_DEFROUTER:
7306550b6e40SSowmini Varadhan 		return (ZONE_NET_RTRNAME);
7307550b6e40SSowmini Varadhan 	default:
7308550b6e40SSowmini Varadhan 		return (NULL);
7309550b6e40SSowmini Varadhan 	}
7310550b6e40SSowmini Varadhan }
7311550b6e40SSowmini Varadhan 
7312550b6e40SSowmini Varadhan static int
zone_set_network(zoneid_t zoneid,zone_net_data_t * znbuf)7313550b6e40SSowmini Varadhan zone_set_network(zoneid_t zoneid, zone_net_data_t *znbuf)
7314550b6e40SSowmini Varadhan {
7315550b6e40SSowmini Varadhan 	zone_t *zone;
7316550b6e40SSowmini Varadhan 	zone_dl_t *zdl;
7317550b6e40SSowmini Varadhan 	nvlist_t *nvl;
7318550b6e40SSowmini Varadhan 	int err = 0;
7319550b6e40SSowmini Varadhan 	uint8_t *new = NULL;
7320550b6e40SSowmini Varadhan 	char *nvname;
7321550b6e40SSowmini Varadhan 	int bufsize;
7322550b6e40SSowmini Varadhan 	datalink_id_t linkid = znbuf->zn_linkid;
7323550b6e40SSowmini Varadhan 
7324550b6e40SSowmini Varadhan 	if (secpolicy_zone_config(CRED()) != 0)
7325550b6e40SSowmini Varadhan 		return (set_errno(EPERM));
7326550b6e40SSowmini Varadhan 
7327550b6e40SSowmini Varadhan 	if (zoneid == GLOBAL_ZONEID)
7328550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
7329550b6e40SSowmini Varadhan 
7330550b6e40SSowmini Varadhan 	nvname = zone_net_type2name(znbuf->zn_type);
7331550b6e40SSowmini Varadhan 	bufsize = znbuf->zn_len;
7332550b6e40SSowmini Varadhan 	new = znbuf->zn_val;
7333550b6e40SSowmini Varadhan 	if (nvname == NULL)
7334550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
7335550b6e40SSowmini Varadhan 
7336550b6e40SSowmini Varadhan 	if ((zone = zone_find_by_id(zoneid)) == NULL) {
7337550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
7338550b6e40SSowmini Varadhan 	}
7339550b6e40SSowmini Varadhan 
7340550b6e40SSowmini Varadhan 	mutex_enter(&zone->zone_lock);
7341550b6e40SSowmini Varadhan 	if ((zdl = zone_find_dl(zone, linkid)) == NULL) {
7342550b6e40SSowmini Varadhan 		err = ENXIO;
7343550b6e40SSowmini Varadhan 		goto done;
7344550b6e40SSowmini Varadhan 	}
7345550b6e40SSowmini Varadhan 	if ((nvl = zdl->zdl_net) == NULL) {
7346550b6e40SSowmini Varadhan 		if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)) {
7347550b6e40SSowmini Varadhan 			err = ENOMEM;
7348550b6e40SSowmini Varadhan 			goto done;
7349550b6e40SSowmini Varadhan 		} else {
7350550b6e40SSowmini Varadhan 			zdl->zdl_net = nvl;
7351550b6e40SSowmini Varadhan 		}
7352550b6e40SSowmini Varadhan 	}
7353550b6e40SSowmini Varadhan 	if (nvlist_exists(nvl, nvname)) {
7354550b6e40SSowmini Varadhan 		err = EINVAL;
7355550b6e40SSowmini Varadhan 		goto done;
7356550b6e40SSowmini Varadhan 	}
7357550b6e40SSowmini Varadhan 	err = nvlist_add_uint8_array(nvl, nvname, new, bufsize);
7358550b6e40SSowmini Varadhan 	ASSERT(err == 0);
7359550b6e40SSowmini Varadhan done:
7360550b6e40SSowmini Varadhan 	mutex_exit(&zone->zone_lock);
7361550b6e40SSowmini Varadhan 	zone_rele(zone);
7362550b6e40SSowmini Varadhan 	if (err != 0)
7363550b6e40SSowmini Varadhan 		return (set_errno(err));
7364550b6e40SSowmini Varadhan 	else
7365550b6e40SSowmini Varadhan 		return (0);
7366550b6e40SSowmini Varadhan }
7367550b6e40SSowmini Varadhan 
7368550b6e40SSowmini Varadhan static int
zone_get_network(zoneid_t zoneid,zone_net_data_t * znbuf)7369550b6e40SSowmini Varadhan zone_get_network(zoneid_t zoneid, zone_net_data_t *znbuf)
7370550b6e40SSowmini Varadhan {
7371550b6e40SSowmini Varadhan 	zone_t *zone;
7372550b6e40SSowmini Varadhan 	zone_dl_t *zdl;
7373550b6e40SSowmini Varadhan 	nvlist_t *nvl;
7374550b6e40SSowmini Varadhan 	uint8_t *ptr;
7375550b6e40SSowmini Varadhan 	uint_t psize;
7376550b6e40SSowmini Varadhan 	int err = 0;
7377550b6e40SSowmini Varadhan 	char *nvname;
7378550b6e40SSowmini Varadhan 	int bufsize;
7379550b6e40SSowmini Varadhan 	void *buf;
7380550b6e40SSowmini Varadhan 	datalink_id_t linkid = znbuf->zn_linkid;
7381550b6e40SSowmini Varadhan 
7382550b6e40SSowmini Varadhan 	if (zoneid == GLOBAL_ZONEID)
7383550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
7384550b6e40SSowmini Varadhan 
7385550b6e40SSowmini Varadhan 	nvname = zone_net_type2name(znbuf->zn_type);
7386550b6e40SSowmini Varadhan 	bufsize = znbuf->zn_len;
7387550b6e40SSowmini Varadhan 	buf = znbuf->zn_val;
7388550b6e40SSowmini Varadhan 
7389550b6e40SSowmini Varadhan 	if (nvname == NULL)
7390550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
7391550b6e40SSowmini Varadhan 	if ((zone = zone_find_by_id(zoneid)) == NULL)
7392550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
7393550b6e40SSowmini Varadhan 
7394550b6e40SSowmini Varadhan 	mutex_enter(&zone->zone_lock);
7395550b6e40SSowmini Varadhan 	if ((zdl = zone_find_dl(zone, linkid)) == NULL) {
7396550b6e40SSowmini Varadhan 		err = ENXIO;
7397550b6e40SSowmini Varadhan 		goto done;
7398550b6e40SSowmini Varadhan 	}
7399550b6e40SSowmini Varadhan 	if ((nvl = zdl->zdl_net) == NULL || !nvlist_exists(nvl, nvname)) {
7400550b6e40SSowmini Varadhan 		err = ENOENT;
7401550b6e40SSowmini Varadhan 		goto done;
7402550b6e40SSowmini Varadhan 	}
7403550b6e40SSowmini Varadhan 	err = nvlist_lookup_uint8_array(nvl, nvname, &ptr, &psize);
7404550b6e40SSowmini Varadhan 	ASSERT(err == 0);
7405550b6e40SSowmini Varadhan 
7406550b6e40SSowmini Varadhan 	if (psize > bufsize) {
7407550b6e40SSowmini Varadhan 		err = ENOBUFS;
7408550b6e40SSowmini Varadhan 		goto done;
7409550b6e40SSowmini Varadhan 	}
7410550b6e40SSowmini Varadhan 	znbuf->zn_len = psize;
7411550b6e40SSowmini Varadhan 	bcopy(ptr, buf, psize);
7412550b6e40SSowmini Varadhan done:
7413550b6e40SSowmini Varadhan 	mutex_exit(&zone->zone_lock);
7414550b6e40SSowmini Varadhan 	zone_rele(zone);
7415550b6e40SSowmini Varadhan 	if (err != 0)
7416550b6e40SSowmini Varadhan 		return (set_errno(err));
7417550b6e40SSowmini Varadhan 	else
7418550b6e40SSowmini Varadhan 		return (0);
7419550b6e40SSowmini Varadhan }
7420