xref: /illumos-gate/usr/src/uts/common/os/zone.c (revision fa9e4066)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
2297eda132Sraf 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Zones
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  *   A zone is a named collection of processes, namespace constraints,
347c478bd9Sstevel@tonic-gate  *   and other system resources which comprise a secure and manageable
357c478bd9Sstevel@tonic-gate  *   application containment facility.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  *   Zones (represented by the reference counted zone_t) are tracked in
387c478bd9Sstevel@tonic-gate  *   the kernel in the zonehash.  Elsewhere in the kernel, Zone IDs
397c478bd9Sstevel@tonic-gate  *   (zoneid_t) are used to track zone association.  Zone IDs are
407c478bd9Sstevel@tonic-gate  *   dynamically generated when the zone is created; if a persistent
417c478bd9Sstevel@tonic-gate  *   identifier is needed (core files, accounting logs, audit trail,
427c478bd9Sstevel@tonic-gate  *   etc.), the zone name should be used.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  *   Global Zone:
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  *   The global zone (zoneid 0) is automatically associated with all
487c478bd9Sstevel@tonic-gate  *   system resources that have not been bound to a user-created zone.
497c478bd9Sstevel@tonic-gate  *   This means that even systems where zones are not in active use
507c478bd9Sstevel@tonic-gate  *   have a global zone, and all processes, mounts, etc. are
517c478bd9Sstevel@tonic-gate  *   associated with that zone.  The global zone is generally
527c478bd9Sstevel@tonic-gate  *   unconstrained in terms of privileges and access, though the usual
537c478bd9Sstevel@tonic-gate  *   credential and privilege based restrictions apply.
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  *   Zone States:
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  *   The states in which a zone may be in and the transitions are as
597c478bd9Sstevel@tonic-gate  *   follows:
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  *   ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially
627c478bd9Sstevel@tonic-gate  *   initialized zone is added to the list of active zones on the system but
637c478bd9Sstevel@tonic-gate  *   isn't accessible.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  *   ZONE_IS_READY: zsched (the kernel dummy process for a zone) is
667c478bd9Sstevel@tonic-gate  *   ready.  The zone is made visible after the ZSD constructor callbacks are
677c478bd9Sstevel@tonic-gate  *   executed.  A zone remains in this state until it transitions into
687c478bd9Sstevel@tonic-gate  *   the ZONE_IS_BOOTING state as a result of a call to zone_boot().
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  *   ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start
717c478bd9Sstevel@tonic-gate  *   init.  Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN
727c478bd9Sstevel@tonic-gate  *   state.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  *   ZONE_IS_RUNNING: The zone is open for business: zsched has
757c478bd9Sstevel@tonic-gate  *   successfully started init.   A zone remains in this state until
767c478bd9Sstevel@tonic-gate  *   zone_shutdown() is called.
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is
797c478bd9Sstevel@tonic-gate  *   killing all processes running in the zone. The zone remains
807c478bd9Sstevel@tonic-gate  *   in this state until there are no more user processes running in the zone.
817c478bd9Sstevel@tonic-gate  *   zone_create(), zone_enter(), and zone_destroy() on this zone will fail.
827c478bd9Sstevel@tonic-gate  *   Since zone_shutdown() is restartable, it may be called successfully
837c478bd9Sstevel@tonic-gate  *   multiple times for the same zone_t.  Setting of the zone's state to
847c478bd9Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check
857c478bd9Sstevel@tonic-gate  *   the zone's status without worrying about it being a moving target.
867c478bd9Sstevel@tonic-gate  *
877c478bd9Sstevel@tonic-gate  *   ZONE_IS_EMPTY: zone_shutdown() has been called, and there
887c478bd9Sstevel@tonic-gate  *   are no more user processes in the zone.  The zone remains in this
897c478bd9Sstevel@tonic-gate  *   state until there are no more kernel threads associated with the
907c478bd9Sstevel@tonic-gate  *   zone.  zone_create(), zone_enter(), and zone_destroy() on this zone will
917c478bd9Sstevel@tonic-gate  *   fail.
927c478bd9Sstevel@tonic-gate  *
937c478bd9Sstevel@tonic-gate  *   ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone
947c478bd9Sstevel@tonic-gate  *   have exited.  zone_shutdown() returns.  Henceforth it is not possible to
957c478bd9Sstevel@tonic-gate  *   join the zone or create kernel threads therein.
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  *   ZONE_IS_DYING: zone_destroy() has been called on the zone; zone
987c478bd9Sstevel@tonic-gate  *   remains in this state until zsched exits.  Calls to zone_find_by_*()
997c478bd9Sstevel@tonic-gate  *   return NULL from now on.
1007c478bd9Sstevel@tonic-gate  *
1017c478bd9Sstevel@tonic-gate  *   ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0).  There are no
1027c478bd9Sstevel@tonic-gate  *   processes or threads doing work on behalf of the zone.  The zone is
1037c478bd9Sstevel@tonic-gate  *   removed from the list of active zones.  zone_destroy() returns, and
1047c478bd9Sstevel@tonic-gate  *   the zone can be recreated.
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  *   ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor
1077c478bd9Sstevel@tonic-gate  *   callbacks are executed, and all memory associated with the zone is
1087c478bd9Sstevel@tonic-gate  *   freed.
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  *   Threads can wait for the zone to enter a requested state by using
1117c478bd9Sstevel@tonic-gate  *   zone_status_wait() or zone_status_timedwait() with the desired
1127c478bd9Sstevel@tonic-gate  *   state passed in as an argument.  Zone state transitions are
1137c478bd9Sstevel@tonic-gate  *   uni-directional; it is not possible to move back to an earlier state.
1147c478bd9Sstevel@tonic-gate  *
1157c478bd9Sstevel@tonic-gate  *
1167c478bd9Sstevel@tonic-gate  *   Zone-Specific Data:
1177c478bd9Sstevel@tonic-gate  *
1187c478bd9Sstevel@tonic-gate  *   Subsystems needing to maintain zone-specific data can store that
1197c478bd9Sstevel@tonic-gate  *   data using the ZSD mechanism.  This provides a zone-specific data
1207c478bd9Sstevel@tonic-gate  *   store, similar to thread-specific data (see pthread_getspecific(3C)
1217c478bd9Sstevel@tonic-gate  *   or the TSD code in uts/common/disp/thread.c.  Also, ZSD can be used
1227c478bd9Sstevel@tonic-gate  *   to register callbacks to be invoked when a zone is created, shut
1237c478bd9Sstevel@tonic-gate  *   down, or destroyed.  This can be used to initialize zone-specific
1247c478bd9Sstevel@tonic-gate  *   data for new zones and to clean up when zones go away.
1257c478bd9Sstevel@tonic-gate  *
1267c478bd9Sstevel@tonic-gate  *
1277c478bd9Sstevel@tonic-gate  *   Data Structures:
1287c478bd9Sstevel@tonic-gate  *
1297c478bd9Sstevel@tonic-gate  *   The per-zone structure (zone_t) is reference counted, and freed
1307c478bd9Sstevel@tonic-gate  *   when all references are released.  zone_hold and zone_rele can be
1317c478bd9Sstevel@tonic-gate  *   used to adjust the reference count.  In addition, reference counts
1327c478bd9Sstevel@tonic-gate  *   associated with the cred_t structure are tracked separately using
1337c478bd9Sstevel@tonic-gate  *   zone_cred_hold and zone_cred_rele.
1347c478bd9Sstevel@tonic-gate  *
1357c478bd9Sstevel@tonic-gate  *   Pointers to active zone_t's are stored in two hash tables; one
1367c478bd9Sstevel@tonic-gate  *   for searching by id, the other for searching by name.  Lookups
1377c478bd9Sstevel@tonic-gate  *   can be performed on either basis, using zone_find_by_id and
1387c478bd9Sstevel@tonic-gate  *   zone_find_by_name.  Both return zone_t pointers with the zone
1397c478bd9Sstevel@tonic-gate  *   held, so zone_rele should be called when the pointer is no longer
1407c478bd9Sstevel@tonic-gate  *   needed.  Zones can also be searched by path; zone_find_by_path
1417c478bd9Sstevel@tonic-gate  *   returns the zone with which a path name is associated (global
1427c478bd9Sstevel@tonic-gate  *   zone if the path is not within some other zone's file system
1437c478bd9Sstevel@tonic-gate  *   hierarchy).  This currently requires iterating through each zone,
1447c478bd9Sstevel@tonic-gate  *   so it is slower than an id or name search via a hash table.
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  *
1477c478bd9Sstevel@tonic-gate  *   Locking:
1487c478bd9Sstevel@tonic-gate  *
1497c478bd9Sstevel@tonic-gate  *   zonehash_lock: This is a top-level global lock used to protect the
1507c478bd9Sstevel@tonic-gate  *       zone hash tables and lists.  Zones cannot be created or destroyed
1517c478bd9Sstevel@tonic-gate  *       while this lock is held.
1527c478bd9Sstevel@tonic-gate  *   zone_status_lock: This is a global lock protecting zone state.
1537c478bd9Sstevel@tonic-gate  *       Zones cannot change state while this lock is held.  It also
1547c478bd9Sstevel@tonic-gate  *       protects the list of kernel threads associated with a zone.
1557c478bd9Sstevel@tonic-gate  *   zone_lock: This is a per-zone lock used to protect several fields of
1567c478bd9Sstevel@tonic-gate  *       the zone_t (see <sys/zone.h> for details).  In addition, holding
1577c478bd9Sstevel@tonic-gate  *       this lock means that the zone cannot go away.
1587c478bd9Sstevel@tonic-gate  *   zsd_key_lock: This is a global lock protecting the key state for ZSD.
1597c478bd9Sstevel@tonic-gate  *   zone_deathrow_lock: This is a global lock protecting the "deathrow"
1607c478bd9Sstevel@tonic-gate  *       list (a list of zones in the ZONE_IS_DEAD state).
1617c478bd9Sstevel@tonic-gate  *
1627c478bd9Sstevel@tonic-gate  *   Ordering requirements:
1637c478bd9Sstevel@tonic-gate  *       pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock -->
1647c478bd9Sstevel@tonic-gate  *       	zone_lock --> zsd_key_lock --> pidlock --> p_lock
1657c478bd9Sstevel@tonic-gate  *
1667c478bd9Sstevel@tonic-gate  *   Blocking memory allocations are permitted while holding any of the
1677c478bd9Sstevel@tonic-gate  *   zone locks.
1687c478bd9Sstevel@tonic-gate  *
1697c478bd9Sstevel@tonic-gate  *
1707c478bd9Sstevel@tonic-gate  *   System Call Interface:
1717c478bd9Sstevel@tonic-gate  *
1727c478bd9Sstevel@tonic-gate  *   The zone subsystem can be managed and queried from user level with
1737c478bd9Sstevel@tonic-gate  *   the following system calls (all subcodes of the primary "zone"
1747c478bd9Sstevel@tonic-gate  *   system call):
1757c478bd9Sstevel@tonic-gate  *   - zone_create: creates a zone with selected attributes (name,
176*fa9e4066Sahrens  *     root path, privileges, resource controls, ZFS datasets)
1777c478bd9Sstevel@tonic-gate  *   - zone_enter: allows the current process to enter a zone
1787c478bd9Sstevel@tonic-gate  *   - zone_getattr: reports attributes of a zone
1797c478bd9Sstevel@tonic-gate  *   - zone_list: lists all zones active in the system
1807c478bd9Sstevel@tonic-gate  *   - zone_lookup: looks up zone id based on name
1817c478bd9Sstevel@tonic-gate  *   - zone_shutdown: initiates shutdown process (see states above)
1827c478bd9Sstevel@tonic-gate  *   - zone_destroy: completes shutdown process (see states above)
1837c478bd9Sstevel@tonic-gate  *
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate #include <sys/priv_impl.h>
1877c478bd9Sstevel@tonic-gate #include <sys/cred.h>
1887c478bd9Sstevel@tonic-gate #include <c2/audit.h>
1897c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
1907c478bd9Sstevel@tonic-gate #include <sys/debug.h>
1917c478bd9Sstevel@tonic-gate #include <sys/file.h>
1927c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
1937c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
1947c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
1957c478bd9Sstevel@tonic-gate #include <sys/proc.h>
1967c478bd9Sstevel@tonic-gate #include <sys/project.h>
1977c478bd9Sstevel@tonic-gate #include <sys/task.h>
1987c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1997c478bd9Sstevel@tonic-gate #include <sys/types.h>
2007c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
2017c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
2027c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
2037c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
2047c478bd9Sstevel@tonic-gate #include <sys/policy.h>
2057c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
2067c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h>
2077c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
2087c478bd9Sstevel@tonic-gate #include <sys/class.h>
2097c478bd9Sstevel@tonic-gate #include <sys/pool.h>
2107c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
2117c478bd9Sstevel@tonic-gate #include <sys/pset.h>
2127c478bd9Sstevel@tonic-gate #include <sys/log.h>
2137c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
2147c478bd9Sstevel@tonic-gate #include <sys/callb.h>
2157c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
2167c478bd9Sstevel@tonic-gate #include <sys/corectl.h>
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate #include <sys/door.h>
2197c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
2207c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
2237c478bd9Sstevel@tonic-gate #include <sys/session.h>
2247c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
2257c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
2267c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
2277c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
2287c478bd9Sstevel@tonic-gate #include <sys/fss.h>
2297c478bd9Sstevel@tonic-gate #include <sys/zone.h>
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * cv used to signal that all references to the zone have been released.  This
2337c478bd9Sstevel@tonic-gate  * needs to be global since there may be multiple waiters, and the first to
2347c478bd9Sstevel@tonic-gate  * wake up will free the zone_t, hence we cannot use zone->zone_cv.
2357c478bd9Sstevel@tonic-gate  */
2367c478bd9Sstevel@tonic-gate static kcondvar_t zone_destroy_cv;
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate  * Lock used to serialize access to zone_cv.  This could have been per-zone,
2397c478bd9Sstevel@tonic-gate  * but then we'd need another lock for zone_destroy_cv, and why bother?
2407c478bd9Sstevel@tonic-gate  */
2417c478bd9Sstevel@tonic-gate static kmutex_t zone_status_lock;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate  * ZSD-related global variables.
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate static kmutex_t zsd_key_lock;	/* protects the following two */
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval.
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate static zone_key_t zsd_keyval = 0;
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * Global list of registered keys.  We use this when a new zone is created.
2537c478bd9Sstevel@tonic-gate  */
2547c478bd9Sstevel@tonic-gate static list_t zsd_registered_keys;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate int zone_hash_size = 256;
2577c478bd9Sstevel@tonic-gate static mod_hash_t *zonehashbyname, *zonehashbyid;
2587c478bd9Sstevel@tonic-gate static kmutex_t zonehash_lock;
2597c478bd9Sstevel@tonic-gate static uint_t zonecount;
2607c478bd9Sstevel@tonic-gate static id_space_t *zoneid_space;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate  * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the
2647c478bd9Sstevel@tonic-gate  * kernel proper runs, and which manages all other zones.
2657c478bd9Sstevel@tonic-gate  *
2667c478bd9Sstevel@tonic-gate  * Although not declared as static, the variable "zone0" should not be used
2677c478bd9Sstevel@tonic-gate  * except for by code that needs to reference the global zone early on in boot,
2687c478bd9Sstevel@tonic-gate  * before it is fully initialized.  All other consumers should use
2697c478bd9Sstevel@tonic-gate  * 'global_zone'.
2707c478bd9Sstevel@tonic-gate  */
2717c478bd9Sstevel@tonic-gate zone_t zone0;
2727c478bd9Sstevel@tonic-gate zone_t *global_zone = NULL;	/* Set when the global zone is initialized */
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate  * List of active zones, protected by zonehash_lock.
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate static list_t zone_active;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate  * List of destroyed zones that still have outstanding cred references.
2817c478bd9Sstevel@tonic-gate  * Used for debugging.  Uses a separate lock to avoid lock ordering
2827c478bd9Sstevel@tonic-gate  * problems in zone_free.
2837c478bd9Sstevel@tonic-gate  */
2847c478bd9Sstevel@tonic-gate static list_t zone_deathrow;
2857c478bd9Sstevel@tonic-gate static kmutex_t zone_deathrow_lock;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */
2887c478bd9Sstevel@tonic-gate uint_t maxzones = 8192;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate /*
2917c478bd9Sstevel@tonic-gate  * This isn't static so lint doesn't complain.
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares;
2947c478bd9Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps;
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate  * Synchronization primitives used to synchronize between mounts and zone
2977c478bd9Sstevel@tonic-gate  * creation/destruction.
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate static int mounts_in_progress;
3007c478bd9Sstevel@tonic-gate static kcondvar_t mount_cv;
3017c478bd9Sstevel@tonic-gate static kmutex_t mount_lock;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate const char * const zone_initname = "/sbin/init";
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate /*
3087c478bd9Sstevel@tonic-gate  * Certain filesystems (such as NFS and autofs) need to know which zone
3097c478bd9Sstevel@tonic-gate  * the mount is being placed in.  Because of this, we need to be able to
3107c478bd9Sstevel@tonic-gate  * ensure that a zone isn't in the process of being created such that
3117c478bd9Sstevel@tonic-gate  * nfs_mount() thinks it is in the global zone, while by the time it
3127c478bd9Sstevel@tonic-gate  * gets added the list of mounted zones, it ends up on zoneA's mount
3137c478bd9Sstevel@tonic-gate  * list.
3147c478bd9Sstevel@tonic-gate  *
3157c478bd9Sstevel@tonic-gate  * The following functions: block_mounts()/resume_mounts() and
3167c478bd9Sstevel@tonic-gate  * mount_in_progress()/mount_completed() are used by zones and the VFS
3177c478bd9Sstevel@tonic-gate  * layer (respectively) to synchronize zone creation and new mounts.
3187c478bd9Sstevel@tonic-gate  *
3197c478bd9Sstevel@tonic-gate  * The semantics are like a reader-reader lock such that there may
3207c478bd9Sstevel@tonic-gate  * either be multiple mounts (or zone creations, if that weren't
3217c478bd9Sstevel@tonic-gate  * serialized by zonehash_lock) in progress at the same time, but not
3227c478bd9Sstevel@tonic-gate  * both.
3237c478bd9Sstevel@tonic-gate  *
3247c478bd9Sstevel@tonic-gate  * We use cv's so the user can ctrl-C out of the operation if it's
3257c478bd9Sstevel@tonic-gate  * taking too long.
3267c478bd9Sstevel@tonic-gate  *
3277c478bd9Sstevel@tonic-gate  * The semantics are such that there is unfair bias towards the
3287c478bd9Sstevel@tonic-gate  * "current" operation.  This means that zone creations may starve if
3297c478bd9Sstevel@tonic-gate  * there is a rapid succession of new mounts coming in to the system, or
3307c478bd9Sstevel@tonic-gate  * there is a remote possibility that zones will be created at such a
3317c478bd9Sstevel@tonic-gate  * rate that new mounts will not be able to proceed.
3327c478bd9Sstevel@tonic-gate  */
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate  * Prevent new mounts from progressing to the point of calling
3357c478bd9Sstevel@tonic-gate  * VFS_MOUNT().  If there are already mounts in this "region", wait for
3367c478bd9Sstevel@tonic-gate  * them to complete.
3377c478bd9Sstevel@tonic-gate  */
3387c478bd9Sstevel@tonic-gate static int
3397c478bd9Sstevel@tonic-gate block_mounts(void)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	int retval = 0;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	/*
3447c478bd9Sstevel@tonic-gate 	 * Since it may block for a long time, block_mounts() shouldn't be
3457c478bd9Sstevel@tonic-gate 	 * called with zonehash_lock held.
3467c478bd9Sstevel@tonic-gate 	 */
3477c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
3487c478bd9Sstevel@tonic-gate 	mutex_enter(&mount_lock);
3497c478bd9Sstevel@tonic-gate 	while (mounts_in_progress > 0) {
3507c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&mount_cv, &mount_lock) == 0)
3517c478bd9Sstevel@tonic-gate 			goto signaled;
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 	/*
3547c478bd9Sstevel@tonic-gate 	 * A negative value of mounts_in_progress indicates that mounts
3557c478bd9Sstevel@tonic-gate 	 * have been blocked by (-mounts_in_progress) different callers.
3567c478bd9Sstevel@tonic-gate 	 */
3577c478bd9Sstevel@tonic-gate 	mounts_in_progress--;
3587c478bd9Sstevel@tonic-gate 	retval = 1;
3597c478bd9Sstevel@tonic-gate signaled:
3607c478bd9Sstevel@tonic-gate 	mutex_exit(&mount_lock);
3617c478bd9Sstevel@tonic-gate 	return (retval);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate  * The VFS layer may progress with new mounts as far as we're concerned.
3667c478bd9Sstevel@tonic-gate  * Allow them to progress if we were the last obstacle.
3677c478bd9Sstevel@tonic-gate  */
3687c478bd9Sstevel@tonic-gate static void
3697c478bd9Sstevel@tonic-gate resume_mounts(void)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	mutex_enter(&mount_lock);
3727c478bd9Sstevel@tonic-gate 	if (++mounts_in_progress == 0)
3737c478bd9Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
3747c478bd9Sstevel@tonic-gate 	mutex_exit(&mount_lock);
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * The VFS layer is busy with a mount; zones should wait until all
3797c478bd9Sstevel@tonic-gate  * mounts are completed to progress.
3807c478bd9Sstevel@tonic-gate  */
3817c478bd9Sstevel@tonic-gate void
3827c478bd9Sstevel@tonic-gate mount_in_progress(void)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	mutex_enter(&mount_lock);
3857c478bd9Sstevel@tonic-gate 	while (mounts_in_progress < 0)
3867c478bd9Sstevel@tonic-gate 		cv_wait(&mount_cv, &mount_lock);
3877c478bd9Sstevel@tonic-gate 	mounts_in_progress++;
3887c478bd9Sstevel@tonic-gate 	mutex_exit(&mount_lock);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  * VFS is done with one mount; wake up any waiting block_mounts()
3937c478bd9Sstevel@tonic-gate  * callers if this is the last mount.
3947c478bd9Sstevel@tonic-gate  */
3957c478bd9Sstevel@tonic-gate void
3967c478bd9Sstevel@tonic-gate mount_completed(void)
3977c478bd9Sstevel@tonic-gate {
3987c478bd9Sstevel@tonic-gate 	mutex_enter(&mount_lock);
3997c478bd9Sstevel@tonic-gate 	if (--mounts_in_progress == 0)
4007c478bd9Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
4017c478bd9Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate /*
4057c478bd9Sstevel@tonic-gate  * ZSD routines.
4067c478bd9Sstevel@tonic-gate  *
4077c478bd9Sstevel@tonic-gate  * Zone Specific Data (ZSD) is modeled after Thread Specific Data as
4087c478bd9Sstevel@tonic-gate  * defined by the pthread_key_create() and related interfaces.
4097c478bd9Sstevel@tonic-gate  *
4107c478bd9Sstevel@tonic-gate  * Kernel subsystems may register one or more data items and/or
4117c478bd9Sstevel@tonic-gate  * callbacks to be executed when a zone is created, shutdown, or
4127c478bd9Sstevel@tonic-gate  * destroyed.
4137c478bd9Sstevel@tonic-gate  *
4147c478bd9Sstevel@tonic-gate  * Unlike the thread counterpart, destructor callbacks will be executed
4157c478bd9Sstevel@tonic-gate  * even if the data pointer is NULL and/or there are no constructor
4167c478bd9Sstevel@tonic-gate  * callbacks, so it is the responsibility of such callbacks to check for
4177c478bd9Sstevel@tonic-gate  * NULL data values if necessary.
4187c478bd9Sstevel@tonic-gate  *
4197c478bd9Sstevel@tonic-gate  * The locking strategy and overall picture is as follows:
4207c478bd9Sstevel@tonic-gate  *
4217c478bd9Sstevel@tonic-gate  * When someone calls zone_key_create(), a template ZSD entry is added to the
4227c478bd9Sstevel@tonic-gate  * global list "zsd_registered_keys", protected by zsd_key_lock.  The
4237c478bd9Sstevel@tonic-gate  * constructor callback is called immediately on all existing zones, and a
4247c478bd9Sstevel@tonic-gate  * copy of the ZSD entry added to the per-zone zone_zsd list (protected by
4257c478bd9Sstevel@tonic-gate  * zone_lock).  As this operation requires the list of zones, the list of
4267c478bd9Sstevel@tonic-gate  * registered keys, and the per-zone list of ZSD entries to remain constant
4277c478bd9Sstevel@tonic-gate  * throughout the entire operation, it must grab zonehash_lock, zone_lock for
4287c478bd9Sstevel@tonic-gate  * all existing zones, and zsd_key_lock, in that order.  Similar locking is
4297c478bd9Sstevel@tonic-gate  * needed when zone_key_delete() is called.  It is thus sufficient to hold
4307c478bd9Sstevel@tonic-gate  * zsd_key_lock *or* zone_lock to prevent additions to or removals from the
4317c478bd9Sstevel@tonic-gate  * per-zone zone_zsd list.
4327c478bd9Sstevel@tonic-gate  *
4337c478bd9Sstevel@tonic-gate  * Note that this implementation does not make a copy of the ZSD entry if a
4347c478bd9Sstevel@tonic-gate  * constructor callback is not provided.  A zone_getspecific() on such an
4357c478bd9Sstevel@tonic-gate  * uninitialized ZSD entry will return NULL.
4367c478bd9Sstevel@tonic-gate  *
4377c478bd9Sstevel@tonic-gate  * When new zones are created constructor callbacks for all registered ZSD
4387c478bd9Sstevel@tonic-gate  * entries will be called.
4397c478bd9Sstevel@tonic-gate  *
4407c478bd9Sstevel@tonic-gate  * The framework does not provide any locking around zone_getspecific() and
4417c478bd9Sstevel@tonic-gate  * zone_setspecific() apart from that needed for internal consistency, so
4427c478bd9Sstevel@tonic-gate  * callers interested in atomic "test-and-set" semantics will need to provide
4437c478bd9Sstevel@tonic-gate  * their own locking.
4447c478bd9Sstevel@tonic-gate  */
4457c478bd9Sstevel@tonic-gate void
4467c478bd9Sstevel@tonic-gate zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t),
4477c478bd9Sstevel@tonic-gate     void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *))
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsdp;
4507c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
4517c478bd9Sstevel@tonic-gate 	struct zone *zone;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	zsdp = kmem_alloc(sizeof (*zsdp), KM_SLEEP);
4547c478bd9Sstevel@tonic-gate 	zsdp->zsd_data = NULL;
4557c478bd9Sstevel@tonic-gate 	zsdp->zsd_create = create;
4567c478bd9Sstevel@tonic-gate 	zsdp->zsd_shutdown = shutdown;
4577c478bd9Sstevel@tonic-gate 	zsdp->zsd_destroy = destroy;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);	/* stop the world */
4607c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
4617c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
4627c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);	/* lock all zones */
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
4657c478bd9Sstevel@tonic-gate 	*keyp = zsdp->zsd_key = ++zsd_keyval;
4667c478bd9Sstevel@tonic-gate 	ASSERT(zsd_keyval != 0);
4677c478bd9Sstevel@tonic-gate 	list_insert_tail(&zsd_registered_keys, zsdp);
4687c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if (create != NULL) {
4717c478bd9Sstevel@tonic-gate 		for (zone = list_head(&zone_active); zone != NULL;
4727c478bd9Sstevel@tonic-gate 		    zone = list_next(&zone_active, zone)) {
4737c478bd9Sstevel@tonic-gate 			t = kmem_alloc(sizeof (*t), KM_SLEEP);
4747c478bd9Sstevel@tonic-gate 			t->zsd_key = *keyp;
4757c478bd9Sstevel@tonic-gate 			t->zsd_data = (*create)(zone->zone_id);
4767c478bd9Sstevel@tonic-gate 			t->zsd_create = create;
4777c478bd9Sstevel@tonic-gate 			t->zsd_shutdown = shutdown;
4787c478bd9Sstevel@tonic-gate 			t->zsd_destroy = destroy;
4797c478bd9Sstevel@tonic-gate 			list_insert_tail(&zone->zone_zsd, t);
4807c478bd9Sstevel@tonic-gate 		}
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
4837c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
4847c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
4857c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate /*
4897c478bd9Sstevel@tonic-gate  * Helper function to find the zsd_entry associated with the key in the
4907c478bd9Sstevel@tonic-gate  * given list.
4917c478bd9Sstevel@tonic-gate  */
4927c478bd9Sstevel@tonic-gate static struct zsd_entry *
4937c478bd9Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsd;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) {
4987c478bd9Sstevel@tonic-gate 		if (zsd->zsd_key == key) {
4997c478bd9Sstevel@tonic-gate 			/*
5007c478bd9Sstevel@tonic-gate 			 * Move to head of list to keep list in MRU order.
5017c478bd9Sstevel@tonic-gate 			 */
5027c478bd9Sstevel@tonic-gate 			if (zsd != list_head(l)) {
5037c478bd9Sstevel@tonic-gate 				list_remove(l, zsd);
5047c478bd9Sstevel@tonic-gate 				list_insert_head(l, zsd);
5057c478bd9Sstevel@tonic-gate 			}
5067c478bd9Sstevel@tonic-gate 			return (zsd);
5077c478bd9Sstevel@tonic-gate 		}
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 	return (NULL);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate /*
5137c478bd9Sstevel@tonic-gate  * Function called when a module is being unloaded, or otherwise wishes
5147c478bd9Sstevel@tonic-gate  * to unregister its ZSD key and callbacks.
5157c478bd9Sstevel@tonic-gate  */
5167c478bd9Sstevel@tonic-gate int
5177c478bd9Sstevel@tonic-gate zone_key_delete(zone_key_t key)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsdp = NULL;
5207c478bd9Sstevel@tonic-gate 	zone_t *zone;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);	/* Zone create/delete waits for us */
5237c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
5247c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
5257c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);	/* lock all zones */
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
5287c478bd9Sstevel@tonic-gate 	zsdp = zsd_find(&zsd_registered_keys, key);
5297c478bd9Sstevel@tonic-gate 	if (zsdp == NULL)
5307c478bd9Sstevel@tonic-gate 		goto notfound;
5317c478bd9Sstevel@tonic-gate 	list_remove(&zsd_registered_keys, zsdp);
5327c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
5357c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
5367c478bd9Sstevel@tonic-gate 		struct zsd_entry *del;
5377c478bd9Sstevel@tonic-gate 		void *data;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 		if (!(zone->zone_flags & ZF_DESTROYED)) {
5407c478bd9Sstevel@tonic-gate 			del = zsd_find(&zone->zone_zsd, key);
5417c478bd9Sstevel@tonic-gate 			if (del != NULL) {
5427c478bd9Sstevel@tonic-gate 				data = del->zsd_data;
5437c478bd9Sstevel@tonic-gate 				ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown);
5447c478bd9Sstevel@tonic-gate 				ASSERT(del->zsd_destroy == zsdp->zsd_destroy);
5457c478bd9Sstevel@tonic-gate 				list_remove(&zone->zone_zsd, del);
5467c478bd9Sstevel@tonic-gate 				kmem_free(del, sizeof (*del));
5477c478bd9Sstevel@tonic-gate 			} else {
5487c478bd9Sstevel@tonic-gate 				data = NULL;
5497c478bd9Sstevel@tonic-gate 			}
5507c478bd9Sstevel@tonic-gate 			if (zsdp->zsd_shutdown)
5517c478bd9Sstevel@tonic-gate 				zsdp->zsd_shutdown(zone->zone_id, data);
5527c478bd9Sstevel@tonic-gate 			if (zsdp->zsd_destroy)
5537c478bd9Sstevel@tonic-gate 				zsdp->zsd_destroy(zone->zone_id, data);
5547c478bd9Sstevel@tonic-gate 		}
5557c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
5587c478bd9Sstevel@tonic-gate 	kmem_free(zsdp, sizeof (*zsdp));
5597c478bd9Sstevel@tonic-gate 	return (0);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate notfound:
5627c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
5637c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
5647c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
5657c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
5667c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
5677c478bd9Sstevel@tonic-gate 	return (-1);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate  * ZSD counterpart of pthread_setspecific().
5727c478bd9Sstevel@tonic-gate  */
5737c478bd9Sstevel@tonic-gate int
5747c478bd9Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
5777c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsdp = NULL;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
5807c478bd9Sstevel@tonic-gate 	t = zsd_find(&zone->zone_zsd, key);
5817c478bd9Sstevel@tonic-gate 	if (t != NULL) {
5827c478bd9Sstevel@tonic-gate 		/*
5837c478bd9Sstevel@tonic-gate 		 * Replace old value with new
5847c478bd9Sstevel@tonic-gate 		 */
5857c478bd9Sstevel@tonic-gate 		t->zsd_data = (void *)data;
5867c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
5877c478bd9Sstevel@tonic-gate 		return (0);
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 	/*
5907c478bd9Sstevel@tonic-gate 	 * If there was no previous value, go through the list of registered
5917c478bd9Sstevel@tonic-gate 	 * keys.
5927c478bd9Sstevel@tonic-gate 	 *
5937c478bd9Sstevel@tonic-gate 	 * We avoid grabbing zsd_key_lock until we are sure we need it; this is
5947c478bd9Sstevel@tonic-gate 	 * necessary for shutdown callbacks to be able to execute without fear
5957c478bd9Sstevel@tonic-gate 	 * of deadlock.
5967c478bd9Sstevel@tonic-gate 	 */
5977c478bd9Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
5987c478bd9Sstevel@tonic-gate 	zsdp = zsd_find(&zsd_registered_keys, key);
5997c478bd9Sstevel@tonic-gate 	if (zsdp == NULL) { 	/* Key was not registered */
6007c478bd9Sstevel@tonic-gate 		mutex_exit(&zsd_key_lock);
6017c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
6027c478bd9Sstevel@tonic-gate 		return (-1);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	/*
6067c478bd9Sstevel@tonic-gate 	 * Add a zsd_entry to this zone, using the template we just retrieved
6077c478bd9Sstevel@tonic-gate 	 * to initialize the constructor and destructor(s).
6087c478bd9Sstevel@tonic-gate 	 */
6097c478bd9Sstevel@tonic-gate 	t = kmem_alloc(sizeof (*t), KM_SLEEP);
6107c478bd9Sstevel@tonic-gate 	t->zsd_key = key;
6117c478bd9Sstevel@tonic-gate 	t->zsd_data = (void *)data;
6127c478bd9Sstevel@tonic-gate 	t->zsd_create = zsdp->zsd_create;
6137c478bd9Sstevel@tonic-gate 	t->zsd_shutdown = zsdp->zsd_shutdown;
6147c478bd9Sstevel@tonic-gate 	t->zsd_destroy = zsdp->zsd_destroy;
6157c478bd9Sstevel@tonic-gate 	list_insert_tail(&zone->zone_zsd, t);
6167c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
6177c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
6187c478bd9Sstevel@tonic-gate 	return (0);
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate /*
6227c478bd9Sstevel@tonic-gate  * ZSD counterpart of pthread_getspecific().
6237c478bd9Sstevel@tonic-gate  */
6247c478bd9Sstevel@tonic-gate void *
6257c478bd9Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone)
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
6287c478bd9Sstevel@tonic-gate 	void *data;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
6317c478bd9Sstevel@tonic-gate 	t = zsd_find(&zone->zone_zsd, key);
6327c478bd9Sstevel@tonic-gate 	data = (t == NULL ? NULL : t->zsd_data);
6337c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
6347c478bd9Sstevel@tonic-gate 	return (data);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate  * Function used to initialize a zone's list of ZSD callbacks and data
6397c478bd9Sstevel@tonic-gate  * when the zone is being created.  The callbacks are initialized from
6407c478bd9Sstevel@tonic-gate  * the template list (zsd_registered_keys), and the constructor
6417c478bd9Sstevel@tonic-gate  * callback executed (if one exists).
6427c478bd9Sstevel@tonic-gate  *
6437c478bd9Sstevel@tonic-gate  * This is called before the zone is made publicly available, hence no
6447c478bd9Sstevel@tonic-gate  * need to grab zone_lock.
6457c478bd9Sstevel@tonic-gate  *
6467c478bd9Sstevel@tonic-gate  * Although we grab and release zsd_key_lock, new entries cannot be
6477c478bd9Sstevel@tonic-gate  * added to or removed from the zsd_registered_keys list until we
6487c478bd9Sstevel@tonic-gate  * release zonehash_lock, so there isn't a window for a
6497c478bd9Sstevel@tonic-gate  * zone_key_create() to come in after we've dropped zsd_key_lock but
6507c478bd9Sstevel@tonic-gate  * before the zone is added to the zone list, such that the constructor
6517c478bd9Sstevel@tonic-gate  * callbacks aren't executed for the new zone.
6527c478bd9Sstevel@tonic-gate  */
6537c478bd9Sstevel@tonic-gate static void
6547c478bd9Sstevel@tonic-gate zone_zsd_configure(zone_t *zone)
6557c478bd9Sstevel@tonic-gate {
6567c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsdp;
6577c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
6587c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = zone->zone_id;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
6617c478bd9Sstevel@tonic-gate 	ASSERT(list_head(&zone->zone_zsd) == NULL);
6627c478bd9Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
6637c478bd9Sstevel@tonic-gate 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
6647c478bd9Sstevel@tonic-gate 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
6657c478bd9Sstevel@tonic-gate 		if (zsdp->zsd_create != NULL) {
6667c478bd9Sstevel@tonic-gate 			t = kmem_alloc(sizeof (*t), KM_SLEEP);
6677c478bd9Sstevel@tonic-gate 			t->zsd_key = zsdp->zsd_key;
6687c478bd9Sstevel@tonic-gate 			t->zsd_create = zsdp->zsd_create;
6697c478bd9Sstevel@tonic-gate 			t->zsd_data = (*t->zsd_create)(zoneid);
6707c478bd9Sstevel@tonic-gate 			t->zsd_shutdown = zsdp->zsd_shutdown;
6717c478bd9Sstevel@tonic-gate 			t->zsd_destroy = zsdp->zsd_destroy;
6727c478bd9Sstevel@tonic-gate 			list_insert_tail(&zone->zone_zsd, t);
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY };
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate /*
6817c478bd9Sstevel@tonic-gate  * Helper function to execute shutdown or destructor callbacks.
6827c478bd9Sstevel@tonic-gate  */
6837c478bd9Sstevel@tonic-gate static void
6847c478bd9Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsdp;
6877c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
6887c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = zone->zone_id;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY);
6917c478bd9Sstevel@tonic-gate 	ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY);
6927c478bd9Sstevel@tonic-gate 	ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN);
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
6957c478bd9Sstevel@tonic-gate 	if (ct == ZSD_DESTROY) {
6967c478bd9Sstevel@tonic-gate 		if (zone->zone_flags & ZF_DESTROYED) {
6977c478bd9Sstevel@tonic-gate 			/*
6987c478bd9Sstevel@tonic-gate 			 * Make sure destructors are only called once.
6997c478bd9Sstevel@tonic-gate 			 */
7007c478bd9Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
7017c478bd9Sstevel@tonic-gate 			return;
7027c478bd9Sstevel@tonic-gate 		}
7037c478bd9Sstevel@tonic-gate 		zone->zone_flags |= ZF_DESTROYED;
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	/*
7087c478bd9Sstevel@tonic-gate 	 * Both zsd_key_lock and zone_lock need to be held in order to add or
7097c478bd9Sstevel@tonic-gate 	 * remove a ZSD key, (either globally as part of
7107c478bd9Sstevel@tonic-gate 	 * zone_key_create()/zone_key_delete(), or on a per-zone basis, as is
7117c478bd9Sstevel@tonic-gate 	 * possible through zone_setspecific()), so it's sufficient to hold
7127c478bd9Sstevel@tonic-gate 	 * zsd_key_lock here.
7137c478bd9Sstevel@tonic-gate 	 *
7147c478bd9Sstevel@tonic-gate 	 * This is a good thing, since we don't want to recursively try to grab
7157c478bd9Sstevel@tonic-gate 	 * zone_lock if a callback attempts to do something like a crfree() or
7167c478bd9Sstevel@tonic-gate 	 * zone_rele().
7177c478bd9Sstevel@tonic-gate 	 */
7187c478bd9Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
7197c478bd9Sstevel@tonic-gate 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
7207c478bd9Sstevel@tonic-gate 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
7217c478bd9Sstevel@tonic-gate 		zone_key_t key = zsdp->zsd_key;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 		/* Skip if no callbacks registered */
7247c478bd9Sstevel@tonic-gate 		if (ct == ZSD_SHUTDOWN && zsdp->zsd_shutdown == NULL)
7257c478bd9Sstevel@tonic-gate 			continue;
7267c478bd9Sstevel@tonic-gate 		if (ct == ZSD_DESTROY && zsdp->zsd_destroy == NULL)
7277c478bd9Sstevel@tonic-gate 			continue;
7287c478bd9Sstevel@tonic-gate 		/*
7297c478bd9Sstevel@tonic-gate 		 * Call the callback with the zone-specific data if we can find
7307c478bd9Sstevel@tonic-gate 		 * any, otherwise with NULL.
7317c478bd9Sstevel@tonic-gate 		 */
7327c478bd9Sstevel@tonic-gate 		t = zsd_find(&zone->zone_zsd, key);
7337c478bd9Sstevel@tonic-gate 		if (t != NULL) {
7347c478bd9Sstevel@tonic-gate 			if (ct == ZSD_SHUTDOWN) {
7357c478bd9Sstevel@tonic-gate 				t->zsd_shutdown(zoneid, t->zsd_data);
7367c478bd9Sstevel@tonic-gate 			} else {
7377c478bd9Sstevel@tonic-gate 				ASSERT(ct == ZSD_DESTROY);
7387c478bd9Sstevel@tonic-gate 				t->zsd_destroy(zoneid, t->zsd_data);
7397c478bd9Sstevel@tonic-gate 			}
7407c478bd9Sstevel@tonic-gate 		} else {
7417c478bd9Sstevel@tonic-gate 			if (ct == ZSD_SHUTDOWN) {
7427c478bd9Sstevel@tonic-gate 				zsdp->zsd_shutdown(zoneid, NULL);
7437c478bd9Sstevel@tonic-gate 			} else {
7447c478bd9Sstevel@tonic-gate 				ASSERT(ct == ZSD_DESTROY);
7457c478bd9Sstevel@tonic-gate 				zsdp->zsd_destroy(zoneid, NULL);
7467c478bd9Sstevel@tonic-gate 			}
7477c478bd9Sstevel@tonic-gate 		}
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate /*
7537c478bd9Sstevel@tonic-gate  * Called when the zone is going away; free ZSD-related memory, and
7547c478bd9Sstevel@tonic-gate  * destroy the zone_zsd list.
7557c478bd9Sstevel@tonic-gate  */
7567c478bd9Sstevel@tonic-gate static void
7577c478bd9Sstevel@tonic-gate zone_free_zsd(zone_t *zone)
7587c478bd9Sstevel@tonic-gate {
7597c478bd9Sstevel@tonic-gate 	struct zsd_entry *t, *next;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	/*
7627c478bd9Sstevel@tonic-gate 	 * Free all the zsd_entry's we had on this zone.
7637c478bd9Sstevel@tonic-gate 	 */
7647c478bd9Sstevel@tonic-gate 	for (t = list_head(&zone->zone_zsd); t != NULL; t = next) {
7657c478bd9Sstevel@tonic-gate 		next = list_next(&zone->zone_zsd, t);
7667c478bd9Sstevel@tonic-gate 		list_remove(&zone->zone_zsd, t);
7677c478bd9Sstevel@tonic-gate 		kmem_free(t, sizeof (*t));
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 	list_destroy(&zone->zone_zsd);
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate 
772*fa9e4066Sahrens /*
773*fa9e4066Sahrens  * Frees memory associated with the zone dataset list.
774*fa9e4066Sahrens  */
775*fa9e4066Sahrens static void
776*fa9e4066Sahrens zone_free_datasets(zone_t *zone)
777*fa9e4066Sahrens {
778*fa9e4066Sahrens 	zone_dataset_t *t, *next;
779*fa9e4066Sahrens 
780*fa9e4066Sahrens 	for (t = list_head(&zone->zone_datasets); t != NULL; t = next) {
781*fa9e4066Sahrens 		next = list_next(&zone->zone_datasets, t);
782*fa9e4066Sahrens 		list_remove(&zone->zone_datasets, t);
783*fa9e4066Sahrens 		kmem_free(t->zd_dataset, strlen(t->zd_dataset) + 1);
784*fa9e4066Sahrens 		kmem_free(t, sizeof (*t));
785*fa9e4066Sahrens 	}
786*fa9e4066Sahrens 	list_destroy(&zone->zone_datasets);
787*fa9e4066Sahrens }
788*fa9e4066Sahrens 
7897c478bd9Sstevel@tonic-gate /*
7907c478bd9Sstevel@tonic-gate  * zone.cpu-shares resource control support.
7917c478bd9Sstevel@tonic-gate  */
7927c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7937c478bd9Sstevel@tonic-gate static rctl_qty_t
7947c478bd9Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
7977c478bd9Sstevel@tonic-gate 	return (p->p_zone->zone_shares);
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8017c478bd9Sstevel@tonic-gate static int
8027c478bd9Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
8037c478bd9Sstevel@tonic-gate     rctl_qty_t nv)
8047c478bd9Sstevel@tonic-gate {
8057c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8067c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
8077c478bd9Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
8087c478bd9Sstevel@tonic-gate 		return (0);
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	e->rcep_p.zone->zone_shares = nv;
8117c478bd9Sstevel@tonic-gate 	return (0);
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = {
8157c478bd9Sstevel@tonic-gate 	rcop_no_action,
8167c478bd9Sstevel@tonic-gate 	zone_cpu_shares_usage,
8177c478bd9Sstevel@tonic-gate 	zone_cpu_shares_set,
8187c478bd9Sstevel@tonic-gate 	rcop_no_test
8197c478bd9Sstevel@tonic-gate };
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8227c478bd9Sstevel@tonic-gate static rctl_qty_t
8237c478bd9Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p)
8247c478bd9Sstevel@tonic-gate {
8257c478bd9Sstevel@tonic-gate 	rctl_qty_t nlwps;
8267c478bd9Sstevel@tonic-gate 	zone_t *zone = p->p_zone;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
8317c478bd9Sstevel@tonic-gate 	nlwps = zone->zone_nlwps;
8327c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	return (nlwps);
8357c478bd9Sstevel@tonic-gate }
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8387c478bd9Sstevel@tonic-gate static int
8397c478bd9Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
8407c478bd9Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate 	rctl_qty_t nlwps;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8457c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
8467c478bd9Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
8477c478bd9Sstevel@tonic-gate 		return (0);
8487c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock)));
8497c478bd9Sstevel@tonic-gate 	nlwps = e->rcep_p.zone->zone_nlwps;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	if (nlwps + incr > rcntl->rcv_value)
8527c478bd9Sstevel@tonic-gate 		return (1);
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	return (0);
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8587c478bd9Sstevel@tonic-gate static int
8597c478bd9Sstevel@tonic-gate zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) {
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8627c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
8637c478bd9Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
8647c478bd9Sstevel@tonic-gate 		return (0);
8657c478bd9Sstevel@tonic-gate 	e->rcep_p.zone->zone_nlwps_ctl = nv;
8667c478bd9Sstevel@tonic-gate 	return (0);
8677c478bd9Sstevel@tonic-gate }
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = {
8707c478bd9Sstevel@tonic-gate 	rcop_no_action,
8717c478bd9Sstevel@tonic-gate 	zone_lwps_usage,
8727c478bd9Sstevel@tonic-gate 	zone_lwps_set,
8737c478bd9Sstevel@tonic-gate 	zone_lwps_test,
8747c478bd9Sstevel@tonic-gate };
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate /*
8777c478bd9Sstevel@tonic-gate  * Helper function to brand the zone with a unique ID.
8787c478bd9Sstevel@tonic-gate  */
8797c478bd9Sstevel@tonic-gate static void
8807c478bd9Sstevel@tonic-gate zone_uniqid(zone_t *zone)
8817c478bd9Sstevel@tonic-gate {
8827c478bd9Sstevel@tonic-gate 	static uint64_t uniqid = 0;
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
8857c478bd9Sstevel@tonic-gate 	zone->zone_uniqid = uniqid++;
8867c478bd9Sstevel@tonic-gate }
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate /*
8897c478bd9Sstevel@tonic-gate  * Returns a held pointer to the "kcred" for the specified zone.
8907c478bd9Sstevel@tonic-gate  */
8917c478bd9Sstevel@tonic-gate struct cred *
8927c478bd9Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid)
8937c478bd9Sstevel@tonic-gate {
8947c478bd9Sstevel@tonic-gate 	zone_t *zone;
8957c478bd9Sstevel@tonic-gate 	cred_t *cr;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_by_id(zoneid)) == NULL)
8987c478bd9Sstevel@tonic-gate 		return (NULL);
8997c478bd9Sstevel@tonic-gate 	cr = zone->zone_kcred;
9007c478bd9Sstevel@tonic-gate 	crhold(cr);
9017c478bd9Sstevel@tonic-gate 	zone_rele(zone);
9027c478bd9Sstevel@tonic-gate 	return (cr);
9037c478bd9Sstevel@tonic-gate }
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate /*
9067c478bd9Sstevel@tonic-gate  * Called very early on in boot to initialize the ZSD list so that
9077c478bd9Sstevel@tonic-gate  * zone_key_create() can be called before zone_init().  It also initializes
9087c478bd9Sstevel@tonic-gate  * portions of zone0 which may be used before zone_init() is called.  The
9097c478bd9Sstevel@tonic-gate  * variable "global_zone" will be set when zone0 is fully initialized by
9107c478bd9Sstevel@tonic-gate  * zone_init().
9117c478bd9Sstevel@tonic-gate  */
9127c478bd9Sstevel@tonic-gate void
9137c478bd9Sstevel@tonic-gate zone_zsd_init(void)
9147c478bd9Sstevel@tonic-gate {
9157c478bd9Sstevel@tonic-gate 	mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL);
9167c478bd9Sstevel@tonic-gate 	mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL);
9177c478bd9Sstevel@tonic-gate 	list_create(&zsd_registered_keys, sizeof (struct zsd_entry),
9187c478bd9Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
9197c478bd9Sstevel@tonic-gate 	list_create(&zone_active, sizeof (zone_t),
9207c478bd9Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
9217c478bd9Sstevel@tonic-gate 	list_create(&zone_deathrow, sizeof (zone_t),
9227c478bd9Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL);
9257c478bd9Sstevel@tonic-gate 	mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
9267c478bd9Sstevel@tonic-gate 	zone0.zone_shares = 1;
9277c478bd9Sstevel@tonic-gate 	zone0.zone_nlwps_ctl = INT_MAX;
9287c478bd9Sstevel@tonic-gate 	zone0.zone_name = GLOBAL_ZONENAME;
9297c478bd9Sstevel@tonic-gate 	zone0.zone_nodename = utsname.nodename;
9307c478bd9Sstevel@tonic-gate 	zone0.zone_domain = srpc_domain;
9317c478bd9Sstevel@tonic-gate 	zone0.zone_ref = 1;
9327c478bd9Sstevel@tonic-gate 	zone0.zone_id = GLOBAL_ZONEID;
9337c478bd9Sstevel@tonic-gate 	zone0.zone_status = ZONE_IS_RUNNING;
9347c478bd9Sstevel@tonic-gate 	zone0.zone_rootpath = "/";
9357c478bd9Sstevel@tonic-gate 	zone0.zone_rootpathlen = 2;
9367c478bd9Sstevel@tonic-gate 	zone0.zone_psetid = ZONE_PS_INVAL;
9377c478bd9Sstevel@tonic-gate 	zone0.zone_ncpus = 0;
9387c478bd9Sstevel@tonic-gate 	zone0.zone_ncpus_online = 0;
9397c478bd9Sstevel@tonic-gate 	zone0.zone_proc_initpid = 1;
9407c478bd9Sstevel@tonic-gate 	list_create(&zone0.zone_zsd, sizeof (struct zsd_entry),
9417c478bd9Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
9427c478bd9Sstevel@tonic-gate 	list_insert_head(&zone_active, &zone0);
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	/*
9457c478bd9Sstevel@tonic-gate 	 * The root filesystem is not mounted yet, so zone_rootvp cannot be set
9467c478bd9Sstevel@tonic-gate 	 * to anything meaningful.  It is assigned to be 'rootdir' in
9477c478bd9Sstevel@tonic-gate 	 * vfs_mountroot().
9487c478bd9Sstevel@tonic-gate 	 */
9497c478bd9Sstevel@tonic-gate 	zone0.zone_rootvp = NULL;
9507c478bd9Sstevel@tonic-gate 	zone0.zone_vfslist = NULL;
9517c478bd9Sstevel@tonic-gate 	zone0.zone_bootargs = NULL;
9527c478bd9Sstevel@tonic-gate 	zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
9537c478bd9Sstevel@tonic-gate 	/*
9547c478bd9Sstevel@tonic-gate 	 * The global zone has all privileges
9557c478bd9Sstevel@tonic-gate 	 */
9567c478bd9Sstevel@tonic-gate 	priv_fillset(zone0.zone_privset);
9577c478bd9Sstevel@tonic-gate 	/*
9587c478bd9Sstevel@tonic-gate 	 * Add p0 to the global zone
9597c478bd9Sstevel@tonic-gate 	 */
9607c478bd9Sstevel@tonic-gate 	zone0.zone_zsched = &p0;
9617c478bd9Sstevel@tonic-gate 	p0.p_zone = &zone0;
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate /*
9657c478bd9Sstevel@tonic-gate  * Called by main() to initialize the zones framework.
9667c478bd9Sstevel@tonic-gate  */
9677c478bd9Sstevel@tonic-gate void
9687c478bd9Sstevel@tonic-gate zone_init(void)
9697c478bd9Sstevel@tonic-gate {
9707c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
9717c478bd9Sstevel@tonic-gate 	rctl_val_t *dval;
9727c478bd9Sstevel@tonic-gate 	rctl_set_t *set;
9737c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
9747c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	ASSERT(curproc == &p0);
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	/*
9797c478bd9Sstevel@tonic-gate 	 * Create ID space for zone IDs.  ID 0 is reserved for the
9807c478bd9Sstevel@tonic-gate 	 * global zone.
9817c478bd9Sstevel@tonic-gate 	 */
9827c478bd9Sstevel@tonic-gate 	zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID);
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	/*
9857c478bd9Sstevel@tonic-gate 	 * Initialize generic zone resource controls, if any.
9867c478bd9Sstevel@tonic-gate 	 */
9877c478bd9Sstevel@tonic-gate 	rc_zone_cpu_shares = rctl_register("zone.cpu-shares",
9887c478bd9Sstevel@tonic-gate 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER |
9897c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_NOBASIC |
9907c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, FSS_MAXSHARES, FSS_MAXSHARES,
9917c478bd9Sstevel@tonic-gate 	    &zone_cpu_shares_ops);
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE,
9947c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
9957c478bd9Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &zone_lwps_ops);
9967c478bd9Sstevel@tonic-gate 	/*
9977c478bd9Sstevel@tonic-gate 	 * Create a rctl_val with PRIVILEGED, NOACTION, value = 1.  Then attach
9987c478bd9Sstevel@tonic-gate 	 * this at the head of the rctl_dict_entry for ``zone.cpu-shares''.
9997c478bd9Sstevel@tonic-gate 	 */
10007c478bd9Sstevel@tonic-gate 	dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
10017c478bd9Sstevel@tonic-gate 	bzero(dval, sizeof (rctl_val_t));
10027c478bd9Sstevel@tonic-gate 	dval->rcv_value = 1;
10037c478bd9Sstevel@tonic-gate 	dval->rcv_privilege = RCPRIV_PRIVILEGED;
10047c478bd9Sstevel@tonic-gate 	dval->rcv_flagaction = RCTL_LOCAL_NOACTION;
10057c478bd9Sstevel@tonic-gate 	dval->rcv_action_recip_pid = -1;
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	rde = rctl_dict_lookup("zone.cpu-shares");
10087c478bd9Sstevel@tonic-gate 	(void) rctl_val_list_insert(&rde->rcd_default_value, dval);
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	/*
10117c478bd9Sstevel@tonic-gate 	 * Initialize the ``global zone''.
10127c478bd9Sstevel@tonic-gate 	 */
10137c478bd9Sstevel@tonic-gate 	set = rctl_set_create();
10147c478bd9Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
10157c478bd9Sstevel@tonic-gate 	mutex_enter(&p0.p_lock);
10167c478bd9Sstevel@tonic-gate 	e.rcep_p.zone = &zone0;
10177c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
10187c478bd9Sstevel@tonic-gate 	zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set,
10197c478bd9Sstevel@tonic-gate 	    gp);
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	zone0.zone_nlwps = p0.p_lwpcnt;
10227c478bd9Sstevel@tonic-gate 	zone0.zone_ntasks = 1;
10237c478bd9Sstevel@tonic-gate 	mutex_exit(&p0.p_lock);
10247c478bd9Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
10257c478bd9Sstevel@tonic-gate 	/*
10267c478bd9Sstevel@tonic-gate 	 * pool_default hasn't been initialized yet, so we let pool_init() take
10277c478bd9Sstevel@tonic-gate 	 * care of making the global zone is in the default pool.
10287c478bd9Sstevel@tonic-gate 	 */
10297c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
10307c478bd9Sstevel@tonic-gate 	zone_uniqid(&zone0);
10317c478bd9Sstevel@tonic-gate 	ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID);
10327c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
10337c478bd9Sstevel@tonic-gate 	zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size,
10347c478bd9Sstevel@tonic-gate 	    mod_hash_null_valdtor);
10357c478bd9Sstevel@tonic-gate 	zonehashbyname = mod_hash_create_strhash("zone_by_name",
10367c478bd9Sstevel@tonic-gate 	    zone_hash_size, mod_hash_null_valdtor);
10377c478bd9Sstevel@tonic-gate 	zonecount = 1;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID,
10407c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
10417c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name,
10427c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
10437c478bd9Sstevel@tonic-gate 	/*
10447c478bd9Sstevel@tonic-gate 	 * We avoid setting zone_kcred until now, since kcred is initialized
10457c478bd9Sstevel@tonic-gate 	 * sometime after zone_zsd_init() and before zone_init().
10467c478bd9Sstevel@tonic-gate 	 */
10477c478bd9Sstevel@tonic-gate 	zone0.zone_kcred = kcred;
10487c478bd9Sstevel@tonic-gate 	/*
10497c478bd9Sstevel@tonic-gate 	 * The global zone is fully initialized (except for zone_rootvp which
10507c478bd9Sstevel@tonic-gate 	 * will be set when the root filesystem is mounted).
10517c478bd9Sstevel@tonic-gate 	 */
10527c478bd9Sstevel@tonic-gate 	global_zone = &zone0;
10537c478bd9Sstevel@tonic-gate }
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate static void
10567c478bd9Sstevel@tonic-gate zone_free(zone_t *zone)
10577c478bd9Sstevel@tonic-gate {
10587c478bd9Sstevel@tonic-gate 	ASSERT(zone != global_zone);
10597c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks == 0);
10607c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_nlwps == 0);
10617c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 0);
10627c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_kcred == NULL);
10637c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_DEAD ||
10647c478bd9Sstevel@tonic-gate 	    zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	/* remove from deathrow list */
10677c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_DEAD) {
10687c478bd9Sstevel@tonic-gate 		ASSERT(zone->zone_ref == 0);
10697c478bd9Sstevel@tonic-gate 		mutex_enter(&zone_deathrow_lock);
10707c478bd9Sstevel@tonic-gate 		list_remove(&zone_deathrow, zone);
10717c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_deathrow_lock);
10727c478bd9Sstevel@tonic-gate 	}
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	zone_free_zsd(zone);
1075*fa9e4066Sahrens 	zone_free_datasets(zone);
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	if (zone->zone_rootvp != NULL)
10787c478bd9Sstevel@tonic-gate 		VN_RELE(zone->zone_rootvp);
10797c478bd9Sstevel@tonic-gate 	if (zone->zone_rootpath)
10807c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_rootpath, zone->zone_rootpathlen);
10817c478bd9Sstevel@tonic-gate 	if (zone->zone_name != NULL)
10827c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_name, ZONENAME_MAX);
10837c478bd9Sstevel@tonic-gate 	if (zone->zone_nodename != NULL)
10847c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_nodename, _SYS_NMLN);
10857c478bd9Sstevel@tonic-gate 	if (zone->zone_domain != NULL)
10867c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_domain, _SYS_NMLN);
10877c478bd9Sstevel@tonic-gate 	if (zone->zone_privset != NULL)
10887c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_privset, sizeof (priv_set_t));
10897c478bd9Sstevel@tonic-gate 	if (zone->zone_rctls != NULL)
10907c478bd9Sstevel@tonic-gate 		rctl_set_free(zone->zone_rctls);
10917c478bd9Sstevel@tonic-gate 	if (zone->zone_bootargs != NULL)
10927c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_bootargs, ZONEBOOTARGS_MAX);
10937c478bd9Sstevel@tonic-gate 	id_free(zoneid_space, zone->zone_id);
10947c478bd9Sstevel@tonic-gate 	mutex_destroy(&zone->zone_lock);
10957c478bd9Sstevel@tonic-gate 	cv_destroy(&zone->zone_cv);
10967c478bd9Sstevel@tonic-gate 	kmem_free(zone, sizeof (zone_t));
10977c478bd9Sstevel@tonic-gate }
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate /*
11007c478bd9Sstevel@tonic-gate  * See block comment at the top of this file for information about zone
11017c478bd9Sstevel@tonic-gate  * status values.
11027c478bd9Sstevel@tonic-gate  */
11037c478bd9Sstevel@tonic-gate /*
11047c478bd9Sstevel@tonic-gate  * Convenience function for setting zone status.
11057c478bd9Sstevel@tonic-gate  */
11067c478bd9Sstevel@tonic-gate static void
11077c478bd9Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status)
11087c478bd9Sstevel@tonic-gate {
11097c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zone_status_lock));
11107c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE &&
11117c478bd9Sstevel@tonic-gate 	    status >= zone_status_get(zone));
11127c478bd9Sstevel@tonic-gate 	zone->zone_status = status;
11137c478bd9Sstevel@tonic-gate 	cv_broadcast(&zone->zone_cv);
11147c478bd9Sstevel@tonic-gate }
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate /*
11177c478bd9Sstevel@tonic-gate  * Public function to retrieve the zone status.  The zone status may
11187c478bd9Sstevel@tonic-gate  * change after it is retrieved.
11197c478bd9Sstevel@tonic-gate  */
11207c478bd9Sstevel@tonic-gate zone_status_t
11217c478bd9Sstevel@tonic-gate zone_status_get(zone_t *zone)
11227c478bd9Sstevel@tonic-gate {
11237c478bd9Sstevel@tonic-gate 	return (zone->zone_status);
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate static int
11277c478bd9Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs)
11287c478bd9Sstevel@tonic-gate {
11297c478bd9Sstevel@tonic-gate 	char *bootargs = kmem_zalloc(ZONEBOOTARGS_MAX, KM_SLEEP);
11307c478bd9Sstevel@tonic-gate 	size_t len;
11317c478bd9Sstevel@tonic-gate 	int err;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	err = copyinstr(zone_bootargs, bootargs, ZONEBOOTARGS_MAX - 1, &len);
11347c478bd9Sstevel@tonic-gate 	if (err != 0) {
11357c478bd9Sstevel@tonic-gate 		kmem_free(bootargs, ZONEBOOTARGS_MAX);
11367c478bd9Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
11377c478bd9Sstevel@tonic-gate 	}
11387c478bd9Sstevel@tonic-gate 	bootargs[len] = '\0';
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_bootargs == NULL);
11417c478bd9Sstevel@tonic-gate 	zone->zone_bootargs = bootargs;
11427c478bd9Sstevel@tonic-gate 	return (0);
11437c478bd9Sstevel@tonic-gate }
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate /*
11467c478bd9Sstevel@tonic-gate  * Block indefinitely waiting for (zone_status >= status)
11477c478bd9Sstevel@tonic-gate  */
11487c478bd9Sstevel@tonic-gate void
11497c478bd9Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status)
11507c478bd9Sstevel@tonic-gate {
11517c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
11547c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
11557c478bd9Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
11567c478bd9Sstevel@tonic-gate 	}
11577c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
11587c478bd9Sstevel@tonic-gate }
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate /*
11617c478bd9Sstevel@tonic-gate  * Private CPR-safe version of zone_status_wait().
11627c478bd9Sstevel@tonic-gate  */
11637c478bd9Sstevel@tonic-gate static void
11647c478bd9Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str)
11657c478bd9Sstevel@tonic-gate {
11667c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr,
11717c478bd9Sstevel@tonic-gate 	    str);
11727c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
11737c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
11747c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
11757c478bd9Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
11767c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock);
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 	/*
11797c478bd9Sstevel@tonic-gate 	 * zone_status_lock is implicitly released by the following.
11807c478bd9Sstevel@tonic-gate 	 */
11817c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
11827c478bd9Sstevel@tonic-gate }
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate /*
11857c478bd9Sstevel@tonic-gate  * Block until zone enters requested state or signal is received.  Return (0)
11867c478bd9Sstevel@tonic-gate  * if signaled, non-zero otherwise.
11877c478bd9Sstevel@tonic-gate  */
11887c478bd9Sstevel@tonic-gate int
11897c478bd9Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status)
11907c478bd9Sstevel@tonic-gate {
11917c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
11947c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
11957c478bd9Sstevel@tonic-gate 		if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) {
11967c478bd9Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
11977c478bd9Sstevel@tonic-gate 			return (0);
11987c478bd9Sstevel@tonic-gate 		}
11997c478bd9Sstevel@tonic-gate 	}
12007c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
12017c478bd9Sstevel@tonic-gate 	return (1);
12027c478bd9Sstevel@tonic-gate }
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate /*
12057c478bd9Sstevel@tonic-gate  * Block until the zone enters the requested state or the timeout expires,
12067c478bd9Sstevel@tonic-gate  * whichever happens first.  Return (-1) if operation timed out, time remaining
12077c478bd9Sstevel@tonic-gate  * otherwise.
12087c478bd9Sstevel@tonic-gate  */
12097c478bd9Sstevel@tonic-gate clock_t
12107c478bd9Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status)
12117c478bd9Sstevel@tonic-gate {
12127c478bd9Sstevel@tonic-gate 	clock_t timeleft = 0;
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
12177c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status && timeleft != -1) {
12187c478bd9Sstevel@tonic-gate 		timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim);
12197c478bd9Sstevel@tonic-gate 	}
12207c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
12217c478bd9Sstevel@tonic-gate 	return (timeleft);
12227c478bd9Sstevel@tonic-gate }
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate /*
12257c478bd9Sstevel@tonic-gate  * Block until the zone enters the requested state, the current process is
12267c478bd9Sstevel@tonic-gate  * signaled,  or the timeout expires, whichever happens first.  Return (-1) if
12277c478bd9Sstevel@tonic-gate  * operation timed out, 0 if signaled, time remaining otherwise.
12287c478bd9Sstevel@tonic-gate  */
12297c478bd9Sstevel@tonic-gate clock_t
12307c478bd9Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status)
12317c478bd9Sstevel@tonic-gate {
12327c478bd9Sstevel@tonic-gate 	clock_t timeleft = tim - lbolt;
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
12377c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
12387c478bd9Sstevel@tonic-gate 		timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock,
12397c478bd9Sstevel@tonic-gate 		    tim);
12407c478bd9Sstevel@tonic-gate 		if (timeleft <= 0)
12417c478bd9Sstevel@tonic-gate 			break;
12427c478bd9Sstevel@tonic-gate 	}
12437c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
12447c478bd9Sstevel@tonic-gate 	return (timeleft);
12457c478bd9Sstevel@tonic-gate }
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate /*
12487c478bd9Sstevel@tonic-gate  * Zones have two reference counts: one for references from credential
12497c478bd9Sstevel@tonic-gate  * structures (zone_cred_ref), and one (zone_ref) for everything else.
12507c478bd9Sstevel@tonic-gate  * This is so we can allow a zone to be rebooted while there are still
12517c478bd9Sstevel@tonic-gate  * outstanding cred references, since certain drivers cache dblks (which
12527c478bd9Sstevel@tonic-gate  * implicitly results in cached creds).  We wait for zone_ref to drop to
12537c478bd9Sstevel@tonic-gate  * 0 (actually 1), but not zone_cred_ref.  The zone structure itself is
12547c478bd9Sstevel@tonic-gate  * later freed when the zone_cred_ref drops to 0, though nothing other
12557c478bd9Sstevel@tonic-gate  * than the zone id and privilege set should be accessed once the zone
12567c478bd9Sstevel@tonic-gate  * is "dead".
12577c478bd9Sstevel@tonic-gate  *
12587c478bd9Sstevel@tonic-gate  * A debugging flag, zone_wait_for_cred, can be set to a non-zero value
12597c478bd9Sstevel@tonic-gate  * to force halt/reboot to block waiting for the zone_cred_ref to drop
12607c478bd9Sstevel@tonic-gate  * to 0.  This can be useful to flush out other sources of cached creds
12617c478bd9Sstevel@tonic-gate  * that may be less innocuous than the driver case.
12627c478bd9Sstevel@tonic-gate  */
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate int zone_wait_for_cred = 0;
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate static void
12677c478bd9Sstevel@tonic-gate zone_hold_locked(zone_t *z)
12687c478bd9Sstevel@tonic-gate {
12697c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&z->zone_lock));
12707c478bd9Sstevel@tonic-gate 	z->zone_ref++;
12717c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
12727c478bd9Sstevel@tonic-gate }
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate void
12757c478bd9Sstevel@tonic-gate zone_hold(zone_t *z)
12767c478bd9Sstevel@tonic-gate {
12777c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
12787c478bd9Sstevel@tonic-gate 	zone_hold_locked(z);
12797c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
12807c478bd9Sstevel@tonic-gate }
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate /*
12837c478bd9Sstevel@tonic-gate  * If the non-cred ref count drops to 1 and either the cred ref count
12847c478bd9Sstevel@tonic-gate  * is 0 or we aren't waiting for cred references, the zone is ready to
12857c478bd9Sstevel@tonic-gate  * be destroyed.
12867c478bd9Sstevel@tonic-gate  */
12877c478bd9Sstevel@tonic-gate #define	ZONE_IS_UNREF(zone)	((zone)->zone_ref == 1 && \
12887c478bd9Sstevel@tonic-gate 	    (!zone_wait_for_cred || (zone)->zone_cred_ref == 0))
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate void
12917c478bd9Sstevel@tonic-gate zone_rele(zone_t *z)
12927c478bd9Sstevel@tonic-gate {
12937c478bd9Sstevel@tonic-gate 	boolean_t wakeup;
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
12967c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
12977c478bd9Sstevel@tonic-gate 	z->zone_ref--;
12987c478bd9Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
12997c478bd9Sstevel@tonic-gate 		/* no more refs, free the structure */
13007c478bd9Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
13017c478bd9Sstevel@tonic-gate 		zone_free(z);
13027c478bd9Sstevel@tonic-gate 		return;
13037c478bd9Sstevel@tonic-gate 	}
13047c478bd9Sstevel@tonic-gate 	/* signal zone_destroy so the zone can finish halting */
13057c478bd9Sstevel@tonic-gate 	wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD);
13067c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	if (wakeup) {
13097c478bd9Sstevel@tonic-gate 		/*
13107c478bd9Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
13117c478bd9Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
13127c478bd9Sstevel@tonic-gate 		 */
13137c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
13147c478bd9Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
13157c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
13167c478bd9Sstevel@tonic-gate 	}
13177c478bd9Sstevel@tonic-gate }
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate void
13207c478bd9Sstevel@tonic-gate zone_cred_hold(zone_t *z)
13217c478bd9Sstevel@tonic-gate {
13227c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
13237c478bd9Sstevel@tonic-gate 	z->zone_cred_ref++;
13247c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
13257c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
13267c478bd9Sstevel@tonic-gate }
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate void
13297c478bd9Sstevel@tonic-gate zone_cred_rele(zone_t *z)
13307c478bd9Sstevel@tonic-gate {
13317c478bd9Sstevel@tonic-gate 	boolean_t wakeup;
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
13347c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
13357c478bd9Sstevel@tonic-gate 	z->zone_cred_ref--;
13367c478bd9Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
13377c478bd9Sstevel@tonic-gate 		/* no more refs, free the structure */
13387c478bd9Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
13397c478bd9Sstevel@tonic-gate 		zone_free(z);
13407c478bd9Sstevel@tonic-gate 		return;
13417c478bd9Sstevel@tonic-gate 	}
13427c478bd9Sstevel@tonic-gate 	/*
13437c478bd9Sstevel@tonic-gate 	 * If zone_destroy is waiting for the cred references to drain
13447c478bd9Sstevel@tonic-gate 	 * out, and they have, signal it.
13457c478bd9Sstevel@tonic-gate 	 */
13467c478bd9Sstevel@tonic-gate 	wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) &&
13477c478bd9Sstevel@tonic-gate 	    zone_status_get(z) >= ZONE_IS_DEAD);
13487c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	if (wakeup) {
13517c478bd9Sstevel@tonic-gate 		/*
13527c478bd9Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
13537c478bd9Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
13547c478bd9Sstevel@tonic-gate 		 */
13557c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
13567c478bd9Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
13577c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
13587c478bd9Sstevel@tonic-gate 	}
13597c478bd9Sstevel@tonic-gate }
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate void
13627c478bd9Sstevel@tonic-gate zone_task_hold(zone_t *z)
13637c478bd9Sstevel@tonic-gate {
13647c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
13657c478bd9Sstevel@tonic-gate 	z->zone_ntasks++;
13667c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_ntasks != 0);
13677c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
13687c478bd9Sstevel@tonic-gate }
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate void
13717c478bd9Sstevel@tonic-gate zone_task_rele(zone_t *zone)
13727c478bd9Sstevel@tonic-gate {
13737c478bd9Sstevel@tonic-gate 	uint_t refcnt;
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
13767c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks != 0);
13777c478bd9Sstevel@tonic-gate 	refcnt = --zone->zone_ntasks;
13787c478bd9Sstevel@tonic-gate 	if (refcnt > 1)	{	/* Common case */
13797c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
13807c478bd9Sstevel@tonic-gate 		return;
13817c478bd9Sstevel@tonic-gate 	}
13827c478bd9Sstevel@tonic-gate 	zone_hold_locked(zone);	/* so we can use the zone_t later */
13837c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
13847c478bd9Sstevel@tonic-gate 	if (refcnt == 1) {
13857c478bd9Sstevel@tonic-gate 		/*
13867c478bd9Sstevel@tonic-gate 		 * See if the zone is shutting down.
13877c478bd9Sstevel@tonic-gate 		 */
13887c478bd9Sstevel@tonic-gate 		mutex_enter(&zone_status_lock);
13897c478bd9Sstevel@tonic-gate 		if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) {
13907c478bd9Sstevel@tonic-gate 			goto out;
13917c478bd9Sstevel@tonic-gate 		}
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 		/*
13947c478bd9Sstevel@tonic-gate 		 * Make sure the ntasks didn't change since we
13957c478bd9Sstevel@tonic-gate 		 * dropped zone_lock.
13967c478bd9Sstevel@tonic-gate 		 */
13977c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
13987c478bd9Sstevel@tonic-gate 		if (refcnt != zone->zone_ntasks) {
13997c478bd9Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
14007c478bd9Sstevel@tonic-gate 			goto out;
14017c478bd9Sstevel@tonic-gate 		}
14027c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 		/*
14057c478bd9Sstevel@tonic-gate 		 * No more user processes in the zone.  The zone is empty.
14067c478bd9Sstevel@tonic-gate 		 */
14077c478bd9Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_EMPTY);
14087c478bd9Sstevel@tonic-gate 		goto out;
14097c478bd9Sstevel@tonic-gate 	}
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	ASSERT(refcnt == 0);
14127c478bd9Sstevel@tonic-gate 	/*
14137c478bd9Sstevel@tonic-gate 	 * zsched has exited; the zone is dead.
14147c478bd9Sstevel@tonic-gate 	 */
14157c478bd9Sstevel@tonic-gate 	zone->zone_zsched = NULL;		/* paranoia */
14167c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
14177c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_DEAD);
14187c478bd9Sstevel@tonic-gate out:
14197c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
14207c478bd9Sstevel@tonic-gate 	zone_rele(zone);
14217c478bd9Sstevel@tonic-gate }
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate zoneid_t
14247c478bd9Sstevel@tonic-gate getzoneid(void)
14257c478bd9Sstevel@tonic-gate {
14267c478bd9Sstevel@tonic-gate 	return (curproc->p_zone->zone_id);
14277c478bd9Sstevel@tonic-gate }
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate /*
14307c478bd9Sstevel@tonic-gate  * Internal versions of zone_find_by_*().  These don't zone_hold() or
14317c478bd9Sstevel@tonic-gate  * check the validity of a zone's state.
14327c478bd9Sstevel@tonic-gate  */
14337c478bd9Sstevel@tonic-gate static zone_t *
14347c478bd9Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid)
14357c478bd9Sstevel@tonic-gate {
14367c478bd9Sstevel@tonic-gate 	mod_hash_val_t hv;
14377c478bd9Sstevel@tonic-gate 	zone_t *zone = NULL;
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyid,
14427c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0)
14437c478bd9Sstevel@tonic-gate 		zone = (zone_t *)hv;
14447c478bd9Sstevel@tonic-gate 	return (zone);
14457c478bd9Sstevel@tonic-gate }
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate static zone_t *
14487c478bd9Sstevel@tonic-gate zone_find_all_by_name(char *name)
14497c478bd9Sstevel@tonic-gate {
14507c478bd9Sstevel@tonic-gate 	mod_hash_val_t hv;
14517c478bd9Sstevel@tonic-gate 	zone_t *zone = NULL;
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0)
14567c478bd9Sstevel@tonic-gate 		zone = (zone_t *)hv;
14577c478bd9Sstevel@tonic-gate 	return (zone);
14587c478bd9Sstevel@tonic-gate }
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate /*
14617c478bd9Sstevel@tonic-gate  * Public interface for looking up a zone by zoneid.  Only returns the zone if
14627c478bd9Sstevel@tonic-gate  * it is fully initialized, and has not yet begun the zone_destroy() sequence.
14637c478bd9Sstevel@tonic-gate  * Caller must call zone_rele() once it is done with the zone.
14647c478bd9Sstevel@tonic-gate  *
14657c478bd9Sstevel@tonic-gate  * The zone may begin the zone_destroy() sequence immediately after this
14667c478bd9Sstevel@tonic-gate  * function returns, but may be safely used until zone_rele() is called.
14677c478bd9Sstevel@tonic-gate  */
14687c478bd9Sstevel@tonic-gate zone_t *
14697c478bd9Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid)
14707c478bd9Sstevel@tonic-gate {
14717c478bd9Sstevel@tonic-gate 	zone_t *zone;
14727c478bd9Sstevel@tonic-gate 	zone_status_t status;
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
14757c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
14767c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
14777c478bd9Sstevel@tonic-gate 		return (NULL);
14787c478bd9Sstevel@tonic-gate 	}
14797c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
14807c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
14817c478bd9Sstevel@tonic-gate 		/*
14827c478bd9Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
14837c478bd9Sstevel@tonic-gate 		 */
14847c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
14857c478bd9Sstevel@tonic-gate 		return (NULL);
14867c478bd9Sstevel@tonic-gate 	}
14877c478bd9Sstevel@tonic-gate 	zone_hold(zone);
14887c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
14897c478bd9Sstevel@tonic-gate 	return (zone);
14907c478bd9Sstevel@tonic-gate }
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate /*
14937c478bd9Sstevel@tonic-gate  * Similar to zone_find_by_id, but using zone name as the key.
14947c478bd9Sstevel@tonic-gate  */
14957c478bd9Sstevel@tonic-gate zone_t *
14967c478bd9Sstevel@tonic-gate zone_find_by_name(char *name)
14977c478bd9Sstevel@tonic-gate {
14987c478bd9Sstevel@tonic-gate 	zone_t *zone;
14997c478bd9Sstevel@tonic-gate 	zone_status_t status;
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
15027c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_name(name)) == NULL) {
15037c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
15047c478bd9Sstevel@tonic-gate 		return (NULL);
15057c478bd9Sstevel@tonic-gate 	}
15067c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
15077c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
15087c478bd9Sstevel@tonic-gate 		/*
15097c478bd9Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
15107c478bd9Sstevel@tonic-gate 		 */
15117c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
15127c478bd9Sstevel@tonic-gate 		return (NULL);
15137c478bd9Sstevel@tonic-gate 	}
15147c478bd9Sstevel@tonic-gate 	zone_hold(zone);
15157c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
15167c478bd9Sstevel@tonic-gate 	return (zone);
15177c478bd9Sstevel@tonic-gate }
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate /*
15207c478bd9Sstevel@tonic-gate  * Similar to zone_find_by_id(), using the path as a key.  For instance,
15217c478bd9Sstevel@tonic-gate  * if there is a zone "foo" rooted at /foo/root, and the path argument
15227c478bd9Sstevel@tonic-gate  * is "/foo/root/proc", it will return the held zone_t corresponding to
15237c478bd9Sstevel@tonic-gate  * zone "foo".
15247c478bd9Sstevel@tonic-gate  *
15257c478bd9Sstevel@tonic-gate  * zone_find_by_path() always returns a non-NULL value, since at the
15267c478bd9Sstevel@tonic-gate  * very least every path will be contained in the global zone.
15277c478bd9Sstevel@tonic-gate  *
15287c478bd9Sstevel@tonic-gate  * As with the other zone_find_by_*() functions, the caller is
15297c478bd9Sstevel@tonic-gate  * responsible for zone_rele()ing the return value of this function.
15307c478bd9Sstevel@tonic-gate  */
15317c478bd9Sstevel@tonic-gate zone_t *
15327c478bd9Sstevel@tonic-gate zone_find_by_path(const char *path)
15337c478bd9Sstevel@tonic-gate {
15347c478bd9Sstevel@tonic-gate 	zone_t *zone;
15357c478bd9Sstevel@tonic-gate 	zone_t *zret = NULL;
15367c478bd9Sstevel@tonic-gate 	zone_status_t status;
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	if (path == NULL) {
15397c478bd9Sstevel@tonic-gate 		/*
15407c478bd9Sstevel@tonic-gate 		 * Call from rootconf().
15417c478bd9Sstevel@tonic-gate 		 */
15427c478bd9Sstevel@tonic-gate 		zone_hold(global_zone);
15437c478bd9Sstevel@tonic-gate 		return (global_zone);
15447c478bd9Sstevel@tonic-gate 	}
15457c478bd9Sstevel@tonic-gate 	ASSERT(*path == '/');
15467c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
15477c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
15487c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
15497c478bd9Sstevel@tonic-gate 		if (ZONE_PATH_VISIBLE(path, zone))
15507c478bd9Sstevel@tonic-gate 			zret = zone;
15517c478bd9Sstevel@tonic-gate 	}
15527c478bd9Sstevel@tonic-gate 	ASSERT(zret != NULL);
15537c478bd9Sstevel@tonic-gate 	status = zone_status_get(zret);
15547c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
15557c478bd9Sstevel@tonic-gate 		/*
15567c478bd9Sstevel@tonic-gate 		 * Zone practically doesn't exist.
15577c478bd9Sstevel@tonic-gate 		 */
15587c478bd9Sstevel@tonic-gate 		zret = global_zone;
15597c478bd9Sstevel@tonic-gate 	}
15607c478bd9Sstevel@tonic-gate 	zone_hold(zret);
15617c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
15627c478bd9Sstevel@tonic-gate 	return (zret);
15637c478bd9Sstevel@tonic-gate }
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate /*
15667c478bd9Sstevel@tonic-gate  * Get the number of cpus visible to this zone.  The system-wide global
15677c478bd9Sstevel@tonic-gate  * 'ncpus' is returned if pools are disabled, the caller is in the
15687c478bd9Sstevel@tonic-gate  * global zone, or a NULL zone argument is passed in.
15697c478bd9Sstevel@tonic-gate  */
15707c478bd9Sstevel@tonic-gate int
15717c478bd9Sstevel@tonic-gate zone_ncpus_get(zone_t *zone)
15727c478bd9Sstevel@tonic-gate {
15737c478bd9Sstevel@tonic-gate 	int myncpus = zone == NULL ? 0 : zone->zone_ncpus;
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	return (myncpus != 0 ? myncpus : ncpus);
15767c478bd9Sstevel@tonic-gate }
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate /*
15797c478bd9Sstevel@tonic-gate  * Get the number of online cpus visible to this zone.  The system-wide
15807c478bd9Sstevel@tonic-gate  * global 'ncpus_online' is returned if pools are disabled, the caller
15817c478bd9Sstevel@tonic-gate  * is in the global zone, or a NULL zone argument is passed in.
15827c478bd9Sstevel@tonic-gate  */
15837c478bd9Sstevel@tonic-gate int
15847c478bd9Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone)
15857c478bd9Sstevel@tonic-gate {
15867c478bd9Sstevel@tonic-gate 	int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online;
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	return (myncpus_online != 0 ? myncpus_online : ncpus_online);
15897c478bd9Sstevel@tonic-gate }
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate /*
15927c478bd9Sstevel@tonic-gate  * Return the pool to which the zone is currently bound.
15937c478bd9Sstevel@tonic-gate  */
15947c478bd9Sstevel@tonic-gate pool_t *
15957c478bd9Sstevel@tonic-gate zone_pool_get(zone_t *zone)
15967c478bd9Sstevel@tonic-gate {
15977c478bd9Sstevel@tonic-gate 	ASSERT(pool_lock_held());
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	return (zone->zone_pool);
16007c478bd9Sstevel@tonic-gate }
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate /*
16037c478bd9Sstevel@tonic-gate  * Set the zone's pool pointer and update the zone's visibility to match
16047c478bd9Sstevel@tonic-gate  * the resources in the new pool.
16057c478bd9Sstevel@tonic-gate  */
16067c478bd9Sstevel@tonic-gate void
16077c478bd9Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool)
16087c478bd9Sstevel@tonic-gate {
16097c478bd9Sstevel@tonic-gate 	ASSERT(pool_lock_held());
16107c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	zone->zone_pool = pool;
16137c478bd9Sstevel@tonic-gate 	zone_pset_set(zone, pool->pool_pset->pset_id);
16147c478bd9Sstevel@tonic-gate }
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate /*
16177c478bd9Sstevel@tonic-gate  * Return the cached value of the id of the processor set to which the
16187c478bd9Sstevel@tonic-gate  * zone is currently bound.  The value will be ZONE_PS_INVAL if the pools
16197c478bd9Sstevel@tonic-gate  * facility is disabled.
16207c478bd9Sstevel@tonic-gate  */
16217c478bd9Sstevel@tonic-gate psetid_t
16227c478bd9Sstevel@tonic-gate zone_pset_get(zone_t *zone)
16237c478bd9Sstevel@tonic-gate {
16247c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	return (zone->zone_psetid);
16277c478bd9Sstevel@tonic-gate }
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate /*
16307c478bd9Sstevel@tonic-gate  * Set the cached value of the id of the processor set to which the zone
16317c478bd9Sstevel@tonic-gate  * is currently bound.  Also update the zone's visibility to match the
16327c478bd9Sstevel@tonic-gate  * resources in the new processor set.
16337c478bd9Sstevel@tonic-gate  */
16347c478bd9Sstevel@tonic-gate void
16357c478bd9Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid)
16367c478bd9Sstevel@tonic-gate {
16377c478bd9Sstevel@tonic-gate 	psetid_t oldpsetid;
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16407c478bd9Sstevel@tonic-gate 	oldpsetid = zone_pset_get(zone);
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	if (oldpsetid == newpsetid)
16437c478bd9Sstevel@tonic-gate 		return;
16447c478bd9Sstevel@tonic-gate 	/*
16457c478bd9Sstevel@tonic-gate 	 * Global zone sees all.
16467c478bd9Sstevel@tonic-gate 	 */
16477c478bd9Sstevel@tonic-gate 	if (zone != global_zone) {
16487c478bd9Sstevel@tonic-gate 		zone->zone_psetid = newpsetid;
16497c478bd9Sstevel@tonic-gate 		if (newpsetid != ZONE_PS_INVAL)
16507c478bd9Sstevel@tonic-gate 			pool_pset_visibility_add(newpsetid, zone);
16517c478bd9Sstevel@tonic-gate 		if (oldpsetid != ZONE_PS_INVAL)
16527c478bd9Sstevel@tonic-gate 			pool_pset_visibility_remove(oldpsetid, zone);
16537c478bd9Sstevel@tonic-gate 	}
16547c478bd9Sstevel@tonic-gate 	/*
16557c478bd9Sstevel@tonic-gate 	 * Disabling pools, so we should start using the global values
16567c478bd9Sstevel@tonic-gate 	 * for ncpus and ncpus_online.
16577c478bd9Sstevel@tonic-gate 	 */
16587c478bd9Sstevel@tonic-gate 	if (newpsetid == ZONE_PS_INVAL) {
16597c478bd9Sstevel@tonic-gate 		zone->zone_ncpus = 0;
16607c478bd9Sstevel@tonic-gate 		zone->zone_ncpus_online = 0;
16617c478bd9Sstevel@tonic-gate 	}
16627c478bd9Sstevel@tonic-gate }
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate /*
16657c478bd9Sstevel@tonic-gate  * Walk the list of active zones and issue the provided callback for
16667c478bd9Sstevel@tonic-gate  * each of them.
16677c478bd9Sstevel@tonic-gate  *
16687c478bd9Sstevel@tonic-gate  * Caller must not be holding any locks that may be acquired under
16697c478bd9Sstevel@tonic-gate  * zonehash_lock.  See comment at the beginning of the file for a list of
16707c478bd9Sstevel@tonic-gate  * common locks and their interactions with zones.
16717c478bd9Sstevel@tonic-gate  */
16727c478bd9Sstevel@tonic-gate int
16737c478bd9Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data)
16747c478bd9Sstevel@tonic-gate {
16757c478bd9Sstevel@tonic-gate 	zone_t *zone;
16767c478bd9Sstevel@tonic-gate 	int ret = 0;
16777c478bd9Sstevel@tonic-gate 	zone_status_t status;
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
16807c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
16817c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
16827c478bd9Sstevel@tonic-gate 		/*
16837c478bd9Sstevel@tonic-gate 		 * Skip zones that shouldn't be externally visible.
16847c478bd9Sstevel@tonic-gate 		 */
16857c478bd9Sstevel@tonic-gate 		status = zone_status_get(zone);
16867c478bd9Sstevel@tonic-gate 		if (status < ZONE_IS_READY || status > ZONE_IS_DOWN)
16877c478bd9Sstevel@tonic-gate 			continue;
16887c478bd9Sstevel@tonic-gate 		/*
16897c478bd9Sstevel@tonic-gate 		 * Bail immediately if any callback invocation returns a
16907c478bd9Sstevel@tonic-gate 		 * non-zero value.
16917c478bd9Sstevel@tonic-gate 		 */
16927c478bd9Sstevel@tonic-gate 		ret = (*cb)(zone, data);
16937c478bd9Sstevel@tonic-gate 		if (ret != 0)
16947c478bd9Sstevel@tonic-gate 			break;
16957c478bd9Sstevel@tonic-gate 	}
16967c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
16977c478bd9Sstevel@tonic-gate 	return (ret);
16987c478bd9Sstevel@tonic-gate }
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate static int
17017c478bd9Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath)
17027c478bd9Sstevel@tonic-gate {
17037c478bd9Sstevel@tonic-gate 	vnode_t *vp;
17047c478bd9Sstevel@tonic-gate 	int trycount;
17057c478bd9Sstevel@tonic-gate 	int error = 0;
17067c478bd9Sstevel@tonic-gate 	char *path;
17077c478bd9Sstevel@tonic-gate 	struct pathname upn, pn;
17087c478bd9Sstevel@tonic-gate 	size_t pathlen;
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0)
17117c478bd9Sstevel@tonic-gate 		return (error);
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	pn_alloc(&pn);
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 	/* prevent infinite loop */
17167c478bd9Sstevel@tonic-gate 	trycount = 10;
17177c478bd9Sstevel@tonic-gate 	for (;;) {
17187c478bd9Sstevel@tonic-gate 		if (--trycount <= 0) {
17197c478bd9Sstevel@tonic-gate 			error = ESTALE;
17207c478bd9Sstevel@tonic-gate 			goto out;
17217c478bd9Sstevel@tonic-gate 		}
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 		if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) {
17247c478bd9Sstevel@tonic-gate 			/*
17257c478bd9Sstevel@tonic-gate 			 * VOP_ACCESS() may cover 'vp' with a new
17267c478bd9Sstevel@tonic-gate 			 * filesystem, if 'vp' is an autoFS vnode.
17277c478bd9Sstevel@tonic-gate 			 * Get the new 'vp' if so.
17287c478bd9Sstevel@tonic-gate 			 */
17297c478bd9Sstevel@tonic-gate 			if ((error = VOP_ACCESS(vp, VEXEC, 0, CRED())) == 0 &&
17307c478bd9Sstevel@tonic-gate 			    (vp->v_vfsmountedhere == NULL ||
17317c478bd9Sstevel@tonic-gate 			    (error = traverse(&vp)) == 0)) {
17327c478bd9Sstevel@tonic-gate 				pathlen = pn.pn_pathlen + 2;
17337c478bd9Sstevel@tonic-gate 				path = kmem_alloc(pathlen, KM_SLEEP);
17347c478bd9Sstevel@tonic-gate 				(void) strncpy(path, pn.pn_path,
17357c478bd9Sstevel@tonic-gate 				    pn.pn_pathlen + 1);
17367c478bd9Sstevel@tonic-gate 				path[pathlen - 2] = '/';
17377c478bd9Sstevel@tonic-gate 				path[pathlen - 1] = '\0';
17387c478bd9Sstevel@tonic-gate 				pn_free(&pn);
17397c478bd9Sstevel@tonic-gate 				pn_free(&upn);
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 				/* Success! */
17427c478bd9Sstevel@tonic-gate 				break;
17437c478bd9Sstevel@tonic-gate 			}
17447c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
17457c478bd9Sstevel@tonic-gate 		}
17467c478bd9Sstevel@tonic-gate 		if (error != ESTALE)
17477c478bd9Sstevel@tonic-gate 			goto out;
17487c478bd9Sstevel@tonic-gate 	}
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 	ASSERT(error == 0);
17517c478bd9Sstevel@tonic-gate 	zone->zone_rootvp = vp;		/* we hold a reference to vp */
17527c478bd9Sstevel@tonic-gate 	zone->zone_rootpath = path;
17537c478bd9Sstevel@tonic-gate 	zone->zone_rootpathlen = pathlen;
17547c478bd9Sstevel@tonic-gate 	return (0);
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate out:
17577c478bd9Sstevel@tonic-gate 	pn_free(&pn);
17587c478bd9Sstevel@tonic-gate 	pn_free(&upn);
17597c478bd9Sstevel@tonic-gate 	return (error);
17607c478bd9Sstevel@tonic-gate }
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate #define	isalnum(c)	(((c) >= '0' && (c) <= '9') || \
17637c478bd9Sstevel@tonic-gate 			((c) >= 'a' && (c) <= 'z') || \
17647c478bd9Sstevel@tonic-gate 			((c) >= 'A' && (c) <= 'Z'))
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate static int
17677c478bd9Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname)
17687c478bd9Sstevel@tonic-gate {
17697c478bd9Sstevel@tonic-gate 	char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
17707c478bd9Sstevel@tonic-gate 	size_t len;
17717c478bd9Sstevel@tonic-gate 	int i, err;
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) {
17747c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
17757c478bd9Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
17767c478bd9Sstevel@tonic-gate 	}
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	/* must be less than ZONENAME_MAX */
17797c478bd9Sstevel@tonic-gate 	if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') {
17807c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
17817c478bd9Sstevel@tonic-gate 		return (EINVAL);
17827c478bd9Sstevel@tonic-gate 	}
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 	/*
17857c478bd9Sstevel@tonic-gate 	 * Name must start with an alphanumeric and must contain only
17867c478bd9Sstevel@tonic-gate 	 * alphanumerics, '-', '_' and '.'.
17877c478bd9Sstevel@tonic-gate 	 */
17887c478bd9Sstevel@tonic-gate 	if (!isalnum(kname[0])) {
17897c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
17907c478bd9Sstevel@tonic-gate 		return (EINVAL);
17917c478bd9Sstevel@tonic-gate 	}
17927c478bd9Sstevel@tonic-gate 	for (i = 1; i < len - 1; i++) {
17937c478bd9Sstevel@tonic-gate 		if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' &&
17947c478bd9Sstevel@tonic-gate 		    kname[i] != '.') {
17957c478bd9Sstevel@tonic-gate 			kmem_free(kname, ZONENAME_MAX);
17967c478bd9Sstevel@tonic-gate 			return (EINVAL);
17977c478bd9Sstevel@tonic-gate 		}
17987c478bd9Sstevel@tonic-gate 	}
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	zone->zone_name = kname;
18017c478bd9Sstevel@tonic-gate 	return (0);
18027c478bd9Sstevel@tonic-gate }
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate /*
18057c478bd9Sstevel@tonic-gate  * Similar to thread_create(), but makes sure the thread is in the appropriate
18067c478bd9Sstevel@tonic-gate  * zone's zsched process (curproc->p_zone->zone_zsched) before returning.
18077c478bd9Sstevel@tonic-gate  */
18087c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18097c478bd9Sstevel@tonic-gate kthread_t *
18107c478bd9Sstevel@tonic-gate zthread_create(
18117c478bd9Sstevel@tonic-gate     caddr_t stk,
18127c478bd9Sstevel@tonic-gate     size_t stksize,
18137c478bd9Sstevel@tonic-gate     void (*proc)(),
18147c478bd9Sstevel@tonic-gate     void *arg,
18157c478bd9Sstevel@tonic-gate     size_t len,
18167c478bd9Sstevel@tonic-gate     pri_t pri)
18177c478bd9Sstevel@tonic-gate {
18187c478bd9Sstevel@tonic-gate 	kthread_t *t;
18197c478bd9Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
18207c478bd9Sstevel@tonic-gate 	proc_t *pp = zone->zone_zsched;
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	zone_hold(zone);	/* Reference to be dropped when thread exits */
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	/*
18257c478bd9Sstevel@tonic-gate 	 * No-one should be trying to create threads if the zone is shutting
18267c478bd9Sstevel@tonic-gate 	 * down and there aren't any kernel threads around.  See comment
18277c478bd9Sstevel@tonic-gate 	 * in zthread_exit().
18287c478bd9Sstevel@tonic-gate 	 */
18297c478bd9Sstevel@tonic-gate 	ASSERT(!(zone->zone_kthreads == NULL &&
18307c478bd9Sstevel@tonic-gate 	    zone_status_get(zone) >= ZONE_IS_EMPTY));
18317c478bd9Sstevel@tonic-gate 	/*
18327c478bd9Sstevel@tonic-gate 	 * Create a thread, but don't let it run until we've finished setting
18337c478bd9Sstevel@tonic-gate 	 * things up.
18347c478bd9Sstevel@tonic-gate 	 */
18357c478bd9Sstevel@tonic-gate 	t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri);
18367c478bd9Sstevel@tonic-gate 	ASSERT(t->t_forw == NULL);
18377c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
18387c478bd9Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL) {
18397c478bd9Sstevel@tonic-gate 		t->t_forw = t->t_back = t;
18407c478bd9Sstevel@tonic-gate 	} else {
18417c478bd9Sstevel@tonic-gate 		kthread_t *tx = zone->zone_kthreads;
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 		t->t_forw = tx;
18447c478bd9Sstevel@tonic-gate 		t->t_back = tx->t_back;
18457c478bd9Sstevel@tonic-gate 		tx->t_back->t_forw = t;
18467c478bd9Sstevel@tonic-gate 		tx->t_back = t;
18477c478bd9Sstevel@tonic-gate 	}
18487c478bd9Sstevel@tonic-gate 	zone->zone_kthreads = t;
18497c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
18527c478bd9Sstevel@tonic-gate 	t->t_proc_flag |= TP_ZTHREAD;
18537c478bd9Sstevel@tonic-gate 	project_rele(t->t_proj);
18547c478bd9Sstevel@tonic-gate 	t->t_proj = project_hold(pp->p_task->tk_proj);
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 	/*
18577c478bd9Sstevel@tonic-gate 	 * Setup complete, let it run.
18587c478bd9Sstevel@tonic-gate 	 */
18597c478bd9Sstevel@tonic-gate 	thread_lock(t);
18607c478bd9Sstevel@tonic-gate 	t->t_schedflag |= TS_ALLSTART;
18617c478bd9Sstevel@tonic-gate 	setrun_locked(t);
18627c478bd9Sstevel@tonic-gate 	thread_unlock(t);
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 	return (t);
18677c478bd9Sstevel@tonic-gate }
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate /*
18707c478bd9Sstevel@tonic-gate  * Similar to thread_exit().  Must be called by threads created via
18717c478bd9Sstevel@tonic-gate  * zthread_exit().
18727c478bd9Sstevel@tonic-gate  */
18737c478bd9Sstevel@tonic-gate void
18747c478bd9Sstevel@tonic-gate zthread_exit(void)
18757c478bd9Sstevel@tonic-gate {
18767c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
18777c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
18787c478bd9Sstevel@tonic-gate 	zone_t *zone = pp->p_zone;
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	/*
18837c478bd9Sstevel@tonic-gate 	 * Reparent to p0
18847c478bd9Sstevel@tonic-gate 	 */
18857c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
18867c478bd9Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_ZTHREAD;
18877c478bd9Sstevel@tonic-gate 	t->t_procp = &p0;
18887c478bd9Sstevel@tonic-gate 	hat_thread_exit(t);
18897c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	if (t->t_back == t) {
18927c478bd9Sstevel@tonic-gate 		ASSERT(t->t_forw == t);
18937c478bd9Sstevel@tonic-gate 		/*
18947c478bd9Sstevel@tonic-gate 		 * If the zone is empty, once the thread count
18957c478bd9Sstevel@tonic-gate 		 * goes to zero no further kernel threads can be
18967c478bd9Sstevel@tonic-gate 		 * created.  This is because if the creator is a process
18977c478bd9Sstevel@tonic-gate 		 * in the zone, then it must have exited before the zone
18987c478bd9Sstevel@tonic-gate 		 * state could be set to ZONE_IS_EMPTY.
18997c478bd9Sstevel@tonic-gate 		 * Otherwise, if the creator is a kernel thread in the
19007c478bd9Sstevel@tonic-gate 		 * zone, the thread count is non-zero.
19017c478bd9Sstevel@tonic-gate 		 *
19027c478bd9Sstevel@tonic-gate 		 * This really means that non-zone kernel threads should
19037c478bd9Sstevel@tonic-gate 		 * not create zone kernel threads.
19047c478bd9Sstevel@tonic-gate 		 */
19057c478bd9Sstevel@tonic-gate 		zone->zone_kthreads = NULL;
19067c478bd9Sstevel@tonic-gate 		if (zone_status_get(zone) == ZONE_IS_EMPTY) {
19077c478bd9Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_DOWN);
19087c478bd9Sstevel@tonic-gate 		}
19097c478bd9Sstevel@tonic-gate 	} else {
19107c478bd9Sstevel@tonic-gate 		t->t_forw->t_back = t->t_back;
19117c478bd9Sstevel@tonic-gate 		t->t_back->t_forw = t->t_forw;
19127c478bd9Sstevel@tonic-gate 		if (zone->zone_kthreads == t)
19137c478bd9Sstevel@tonic-gate 			zone->zone_kthreads = t->t_forw;
19147c478bd9Sstevel@tonic-gate 	}
19157c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
19167c478bd9Sstevel@tonic-gate 	zone_rele(zone);
19177c478bd9Sstevel@tonic-gate 	thread_exit();
19187c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
19197c478bd9Sstevel@tonic-gate }
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate static void
19227c478bd9Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp)
19237c478bd9Sstevel@tonic-gate {
19247c478bd9Sstevel@tonic-gate 	vnode_t *oldvp;
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	/* we're going to hold a reference here to the directory */
19277c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
19307c478bd9Sstevel@tonic-gate 	if (audit_active)	/* update abs cwd/root path see c2audit.c */
19317c478bd9Sstevel@tonic-gate 		audit_chdirec(vp, vpp);
19327c478bd9Sstevel@tonic-gate #endif
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
19357c478bd9Sstevel@tonic-gate 	oldvp = *vpp;
19367c478bd9Sstevel@tonic-gate 	*vpp = vp;
19377c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
19387c478bd9Sstevel@tonic-gate 	if (oldvp != NULL)
19397c478bd9Sstevel@tonic-gate 		VN_RELE(oldvp);
19407c478bd9Sstevel@tonic-gate }
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate /*
19437c478bd9Sstevel@tonic-gate  * Convert an rctl value represented by an nvlist_t into an rctl_val_t.
19447c478bd9Sstevel@tonic-gate  */
19457c478bd9Sstevel@tonic-gate static int
19467c478bd9Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv)
19477c478bd9Sstevel@tonic-gate {
19487c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
19497c478bd9Sstevel@tonic-gate 	boolean_t priv_set = B_FALSE;
19507c478bd9Sstevel@tonic-gate 	boolean_t limit_set = B_FALSE;
19517c478bd9Sstevel@tonic-gate 	boolean_t action_set = B_FALSE;
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
19547c478bd9Sstevel@tonic-gate 		const char *name;
19557c478bd9Sstevel@tonic-gate 		uint64_t ui64;
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
19587c478bd9Sstevel@tonic-gate 		if (nvpair_type(nvp) != DATA_TYPE_UINT64)
19597c478bd9Sstevel@tonic-gate 			return (EINVAL);
19607c478bd9Sstevel@tonic-gate 		(void) nvpair_value_uint64(nvp, &ui64);
19617c478bd9Sstevel@tonic-gate 		if (strcmp(name, "privilege") == 0) {
19627c478bd9Sstevel@tonic-gate 			/*
19637c478bd9Sstevel@tonic-gate 			 * Currently only privileged values are allowed, but
19647c478bd9Sstevel@tonic-gate 			 * this may change in the future.
19657c478bd9Sstevel@tonic-gate 			 */
19667c478bd9Sstevel@tonic-gate 			if (ui64 != RCPRIV_PRIVILEGED)
19677c478bd9Sstevel@tonic-gate 				return (EINVAL);
19687c478bd9Sstevel@tonic-gate 			rv->rcv_privilege = ui64;
19697c478bd9Sstevel@tonic-gate 			priv_set = B_TRUE;
19707c478bd9Sstevel@tonic-gate 		} else if (strcmp(name, "limit") == 0) {
19717c478bd9Sstevel@tonic-gate 			rv->rcv_value = ui64;
19727c478bd9Sstevel@tonic-gate 			limit_set = B_TRUE;
19737c478bd9Sstevel@tonic-gate 		} else if (strcmp(name, "action") == 0) {
19747c478bd9Sstevel@tonic-gate 			if (ui64 != RCTL_LOCAL_NOACTION &&
19757c478bd9Sstevel@tonic-gate 			    ui64 != RCTL_LOCAL_DENY)
19767c478bd9Sstevel@tonic-gate 				return (EINVAL);
19777c478bd9Sstevel@tonic-gate 			rv->rcv_flagaction = ui64;
19787c478bd9Sstevel@tonic-gate 			action_set = B_TRUE;
19797c478bd9Sstevel@tonic-gate 		} else {
19807c478bd9Sstevel@tonic-gate 			return (EINVAL);
19817c478bd9Sstevel@tonic-gate 		}
19827c478bd9Sstevel@tonic-gate 	}
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 	if (!(priv_set && limit_set && action_set))
19857c478bd9Sstevel@tonic-gate 		return (EINVAL);
19867c478bd9Sstevel@tonic-gate 	rv->rcv_action_signal = 0;
19877c478bd9Sstevel@tonic-gate 	rv->rcv_action_recipient = NULL;
19887c478bd9Sstevel@tonic-gate 	rv->rcv_action_recip_pid = -1;
19897c478bd9Sstevel@tonic-gate 	rv->rcv_firing_time = 0;
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 	return (0);
19927c478bd9Sstevel@tonic-gate }
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate void
19957c478bd9Sstevel@tonic-gate zone_icode(void)
19967c478bd9Sstevel@tonic-gate {
19977c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
19987c478bd9Sstevel@tonic-gate 	struct core_globals	*cg;
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 	/*
20017c478bd9Sstevel@tonic-gate 	 * For all purposes (ZONE_ATTR_INITPID and restart_init),
20027c478bd9Sstevel@tonic-gate 	 * storing just the pid of init is sufficient.
20037c478bd9Sstevel@tonic-gate 	 */
20047c478bd9Sstevel@tonic-gate 	p->p_zone->zone_proc_initpid = p->p_pid;
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	/*
20077c478bd9Sstevel@tonic-gate 	 * Allocate user address space and stack segment
20087c478bd9Sstevel@tonic-gate 	 */
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 	p->p_cstime = p->p_stime = p->p_cutime = p->p_utime = 0;
20117c478bd9Sstevel@tonic-gate 	p->p_usrstack = (caddr_t)USRSTACK32;
20127c478bd9Sstevel@tonic-gate 	p->p_model = DATAMODEL_ILP32;
20137c478bd9Sstevel@tonic-gate 	p->p_stkprot = PROT_ZFOD & ~PROT_EXEC;
20147c478bd9Sstevel@tonic-gate 	p->p_datprot = PROT_ZFOD & ~PROT_EXEC;
20157c478bd9Sstevel@tonic-gate 	p->p_stk_ctl = INT32_MAX;
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 	p->p_as = as_alloc();
20187c478bd9Sstevel@tonic-gate 	p->p_as->a_userlimit = (caddr_t)USERLIMIT32;
20197c478bd9Sstevel@tonic-gate 	(void) hat_setup(p->p_as->a_hat, HAT_INIT);
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	cg = zone_getspecific(core_zone_key, p->p_zone);
20227c478bd9Sstevel@tonic-gate 	ASSERT(cg != NULL);
20237c478bd9Sstevel@tonic-gate 	corectl_path_hold(cg->core_default_path);
20247c478bd9Sstevel@tonic-gate 	corectl_content_hold(cg->core_default_content);
20257c478bd9Sstevel@tonic-gate 	p->p_corefile = cg->core_default_path;
20267c478bd9Sstevel@tonic-gate 	p->p_content = cg->core_default_content;
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	init_mstate(curthread, LMS_SYSTEM);
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 	p->p_zone->zone_boot_err = exec_init(zone_initname, 0,
20317c478bd9Sstevel@tonic-gate 	    p->p_zone->zone_bootargs);
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
20347c478bd9Sstevel@tonic-gate 	if (p->p_zone->zone_boot_err != 0) {
20357c478bd9Sstevel@tonic-gate 		/*
20367c478bd9Sstevel@tonic-gate 		 * Make sure we are still in the booting state-- we could have
20377c478bd9Sstevel@tonic-gate 		 * raced and already be shutting down, or even further along.
20387c478bd9Sstevel@tonic-gate 		 */
20397c478bd9Sstevel@tonic-gate 		if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING)
20407c478bd9Sstevel@tonic-gate 			zone_status_set(p->p_zone, ZONE_IS_SHUTTING_DOWN);
20417c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
20427c478bd9Sstevel@tonic-gate 		/* It's gone bad, dispose of the process */
20437c478bd9Sstevel@tonic-gate 		if (proc_exit(CLD_EXITED, p->p_zone->zone_boot_err) != 0) {
204497eda132Sraf 			mutex_enter(&p->p_lock);
204597eda132Sraf 			ASSERT(p->p_flag & SEXITLWPS);
20467c478bd9Sstevel@tonic-gate 			lwp_exit();
20477c478bd9Sstevel@tonic-gate 		}
20487c478bd9Sstevel@tonic-gate 	} else {
20497c478bd9Sstevel@tonic-gate 		if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING)
20507c478bd9Sstevel@tonic-gate 			zone_status_set(p->p_zone, ZONE_IS_RUNNING);
20517c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
20527c478bd9Sstevel@tonic-gate 		/* cause the process to return to userland. */
20537c478bd9Sstevel@tonic-gate 		lwp_rtt();
20547c478bd9Sstevel@tonic-gate 	}
20557c478bd9Sstevel@tonic-gate }
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate struct zsched_arg {
20587c478bd9Sstevel@tonic-gate 	zone_t *zone;
20597c478bd9Sstevel@tonic-gate 	nvlist_t *nvlist;
20607c478bd9Sstevel@tonic-gate };
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate /*
20637c478bd9Sstevel@tonic-gate  * Per-zone "sched" workalike.  The similarity to "sched" doesn't have
20647c478bd9Sstevel@tonic-gate  * anything to do with scheduling, but rather with the fact that
20657c478bd9Sstevel@tonic-gate  * per-zone kernel threads are parented to zsched, just like regular
20667c478bd9Sstevel@tonic-gate  * kernel threads are parented to sched (p0).
20677c478bd9Sstevel@tonic-gate  *
20687c478bd9Sstevel@tonic-gate  * zsched is also responsible for launching init for the zone.
20697c478bd9Sstevel@tonic-gate  */
20707c478bd9Sstevel@tonic-gate static void
20717c478bd9Sstevel@tonic-gate zsched(void *arg)
20727c478bd9Sstevel@tonic-gate {
20737c478bd9Sstevel@tonic-gate 	struct zsched_arg *za = arg;
20747c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
20757c478bd9Sstevel@tonic-gate 	proc_t *initp = proc_init;
20767c478bd9Sstevel@tonic-gate 	zone_t *zone = za->zone;
20777c478bd9Sstevel@tonic-gate 	cred_t *cr, *oldcred;
20787c478bd9Sstevel@tonic-gate 	rctl_set_t *set;
20797c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
20807c478bd9Sstevel@tonic-gate 	contract_t *ct = NULL;
20817c478bd9Sstevel@tonic-gate 	task_t *tk, *oldtk;
20827c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
20837c478bd9Sstevel@tonic-gate 	kproject_t *pj;
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = za->nvlist;
20867c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	bcopy("zsched", u.u_psargs, sizeof ("zsched"));
20897c478bd9Sstevel@tonic-gate 	bcopy("zsched", u.u_comm, sizeof ("zsched"));
20907c478bd9Sstevel@tonic-gate 	u.u_argc = 0;
20917c478bd9Sstevel@tonic-gate 	u.u_argv = NULL;
20927c478bd9Sstevel@tonic-gate 	u.u_envp = NULL;
20937c478bd9Sstevel@tonic-gate 	closeall(P_FINFO(pp));
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 	/*
20967c478bd9Sstevel@tonic-gate 	 * We are this zone's "zsched" process.  As the zone isn't generally
20977c478bd9Sstevel@tonic-gate 	 * visible yet we don't need to grab any locks before initializing its
20987c478bd9Sstevel@tonic-gate 	 * zone_proc pointer.
20997c478bd9Sstevel@tonic-gate 	 */
21007c478bd9Sstevel@tonic-gate 	zone_hold(zone);  /* this hold is released by zone_destroy() */
21017c478bd9Sstevel@tonic-gate 	zone->zone_zsched = pp;
21027c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
21037c478bd9Sstevel@tonic-gate 	pp->p_zone = zone;
21047c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	/*
21077c478bd9Sstevel@tonic-gate 	 * Disassociate process from its 'parent'; parent ourselves to init
21087c478bd9Sstevel@tonic-gate 	 * (pid 1) and change other values as needed.
21097c478bd9Sstevel@tonic-gate 	 */
21107c478bd9Sstevel@tonic-gate 	sess_create();
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
21137c478bd9Sstevel@tonic-gate 	proc_detach(pp);
21147c478bd9Sstevel@tonic-gate 	pp->p_ppid = 1;
21157c478bd9Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
21167c478bd9Sstevel@tonic-gate 	pp->p_ancpid = 1;
21177c478bd9Sstevel@tonic-gate 	pp->p_parent = initp;
21187c478bd9Sstevel@tonic-gate 	pp->p_psibling = NULL;
21197c478bd9Sstevel@tonic-gate 	if (initp->p_child)
21207c478bd9Sstevel@tonic-gate 		initp->p_child->p_psibling = pp;
21217c478bd9Sstevel@tonic-gate 	pp->p_sibling = initp->p_child;
21227c478bd9Sstevel@tonic-gate 	initp->p_child = pp;
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 	/* Decrement what newproc() incremented. */
21257c478bd9Sstevel@tonic-gate 	upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID);
21267c478bd9Sstevel@tonic-gate 	/*
21277c478bd9Sstevel@tonic-gate 	 * Our credentials are about to become kcred-like, so we don't care
21287c478bd9Sstevel@tonic-gate 	 * about the caller's ruid.
21297c478bd9Sstevel@tonic-gate 	 */
21307c478bd9Sstevel@tonic-gate 	upcount_inc(crgetruid(kcred), zone->zone_id);
21317c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 	/*
21347c478bd9Sstevel@tonic-gate 	 * getting out of global zone, so decrement lwp counts
21357c478bd9Sstevel@tonic-gate 	 */
21367c478bd9Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
21377c478bd9Sstevel@tonic-gate 	mutex_enter(&global_zone->zone_nlwps_lock);
21387c478bd9Sstevel@tonic-gate 	pj->kpj_nlwps -= pp->p_lwpcnt;
21397c478bd9Sstevel@tonic-gate 	global_zone->zone_nlwps -= pp->p_lwpcnt;
21407c478bd9Sstevel@tonic-gate 	mutex_exit(&global_zone->zone_nlwps_lock);
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 	/*
21437c478bd9Sstevel@tonic-gate 	 * Create and join a new task in project '0' of this zone.
21447c478bd9Sstevel@tonic-gate 	 *
21457c478bd9Sstevel@tonic-gate 	 * We don't need to call holdlwps() since we know we're the only lwp in
21467c478bd9Sstevel@tonic-gate 	 * this process.
21477c478bd9Sstevel@tonic-gate 	 *
21487c478bd9Sstevel@tonic-gate 	 * task_join() returns with p_lock held.
21497c478bd9Sstevel@tonic-gate 	 */
21507c478bd9Sstevel@tonic-gate 	tk = task_create(0, zone);
21517c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
21527c478bd9Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
21537c478bd9Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
21547c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
21557c478bd9Sstevel@tonic-gate 	task_rele(oldtk);
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 	/*
21587c478bd9Sstevel@tonic-gate 	 * add lwp counts to zsched's zone, and increment project's task count
21597c478bd9Sstevel@tonic-gate 	 * due to the task created in the above tasksys_settaskid
21607c478bd9Sstevel@tonic-gate 	 */
21617c478bd9Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
21627c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
21637c478bd9Sstevel@tonic-gate 	pj->kpj_nlwps += pp->p_lwpcnt;
21647c478bd9Sstevel@tonic-gate 	pj->kpj_ntasks += 1;
21657c478bd9Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
21667c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate 	/*
21697c478bd9Sstevel@tonic-gate 	 * The process was created by a process in the global zone, hence the
21707c478bd9Sstevel@tonic-gate 	 * credentials are wrong.  We might as well have kcred-ish credentials.
21717c478bd9Sstevel@tonic-gate 	 */
21727c478bd9Sstevel@tonic-gate 	cr = zone->zone_kcred;
21737c478bd9Sstevel@tonic-gate 	crhold(cr);
21747c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
21757c478bd9Sstevel@tonic-gate 	oldcred = pp->p_cred;
21767c478bd9Sstevel@tonic-gate 	pp->p_cred = cr;
21777c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
21787c478bd9Sstevel@tonic-gate 	crfree(oldcred);
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 	/*
21817c478bd9Sstevel@tonic-gate 	 * Hold credentials again (for thread)
21827c478bd9Sstevel@tonic-gate 	 */
21837c478bd9Sstevel@tonic-gate 	crhold(cr);
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate 	/*
21867c478bd9Sstevel@tonic-gate 	 * p_lwpcnt can't change since this is a kernel process.
21877c478bd9Sstevel@tonic-gate 	 */
21887c478bd9Sstevel@tonic-gate 	crset(pp, cr);
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 	/*
21917c478bd9Sstevel@tonic-gate 	 * Chroot
21927c478bd9Sstevel@tonic-gate 	 */
21937c478bd9Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp);
21947c478bd9Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp);
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 	/*
21977c478bd9Sstevel@tonic-gate 	 * Initialize zone's rctl set.
21987c478bd9Sstevel@tonic-gate 	 */
21997c478bd9Sstevel@tonic-gate 	set = rctl_set_create();
22007c478bd9Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
22017c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
22027c478bd9Sstevel@tonic-gate 	e.rcep_p.zone = zone;
22037c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
22047c478bd9Sstevel@tonic-gate 	zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp);
22057c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
22067c478bd9Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	/*
22097c478bd9Sstevel@tonic-gate 	 * Apply the rctls passed in to zone_create().  This is basically a list
22107c478bd9Sstevel@tonic-gate 	 * assignment: all of the old values are removed and the new ones
22117c478bd9Sstevel@tonic-gate 	 * inserted.  That is, if an empty list is passed in, all values are
22127c478bd9Sstevel@tonic-gate 	 * removed.
22137c478bd9Sstevel@tonic-gate 	 */
22147c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
22157c478bd9Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
22167c478bd9Sstevel@tonic-gate 		rctl_hndl_t hndl;
22177c478bd9Sstevel@tonic-gate 		char *name;
22187c478bd9Sstevel@tonic-gate 		nvlist_t **nvlarray;
22197c478bd9Sstevel@tonic-gate 		uint_t i, nelem;
22207c478bd9Sstevel@tonic-gate 		int error;	/* For ASSERT()s */
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
22237c478bd9Sstevel@tonic-gate 		hndl = rctl_hndl_lookup(name);
22247c478bd9Sstevel@tonic-gate 		ASSERT(hndl != -1);
22257c478bd9Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
22267c478bd9Sstevel@tonic-gate 		ASSERT(rde != NULL);
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 		for (; /* ever */; ) {
22297c478bd9Sstevel@tonic-gate 			rctl_val_t oval;
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
22327c478bd9Sstevel@tonic-gate 			error = rctl_local_get(hndl, NULL, &oval, pp);
22337c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
22347c478bd9Sstevel@tonic-gate 			ASSERT(error == 0);	/* Can't fail for RCTL_FIRST */
22357c478bd9Sstevel@tonic-gate 			ASSERT(oval.rcv_privilege != RCPRIV_BASIC);
22367c478bd9Sstevel@tonic-gate 			if (oval.rcv_privilege == RCPRIV_SYSTEM)
22377c478bd9Sstevel@tonic-gate 				break;
22387c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
22397c478bd9Sstevel@tonic-gate 			error = rctl_local_delete(hndl, &oval, pp);
22407c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
22417c478bd9Sstevel@tonic-gate 			ASSERT(error == 0);
22427c478bd9Sstevel@tonic-gate 		}
22437c478bd9Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
22447c478bd9Sstevel@tonic-gate 		ASSERT(error == 0);
22457c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
22467c478bd9Sstevel@tonic-gate 			rctl_val_t *nvalp;
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 			nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
22497c478bd9Sstevel@tonic-gate 			error = nvlist2rctlval(nvlarray[i], nvalp);
22507c478bd9Sstevel@tonic-gate 			ASSERT(error == 0);
22517c478bd9Sstevel@tonic-gate 			/*
22527c478bd9Sstevel@tonic-gate 			 * rctl_local_insert can fail if the value being
22537c478bd9Sstevel@tonic-gate 			 * inserted is a duplicate; this is OK.
22547c478bd9Sstevel@tonic-gate 			 */
22557c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
22567c478bd9Sstevel@tonic-gate 			if (rctl_local_insert(hndl, nvalp, pp) != 0)
22577c478bd9Sstevel@tonic-gate 				kmem_cache_free(rctl_val_cache, nvalp);
22587c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
22597c478bd9Sstevel@tonic-gate 		}
22607c478bd9Sstevel@tonic-gate 	}
22617c478bd9Sstevel@tonic-gate 	/*
22627c478bd9Sstevel@tonic-gate 	 * Tell the world that we're done setting up.
22637c478bd9Sstevel@tonic-gate 	 *
22647c478bd9Sstevel@tonic-gate 	 * At this point we want to set the zone status to ZONE_IS_READY
22657c478bd9Sstevel@tonic-gate 	 * and atomically set the zone's processor set visibility.  Once
22667c478bd9Sstevel@tonic-gate 	 * we drop pool_lock() this zone will automatically get updated
22677c478bd9Sstevel@tonic-gate 	 * to reflect any future changes to the pools configuration.
22687c478bd9Sstevel@tonic-gate 	 */
22697c478bd9Sstevel@tonic-gate 	pool_lock();
22707c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
22717c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
22727c478bd9Sstevel@tonic-gate 	zone_uniqid(zone);
22737c478bd9Sstevel@tonic-gate 	zone_zsd_configure(zone);
22747c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED)
22757c478bd9Sstevel@tonic-gate 		zone_pset_set(zone, pool_default->pool_pset->pset_id);
22767c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
22777c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
22787c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_READY);
22797c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
22807c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
22817c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
22827c478bd9Sstevel@tonic-gate 	pool_unlock();
22837c478bd9Sstevel@tonic-gate 
22847c478bd9Sstevel@tonic-gate 	/*
22857c478bd9Sstevel@tonic-gate 	 * Once we see the zone transition to the ZONE_IS_BOOTING state,
22867c478bd9Sstevel@tonic-gate 	 * we launch init, and set the state to running.
22877c478bd9Sstevel@tonic-gate 	 */
22887c478bd9Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched");
22897c478bd9Sstevel@tonic-gate 
22907c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_BOOTING) {
22917c478bd9Sstevel@tonic-gate 		id_t cid;
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 		/*
22947c478bd9Sstevel@tonic-gate 		 * Ok, this is a little complicated.  We need to grab the
22957c478bd9Sstevel@tonic-gate 		 * zone's pool's scheduling class ID; note that by now, we
22967c478bd9Sstevel@tonic-gate 		 * are already bound to a pool if we need to be (zoneadmd
22977c478bd9Sstevel@tonic-gate 		 * will have done that to us while we're in the READY
22987c478bd9Sstevel@tonic-gate 		 * state).  *But* the scheduling class for the zone's 'init'
22997c478bd9Sstevel@tonic-gate 		 * must be explicitly passed to newproc, which doesn't
23007c478bd9Sstevel@tonic-gate 		 * respect pool bindings.
23017c478bd9Sstevel@tonic-gate 		 *
23027c478bd9Sstevel@tonic-gate 		 * We hold the pool_lock across the call to newproc() to
23037c478bd9Sstevel@tonic-gate 		 * close the obvious race: the pool's scheduling class
23047c478bd9Sstevel@tonic-gate 		 * could change before we manage to create the LWP with
23057c478bd9Sstevel@tonic-gate 		 * classid 'cid'.
23067c478bd9Sstevel@tonic-gate 		 */
23077c478bd9Sstevel@tonic-gate 		pool_lock();
23087c478bd9Sstevel@tonic-gate 		cid = pool_get_class(zone->zone_pool);
23097c478bd9Sstevel@tonic-gate 		if (cid == -1)
23107c478bd9Sstevel@tonic-gate 			cid = defaultcid;
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 		/*
23137c478bd9Sstevel@tonic-gate 		 * If this fails, zone_boot will ultimately fail.  The
23147c478bd9Sstevel@tonic-gate 		 * state of the zone will be set to SHUTTING_DOWN-- userland
23157c478bd9Sstevel@tonic-gate 		 * will have to tear down the zone, and fail, or try again.
23167c478bd9Sstevel@tonic-gate 		 */
23177c478bd9Sstevel@tonic-gate 		if ((zone->zone_boot_err = newproc(zone_icode, NULL, cid,
23187c478bd9Sstevel@tonic-gate 		    minclsyspri - 1, &ct)) != 0) {
23197c478bd9Sstevel@tonic-gate 			mutex_enter(&zone_status_lock);
23207c478bd9Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
23217c478bd9Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
23227c478bd9Sstevel@tonic-gate 		}
23237c478bd9Sstevel@tonic-gate 		pool_unlock();
23247c478bd9Sstevel@tonic-gate 	}
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate 	/*
23277c478bd9Sstevel@tonic-gate 	 * Wait for zone_destroy() to be called.  This is what we spend
23287c478bd9Sstevel@tonic-gate 	 * most of our life doing.
23297c478bd9Sstevel@tonic-gate 	 */
23307c478bd9Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched");
23317c478bd9Sstevel@tonic-gate 
23327c478bd9Sstevel@tonic-gate 	if (ct)
23337c478bd9Sstevel@tonic-gate 		/*
23347c478bd9Sstevel@tonic-gate 		 * At this point the process contract should be empty.
23357c478bd9Sstevel@tonic-gate 		 * (Though if it isn't, it's not the end of the world.)
23367c478bd9Sstevel@tonic-gate 		 */
23377c478bd9Sstevel@tonic-gate 		VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0);
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 	/*
23407c478bd9Sstevel@tonic-gate 	 * Allow kcred to be freed when all referring processes
23417c478bd9Sstevel@tonic-gate 	 * (including this one) go away.  We can't just do this in
23427c478bd9Sstevel@tonic-gate 	 * zone_free because we need to wait for the zone_cred_ref to
23437c478bd9Sstevel@tonic-gate 	 * drop to 0 before calling zone_free, and the existence of
23447c478bd9Sstevel@tonic-gate 	 * zone_kcred will prevent that.  Thus, we call crfree here to
23457c478bd9Sstevel@tonic-gate 	 * balance the crdup in zone_create.  The crhold calls earlier
23467c478bd9Sstevel@tonic-gate 	 * in zsched will be dropped when the thread and process exit.
23477c478bd9Sstevel@tonic-gate 	 */
23487c478bd9Sstevel@tonic-gate 	crfree(zone->zone_kcred);
23497c478bd9Sstevel@tonic-gate 	zone->zone_kcred = NULL;
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
23527c478bd9Sstevel@tonic-gate }
23537c478bd9Sstevel@tonic-gate 
23547c478bd9Sstevel@tonic-gate /*
23557c478bd9Sstevel@tonic-gate  * Helper function to determine if there are any submounts of the
23567c478bd9Sstevel@tonic-gate  * provided path.  Used to make sure the zone doesn't "inherit" any
23577c478bd9Sstevel@tonic-gate  * mounts from before it is created.
23587c478bd9Sstevel@tonic-gate  */
23597c478bd9Sstevel@tonic-gate static uint_t
23607c478bd9Sstevel@tonic-gate zone_mount_count(const char *rootpath)
23617c478bd9Sstevel@tonic-gate {
23627c478bd9Sstevel@tonic-gate 	vfs_t *vfsp;
23637c478bd9Sstevel@tonic-gate 	uint_t count = 0;
23647c478bd9Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
23657c478bd9Sstevel@tonic-gate 
23667c478bd9Sstevel@tonic-gate 	/*
23677c478bd9Sstevel@tonic-gate 	 * Holding zonehash_lock prevents race conditions with
23687c478bd9Sstevel@tonic-gate 	 * vfs_list_add()/vfs_list_remove() since we serialize with
23697c478bd9Sstevel@tonic-gate 	 * zone_find_by_path().
23707c478bd9Sstevel@tonic-gate 	 */
23717c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
23727c478bd9Sstevel@tonic-gate 	/*
23737c478bd9Sstevel@tonic-gate 	 * The rootpath must end with a '/'
23747c478bd9Sstevel@tonic-gate 	 */
23757c478bd9Sstevel@tonic-gate 	ASSERT(rootpath[rootpathlen - 1] == '/');
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate 	/*
23787c478bd9Sstevel@tonic-gate 	 * This intentionally does not count the rootpath itself if that
23797c478bd9Sstevel@tonic-gate 	 * happens to be a mount point.
23807c478bd9Sstevel@tonic-gate 	 */
23817c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
23827c478bd9Sstevel@tonic-gate 	vfsp = rootvfs;
23837c478bd9Sstevel@tonic-gate 	do {
23847c478bd9Sstevel@tonic-gate 		if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt),
23857c478bd9Sstevel@tonic-gate 		    rootpathlen) == 0)
23867c478bd9Sstevel@tonic-gate 			count++;
23877c478bd9Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
23887c478bd9Sstevel@tonic-gate 	} while (vfsp != rootvfs);
23897c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
23907c478bd9Sstevel@tonic-gate 	return (count);
23917c478bd9Sstevel@tonic-gate }
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate /*
23947c478bd9Sstevel@tonic-gate  * Helper function to make sure that a zone created on 'rootpath'
23957c478bd9Sstevel@tonic-gate  * wouldn't end up containing other zones' rootpaths.
23967c478bd9Sstevel@tonic-gate  */
23977c478bd9Sstevel@tonic-gate static boolean_t
23987c478bd9Sstevel@tonic-gate zone_is_nested(const char *rootpath)
23997c478bd9Sstevel@tonic-gate {
24007c478bd9Sstevel@tonic-gate 	zone_t *zone;
24017c478bd9Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
24027c478bd9Sstevel@tonic-gate 	size_t len;
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
24077c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
24087c478bd9Sstevel@tonic-gate 		if (zone == global_zone)
24097c478bd9Sstevel@tonic-gate 			continue;
24107c478bd9Sstevel@tonic-gate 		len = strlen(zone->zone_rootpath);
24117c478bd9Sstevel@tonic-gate 		if (strncmp(rootpath, zone->zone_rootpath,
24127c478bd9Sstevel@tonic-gate 		    MIN(rootpathlen, len)) == 0)
24137c478bd9Sstevel@tonic-gate 			return (B_TRUE);
24147c478bd9Sstevel@tonic-gate 	}
24157c478bd9Sstevel@tonic-gate 	return (B_FALSE);
24167c478bd9Sstevel@tonic-gate }
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate static int
24197c478bd9Sstevel@tonic-gate zone_set_privset(zone_t *zone, const priv_set_t *zone_privs)
24207c478bd9Sstevel@tonic-gate {
24217c478bd9Sstevel@tonic-gate 	priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 	if (copyin(zone_privs, privs, sizeof (priv_set_t))) {
24247c478bd9Sstevel@tonic-gate 		kmem_free(privs, sizeof (priv_set_t));
24257c478bd9Sstevel@tonic-gate 		return (EFAULT);
24267c478bd9Sstevel@tonic-gate 	}
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate 	zone->zone_privset = privs;
24297c478bd9Sstevel@tonic-gate 	return (0);
24307c478bd9Sstevel@tonic-gate }
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate /*
24337c478bd9Sstevel@tonic-gate  * We make creative use of nvlists to pass in rctls from userland.  The list is
24347c478bd9Sstevel@tonic-gate  * a list of the following structures:
24357c478bd9Sstevel@tonic-gate  *
24367c478bd9Sstevel@tonic-gate  * (name = rctl_name, value = nvpair_list_array)
24377c478bd9Sstevel@tonic-gate  *
24387c478bd9Sstevel@tonic-gate  * Where each element of the nvpair_list_array is of the form:
24397c478bd9Sstevel@tonic-gate  *
24407c478bd9Sstevel@tonic-gate  * [(name = "privilege", value = RCPRIV_PRIVILEGED),
24417c478bd9Sstevel@tonic-gate  * 	(name = "limit", value = uint64_t),
24427c478bd9Sstevel@tonic-gate  * 	(name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))]
24437c478bd9Sstevel@tonic-gate  */
24447c478bd9Sstevel@tonic-gate static int
24457c478bd9Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp)
24467c478bd9Sstevel@tonic-gate {
24477c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
24487c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
24497c478bd9Sstevel@tonic-gate 	char *kbuf;
24507c478bd9Sstevel@tonic-gate 	int error;
24517c478bd9Sstevel@tonic-gate 	rctl_val_t rv;
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate 	*nvlp = NULL;
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate 	if (buflen == 0)
24567c478bd9Sstevel@tonic-gate 		return (0);
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
24597c478bd9Sstevel@tonic-gate 		return (ENOMEM);
24607c478bd9Sstevel@tonic-gate 	if (copyin(ubuf, kbuf, buflen)) {
24617c478bd9Sstevel@tonic-gate 		error = EFAULT;
24627c478bd9Sstevel@tonic-gate 		goto out;
24637c478bd9Sstevel@tonic-gate 	}
24647c478bd9Sstevel@tonic-gate 	if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) {
24657c478bd9Sstevel@tonic-gate 		/*
24667c478bd9Sstevel@tonic-gate 		 * nvl may have been allocated/free'd, but the value set to
24677c478bd9Sstevel@tonic-gate 		 * non-NULL, so we reset it here.
24687c478bd9Sstevel@tonic-gate 		 */
24697c478bd9Sstevel@tonic-gate 		nvl = NULL;
24707c478bd9Sstevel@tonic-gate 		error = EINVAL;
24717c478bd9Sstevel@tonic-gate 		goto out;
24727c478bd9Sstevel@tonic-gate 	}
24737c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
24747c478bd9Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
24757c478bd9Sstevel@tonic-gate 		rctl_hndl_t hndl;
24767c478bd9Sstevel@tonic-gate 		nvlist_t **nvlarray;
24777c478bd9Sstevel@tonic-gate 		uint_t i, nelem;
24787c478bd9Sstevel@tonic-gate 		char *name;
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate 		error = EINVAL;
24817c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
24827c478bd9Sstevel@tonic-gate 		if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1)
24837c478bd9Sstevel@tonic-gate 		    != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) {
24847c478bd9Sstevel@tonic-gate 			goto out;
24857c478bd9Sstevel@tonic-gate 		}
24867c478bd9Sstevel@tonic-gate 		if ((hndl = rctl_hndl_lookup(name)) == -1) {
24877c478bd9Sstevel@tonic-gate 			goto out;
24887c478bd9Sstevel@tonic-gate 		}
24897c478bd9Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
24907c478bd9Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
24917c478bd9Sstevel@tonic-gate 		ASSERT(error == 0);
24927c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
24937c478bd9Sstevel@tonic-gate 			if (error = nvlist2rctlval(nvlarray[i], &rv))
24947c478bd9Sstevel@tonic-gate 				goto out;
24957c478bd9Sstevel@tonic-gate 		}
24967c478bd9Sstevel@tonic-gate 		if (rctl_invalid_value(rde, &rv)) {
24977c478bd9Sstevel@tonic-gate 			error = EINVAL;
24987c478bd9Sstevel@tonic-gate 			goto out;
24997c478bd9Sstevel@tonic-gate 		}
25007c478bd9Sstevel@tonic-gate 	}
25017c478bd9Sstevel@tonic-gate 	error = 0;
25027c478bd9Sstevel@tonic-gate 	*nvlp = nvl;
25037c478bd9Sstevel@tonic-gate out:
25047c478bd9Sstevel@tonic-gate 	kmem_free(kbuf, buflen);
25057c478bd9Sstevel@tonic-gate 	if (error && nvl != NULL)
25067c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
25077c478bd9Sstevel@tonic-gate 	return (error);
25087c478bd9Sstevel@tonic-gate }
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate int
25117c478bd9Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) {
25127c478bd9Sstevel@tonic-gate 	if (er_out != NULL) {
25137c478bd9Sstevel@tonic-gate 		if (copyout(&er_ext, er_out, sizeof (int))) {
25147c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
25157c478bd9Sstevel@tonic-gate 		}
25167c478bd9Sstevel@tonic-gate 	}
25177c478bd9Sstevel@tonic-gate 	return (set_errno(er_error));
25187c478bd9Sstevel@tonic-gate }
25197c478bd9Sstevel@tonic-gate 
2520*fa9e4066Sahrens /*
2521*fa9e4066Sahrens  * Parses a comma-separated list of ZFS datasets into a per-zone dictionary.
2522*fa9e4066Sahrens  */
2523*fa9e4066Sahrens static int
2524*fa9e4066Sahrens parse_zfs(zone_t *zone, caddr_t ubuf, size_t buflen)
2525*fa9e4066Sahrens {
2526*fa9e4066Sahrens 	char *kbuf;
2527*fa9e4066Sahrens 	char *dataset, *next;
2528*fa9e4066Sahrens 	zone_dataset_t *zd;
2529*fa9e4066Sahrens 	size_t len;
2530*fa9e4066Sahrens 
2531*fa9e4066Sahrens 	if (ubuf == NULL || buflen == 0)
2532*fa9e4066Sahrens 		return (0);
2533*fa9e4066Sahrens 
2534*fa9e4066Sahrens 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
2535*fa9e4066Sahrens 		return (ENOMEM);
2536*fa9e4066Sahrens 
2537*fa9e4066Sahrens 	if (copyin(ubuf, kbuf, buflen) != 0) {
2538*fa9e4066Sahrens 		kmem_free(kbuf, buflen);
2539*fa9e4066Sahrens 		return (EFAULT);
2540*fa9e4066Sahrens 	}
2541*fa9e4066Sahrens 
2542*fa9e4066Sahrens 	dataset = next = kbuf;
2543*fa9e4066Sahrens 	for (;;) {
2544*fa9e4066Sahrens 		zd = kmem_alloc(sizeof (zone_dataset_t), KM_SLEEP);
2545*fa9e4066Sahrens 
2546*fa9e4066Sahrens 		next = strchr(dataset, ',');
2547*fa9e4066Sahrens 
2548*fa9e4066Sahrens 		if (next == NULL)
2549*fa9e4066Sahrens 			len = strlen(dataset);
2550*fa9e4066Sahrens 		else
2551*fa9e4066Sahrens 			len = next - dataset;
2552*fa9e4066Sahrens 
2553*fa9e4066Sahrens 		zd->zd_dataset = kmem_alloc(len + 1, KM_SLEEP);
2554*fa9e4066Sahrens 		bcopy(dataset, zd->zd_dataset, len);
2555*fa9e4066Sahrens 		zd->zd_dataset[len] = '\0';
2556*fa9e4066Sahrens 
2557*fa9e4066Sahrens 		list_insert_head(&zone->zone_datasets, zd);
2558*fa9e4066Sahrens 
2559*fa9e4066Sahrens 		if (next == NULL)
2560*fa9e4066Sahrens 			break;
2561*fa9e4066Sahrens 
2562*fa9e4066Sahrens 		dataset = next + 1;
2563*fa9e4066Sahrens 	}
2564*fa9e4066Sahrens 
2565*fa9e4066Sahrens 	kmem_free(kbuf, buflen);
2566*fa9e4066Sahrens 	return (0);
2567*fa9e4066Sahrens }
2568*fa9e4066Sahrens 
25697c478bd9Sstevel@tonic-gate /*
25707c478bd9Sstevel@tonic-gate  * System call to create/initialize a new zone named 'zone_name', rooted
25717c478bd9Sstevel@tonic-gate  * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs',
25727c478bd9Sstevel@tonic-gate  * and initialized with the zone-wide rctls described in 'rctlbuf'.
25737c478bd9Sstevel@tonic-gate  *
25747c478bd9Sstevel@tonic-gate  * If extended error is non-null, we may use it to return more detailed
25757c478bd9Sstevel@tonic-gate  * error information.
25767c478bd9Sstevel@tonic-gate  */
25777c478bd9Sstevel@tonic-gate static zoneid_t
25787c478bd9Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root,
25797c478bd9Sstevel@tonic-gate     const priv_set_t *zone_privs, caddr_t rctlbuf, size_t rctlbufsz,
2580*fa9e4066Sahrens     caddr_t zfsbuf, size_t zfsbufsz, int *extended_error)
25817c478bd9Sstevel@tonic-gate {
25827c478bd9Sstevel@tonic-gate 	struct zsched_arg zarg;
25837c478bd9Sstevel@tonic-gate 	nvlist_t *rctls = NULL;
25847c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
25857c478bd9Sstevel@tonic-gate 	zone_t *zone, *ztmp;
25867c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
25877c478bd9Sstevel@tonic-gate 	int error;
25887c478bd9Sstevel@tonic-gate 	int error2 = 0;
25897c478bd9Sstevel@tonic-gate 	char *str;
25907c478bd9Sstevel@tonic-gate 	cred_t *zkcr;
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
25937c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate 	/* can't boot zone from within chroot environment */
25967c478bd9Sstevel@tonic-gate 	if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir)
25977c478bd9Sstevel@tonic-gate 		return (zone_create_error(ENOTSUP, ZE_CHROOTED,
25987c478bd9Sstevel@tonic-gate 			extended_error));
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate 	zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP);
26017c478bd9Sstevel@tonic-gate 	zoneid = zone->zone_id = id_alloc(zoneid_space);
26027c478bd9Sstevel@tonic-gate 	zone->zone_status = ZONE_IS_UNINITIALIZED;
26037c478bd9Sstevel@tonic-gate 	zone->zone_pool = pool_default;
26047c478bd9Sstevel@tonic-gate 	zone->zone_pool_mod = gethrtime();
26057c478bd9Sstevel@tonic-gate 	zone->zone_psetid = ZONE_PS_INVAL;
26067c478bd9Sstevel@tonic-gate 	zone->zone_ncpus = 0;
26077c478bd9Sstevel@tonic-gate 	zone->zone_ncpus_online = 0;
26087c478bd9Sstevel@tonic-gate 	mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL);
26097c478bd9Sstevel@tonic-gate 	mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
26107c478bd9Sstevel@tonic-gate 	cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL);
26117c478bd9Sstevel@tonic-gate 	list_create(&zone->zone_zsd, sizeof (struct zsd_entry),
26127c478bd9Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
2613*fa9e4066Sahrens 	list_create(&zone->zone_datasets, sizeof (zone_dataset_t),
2614*fa9e4066Sahrens 	    offsetof(zone_dataset_t, zd_linkage));
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate 	if ((error = zone_set_name(zone, zone_name)) != 0) {
26177c478bd9Sstevel@tonic-gate 		zone_free(zone);
26187c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
26197c478bd9Sstevel@tonic-gate 	}
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate 	if ((error = zone_set_root(zone, zone_root)) != 0) {
26227c478bd9Sstevel@tonic-gate 		zone_free(zone);
26237c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
26247c478bd9Sstevel@tonic-gate 	}
26257c478bd9Sstevel@tonic-gate 	if ((error = zone_set_privset(zone, zone_privs)) != 0) {
26267c478bd9Sstevel@tonic-gate 		zone_free(zone);
26277c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
26287c478bd9Sstevel@tonic-gate 	}
26297c478bd9Sstevel@tonic-gate 
26307c478bd9Sstevel@tonic-gate 	/* initialize node name to be the same as zone name */
26317c478bd9Sstevel@tonic-gate 	zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP);
26327c478bd9Sstevel@tonic-gate 	(void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN);
26337c478bd9Sstevel@tonic-gate 	zone->zone_nodename[_SYS_NMLN - 1] = '\0';
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate 	zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP);
26367c478bd9Sstevel@tonic-gate 	zone->zone_domain[0] = '\0';
26377c478bd9Sstevel@tonic-gate 	zone->zone_shares = 1;
26387c478bd9Sstevel@tonic-gate 	zone->zone_bootargs = NULL;
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate 	/*
26417c478bd9Sstevel@tonic-gate 	 * Zsched initializes the rctls.
26427c478bd9Sstevel@tonic-gate 	 */
26437c478bd9Sstevel@tonic-gate 	zone->zone_rctls = NULL;
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) {
26467c478bd9Sstevel@tonic-gate 		zone_free(zone);
26477c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
26487c478bd9Sstevel@tonic-gate 	}
26497c478bd9Sstevel@tonic-gate 
2650*fa9e4066Sahrens 	if ((error = parse_zfs(zone, zfsbuf, zfsbufsz)) != 0) {
2651*fa9e4066Sahrens 		zone_free(zone);
2652*fa9e4066Sahrens 		return (set_errno(error));
2653*fa9e4066Sahrens 	}
2654*fa9e4066Sahrens 
26557c478bd9Sstevel@tonic-gate 	/*
26567c478bd9Sstevel@tonic-gate 	 * Stop all lwps since that's what normally happens as part of fork().
26577c478bd9Sstevel@tonic-gate 	 * This needs to happen before we grab any locks to avoid deadlock
26587c478bd9Sstevel@tonic-gate 	 * (another lwp in the process could be waiting for the held lock).
26597c478bd9Sstevel@tonic-gate 	 */
26607c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) {
26617c478bd9Sstevel@tonic-gate 		zone_free(zone);
26627c478bd9Sstevel@tonic-gate 		if (rctls)
26637c478bd9Sstevel@tonic-gate 			nvlist_free(rctls);
26647c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
26657c478bd9Sstevel@tonic-gate 	}
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate 	if (block_mounts() == 0) {
26687c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
26697c478bd9Sstevel@tonic-gate 		if (curthread != pp->p_agenttp)
26707c478bd9Sstevel@tonic-gate 			continuelwps(pp);
26717c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
26727c478bd9Sstevel@tonic-gate 		zone_free(zone);
26737c478bd9Sstevel@tonic-gate 		if (rctls)
26747c478bd9Sstevel@tonic-gate 			nvlist_free(rctls);
26757c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
26767c478bd9Sstevel@tonic-gate 	}
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 	/*
26797c478bd9Sstevel@tonic-gate 	 * Set up credential for kernel access.  After this, any errors
26807c478bd9Sstevel@tonic-gate 	 * should go through the dance in errout rather than calling
26817c478bd9Sstevel@tonic-gate 	 * zone_free directly.
26827c478bd9Sstevel@tonic-gate 	 */
26837c478bd9Sstevel@tonic-gate 	zone->zone_kcred = crdup(kcred);
26847c478bd9Sstevel@tonic-gate 	crsetzone(zone->zone_kcred, zone);
26857c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred));
26867c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred));
26877c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred));
26887c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred));
26897c478bd9Sstevel@tonic-gate 
26907c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
26917c478bd9Sstevel@tonic-gate 	/*
26927c478bd9Sstevel@tonic-gate 	 * Make sure zone doesn't already exist.
26937c478bd9Sstevel@tonic-gate 	 */
26947c478bd9Sstevel@tonic-gate 	if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL) {
26957c478bd9Sstevel@tonic-gate 		zone_status_t status;
26967c478bd9Sstevel@tonic-gate 
26977c478bd9Sstevel@tonic-gate 		status = zone_status_get(ztmp);
26987c478bd9Sstevel@tonic-gate 		if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING)
26997c478bd9Sstevel@tonic-gate 			error = EEXIST;
27007c478bd9Sstevel@tonic-gate 		else
27017c478bd9Sstevel@tonic-gate 			error = EBUSY;
27027c478bd9Sstevel@tonic-gate 		goto errout;
27037c478bd9Sstevel@tonic-gate 	}
27047c478bd9Sstevel@tonic-gate 
27057c478bd9Sstevel@tonic-gate 	/*
27067c478bd9Sstevel@tonic-gate 	 * Don't allow zone creations which would cause one zone's rootpath to
27077c478bd9Sstevel@tonic-gate 	 * be accessible from that of another (non-global) zone.
27087c478bd9Sstevel@tonic-gate 	 */
27097c478bd9Sstevel@tonic-gate 	if (zone_is_nested(zone->zone_rootpath)) {
27107c478bd9Sstevel@tonic-gate 		error = EBUSY;
27117c478bd9Sstevel@tonic-gate 		goto errout;
27127c478bd9Sstevel@tonic-gate 	}
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate 	ASSERT(zonecount != 0);		/* check for leaks */
27157c478bd9Sstevel@tonic-gate 	if (zonecount + 1 > maxzones) {
27167c478bd9Sstevel@tonic-gate 		error = ENOMEM;
27177c478bd9Sstevel@tonic-gate 		goto errout;
27187c478bd9Sstevel@tonic-gate 	}
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
27217c478bd9Sstevel@tonic-gate 		error = EBUSY;
27227c478bd9Sstevel@tonic-gate 		error2 = ZE_AREMOUNTS;
27237c478bd9Sstevel@tonic-gate 		goto errout;
27247c478bd9Sstevel@tonic-gate 	}
27257c478bd9Sstevel@tonic-gate 
27267c478bd9Sstevel@tonic-gate 	/*
27277c478bd9Sstevel@tonic-gate 	 * Zone is still incomplete, but we need to drop all locks while
27287c478bd9Sstevel@tonic-gate 	 * zsched() initializes this zone's kernel process.  We
27297c478bd9Sstevel@tonic-gate 	 * optimistically add the zone to the hashtable and associated
27307c478bd9Sstevel@tonic-gate 	 * lists so a parallel zone_create() doesn't try to create the
27317c478bd9Sstevel@tonic-gate 	 * same zone.
27327c478bd9Sstevel@tonic-gate 	 */
27337c478bd9Sstevel@tonic-gate 	zonecount++;
27347c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid,
27357c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id,
27367c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
27377c478bd9Sstevel@tonic-gate 	str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP);
27387c478bd9Sstevel@tonic-gate 	(void) strcpy(str, zone->zone_name);
27397c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str,
27407c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
27417c478bd9Sstevel@tonic-gate 	/*
27427c478bd9Sstevel@tonic-gate 	 * Insert into active list.  At this point there are no 'hold's
27437c478bd9Sstevel@tonic-gate 	 * on the zone, but everyone else knows not to use it, so we can
27447c478bd9Sstevel@tonic-gate 	 * continue to use it.  zsched() will do a zone_hold() if the
27457c478bd9Sstevel@tonic-gate 	 * newproc() is successful.
27467c478bd9Sstevel@tonic-gate 	 */
27477c478bd9Sstevel@tonic-gate 	list_insert_tail(&zone_active, zone);
27487c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate 	zarg.zone = zone;
27517c478bd9Sstevel@tonic-gate 	zarg.nvlist = rctls;
27527c478bd9Sstevel@tonic-gate 	/*
27537c478bd9Sstevel@tonic-gate 	 * The process, task, and project rctls are probably wrong;
27547c478bd9Sstevel@tonic-gate 	 * we need an interface to get the default values of all rctls,
27557c478bd9Sstevel@tonic-gate 	 * and initialize zsched appropriately.  I'm not sure that that
27567c478bd9Sstevel@tonic-gate 	 * makes much of a difference, though.
27577c478bd9Sstevel@tonic-gate 	 */
27587c478bd9Sstevel@tonic-gate 	if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) {
27597c478bd9Sstevel@tonic-gate 		/*
27607c478bd9Sstevel@tonic-gate 		 * We need to undo all globally visible state.
27617c478bd9Sstevel@tonic-gate 		 */
27627c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
27637c478bd9Sstevel@tonic-gate 		list_remove(&zone_active, zone);
27647c478bd9Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyname,
27657c478bd9Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_name);
27667c478bd9Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyid,
27677c478bd9Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_id);
27687c478bd9Sstevel@tonic-gate 		ASSERT(zonecount > 1);
27697c478bd9Sstevel@tonic-gate 		zonecount--;
27707c478bd9Sstevel@tonic-gate 		goto errout;
27717c478bd9Sstevel@tonic-gate 	}
27727c478bd9Sstevel@tonic-gate 
27737c478bd9Sstevel@tonic-gate 	/*
27747c478bd9Sstevel@tonic-gate 	 * Zone creation can't fail from now on.
27757c478bd9Sstevel@tonic-gate 	 */
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 	/*
27787c478bd9Sstevel@tonic-gate 	 * Let the other lwps continue.
27797c478bd9Sstevel@tonic-gate 	 */
27807c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
27817c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
27827c478bd9Sstevel@tonic-gate 		continuelwps(pp);
27837c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
27847c478bd9Sstevel@tonic-gate 
27857c478bd9Sstevel@tonic-gate 	/*
27867c478bd9Sstevel@tonic-gate 	 * Wait for zsched to finish initializing the zone.
27877c478bd9Sstevel@tonic-gate 	 */
27887c478bd9Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_READY);
27897c478bd9Sstevel@tonic-gate 	/*
27907c478bd9Sstevel@tonic-gate 	 * The zone is fully visible, so we can let mounts progress.
27917c478bd9Sstevel@tonic-gate 	 */
27927c478bd9Sstevel@tonic-gate 	resume_mounts();
27937c478bd9Sstevel@tonic-gate 	if (rctls)
27947c478bd9Sstevel@tonic-gate 		nvlist_free(rctls);
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate 	return (zoneid);
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate errout:
27997c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
28007c478bd9Sstevel@tonic-gate 	/*
28017c478bd9Sstevel@tonic-gate 	 * Let the other lwps continue.
28027c478bd9Sstevel@tonic-gate 	 */
28037c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
28047c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
28057c478bd9Sstevel@tonic-gate 		continuelwps(pp);
28067c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	resume_mounts();
28097c478bd9Sstevel@tonic-gate 	if (rctls)
28107c478bd9Sstevel@tonic-gate 		nvlist_free(rctls);
28117c478bd9Sstevel@tonic-gate 	/*
28127c478bd9Sstevel@tonic-gate 	 * There is currently one reference to the zone, a cred_ref from
28137c478bd9Sstevel@tonic-gate 	 * zone_kcred.  To free the zone, we call crfree, which will call
28147c478bd9Sstevel@tonic-gate 	 * zone_cred_rele, which will call zone_free.
28157c478bd9Sstevel@tonic-gate 	 */
28167c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 1);	/* for zone_kcred */
28177c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_kcred->cr_ref == 1);
28187c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_ref == 0);
28197c478bd9Sstevel@tonic-gate 	zkcr = zone->zone_kcred;
28207c478bd9Sstevel@tonic-gate 	zone->zone_kcred = NULL;
28217c478bd9Sstevel@tonic-gate 	crfree(zkcr);				/* triggers call to zone_free */
28227c478bd9Sstevel@tonic-gate 	return (zone_create_error(error, error2, extended_error));
28237c478bd9Sstevel@tonic-gate }
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate /*
28267c478bd9Sstevel@tonic-gate  * Cause the zone to boot.  This is pretty simple, since we let zoneadmd do
28277c478bd9Sstevel@tonic-gate  * the heavy lifting.
28287c478bd9Sstevel@tonic-gate  */
28297c478bd9Sstevel@tonic-gate static int
28307c478bd9Sstevel@tonic-gate zone_boot(zoneid_t zoneid, const char *bootargs)
28317c478bd9Sstevel@tonic-gate {
28327c478bd9Sstevel@tonic-gate 	int err;
28337c478bd9Sstevel@tonic-gate 	zone_t *zone;
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
28367c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
28377c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
28387c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
28397c478bd9Sstevel@tonic-gate 
28407c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
28417c478bd9Sstevel@tonic-gate 	/*
28427c478bd9Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with calls to
28437c478bd9Sstevel@tonic-gate 	 * zone_shutdown, zone_destroy, etc.
28447c478bd9Sstevel@tonic-gate 	 */
28457c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
28467c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
28477c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
28487c478bd9Sstevel@tonic-gate 	}
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate 	if ((err = zone_set_bootargs(zone, bootargs)) != 0) {
28517c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
28527c478bd9Sstevel@tonic-gate 		return (set_errno(err));
28537c478bd9Sstevel@tonic-gate 	}
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
28567c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) != ZONE_IS_READY) {
28577c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
28587c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
28597c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
28607c478bd9Sstevel@tonic-gate 	}
28617c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_BOOTING);
28627c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
28657c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate 	if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) {
28687c478bd9Sstevel@tonic-gate 		zone_rele(zone);
28697c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
28707c478bd9Sstevel@tonic-gate 	}
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate 	/*
28737c478bd9Sstevel@tonic-gate 	 * Boot (starting init) might have failed, in which case the zone
28747c478bd9Sstevel@tonic-gate 	 * will go to the SHUTTING_DOWN state; an appropriate errno will
28757c478bd9Sstevel@tonic-gate 	 * be placed in zone->zone_boot_err, and so we return that.
28767c478bd9Sstevel@tonic-gate 	 */
28777c478bd9Sstevel@tonic-gate 	err = zone->zone_boot_err;
28787c478bd9Sstevel@tonic-gate 	zone_rele(zone);
28797c478bd9Sstevel@tonic-gate 	return (err ? set_errno(err) : 0);
28807c478bd9Sstevel@tonic-gate }
28817c478bd9Sstevel@tonic-gate 
28827c478bd9Sstevel@tonic-gate /*
28837c478bd9Sstevel@tonic-gate  * Kills all user processes in the zone, waiting for them all to exit
28847c478bd9Sstevel@tonic-gate  * before returning.
28857c478bd9Sstevel@tonic-gate  */
28867c478bd9Sstevel@tonic-gate static int
28877c478bd9Sstevel@tonic-gate zone_empty(zone_t *zone)
28887c478bd9Sstevel@tonic-gate {
28897c478bd9Sstevel@tonic-gate 	int waitstatus;
28907c478bd9Sstevel@tonic-gate 
28917c478bd9Sstevel@tonic-gate 	/*
28927c478bd9Sstevel@tonic-gate 	 * We need to drop zonehash_lock before killing all
28937c478bd9Sstevel@tonic-gate 	 * processes, otherwise we'll deadlock with zone_find_*
28947c478bd9Sstevel@tonic-gate 	 * which can be called from the exit path.
28957c478bd9Sstevel@tonic-gate 	 */
28967c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
28977c478bd9Sstevel@tonic-gate 	while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz,
28987c478bd9Sstevel@tonic-gate 	    ZONE_IS_EMPTY)) == -1) {
28997c478bd9Sstevel@tonic-gate 		killall(zone->zone_id);
29007c478bd9Sstevel@tonic-gate 	}
29017c478bd9Sstevel@tonic-gate 	/*
29027c478bd9Sstevel@tonic-gate 	 * return EINTR if we were signaled
29037c478bd9Sstevel@tonic-gate 	 */
29047c478bd9Sstevel@tonic-gate 	if (waitstatus == 0)
29057c478bd9Sstevel@tonic-gate 		return (EINTR);
29067c478bd9Sstevel@tonic-gate 	return (0);
29077c478bd9Sstevel@tonic-gate }
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate /*
29107c478bd9Sstevel@tonic-gate  * Systemcall to start the zone's halt sequence.  By the time this
29117c478bd9Sstevel@tonic-gate  * function successfully returns, all user processes and kernel threads
29127c478bd9Sstevel@tonic-gate  * executing in it will have exited, ZSD shutdown callbacks executed,
29137c478bd9Sstevel@tonic-gate  * and the zone status set to ZONE_IS_DOWN.
29147c478bd9Sstevel@tonic-gate  *
29157c478bd9Sstevel@tonic-gate  * It is possible that the call will interrupt itself if the caller is the
29167c478bd9Sstevel@tonic-gate  * parent of any process running in the zone, and doesn't have SIGCHLD blocked.
29177c478bd9Sstevel@tonic-gate  */
29187c478bd9Sstevel@tonic-gate static int
29197c478bd9Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid)
29207c478bd9Sstevel@tonic-gate {
29217c478bd9Sstevel@tonic-gate 	int error;
29227c478bd9Sstevel@tonic-gate 	zone_t *zone;
29237c478bd9Sstevel@tonic-gate 	zone_status_t status;
29247c478bd9Sstevel@tonic-gate 
29257c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
29267c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
29277c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
29287c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate 	/*
29317c478bd9Sstevel@tonic-gate 	 * Block mounts so that VFS_MOUNT() can get an accurate view of
29327c478bd9Sstevel@tonic-gate 	 * the zone's status with regards to ZONE_IS_SHUTTING down.
29337c478bd9Sstevel@tonic-gate 	 *
29347c478bd9Sstevel@tonic-gate 	 * e.g. NFS can fail the mount if it determines that the zone
29357c478bd9Sstevel@tonic-gate 	 * has already begun the shutdown sequence.
29367c478bd9Sstevel@tonic-gate 	 */
29377c478bd9Sstevel@tonic-gate 	if (block_mounts() == 0)
29387c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
29397c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
29407c478bd9Sstevel@tonic-gate 	/*
29417c478bd9Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
29427c478bd9Sstevel@tonic-gate 	 * calls to zone_shutdown and zone_destroy.
29437c478bd9Sstevel@tonic-gate 	 */
29447c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
29457c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
29467c478bd9Sstevel@tonic-gate 		resume_mounts();
29477c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
29487c478bd9Sstevel@tonic-gate 	}
29497c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
29507c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
29517c478bd9Sstevel@tonic-gate 	/*
29527c478bd9Sstevel@tonic-gate 	 * Fail if the zone isn't fully initialized yet.
29537c478bd9Sstevel@tonic-gate 	 */
29547c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY) {
29557c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
29567c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
29577c478bd9Sstevel@tonic-gate 		resume_mounts();
29587c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
29597c478bd9Sstevel@tonic-gate 	}
29607c478bd9Sstevel@tonic-gate 	/*
29617c478bd9Sstevel@tonic-gate 	 * If conditions required for zone_shutdown() to return have been met,
29627c478bd9Sstevel@tonic-gate 	 * return success.
29637c478bd9Sstevel@tonic-gate 	 */
29647c478bd9Sstevel@tonic-gate 	if (status >= ZONE_IS_DOWN) {
29657c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
29667c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
29677c478bd9Sstevel@tonic-gate 		resume_mounts();
29687c478bd9Sstevel@tonic-gate 		return (0);
29697c478bd9Sstevel@tonic-gate 	}
29707c478bd9Sstevel@tonic-gate 	/*
29717c478bd9Sstevel@tonic-gate 	 * If zone_shutdown() hasn't been called before, go through the motions.
29727c478bd9Sstevel@tonic-gate 	 * If it has, there's nothing to do but wait for the kernel threads to
29737c478bd9Sstevel@tonic-gate 	 * drain.
29747c478bd9Sstevel@tonic-gate 	 */
29757c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_EMPTY) {
29767c478bd9Sstevel@tonic-gate 		uint_t ntasks;
29777c478bd9Sstevel@tonic-gate 
29787c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
29797c478bd9Sstevel@tonic-gate 		if ((ntasks = zone->zone_ntasks) != 1) {
29807c478bd9Sstevel@tonic-gate 			/*
29817c478bd9Sstevel@tonic-gate 			 * There's still stuff running.
29827c478bd9Sstevel@tonic-gate 			 */
29837c478bd9Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
29847c478bd9Sstevel@tonic-gate 		}
29857c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
29867c478bd9Sstevel@tonic-gate 		if (ntasks == 1) {
29877c478bd9Sstevel@tonic-gate 			/*
29887c478bd9Sstevel@tonic-gate 			 * The only way to create another task is through
29897c478bd9Sstevel@tonic-gate 			 * zone_enter(), which will block until we drop
29907c478bd9Sstevel@tonic-gate 			 * zonehash_lock.  The zone is empty.
29917c478bd9Sstevel@tonic-gate 			 */
29927c478bd9Sstevel@tonic-gate 			if (zone->zone_kthreads == NULL) {
29937c478bd9Sstevel@tonic-gate 				/*
29947c478bd9Sstevel@tonic-gate 				 * Skip ahead to ZONE_IS_DOWN
29957c478bd9Sstevel@tonic-gate 				 */
29967c478bd9Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_DOWN);
29977c478bd9Sstevel@tonic-gate 			} else {
29987c478bd9Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_EMPTY);
29997c478bd9Sstevel@tonic-gate 			}
30007c478bd9Sstevel@tonic-gate 		}
30017c478bd9Sstevel@tonic-gate 	}
30027c478bd9Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
30037c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
30047c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
30057c478bd9Sstevel@tonic-gate 	resume_mounts();
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate 	if (error = zone_empty(zone)) {
30087c478bd9Sstevel@tonic-gate 		zone_rele(zone);
30097c478bd9Sstevel@tonic-gate 		return (set_errno(error));
30107c478bd9Sstevel@tonic-gate 	}
30117c478bd9Sstevel@tonic-gate 	/*
30127c478bd9Sstevel@tonic-gate 	 * After the zone status goes to ZONE_IS_DOWN this zone will no
30137c478bd9Sstevel@tonic-gate 	 * longer be notified of changes to the pools configuration, so
30147c478bd9Sstevel@tonic-gate 	 * in order to not end up with a stale pool pointer, we point
30157c478bd9Sstevel@tonic-gate 	 * ourselves at the default pool and remove all resource
30167c478bd9Sstevel@tonic-gate 	 * visibility.  This is especially important as the zone_t may
30177c478bd9Sstevel@tonic-gate 	 * languish on the deathrow for a very long time waiting for
30187c478bd9Sstevel@tonic-gate 	 * cred's to drain out.
30197c478bd9Sstevel@tonic-gate 	 *
30207c478bd9Sstevel@tonic-gate 	 * This rebinding of the zone can happen multiple times
30217c478bd9Sstevel@tonic-gate 	 * (presumably due to interrupted or parallel systemcalls)
30227c478bd9Sstevel@tonic-gate 	 * without any adverse effects.
30237c478bd9Sstevel@tonic-gate 	 */
30247c478bd9Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
30257c478bd9Sstevel@tonic-gate 		zone_rele(zone);
30267c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
30277c478bd9Sstevel@tonic-gate 	}
30287c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
30297c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
30307c478bd9Sstevel@tonic-gate 		zone_pool_set(zone, pool_default);
30317c478bd9Sstevel@tonic-gate 		/*
30327c478bd9Sstevel@tonic-gate 		 * The zone no longer needs to be able to see any cpus.
30337c478bd9Sstevel@tonic-gate 		 */
30347c478bd9Sstevel@tonic-gate 		zone_pset_set(zone, ZONE_PS_INVAL);
30357c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
30367c478bd9Sstevel@tonic-gate 	}
30377c478bd9Sstevel@tonic-gate 	pool_unlock();
30387c478bd9Sstevel@tonic-gate 
30397c478bd9Sstevel@tonic-gate 	/*
30407c478bd9Sstevel@tonic-gate 	 * ZSD shutdown callbacks can be executed multiple times, hence
30417c478bd9Sstevel@tonic-gate 	 * it is safe to not be holding any locks across this call.
30427c478bd9Sstevel@tonic-gate 	 */
30437c478bd9Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_SHUTDOWN);
30447c478bd9Sstevel@tonic-gate 
30457c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
30467c478bd9Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN)
30477c478bd9Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DOWN);
30487c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
30497c478bd9Sstevel@tonic-gate 
30507c478bd9Sstevel@tonic-gate 	/*
30517c478bd9Sstevel@tonic-gate 	 * Wait for kernel threads to drain.
30527c478bd9Sstevel@tonic-gate 	 */
30537c478bd9Sstevel@tonic-gate 	if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) {
30547c478bd9Sstevel@tonic-gate 		zone_rele(zone);
30557c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
30567c478bd9Sstevel@tonic-gate 	}
30577c478bd9Sstevel@tonic-gate 	zone_rele(zone);
30587c478bd9Sstevel@tonic-gate 	return (0);
30597c478bd9Sstevel@tonic-gate }
30607c478bd9Sstevel@tonic-gate 
30617c478bd9Sstevel@tonic-gate /*
30627c478bd9Sstevel@tonic-gate  * Systemcall entry point to finalize the zone halt process.  The caller
30637c478bd9Sstevel@tonic-gate  * must have already successfully callefd zone_shutdown().
30647c478bd9Sstevel@tonic-gate  *
30657c478bd9Sstevel@tonic-gate  * Upon successful completion, the zone will have been fully destroyed:
30667c478bd9Sstevel@tonic-gate  * zsched will have exited, destructor callbacks executed, and the zone
30677c478bd9Sstevel@tonic-gate  * removed from the list of active zones.
30687c478bd9Sstevel@tonic-gate  */
30697c478bd9Sstevel@tonic-gate static int
30707c478bd9Sstevel@tonic-gate zone_destroy(zoneid_t zoneid)
30717c478bd9Sstevel@tonic-gate {
30727c478bd9Sstevel@tonic-gate 	uint64_t uniqid;
30737c478bd9Sstevel@tonic-gate 	zone_t *zone;
30747c478bd9Sstevel@tonic-gate 	zone_status_t status;
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
30777c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
30787c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
30797c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
30827c478bd9Sstevel@tonic-gate 	/*
30837c478bd9Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
30847c478bd9Sstevel@tonic-gate 	 * calls to zone_destroy.
30857c478bd9Sstevel@tonic-gate 	 */
30867c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
30877c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
30887c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
30897c478bd9Sstevel@tonic-gate 	}
30907c478bd9Sstevel@tonic-gate 
30917c478bd9Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
30927c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
30937c478bd9Sstevel@tonic-gate 		return (set_errno(EBUSY));
30947c478bd9Sstevel@tonic-gate 	}
30957c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
30967c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
30977c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_DOWN) {
30987c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
30997c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
31007c478bd9Sstevel@tonic-gate 		return (set_errno(EBUSY));
31017c478bd9Sstevel@tonic-gate 	} else if (status == ZONE_IS_DOWN) {
31027c478bd9Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */
31037c478bd9Sstevel@tonic-gate 	}
31047c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
31057c478bd9Sstevel@tonic-gate 	zone_hold(zone);
31067c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
31077c478bd9Sstevel@tonic-gate 
31087c478bd9Sstevel@tonic-gate 	/*
31097c478bd9Sstevel@tonic-gate 	 * wait for zsched to exit
31107c478bd9Sstevel@tonic-gate 	 */
31117c478bd9Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_DEAD);
31127c478bd9Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_DESTROY);
31137c478bd9Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
31147c478bd9Sstevel@tonic-gate 	zone_rele(zone);
31157c478bd9Sstevel@tonic-gate 	zone = NULL;	/* potentially free'd */
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
31187c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
31197c478bd9Sstevel@tonic-gate 		boolean_t unref;
31207c478bd9Sstevel@tonic-gate 
31217c478bd9Sstevel@tonic-gate 		if ((zone = zone_find_all_by_id(zoneid)) == NULL ||
31227c478bd9Sstevel@tonic-gate 		    zone->zone_uniqid != uniqid) {
31237c478bd9Sstevel@tonic-gate 			/*
31247c478bd9Sstevel@tonic-gate 			 * The zone has gone away.  Necessary conditions
31257c478bd9Sstevel@tonic-gate 			 * are met, so we return success.
31267c478bd9Sstevel@tonic-gate 			 */
31277c478bd9Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
31287c478bd9Sstevel@tonic-gate 			return (0);
31297c478bd9Sstevel@tonic-gate 		}
31307c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
31317c478bd9Sstevel@tonic-gate 		unref = ZONE_IS_UNREF(zone);
31327c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
31337c478bd9Sstevel@tonic-gate 		if (unref) {
31347c478bd9Sstevel@tonic-gate 			/*
31357c478bd9Sstevel@tonic-gate 			 * There is only one reference to the zone -- that
31367c478bd9Sstevel@tonic-gate 			 * added when the zone was added to the hashtables --
31377c478bd9Sstevel@tonic-gate 			 * and things will remain this way until we drop
31387c478bd9Sstevel@tonic-gate 			 * zonehash_lock... we can go ahead and cleanup the
31397c478bd9Sstevel@tonic-gate 			 * zone.
31407c478bd9Sstevel@tonic-gate 			 */
31417c478bd9Sstevel@tonic-gate 			break;
31427c478bd9Sstevel@tonic-gate 		}
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) {
31457c478bd9Sstevel@tonic-gate 			/* Signaled */
31467c478bd9Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
31477c478bd9Sstevel@tonic-gate 			return (set_errno(EINTR));
31487c478bd9Sstevel@tonic-gate 		}
31497c478bd9Sstevel@tonic-gate 
31507c478bd9Sstevel@tonic-gate 	}
31517c478bd9Sstevel@tonic-gate 
31527c478bd9Sstevel@tonic-gate 	/*
31537c478bd9Sstevel@tonic-gate 	 * It is now safe to let the zone be recreated; remove it from the
31547c478bd9Sstevel@tonic-gate 	 * lists.  The memory will not be freed until the last cred
31557c478bd9Sstevel@tonic-gate 	 * reference goes away.
31567c478bd9Sstevel@tonic-gate 	 */
31577c478bd9Sstevel@tonic-gate 	ASSERT(zonecount > 1);	/* must be > 1; can't destroy global zone */
31587c478bd9Sstevel@tonic-gate 	zonecount--;
31597c478bd9Sstevel@tonic-gate 	/* remove from active list and hash tables */
31607c478bd9Sstevel@tonic-gate 	list_remove(&zone_active, zone);
31617c478bd9Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyname,
31627c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)zone->zone_name);
31637c478bd9Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyid,
31647c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id);
31657c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
31667c478bd9Sstevel@tonic-gate 
3167108322fbScarlsonj 	/*
3168108322fbScarlsonj 	 * Release the root vnode; we're not using it anymore.  Nor should any
3169108322fbScarlsonj 	 * other thread that might access it exist.
3170108322fbScarlsonj 	 */
3171108322fbScarlsonj 	if (zone->zone_rootvp != NULL) {
3172108322fbScarlsonj 		VN_RELE(zone->zone_rootvp);
3173108322fbScarlsonj 		zone->zone_rootvp = NULL;
3174108322fbScarlsonj 	}
3175108322fbScarlsonj 
31767c478bd9Sstevel@tonic-gate 	/* add to deathrow list */
31777c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_deathrow_lock);
31787c478bd9Sstevel@tonic-gate 	list_insert_tail(&zone_deathrow, zone);
31797c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_deathrow_lock);
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate 	/*
31827c478bd9Sstevel@tonic-gate 	 * Drop last reference (which was added by zsched()), this will
31837c478bd9Sstevel@tonic-gate 	 * free the zone unless there are outstanding cred references.
31847c478bd9Sstevel@tonic-gate 	 */
31857c478bd9Sstevel@tonic-gate 	zone_rele(zone);
31867c478bd9Sstevel@tonic-gate 	return (0);
31877c478bd9Sstevel@tonic-gate }
31887c478bd9Sstevel@tonic-gate 
31897c478bd9Sstevel@tonic-gate /*
31907c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_getattr(2).
31917c478bd9Sstevel@tonic-gate  */
31927c478bd9Sstevel@tonic-gate static ssize_t
31937c478bd9Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
31947c478bd9Sstevel@tonic-gate {
31957c478bd9Sstevel@tonic-gate 	size_t size;
31967c478bd9Sstevel@tonic-gate 	int error = 0, err;
31977c478bd9Sstevel@tonic-gate 	zone_t *zone;
31987c478bd9Sstevel@tonic-gate 	char *zonepath;
31997c478bd9Sstevel@tonic-gate 	zone_status_t zone_status;
32007c478bd9Sstevel@tonic-gate 	pid_t initpid;
32017c478bd9Sstevel@tonic-gate 	boolean_t global = (curproc->p_zone == global_zone);
32027c478bd9Sstevel@tonic-gate 
32037c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
32047c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
32057c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
32067c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
32077c478bd9Sstevel@tonic-gate 	}
32087c478bd9Sstevel@tonic-gate 	zone_status = zone_status_get(zone);
32097c478bd9Sstevel@tonic-gate 	if (zone_status < ZONE_IS_READY) {
32107c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
32117c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
32127c478bd9Sstevel@tonic-gate 	}
32137c478bd9Sstevel@tonic-gate 	zone_hold(zone);
32147c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate 	/*
32177c478bd9Sstevel@tonic-gate 	 * If not in the global zone, don't show information about other zones.
32187c478bd9Sstevel@tonic-gate 	 */
32197c478bd9Sstevel@tonic-gate 	if (!global && curproc->p_zone != zone) {
32207c478bd9Sstevel@tonic-gate 		zone_rele(zone);
32217c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
32227c478bd9Sstevel@tonic-gate 	}
32237c478bd9Sstevel@tonic-gate 
32247c478bd9Sstevel@tonic-gate 	switch (attr) {
32257c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_ROOT:
32267c478bd9Sstevel@tonic-gate 		if (global) {
32277c478bd9Sstevel@tonic-gate 			/*
32287c478bd9Sstevel@tonic-gate 			 * Copy the path to trim the trailing "/" (except for
32297c478bd9Sstevel@tonic-gate 			 * the global zone).
32307c478bd9Sstevel@tonic-gate 			 */
32317c478bd9Sstevel@tonic-gate 			if (zone != global_zone)
32327c478bd9Sstevel@tonic-gate 				size = zone->zone_rootpathlen - 1;
32337c478bd9Sstevel@tonic-gate 			else
32347c478bd9Sstevel@tonic-gate 				size = zone->zone_rootpathlen;
32357c478bd9Sstevel@tonic-gate 			zonepath = kmem_alloc(size, KM_SLEEP);
32367c478bd9Sstevel@tonic-gate 			bcopy(zone->zone_rootpath, zonepath, size);
32377c478bd9Sstevel@tonic-gate 			zonepath[size - 1] = '\0';
32387c478bd9Sstevel@tonic-gate 		} else {
32397c478bd9Sstevel@tonic-gate 			/*
32407c478bd9Sstevel@tonic-gate 			 * Caller is not in the global zone, just return
32417c478bd9Sstevel@tonic-gate 			 * faked-up path for current zone.
32427c478bd9Sstevel@tonic-gate 			 */
32437c478bd9Sstevel@tonic-gate 			zonepath = "/";
32447c478bd9Sstevel@tonic-gate 			size = 2;
32457c478bd9Sstevel@tonic-gate 		}
32467c478bd9Sstevel@tonic-gate 		if (bufsize > size)
32477c478bd9Sstevel@tonic-gate 			bufsize = size;
32487c478bd9Sstevel@tonic-gate 		if (buf != NULL) {
32497c478bd9Sstevel@tonic-gate 			err = copyoutstr(zonepath, buf, bufsize, NULL);
32507c478bd9Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
32517c478bd9Sstevel@tonic-gate 				error = EFAULT;
32527c478bd9Sstevel@tonic-gate 		}
32537c478bd9Sstevel@tonic-gate 		if (global)
32547c478bd9Sstevel@tonic-gate 			kmem_free(zonepath, size);
32557c478bd9Sstevel@tonic-gate 		break;
32567c478bd9Sstevel@tonic-gate 
32577c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_NAME:
32587c478bd9Sstevel@tonic-gate 		size = strlen(zone->zone_name) + 1;
32597c478bd9Sstevel@tonic-gate 		if (bufsize > size)
32607c478bd9Sstevel@tonic-gate 			bufsize = size;
32617c478bd9Sstevel@tonic-gate 		if (buf != NULL) {
32627c478bd9Sstevel@tonic-gate 			err = copyoutstr(zone->zone_name, buf, bufsize, NULL);
32637c478bd9Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
32647c478bd9Sstevel@tonic-gate 				error = EFAULT;
32657c478bd9Sstevel@tonic-gate 		}
32667c478bd9Sstevel@tonic-gate 		break;
32677c478bd9Sstevel@tonic-gate 
32687c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_STATUS:
32697c478bd9Sstevel@tonic-gate 		/*
32707c478bd9Sstevel@tonic-gate 		 * Since we're not holding zonehash_lock, the zone status
32717c478bd9Sstevel@tonic-gate 		 * may be anything; leave it up to userland to sort it out.
32727c478bd9Sstevel@tonic-gate 		 */
32737c478bd9Sstevel@tonic-gate 		size = sizeof (zone_status);
32747c478bd9Sstevel@tonic-gate 		if (bufsize > size)
32757c478bd9Sstevel@tonic-gate 			bufsize = size;
32767c478bd9Sstevel@tonic-gate 		zone_status = zone_status_get(zone);
32777c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
32787c478bd9Sstevel@tonic-gate 		    copyout(&zone_status, buf, bufsize) != 0)
32797c478bd9Sstevel@tonic-gate 			error = EFAULT;
32807c478bd9Sstevel@tonic-gate 		break;
32817c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_PRIVSET:
32827c478bd9Sstevel@tonic-gate 		size = sizeof (priv_set_t);
32837c478bd9Sstevel@tonic-gate 		if (bufsize > size)
32847c478bd9Sstevel@tonic-gate 			bufsize = size;
32857c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
32867c478bd9Sstevel@tonic-gate 		    copyout(zone->zone_privset, buf, bufsize) != 0)
32877c478bd9Sstevel@tonic-gate 			error = EFAULT;
32887c478bd9Sstevel@tonic-gate 		break;
32897c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_UNIQID:
32907c478bd9Sstevel@tonic-gate 		size = sizeof (zone->zone_uniqid);
32917c478bd9Sstevel@tonic-gate 		if (bufsize > size)
32927c478bd9Sstevel@tonic-gate 			bufsize = size;
32937c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
32947c478bd9Sstevel@tonic-gate 		    copyout(&zone->zone_uniqid, buf, bufsize) != 0)
32957c478bd9Sstevel@tonic-gate 			error = EFAULT;
32967c478bd9Sstevel@tonic-gate 		break;
32977c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_POOLID:
32987c478bd9Sstevel@tonic-gate 		{
32997c478bd9Sstevel@tonic-gate 			pool_t *pool;
33007c478bd9Sstevel@tonic-gate 			poolid_t poolid;
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate 			if (pool_lock_intr() != 0) {
33037c478bd9Sstevel@tonic-gate 				error = EINTR;
33047c478bd9Sstevel@tonic-gate 				break;
33057c478bd9Sstevel@tonic-gate 			}
33067c478bd9Sstevel@tonic-gate 			pool = zone_pool_get(zone);
33077c478bd9Sstevel@tonic-gate 			poolid = pool->pool_id;
33087c478bd9Sstevel@tonic-gate 			pool_unlock();
33097c478bd9Sstevel@tonic-gate 			size = sizeof (poolid);
33107c478bd9Sstevel@tonic-gate 			if (bufsize > size)
33117c478bd9Sstevel@tonic-gate 				bufsize = size;
33127c478bd9Sstevel@tonic-gate 			if (buf != NULL && copyout(&poolid, buf, size) != 0)
33137c478bd9Sstevel@tonic-gate 				error = EFAULT;
33147c478bd9Sstevel@tonic-gate 		}
33157c478bd9Sstevel@tonic-gate 		break;
33167c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_INITPID:
33177c478bd9Sstevel@tonic-gate 		size = sizeof (initpid);
33187c478bd9Sstevel@tonic-gate 		if (bufsize > size)
33197c478bd9Sstevel@tonic-gate 			bufsize = size;
33207c478bd9Sstevel@tonic-gate 		initpid = zone->zone_proc_initpid;
33217c478bd9Sstevel@tonic-gate 		if (initpid == -1) {
33227c478bd9Sstevel@tonic-gate 			error = ESRCH;
33237c478bd9Sstevel@tonic-gate 			break;
33247c478bd9Sstevel@tonic-gate 		}
33257c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
33267c478bd9Sstevel@tonic-gate 		    copyout(&initpid, buf, bufsize) != 0)
33277c478bd9Sstevel@tonic-gate 			error = EFAULT;
33287c478bd9Sstevel@tonic-gate 		break;
33297c478bd9Sstevel@tonic-gate 	default:
33307c478bd9Sstevel@tonic-gate 		error = EINVAL;
33317c478bd9Sstevel@tonic-gate 	}
33327c478bd9Sstevel@tonic-gate 	zone_rele(zone);
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 	if (error)
33357c478bd9Sstevel@tonic-gate 		return (set_errno(error));
33367c478bd9Sstevel@tonic-gate 	return ((ssize_t)size);
33377c478bd9Sstevel@tonic-gate }
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate /*
33407c478bd9Sstevel@tonic-gate  * Return zero if the process has at least one vnode mapped in to its
33417c478bd9Sstevel@tonic-gate  * address space which shouldn't be allowed to change zones.
33427c478bd9Sstevel@tonic-gate  */
33437c478bd9Sstevel@tonic-gate static int
33447c478bd9Sstevel@tonic-gate as_can_change_zones(void)
33457c478bd9Sstevel@tonic-gate {
33467c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
33477c478bd9Sstevel@tonic-gate 	struct seg *seg;
33487c478bd9Sstevel@tonic-gate 	struct as *as = pp->p_as;
33497c478bd9Sstevel@tonic-gate 	vnode_t *vp;
33507c478bd9Sstevel@tonic-gate 	int allow = 1;
33517c478bd9Sstevel@tonic-gate 
33527c478bd9Sstevel@tonic-gate 	ASSERT(pp->p_as != &kas);
33537c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(&as, &as->a_lock, RW_READER);
33547c478bd9Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
33557c478bd9Sstevel@tonic-gate 		/*
33567c478bd9Sstevel@tonic-gate 		 * if we can't get a backing vnode for this segment then skip
33577c478bd9Sstevel@tonic-gate 		 * it.
33587c478bd9Sstevel@tonic-gate 		 */
33597c478bd9Sstevel@tonic-gate 		vp = NULL;
33607c478bd9Sstevel@tonic-gate 		if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL)
33617c478bd9Sstevel@tonic-gate 			continue;
33627c478bd9Sstevel@tonic-gate 		if (!vn_can_change_zones(vp)) { /* bail on first match */
33637c478bd9Sstevel@tonic-gate 			allow = 0;
33647c478bd9Sstevel@tonic-gate 			break;
33657c478bd9Sstevel@tonic-gate 		}
33667c478bd9Sstevel@tonic-gate 	}
33677c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(&as, &as->a_lock);
33687c478bd9Sstevel@tonic-gate 	return (allow);
33697c478bd9Sstevel@tonic-gate }
33707c478bd9Sstevel@tonic-gate 
33717c478bd9Sstevel@tonic-gate /*
33727c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_enter().
33737c478bd9Sstevel@tonic-gate  *
33747c478bd9Sstevel@tonic-gate  * The current process is injected into said zone.  In the process
33757c478bd9Sstevel@tonic-gate  * it will change its project membership, privileges, rootdir/cwd,
33767c478bd9Sstevel@tonic-gate  * zone-wide rctls, and pool association to match those of the zone.
33777c478bd9Sstevel@tonic-gate  *
33787c478bd9Sstevel@tonic-gate  * The first zone_enter() called while the zone is in the ZONE_IS_READY
33797c478bd9Sstevel@tonic-gate  * state will transition it to ZONE_IS_RUNNING.  Processes may only
33807c478bd9Sstevel@tonic-gate  * enter a zone that is "ready" or "running".
33817c478bd9Sstevel@tonic-gate  */
33827c478bd9Sstevel@tonic-gate static int
33837c478bd9Sstevel@tonic-gate zone_enter(zoneid_t zoneid)
33847c478bd9Sstevel@tonic-gate {
33857c478bd9Sstevel@tonic-gate 	zone_t *zone;
33867c478bd9Sstevel@tonic-gate 	vnode_t *vp;
33877c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
33887c478bd9Sstevel@tonic-gate 	contract_t *ct;
33897c478bd9Sstevel@tonic-gate 	cont_process_t *ctp;
33907c478bd9Sstevel@tonic-gate 	task_t *tk, *oldtk;
33917c478bd9Sstevel@tonic-gate 	kproject_t *zone_proj0;
33927c478bd9Sstevel@tonic-gate 	cred_t *cr, *newcr;
33937c478bd9Sstevel@tonic-gate 	pool_t *oldpool, *newpool;
33947c478bd9Sstevel@tonic-gate 	sess_t *sp;
33957c478bd9Sstevel@tonic-gate 	uid_t uid;
33967c478bd9Sstevel@tonic-gate 	zone_status_t status;
33977c478bd9Sstevel@tonic-gate 	int err = 0;
33987c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
33997c478bd9Sstevel@tonic-gate 
34007c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
34017c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
34027c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
34037c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
34047c478bd9Sstevel@tonic-gate 
34057c478bd9Sstevel@tonic-gate 	/*
34067c478bd9Sstevel@tonic-gate 	 * Stop all lwps so we don't need to hold a lock to look at
34077c478bd9Sstevel@tonic-gate 	 * curproc->p_zone.  This needs to happen before we grab any
34087c478bd9Sstevel@tonic-gate 	 * locks to avoid deadlock (another lwp in the process could
34097c478bd9Sstevel@tonic-gate 	 * be waiting for the held lock).
34107c478bd9Sstevel@tonic-gate 	 */
34117c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK))
34127c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
34137c478bd9Sstevel@tonic-gate 
34147c478bd9Sstevel@tonic-gate 	/*
34157c478bd9Sstevel@tonic-gate 	 * Make sure we're not changing zones with files open or mapped in
34167c478bd9Sstevel@tonic-gate 	 * to our address space which shouldn't be changing zones.
34177c478bd9Sstevel@tonic-gate 	 */
34187c478bd9Sstevel@tonic-gate 	if (!files_can_change_zones()) {
34197c478bd9Sstevel@tonic-gate 		err = EBADF;
34207c478bd9Sstevel@tonic-gate 		goto out;
34217c478bd9Sstevel@tonic-gate 	}
34227c478bd9Sstevel@tonic-gate 	if (!as_can_change_zones()) {
34237c478bd9Sstevel@tonic-gate 		err = EFAULT;
34247c478bd9Sstevel@tonic-gate 		goto out;
34257c478bd9Sstevel@tonic-gate 	}
34267c478bd9Sstevel@tonic-gate 
34277c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
34287c478bd9Sstevel@tonic-gate 	if (pp->p_zone != global_zone) {
34297c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
34307c478bd9Sstevel@tonic-gate 		err = EINVAL;
34317c478bd9Sstevel@tonic-gate 		goto out;
34327c478bd9Sstevel@tonic-gate 	}
34337c478bd9Sstevel@tonic-gate 
34347c478bd9Sstevel@tonic-gate 	zone = zone_find_all_by_id(zoneid);
34357c478bd9Sstevel@tonic-gate 	if (zone == NULL) {
34367c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
34377c478bd9Sstevel@tonic-gate 		err = EINVAL;
34387c478bd9Sstevel@tonic-gate 		goto out;
34397c478bd9Sstevel@tonic-gate 	}
34407c478bd9Sstevel@tonic-gate 
34417c478bd9Sstevel@tonic-gate 	/*
34427c478bd9Sstevel@tonic-gate 	 * To prevent processes in a zone from holding contracts on
34437c478bd9Sstevel@tonic-gate 	 * extrazonal resources, and to avoid process contract
34447c478bd9Sstevel@tonic-gate 	 * memberships which span zones, contract holders and processes
34457c478bd9Sstevel@tonic-gate 	 * which aren't the sole members of their encapsulating process
34467c478bd9Sstevel@tonic-gate 	 * contracts are not allowed to zone_enter.
34477c478bd9Sstevel@tonic-gate 	 */
34487c478bd9Sstevel@tonic-gate 	ctp = pp->p_ct_process;
34497c478bd9Sstevel@tonic-gate 	ct = &ctp->conp_contract;
34507c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
34517c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
34527c478bd9Sstevel@tonic-gate 	if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) {
34537c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
34547c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
34557c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
34567c478bd9Sstevel@tonic-gate 		pool_unlock();
34577c478bd9Sstevel@tonic-gate 		err = EINVAL;
34587c478bd9Sstevel@tonic-gate 		goto out;
34597c478bd9Sstevel@tonic-gate 	}
34607c478bd9Sstevel@tonic-gate 
34617c478bd9Sstevel@tonic-gate 	/*
34627c478bd9Sstevel@tonic-gate 	 * Moreover, we don't allow processes whose encapsulating
34637c478bd9Sstevel@tonic-gate 	 * process contracts have inherited extrazonal contracts.
34647c478bd9Sstevel@tonic-gate 	 * While it would be easier to eliminate all process contracts
34657c478bd9Sstevel@tonic-gate 	 * with inherited contracts, we need to be able to give a
34667c478bd9Sstevel@tonic-gate 	 * restarted init (or other zone-penetrating process) its
34677c478bd9Sstevel@tonic-gate 	 * predecessor's contracts.
34687c478bd9Sstevel@tonic-gate 	 */
34697c478bd9Sstevel@tonic-gate 	if (ctp->conp_ninherited != 0) {
34707c478bd9Sstevel@tonic-gate 		contract_t *next;
34717c478bd9Sstevel@tonic-gate 		for (next = list_head(&ctp->conp_inherited); next;
34727c478bd9Sstevel@tonic-gate 		    next = list_next(&ctp->conp_inherited, next)) {
34737c478bd9Sstevel@tonic-gate 			if (contract_getzuniqid(next) != zone->zone_uniqid) {
34747c478bd9Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
34757c478bd9Sstevel@tonic-gate 				mutex_exit(&ct->ct_lock);
34767c478bd9Sstevel@tonic-gate 				mutex_exit(&zonehash_lock);
34777c478bd9Sstevel@tonic-gate 				pool_unlock();
34787c478bd9Sstevel@tonic-gate 				err = EINVAL;
34797c478bd9Sstevel@tonic-gate 				goto out;
34807c478bd9Sstevel@tonic-gate 			}
34817c478bd9Sstevel@tonic-gate 		}
34827c478bd9Sstevel@tonic-gate 	}
34837c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
34847c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
34857c478bd9Sstevel@tonic-gate 
34867c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
34877c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) {
34887c478bd9Sstevel@tonic-gate 		/*
34897c478bd9Sstevel@tonic-gate 		 * Can't join
34907c478bd9Sstevel@tonic-gate 		 */
34917c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
34927c478bd9Sstevel@tonic-gate 		err = EINVAL;
34937c478bd9Sstevel@tonic-gate 		goto out;
34947c478bd9Sstevel@tonic-gate 	}
34957c478bd9Sstevel@tonic-gate 
34967c478bd9Sstevel@tonic-gate 	/*
34977c478bd9Sstevel@tonic-gate 	 * Make sure new priv set is within the permitted set for caller
34987c478bd9Sstevel@tonic-gate 	 */
34997c478bd9Sstevel@tonic-gate 	if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) {
35007c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
35017c478bd9Sstevel@tonic-gate 		err = EPERM;
35027c478bd9Sstevel@tonic-gate 		goto out;
35037c478bd9Sstevel@tonic-gate 	}
35047c478bd9Sstevel@tonic-gate 	/*
35057c478bd9Sstevel@tonic-gate 	 * We want to momentarily drop zonehash_lock while we optimistically
35067c478bd9Sstevel@tonic-gate 	 * bind curproc to the pool it should be running in.  This is safe
35077c478bd9Sstevel@tonic-gate 	 * since the zone can't disappear (we have a hold on it).
35087c478bd9Sstevel@tonic-gate 	 */
35097c478bd9Sstevel@tonic-gate 	zone_hold(zone);
35107c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
35117c478bd9Sstevel@tonic-gate 
35127c478bd9Sstevel@tonic-gate 	/*
35137c478bd9Sstevel@tonic-gate 	 * Grab pool_lock to keep the pools configuration from changing
35147c478bd9Sstevel@tonic-gate 	 * and to stop ourselves from getting rebound to another pool
35157c478bd9Sstevel@tonic-gate 	 * until we join the zone.
35167c478bd9Sstevel@tonic-gate 	 */
35177c478bd9Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
35187c478bd9Sstevel@tonic-gate 		zone_rele(zone);
35197c478bd9Sstevel@tonic-gate 		err = EINTR;
35207c478bd9Sstevel@tonic-gate 		goto out;
35217c478bd9Sstevel@tonic-gate 	}
35227c478bd9Sstevel@tonic-gate 	ASSERT(secpolicy_pool(CRED()) == 0);
35237c478bd9Sstevel@tonic-gate 	/*
35247c478bd9Sstevel@tonic-gate 	 * Bind ourselves to the pool currently associated with the zone.
35257c478bd9Sstevel@tonic-gate 	 */
35267c478bd9Sstevel@tonic-gate 	oldpool = curproc->p_pool;
35277c478bd9Sstevel@tonic-gate 	newpool = zone_pool_get(zone);
35287c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED && newpool != oldpool &&
35297c478bd9Sstevel@tonic-gate 	    (err = pool_do_bind(newpool, P_PID, P_MYID,
35307c478bd9Sstevel@tonic-gate 	    POOL_BIND_ALL)) != 0) {
35317c478bd9Sstevel@tonic-gate 		pool_unlock();
35327c478bd9Sstevel@tonic-gate 		zone_rele(zone);
35337c478bd9Sstevel@tonic-gate 		goto out;
35347c478bd9Sstevel@tonic-gate 	}
35357c478bd9Sstevel@tonic-gate 
35367c478bd9Sstevel@tonic-gate 	/*
35377c478bd9Sstevel@tonic-gate 	 * Grab cpu_lock now; we'll need it later when we call
35387c478bd9Sstevel@tonic-gate 	 * task_join().
35397c478bd9Sstevel@tonic-gate 	 */
35407c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
35417c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
35427c478bd9Sstevel@tonic-gate 	/*
35437c478bd9Sstevel@tonic-gate 	 * Make sure the zone hasn't moved on since we dropped zonehash_lock.
35447c478bd9Sstevel@tonic-gate 	 */
35457c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
35467c478bd9Sstevel@tonic-gate 		/*
35477c478bd9Sstevel@tonic-gate 		 * Can't join anymore.
35487c478bd9Sstevel@tonic-gate 		 */
35497c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
35507c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
35517c478bd9Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED &&
35527c478bd9Sstevel@tonic-gate 		    newpool != oldpool)
35537c478bd9Sstevel@tonic-gate 			(void) pool_do_bind(oldpool, P_PID, P_MYID,
35547c478bd9Sstevel@tonic-gate 			    POOL_BIND_ALL);
35557c478bd9Sstevel@tonic-gate 		pool_unlock();
35567c478bd9Sstevel@tonic-gate 		zone_rele(zone);
35577c478bd9Sstevel@tonic-gate 		err = EINVAL;
35587c478bd9Sstevel@tonic-gate 		goto out;
35597c478bd9Sstevel@tonic-gate 	}
35607c478bd9Sstevel@tonic-gate 
35617c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
35627c478bd9Sstevel@tonic-gate 	zone_proj0 = zone->zone_zsched->p_task->tk_proj;
35637c478bd9Sstevel@tonic-gate 	/* verify that we do not exceed and task or lwp limits */
35647c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
35657c478bd9Sstevel@tonic-gate 	/* add new lwps to zone and zone's proj0 */
35667c478bd9Sstevel@tonic-gate 	zone_proj0->kpj_nlwps += pp->p_lwpcnt;
35677c478bd9Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
35687c478bd9Sstevel@tonic-gate 	/* add 1 task to zone's proj0 */
35697c478bd9Sstevel@tonic-gate 	zone_proj0->kpj_ntasks += 1;
35707c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
35717c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
35727c478bd9Sstevel@tonic-gate 
35737c478bd9Sstevel@tonic-gate 	/* remove lwps from proc's old zone and old project */
35747c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_zone->zone_nlwps_lock);
35757c478bd9Sstevel@tonic-gate 	pp->p_zone->zone_nlwps -= pp->p_lwpcnt;
35767c478bd9Sstevel@tonic-gate 	pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt;
35777c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_zone->zone_nlwps_lock);
35787c478bd9Sstevel@tonic-gate 
35797c478bd9Sstevel@tonic-gate 	/*
35807c478bd9Sstevel@tonic-gate 	 * Joining the zone cannot fail from now on.
35817c478bd9Sstevel@tonic-gate 	 *
35827c478bd9Sstevel@tonic-gate 	 * This means that a lot of the following code can be commonized and
35837c478bd9Sstevel@tonic-gate 	 * shared with zsched().
35847c478bd9Sstevel@tonic-gate 	 */
35857c478bd9Sstevel@tonic-gate 
35867c478bd9Sstevel@tonic-gate 	/*
35877c478bd9Sstevel@tonic-gate 	 * Reset the encapsulating process contract's zone.
35887c478bd9Sstevel@tonic-gate 	 */
35897c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID);
35907c478bd9Sstevel@tonic-gate 	contract_setzuniqid(ct, zone->zone_uniqid);
35917c478bd9Sstevel@tonic-gate 
35927c478bd9Sstevel@tonic-gate 	/*
35937c478bd9Sstevel@tonic-gate 	 * Create a new task and associate the process with the project keyed
35947c478bd9Sstevel@tonic-gate 	 * by (projid,zoneid).
35957c478bd9Sstevel@tonic-gate 	 *
35967c478bd9Sstevel@tonic-gate 	 * We might as well be in project 0; the global zone's projid doesn't
35977c478bd9Sstevel@tonic-gate 	 * make much sense in a zone anyhow.
35987c478bd9Sstevel@tonic-gate 	 *
35997c478bd9Sstevel@tonic-gate 	 * This also increments zone_ntasks, and returns with p_lock held.
36007c478bd9Sstevel@tonic-gate 	 */
36017c478bd9Sstevel@tonic-gate 	tk = task_create(0, zone);
36027c478bd9Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
36037c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
36047c478bd9Sstevel@tonic-gate 
36057c478bd9Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
36067c478bd9Sstevel@tonic-gate 	pp->p_zone = zone;
36077c478bd9Sstevel@tonic-gate 
36087c478bd9Sstevel@tonic-gate 	/*
36097c478bd9Sstevel@tonic-gate 	 * call RCTLOP_SET functions on this proc
36107c478bd9Sstevel@tonic-gate 	 */
36117c478bd9Sstevel@tonic-gate 	e.rcep_p.zone = zone;
36127c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
36137c478bd9Sstevel@tonic-gate 	(void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL,
36147c478bd9Sstevel@tonic-gate 	    RCD_CALLBACK);
36157c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
36167c478bd9Sstevel@tonic-gate 
36177c478bd9Sstevel@tonic-gate 	/*
36187c478bd9Sstevel@tonic-gate 	 * We don't need to hold any of zsched's locks here; not only do we know
36197c478bd9Sstevel@tonic-gate 	 * the process and zone aren't going away, we know its session isn't
36207c478bd9Sstevel@tonic-gate 	 * changing either.
36217c478bd9Sstevel@tonic-gate 	 *
36227c478bd9Sstevel@tonic-gate 	 * By joining zsched's session here, we mimic the behavior in the
36237c478bd9Sstevel@tonic-gate 	 * global zone of init's sid being the pid of sched.  We extend this
36247c478bd9Sstevel@tonic-gate 	 * to all zlogin-like zone_enter()'ing processes as well.
36257c478bd9Sstevel@tonic-gate 	 */
36267c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
36277c478bd9Sstevel@tonic-gate 	sp = zone->zone_zsched->p_sessp;
36287c478bd9Sstevel@tonic-gate 	SESS_HOLD(sp);
36297c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
36307c478bd9Sstevel@tonic-gate 	pgexit(pp);
36317c478bd9Sstevel@tonic-gate 	SESS_RELE(pp->p_sessp);
36327c478bd9Sstevel@tonic-gate 	pp->p_sessp = sp;
36337c478bd9Sstevel@tonic-gate 	pgjoin(pp, zone->zone_zsched->p_pidp);
36347c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
36357c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
36367c478bd9Sstevel@tonic-gate 
36377c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
36387c478bd9Sstevel@tonic-gate 	/*
36397c478bd9Sstevel@tonic-gate 	 * We're firmly in the zone; let pools progress.
36407c478bd9Sstevel@tonic-gate 	 */
36417c478bd9Sstevel@tonic-gate 	pool_unlock();
36427c478bd9Sstevel@tonic-gate 	task_rele(oldtk);
36437c478bd9Sstevel@tonic-gate 	/*
36447c478bd9Sstevel@tonic-gate 	 * We don't need to retain a hold on the zone since we already
36457c478bd9Sstevel@tonic-gate 	 * incremented zone_ntasks, so the zone isn't going anywhere.
36467c478bd9Sstevel@tonic-gate 	 */
36477c478bd9Sstevel@tonic-gate 	zone_rele(zone);
36487c478bd9Sstevel@tonic-gate 
36497c478bd9Sstevel@tonic-gate 	/*
36507c478bd9Sstevel@tonic-gate 	 * Chroot
36517c478bd9Sstevel@tonic-gate 	 */
36527c478bd9Sstevel@tonic-gate 	vp = zone->zone_rootvp;
36537c478bd9Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_cdir, pp);
36547c478bd9Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_rdir, pp);
36557c478bd9Sstevel@tonic-gate 
36567c478bd9Sstevel@tonic-gate 	/*
36577c478bd9Sstevel@tonic-gate 	 * Change process credentials
36587c478bd9Sstevel@tonic-gate 	 */
36597c478bd9Sstevel@tonic-gate 	newcr = cralloc();
36607c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
36617c478bd9Sstevel@tonic-gate 	cr = pp->p_cred;
36627c478bd9Sstevel@tonic-gate 	crcopy_to(cr, newcr);
36637c478bd9Sstevel@tonic-gate 	crsetzone(newcr, zone);
36647c478bd9Sstevel@tonic-gate 	pp->p_cred = newcr;
36657c478bd9Sstevel@tonic-gate 
36667c478bd9Sstevel@tonic-gate 	/*
36677c478bd9Sstevel@tonic-gate 	 * Restrict all process privilege sets to zone limit
36687c478bd9Sstevel@tonic-gate 	 */
36697c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(newcr));
36707c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(newcr));
36717c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(newcr));
36727c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(newcr));
36737c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
36747c478bd9Sstevel@tonic-gate 	crset(pp, newcr);
36757c478bd9Sstevel@tonic-gate 
36767c478bd9Sstevel@tonic-gate 	/*
36777c478bd9Sstevel@tonic-gate 	 * Adjust upcount to reflect zone entry.
36787c478bd9Sstevel@tonic-gate 	 */
36797c478bd9Sstevel@tonic-gate 	uid = crgetruid(newcr);
36807c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
36817c478bd9Sstevel@tonic-gate 	upcount_dec(uid, GLOBAL_ZONEID);
36827c478bd9Sstevel@tonic-gate 	upcount_inc(uid, zoneid);
36837c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
36847c478bd9Sstevel@tonic-gate 
36857c478bd9Sstevel@tonic-gate 	/*
36867c478bd9Sstevel@tonic-gate 	 * Set up core file path and content.
36877c478bd9Sstevel@tonic-gate 	 */
36887c478bd9Sstevel@tonic-gate 	set_core_defaults();
36897c478bd9Sstevel@tonic-gate 
36907c478bd9Sstevel@tonic-gate out:
36917c478bd9Sstevel@tonic-gate 	/*
36927c478bd9Sstevel@tonic-gate 	 * Let the other lwps continue.
36937c478bd9Sstevel@tonic-gate 	 */
36947c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
36957c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
36967c478bd9Sstevel@tonic-gate 		continuelwps(pp);
36977c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
36987c478bd9Sstevel@tonic-gate 
36997c478bd9Sstevel@tonic-gate 	return (err != 0 ? set_errno(err) : 0);
37007c478bd9Sstevel@tonic-gate }
37017c478bd9Sstevel@tonic-gate 
37027c478bd9Sstevel@tonic-gate /*
37037c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_list(2).
37047c478bd9Sstevel@tonic-gate  *
37057c478bd9Sstevel@tonic-gate  * Processes running in a (non-global) zone only see themselves.
37067c478bd9Sstevel@tonic-gate  */
37077c478bd9Sstevel@tonic-gate static int
37087c478bd9Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones)
37097c478bd9Sstevel@tonic-gate {
37107c478bd9Sstevel@tonic-gate 	zoneid_t *zoneids;
37117c478bd9Sstevel@tonic-gate 	zone_t *zone;
37127c478bd9Sstevel@tonic-gate 	uint_t user_nzones, real_nzones;
37137c478bd9Sstevel@tonic-gate 	int error = 0;
37147c478bd9Sstevel@tonic-gate 	uint_t i;
37157c478bd9Sstevel@tonic-gate 
37167c478bd9Sstevel@tonic-gate 	if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0)
37177c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
37187c478bd9Sstevel@tonic-gate 
37197c478bd9Sstevel@tonic-gate 	if (curproc->p_zone != global_zone) {
37207c478bd9Sstevel@tonic-gate 		/* just return current zone */
37217c478bd9Sstevel@tonic-gate 		real_nzones = 1;
37227c478bd9Sstevel@tonic-gate 		zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP);
37237c478bd9Sstevel@tonic-gate 		zoneids[0] = curproc->p_zone->zone_id;
37247c478bd9Sstevel@tonic-gate 	} else {
37257c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
37267c478bd9Sstevel@tonic-gate 		real_nzones = zonecount;
37277c478bd9Sstevel@tonic-gate 		if (real_nzones) {
37287c478bd9Sstevel@tonic-gate 			zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t),
37297c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
37307c478bd9Sstevel@tonic-gate 			i = 0;
37317c478bd9Sstevel@tonic-gate 			for (zone = list_head(&zone_active); zone != NULL;
37327c478bd9Sstevel@tonic-gate 			    zone = list_next(&zone_active, zone))
37337c478bd9Sstevel@tonic-gate 				zoneids[i++] = zone->zone_id;
37347c478bd9Sstevel@tonic-gate 			ASSERT(i == real_nzones);
37357c478bd9Sstevel@tonic-gate 		}
37367c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
37377c478bd9Sstevel@tonic-gate 	}
37387c478bd9Sstevel@tonic-gate 
37397c478bd9Sstevel@tonic-gate 	if (user_nzones > real_nzones)
37407c478bd9Sstevel@tonic-gate 		user_nzones = real_nzones;
37417c478bd9Sstevel@tonic-gate 
37427c478bd9Sstevel@tonic-gate 	if (copyout(&real_nzones, numzones, sizeof (uint_t)) != 0)
37437c478bd9Sstevel@tonic-gate 		error = EFAULT;
37447c478bd9Sstevel@tonic-gate 	else if (zoneidlist != NULL && user_nzones != 0) {
37457c478bd9Sstevel@tonic-gate 		if (copyout(zoneids, zoneidlist,
37467c478bd9Sstevel@tonic-gate 		    user_nzones * sizeof (zoneid_t)) != 0)
37477c478bd9Sstevel@tonic-gate 			error = EFAULT;
37487c478bd9Sstevel@tonic-gate 	}
37497c478bd9Sstevel@tonic-gate 
37507c478bd9Sstevel@tonic-gate 	if (real_nzones)
37517c478bd9Sstevel@tonic-gate 		kmem_free(zoneids, real_nzones * sizeof (zoneid_t));
37527c478bd9Sstevel@tonic-gate 
37537c478bd9Sstevel@tonic-gate 	if (error)
37547c478bd9Sstevel@tonic-gate 		return (set_errno(error));
37557c478bd9Sstevel@tonic-gate 	else
37567c478bd9Sstevel@tonic-gate 		return (0);
37577c478bd9Sstevel@tonic-gate }
37587c478bd9Sstevel@tonic-gate 
37597c478bd9Sstevel@tonic-gate /*
37607c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_lookup(2).
37617c478bd9Sstevel@tonic-gate  *
37627c478bd9Sstevel@tonic-gate  * Non-global zones are only able to see themselves.
37637c478bd9Sstevel@tonic-gate  */
37647c478bd9Sstevel@tonic-gate static zoneid_t
37657c478bd9Sstevel@tonic-gate zone_lookup(const char *zone_name)
37667c478bd9Sstevel@tonic-gate {
37677c478bd9Sstevel@tonic-gate 	char *kname;
37687c478bd9Sstevel@tonic-gate 	zone_t *zone;
37697c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
37707c478bd9Sstevel@tonic-gate 	int err;
37717c478bd9Sstevel@tonic-gate 
37727c478bd9Sstevel@tonic-gate 	if (zone_name == NULL) {
37737c478bd9Sstevel@tonic-gate 		/* return caller's zone id */
37747c478bd9Sstevel@tonic-gate 		return (getzoneid());
37757c478bd9Sstevel@tonic-gate 	}
37767c478bd9Sstevel@tonic-gate 
37777c478bd9Sstevel@tonic-gate 	kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
37787c478bd9Sstevel@tonic-gate 	if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) {
37797c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
37807c478bd9Sstevel@tonic-gate 		return (set_errno(err));
37817c478bd9Sstevel@tonic-gate 	}
37827c478bd9Sstevel@tonic-gate 
37837c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
37847c478bd9Sstevel@tonic-gate 	zone = zone_find_all_by_name(kname);
37857c478bd9Sstevel@tonic-gate 	kmem_free(kname, ZONENAME_MAX);
37867c478bd9Sstevel@tonic-gate 	if (zone == NULL || zone_status_get(zone) < ZONE_IS_READY ||
37877c478bd9Sstevel@tonic-gate 	    (curproc->p_zone != global_zone && curproc->p_zone != zone)) {
37887c478bd9Sstevel@tonic-gate 		/* in non-global zone, can only lookup own name */
37897c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
37907c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
37917c478bd9Sstevel@tonic-gate 	}
37927c478bd9Sstevel@tonic-gate 	zoneid = zone->zone_id;
37937c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
37947c478bd9Sstevel@tonic-gate 	return (zoneid);
37957c478bd9Sstevel@tonic-gate }
37967c478bd9Sstevel@tonic-gate 
37977c478bd9Sstevel@tonic-gate /* ARGSUSED */
37987c478bd9Sstevel@tonic-gate long
3799*fa9e4066Sahrens zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4)
38007c478bd9Sstevel@tonic-gate {
38017c478bd9Sstevel@tonic-gate 	zone_def zs;
38027c478bd9Sstevel@tonic-gate 
38037c478bd9Sstevel@tonic-gate 	switch (cmd) {
38047c478bd9Sstevel@tonic-gate 	case ZONE_CREATE:
38057c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
38067c478bd9Sstevel@tonic-gate 			if (copyin(arg1, &zs, sizeof (zone_def))) {
38077c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
38087c478bd9Sstevel@tonic-gate 			}
38097c478bd9Sstevel@tonic-gate 		} else {
38107c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
38117c478bd9Sstevel@tonic-gate 			zone_def32 zs32;
38127c478bd9Sstevel@tonic-gate 
38137c478bd9Sstevel@tonic-gate 			if (copyin(arg1, &zs32, sizeof (zone_def32))) {
38147c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
38157c478bd9Sstevel@tonic-gate 			}
38167c478bd9Sstevel@tonic-gate 			zs.zone_name =
38177c478bd9Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_name;
38187c478bd9Sstevel@tonic-gate 			zs.zone_root =
38197c478bd9Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_root;
38207c478bd9Sstevel@tonic-gate 			zs.zone_privs =
38217c478bd9Sstevel@tonic-gate 			    (const struct priv_set *)
38227c478bd9Sstevel@tonic-gate 			    (unsigned long)zs32.zone_privs;
38237c478bd9Sstevel@tonic-gate 			zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf;
38247c478bd9Sstevel@tonic-gate 			zs.rctlbufsz = zs32.rctlbufsz;
3825*fa9e4066Sahrens 			zs.zfsbuf = (caddr_t)(unsigned long)zs32.zfsbuf;
3826*fa9e4066Sahrens 			zs.zfsbufsz = zs32.zfsbufsz;
38277c478bd9Sstevel@tonic-gate 			zs.extended_error =
38287c478bd9Sstevel@tonic-gate 			    (int *)(unsigned long)zs32.extended_error;
38297c478bd9Sstevel@tonic-gate #else
38307c478bd9Sstevel@tonic-gate 			panic("get_udatamodel() returned bogus result\n");
38317c478bd9Sstevel@tonic-gate #endif
38327c478bd9Sstevel@tonic-gate 		}
38337c478bd9Sstevel@tonic-gate 
38347c478bd9Sstevel@tonic-gate 		return (zone_create(zs.zone_name, zs.zone_root,
38357c478bd9Sstevel@tonic-gate 			zs.zone_privs, (caddr_t)zs.rctlbuf, zs.rctlbufsz,
3836*fa9e4066Sahrens 			(caddr_t)zs.zfsbuf, zs.zfsbufsz,
38377c478bd9Sstevel@tonic-gate 			zs.extended_error));
38387c478bd9Sstevel@tonic-gate 	case ZONE_BOOT:
38397c478bd9Sstevel@tonic-gate 		return (zone_boot((zoneid_t)(uintptr_t)arg1,
38407c478bd9Sstevel@tonic-gate 		    (const char *)arg2));
38417c478bd9Sstevel@tonic-gate 	case ZONE_DESTROY:
38427c478bd9Sstevel@tonic-gate 		return (zone_destroy((zoneid_t)(uintptr_t)arg1));
38437c478bd9Sstevel@tonic-gate 	case ZONE_GETATTR:
38447c478bd9Sstevel@tonic-gate 		return (zone_getattr((zoneid_t)(uintptr_t)arg1,
38457c478bd9Sstevel@tonic-gate 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
38467c478bd9Sstevel@tonic-gate 	case ZONE_ENTER:
38477c478bd9Sstevel@tonic-gate 		return (zone_enter((zoneid_t)(uintptr_t)arg1));
38487c478bd9Sstevel@tonic-gate 	case ZONE_LIST:
38497c478bd9Sstevel@tonic-gate 		return (zone_list((zoneid_t *)arg1, (uint_t *)arg2));
38507c478bd9Sstevel@tonic-gate 	case ZONE_SHUTDOWN:
38517c478bd9Sstevel@tonic-gate 		return (zone_shutdown((zoneid_t)(uintptr_t)arg1));
38527c478bd9Sstevel@tonic-gate 	case ZONE_LOOKUP:
38537c478bd9Sstevel@tonic-gate 		return (zone_lookup((const char *)arg1));
38547c478bd9Sstevel@tonic-gate 	default:
38557c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
38567c478bd9Sstevel@tonic-gate 	}
38577c478bd9Sstevel@tonic-gate }
38587c478bd9Sstevel@tonic-gate 
38597c478bd9Sstevel@tonic-gate struct zarg {
38607c478bd9Sstevel@tonic-gate 	zone_t *zone;
38617c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t arg;
38627c478bd9Sstevel@tonic-gate };
38637c478bd9Sstevel@tonic-gate 
38647c478bd9Sstevel@tonic-gate static int
38657c478bd9Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp)
38667c478bd9Sstevel@tonic-gate {
38677c478bd9Sstevel@tonic-gate 	char *buf;
38687c478bd9Sstevel@tonic-gate 	size_t buflen;
38697c478bd9Sstevel@tonic-gate 	int error;
38707c478bd9Sstevel@tonic-gate 
38717c478bd9Sstevel@tonic-gate 	buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name);
38727c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(buflen, KM_SLEEP);
38737c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name);
38747c478bd9Sstevel@tonic-gate 	error = door_ki_open(buf, doorp);
38757c478bd9Sstevel@tonic-gate 	kmem_free(buf, buflen);
38767c478bd9Sstevel@tonic-gate 	return (error);
38777c478bd9Sstevel@tonic-gate }
38787c478bd9Sstevel@tonic-gate 
38797c478bd9Sstevel@tonic-gate static void
38807c478bd9Sstevel@tonic-gate zone_release_door(door_handle_t *doorp)
38817c478bd9Sstevel@tonic-gate {
38827c478bd9Sstevel@tonic-gate 	door_ki_rele(*doorp);
38837c478bd9Sstevel@tonic-gate 	*doorp = NULL;
38847c478bd9Sstevel@tonic-gate }
38857c478bd9Sstevel@tonic-gate 
38867c478bd9Sstevel@tonic-gate static void
38877c478bd9Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp)
38887c478bd9Sstevel@tonic-gate {
38897c478bd9Sstevel@tonic-gate 	door_handle_t door = NULL;
38907c478bd9Sstevel@tonic-gate 	door_arg_t darg, save_arg;
38917c478bd9Sstevel@tonic-gate 	char *zone_name;
38927c478bd9Sstevel@tonic-gate 	size_t zone_namelen;
38937c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
38947c478bd9Sstevel@tonic-gate 	zone_t *zone;
38957c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t arg;
38967c478bd9Sstevel@tonic-gate 	uint64_t uniqid;
38977c478bd9Sstevel@tonic-gate 	size_t size;
38987c478bd9Sstevel@tonic-gate 	int error;
38997c478bd9Sstevel@tonic-gate 	int retry;
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate 	zone = zargp->zone;
39027c478bd9Sstevel@tonic-gate 	arg = zargp->arg;
39037c478bd9Sstevel@tonic-gate 	kmem_free(zargp, sizeof (*zargp));
39047c478bd9Sstevel@tonic-gate 
39057c478bd9Sstevel@tonic-gate 	zone_namelen = strlen(zone->zone_name) + 1;
39067c478bd9Sstevel@tonic-gate 	zone_name = kmem_alloc(zone_namelen, KM_SLEEP);
39077c478bd9Sstevel@tonic-gate 	bcopy(zone->zone_name, zone_name, zone_namelen);
39087c478bd9Sstevel@tonic-gate 	zoneid = zone->zone_id;
39097c478bd9Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
39107c478bd9Sstevel@tonic-gate 	/*
39117c478bd9Sstevel@tonic-gate 	 * zoneadmd may be down, but at least we can empty out the zone.
39127c478bd9Sstevel@tonic-gate 	 * We can ignore the return value of zone_empty() since we're called
39137c478bd9Sstevel@tonic-gate 	 * from a kernel thread and know we won't be delivered any signals.
39147c478bd9Sstevel@tonic-gate 	 */
39157c478bd9Sstevel@tonic-gate 	ASSERT(curproc == &p0);
39167c478bd9Sstevel@tonic-gate 	(void) zone_empty(zone);
39177c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY);
39187c478bd9Sstevel@tonic-gate 	zone_rele(zone);
39197c478bd9Sstevel@tonic-gate 
39207c478bd9Sstevel@tonic-gate 	size = sizeof (arg);
39217c478bd9Sstevel@tonic-gate 	darg.rbuf = (char *)&arg;
39227c478bd9Sstevel@tonic-gate 	darg.data_ptr = (char *)&arg;
39237c478bd9Sstevel@tonic-gate 	darg.rsize = size;
39247c478bd9Sstevel@tonic-gate 	darg.data_size = size;
39257c478bd9Sstevel@tonic-gate 	darg.desc_ptr = NULL;
39267c478bd9Sstevel@tonic-gate 	darg.desc_num = 0;
39277c478bd9Sstevel@tonic-gate 
39287c478bd9Sstevel@tonic-gate 	save_arg = darg;
39297c478bd9Sstevel@tonic-gate 	/*
39307c478bd9Sstevel@tonic-gate 	 * Since we're not holding a reference to the zone, any number of
39317c478bd9Sstevel@tonic-gate 	 * things can go wrong, including the zone disappearing before we get a
39327c478bd9Sstevel@tonic-gate 	 * chance to talk to zoneadmd.
39337c478bd9Sstevel@tonic-gate 	 */
39347c478bd9Sstevel@tonic-gate 	for (retry = 0; /* forever */; retry++) {
39357c478bd9Sstevel@tonic-gate 		if (door == NULL &&
39367c478bd9Sstevel@tonic-gate 		    (error = zone_lookup_door(zone_name, &door)) != 0) {
39377c478bd9Sstevel@tonic-gate 			goto next;
39387c478bd9Sstevel@tonic-gate 		}
39397c478bd9Sstevel@tonic-gate 		ASSERT(door != NULL);
39407c478bd9Sstevel@tonic-gate 
39417c478bd9Sstevel@tonic-gate 		if ((error = door_ki_upcall(door, &darg)) == 0) {
39427c478bd9Sstevel@tonic-gate 			break;
39437c478bd9Sstevel@tonic-gate 		}
39447c478bd9Sstevel@tonic-gate 		switch (error) {
39457c478bd9Sstevel@tonic-gate 		case EINTR:
39467c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
39477c478bd9Sstevel@tonic-gate 		case EAGAIN:	/* process may be forking */
39487c478bd9Sstevel@tonic-gate 			/*
39497c478bd9Sstevel@tonic-gate 			 * Back off for a bit
39507c478bd9Sstevel@tonic-gate 			 */
39517c478bd9Sstevel@tonic-gate 			break;
39527c478bd9Sstevel@tonic-gate 		case EBADF:
39537c478bd9Sstevel@tonic-gate 			zone_release_door(&door);
39547c478bd9Sstevel@tonic-gate 			if (zone_lookup_door(zone_name, &door) != 0) {
39557c478bd9Sstevel@tonic-gate 				/*
39567c478bd9Sstevel@tonic-gate 				 * zoneadmd may be dead, but it may come back to
39577c478bd9Sstevel@tonic-gate 				 * life later.
39587c478bd9Sstevel@tonic-gate 				 */
39597c478bd9Sstevel@tonic-gate 				break;
39607c478bd9Sstevel@tonic-gate 			}
39617c478bd9Sstevel@tonic-gate 			break;
39627c478bd9Sstevel@tonic-gate 		default:
39637c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
39647c478bd9Sstevel@tonic-gate 			    "zone_ki_call_zoneadmd: door_ki_upcall error %d\n",
39657c478bd9Sstevel@tonic-gate 			    error);
39667c478bd9Sstevel@tonic-gate 			goto out;
39677c478bd9Sstevel@tonic-gate 		}
39687c478bd9Sstevel@tonic-gate next:
39697c478bd9Sstevel@tonic-gate 		/*
39707c478bd9Sstevel@tonic-gate 		 * If this isn't the same zone_t that we originally had in mind,
39717c478bd9Sstevel@tonic-gate 		 * then this is the same as if two kadmin requests come in at
39727c478bd9Sstevel@tonic-gate 		 * the same time: the first one wins.  This means we lose, so we
39737c478bd9Sstevel@tonic-gate 		 * bail.
39747c478bd9Sstevel@tonic-gate 		 */
39757c478bd9Sstevel@tonic-gate 		if ((zone = zone_find_by_id(zoneid)) == NULL) {
39767c478bd9Sstevel@tonic-gate 			/*
39777c478bd9Sstevel@tonic-gate 			 * Problem is solved.
39787c478bd9Sstevel@tonic-gate 			 */
39797c478bd9Sstevel@tonic-gate 			break;
39807c478bd9Sstevel@tonic-gate 		}
39817c478bd9Sstevel@tonic-gate 		if (zone->zone_uniqid != uniqid) {
39827c478bd9Sstevel@tonic-gate 			/*
39837c478bd9Sstevel@tonic-gate 			 * zoneid recycled
39847c478bd9Sstevel@tonic-gate 			 */
39857c478bd9Sstevel@tonic-gate 			zone_rele(zone);
39867c478bd9Sstevel@tonic-gate 			break;
39877c478bd9Sstevel@tonic-gate 		}
39887c478bd9Sstevel@tonic-gate 		/*
39897c478bd9Sstevel@tonic-gate 		 * We could zone_status_timedwait(), but there doesn't seem to
39907c478bd9Sstevel@tonic-gate 		 * be much point in doing that (plus, it would mean that
39917c478bd9Sstevel@tonic-gate 		 * zone_free() isn't called until this thread exits).
39927c478bd9Sstevel@tonic-gate 		 */
39937c478bd9Sstevel@tonic-gate 		zone_rele(zone);
39947c478bd9Sstevel@tonic-gate 		delay(hz);
39957c478bd9Sstevel@tonic-gate 		darg = save_arg;
39967c478bd9Sstevel@tonic-gate 	}
39977c478bd9Sstevel@tonic-gate out:
39987c478bd9Sstevel@tonic-gate 	if (door != NULL) {
39997c478bd9Sstevel@tonic-gate 		zone_release_door(&door);
40007c478bd9Sstevel@tonic-gate 	}
40017c478bd9Sstevel@tonic-gate 	kmem_free(zone_name, zone_namelen);
40027c478bd9Sstevel@tonic-gate 	thread_exit();
40037c478bd9Sstevel@tonic-gate }
40047c478bd9Sstevel@tonic-gate 
40057c478bd9Sstevel@tonic-gate /*
40067c478bd9Sstevel@tonic-gate  * Entry point for uadmin() to tell the zone to go away or reboot.  The caller
40077c478bd9Sstevel@tonic-gate  * is a process in the zone to be modified.
40087c478bd9Sstevel@tonic-gate  *
40097c478bd9Sstevel@tonic-gate  * In order to shutdown the zone, we will hand off control to zoneadmd
40107c478bd9Sstevel@tonic-gate  * (running in the global zone) via a door.  We do a half-hearted job at
40117c478bd9Sstevel@tonic-gate  * killing all processes in the zone, create a kernel thread to contact
40127c478bd9Sstevel@tonic-gate  * zoneadmd, and make note of the "uniqid" of the zone.  The uniqid is
40137c478bd9Sstevel@tonic-gate  * a form of generation number used to let zoneadmd (as well as
40147c478bd9Sstevel@tonic-gate  * zone_destroy()) know exactly which zone they're re talking about.
40157c478bd9Sstevel@tonic-gate  */
40167c478bd9Sstevel@tonic-gate int
40177c478bd9Sstevel@tonic-gate zone_uadmin(int cmd, int fcn, cred_t *credp)
40187c478bd9Sstevel@tonic-gate {
40197c478bd9Sstevel@tonic-gate 	struct zarg *zargp;
40207c478bd9Sstevel@tonic-gate 	zone_cmd_t zcmd;
40217c478bd9Sstevel@tonic-gate 	zone_t *zone;
40227c478bd9Sstevel@tonic-gate 
40237c478bd9Sstevel@tonic-gate 	zone = curproc->p_zone;
40247c478bd9Sstevel@tonic-gate 	ASSERT(getzoneid() != GLOBAL_ZONEID);
40257c478bd9Sstevel@tonic-gate 
40267c478bd9Sstevel@tonic-gate 	switch (cmd) {
40277c478bd9Sstevel@tonic-gate 	case A_SHUTDOWN:
40287c478bd9Sstevel@tonic-gate 		switch (fcn) {
40297c478bd9Sstevel@tonic-gate 		case AD_HALT:
40307c478bd9Sstevel@tonic-gate 		case AD_POWEROFF:
40317c478bd9Sstevel@tonic-gate 			zcmd = Z_HALT;
40327c478bd9Sstevel@tonic-gate 			break;
40337c478bd9Sstevel@tonic-gate 		case AD_BOOT:
40347c478bd9Sstevel@tonic-gate 			zcmd = Z_REBOOT;
40357c478bd9Sstevel@tonic-gate 			break;
40367c478bd9Sstevel@tonic-gate 		case AD_IBOOT:
40377c478bd9Sstevel@tonic-gate 		case AD_SBOOT:
40387c478bd9Sstevel@tonic-gate 		case AD_SIBOOT:
40397c478bd9Sstevel@tonic-gate 		case AD_NOSYNC:
40407c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
40417c478bd9Sstevel@tonic-gate 		default:
40427c478bd9Sstevel@tonic-gate 			return (EINVAL);
40437c478bd9Sstevel@tonic-gate 		}
40447c478bd9Sstevel@tonic-gate 		break;
40457c478bd9Sstevel@tonic-gate 	case A_REBOOT:
40467c478bd9Sstevel@tonic-gate 		zcmd = Z_REBOOT;
40477c478bd9Sstevel@tonic-gate 		break;
40487c478bd9Sstevel@tonic-gate 	case A_FTRACE:
40497c478bd9Sstevel@tonic-gate 	case A_REMOUNT:
40507c478bd9Sstevel@tonic-gate 	case A_FREEZE:
40517c478bd9Sstevel@tonic-gate 	case A_DUMP:
40527c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
40537c478bd9Sstevel@tonic-gate 	default:
40547c478bd9Sstevel@tonic-gate 		ASSERT(cmd != A_SWAPCTL);	/* handled by uadmin() */
40557c478bd9Sstevel@tonic-gate 		return (EINVAL);
40567c478bd9Sstevel@tonic-gate 	}
40577c478bd9Sstevel@tonic-gate 
40587c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_admin(credp, B_FALSE))
40597c478bd9Sstevel@tonic-gate 		return (EPERM);
40607c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
40617c478bd9Sstevel@tonic-gate 	/*
40627c478bd9Sstevel@tonic-gate 	 * zone_status can't be ZONE_IS_EMPTY or higher since curproc
40637c478bd9Sstevel@tonic-gate 	 * is in the zone.
40647c478bd9Sstevel@tonic-gate 	 */
40657c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY);
40667c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) > ZONE_IS_RUNNING) {
40677c478bd9Sstevel@tonic-gate 		/*
40687c478bd9Sstevel@tonic-gate 		 * This zone is already on its way down.
40697c478bd9Sstevel@tonic-gate 		 */
40707c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
40717c478bd9Sstevel@tonic-gate 		return (0);
40727c478bd9Sstevel@tonic-gate 	}
40737c478bd9Sstevel@tonic-gate 	/*
40747c478bd9Sstevel@tonic-gate 	 * Prevent future zone_enter()s
40757c478bd9Sstevel@tonic-gate 	 */
40767c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
40777c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
40787c478bd9Sstevel@tonic-gate 
40797c478bd9Sstevel@tonic-gate 	/*
40807c478bd9Sstevel@tonic-gate 	 * Kill everyone now and call zoneadmd later.
40817c478bd9Sstevel@tonic-gate 	 * zone_ki_call_zoneadmd() will do a more thorough job of this
40827c478bd9Sstevel@tonic-gate 	 * later.
40837c478bd9Sstevel@tonic-gate 	 */
40847c478bd9Sstevel@tonic-gate 	killall(zone->zone_id);
40857c478bd9Sstevel@tonic-gate 	/*
40867c478bd9Sstevel@tonic-gate 	 * Now, create the thread to contact zoneadmd and do the rest of the
40877c478bd9Sstevel@tonic-gate 	 * work.  This thread can't be created in our zone otherwise
40887c478bd9Sstevel@tonic-gate 	 * zone_destroy() would deadlock.
40897c478bd9Sstevel@tonic-gate 	 */
40907c478bd9Sstevel@tonic-gate 	zargp = kmem_alloc(sizeof (*zargp), KM_SLEEP);
40917c478bd9Sstevel@tonic-gate 	zargp->arg.cmd = zcmd;
40927c478bd9Sstevel@tonic-gate 	zargp->arg.uniqid = zone->zone_uniqid;
40937c478bd9Sstevel@tonic-gate 	(void) strcpy(zargp->arg.locale, "C");
40947c478bd9Sstevel@tonic-gate 	zone_hold(zargp->zone = zone);
40957c478bd9Sstevel@tonic-gate 
40967c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0,
40977c478bd9Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
40987c478bd9Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
40997c478bd9Sstevel@tonic-gate 
41007c478bd9Sstevel@tonic-gate 	return (EINVAL);
41017c478bd9Sstevel@tonic-gate }
41027c478bd9Sstevel@tonic-gate 
41037c478bd9Sstevel@tonic-gate /*
41047c478bd9Sstevel@tonic-gate  * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's
41057c478bd9Sstevel@tonic-gate  * status to ZONE_IS_SHUTTING_DOWN.
41067c478bd9Sstevel@tonic-gate  */
41077c478bd9Sstevel@tonic-gate void
41087c478bd9Sstevel@tonic-gate zone_shutdown_global(void)
41097c478bd9Sstevel@tonic-gate {
41107c478bd9Sstevel@tonic-gate 	ASSERT(curproc->p_zone == global_zone);
41117c478bd9Sstevel@tonic-gate 
41127c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
41137c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING);
41147c478bd9Sstevel@tonic-gate 	zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN);
41157c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
41167c478bd9Sstevel@tonic-gate }
4117*fa9e4066Sahrens 
4118*fa9e4066Sahrens /*
4119*fa9e4066Sahrens  * Returns true if the named dataset is visible in the current zone.
4120*fa9e4066Sahrens  * The 'write' parameter is set to 1 if the dataset is also writable.
4121*fa9e4066Sahrens  */
4122*fa9e4066Sahrens int
4123*fa9e4066Sahrens zone_dataset_visible(const char *dataset, int *write)
4124*fa9e4066Sahrens {
4125*fa9e4066Sahrens 	zone_dataset_t *zd;
4126*fa9e4066Sahrens 	size_t len;
4127*fa9e4066Sahrens 	zone_t *zone = curproc->p_zone;
4128*fa9e4066Sahrens 
4129*fa9e4066Sahrens 	if (dataset[0] == '\0')
4130*fa9e4066Sahrens 		return (0);
4131*fa9e4066Sahrens 
4132*fa9e4066Sahrens 	/*
4133*fa9e4066Sahrens 	 * Walk the list once, looking for datasets which match exactly, or
4134*fa9e4066Sahrens 	 * specify a dataset underneath an exported dataset.  If found, return
4135*fa9e4066Sahrens 	 * true and note that it is writable.
4136*fa9e4066Sahrens 	 */
4137*fa9e4066Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
4138*fa9e4066Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
4139*fa9e4066Sahrens 
4140*fa9e4066Sahrens 		len = strlen(zd->zd_dataset);
4141*fa9e4066Sahrens 		if (strlen(dataset) >= len &&
4142*fa9e4066Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
4143*fa9e4066Sahrens 		    (zd->zd_dataset[len-1] == '/' ||
4144*fa9e4066Sahrens 		    dataset[len] == '\0' || dataset[len] == '/')) {
4145*fa9e4066Sahrens 			if (write)
4146*fa9e4066Sahrens 				*write = 1;
4147*fa9e4066Sahrens 			return (1);
4148*fa9e4066Sahrens 		}
4149*fa9e4066Sahrens 	}
4150*fa9e4066Sahrens 
4151*fa9e4066Sahrens 	/*
4152*fa9e4066Sahrens 	 * Walk the list a second time, searching for datasets which are parents
4153*fa9e4066Sahrens 	 * of exported datasets.  These should be visible, but read-only.
4154*fa9e4066Sahrens 	 *
4155*fa9e4066Sahrens 	 * Note that we also have to support forms such as 'pool/dataset/', with
4156*fa9e4066Sahrens 	 * a trailing slash.
4157*fa9e4066Sahrens 	 */
4158*fa9e4066Sahrens 	for (zd = list_head(&zone->zone_datasets); zd != NULL;
4159*fa9e4066Sahrens 	    zd = list_next(&zone->zone_datasets, zd)) {
4160*fa9e4066Sahrens 
4161*fa9e4066Sahrens 		len = strlen(dataset);
4162*fa9e4066Sahrens 		if (dataset[len - 1] == '/')
4163*fa9e4066Sahrens 			len--;	/* Ignore trailing slash */
4164*fa9e4066Sahrens 		if (len < strlen(zd->zd_dataset) &&
4165*fa9e4066Sahrens 		    bcmp(dataset, zd->zd_dataset, len) == 0 &&
4166*fa9e4066Sahrens 		    zd->zd_dataset[len] == '/') {
4167*fa9e4066Sahrens 			if (write)
4168*fa9e4066Sahrens 				*write = 0;
4169*fa9e4066Sahrens 			return (1);
4170*fa9e4066Sahrens 		}
4171*fa9e4066Sahrens 	}
4172*fa9e4066Sahrens 
4173*fa9e4066Sahrens 	return (0);
4174*fa9e4066Sahrens }
4175