xref: /titanic_44/usr/src/uts/common/os/zone.c (revision 81d43577d1b5e76e6016ba642ecc1a76fde43021)
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.
24542a813cSJerry Jelinek  * Copyright 2013, Joyent Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Zones
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  *   A zone is a named collection of processes, namespace constraints,
317c478bd9Sstevel@tonic-gate  *   and other system resources which comprise a secure and manageable
327c478bd9Sstevel@tonic-gate  *   application containment facility.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  *   Zones (represented by the reference counted zone_t) are tracked in
357c478bd9Sstevel@tonic-gate  *   the kernel in the zonehash.  Elsewhere in the kernel, Zone IDs
367c478bd9Sstevel@tonic-gate  *   (zoneid_t) are used to track zone association.  Zone IDs are
377c478bd9Sstevel@tonic-gate  *   dynamically generated when the zone is created; if a persistent
387c478bd9Sstevel@tonic-gate  *   identifier is needed (core files, accounting logs, audit trail,
397c478bd9Sstevel@tonic-gate  *   etc.), the zone name should be used.
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  *   Global Zone:
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  *   The global zone (zoneid 0) is automatically associated with all
457c478bd9Sstevel@tonic-gate  *   system resources that have not been bound to a user-created zone.
467c478bd9Sstevel@tonic-gate  *   This means that even systems where zones are not in active use
477c478bd9Sstevel@tonic-gate  *   have a global zone, and all processes, mounts, etc. are
487c478bd9Sstevel@tonic-gate  *   associated with that zone.  The global zone is generally
497c478bd9Sstevel@tonic-gate  *   unconstrained in terms of privileges and access, though the usual
507c478bd9Sstevel@tonic-gate  *   credential and privilege based restrictions apply.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  *   Zone States:
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  *   The states in which a zone may be in and the transitions are as
567c478bd9Sstevel@tonic-gate  *   follows:
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  *   ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially
597c478bd9Sstevel@tonic-gate  *   initialized zone is added to the list of active zones on the system but
607c478bd9Sstevel@tonic-gate  *   isn't accessible.
617c478bd9Sstevel@tonic-gate  *
62bd41d0a8Snordmark  *   ZONE_IS_INITIALIZED: Initialization complete except the ZSD callbacks are
63bd41d0a8Snordmark  *   not yet completed. Not possible to enter the zone, but attributes can
64bd41d0a8Snordmark  *   be retrieved.
65bd41d0a8Snordmark  *
667c478bd9Sstevel@tonic-gate  *   ZONE_IS_READY: zsched (the kernel dummy process for a zone) is
677c478bd9Sstevel@tonic-gate  *   ready.  The zone is made visible after the ZSD constructor callbacks are
687c478bd9Sstevel@tonic-gate  *   executed.  A zone remains in this state until it transitions into
697c478bd9Sstevel@tonic-gate  *   the ZONE_IS_BOOTING state as a result of a call to zone_boot().
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  *   ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start
727c478bd9Sstevel@tonic-gate  *   init.  Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN
737c478bd9Sstevel@tonic-gate  *   state.
747c478bd9Sstevel@tonic-gate  *
757c478bd9Sstevel@tonic-gate  *   ZONE_IS_RUNNING: The zone is open for business: zsched has
767c478bd9Sstevel@tonic-gate  *   successfully started init.   A zone remains in this state until
777c478bd9Sstevel@tonic-gate  *   zone_shutdown() is called.
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is
807c478bd9Sstevel@tonic-gate  *   killing all processes running in the zone. The zone remains
817c478bd9Sstevel@tonic-gate  *   in this state until there are no more user processes running in the zone.
827c478bd9Sstevel@tonic-gate  *   zone_create(), zone_enter(), and zone_destroy() on this zone will fail.
837c478bd9Sstevel@tonic-gate  *   Since zone_shutdown() is restartable, it may be called successfully
847c478bd9Sstevel@tonic-gate  *   multiple times for the same zone_t.  Setting of the zone's state to
857c478bd9Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check
867c478bd9Sstevel@tonic-gate  *   the zone's status without worrying about it being a moving target.
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  *   ZONE_IS_EMPTY: zone_shutdown() has been called, and there
897c478bd9Sstevel@tonic-gate  *   are no more user processes in the zone.  The zone remains in this
907c478bd9Sstevel@tonic-gate  *   state until there are no more kernel threads associated with the
917c478bd9Sstevel@tonic-gate  *   zone.  zone_create(), zone_enter(), and zone_destroy() on this zone will
927c478bd9Sstevel@tonic-gate  *   fail.
937c478bd9Sstevel@tonic-gate  *
947c478bd9Sstevel@tonic-gate  *   ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone
957c478bd9Sstevel@tonic-gate  *   have exited.  zone_shutdown() returns.  Henceforth it is not possible to
967c478bd9Sstevel@tonic-gate  *   join the zone or create kernel threads therein.
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  *   ZONE_IS_DYING: zone_destroy() has been called on the zone; zone
997c478bd9Sstevel@tonic-gate  *   remains in this state until zsched exits.  Calls to zone_find_by_*()
1007c478bd9Sstevel@tonic-gate  *   return NULL from now on.
1017c478bd9Sstevel@tonic-gate  *
1027c478bd9Sstevel@tonic-gate  *   ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0).  There are no
1037c478bd9Sstevel@tonic-gate  *   processes or threads doing work on behalf of the zone.  The zone is
1047c478bd9Sstevel@tonic-gate  *   removed from the list of active zones.  zone_destroy() returns, and
1057c478bd9Sstevel@tonic-gate  *   the zone can be recreated.
1067c478bd9Sstevel@tonic-gate  *
1077c478bd9Sstevel@tonic-gate  *   ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor
1087c478bd9Sstevel@tonic-gate  *   callbacks are executed, and all memory associated with the zone is
1097c478bd9Sstevel@tonic-gate  *   freed.
1107c478bd9Sstevel@tonic-gate  *
1117c478bd9Sstevel@tonic-gate  *   Threads can wait for the zone to enter a requested state by using
1127c478bd9Sstevel@tonic-gate  *   zone_status_wait() or zone_status_timedwait() with the desired
1137c478bd9Sstevel@tonic-gate  *   state passed in as an argument.  Zone state transitions are
1147c478bd9Sstevel@tonic-gate  *   uni-directional; it is not possible to move back to an earlier state.
1157c478bd9Sstevel@tonic-gate  *
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  *   Zone-Specific Data:
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  *   Subsystems needing to maintain zone-specific data can store that
1207c478bd9Sstevel@tonic-gate  *   data using the ZSD mechanism.  This provides a zone-specific data
1217c478bd9Sstevel@tonic-gate  *   store, similar to thread-specific data (see pthread_getspecific(3C)
1227c478bd9Sstevel@tonic-gate  *   or the TSD code in uts/common/disp/thread.c.  Also, ZSD can be used
1237c478bd9Sstevel@tonic-gate  *   to register callbacks to be invoked when a zone is created, shut
1247c478bd9Sstevel@tonic-gate  *   down, or destroyed.  This can be used to initialize zone-specific
1257c478bd9Sstevel@tonic-gate  *   data for new zones and to clean up when zones go away.
1267c478bd9Sstevel@tonic-gate  *
1277c478bd9Sstevel@tonic-gate  *
1287c478bd9Sstevel@tonic-gate  *   Data Structures:
1297c478bd9Sstevel@tonic-gate  *
1307c478bd9Sstevel@tonic-gate  *   The per-zone structure (zone_t) is reference counted, and freed
1317c478bd9Sstevel@tonic-gate  *   when all references are released.  zone_hold and zone_rele can be
1327c478bd9Sstevel@tonic-gate  *   used to adjust the reference count.  In addition, reference counts
1337c478bd9Sstevel@tonic-gate  *   associated with the cred_t structure are tracked separately using
1347c478bd9Sstevel@tonic-gate  *   zone_cred_hold and zone_cred_rele.
1357c478bd9Sstevel@tonic-gate  *
1367c478bd9Sstevel@tonic-gate  *   Pointers to active zone_t's are stored in two hash tables; one
1377c478bd9Sstevel@tonic-gate  *   for searching by id, the other for searching by name.  Lookups
1387c478bd9Sstevel@tonic-gate  *   can be performed on either basis, using zone_find_by_id and
1397c478bd9Sstevel@tonic-gate  *   zone_find_by_name.  Both return zone_t pointers with the zone
1407c478bd9Sstevel@tonic-gate  *   held, so zone_rele should be called when the pointer is no longer
1417c478bd9Sstevel@tonic-gate  *   needed.  Zones can also be searched by path; zone_find_by_path
1427c478bd9Sstevel@tonic-gate  *   returns the zone with which a path name is associated (global
1437c478bd9Sstevel@tonic-gate  *   zone if the path is not within some other zone's file system
1447c478bd9Sstevel@tonic-gate  *   hierarchy).  This currently requires iterating through each zone,
1457c478bd9Sstevel@tonic-gate  *   so it is slower than an id or name search via a hash table.
1467c478bd9Sstevel@tonic-gate  *
1477c478bd9Sstevel@tonic-gate  *
1487c478bd9Sstevel@tonic-gate  *   Locking:
1497c478bd9Sstevel@tonic-gate  *
1507c478bd9Sstevel@tonic-gate  *   zonehash_lock: This is a top-level global lock used to protect the
1517c478bd9Sstevel@tonic-gate  *       zone hash tables and lists.  Zones cannot be created or destroyed
1527c478bd9Sstevel@tonic-gate  *       while this lock is held.
1537c478bd9Sstevel@tonic-gate  *   zone_status_lock: This is a global lock protecting zone state.
1547c478bd9Sstevel@tonic-gate  *       Zones cannot change state while this lock is held.  It also
1557c478bd9Sstevel@tonic-gate  *       protects the list of kernel threads associated with a zone.
1567c478bd9Sstevel@tonic-gate  *   zone_lock: This is a per-zone lock used to protect several fields of
1577c478bd9Sstevel@tonic-gate  *       the zone_t (see <sys/zone.h> for details).  In addition, holding
1587c478bd9Sstevel@tonic-gate  *       this lock means that the zone cannot go away.
1590209230bSgjelinek  *   zone_nlwps_lock: This is a per-zone lock used to protect the fields
1600209230bSgjelinek  *	 related to the zone.max-lwps rctl.
1610209230bSgjelinek  *   zone_mem_lock: This is a per-zone lock used to protect the fields
1620209230bSgjelinek  *	 related to the zone.max-locked-memory and zone.max-swap rctls.
1630fbb751dSJohn Levon  *   zone_rctl_lock: This is a per-zone lock used to protect other rctls,
1640fbb751dSJohn Levon  *       currently just max_lofi
1657c478bd9Sstevel@tonic-gate  *   zsd_key_lock: This is a global lock protecting the key state for ZSD.
1667c478bd9Sstevel@tonic-gate  *   zone_deathrow_lock: This is a global lock protecting the "deathrow"
1677c478bd9Sstevel@tonic-gate  *       list (a list of zones in the ZONE_IS_DEAD state).
1687c478bd9Sstevel@tonic-gate  *
1697c478bd9Sstevel@tonic-gate  *   Ordering requirements:
1707c478bd9Sstevel@tonic-gate  *       pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock -->
1717c478bd9Sstevel@tonic-gate  *       	zone_lock --> zsd_key_lock --> pidlock --> p_lock
1727c478bd9Sstevel@tonic-gate  *
1730209230bSgjelinek  *   When taking zone_mem_lock or zone_nlwps_lock, the lock ordering is:
1740209230bSgjelinek  *	zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_mem_lock
175ff19e029SMenno Lageman  *	zonehash_lock --> a_lock --> pidlock --> p_lock --> zone_nlwps_lock
1760209230bSgjelinek  *
1777c478bd9Sstevel@tonic-gate  *   Blocking memory allocations are permitted while holding any of the
1787c478bd9Sstevel@tonic-gate  *   zone locks.
1797c478bd9Sstevel@tonic-gate  *
1807c478bd9Sstevel@tonic-gate  *
1817c478bd9Sstevel@tonic-gate  *   System Call Interface:
1827c478bd9Sstevel@tonic-gate  *
1837c478bd9Sstevel@tonic-gate  *   The zone subsystem can be managed and queried from user level with
1847c478bd9Sstevel@tonic-gate  *   the following system calls (all subcodes of the primary "zone"
1857c478bd9Sstevel@tonic-gate  *   system call):
1867c478bd9Sstevel@tonic-gate  *   - zone_create: creates a zone with selected attributes (name,
187fa9e4066Sahrens  *     root path, privileges, resource controls, ZFS datasets)
1887c478bd9Sstevel@tonic-gate  *   - zone_enter: allows the current process to enter a zone
1897c478bd9Sstevel@tonic-gate  *   - zone_getattr: reports attributes of a zone
1903f2f09c1Sdp  *   - zone_setattr: set attributes of a zone
1913f2f09c1Sdp  *   - zone_boot: set 'init' running for the zone
1927c478bd9Sstevel@tonic-gate  *   - zone_list: lists all zones active in the system
1937c478bd9Sstevel@tonic-gate  *   - zone_lookup: looks up zone id based on name
1947c478bd9Sstevel@tonic-gate  *   - zone_shutdown: initiates shutdown process (see states above)
1957c478bd9Sstevel@tonic-gate  *   - zone_destroy: completes shutdown process (see states above)
1967c478bd9Sstevel@tonic-gate  *
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate #include <sys/priv_impl.h>
2007c478bd9Sstevel@tonic-gate #include <sys/cred.h>
2017c478bd9Sstevel@tonic-gate #include <c2/audit.h>
2027c478bd9Sstevel@tonic-gate #include <sys/debug.h>
2037c478bd9Sstevel@tonic-gate #include <sys/file.h>
2047c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
2050209230bSgjelinek #include <sys/kstat.h>
2067c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
20745916cd2Sjpk #include <sys/note.h>
2087c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
2097c478bd9Sstevel@tonic-gate #include <sys/proc.h>
2107c478bd9Sstevel@tonic-gate #include <sys/project.h>
211cf8f45c7Sdstaff #include <sys/sysevent.h>
2127c478bd9Sstevel@tonic-gate #include <sys/task.h>
2137c478bd9Sstevel@tonic-gate #include <sys/systm.h>
2147c478bd9Sstevel@tonic-gate #include <sys/types.h>
2157c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
2167c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
2177c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
2187c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
2197c478bd9Sstevel@tonic-gate #include <sys/policy.h>
2207c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
2217c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h>
2227c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
2237c478bd9Sstevel@tonic-gate #include <sys/class.h>
2247c478bd9Sstevel@tonic-gate #include <sys/pool.h>
2257c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
2267c478bd9Sstevel@tonic-gate #include <sys/pset.h>
227a19609f8Sjv227347 #include <sys/strlog.h>
2287c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
2297c478bd9Sstevel@tonic-gate #include <sys/callb.h>
2307c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
2317c478bd9Sstevel@tonic-gate #include <sys/corectl.h>
232824c205fSml93401 #include <sys/ipc_impl.h>
233134a1f4eSCasper H.S. Dik #include <sys/klpd.h>
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate #include <sys/door.h>
2367c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
237bd41d0a8Snordmark #include <sys/sdt.h>
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
2407c478bd9Sstevel@tonic-gate #include <sys/session.h>
2417c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
2427c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
2433f2f09c1Sdp #include <sys/sunddi.h>
2447c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
2457c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
2467c478bd9Sstevel@tonic-gate #include <sys/fss.h>
2479acbbeafSnn35248 #include <sys/brand.h>
2487c478bd9Sstevel@tonic-gate #include <sys/zone.h>
249f4b3ec61Sdh155122 #include <net/if.h>
250c97ad5cdSakolb #include <sys/cpucaps.h>
2510209230bSgjelinek #include <vm/seg.h>
2522b24ab6bSSebastien Roy #include <sys/mac.h>
2532b24ab6bSSebastien Roy 
254a19609f8Sjv227347 /*
255a19609f8Sjv227347  * This constant specifies the number of seconds that threads waiting for
256a19609f8Sjv227347  * subsystems to release a zone's general-purpose references will wait before
257a19609f8Sjv227347  * they log the zone's reference counts.  The constant's value shouldn't
258a19609f8Sjv227347  * be so small that reference counts are unnecessarily reported for zones
259a19609f8Sjv227347  * whose references are slowly released.  On the other hand, it shouldn't be so
260a19609f8Sjv227347  * large that users reboot their systems out of frustration over hung zones
261a19609f8Sjv227347  * before the system logs the zones' reference counts.
262a19609f8Sjv227347  */
263a19609f8Sjv227347 #define	ZONE_DESTROY_TIMEOUT_SECS	60
264a19609f8Sjv227347 
2652b24ab6bSSebastien Roy /* List of data link IDs which are accessible from the zone */
2662b24ab6bSSebastien Roy typedef struct zone_dl {
2672b24ab6bSSebastien Roy 	datalink_id_t	zdl_id;
268550b6e40SSowmini Varadhan 	nvlist_t	*zdl_net;
2692b24ab6bSSebastien Roy 	list_node_t	zdl_linkage;
2702b24ab6bSSebastien Roy } zone_dl_t;
2710209230bSgjelinek 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  * cv used to signal that all references to the zone have been released.  This
2747c478bd9Sstevel@tonic-gate  * needs to be global since there may be multiple waiters, and the first to
2757c478bd9Sstevel@tonic-gate  * wake up will free the zone_t, hence we cannot use zone->zone_cv.
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate static kcondvar_t zone_destroy_cv;
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate  * Lock used to serialize access to zone_cv.  This could have been per-zone,
2807c478bd9Sstevel@tonic-gate  * but then we'd need another lock for zone_destroy_cv, and why bother?
2817c478bd9Sstevel@tonic-gate  */
2827c478bd9Sstevel@tonic-gate static kmutex_t zone_status_lock;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate  * ZSD-related global variables.
2867c478bd9Sstevel@tonic-gate  */
2877c478bd9Sstevel@tonic-gate static kmutex_t zsd_key_lock;	/* protects the following two */
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval.
2907c478bd9Sstevel@tonic-gate  */
2917c478bd9Sstevel@tonic-gate static zone_key_t zsd_keyval = 0;
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  * Global list of registered keys.  We use this when a new zone is created.
2947c478bd9Sstevel@tonic-gate  */
2957c478bd9Sstevel@tonic-gate static list_t zsd_registered_keys;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate int zone_hash_size = 256;
29845916cd2Sjpk static mod_hash_t *zonehashbyname, *zonehashbyid, *zonehashbylabel;
2997c478bd9Sstevel@tonic-gate static kmutex_t zonehash_lock;
3007c478bd9Sstevel@tonic-gate static uint_t zonecount;
3017c478bd9Sstevel@tonic-gate static id_space_t *zoneid_space;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate  * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the
3057c478bd9Sstevel@tonic-gate  * kernel proper runs, and which manages all other zones.
3067c478bd9Sstevel@tonic-gate  *
3077c478bd9Sstevel@tonic-gate  * Although not declared as static, the variable "zone0" should not be used
3087c478bd9Sstevel@tonic-gate  * except for by code that needs to reference the global zone early on in boot,
3097c478bd9Sstevel@tonic-gate  * before it is fully initialized.  All other consumers should use
3107c478bd9Sstevel@tonic-gate  * 'global_zone'.
3117c478bd9Sstevel@tonic-gate  */
3127c478bd9Sstevel@tonic-gate zone_t zone0;
3137c478bd9Sstevel@tonic-gate zone_t *global_zone = NULL;	/* Set when the global zone is initialized */
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate /*
3167c478bd9Sstevel@tonic-gate  * List of active zones, protected by zonehash_lock.
3177c478bd9Sstevel@tonic-gate  */
3187c478bd9Sstevel@tonic-gate static list_t zone_active;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate  * List of destroyed zones that still have outstanding cred references.
3227c478bd9Sstevel@tonic-gate  * Used for debugging.  Uses a separate lock to avoid lock ordering
3237c478bd9Sstevel@tonic-gate  * problems in zone_free.
3247c478bd9Sstevel@tonic-gate  */
3257c478bd9Sstevel@tonic-gate static list_t zone_deathrow;
3267c478bd9Sstevel@tonic-gate static kmutex_t zone_deathrow_lock;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */
3297c478bd9Sstevel@tonic-gate uint_t maxzones = 8192;
3307c478bd9Sstevel@tonic-gate 
331cf8f45c7Sdstaff /* Event channel to sent zone state change notifications */
332cf8f45c7Sdstaff evchan_t *zone_event_chan;
333cf8f45c7Sdstaff 
334cf8f45c7Sdstaff /*
335cf8f45c7Sdstaff  * This table holds the mapping from kernel zone states to
336cf8f45c7Sdstaff  * states visible in the state notification API.
337cf8f45c7Sdstaff  * The idea is that we only expose "obvious" states and
338cf8f45c7Sdstaff  * do not expose states which are just implementation details.
339cf8f45c7Sdstaff  */
340cf8f45c7Sdstaff const char  *zone_status_table[] = {
341cf8f45c7Sdstaff 	ZONE_EVENT_UNINITIALIZED,	/* uninitialized */
342bd41d0a8Snordmark 	ZONE_EVENT_INITIALIZED,		/* initialized */
343cf8f45c7Sdstaff 	ZONE_EVENT_READY,		/* ready */
344cf8f45c7Sdstaff 	ZONE_EVENT_READY,		/* booting */
345cf8f45c7Sdstaff 	ZONE_EVENT_RUNNING,		/* running */
346cf8f45c7Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* shutting_down */
347cf8f45c7Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* empty */
348cf8f45c7Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* down */
349cf8f45c7Sdstaff 	ZONE_EVENT_SHUTTING_DOWN,	/* dying */
350cf8f45c7Sdstaff 	ZONE_EVENT_UNINITIALIZED,	/* dead */
351cf8f45c7Sdstaff };
352cf8f45c7Sdstaff 
3537c478bd9Sstevel@tonic-gate /*
354a19609f8Sjv227347  * This array contains the names of the subsystems listed in zone_ref_subsys_t
355a19609f8Sjv227347  * (see sys/zone.h).
356a19609f8Sjv227347  */
357a19609f8Sjv227347 static char *zone_ref_subsys_names[] = {
358a19609f8Sjv227347 	"NFS",		/* ZONE_REF_NFS */
359a19609f8Sjv227347 	"NFSv4",	/* ZONE_REF_NFSV4 */
360a19609f8Sjv227347 	"SMBFS",	/* ZONE_REF_SMBFS */
361a19609f8Sjv227347 	"MNTFS",	/* ZONE_REF_MNTFS */
362a19609f8Sjv227347 	"LOFI",		/* ZONE_REF_LOFI */
363a19609f8Sjv227347 	"VFS",		/* ZONE_REF_VFS */
364a19609f8Sjv227347 	"IPC"		/* ZONE_REF_IPC */
365a19609f8Sjv227347 };
366a19609f8Sjv227347 
367a19609f8Sjv227347 /*
3687c478bd9Sstevel@tonic-gate  * This isn't static so lint doesn't complain.
3697c478bd9Sstevel@tonic-gate  */
3707c478bd9Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares;
371c6939658Ssl108498 rctl_hndl_t rc_zone_locked_mem;
3720209230bSgjelinek rctl_hndl_t rc_zone_max_swap;
3730fbb751dSJohn Levon rctl_hndl_t rc_zone_max_lofi;
374c97ad5cdSakolb rctl_hndl_t rc_zone_cpu_cap;
3757c478bd9Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps;
376ff19e029SMenno Lageman rctl_hndl_t rc_zone_nprocs;
377824c205fSml93401 rctl_hndl_t rc_zone_shmmax;
378824c205fSml93401 rctl_hndl_t rc_zone_shmmni;
379824c205fSml93401 rctl_hndl_t rc_zone_semmni;
380824c205fSml93401 rctl_hndl_t rc_zone_msgmni;
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate  * Synchronization primitives used to synchronize between mounts and zone
3837c478bd9Sstevel@tonic-gate  * creation/destruction.
3847c478bd9Sstevel@tonic-gate  */
3857c478bd9Sstevel@tonic-gate static int mounts_in_progress;
3867c478bd9Sstevel@tonic-gate static kcondvar_t mount_cv;
3877c478bd9Sstevel@tonic-gate static kmutex_t mount_lock;
3887c478bd9Sstevel@tonic-gate 
3893f2f09c1Sdp const char * const zone_default_initname = "/sbin/init";
39045916cd2Sjpk static char * const zone_prefix = "/zone/";
3917c478bd9Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid);
3922b24ab6bSSebastien Roy static int zone_add_datalink(zoneid_t, datalink_id_t);
3932b24ab6bSSebastien Roy static int zone_remove_datalink(zoneid_t, datalink_id_t);
3942b24ab6bSSebastien Roy static int zone_list_datalink(zoneid_t, int *, datalink_id_t *);
395550b6e40SSowmini Varadhan static int zone_set_network(zoneid_t, zone_net_data_t *);
396550b6e40SSowmini Varadhan static int zone_get_network(zoneid_t, zone_net_data_t *);
3977c478bd9Sstevel@tonic-gate 
398bd41d0a8Snordmark typedef boolean_t zsd_applyfn_t(kmutex_t *, boolean_t, zone_t *, zone_key_t);
399bd41d0a8Snordmark 
400bd41d0a8Snordmark static void zsd_apply_all_zones(zsd_applyfn_t *, zone_key_t);
401bd41d0a8Snordmark static void zsd_apply_all_keys(zsd_applyfn_t *, zone_t *);
402bd41d0a8Snordmark static boolean_t zsd_apply_create(kmutex_t *, boolean_t, zone_t *, zone_key_t);
403bd41d0a8Snordmark static boolean_t zsd_apply_shutdown(kmutex_t *, boolean_t, zone_t *,
404bd41d0a8Snordmark     zone_key_t);
405bd41d0a8Snordmark static boolean_t zsd_apply_destroy(kmutex_t *, boolean_t, zone_t *, zone_key_t);
406bd41d0a8Snordmark static boolean_t zsd_wait_for_creator(zone_t *, struct zsd_entry *,
407bd41d0a8Snordmark     kmutex_t *);
408bd41d0a8Snordmark static boolean_t zsd_wait_for_inprogress(zone_t *, struct zsd_entry *,
409bd41d0a8Snordmark     kmutex_t *);
410bd41d0a8Snordmark 
4117c478bd9Sstevel@tonic-gate /*
412821c4a97Sdp  * Bump this number when you alter the zone syscall interfaces; this is
413821c4a97Sdp  * because we need to have support for previous API versions in libc
414821c4a97Sdp  * to support patching; libc calls into the kernel to determine this number.
415821c4a97Sdp  *
416821c4a97Sdp  * Version 1 of the API is the version originally shipped with Solaris 10
417821c4a97Sdp  * Version 2 alters the zone_create system call in order to support more
418821c4a97Sdp  *     arguments by moving the args into a structure; and to do better
419821c4a97Sdp  *     error reporting when zone_create() fails.
420821c4a97Sdp  * Version 3 alters the zone_create system call in order to support the
421821c4a97Sdp  *     import of ZFS datasets to zones.
42245916cd2Sjpk  * Version 4 alters the zone_create system call in order to support
42345916cd2Sjpk  *     Trusted Extensions.
4243f2f09c1Sdp  * Version 5 alters the zone_boot system call, and converts its old
4253f2f09c1Sdp  *     bootargs parameter to be set by the zone_setattr API instead.
426f4b3ec61Sdh155122  * Version 6 adds the flag argument to zone_create.
427821c4a97Sdp  */
428f4b3ec61Sdh155122 static const int ZONE_SYSCALL_API_VERSION = 6;
429821c4a97Sdp 
430821c4a97Sdp /*
4317c478bd9Sstevel@tonic-gate  * Certain filesystems (such as NFS and autofs) need to know which zone
4327c478bd9Sstevel@tonic-gate  * the mount is being placed in.  Because of this, we need to be able to
4337c478bd9Sstevel@tonic-gate  * ensure that a zone isn't in the process of being created such that
4347c478bd9Sstevel@tonic-gate  * nfs_mount() thinks it is in the global zone, while by the time it
4357c478bd9Sstevel@tonic-gate  * gets added the list of mounted zones, it ends up on zoneA's mount
4367c478bd9Sstevel@tonic-gate  * list.
4377c478bd9Sstevel@tonic-gate  *
4387c478bd9Sstevel@tonic-gate  * The following functions: block_mounts()/resume_mounts() and
4397c478bd9Sstevel@tonic-gate  * mount_in_progress()/mount_completed() are used by zones and the VFS
4407c478bd9Sstevel@tonic-gate  * layer (respectively) to synchronize zone creation and new mounts.
4417c478bd9Sstevel@tonic-gate  *
4427c478bd9Sstevel@tonic-gate  * The semantics are like a reader-reader lock such that there may
4437c478bd9Sstevel@tonic-gate  * either be multiple mounts (or zone creations, if that weren't
4447c478bd9Sstevel@tonic-gate  * serialized by zonehash_lock) in progress at the same time, but not
4457c478bd9Sstevel@tonic-gate  * both.
4467c478bd9Sstevel@tonic-gate  *
4477c478bd9Sstevel@tonic-gate  * We use cv's so the user can ctrl-C out of the operation if it's
4487c478bd9Sstevel@tonic-gate  * taking too long.
4497c478bd9Sstevel@tonic-gate  *
4507c478bd9Sstevel@tonic-gate  * The semantics are such that there is unfair bias towards the
4517c478bd9Sstevel@tonic-gate  * "current" operation.  This means that zone creations may starve if
4527c478bd9Sstevel@tonic-gate  * there is a rapid succession of new mounts coming in to the system, or
4537c478bd9Sstevel@tonic-gate  * there is a remote possibility that zones will be created at such a
4547c478bd9Sstevel@tonic-gate  * rate that new mounts will not be able to proceed.
4557c478bd9Sstevel@tonic-gate  */
4567c478bd9Sstevel@tonic-gate /*
4577c478bd9Sstevel@tonic-gate  * Prevent new mounts from progressing to the point of calling
4587c478bd9Sstevel@tonic-gate  * VFS_MOUNT().  If there are already mounts in this "region", wait for
4597c478bd9Sstevel@tonic-gate  * them to complete.
4607c478bd9Sstevel@tonic-gate  */
4617c478bd9Sstevel@tonic-gate static int
4627c478bd9Sstevel@tonic-gate block_mounts(void)
4637c478bd9Sstevel@tonic-gate {
4647c478bd9Sstevel@tonic-gate 	int retval = 0;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	/*
4677c478bd9Sstevel@tonic-gate 	 * Since it may block for a long time, block_mounts() shouldn't be
4687c478bd9Sstevel@tonic-gate 	 * called with zonehash_lock held.
4697c478bd9Sstevel@tonic-gate 	 */
4707c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
4717c478bd9Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4727c478bd9Sstevel@tonic-gate 	while (mounts_in_progress > 0) {
4737c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&mount_cv, &mount_lock) == 0)
4747c478bd9Sstevel@tonic-gate 			goto signaled;
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 	/*
4777c478bd9Sstevel@tonic-gate 	 * A negative value of mounts_in_progress indicates that mounts
4787c478bd9Sstevel@tonic-gate 	 * have been blocked by (-mounts_in_progress) different callers.
4797c478bd9Sstevel@tonic-gate 	 */
4807c478bd9Sstevel@tonic-gate 	mounts_in_progress--;
4817c478bd9Sstevel@tonic-gate 	retval = 1;
4827c478bd9Sstevel@tonic-gate signaled:
4837c478bd9Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4847c478bd9Sstevel@tonic-gate 	return (retval);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /*
4887c478bd9Sstevel@tonic-gate  * The VFS layer may progress with new mounts as far as we're concerned.
4897c478bd9Sstevel@tonic-gate  * Allow them to progress if we were the last obstacle.
4907c478bd9Sstevel@tonic-gate  */
4917c478bd9Sstevel@tonic-gate static void
4927c478bd9Sstevel@tonic-gate resume_mounts(void)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	mutex_enter(&mount_lock);
4957c478bd9Sstevel@tonic-gate 	if (++mounts_in_progress == 0)
4967c478bd9Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
4977c478bd9Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate /*
5017c478bd9Sstevel@tonic-gate  * The VFS layer is busy with a mount; zones should wait until all
5027c478bd9Sstevel@tonic-gate  * mounts are completed to progress.
5037c478bd9Sstevel@tonic-gate  */
5047c478bd9Sstevel@tonic-gate void
5057c478bd9Sstevel@tonic-gate mount_in_progress(void)
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate 	mutex_enter(&mount_lock);
5087c478bd9Sstevel@tonic-gate 	while (mounts_in_progress < 0)
5097c478bd9Sstevel@tonic-gate 		cv_wait(&mount_cv, &mount_lock);
5107c478bd9Sstevel@tonic-gate 	mounts_in_progress++;
5117c478bd9Sstevel@tonic-gate 	mutex_exit(&mount_lock);
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate /*
5157c478bd9Sstevel@tonic-gate  * VFS is done with one mount; wake up any waiting block_mounts()
5167c478bd9Sstevel@tonic-gate  * callers if this is the last mount.
5177c478bd9Sstevel@tonic-gate  */
5187c478bd9Sstevel@tonic-gate void
5197c478bd9Sstevel@tonic-gate mount_completed(void)
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate 	mutex_enter(&mount_lock);
5227c478bd9Sstevel@tonic-gate 	if (--mounts_in_progress == 0)
5237c478bd9Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
5247c478bd9Sstevel@tonic-gate 	mutex_exit(&mount_lock);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate /*
5287c478bd9Sstevel@tonic-gate  * ZSD routines.
5297c478bd9Sstevel@tonic-gate  *
5307c478bd9Sstevel@tonic-gate  * Zone Specific Data (ZSD) is modeled after Thread Specific Data as
5317c478bd9Sstevel@tonic-gate  * defined by the pthread_key_create() and related interfaces.
5327c478bd9Sstevel@tonic-gate  *
5337c478bd9Sstevel@tonic-gate  * Kernel subsystems may register one or more data items and/or
5347c478bd9Sstevel@tonic-gate  * callbacks to be executed when a zone is created, shutdown, or
5357c478bd9Sstevel@tonic-gate  * destroyed.
5367c478bd9Sstevel@tonic-gate  *
5377c478bd9Sstevel@tonic-gate  * Unlike the thread counterpart, destructor callbacks will be executed
5387c478bd9Sstevel@tonic-gate  * even if the data pointer is NULL and/or there are no constructor
5397c478bd9Sstevel@tonic-gate  * callbacks, so it is the responsibility of such callbacks to check for
5407c478bd9Sstevel@tonic-gate  * NULL data values if necessary.
5417c478bd9Sstevel@tonic-gate  *
5427c478bd9Sstevel@tonic-gate  * The locking strategy and overall picture is as follows:
5437c478bd9Sstevel@tonic-gate  *
5447c478bd9Sstevel@tonic-gate  * When someone calls zone_key_create(), a template ZSD entry is added to the
545bd41d0a8Snordmark  * global list "zsd_registered_keys", protected by zsd_key_lock.  While
546bd41d0a8Snordmark  * holding that lock all the existing zones are marked as
547bd41d0a8Snordmark  * ZSD_CREATE_NEEDED and a copy of the ZSD entry added to the per-zone
548bd41d0a8Snordmark  * zone_zsd list (protected by zone_lock). The global list is updated first
549bd41d0a8Snordmark  * (under zone_key_lock) to make sure that newly created zones use the
550bd41d0a8Snordmark  * most recent list of keys. Then under zonehash_lock we walk the zones
551bd41d0a8Snordmark  * and mark them.  Similar locking is used in zone_key_delete().
5527c478bd9Sstevel@tonic-gate  *
553bd41d0a8Snordmark  * The actual create, shutdown, and destroy callbacks are done without
554bd41d0a8Snordmark  * holding any lock. And zsd_flags are used to ensure that the operations
555bd41d0a8Snordmark  * completed so that when zone_key_create (and zone_create) is done, as well as
556bd41d0a8Snordmark  * zone_key_delete (and zone_destroy) is done, all the necessary callbacks
557bd41d0a8Snordmark  * are completed.
5587c478bd9Sstevel@tonic-gate  *
5597c478bd9Sstevel@tonic-gate  * When new zones are created constructor callbacks for all registered ZSD
560bd41d0a8Snordmark  * entries will be called. That also uses the above two phases of marking
561bd41d0a8Snordmark  * what needs to be done, and then running the callbacks without holding
562bd41d0a8Snordmark  * any locks.
5637c478bd9Sstevel@tonic-gate  *
5647c478bd9Sstevel@tonic-gate  * The framework does not provide any locking around zone_getspecific() and
5657c478bd9Sstevel@tonic-gate  * zone_setspecific() apart from that needed for internal consistency, so
5667c478bd9Sstevel@tonic-gate  * callers interested in atomic "test-and-set" semantics will need to provide
5677c478bd9Sstevel@tonic-gate  * their own locking.
5687c478bd9Sstevel@tonic-gate  */
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate  * Helper function to find the zsd_entry associated with the key in the
5727c478bd9Sstevel@tonic-gate  * given list.
5737c478bd9Sstevel@tonic-gate  */
5747c478bd9Sstevel@tonic-gate static struct zsd_entry *
5757c478bd9Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key)
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsd;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) {
5807c478bd9Sstevel@tonic-gate 		if (zsd->zsd_key == key) {
581bd41d0a8Snordmark 			return (zsd);
582bd41d0a8Snordmark 		}
583bd41d0a8Snordmark 	}
584bd41d0a8Snordmark 	return (NULL);
585bd41d0a8Snordmark }
586bd41d0a8Snordmark 
587bd41d0a8Snordmark /*
588bd41d0a8Snordmark  * Helper function to find the zsd_entry associated with the key in the
589bd41d0a8Snordmark  * given list. Move it to the front of the list.
590bd41d0a8Snordmark  */
591bd41d0a8Snordmark static struct zsd_entry *
592bd41d0a8Snordmark zsd_find_mru(list_t *l, zone_key_t key)
593bd41d0a8Snordmark {
594bd41d0a8Snordmark 	struct zsd_entry *zsd;
595bd41d0a8Snordmark 
596bd41d0a8Snordmark 	for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) {
597bd41d0a8Snordmark 		if (zsd->zsd_key == key) {
5987c478bd9Sstevel@tonic-gate 			/*
5997c478bd9Sstevel@tonic-gate 			 * Move to head of list to keep list in MRU order.
6007c478bd9Sstevel@tonic-gate 			 */
6017c478bd9Sstevel@tonic-gate 			if (zsd != list_head(l)) {
6027c478bd9Sstevel@tonic-gate 				list_remove(l, zsd);
6037c478bd9Sstevel@tonic-gate 				list_insert_head(l, zsd);
6047c478bd9Sstevel@tonic-gate 			}
6057c478bd9Sstevel@tonic-gate 			return (zsd);
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 	return (NULL);
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate 
611bd41d0a8Snordmark void
612bd41d0a8Snordmark zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t),
613bd41d0a8Snordmark     void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *))
614bd41d0a8Snordmark {
615bd41d0a8Snordmark 	struct zsd_entry *zsdp;
616bd41d0a8Snordmark 	struct zsd_entry *t;
617bd41d0a8Snordmark 	struct zone *zone;
618bd41d0a8Snordmark 	zone_key_t  key;
619bd41d0a8Snordmark 
620bd41d0a8Snordmark 	zsdp = kmem_zalloc(sizeof (*zsdp), KM_SLEEP);
621bd41d0a8Snordmark 	zsdp->zsd_data = NULL;
622bd41d0a8Snordmark 	zsdp->zsd_create = create;
623bd41d0a8Snordmark 	zsdp->zsd_shutdown = shutdown;
624bd41d0a8Snordmark 	zsdp->zsd_destroy = destroy;
625bd41d0a8Snordmark 
626bd41d0a8Snordmark 	/*
627bd41d0a8Snordmark 	 * Insert in global list of callbacks. Makes future zone creations
628bd41d0a8Snordmark 	 * see it.
629bd41d0a8Snordmark 	 */
630bd41d0a8Snordmark 	mutex_enter(&zsd_key_lock);
631fe16170aSPramod Batni 	key = zsdp->zsd_key = ++zsd_keyval;
632bd41d0a8Snordmark 	ASSERT(zsd_keyval != 0);
633bd41d0a8Snordmark 	list_insert_tail(&zsd_registered_keys, zsdp);
634bd41d0a8Snordmark 	mutex_exit(&zsd_key_lock);
635bd41d0a8Snordmark 
636bd41d0a8Snordmark 	/*
637bd41d0a8Snordmark 	 * Insert for all existing zones and mark them as needing
638bd41d0a8Snordmark 	 * a create callback.
639bd41d0a8Snordmark 	 */
640bd41d0a8Snordmark 	mutex_enter(&zonehash_lock);	/* stop the world */
641bd41d0a8Snordmark 	for (zone = list_head(&zone_active); zone != NULL;
642bd41d0a8Snordmark 	    zone = list_next(&zone_active, zone)) {
643bd41d0a8Snordmark 		zone_status_t status;
644bd41d0a8Snordmark 
645bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
646bd41d0a8Snordmark 
647bd41d0a8Snordmark 		/* Skip zones that are on the way down or not yet up */
648bd41d0a8Snordmark 		status = zone_status_get(zone);
649bd41d0a8Snordmark 		if (status >= ZONE_IS_DOWN ||
650bd41d0a8Snordmark 		    status == ZONE_IS_UNINITIALIZED) {
651bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
652bd41d0a8Snordmark 			continue;
653bd41d0a8Snordmark 		}
654bd41d0a8Snordmark 
655bd41d0a8Snordmark 		t = zsd_find_mru(&zone->zone_zsd, key);
656bd41d0a8Snordmark 		if (t != NULL) {
657bd41d0a8Snordmark 			/*
658bd41d0a8Snordmark 			 * A zsd_configure already inserted it after
659bd41d0a8Snordmark 			 * we dropped zsd_key_lock above.
660bd41d0a8Snordmark 			 */
661bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
662bd41d0a8Snordmark 			continue;
663bd41d0a8Snordmark 		}
664bd41d0a8Snordmark 		t = kmem_zalloc(sizeof (*t), KM_SLEEP);
665bd41d0a8Snordmark 		t->zsd_key = key;
666bd41d0a8Snordmark 		t->zsd_create = create;
667bd41d0a8Snordmark 		t->zsd_shutdown = shutdown;
668bd41d0a8Snordmark 		t->zsd_destroy = destroy;
669bd41d0a8Snordmark 		if (create != NULL) {
670bd41d0a8Snordmark 			t->zsd_flags = ZSD_CREATE_NEEDED;
671bd41d0a8Snordmark 			DTRACE_PROBE2(zsd__create__needed,
672bd41d0a8Snordmark 			    zone_t *, zone, zone_key_t, key);
673bd41d0a8Snordmark 		}
674bd41d0a8Snordmark 		list_insert_tail(&zone->zone_zsd, t);
675bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
676bd41d0a8Snordmark 	}
677bd41d0a8Snordmark 	mutex_exit(&zonehash_lock);
678bd41d0a8Snordmark 
679bd41d0a8Snordmark 	if (create != NULL) {
680bd41d0a8Snordmark 		/* Now call the create callback for this key */
681bd41d0a8Snordmark 		zsd_apply_all_zones(zsd_apply_create, key);
682bd41d0a8Snordmark 	}
683fe16170aSPramod Batni 	/*
684fe16170aSPramod Batni 	 * It is safe for consumers to use the key now, make it
685fe16170aSPramod Batni 	 * globally visible. Specifically zone_getspecific() will
686fe16170aSPramod Batni 	 * always successfully return the zone specific data associated
687fe16170aSPramod Batni 	 * with the key.
688fe16170aSPramod Batni 	 */
689fe16170aSPramod Batni 	*keyp = key;
690fe16170aSPramod Batni 
691bd41d0a8Snordmark }
692bd41d0a8Snordmark 
6937c478bd9Sstevel@tonic-gate /*
6947c478bd9Sstevel@tonic-gate  * Function called when a module is being unloaded, or otherwise wishes
6957c478bd9Sstevel@tonic-gate  * to unregister its ZSD key and callbacks.
696bd41d0a8Snordmark  *
697bd41d0a8Snordmark  * Remove from the global list and determine the functions that need to
698bd41d0a8Snordmark  * be called under a global lock. Then call the functions without
699bd41d0a8Snordmark  * holding any locks. Finally free up the zone_zsd entries. (The apply
700bd41d0a8Snordmark  * functions need to access the zone_zsd entries to find zsd_data etc.)
7017c478bd9Sstevel@tonic-gate  */
7027c478bd9Sstevel@tonic-gate int
7037c478bd9Sstevel@tonic-gate zone_key_delete(zone_key_t key)
7047c478bd9Sstevel@tonic-gate {
7057c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsdp = NULL;
7067c478bd9Sstevel@tonic-gate 	zone_t *zone;
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
709bd41d0a8Snordmark 	zsdp = zsd_find_mru(&zsd_registered_keys, key);
710bd41d0a8Snordmark 	if (zsdp == NULL) {
711bd41d0a8Snordmark 		mutex_exit(&zsd_key_lock);
712bd41d0a8Snordmark 		return (-1);
713bd41d0a8Snordmark 	}
7147c478bd9Sstevel@tonic-gate 	list_remove(&zsd_registered_keys, zsdp);
7157c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
7167c478bd9Sstevel@tonic-gate 
717bd41d0a8Snordmark 	mutex_enter(&zonehash_lock);
7187c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
7197c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
7207c478bd9Sstevel@tonic-gate 		struct zsd_entry *del;
7217c478bd9Sstevel@tonic-gate 
722bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
723bd41d0a8Snordmark 		del = zsd_find_mru(&zone->zone_zsd, key);
724bd41d0a8Snordmark 		if (del == NULL) {
725bd41d0a8Snordmark 			/*
726bd41d0a8Snordmark 			 * Somebody else got here first e.g the zone going
727bd41d0a8Snordmark 			 * away.
728bd41d0a8Snordmark 			 */
729bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
730bd41d0a8Snordmark 			continue;
731bd41d0a8Snordmark 		}
7327c478bd9Sstevel@tonic-gate 		ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown);
7337c478bd9Sstevel@tonic-gate 		ASSERT(del->zsd_destroy == zsdp->zsd_destroy);
734bd41d0a8Snordmark 		if (del->zsd_shutdown != NULL &&
735bd41d0a8Snordmark 		    (del->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) {
736bd41d0a8Snordmark 			del->zsd_flags |= ZSD_SHUTDOWN_NEEDED;
737bd41d0a8Snordmark 			DTRACE_PROBE2(zsd__shutdown__needed,
738bd41d0a8Snordmark 			    zone_t *, zone, zone_key_t, key);
7397c478bd9Sstevel@tonic-gate 		}
740bd41d0a8Snordmark 		if (del->zsd_destroy != NULL &&
741bd41d0a8Snordmark 		    (del->zsd_flags & ZSD_DESTROY_ALL) == 0) {
742bd41d0a8Snordmark 			del->zsd_flags |= ZSD_DESTROY_NEEDED;
743bd41d0a8Snordmark 			DTRACE_PROBE2(zsd__destroy__needed,
744bd41d0a8Snordmark 			    zone_t *, zone, zone_key_t, key);
7457c478bd9Sstevel@tonic-gate 		}
7467c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
7497c478bd9Sstevel@tonic-gate 	kmem_free(zsdp, sizeof (*zsdp));
7507c478bd9Sstevel@tonic-gate 
751bd41d0a8Snordmark 	/* Now call the shutdown and destroy callback for this key */
752bd41d0a8Snordmark 	zsd_apply_all_zones(zsd_apply_shutdown, key);
753bd41d0a8Snordmark 	zsd_apply_all_zones(zsd_apply_destroy, key);
754bd41d0a8Snordmark 
755bd41d0a8Snordmark 	/* Now we can free up the zsdp structures in each zone */
756bd41d0a8Snordmark 	mutex_enter(&zonehash_lock);
7577c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
758bd41d0a8Snordmark 	    zone = list_next(&zone_active, zone)) {
759bd41d0a8Snordmark 		struct zsd_entry *del;
760bd41d0a8Snordmark 
761bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
762bd41d0a8Snordmark 		del = zsd_find(&zone->zone_zsd, key);
763bd41d0a8Snordmark 		if (del != NULL) {
764bd41d0a8Snordmark 			list_remove(&zone->zone_zsd, del);
765bd41d0a8Snordmark 			ASSERT(!(del->zsd_flags & ZSD_ALL_INPROGRESS));
766bd41d0a8Snordmark 			kmem_free(del, sizeof (*del));
767bd41d0a8Snordmark 		}
7687c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
769bd41d0a8Snordmark 	}
7707c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
771bd41d0a8Snordmark 
772bd41d0a8Snordmark 	return (0);
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate /*
7767c478bd9Sstevel@tonic-gate  * ZSD counterpart of pthread_setspecific().
777bd41d0a8Snordmark  *
778bd41d0a8Snordmark  * Since all zsd callbacks, including those with no create function,
779bd41d0a8Snordmark  * have an entry in zone_zsd, if the key is registered it is part of
780bd41d0a8Snordmark  * the zone_zsd list.
781bd41d0a8Snordmark  * Return an error if the key wasn't registerd.
7827c478bd9Sstevel@tonic-gate  */
7837c478bd9Sstevel@tonic-gate int
7847c478bd9Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
789bd41d0a8Snordmark 	t = zsd_find_mru(&zone->zone_zsd, key);
7907c478bd9Sstevel@tonic-gate 	if (t != NULL) {
7917c478bd9Sstevel@tonic-gate 		/*
7927c478bd9Sstevel@tonic-gate 		 * Replace old value with new
7937c478bd9Sstevel@tonic-gate 		 */
7947c478bd9Sstevel@tonic-gate 		t->zsd_data = (void *)data;
7957c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
7967c478bd9Sstevel@tonic-gate 		return (0);
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
7997c478bd9Sstevel@tonic-gate 	return (-1);
8007c478bd9Sstevel@tonic-gate }
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate /*
8037c478bd9Sstevel@tonic-gate  * ZSD counterpart of pthread_getspecific().
8047c478bd9Sstevel@tonic-gate  */
8057c478bd9Sstevel@tonic-gate void *
8067c478bd9Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone)
8077c478bd9Sstevel@tonic-gate {
8087c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
8097c478bd9Sstevel@tonic-gate 	void *data;
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
812bd41d0a8Snordmark 	t = zsd_find_mru(&zone->zone_zsd, key);
8137c478bd9Sstevel@tonic-gate 	data = (t == NULL ? NULL : t->zsd_data);
8147c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
8157c478bd9Sstevel@tonic-gate 	return (data);
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate /*
8197c478bd9Sstevel@tonic-gate  * Function used to initialize a zone's list of ZSD callbacks and data
8207c478bd9Sstevel@tonic-gate  * when the zone is being created.  The callbacks are initialized from
821bd41d0a8Snordmark  * the template list (zsd_registered_keys). The constructor callback is
822bd41d0a8Snordmark  * executed later (once the zone exists and with locks dropped).
8237c478bd9Sstevel@tonic-gate  */
8247c478bd9Sstevel@tonic-gate static void
8257c478bd9Sstevel@tonic-gate zone_zsd_configure(zone_t *zone)
8267c478bd9Sstevel@tonic-gate {
8277c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsdp;
8287c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
8317c478bd9Sstevel@tonic-gate 	ASSERT(list_head(&zone->zone_zsd) == NULL);
832bd41d0a8Snordmark 	mutex_enter(&zone->zone_lock);
8337c478bd9Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
8347c478bd9Sstevel@tonic-gate 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
8357c478bd9Sstevel@tonic-gate 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
836bd41d0a8Snordmark 		/*
837bd41d0a8Snordmark 		 * Since this zone is ZONE_IS_UNCONFIGURED, zone_key_create
838bd41d0a8Snordmark 		 * should not have added anything to it.
839bd41d0a8Snordmark 		 */
840bd41d0a8Snordmark 		ASSERT(zsd_find(&zone->zone_zsd, zsdp->zsd_key) == NULL);
841bd41d0a8Snordmark 
842bd41d0a8Snordmark 		t = kmem_zalloc(sizeof (*t), KM_SLEEP);
8437c478bd9Sstevel@tonic-gate 		t->zsd_key = zsdp->zsd_key;
8447c478bd9Sstevel@tonic-gate 		t->zsd_create = zsdp->zsd_create;
8457c478bd9Sstevel@tonic-gate 		t->zsd_shutdown = zsdp->zsd_shutdown;
8467c478bd9Sstevel@tonic-gate 		t->zsd_destroy = zsdp->zsd_destroy;
847bd41d0a8Snordmark 		if (zsdp->zsd_create != NULL) {
848bd41d0a8Snordmark 			t->zsd_flags = ZSD_CREATE_NEEDED;
849bd41d0a8Snordmark 			DTRACE_PROBE2(zsd__create__needed,
850bd41d0a8Snordmark 			    zone_t *, zone, zone_key_t, zsdp->zsd_key);
851bd41d0a8Snordmark 		}
8527c478bd9Sstevel@tonic-gate 		list_insert_tail(&zone->zone_zsd, t);
8537c478bd9Sstevel@tonic-gate 	}
8547c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
855bd41d0a8Snordmark 	mutex_exit(&zone->zone_lock);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY };
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate /*
8617c478bd9Sstevel@tonic-gate  * Helper function to execute shutdown or destructor callbacks.
8627c478bd9Sstevel@tonic-gate  */
8637c478bd9Sstevel@tonic-gate static void
8647c478bd9Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct)
8657c478bd9Sstevel@tonic-gate {
8667c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY);
8697c478bd9Sstevel@tonic-gate 	ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY);
8707c478bd9Sstevel@tonic-gate 	ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN);
8717c478bd9Sstevel@tonic-gate 
872bd41d0a8Snordmark 	/*
873bd41d0a8Snordmark 	 * Run the callback solely based on what is registered for the zone
874bd41d0a8Snordmark 	 * in zone_zsd. The global list can change independently of this
875bd41d0a8Snordmark 	 * as keys are registered and unregistered and we don't register new
876bd41d0a8Snordmark 	 * callbacks for a zone that is in the process of going away.
877bd41d0a8Snordmark 	 */
8787c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
879bd41d0a8Snordmark 	for (t = list_head(&zone->zone_zsd); t != NULL;
880bd41d0a8Snordmark 	    t = list_next(&zone->zone_zsd, t)) {
881bd41d0a8Snordmark 		zone_key_t key = t->zsd_key;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 		/* Skip if no callbacks registered */
884bd41d0a8Snordmark 
8857c478bd9Sstevel@tonic-gate 		if (ct == ZSD_SHUTDOWN) {
886bd41d0a8Snordmark 			if (t->zsd_shutdown != NULL &&
887bd41d0a8Snordmark 			    (t->zsd_flags & ZSD_SHUTDOWN_ALL) == 0) {
888bd41d0a8Snordmark 				t->zsd_flags |= ZSD_SHUTDOWN_NEEDED;
889bd41d0a8Snordmark 				DTRACE_PROBE2(zsd__shutdown__needed,
890bd41d0a8Snordmark 				    zone_t *, zone, zone_key_t, key);
8917c478bd9Sstevel@tonic-gate 			}
8927c478bd9Sstevel@tonic-gate 		} else {
893bd41d0a8Snordmark 			if (t->zsd_destroy != NULL &&
894bd41d0a8Snordmark 			    (t->zsd_flags & ZSD_DESTROY_ALL) == 0) {
895bd41d0a8Snordmark 				t->zsd_flags |= ZSD_DESTROY_NEEDED;
896bd41d0a8Snordmark 				DTRACE_PROBE2(zsd__destroy__needed,
897bd41d0a8Snordmark 				    zone_t *, zone, zone_key_t, key);
8987c478bd9Sstevel@tonic-gate 			}
8997c478bd9Sstevel@tonic-gate 		}
9007c478bd9Sstevel@tonic-gate 	}
901bd41d0a8Snordmark 	mutex_exit(&zone->zone_lock);
902bd41d0a8Snordmark 
903bd41d0a8Snordmark 	/* Now call the shutdown and destroy callback for this key */
904bd41d0a8Snordmark 	zsd_apply_all_keys(zsd_apply_shutdown, zone);
905bd41d0a8Snordmark 	zsd_apply_all_keys(zsd_apply_destroy, zone);
906bd41d0a8Snordmark 
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate /*
9107c478bd9Sstevel@tonic-gate  * Called when the zone is going away; free ZSD-related memory, and
9117c478bd9Sstevel@tonic-gate  * destroy the zone_zsd list.
9127c478bd9Sstevel@tonic-gate  */
9137c478bd9Sstevel@tonic-gate static void
9147c478bd9Sstevel@tonic-gate zone_free_zsd(zone_t *zone)
9157c478bd9Sstevel@tonic-gate {
9167c478bd9Sstevel@tonic-gate 	struct zsd_entry *t, *next;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	/*
9197c478bd9Sstevel@tonic-gate 	 * Free all the zsd_entry's we had on this zone.
9207c478bd9Sstevel@tonic-gate 	 */
921bd41d0a8Snordmark 	mutex_enter(&zone->zone_lock);
9227c478bd9Sstevel@tonic-gate 	for (t = list_head(&zone->zone_zsd); t != NULL; t = next) {
9237c478bd9Sstevel@tonic-gate 		next = list_next(&zone->zone_zsd, t);
9247c478bd9Sstevel@tonic-gate 		list_remove(&zone->zone_zsd, t);
925bd41d0a8Snordmark 		ASSERT(!(t->zsd_flags & ZSD_ALL_INPROGRESS));
9267c478bd9Sstevel@tonic-gate 		kmem_free(t, sizeof (*t));
9277c478bd9Sstevel@tonic-gate 	}
9287c478bd9Sstevel@tonic-gate 	list_destroy(&zone->zone_zsd);
929bd41d0a8Snordmark 	mutex_exit(&zone->zone_lock);
930bd41d0a8Snordmark 
931bd41d0a8Snordmark }
932bd41d0a8Snordmark 
933bd41d0a8Snordmark /*
934bd41d0a8Snordmark  * Apply a function to all zones for particular key value.
935bd41d0a8Snordmark  *
936bd41d0a8Snordmark  * The applyfn has to drop zonehash_lock if it does some work, and
937bd41d0a8Snordmark  * then reacquire it before it returns.
938bd41d0a8Snordmark  * When the lock is dropped we don't follow list_next even
939bd41d0a8Snordmark  * if it is possible to do so without any hazards. This is
940bd41d0a8Snordmark  * because we want the design to allow for the list of zones
941bd41d0a8Snordmark  * to change in any arbitrary way during the time the
942bd41d0a8Snordmark  * lock was dropped.
943bd41d0a8Snordmark  *
944bd41d0a8Snordmark  * It is safe to restart the loop at list_head since the applyfn
945bd41d0a8Snordmark  * changes the zsd_flags as it does work, so a subsequent
946bd41d0a8Snordmark  * pass through will have no effect in applyfn, hence the loop will terminate
947bd41d0a8Snordmark  * in at worst O(N^2).
948bd41d0a8Snordmark  */
949bd41d0a8Snordmark static void
950bd41d0a8Snordmark zsd_apply_all_zones(zsd_applyfn_t *applyfn, zone_key_t key)
951bd41d0a8Snordmark {
952bd41d0a8Snordmark 	zone_t *zone;
953bd41d0a8Snordmark 
954bd41d0a8Snordmark 	mutex_enter(&zonehash_lock);
955bd41d0a8Snordmark 	zone = list_head(&zone_active);
956bd41d0a8Snordmark 	while (zone != NULL) {
957bd41d0a8Snordmark 		if ((applyfn)(&zonehash_lock, B_FALSE, zone, key)) {
958bd41d0a8Snordmark 			/* Lock dropped - restart at head */
959bd41d0a8Snordmark 			zone = list_head(&zone_active);
960bd41d0a8Snordmark 		} else {
961bd41d0a8Snordmark 			zone = list_next(&zone_active, zone);
962bd41d0a8Snordmark 		}
963bd41d0a8Snordmark 	}
964bd41d0a8Snordmark 	mutex_exit(&zonehash_lock);
965bd41d0a8Snordmark }
966bd41d0a8Snordmark 
967bd41d0a8Snordmark /*
968bd41d0a8Snordmark  * Apply a function to all keys for a particular zone.
969bd41d0a8Snordmark  *
970bd41d0a8Snordmark  * The applyfn has to drop zonehash_lock if it does some work, and
971bd41d0a8Snordmark  * then reacquire it before it returns.
972bd41d0a8Snordmark  * When the lock is dropped we don't follow list_next even
973bd41d0a8Snordmark  * if it is possible to do so without any hazards. This is
974bd41d0a8Snordmark  * because we want the design to allow for the list of zsd callbacks
975bd41d0a8Snordmark  * to change in any arbitrary way during the time the
976bd41d0a8Snordmark  * lock was dropped.
977bd41d0a8Snordmark  *
978bd41d0a8Snordmark  * It is safe to restart the loop at list_head since the applyfn
979bd41d0a8Snordmark  * changes the zsd_flags as it does work, so a subsequent
980bd41d0a8Snordmark  * pass through will have no effect in applyfn, hence the loop will terminate
981bd41d0a8Snordmark  * in at worst O(N^2).
982bd41d0a8Snordmark  */
983bd41d0a8Snordmark static void
984bd41d0a8Snordmark zsd_apply_all_keys(zsd_applyfn_t *applyfn, zone_t *zone)
985bd41d0a8Snordmark {
986bd41d0a8Snordmark 	struct zsd_entry *t;
987bd41d0a8Snordmark 
988bd41d0a8Snordmark 	mutex_enter(&zone->zone_lock);
989bd41d0a8Snordmark 	t = list_head(&zone->zone_zsd);
990bd41d0a8Snordmark 	while (t != NULL) {
991bd41d0a8Snordmark 		if ((applyfn)(NULL, B_TRUE, zone, t->zsd_key)) {
992bd41d0a8Snordmark 			/* Lock dropped - restart at head */
993bd41d0a8Snordmark 			t = list_head(&zone->zone_zsd);
994bd41d0a8Snordmark 		} else {
995bd41d0a8Snordmark 			t = list_next(&zone->zone_zsd, t);
996bd41d0a8Snordmark 		}
997bd41d0a8Snordmark 	}
998bd41d0a8Snordmark 	mutex_exit(&zone->zone_lock);
999bd41d0a8Snordmark }
1000bd41d0a8Snordmark 
1001bd41d0a8Snordmark /*
1002bd41d0a8Snordmark  * Call the create function for the zone and key if CREATE_NEEDED
1003bd41d0a8Snordmark  * is set.
1004bd41d0a8Snordmark  * If some other thread gets here first and sets CREATE_INPROGRESS, then
1005bd41d0a8Snordmark  * we wait for that thread to complete so that we can ensure that
1006bd41d0a8Snordmark  * all the callbacks are done when we've looped over all zones/keys.
1007bd41d0a8Snordmark  *
1008bd41d0a8Snordmark  * When we call the create function, we drop the global held by the
1009bd41d0a8Snordmark  * caller, and return true to tell the caller it needs to re-evalute the
1010bd41d0a8Snordmark  * state.
1011bd41d0a8Snordmark  * If the caller holds zone_lock then zone_lock_held is set, and zone_lock
1012bd41d0a8Snordmark  * remains held on exit.
1013bd41d0a8Snordmark  */
1014bd41d0a8Snordmark static boolean_t
1015bd41d0a8Snordmark zsd_apply_create(kmutex_t *lockp, boolean_t zone_lock_held,
1016bd41d0a8Snordmark     zone_t *zone, zone_key_t key)
1017bd41d0a8Snordmark {
1018bd41d0a8Snordmark 	void *result;
1019bd41d0a8Snordmark 	struct zsd_entry *t;
1020bd41d0a8Snordmark 	boolean_t dropped;
1021bd41d0a8Snordmark 
1022bd41d0a8Snordmark 	if (lockp != NULL) {
1023bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(lockp));
1024bd41d0a8Snordmark 	}
1025bd41d0a8Snordmark 	if (zone_lock_held) {
1026bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(&zone->zone_lock));
1027bd41d0a8Snordmark 	} else {
1028bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1029bd41d0a8Snordmark 	}
1030bd41d0a8Snordmark 
1031bd41d0a8Snordmark 	t = zsd_find(&zone->zone_zsd, key);
1032bd41d0a8Snordmark 	if (t == NULL) {
1033bd41d0a8Snordmark 		/*
1034bd41d0a8Snordmark 		 * Somebody else got here first e.g the zone going
1035bd41d0a8Snordmark 		 * away.
1036bd41d0a8Snordmark 		 */
1037bd41d0a8Snordmark 		if (!zone_lock_held)
1038bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
1039bd41d0a8Snordmark 		return (B_FALSE);
1040bd41d0a8Snordmark 	}
1041bd41d0a8Snordmark 	dropped = B_FALSE;
1042bd41d0a8Snordmark 	if (zsd_wait_for_inprogress(zone, t, lockp))
1043bd41d0a8Snordmark 		dropped = B_TRUE;
1044bd41d0a8Snordmark 
1045bd41d0a8Snordmark 	if (t->zsd_flags & ZSD_CREATE_NEEDED) {
1046bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_CREATE_NEEDED;
1047bd41d0a8Snordmark 		t->zsd_flags |= ZSD_CREATE_INPROGRESS;
1048bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__create__inprogress,
1049bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1050bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1051bd41d0a8Snordmark 		if (lockp != NULL)
1052bd41d0a8Snordmark 			mutex_exit(lockp);
1053bd41d0a8Snordmark 
1054bd41d0a8Snordmark 		dropped = B_TRUE;
1055bd41d0a8Snordmark 		ASSERT(t->zsd_create != NULL);
1056bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__create__start,
1057bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1058bd41d0a8Snordmark 
1059bd41d0a8Snordmark 		result = (*t->zsd_create)(zone->zone_id);
1060bd41d0a8Snordmark 
1061bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__create__end,
1062bd41d0a8Snordmark 		    zone_t *, zone, voidn *, result);
1063bd41d0a8Snordmark 
1064bd41d0a8Snordmark 		ASSERT(result != NULL);
1065bd41d0a8Snordmark 		if (lockp != NULL)
1066bd41d0a8Snordmark 			mutex_enter(lockp);
1067bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1068bd41d0a8Snordmark 		t->zsd_data = result;
1069bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_CREATE_INPROGRESS;
1070bd41d0a8Snordmark 		t->zsd_flags |= ZSD_CREATE_COMPLETED;
1071bd41d0a8Snordmark 		cv_broadcast(&t->zsd_cv);
1072bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__create__completed,
1073bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1074bd41d0a8Snordmark 	}
1075bd41d0a8Snordmark 	if (!zone_lock_held)
1076bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1077bd41d0a8Snordmark 	return (dropped);
1078bd41d0a8Snordmark }
1079bd41d0a8Snordmark 
1080bd41d0a8Snordmark /*
1081bd41d0a8Snordmark  * Call the shutdown function for the zone and key if SHUTDOWN_NEEDED
1082bd41d0a8Snordmark  * is set.
1083bd41d0a8Snordmark  * If some other thread gets here first and sets *_INPROGRESS, then
1084bd41d0a8Snordmark  * we wait for that thread to complete so that we can ensure that
1085bd41d0a8Snordmark  * all the callbacks are done when we've looped over all zones/keys.
1086bd41d0a8Snordmark  *
1087bd41d0a8Snordmark  * When we call the shutdown function, we drop the global held by the
1088bd41d0a8Snordmark  * caller, and return true to tell the caller it needs to re-evalute the
1089bd41d0a8Snordmark  * state.
1090bd41d0a8Snordmark  * If the caller holds zone_lock then zone_lock_held is set, and zone_lock
1091bd41d0a8Snordmark  * remains held on exit.
1092bd41d0a8Snordmark  */
1093bd41d0a8Snordmark static boolean_t
1094bd41d0a8Snordmark zsd_apply_shutdown(kmutex_t *lockp, boolean_t zone_lock_held,
1095bd41d0a8Snordmark     zone_t *zone, zone_key_t key)
1096bd41d0a8Snordmark {
1097bd41d0a8Snordmark 	struct zsd_entry *t;
1098bd41d0a8Snordmark 	void *data;
1099bd41d0a8Snordmark 	boolean_t dropped;
1100bd41d0a8Snordmark 
1101bd41d0a8Snordmark 	if (lockp != NULL) {
1102bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(lockp));
1103bd41d0a8Snordmark 	}
1104bd41d0a8Snordmark 	if (zone_lock_held) {
1105bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(&zone->zone_lock));
1106bd41d0a8Snordmark 	} else {
1107bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1108bd41d0a8Snordmark 	}
1109bd41d0a8Snordmark 
1110bd41d0a8Snordmark 	t = zsd_find(&zone->zone_zsd, key);
1111bd41d0a8Snordmark 	if (t == NULL) {
1112bd41d0a8Snordmark 		/*
1113bd41d0a8Snordmark 		 * Somebody else got here first e.g the zone going
1114bd41d0a8Snordmark 		 * away.
1115bd41d0a8Snordmark 		 */
1116bd41d0a8Snordmark 		if (!zone_lock_held)
1117bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
1118bd41d0a8Snordmark 		return (B_FALSE);
1119bd41d0a8Snordmark 	}
1120bd41d0a8Snordmark 	dropped = B_FALSE;
1121bd41d0a8Snordmark 	if (zsd_wait_for_creator(zone, t, lockp))
1122bd41d0a8Snordmark 		dropped = B_TRUE;
1123bd41d0a8Snordmark 
1124bd41d0a8Snordmark 	if (zsd_wait_for_inprogress(zone, t, lockp))
1125bd41d0a8Snordmark 		dropped = B_TRUE;
1126bd41d0a8Snordmark 
1127bd41d0a8Snordmark 	if (t->zsd_flags & ZSD_SHUTDOWN_NEEDED) {
1128bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_SHUTDOWN_NEEDED;
1129bd41d0a8Snordmark 		t->zsd_flags |= ZSD_SHUTDOWN_INPROGRESS;
1130bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__shutdown__inprogress,
1131bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1132bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1133bd41d0a8Snordmark 		if (lockp != NULL)
1134bd41d0a8Snordmark 			mutex_exit(lockp);
1135bd41d0a8Snordmark 		dropped = B_TRUE;
1136bd41d0a8Snordmark 
1137bd41d0a8Snordmark 		ASSERT(t->zsd_shutdown != NULL);
1138bd41d0a8Snordmark 		data = t->zsd_data;
1139bd41d0a8Snordmark 
1140bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__shutdown__start,
1141bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1142bd41d0a8Snordmark 
1143bd41d0a8Snordmark 		(t->zsd_shutdown)(zone->zone_id, data);
1144bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__shutdown__end,
1145bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1146bd41d0a8Snordmark 
1147bd41d0a8Snordmark 		if (lockp != NULL)
1148bd41d0a8Snordmark 			mutex_enter(lockp);
1149bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1150bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_SHUTDOWN_INPROGRESS;
1151bd41d0a8Snordmark 		t->zsd_flags |= ZSD_SHUTDOWN_COMPLETED;
1152bd41d0a8Snordmark 		cv_broadcast(&t->zsd_cv);
1153bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__shutdown__completed,
1154bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1155bd41d0a8Snordmark 	}
1156bd41d0a8Snordmark 	if (!zone_lock_held)
1157bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1158bd41d0a8Snordmark 	return (dropped);
1159bd41d0a8Snordmark }
1160bd41d0a8Snordmark 
1161bd41d0a8Snordmark /*
1162bd41d0a8Snordmark  * Call the destroy function for the zone and key if DESTROY_NEEDED
1163bd41d0a8Snordmark  * is set.
1164bd41d0a8Snordmark  * If some other thread gets here first and sets *_INPROGRESS, then
1165bd41d0a8Snordmark  * we wait for that thread to complete so that we can ensure that
1166bd41d0a8Snordmark  * all the callbacks are done when we've looped over all zones/keys.
1167bd41d0a8Snordmark  *
1168bd41d0a8Snordmark  * When we call the destroy function, we drop the global held by the
1169bd41d0a8Snordmark  * caller, and return true to tell the caller it needs to re-evalute the
1170bd41d0a8Snordmark  * state.
1171bd41d0a8Snordmark  * If the caller holds zone_lock then zone_lock_held is set, and zone_lock
1172bd41d0a8Snordmark  * remains held on exit.
1173bd41d0a8Snordmark  */
1174bd41d0a8Snordmark static boolean_t
1175bd41d0a8Snordmark zsd_apply_destroy(kmutex_t *lockp, boolean_t zone_lock_held,
1176bd41d0a8Snordmark     zone_t *zone, zone_key_t key)
1177bd41d0a8Snordmark {
1178bd41d0a8Snordmark 	struct zsd_entry *t;
1179bd41d0a8Snordmark 	void *data;
1180bd41d0a8Snordmark 	boolean_t dropped;
1181bd41d0a8Snordmark 
1182bd41d0a8Snordmark 	if (lockp != NULL) {
1183bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(lockp));
1184bd41d0a8Snordmark 	}
1185bd41d0a8Snordmark 	if (zone_lock_held) {
1186bd41d0a8Snordmark 		ASSERT(MUTEX_HELD(&zone->zone_lock));
1187bd41d0a8Snordmark 	} else {
1188bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1189bd41d0a8Snordmark 	}
1190bd41d0a8Snordmark 
1191bd41d0a8Snordmark 	t = zsd_find(&zone->zone_zsd, key);
1192bd41d0a8Snordmark 	if (t == NULL) {
1193bd41d0a8Snordmark 		/*
1194bd41d0a8Snordmark 		 * Somebody else got here first e.g the zone going
1195bd41d0a8Snordmark 		 * away.
1196bd41d0a8Snordmark 		 */
1197bd41d0a8Snordmark 		if (!zone_lock_held)
1198bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
1199bd41d0a8Snordmark 		return (B_FALSE);
1200bd41d0a8Snordmark 	}
1201bd41d0a8Snordmark 	dropped = B_FALSE;
1202bd41d0a8Snordmark 	if (zsd_wait_for_creator(zone, t, lockp))
1203bd41d0a8Snordmark 		dropped = B_TRUE;
1204bd41d0a8Snordmark 
1205bd41d0a8Snordmark 	if (zsd_wait_for_inprogress(zone, t, lockp))
1206bd41d0a8Snordmark 		dropped = B_TRUE;
1207bd41d0a8Snordmark 
1208bd41d0a8Snordmark 	if (t->zsd_flags & ZSD_DESTROY_NEEDED) {
1209bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_DESTROY_NEEDED;
1210bd41d0a8Snordmark 		t->zsd_flags |= ZSD_DESTROY_INPROGRESS;
1211bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__destroy__inprogress,
1212bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1213bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1214bd41d0a8Snordmark 		if (lockp != NULL)
1215bd41d0a8Snordmark 			mutex_exit(lockp);
1216bd41d0a8Snordmark 		dropped = B_TRUE;
1217bd41d0a8Snordmark 
1218bd41d0a8Snordmark 		ASSERT(t->zsd_destroy != NULL);
1219bd41d0a8Snordmark 		data = t->zsd_data;
1220bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__destroy__start,
1221bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1222bd41d0a8Snordmark 
1223bd41d0a8Snordmark 		(t->zsd_destroy)(zone->zone_id, data);
1224bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__destroy__end,
1225bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1226bd41d0a8Snordmark 
1227bd41d0a8Snordmark 		if (lockp != NULL)
1228bd41d0a8Snordmark 			mutex_enter(lockp);
1229bd41d0a8Snordmark 		mutex_enter(&zone->zone_lock);
1230bd41d0a8Snordmark 		t->zsd_data = NULL;
1231bd41d0a8Snordmark 		t->zsd_flags &= ~ZSD_DESTROY_INPROGRESS;
1232bd41d0a8Snordmark 		t->zsd_flags |= ZSD_DESTROY_COMPLETED;
1233bd41d0a8Snordmark 		cv_broadcast(&t->zsd_cv);
1234bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__destroy__completed,
1235bd41d0a8Snordmark 		    zone_t *, zone, zone_key_t, key);
1236bd41d0a8Snordmark 	}
1237bd41d0a8Snordmark 	if (!zone_lock_held)
1238bd41d0a8Snordmark 		mutex_exit(&zone->zone_lock);
1239bd41d0a8Snordmark 	return (dropped);
1240bd41d0a8Snordmark }
1241bd41d0a8Snordmark 
1242bd41d0a8Snordmark /*
1243bd41d0a8Snordmark  * Wait for any CREATE_NEEDED flag to be cleared.
1244bd41d0a8Snordmark  * Returns true if lockp was temporarily dropped while waiting.
1245bd41d0a8Snordmark  */
1246bd41d0a8Snordmark static boolean_t
1247bd41d0a8Snordmark zsd_wait_for_creator(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp)
1248bd41d0a8Snordmark {
1249bd41d0a8Snordmark 	boolean_t dropped = B_FALSE;
1250bd41d0a8Snordmark 
1251bd41d0a8Snordmark 	while (t->zsd_flags & ZSD_CREATE_NEEDED) {
1252bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__wait__for__creator,
1253bd41d0a8Snordmark 		    zone_t *, zone, struct zsd_entry *, t);
1254bd41d0a8Snordmark 		if (lockp != NULL) {
1255bd41d0a8Snordmark 			dropped = B_TRUE;
1256bd41d0a8Snordmark 			mutex_exit(lockp);
1257bd41d0a8Snordmark 		}
1258bd41d0a8Snordmark 		cv_wait(&t->zsd_cv, &zone->zone_lock);
1259bd41d0a8Snordmark 		if (lockp != NULL) {
1260bd41d0a8Snordmark 			/* First drop zone_lock to preserve order */
1261bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
1262bd41d0a8Snordmark 			mutex_enter(lockp);
1263bd41d0a8Snordmark 			mutex_enter(&zone->zone_lock);
1264bd41d0a8Snordmark 		}
1265bd41d0a8Snordmark 	}
1266bd41d0a8Snordmark 	return (dropped);
1267bd41d0a8Snordmark }
1268bd41d0a8Snordmark 
1269bd41d0a8Snordmark /*
1270bd41d0a8Snordmark  * Wait for any INPROGRESS flag to be cleared.
1271bd41d0a8Snordmark  * Returns true if lockp was temporarily dropped while waiting.
1272bd41d0a8Snordmark  */
1273bd41d0a8Snordmark static boolean_t
1274bd41d0a8Snordmark zsd_wait_for_inprogress(zone_t *zone, struct zsd_entry *t, kmutex_t *lockp)
1275bd41d0a8Snordmark {
1276bd41d0a8Snordmark 	boolean_t dropped = B_FALSE;
1277bd41d0a8Snordmark 
1278bd41d0a8Snordmark 	while (t->zsd_flags & ZSD_ALL_INPROGRESS) {
1279bd41d0a8Snordmark 		DTRACE_PROBE2(zsd__wait__for__inprogress,
1280bd41d0a8Snordmark 		    zone_t *, zone, struct zsd_entry *, t);
1281bd41d0a8Snordmark 		if (lockp != NULL) {
1282bd41d0a8Snordmark 			dropped = B_TRUE;
1283bd41d0a8Snordmark 			mutex_exit(lockp);
1284bd41d0a8Snordmark 		}
1285bd41d0a8Snordmark 		cv_wait(&t->zsd_cv, &zone->zone_lock);
1286bd41d0a8Snordmark 		if (lockp != NULL) {
1287bd41d0a8Snordmark 			/* First drop zone_lock to preserve order */
1288bd41d0a8Snordmark 			mutex_exit(&zone->zone_lock);
1289bd41d0a8Snordmark 			mutex_enter(lockp);
1290bd41d0a8Snordmark 			mutex_enter(&zone->zone_lock);
1291bd41d0a8Snordmark 		}
1292bd41d0a8Snordmark 	}
1293bd41d0a8Snordmark 	return (dropped);
12947c478bd9Sstevel@tonic-gate }
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate /*
1297fa9e4066Sahrens  * Frees memory associated with the zone dataset list.
1298fa9e4066Sahrens  */
1299fa9e4066Sahrens static void
1300fa9e4066Sahrens zone_free_datasets(zone_t *zone)
1301fa9e4066Sahrens {
1302fa9e4066Sahrens 	zone_dataset_t *t, *next;
1303fa9e4066Sahrens 
1304fa9e4066Sahrens 	for (t = list_head(&zone->zone_datasets); t != NULL; t = next) {
1305fa9e4066Sahrens 		next = list_next(&zone->zone_datasets, t);
1306fa9e4066Sahrens 		list_remove(&zone->zone_datasets, t);
1307fa9e4066Sahrens 		kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1);
1308fa9e4066Sahrens 		kmem_free(t, sizeof (*t));
1309fa9e4066Sahrens 	}
1310fa9e4066Sahrens 	list_destroy(&zone->zone_datasets);
1311fa9e4066Sahrens }
1312fa9e4066Sahrens 
1313fa9e4066Sahrens /*
13147c478bd9Sstevel@tonic-gate  * zone.cpu-shares resource control support.
13157c478bd9Sstevel@tonic-gate  */
13167c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13177c478bd9Sstevel@tonic-gate static rctl_qty_t
13187c478bd9Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p)
13197c478bd9Sstevel@tonic-gate {
13207c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
13217c478bd9Sstevel@tonic-gate 	return (p->p_zone->zone_shares);
13227c478bd9Sstevel@tonic-gate }
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13257c478bd9Sstevel@tonic-gate static int
13267c478bd9Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
13277c478bd9Sstevel@tonic-gate     rctl_qty_t nv)
13287c478bd9Sstevel@tonic-gate {
13297c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
13307c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
13317c478bd9Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
13327c478bd9Sstevel@tonic-gate 		return (0);
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 	e->rcep_p.zone->zone_shares = nv;
13357c478bd9Sstevel@tonic-gate 	return (0);
13367c478bd9Sstevel@tonic-gate }
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = {
13397c478bd9Sstevel@tonic-gate 	rcop_no_action,
13407c478bd9Sstevel@tonic-gate 	zone_cpu_shares_usage,
13417c478bd9Sstevel@tonic-gate 	zone_cpu_shares_set,
13427c478bd9Sstevel@tonic-gate 	rcop_no_test
13437c478bd9Sstevel@tonic-gate };
13447c478bd9Sstevel@tonic-gate 
1345c97ad5cdSakolb /*
1346c97ad5cdSakolb  * zone.cpu-cap resource control support.
1347c97ad5cdSakolb  */
1348c97ad5cdSakolb /*ARGSUSED*/
1349c97ad5cdSakolb static rctl_qty_t
1350c97ad5cdSakolb zone_cpu_cap_get(rctl_t *rctl, struct proc *p)
1351c97ad5cdSakolb {
1352c97ad5cdSakolb 	ASSERT(MUTEX_HELD(&p->p_lock));
1353c97ad5cdSakolb 	return (cpucaps_zone_get(p->p_zone));
1354c97ad5cdSakolb }
1355c97ad5cdSakolb 
1356c97ad5cdSakolb /*ARGSUSED*/
1357c97ad5cdSakolb static int
1358c97ad5cdSakolb zone_cpu_cap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
1359c97ad5cdSakolb     rctl_qty_t nv)
1360c97ad5cdSakolb {
1361c97ad5cdSakolb 	zone_t *zone = e->rcep_p.zone;
1362c97ad5cdSakolb 
1363c97ad5cdSakolb 	ASSERT(MUTEX_HELD(&p->p_lock));
1364c97ad5cdSakolb 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1365c97ad5cdSakolb 
1366c97ad5cdSakolb 	if (zone == NULL)
1367c97ad5cdSakolb 		return (0);
1368c97ad5cdSakolb 
1369c97ad5cdSakolb 	/*
1370c97ad5cdSakolb 	 * set cap to the new value.
1371c97ad5cdSakolb 	 */
1372c97ad5cdSakolb 	return (cpucaps_zone_set(zone, nv));
1373c97ad5cdSakolb }
1374c97ad5cdSakolb 
1375c97ad5cdSakolb static rctl_ops_t zone_cpu_cap_ops = {
1376c97ad5cdSakolb 	rcop_no_action,
1377c97ad5cdSakolb 	zone_cpu_cap_get,
1378c97ad5cdSakolb 	zone_cpu_cap_set,
1379c97ad5cdSakolb 	rcop_no_test
1380c97ad5cdSakolb };
1381c97ad5cdSakolb 
13827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13837c478bd9Sstevel@tonic-gate static rctl_qty_t
13847c478bd9Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p)
13857c478bd9Sstevel@tonic-gate {
13867c478bd9Sstevel@tonic-gate 	rctl_qty_t nlwps;
13877c478bd9Sstevel@tonic-gate 	zone_t *zone = p->p_zone;
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
13927c478bd9Sstevel@tonic-gate 	nlwps = zone->zone_nlwps;
13937c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	return (nlwps);
13967c478bd9Sstevel@tonic-gate }
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13997c478bd9Sstevel@tonic-gate static int
14007c478bd9Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
14017c478bd9Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
14027c478bd9Sstevel@tonic-gate {
14037c478bd9Sstevel@tonic-gate 	rctl_qty_t nlwps;
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
14067c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
14077c478bd9Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
14087c478bd9Sstevel@tonic-gate 		return (0);
14097c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock)));
14107c478bd9Sstevel@tonic-gate 	nlwps = e->rcep_p.zone->zone_nlwps;
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	if (nlwps + incr > rcntl->rcv_value)
14137c478bd9Sstevel@tonic-gate 		return (1);
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	return (0);
14167c478bd9Sstevel@tonic-gate }
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14197c478bd9Sstevel@tonic-gate static int
1420c6939658Ssl108498 zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv)
1421c6939658Ssl108498 {
14227c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
14237c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
14247c478bd9Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
14257c478bd9Sstevel@tonic-gate 		return (0);
14267c478bd9Sstevel@tonic-gate 	e->rcep_p.zone->zone_nlwps_ctl = nv;
14277c478bd9Sstevel@tonic-gate 	return (0);
14287c478bd9Sstevel@tonic-gate }
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = {
14317c478bd9Sstevel@tonic-gate 	rcop_no_action,
14327c478bd9Sstevel@tonic-gate 	zone_lwps_usage,
14337c478bd9Sstevel@tonic-gate 	zone_lwps_set,
14347c478bd9Sstevel@tonic-gate 	zone_lwps_test,
14357c478bd9Sstevel@tonic-gate };
14367c478bd9Sstevel@tonic-gate 
1437824c205fSml93401 /*ARGSUSED*/
1438ff19e029SMenno Lageman static rctl_qty_t
1439ff19e029SMenno Lageman zone_procs_usage(rctl_t *r, proc_t *p)
1440ff19e029SMenno Lageman {
1441ff19e029SMenno Lageman 	rctl_qty_t nprocs;
1442ff19e029SMenno Lageman 	zone_t *zone = p->p_zone;
1443ff19e029SMenno Lageman 
1444ff19e029SMenno Lageman 	ASSERT(MUTEX_HELD(&p->p_lock));
1445ff19e029SMenno Lageman 
1446ff19e029SMenno Lageman 	mutex_enter(&zone->zone_nlwps_lock);
1447ff19e029SMenno Lageman 	nprocs = zone->zone_nprocs;
1448ff19e029SMenno Lageman 	mutex_exit(&zone->zone_nlwps_lock);
1449ff19e029SMenno Lageman 
1450ff19e029SMenno Lageman 	return (nprocs);
1451ff19e029SMenno Lageman }
1452ff19e029SMenno Lageman 
1453ff19e029SMenno Lageman /*ARGSUSED*/
1454ff19e029SMenno Lageman static int
1455ff19e029SMenno Lageman zone_procs_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
1456ff19e029SMenno Lageman     rctl_qty_t incr, uint_t flags)
1457ff19e029SMenno Lageman {
1458ff19e029SMenno Lageman 	rctl_qty_t nprocs;
1459ff19e029SMenno Lageman 
1460ff19e029SMenno Lageman 	ASSERT(MUTEX_HELD(&p->p_lock));
1461ff19e029SMenno Lageman 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1462ff19e029SMenno Lageman 	if (e->rcep_p.zone == NULL)
1463ff19e029SMenno Lageman 		return (0);
1464ff19e029SMenno Lageman 	ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock)));
1465ff19e029SMenno Lageman 	nprocs = e->rcep_p.zone->zone_nprocs;
1466ff19e029SMenno Lageman 
1467ff19e029SMenno Lageman 	if (nprocs + incr > rcntl->rcv_value)
1468ff19e029SMenno Lageman 		return (1);
1469ff19e029SMenno Lageman 
1470ff19e029SMenno Lageman 	return (0);
1471ff19e029SMenno Lageman }
1472ff19e029SMenno Lageman 
1473ff19e029SMenno Lageman /*ARGSUSED*/
1474ff19e029SMenno Lageman static int
1475ff19e029SMenno Lageman zone_procs_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv)
1476ff19e029SMenno Lageman {
1477ff19e029SMenno Lageman 	ASSERT(MUTEX_HELD(&p->p_lock));
1478ff19e029SMenno Lageman 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1479ff19e029SMenno Lageman 	if (e->rcep_p.zone == NULL)
1480ff19e029SMenno Lageman 		return (0);
1481ff19e029SMenno Lageman 	e->rcep_p.zone->zone_nprocs_ctl = nv;
1482ff19e029SMenno Lageman 	return (0);
1483ff19e029SMenno Lageman }
1484ff19e029SMenno Lageman 
1485ff19e029SMenno Lageman static rctl_ops_t zone_procs_ops = {
1486ff19e029SMenno Lageman 	rcop_no_action,
1487ff19e029SMenno Lageman 	zone_procs_usage,
1488ff19e029SMenno Lageman 	zone_procs_set,
1489ff19e029SMenno Lageman 	zone_procs_test,
1490ff19e029SMenno Lageman };
1491ff19e029SMenno Lageman 
1492ff19e029SMenno Lageman /*ARGSUSED*/
1493824c205fSml93401 static int
1494824c205fSml93401 zone_shmmax_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
1495824c205fSml93401     rctl_qty_t incr, uint_t flags)
1496824c205fSml93401 {
1497824c205fSml93401 	rctl_qty_t v;
1498824c205fSml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
1499824c205fSml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1500824c205fSml93401 	v = e->rcep_p.zone->zone_shmmax + incr;
1501824c205fSml93401 	if (v > rval->rcv_value)
1502824c205fSml93401 		return (1);
1503824c205fSml93401 	return (0);
1504824c205fSml93401 }
1505824c205fSml93401 
1506824c205fSml93401 static rctl_ops_t zone_shmmax_ops = {
1507824c205fSml93401 	rcop_no_action,
1508824c205fSml93401 	rcop_no_usage,
1509824c205fSml93401 	rcop_no_set,
1510824c205fSml93401 	zone_shmmax_test
1511824c205fSml93401 };
1512824c205fSml93401 
1513824c205fSml93401 /*ARGSUSED*/
1514824c205fSml93401 static int
1515824c205fSml93401 zone_shmmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
1516824c205fSml93401     rctl_qty_t incr, uint_t flags)
1517824c205fSml93401 {
1518824c205fSml93401 	rctl_qty_t v;
1519824c205fSml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
1520824c205fSml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1521824c205fSml93401 	v = e->rcep_p.zone->zone_ipc.ipcq_shmmni + incr;
1522824c205fSml93401 	if (v > rval->rcv_value)
1523824c205fSml93401 		return (1);
1524824c205fSml93401 	return (0);
1525824c205fSml93401 }
1526824c205fSml93401 
1527824c205fSml93401 static rctl_ops_t zone_shmmni_ops = {
1528824c205fSml93401 	rcop_no_action,
1529824c205fSml93401 	rcop_no_usage,
1530824c205fSml93401 	rcop_no_set,
1531824c205fSml93401 	zone_shmmni_test
1532824c205fSml93401 };
1533824c205fSml93401 
1534824c205fSml93401 /*ARGSUSED*/
1535824c205fSml93401 static int
1536824c205fSml93401 zone_semmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
1537824c205fSml93401     rctl_qty_t incr, uint_t flags)
1538824c205fSml93401 {
1539824c205fSml93401 	rctl_qty_t v;
1540824c205fSml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
1541824c205fSml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1542824c205fSml93401 	v = e->rcep_p.zone->zone_ipc.ipcq_semmni + incr;
1543824c205fSml93401 	if (v > rval->rcv_value)
1544824c205fSml93401 		return (1);
1545824c205fSml93401 	return (0);
1546824c205fSml93401 }
1547824c205fSml93401 
1548824c205fSml93401 static rctl_ops_t zone_semmni_ops = {
1549824c205fSml93401 	rcop_no_action,
1550824c205fSml93401 	rcop_no_usage,
1551824c205fSml93401 	rcop_no_set,
1552824c205fSml93401 	zone_semmni_test
1553824c205fSml93401 };
1554824c205fSml93401 
1555824c205fSml93401 /*ARGSUSED*/
1556824c205fSml93401 static int
1557824c205fSml93401 zone_msgmni_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rval,
1558824c205fSml93401     rctl_qty_t incr, uint_t flags)
1559824c205fSml93401 {
1560824c205fSml93401 	rctl_qty_t v;
1561824c205fSml93401 	ASSERT(MUTEX_HELD(&p->p_lock));
1562824c205fSml93401 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1563824c205fSml93401 	v = e->rcep_p.zone->zone_ipc.ipcq_msgmni + incr;
1564824c205fSml93401 	if (v > rval->rcv_value)
1565824c205fSml93401 		return (1);
1566824c205fSml93401 	return (0);
1567824c205fSml93401 }
1568824c205fSml93401 
1569824c205fSml93401 static rctl_ops_t zone_msgmni_ops = {
1570824c205fSml93401 	rcop_no_action,
1571824c205fSml93401 	rcop_no_usage,
1572824c205fSml93401 	rcop_no_set,
1573824c205fSml93401 	zone_msgmni_test
1574824c205fSml93401 };
1575824c205fSml93401 
1576c6939658Ssl108498 /*ARGSUSED*/
1577c6939658Ssl108498 static rctl_qty_t
1578c6939658Ssl108498 zone_locked_mem_usage(rctl_t *rctl, struct proc *p)
1579c6939658Ssl108498 {
1580c6939658Ssl108498 	rctl_qty_t q;
1581c6939658Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
15820209230bSgjelinek 	mutex_enter(&p->p_zone->zone_mem_lock);
1583c6939658Ssl108498 	q = p->p_zone->zone_locked_mem;
15840209230bSgjelinek 	mutex_exit(&p->p_zone->zone_mem_lock);
1585c6939658Ssl108498 	return (q);
1586c6939658Ssl108498 }
1587c6939658Ssl108498 
1588c6939658Ssl108498 /*ARGSUSED*/
1589c6939658Ssl108498 static int
1590c6939658Ssl108498 zone_locked_mem_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
1591c6939658Ssl108498     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
1592c6939658Ssl108498 {
1593c6939658Ssl108498 	rctl_qty_t q;
15940209230bSgjelinek 	zone_t *z;
15950209230bSgjelinek 
15960209230bSgjelinek 	z = e->rcep_p.zone;
1597c6939658Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
15980209230bSgjelinek 	ASSERT(MUTEX_HELD(&z->zone_mem_lock));
15990209230bSgjelinek 	q = z->zone_locked_mem;
1600c6939658Ssl108498 	if (q + incr > rcntl->rcv_value)
1601c6939658Ssl108498 		return (1);
1602c6939658Ssl108498 	return (0);
1603c6939658Ssl108498 }
1604c6939658Ssl108498 
1605c6939658Ssl108498 /*ARGSUSED*/
1606c6939658Ssl108498 static int
1607c6939658Ssl108498 zone_locked_mem_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
1608c6939658Ssl108498     rctl_qty_t nv)
1609c6939658Ssl108498 {
1610c6939658Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
1611c6939658Ssl108498 	ASSERT(e->rcep_t == RCENTITY_ZONE);
1612c6939658Ssl108498 	if (e->rcep_p.zone == NULL)
1613c6939658Ssl108498 		return (0);
1614c6939658Ssl108498 	e->rcep_p.zone->zone_locked_mem_ctl = nv;
1615c6939658Ssl108498 	return (0);
1616c6939658Ssl108498 }
1617c6939658Ssl108498 
1618c6939658Ssl108498 static rctl_ops_t zone_locked_mem_ops = {
1619c6939658Ssl108498 	rcop_no_action,
1620c6939658Ssl108498 	zone_locked_mem_usage,
1621c6939658Ssl108498 	zone_locked_mem_set,
1622c6939658Ssl108498 	zone_locked_mem_test
1623c6939658Ssl108498 };
1624824c205fSml93401 
16250209230bSgjelinek /*ARGSUSED*/
16260209230bSgjelinek static rctl_qty_t
16270209230bSgjelinek zone_max_swap_usage(rctl_t *rctl, struct proc *p)
16280209230bSgjelinek {
16290209230bSgjelinek 	rctl_qty_t q;
16300209230bSgjelinek 	zone_t *z = p->p_zone;
16310209230bSgjelinek 
16320209230bSgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
16330209230bSgjelinek 	mutex_enter(&z->zone_mem_lock);
16340209230bSgjelinek 	q = z->zone_max_swap;
16350209230bSgjelinek 	mutex_exit(&z->zone_mem_lock);
16360209230bSgjelinek 	return (q);
16370209230bSgjelinek }
16380209230bSgjelinek 
16390209230bSgjelinek /*ARGSUSED*/
16400209230bSgjelinek static int
16410209230bSgjelinek zone_max_swap_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
16420209230bSgjelinek     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
16430209230bSgjelinek {
16440209230bSgjelinek 	rctl_qty_t q;
16450209230bSgjelinek 	zone_t *z;
16460209230bSgjelinek 
16470209230bSgjelinek 	z = e->rcep_p.zone;
16480209230bSgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
16490209230bSgjelinek 	ASSERT(MUTEX_HELD(&z->zone_mem_lock));
16500209230bSgjelinek 	q = z->zone_max_swap;
16510209230bSgjelinek 	if (q + incr > rcntl->rcv_value)
16520209230bSgjelinek 		return (1);
16530209230bSgjelinek 	return (0);
16540209230bSgjelinek }
16550209230bSgjelinek 
16560209230bSgjelinek /*ARGSUSED*/
16570209230bSgjelinek static int
16580209230bSgjelinek zone_max_swap_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
16590209230bSgjelinek     rctl_qty_t nv)
16600209230bSgjelinek {
16610209230bSgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
16620209230bSgjelinek 	ASSERT(e->rcep_t == RCENTITY_ZONE);
16630209230bSgjelinek 	if (e->rcep_p.zone == NULL)
16640209230bSgjelinek 		return (0);
16650209230bSgjelinek 	e->rcep_p.zone->zone_max_swap_ctl = nv;
16660209230bSgjelinek 	return (0);
16670209230bSgjelinek }
16680209230bSgjelinek 
16690209230bSgjelinek static rctl_ops_t zone_max_swap_ops = {
16700209230bSgjelinek 	rcop_no_action,
16710209230bSgjelinek 	zone_max_swap_usage,
16720209230bSgjelinek 	zone_max_swap_set,
16730209230bSgjelinek 	zone_max_swap_test
16740209230bSgjelinek };
16750209230bSgjelinek 
16760fbb751dSJohn Levon /*ARGSUSED*/
16770fbb751dSJohn Levon static rctl_qty_t
16780fbb751dSJohn Levon zone_max_lofi_usage(rctl_t *rctl, struct proc *p)
16790fbb751dSJohn Levon {
16800fbb751dSJohn Levon 	rctl_qty_t q;
16810fbb751dSJohn Levon 	zone_t *z = p->p_zone;
16820fbb751dSJohn Levon 
16830fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&p->p_lock));
16840fbb751dSJohn Levon 	mutex_enter(&z->zone_rctl_lock);
16850fbb751dSJohn Levon 	q = z->zone_max_lofi;
16860fbb751dSJohn Levon 	mutex_exit(&z->zone_rctl_lock);
16870fbb751dSJohn Levon 	return (q);
16880fbb751dSJohn Levon }
16890fbb751dSJohn Levon 
16900fbb751dSJohn Levon /*ARGSUSED*/
16910fbb751dSJohn Levon static int
16920fbb751dSJohn Levon zone_max_lofi_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
16930fbb751dSJohn Levon     rctl_val_t *rcntl, rctl_qty_t incr, uint_t flags)
16940fbb751dSJohn Levon {
16950fbb751dSJohn Levon 	rctl_qty_t q;
16960fbb751dSJohn Levon 	zone_t *z;
16970fbb751dSJohn Levon 
16980fbb751dSJohn Levon 	z = e->rcep_p.zone;
16990fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&p->p_lock));
17000fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&z->zone_rctl_lock));
17010fbb751dSJohn Levon 	q = z->zone_max_lofi;
17020fbb751dSJohn Levon 	if (q + incr > rcntl->rcv_value)
17030fbb751dSJohn Levon 		return (1);
17040fbb751dSJohn Levon 	return (0);
17050fbb751dSJohn Levon }
17060fbb751dSJohn Levon 
17070fbb751dSJohn Levon /*ARGSUSED*/
17080fbb751dSJohn Levon static int
17090fbb751dSJohn Levon zone_max_lofi_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
17100fbb751dSJohn Levon     rctl_qty_t nv)
17110fbb751dSJohn Levon {
17120fbb751dSJohn Levon 	ASSERT(MUTEX_HELD(&p->p_lock));
17130fbb751dSJohn Levon 	ASSERT(e->rcep_t == RCENTITY_ZONE);
17140fbb751dSJohn Levon 	if (e->rcep_p.zone == NULL)
17150fbb751dSJohn Levon 		return (0);
17160fbb751dSJohn Levon 	e->rcep_p.zone->zone_max_lofi_ctl = nv;
17170fbb751dSJohn Levon 	return (0);
17180fbb751dSJohn Levon }
17190fbb751dSJohn Levon 
17200fbb751dSJohn Levon static rctl_ops_t zone_max_lofi_ops = {
17210fbb751dSJohn Levon 	rcop_no_action,
17220fbb751dSJohn Levon 	zone_max_lofi_usage,
17230fbb751dSJohn Levon 	zone_max_lofi_set,
17240fbb751dSJohn Levon 	zone_max_lofi_test
17250fbb751dSJohn Levon };
17260fbb751dSJohn Levon 
17277c478bd9Sstevel@tonic-gate /*
17287c478bd9Sstevel@tonic-gate  * Helper function to brand the zone with a unique ID.
17297c478bd9Sstevel@tonic-gate  */
17307c478bd9Sstevel@tonic-gate static void
17317c478bd9Sstevel@tonic-gate zone_uniqid(zone_t *zone)
17327c478bd9Sstevel@tonic-gate {
17337c478bd9Sstevel@tonic-gate 	static uint64_t uniqid = 0;
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
17367c478bd9Sstevel@tonic-gate 	zone->zone_uniqid = uniqid++;
17377c478bd9Sstevel@tonic-gate }
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate /*
17407c478bd9Sstevel@tonic-gate  * Returns a held pointer to the "kcred" for the specified zone.
17417c478bd9Sstevel@tonic-gate  */
17427c478bd9Sstevel@tonic-gate struct cred *
17437c478bd9Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid)
17447c478bd9Sstevel@tonic-gate {
17457c478bd9Sstevel@tonic-gate 	zone_t *zone;
17467c478bd9Sstevel@tonic-gate 	cred_t *cr;
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_by_id(zoneid)) == NULL)
17497c478bd9Sstevel@tonic-gate 		return (NULL);
17507c478bd9Sstevel@tonic-gate 	cr = zone->zone_kcred;
17517c478bd9Sstevel@tonic-gate 	crhold(cr);
17527c478bd9Sstevel@tonic-gate 	zone_rele(zone);
17537c478bd9Sstevel@tonic-gate 	return (cr);
17547c478bd9Sstevel@tonic-gate }
17557c478bd9Sstevel@tonic-gate 
17560209230bSgjelinek static int
17570209230bSgjelinek zone_lockedmem_kstat_update(kstat_t *ksp, int rw)
17580209230bSgjelinek {
17590209230bSgjelinek 	zone_t *zone = ksp->ks_private;
17600209230bSgjelinek 	zone_kstat_t *zk = ksp->ks_data;
17610209230bSgjelinek 
17620209230bSgjelinek 	if (rw == KSTAT_WRITE)
17630209230bSgjelinek 		return (EACCES);
17640209230bSgjelinek 
17650209230bSgjelinek 	zk->zk_usage.value.ui64 = zone->zone_locked_mem;
17660209230bSgjelinek 	zk->zk_value.value.ui64 = zone->zone_locked_mem_ctl;
17670209230bSgjelinek 	return (0);
17680209230bSgjelinek }
17690209230bSgjelinek 
17700209230bSgjelinek static int
1771ff19e029SMenno Lageman zone_nprocs_kstat_update(kstat_t *ksp, int rw)
1772ff19e029SMenno Lageman {
1773ff19e029SMenno Lageman 	zone_t *zone = ksp->ks_private;
1774ff19e029SMenno Lageman 	zone_kstat_t *zk = ksp->ks_data;
1775ff19e029SMenno Lageman 
1776ff19e029SMenno Lageman 	if (rw == KSTAT_WRITE)
1777ff19e029SMenno Lageman 		return (EACCES);
1778ff19e029SMenno Lageman 
1779ff19e029SMenno Lageman 	zk->zk_usage.value.ui64 = zone->zone_nprocs;
1780ff19e029SMenno Lageman 	zk->zk_value.value.ui64 = zone->zone_nprocs_ctl;
1781ff19e029SMenno Lageman 	return (0);
1782ff19e029SMenno Lageman }
1783ff19e029SMenno Lageman 
1784ff19e029SMenno Lageman static int
17850209230bSgjelinek zone_swapresv_kstat_update(kstat_t *ksp, int rw)
17860209230bSgjelinek {
17870209230bSgjelinek 	zone_t *zone = ksp->ks_private;
17880209230bSgjelinek 	zone_kstat_t *zk = ksp->ks_data;
17890209230bSgjelinek 
17900209230bSgjelinek 	if (rw == KSTAT_WRITE)
17910209230bSgjelinek 		return (EACCES);
17920209230bSgjelinek 
17930209230bSgjelinek 	zk->zk_usage.value.ui64 = zone->zone_max_swap;
17940209230bSgjelinek 	zk->zk_value.value.ui64 = zone->zone_max_swap_ctl;
17950209230bSgjelinek 	return (0);
17960209230bSgjelinek }
17970209230bSgjelinek 
1798ff19e029SMenno Lageman static kstat_t *
1799ff19e029SMenno Lageman zone_kstat_create_common(zone_t *zone, char *name,
1800ff19e029SMenno Lageman     int (*updatefunc) (kstat_t *, int))
18010209230bSgjelinek {
18020209230bSgjelinek 	kstat_t *ksp;
18030209230bSgjelinek 	zone_kstat_t *zk;
18040209230bSgjelinek 
1805ff19e029SMenno Lageman 	ksp = rctl_kstat_create_zone(zone, name, KSTAT_TYPE_NAMED,
18060209230bSgjelinek 	    sizeof (zone_kstat_t) / sizeof (kstat_named_t),
18070209230bSgjelinek 	    KSTAT_FLAG_VIRTUAL);
18080209230bSgjelinek 
18090209230bSgjelinek 	if (ksp == NULL)
1810ff19e029SMenno Lageman 		return (NULL);
18110209230bSgjelinek 
18120209230bSgjelinek 	zk = ksp->ks_data = kmem_alloc(sizeof (zone_kstat_t), KM_SLEEP);
18130209230bSgjelinek 	ksp->ks_data_size += strlen(zone->zone_name) + 1;
18140209230bSgjelinek 	kstat_named_init(&zk->zk_zonename, "zonename", KSTAT_DATA_STRING);
18150209230bSgjelinek 	kstat_named_setstr(&zk->zk_zonename, zone->zone_name);
18160209230bSgjelinek 	kstat_named_init(&zk->zk_usage, "usage", KSTAT_DATA_UINT64);
18170209230bSgjelinek 	kstat_named_init(&zk->zk_value, "value", KSTAT_DATA_UINT64);
1818ff19e029SMenno Lageman 	ksp->ks_update = updatefunc;
18190209230bSgjelinek 	ksp->ks_private = zone;
18200209230bSgjelinek 	kstat_install(ksp);
1821ff19e029SMenno Lageman 	return (ksp);
1822ff19e029SMenno Lageman }
18230209230bSgjelinek 
1824542a813cSJerry Jelinek static int
1825542a813cSJerry Jelinek zone_misc_kstat_update(kstat_t *ksp, int rw)
1826542a813cSJerry Jelinek {
1827542a813cSJerry Jelinek 	zone_t *zone = ksp->ks_private;
1828542a813cSJerry Jelinek 	zone_misc_kstat_t *zmp = ksp->ks_data;
1829542a813cSJerry Jelinek 	hrtime_t tmp;
1830542a813cSJerry Jelinek 
1831542a813cSJerry Jelinek 	if (rw == KSTAT_WRITE)
1832542a813cSJerry Jelinek 		return (EACCES);
1833542a813cSJerry Jelinek 
1834542a813cSJerry Jelinek 	tmp = zone->zone_utime;
1835542a813cSJerry Jelinek 	scalehrtime(&tmp);
1836542a813cSJerry Jelinek 	zmp->zm_utime.value.ui64 = tmp;
1837542a813cSJerry Jelinek 	tmp = zone->zone_stime;
1838542a813cSJerry Jelinek 	scalehrtime(&tmp);
1839542a813cSJerry Jelinek 	zmp->zm_stime.value.ui64 = tmp;
1840542a813cSJerry Jelinek 	tmp = zone->zone_wtime;
1841542a813cSJerry Jelinek 	scalehrtime(&tmp);
1842542a813cSJerry Jelinek 	zmp->zm_wtime.value.ui64 = tmp;
1843542a813cSJerry Jelinek 
1844*81d43577SJerry Jelinek 	zmp->zm_avenrun1.value.ui32 = zone->zone_avenrun[0];
1845*81d43577SJerry Jelinek 	zmp->zm_avenrun5.value.ui32 = zone->zone_avenrun[1];
1846*81d43577SJerry Jelinek 	zmp->zm_avenrun15.value.ui32 = zone->zone_avenrun[2];
1847*81d43577SJerry Jelinek 
1848542a813cSJerry Jelinek 	return (0);
1849542a813cSJerry Jelinek }
1850542a813cSJerry Jelinek 
1851542a813cSJerry Jelinek static kstat_t *
1852542a813cSJerry Jelinek zone_misc_kstat_create(zone_t *zone)
1853542a813cSJerry Jelinek {
1854542a813cSJerry Jelinek 	kstat_t *ksp;
1855542a813cSJerry Jelinek 	zone_misc_kstat_t *zmp;
1856542a813cSJerry Jelinek 
1857542a813cSJerry Jelinek 	if ((ksp = kstat_create_zone("zones", zone->zone_id,
1858542a813cSJerry Jelinek 	    zone->zone_name, "zone_misc", KSTAT_TYPE_NAMED,
1859542a813cSJerry Jelinek 	    sizeof (zone_misc_kstat_t) / sizeof (kstat_named_t),
1860542a813cSJerry Jelinek 	    KSTAT_FLAG_VIRTUAL, zone->zone_id)) == NULL)
1861542a813cSJerry Jelinek 		return (NULL);
1862542a813cSJerry Jelinek 
1863542a813cSJerry Jelinek 	if (zone->zone_id != GLOBAL_ZONEID)
1864542a813cSJerry Jelinek 		kstat_zone_add(ksp, GLOBAL_ZONEID);
1865542a813cSJerry Jelinek 
1866542a813cSJerry Jelinek 	zmp = ksp->ks_data = kmem_zalloc(sizeof (zone_misc_kstat_t), KM_SLEEP);
1867542a813cSJerry Jelinek 	ksp->ks_data_size += strlen(zone->zone_name) + 1;
1868542a813cSJerry Jelinek 	ksp->ks_lock = &zone->zone_misc_lock;
1869542a813cSJerry Jelinek 	zone->zone_misc_stats = zmp;
1870542a813cSJerry Jelinek 
1871542a813cSJerry Jelinek 	/* The kstat "name" field is not large enough for a full zonename */
1872542a813cSJerry Jelinek 	kstat_named_init(&zmp->zm_zonename, "zonename", KSTAT_DATA_STRING);
1873542a813cSJerry Jelinek 	kstat_named_setstr(&zmp->zm_zonename, zone->zone_name);
1874542a813cSJerry Jelinek 	kstat_named_init(&zmp->zm_utime, "nsec_user", KSTAT_DATA_UINT64);
1875542a813cSJerry Jelinek 	kstat_named_init(&zmp->zm_stime, "nsec_sys", KSTAT_DATA_UINT64);
1876542a813cSJerry Jelinek 	kstat_named_init(&zmp->zm_wtime, "nsec_waitrq", KSTAT_DATA_UINT64);
1877*81d43577SJerry Jelinek 	kstat_named_init(&zmp->zm_avenrun1, "avenrun_1min", KSTAT_DATA_UINT32);
1878*81d43577SJerry Jelinek 	kstat_named_init(&zmp->zm_avenrun5, "avenrun_5min", KSTAT_DATA_UINT32);
1879*81d43577SJerry Jelinek 	kstat_named_init(&zmp->zm_avenrun15, "avenrun_15min",
1880*81d43577SJerry Jelinek 	    KSTAT_DATA_UINT32);
1881542a813cSJerry Jelinek 
1882542a813cSJerry Jelinek 	ksp->ks_update = zone_misc_kstat_update;
1883542a813cSJerry Jelinek 	ksp->ks_private = zone;
1884542a813cSJerry Jelinek 
1885542a813cSJerry Jelinek 	kstat_install(ksp);
1886542a813cSJerry Jelinek 	return (ksp);
1887542a813cSJerry Jelinek }
1888542a813cSJerry Jelinek 
1889ff19e029SMenno Lageman static void
1890ff19e029SMenno Lageman zone_kstat_create(zone_t *zone)
1891ff19e029SMenno Lageman {
1892ff19e029SMenno Lageman 	zone->zone_lockedmem_kstat = zone_kstat_create_common(zone,
1893ff19e029SMenno Lageman 	    "lockedmem", zone_lockedmem_kstat_update);
1894ff19e029SMenno Lageman 	zone->zone_swapresv_kstat = zone_kstat_create_common(zone,
1895ff19e029SMenno Lageman 	    "swapresv", zone_swapresv_kstat_update);
1896ff19e029SMenno Lageman 	zone->zone_nprocs_kstat = zone_kstat_create_common(zone,
1897ff19e029SMenno Lageman 	    "nprocs", zone_nprocs_kstat_update);
1898542a813cSJerry Jelinek 
1899542a813cSJerry Jelinek 	if ((zone->zone_misc_ksp = zone_misc_kstat_create(zone)) == NULL) {
1900542a813cSJerry Jelinek 		zone->zone_misc_stats = kmem_zalloc(
1901542a813cSJerry Jelinek 		    sizeof (zone_misc_kstat_t), KM_SLEEP);
1902542a813cSJerry Jelinek 	}
1903ff19e029SMenno Lageman }
19040209230bSgjelinek 
1905ff19e029SMenno Lageman static void
1906542a813cSJerry Jelinek zone_kstat_delete_common(kstat_t **pkstat, size_t datasz)
1907ff19e029SMenno Lageman {
1908ff19e029SMenno Lageman 	void *data;
19090209230bSgjelinek 
1910ff19e029SMenno Lageman 	if (*pkstat != NULL) {
1911ff19e029SMenno Lageman 		data = (*pkstat)->ks_data;
1912ff19e029SMenno Lageman 		kstat_delete(*pkstat);
1913542a813cSJerry Jelinek 		kmem_free(data, datasz);
1914ff19e029SMenno Lageman 		*pkstat = NULL;
1915ff19e029SMenno Lageman 	}
19160209230bSgjelinek }
19170209230bSgjelinek 
19180209230bSgjelinek static void
19190209230bSgjelinek zone_kstat_delete(zone_t *zone)
19200209230bSgjelinek {
1921542a813cSJerry Jelinek 	zone_kstat_delete_common(&zone->zone_lockedmem_kstat,
1922542a813cSJerry Jelinek 	    sizeof (zone_kstat_t));
1923542a813cSJerry Jelinek 	zone_kstat_delete_common(&zone->zone_swapresv_kstat,
1924542a813cSJerry Jelinek 	    sizeof (zone_kstat_t));
1925542a813cSJerry Jelinek 	zone_kstat_delete_common(&zone->zone_nprocs_kstat,
1926542a813cSJerry Jelinek 	    sizeof (zone_kstat_t));
1927542a813cSJerry Jelinek 	zone_kstat_delete_common(&zone->zone_misc_ksp,
1928542a813cSJerry Jelinek 	    sizeof (zone_misc_kstat_t));
19290209230bSgjelinek }
19300209230bSgjelinek 
19317c478bd9Sstevel@tonic-gate /*
19327c478bd9Sstevel@tonic-gate  * Called very early on in boot to initialize the ZSD list so that
19337c478bd9Sstevel@tonic-gate  * zone_key_create() can be called before zone_init().  It also initializes
19347c478bd9Sstevel@tonic-gate  * portions of zone0 which may be used before zone_init() is called.  The
19357c478bd9Sstevel@tonic-gate  * variable "global_zone" will be set when zone0 is fully initialized by
19367c478bd9Sstevel@tonic-gate  * zone_init().
19377c478bd9Sstevel@tonic-gate  */
19387c478bd9Sstevel@tonic-gate void
19397c478bd9Sstevel@tonic-gate zone_zsd_init(void)
19407c478bd9Sstevel@tonic-gate {
19417c478bd9Sstevel@tonic-gate 	mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL);
19427c478bd9Sstevel@tonic-gate 	mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL);
19437c478bd9Sstevel@tonic-gate 	list_create(&zsd_registered_keys, sizeof (struct zsd_entry),
19447c478bd9Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
19457c478bd9Sstevel@tonic-gate 	list_create(&zone_active, sizeof (zone_t),
19467c478bd9Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
19477c478bd9Sstevel@tonic-gate 	list_create(&zone_deathrow, sizeof (zone_t),
19487c478bd9Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 	mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL);
19517c478bd9Sstevel@tonic-gate 	mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
19520209230bSgjelinek 	mutex_init(&zone0.zone_mem_lock, NULL, MUTEX_DEFAULT, NULL);
19537c478bd9Sstevel@tonic-gate 	zone0.zone_shares = 1;
19540209230bSgjelinek 	zone0.zone_nlwps = 0;
19557c478bd9Sstevel@tonic-gate 	zone0.zone_nlwps_ctl = INT_MAX;
1956ff19e029SMenno Lageman 	zone0.zone_nprocs = 0;
1957ff19e029SMenno Lageman 	zone0.zone_nprocs_ctl = INT_MAX;
19580209230bSgjelinek 	zone0.zone_locked_mem = 0;
19590209230bSgjelinek 	zone0.zone_locked_mem_ctl = UINT64_MAX;
19600209230bSgjelinek 	ASSERT(zone0.zone_max_swap == 0);
19610209230bSgjelinek 	zone0.zone_max_swap_ctl = UINT64_MAX;
19620fbb751dSJohn Levon 	zone0.zone_max_lofi = 0;
19630fbb751dSJohn Levon 	zone0.zone_max_lofi_ctl = UINT64_MAX;
1964824c205fSml93401 	zone0.zone_shmmax = 0;
1965824c205fSml93401 	zone0.zone_ipc.ipcq_shmmni = 0;
1966824c205fSml93401 	zone0.zone_ipc.ipcq_semmni = 0;
1967824c205fSml93401 	zone0.zone_ipc.ipcq_msgmni = 0;
19687c478bd9Sstevel@tonic-gate 	zone0.zone_name = GLOBAL_ZONENAME;
19697c478bd9Sstevel@tonic-gate 	zone0.zone_nodename = utsname.nodename;
19707c478bd9Sstevel@tonic-gate 	zone0.zone_domain = srpc_domain;
19715679c89fSjv227347 	zone0.zone_hostid = HW_INVALID_HOSTID;
19720fbb751dSJohn Levon 	zone0.zone_fs_allowed = NULL;
19737c478bd9Sstevel@tonic-gate 	zone0.zone_ref = 1;
19747c478bd9Sstevel@tonic-gate 	zone0.zone_id = GLOBAL_ZONEID;
19757c478bd9Sstevel@tonic-gate 	zone0.zone_status = ZONE_IS_RUNNING;
19767c478bd9Sstevel@tonic-gate 	zone0.zone_rootpath = "/";
19777c478bd9Sstevel@tonic-gate 	zone0.zone_rootpathlen = 2;
19787c478bd9Sstevel@tonic-gate 	zone0.zone_psetid = ZONE_PS_INVAL;
19797c478bd9Sstevel@tonic-gate 	zone0.zone_ncpus = 0;
19807c478bd9Sstevel@tonic-gate 	zone0.zone_ncpus_online = 0;
19817c478bd9Sstevel@tonic-gate 	zone0.zone_proc_initpid = 1;
19823f2f09c1Sdp 	zone0.zone_initname = initname;
19830209230bSgjelinek 	zone0.zone_lockedmem_kstat = NULL;
19840209230bSgjelinek 	zone0.zone_swapresv_kstat = NULL;
1985ff19e029SMenno Lageman 	zone0.zone_nprocs_kstat = NULL;
1986542a813cSJerry Jelinek 
1987542a813cSJerry Jelinek 	zone0.zone_stime = 0;
1988542a813cSJerry Jelinek 	zone0.zone_utime = 0;
1989542a813cSJerry Jelinek 	zone0.zone_wtime = 0;
1990542a813cSJerry Jelinek 
1991a19609f8Sjv227347 	list_create(&zone0.zone_ref_list, sizeof (zone_ref_t),
1992a19609f8Sjv227347 	    offsetof(zone_ref_t, zref_linkage));
19937c478bd9Sstevel@tonic-gate 	list_create(&zone0.zone_zsd, sizeof (struct zsd_entry),
19947c478bd9Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
19957c478bd9Sstevel@tonic-gate 	list_insert_head(&zone_active, &zone0);
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 	/*
19987c478bd9Sstevel@tonic-gate 	 * The root filesystem is not mounted yet, so zone_rootvp cannot be set
19997c478bd9Sstevel@tonic-gate 	 * to anything meaningful.  It is assigned to be 'rootdir' in
20007c478bd9Sstevel@tonic-gate 	 * vfs_mountroot().
20017c478bd9Sstevel@tonic-gate 	 */
20027c478bd9Sstevel@tonic-gate 	zone0.zone_rootvp = NULL;
20037c478bd9Sstevel@tonic-gate 	zone0.zone_vfslist = NULL;
20043f2f09c1Sdp 	zone0.zone_bootargs = initargs;
20057c478bd9Sstevel@tonic-gate 	zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
20067c478bd9Sstevel@tonic-gate 	/*
20077c478bd9Sstevel@tonic-gate 	 * The global zone has all privileges
20087c478bd9Sstevel@tonic-gate 	 */
20097c478bd9Sstevel@tonic-gate 	priv_fillset(zone0.zone_privset);
20107c478bd9Sstevel@tonic-gate 	/*
20117c478bd9Sstevel@tonic-gate 	 * Add p0 to the global zone
20127c478bd9Sstevel@tonic-gate 	 */
20137c478bd9Sstevel@tonic-gate 	zone0.zone_zsched = &p0;
20147c478bd9Sstevel@tonic-gate 	p0.p_zone = &zone0;
20157c478bd9Sstevel@tonic-gate }
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate /*
201845916cd2Sjpk  * Compute a hash value based on the contents of the label and the DOI.  The
201945916cd2Sjpk  * hash algorithm is somewhat arbitrary, but is based on the observation that
202045916cd2Sjpk  * humans will likely pick labels that differ by amounts that work out to be
202145916cd2Sjpk  * multiples of the number of hash chains, and thus stirring in some primes
202245916cd2Sjpk  * should help.
202345916cd2Sjpk  */
202445916cd2Sjpk static uint_t
202545916cd2Sjpk hash_bylabel(void *hdata, mod_hash_key_t key)
202645916cd2Sjpk {
202745916cd2Sjpk 	const ts_label_t *lab = (ts_label_t *)key;
202845916cd2Sjpk 	const uint32_t *up, *ue;
202945916cd2Sjpk 	uint_t hash;
203045916cd2Sjpk 	int i;
203145916cd2Sjpk 
203245916cd2Sjpk 	_NOTE(ARGUNUSED(hdata));
203345916cd2Sjpk 
203445916cd2Sjpk 	hash = lab->tsl_doi + (lab->tsl_doi << 1);
203545916cd2Sjpk 	/* we depend on alignment of label, but not representation */
203645916cd2Sjpk 	up = (const uint32_t *)&lab->tsl_label;
203745916cd2Sjpk 	ue = up + sizeof (lab->tsl_label) / sizeof (*up);
203845916cd2Sjpk 	i = 1;
203945916cd2Sjpk 	while (up < ue) {
204045916cd2Sjpk 		/* using 2^n + 1, 1 <= n <= 16 as source of many primes */
204145916cd2Sjpk 		hash += *up + (*up << ((i % 16) + 1));
204245916cd2Sjpk 		up++;
204345916cd2Sjpk 		i++;
204445916cd2Sjpk 	}
204545916cd2Sjpk 	return (hash);
204645916cd2Sjpk }
204745916cd2Sjpk 
204845916cd2Sjpk /*
204945916cd2Sjpk  * All that mod_hash cares about here is zero (equal) versus non-zero (not
205045916cd2Sjpk  * equal).  This may need to be changed if less than / greater than is ever
205145916cd2Sjpk  * needed.
205245916cd2Sjpk  */
205345916cd2Sjpk static int
205445916cd2Sjpk hash_labelkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
205545916cd2Sjpk {
205645916cd2Sjpk 	ts_label_t *lab1 = (ts_label_t *)key1;
205745916cd2Sjpk 	ts_label_t *lab2 = (ts_label_t *)key2;
205845916cd2Sjpk 
205945916cd2Sjpk 	return (label_equal(lab1, lab2) ? 0 : 1);
206045916cd2Sjpk }
206145916cd2Sjpk 
206245916cd2Sjpk /*
20637c478bd9Sstevel@tonic-gate  * Called by main() to initialize the zones framework.
20647c478bd9Sstevel@tonic-gate  */
20657c478bd9Sstevel@tonic-gate void
20667c478bd9Sstevel@tonic-gate zone_init(void)
20677c478bd9Sstevel@tonic-gate {
20687c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
20697c478bd9Sstevel@tonic-gate 	rctl_val_t *dval;
20707c478bd9Sstevel@tonic-gate 	rctl_set_t *set;
20717c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
20727c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
2073cf8f45c7Sdstaff 	int res;
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	ASSERT(curproc == &p0);
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	/*
20787c478bd9Sstevel@tonic-gate 	 * Create ID space for zone IDs.  ID 0 is reserved for the
20797c478bd9Sstevel@tonic-gate 	 * global zone.
20807c478bd9Sstevel@tonic-gate 	 */
20817c478bd9Sstevel@tonic-gate 	zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID);
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	/*
20847c478bd9Sstevel@tonic-gate 	 * Initialize generic zone resource controls, if any.
20857c478bd9Sstevel@tonic-gate 	 */
20867c478bd9Sstevel@tonic-gate 	rc_zone_cpu_shares = rctl_register("zone.cpu-shares",
20877c478bd9Sstevel@tonic-gate 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER |
208819f92332Sml93401 	    RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER,
2089c97ad5cdSakolb 	    FSS_MAXSHARES, FSS_MAXSHARES, &zone_cpu_shares_ops);
2090c97ad5cdSakolb 
2091c97ad5cdSakolb 	rc_zone_cpu_cap = rctl_register("zone.cpu-cap",
2092c97ad5cdSakolb 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_ALWAYS |
2093c97ad5cdSakolb 	    RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT |RCTL_GLOBAL_SYSLOG_NEVER |
2094c97ad5cdSakolb 	    RCTL_GLOBAL_INFINITE,
2095c97ad5cdSakolb 	    MAXCAP, MAXCAP, &zone_cpu_cap_ops);
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE,
20987c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
20997c478bd9Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &zone_lwps_ops);
2100ff19e029SMenno Lageman 
2101ff19e029SMenno Lageman 	rc_zone_nprocs = rctl_register("zone.max-processes", RCENTITY_ZONE,
2102ff19e029SMenno Lageman 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
2103ff19e029SMenno Lageman 	    INT_MAX, INT_MAX, &zone_procs_ops);
2104ff19e029SMenno Lageman 
21057c478bd9Sstevel@tonic-gate 	/*
2106824c205fSml93401 	 * System V IPC resource controls
2107824c205fSml93401 	 */
2108824c205fSml93401 	rc_zone_msgmni = rctl_register("zone.max-msg-ids",
2109824c205fSml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
2110824c205fSml93401 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_msgmni_ops);
2111824c205fSml93401 
2112824c205fSml93401 	rc_zone_semmni = rctl_register("zone.max-sem-ids",
2113824c205fSml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
2114824c205fSml93401 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_semmni_ops);
2115824c205fSml93401 
2116824c205fSml93401 	rc_zone_shmmni = rctl_register("zone.max-shm-ids",
2117824c205fSml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
2118824c205fSml93401 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &zone_shmmni_ops);
2119824c205fSml93401 
2120824c205fSml93401 	rc_zone_shmmax = rctl_register("zone.max-shm-memory",
2121824c205fSml93401 	    RCENTITY_ZONE, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
2122824c205fSml93401 	    RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &zone_shmmax_ops);
2123824c205fSml93401 
2124824c205fSml93401 	/*
21257c478bd9Sstevel@tonic-gate 	 * Create a rctl_val with PRIVILEGED, NOACTION, value = 1.  Then attach
21267c478bd9Sstevel@tonic-gate 	 * this at the head of the rctl_dict_entry for ``zone.cpu-shares''.
21277c478bd9Sstevel@tonic-gate 	 */
21287c478bd9Sstevel@tonic-gate 	dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
21297c478bd9Sstevel@tonic-gate 	bzero(dval, sizeof (rctl_val_t));
21307c478bd9Sstevel@tonic-gate 	dval->rcv_value = 1;
21317c478bd9Sstevel@tonic-gate 	dval->rcv_privilege = RCPRIV_PRIVILEGED;
21327c478bd9Sstevel@tonic-gate 	dval->rcv_flagaction = RCTL_LOCAL_NOACTION;
21337c478bd9Sstevel@tonic-gate 	dval->rcv_action_recip_pid = -1;
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	rde = rctl_dict_lookup("zone.cpu-shares");
21367c478bd9Sstevel@tonic-gate 	(void) rctl_val_list_insert(&rde->rcd_default_value, dval);
21377c478bd9Sstevel@tonic-gate 
2138c6939658Ssl108498 	rc_zone_locked_mem = rctl_register("zone.max-locked-memory",
2139c6939658Ssl108498 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES |
2140c6939658Ssl108498 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
2141c6939658Ssl108498 	    &zone_locked_mem_ops);
21420209230bSgjelinek 
21430209230bSgjelinek 	rc_zone_max_swap = rctl_register("zone.max-swap",
21440209230bSgjelinek 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES |
21450209230bSgjelinek 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
21460209230bSgjelinek 	    &zone_max_swap_ops);
21470209230bSgjelinek 
21480fbb751dSJohn Levon 	rc_zone_max_lofi = rctl_register("zone.max-lofi",
21490fbb751dSJohn Levon 	    RCENTITY_ZONE, RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT |
21500fbb751dSJohn Levon 	    RCTL_GLOBAL_DENY_ALWAYS, UINT64_MAX, UINT64_MAX,
21510fbb751dSJohn Levon 	    &zone_max_lofi_ops);
21520fbb751dSJohn Levon 
21537c478bd9Sstevel@tonic-gate 	/*
21547c478bd9Sstevel@tonic-gate 	 * Initialize the ``global zone''.
21557c478bd9Sstevel@tonic-gate 	 */
21567c478bd9Sstevel@tonic-gate 	set = rctl_set_create();
21577c478bd9Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
21587c478bd9Sstevel@tonic-gate 	mutex_enter(&p0.p_lock);
21597c478bd9Sstevel@tonic-gate 	e.rcep_p.zone = &zone0;
21607c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
21617c478bd9Sstevel@tonic-gate 	zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set,
21627c478bd9Sstevel@tonic-gate 	    gp);
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate 	zone0.zone_nlwps = p0.p_lwpcnt;
2165ff19e029SMenno Lageman 	zone0.zone_nprocs = 1;
21667c478bd9Sstevel@tonic-gate 	zone0.zone_ntasks = 1;
21677c478bd9Sstevel@tonic-gate 	mutex_exit(&p0.p_lock);
21689acbbeafSnn35248 	zone0.zone_restart_init = B_TRUE;
21699acbbeafSnn35248 	zone0.zone_brand = &native_brand;
21707c478bd9Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
21717c478bd9Sstevel@tonic-gate 	/*
21720209230bSgjelinek 	 * pool_default hasn't been initialized yet, so we let pool_init()
21730209230bSgjelinek 	 * take care of making sure the global zone is in the default pool.
21747c478bd9Sstevel@tonic-gate 	 */
217545916cd2Sjpk 
217645916cd2Sjpk 	/*
21770209230bSgjelinek 	 * Initialize global zone kstats
21780209230bSgjelinek 	 */
21790209230bSgjelinek 	zone_kstat_create(&zone0);
21800209230bSgjelinek 
21810209230bSgjelinek 	/*
218245916cd2Sjpk 	 * Initialize zone label.
218345916cd2Sjpk 	 * mlp are initialized when tnzonecfg is loaded.
218445916cd2Sjpk 	 */
218545916cd2Sjpk 	zone0.zone_slabel = l_admin_low;
218645916cd2Sjpk 	rw_init(&zone0.zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
218745916cd2Sjpk 	label_hold(l_admin_low);
218845916cd2Sjpk 
2189835ee219SRobert Harris 	/*
2190835ee219SRobert Harris 	 * Initialise the lock for the database structure used by mntfs.
2191835ee219SRobert Harris 	 */
2192835ee219SRobert Harris 	rw_init(&zone0.zone_mntfs_db_lock, NULL, RW_DEFAULT, NULL);
2193835ee219SRobert Harris 
21947c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
21957c478bd9Sstevel@tonic-gate 	zone_uniqid(&zone0);
21967c478bd9Sstevel@tonic-gate 	ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID);
219745916cd2Sjpk 
21987c478bd9Sstevel@tonic-gate 	zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size,
21997c478bd9Sstevel@tonic-gate 	    mod_hash_null_valdtor);
22007c478bd9Sstevel@tonic-gate 	zonehashbyname = mod_hash_create_strhash("zone_by_name",
22017c478bd9Sstevel@tonic-gate 	    zone_hash_size, mod_hash_null_valdtor);
220245916cd2Sjpk 	/*
220345916cd2Sjpk 	 * maintain zonehashbylabel only for labeled systems
220445916cd2Sjpk 	 */
220545916cd2Sjpk 	if (is_system_labeled())
220645916cd2Sjpk 		zonehashbylabel = mod_hash_create_extended("zone_by_label",
220745916cd2Sjpk 		    zone_hash_size, mod_hash_null_keydtor,
220845916cd2Sjpk 		    mod_hash_null_valdtor, hash_bylabel, NULL,
220945916cd2Sjpk 		    hash_labelkey_cmp, KM_SLEEP);
22107c478bd9Sstevel@tonic-gate 	zonecount = 1;
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID,
22137c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
22147c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name,
22157c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
221648451833Scarlsonj 	if (is_system_labeled()) {
221748451833Scarlsonj 		zone0.zone_flags |= ZF_HASHED_LABEL;
221845916cd2Sjpk 		(void) mod_hash_insert(zonehashbylabel,
221945916cd2Sjpk 		    (mod_hash_key_t)zone0.zone_slabel, (mod_hash_val_t)&zone0);
222048451833Scarlsonj 	}
222145916cd2Sjpk 	mutex_exit(&zonehash_lock);
222245916cd2Sjpk 
22237c478bd9Sstevel@tonic-gate 	/*
22247c478bd9Sstevel@tonic-gate 	 * We avoid setting zone_kcred until now, since kcred is initialized
22257c478bd9Sstevel@tonic-gate 	 * sometime after zone_zsd_init() and before zone_init().
22267c478bd9Sstevel@tonic-gate 	 */
22277c478bd9Sstevel@tonic-gate 	zone0.zone_kcred = kcred;
22287c478bd9Sstevel@tonic-gate 	/*
22297c478bd9Sstevel@tonic-gate 	 * The global zone is fully initialized (except for zone_rootvp which
22307c478bd9Sstevel@tonic-gate 	 * will be set when the root filesystem is mounted).
22317c478bd9Sstevel@tonic-gate 	 */
22327c478bd9Sstevel@tonic-gate 	global_zone = &zone0;
2233cf8f45c7Sdstaff 
2234cf8f45c7Sdstaff 	/*
2235cf8f45c7Sdstaff 	 * Setup an event channel to send zone status change notifications on
2236cf8f45c7Sdstaff 	 */
2237cf8f45c7Sdstaff 	res = sysevent_evc_bind(ZONE_EVENT_CHANNEL, &zone_event_chan,
2238cf8f45c7Sdstaff 	    EVCH_CREAT);
2239cf8f45c7Sdstaff 
2240cf8f45c7Sdstaff 	if (res)
2241cf8f45c7Sdstaff 		panic("Sysevent_evc_bind failed during zone setup.\n");
22420209230bSgjelinek 
22437c478bd9Sstevel@tonic-gate }
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate static void
22467c478bd9Sstevel@tonic-gate zone_free(zone_t *zone)
22477c478bd9Sstevel@tonic-gate {
22487c478bd9Sstevel@tonic-gate 	ASSERT(zone != global_zone);
22497c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks == 0);
22507c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_nlwps == 0);
2251ff19e029SMenno Lageman 	ASSERT(zone->zone_nprocs == 0);
22527c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 0);
22537c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_kcred == NULL);
22547c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_DEAD ||
22557c478bd9Sstevel@tonic-gate 	    zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
2256a19609f8Sjv227347 	ASSERT(list_is_empty(&zone->zone_ref_list));
22577c478bd9Sstevel@tonic-gate 
2258c97ad5cdSakolb 	/*
2259c97ad5cdSakolb 	 * Remove any zone caps.
2260c97ad5cdSakolb 	 */
2261c97ad5cdSakolb 	cpucaps_zone_remove(zone);
2262c97ad5cdSakolb 
2263c97ad5cdSakolb 	ASSERT(zone->zone_cpucap == NULL);
2264c97ad5cdSakolb 
22657c478bd9Sstevel@tonic-gate 	/* remove from deathrow list */
22667c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_DEAD) {
22677c478bd9Sstevel@tonic-gate 		ASSERT(zone->zone_ref == 0);
22687c478bd9Sstevel@tonic-gate 		mutex_enter(&zone_deathrow_lock);
22697c478bd9Sstevel@tonic-gate 		list_remove(&zone_deathrow, zone);
22707c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_deathrow_lock);
22717c478bd9Sstevel@tonic-gate 	}
22727c478bd9Sstevel@tonic-gate 
2273a19609f8Sjv227347 	list_destroy(&zone->zone_ref_list);
22747c478bd9Sstevel@tonic-gate 	zone_free_zsd(zone);
2275fa9e4066Sahrens 	zone_free_datasets(zone);
22762b24ab6bSSebastien Roy 	list_destroy(&zone->zone_dl_list);
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 	if (zone->zone_rootvp != NULL)
22797c478bd9Sstevel@tonic-gate 		VN_RELE(zone->zone_rootvp);
22807c478bd9Sstevel@tonic-gate 	if (zone->zone_rootpath)
22817c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_rootpath, zone->zone_rootpathlen);
22827c478bd9Sstevel@tonic-gate 	if (zone->zone_name != NULL)
22837c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_name, ZONENAME_MAX);
228445916cd2Sjpk 	if (zone->zone_slabel != NULL)
228545916cd2Sjpk 		label_rele(zone->zone_slabel);
22867c478bd9Sstevel@tonic-gate 	if (zone->zone_nodename != NULL)
22877c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_nodename, _SYS_NMLN);
22887c478bd9Sstevel@tonic-gate 	if (zone->zone_domain != NULL)
22897c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_domain, _SYS_NMLN);
22907c478bd9Sstevel@tonic-gate 	if (zone->zone_privset != NULL)
22917c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_privset, sizeof (priv_set_t));
22927c478bd9Sstevel@tonic-gate 	if (zone->zone_rctls != NULL)
22937c478bd9Sstevel@tonic-gate 		rctl_set_free(zone->zone_rctls);
22947c478bd9Sstevel@tonic-gate 	if (zone->zone_bootargs != NULL)
22950fbb751dSJohn Levon 		strfree(zone->zone_bootargs);
22963f2f09c1Sdp 	if (zone->zone_initname != NULL)
22970fbb751dSJohn Levon 		strfree(zone->zone_initname);
22980fbb751dSJohn Levon 	if (zone->zone_fs_allowed != NULL)
22990fbb751dSJohn Levon 		strfree(zone->zone_fs_allowed);
2300134a1f4eSCasper H.S. Dik 	if (zone->zone_pfexecd != NULL)
2301134a1f4eSCasper H.S. Dik 		klpd_freelist(&zone->zone_pfexecd);
23027c478bd9Sstevel@tonic-gate 	id_free(zoneid_space, zone->zone_id);
23037c478bd9Sstevel@tonic-gate 	mutex_destroy(&zone->zone_lock);
23047c478bd9Sstevel@tonic-gate 	cv_destroy(&zone->zone_cv);
230545916cd2Sjpk 	rw_destroy(&zone->zone_mlps.mlpl_rwlock);
2306835ee219SRobert Harris 	rw_destroy(&zone->zone_mntfs_db_lock);
23077c478bd9Sstevel@tonic-gate 	kmem_free(zone, sizeof (zone_t));
23087c478bd9Sstevel@tonic-gate }
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate /*
23117c478bd9Sstevel@tonic-gate  * See block comment at the top of this file for information about zone
23127c478bd9Sstevel@tonic-gate  * status values.
23137c478bd9Sstevel@tonic-gate  */
23147c478bd9Sstevel@tonic-gate /*
23157c478bd9Sstevel@tonic-gate  * Convenience function for setting zone status.
23167c478bd9Sstevel@tonic-gate  */
23177c478bd9Sstevel@tonic-gate static void
23187c478bd9Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status)
23197c478bd9Sstevel@tonic-gate {
2320cf8f45c7Sdstaff 
2321cf8f45c7Sdstaff 	nvlist_t *nvl = NULL;
23227c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zone_status_lock));
23237c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE &&
23247c478bd9Sstevel@tonic-gate 	    status >= zone_status_get(zone));
2325cf8f45c7Sdstaff 
2326cf8f45c7Sdstaff 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) ||
2327cf8f45c7Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_NAME, zone->zone_name) ||
2328cf8f45c7Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_NEWSTATE,
2329cf8f45c7Sdstaff 	    zone_status_table[status]) ||
2330cf8f45c7Sdstaff 	    nvlist_add_string(nvl, ZONE_CB_OLDSTATE,
2331cf8f45c7Sdstaff 	    zone_status_table[zone->zone_status]) ||
2332cf8f45c7Sdstaff 	    nvlist_add_int32(nvl, ZONE_CB_ZONEID, zone->zone_id) ||
2333cf8f45c7Sdstaff 	    nvlist_add_uint64(nvl, ZONE_CB_TIMESTAMP, (uint64_t)gethrtime()) ||
2334cf8f45c7Sdstaff 	    sysevent_evc_publish(zone_event_chan, ZONE_EVENT_STATUS_CLASS,
23353f2f09c1Sdp 	    ZONE_EVENT_STATUS_SUBCLASS, "sun.com", "kernel", nvl, EVCH_SLEEP)) {
2336cf8f45c7Sdstaff #ifdef DEBUG
2337cf8f45c7Sdstaff 		(void) printf(
2338cf8f45c7Sdstaff 		    "Failed to allocate and send zone state change event.\n");
2339cf8f45c7Sdstaff #endif
2340cf8f45c7Sdstaff 	}
2341cf8f45c7Sdstaff 	nvlist_free(nvl);
2342cf8f45c7Sdstaff 
23437c478bd9Sstevel@tonic-gate 	zone->zone_status = status;
2344cf8f45c7Sdstaff 
23457c478bd9Sstevel@tonic-gate 	cv_broadcast(&zone->zone_cv);
23467c478bd9Sstevel@tonic-gate }
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate /*
23497c478bd9Sstevel@tonic-gate  * Public function to retrieve the zone status.  The zone status may
23507c478bd9Sstevel@tonic-gate  * change after it is retrieved.
23517c478bd9Sstevel@tonic-gate  */
23527c478bd9Sstevel@tonic-gate zone_status_t
23537c478bd9Sstevel@tonic-gate zone_status_get(zone_t *zone)
23547c478bd9Sstevel@tonic-gate {
23557c478bd9Sstevel@tonic-gate 	return (zone->zone_status);
23567c478bd9Sstevel@tonic-gate }
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate static int
23597c478bd9Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs)
23607c478bd9Sstevel@tonic-gate {
23610fbb751dSJohn Levon 	char *buf = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP);
23623f2f09c1Sdp 	int err = 0;
23637c478bd9Sstevel@tonic-gate 
23643f2f09c1Sdp 	ASSERT(zone != global_zone);
23650fbb751dSJohn Levon 	if ((err = copyinstr(zone_bootargs, buf, BOOTARGS_MAX, NULL)) != 0)
23663f2f09c1Sdp 		goto done;	/* EFAULT or ENAMETOOLONG */
23673f2f09c1Sdp 
23683f2f09c1Sdp 	if (zone->zone_bootargs != NULL)
23690fbb751dSJohn Levon 		strfree(zone->zone_bootargs);
23703f2f09c1Sdp 
23710fbb751dSJohn Levon 	zone->zone_bootargs = strdup(buf);
23723f2f09c1Sdp 
23733f2f09c1Sdp done:
23740fbb751dSJohn Levon 	kmem_free(buf, BOOTARGS_MAX);
23753f2f09c1Sdp 	return (err);
23767c478bd9Sstevel@tonic-gate }
23777c478bd9Sstevel@tonic-gate 
23783f2f09c1Sdp static int
237959f2ff5cSedp zone_set_brand(zone_t *zone, const char *brand)
238059f2ff5cSedp {
238159f2ff5cSedp 	struct brand_attr *attrp;
238259f2ff5cSedp 	brand_t *bp;
238359f2ff5cSedp 
238459f2ff5cSedp 	attrp = kmem_alloc(sizeof (struct brand_attr), KM_SLEEP);
238559f2ff5cSedp 	if (copyin(brand, attrp, sizeof (struct brand_attr)) != 0) {
238659f2ff5cSedp 		kmem_free(attrp, sizeof (struct brand_attr));
238759f2ff5cSedp 		return (EFAULT);
238859f2ff5cSedp 	}
238959f2ff5cSedp 
239059f2ff5cSedp 	bp = brand_register_zone(attrp);
239159f2ff5cSedp 	kmem_free(attrp, sizeof (struct brand_attr));
239259f2ff5cSedp 	if (bp == NULL)
239359f2ff5cSedp 		return (EINVAL);
239459f2ff5cSedp 
239559f2ff5cSedp 	/*
239659f2ff5cSedp 	 * This is the only place where a zone can change it's brand.
239759f2ff5cSedp 	 * We already need to hold zone_status_lock to check the zone
239859f2ff5cSedp 	 * status, so we'll just use that lock to serialize zone
239959f2ff5cSedp 	 * branding requests as well.
240059f2ff5cSedp 	 */
240159f2ff5cSedp 	mutex_enter(&zone_status_lock);
240259f2ff5cSedp 
240359f2ff5cSedp 	/* Re-Branding is not allowed and the zone can't be booted yet */
240459f2ff5cSedp 	if ((ZONE_IS_BRANDED(zone)) ||
240559f2ff5cSedp 	    (zone_status_get(zone) >= ZONE_IS_BOOTING)) {
240659f2ff5cSedp 		mutex_exit(&zone_status_lock);
240759f2ff5cSedp 		brand_unregister_zone(bp);
240859f2ff5cSedp 		return (EINVAL);
240959f2ff5cSedp 	}
241059f2ff5cSedp 
2411319378d9Seh208807 	/* set up the brand specific data */
241259f2ff5cSedp 	zone->zone_brand = bp;
2413319378d9Seh208807 	ZBROP(zone)->b_init_brand_data(zone);
2414319378d9Seh208807 
241559f2ff5cSedp 	mutex_exit(&zone_status_lock);
241659f2ff5cSedp 	return (0);
241759f2ff5cSedp }
241859f2ff5cSedp 
241959f2ff5cSedp static int
24200fbb751dSJohn Levon zone_set_fs_allowed(zone_t *zone, const char *zone_fs_allowed)
24210fbb751dSJohn Levon {
24220fbb751dSJohn Levon 	char *buf = kmem_zalloc(ZONE_FS_ALLOWED_MAX, KM_SLEEP);
24230fbb751dSJohn Levon 	int err = 0;
24240fbb751dSJohn Levon 
24250fbb751dSJohn Levon 	ASSERT(zone != global_zone);
24260fbb751dSJohn Levon 	if ((err = copyinstr(zone_fs_allowed, buf,
24270fbb751dSJohn Levon 	    ZONE_FS_ALLOWED_MAX, NULL)) != 0)
24280fbb751dSJohn Levon 		goto done;
24290fbb751dSJohn Levon 
24300fbb751dSJohn Levon 	if (zone->zone_fs_allowed != NULL)
24310fbb751dSJohn Levon 		strfree(zone->zone_fs_allowed);
24320fbb751dSJohn Levon 
24330fbb751dSJohn Levon 	zone->zone_fs_allowed = strdup(buf);
24340fbb751dSJohn Levon 
24350fbb751dSJohn Levon done:
24360fbb751dSJohn Levon 	kmem_free(buf, ZONE_FS_ALLOWED_MAX);
24370fbb751dSJohn Levon 	return (err);
24380fbb751dSJohn Levon }
24390fbb751dSJohn Levon 
24400fbb751dSJohn Levon static int
24413f2f09c1Sdp zone_set_initname(zone_t *zone, const char *zone_initname)
24423f2f09c1Sdp {
24433f2f09c1Sdp 	char initname[INITNAME_SZ];
24443f2f09c1Sdp 	size_t len;
24453f2f09c1Sdp 	int err = 0;
24463f2f09c1Sdp 
24473f2f09c1Sdp 	ASSERT(zone != global_zone);
24483f2f09c1Sdp 	if ((err = copyinstr(zone_initname, initname, INITNAME_SZ, &len)) != 0)
24493f2f09c1Sdp 		return (err);	/* EFAULT or ENAMETOOLONG */
24503f2f09c1Sdp 
24513f2f09c1Sdp 	if (zone->zone_initname != NULL)
24520fbb751dSJohn Levon 		strfree(zone->zone_initname);
24533f2f09c1Sdp 
24543f2f09c1Sdp 	zone->zone_initname = kmem_alloc(strlen(initname) + 1, KM_SLEEP);
24553f2f09c1Sdp 	(void) strcpy(zone->zone_initname, initname);
24567c478bd9Sstevel@tonic-gate 	return (0);
24577c478bd9Sstevel@tonic-gate }
24587c478bd9Sstevel@tonic-gate 
24590209230bSgjelinek static int
24600209230bSgjelinek zone_set_phys_mcap(zone_t *zone, const uint64_t *zone_mcap)
24610209230bSgjelinek {
24620209230bSgjelinek 	uint64_t mcap;
24630209230bSgjelinek 	int err = 0;
24640209230bSgjelinek 
24650209230bSgjelinek 	if ((err = copyin(zone_mcap, &mcap, sizeof (uint64_t))) == 0)
24660209230bSgjelinek 		zone->zone_phys_mcap = mcap;
24670209230bSgjelinek 
24680209230bSgjelinek 	return (err);
24690209230bSgjelinek }
24700209230bSgjelinek 
24710209230bSgjelinek static int
24720209230bSgjelinek zone_set_sched_class(zone_t *zone, const char *new_class)
24730209230bSgjelinek {
24740209230bSgjelinek 	char sched_class[PC_CLNMSZ];
24750209230bSgjelinek 	id_t classid;
24760209230bSgjelinek 	int err;
24770209230bSgjelinek 
24780209230bSgjelinek 	ASSERT(zone != global_zone);
24790209230bSgjelinek 	if ((err = copyinstr(new_class, sched_class, PC_CLNMSZ, NULL)) != 0)
24800209230bSgjelinek 		return (err);	/* EFAULT or ENAMETOOLONG */
24810209230bSgjelinek 
248235a5a358SJonathan Adams 	if (getcid(sched_class, &classid) != 0 || CLASS_KERNEL(classid))
24830209230bSgjelinek 		return (set_errno(EINVAL));
24840209230bSgjelinek 	zone->zone_defaultcid = classid;
24850209230bSgjelinek 	ASSERT(zone->zone_defaultcid > 0 &&
24860209230bSgjelinek 	    zone->zone_defaultcid < loaded_classes);
24870209230bSgjelinek 
24880209230bSgjelinek 	return (0);
24890209230bSgjelinek }
24900209230bSgjelinek 
24917c478bd9Sstevel@tonic-gate /*
24927c478bd9Sstevel@tonic-gate  * Block indefinitely waiting for (zone_status >= status)
24937c478bd9Sstevel@tonic-gate  */
24947c478bd9Sstevel@tonic-gate void
24957c478bd9Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status)
24967c478bd9Sstevel@tonic-gate {
24977c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
24987c478bd9Sstevel@tonic-gate 
24997c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
25007c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
25017c478bd9Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
25027c478bd9Sstevel@tonic-gate 	}
25037c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
25047c478bd9Sstevel@tonic-gate }
25057c478bd9Sstevel@tonic-gate 
25067c478bd9Sstevel@tonic-gate /*
25077c478bd9Sstevel@tonic-gate  * Private CPR-safe version of zone_status_wait().
25087c478bd9Sstevel@tonic-gate  */
25097c478bd9Sstevel@tonic-gate static void
25107c478bd9Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str)
25117c478bd9Sstevel@tonic-gate {
25127c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr,
25177c478bd9Sstevel@tonic-gate 	    str);
25187c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
25197c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
25207c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
25217c478bd9Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
25227c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock);
25237c478bd9Sstevel@tonic-gate 	}
25247c478bd9Sstevel@tonic-gate 	/*
25257c478bd9Sstevel@tonic-gate 	 * zone_status_lock is implicitly released by the following.
25267c478bd9Sstevel@tonic-gate 	 */
25277c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
25287c478bd9Sstevel@tonic-gate }
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate /*
25317c478bd9Sstevel@tonic-gate  * Block until zone enters requested state or signal is received.  Return (0)
25327c478bd9Sstevel@tonic-gate  * if signaled, non-zero otherwise.
25337c478bd9Sstevel@tonic-gate  */
25347c478bd9Sstevel@tonic-gate int
25357c478bd9Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status)
25367c478bd9Sstevel@tonic-gate {
25377c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
25407c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
25417c478bd9Sstevel@tonic-gate 		if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) {
25427c478bd9Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
25437c478bd9Sstevel@tonic-gate 			return (0);
25447c478bd9Sstevel@tonic-gate 		}
25457c478bd9Sstevel@tonic-gate 	}
25467c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
25477c478bd9Sstevel@tonic-gate 	return (1);
25487c478bd9Sstevel@tonic-gate }
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate /*
25517c478bd9Sstevel@tonic-gate  * Block until the zone enters the requested state or the timeout expires,
25527c478bd9Sstevel@tonic-gate  * whichever happens first.  Return (-1) if operation timed out, time remaining
25537c478bd9Sstevel@tonic-gate  * otherwise.
25547c478bd9Sstevel@tonic-gate  */
25557c478bd9Sstevel@tonic-gate clock_t
25567c478bd9Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status)
25577c478bd9Sstevel@tonic-gate {
25587c478bd9Sstevel@tonic-gate 	clock_t timeleft = 0;
25597c478bd9Sstevel@tonic-gate 
25607c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
25637c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status && timeleft != -1) {
25647c478bd9Sstevel@tonic-gate 		timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim);
25657c478bd9Sstevel@tonic-gate 	}
25667c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
25677c478bd9Sstevel@tonic-gate 	return (timeleft);
25687c478bd9Sstevel@tonic-gate }
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate /*
25717c478bd9Sstevel@tonic-gate  * Block until the zone enters the requested state, the current process is
25727c478bd9Sstevel@tonic-gate  * signaled,  or the timeout expires, whichever happens first.  Return (-1) if
25737c478bd9Sstevel@tonic-gate  * operation timed out, 0 if signaled, time remaining otherwise.
25747c478bd9Sstevel@tonic-gate  */
25757c478bd9Sstevel@tonic-gate clock_t
25767c478bd9Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status)
25777c478bd9Sstevel@tonic-gate {
2578d3d50737SRafael Vanoni 	clock_t timeleft = tim - ddi_get_lbolt();
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
25837c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
25847c478bd9Sstevel@tonic-gate 		timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock,
25857c478bd9Sstevel@tonic-gate 		    tim);
25867c478bd9Sstevel@tonic-gate 		if (timeleft <= 0)
25877c478bd9Sstevel@tonic-gate 			break;
25887c478bd9Sstevel@tonic-gate 	}
25897c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
25907c478bd9Sstevel@tonic-gate 	return (timeleft);
25917c478bd9Sstevel@tonic-gate }
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate /*
25947c478bd9Sstevel@tonic-gate  * Zones have two reference counts: one for references from credential
25957c478bd9Sstevel@tonic-gate  * structures (zone_cred_ref), and one (zone_ref) for everything else.
25967c478bd9Sstevel@tonic-gate  * This is so we can allow a zone to be rebooted while there are still
25977c478bd9Sstevel@tonic-gate  * outstanding cred references, since certain drivers cache dblks (which
25987c478bd9Sstevel@tonic-gate  * implicitly results in cached creds).  We wait for zone_ref to drop to
25997c478bd9Sstevel@tonic-gate  * 0 (actually 1), but not zone_cred_ref.  The zone structure itself is
26007c478bd9Sstevel@tonic-gate  * later freed when the zone_cred_ref drops to 0, though nothing other
26017c478bd9Sstevel@tonic-gate  * than the zone id and privilege set should be accessed once the zone
26027c478bd9Sstevel@tonic-gate  * is "dead".
26037c478bd9Sstevel@tonic-gate  *
26047c478bd9Sstevel@tonic-gate  * A debugging flag, zone_wait_for_cred, can be set to a non-zero value
26057c478bd9Sstevel@tonic-gate  * to force halt/reboot to block waiting for the zone_cred_ref to drop
26067c478bd9Sstevel@tonic-gate  * to 0.  This can be useful to flush out other sources of cached creds
26077c478bd9Sstevel@tonic-gate  * that may be less innocuous than the driver case.
2608a19609f8Sjv227347  *
2609a19609f8Sjv227347  * Zones also provide a tracked reference counting mechanism in which zone
2610a19609f8Sjv227347  * references are represented by "crumbs" (zone_ref structures).  Crumbs help
2611a19609f8Sjv227347  * debuggers determine the sources of leaked zone references.  See
2612a19609f8Sjv227347  * zone_hold_ref() and zone_rele_ref() below for more information.
26137c478bd9Sstevel@tonic-gate  */
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate int zone_wait_for_cred = 0;
26167c478bd9Sstevel@tonic-gate 
26177c478bd9Sstevel@tonic-gate static void
26187c478bd9Sstevel@tonic-gate zone_hold_locked(zone_t *z)
26197c478bd9Sstevel@tonic-gate {
26207c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&z->zone_lock));
26217c478bd9Sstevel@tonic-gate 	z->zone_ref++;
26227c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
26237c478bd9Sstevel@tonic-gate }
26247c478bd9Sstevel@tonic-gate 
2625a19609f8Sjv227347 /*
2626a19609f8Sjv227347  * Increment the specified zone's reference count.  The zone's zone_t structure
2627a19609f8Sjv227347  * will not be freed as long as the zone's reference count is nonzero.
2628a19609f8Sjv227347  * Decrement the zone's reference count via zone_rele().
2629a19609f8Sjv227347  *
2630a19609f8Sjv227347  * NOTE: This function should only be used to hold zones for short periods of
2631a19609f8Sjv227347  * time.  Use zone_hold_ref() if the zone must be held for a long time.
2632a19609f8Sjv227347  */
26337c478bd9Sstevel@tonic-gate void
26347c478bd9Sstevel@tonic-gate zone_hold(zone_t *z)
26357c478bd9Sstevel@tonic-gate {
26367c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
26377c478bd9Sstevel@tonic-gate 	zone_hold_locked(z);
26387c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
26397c478bd9Sstevel@tonic-gate }
26407c478bd9Sstevel@tonic-gate 
26417c478bd9Sstevel@tonic-gate /*
26427c478bd9Sstevel@tonic-gate  * If the non-cred ref count drops to 1 and either the cred ref count
26437c478bd9Sstevel@tonic-gate  * is 0 or we aren't waiting for cred references, the zone is ready to
26447c478bd9Sstevel@tonic-gate  * be destroyed.
26457c478bd9Sstevel@tonic-gate  */
26467c478bd9Sstevel@tonic-gate #define	ZONE_IS_UNREF(zone)	((zone)->zone_ref == 1 && \
26477c478bd9Sstevel@tonic-gate 	    (!zone_wait_for_cred || (zone)->zone_cred_ref == 0))
26487c478bd9Sstevel@tonic-gate 
2649a19609f8Sjv227347 /*
2650a19609f8Sjv227347  * Common zone reference release function invoked by zone_rele() and
2651a19609f8Sjv227347  * zone_rele_ref().  If subsys is ZONE_REF_NUM_SUBSYS, then the specified
2652a19609f8Sjv227347  * zone's subsystem-specific reference counters are not affected by the
2653a19609f8Sjv227347  * release.  If ref is not NULL, then the zone_ref_t to which it refers is
2654a19609f8Sjv227347  * removed from the specified zone's reference list.  ref must be non-NULL iff
2655a19609f8Sjv227347  * subsys is not ZONE_REF_NUM_SUBSYS.
2656a19609f8Sjv227347  */
2657a19609f8Sjv227347 static void
2658a19609f8Sjv227347 zone_rele_common(zone_t *z, zone_ref_t *ref, zone_ref_subsys_t subsys)
26597c478bd9Sstevel@tonic-gate {
26607c478bd9Sstevel@tonic-gate 	boolean_t wakeup;
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
26637c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
26647c478bd9Sstevel@tonic-gate 	z->zone_ref--;
2665a19609f8Sjv227347 	if (subsys != ZONE_REF_NUM_SUBSYS) {
2666a19609f8Sjv227347 		ASSERT(z->zone_subsys_ref[subsys] != 0);
2667a19609f8Sjv227347 		z->zone_subsys_ref[subsys]--;
2668a19609f8Sjv227347 		list_remove(&z->zone_ref_list, ref);
2669a19609f8Sjv227347 	}
26707c478bd9Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
26717c478bd9Sstevel@tonic-gate 		/* no more refs, free the structure */
26727c478bd9Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
26737c478bd9Sstevel@tonic-gate 		zone_free(z);
26747c478bd9Sstevel@tonic-gate 		return;
26757c478bd9Sstevel@tonic-gate 	}
26767c478bd9Sstevel@tonic-gate 	/* signal zone_destroy so the zone can finish halting */
26777c478bd9Sstevel@tonic-gate 	wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD);
26787c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
26797c478bd9Sstevel@tonic-gate 
26807c478bd9Sstevel@tonic-gate 	if (wakeup) {
26817c478bd9Sstevel@tonic-gate 		/*
26827c478bd9Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
26837c478bd9Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
26847c478bd9Sstevel@tonic-gate 		 */
26857c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
26867c478bd9Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
26877c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
26887c478bd9Sstevel@tonic-gate 	}
26897c478bd9Sstevel@tonic-gate }
26907c478bd9Sstevel@tonic-gate 
2691a19609f8Sjv227347 /*
2692a19609f8Sjv227347  * Decrement the specified zone's reference count.  The specified zone will
2693a19609f8Sjv227347  * cease to exist after this function returns if the reference count drops to
2694a19609f8Sjv227347  * zero.  This function should be paired with zone_hold().
2695a19609f8Sjv227347  */
2696a19609f8Sjv227347 void
2697a19609f8Sjv227347 zone_rele(zone_t *z)
2698a19609f8Sjv227347 {
2699a19609f8Sjv227347 	zone_rele_common(z, NULL, ZONE_REF_NUM_SUBSYS);
2700a19609f8Sjv227347 }
2701a19609f8Sjv227347 
2702a19609f8Sjv227347 /*
2703a19609f8Sjv227347  * Initialize a zone reference structure.  This function must be invoked for
2704a19609f8Sjv227347  * a reference structure before the structure is passed to zone_hold_ref().
2705a19609f8Sjv227347  */
2706a19609f8Sjv227347 void
2707a19609f8Sjv227347 zone_init_ref(zone_ref_t *ref)
2708a19609f8Sjv227347 {
2709a19609f8Sjv227347 	ref->zref_zone = NULL;
2710a19609f8Sjv227347 	list_link_init(&ref->zref_linkage);
2711a19609f8Sjv227347 }
2712a19609f8Sjv227347 
2713a19609f8Sjv227347 /*
2714a19609f8Sjv227347  * Acquire a reference to zone z.  The caller must specify the
2715a19609f8Sjv227347  * zone_ref_subsys_t constant associated with its subsystem.  The specified
2716a19609f8Sjv227347  * zone_ref_t structure will represent a reference to the specified zone.  Use
2717a19609f8Sjv227347  * zone_rele_ref() to release the reference.
2718a19609f8Sjv227347  *
2719a19609f8Sjv227347  * The referenced zone_t structure will not be freed as long as the zone_t's
2720a19609f8Sjv227347  * zone_status field is not ZONE_IS_DEAD and the zone has outstanding
2721a19609f8Sjv227347  * references.
2722a19609f8Sjv227347  *
2723a19609f8Sjv227347  * NOTE: The zone_ref_t structure must be initialized before it is used.
2724a19609f8Sjv227347  * See zone_init_ref() above.
2725a19609f8Sjv227347  */
2726a19609f8Sjv227347 void
2727a19609f8Sjv227347 zone_hold_ref(zone_t *z, zone_ref_t *ref, zone_ref_subsys_t subsys)
2728a19609f8Sjv227347 {
2729a19609f8Sjv227347 	ASSERT(subsys >= 0 && subsys < ZONE_REF_NUM_SUBSYS);
2730a19609f8Sjv227347 
2731a19609f8Sjv227347 	/*
2732a19609f8Sjv227347 	 * Prevent consumers from reusing a reference structure before
2733a19609f8Sjv227347 	 * releasing it.
2734a19609f8Sjv227347 	 */
2735a19609f8Sjv227347 	VERIFY(ref->zref_zone == NULL);
2736a19609f8Sjv227347 
2737a19609f8Sjv227347 	ref->zref_zone = z;
2738a19609f8Sjv227347 	mutex_enter(&z->zone_lock);
2739a19609f8Sjv227347 	zone_hold_locked(z);
2740a19609f8Sjv227347 	z->zone_subsys_ref[subsys]++;
2741a19609f8Sjv227347 	ASSERT(z->zone_subsys_ref[subsys] != 0);
2742a19609f8Sjv227347 	list_insert_head(&z->zone_ref_list, ref);
2743a19609f8Sjv227347 	mutex_exit(&z->zone_lock);
2744a19609f8Sjv227347 }
2745a19609f8Sjv227347 
2746a19609f8Sjv227347 /*
2747a19609f8Sjv227347  * Release the zone reference represented by the specified zone_ref_t.
2748a19609f8Sjv227347  * The reference is invalid after it's released; however, the zone_ref_t
2749a19609f8Sjv227347  * structure can be reused without having to invoke zone_init_ref().
2750a19609f8Sjv227347  * subsys should be the same value that was passed to zone_hold_ref()
2751a19609f8Sjv227347  * when the reference was acquired.
2752a19609f8Sjv227347  */
2753a19609f8Sjv227347 void
2754a19609f8Sjv227347 zone_rele_ref(zone_ref_t *ref, zone_ref_subsys_t subsys)
2755a19609f8Sjv227347 {
2756a19609f8Sjv227347 	zone_rele_common(ref->zref_zone, ref, subsys);
2757a19609f8Sjv227347 
2758a19609f8Sjv227347 	/*
2759a19609f8Sjv227347 	 * Set the zone_ref_t's zref_zone field to NULL to generate panics
2760a19609f8Sjv227347 	 * when consumers dereference the reference.  This helps us catch
2761a19609f8Sjv227347 	 * consumers who use released references.  Furthermore, this lets
2762a19609f8Sjv227347 	 * consumers reuse the zone_ref_t structure without having to
2763a19609f8Sjv227347 	 * invoke zone_init_ref().
2764a19609f8Sjv227347 	 */
2765a19609f8Sjv227347 	ref->zref_zone = NULL;
2766a19609f8Sjv227347 }
2767a19609f8Sjv227347 
27687c478bd9Sstevel@tonic-gate void
27697c478bd9Sstevel@tonic-gate zone_cred_hold(zone_t *z)
27707c478bd9Sstevel@tonic-gate {
27717c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
27727c478bd9Sstevel@tonic-gate 	z->zone_cred_ref++;
27737c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
27747c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
27757c478bd9Sstevel@tonic-gate }
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate void
27787c478bd9Sstevel@tonic-gate zone_cred_rele(zone_t *z)
27797c478bd9Sstevel@tonic-gate {
27807c478bd9Sstevel@tonic-gate 	boolean_t wakeup;
27817c478bd9Sstevel@tonic-gate 
27827c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
27837c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
27847c478bd9Sstevel@tonic-gate 	z->zone_cred_ref--;
27857c478bd9Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
27867c478bd9Sstevel@tonic-gate 		/* no more refs, free the structure */
27877c478bd9Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
27887c478bd9Sstevel@tonic-gate 		zone_free(z);
27897c478bd9Sstevel@tonic-gate 		return;
27907c478bd9Sstevel@tonic-gate 	}
27917c478bd9Sstevel@tonic-gate 	/*
27927c478bd9Sstevel@tonic-gate 	 * If zone_destroy is waiting for the cred references to drain
27937c478bd9Sstevel@tonic-gate 	 * out, and they have, signal it.
27947c478bd9Sstevel@tonic-gate 	 */
27957c478bd9Sstevel@tonic-gate 	wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) &&
27967c478bd9Sstevel@tonic-gate 	    zone_status_get(z) >= ZONE_IS_DEAD);
27977c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	if (wakeup) {
28007c478bd9Sstevel@tonic-gate 		/*
28017c478bd9Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
28027c478bd9Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
28037c478bd9Sstevel@tonic-gate 		 */
28047c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
28057c478bd9Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
28067c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
28077c478bd9Sstevel@tonic-gate 	}
28087c478bd9Sstevel@tonic-gate }
28097c478bd9Sstevel@tonic-gate 
28107c478bd9Sstevel@tonic-gate void
28117c478bd9Sstevel@tonic-gate zone_task_hold(zone_t *z)
28127c478bd9Sstevel@tonic-gate {
28137c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
28147c478bd9Sstevel@tonic-gate 	z->zone_ntasks++;
28157c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_ntasks != 0);
28167c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
28177c478bd9Sstevel@tonic-gate }
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate void
28207c478bd9Sstevel@tonic-gate zone_task_rele(zone_t *zone)
28217c478bd9Sstevel@tonic-gate {
28227c478bd9Sstevel@tonic-gate 	uint_t refcnt;
28237c478bd9Sstevel@tonic-gate 
28247c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
28257c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks != 0);
28267c478bd9Sstevel@tonic-gate 	refcnt = --zone->zone_ntasks;
28277c478bd9Sstevel@tonic-gate 	if (refcnt > 1)	{	/* Common case */
28287c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
28297c478bd9Sstevel@tonic-gate 		return;
28307c478bd9Sstevel@tonic-gate 	}
28317c478bd9Sstevel@tonic-gate 	zone_hold_locked(zone);	/* so we can use the zone_t later */
28327c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
28337c478bd9Sstevel@tonic-gate 	if (refcnt == 1) {
28347c478bd9Sstevel@tonic-gate 		/*
28357c478bd9Sstevel@tonic-gate 		 * See if the zone is shutting down.
28367c478bd9Sstevel@tonic-gate 		 */
28377c478bd9Sstevel@tonic-gate 		mutex_enter(&zone_status_lock);
28387c478bd9Sstevel@tonic-gate 		if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) {
28397c478bd9Sstevel@tonic-gate 			goto out;
28407c478bd9Sstevel@tonic-gate 		}
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 		/*
28437c478bd9Sstevel@tonic-gate 		 * Make sure the ntasks didn't change since we
28447c478bd9Sstevel@tonic-gate 		 * dropped zone_lock.
28457c478bd9Sstevel@tonic-gate 		 */
28467c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
28477c478bd9Sstevel@tonic-gate 		if (refcnt != zone->zone_ntasks) {
28487c478bd9Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
28497c478bd9Sstevel@tonic-gate 			goto out;
28507c478bd9Sstevel@tonic-gate 		}
28517c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
28527c478bd9Sstevel@tonic-gate 
28537c478bd9Sstevel@tonic-gate 		/*
28547c478bd9Sstevel@tonic-gate 		 * No more user processes in the zone.  The zone is empty.
28557c478bd9Sstevel@tonic-gate 		 */
28567c478bd9Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_EMPTY);
28577c478bd9Sstevel@tonic-gate 		goto out;
28587c478bd9Sstevel@tonic-gate 	}
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate 	ASSERT(refcnt == 0);
28617c478bd9Sstevel@tonic-gate 	/*
28627c478bd9Sstevel@tonic-gate 	 * zsched has exited; the zone is dead.
28637c478bd9Sstevel@tonic-gate 	 */
28647c478bd9Sstevel@tonic-gate 	zone->zone_zsched = NULL;		/* paranoia */
28657c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
28667c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_DEAD);
28677c478bd9Sstevel@tonic-gate out:
28687c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
28697c478bd9Sstevel@tonic-gate 	zone_rele(zone);
28707c478bd9Sstevel@tonic-gate }
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate zoneid_t
28737c478bd9Sstevel@tonic-gate getzoneid(void)
28747c478bd9Sstevel@tonic-gate {
28757c478bd9Sstevel@tonic-gate 	return (curproc->p_zone->zone_id);
28767c478bd9Sstevel@tonic-gate }
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate /*
28797c478bd9Sstevel@tonic-gate  * Internal versions of zone_find_by_*().  These don't zone_hold() or
28807c478bd9Sstevel@tonic-gate  * check the validity of a zone's state.
28817c478bd9Sstevel@tonic-gate  */
28827c478bd9Sstevel@tonic-gate static zone_t *
28837c478bd9Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid)
28847c478bd9Sstevel@tonic-gate {
28857c478bd9Sstevel@tonic-gate 	mod_hash_val_t hv;
28867c478bd9Sstevel@tonic-gate 	zone_t *zone = NULL;
28877c478bd9Sstevel@tonic-gate 
28887c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyid,
28917c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0)
28927c478bd9Sstevel@tonic-gate 		zone = (zone_t *)hv;
28937c478bd9Sstevel@tonic-gate 	return (zone);
28947c478bd9Sstevel@tonic-gate }
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate static zone_t *
289745916cd2Sjpk zone_find_all_by_label(const ts_label_t *label)
289845916cd2Sjpk {
289945916cd2Sjpk 	mod_hash_val_t hv;
290045916cd2Sjpk 	zone_t *zone = NULL;
290145916cd2Sjpk 
290245916cd2Sjpk 	ASSERT(MUTEX_HELD(&zonehash_lock));
290345916cd2Sjpk 
290445916cd2Sjpk 	/*
290545916cd2Sjpk 	 * zonehashbylabel is not maintained for unlabeled systems
290645916cd2Sjpk 	 */
290745916cd2Sjpk 	if (!is_system_labeled())
290845916cd2Sjpk 		return (NULL);
290945916cd2Sjpk 	if (mod_hash_find(zonehashbylabel, (mod_hash_key_t)label, &hv) == 0)
291045916cd2Sjpk 		zone = (zone_t *)hv;
291145916cd2Sjpk 	return (zone);
291245916cd2Sjpk }
291345916cd2Sjpk 
291445916cd2Sjpk static zone_t *
29157c478bd9Sstevel@tonic-gate zone_find_all_by_name(char *name)
29167c478bd9Sstevel@tonic-gate {
29177c478bd9Sstevel@tonic-gate 	mod_hash_val_t hv;
29187c478bd9Sstevel@tonic-gate 	zone_t *zone = NULL;
29197c478bd9Sstevel@tonic-gate 
29207c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0)
29237c478bd9Sstevel@tonic-gate 		zone = (zone_t *)hv;
29247c478bd9Sstevel@tonic-gate 	return (zone);
29257c478bd9Sstevel@tonic-gate }
29267c478bd9Sstevel@tonic-gate 
29277c478bd9Sstevel@tonic-gate /*
29287c478bd9Sstevel@tonic-gate  * Public interface for looking up a zone by zoneid.  Only returns the zone if
29297c478bd9Sstevel@tonic-gate  * it is fully initialized, and has not yet begun the zone_destroy() sequence.
29307c478bd9Sstevel@tonic-gate  * Caller must call zone_rele() once it is done with the zone.
29317c478bd9Sstevel@tonic-gate  *
29327c478bd9Sstevel@tonic-gate  * The zone may begin the zone_destroy() sequence immediately after this
29337c478bd9Sstevel@tonic-gate  * function returns, but may be safely used until zone_rele() is called.
29347c478bd9Sstevel@tonic-gate  */
29357c478bd9Sstevel@tonic-gate zone_t *
29367c478bd9Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid)
29377c478bd9Sstevel@tonic-gate {
29387c478bd9Sstevel@tonic-gate 	zone_t *zone;
29397c478bd9Sstevel@tonic-gate 	zone_status_t status;
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
29427c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
29437c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
29447c478bd9Sstevel@tonic-gate 		return (NULL);
29457c478bd9Sstevel@tonic-gate 	}
29467c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
29477c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
29487c478bd9Sstevel@tonic-gate 		/*
29497c478bd9Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
29507c478bd9Sstevel@tonic-gate 		 */
29517c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
29527c478bd9Sstevel@tonic-gate 		return (NULL);
29537c478bd9Sstevel@tonic-gate 	}
29547c478bd9Sstevel@tonic-gate 	zone_hold(zone);
29557c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
29567c478bd9Sstevel@tonic-gate 	return (zone);
29577c478bd9Sstevel@tonic-gate }
29587c478bd9Sstevel@tonic-gate 
29597c478bd9Sstevel@tonic-gate /*
296045916cd2Sjpk  * Similar to zone_find_by_id, but using zone label as the key.
296145916cd2Sjpk  */
296245916cd2Sjpk zone_t *
296345916cd2Sjpk zone_find_by_label(const ts_label_t *label)
296445916cd2Sjpk {
296545916cd2Sjpk 	zone_t *zone;
296642bc57c4Srica 	zone_status_t status;
296745916cd2Sjpk 
296845916cd2Sjpk 	mutex_enter(&zonehash_lock);
296945916cd2Sjpk 	if ((zone = zone_find_all_by_label(label)) == NULL) {
297045916cd2Sjpk 		mutex_exit(&zonehash_lock);
297145916cd2Sjpk 		return (NULL);
297245916cd2Sjpk 	}
297342bc57c4Srica 
297442bc57c4Srica 	status = zone_status_get(zone);
297542bc57c4Srica 	if (status > ZONE_IS_DOWN) {
297645916cd2Sjpk 		/*
297745916cd2Sjpk 		 * For all practical purposes the zone doesn't exist.
297845916cd2Sjpk 		 */
297942bc57c4Srica 		mutex_exit(&zonehash_lock);
298042bc57c4Srica 		return (NULL);
298145916cd2Sjpk 	}
298242bc57c4Srica 	zone_hold(zone);
298345916cd2Sjpk 	mutex_exit(&zonehash_lock);
298445916cd2Sjpk 	return (zone);
298545916cd2Sjpk }
298645916cd2Sjpk 
298745916cd2Sjpk /*
29887c478bd9Sstevel@tonic-gate  * Similar to zone_find_by_id, but using zone name as the key.
29897c478bd9Sstevel@tonic-gate  */
29907c478bd9Sstevel@tonic-gate zone_t *
29917c478bd9Sstevel@tonic-gate zone_find_by_name(char *name)
29927c478bd9Sstevel@tonic-gate {
29937c478bd9Sstevel@tonic-gate 	zone_t *zone;
29947c478bd9Sstevel@tonic-gate 	zone_status_t status;
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
29977c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_name(name)) == NULL) {
29987c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
29997c478bd9Sstevel@tonic-gate 		return (NULL);
30007c478bd9Sstevel@tonic-gate 	}
30017c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
30027c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
30037c478bd9Sstevel@tonic-gate 		/*
30047c478bd9Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
30057c478bd9Sstevel@tonic-gate 		 */
30067c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
30077c478bd9Sstevel@tonic-gate 		return (NULL);
30087c478bd9Sstevel@tonic-gate 	}
30097c478bd9Sstevel@tonic-gate 	zone_hold(zone);
30107c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
30117c478bd9Sstevel@tonic-gate 	return (zone);
30127c478bd9Sstevel@tonic-gate }
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate /*
30157c478bd9Sstevel@tonic-gate  * Similar to zone_find_by_id(), using the path as a key.  For instance,
30167c478bd9Sstevel@tonic-gate  * if there is a zone "foo" rooted at /foo/root, and the path argument
30177c478bd9Sstevel@tonic-gate  * is "/foo/root/proc", it will return the held zone_t corresponding to
30187c478bd9Sstevel@tonic-gate  * zone "foo".
30197c478bd9Sstevel@tonic-gate  *
30207c478bd9Sstevel@tonic-gate  * zone_find_by_path() always returns a non-NULL value, since at the
30217c478bd9Sstevel@tonic-gate  * very least every path will be contained in the global zone.
30227c478bd9Sstevel@tonic-gate  *
30237c478bd9Sstevel@tonic-gate  * As with the other zone_find_by_*() functions, the caller is
30247c478bd9Sstevel@tonic-gate  * responsible for zone_rele()ing the return value of this function.
30257c478bd9Sstevel@tonic-gate  */
30267c478bd9Sstevel@tonic-gate zone_t *
30277c478bd9Sstevel@tonic-gate zone_find_by_path(const char *path)
30287c478bd9Sstevel@tonic-gate {
30297c478bd9Sstevel@tonic-gate 	zone_t *zone;
30307c478bd9Sstevel@tonic-gate 	zone_t *zret = NULL;
30317c478bd9Sstevel@tonic-gate 	zone_status_t status;
30327c478bd9Sstevel@tonic-gate 
30337c478bd9Sstevel@tonic-gate 	if (path == NULL) {
30347c478bd9Sstevel@tonic-gate 		/*
30357c478bd9Sstevel@tonic-gate 		 * Call from rootconf().
30367c478bd9Sstevel@tonic-gate 		 */
30377c478bd9Sstevel@tonic-gate 		zone_hold(global_zone);
30387c478bd9Sstevel@tonic-gate 		return (global_zone);
30397c478bd9Sstevel@tonic-gate 	}
30407c478bd9Sstevel@tonic-gate 	ASSERT(*path == '/');
30417c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
30427c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
30437c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
30447c478bd9Sstevel@tonic-gate 		if (ZONE_PATH_VISIBLE(path, zone))
30457c478bd9Sstevel@tonic-gate 			zret = zone;
30467c478bd9Sstevel@tonic-gate 	}
30477c478bd9Sstevel@tonic-gate 	ASSERT(zret != NULL);
30487c478bd9Sstevel@tonic-gate 	status = zone_status_get(zret);
30497c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
30507c478bd9Sstevel@tonic-gate 		/*
30517c478bd9Sstevel@tonic-gate 		 * Zone practically doesn't exist.
30527c478bd9Sstevel@tonic-gate 		 */
30537c478bd9Sstevel@tonic-gate 		zret = global_zone;
30547c478bd9Sstevel@tonic-gate 	}
30557c478bd9Sstevel@tonic-gate 	zone_hold(zret);
30567c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
30577c478bd9Sstevel@tonic-gate 	return (zret);
30587c478bd9Sstevel@tonic-gate }
30597c478bd9Sstevel@tonic-gate 
30607c478bd9Sstevel@tonic-gate /*
3061*81d43577SJerry Jelinek  * Public interface for updating per-zone load averages.  Called once per
3062*81d43577SJerry Jelinek  * second.
3063*81d43577SJerry Jelinek  *
3064*81d43577SJerry Jelinek  * Based on loadavg_update(), genloadavg() and calcloadavg() from clock.c.
3065*81d43577SJerry Jelinek  */
3066*81d43577SJerry Jelinek void
3067*81d43577SJerry Jelinek zone_loadavg_update()
3068*81d43577SJerry Jelinek {
3069*81d43577SJerry Jelinek 	zone_t *zp;
3070*81d43577SJerry Jelinek 	zone_status_t status;
3071*81d43577SJerry Jelinek 	struct loadavg_s *lavg;
3072*81d43577SJerry Jelinek 	hrtime_t zone_total;
3073*81d43577SJerry Jelinek 	int i;
3074*81d43577SJerry Jelinek 	hrtime_t hr_avg;
3075*81d43577SJerry Jelinek 	int nrun;
3076*81d43577SJerry Jelinek 	static int64_t f[3] = { 135, 27, 9 };
3077*81d43577SJerry Jelinek 	int64_t q, r;
3078*81d43577SJerry Jelinek 
3079*81d43577SJerry Jelinek 	mutex_enter(&zonehash_lock);
3080*81d43577SJerry Jelinek 	for (zp = list_head(&zone_active); zp != NULL;
3081*81d43577SJerry Jelinek 	    zp = list_next(&zone_active, zp)) {
3082*81d43577SJerry Jelinek 		mutex_enter(&zp->zone_lock);
3083*81d43577SJerry Jelinek 
3084*81d43577SJerry Jelinek 		/* Skip zones that are on the way down or not yet up */
3085*81d43577SJerry Jelinek 		status = zone_status_get(zp);
3086*81d43577SJerry Jelinek 		if (status < ZONE_IS_READY || status >= ZONE_IS_DOWN) {
3087*81d43577SJerry Jelinek 			/* For all practical purposes the zone doesn't exist. */
3088*81d43577SJerry Jelinek 			mutex_exit(&zp->zone_lock);
3089*81d43577SJerry Jelinek 			continue;
3090*81d43577SJerry Jelinek 		}
3091*81d43577SJerry Jelinek 
3092*81d43577SJerry Jelinek 		/*
3093*81d43577SJerry Jelinek 		 * Update the 10 second moving average data in zone_loadavg.
3094*81d43577SJerry Jelinek 		 */
3095*81d43577SJerry Jelinek 		lavg = &zp->zone_loadavg;
3096*81d43577SJerry Jelinek 
3097*81d43577SJerry Jelinek 		zone_total = zp->zone_utime + zp->zone_stime + zp->zone_wtime;
3098*81d43577SJerry Jelinek 		scalehrtime(&zone_total);
3099*81d43577SJerry Jelinek 
3100*81d43577SJerry Jelinek 		/* The zone_total should always be increasing. */
3101*81d43577SJerry Jelinek 		lavg->lg_loads[lavg->lg_cur] = (zone_total > lavg->lg_total) ?
3102*81d43577SJerry Jelinek 		    zone_total - lavg->lg_total : 0;
3103*81d43577SJerry Jelinek 		lavg->lg_cur = (lavg->lg_cur + 1) % S_LOADAVG_SZ;
3104*81d43577SJerry Jelinek 		/* lg_total holds the prev. 1 sec. total */
3105*81d43577SJerry Jelinek 		lavg->lg_total = zone_total;
3106*81d43577SJerry Jelinek 
3107*81d43577SJerry Jelinek 		/*
3108*81d43577SJerry Jelinek 		 * To simplify the calculation, we don't calculate the load avg.
3109*81d43577SJerry Jelinek 		 * until the zone has been up for at least 10 seconds and our
3110*81d43577SJerry Jelinek 		 * moving average is thus full.
3111*81d43577SJerry Jelinek 		 */
3112*81d43577SJerry Jelinek 		if ((lavg->lg_len + 1) < S_LOADAVG_SZ) {
3113*81d43577SJerry Jelinek 			lavg->lg_len++;
3114*81d43577SJerry Jelinek 			mutex_exit(&zp->zone_lock);
3115*81d43577SJerry Jelinek 			continue;
3116*81d43577SJerry Jelinek 		}
3117*81d43577SJerry Jelinek 
3118*81d43577SJerry Jelinek 		/* Now calculate the 1min, 5min, 15 min load avg. */
3119*81d43577SJerry Jelinek 		hr_avg = 0;
3120*81d43577SJerry Jelinek 		for (i = 0; i < S_LOADAVG_SZ; i++)
3121*81d43577SJerry Jelinek 			hr_avg += lavg->lg_loads[i];
3122*81d43577SJerry Jelinek 		hr_avg = hr_avg / S_LOADAVG_SZ;
3123*81d43577SJerry Jelinek 		nrun = hr_avg / (NANOSEC / LGRP_LOADAVG_IN_THREAD_MAX);
3124*81d43577SJerry Jelinek 
3125*81d43577SJerry Jelinek 		/* Compute load avg. See comment in calcloadavg() */
3126*81d43577SJerry Jelinek 		for (i = 0; i < 3; i++) {
3127*81d43577SJerry Jelinek 			q = (zp->zone_hp_avenrun[i] >> 16) << 7;
3128*81d43577SJerry Jelinek 			r = (zp->zone_hp_avenrun[i] & 0xffff) << 7;
3129*81d43577SJerry Jelinek 			zp->zone_hp_avenrun[i] +=
3130*81d43577SJerry Jelinek 			    ((nrun - q) * f[i] - ((r * f[i]) >> 16)) >> 4;
3131*81d43577SJerry Jelinek 
3132*81d43577SJerry Jelinek 			/* avenrun[] can only hold 31 bits of load avg. */
3133*81d43577SJerry Jelinek 			if (zp->zone_hp_avenrun[i] <
3134*81d43577SJerry Jelinek 			    ((uint64_t)1<<(31+16-FSHIFT)))
3135*81d43577SJerry Jelinek 				zp->zone_avenrun[i] = (int32_t)
3136*81d43577SJerry Jelinek 				    (zp->zone_hp_avenrun[i] >> (16 - FSHIFT));
3137*81d43577SJerry Jelinek 			else
3138*81d43577SJerry Jelinek 				zp->zone_avenrun[i] = 0x7fffffff;
3139*81d43577SJerry Jelinek 		}
3140*81d43577SJerry Jelinek 
3141*81d43577SJerry Jelinek 		mutex_exit(&zp->zone_lock);
3142*81d43577SJerry Jelinek 	}
3143*81d43577SJerry Jelinek 	mutex_exit(&zonehash_lock);
3144*81d43577SJerry Jelinek }
3145*81d43577SJerry Jelinek 
3146*81d43577SJerry Jelinek /*
31477c478bd9Sstevel@tonic-gate  * Get the number of cpus visible to this zone.  The system-wide global
31487c478bd9Sstevel@tonic-gate  * 'ncpus' is returned if pools are disabled, the caller is in the
31497c478bd9Sstevel@tonic-gate  * global zone, or a NULL zone argument is passed in.
31507c478bd9Sstevel@tonic-gate  */
31517c478bd9Sstevel@tonic-gate int
31527c478bd9Sstevel@tonic-gate zone_ncpus_get(zone_t *zone)
31537c478bd9Sstevel@tonic-gate {
31547c478bd9Sstevel@tonic-gate 	int myncpus = zone == NULL ? 0 : zone->zone_ncpus;
31557c478bd9Sstevel@tonic-gate 
31567c478bd9Sstevel@tonic-gate 	return (myncpus != 0 ? myncpus : ncpus);
31577c478bd9Sstevel@tonic-gate }
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate /*
31607c478bd9Sstevel@tonic-gate  * Get the number of online cpus visible to this zone.  The system-wide
31617c478bd9Sstevel@tonic-gate  * global 'ncpus_online' is returned if pools are disabled, the caller
31627c478bd9Sstevel@tonic-gate  * is in the global zone, or a NULL zone argument is passed in.
31637c478bd9Sstevel@tonic-gate  */
31647c478bd9Sstevel@tonic-gate int
31657c478bd9Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone)
31667c478bd9Sstevel@tonic-gate {
31677c478bd9Sstevel@tonic-gate 	int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online;
31687c478bd9Sstevel@tonic-gate 
31697c478bd9Sstevel@tonic-gate 	return (myncpus_online != 0 ? myncpus_online : ncpus_online);
31707c478bd9Sstevel@tonic-gate }
31717c478bd9Sstevel@tonic-gate 
31727c478bd9Sstevel@tonic-gate /*
31737c478bd9Sstevel@tonic-gate  * Return the pool to which the zone is currently bound.
31747c478bd9Sstevel@tonic-gate  */
31757c478bd9Sstevel@tonic-gate pool_t *
31767c478bd9Sstevel@tonic-gate zone_pool_get(zone_t *zone)
31777c478bd9Sstevel@tonic-gate {
31787c478bd9Sstevel@tonic-gate 	ASSERT(pool_lock_held());
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate 	return (zone->zone_pool);
31817c478bd9Sstevel@tonic-gate }
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate /*
31847c478bd9Sstevel@tonic-gate  * Set the zone's pool pointer and update the zone's visibility to match
31857c478bd9Sstevel@tonic-gate  * the resources in the new pool.
31867c478bd9Sstevel@tonic-gate  */
31877c478bd9Sstevel@tonic-gate void
31887c478bd9Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool)
31897c478bd9Sstevel@tonic-gate {
31907c478bd9Sstevel@tonic-gate 	ASSERT(pool_lock_held());
31917c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate 	zone->zone_pool = pool;
31947c478bd9Sstevel@tonic-gate 	zone_pset_set(zone, pool->pool_pset->pset_id);
31957c478bd9Sstevel@tonic-gate }
31967c478bd9Sstevel@tonic-gate 
31977c478bd9Sstevel@tonic-gate /*
31987c478bd9Sstevel@tonic-gate  * Return the cached value of the id of the processor set to which the
31997c478bd9Sstevel@tonic-gate  * zone is currently bound.  The value will be ZONE_PS_INVAL if the pools
32007c478bd9Sstevel@tonic-gate  * facility is disabled.
32017c478bd9Sstevel@tonic-gate  */
32027c478bd9Sstevel@tonic-gate psetid_t
32037c478bd9Sstevel@tonic-gate zone_pset_get(zone_t *zone)
32047c478bd9Sstevel@tonic-gate {
32057c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
32067c478bd9Sstevel@tonic-gate 
32077c478bd9Sstevel@tonic-gate 	return (zone->zone_psetid);
32087c478bd9Sstevel@tonic-gate }
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate /*
32117c478bd9Sstevel@tonic-gate  * Set the cached value of the id of the processor set to which the zone
32127c478bd9Sstevel@tonic-gate  * is currently bound.  Also update the zone's visibility to match the
32137c478bd9Sstevel@tonic-gate  * resources in the new processor set.
32147c478bd9Sstevel@tonic-gate  */
32157c478bd9Sstevel@tonic-gate void
32167c478bd9Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid)
32177c478bd9Sstevel@tonic-gate {
32187c478bd9Sstevel@tonic-gate 	psetid_t oldpsetid;
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
32217c478bd9Sstevel@tonic-gate 	oldpsetid = zone_pset_get(zone);
32227c478bd9Sstevel@tonic-gate 
32237c478bd9Sstevel@tonic-gate 	if (oldpsetid == newpsetid)
32247c478bd9Sstevel@tonic-gate 		return;
32257c478bd9Sstevel@tonic-gate 	/*
32267c478bd9Sstevel@tonic-gate 	 * Global zone sees all.
32277c478bd9Sstevel@tonic-gate 	 */
32287c478bd9Sstevel@tonic-gate 	if (zone != global_zone) {
32297c478bd9Sstevel@tonic-gate 		zone->zone_psetid = newpsetid;
32307c478bd9Sstevel@tonic-gate 		if (newpsetid != ZONE_PS_INVAL)
32317c478bd9Sstevel@tonic-gate 			pool_pset_visibility_add(newpsetid, zone);
32327c478bd9Sstevel@tonic-gate 		if (oldpsetid != ZONE_PS_INVAL)
32337c478bd9Sstevel@tonic-gate 			pool_pset_visibility_remove(oldpsetid, zone);
32347c478bd9Sstevel@tonic-gate 	}
32357c478bd9Sstevel@tonic-gate 	/*
32367c478bd9Sstevel@tonic-gate 	 * Disabling pools, so we should start using the global values
32377c478bd9Sstevel@tonic-gate 	 * for ncpus and ncpus_online.
32387c478bd9Sstevel@tonic-gate 	 */
32397c478bd9Sstevel@tonic-gate 	if (newpsetid == ZONE_PS_INVAL) {
32407c478bd9Sstevel@tonic-gate 		zone->zone_ncpus = 0;
32417c478bd9Sstevel@tonic-gate 		zone->zone_ncpus_online = 0;
32427c478bd9Sstevel@tonic-gate 	}
32437c478bd9Sstevel@tonic-gate }
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate /*
32467c478bd9Sstevel@tonic-gate  * Walk the list of active zones and issue the provided callback for
32477c478bd9Sstevel@tonic-gate  * each of them.
32487c478bd9Sstevel@tonic-gate  *
32497c478bd9Sstevel@tonic-gate  * Caller must not be holding any locks that may be acquired under
32507c478bd9Sstevel@tonic-gate  * zonehash_lock.  See comment at the beginning of the file for a list of
32517c478bd9Sstevel@tonic-gate  * common locks and their interactions with zones.
32527c478bd9Sstevel@tonic-gate  */
32537c478bd9Sstevel@tonic-gate int
32547c478bd9Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data)
32557c478bd9Sstevel@tonic-gate {
32567c478bd9Sstevel@tonic-gate 	zone_t *zone;
32577c478bd9Sstevel@tonic-gate 	int ret = 0;
32587c478bd9Sstevel@tonic-gate 	zone_status_t status;
32597c478bd9Sstevel@tonic-gate 
32607c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
32617c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
32627c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
32637c478bd9Sstevel@tonic-gate 		/*
32647c478bd9Sstevel@tonic-gate 		 * Skip zones that shouldn't be externally visible.
32657c478bd9Sstevel@tonic-gate 		 */
32667c478bd9Sstevel@tonic-gate 		status = zone_status_get(zone);
32677c478bd9Sstevel@tonic-gate 		if (status < ZONE_IS_READY || status > ZONE_IS_DOWN)
32687c478bd9Sstevel@tonic-gate 			continue;
32697c478bd9Sstevel@tonic-gate 		/*
32707c478bd9Sstevel@tonic-gate 		 * Bail immediately if any callback invocation returns a
32717c478bd9Sstevel@tonic-gate 		 * non-zero value.
32727c478bd9Sstevel@tonic-gate 		 */
32737c478bd9Sstevel@tonic-gate 		ret = (*cb)(zone, data);
32747c478bd9Sstevel@tonic-gate 		if (ret != 0)
32757c478bd9Sstevel@tonic-gate 			break;
32767c478bd9Sstevel@tonic-gate 	}
32777c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
32787c478bd9Sstevel@tonic-gate 	return (ret);
32797c478bd9Sstevel@tonic-gate }
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate static int
32827c478bd9Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath)
32837c478bd9Sstevel@tonic-gate {
32847c478bd9Sstevel@tonic-gate 	vnode_t *vp;
32857c478bd9Sstevel@tonic-gate 	int trycount;
32867c478bd9Sstevel@tonic-gate 	int error = 0;
32877c478bd9Sstevel@tonic-gate 	char *path;
32887c478bd9Sstevel@tonic-gate 	struct pathname upn, pn;
32897c478bd9Sstevel@tonic-gate 	size_t pathlen;
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate 	if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0)
32927c478bd9Sstevel@tonic-gate 		return (error);
32937c478bd9Sstevel@tonic-gate 
32947c478bd9Sstevel@tonic-gate 	pn_alloc(&pn);
32957c478bd9Sstevel@tonic-gate 
32967c478bd9Sstevel@tonic-gate 	/* prevent infinite loop */
32977c478bd9Sstevel@tonic-gate 	trycount = 10;
32987c478bd9Sstevel@tonic-gate 	for (;;) {
32997c478bd9Sstevel@tonic-gate 		if (--trycount <= 0) {
33007c478bd9Sstevel@tonic-gate 			error = ESTALE;
33017c478bd9Sstevel@tonic-gate 			goto out;
33027c478bd9Sstevel@tonic-gate 		}
33037c478bd9Sstevel@tonic-gate 
33047c478bd9Sstevel@tonic-gate 		if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) {
33057c478bd9Sstevel@tonic-gate 			/*
33067c478bd9Sstevel@tonic-gate 			 * VOP_ACCESS() may cover 'vp' with a new
33077c478bd9Sstevel@tonic-gate 			 * filesystem, if 'vp' is an autoFS vnode.
33087c478bd9Sstevel@tonic-gate 			 * Get the new 'vp' if so.
33097c478bd9Sstevel@tonic-gate 			 */
3310da6c28aaSamw 			if ((error =
3311da6c28aaSamw 			    VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL)) == 0 &&
331225d2dc23Seh208807 			    (!vn_ismntpt(vp) ||
33137c478bd9Sstevel@tonic-gate 			    (error = traverse(&vp)) == 0)) {
33147c478bd9Sstevel@tonic-gate 				pathlen = pn.pn_pathlen + 2;
33157c478bd9Sstevel@tonic-gate 				path = kmem_alloc(pathlen, KM_SLEEP);
33167c478bd9Sstevel@tonic-gate 				(void) strncpy(path, pn.pn_path,
33177c478bd9Sstevel@tonic-gate 				    pn.pn_pathlen + 1);
33187c478bd9Sstevel@tonic-gate 				path[pathlen - 2] = '/';
33197c478bd9Sstevel@tonic-gate 				path[pathlen - 1] = '\0';
33207c478bd9Sstevel@tonic-gate 				pn_free(&pn);
33217c478bd9Sstevel@tonic-gate 				pn_free(&upn);
33227c478bd9Sstevel@tonic-gate 
33237c478bd9Sstevel@tonic-gate 				/* Success! */
33247c478bd9Sstevel@tonic-gate 				break;
33257c478bd9Sstevel@tonic-gate 			}
33267c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
33277c478bd9Sstevel@tonic-gate 		}
33287c478bd9Sstevel@tonic-gate 		if (error != ESTALE)
33297c478bd9Sstevel@tonic-gate 			goto out;
33307c478bd9Sstevel@tonic-gate 	}
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate 	ASSERT(error == 0);
33337c478bd9Sstevel@tonic-gate 	zone->zone_rootvp = vp;		/* we hold a reference to vp */
33347c478bd9Sstevel@tonic-gate 	zone->zone_rootpath = path;
33357c478bd9Sstevel@tonic-gate 	zone->zone_rootpathlen = pathlen;
333648451833Scarlsonj 	if (pathlen > 5 && strcmp(path + pathlen - 5, "/lu/") == 0)
333748451833Scarlsonj 		zone->zone_flags |= ZF_IS_SCRATCH;
33387c478bd9Sstevel@tonic-gate 	return (0);
33397c478bd9Sstevel@tonic-gate 
33407c478bd9Sstevel@tonic-gate out:
33417c478bd9Sstevel@tonic-gate 	pn_free(&pn);
33427c478bd9Sstevel@tonic-gate 	pn_free(&upn);
33437c478bd9Sstevel@tonic-gate 	return (error);
33447c478bd9Sstevel@tonic-gate }
33457c478bd9Sstevel@tonic-gate 
33467c478bd9Sstevel@tonic-gate #define	isalnum(c)	(((c) >= '0' && (c) <= '9') || \
33477c478bd9Sstevel@tonic-gate 			((c) >= 'a' && (c) <= 'z') || \
33487c478bd9Sstevel@tonic-gate 			((c) >= 'A' && (c) <= 'Z'))
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate static int
33517c478bd9Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname)
33527c478bd9Sstevel@tonic-gate {
33537c478bd9Sstevel@tonic-gate 	char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
33547c478bd9Sstevel@tonic-gate 	size_t len;
33557c478bd9Sstevel@tonic-gate 	int i, err;
33567c478bd9Sstevel@tonic-gate 
33577c478bd9Sstevel@tonic-gate 	if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) {
33587c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
33597c478bd9Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
33607c478bd9Sstevel@tonic-gate 	}
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate 	/* must be less than ZONENAME_MAX */
33637c478bd9Sstevel@tonic-gate 	if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') {
33647c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
33657c478bd9Sstevel@tonic-gate 		return (EINVAL);
33667c478bd9Sstevel@tonic-gate 	}
33677c478bd9Sstevel@tonic-gate 
33687c478bd9Sstevel@tonic-gate 	/*
33697c478bd9Sstevel@tonic-gate 	 * Name must start with an alphanumeric and must contain only
33707c478bd9Sstevel@tonic-gate 	 * alphanumerics, '-', '_' and '.'.
33717c478bd9Sstevel@tonic-gate 	 */
33727c478bd9Sstevel@tonic-gate 	if (!isalnum(kname[0])) {
33737c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
33747c478bd9Sstevel@tonic-gate 		return (EINVAL);
33757c478bd9Sstevel@tonic-gate 	}
33767c478bd9Sstevel@tonic-gate 	for (i = 1; i < len - 1; i++) {
33777c478bd9Sstevel@tonic-gate 		if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' &&
33787c478bd9Sstevel@tonic-gate 		    kname[i] != '.') {
33797c478bd9Sstevel@tonic-gate 			kmem_free(kname, ZONENAME_MAX);
33807c478bd9Sstevel@tonic-gate 			return (EINVAL);
33817c478bd9Sstevel@tonic-gate 		}
33827c478bd9Sstevel@tonic-gate 	}
33837c478bd9Sstevel@tonic-gate 
33847c478bd9Sstevel@tonic-gate 	zone->zone_name = kname;
33857c478bd9Sstevel@tonic-gate 	return (0);
33867c478bd9Sstevel@tonic-gate }
33877c478bd9Sstevel@tonic-gate 
33887c478bd9Sstevel@tonic-gate /*
33895679c89fSjv227347  * Gets the 32-bit hostid of the specified zone as an unsigned int.  If 'zonep'
33905679c89fSjv227347  * is NULL or it points to a zone with no hostid emulation, then the machine's
33915679c89fSjv227347  * hostid (i.e., the global zone's hostid) is returned.  This function returns
33925679c89fSjv227347  * zero if neither the zone nor the host machine (global zone) have hostids.  It
33935679c89fSjv227347  * returns HW_INVALID_HOSTID if the function attempts to return the machine's
33945679c89fSjv227347  * hostid and the machine's hostid is invalid.
33955679c89fSjv227347  */
33965679c89fSjv227347 uint32_t
33975679c89fSjv227347 zone_get_hostid(zone_t *zonep)
33985679c89fSjv227347 {
33995679c89fSjv227347 	unsigned long machine_hostid;
34005679c89fSjv227347 
34015679c89fSjv227347 	if (zonep == NULL || zonep->zone_hostid == HW_INVALID_HOSTID) {
34025679c89fSjv227347 		if (ddi_strtoul(hw_serial, NULL, 10, &machine_hostid) != 0)
34035679c89fSjv227347 			return (HW_INVALID_HOSTID);
34045679c89fSjv227347 		return ((uint32_t)machine_hostid);
34055679c89fSjv227347 	}
34065679c89fSjv227347 	return (zonep->zone_hostid);
34075679c89fSjv227347 }
34085679c89fSjv227347 
34095679c89fSjv227347 /*
34107c478bd9Sstevel@tonic-gate  * Similar to thread_create(), but makes sure the thread is in the appropriate
34117c478bd9Sstevel@tonic-gate  * zone's zsched process (curproc->p_zone->zone_zsched) before returning.
34127c478bd9Sstevel@tonic-gate  */
34137c478bd9Sstevel@tonic-gate /*ARGSUSED*/
34147c478bd9Sstevel@tonic-gate kthread_t *
34157c478bd9Sstevel@tonic-gate zthread_create(
34167c478bd9Sstevel@tonic-gate     caddr_t stk,
34177c478bd9Sstevel@tonic-gate     size_t stksize,
34187c478bd9Sstevel@tonic-gate     void (*proc)(),
34197c478bd9Sstevel@tonic-gate     void *arg,
34207c478bd9Sstevel@tonic-gate     size_t len,
34217c478bd9Sstevel@tonic-gate     pri_t pri)
34227c478bd9Sstevel@tonic-gate {
34237c478bd9Sstevel@tonic-gate 	kthread_t *t;
34247c478bd9Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
34257c478bd9Sstevel@tonic-gate 	proc_t *pp = zone->zone_zsched;
34267c478bd9Sstevel@tonic-gate 
34277c478bd9Sstevel@tonic-gate 	zone_hold(zone);	/* Reference to be dropped when thread exits */
34287c478bd9Sstevel@tonic-gate 
34297c478bd9Sstevel@tonic-gate 	/*
34307c478bd9Sstevel@tonic-gate 	 * No-one should be trying to create threads if the zone is shutting
34317c478bd9Sstevel@tonic-gate 	 * down and there aren't any kernel threads around.  See comment
34327c478bd9Sstevel@tonic-gate 	 * in zthread_exit().
34337c478bd9Sstevel@tonic-gate 	 */
34347c478bd9Sstevel@tonic-gate 	ASSERT(!(zone->zone_kthreads == NULL &&
34357c478bd9Sstevel@tonic-gate 	    zone_status_get(zone) >= ZONE_IS_EMPTY));
34367c478bd9Sstevel@tonic-gate 	/*
34377c478bd9Sstevel@tonic-gate 	 * Create a thread, but don't let it run until we've finished setting
34387c478bd9Sstevel@tonic-gate 	 * things up.
34397c478bd9Sstevel@tonic-gate 	 */
34407c478bd9Sstevel@tonic-gate 	t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri);
34417c478bd9Sstevel@tonic-gate 	ASSERT(t->t_forw == NULL);
34427c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
34437c478bd9Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL) {
34447c478bd9Sstevel@tonic-gate 		t->t_forw = t->t_back = t;
34457c478bd9Sstevel@tonic-gate 	} else {
34467c478bd9Sstevel@tonic-gate 		kthread_t *tx = zone->zone_kthreads;
34477c478bd9Sstevel@tonic-gate 
34487c478bd9Sstevel@tonic-gate 		t->t_forw = tx;
34497c478bd9Sstevel@tonic-gate 		t->t_back = tx->t_back;
34507c478bd9Sstevel@tonic-gate 		tx->t_back->t_forw = t;
34517c478bd9Sstevel@tonic-gate 		tx->t_back = t;
34527c478bd9Sstevel@tonic-gate 	}
34537c478bd9Sstevel@tonic-gate 	zone->zone_kthreads = t;
34547c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
34557c478bd9Sstevel@tonic-gate 
34567c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
34577c478bd9Sstevel@tonic-gate 	t->t_proc_flag |= TP_ZTHREAD;
34587c478bd9Sstevel@tonic-gate 	project_rele(t->t_proj);
34597c478bd9Sstevel@tonic-gate 	t->t_proj = project_hold(pp->p_task->tk_proj);
34607c478bd9Sstevel@tonic-gate 
34617c478bd9Sstevel@tonic-gate 	/*
34627c478bd9Sstevel@tonic-gate 	 * Setup complete, let it run.
34637c478bd9Sstevel@tonic-gate 	 */
34647c478bd9Sstevel@tonic-gate 	thread_lock(t);
34657c478bd9Sstevel@tonic-gate 	t->t_schedflag |= TS_ALLSTART;
34667c478bd9Sstevel@tonic-gate 	setrun_locked(t);
34677c478bd9Sstevel@tonic-gate 	thread_unlock(t);
34687c478bd9Sstevel@tonic-gate 
34697c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
34707c478bd9Sstevel@tonic-gate 
34717c478bd9Sstevel@tonic-gate 	return (t);
34727c478bd9Sstevel@tonic-gate }
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate /*
34757c478bd9Sstevel@tonic-gate  * Similar to thread_exit().  Must be called by threads created via
34767c478bd9Sstevel@tonic-gate  * zthread_exit().
34777c478bd9Sstevel@tonic-gate  */
34787c478bd9Sstevel@tonic-gate void
34797c478bd9Sstevel@tonic-gate zthread_exit(void)
34807c478bd9Sstevel@tonic-gate {
34817c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
34827c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
34837c478bd9Sstevel@tonic-gate 	zone_t *zone = pp->p_zone;
34847c478bd9Sstevel@tonic-gate 
34857c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
34867c478bd9Sstevel@tonic-gate 
34877c478bd9Sstevel@tonic-gate 	/*
34887c478bd9Sstevel@tonic-gate 	 * Reparent to p0
34897c478bd9Sstevel@tonic-gate 	 */
3490b4b07f87Sjosephb 	kpreempt_disable();
34917c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
34927c478bd9Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_ZTHREAD;
34937c478bd9Sstevel@tonic-gate 	t->t_procp = &p0;
34947c478bd9Sstevel@tonic-gate 	hat_thread_exit(t);
34957c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
3496b4b07f87Sjosephb 	kpreempt_enable();
34977c478bd9Sstevel@tonic-gate 
34987c478bd9Sstevel@tonic-gate 	if (t->t_back == t) {
34997c478bd9Sstevel@tonic-gate 		ASSERT(t->t_forw == t);
35007c478bd9Sstevel@tonic-gate 		/*
35017c478bd9Sstevel@tonic-gate 		 * If the zone is empty, once the thread count
35027c478bd9Sstevel@tonic-gate 		 * goes to zero no further kernel threads can be
35037c478bd9Sstevel@tonic-gate 		 * created.  This is because if the creator is a process
35047c478bd9Sstevel@tonic-gate 		 * in the zone, then it must have exited before the zone
35057c478bd9Sstevel@tonic-gate 		 * state could be set to ZONE_IS_EMPTY.
35067c478bd9Sstevel@tonic-gate 		 * Otherwise, if the creator is a kernel thread in the
35077c478bd9Sstevel@tonic-gate 		 * zone, the thread count is non-zero.
35087c478bd9Sstevel@tonic-gate 		 *
35097c478bd9Sstevel@tonic-gate 		 * This really means that non-zone kernel threads should
35107c478bd9Sstevel@tonic-gate 		 * not create zone kernel threads.
35117c478bd9Sstevel@tonic-gate 		 */
35127c478bd9Sstevel@tonic-gate 		zone->zone_kthreads = NULL;
35137c478bd9Sstevel@tonic-gate 		if (zone_status_get(zone) == ZONE_IS_EMPTY) {
35147c478bd9Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_DOWN);
3515c97ad5cdSakolb 			/*
3516c97ad5cdSakolb 			 * Remove any CPU caps on this zone.
3517c97ad5cdSakolb 			 */
3518c97ad5cdSakolb 			cpucaps_zone_remove(zone);
35197c478bd9Sstevel@tonic-gate 		}
35207c478bd9Sstevel@tonic-gate 	} else {
35217c478bd9Sstevel@tonic-gate 		t->t_forw->t_back = t->t_back;
35227c478bd9Sstevel@tonic-gate 		t->t_back->t_forw = t->t_forw;
35237c478bd9Sstevel@tonic-gate 		if (zone->zone_kthreads == t)
35247c478bd9Sstevel@tonic-gate 			zone->zone_kthreads = t->t_forw;
35257c478bd9Sstevel@tonic-gate 	}
35267c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
35277c478bd9Sstevel@tonic-gate 	zone_rele(zone);
35287c478bd9Sstevel@tonic-gate 	thread_exit();
35297c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
35307c478bd9Sstevel@tonic-gate }
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate static void
35337c478bd9Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp)
35347c478bd9Sstevel@tonic-gate {
35357c478bd9Sstevel@tonic-gate 	vnode_t *oldvp;
35367c478bd9Sstevel@tonic-gate 
35377c478bd9Sstevel@tonic-gate 	/* we're going to hold a reference here to the directory */
35387c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
35397c478bd9Sstevel@tonic-gate 
3540005d3febSMarek Pospisil 	/* update abs cwd/root path see c2/audit.c */
3541005d3febSMarek Pospisil 	if (AU_AUDITING())
35427c478bd9Sstevel@tonic-gate 		audit_chdirec(vp, vpp);
35437c478bd9Sstevel@tonic-gate 
35447c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
35457c478bd9Sstevel@tonic-gate 	oldvp = *vpp;
35467c478bd9Sstevel@tonic-gate 	*vpp = vp;
35477c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
35487c478bd9Sstevel@tonic-gate 	if (oldvp != NULL)
35497c478bd9Sstevel@tonic-gate 		VN_RELE(oldvp);
35507c478bd9Sstevel@tonic-gate }
35517c478bd9Sstevel@tonic-gate 
35527c478bd9Sstevel@tonic-gate /*
35537c478bd9Sstevel@tonic-gate  * Convert an rctl value represented by an nvlist_t into an rctl_val_t.
35547c478bd9Sstevel@tonic-gate  */
35557c478bd9Sstevel@tonic-gate static int
35567c478bd9Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv)
35577c478bd9Sstevel@tonic-gate {
35587c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
35597c478bd9Sstevel@tonic-gate 	boolean_t priv_set = B_FALSE;
35607c478bd9Sstevel@tonic-gate 	boolean_t limit_set = B_FALSE;
35617c478bd9Sstevel@tonic-gate 	boolean_t action_set = B_FALSE;
35627c478bd9Sstevel@tonic-gate 
35637c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
35647c478bd9Sstevel@tonic-gate 		const char *name;
35657c478bd9Sstevel@tonic-gate 		uint64_t ui64;
35667c478bd9Sstevel@tonic-gate 
35677c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
35687c478bd9Sstevel@tonic-gate 		if (nvpair_type(nvp) != DATA_TYPE_UINT64)
35697c478bd9Sstevel@tonic-gate 			return (EINVAL);
35707c478bd9Sstevel@tonic-gate 		(void) nvpair_value_uint64(nvp, &ui64);
35717c478bd9Sstevel@tonic-gate 		if (strcmp(name, "privilege") == 0) {
35727c478bd9Sstevel@tonic-gate 			/*
35737c478bd9Sstevel@tonic-gate 			 * Currently only privileged values are allowed, but
35747c478bd9Sstevel@tonic-gate 			 * this may change in the future.
35757c478bd9Sstevel@tonic-gate 			 */
35767c478bd9Sstevel@tonic-gate 			if (ui64 != RCPRIV_PRIVILEGED)
35777c478bd9Sstevel@tonic-gate 				return (EINVAL);
35787c478bd9Sstevel@tonic-gate 			rv->rcv_privilege = ui64;
35797c478bd9Sstevel@tonic-gate 			priv_set = B_TRUE;
35807c478bd9Sstevel@tonic-gate 		} else if (strcmp(name, "limit") == 0) {
35817c478bd9Sstevel@tonic-gate 			rv->rcv_value = ui64;
35827c478bd9Sstevel@tonic-gate 			limit_set = B_TRUE;
35837c478bd9Sstevel@tonic-gate 		} else if (strcmp(name, "action") == 0) {
35847c478bd9Sstevel@tonic-gate 			if (ui64 != RCTL_LOCAL_NOACTION &&
35857c478bd9Sstevel@tonic-gate 			    ui64 != RCTL_LOCAL_DENY)
35867c478bd9Sstevel@tonic-gate 				return (EINVAL);
35877c478bd9Sstevel@tonic-gate 			rv->rcv_flagaction = ui64;
35887c478bd9Sstevel@tonic-gate 			action_set = B_TRUE;
35897c478bd9Sstevel@tonic-gate 		} else {
35907c478bd9Sstevel@tonic-gate 			return (EINVAL);
35917c478bd9Sstevel@tonic-gate 		}
35927c478bd9Sstevel@tonic-gate 	}
35937c478bd9Sstevel@tonic-gate 
35947c478bd9Sstevel@tonic-gate 	if (!(priv_set && limit_set && action_set))
35957c478bd9Sstevel@tonic-gate 		return (EINVAL);
35967c478bd9Sstevel@tonic-gate 	rv->rcv_action_signal = 0;
35977c478bd9Sstevel@tonic-gate 	rv->rcv_action_recipient = NULL;
35987c478bd9Sstevel@tonic-gate 	rv->rcv_action_recip_pid = -1;
35997c478bd9Sstevel@tonic-gate 	rv->rcv_firing_time = 0;
36007c478bd9Sstevel@tonic-gate 
36017c478bd9Sstevel@tonic-gate 	return (0);
36027c478bd9Sstevel@tonic-gate }
36037c478bd9Sstevel@tonic-gate 
36043f2f09c1Sdp /*
36053f2f09c1Sdp  * Non-global zone version of start_init.
36063f2f09c1Sdp  */
36077c478bd9Sstevel@tonic-gate void
36083f2f09c1Sdp zone_start_init(void)
36097c478bd9Sstevel@tonic-gate {
36107c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
36119acbbeafSnn35248 	zone_t *z = p->p_zone;
36123f2f09c1Sdp 
36133f2f09c1Sdp 	ASSERT(!INGLOBALZONE(curproc));
36147c478bd9Sstevel@tonic-gate 
36157c478bd9Sstevel@tonic-gate 	/*
36169acbbeafSnn35248 	 * For all purposes (ZONE_ATTR_INITPID and restart_init),
36179acbbeafSnn35248 	 * storing just the pid of init is sufficient.
36189acbbeafSnn35248 	 */
36199acbbeafSnn35248 	z->zone_proc_initpid = p->p_pid;
36209acbbeafSnn35248 
36219acbbeafSnn35248 	/*
36223f2f09c1Sdp 	 * We maintain zone_boot_err so that we can return the cause of the
36233f2f09c1Sdp 	 * failure back to the caller of the zone_boot syscall.
36247c478bd9Sstevel@tonic-gate 	 */
36253f2f09c1Sdp 	p->p_zone->zone_boot_err = start_init_common();
36267c478bd9Sstevel@tonic-gate 
36278f983ab3Sjv227347 	/*
36288f983ab3Sjv227347 	 * We will prevent booting zones from becoming running zones if the
36298f983ab3Sjv227347 	 * global zone is shutting down.
36308f983ab3Sjv227347 	 */
36317c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
36328f983ab3Sjv227347 	if (z->zone_boot_err != 0 || zone_status_get(global_zone) >=
36338f983ab3Sjv227347 	    ZONE_IS_SHUTTING_DOWN) {
36347c478bd9Sstevel@tonic-gate 		/*
36357c478bd9Sstevel@tonic-gate 		 * Make sure we are still in the booting state-- we could have
36367c478bd9Sstevel@tonic-gate 		 * raced and already be shutting down, or even further along.
36377c478bd9Sstevel@tonic-gate 		 */
3638c97ad5cdSakolb 		if (zone_status_get(z) == ZONE_IS_BOOTING) {
36399acbbeafSnn35248 			zone_status_set(z, ZONE_IS_SHUTTING_DOWN);
3640c97ad5cdSakolb 		}
36417c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
36427c478bd9Sstevel@tonic-gate 		/* It's gone bad, dispose of the process */
36439acbbeafSnn35248 		if (proc_exit(CLD_EXITED, z->zone_boot_err) != 0) {
364497eda132Sraf 			mutex_enter(&p->p_lock);
364597eda132Sraf 			ASSERT(p->p_flag & SEXITLWPS);
36467c478bd9Sstevel@tonic-gate 			lwp_exit();
36477c478bd9Sstevel@tonic-gate 		}
36487c478bd9Sstevel@tonic-gate 	} else {
36499acbbeafSnn35248 		if (zone_status_get(z) == ZONE_IS_BOOTING)
36509acbbeafSnn35248 			zone_status_set(z, ZONE_IS_RUNNING);
36517c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
36527c478bd9Sstevel@tonic-gate 		/* cause the process to return to userland. */
36537c478bd9Sstevel@tonic-gate 		lwp_rtt();
36547c478bd9Sstevel@tonic-gate 	}
36557c478bd9Sstevel@tonic-gate }
36567c478bd9Sstevel@tonic-gate 
36577c478bd9Sstevel@tonic-gate struct zsched_arg {
36587c478bd9Sstevel@tonic-gate 	zone_t *zone;
36597c478bd9Sstevel@tonic-gate 	nvlist_t *nvlist;
36607c478bd9Sstevel@tonic-gate };
36617c478bd9Sstevel@tonic-gate 
36627c478bd9Sstevel@tonic-gate /*
36637c478bd9Sstevel@tonic-gate  * Per-zone "sched" workalike.  The similarity to "sched" doesn't have
36647c478bd9Sstevel@tonic-gate  * anything to do with scheduling, but rather with the fact that
36657c478bd9Sstevel@tonic-gate  * per-zone kernel threads are parented to zsched, just like regular
36667c478bd9Sstevel@tonic-gate  * kernel threads are parented to sched (p0).
36677c478bd9Sstevel@tonic-gate  *
36687c478bd9Sstevel@tonic-gate  * zsched is also responsible for launching init for the zone.
36697c478bd9Sstevel@tonic-gate  */
36707c478bd9Sstevel@tonic-gate static void
36717c478bd9Sstevel@tonic-gate zsched(void *arg)
36727c478bd9Sstevel@tonic-gate {
36737c478bd9Sstevel@tonic-gate 	struct zsched_arg *za = arg;
36747c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
36757c478bd9Sstevel@tonic-gate 	proc_t *initp = proc_init;
36767c478bd9Sstevel@tonic-gate 	zone_t *zone = za->zone;
36777c478bd9Sstevel@tonic-gate 	cred_t *cr, *oldcred;
36787c478bd9Sstevel@tonic-gate 	rctl_set_t *set;
36797c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
36807c478bd9Sstevel@tonic-gate 	contract_t *ct = NULL;
36817c478bd9Sstevel@tonic-gate 	task_t *tk, *oldtk;
36827c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
36837c478bd9Sstevel@tonic-gate 	kproject_t *pj;
36847c478bd9Sstevel@tonic-gate 
36857c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = za->nvlist;
36867c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
36877c478bd9Sstevel@tonic-gate 
3688ae115bc7Smrj 	bcopy("zsched", PTOU(pp)->u_psargs, sizeof ("zsched"));
3689ae115bc7Smrj 	bcopy("zsched", PTOU(pp)->u_comm, sizeof ("zsched"));
3690ae115bc7Smrj 	PTOU(pp)->u_argc = 0;
3691ae115bc7Smrj 	PTOU(pp)->u_argv = NULL;
3692ae115bc7Smrj 	PTOU(pp)->u_envp = NULL;
36937c478bd9Sstevel@tonic-gate 	closeall(P_FINFO(pp));
36947c478bd9Sstevel@tonic-gate 
36957c478bd9Sstevel@tonic-gate 	/*
36967c478bd9Sstevel@tonic-gate 	 * We are this zone's "zsched" process.  As the zone isn't generally
36977c478bd9Sstevel@tonic-gate 	 * visible yet we don't need to grab any locks before initializing its
36987c478bd9Sstevel@tonic-gate 	 * zone_proc pointer.
36997c478bd9Sstevel@tonic-gate 	 */
37007c478bd9Sstevel@tonic-gate 	zone_hold(zone);  /* this hold is released by zone_destroy() */
37017c478bd9Sstevel@tonic-gate 	zone->zone_zsched = pp;
37027c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
37037c478bd9Sstevel@tonic-gate 	pp->p_zone = zone;
37047c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
37057c478bd9Sstevel@tonic-gate 
37067c478bd9Sstevel@tonic-gate 	/*
37077c478bd9Sstevel@tonic-gate 	 * Disassociate process from its 'parent'; parent ourselves to init
37087c478bd9Sstevel@tonic-gate 	 * (pid 1) and change other values as needed.
37097c478bd9Sstevel@tonic-gate 	 */
37107c478bd9Sstevel@tonic-gate 	sess_create();
37117c478bd9Sstevel@tonic-gate 
37127c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
37137c478bd9Sstevel@tonic-gate 	proc_detach(pp);
37147c478bd9Sstevel@tonic-gate 	pp->p_ppid = 1;
37157c478bd9Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
37167c478bd9Sstevel@tonic-gate 	pp->p_ancpid = 1;
37177c478bd9Sstevel@tonic-gate 	pp->p_parent = initp;
37187c478bd9Sstevel@tonic-gate 	pp->p_psibling = NULL;
37197c478bd9Sstevel@tonic-gate 	if (initp->p_child)
37207c478bd9Sstevel@tonic-gate 		initp->p_child->p_psibling = pp;
37217c478bd9Sstevel@tonic-gate 	pp->p_sibling = initp->p_child;
37227c478bd9Sstevel@tonic-gate 	initp->p_child = pp;
37237c478bd9Sstevel@tonic-gate 
37247c478bd9Sstevel@tonic-gate 	/* Decrement what newproc() incremented. */
37257c478bd9Sstevel@tonic-gate 	upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID);
37267c478bd9Sstevel@tonic-gate 	/*
37277c478bd9Sstevel@tonic-gate 	 * Our credentials are about to become kcred-like, so we don't care
37287c478bd9Sstevel@tonic-gate 	 * about the caller's ruid.
37297c478bd9Sstevel@tonic-gate 	 */
37307c478bd9Sstevel@tonic-gate 	upcount_inc(crgetruid(kcred), zone->zone_id);
37317c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
37327c478bd9Sstevel@tonic-gate 
37337c478bd9Sstevel@tonic-gate 	/*
3734ff19e029SMenno Lageman 	 * getting out of global zone, so decrement lwp and process counts
37357c478bd9Sstevel@tonic-gate 	 */
37367c478bd9Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
37377c478bd9Sstevel@tonic-gate 	mutex_enter(&global_zone->zone_nlwps_lock);
37387c478bd9Sstevel@tonic-gate 	pj->kpj_nlwps -= pp->p_lwpcnt;
37397c478bd9Sstevel@tonic-gate 	global_zone->zone_nlwps -= pp->p_lwpcnt;
3740ff19e029SMenno Lageman 	pj->kpj_nprocs--;
3741ff19e029SMenno Lageman 	global_zone->zone_nprocs--;
37427c478bd9Sstevel@tonic-gate 	mutex_exit(&global_zone->zone_nlwps_lock);
37437c478bd9Sstevel@tonic-gate 
37447c478bd9Sstevel@tonic-gate 	/*
3745c6939658Ssl108498 	 * Decrement locked memory counts on old zone and project.
3746c6939658Ssl108498 	 */
37470209230bSgjelinek 	mutex_enter(&global_zone->zone_mem_lock);
3748c6939658Ssl108498 	global_zone->zone_locked_mem -= pp->p_locked_mem;
3749c6939658Ssl108498 	pj->kpj_data.kpd_locked_mem -= pp->p_locked_mem;
37500209230bSgjelinek 	mutex_exit(&global_zone->zone_mem_lock);
3751c6939658Ssl108498 
3752c6939658Ssl108498 	/*
37537c478bd9Sstevel@tonic-gate 	 * Create and join a new task in project '0' of this zone.
37547c478bd9Sstevel@tonic-gate 	 *
37557c478bd9Sstevel@tonic-gate 	 * We don't need to call holdlwps() since we know we're the only lwp in
37567c478bd9Sstevel@tonic-gate 	 * this process.
37577c478bd9Sstevel@tonic-gate 	 *
37587c478bd9Sstevel@tonic-gate 	 * task_join() returns with p_lock held.
37597c478bd9Sstevel@tonic-gate 	 */
37607c478bd9Sstevel@tonic-gate 	tk = task_create(0, zone);
37617c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
37627c478bd9Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
3763c6939658Ssl108498 
3764c6939658Ssl108498 	pj = pp->p_task->tk_proj;
3765c6939658Ssl108498 
37660209230bSgjelinek 	mutex_enter(&zone->zone_mem_lock);
3767c6939658Ssl108498 	zone->zone_locked_mem += pp->p_locked_mem;
3768c6939658Ssl108498 	pj->kpj_data.kpd_locked_mem += pp->p_locked_mem;
37690209230bSgjelinek 	mutex_exit(&zone->zone_mem_lock);
37707c478bd9Sstevel@tonic-gate 
37717c478bd9Sstevel@tonic-gate 	/*
3772ff19e029SMenno Lageman 	 * add lwp and process counts to zsched's zone, and increment
3773ff19e029SMenno Lageman 	 * project's task and process count due to the task created in
3774ff19e029SMenno Lageman 	 * the above task_create.
37757c478bd9Sstevel@tonic-gate 	 */
37767c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
37777c478bd9Sstevel@tonic-gate 	pj->kpj_nlwps += pp->p_lwpcnt;
37787c478bd9Sstevel@tonic-gate 	pj->kpj_ntasks += 1;
37797c478bd9Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
3780ff19e029SMenno Lageman 	pj->kpj_nprocs++;
3781ff19e029SMenno Lageman 	zone->zone_nprocs++;
37827c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
37837c478bd9Sstevel@tonic-gate 
3784c6939658Ssl108498 	mutex_exit(&curproc->p_lock);
3785c6939658Ssl108498 	mutex_exit(&cpu_lock);
3786c6939658Ssl108498 	task_rele(oldtk);
3787c6939658Ssl108498 
37887c478bd9Sstevel@tonic-gate 	/*
37897c478bd9Sstevel@tonic-gate 	 * The process was created by a process in the global zone, hence the
37907c478bd9Sstevel@tonic-gate 	 * credentials are wrong.  We might as well have kcred-ish credentials.
37917c478bd9Sstevel@tonic-gate 	 */
37927c478bd9Sstevel@tonic-gate 	cr = zone->zone_kcred;
37937c478bd9Sstevel@tonic-gate 	crhold(cr);
37947c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
37957c478bd9Sstevel@tonic-gate 	oldcred = pp->p_cred;
37967c478bd9Sstevel@tonic-gate 	pp->p_cred = cr;
37977c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
37987c478bd9Sstevel@tonic-gate 	crfree(oldcred);
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate 	/*
38017c478bd9Sstevel@tonic-gate 	 * Hold credentials again (for thread)
38027c478bd9Sstevel@tonic-gate 	 */
38037c478bd9Sstevel@tonic-gate 	crhold(cr);
38047c478bd9Sstevel@tonic-gate 
38057c478bd9Sstevel@tonic-gate 	/*
38067c478bd9Sstevel@tonic-gate 	 * p_lwpcnt can't change since this is a kernel process.
38077c478bd9Sstevel@tonic-gate 	 */
38087c478bd9Sstevel@tonic-gate 	crset(pp, cr);
38097c478bd9Sstevel@tonic-gate 
38107c478bd9Sstevel@tonic-gate 	/*
38117c478bd9Sstevel@tonic-gate 	 * Chroot
38127c478bd9Sstevel@tonic-gate 	 */
38137c478bd9Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp);
38147c478bd9Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp);
38157c478bd9Sstevel@tonic-gate 
38167c478bd9Sstevel@tonic-gate 	/*
38177c478bd9Sstevel@tonic-gate 	 * Initialize zone's rctl set.
38187c478bd9Sstevel@tonic-gate 	 */
38197c478bd9Sstevel@tonic-gate 	set = rctl_set_create();
38207c478bd9Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
38217c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
38227c478bd9Sstevel@tonic-gate 	e.rcep_p.zone = zone;
38237c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
38247c478bd9Sstevel@tonic-gate 	zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp);
38257c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
38267c478bd9Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
38277c478bd9Sstevel@tonic-gate 
38287c478bd9Sstevel@tonic-gate 	/*
38297c478bd9Sstevel@tonic-gate 	 * Apply the rctls passed in to zone_create().  This is basically a list
38307c478bd9Sstevel@tonic-gate 	 * assignment: all of the old values are removed and the new ones
38317c478bd9Sstevel@tonic-gate 	 * inserted.  That is, if an empty list is passed in, all values are
38327c478bd9Sstevel@tonic-gate 	 * removed.
38337c478bd9Sstevel@tonic-gate 	 */
38347c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
38357c478bd9Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
38367c478bd9Sstevel@tonic-gate 		rctl_hndl_t hndl;
38377c478bd9Sstevel@tonic-gate 		char *name;
38387c478bd9Sstevel@tonic-gate 		nvlist_t **nvlarray;
38397c478bd9Sstevel@tonic-gate 		uint_t i, nelem;
38407c478bd9Sstevel@tonic-gate 		int error;	/* For ASSERT()s */
38417c478bd9Sstevel@tonic-gate 
38427c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
38437c478bd9Sstevel@tonic-gate 		hndl = rctl_hndl_lookup(name);
38447c478bd9Sstevel@tonic-gate 		ASSERT(hndl != -1);
38457c478bd9Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
38467c478bd9Sstevel@tonic-gate 		ASSERT(rde != NULL);
38477c478bd9Sstevel@tonic-gate 
38487c478bd9Sstevel@tonic-gate 		for (; /* ever */; ) {
38497c478bd9Sstevel@tonic-gate 			rctl_val_t oval;
38507c478bd9Sstevel@tonic-gate 
38517c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
38527c478bd9Sstevel@tonic-gate 			error = rctl_local_get(hndl, NULL, &oval, pp);
38537c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
38547c478bd9Sstevel@tonic-gate 			ASSERT(error == 0);	/* Can't fail for RCTL_FIRST */
38557c478bd9Sstevel@tonic-gate 			ASSERT(oval.rcv_privilege != RCPRIV_BASIC);
38567c478bd9Sstevel@tonic-gate 			if (oval.rcv_privilege == RCPRIV_SYSTEM)
38577c478bd9Sstevel@tonic-gate 				break;
38587c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
38597c478bd9Sstevel@tonic-gate 			error = rctl_local_delete(hndl, &oval, pp);
38607c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
38617c478bd9Sstevel@tonic-gate 			ASSERT(error == 0);
38627c478bd9Sstevel@tonic-gate 		}
38637c478bd9Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
38647c478bd9Sstevel@tonic-gate 		ASSERT(error == 0);
38657c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
38667c478bd9Sstevel@tonic-gate 			rctl_val_t *nvalp;
38677c478bd9Sstevel@tonic-gate 
38687c478bd9Sstevel@tonic-gate 			nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
38697c478bd9Sstevel@tonic-gate 			error = nvlist2rctlval(nvlarray[i], nvalp);
38707c478bd9Sstevel@tonic-gate 			ASSERT(error == 0);
38717c478bd9Sstevel@tonic-gate 			/*
38727c478bd9Sstevel@tonic-gate 			 * rctl_local_insert can fail if the value being
38737c478bd9Sstevel@tonic-gate 			 * inserted is a duplicate; this is OK.
38747c478bd9Sstevel@tonic-gate 			 */
38757c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
38767c478bd9Sstevel@tonic-gate 			if (rctl_local_insert(hndl, nvalp, pp) != 0)
38777c478bd9Sstevel@tonic-gate 				kmem_cache_free(rctl_val_cache, nvalp);
38787c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
38797c478bd9Sstevel@tonic-gate 		}
38807c478bd9Sstevel@tonic-gate 	}
38817c478bd9Sstevel@tonic-gate 	/*
38827c478bd9Sstevel@tonic-gate 	 * Tell the world that we're done setting up.
38837c478bd9Sstevel@tonic-gate 	 *
3884bd41d0a8Snordmark 	 * At this point we want to set the zone status to ZONE_IS_INITIALIZED
38857c478bd9Sstevel@tonic-gate 	 * and atomically set the zone's processor set visibility.  Once
38867c478bd9Sstevel@tonic-gate 	 * we drop pool_lock() this zone will automatically get updated
38877c478bd9Sstevel@tonic-gate 	 * to reflect any future changes to the pools configuration.
3888bd41d0a8Snordmark 	 *
3889bd41d0a8Snordmark 	 * Note that after we drop the locks below (zonehash_lock in
3890bd41d0a8Snordmark 	 * particular) other operations such as a zone_getattr call can
3891bd41d0a8Snordmark 	 * now proceed and observe the zone. That is the reason for doing a
3892bd41d0a8Snordmark 	 * state transition to the INITIALIZED state.
38937c478bd9Sstevel@tonic-gate 	 */
38947c478bd9Sstevel@tonic-gate 	pool_lock();
38957c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
38967c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
38977c478bd9Sstevel@tonic-gate 	zone_uniqid(zone);
38987c478bd9Sstevel@tonic-gate 	zone_zsd_configure(zone);
38997c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED)
39007c478bd9Sstevel@tonic-gate 		zone_pset_set(zone, pool_default->pool_pset->pset_id);
39017c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
39027c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
3903bd41d0a8Snordmark 	zone_status_set(zone, ZONE_IS_INITIALIZED);
39047c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
39057c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
39067c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
39077c478bd9Sstevel@tonic-gate 	pool_unlock();
39087c478bd9Sstevel@tonic-gate 
3909bd41d0a8Snordmark 	/* Now call the create callback for this key */
3910bd41d0a8Snordmark 	zsd_apply_all_keys(zsd_apply_create, zone);
3911bd41d0a8Snordmark 
3912bd41d0a8Snordmark 	/* The callbacks are complete. Mark ZONE_IS_READY */
3913bd41d0a8Snordmark 	mutex_enter(&zone_status_lock);
3914bd41d0a8Snordmark 	ASSERT(zone_status_get(zone) == ZONE_IS_INITIALIZED);
3915bd41d0a8Snordmark 	zone_status_set(zone, ZONE_IS_READY);
3916bd41d0a8Snordmark 	mutex_exit(&zone_status_lock);
3917bd41d0a8Snordmark 
39187c478bd9Sstevel@tonic-gate 	/*
39197c478bd9Sstevel@tonic-gate 	 * Once we see the zone transition to the ZONE_IS_BOOTING state,
39207c478bd9Sstevel@tonic-gate 	 * we launch init, and set the state to running.
39217c478bd9Sstevel@tonic-gate 	 */
39227c478bd9Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched");
39237c478bd9Sstevel@tonic-gate 
39247c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_BOOTING) {
39257c478bd9Sstevel@tonic-gate 		id_t cid;
39267c478bd9Sstevel@tonic-gate 
39277c478bd9Sstevel@tonic-gate 		/*
39287c478bd9Sstevel@tonic-gate 		 * Ok, this is a little complicated.  We need to grab the
39297c478bd9Sstevel@tonic-gate 		 * zone's pool's scheduling class ID; note that by now, we
39307c478bd9Sstevel@tonic-gate 		 * are already bound to a pool if we need to be (zoneadmd
39317c478bd9Sstevel@tonic-gate 		 * will have done that to us while we're in the READY
39327c478bd9Sstevel@tonic-gate 		 * state).  *But* the scheduling class for the zone's 'init'
39337c478bd9Sstevel@tonic-gate 		 * must be explicitly passed to newproc, which doesn't
39347c478bd9Sstevel@tonic-gate 		 * respect pool bindings.
39357c478bd9Sstevel@tonic-gate 		 *
39367c478bd9Sstevel@tonic-gate 		 * We hold the pool_lock across the call to newproc() to
39377c478bd9Sstevel@tonic-gate 		 * close the obvious race: the pool's scheduling class
39387c478bd9Sstevel@tonic-gate 		 * could change before we manage to create the LWP with
39397c478bd9Sstevel@tonic-gate 		 * classid 'cid'.
39407c478bd9Sstevel@tonic-gate 		 */
39417c478bd9Sstevel@tonic-gate 		pool_lock();
39420209230bSgjelinek 		if (zone->zone_defaultcid > 0)
39430209230bSgjelinek 			cid = zone->zone_defaultcid;
39440209230bSgjelinek 		else
39457c478bd9Sstevel@tonic-gate 			cid = pool_get_class(zone->zone_pool);
39467c478bd9Sstevel@tonic-gate 		if (cid == -1)
39477c478bd9Sstevel@tonic-gate 			cid = defaultcid;
39487c478bd9Sstevel@tonic-gate 
39497c478bd9Sstevel@tonic-gate 		/*
39507c478bd9Sstevel@tonic-gate 		 * If this fails, zone_boot will ultimately fail.  The
39517c478bd9Sstevel@tonic-gate 		 * state of the zone will be set to SHUTTING_DOWN-- userland
39527c478bd9Sstevel@tonic-gate 		 * will have to tear down the zone, and fail, or try again.
39537c478bd9Sstevel@tonic-gate 		 */
39543f2f09c1Sdp 		if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid,
395535a5a358SJonathan Adams 		    minclsyspri - 1, &ct, 0)) != 0) {
39567c478bd9Sstevel@tonic-gate 			mutex_enter(&zone_status_lock);
39577c478bd9Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
39587c478bd9Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
395986ad481cSJerry Jelinek 		} else {
396086ad481cSJerry Jelinek 			zone->zone_boot_time = gethrestime_sec();
39617c478bd9Sstevel@tonic-gate 		}
396286ad481cSJerry Jelinek 
39637c478bd9Sstevel@tonic-gate 		pool_unlock();
39647c478bd9Sstevel@tonic-gate 	}
39657c478bd9Sstevel@tonic-gate 
39667c478bd9Sstevel@tonic-gate 	/*
39677c478bd9Sstevel@tonic-gate 	 * Wait for zone_destroy() to be called.  This is what we spend
39687c478bd9Sstevel@tonic-gate 	 * most of our life doing.
39697c478bd9Sstevel@tonic-gate 	 */
39707c478bd9Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched");
39717c478bd9Sstevel@tonic-gate 
39727c478bd9Sstevel@tonic-gate 	if (ct)
39737c478bd9Sstevel@tonic-gate 		/*
39747c478bd9Sstevel@tonic-gate 		 * At this point the process contract should be empty.
39757c478bd9Sstevel@tonic-gate 		 * (Though if it isn't, it's not the end of the world.)
39767c478bd9Sstevel@tonic-gate 		 */
39777c478bd9Sstevel@tonic-gate 		VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0);
39787c478bd9Sstevel@tonic-gate 
39797c478bd9Sstevel@tonic-gate 	/*
39807c478bd9Sstevel@tonic-gate 	 * Allow kcred to be freed when all referring processes
39817c478bd9Sstevel@tonic-gate 	 * (including this one) go away.  We can't just do this in
39827c478bd9Sstevel@tonic-gate 	 * zone_free because we need to wait for the zone_cred_ref to
39837c478bd9Sstevel@tonic-gate 	 * drop to 0 before calling zone_free, and the existence of
39847c478bd9Sstevel@tonic-gate 	 * zone_kcred will prevent that.  Thus, we call crfree here to
39857c478bd9Sstevel@tonic-gate 	 * balance the crdup in zone_create.  The crhold calls earlier
39867c478bd9Sstevel@tonic-gate 	 * in zsched will be dropped when the thread and process exit.
39877c478bd9Sstevel@tonic-gate 	 */
39887c478bd9Sstevel@tonic-gate 	crfree(zone->zone_kcred);
39897c478bd9Sstevel@tonic-gate 	zone->zone_kcred = NULL;
39907c478bd9Sstevel@tonic-gate 
39917c478bd9Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
39927c478bd9Sstevel@tonic-gate }
39937c478bd9Sstevel@tonic-gate 
39947c478bd9Sstevel@tonic-gate /*
39957c478bd9Sstevel@tonic-gate  * Helper function to determine if there are any submounts of the
39967c478bd9Sstevel@tonic-gate  * provided path.  Used to make sure the zone doesn't "inherit" any
39977c478bd9Sstevel@tonic-gate  * mounts from before it is created.
39987c478bd9Sstevel@tonic-gate  */
39997c478bd9Sstevel@tonic-gate static uint_t
40007c478bd9Sstevel@tonic-gate zone_mount_count(const char *rootpath)
40017c478bd9Sstevel@tonic-gate {
40027c478bd9Sstevel@tonic-gate 	vfs_t *vfsp;
40037c478bd9Sstevel@tonic-gate 	uint_t count = 0;
40047c478bd9Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
40057c478bd9Sstevel@tonic-gate 
40067c478bd9Sstevel@tonic-gate 	/*
40077c478bd9Sstevel@tonic-gate 	 * Holding zonehash_lock prevents race conditions with
40087c478bd9Sstevel@tonic-gate 	 * vfs_list_add()/vfs_list_remove() since we serialize with
40097c478bd9Sstevel@tonic-gate 	 * zone_find_by_path().
40107c478bd9Sstevel@tonic-gate 	 */
40117c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
40127c478bd9Sstevel@tonic-gate 	/*
40137c478bd9Sstevel@tonic-gate 	 * The rootpath must end with a '/'
40147c478bd9Sstevel@tonic-gate 	 */
40157c478bd9Sstevel@tonic-gate 	ASSERT(rootpath[rootpathlen - 1] == '/');
40167c478bd9Sstevel@tonic-gate 
40177c478bd9Sstevel@tonic-gate 	/*
40187c478bd9Sstevel@tonic-gate 	 * This intentionally does not count the rootpath itself if that
40197c478bd9Sstevel@tonic-gate 	 * happens to be a mount point.
40207c478bd9Sstevel@tonic-gate 	 */
40217c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
40227c478bd9Sstevel@tonic-gate 	vfsp = rootvfs;
40237c478bd9Sstevel@tonic-gate 	do {
40247c478bd9Sstevel@tonic-gate 		if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt),
40257c478bd9Sstevel@tonic-gate 		    rootpathlen) == 0)
40267c478bd9Sstevel@tonic-gate 			count++;
40277c478bd9Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
40287c478bd9Sstevel@tonic-gate 	} while (vfsp != rootvfs);
40297c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
40307c478bd9Sstevel@tonic-gate 	return (count);
40317c478bd9Sstevel@tonic-gate }
40327c478bd9Sstevel@tonic-gate 
40337c478bd9Sstevel@tonic-gate /*
40347c478bd9Sstevel@tonic-gate  * Helper function to make sure that a zone created on 'rootpath'
40357c478bd9Sstevel@tonic-gate  * wouldn't end up containing other zones' rootpaths.
40367c478bd9Sstevel@tonic-gate  */
40377c478bd9Sstevel@tonic-gate static boolean_t
40387c478bd9Sstevel@tonic-gate zone_is_nested(const char *rootpath)
40397c478bd9Sstevel@tonic-gate {
40407c478bd9Sstevel@tonic-gate 	zone_t *zone;
40417c478bd9Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
40427c478bd9Sstevel@tonic-gate 	size_t len;
40437c478bd9Sstevel@tonic-gate 
40447c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
40457c478bd9Sstevel@tonic-gate 
40462bb88978SDhanaraj M 	/*
40472bb88978SDhanaraj M 	 * zone_set_root() appended '/' and '\0' at the end of rootpath
40482bb88978SDhanaraj M 	 */
40492bb88978SDhanaraj M 	if ((rootpathlen <= 3) && (rootpath[0] == '/') &&
40502bb88978SDhanaraj M 	    (rootpath[1] == '/') && (rootpath[2] == '\0'))
40512bb88978SDhanaraj M 		return (B_TRUE);
40522bb88978SDhanaraj M 
40537c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
40547c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
40557c478bd9Sstevel@tonic-gate 		if (zone == global_zone)
40567c478bd9Sstevel@tonic-gate 			continue;
40577c478bd9Sstevel@tonic-gate 		len = strlen(zone->zone_rootpath);
40587c478bd9Sstevel@tonic-gate 		if (strncmp(rootpath, zone->zone_rootpath,
40597c478bd9Sstevel@tonic-gate 		    MIN(rootpathlen, len)) == 0)
40607c478bd9Sstevel@tonic-gate 			return (B_TRUE);
40617c478bd9Sstevel@tonic-gate 	}
40627c478bd9Sstevel@tonic-gate 	return (B_FALSE);
40637c478bd9Sstevel@tonic-gate }
40647c478bd9Sstevel@tonic-gate 
40657c478bd9Sstevel@tonic-gate static int
4066821c4a97Sdp zone_set_privset(zone_t *zone, const priv_set_t *zone_privs,
4067821c4a97Sdp     size_t zone_privssz)
40687c478bd9Sstevel@tonic-gate {
40691a88ce5cSDan Price 	priv_set_t *privs;
40707c478bd9Sstevel@tonic-gate 
4071821c4a97Sdp 	if (zone_privssz < sizeof (priv_set_t))
40721a88ce5cSDan Price 		return (ENOMEM);
40731a88ce5cSDan Price 
40741a88ce5cSDan Price 	privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
4075821c4a97Sdp 
40767c478bd9Sstevel@tonic-gate 	if (copyin(zone_privs, privs, sizeof (priv_set_t))) {
40777c478bd9Sstevel@tonic-gate 		kmem_free(privs, sizeof (priv_set_t));
40787c478bd9Sstevel@tonic-gate 		return (EFAULT);
40797c478bd9Sstevel@tonic-gate 	}
40807c478bd9Sstevel@tonic-gate 
40817c478bd9Sstevel@tonic-gate 	zone->zone_privset = privs;
40827c478bd9Sstevel@tonic-gate 	return (0);
40837c478bd9Sstevel@tonic-gate }
40847c478bd9Sstevel@tonic-gate 
40857c478bd9Sstevel@tonic-gate /*
40867c478bd9Sstevel@tonic-gate  * We make creative use of nvlists to pass in rctls from userland.  The list is
40877c478bd9Sstevel@tonic-gate  * a list of the following structures:
40887c478bd9Sstevel@tonic-gate  *
40897c478bd9Sstevel@tonic-gate  * (name = rctl_name, value = nvpair_list_array)
40907c478bd9Sstevel@tonic-gate  *
40917c478bd9Sstevel@tonic-gate  * Where each element of the nvpair_list_array is of the form:
40927c478bd9Sstevel@tonic-gate  *
40937c478bd9Sstevel@tonic-gate  * [(name = "privilege", value = RCPRIV_PRIVILEGED),
40947c478bd9Sstevel@tonic-gate  * 	(name = "limit", value = uint64_t),
40957c478bd9Sstevel@tonic-gate  * 	(name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))]
40967c478bd9Sstevel@tonic-gate  */
40977c478bd9Sstevel@tonic-gate static int
40987c478bd9Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp)
40997c478bd9Sstevel@tonic-gate {
41007c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
41017c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
41027c478bd9Sstevel@tonic-gate 	char *kbuf;
41037c478bd9Sstevel@tonic-gate 	int error;
41047c478bd9Sstevel@tonic-gate 	rctl_val_t rv;
41057c478bd9Sstevel@tonic-gate 
41067c478bd9Sstevel@tonic-gate 	*nvlp = NULL;
41077c478bd9Sstevel@tonic-gate 
41087c478bd9Sstevel@tonic-gate 	if (buflen == 0)
41097c478bd9Sstevel@tonic-gate 		return (0);
41107c478bd9Sstevel@tonic-gate 
41117c478bd9Sstevel@tonic-gate 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
41127c478bd9Sstevel@tonic-gate 		return (ENOMEM);
41137c478bd9Sstevel@tonic-gate 	if (copyin(ubuf, kbuf, buflen)) {
41147c478bd9Sstevel@tonic-gate 		error = EFAULT;
41157c478bd9Sstevel@tonic-gate 		goto out;
41167c478bd9Sstevel@tonic-gate 	}
41177c478bd9Sstevel@tonic-gate 	if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) {
41187c478bd9Sstevel@tonic-gate 		/*
41197c478bd9Sstevel@tonic-gate 		 * nvl may have been allocated/free'd, but the value set to
41207c478bd9Sstevel@tonic-gate 		 * non-NULL, so we reset it here.
41217c478bd9Sstevel@tonic-gate 		 */
41227c478bd9Sstevel@tonic-gate 		nvl = NULL;
41237c478bd9Sstevel@tonic-gate 		error = EINVAL;
41247c478bd9Sstevel@tonic-gate 		goto out;
41257c478bd9Sstevel@tonic-gate 	}
41267c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
41277c478bd9Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
41287c478bd9Sstevel@tonic-gate 		rctl_hndl_t hndl;
41297c478bd9Sstevel@tonic-gate 		nvlist_t **nvlarray;
41307c478bd9Sstevel@tonic-gate 		uint_t i, nelem;
41317c478bd9Sstevel@tonic-gate 		char *name;
41327c478bd9Sstevel@tonic-gate 
41337c478bd9Sstevel@tonic-gate 		error = EINVAL;
41347c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
41357c478bd9Sstevel@tonic-gate 		if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1)
41367c478bd9Sstevel@tonic-gate 		    != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) {
41377c478bd9Sstevel@tonic-gate 			goto out;
41387c478bd9Sstevel@tonic-gate 		}
41397c478bd9Sstevel@tonic-gate 		if ((hndl = rctl_hndl_lookup(name)) == -1) {
41407c478bd9Sstevel@tonic-gate 			goto out;
41417c478bd9Sstevel@tonic-gate 		}
41427c478bd9Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
41437c478bd9Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
41447c478bd9Sstevel@tonic-gate 		ASSERT(error == 0);
41457c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
41467c478bd9Sstevel@tonic-gate 			if (error = nvlist2rctlval(nvlarray[i], &rv))
41477c478bd9Sstevel@tonic-gate 				goto out;
41487c478bd9Sstevel@tonic-gate 		}
41497c478bd9Sstevel@tonic-gate 		if (rctl_invalid_value(rde, &rv)) {
41507c478bd9Sstevel@tonic-gate 			error = EINVAL;
41517c478bd9Sstevel@tonic-gate 			goto out;
41527c478bd9Sstevel@tonic-gate 		}
41537c478bd9Sstevel@tonic-gate 	}
41547c478bd9Sstevel@tonic-gate 	error = 0;
41557c478bd9Sstevel@tonic-gate 	*nvlp = nvl;
41567c478bd9Sstevel@tonic-gate out:
41577c478bd9Sstevel@tonic-gate 	kmem_free(kbuf, buflen);
41587c478bd9Sstevel@tonic-gate 	if (error && nvl != NULL)
41597c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
41607c478bd9Sstevel@tonic-gate 	return (error);
41617c478bd9Sstevel@tonic-gate }
41627c478bd9Sstevel@tonic-gate 
41637c478bd9Sstevel@tonic-gate int
41647c478bd9Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) {
41657c478bd9Sstevel@tonic-gate 	if (er_out != NULL) {
41667c478bd9Sstevel@tonic-gate 		if (copyout(&er_ext, er_out, sizeof (int))) {
41677c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
41687c478bd9Sstevel@tonic-gate 		}
41697c478bd9Sstevel@tonic-gate 	}
41707c478bd9Sstevel@tonic-gate 	return (set_errno(er_error));
41717c478bd9Sstevel@tonic-gate }
41727c478bd9Sstevel@tonic-gate 
417345916cd2Sjpk static int
417445916cd2Sjpk zone_set_label(zone_t *zone, const bslabel_t *lab, uint32_t doi)
417545916cd2Sjpk {
417645916cd2Sjpk 	ts_label_t *tsl;
417745916cd2Sjpk 	bslabel_t blab;
417845916cd2Sjpk 
417945916cd2Sjpk 	/* Get label from user */
418045916cd2Sjpk 	if (copyin(lab, &blab, sizeof (blab)) != 0)
418145916cd2Sjpk 		return (EFAULT);
418245916cd2Sjpk 	tsl = labelalloc(&blab, doi, KM_NOSLEEP);
418345916cd2Sjpk 	if (tsl == NULL)
418445916cd2Sjpk 		return (ENOMEM);
418545916cd2Sjpk 
418645916cd2Sjpk 	zone->zone_slabel = tsl;
418745916cd2Sjpk 	return (0);
418845916cd2Sjpk }
418945916cd2Sjpk 
41907c478bd9Sstevel@tonic-gate /*
4191fa9e4066Sahrens  * Parses a comma-separated list of ZFS datasets into a per-zone dictionary.
4192fa9e4066Sahrens  */
4193fa9e4066Sahrens static int
4194fa9e4066Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen)
4195fa9e4066Sahrens {
4196fa9e4066Sahrens 	char *kbuf;
4197fa9e4066Sahrens 	char *dataset, *next;
4198fa9e4066Sahrens 	zone_dataset_t *zd;
4199fa9e4066Sahrens 	size_t len;
4200fa9e4066Sahrens 
4201fa9e4066Sahrens 	if (ubuf == NULL || buflen == 0)
4202fa9e4066Sahrens 		return (0);
4203fa9e4066Sahrens 
4204fa9e4066Sahrens 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
4205fa9e4066Sahrens 		return (ENOMEM);
4206fa9e4066Sahrens 
4207fa9e4066Sahrens 	if (copyin(ubuf, kbuf, buflen) != 0) {
4208fa9e4066Sahrens 		kmem_free(kbuf, buflen);
4209fa9e4066Sahrens 		return (EFAULT);
4210fa9e4066Sahrens 	}
4211fa9e4066Sahrens 
4212fa9e4066Sahrens 	dataset = next = kbuf;
4213fa9e4066Sahrens 	for (;;) {
4214fa9e4066Sahrens 		zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP);
4215fa9e4066Sahrens 
4216fa9e4066Sahrens 		next = strchr(dataset, ',');
4217fa9e4066Sahrens 
4218fa9e4066Sahrens 		if (next == NULL)
4219fa9e4066Sahrens 			len = strlen(dataset);
4220fa9e4066Sahrens 		else
4221fa9e4066Sahrens 			len = next - dataset;
4222fa9e4066Sahrens 
4223fa9e4066Sahrens 		zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP);
4224fa9e4066Sahrens 		bcopy(dataset, zd->zd_dataset, len);
4225fa9e4066Sahrens 		zd->zd_dataset[len] = '\0';
4226fa9e4066Sahrens 
4227fa9e4066Sahrens 		list_insert_head(&zone->zone_datasets, zd);
4228fa9e4066Sahrens 
4229fa9e4066Sahrens 		if (next == NULL)
4230fa9e4066Sahrens 			break;
4231fa9e4066Sahrens 
4232fa9e4066Sahrens 		dataset = next + 1;
4233fa9e4066Sahrens 	}
4234fa9e4066Sahrens 
4235fa9e4066Sahrens 	kmem_free(kbuf, buflen);
4236fa9e4066Sahrens 	return (0);
4237fa9e4066Sahrens }
4238fa9e4066Sahrens 
4239fa9e4066Sahrens /*
42407c478bd9Sstevel@tonic-gate  * System call to create/initialize a new zone named 'zone_name', rooted
42417c478bd9Sstevel@tonic-gate  * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs',
424245916cd2Sjpk  * and initialized with the zone-wide rctls described in 'rctlbuf', and
424345916cd2Sjpk  * with labeling set by 'match', 'doi', and 'label'.
42447c478bd9Sstevel@tonic-gate  *
42457c478bd9Sstevel@tonic-gate  * If extended error is non-null, we may use it to return more detailed
42467c478bd9Sstevel@tonic-gate  * error information.
42477c478bd9Sstevel@tonic-gate  */
42487c478bd9Sstevel@tonic-gate static zoneid_t
42497c478bd9Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root,
4250821c4a97Sdp     const priv_set_t *zone_privs, size_t zone_privssz,
4251821c4a97Sdp     caddr_t rctlbuf, size_t rctlbufsz,
425245916cd2Sjpk     caddr_t zfsbuf, size_t zfsbufsz, int *extended_error,
4253f4b3ec61Sdh155122     int match, uint32_t doi, const bslabel_t *label,
4254f4b3ec61Sdh155122     int flags)
42557c478bd9Sstevel@tonic-gate {
42567c478bd9Sstevel@tonic-gate 	struct zsched_arg zarg;
42577c478bd9Sstevel@tonic-gate 	nvlist_t *rctls = NULL;
42587c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
42597c478bd9Sstevel@tonic-gate 	zone_t *zone, *ztmp;
42607c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
42617c478bd9Sstevel@tonic-gate 	int error;
42627c478bd9Sstevel@tonic-gate 	int error2 = 0;
42637c478bd9Sstevel@tonic-gate 	char *str;
42647c478bd9Sstevel@tonic-gate 	cred_t *zkcr;
426548451833Scarlsonj 	boolean_t insert_label_hash;
42667c478bd9Sstevel@tonic-gate 
42677c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
42687c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
42697c478bd9Sstevel@tonic-gate 
42707c478bd9Sstevel@tonic-gate 	/* can't boot zone from within chroot environment */
42717c478bd9Sstevel@tonic-gate 	if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir)
42727c478bd9Sstevel@tonic-gate 		return (zone_create_error(ENOTSUP, ZE_CHROOTED,
42737c478bd9Sstevel@tonic-gate 		    extended_error));
42747c478bd9Sstevel@tonic-gate 
42757c478bd9Sstevel@tonic-gate 	zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP);
42767c478bd9Sstevel@tonic-gate 	zoneid = zone->zone_id = id_alloc(zoneid_space);
42777c478bd9Sstevel@tonic-gate 	zone->zone_status = ZONE_IS_UNINITIALIZED;
42787c478bd9Sstevel@tonic-gate 	zone->zone_pool = pool_default;
42797c478bd9Sstevel@tonic-gate 	zone->zone_pool_mod = gethrtime();
42807c478bd9Sstevel@tonic-gate 	zone->zone_psetid = ZONE_PS_INVAL;
42817c478bd9Sstevel@tonic-gate 	zone->zone_ncpus = 0;
42827c478bd9Sstevel@tonic-gate 	zone->zone_ncpus_online = 0;
42839acbbeafSnn35248 	zone->zone_restart_init = B_TRUE;
42849acbbeafSnn35248 	zone->zone_brand = &native_brand;
42859acbbeafSnn35248 	zone->zone_initname = NULL;
42867c478bd9Sstevel@tonic-gate 	mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL);
42877c478bd9Sstevel@tonic-gate 	mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
42880209230bSgjelinek 	mutex_init(&zone->zone_mem_lock, NULL, MUTEX_DEFAULT, NULL);
42897c478bd9Sstevel@tonic-gate 	cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL);
4290a19609f8Sjv227347 	list_create(&zone->zone_ref_list, sizeof (zone_ref_t),
4291a19609f8Sjv227347 	    offsetof(zone_ref_t, zref_linkage));
42927c478bd9Sstevel@tonic-gate 	list_create(&zone->zone_zsd, sizeof (struct zsd_entry),
42937c478bd9Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
4294fa9e4066Sahrens 	list_create(&zone->zone_datasets, sizeof (zone_dataset_t),
4295fa9e4066Sahrens 	    offsetof(zone_dataset_t, zd_linkage));
42962b24ab6bSSebastien Roy 	list_create(&zone->zone_dl_list, sizeof (zone_dl_t),
42972b24ab6bSSebastien Roy 	    offsetof(zone_dl_t, zdl_linkage));
429845916cd2Sjpk 	rw_init(&zone->zone_mlps.mlpl_rwlock, NULL, RW_DEFAULT, NULL);
4299835ee219SRobert Harris 	rw_init(&zone->zone_mntfs_db_lock, NULL, RW_DEFAULT, NULL);
43007c478bd9Sstevel@tonic-gate 
4301f4b3ec61Sdh155122 	if (flags & ZCF_NET_EXCL) {
4302f4b3ec61Sdh155122 		zone->zone_flags |= ZF_NET_EXCL;
4303f4b3ec61Sdh155122 	}
4304f4b3ec61Sdh155122 
43057c478bd9Sstevel@tonic-gate 	if ((error = zone_set_name(zone, zone_name)) != 0) {
43067c478bd9Sstevel@tonic-gate 		zone_free(zone);
43077c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
43087c478bd9Sstevel@tonic-gate 	}
43097c478bd9Sstevel@tonic-gate 
43107c478bd9Sstevel@tonic-gate 	if ((error = zone_set_root(zone, zone_root)) != 0) {
43117c478bd9Sstevel@tonic-gate 		zone_free(zone);
43127c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
43137c478bd9Sstevel@tonic-gate 	}
4314821c4a97Sdp 	if ((error = zone_set_privset(zone, zone_privs, zone_privssz)) != 0) {
43157c478bd9Sstevel@tonic-gate 		zone_free(zone);
43167c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
43177c478bd9Sstevel@tonic-gate 	}
43187c478bd9Sstevel@tonic-gate 
43197c478bd9Sstevel@tonic-gate 	/* initialize node name to be the same as zone name */
43207c478bd9Sstevel@tonic-gate 	zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP);
43217c478bd9Sstevel@tonic-gate 	(void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN);
43227c478bd9Sstevel@tonic-gate 	zone->zone_nodename[_SYS_NMLN - 1] = '\0';
43237c478bd9Sstevel@tonic-gate 
43247c478bd9Sstevel@tonic-gate 	zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP);
43257c478bd9Sstevel@tonic-gate 	zone->zone_domain[0] = '\0';
43265679c89fSjv227347 	zone->zone_hostid = HW_INVALID_HOSTID;
43277c478bd9Sstevel@tonic-gate 	zone->zone_shares = 1;
4328824c205fSml93401 	zone->zone_shmmax = 0;
4329824c205fSml93401 	zone->zone_ipc.ipcq_shmmni = 0;
4330824c205fSml93401 	zone->zone_ipc.ipcq_semmni = 0;
4331824c205fSml93401 	zone->zone_ipc.ipcq_msgmni = 0;
43327c478bd9Sstevel@tonic-gate 	zone->zone_bootargs = NULL;
43330fbb751dSJohn Levon 	zone->zone_fs_allowed = NULL;
43343f2f09c1Sdp 	zone->zone_initname =
43353f2f09c1Sdp 	    kmem_alloc(strlen(zone_default_initname) + 1, KM_SLEEP);
43363f2f09c1Sdp 	(void) strcpy(zone->zone_initname, zone_default_initname);
43370209230bSgjelinek 	zone->zone_nlwps = 0;
43380209230bSgjelinek 	zone->zone_nlwps_ctl = INT_MAX;
4339ff19e029SMenno Lageman 	zone->zone_nprocs = 0;
4340ff19e029SMenno Lageman 	zone->zone_nprocs_ctl = INT_MAX;
4341c6939658Ssl108498 	zone->zone_locked_mem = 0;
4342c6939658Ssl108498 	zone->zone_locked_mem_ctl = UINT64_MAX;
43430209230bSgjelinek 	zone->zone_max_swap = 0;
43440209230bSgjelinek 	zone->zone_max_swap_ctl = UINT64_MAX;
43450fbb751dSJohn Levon 	zone->zone_max_lofi = 0;
43460fbb751dSJohn Levon 	zone->zone_max_lofi_ctl = UINT64_MAX;
43470209230bSgjelinek 	zone0.zone_lockedmem_kstat = NULL;
43480209230bSgjelinek 	zone0.zone_swapresv_kstat = NULL;
43497c478bd9Sstevel@tonic-gate 
43507c478bd9Sstevel@tonic-gate 	/*
43517c478bd9Sstevel@tonic-gate 	 * Zsched initializes the rctls.
43527c478bd9Sstevel@tonic-gate 	 */
43537c478bd9Sstevel@tonic-gate 	zone->zone_rctls = NULL;
43547c478bd9Sstevel@tonic-gate 
43557c478bd9Sstevel@tonic-gate 	if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) {
43567c478bd9Sstevel@tonic-gate 		zone_free(zone);
43577c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
43587c478bd9Sstevel@tonic-gate 	}
43597c478bd9Sstevel@tonic-gate 
4360fa9e4066Sahrens 	if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) {
4361fa9e4066Sahrens 		zone_free(zone);
4362fa9e4066Sahrens 		return (set_errno(error));
4363fa9e4066Sahrens 	}
4364fa9e4066Sahrens 
43657c478bd9Sstevel@tonic-gate 	/*
436645916cd2Sjpk 	 * Read in the trusted system parameters:
436745916cd2Sjpk 	 * match flag and sensitivity label.
436845916cd2Sjpk 	 */
436945916cd2Sjpk 	zone->zone_match = match;
437048451833Scarlsonj 	if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) {
43717e6639c2Skp158701 		/* Fail if requested to set doi to anything but system's doi */
43727e6639c2Skp158701 		if (doi != 0 && doi != default_doi) {
43737e6639c2Skp158701 			zone_free(zone);
43747e6639c2Skp158701 			return (set_errno(EINVAL));
43757e6639c2Skp158701 		}
43767e6639c2Skp158701 		/* Always apply system's doi to the zone */
43777e6639c2Skp158701 		error = zone_set_label(zone, label, default_doi);
437845916cd2Sjpk 		if (error != 0) {
437945916cd2Sjpk 			zone_free(zone);
438045916cd2Sjpk 			return (set_errno(error));
438145916cd2Sjpk 		}
438248451833Scarlsonj 		insert_label_hash = B_TRUE;
438345916cd2Sjpk 	} else {
438445916cd2Sjpk 		/* all zones get an admin_low label if system is not labeled */
438545916cd2Sjpk 		zone->zone_slabel = l_admin_low;
438645916cd2Sjpk 		label_hold(l_admin_low);
438748451833Scarlsonj 		insert_label_hash = B_FALSE;
438845916cd2Sjpk 	}
438945916cd2Sjpk 
439045916cd2Sjpk 	/*
43917c478bd9Sstevel@tonic-gate 	 * Stop all lwps since that's what normally happens as part of fork().
43927c478bd9Sstevel@tonic-gate 	 * This needs to happen before we grab any locks to avoid deadlock
43937c478bd9Sstevel@tonic-gate 	 * (another lwp in the process could be waiting for the held lock).
43947c478bd9Sstevel@tonic-gate 	 */
43957c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) {
43967c478bd9Sstevel@tonic-gate 		zone_free(zone);
43977c478bd9Sstevel@tonic-gate 		if (rctls)
43987c478bd9Sstevel@tonic-gate 			nvlist_free(rctls);
43997c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
44007c478bd9Sstevel@tonic-gate 	}
44017c478bd9Sstevel@tonic-gate 
44027c478bd9Sstevel@tonic-gate 	if (block_mounts() == 0) {
44037c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
44047c478bd9Sstevel@tonic-gate 		if (curthread != pp->p_agenttp)
44057c478bd9Sstevel@tonic-gate 			continuelwps(pp);
44067c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
44077c478bd9Sstevel@tonic-gate 		zone_free(zone);
44087c478bd9Sstevel@tonic-gate 		if (rctls)
44097c478bd9Sstevel@tonic-gate 			nvlist_free(rctls);
44107c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
44117c478bd9Sstevel@tonic-gate 	}
44127c478bd9Sstevel@tonic-gate 
44137c478bd9Sstevel@tonic-gate 	/*
44147c478bd9Sstevel@tonic-gate 	 * Set up credential for kernel access.  After this, any errors
44157c478bd9Sstevel@tonic-gate 	 * should go through the dance in errout rather than calling
44167c478bd9Sstevel@tonic-gate 	 * zone_free directly.
44177c478bd9Sstevel@tonic-gate 	 */
44187c478bd9Sstevel@tonic-gate 	zone->zone_kcred = crdup(kcred);
44197c478bd9Sstevel@tonic-gate 	crsetzone(zone->zone_kcred, zone);
44207c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred));
44217c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred));
44227c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred));
44237c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred));
44247c478bd9Sstevel@tonic-gate 
44257c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
44267c478bd9Sstevel@tonic-gate 	/*
44277c478bd9Sstevel@tonic-gate 	 * Make sure zone doesn't already exist.
442845916cd2Sjpk 	 *
442945916cd2Sjpk 	 * If the system and zone are labeled,
443045916cd2Sjpk 	 * make sure no other zone exists that has the same label.
44317c478bd9Sstevel@tonic-gate 	 */
443245916cd2Sjpk 	if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL ||
443348451833Scarlsonj 	    (insert_label_hash &&
443445916cd2Sjpk 	    (ztmp = zone_find_all_by_label(zone->zone_slabel)) != NULL)) {
44357c478bd9Sstevel@tonic-gate 		zone_status_t status;
44367c478bd9Sstevel@tonic-gate 
44377c478bd9Sstevel@tonic-gate 		status = zone_status_get(ztmp);
44387c478bd9Sstevel@tonic-gate 		if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING)
44397c478bd9Sstevel@tonic-gate 			error = EEXIST;
44407c478bd9Sstevel@tonic-gate 		else
44417c478bd9Sstevel@tonic-gate 			error = EBUSY;
44428c55461bSton 
44438c55461bSton 		if (insert_label_hash)
44448c55461bSton 			error2 = ZE_LABELINUSE;
44458c55461bSton 
44467c478bd9Sstevel@tonic-gate 		goto errout;
44477c478bd9Sstevel@tonic-gate 	}
44487c478bd9Sstevel@tonic-gate 
44497c478bd9Sstevel@tonic-gate 	/*
44507c478bd9Sstevel@tonic-gate 	 * Don't allow zone creations which would cause one zone's rootpath to
44517c478bd9Sstevel@tonic-gate 	 * be accessible from that of another (non-global) zone.
44527c478bd9Sstevel@tonic-gate 	 */
44537c478bd9Sstevel@tonic-gate 	if (zone_is_nested(zone->zone_rootpath)) {
44547c478bd9Sstevel@tonic-gate 		error = EBUSY;
44557c478bd9Sstevel@tonic-gate 		goto errout;
44567c478bd9Sstevel@tonic-gate 	}
44577c478bd9Sstevel@tonic-gate 
44587c478bd9Sstevel@tonic-gate 	ASSERT(zonecount != 0);		/* check for leaks */
44597c478bd9Sstevel@tonic-gate 	if (zonecount + 1 > maxzones) {
44607c478bd9Sstevel@tonic-gate 		error = ENOMEM;
44617c478bd9Sstevel@tonic-gate 		goto errout;
44627c478bd9Sstevel@tonic-gate 	}
44637c478bd9Sstevel@tonic-gate 
44647c478bd9Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
44657c478bd9Sstevel@tonic-gate 		error = EBUSY;
44667c478bd9Sstevel@tonic-gate 		error2 = ZE_AREMOUNTS;
44677c478bd9Sstevel@tonic-gate 		goto errout;
44687c478bd9Sstevel@tonic-gate 	}
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate 	/*
44717c478bd9Sstevel@tonic-gate 	 * Zone is still incomplete, but we need to drop all locks while
44727c478bd9Sstevel@tonic-gate 	 * zsched() initializes this zone's kernel process.  We
44737c478bd9Sstevel@tonic-gate 	 * optimistically add the zone to the hashtable and associated
44747c478bd9Sstevel@tonic-gate 	 * lists so a parallel zone_create() doesn't try to create the
44757c478bd9Sstevel@tonic-gate 	 * same zone.
44767c478bd9Sstevel@tonic-gate 	 */
44777c478bd9Sstevel@tonic-gate 	zonecount++;
44787c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid,
44797c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id,
44807c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
44817c478bd9Sstevel@tonic-gate 	str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP);
44827c478bd9Sstevel@tonic-gate 	(void) strcpy(str, zone->zone_name);
44837c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str,
44847c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
448548451833Scarlsonj 	if (insert_label_hash) {
448645916cd2Sjpk 		(void) mod_hash_insert(zonehashbylabel,
448745916cd2Sjpk 		    (mod_hash_key_t)zone->zone_slabel, (mod_hash_val_t)zone);
448848451833Scarlsonj 		zone->zone_flags |= ZF_HASHED_LABEL;
448945916cd2Sjpk 	}
449045916cd2Sjpk 
44917c478bd9Sstevel@tonic-gate 	/*
44927c478bd9Sstevel@tonic-gate 	 * Insert into active list.  At this point there are no 'hold's
44937c478bd9Sstevel@tonic-gate 	 * on the zone, but everyone else knows not to use it, so we can
44947c478bd9Sstevel@tonic-gate 	 * continue to use it.  zsched() will do a zone_hold() if the
44957c478bd9Sstevel@tonic-gate 	 * newproc() is successful.
44967c478bd9Sstevel@tonic-gate 	 */
44977c478bd9Sstevel@tonic-gate 	list_insert_tail(&zone_active, zone);
44987c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
44997c478bd9Sstevel@tonic-gate 
45007c478bd9Sstevel@tonic-gate 	zarg.zone = zone;
45017c478bd9Sstevel@tonic-gate 	zarg.nvlist = rctls;
45027c478bd9Sstevel@tonic-gate 	/*
45037c478bd9Sstevel@tonic-gate 	 * The process, task, and project rctls are probably wrong;
45047c478bd9Sstevel@tonic-gate 	 * we need an interface to get the default values of all rctls,
45057c478bd9Sstevel@tonic-gate 	 * and initialize zsched appropriately.  I'm not sure that that
45067c478bd9Sstevel@tonic-gate 	 * makes much of a difference, though.
45077c478bd9Sstevel@tonic-gate 	 */
450835a5a358SJonathan Adams 	error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL, 0);
450935a5a358SJonathan Adams 	if (error != 0) {
45107c478bd9Sstevel@tonic-gate 		/*
45117c478bd9Sstevel@tonic-gate 		 * We need to undo all globally visible state.
45127c478bd9Sstevel@tonic-gate 		 */
45137c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
45147c478bd9Sstevel@tonic-gate 		list_remove(&zone_active, zone);
451548451833Scarlsonj 		if (zone->zone_flags & ZF_HASHED_LABEL) {
451645916cd2Sjpk 			ASSERT(zone->zone_slabel != NULL);
451745916cd2Sjpk 			(void) mod_hash_destroy(zonehashbylabel,
451845916cd2Sjpk 			    (mod_hash_key_t)zone->zone_slabel);
451945916cd2Sjpk 		}
45207c478bd9Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyname,
45217c478bd9Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_name);
45227c478bd9Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyid,
45237c478bd9Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_id);
45247c478bd9Sstevel@tonic-gate 		ASSERT(zonecount > 1);
45257c478bd9Sstevel@tonic-gate 		zonecount--;
45267c478bd9Sstevel@tonic-gate 		goto errout;
45277c478bd9Sstevel@tonic-gate 	}
45287c478bd9Sstevel@tonic-gate 
45297c478bd9Sstevel@tonic-gate 	/*
45307c478bd9Sstevel@tonic-gate 	 * Zone creation can't fail from now on.
45317c478bd9Sstevel@tonic-gate 	 */
45327c478bd9Sstevel@tonic-gate 
45337c478bd9Sstevel@tonic-gate 	/*
45340209230bSgjelinek 	 * Create zone kstats
45350209230bSgjelinek 	 */
45360209230bSgjelinek 	zone_kstat_create(zone);
45370209230bSgjelinek 
45380209230bSgjelinek 	/*
45397c478bd9Sstevel@tonic-gate 	 * Let the other lwps continue.
45407c478bd9Sstevel@tonic-gate 	 */
45417c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
45427c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
45437c478bd9Sstevel@tonic-gate 		continuelwps(pp);
45447c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
45457c478bd9Sstevel@tonic-gate 
45467c478bd9Sstevel@tonic-gate 	/*
45477c478bd9Sstevel@tonic-gate 	 * Wait for zsched to finish initializing the zone.
45487c478bd9Sstevel@tonic-gate 	 */
45497c478bd9Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_READY);
45507c478bd9Sstevel@tonic-gate 	/*
45517c478bd9Sstevel@tonic-gate 	 * The zone is fully visible, so we can let mounts progress.
45527c478bd9Sstevel@tonic-gate 	 */
45537c478bd9Sstevel@tonic-gate 	resume_mounts();
45547c478bd9Sstevel@tonic-gate 	if (rctls)
45557c478bd9Sstevel@tonic-gate 		nvlist_free(rctls);
45567c478bd9Sstevel@tonic-gate 
45577c478bd9Sstevel@tonic-gate 	return (zoneid);
45587c478bd9Sstevel@tonic-gate 
45597c478bd9Sstevel@tonic-gate errout:
45607c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
45617c478bd9Sstevel@tonic-gate 	/*
45627c478bd9Sstevel@tonic-gate 	 * Let the other lwps continue.
45637c478bd9Sstevel@tonic-gate 	 */
45647c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
45657c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
45667c478bd9Sstevel@tonic-gate 		continuelwps(pp);
45677c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
45687c478bd9Sstevel@tonic-gate 
45697c478bd9Sstevel@tonic-gate 	resume_mounts();
45707c478bd9Sstevel@tonic-gate 	if (rctls)
45717c478bd9Sstevel@tonic-gate 		nvlist_free(rctls);
45727c478bd9Sstevel@tonic-gate 	/*
45737c478bd9Sstevel@tonic-gate 	 * There is currently one reference to the zone, a cred_ref from
45747c478bd9Sstevel@tonic-gate 	 * zone_kcred.  To free the zone, we call crfree, which will call
45757c478bd9Sstevel@tonic-gate 	 * zone_cred_rele, which will call zone_free.
45767c478bd9Sstevel@tonic-gate 	 */
4577a19609f8Sjv227347 	ASSERT(zone->zone_cred_ref == 1);
45787c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_kcred->cr_ref == 1);
45797c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_ref == 0);
45807c478bd9Sstevel@tonic-gate 	zkcr = zone->zone_kcred;
45817c478bd9Sstevel@tonic-gate 	zone->zone_kcred = NULL;
45827c478bd9Sstevel@tonic-gate 	crfree(zkcr);				/* triggers call to zone_free */
45837c478bd9Sstevel@tonic-gate 	return (zone_create_error(error, error2, extended_error));
45847c478bd9Sstevel@tonic-gate }
45857c478bd9Sstevel@tonic-gate 
45867c478bd9Sstevel@tonic-gate /*
45877c478bd9Sstevel@tonic-gate  * Cause the zone to boot.  This is pretty simple, since we let zoneadmd do
45883f2f09c1Sdp  * the heavy lifting.  initname is the path to the program to launch
45893f2f09c1Sdp  * at the "top" of the zone; if this is NULL, we use the system default,
45903f2f09c1Sdp  * which is stored at zone_default_initname.
45917c478bd9Sstevel@tonic-gate  */
45927c478bd9Sstevel@tonic-gate static int
45933f2f09c1Sdp zone_boot(zoneid_t zoneid)
45947c478bd9Sstevel@tonic-gate {
45957c478bd9Sstevel@tonic-gate 	int err;
45967c478bd9Sstevel@tonic-gate 	zone_t *zone;
45977c478bd9Sstevel@tonic-gate 
45987c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
45997c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
46007c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
46017c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
46027c478bd9Sstevel@tonic-gate 
46037c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
46047c478bd9Sstevel@tonic-gate 	/*
46057c478bd9Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with calls to
46067c478bd9Sstevel@tonic-gate 	 * zone_shutdown, zone_destroy, etc.
46077c478bd9Sstevel@tonic-gate 	 */
46087c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
46097c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
46107c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
46117c478bd9Sstevel@tonic-gate 	}
46127c478bd9Sstevel@tonic-gate 
46137c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
46147c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) != ZONE_IS_READY) {
46157c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
46167c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
46177c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
46187c478bd9Sstevel@tonic-gate 	}
46197c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_BOOTING);
46207c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
46217c478bd9Sstevel@tonic-gate 
46227c478bd9Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
46237c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
46247c478bd9Sstevel@tonic-gate 
46257c478bd9Sstevel@tonic-gate 	if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) {
46267c478bd9Sstevel@tonic-gate 		zone_rele(zone);
46277c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
46287c478bd9Sstevel@tonic-gate 	}
46297c478bd9Sstevel@tonic-gate 
46307c478bd9Sstevel@tonic-gate 	/*
46317c478bd9Sstevel@tonic-gate 	 * Boot (starting init) might have failed, in which case the zone
46327c478bd9Sstevel@tonic-gate 	 * will go to the SHUTTING_DOWN state; an appropriate errno will
46337c478bd9Sstevel@tonic-gate 	 * be placed in zone->zone_boot_err, and so we return that.
46347c478bd9Sstevel@tonic-gate 	 */
46357c478bd9Sstevel@tonic-gate 	err = zone->zone_boot_err;
46367c478bd9Sstevel@tonic-gate 	zone_rele(zone);
46377c478bd9Sstevel@tonic-gate 	return (err ? set_errno(err) : 0);
46387c478bd9Sstevel@tonic-gate }
46397c478bd9Sstevel@tonic-gate 
46407c478bd9Sstevel@tonic-gate /*
46417c478bd9Sstevel@tonic-gate  * Kills all user processes in the zone, waiting for them all to exit
46427c478bd9Sstevel@tonic-gate  * before returning.
46437c478bd9Sstevel@tonic-gate  */
46447c478bd9Sstevel@tonic-gate static int
46457c478bd9Sstevel@tonic-gate zone_empty(zone_t *zone)
46467c478bd9Sstevel@tonic-gate {
46477c478bd9Sstevel@tonic-gate 	int waitstatus;
46487c478bd9Sstevel@tonic-gate 
46497c478bd9Sstevel@tonic-gate 	/*
46507c478bd9Sstevel@tonic-gate 	 * We need to drop zonehash_lock before killing all
46517c478bd9Sstevel@tonic-gate 	 * processes, otherwise we'll deadlock with zone_find_*
46527c478bd9Sstevel@tonic-gate 	 * which can be called from the exit path.
46537c478bd9Sstevel@tonic-gate 	 */
46547c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
4655d3d50737SRafael Vanoni 	while ((waitstatus = zone_status_timedwait_sig(zone,
4656d3d50737SRafael Vanoni 	    ddi_get_lbolt() + hz, ZONE_IS_EMPTY)) == -1) {
46577c478bd9Sstevel@tonic-gate 		killall(zone->zone_id);
46587c478bd9Sstevel@tonic-gate 	}
46597c478bd9Sstevel@tonic-gate 	/*
46607c478bd9Sstevel@tonic-gate 	 * return EINTR if we were signaled
46617c478bd9Sstevel@tonic-gate 	 */
46627c478bd9Sstevel@tonic-gate 	if (waitstatus == 0)
46637c478bd9Sstevel@tonic-gate 		return (EINTR);
46647c478bd9Sstevel@tonic-gate 	return (0);
46657c478bd9Sstevel@tonic-gate }
46667c478bd9Sstevel@tonic-gate 
46677c478bd9Sstevel@tonic-gate /*
466845916cd2Sjpk  * This function implements the policy for zone visibility.
466945916cd2Sjpk  *
467045916cd2Sjpk  * In standard Solaris, a non-global zone can only see itself.
467145916cd2Sjpk  *
467245916cd2Sjpk  * In Trusted Extensions, a labeled zone can lookup any zone whose label
467345916cd2Sjpk  * it dominates. For this test, the label of the global zone is treated as
467445916cd2Sjpk  * admin_high so it is special-cased instead of being checked for dominance.
467545916cd2Sjpk  *
467645916cd2Sjpk  * Returns true if zone attributes are viewable, false otherwise.
467745916cd2Sjpk  */
467845916cd2Sjpk static boolean_t
467945916cd2Sjpk zone_list_access(zone_t *zone)
468045916cd2Sjpk {
468145916cd2Sjpk 
468245916cd2Sjpk 	if (curproc->p_zone == global_zone ||
468345916cd2Sjpk 	    curproc->p_zone == zone) {
468445916cd2Sjpk 		return (B_TRUE);
468548451833Scarlsonj 	} else if (is_system_labeled() && !(zone->zone_flags & ZF_IS_SCRATCH)) {
468645916cd2Sjpk 		bslabel_t *curproc_label;
468745916cd2Sjpk 		bslabel_t *zone_label;
468845916cd2Sjpk 
468945916cd2Sjpk 		curproc_label = label2bslabel(curproc->p_zone->zone_slabel);
469045916cd2Sjpk 		zone_label = label2bslabel(zone->zone_slabel);
469145916cd2Sjpk 
469245916cd2Sjpk 		if (zone->zone_id != GLOBAL_ZONEID &&
469345916cd2Sjpk 		    bldominates(curproc_label, zone_label)) {
469445916cd2Sjpk 			return (B_TRUE);
469545916cd2Sjpk 		} else {
469645916cd2Sjpk 			return (B_FALSE);
469745916cd2Sjpk 		}
469845916cd2Sjpk 	} else {
469945916cd2Sjpk 		return (B_FALSE);
470045916cd2Sjpk 	}
470145916cd2Sjpk }
470245916cd2Sjpk 
470345916cd2Sjpk /*
47047c478bd9Sstevel@tonic-gate  * Systemcall to start the zone's halt sequence.  By the time this
47057c478bd9Sstevel@tonic-gate  * function successfully returns, all user processes and kernel threads
47067c478bd9Sstevel@tonic-gate  * executing in it will have exited, ZSD shutdown callbacks executed,
47077c478bd9Sstevel@tonic-gate  * and the zone status set to ZONE_IS_DOWN.
47087c478bd9Sstevel@tonic-gate  *
47097c478bd9Sstevel@tonic-gate  * It is possible that the call will interrupt itself if the caller is the
47107c478bd9Sstevel@tonic-gate  * parent of any process running in the zone, and doesn't have SIGCHLD blocked.
47117c478bd9Sstevel@tonic-gate  */
47127c478bd9Sstevel@tonic-gate static int
47137c478bd9Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid)
47147c478bd9Sstevel@tonic-gate {
47157c478bd9Sstevel@tonic-gate 	int error;
47167c478bd9Sstevel@tonic-gate 	zone_t *zone;
47177c478bd9Sstevel@tonic-gate 	zone_status_t status;
47187c478bd9Sstevel@tonic-gate 
47197c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
47207c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
47217c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
47227c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
47237c478bd9Sstevel@tonic-gate 
47247c478bd9Sstevel@tonic-gate 	/*
47257c478bd9Sstevel@tonic-gate 	 * Block mounts so that VFS_MOUNT() can get an accurate view of
47267c478bd9Sstevel@tonic-gate 	 * the zone's status with regards to ZONE_IS_SHUTTING down.
47277c478bd9Sstevel@tonic-gate 	 *
47287c478bd9Sstevel@tonic-gate 	 * e.g. NFS can fail the mount if it determines that the zone
47297c478bd9Sstevel@tonic-gate 	 * has already begun the shutdown sequence.
47307c478bd9Sstevel@tonic-gate 	 */
47317c478bd9Sstevel@tonic-gate 	if (block_mounts() == 0)
47327c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
47337c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
47347c478bd9Sstevel@tonic-gate 	/*
47357c478bd9Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
47367c478bd9Sstevel@tonic-gate 	 * calls to zone_shutdown and zone_destroy.
47377c478bd9Sstevel@tonic-gate 	 */
47387c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
47397c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
47407c478bd9Sstevel@tonic-gate 		resume_mounts();
47417c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
47427c478bd9Sstevel@tonic-gate 	}
47437c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
47447c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
47457c478bd9Sstevel@tonic-gate 	/*
47467c478bd9Sstevel@tonic-gate 	 * Fail if the zone isn't fully initialized yet.
47477c478bd9Sstevel@tonic-gate 	 */
47487c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY) {
47497c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
47507c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
47517c478bd9Sstevel@tonic-gate 		resume_mounts();
47527c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
47537c478bd9Sstevel@tonic-gate 	}
47547c478bd9Sstevel@tonic-gate 	/*
47557c478bd9Sstevel@tonic-gate 	 * If conditions required for zone_shutdown() to return have been met,
47567c478bd9Sstevel@tonic-gate 	 * return success.
47577c478bd9Sstevel@tonic-gate 	 */
47587c478bd9Sstevel@tonic-gate 	if (status >= ZONE_IS_DOWN) {
47597c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
47607c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
47617c478bd9Sstevel@tonic-gate 		resume_mounts();
47627c478bd9Sstevel@tonic-gate 		return (0);
47637c478bd9Sstevel@tonic-gate 	}
47647c478bd9Sstevel@tonic-gate 	/*
47657c478bd9Sstevel@tonic-gate 	 * If zone_shutdown() hasn't been called before, go through the motions.
47667c478bd9Sstevel@tonic-gate 	 * If it has, there's nothing to do but wait for the kernel threads to
47677c478bd9Sstevel@tonic-gate 	 * drain.
47687c478bd9Sstevel@tonic-gate 	 */
47697c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_EMPTY) {
47707c478bd9Sstevel@tonic-gate 		uint_t ntasks;
47717c478bd9Sstevel@tonic-gate 
47727c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
47737c478bd9Sstevel@tonic-gate 		if ((ntasks = zone->zone_ntasks) != 1) {
47747c478bd9Sstevel@tonic-gate 			/*
47757c478bd9Sstevel@tonic-gate 			 * There's still stuff running.
47767c478bd9Sstevel@tonic-gate 			 */
47777c478bd9Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
47787c478bd9Sstevel@tonic-gate 		}
47797c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
47807c478bd9Sstevel@tonic-gate 		if (ntasks == 1) {
47817c478bd9Sstevel@tonic-gate 			/*
47827c478bd9Sstevel@tonic-gate 			 * The only way to create another task is through
47837c478bd9Sstevel@tonic-gate 			 * zone_enter(), which will block until we drop
47847c478bd9Sstevel@tonic-gate 			 * zonehash_lock.  The zone is empty.
47857c478bd9Sstevel@tonic-gate 			 */
47867c478bd9Sstevel@tonic-gate 			if (zone->zone_kthreads == NULL) {
47877c478bd9Sstevel@tonic-gate 				/*
47887c478bd9Sstevel@tonic-gate 				 * Skip ahead to ZONE_IS_DOWN
47897c478bd9Sstevel@tonic-gate 				 */
47907c478bd9Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_DOWN);
47917c478bd9Sstevel@tonic-gate 			} else {
47927c478bd9Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_EMPTY);
47937c478bd9Sstevel@tonic-gate 			}
47947c478bd9Sstevel@tonic-gate 		}
47957c478bd9Sstevel@tonic-gate 	}
47967c478bd9Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
47977c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
47987c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
47997c478bd9Sstevel@tonic-gate 	resume_mounts();
48007c478bd9Sstevel@tonic-gate 
48017c478bd9Sstevel@tonic-gate 	if (error = zone_empty(zone)) {
48027c478bd9Sstevel@tonic-gate 		zone_rele(zone);
48037c478bd9Sstevel@tonic-gate 		return (set_errno(error));
48047c478bd9Sstevel@tonic-gate 	}
48057c478bd9Sstevel@tonic-gate 	/*
48067c478bd9Sstevel@tonic-gate 	 * After the zone status goes to ZONE_IS_DOWN this zone will no
48077c478bd9Sstevel@tonic-gate 	 * longer be notified of changes to the pools configuration, so
48087c478bd9Sstevel@tonic-gate 	 * in order to not end up with a stale pool pointer, we point
48097c478bd9Sstevel@tonic-gate 	 * ourselves at the default pool and remove all resource
48107c478bd9Sstevel@tonic-gate 	 * visibility.  This is especially important as the zone_t may
48117c478bd9Sstevel@tonic-gate 	 * languish on the deathrow for a very long time waiting for
48127c478bd9Sstevel@tonic-gate 	 * cred's to drain out.
48137c478bd9Sstevel@tonic-gate 	 *
48147c478bd9Sstevel@tonic-gate 	 * This rebinding of the zone can happen multiple times
48157c478bd9Sstevel@tonic-gate 	 * (presumably due to interrupted or parallel systemcalls)
48167c478bd9Sstevel@tonic-gate 	 * without any adverse effects.
48177c478bd9Sstevel@tonic-gate 	 */
48187c478bd9Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
48197c478bd9Sstevel@tonic-gate 		zone_rele(zone);
48207c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
48217c478bd9Sstevel@tonic-gate 	}
48227c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
48237c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
48247c478bd9Sstevel@tonic-gate 		zone_pool_set(zone, pool_default);
48257c478bd9Sstevel@tonic-gate 		/*
48267c478bd9Sstevel@tonic-gate 		 * The zone no longer needs to be able to see any cpus.
48277c478bd9Sstevel@tonic-gate 		 */
48287c478bd9Sstevel@tonic-gate 		zone_pset_set(zone, ZONE_PS_INVAL);
48297c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
48307c478bd9Sstevel@tonic-gate 	}
48317c478bd9Sstevel@tonic-gate 	pool_unlock();
48327c478bd9Sstevel@tonic-gate 
48337c478bd9Sstevel@tonic-gate 	/*
48347c478bd9Sstevel@tonic-gate 	 * ZSD shutdown callbacks can be executed multiple times, hence
48357c478bd9Sstevel@tonic-gate 	 * it is safe to not be holding any locks across this call.
48367c478bd9Sstevel@tonic-gate 	 */
48377c478bd9Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_SHUTDOWN);
48387c478bd9Sstevel@tonic-gate 
48397c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
48407c478bd9Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN)
48417c478bd9Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DOWN);
48427c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
48437c478bd9Sstevel@tonic-gate 
48447c478bd9Sstevel@tonic-gate 	/*
48457c478bd9Sstevel@tonic-gate 	 * Wait for kernel threads to drain.
48467c478bd9Sstevel@tonic-gate 	 */
48477c478bd9Sstevel@tonic-gate 	if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) {
48487c478bd9Sstevel@tonic-gate 		zone_rele(zone);
48497c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
48507c478bd9Sstevel@tonic-gate 	}
48519acbbeafSnn35248 
485252978630Ssl108498 	/*
485352978630Ssl108498 	 * Zone can be become down/destroyable even if the above wait
485452978630Ssl108498 	 * returns EINTR, so any code added here may never execute.
485552978630Ssl108498 	 * (i.e. don't add code here)
485652978630Ssl108498 	 */
48579acbbeafSnn35248 
48587c478bd9Sstevel@tonic-gate 	zone_rele(zone);
48597c478bd9Sstevel@tonic-gate 	return (0);
48607c478bd9Sstevel@tonic-gate }
48617c478bd9Sstevel@tonic-gate 
48627c478bd9Sstevel@tonic-gate /*
4863a19609f8Sjv227347  * Log the specified zone's reference counts.  The caller should not be
4864a19609f8Sjv227347  * holding the zone's zone_lock.
4865a19609f8Sjv227347  */
4866a19609f8Sjv227347 static void
4867a19609f8Sjv227347 zone_log_refcounts(zone_t *zone)
4868a19609f8Sjv227347 {
4869a19609f8Sjv227347 	char *buffer;
4870a19609f8Sjv227347 	char *buffer_position;
4871a19609f8Sjv227347 	uint32_t buffer_size;
4872a19609f8Sjv227347 	uint32_t index;
4873a19609f8Sjv227347 	uint_t ref;
4874a19609f8Sjv227347 	uint_t cred_ref;
4875a19609f8Sjv227347 
4876a19609f8Sjv227347 	/*
4877a19609f8Sjv227347 	 * Construct a string representing the subsystem-specific reference
4878a19609f8Sjv227347 	 * counts.  The counts are printed in ascending order by index into the
4879a19609f8Sjv227347 	 * zone_t::zone_subsys_ref array.  The list will be surrounded by
4880a19609f8Sjv227347 	 * square brackets [] and will only contain nonzero reference counts.
4881a19609f8Sjv227347 	 *
4882a19609f8Sjv227347 	 * The buffer will hold two square bracket characters plus ten digits,
4883a19609f8Sjv227347 	 * one colon, one space, one comma, and some characters for a
4884a19609f8Sjv227347 	 * subsystem name per subsystem-specific reference count.  (Unsigned 32-
4885a19609f8Sjv227347 	 * bit integers have at most ten decimal digits.)  The last
4886a19609f8Sjv227347 	 * reference count's comma is replaced by the closing square
4887a19609f8Sjv227347 	 * bracket and a NULL character to terminate the string.
4888a19609f8Sjv227347 	 *
4889a19609f8Sjv227347 	 * NOTE: We have to grab the zone's zone_lock to create a consistent
4890a19609f8Sjv227347 	 * snapshot of the zone's reference counters.
4891a19609f8Sjv227347 	 *
4892a19609f8Sjv227347 	 * First, figure out how much space the string buffer will need.
4893a19609f8Sjv227347 	 * The buffer's size is stored in buffer_size.
4894a19609f8Sjv227347 	 */
4895a19609f8Sjv227347 	buffer_size = 2;			/* for the square brackets */
4896a19609f8Sjv227347 	mutex_enter(&zone->zone_lock);
4897a19609f8Sjv227347 	zone->zone_flags |= ZF_REFCOUNTS_LOGGED;
4898a19609f8Sjv227347 	ref = zone->zone_ref;
4899a19609f8Sjv227347 	cred_ref = zone->zone_cred_ref;
4900a19609f8Sjv227347 	for (index = 0; index < ZONE_REF_NUM_SUBSYS; ++index)
4901a19609f8Sjv227347 		if (zone->zone_subsys_ref[index] != 0)
4902a19609f8Sjv227347 			buffer_size += strlen(zone_ref_subsys_names[index]) +
4903a19609f8Sjv227347 			    13;
4904a19609f8Sjv227347 	if (buffer_size == 2) {
4905a19609f8Sjv227347 		/*
4906a19609f8Sjv227347 		 * No subsystems had nonzero reference counts.  Don't bother
4907a19609f8Sjv227347 		 * with allocating a buffer; just log the general-purpose and
4908a19609f8Sjv227347 		 * credential reference counts.
4909a19609f8Sjv227347 		 */
4910a19609f8Sjv227347 		mutex_exit(&zone->zone_lock);
4911a19609f8Sjv227347 		(void) strlog(0, 0, 1, SL_CONSOLE | SL_NOTE,
4912a19609f8Sjv227347 		    "Zone '%s' (ID: %d) is shutting down, but %u zone "
4913a19609f8Sjv227347 		    "references and %u credential references are still extant",
4914a19609f8Sjv227347 		    zone->zone_name, zone->zone_id, ref, cred_ref);
4915a19609f8Sjv227347 		return;
4916a19609f8Sjv227347 	}
4917a19609f8Sjv227347 
4918a19609f8Sjv227347 	/*
4919a19609f8Sjv227347 	 * buffer_size contains the exact number of characters that the
4920a19609f8Sjv227347 	 * buffer will need.  Allocate the buffer and fill it with nonzero
4921a19609f8Sjv227347 	 * subsystem-specific reference counts.  Surround the results with
4922a19609f8Sjv227347 	 * square brackets afterwards.
4923a19609f8Sjv227347 	 */
4924a19609f8Sjv227347 	buffer = kmem_alloc(buffer_size, KM_SLEEP);
4925a19609f8Sjv227347 	buffer_position = &buffer[1];
4926a19609f8Sjv227347 	for (index = 0; index < ZONE_REF_NUM_SUBSYS; ++index) {
4927a19609f8Sjv227347 		/*
4928a19609f8Sjv227347 		 * NOTE: The DDI's version of sprintf() returns a pointer to
4929a19609f8Sjv227347 		 * the modified buffer rather than the number of bytes written
4930a19609f8Sjv227347 		 * (as in snprintf(3C)).  This is unfortunate and annoying.
4931a19609f8Sjv227347 		 * Therefore, we'll use snprintf() with INT_MAX to get the
4932a19609f8Sjv227347 		 * number of bytes written.  Using INT_MAX is safe because
4933a19609f8Sjv227347 		 * the buffer is perfectly sized for the data: we'll never
4934a19609f8Sjv227347 		 * overrun the buffer.
4935a19609f8Sjv227347 		 */
4936a19609f8Sjv227347 		if (zone->zone_subsys_ref[index] != 0)
4937a19609f8Sjv227347 			buffer_position += snprintf(buffer_position, INT_MAX,
4938a19609f8Sjv227347 			    "%s: %u,", zone_ref_subsys_names[index],
4939a19609f8Sjv227347 			    zone->zone_subsys_ref[index]);
4940a19609f8Sjv227347 	}
4941a19609f8Sjv227347 	mutex_exit(&zone->zone_lock);
4942a19609f8Sjv227347 	buffer[0] = '[';
4943a19609f8Sjv227347 	ASSERT((uintptr_t)(buffer_position - buffer) < buffer_size);
4944a19609f8Sjv227347 	ASSERT(buffer_position[0] == '\0' && buffer_position[-1] == ',');
4945a19609f8Sjv227347 	buffer_position[-1] = ']';
4946a19609f8Sjv227347 
4947a19609f8Sjv227347 	/*
4948a19609f8Sjv227347 	 * Log the reference counts and free the message buffer.
4949a19609f8Sjv227347 	 */
4950a19609f8Sjv227347 	(void) strlog(0, 0, 1, SL_CONSOLE | SL_NOTE,
4951a19609f8Sjv227347 	    "Zone '%s' (ID: %d) is shutting down, but %u zone references and "
4952a19609f8Sjv227347 	    "%u credential references are still extant %s", zone->zone_name,
4953a19609f8Sjv227347 	    zone->zone_id, ref, cred_ref, buffer);
4954a19609f8Sjv227347 	kmem_free(buffer, buffer_size);
4955a19609f8Sjv227347 }
4956a19609f8Sjv227347 
4957a19609f8Sjv227347 /*
49587c478bd9Sstevel@tonic-gate  * Systemcall entry point to finalize the zone halt process.  The caller
4959824c205fSml93401  * must have already successfully called zone_shutdown().
49607c478bd9Sstevel@tonic-gate  *
49617c478bd9Sstevel@tonic-gate  * Upon successful completion, the zone will have been fully destroyed:
49627c478bd9Sstevel@tonic-gate  * zsched will have exited, destructor callbacks executed, and the zone
49637c478bd9Sstevel@tonic-gate  * removed from the list of active zones.
49647c478bd9Sstevel@tonic-gate  */
49657c478bd9Sstevel@tonic-gate static int
49667c478bd9Sstevel@tonic-gate zone_destroy(zoneid_t zoneid)
49677c478bd9Sstevel@tonic-gate {
49687c478bd9Sstevel@tonic-gate 	uint64_t uniqid;
49697c478bd9Sstevel@tonic-gate 	zone_t *zone;
49707c478bd9Sstevel@tonic-gate 	zone_status_t status;
4971a19609f8Sjv227347 	clock_t wait_time;
4972a19609f8Sjv227347 	boolean_t log_refcounts;
49737c478bd9Sstevel@tonic-gate 
49747c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
49757c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
49767c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
49777c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
49787c478bd9Sstevel@tonic-gate 
49797c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
49807c478bd9Sstevel@tonic-gate 	/*
49817c478bd9Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
49827c478bd9Sstevel@tonic-gate 	 * calls to zone_destroy.
49837c478bd9Sstevel@tonic-gate 	 */
49847c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
49857c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
49867c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
49877c478bd9Sstevel@tonic-gate 	}
49887c478bd9Sstevel@tonic-gate 
49897c478bd9Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
49907c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
49917c478bd9Sstevel@tonic-gate 		return (set_errno(EBUSY));
49927c478bd9Sstevel@tonic-gate 	}
49937c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
49947c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
49957c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_DOWN) {
49967c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
49977c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
49987c478bd9Sstevel@tonic-gate 		return (set_errno(EBUSY));
49997c478bd9Sstevel@tonic-gate 	} else if (status == ZONE_IS_DOWN) {
50007c478bd9Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */
50017c478bd9Sstevel@tonic-gate 	}
50027c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
50037c478bd9Sstevel@tonic-gate 	zone_hold(zone);
50047c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
50057c478bd9Sstevel@tonic-gate 
50067c478bd9Sstevel@tonic-gate 	/*
50077c478bd9Sstevel@tonic-gate 	 * wait for zsched to exit
50087c478bd9Sstevel@tonic-gate 	 */
50097c478bd9Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_DEAD);
50107c478bd9Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_DESTROY);
5011f4b3ec61Sdh155122 	zone->zone_netstack = NULL;
50127c478bd9Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
50137c478bd9Sstevel@tonic-gate 	zone_rele(zone);
50147c478bd9Sstevel@tonic-gate 	zone = NULL;	/* potentially free'd */
50157c478bd9Sstevel@tonic-gate 
5016a19609f8Sjv227347 	log_refcounts = B_FALSE;
5017a19609f8Sjv227347 	wait_time = SEC_TO_TICK(ZONE_DESTROY_TIMEOUT_SECS);
50187c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
50197c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
50207c478bd9Sstevel@tonic-gate 		boolean_t unref;
5021a19609f8Sjv227347 		boolean_t refs_have_been_logged;
50227c478bd9Sstevel@tonic-gate 
50237c478bd9Sstevel@tonic-gate 		if ((zone = zone_find_all_by_id(zoneid)) == NULL ||
50247c478bd9Sstevel@tonic-gate 		    zone->zone_uniqid != uniqid) {
50257c478bd9Sstevel@tonic-gate 			/*
50267c478bd9Sstevel@tonic-gate 			 * The zone has gone away.  Necessary conditions
50277c478bd9Sstevel@tonic-gate 			 * are met, so we return success.
50287c478bd9Sstevel@tonic-gate 			 */
50297c478bd9Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
50307c478bd9Sstevel@tonic-gate 			return (0);
50317c478bd9Sstevel@tonic-gate 		}
50327c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
50337c478bd9Sstevel@tonic-gate 		unref = ZONE_IS_UNREF(zone);
5034a19609f8Sjv227347 		refs_have_been_logged = (zone->zone_flags &
5035a19609f8Sjv227347 		    ZF_REFCOUNTS_LOGGED);
50367c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
50377c478bd9Sstevel@tonic-gate 		if (unref) {
50387c478bd9Sstevel@tonic-gate 			/*
50397c478bd9Sstevel@tonic-gate 			 * There is only one reference to the zone -- that
50407c478bd9Sstevel@tonic-gate 			 * added when the zone was added to the hashtables --
50417c478bd9Sstevel@tonic-gate 			 * and things will remain this way until we drop
50427c478bd9Sstevel@tonic-gate 			 * zonehash_lock... we can go ahead and cleanup the
50437c478bd9Sstevel@tonic-gate 			 * zone.
50447c478bd9Sstevel@tonic-gate 			 */
50457c478bd9Sstevel@tonic-gate 			break;
50467c478bd9Sstevel@tonic-gate 		}
50477c478bd9Sstevel@tonic-gate 
5048a19609f8Sjv227347 		/*
5049a19609f8Sjv227347 		 * Wait for zone_rele_common() or zone_cred_rele() to signal
5050a19609f8Sjv227347 		 * zone_destroy_cv.  zone_destroy_cv is signaled only when
5051a19609f8Sjv227347 		 * some zone's general-purpose reference count reaches one.
5052a19609f8Sjv227347 		 * If ZONE_DESTROY_TIMEOUT_SECS seconds elapse while waiting
5053a19609f8Sjv227347 		 * on zone_destroy_cv, then log the zone's reference counts and
5054a19609f8Sjv227347 		 * continue to wait for zone_rele() and zone_cred_rele().
5055a19609f8Sjv227347 		 */
5056a19609f8Sjv227347 		if (!refs_have_been_logged) {
5057a19609f8Sjv227347 			if (!log_refcounts) {
5058a19609f8Sjv227347 				/*
5059a19609f8Sjv227347 				 * This thread hasn't timed out waiting on
5060a19609f8Sjv227347 				 * zone_destroy_cv yet.  Wait wait_time clock
5061a19609f8Sjv227347 				 * ticks (initially ZONE_DESTROY_TIMEOUT_SECS
5062a19609f8Sjv227347 				 * seconds) for the zone's references to clear.
5063a19609f8Sjv227347 				 */
5064a19609f8Sjv227347 				ASSERT(wait_time > 0);
5065a19609f8Sjv227347 				wait_time = cv_reltimedwait_sig(
5066a19609f8Sjv227347 				    &zone_destroy_cv, &zonehash_lock, wait_time,
5067a19609f8Sjv227347 				    TR_SEC);
5068a19609f8Sjv227347 				if (wait_time > 0) {
5069a19609f8Sjv227347 					/*
5070a19609f8Sjv227347 					 * A thread in zone_rele() or
5071a19609f8Sjv227347 					 * zone_cred_rele() signaled
5072a19609f8Sjv227347 					 * zone_destroy_cv before this thread's
5073a19609f8Sjv227347 					 * wait timed out.  The zone might have
5074a19609f8Sjv227347 					 * only one reference left; find out!
5075a19609f8Sjv227347 					 */
5076a19609f8Sjv227347 					continue;
5077a19609f8Sjv227347 				} else if (wait_time == 0) {
5078a19609f8Sjv227347 					/* The thread's process was signaled. */
50797c478bd9Sstevel@tonic-gate 					mutex_exit(&zonehash_lock);
50807c478bd9Sstevel@tonic-gate 					return (set_errno(EINTR));
50817c478bd9Sstevel@tonic-gate 				}
50827c478bd9Sstevel@tonic-gate 
5083a19609f8Sjv227347 				/*
5084a19609f8Sjv227347 				 * The thread timed out while waiting on
5085a19609f8Sjv227347 				 * zone_destroy_cv.  Even though the thread
5086a19609f8Sjv227347 				 * timed out, it has to check whether another
5087a19609f8Sjv227347 				 * thread woke up from zone_destroy_cv and
5088a19609f8Sjv227347 				 * destroyed the zone.
5089a19609f8Sjv227347 				 *
5090a19609f8Sjv227347 				 * If the zone still exists and has more than
5091a19609f8Sjv227347 				 * one unreleased general-purpose reference,
5092a19609f8Sjv227347 				 * then log the zone's reference counts.
5093a19609f8Sjv227347 				 */
5094a19609f8Sjv227347 				log_refcounts = B_TRUE;
5095a19609f8Sjv227347 				continue;
5096a19609f8Sjv227347 			}
5097a19609f8Sjv227347 
5098a19609f8Sjv227347 			/*
5099a19609f8Sjv227347 			 * The thread already timed out on zone_destroy_cv while
5100a19609f8Sjv227347 			 * waiting for subsystems to release the zone's last
5101a19609f8Sjv227347 			 * general-purpose references.  Log the zone's reference
5102a19609f8Sjv227347 			 * counts and wait indefinitely on zone_destroy_cv.
5103a19609f8Sjv227347 			 */
5104a19609f8Sjv227347 			zone_log_refcounts(zone);
5105a19609f8Sjv227347 		}
5106a19609f8Sjv227347 		if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) {
5107a19609f8Sjv227347 			/* The thread's process was signaled. */
5108a19609f8Sjv227347 			mutex_exit(&zonehash_lock);
5109a19609f8Sjv227347 			return (set_errno(EINTR));
5110a19609f8Sjv227347 		}
51117c478bd9Sstevel@tonic-gate 	}
51127c478bd9Sstevel@tonic-gate 
5113c97ad5cdSakolb 	/*
5114c97ad5cdSakolb 	 * Remove CPU cap for this zone now since we're not going to
5115c97ad5cdSakolb 	 * fail below this point.
5116c97ad5cdSakolb 	 */
5117c97ad5cdSakolb 	cpucaps_zone_remove(zone);
5118c97ad5cdSakolb 
5119c97ad5cdSakolb 	/* Get rid of the zone's kstats */
51200209230bSgjelinek 	zone_kstat_delete(zone);
51210209230bSgjelinek 
5122134a1f4eSCasper H.S. Dik 	/* remove the pfexecd doors */
5123134a1f4eSCasper H.S. Dik 	if (zone->zone_pfexecd != NULL) {
5124134a1f4eSCasper H.S. Dik 		klpd_freelist(&zone->zone_pfexecd);
5125134a1f4eSCasper H.S. Dik 		zone->zone_pfexecd = NULL;
5126134a1f4eSCasper H.S. Dik 	}
5127134a1f4eSCasper H.S. Dik 
5128319378d9Seh208807 	/* free brand specific data */
5129319378d9Seh208807 	if (ZONE_IS_BRANDED(zone))
5130319378d9Seh208807 		ZBROP(zone)->b_free_brand_data(zone);
5131319378d9Seh208807 
513252978630Ssl108498 	/* Say goodbye to brand framework. */
513352978630Ssl108498 	brand_unregister_zone(zone->zone_brand);
513452978630Ssl108498 
51357c478bd9Sstevel@tonic-gate 	/*
51367c478bd9Sstevel@tonic-gate 	 * It is now safe to let the zone be recreated; remove it from the
51377c478bd9Sstevel@tonic-gate 	 * lists.  The memory will not be freed until the last cred
51387c478bd9Sstevel@tonic-gate 	 * reference goes away.
51397c478bd9Sstevel@tonic-gate 	 */
51407c478bd9Sstevel@tonic-gate 	ASSERT(zonecount > 1);	/* must be > 1; can't destroy global zone */
51417c478bd9Sstevel@tonic-gate 	zonecount--;
51427c478bd9Sstevel@tonic-gate 	/* remove from active list and hash tables */
51437c478bd9Sstevel@tonic-gate 	list_remove(&zone_active, zone);
51447c478bd9Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyname,
51457c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)zone->zone_name);
51467c478bd9Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyid,
51477c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id);
514848451833Scarlsonj 	if (zone->zone_flags & ZF_HASHED_LABEL)
514945916cd2Sjpk 		(void) mod_hash_destroy(zonehashbylabel,
515045916cd2Sjpk 		    (mod_hash_key_t)zone->zone_slabel);
51517c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
51527c478bd9Sstevel@tonic-gate 
5153108322fbScarlsonj 	/*
5154108322fbScarlsonj 	 * Release the root vnode; we're not using it anymore.  Nor should any
5155108322fbScarlsonj 	 * other thread that might access it exist.
5156108322fbScarlsonj 	 */
5157108322fbScarlsonj 	if (zone->zone_rootvp != NULL) {
5158108322fbScarlsonj 		VN_RELE(zone->zone_rootvp);
5159108322fbScarlsonj 		zone->zone_rootvp = NULL;
5160108322fbScarlsonj 	}
5161108322fbScarlsonj 
51627c478bd9Sstevel@tonic-gate 	/* add to deathrow list */
51637c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_deathrow_lock);
51647c478bd9Sstevel@tonic-gate 	list_insert_tail(&zone_deathrow, zone);
51657c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_deathrow_lock);
51667c478bd9Sstevel@tonic-gate 
51677c478bd9Sstevel@tonic-gate 	/*
51687c478bd9Sstevel@tonic-gate 	 * Drop last reference (which was added by zsched()), this will
51697c478bd9Sstevel@tonic-gate 	 * free the zone unless there are outstanding cred references.
51707c478bd9Sstevel@tonic-gate 	 */
51717c478bd9Sstevel@tonic-gate 	zone_rele(zone);
51727c478bd9Sstevel@tonic-gate 	return (0);
51737c478bd9Sstevel@tonic-gate }
51747c478bd9Sstevel@tonic-gate 
51757c478bd9Sstevel@tonic-gate /*
51767c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_getattr(2).
51777c478bd9Sstevel@tonic-gate  */
51787c478bd9Sstevel@tonic-gate static ssize_t
51797c478bd9Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
51807c478bd9Sstevel@tonic-gate {
51817c478bd9Sstevel@tonic-gate 	size_t size;
51827c478bd9Sstevel@tonic-gate 	int error = 0, err;
51837c478bd9Sstevel@tonic-gate 	zone_t *zone;
51847c478bd9Sstevel@tonic-gate 	char *zonepath;
51853f2f09c1Sdp 	char *outstr;
51867c478bd9Sstevel@tonic-gate 	zone_status_t zone_status;
51877c478bd9Sstevel@tonic-gate 	pid_t initpid;
5188c97ad5cdSakolb 	boolean_t global = (curzone == global_zone);
5189c97ad5cdSakolb 	boolean_t inzone = (curzone->zone_id == zoneid);
5190f4b3ec61Sdh155122 	ushort_t flags;
5191550b6e40SSowmini Varadhan 	zone_net_data_t *zbuf;
51927c478bd9Sstevel@tonic-gate 
51937c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
51947c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
51957c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
51967c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
51977c478bd9Sstevel@tonic-gate 	}
51987c478bd9Sstevel@tonic-gate 	zone_status = zone_status_get(zone);
5199bd41d0a8Snordmark 	if (zone_status < ZONE_IS_INITIALIZED) {
52007c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
52017c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
52027c478bd9Sstevel@tonic-gate 	}
52037c478bd9Sstevel@tonic-gate 	zone_hold(zone);
52047c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
52057c478bd9Sstevel@tonic-gate 
52067c478bd9Sstevel@tonic-gate 	/*
520745916cd2Sjpk 	 * If not in the global zone, don't show information about other zones,
520845916cd2Sjpk 	 * unless the system is labeled and the local zone's label dominates
520945916cd2Sjpk 	 * the other zone.
52107c478bd9Sstevel@tonic-gate 	 */
521145916cd2Sjpk 	if (!zone_list_access(zone)) {
52127c478bd9Sstevel@tonic-gate 		zone_rele(zone);
52137c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
52147c478bd9Sstevel@tonic-gate 	}
52157c478bd9Sstevel@tonic-gate 
52167c478bd9Sstevel@tonic-gate 	switch (attr) {
52177c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_ROOT:
52187c478bd9Sstevel@tonic-gate 		if (global) {
52197c478bd9Sstevel@tonic-gate 			/*
52207c478bd9Sstevel@tonic-gate 			 * Copy the path to trim the trailing "/" (except for
52217c478bd9Sstevel@tonic-gate 			 * the global zone).
52227c478bd9Sstevel@tonic-gate 			 */
52237c478bd9Sstevel@tonic-gate 			if (zone != global_zone)
52247c478bd9Sstevel@tonic-gate 				size = zone->zone_rootpathlen - 1;
52257c478bd9Sstevel@tonic-gate 			else
52267c478bd9Sstevel@tonic-gate 				size = zone->zone_rootpathlen;
52277c478bd9Sstevel@tonic-gate 			zonepath = kmem_alloc(size, KM_SLEEP);
52287c478bd9Sstevel@tonic-gate 			bcopy(zone->zone_rootpath, zonepath, size);
52297c478bd9Sstevel@tonic-gate 			zonepath[size - 1] = '\0';
52307c478bd9Sstevel@tonic-gate 		} else {
5231c97ad5cdSakolb 			if (inzone || !is_system_labeled()) {
52327c478bd9Sstevel@tonic-gate 				/*
523345916cd2Sjpk 				 * Caller is not in the global zone.
523445916cd2Sjpk 				 * if the query is on the current zone
523545916cd2Sjpk 				 * or the system is not labeled,
523645916cd2Sjpk 				 * just return faked-up path for current zone.
52377c478bd9Sstevel@tonic-gate 				 */
52387c478bd9Sstevel@tonic-gate 				zonepath = "/";
52397c478bd9Sstevel@tonic-gate 				size = 2;
524045916cd2Sjpk 			} else {
524145916cd2Sjpk 				/*
524245916cd2Sjpk 				 * Return related path for current zone.
524345916cd2Sjpk 				 */
524445916cd2Sjpk 				int prefix_len = strlen(zone_prefix);
524545916cd2Sjpk 				int zname_len = strlen(zone->zone_name);
524645916cd2Sjpk 
524745916cd2Sjpk 				size = prefix_len + zname_len + 1;
524845916cd2Sjpk 				zonepath = kmem_alloc(size, KM_SLEEP);
524945916cd2Sjpk 				bcopy(zone_prefix, zonepath, prefix_len);
525045916cd2Sjpk 				bcopy(zone->zone_name, zonepath +
525145916cd2Sjpk 				    prefix_len, zname_len);
525245916cd2Sjpk 				zonepath[size - 1] = '\0';
525345916cd2Sjpk 			}
52547c478bd9Sstevel@tonic-gate 		}
52557c478bd9Sstevel@tonic-gate 		if (bufsize > size)
52567c478bd9Sstevel@tonic-gate 			bufsize = size;
52577c478bd9Sstevel@tonic-gate 		if (buf != NULL) {
52587c478bd9Sstevel@tonic-gate 			err = copyoutstr(zonepath, buf, bufsize, NULL);
52597c478bd9Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
52607c478bd9Sstevel@tonic-gate 				error = EFAULT;
52617c478bd9Sstevel@tonic-gate 		}
5262c97ad5cdSakolb 		if (global || (is_system_labeled() && !inzone))
52637c478bd9Sstevel@tonic-gate 			kmem_free(zonepath, size);
52647c478bd9Sstevel@tonic-gate 		break;
52657c478bd9Sstevel@tonic-gate 
52667c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_NAME:
52677c478bd9Sstevel@tonic-gate 		size = strlen(zone->zone_name) + 1;
52687c478bd9Sstevel@tonic-gate 		if (bufsize > size)
52697c478bd9Sstevel@tonic-gate 			bufsize = size;
52707c478bd9Sstevel@tonic-gate 		if (buf != NULL) {
52717c478bd9Sstevel@tonic-gate 			err = copyoutstr(zone->zone_name, buf, bufsize, NULL);
52727c478bd9Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
52737c478bd9Sstevel@tonic-gate 				error = EFAULT;
52747c478bd9Sstevel@tonic-gate 		}
52757c478bd9Sstevel@tonic-gate 		break;
52767c478bd9Sstevel@tonic-gate 
52777c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_STATUS:
52787c478bd9Sstevel@tonic-gate 		/*
52797c478bd9Sstevel@tonic-gate 		 * Since we're not holding zonehash_lock, the zone status
52807c478bd9Sstevel@tonic-gate 		 * may be anything; leave it up to userland to sort it out.
52817c478bd9Sstevel@tonic-gate 		 */
52827c478bd9Sstevel@tonic-gate 		size = sizeof (zone_status);
52837c478bd9Sstevel@tonic-gate 		if (bufsize > size)
52847c478bd9Sstevel@tonic-gate 			bufsize = size;
52857c478bd9Sstevel@tonic-gate 		zone_status = zone_status_get(zone);
52867c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
52877c478bd9Sstevel@tonic-gate 		    copyout(&zone_status, buf, bufsize) != 0)
52887c478bd9Sstevel@tonic-gate 			error = EFAULT;
52897c478bd9Sstevel@tonic-gate 		break;
5290f4b3ec61Sdh155122 	case ZONE_ATTR_FLAGS:
5291f4b3ec61Sdh155122 		size = sizeof (zone->zone_flags);
5292f4b3ec61Sdh155122 		if (bufsize > size)
5293f4b3ec61Sdh155122 			bufsize = size;
5294f4b3ec61Sdh155122 		flags = zone->zone_flags;
5295f4b3ec61Sdh155122 		if (buf != NULL &&
5296f4b3ec61Sdh155122 		    copyout(&flags, buf, bufsize) != 0)
5297f4b3ec61Sdh155122 			error = EFAULT;
5298f4b3ec61Sdh155122 		break;
52997c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_PRIVSET:
53007c478bd9Sstevel@tonic-gate 		size = sizeof (priv_set_t);
53017c478bd9Sstevel@tonic-gate 		if (bufsize > size)
53027c478bd9Sstevel@tonic-gate 			bufsize = size;
53037c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
53047c478bd9Sstevel@tonic-gate 		    copyout(zone->zone_privset, buf, bufsize) != 0)
53057c478bd9Sstevel@tonic-gate 			error = EFAULT;
53067c478bd9Sstevel@tonic-gate 		break;
53077c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_UNIQID:
53087c478bd9Sstevel@tonic-gate 		size = sizeof (zone->zone_uniqid);
53097c478bd9Sstevel@tonic-gate 		if (bufsize > size)
53107c478bd9Sstevel@tonic-gate 			bufsize = size;
53117c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
53127c478bd9Sstevel@tonic-gate 		    copyout(&zone->zone_uniqid, buf, bufsize) != 0)
53137c478bd9Sstevel@tonic-gate 			error = EFAULT;
53147c478bd9Sstevel@tonic-gate 		break;
53157c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_POOLID:
53167c478bd9Sstevel@tonic-gate 		{
53177c478bd9Sstevel@tonic-gate 			pool_t *pool;
53187c478bd9Sstevel@tonic-gate 			poolid_t poolid;
53197c478bd9Sstevel@tonic-gate 
53207c478bd9Sstevel@tonic-gate 			if (pool_lock_intr() != 0) {
53217c478bd9Sstevel@tonic-gate 				error = EINTR;
53227c478bd9Sstevel@tonic-gate 				break;
53237c478bd9Sstevel@tonic-gate 			}
53247c478bd9Sstevel@tonic-gate 			pool = zone_pool_get(zone);
53257c478bd9Sstevel@tonic-gate 			poolid = pool->pool_id;
53267c478bd9Sstevel@tonic-gate 			pool_unlock();
53277c478bd9Sstevel@tonic-gate 			size = sizeof (poolid);
53287c478bd9Sstevel@tonic-gate 			if (bufsize > size)
53297c478bd9Sstevel@tonic-gate 				bufsize = size;
53307c478bd9Sstevel@tonic-gate 			if (buf != NULL && copyout(&poolid, buf, size) != 0)
53317c478bd9Sstevel@tonic-gate 				error = EFAULT;
53327c478bd9Sstevel@tonic-gate 		}
53337c478bd9Sstevel@tonic-gate 		break;
533445916cd2Sjpk 	case ZONE_ATTR_SLBL:
533545916cd2Sjpk 		size = sizeof (bslabel_t);
533645916cd2Sjpk 		if (bufsize > size)
533745916cd2Sjpk 			bufsize = size;
533845916cd2Sjpk 		if (zone->zone_slabel == NULL)
533945916cd2Sjpk 			error = EINVAL;
534045916cd2Sjpk 		else if (buf != NULL &&
534145916cd2Sjpk 		    copyout(label2bslabel(zone->zone_slabel), buf,
534245916cd2Sjpk 		    bufsize) != 0)
534345916cd2Sjpk 			error = EFAULT;
534445916cd2Sjpk 		break;
53457c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_INITPID:
53467c478bd9Sstevel@tonic-gate 		size = sizeof (initpid);
53477c478bd9Sstevel@tonic-gate 		if (bufsize > size)
53487c478bd9Sstevel@tonic-gate 			bufsize = size;
53497c478bd9Sstevel@tonic-gate 		initpid = zone->zone_proc_initpid;
53507c478bd9Sstevel@tonic-gate 		if (initpid == -1) {
53517c478bd9Sstevel@tonic-gate 			error = ESRCH;
53527c478bd9Sstevel@tonic-gate 			break;
53537c478bd9Sstevel@tonic-gate 		}
53547c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
53557c478bd9Sstevel@tonic-gate 		    copyout(&initpid, buf, bufsize) != 0)
53567c478bd9Sstevel@tonic-gate 			error = EFAULT;
53577c478bd9Sstevel@tonic-gate 		break;
53589acbbeafSnn35248 	case ZONE_ATTR_BRAND:
53599acbbeafSnn35248 		size = strlen(zone->zone_brand->b_name) + 1;
53609acbbeafSnn35248 
53619acbbeafSnn35248 		if (bufsize > size)
53629acbbeafSnn35248 			bufsize = size;
53639acbbeafSnn35248 		if (buf != NULL) {
53649acbbeafSnn35248 			err = copyoutstr(zone->zone_brand->b_name, buf,
53659acbbeafSnn35248 			    bufsize, NULL);
53669acbbeafSnn35248 			if (err != 0 && err != ENAMETOOLONG)
53679acbbeafSnn35248 				error = EFAULT;
53689acbbeafSnn35248 		}
53699acbbeafSnn35248 		break;
53703f2f09c1Sdp 	case ZONE_ATTR_INITNAME:
53713f2f09c1Sdp 		size = strlen(zone->zone_initname) + 1;
53723f2f09c1Sdp 		if (bufsize > size)
53733f2f09c1Sdp 			bufsize = size;
53743f2f09c1Sdp 		if (buf != NULL) {
53753f2f09c1Sdp 			err = copyoutstr(zone->zone_initname, buf, bufsize,
53763f2f09c1Sdp 			    NULL);
53773f2f09c1Sdp 			if (err != 0 && err != ENAMETOOLONG)
53783f2f09c1Sdp 				error = EFAULT;
53793f2f09c1Sdp 		}
53803f2f09c1Sdp 		break;
53813f2f09c1Sdp 	case ZONE_ATTR_BOOTARGS:
53823f2f09c1Sdp 		if (zone->zone_bootargs == NULL)
53833f2f09c1Sdp 			outstr = "";
53843f2f09c1Sdp 		else
53853f2f09c1Sdp 			outstr = zone->zone_bootargs;
53863f2f09c1Sdp 		size = strlen(outstr) + 1;
53873f2f09c1Sdp 		if (bufsize > size)
53883f2f09c1Sdp 			bufsize = size;
53893f2f09c1Sdp 		if (buf != NULL) {
53903f2f09c1Sdp 			err = copyoutstr(outstr, buf, bufsize, NULL);
53913f2f09c1Sdp 			if (err != 0 && err != ENAMETOOLONG)
53923f2f09c1Sdp 				error = EFAULT;
53933f2f09c1Sdp 		}
53943f2f09c1Sdp 		break;
53950209230bSgjelinek 	case ZONE_ATTR_PHYS_MCAP:
53960209230bSgjelinek 		size = sizeof (zone->zone_phys_mcap);
53970209230bSgjelinek 		if (bufsize > size)
53980209230bSgjelinek 			bufsize = size;
53990209230bSgjelinek 		if (buf != NULL &&
54000209230bSgjelinek 		    copyout(&zone->zone_phys_mcap, buf, bufsize) != 0)
54010209230bSgjelinek 			error = EFAULT;
54020209230bSgjelinek 		break;
54030209230bSgjelinek 	case ZONE_ATTR_SCHED_CLASS:
54040209230bSgjelinek 		mutex_enter(&class_lock);
54050209230bSgjelinek 
54060209230bSgjelinek 		if (zone->zone_defaultcid >= loaded_classes)
54070209230bSgjelinek 			outstr = "";
54080209230bSgjelinek 		else
54090209230bSgjelinek 			outstr = sclass[zone->zone_defaultcid].cl_name;
54100209230bSgjelinek 		size = strlen(outstr) + 1;
54110209230bSgjelinek 		if (bufsize > size)
54120209230bSgjelinek 			bufsize = size;
54130209230bSgjelinek 		if (buf != NULL) {
54140209230bSgjelinek 			err = copyoutstr(outstr, buf, bufsize, NULL);
54150209230bSgjelinek 			if (err != 0 && err != ENAMETOOLONG)
54160209230bSgjelinek 				error = EFAULT;
54170209230bSgjelinek 		}
54180209230bSgjelinek 
54190209230bSgjelinek 		mutex_exit(&class_lock);
54200209230bSgjelinek 		break;
54215679c89fSjv227347 	case ZONE_ATTR_HOSTID:
54225679c89fSjv227347 		if (zone->zone_hostid != HW_INVALID_HOSTID &&
54235679c89fSjv227347 		    bufsize == sizeof (zone->zone_hostid)) {
54245679c89fSjv227347 			size = sizeof (zone->zone_hostid);
54255679c89fSjv227347 			if (buf != NULL && copyout(&zone->zone_hostid, buf,
54265679c89fSjv227347 			    bufsize) != 0)
54275679c89fSjv227347 				error = EFAULT;
54285679c89fSjv227347 		} else {
54295679c89fSjv227347 			error = EINVAL;
54305679c89fSjv227347 		}
54315679c89fSjv227347 		break;
54320fbb751dSJohn Levon 	case ZONE_ATTR_FS_ALLOWED:
54330fbb751dSJohn Levon 		if (zone->zone_fs_allowed == NULL)
54340fbb751dSJohn Levon 			outstr = "";
54350fbb751dSJohn Levon 		else
54360fbb751dSJohn Levon 			outstr = zone->zone_fs_allowed;
54370fbb751dSJohn Levon 		size = strlen(outstr) + 1;
54380fbb751dSJohn Levon 		if (bufsize > size)
54390fbb751dSJohn Levon 			bufsize = size;
54400fbb751dSJohn Levon 		if (buf != NULL) {
54410fbb751dSJohn Levon 			err = copyoutstr(outstr, buf, bufsize, NULL);
54420fbb751dSJohn Levon 			if (err != 0 && err != ENAMETOOLONG)
54430fbb751dSJohn Levon 				error = EFAULT;
54440fbb751dSJohn Levon 		}
54450fbb751dSJohn Levon 		break;
5446550b6e40SSowmini Varadhan 	case ZONE_ATTR_NETWORK:
5447550b6e40SSowmini Varadhan 		zbuf = kmem_alloc(bufsize, KM_SLEEP);
5448550b6e40SSowmini Varadhan 		if (copyin(buf, zbuf, bufsize) != 0) {
5449550b6e40SSowmini Varadhan 			error = EFAULT;
5450550b6e40SSowmini Varadhan 		} else {
5451550b6e40SSowmini Varadhan 			error = zone_get_network(zoneid, zbuf);
5452550b6e40SSowmini Varadhan 			if (error == 0 && copyout(zbuf, buf, bufsize) != 0)
5453550b6e40SSowmini Varadhan 				error = EFAULT;
5454550b6e40SSowmini Varadhan 		}
5455550b6e40SSowmini Varadhan 		kmem_free(zbuf, bufsize);
5456550b6e40SSowmini Varadhan 		break;
54577c478bd9Sstevel@tonic-gate 	default:
54589acbbeafSnn35248 		if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone)) {
54599acbbeafSnn35248 			size = bufsize;
54609acbbeafSnn35248 			error = ZBROP(zone)->b_getattr(zone, attr, buf, &size);
54619acbbeafSnn35248 		} else {
54627c478bd9Sstevel@tonic-gate 			error = EINVAL;
54637c478bd9Sstevel@tonic-gate 		}
54649acbbeafSnn35248 	}
54657c478bd9Sstevel@tonic-gate 	zone_rele(zone);
54667c478bd9Sstevel@tonic-gate 
54677c478bd9Sstevel@tonic-gate 	if (error)
54687c478bd9Sstevel@tonic-gate 		return (set_errno(error));
54697c478bd9Sstevel@tonic-gate 	return ((ssize_t)size);
54707c478bd9Sstevel@tonic-gate }
54717c478bd9Sstevel@tonic-gate 
54727c478bd9Sstevel@tonic-gate /*
54733f2f09c1Sdp  * Systemcall entry point for zone_setattr(2).
54743f2f09c1Sdp  */
54753f2f09c1Sdp /*ARGSUSED*/
54763f2f09c1Sdp static int
54773f2f09c1Sdp zone_setattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
54783f2f09c1Sdp {
54793f2f09c1Sdp 	zone_t *zone;
54803f2f09c1Sdp 	zone_status_t zone_status;
54811a88ce5cSDan Price 	int err = -1;
5482550b6e40SSowmini Varadhan 	zone_net_data_t *zbuf;
54833f2f09c1Sdp 
54843f2f09c1Sdp 	if (secpolicy_zone_config(CRED()) != 0)
54853f2f09c1Sdp 		return (set_errno(EPERM));
54863f2f09c1Sdp 
54873f2f09c1Sdp 	/*
54880209230bSgjelinek 	 * Only the ZONE_ATTR_PHYS_MCAP attribute can be set on the
54890209230bSgjelinek 	 * global zone.
54903f2f09c1Sdp 	 */
54910209230bSgjelinek 	if (zoneid == GLOBAL_ZONEID && attr != ZONE_ATTR_PHYS_MCAP) {
54923f2f09c1Sdp 		return (set_errno(EINVAL));
54933f2f09c1Sdp 	}
54943f2f09c1Sdp 
54953f2f09c1Sdp 	mutex_enter(&zonehash_lock);
54963f2f09c1Sdp 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
54973f2f09c1Sdp 		mutex_exit(&zonehash_lock);
54983f2f09c1Sdp 		return (set_errno(EINVAL));
54993f2f09c1Sdp 	}
55003f2f09c1Sdp 	zone_hold(zone);
55013f2f09c1Sdp 	mutex_exit(&zonehash_lock);
55023f2f09c1Sdp 
55030209230bSgjelinek 	/*
55040209230bSgjelinek 	 * At present most attributes can only be set on non-running,
55050209230bSgjelinek 	 * non-global zones.
55060209230bSgjelinek 	 */
55073f2f09c1Sdp 	zone_status = zone_status_get(zone);
55081a88ce5cSDan Price 	if (attr != ZONE_ATTR_PHYS_MCAP && zone_status > ZONE_IS_READY) {
55091a88ce5cSDan Price 		err = EINVAL;
55103f2f09c1Sdp 		goto done;
55111a88ce5cSDan Price 	}
55123f2f09c1Sdp 
55133f2f09c1Sdp 	switch (attr) {
55143f2f09c1Sdp 	case ZONE_ATTR_INITNAME:
55153f2f09c1Sdp 		err = zone_set_initname(zone, (const char *)buf);
55163f2f09c1Sdp 		break;
55173f2f09c1Sdp 	case ZONE_ATTR_BOOTARGS:
55183f2f09c1Sdp 		err = zone_set_bootargs(zone, (const char *)buf);
55193f2f09c1Sdp 		break;
55209acbbeafSnn35248 	case ZONE_ATTR_BRAND:
552159f2ff5cSedp 		err = zone_set_brand(zone, (const char *)buf);
55229acbbeafSnn35248 		break;
55230fbb751dSJohn Levon 	case ZONE_ATTR_FS_ALLOWED:
55240fbb751dSJohn Levon 		err = zone_set_fs_allowed(zone, (const char *)buf);
55250fbb751dSJohn Levon 		break;
55260209230bSgjelinek 	case ZONE_ATTR_PHYS_MCAP:
55270209230bSgjelinek 		err = zone_set_phys_mcap(zone, (const uint64_t *)buf);
55280209230bSgjelinek 		break;
55290209230bSgjelinek 	case ZONE_ATTR_SCHED_CLASS:
55300209230bSgjelinek 		err = zone_set_sched_class(zone, (const char *)buf);
55310209230bSgjelinek 		break;
55325679c89fSjv227347 	case ZONE_ATTR_HOSTID:
55335679c89fSjv227347 		if (bufsize == sizeof (zone->zone_hostid)) {
55345679c89fSjv227347 			if (copyin(buf, &zone->zone_hostid, bufsize) == 0)
55355679c89fSjv227347 				err = 0;
55365679c89fSjv227347 			else
55375679c89fSjv227347 				err = EFAULT;
55385679c89fSjv227347 		} else {
55395679c89fSjv227347 			err = EINVAL;
55405679c89fSjv227347 		}
55415679c89fSjv227347 		break;
5542550b6e40SSowmini Varadhan 	case ZONE_ATTR_NETWORK:
5543550b6e40SSowmini Varadhan 		if (bufsize > (PIPE_BUF + sizeof (zone_net_data_t))) {
5544550b6e40SSowmini Varadhan 			err = EINVAL;
55451a88ce5cSDan Price 			break;
5546550b6e40SSowmini Varadhan 		}
5547550b6e40SSowmini Varadhan 		zbuf = kmem_alloc(bufsize, KM_SLEEP);
5548550b6e40SSowmini Varadhan 		if (copyin(buf, zbuf, bufsize) != 0) {
55491a88ce5cSDan Price 			kmem_free(zbuf, bufsize);
5550550b6e40SSowmini Varadhan 			err = EFAULT;
55511a88ce5cSDan Price 			break;
5552550b6e40SSowmini Varadhan 		}
5553550b6e40SSowmini Varadhan 		err = zone_set_network(zoneid, zbuf);
5554550b6e40SSowmini Varadhan 		kmem_free(zbuf, bufsize);
5555550b6e40SSowmini Varadhan 		break;
55563f2f09c1Sdp 	default:
55579acbbeafSnn35248 		if ((attr >= ZONE_ATTR_BRAND_ATTRS) && ZONE_IS_BRANDED(zone))
55589acbbeafSnn35248 			err = ZBROP(zone)->b_setattr(zone, attr, buf, bufsize);
55599acbbeafSnn35248 		else
55603f2f09c1Sdp 			err = EINVAL;
55613f2f09c1Sdp 	}
55623f2f09c1Sdp 
55633f2f09c1Sdp done:
55643f2f09c1Sdp 	zone_rele(zone);
55651a88ce5cSDan Price 	ASSERT(err != -1);
55663f2f09c1Sdp 	return (err != 0 ? set_errno(err) : 0);
55673f2f09c1Sdp }
55683f2f09c1Sdp 
55693f2f09c1Sdp /*
55707c478bd9Sstevel@tonic-gate  * Return zero if the process has at least one vnode mapped in to its
55717c478bd9Sstevel@tonic-gate  * address space which shouldn't be allowed to change zones.
55720209230bSgjelinek  *
55730209230bSgjelinek  * Also return zero if the process has any shared mappings which reserve
55740209230bSgjelinek  * swap.  This is because the counting for zone.max-swap does not allow swap
5575da6c28aaSamw  * reservation to be shared between zones.  zone swap reservation is counted
55760209230bSgjelinek  * on zone->zone_max_swap.
55777c478bd9Sstevel@tonic-gate  */
55787c478bd9Sstevel@tonic-gate static int
55797c478bd9Sstevel@tonic-gate as_can_change_zones(void)
55807c478bd9Sstevel@tonic-gate {
55817c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
55827c478bd9Sstevel@tonic-gate 	struct seg *seg;
55837c478bd9Sstevel@tonic-gate 	struct as *as = pp->p_as;
55847c478bd9Sstevel@tonic-gate 	vnode_t *vp;
55857c478bd9Sstevel@tonic-gate 	int allow = 1;
55867c478bd9Sstevel@tonic-gate 
55877c478bd9Sstevel@tonic-gate 	ASSERT(pp->p_as != &kas);
55880209230bSgjelinek 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
55897c478bd9Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
55900209230bSgjelinek 
55910209230bSgjelinek 		/*
55920209230bSgjelinek 		 * Cannot enter zone with shared anon memory which
55930209230bSgjelinek 		 * reserves swap.  See comment above.
55940209230bSgjelinek 		 */
55950209230bSgjelinek 		if (seg_can_change_zones(seg) == B_FALSE) {
55960209230bSgjelinek 			allow = 0;
55970209230bSgjelinek 			break;
55980209230bSgjelinek 		}
55997c478bd9Sstevel@tonic-gate 		/*
56007c478bd9Sstevel@tonic-gate 		 * if we can't get a backing vnode for this segment then skip
56017c478bd9Sstevel@tonic-gate 		 * it.
56027c478bd9Sstevel@tonic-gate 		 */
56037c478bd9Sstevel@tonic-gate 		vp = NULL;
56047c478bd9Sstevel@tonic-gate 		if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL)
56057c478bd9Sstevel@tonic-gate 			continue;
56067c478bd9Sstevel@tonic-gate 		if (!vn_can_change_zones(vp)) { /* bail on first match */
56077c478bd9Sstevel@tonic-gate 			allow = 0;
56087c478bd9Sstevel@tonic-gate 			break;
56097c478bd9Sstevel@tonic-gate 		}
56107c478bd9Sstevel@tonic-gate 	}
56110209230bSgjelinek 	AS_LOCK_EXIT(as, &as->a_lock);
56127c478bd9Sstevel@tonic-gate 	return (allow);
56137c478bd9Sstevel@tonic-gate }
56147c478bd9Sstevel@tonic-gate 
56157c478bd9Sstevel@tonic-gate /*
56160209230bSgjelinek  * Count swap reserved by curproc's address space
56170209230bSgjelinek  */
56180209230bSgjelinek static size_t
56190209230bSgjelinek as_swresv(void)
56200209230bSgjelinek {
56210209230bSgjelinek 	proc_t *pp = curproc;
56220209230bSgjelinek 	struct seg *seg;
56230209230bSgjelinek 	struct as *as = pp->p_as;
56240209230bSgjelinek 	size_t swap = 0;
56250209230bSgjelinek 
56260209230bSgjelinek 	ASSERT(pp->p_as != &kas);
56270209230bSgjelinek 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
56280209230bSgjelinek 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg))
56290209230bSgjelinek 		swap += seg_swresv(seg);
56300209230bSgjelinek 
56310209230bSgjelinek 	return (swap);
56320209230bSgjelinek }
56330209230bSgjelinek 
56340209230bSgjelinek /*
56357c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_enter().
56367c478bd9Sstevel@tonic-gate  *
56377c478bd9Sstevel@tonic-gate  * The current process is injected into said zone.  In the process
56387c478bd9Sstevel@tonic-gate  * it will change its project membership, privileges, rootdir/cwd,
56397c478bd9Sstevel@tonic-gate  * zone-wide rctls, and pool association to match those of the zone.
56407c478bd9Sstevel@tonic-gate  *
56417c478bd9Sstevel@tonic-gate  * The first zone_enter() called while the zone is in the ZONE_IS_READY
56427c478bd9Sstevel@tonic-gate  * state will transition it to ZONE_IS_RUNNING.  Processes may only
56437c478bd9Sstevel@tonic-gate  * enter a zone that is "ready" or "running".
56447c478bd9Sstevel@tonic-gate  */
56457c478bd9Sstevel@tonic-gate static int
56467c478bd9Sstevel@tonic-gate zone_enter(zoneid_t zoneid)
56477c478bd9Sstevel@tonic-gate {
56487c478bd9Sstevel@tonic-gate 	zone_t *zone;
56497c478bd9Sstevel@tonic-gate 	vnode_t *vp;
56507c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
56517c478bd9Sstevel@tonic-gate 	contract_t *ct;
56527c478bd9Sstevel@tonic-gate 	cont_process_t *ctp;
56537c478bd9Sstevel@tonic-gate 	task_t *tk, *oldtk;
56547c478bd9Sstevel@tonic-gate 	kproject_t *zone_proj0;
56557c478bd9Sstevel@tonic-gate 	cred_t *cr, *newcr;
56567c478bd9Sstevel@tonic-gate 	pool_t *oldpool, *newpool;
56577c478bd9Sstevel@tonic-gate 	sess_t *sp;
56587c478bd9Sstevel@tonic-gate 	uid_t uid;
56597c478bd9Sstevel@tonic-gate 	zone_status_t status;
56607c478bd9Sstevel@tonic-gate 	int err = 0;
56617c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
56620209230bSgjelinek 	size_t swap;
5663c97ad5cdSakolb 	kthread_id_t t;
56647c478bd9Sstevel@tonic-gate 
56657c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
56667c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
56677c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
56687c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
56697c478bd9Sstevel@tonic-gate 
56707c478bd9Sstevel@tonic-gate 	/*
56717c478bd9Sstevel@tonic-gate 	 * Stop all lwps so we don't need to hold a lock to look at
56727c478bd9Sstevel@tonic-gate 	 * curproc->p_zone.  This needs to happen before we grab any
56737c478bd9Sstevel@tonic-gate 	 * locks to avoid deadlock (another lwp in the process could
56747c478bd9Sstevel@tonic-gate 	 * be waiting for the held lock).
56757c478bd9Sstevel@tonic-gate 	 */
56767c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK))
56777c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
56787c478bd9Sstevel@tonic-gate 
56797c478bd9Sstevel@tonic-gate 	/*
56807c478bd9Sstevel@tonic-gate 	 * Make sure we're not changing zones with files open or mapped in
56817c478bd9Sstevel@tonic-gate 	 * to our address space which shouldn't be changing zones.
56827c478bd9Sstevel@tonic-gate 	 */
56837c478bd9Sstevel@tonic-gate 	if (!files_can_change_zones()) {
56847c478bd9Sstevel@tonic-gate 		err = EBADF;
56857c478bd9Sstevel@tonic-gate 		goto out;
56867c478bd9Sstevel@tonic-gate 	}
56877c478bd9Sstevel@tonic-gate 	if (!as_can_change_zones()) {
56887c478bd9Sstevel@tonic-gate 		err = EFAULT;
56897c478bd9Sstevel@tonic-gate 		goto out;
56907c478bd9Sstevel@tonic-gate 	}
56917c478bd9Sstevel@tonic-gate 
56927c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
56937c478bd9Sstevel@tonic-gate 	if (pp->p_zone != global_zone) {
56947c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
56957c478bd9Sstevel@tonic-gate 		err = EINVAL;
56967c478bd9Sstevel@tonic-gate 		goto out;
56977c478bd9Sstevel@tonic-gate 	}
56987c478bd9Sstevel@tonic-gate 
56997c478bd9Sstevel@tonic-gate 	zone = zone_find_all_by_id(zoneid);
57007c478bd9Sstevel@tonic-gate 	if (zone == NULL) {
57017c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
57027c478bd9Sstevel@tonic-gate 		err = EINVAL;
57037c478bd9Sstevel@tonic-gate 		goto out;
57047c478bd9Sstevel@tonic-gate 	}
57057c478bd9Sstevel@tonic-gate 
57067c478bd9Sstevel@tonic-gate 	/*
57077c478bd9Sstevel@tonic-gate 	 * To prevent processes in a zone from holding contracts on
57087c478bd9Sstevel@tonic-gate 	 * extrazonal resources, and to avoid process contract
57097c478bd9Sstevel@tonic-gate 	 * memberships which span zones, contract holders and processes
57107c478bd9Sstevel@tonic-gate 	 * which aren't the sole members of their encapsulating process
57117c478bd9Sstevel@tonic-gate 	 * contracts are not allowed to zone_enter.
57127c478bd9Sstevel@tonic-gate 	 */
57137c478bd9Sstevel@tonic-gate 	ctp = pp->p_ct_process;
57147c478bd9Sstevel@tonic-gate 	ct = &ctp->conp_contract;
57157c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
57167c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
57177c478bd9Sstevel@tonic-gate 	if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) {
57187c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
57197c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
57207c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
57217c478bd9Sstevel@tonic-gate 		err = EINVAL;
57227c478bd9Sstevel@tonic-gate 		goto out;
57237c478bd9Sstevel@tonic-gate 	}
57247c478bd9Sstevel@tonic-gate 
57257c478bd9Sstevel@tonic-gate 	/*
57267c478bd9Sstevel@tonic-gate 	 * Moreover, we don't allow processes whose encapsulating
57277c478bd9Sstevel@tonic-gate 	 * process contracts have inherited extrazonal contracts.
57287c478bd9Sstevel@tonic-gate 	 * While it would be easier to eliminate all process contracts
57297c478bd9Sstevel@tonic-gate 	 * with inherited contracts, we need to be able to give a
57307c478bd9Sstevel@tonic-gate 	 * restarted init (or other zone-penetrating process) its
57317c478bd9Sstevel@tonic-gate 	 * predecessor's contracts.
57327c478bd9Sstevel@tonic-gate 	 */
57337c478bd9Sstevel@tonic-gate 	if (ctp->conp_ninherited != 0) {
57347c478bd9Sstevel@tonic-gate 		contract_t *next;
57357c478bd9Sstevel@tonic-gate 		for (next = list_head(&ctp->conp_inherited); next;
57367c478bd9Sstevel@tonic-gate 		    next = list_next(&ctp->conp_inherited, next)) {
57377c478bd9Sstevel@tonic-gate 			if (contract_getzuniqid(next) != zone->zone_uniqid) {
57387c478bd9Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
57397c478bd9Sstevel@tonic-gate 				mutex_exit(&ct->ct_lock);
57407c478bd9Sstevel@tonic-gate 				mutex_exit(&zonehash_lock);
57417c478bd9Sstevel@tonic-gate 				err = EINVAL;
57427c478bd9Sstevel@tonic-gate 				goto out;
57437c478bd9Sstevel@tonic-gate 			}
57447c478bd9Sstevel@tonic-gate 		}
57457c478bd9Sstevel@tonic-gate 	}
57467b209c2cSacruz 
57477c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
57487c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
57497c478bd9Sstevel@tonic-gate 
57507c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
57517c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) {
57527c478bd9Sstevel@tonic-gate 		/*
57537c478bd9Sstevel@tonic-gate 		 * Can't join
57547c478bd9Sstevel@tonic-gate 		 */
57557c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
57567c478bd9Sstevel@tonic-gate 		err = EINVAL;
57577c478bd9Sstevel@tonic-gate 		goto out;
57587c478bd9Sstevel@tonic-gate 	}
57597c478bd9Sstevel@tonic-gate 
57607c478bd9Sstevel@tonic-gate 	/*
57617c478bd9Sstevel@tonic-gate 	 * Make sure new priv set is within the permitted set for caller
57627c478bd9Sstevel@tonic-gate 	 */
57637c478bd9Sstevel@tonic-gate 	if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) {
57647c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
57657c478bd9Sstevel@tonic-gate 		err = EPERM;
57667c478bd9Sstevel@tonic-gate 		goto out;
57677c478bd9Sstevel@tonic-gate 	}
57687c478bd9Sstevel@tonic-gate 	/*
57697c478bd9Sstevel@tonic-gate 	 * We want to momentarily drop zonehash_lock while we optimistically
57707c478bd9Sstevel@tonic-gate 	 * bind curproc to the pool it should be running in.  This is safe
57717c478bd9Sstevel@tonic-gate 	 * since the zone can't disappear (we have a hold on it).
57727c478bd9Sstevel@tonic-gate 	 */
57737c478bd9Sstevel@tonic-gate 	zone_hold(zone);
57747c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
57757c478bd9Sstevel@tonic-gate 
57767c478bd9Sstevel@tonic-gate 	/*
57777c478bd9Sstevel@tonic-gate 	 * Grab pool_lock to keep the pools configuration from changing
57787c478bd9Sstevel@tonic-gate 	 * and to stop ourselves from getting rebound to another pool
57797c478bd9Sstevel@tonic-gate 	 * until we join the zone.
57807c478bd9Sstevel@tonic-gate 	 */
57817c478bd9Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
57827c478bd9Sstevel@tonic-gate 		zone_rele(zone);
57837c478bd9Sstevel@tonic-gate 		err = EINTR;
57847c478bd9Sstevel@tonic-gate 		goto out;
57857c478bd9Sstevel@tonic-gate 	}
57867c478bd9Sstevel@tonic-gate 	ASSERT(secpolicy_pool(CRED()) == 0);
57877c478bd9Sstevel@tonic-gate 	/*
57887c478bd9Sstevel@tonic-gate 	 * Bind ourselves to the pool currently associated with the zone.
57897c478bd9Sstevel@tonic-gate 	 */
57907c478bd9Sstevel@tonic-gate 	oldpool = curproc->p_pool;
57917c478bd9Sstevel@tonic-gate 	newpool = zone_pool_get(zone);
57927c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED && newpool != oldpool &&
57937c478bd9Sstevel@tonic-gate 	    (err = pool_do_bind(newpool, P_PID, P_MYID,
57947c478bd9Sstevel@tonic-gate 	    POOL_BIND_ALL)) != 0) {
57957c478bd9Sstevel@tonic-gate 		pool_unlock();
57967c478bd9Sstevel@tonic-gate 		zone_rele(zone);
57977c478bd9Sstevel@tonic-gate 		goto out;
57987c478bd9Sstevel@tonic-gate 	}
57997c478bd9Sstevel@tonic-gate 
58007c478bd9Sstevel@tonic-gate 	/*
58017c478bd9Sstevel@tonic-gate 	 * Grab cpu_lock now; we'll need it later when we call
58027c478bd9Sstevel@tonic-gate 	 * task_join().
58037c478bd9Sstevel@tonic-gate 	 */
58047c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
58057c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
58067c478bd9Sstevel@tonic-gate 	/*
58077c478bd9Sstevel@tonic-gate 	 * Make sure the zone hasn't moved on since we dropped zonehash_lock.
58087c478bd9Sstevel@tonic-gate 	 */
58097c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
58107c478bd9Sstevel@tonic-gate 		/*
58117c478bd9Sstevel@tonic-gate 		 * Can't join anymore.
58127c478bd9Sstevel@tonic-gate 		 */
58137c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
58147c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
58157c478bd9Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED &&
58167c478bd9Sstevel@tonic-gate 		    newpool != oldpool)
58177c478bd9Sstevel@tonic-gate 			(void) pool_do_bind(oldpool, P_PID, P_MYID,
58187c478bd9Sstevel@tonic-gate 			    POOL_BIND_ALL);
58197c478bd9Sstevel@tonic-gate 		pool_unlock();
58207c478bd9Sstevel@tonic-gate 		zone_rele(zone);
58217c478bd9Sstevel@tonic-gate 		err = EINVAL;
58227c478bd9Sstevel@tonic-gate 		goto out;
58237c478bd9Sstevel@tonic-gate 	}
58247c478bd9Sstevel@tonic-gate 
58250209230bSgjelinek 	/*
58260209230bSgjelinek 	 * a_lock must be held while transfering locked memory and swap
58270209230bSgjelinek 	 * reservation from the global zone to the non global zone because
58280209230bSgjelinek 	 * asynchronous faults on the processes' address space can lock
58290209230bSgjelinek 	 * memory and reserve swap via MCL_FUTURE and MAP_NORESERVE
58300209230bSgjelinek 	 * segments respectively.
58310209230bSgjelinek 	 */
58320209230bSgjelinek 	AS_LOCK_ENTER(pp->as, &pp->p_as->a_lock, RW_WRITER);
58330209230bSgjelinek 	swap = as_swresv();
58347c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
58357c478bd9Sstevel@tonic-gate 	zone_proj0 = zone->zone_zsched->p_task->tk_proj;
58367c478bd9Sstevel@tonic-gate 	/* verify that we do not exceed and task or lwp limits */
58377c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
58387c478bd9Sstevel@tonic-gate 	/* add new lwps to zone and zone's proj0 */
58397c478bd9Sstevel@tonic-gate 	zone_proj0->kpj_nlwps += pp->p_lwpcnt;
58407c478bd9Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
58417c478bd9Sstevel@tonic-gate 	/* add 1 task to zone's proj0 */
58427c478bd9Sstevel@tonic-gate 	zone_proj0->kpj_ntasks += 1;
5843ff19e029SMenno Lageman 
5844ff19e029SMenno Lageman 	zone_proj0->kpj_nprocs++;
5845ff19e029SMenno Lageman 	zone->zone_nprocs++;
58467c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
58477c478bd9Sstevel@tonic-gate 
58480209230bSgjelinek 	mutex_enter(&zone->zone_mem_lock);
5849c6939658Ssl108498 	zone->zone_locked_mem += pp->p_locked_mem;
5850c6939658Ssl108498 	zone_proj0->kpj_data.kpd_locked_mem += pp->p_locked_mem;
58510209230bSgjelinek 	zone->zone_max_swap += swap;
58520209230bSgjelinek 	mutex_exit(&zone->zone_mem_lock);
5853c6939658Ssl108498 
5854c1a9a9c3Skrishna 	mutex_enter(&(zone_proj0->kpj_data.kpd_crypto_lock));
5855c1a9a9c3Skrishna 	zone_proj0->kpj_data.kpd_crypto_mem += pp->p_crypto_mem;
5856c1a9a9c3Skrishna 	mutex_exit(&(zone_proj0->kpj_data.kpd_crypto_lock));
5857c1a9a9c3Skrishna 
5858ff19e029SMenno Lageman 	/* remove lwps and process from proc's old zone and old project */
58597c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_zone->zone_nlwps_lock);
58607c478bd9Sstevel@tonic-gate 	pp->p_zone->zone_nlwps -= pp->p_lwpcnt;
58617c478bd9Sstevel@tonic-gate 	pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt;
5862ff19e029SMenno Lageman 	pp->p_task->tk_proj->kpj_nprocs--;
5863ff19e029SMenno Lageman 	pp->p_zone->zone_nprocs--;
58647c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_zone->zone_nlwps_lock);
58657c478bd9Sstevel@tonic-gate 
58660209230bSgjelinek 	mutex_enter(&pp->p_zone->zone_mem_lock);
5867c6939658Ssl108498 	pp->p_zone->zone_locked_mem -= pp->p_locked_mem;
5868c6939658Ssl108498 	pp->p_task->tk_proj->kpj_data.kpd_locked_mem -= pp->p_locked_mem;
58690209230bSgjelinek 	pp->p_zone->zone_max_swap -= swap;
58700209230bSgjelinek 	mutex_exit(&pp->p_zone->zone_mem_lock);
5871c6939658Ssl108498 
5872c1a9a9c3Skrishna 	mutex_enter(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock));
5873c1a9a9c3Skrishna 	pp->p_task->tk_proj->kpj_data.kpd_crypto_mem -= pp->p_crypto_mem;
5874c1a9a9c3Skrishna 	mutex_exit(&(pp->p_task->tk_proj->kpj_data.kpd_crypto_lock));
5875c1a9a9c3Skrishna 
5876bb5ca623SVamsi Nagineni 	pp->p_flag |= SZONETOP;
5877bb5ca623SVamsi Nagineni 	pp->p_zone = zone;
5878c6939658Ssl108498 	mutex_exit(&pp->p_lock);
58790209230bSgjelinek 	AS_LOCK_EXIT(pp->p_as, &pp->p_as->a_lock);
5880c6939658Ssl108498 
58817c478bd9Sstevel@tonic-gate 	/*
58827c478bd9Sstevel@tonic-gate 	 * Joining the zone cannot fail from now on.
58837c478bd9Sstevel@tonic-gate 	 *
58847c478bd9Sstevel@tonic-gate 	 * This means that a lot of the following code can be commonized and
58857c478bd9Sstevel@tonic-gate 	 * shared with zsched().
58867c478bd9Sstevel@tonic-gate 	 */
58877c478bd9Sstevel@tonic-gate 
58887c478bd9Sstevel@tonic-gate 	/*
58897b209c2cSacruz 	 * If the process contract fmri was inherited, we need to
58907b209c2cSacruz 	 * flag this so that any contract status will not leak
58917b209c2cSacruz 	 * extra zone information, svc_fmri in this case
58927b209c2cSacruz 	 */
58937b209c2cSacruz 	if (ctp->conp_svc_ctid != ct->ct_id) {
58947b209c2cSacruz 		mutex_enter(&ct->ct_lock);
58957b209c2cSacruz 		ctp->conp_svc_zone_enter = ct->ct_id;
58967b209c2cSacruz 		mutex_exit(&ct->ct_lock);
58977b209c2cSacruz 	}
58987b209c2cSacruz 
58997b209c2cSacruz 	/*
59007c478bd9Sstevel@tonic-gate 	 * Reset the encapsulating process contract's zone.
59017c478bd9Sstevel@tonic-gate 	 */
59027c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID);
59037c478bd9Sstevel@tonic-gate 	contract_setzuniqid(ct, zone->zone_uniqid);
59047c478bd9Sstevel@tonic-gate 
59057c478bd9Sstevel@tonic-gate 	/*
59067c478bd9Sstevel@tonic-gate 	 * Create a new task and associate the process with the project keyed
59077c478bd9Sstevel@tonic-gate 	 * by (projid,zoneid).
59087c478bd9Sstevel@tonic-gate 	 *
59097c478bd9Sstevel@tonic-gate 	 * We might as well be in project 0; the global zone's projid doesn't
59107c478bd9Sstevel@tonic-gate 	 * make much sense in a zone anyhow.
59117c478bd9Sstevel@tonic-gate 	 *
59127c478bd9Sstevel@tonic-gate 	 * This also increments zone_ntasks, and returns with p_lock held.
59137c478bd9Sstevel@tonic-gate 	 */
59147c478bd9Sstevel@tonic-gate 	tk = task_create(0, zone);
59157c478bd9Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
59167c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
59177c478bd9Sstevel@tonic-gate 
59187c478bd9Sstevel@tonic-gate 	/*
59197c478bd9Sstevel@tonic-gate 	 * call RCTLOP_SET functions on this proc
59207c478bd9Sstevel@tonic-gate 	 */
59217c478bd9Sstevel@tonic-gate 	e.rcep_p.zone = zone;
59227c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
59237c478bd9Sstevel@tonic-gate 	(void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL,
59247c478bd9Sstevel@tonic-gate 	    RCD_CALLBACK);
59257c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
59267c478bd9Sstevel@tonic-gate 
59277c478bd9Sstevel@tonic-gate 	/*
59287c478bd9Sstevel@tonic-gate 	 * We don't need to hold any of zsched's locks here; not only do we know
59297c478bd9Sstevel@tonic-gate 	 * the process and zone aren't going away, we know its session isn't
59307c478bd9Sstevel@tonic-gate 	 * changing either.
59317c478bd9Sstevel@tonic-gate 	 *
59327c478bd9Sstevel@tonic-gate 	 * By joining zsched's session here, we mimic the behavior in the
59337c478bd9Sstevel@tonic-gate 	 * global zone of init's sid being the pid of sched.  We extend this
59347c478bd9Sstevel@tonic-gate 	 * to all zlogin-like zone_enter()'ing processes as well.
59357c478bd9Sstevel@tonic-gate 	 */
59367c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
59377c478bd9Sstevel@tonic-gate 	sp = zone->zone_zsched->p_sessp;
59389acbbeafSnn35248 	sess_hold(zone->zone_zsched);
59397c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
59407c478bd9Sstevel@tonic-gate 	pgexit(pp);
59419acbbeafSnn35248 	sess_rele(pp->p_sessp, B_TRUE);
59427c478bd9Sstevel@tonic-gate 	pp->p_sessp = sp;
59437c478bd9Sstevel@tonic-gate 	pgjoin(pp, zone->zone_zsched->p_pidp);
59440209230bSgjelinek 
59450209230bSgjelinek 	/*
5946c97ad5cdSakolb 	 * If any threads are scheduled to be placed on zone wait queue they
5947c97ad5cdSakolb 	 * should abandon the idea since the wait queue is changing.
5948c97ad5cdSakolb 	 * We need to be holding pidlock & p_lock to do this.
5949c97ad5cdSakolb 	 */
5950c97ad5cdSakolb 	if ((t = pp->p_tlist) != NULL) {
5951c97ad5cdSakolb 		do {
5952c97ad5cdSakolb 			thread_lock(t);
5953c97ad5cdSakolb 			/*
5954c97ad5cdSakolb 			 * Kick this thread so that he doesn't sit
5955c97ad5cdSakolb 			 * on a wrong wait queue.
5956c97ad5cdSakolb 			 */
5957c97ad5cdSakolb 			if (ISWAITING(t))
5958c97ad5cdSakolb 				setrun_locked(t);
5959c97ad5cdSakolb 
5960c97ad5cdSakolb 			if (t->t_schedflag & TS_ANYWAITQ)
5961c97ad5cdSakolb 				t->t_schedflag &= ~ TS_ANYWAITQ;
5962c97ad5cdSakolb 
5963c97ad5cdSakolb 			thread_unlock(t);
5964c97ad5cdSakolb 		} while ((t = t->t_forw) != pp->p_tlist);
5965c97ad5cdSakolb 	}
5966c97ad5cdSakolb 
5967c97ad5cdSakolb 	/*
59680209230bSgjelinek 	 * If there is a default scheduling class for the zone and it is not
59690209230bSgjelinek 	 * the class we are currently in, change all of the threads in the
59700209230bSgjelinek 	 * process to the new class.  We need to be holding pidlock & p_lock
59710209230bSgjelinek 	 * when we call parmsset so this is a good place to do it.
59720209230bSgjelinek 	 */
59730209230bSgjelinek 	if (zone->zone_defaultcid > 0 &&
59740209230bSgjelinek 	    zone->zone_defaultcid != curthread->t_cid) {
59750209230bSgjelinek 		pcparms_t pcparms;
59760209230bSgjelinek 
59770209230bSgjelinek 		pcparms.pc_cid = zone->zone_defaultcid;
59780209230bSgjelinek 		pcparms.pc_clparms[0] = 0;
59790209230bSgjelinek 
59800209230bSgjelinek 		/*
59810209230bSgjelinek 		 * If setting the class fails, we still want to enter the zone.
59820209230bSgjelinek 		 */
59830209230bSgjelinek 		if ((t = pp->p_tlist) != NULL) {
59840209230bSgjelinek 			do {
59850209230bSgjelinek 				(void) parmsset(&pcparms, t);
59860209230bSgjelinek 			} while ((t = t->t_forw) != pp->p_tlist);
59870209230bSgjelinek 		}
59880209230bSgjelinek 	}
59890209230bSgjelinek 
59907c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
59917c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
59927c478bd9Sstevel@tonic-gate 
59937c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
59947c478bd9Sstevel@tonic-gate 	/*
59957c478bd9Sstevel@tonic-gate 	 * We're firmly in the zone; let pools progress.
59967c478bd9Sstevel@tonic-gate 	 */
59977c478bd9Sstevel@tonic-gate 	pool_unlock();
59987c478bd9Sstevel@tonic-gate 	task_rele(oldtk);
59997c478bd9Sstevel@tonic-gate 	/*
60007c478bd9Sstevel@tonic-gate 	 * We don't need to retain a hold on the zone since we already
60017c478bd9Sstevel@tonic-gate 	 * incremented zone_ntasks, so the zone isn't going anywhere.
60027c478bd9Sstevel@tonic-gate 	 */
60037c478bd9Sstevel@tonic-gate 	zone_rele(zone);
60047c478bd9Sstevel@tonic-gate 
60057c478bd9Sstevel@tonic-gate 	/*
60067c478bd9Sstevel@tonic-gate 	 * Chroot
60077c478bd9Sstevel@tonic-gate 	 */
60087c478bd9Sstevel@tonic-gate 	vp = zone->zone_rootvp;
60097c478bd9Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_cdir, pp);
60107c478bd9Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_rdir, pp);
60117c478bd9Sstevel@tonic-gate 
60127c478bd9Sstevel@tonic-gate 	/*
60137c478bd9Sstevel@tonic-gate 	 * Change process credentials
60147c478bd9Sstevel@tonic-gate 	 */
60157c478bd9Sstevel@tonic-gate 	newcr = cralloc();
60167c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
60177c478bd9Sstevel@tonic-gate 	cr = pp->p_cred;
60187c478bd9Sstevel@tonic-gate 	crcopy_to(cr, newcr);
60197c478bd9Sstevel@tonic-gate 	crsetzone(newcr, zone);
60207c478bd9Sstevel@tonic-gate 	pp->p_cred = newcr;
60217c478bd9Sstevel@tonic-gate 
60227c478bd9Sstevel@tonic-gate 	/*
60237c478bd9Sstevel@tonic-gate 	 * Restrict all process privilege sets to zone limit
60247c478bd9Sstevel@tonic-gate 	 */
60257c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(newcr));
60267c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(newcr));
60277c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(newcr));
60287c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(newcr));
60297c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
60307c478bd9Sstevel@tonic-gate 	crset(pp, newcr);
60317c478bd9Sstevel@tonic-gate 
60327c478bd9Sstevel@tonic-gate 	/*
60337c478bd9Sstevel@tonic-gate 	 * Adjust upcount to reflect zone entry.
60347c478bd9Sstevel@tonic-gate 	 */
60357c478bd9Sstevel@tonic-gate 	uid = crgetruid(newcr);
60367c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
60377c478bd9Sstevel@tonic-gate 	upcount_dec(uid, GLOBAL_ZONEID);
60387c478bd9Sstevel@tonic-gate 	upcount_inc(uid, zoneid);
60397c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
60407c478bd9Sstevel@tonic-gate 
60417c478bd9Sstevel@tonic-gate 	/*
60427c478bd9Sstevel@tonic-gate 	 * Set up core file path and content.
60437c478bd9Sstevel@tonic-gate 	 */
60447c478bd9Sstevel@tonic-gate 	set_core_defaults();
60457c478bd9Sstevel@tonic-gate 
60467c478bd9Sstevel@tonic-gate out:
60477c478bd9Sstevel@tonic-gate 	/*
60487c478bd9Sstevel@tonic-gate 	 * Let the other lwps continue.
60497c478bd9Sstevel@tonic-gate 	 */
60507c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
60517c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
60527c478bd9Sstevel@tonic-gate 		continuelwps(pp);
60537c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
60547c478bd9Sstevel@tonic-gate 
60557c478bd9Sstevel@tonic-gate 	return (err != 0 ? set_errno(err) : 0);
60567c478bd9Sstevel@tonic-gate }
60577c478bd9Sstevel@tonic-gate 
60587c478bd9Sstevel@tonic-gate /*
60597c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_list(2).
60607c478bd9Sstevel@tonic-gate  *
60617c478bd9Sstevel@tonic-gate  * Processes running in a (non-global) zone only see themselves.
606245916cd2Sjpk  * On labeled systems, they see all zones whose label they dominate.
60637c478bd9Sstevel@tonic-gate  */
60647c478bd9Sstevel@tonic-gate static int
60657c478bd9Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones)
60667c478bd9Sstevel@tonic-gate {
60677c478bd9Sstevel@tonic-gate 	zoneid_t *zoneids;
606848451833Scarlsonj 	zone_t *zone, *myzone;
60697c478bd9Sstevel@tonic-gate 	uint_t user_nzones, real_nzones;
607045916cd2Sjpk 	uint_t domi_nzones;
607145916cd2Sjpk 	int error;
60727c478bd9Sstevel@tonic-gate 
60737c478bd9Sstevel@tonic-gate 	if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0)
60747c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
60757c478bd9Sstevel@tonic-gate 
607648451833Scarlsonj 	myzone = curproc->p_zone;
607748451833Scarlsonj 	if (myzone != global_zone) {
607845916cd2Sjpk 		bslabel_t *mybslab;
607945916cd2Sjpk 
608045916cd2Sjpk 		if (!is_system_labeled()) {
60817c478bd9Sstevel@tonic-gate 			/* just return current zone */
608245916cd2Sjpk 			real_nzones = domi_nzones = 1;
60837c478bd9Sstevel@tonic-gate 			zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP);
608448451833Scarlsonj 			zoneids[0] = myzone->zone_id;
60857c478bd9Sstevel@tonic-gate 		} else {
608645916cd2Sjpk 			/* return all zones that are dominated */
60877c478bd9Sstevel@tonic-gate 			mutex_enter(&zonehash_lock);
60887c478bd9Sstevel@tonic-gate 			real_nzones = zonecount;
608945916cd2Sjpk 			domi_nzones = 0;
609045916cd2Sjpk 			if (real_nzones > 0) {
609145916cd2Sjpk 				zoneids = kmem_alloc(real_nzones *
609245916cd2Sjpk 				    sizeof (zoneid_t), KM_SLEEP);
609348451833Scarlsonj 				mybslab = label2bslabel(myzone->zone_slabel);
609445916cd2Sjpk 				for (zone = list_head(&zone_active);
609545916cd2Sjpk 				    zone != NULL;
609645916cd2Sjpk 				    zone = list_next(&zone_active, zone)) {
609745916cd2Sjpk 					if (zone->zone_id == GLOBAL_ZONEID)
609845916cd2Sjpk 						continue;
609948451833Scarlsonj 					if (zone != myzone &&
610048451833Scarlsonj 					    (zone->zone_flags & ZF_IS_SCRATCH))
610148451833Scarlsonj 						continue;
610248451833Scarlsonj 					/*
610348451833Scarlsonj 					 * Note that a label always dominates
610448451833Scarlsonj 					 * itself, so myzone is always included
610548451833Scarlsonj 					 * in the list.
610648451833Scarlsonj 					 */
610745916cd2Sjpk 					if (bldominates(mybslab,
610845916cd2Sjpk 					    label2bslabel(zone->zone_slabel))) {
610945916cd2Sjpk 						zoneids[domi_nzones++] =
611045916cd2Sjpk 						    zone->zone_id;
611145916cd2Sjpk 					}
611245916cd2Sjpk 				}
611345916cd2Sjpk 			}
611445916cd2Sjpk 			mutex_exit(&zonehash_lock);
611545916cd2Sjpk 		}
611645916cd2Sjpk 	} else {
611745916cd2Sjpk 		mutex_enter(&zonehash_lock);
611845916cd2Sjpk 		real_nzones = zonecount;
611945916cd2Sjpk 		domi_nzones = 0;
612045916cd2Sjpk 		if (real_nzones > 0) {
61217c478bd9Sstevel@tonic-gate 			zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t),
61227c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
61237c478bd9Sstevel@tonic-gate 			for (zone = list_head(&zone_active); zone != NULL;
61247c478bd9Sstevel@tonic-gate 			    zone = list_next(&zone_active, zone))
612545916cd2Sjpk 				zoneids[domi_nzones++] = zone->zone_id;
612645916cd2Sjpk 			ASSERT(domi_nzones == real_nzones);
61277c478bd9Sstevel@tonic-gate 		}
61287c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
61297c478bd9Sstevel@tonic-gate 	}
61307c478bd9Sstevel@tonic-gate 
613145916cd2Sjpk 	/*
613245916cd2Sjpk 	 * If user has allocated space for fewer entries than we found, then
613345916cd2Sjpk 	 * return only up to his limit.  Either way, tell him exactly how many
613445916cd2Sjpk 	 * we found.
613545916cd2Sjpk 	 */
613645916cd2Sjpk 	if (domi_nzones < user_nzones)
613745916cd2Sjpk 		user_nzones = domi_nzones;
613845916cd2Sjpk 	error = 0;
613945916cd2Sjpk 	if (copyout(&domi_nzones, numzones, sizeof (uint_t)) != 0) {
61407c478bd9Sstevel@tonic-gate 		error = EFAULT;
614145916cd2Sjpk 	} else if (zoneidlist != NULL && user_nzones != 0) {
61427c478bd9Sstevel@tonic-gate 		if (copyout(zoneids, zoneidlist,
61437c478bd9Sstevel@tonic-gate 		    user_nzones * sizeof (zoneid_t)) != 0)
61447c478bd9Sstevel@tonic-gate 			error = EFAULT;
61457c478bd9Sstevel@tonic-gate 	}
61467c478bd9Sstevel@tonic-gate 
614745916cd2Sjpk 	if (real_nzones > 0)
61487c478bd9Sstevel@tonic-gate 		kmem_free(zoneids, real_nzones * sizeof (zoneid_t));
61497c478bd9Sstevel@tonic-gate 
615045916cd2Sjpk 	if (error != 0)
61517c478bd9Sstevel@tonic-gate 		return (set_errno(error));
61527c478bd9Sstevel@tonic-gate 	else
61537c478bd9Sstevel@tonic-gate 		return (0);
61547c478bd9Sstevel@tonic-gate }
61557c478bd9Sstevel@tonic-gate 
61567c478bd9Sstevel@tonic-gate /*
61577c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_lookup(2).
61587c478bd9Sstevel@tonic-gate  *
615945916cd2Sjpk  * Non-global zones are only able to see themselves and (on labeled systems)
616045916cd2Sjpk  * the zones they dominate.
61617c478bd9Sstevel@tonic-gate  */
61627c478bd9Sstevel@tonic-gate static zoneid_t
61637c478bd9Sstevel@tonic-gate zone_lookup(const char *zone_name)
61647c478bd9Sstevel@tonic-gate {
61657c478bd9Sstevel@tonic-gate 	char *kname;
61667c478bd9Sstevel@tonic-gate 	zone_t *zone;
61677c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
61687c478bd9Sstevel@tonic-gate 	int err;
61697c478bd9Sstevel@tonic-gate 
61707c478bd9Sstevel@tonic-gate 	if (zone_name == NULL) {
61717c478bd9Sstevel@tonic-gate 		/* return caller's zone id */
61727c478bd9Sstevel@tonic-gate 		return (getzoneid());
61737c478bd9Sstevel@tonic-gate 	}
61747c478bd9Sstevel@tonic-gate 
61757c478bd9Sstevel@tonic-gate 	kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
61767c478bd9Sstevel@tonic-gate 	if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) {
61777c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
61787c478bd9Sstevel@tonic-gate 		return (set_errno(err));
61797c478bd9Sstevel@tonic-gate 	}
61807c478bd9Sstevel@tonic-gate 
61817c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
61827c478bd9Sstevel@tonic-gate 	zone = zone_find_all_by_name(kname);
61837c478bd9Sstevel@tonic-gate 	kmem_free(kname, ZONENAME_MAX);
618445916cd2Sjpk 	/*
618545916cd2Sjpk 	 * In a non-global zone, can only lookup global and own name.
618645916cd2Sjpk 	 * In Trusted Extensions zone label dominance rules apply.
618745916cd2Sjpk 	 */
618845916cd2Sjpk 	if (zone == NULL ||
618945916cd2Sjpk 	    zone_status_get(zone) < ZONE_IS_READY ||
619045916cd2Sjpk 	    !zone_list_access(zone)) {
61917c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
61927c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
619345916cd2Sjpk 	} else {
61947c478bd9Sstevel@tonic-gate 		zoneid = zone->zone_id;
61957c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
61967c478bd9Sstevel@tonic-gate 		return (zoneid);
61977c478bd9Sstevel@tonic-gate 	}
619845916cd2Sjpk }
61997c478bd9Sstevel@tonic-gate 
6200821c4a97Sdp static int
6201821c4a97Sdp zone_version(int *version_arg)
6202821c4a97Sdp {
6203821c4a97Sdp 	int version = ZONE_SYSCALL_API_VERSION;
6204821c4a97Sdp 
6205821c4a97Sdp 	if (copyout(&version, version_arg, sizeof (int)) != 0)
6206821c4a97Sdp 		return (set_errno(EFAULT));
6207821c4a97Sdp 	return (0);
6208821c4a97Sdp }
6209821c4a97Sdp 
62107c478bd9Sstevel@tonic-gate /* ARGSUSED */
62117c478bd9Sstevel@tonic-gate long
6212fa9e4066Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4)
62137c478bd9Sstevel@tonic-gate {
62147c478bd9Sstevel@tonic-gate 	zone_def zs;
62152b24ab6bSSebastien Roy 	int err;
62167c478bd9Sstevel@tonic-gate 
62177c478bd9Sstevel@tonic-gate 	switch (cmd) {
62187c478bd9Sstevel@tonic-gate 	case ZONE_CREATE:
62197c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
62207c478bd9Sstevel@tonic-gate 			if (copyin(arg1, &zs, sizeof (zone_def))) {
62217c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
62227c478bd9Sstevel@tonic-gate 			}
62237c478bd9Sstevel@tonic-gate 		} else {
62247c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
62257c478bd9Sstevel@tonic-gate 			zone_def32 zs32;
62267c478bd9Sstevel@tonic-gate 
62277c478bd9Sstevel@tonic-gate 			if (copyin(arg1, &zs32, sizeof (zone_def32))) {
62287c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
62297c478bd9Sstevel@tonic-gate 			}
62307c478bd9Sstevel@tonic-gate 			zs.zone_name =
62317c478bd9Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_name;
62327c478bd9Sstevel@tonic-gate 			zs.zone_root =
62337c478bd9Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_root;
62347c478bd9Sstevel@tonic-gate 			zs.zone_privs =
62357c478bd9Sstevel@tonic-gate 			    (const struct priv_set *)
62367c478bd9Sstevel@tonic-gate 			    (unsigned long)zs32.zone_privs;
62376f70df68Sdp 			zs.zone_privssz = zs32.zone_privssz;
62387c478bd9Sstevel@tonic-gate 			zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf;
62397c478bd9Sstevel@tonic-gate 			zs.rctlbufsz = zs32.rctlbufsz;
6240fa9e4066Sahrens 			zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf;
6241fa9e4066Sahrens 			zs.zfsbufsz = zs32.zfsbufsz;
62427c478bd9Sstevel@tonic-gate 			zs.extended_error =
62437c478bd9Sstevel@tonic-gate 			    (int *)(unsigned long)zs32.extended_error;
624445916cd2Sjpk 			zs.match = zs32.match;
624545916cd2Sjpk 			zs.doi = zs32.doi;
624645916cd2Sjpk 			zs.label = (const bslabel_t *)(uintptr_t)zs32.label;
6247f4b3ec61Sdh155122 			zs.flags = zs32.flags;
62487c478bd9Sstevel@tonic-gate #else
62497c478bd9Sstevel@tonic-gate 			panic("get_udatamodel() returned bogus result\n");
62507c478bd9Sstevel@tonic-gate #endif
62517c478bd9Sstevel@tonic-gate 		}
62527c478bd9Sstevel@tonic-gate 
62537c478bd9Sstevel@tonic-gate 		return (zone_create(zs.zone_name, zs.zone_root,
6254821c4a97Sdp 		    zs.zone_privs, zs.zone_privssz,
6255821c4a97Sdp 		    (caddr_t)zs.rctlbuf, zs.rctlbufsz,
6256fa9e4066Sahrens 		    (caddr_t)zs.zfsbuf, zs.zfsbufsz,
625745916cd2Sjpk 		    zs.extended_error, zs.match, zs.doi,
6258f4b3ec61Sdh155122 		    zs.label, zs.flags));
62597c478bd9Sstevel@tonic-gate 	case ZONE_BOOT:
62603f2f09c1Sdp 		return (zone_boot((zoneid_t)(uintptr_t)arg1));
62617c478bd9Sstevel@tonic-gate 	case ZONE_DESTROY:
62627c478bd9Sstevel@tonic-gate 		return (zone_destroy((zoneid_t)(uintptr_t)arg1));
62637c478bd9Sstevel@tonic-gate 	case ZONE_GETATTR:
62647c478bd9Sstevel@tonic-gate 		return (zone_getattr((zoneid_t)(uintptr_t)arg1,
62657c478bd9Sstevel@tonic-gate 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
62663f2f09c1Sdp 	case ZONE_SETATTR:
62673f2f09c1Sdp 		return (zone_setattr((zoneid_t)(uintptr_t)arg1,
62683f2f09c1Sdp 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
62697c478bd9Sstevel@tonic-gate 	case ZONE_ENTER:
62707c478bd9Sstevel@tonic-gate 		return (zone_enter((zoneid_t)(uintptr_t)arg1));
62717c478bd9Sstevel@tonic-gate 	case ZONE_LIST:
62727c478bd9Sstevel@tonic-gate 		return (zone_list((zoneid_t *)arg1, (uint_t *)arg2));
62737c478bd9Sstevel@tonic-gate 	case ZONE_SHUTDOWN:
62747c478bd9Sstevel@tonic-gate 		return (zone_shutdown((zoneid_t)(uintptr_t)arg1));
62757c478bd9Sstevel@tonic-gate 	case ZONE_LOOKUP:
62767c478bd9Sstevel@tonic-gate 		return (zone_lookup((const char *)arg1));
6277821c4a97Sdp 	case ZONE_VERSION:
6278821c4a97Sdp 		return (zone_version((int *)arg1));
6279f4b3ec61Sdh155122 	case ZONE_ADD_DATALINK:
6280f4b3ec61Sdh155122 		return (zone_add_datalink((zoneid_t)(uintptr_t)arg1,
62812b24ab6bSSebastien Roy 		    (datalink_id_t)(uintptr_t)arg2));
6282f4b3ec61Sdh155122 	case ZONE_DEL_DATALINK:
6283f4b3ec61Sdh155122 		return (zone_remove_datalink((zoneid_t)(uintptr_t)arg1,
62842b24ab6bSSebastien Roy 		    (datalink_id_t)(uintptr_t)arg2));
62852b24ab6bSSebastien Roy 	case ZONE_CHECK_DATALINK: {
62862b24ab6bSSebastien Roy 		zoneid_t	zoneid;
62872b24ab6bSSebastien Roy 		boolean_t	need_copyout;
62882b24ab6bSSebastien Roy 
62892b24ab6bSSebastien Roy 		if (copyin(arg1, &zoneid, sizeof (zoneid)) != 0)
62902b24ab6bSSebastien Roy 			return (EFAULT);
62912b24ab6bSSebastien Roy 		need_copyout = (zoneid == ALL_ZONES);
62922b24ab6bSSebastien Roy 		err = zone_check_datalink(&zoneid,
62932b24ab6bSSebastien Roy 		    (datalink_id_t)(uintptr_t)arg2);
62942b24ab6bSSebastien Roy 		if (err == 0 && need_copyout) {
62952b24ab6bSSebastien Roy 			if (copyout(&zoneid, arg1, sizeof (zoneid)) != 0)
62962b24ab6bSSebastien Roy 				err = EFAULT;
62972b24ab6bSSebastien Roy 		}
62982b24ab6bSSebastien Roy 		return (err == 0 ? 0 : set_errno(err));
62992b24ab6bSSebastien Roy 	}
6300f4b3ec61Sdh155122 	case ZONE_LIST_DATALINK:
6301f4b3ec61Sdh155122 		return (zone_list_datalink((zoneid_t)(uintptr_t)arg1,
63022b24ab6bSSebastien Roy 		    (int *)arg2, (datalink_id_t *)(uintptr_t)arg3));
63037c478bd9Sstevel@tonic-gate 	default:
63047c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
63057c478bd9Sstevel@tonic-gate 	}
63067c478bd9Sstevel@tonic-gate }
63077c478bd9Sstevel@tonic-gate 
63087c478bd9Sstevel@tonic-gate struct zarg {
63097c478bd9Sstevel@tonic-gate 	zone_t *zone;
63107c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t arg;
63117c478bd9Sstevel@tonic-gate };
63127c478bd9Sstevel@tonic-gate 
63137c478bd9Sstevel@tonic-gate static int
63147c478bd9Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp)
63157c478bd9Sstevel@tonic-gate {
63167c478bd9Sstevel@tonic-gate 	char *buf;
63177c478bd9Sstevel@tonic-gate 	size_t buflen;
63187c478bd9Sstevel@tonic-gate 	int error;
63197c478bd9Sstevel@tonic-gate 
63207c478bd9Sstevel@tonic-gate 	buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name);
63217c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(buflen, KM_SLEEP);
63227c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name);
63237c478bd9Sstevel@tonic-gate 	error = door_ki_open(buf, doorp);
63247c478bd9Sstevel@tonic-gate 	kmem_free(buf, buflen);
63257c478bd9Sstevel@tonic-gate 	return (error);
63267c478bd9Sstevel@tonic-gate }
63277c478bd9Sstevel@tonic-gate 
63287c478bd9Sstevel@tonic-gate static void
63297c478bd9Sstevel@tonic-gate zone_release_door(door_handle_t *doorp)
63307c478bd9Sstevel@tonic-gate {
63317c478bd9Sstevel@tonic-gate 	door_ki_rele(*doorp);
63327c478bd9Sstevel@tonic-gate 	*doorp = NULL;
63337c478bd9Sstevel@tonic-gate }
63347c478bd9Sstevel@tonic-gate 
63357c478bd9Sstevel@tonic-gate static void
63367c478bd9Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp)
63377c478bd9Sstevel@tonic-gate {
63387c478bd9Sstevel@tonic-gate 	door_handle_t door = NULL;
63397c478bd9Sstevel@tonic-gate 	door_arg_t darg, save_arg;
63407c478bd9Sstevel@tonic-gate 	char *zone_name;
63417c478bd9Sstevel@tonic-gate 	size_t zone_namelen;
63427c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
63437c478bd9Sstevel@tonic-gate 	zone_t *zone;
63447c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t arg;
63457c478bd9Sstevel@tonic-gate 	uint64_t uniqid;
63467c478bd9Sstevel@tonic-gate 	size_t size;
63477c478bd9Sstevel@tonic-gate 	int error;
63487c478bd9Sstevel@tonic-gate 	int retry;
63497c478bd9Sstevel@tonic-gate 
63507c478bd9Sstevel@tonic-gate 	zone = zargp->zone;
63517c478bd9Sstevel@tonic-gate 	arg = zargp->arg;
63527c478bd9Sstevel@tonic-gate 	kmem_free(zargp, sizeof (*zargp));
63537c478bd9Sstevel@tonic-gate 
63547c478bd9Sstevel@tonic-gate 	zone_namelen = strlen(zone->zone_name) + 1;
63557c478bd9Sstevel@tonic-gate 	zone_name = kmem_alloc(zone_namelen, KM_SLEEP);
63567c478bd9Sstevel@tonic-gate 	bcopy(zone->zone_name, zone_name, zone_namelen);
63577c478bd9Sstevel@tonic-gate 	zoneid = zone->zone_id;
63587c478bd9Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
63597c478bd9Sstevel@tonic-gate 	/*
63607c478bd9Sstevel@tonic-gate 	 * zoneadmd may be down, but at least we can empty out the zone.
63617c478bd9Sstevel@tonic-gate 	 * We can ignore the return value of zone_empty() since we're called
63627c478bd9Sstevel@tonic-gate 	 * from a kernel thread and know we won't be delivered any signals.
63637c478bd9Sstevel@tonic-gate 	 */
63647c478bd9Sstevel@tonic-gate 	ASSERT(curproc == &p0);
63657c478bd9Sstevel@tonic-gate 	(void) zone_empty(zone);
63667c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY);
63677c478bd9Sstevel@tonic-gate 	zone_rele(zone);
63687c478bd9Sstevel@tonic-gate 
63697c478bd9Sstevel@tonic-gate 	size = sizeof (arg);
63707c478bd9Sstevel@tonic-gate 	darg.rbuf = (char *)&arg;
63717c478bd9Sstevel@tonic-gate 	darg.data_ptr = (char *)&arg;
63727c478bd9Sstevel@tonic-gate 	darg.rsize = size;
63737c478bd9Sstevel@tonic-gate 	darg.data_size = size;
63747c478bd9Sstevel@tonic-gate 	darg.desc_ptr = NULL;
63757c478bd9Sstevel@tonic-gate 	darg.desc_num = 0;
63767c478bd9Sstevel@tonic-gate 
63777c478bd9Sstevel@tonic-gate 	save_arg = darg;
63787c478bd9Sstevel@tonic-gate 	/*
63797c478bd9Sstevel@tonic-gate 	 * Since we're not holding a reference to the zone, any number of
63807c478bd9Sstevel@tonic-gate 	 * things can go wrong, including the zone disappearing before we get a
63817c478bd9Sstevel@tonic-gate 	 * chance to talk to zoneadmd.
63827c478bd9Sstevel@tonic-gate 	 */
63837c478bd9Sstevel@tonic-gate 	for (retry = 0; /* forever */; retry++) {
63847c478bd9Sstevel@tonic-gate 		if (door == NULL &&
63857c478bd9Sstevel@tonic-gate 		    (error = zone_lookup_door(zone_name, &door)) != 0) {
63867c478bd9Sstevel@tonic-gate 			goto next;
63877c478bd9Sstevel@tonic-gate 		}
63887c478bd9Sstevel@tonic-gate 		ASSERT(door != NULL);
63897c478bd9Sstevel@tonic-gate 
6390323a81d9Sjwadams 		if ((error = door_ki_upcall_limited(door, &darg, NULL,
6391323a81d9Sjwadams 		    SIZE_MAX, 0)) == 0) {
63927c478bd9Sstevel@tonic-gate 			break;
63937c478bd9Sstevel@tonic-gate 		}
63947c478bd9Sstevel@tonic-gate 		switch (error) {
63957c478bd9Sstevel@tonic-gate 		case EINTR:
63967c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
63977c478bd9Sstevel@tonic-gate 		case EAGAIN:	/* process may be forking */
63987c478bd9Sstevel@tonic-gate 			/*
63997c478bd9Sstevel@tonic-gate 			 * Back off for a bit
64007c478bd9Sstevel@tonic-gate 			 */
64017c478bd9Sstevel@tonic-gate 			break;
64027c478bd9Sstevel@tonic-gate 		case EBADF:
64037c478bd9Sstevel@tonic-gate 			zone_release_door(&door);
64047c478bd9Sstevel@tonic-gate 			if (zone_lookup_door(zone_name, &door) != 0) {
64057c478bd9Sstevel@tonic-gate 				/*
64067c478bd9Sstevel@tonic-gate 				 * zoneadmd may be dead, but it may come back to
64077c478bd9Sstevel@tonic-gate 				 * life later.
64087c478bd9Sstevel@tonic-gate 				 */
64097c478bd9Sstevel@tonic-gate 				break;
64107c478bd9Sstevel@tonic-gate 			}
64117c478bd9Sstevel@tonic-gate 			break;
64127c478bd9Sstevel@tonic-gate 		default:
64137c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
64147c478bd9Sstevel@tonic-gate 			    "zone_ki_call_zoneadmd: door_ki_upcall error %d\n",
64157c478bd9Sstevel@tonic-gate 			    error);
64167c478bd9Sstevel@tonic-gate 			goto out;
64177c478bd9Sstevel@tonic-gate 		}
64187c478bd9Sstevel@tonic-gate next:
64197c478bd9Sstevel@tonic-gate 		/*
64207c478bd9Sstevel@tonic-gate 		 * If this isn't the same zone_t that we originally had in mind,
64217c478bd9Sstevel@tonic-gate 		 * then this is the same as if two kadmin requests come in at
64227c478bd9Sstevel@tonic-gate 		 * the same time: the first one wins.  This means we lose, so we
64237c478bd9Sstevel@tonic-gate 		 * bail.
64247c478bd9Sstevel@tonic-gate 		 */
64257c478bd9Sstevel@tonic-gate 		if ((zone = zone_find_by_id(zoneid)) == NULL) {
64267c478bd9Sstevel@tonic-gate 			/*
64277c478bd9Sstevel@tonic-gate 			 * Problem is solved.
64287c478bd9Sstevel@tonic-gate 			 */
64297c478bd9Sstevel@tonic-gate 			break;
64307c478bd9Sstevel@tonic-gate 		}
64317c478bd9Sstevel@tonic-gate 		if (zone->zone_uniqid != uniqid) {
64327c478bd9Sstevel@tonic-gate 			/*
64337c478bd9Sstevel@tonic-gate 			 * zoneid recycled
64347c478bd9Sstevel@tonic-gate 			 */
64357c478bd9Sstevel@tonic-gate 			zone_rele(zone);
64367c478bd9Sstevel@tonic-gate 			break;
64377c478bd9Sstevel@tonic-gate 		}
64387c478bd9Sstevel@tonic-gate 		/*
64397c478bd9Sstevel@tonic-gate 		 * We could zone_status_timedwait(), but there doesn't seem to
64407c478bd9Sstevel@tonic-gate 		 * be much point in doing that (plus, it would mean that
64417c478bd9Sstevel@tonic-gate 		 * zone_free() isn't called until this thread exits).
64427c478bd9Sstevel@tonic-gate 		 */
64437c478bd9Sstevel@tonic-gate 		zone_rele(zone);
64447c478bd9Sstevel@tonic-gate 		delay(hz);
64457c478bd9Sstevel@tonic-gate 		darg = save_arg;
64467c478bd9Sstevel@tonic-gate 	}
64477c478bd9Sstevel@tonic-gate out:
64487c478bd9Sstevel@tonic-gate 	if (door != NULL) {
64497c478bd9Sstevel@tonic-gate 		zone_release_door(&door);
64507c478bd9Sstevel@tonic-gate 	}
64517c478bd9Sstevel@tonic-gate 	kmem_free(zone_name, zone_namelen);
64527c478bd9Sstevel@tonic-gate 	thread_exit();
64537c478bd9Sstevel@tonic-gate }
64547c478bd9Sstevel@tonic-gate 
64557c478bd9Sstevel@tonic-gate /*
64563f2f09c1Sdp  * Entry point for uadmin() to tell the zone to go away or reboot.  Analog to
64573f2f09c1Sdp  * kadmin().  The caller is a process in the zone.
64587c478bd9Sstevel@tonic-gate  *
64597c478bd9Sstevel@tonic-gate  * In order to shutdown the zone, we will hand off control to zoneadmd
64607c478bd9Sstevel@tonic-gate  * (running in the global zone) via a door.  We do a half-hearted job at
64617c478bd9Sstevel@tonic-gate  * killing all processes in the zone, create a kernel thread to contact
64627c478bd9Sstevel@tonic-gate  * zoneadmd, and make note of the "uniqid" of the zone.  The uniqid is
64637c478bd9Sstevel@tonic-gate  * a form of generation number used to let zoneadmd (as well as
64647c478bd9Sstevel@tonic-gate  * zone_destroy()) know exactly which zone they're re talking about.
64657c478bd9Sstevel@tonic-gate  */
64667c478bd9Sstevel@tonic-gate int
64673f2f09c1Sdp zone_kadmin(int cmd, int fcn, const char *mdep, cred_t *credp)
64687c478bd9Sstevel@tonic-gate {
64697c478bd9Sstevel@tonic-gate 	struct zarg *zargp;
64707c478bd9Sstevel@tonic-gate 	zone_cmd_t zcmd;
64717c478bd9Sstevel@tonic-gate 	zone_t *zone;
64727c478bd9Sstevel@tonic-gate 
64737c478bd9Sstevel@tonic-gate 	zone = curproc->p_zone;
64747c478bd9Sstevel@tonic-gate 	ASSERT(getzoneid() != GLOBAL_ZONEID);
64757c478bd9Sstevel@tonic-gate 
64767c478bd9Sstevel@tonic-gate 	switch (cmd) {
64777c478bd9Sstevel@tonic-gate 	case A_SHUTDOWN:
64787c478bd9Sstevel@tonic-gate 		switch (fcn) {
64797c478bd9Sstevel@tonic-gate 		case AD_HALT:
64807c478bd9Sstevel@tonic-gate 		case AD_POWEROFF:
64817c478bd9Sstevel@tonic-gate 			zcmd = Z_HALT;
64827c478bd9Sstevel@tonic-gate 			break;
64837c478bd9Sstevel@tonic-gate 		case AD_BOOT:
64847c478bd9Sstevel@tonic-gate 			zcmd = Z_REBOOT;
64857c478bd9Sstevel@tonic-gate 			break;
64867c478bd9Sstevel@tonic-gate 		case AD_IBOOT:
64877c478bd9Sstevel@tonic-gate 		case AD_SBOOT:
64887c478bd9Sstevel@tonic-gate 		case AD_SIBOOT:
64897c478bd9Sstevel@tonic-gate 		case AD_NOSYNC:
64907c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
64917c478bd9Sstevel@tonic-gate 		default:
64927c478bd9Sstevel@tonic-gate 			return (EINVAL);
64937c478bd9Sstevel@tonic-gate 		}
64947c478bd9Sstevel@tonic-gate 		break;
64957c478bd9Sstevel@tonic-gate 	case A_REBOOT:
64967c478bd9Sstevel@tonic-gate 		zcmd = Z_REBOOT;
64977c478bd9Sstevel@tonic-gate 		break;
64987c478bd9Sstevel@tonic-gate 	case A_FTRACE:
64997c478bd9Sstevel@tonic-gate 	case A_REMOUNT:
65007c478bd9Sstevel@tonic-gate 	case A_FREEZE:
65017c478bd9Sstevel@tonic-gate 	case A_DUMP:
6502753a6d45SSherry Moore 	case A_CONFIG:
65037c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
65047c478bd9Sstevel@tonic-gate 	default:
65057c478bd9Sstevel@tonic-gate 		ASSERT(cmd != A_SWAPCTL);	/* handled by uadmin() */
65067c478bd9Sstevel@tonic-gate 		return (EINVAL);
65077c478bd9Sstevel@tonic-gate 	}
65087c478bd9Sstevel@tonic-gate 
65097c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_admin(credp, B_FALSE))
65107c478bd9Sstevel@tonic-gate 		return (EPERM);
65117c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
65123f2f09c1Sdp 
65137c478bd9Sstevel@tonic-gate 	/*
65147c478bd9Sstevel@tonic-gate 	 * zone_status can't be ZONE_IS_EMPTY or higher since curproc
65157c478bd9Sstevel@tonic-gate 	 * is in the zone.
65167c478bd9Sstevel@tonic-gate 	 */
65177c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY);
65187c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) > ZONE_IS_RUNNING) {
65197c478bd9Sstevel@tonic-gate 		/*
65207c478bd9Sstevel@tonic-gate 		 * This zone is already on its way down.
65217c478bd9Sstevel@tonic-gate 		 */
65227c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
65237c478bd9Sstevel@tonic-gate 		return (0);
65247c478bd9Sstevel@tonic-gate 	}
65257c478bd9Sstevel@tonic-gate 	/*
65267c478bd9Sstevel@tonic-gate 	 * Prevent future zone_enter()s
65277c478bd9Sstevel@tonic-gate 	 */
65287c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
65297c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
65307c478bd9Sstevel@tonic-gate 
65317c478bd9Sstevel@tonic-gate 	/*
65327c478bd9Sstevel@tonic-gate 	 * Kill everyone now and call zoneadmd later.
65337c478bd9Sstevel@tonic-gate 	 * zone_ki_call_zoneadmd() will do a more thorough job of this
65347c478bd9Sstevel@tonic-gate 	 * later.
65357c478bd9Sstevel@tonic-gate 	 */
65367c478bd9Sstevel@tonic-gate 	killall(zone->zone_id);
65377c478bd9Sstevel@tonic-gate 	/*
65387c478bd9Sstevel@tonic-gate 	 * Now, create the thread to contact zoneadmd and do the rest of the
65397c478bd9Sstevel@tonic-gate 	 * work.  This thread can't be created in our zone otherwise
65407c478bd9Sstevel@tonic-gate 	 * zone_destroy() would deadlock.
65417c478bd9Sstevel@tonic-gate 	 */
65423f2f09c1Sdp 	zargp = kmem_zalloc(sizeof (*zargp), KM_SLEEP);
65437c478bd9Sstevel@tonic-gate 	zargp->arg.cmd = zcmd;
65447c478bd9Sstevel@tonic-gate 	zargp->arg.uniqid = zone->zone_uniqid;
65453f2f09c1Sdp 	zargp->zone = zone;
65467c478bd9Sstevel@tonic-gate 	(void) strcpy(zargp->arg.locale, "C");
65473f2f09c1Sdp 	/* mdep was already copied in for us by uadmin */
65483f2f09c1Sdp 	if (mdep != NULL)
65493f2f09c1Sdp 		(void) strlcpy(zargp->arg.bootbuf, mdep,
65503f2f09c1Sdp 		    sizeof (zargp->arg.bootbuf));
65513f2f09c1Sdp 	zone_hold(zone);
65527c478bd9Sstevel@tonic-gate 
65537c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0,
65547c478bd9Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
65557c478bd9Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
65567c478bd9Sstevel@tonic-gate 
65577c478bd9Sstevel@tonic-gate 	return (EINVAL);
65587c478bd9Sstevel@tonic-gate }
65597c478bd9Sstevel@tonic-gate 
65607c478bd9Sstevel@tonic-gate /*
65617c478bd9Sstevel@tonic-gate  * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's
65627c478bd9Sstevel@tonic-gate  * status to ZONE_IS_SHUTTING_DOWN.
65638f983ab3Sjv227347  *
65648f983ab3Sjv227347  * This function also shuts down all running zones to ensure that they won't
65658f983ab3Sjv227347  * fork new processes.
65667c478bd9Sstevel@tonic-gate  */
65677c478bd9Sstevel@tonic-gate void
65687c478bd9Sstevel@tonic-gate zone_shutdown_global(void)
65697c478bd9Sstevel@tonic-gate {
65708f983ab3Sjv227347 	zone_t *current_zonep;
65717c478bd9Sstevel@tonic-gate 
65728f983ab3Sjv227347 	ASSERT(INGLOBALZONE(curproc));
65738f983ab3Sjv227347 	mutex_enter(&zonehash_lock);
65747c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
65758f983ab3Sjv227347 
65768f983ab3Sjv227347 	/* Modify the global zone's status first. */
65777c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING);
65787c478bd9Sstevel@tonic-gate 	zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN);
65798f983ab3Sjv227347 
65808f983ab3Sjv227347 	/*
65818f983ab3Sjv227347 	 * Now change the states of all running zones to ZONE_IS_SHUTTING_DOWN.
65828f983ab3Sjv227347 	 * We don't mark all zones with ZONE_IS_SHUTTING_DOWN because doing so
65838f983ab3Sjv227347 	 * could cause assertions to fail (e.g., assertions about a zone's
65848f983ab3Sjv227347 	 * state during initialization, readying, or booting) or produce races.
65858f983ab3Sjv227347 	 * We'll let threads continue to initialize and ready new zones: they'll
65868f983ab3Sjv227347 	 * fail to boot the new zones when they see that the global zone is
65878f983ab3Sjv227347 	 * shutting down.
65888f983ab3Sjv227347 	 */
65898f983ab3Sjv227347 	for (current_zonep = list_head(&zone_active); current_zonep != NULL;
65908f983ab3Sjv227347 	    current_zonep = list_next(&zone_active, current_zonep)) {
65918f983ab3Sjv227347 		if (zone_status_get(current_zonep) == ZONE_IS_RUNNING)
65928f983ab3Sjv227347 			zone_status_set(current_zonep, ZONE_IS_SHUTTING_DOWN);
65938f983ab3Sjv227347 	}
65947c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
65958f983ab3Sjv227347 	mutex_exit(&zonehash_lock);
65967c478bd9Sstevel@tonic-gate }
6597fa9e4066Sahrens 
6598fa9e4066Sahrens /*
6599fa9e4066Sahrens  * Returns true if the named dataset is visible in the current zone.
6600fa9e4066Sahrens  * The 'write' parameter is set to 1 if the dataset is also writable.
6601fa9e4066Sahrens  */
6602fa9e4066Sahrens int
6603fa9e4066Sahrens zone_dataset_visible(const char *dataset, int *write)
6604fa9e4066Sahrens {
6605658a1dc7SSanjeev Bagewadi 	static int zfstype = -1;
6606fa9e4066Sahrens 	zone_dataset_t *zd;
6607fa9e4066Sahrens 	size_t len;
6608fa9e4066Sahrens 	zone_t *zone = curproc->p_zone;
6609658a1dc7SSanjeev Bagewadi 	const char *name = NULL;
6610658a1dc7SSanjeev Bagewadi 	vfs_t *vfsp = NULL;
6611fa9e4066Sahrens 
6612fa9e4066Sahrens 	if (dataset[0] == '\0')
6613fa9e4066Sahrens 		return (0);
6614fa9e4066Sahrens 
6615fa9e4066Sahrens 	/*
6616fa9e4066Sahrens 	 * Walk the list once, looking for datasets which match exactly, or
6617fa9e4066Sahrens 	 * specify a dataset underneath an exported dataset.  If found, return
6618fa9e4066Sahrens 	 * true and note that it is writable.
6619fa9e4066Sahrens 	 */
6620fa9e4066Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
6621fa9e4066Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
6622fa9e4066Sahrens 
6623fa9e4066Sahrens 		len = strlen(zd->zd_dataset);
6624fa9e4066Sahrens 		if (strlen(dataset) >= len &&
6625fa9e4066Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
662695c9592aSmaybee 		    (dataset[len] == '\0' || dataset[len] == '/' ||
662795c9592aSmaybee 		    dataset[len] == '@')) {
6628fa9e4066Sahrens 			if (write)
6629fa9e4066Sahrens 				*write = 1;
6630fa9e4066Sahrens 			return (1);
6631fa9e4066Sahrens 		}
6632fa9e4066Sahrens 	}
6633fa9e4066Sahrens 
6634fa9e4066Sahrens 	/*
6635fa9e4066Sahrens 	 * Walk the list a second time, searching for datasets which are parents
6636fa9e4066Sahrens 	 * of exported datasets.  These should be visible, but read-only.
6637fa9e4066Sahrens 	 *
6638fa9e4066Sahrens 	 * Note that we also have to support forms such as 'pool/dataset/', with
6639fa9e4066Sahrens 	 * a trailing slash.
6640fa9e4066Sahrens 	 */
6641fa9e4066Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
6642fa9e4066Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
6643fa9e4066Sahrens 
6644fa9e4066Sahrens 		len = strlen(dataset);
6645fa9e4066Sahrens 		if (dataset[len - 1] == '/')
6646fa9e4066Sahrens 			len--;	/* Ignore trailing slash */
6647fa9e4066Sahrens 		if (len < strlen(zd->zd_dataset) &&
6648fa9e4066Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
6649fa9e4066Sahrens 		    zd->zd_dataset[len] == '/') {
6650fa9e4066Sahrens 			if (write)
6651fa9e4066Sahrens 				*write = 0;
6652fa9e4066Sahrens 			return (1);
6653fa9e4066Sahrens 		}
6654fa9e4066Sahrens 	}
6655fa9e4066Sahrens 
6656658a1dc7SSanjeev Bagewadi 	/*
6657658a1dc7SSanjeev Bagewadi 	 * We reach here if the given dataset is not found in the zone_dataset
6658658a1dc7SSanjeev Bagewadi 	 * list. Check if this dataset was added as a filesystem (ie. "add fs")
6659658a1dc7SSanjeev Bagewadi 	 * instead of delegation. For this we search for the dataset in the
6660658a1dc7SSanjeev Bagewadi 	 * zone_vfslist of this zone. If found, return true and note that it is
6661658a1dc7SSanjeev Bagewadi 	 * not writable.
6662658a1dc7SSanjeev Bagewadi 	 */
6663658a1dc7SSanjeev Bagewadi 
6664658a1dc7SSanjeev Bagewadi 	/*
6665658a1dc7SSanjeev Bagewadi 	 * Initialize zfstype if it is not initialized yet.
6666658a1dc7SSanjeev Bagewadi 	 */
6667658a1dc7SSanjeev Bagewadi 	if (zfstype == -1) {
6668658a1dc7SSanjeev Bagewadi 		struct vfssw *vswp = vfs_getvfssw("zfs");
6669658a1dc7SSanjeev Bagewadi 		zfstype = vswp - vfssw;
6670658a1dc7SSanjeev Bagewadi 		vfs_unrefvfssw(vswp);
6671658a1dc7SSanjeev Bagewadi 	}
6672658a1dc7SSanjeev Bagewadi 
6673658a1dc7SSanjeev Bagewadi 	vfs_list_read_lock();
6674658a1dc7SSanjeev Bagewadi 	vfsp = zone->zone_vfslist;
6675658a1dc7SSanjeev Bagewadi 	do {
6676658a1dc7SSanjeev Bagewadi 		ASSERT(vfsp);
6677658a1dc7SSanjeev Bagewadi 		if (vfsp->vfs_fstype == zfstype) {
6678658a1dc7SSanjeev Bagewadi 			name = refstr_value(vfsp->vfs_resource);
6679658a1dc7SSanjeev Bagewadi 
6680658a1dc7SSanjeev Bagewadi 			/*
6681658a1dc7SSanjeev Bagewadi 			 * Check if we have an exact match.
6682658a1dc7SSanjeev Bagewadi 			 */
6683658a1dc7SSanjeev Bagewadi 			if (strcmp(dataset, name) == 0) {
6684658a1dc7SSanjeev Bagewadi 				vfs_list_unlock();
6685658a1dc7SSanjeev Bagewadi 				if (write)
6686658a1dc7SSanjeev Bagewadi 					*write = 0;
6687658a1dc7SSanjeev Bagewadi 				return (1);
6688658a1dc7SSanjeev Bagewadi 			}
6689658a1dc7SSanjeev Bagewadi 			/*
6690658a1dc7SSanjeev Bagewadi 			 * We need to check if we are looking for parents of
6691658a1dc7SSanjeev Bagewadi 			 * a dataset. These should be visible, but read-only.
6692658a1dc7SSanjeev Bagewadi 			 */
6693658a1dc7SSanjeev Bagewadi 			len = strlen(dataset);
6694658a1dc7SSanjeev Bagewadi 			if (dataset[len - 1] == '/')
6695658a1dc7SSanjeev Bagewadi 				len--;
6696658a1dc7SSanjeev Bagewadi 
6697658a1dc7SSanjeev Bagewadi 			if (len < strlen(name) &&
6698658a1dc7SSanjeev Bagewadi 			    bcmp(dataset, name, len) == 0 && name[len] == '/') {
6699658a1dc7SSanjeev Bagewadi 				vfs_list_unlock();
6700658a1dc7SSanjeev Bagewadi 				if (write)
6701658a1dc7SSanjeev Bagewadi 					*write = 0;
6702658a1dc7SSanjeev Bagewadi 				return (1);
6703658a1dc7SSanjeev Bagewadi 			}
6704658a1dc7SSanjeev Bagewadi 		}
6705658a1dc7SSanjeev Bagewadi 		vfsp = vfsp->vfs_zone_next;
6706658a1dc7SSanjeev Bagewadi 	} while (vfsp != zone->zone_vfslist);
6707658a1dc7SSanjeev Bagewadi 
6708658a1dc7SSanjeev Bagewadi 	vfs_list_unlock();
6709fa9e4066Sahrens 	return (0);
6710fa9e4066Sahrens }
671145916cd2Sjpk 
671245916cd2Sjpk /*
671345916cd2Sjpk  * zone_find_by_any_path() -
671445916cd2Sjpk  *
671545916cd2Sjpk  * kernel-private routine similar to zone_find_by_path(), but which
671645916cd2Sjpk  * effectively compares against zone paths rather than zonerootpath
671745916cd2Sjpk  * (i.e., the last component of zonerootpaths, which should be "root/",
671845916cd2Sjpk  * are not compared.)  This is done in order to accurately identify all
671945916cd2Sjpk  * paths, whether zone-visible or not, including those which are parallel
672045916cd2Sjpk  * to /root/, such as /dev/, /home/, etc...
672145916cd2Sjpk  *
672245916cd2Sjpk  * If the specified path does not fall under any zone path then global
672345916cd2Sjpk  * zone is returned.
672445916cd2Sjpk  *
672545916cd2Sjpk  * The treat_abs parameter indicates whether the path should be treated as
672645916cd2Sjpk  * an absolute path although it does not begin with "/".  (This supports
672745916cd2Sjpk  * nfs mount syntax such as host:any/path.)
672845916cd2Sjpk  *
672945916cd2Sjpk  * The caller is responsible for zone_rele of the returned zone.
673045916cd2Sjpk  */
673145916cd2Sjpk zone_t *
673245916cd2Sjpk zone_find_by_any_path(const char *path, boolean_t treat_abs)
673345916cd2Sjpk {
673445916cd2Sjpk 	zone_t *zone;
673545916cd2Sjpk 	int path_offset = 0;
673645916cd2Sjpk 
673745916cd2Sjpk 	if (path == NULL) {
673845916cd2Sjpk 		zone_hold(global_zone);
673945916cd2Sjpk 		return (global_zone);
674045916cd2Sjpk 	}
674145916cd2Sjpk 
674245916cd2Sjpk 	if (*path != '/') {
674345916cd2Sjpk 		ASSERT(treat_abs);
674445916cd2Sjpk 		path_offset = 1;
674545916cd2Sjpk 	}
674645916cd2Sjpk 
674745916cd2Sjpk 	mutex_enter(&zonehash_lock);
674845916cd2Sjpk 	for (zone = list_head(&zone_active); zone != NULL;
674945916cd2Sjpk 	    zone = list_next(&zone_active, zone)) {
675045916cd2Sjpk 		char	*c;
675145916cd2Sjpk 		size_t	pathlen;
67520f95d722Smp46848 		char *rootpath_start;
675345916cd2Sjpk 
675445916cd2Sjpk 		if (zone == global_zone)	/* skip global zone */
675545916cd2Sjpk 			continue;
675645916cd2Sjpk 
675745916cd2Sjpk 		/* scan backwards to find start of last component */
675845916cd2Sjpk 		c = zone->zone_rootpath + zone->zone_rootpathlen - 2;
675945916cd2Sjpk 		do {
676045916cd2Sjpk 			c--;
676145916cd2Sjpk 		} while (*c != '/');
676245916cd2Sjpk 
67630f95d722Smp46848 		pathlen = c - zone->zone_rootpath + 1 - path_offset;
67640f95d722Smp46848 		rootpath_start = (zone->zone_rootpath + path_offset);
67650f95d722Smp46848 		if (strncmp(path, rootpath_start, pathlen) == 0)
676645916cd2Sjpk 			break;
676745916cd2Sjpk 	}
676845916cd2Sjpk 	if (zone == NULL)
676945916cd2Sjpk 		zone = global_zone;
677045916cd2Sjpk 	zone_hold(zone);
677145916cd2Sjpk 	mutex_exit(&zonehash_lock);
677245916cd2Sjpk 	return (zone);
677345916cd2Sjpk }
6774f4b3ec61Sdh155122 
6775f4b3ec61Sdh155122 /*
67762b24ab6bSSebastien Roy  * Finds a zone_dl_t with the given linkid in the given zone.  Returns the
67772b24ab6bSSebastien Roy  * zone_dl_t pointer if found, and NULL otherwise.
6778f4b3ec61Sdh155122  */
67792b24ab6bSSebastien Roy static zone_dl_t *
67802b24ab6bSSebastien Roy zone_find_dl(zone_t *zone, datalink_id_t linkid)
6781f4b3ec61Sdh155122 {
67822b24ab6bSSebastien Roy 	zone_dl_t *zdl;
6783f4b3ec61Sdh155122 
67842b24ab6bSSebastien Roy 	ASSERT(mutex_owned(&zone->zone_lock));
67852b24ab6bSSebastien Roy 	for (zdl = list_head(&zone->zone_dl_list); zdl != NULL;
67862b24ab6bSSebastien Roy 	    zdl = list_next(&zone->zone_dl_list, zdl)) {
67872b24ab6bSSebastien Roy 		if (zdl->zdl_id == linkid)
6788f4b3ec61Sdh155122 			break;
6789f4b3ec61Sdh155122 	}
67902b24ab6bSSebastien Roy 	return (zdl);
6791f4b3ec61Sdh155122 }
67922b24ab6bSSebastien Roy 
67932b24ab6bSSebastien Roy static boolean_t
67942b24ab6bSSebastien Roy zone_dl_exists(zone_t *zone, datalink_id_t linkid)
67952b24ab6bSSebastien Roy {
67962b24ab6bSSebastien Roy 	boolean_t exists;
67972b24ab6bSSebastien Roy 
67982b24ab6bSSebastien Roy 	mutex_enter(&zone->zone_lock);
67992b24ab6bSSebastien Roy 	exists = (zone_find_dl(zone, linkid) != NULL);
6800f4b3ec61Sdh155122 	mutex_exit(&zone->zone_lock);
68012b24ab6bSSebastien Roy 	return (exists);
6802f4b3ec61Sdh155122 }
6803f4b3ec61Sdh155122 
6804f4b3ec61Sdh155122 /*
68052b24ab6bSSebastien Roy  * Add an data link name for the zone.
6806f4b3ec61Sdh155122  */
6807f4b3ec61Sdh155122 static int
68082b24ab6bSSebastien Roy zone_add_datalink(zoneid_t zoneid, datalink_id_t linkid)
6809f4b3ec61Sdh155122 {
68102b24ab6bSSebastien Roy 	zone_dl_t *zdl;
6811f4b3ec61Sdh155122 	zone_t *zone;
6812f4b3ec61Sdh155122 	zone_t *thiszone;
6813f4b3ec61Sdh155122 
68142b24ab6bSSebastien Roy 	if ((thiszone = zone_find_by_id(zoneid)) == NULL)
6815f4b3ec61Sdh155122 		return (set_errno(ENXIO));
6816f4b3ec61Sdh155122 
68172b24ab6bSSebastien Roy 	/* Verify that the datalink ID doesn't already belong to a zone. */
6818f4b3ec61Sdh155122 	mutex_enter(&zonehash_lock);
6819f4b3ec61Sdh155122 	for (zone = list_head(&zone_active); zone != NULL;
6820f4b3ec61Sdh155122 	    zone = list_next(&zone_active, zone)) {
68212b24ab6bSSebastien Roy 		if (zone_dl_exists(zone, linkid)) {
6822f4b3ec61Sdh155122 			mutex_exit(&zonehash_lock);
6823f4b3ec61Sdh155122 			zone_rele(thiszone);
68242b24ab6bSSebastien Roy 			return (set_errno((zone == thiszone) ? EEXIST : EPERM));
6825f4b3ec61Sdh155122 		}
6826f4b3ec61Sdh155122 	}
68272b24ab6bSSebastien Roy 
68282b24ab6bSSebastien Roy 	zdl = kmem_zalloc(sizeof (*zdl), KM_SLEEP);
68292b24ab6bSSebastien Roy 	zdl->zdl_id = linkid;
6830550b6e40SSowmini Varadhan 	zdl->zdl_net = NULL;
6831f4b3ec61Sdh155122 	mutex_enter(&thiszone->zone_lock);
68322b24ab6bSSebastien Roy 	list_insert_head(&thiszone->zone_dl_list, zdl);
6833f4b3ec61Sdh155122 	mutex_exit(&thiszone->zone_lock);
6834f4b3ec61Sdh155122 	mutex_exit(&zonehash_lock);
6835f4b3ec61Sdh155122 	zone_rele(thiszone);
6836f4b3ec61Sdh155122 	return (0);
6837f4b3ec61Sdh155122 }
6838f4b3ec61Sdh155122 
6839f4b3ec61Sdh155122 static int
68402b24ab6bSSebastien Roy zone_remove_datalink(zoneid_t zoneid, datalink_id_t linkid)
6841f4b3ec61Sdh155122 {
68422b24ab6bSSebastien Roy 	zone_dl_t *zdl;
6843f4b3ec61Sdh155122 	zone_t *zone;
6844f4b3ec61Sdh155122 	int err = 0;
6845f4b3ec61Sdh155122 
68462b24ab6bSSebastien Roy 	if ((zone = zone_find_by_id(zoneid)) == NULL)
68472b24ab6bSSebastien Roy 		return (set_errno(EINVAL));
6848f4b3ec61Sdh155122 
68492b24ab6bSSebastien Roy 	mutex_enter(&zone->zone_lock);
68502b24ab6bSSebastien Roy 	if ((zdl = zone_find_dl(zone, linkid)) == NULL) {
68512b24ab6bSSebastien Roy 		err = ENXIO;
68522b24ab6bSSebastien Roy 	} else {
68532b24ab6bSSebastien Roy 		list_remove(&zone->zone_dl_list, zdl);
6854550b6e40SSowmini Varadhan 		if (zdl->zdl_net != NULL)
6855550b6e40SSowmini Varadhan 			nvlist_free(zdl->zdl_net);
68562b24ab6bSSebastien Roy 		kmem_free(zdl, sizeof (zone_dl_t));
68572b24ab6bSSebastien Roy 	}
68582b24ab6bSSebastien Roy 	mutex_exit(&zone->zone_lock);
68592b24ab6bSSebastien Roy 	zone_rele(zone);
68602b24ab6bSSebastien Roy 	return (err == 0 ? 0 : set_errno(err));
68612b24ab6bSSebastien Roy }
6862f4b3ec61Sdh155122 
6863f4b3ec61Sdh155122 /*
68642b24ab6bSSebastien Roy  * Using the zoneidp as ALL_ZONES, we can lookup which zone has been assigned
68652b24ab6bSSebastien Roy  * the linkid.  Otherwise we just check if the specified zoneidp has been
68662b24ab6bSSebastien Roy  * assigned the supplied linkid.
6867f4b3ec61Sdh155122  */
68682b24ab6bSSebastien Roy int
68692b24ab6bSSebastien Roy zone_check_datalink(zoneid_t *zoneidp, datalink_id_t linkid)
68702b24ab6bSSebastien Roy {
68712b24ab6bSSebastien Roy 	zone_t *zone;
68722b24ab6bSSebastien Roy 	int err = ENXIO;
68732b24ab6bSSebastien Roy 
68742b24ab6bSSebastien Roy 	if (*zoneidp != ALL_ZONES) {
68752b24ab6bSSebastien Roy 		if ((zone = zone_find_by_id(*zoneidp)) != NULL) {
68762b24ab6bSSebastien Roy 			if (zone_dl_exists(zone, linkid))
68772b24ab6bSSebastien Roy 				err = 0;
68782b24ab6bSSebastien Roy 			zone_rele(zone);
68792b24ab6bSSebastien Roy 		}
68802b24ab6bSSebastien Roy 		return (err);
68812b24ab6bSSebastien Roy 	}
68822b24ab6bSSebastien Roy 
6883f4b3ec61Sdh155122 	mutex_enter(&zonehash_lock);
6884f4b3ec61Sdh155122 	for (zone = list_head(&zone_active); zone != NULL;
6885f4b3ec61Sdh155122 	    zone = list_next(&zone_active, zone)) {
68862b24ab6bSSebastien Roy 		if (zone_dl_exists(zone, linkid)) {
68872b24ab6bSSebastien Roy 			*zoneidp = zone->zone_id;
68882b24ab6bSSebastien Roy 			err = 0;
68892b24ab6bSSebastien Roy 			break;
6890f4b3ec61Sdh155122 		}
6891f4b3ec61Sdh155122 	}
6892f4b3ec61Sdh155122 	mutex_exit(&zonehash_lock);
68932b24ab6bSSebastien Roy 	return (err);
6894f4b3ec61Sdh155122 }
6895f4b3ec61Sdh155122 
6896f4b3ec61Sdh155122 /*
68972b24ab6bSSebastien Roy  * Get the list of datalink IDs assigned to a zone.
68982b24ab6bSSebastien Roy  *
68992b24ab6bSSebastien Roy  * On input, *nump is the number of datalink IDs that can fit in the supplied
69002b24ab6bSSebastien Roy  * idarray.  Upon return, *nump is either set to the number of datalink IDs
69012b24ab6bSSebastien Roy  * that were placed in the array if the array was large enough, or to the
69022b24ab6bSSebastien Roy  * number of datalink IDs that the function needs to place in the array if the
69032b24ab6bSSebastien Roy  * array is too small.
6904f4b3ec61Sdh155122  */
6905f4b3ec61Sdh155122 static int
69062b24ab6bSSebastien Roy zone_list_datalink(zoneid_t zoneid, int *nump, datalink_id_t *idarray)
6907f4b3ec61Sdh155122 {
69082b24ab6bSSebastien Roy 	uint_t num, dlcount;
6909f4b3ec61Sdh155122 	zone_t *zone;
69102b24ab6bSSebastien Roy 	zone_dl_t *zdl;
69112b24ab6bSSebastien Roy 	datalink_id_t *idptr = idarray;
6912f4b3ec61Sdh155122 
6913f4b3ec61Sdh155122 	if (copyin(nump, &dlcount, sizeof (dlcount)) != 0)
6914f4b3ec61Sdh155122 		return (set_errno(EFAULT));
69152b24ab6bSSebastien Roy 	if ((zone = zone_find_by_id(zoneid)) == NULL)
6916f4b3ec61Sdh155122 		return (set_errno(ENXIO));
6917f4b3ec61Sdh155122 
6918f4b3ec61Sdh155122 	num = 0;
6919f4b3ec61Sdh155122 	mutex_enter(&zone->zone_lock);
69202b24ab6bSSebastien Roy 	for (zdl = list_head(&zone->zone_dl_list); zdl != NULL;
69212b24ab6bSSebastien Roy 	    zdl = list_next(&zone->zone_dl_list, zdl)) {
6922f4b3ec61Sdh155122 		/*
69232b24ab6bSSebastien Roy 		 * If the list is bigger than what the caller supplied, just
69242b24ab6bSSebastien Roy 		 * count, don't do copyout.
6925f4b3ec61Sdh155122 		 */
6926f4b3ec61Sdh155122 		if (++num > dlcount)
6927f4b3ec61Sdh155122 			continue;
69282b24ab6bSSebastien Roy 		if (copyout(&zdl->zdl_id, idptr, sizeof (*idptr)) != 0) {
6929f4b3ec61Sdh155122 			mutex_exit(&zone->zone_lock);
6930f4b3ec61Sdh155122 			zone_rele(zone);
6931f4b3ec61Sdh155122 			return (set_errno(EFAULT));
6932f4b3ec61Sdh155122 		}
69332b24ab6bSSebastien Roy 		idptr++;
6934f4b3ec61Sdh155122 	}
6935f4b3ec61Sdh155122 	mutex_exit(&zone->zone_lock);
6936f4b3ec61Sdh155122 	zone_rele(zone);
6937f4b3ec61Sdh155122 
6938f4b3ec61Sdh155122 	/* Increased or decreased, caller should be notified. */
6939f4b3ec61Sdh155122 	if (num != dlcount) {
69402b24ab6bSSebastien Roy 		if (copyout(&num, nump, sizeof (num)) != 0)
6941f4b3ec61Sdh155122 			return (set_errno(EFAULT));
6942f4b3ec61Sdh155122 	}
6943f4b3ec61Sdh155122 	return (0);
6944f4b3ec61Sdh155122 }
6945f4b3ec61Sdh155122 
6946f4b3ec61Sdh155122 /*
6947f4b3ec61Sdh155122  * Public interface for looking up a zone by zoneid. It's a customized version
6948bd41d0a8Snordmark  * for netstack_zone_create(). It can only be called from the zsd create
6949bd41d0a8Snordmark  * callbacks, since it doesn't have reference on the zone structure hence if
6950bd41d0a8Snordmark  * it is called elsewhere the zone could disappear after the zonehash_lock
6951bd41d0a8Snordmark  * is dropped.
6952bd41d0a8Snordmark  *
6953bd41d0a8Snordmark  * Furthermore it
6954bd41d0a8Snordmark  * 1. Doesn't check the status of the zone.
6955bd41d0a8Snordmark  * 2. It will be called even before zone_init is called, in that case the
6956f4b3ec61Sdh155122  *    address of zone0 is returned directly, and netstack_zone_create()
6957f4b3ec61Sdh155122  *    will only assign a value to zone0.zone_netstack, won't break anything.
6958bd41d0a8Snordmark  * 3. Returns without the zone being held.
6959f4b3ec61Sdh155122  */
6960f4b3ec61Sdh155122 zone_t *
6961f4b3ec61Sdh155122 zone_find_by_id_nolock(zoneid_t zoneid)
6962f4b3ec61Sdh155122 {
6963bd41d0a8Snordmark 	zone_t *zone;
6964f4b3ec61Sdh155122 
6965bd41d0a8Snordmark 	mutex_enter(&zonehash_lock);
6966f4b3ec61Sdh155122 	if (zonehashbyid == NULL)
6967bd41d0a8Snordmark 		zone = &zone0;
6968f4b3ec61Sdh155122 	else
6969bd41d0a8Snordmark 		zone = zone_find_all_by_id(zoneid);
6970bd41d0a8Snordmark 	mutex_exit(&zonehash_lock);
6971bd41d0a8Snordmark 	return (zone);
6972f4b3ec61Sdh155122 }
6973d62bc4baSyz147064 
6974d62bc4baSyz147064 /*
6975d62bc4baSyz147064  * Walk the datalinks for a given zone
6976d62bc4baSyz147064  */
6977d62bc4baSyz147064 int
69782b24ab6bSSebastien Roy zone_datalink_walk(zoneid_t zoneid, int (*cb)(datalink_id_t, void *),
69792b24ab6bSSebastien Roy     void *data)
6980d62bc4baSyz147064 {
6981d62bc4baSyz147064 	zone_t		*zone;
69822b24ab6bSSebastien Roy 	zone_dl_t	*zdl;
69832b24ab6bSSebastien Roy 	datalink_id_t	*idarray;
69842b24ab6bSSebastien Roy 	uint_t		idcount = 0;
69852b24ab6bSSebastien Roy 	int		i, ret = 0;
6986d62bc4baSyz147064 
6987d62bc4baSyz147064 	if ((zone = zone_find_by_id(zoneid)) == NULL)
6988d62bc4baSyz147064 		return (ENOENT);
6989d62bc4baSyz147064 
69902b24ab6bSSebastien Roy 	/*
69912b24ab6bSSebastien Roy 	 * We first build an array of linkid's so that we can walk these and
69922b24ab6bSSebastien Roy 	 * execute the callback with the zone_lock dropped.
69932b24ab6bSSebastien Roy 	 */
6994d62bc4baSyz147064 	mutex_enter(&zone->zone_lock);
69952b24ab6bSSebastien Roy 	for (zdl = list_head(&zone->zone_dl_list); zdl != NULL;
69962b24ab6bSSebastien Roy 	    zdl = list_next(&zone->zone_dl_list, zdl)) {
69972b24ab6bSSebastien Roy 		idcount++;
6998d62bc4baSyz147064 	}
69992b24ab6bSSebastien Roy 
70002b24ab6bSSebastien Roy 	if (idcount == 0) {
7001d62bc4baSyz147064 		mutex_exit(&zone->zone_lock);
7002d62bc4baSyz147064 		zone_rele(zone);
70032b24ab6bSSebastien Roy 		return (0);
70042b24ab6bSSebastien Roy 	}
70052b24ab6bSSebastien Roy 
70062b24ab6bSSebastien Roy 	idarray = kmem_alloc(sizeof (datalink_id_t) * idcount, KM_NOSLEEP);
70072b24ab6bSSebastien Roy 	if (idarray == NULL) {
70082b24ab6bSSebastien Roy 		mutex_exit(&zone->zone_lock);
70092b24ab6bSSebastien Roy 		zone_rele(zone);
70102b24ab6bSSebastien Roy 		return (ENOMEM);
70112b24ab6bSSebastien Roy 	}
70122b24ab6bSSebastien Roy 
70132b24ab6bSSebastien Roy 	for (i = 0, zdl = list_head(&zone->zone_dl_list); zdl != NULL;
70142b24ab6bSSebastien Roy 	    i++, zdl = list_next(&zone->zone_dl_list, zdl)) {
70152b24ab6bSSebastien Roy 		idarray[i] = zdl->zdl_id;
70162b24ab6bSSebastien Roy 	}
70172b24ab6bSSebastien Roy 
70182b24ab6bSSebastien Roy 	mutex_exit(&zone->zone_lock);
70192b24ab6bSSebastien Roy 
70202b24ab6bSSebastien Roy 	for (i = 0; i < idcount && ret == 0; i++) {
70212b24ab6bSSebastien Roy 		if ((ret = (*cb)(idarray[i], data)) != 0)
70222b24ab6bSSebastien Roy 			break;
70232b24ab6bSSebastien Roy 	}
70242b24ab6bSSebastien Roy 
70252b24ab6bSSebastien Roy 	zone_rele(zone);
70262b24ab6bSSebastien Roy 	kmem_free(idarray, sizeof (datalink_id_t) * idcount);
7027d62bc4baSyz147064 	return (ret);
7028d62bc4baSyz147064 }
7029550b6e40SSowmini Varadhan 
7030550b6e40SSowmini Varadhan static char *
7031550b6e40SSowmini Varadhan zone_net_type2name(int type)
7032550b6e40SSowmini Varadhan {
7033550b6e40SSowmini Varadhan 	switch (type) {
7034550b6e40SSowmini Varadhan 	case ZONE_NETWORK_ADDRESS:
7035550b6e40SSowmini Varadhan 		return (ZONE_NET_ADDRNAME);
7036550b6e40SSowmini Varadhan 	case ZONE_NETWORK_DEFROUTER:
7037550b6e40SSowmini Varadhan 		return (ZONE_NET_RTRNAME);
7038550b6e40SSowmini Varadhan 	default:
7039550b6e40SSowmini Varadhan 		return (NULL);
7040550b6e40SSowmini Varadhan 	}
7041550b6e40SSowmini Varadhan }
7042550b6e40SSowmini Varadhan 
7043550b6e40SSowmini Varadhan static int
7044550b6e40SSowmini Varadhan zone_set_network(zoneid_t zoneid, zone_net_data_t *znbuf)
7045550b6e40SSowmini Varadhan {
7046550b6e40SSowmini Varadhan 	zone_t *zone;
7047550b6e40SSowmini Varadhan 	zone_dl_t *zdl;
7048550b6e40SSowmini Varadhan 	nvlist_t *nvl;
7049550b6e40SSowmini Varadhan 	int err = 0;
7050550b6e40SSowmini Varadhan 	uint8_t *new = NULL;
7051550b6e40SSowmini Varadhan 	char *nvname;
7052550b6e40SSowmini Varadhan 	int bufsize;
7053550b6e40SSowmini Varadhan 	datalink_id_t linkid = znbuf->zn_linkid;
7054550b6e40SSowmini Varadhan 
7055550b6e40SSowmini Varadhan 	if (secpolicy_zone_config(CRED()) != 0)
7056550b6e40SSowmini Varadhan 		return (set_errno(EPERM));
7057550b6e40SSowmini Varadhan 
7058550b6e40SSowmini Varadhan 	if (zoneid == GLOBAL_ZONEID)
7059550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
7060550b6e40SSowmini Varadhan 
7061550b6e40SSowmini Varadhan 	nvname = zone_net_type2name(znbuf->zn_type);
7062550b6e40SSowmini Varadhan 	bufsize = znbuf->zn_len;
7063550b6e40SSowmini Varadhan 	new = znbuf->zn_val;
7064550b6e40SSowmini Varadhan 	if (nvname == NULL)
7065550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
7066550b6e40SSowmini Varadhan 
7067550b6e40SSowmini Varadhan 	if ((zone = zone_find_by_id(zoneid)) == NULL) {
7068550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
7069550b6e40SSowmini Varadhan 	}
7070550b6e40SSowmini Varadhan 
7071550b6e40SSowmini Varadhan 	mutex_enter(&zone->zone_lock);
7072550b6e40SSowmini Varadhan 	if ((zdl = zone_find_dl(zone, linkid)) == NULL) {
7073550b6e40SSowmini Varadhan 		err = ENXIO;
7074550b6e40SSowmini Varadhan 		goto done;
7075550b6e40SSowmini Varadhan 	}
7076550b6e40SSowmini Varadhan 	if ((nvl = zdl->zdl_net) == NULL) {
7077550b6e40SSowmini Varadhan 		if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)) {
7078550b6e40SSowmini Varadhan 			err = ENOMEM;
7079550b6e40SSowmini Varadhan 			goto done;
7080550b6e40SSowmini Varadhan 		} else {
7081550b6e40SSowmini Varadhan 			zdl->zdl_net = nvl;
7082550b6e40SSowmini Varadhan 		}
7083550b6e40SSowmini Varadhan 	}
7084550b6e40SSowmini Varadhan 	if (nvlist_exists(nvl, nvname)) {
7085550b6e40SSowmini Varadhan 		err = EINVAL;
7086550b6e40SSowmini Varadhan 		goto done;
7087550b6e40SSowmini Varadhan 	}
7088550b6e40SSowmini Varadhan 	err = nvlist_add_uint8_array(nvl, nvname, new, bufsize);
7089550b6e40SSowmini Varadhan 	ASSERT(err == 0);
7090550b6e40SSowmini Varadhan done:
7091550b6e40SSowmini Varadhan 	mutex_exit(&zone->zone_lock);
7092550b6e40SSowmini Varadhan 	zone_rele(zone);
7093550b6e40SSowmini Varadhan 	if (err != 0)
7094550b6e40SSowmini Varadhan 		return (set_errno(err));
7095550b6e40SSowmini Varadhan 	else
7096550b6e40SSowmini Varadhan 		return (0);
7097550b6e40SSowmini Varadhan }
7098550b6e40SSowmini Varadhan 
7099550b6e40SSowmini Varadhan static int
7100550b6e40SSowmini Varadhan zone_get_network(zoneid_t zoneid, zone_net_data_t *znbuf)
7101550b6e40SSowmini Varadhan {
7102550b6e40SSowmini Varadhan 	zone_t *zone;
7103550b6e40SSowmini Varadhan 	zone_dl_t *zdl;
7104550b6e40SSowmini Varadhan 	nvlist_t *nvl;
7105550b6e40SSowmini Varadhan 	uint8_t *ptr;
7106550b6e40SSowmini Varadhan 	uint_t psize;
7107550b6e40SSowmini Varadhan 	int err = 0;
7108550b6e40SSowmini Varadhan 	char *nvname;
7109550b6e40SSowmini Varadhan 	int bufsize;
7110550b6e40SSowmini Varadhan 	void *buf;
7111550b6e40SSowmini Varadhan 	datalink_id_t linkid = znbuf->zn_linkid;
7112550b6e40SSowmini Varadhan 
7113550b6e40SSowmini Varadhan 	if (zoneid == GLOBAL_ZONEID)
7114550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
7115550b6e40SSowmini Varadhan 
7116550b6e40SSowmini Varadhan 	nvname = zone_net_type2name(znbuf->zn_type);
7117550b6e40SSowmini Varadhan 	bufsize = znbuf->zn_len;
7118550b6e40SSowmini Varadhan 	buf = znbuf->zn_val;
7119550b6e40SSowmini Varadhan 
7120550b6e40SSowmini Varadhan 	if (nvname == NULL)
7121550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
7122550b6e40SSowmini Varadhan 	if ((zone = zone_find_by_id(zoneid)) == NULL)
7123550b6e40SSowmini Varadhan 		return (set_errno(EINVAL));
7124550b6e40SSowmini Varadhan 
7125550b6e40SSowmini Varadhan 	mutex_enter(&zone->zone_lock);
7126550b6e40SSowmini Varadhan 	if ((zdl = zone_find_dl(zone, linkid)) == NULL) {
7127550b6e40SSowmini Varadhan 		err = ENXIO;
7128550b6e40SSowmini Varadhan 		goto done;
7129550b6e40SSowmini Varadhan 	}
7130550b6e40SSowmini Varadhan 	if ((nvl = zdl->zdl_net) == NULL || !nvlist_exists(nvl, nvname)) {
7131550b6e40SSowmini Varadhan 		err = ENOENT;
7132550b6e40SSowmini Varadhan 		goto done;
7133550b6e40SSowmini Varadhan 	}
7134550b6e40SSowmini Varadhan 	err = nvlist_lookup_uint8_array(nvl, nvname, &ptr, &psize);
7135550b6e40SSowmini Varadhan 	ASSERT(err == 0);
7136550b6e40SSowmini Varadhan 
7137550b6e40SSowmini Varadhan 	if (psize > bufsize) {
7138550b6e40SSowmini Varadhan 		err = ENOBUFS;
7139550b6e40SSowmini Varadhan 		goto done;
7140550b6e40SSowmini Varadhan 	}
7141550b6e40SSowmini Varadhan 	znbuf->zn_len = psize;
7142550b6e40SSowmini Varadhan 	bcopy(ptr, buf, psize);
7143550b6e40SSowmini Varadhan done:
7144550b6e40SSowmini Varadhan 	mutex_exit(&zone->zone_lock);
7145550b6e40SSowmini Varadhan 	zone_rele(zone);
7146550b6e40SSowmini Varadhan 	if (err != 0)
7147550b6e40SSowmini Varadhan 		return (set_errno(err));
7148550b6e40SSowmini Varadhan 	else
7149550b6e40SSowmini Varadhan 		return (0);
7150550b6e40SSowmini Varadhan }
7151